Point Marker

As a final enhancement to limited-save-excursion, rather than recording point as a number, we should record it as a marker, for the same reason we used markers in the definition of unscroll (see the Markers section in Chapter 3): namely, that executing the subexprs may render the saved buffer position inaccurate, because text may be inserted or deleted earlier in the buffer.

This is trivial to change. All that's necessary is to replace the call to point, which returns a number, with a call to point-marker, which returns point's current position as a marker.

(defmacro limited-save-excursion (&rest subexprs)
  "Like save-excursion, but only restores point."
  (let ((orig-point-symbol (make-symbol "orig-point")))
    `(let ((,orig-point-symbol (point-marker)))
       (unwind-protect
           (progn ,@subexprs)
         (goto-char ,orig-point-symbol)))))

Now all that remains is to put this definition, followed by

(provide 'limited)

into a file named limited.el in a directory on your load-path and byte-compile the file (see Chapter 5). Then in refill.el we can replace the calls to save-excursion with calls to limited-save-excursion; add:

(require 'limited)

to the beginning of refill.el; and byte-compile it. Now limited won't be loaded until refill is loaded, and if you also put

(autoload 'refill-mode "refill" "Refill minor mode." t)

in your .emacs, then refill won't be loaded until you invoke refill-mode.

Get Writing GNU Emacs Extensions now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.