It is called `save-restriction'. It is used with a narrowed-buffer. It
can be confusing, I have written the following that I use. I use the
prefix `si:' for convience when I use the minibuffer for expandsion -
stands for system interface...
(defun si:delete-blank-lines ()
"Delete all blank lines from buffer or active region."
(interactive)
(cond ((region-active-p)
(save-excursion
(goto-char (region-end))
(while (not (bobp))
(forward-line -1)
(while (looking-at-p "^$")
(delete-blank-lines)))))
(t
(save-excursion
(while (not (eobp))
(forward-line 1))
(while (not (bobp))
(forward-line -1)
(while (looking-at-p "^$")
(delete-blank-lines)))))))
This routine should run rather fast because most of the function calls
are functions written in C and already compiled to native code. Also by
starting at the end of the region and working backward you push the gap
to the end (I think) of the buffer/region, this should make looking up
regexps faster without a stall. Not that any of this matters these days.
You could do this...
(defun si:delete-lines-for-gnus ()
"Delete all blank lines from buffer or active starting with `>' ."
(interactive)
(cond ((region-active-p)
;; when the region is active only apply to region
(save-excursion
(goto-char (region-end))
(while (not (bobp))
(forward-line -1)
;; change the regexp
;; - the `$' means end of line so you do not need to look for
;; the '\n' or '\r' or '\r\n'...
;;
(while (looking-at-p "^>\s*$")
(delete-blank-lines)))))
(t
;; no region selected - apply to whole buffer
(save-excursion
(while (not (eobp))
(forward-line 1))
(while (not (bobp))
(forward-line -1)
(while (looking-at-p "^>\s*$")
(delete-blank-lines)))))))
Another example i just wrote for doing some database stuff. This one
adds a `interactive' method to the function. This means the function is
designed for user interface. The variables <SREG> and <EREG> are start
of region, and end of region. These buffer positions are passed to the
function automatically by using the interactive method with "r".
The emacs data structure of a buffer is amazingly powerful when used
with an interactive lisp interpreter. Unfortunately the language is old
and hencforth very large and somewhat archaic. On the other hand Lisp does not
change often :)
(defun db-text-kill-lines (sreg ereg)
;; interactive - tell emacs to pass the region as a parameter.
(interactive "r")
(let ((lst '(".jpg" ".png" ".jpeg" ".gif" ".txt" ".cue" ".log" ".m3u" ".md5" ".par.*" ".svf" ".ffp")))
(save-excursion
(dolist (l lst)
(goto-char sreg)
(while (re-search-forward (concat "^.*" (regexp-quote l) "$") ereg t)
(replace-match ""))))))
The real genius of emacs is the interactive minibuffer. The function
`read-from-minibuffer' is great. Although it is getting harder to learn.
(defun si:goto-position (&optional prefix)
"Goto line. With +prefix goto point. With -prefix goto column."
(interactive "P")
(cond ((not prefix)
(call-interactively 'goto-line))
((> (prefix-numeric-value prefix) 0)
(let ((current-prefix-arg nil))
(call-interactively 'goto-char)))
(t (let ((val (condition-case nil
(car (read-from-string
(read-from-minibuffer "Goto Column: ")))
(file-error nil))))
(if (numberp val)
(move-to-column val))))))
> Cheers,
A skip across the pond :)
Hope this helps some.