Function: comment-with-narrowing
comment-with-narrowing is a macro defined in newcomment.el.gz.
Signature
(comment-with-narrowing BEG END &rest BODY)
Documentation
Execute BODY with BEG..END narrowing.
Space is added (and then removed) at the beginning for the text's indentation to be kept as it was before narrowing.
Source Code
;; Defined in /usr/src/emacs/lisp/newcomment.el.gz
(defmacro comment-with-narrowing (beg end &rest body)
"Execute BODY with BEG..END narrowing.
Space is added (and then removed) at the beginning for the text's
indentation to be kept as it was before narrowing."
(declare (debug t) (indent 2))
(let ((bindent (make-symbol "bindent")))
`(let ((,bindent (save-excursion (goto-char ,beg) (current-column))))
(save-restriction
(narrow-to-region ,beg ,end)
(goto-char (point-min))
(insert (make-string ,bindent ? ))
(prog1
(progn ,@body)
;; remove the bindent
(save-excursion
(goto-char (point-min))
(when (looking-at " *")
(let ((n (min (- (match-end 0) (match-beginning 0)) ,bindent)))
(delete-char n)
(setq ,bindent (- ,bindent n))))
(end-of-line)
(let ((e (point)))
(beginning-of-line)
(while (and (> ,bindent 0) (re-search-forward " *" e t))
(let ((n (min ,bindent (- (match-end 0) (match-beginning 0) 1))))
(goto-char (match-beginning 0))
(delete-char n)
(setq ,bindent (- ,bindent n)))))))))))