Download
(defcustom ignore-comments-flag t
"Non-nil means macro `with-comments-hidden' hides comments."
:type 'boolean :group 'matching)
(defmacro with-comments-hidden (start end &rest body)
"Evaluate the forms in BODY while comments are hidden from START to END.
But if `ignore-comments-flag' is nil, just evaluate BODY,
without hiding comments. Show comments again when BODY is finished.
See `hide/show-comments', which is used to hide and show the comments.
Note that prior to Emacs 21, this never hides comments."
(let ((result (make-symbol "result"))
(ostart (make-symbol "ostart"))
(oend (make-symbol "oend")))
`(let ((,ostart ,start)
(,oend ,end)
,result)
(unwind-protect
(setq ,result (progn (when ignore-comments-flag
(hide/show-comments 'hide ,ostart ,oend))
,@body))
(when ignore-comments-flag (hide/show-comments 'show ,ostart ,oend))
,result))))
(defun hide/show-comments (&optional hide/show start end)
"Hide or show comments from START to END.
Interactively, hide comments, or show them if you use a prefix arg.
Interactively, START and END default to the region limits, if active.
Otherwise, including non-interactively, they default to `point-min'
and `point-max'.
Uses `save-excursion', restoring point.
Be aware that using this command to show invisible text shows *all*
such text, regardless of how it was hidden. IOW, it does not just
show invisible text that you previously hid using this command.
From Lisp, a HIDE/SHOW value of `hide' hides comments. Other values
show them.
This command does nothing in Emacs versions prior to Emacs 21, because
it needs `comment-search-forward'."
(interactive
(cons (if current-prefix-arg 'show 'hide)
(if (or (not mark-active) (null (mark)) (= (point) (mark)))
(list (point-min) (point-max))
(if (< (point) (mark)) (list (point) (mark)) (list (mark) (point))))))
(when (require 'newcomment nil t)
(comment-normalize-vars)
(unless start (setq start (point-min)))
(unless end (setq end (point-max)))
(unless (<= start end) (setq start (prog1 end (setq end start))))
(let ((bufmodp (buffer-modified-p))
(buffer-read-only nil)
cbeg cend)
(unwind-protect
(save-excursion
(goto-char start)
(while (and (< start end) (setq cbeg (comment-search-forward end 'NOERROR)))
(setq cend (if (string= "" comment-end)
(min (1+ (line-end-position)) (point-max))
(search-forward comment-end end 'NOERROR)))
(when (and cbeg cend)
(if (eq 'hide hide/show)
(put-text-property cbeg cend 'invisible t)
(put-text-property cbeg cend 'invisible nil)))))
(set-buffer-modified-p bufmodp)))))
(defun hide/show-comments-toggle (&optional start end)
"Toggle hiding/showing of comments in the active region or whole buffer.
If the region is active then toggle in the region. Otherwise, in the
whole buffer.
This command does nothing in Emacs versions prior to Emacs 21, because
it needs `comment-search-forward'.
Interactively, START and END default to the region limits, if active.
Otherwise, including non-interactively, they default to `point-min'
and `point-max'.
See `hide/show-comments' for more information."
(interactive (if (or (not mark-active) (null (mark)) (= (point) (mark)))
(list (point-min) (point-max))
(if (< (point) (mark)) (list (point) (mark)) (list (mark) (point)))))
(when (require 'newcomment nil t)
(comment-normalize-vars)
(if (save-excursion (goto-char start) (and (comment-search-forward end 'NOERROR)
(get-text-property (point) 'invisible)))
(hide/show-comments 'show start end)
(hide/show-comments 'hide start end))))
(provide 'hide-comnt)