The aesthetics of patching


In describing my move to Helm, I quickly mentioned another package from Thierry Volpiatto: psession. psession is a light session saving mechanism and it comes with built-in integration for Helm among other things, which of course renders it a valuable improvement to my setup. The installation instructions are pretty clear, so there is no need to bother you with obvious code snippets.

Instead, I want to bother you with my obsessive-compulsive disorder. You see, I am fond of how my beloved text editor looks, especially after boot. I disabled every message, toolbar, scrollbar, menu, and buffer Emacs can throw at me on startup, and the theme is applied as late as possible to ensure nothing obstructs my way to another great day of text editing.

What’s “wrong” with psession then? In order to make the previous sessions available, psession loads some compiled files when Emacs starts. This is what I expect it to do, because there would be no point in saving sessions without restoring them. My only “problem” with this behaviour is the file loading itself.

(cl-defun psession--restore-objects-from-directory
    (&optional (dir psession-elisp-objects-default-directory))
  (let ((file-list (directory-files dir t directory-files-no-dot-files-regexp)))
    (cl-loop for file in file-list do (and file (load file)))))

The code is not hard to understand. What constitutes a rude attack at my delicate aesthetic feelings is that (load file) right before the good-looking closing parentheses. Calling load in this manner causes a bunch of messages to “interfere” with a clean Emacs boot: they show up in the echo area during the loading of my init.el and they hurt so bad!1

Let’s examine the documentation of the function load:

load is a function defined in lread.c.

Signature

(load FILE &optional NOERROR NOMESSAGE NOSUFFIX MUST-SUFFIX)

Documentation

Execute a file of Lisp code named FILE.

[…]

Print messages at start and end of loading unless optional third arg NOMESSAGE is non-nil (but force-load-messages overrides that).

[…]

Perfect. Now I just have to hack psession--restore-objects-from-directory and make sure load is called with a non-nil value for NOMESSAGE. Usually when I want to silence a command I rely on inhibit-message. Let’s say I want to silence the activation of helm-adaptive-mode:

(let ((inhibit-message t))
  (helm-adaptive-mode +1))

However, psession--restore-objects-from-directory is sort of a private function, so I need something else to change it according to my plans. There are probably several good ways to patch psession--restore-objects-from-directory, but my go-to package in these situations is Radon Rosborough’s el-patch.

(el-patch-feature psession)
(with-eval-after-load 'psession
  (el-patch-cl-defun psession--restore-objects-from-directory
    (&optional (dir psession-elisp-objects-default-directory))
    (let ((file-list (directory-files dir t directory-files-no-dot-files-regexp)))
      (cl-loop for file in file-list do (and file (load file nil 'nomessage))))))

Only six lines of code to please my sense of aesthetic. Beauty is always within reach when we are dealing with Emacs.

Note that at the moment of this writing, el-patch-cl-defun is on the develop branch of el-patch. See here to know more about this and to laugh at my poor reading skills.

Footnotes

  1. I know this is silly, but I hope the double quotation marks and the light tone make the irony clear enough. The point is having fun at hacking Emacs, not insulting Thierry.