emacs-devel
[Top][All Lists]
Advanced

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

prefix-region


From: Hrvoje Niksic
Subject: prefix-region
Date: Mon, 24 Oct 2005 22:28:31 +0200
User-agent: Gnus/5.1006 (Gnus v5.10.6) Emacs/21.4 (gnu/linux)

I have found this function, originally from XEmacs, immensely useful:

    (prefix-region PREFIX)

    Insert PREFIX at the beginning of each line between mark and point.

It is useful for all kinds of editing jobs, including commenting out
pieces of code and quoting mail.  While some of these things can be
achieved using other Emacs functions, `M-x prefix-region' is often
much more convenient, especially when the more sophisticated
mechanisms are unavailable (such as when editing snippets of code in
mail messages).

This function is not as easy to write as it seems because there are
several corner cases that need to be gotten right (empty region,
whether mark precedes point or vice versa, mark/point at line start or
end positions).  Because of that I propose that you include the
function in Emacs.

I wasn't satisfied with the XEmacs implementation of this function so
I wrote and tested this one:

(defun prefix-region (prefix)
  "Insert PREFIX at the beginning of each line between mark and point."
  (interactive "sPrefix string: ")
  (let ((end (region-end)))
    (save-excursion
      (goto-char (region-beginning))
      (beginning-of-line)
      (save-restriction
        (narrow-to-region (point) end)
        (while (not (eobp))
          (insert prefix)
          (forward-line 1))))))




reply via email to

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