automatization: hooks, advices, or new functions

Sat, 26 Mar 2016 20:10:49 +0100

> Would those hooks ... still be the way to go?

You'll have to check the documentation for each one.

In general, if you want something automatized at
command or event E, first see if there are any related
hooks, "after-E-hook", "before-E-hook", and the like.

If there are, put your stuff there, e.g.,

    ;; (setq text-mode-hook nil)
    (defun text-mode-hook-f ()
      (auto-fill-mode)
      (abbrev-mode) )
    (add-hook 'text-mode-hook #'text-mode-hook-f)

If there aren't any hooks, you can either use
so-called advices (which is "function composition" in
CS lingo). Place your stuff before or after E, which
you must isolate to one particular function. It can
help to put the new stuff in a function as well, e.g.,

    (defun gnus-summary-insert-all (&rest unused)
      (gnus-summary-insert-old-articles t) )
    (advice-add       'gnus-summary-limit-to-subject :before #'gnus-summary-insert-all)
    (advice-add       'gnus-summary-limit-to-author  :before #'gnus-summary-insert-all)
    ;; (advice-remove 'gnus-summary-limit-to-subject         #'gnus-summary-insert-all)
    ;; (advice-remove 'gnus-summary-limit-to-author          #'gnus-summary-insert-all)

Either that, or, which I think is simpler and less
error-prone, you can do your own function:

    (defun do-E-with-my-modification ()
      (interactive)
      (do-my-stuff)
      (E) ) ; or use `call-interactively' here for complete transparence

Then bind "do-E-with-my-modification" to a shortcut
and start using that instead. If you didn't use the
old shortcut to call E hundreds of times already, you
can assign a new shortcut to your modified version.
If old habits die hard, it is not a deadly sin to
overwrite the old one :)

Back to Blogomatic.