RE: commenting lines
Arnaldo Mandel <am <at> ime.usp.br>
2002-07-02 12:16:21 GMT
Gareth Walker wrote (on Jul 2, 2002):
> Hi all -- I know it is possible to comment out text in Emacs with
> `comment-region', but is it possible to specify what goes at the
> beginning of each line? I would like to `label' regions of my text
> files with different things.
This oldie is quite reliable, useful enough to deserve a key-binding.
;; Christopher North-Keys, 1989
(defun prefix-region (start end string)
"Insert STRING, default '> ', at the start of each line
in or intersecting region while preserving indentation.
Called from a program, takes three arguments, START, END and STRING."
(interactive "r\nsString: ")
(if (or (equal string "") (equal string nil))
(setq string "> "))
;; Adjust start and end to extremes of
;; lines so lines don't get broken.
(goto-char end)
(end-of-line)
(setq end (point))
(goto-char start)
(beginning-of-line)
(setq start (point))
;; There is another command, replace-regexp, that did not work well.
;; If you narrowed as one would expect, you could not widen to the
;; previous narrow. Saving the old narrow extremes failed, as this
;; routine expands the region. Sadmaking.
(let (line)
(setq lines (count-lines start end))
(Continue reading)