Mode Meat
With the basic structure in place, let's start defining the guts of Refill mode.
We've already identified the basic feature of refill-mode
: each insertion and deletion must ensure that the current paragraph is correctly filled. The correct way to execute code when the buffer is changed, as you may recall from Chapter 4, is by adding a function to the hook variable after-change-functions
when refill-mode
is turned on (and removing it when it is turned off). We'll add a function called refill
(which does not yet exist) that will do all the work of making sure the current paragraph remains correctly filled.
(defun refill-mode (&optional arg) "Refill minor mode." (interactive "P") (setq refill-mode (if (null arg) (not refill-mode) (> (prefix-numeric-value arg) 0))) (make-local-hook 'after-change-functions) (if refill-mode (add-hook 'after-change-functions 'refill nil t) (remove-hook 'after-change-functions 'refill t)))
The extra arguments to add-hook
and remove-hook
ensure that only the buffer-local copies of after-change-functions
are altered. Whether refill-mode
is being turned on or off when this function is called, we call make-local-hook
on after-change-functions
to make it buffer-local. This is because in both cases—turning refill-mode
on or turning it off—we need to manipulate after-change-functions
separately in each buffer. Unconditionally calling make-local-hook
first is the simplest way to do this, especially since make-local-hook
has no effect if the named hook variable ...
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.