hideshow.elとhideshowvis.elで、折りたたみ時にjavadoc形式のコメントのヘッドラインを表示させる

  • TODO
    • javadoc以外のコメントのヘッドライン表示
    • if () {とは違い{が次の行に来ていた場合のhs-hide-toggleの挙動
(defun get-javadoc-comment-headline (start end)
  "Return Javadoc comment headline."
  (save-excursion
    (save-restriction
      (narrow-to-region start end)
      (goto-char (point-min))
      (if (re-search-forward "[ \t]\\*[ \t]?" nil t)
          (let ((beg (point)))
            (end-of-line)
            (buffer-substring beg (point)))
        nil))))

(defun display-code-line-counts-and-comment-headline (ov)
  (let* ((marker-string "*fringe-dummy*")
         (marker-length (length marker-string))
         (line-count (count-lines (overlay-start ov) (overlay-end ov)))
         (headline (get-javadoc-comment-headline (overlay-start ov) (overlay-end ov)))
         (display-string (format "...<%d>" line-count)))
    (when headline
      (setq display-string (concat (format "...[%s]" headline) display-string)))
    (overlay-put ov 'help-echo "Hiddent text. C-c,= to show")
    (put-text-property 0 marker-length 'display (list 'left-fringe 'my-hidesowvis-showable-marker 'hs-fringe-face) marker-string)
    (overlay-put ov 'before-string marker-string)
    (put-text-property 0 (length display-string) 'face 'hs-face display-string)
    (overlay-put ov 'display display-string)))

(setq hs-set-up-overlay 'display-code-line-counts-and-comment-headline)