help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: commenting lines


From: Arnaldo Mandel
Subject: RE: commenting lines
Date: Tue, 2 Jul 2002 09:16:21 -0300

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))
    (while (> lines 0)
      (insert string)
      (search-forward "\n")
      (setq lines (- lines 1))
      )))

Enjoy,

am

-- 
Arnaldo Mandel                        
Departamento de Ciência da Computação - Computer Science Department
Universidade de São Paulo, Bra[sz]il      
am@ime.usp.br



reply via email to

[Prev in Thread] Current Thread [Next in Thread]