this post was submitted on 28 Nov 2023
1 points (100.0% liked)

Emacs

305 readers
3 users here now

A community for the timeless and infinitely powerful editor. Want to see what Emacs is capable of?!

Get Emacs

Rules

  1. Posts should be emacs related
  2. Be kind please
  3. Yes, we already know: Google results for "emacs" and "vi" link to each other. We good.

Emacs Resources

Emacs Tutorials

Useful Emacs configuration files and distributions

Quick pain-saver tip

founded 1 year ago
MODERATORS
 

For example, let's say I were to change backward-delete-char-untabify-method for prog-mode buffers. Naively, I'd write something like,

(add-hook 'prog-mode-hook (lambda ()
                       (setq-local backward-delete-char-untabify-method 'hungry)))

but the documentation recommends against using lambdas in add-hook calls (which makes sense). I can, of course, just make a named function instead of a lambda and pass that to add-hooks. But, rather than do that, is there any other option for setting variables automatically for modes besides a hook like this?

Also, as a bonus question, if I wanted to also do something like enable show-paren-mode for all prog-mode buffers, which of the following do you consider "better" (assume that this is not something that is likely to change frequently):

;; option 1
(defun my-prog-mode-settings ()
  (setq-local backward-delete-char-untabify-method 'hungry))

(add-hook 'prog-mode-hook #'my-prog-mode-settings)
(add-hook 'prog-mode-hook #'show-paren-mode)

;; option 2
(defun my-prog-mode-settings ()
  (setq-local backward-delete-char-untabify-method 'hungry)
  (show-paren-mode))

(add-hook 'prog-mode-hook #'my-prog-mode-settings)

Obviously, I realize that it's not really important, but I feel like bike-shedding this morning.

top 6 comments
sorted by: hot top controversial new old
[–] [email protected] 1 points 9 months ago (1 children)

When it comes to personal configuration, I see no good reason not to use lambdas in this case. I also use them a lot when binding keys to simple commands which I don't want to name.

Also: option 1.

[–] [email protected] 1 points 9 months ago
  • They used to be hard to remove. As mentioned, M-x remove-hook has alleviated that to an extent.
  • They are still harder to update because you need to remember to remove the old one when adding the replacement. I've seen any number of cases where people were inadvertently adding lots of slightly different versions of their function to a hook variable, and wondering why they were still having problems.
  • They are very unhelpful when you inspect the hook variable. Rather than seeing a function name (from which you could jump to the nicely-formatted function definition), you see at best the lisp code all jammed into a single line, and at worst a heap of unreadable byte-code.

Use named functions. It's just better.

[–] [email protected] 1 points 9 months ago

Hej Hej,

Or you could just write the defun inside the add-hook

(add-hook 'org-mode-hook (defun silly-hook-defun () (message "it works)))

This should allow to remove later the hook if you want.

Cheers!

[–] [email protected] 1 points 9 months ago

You can also use this approach:

(add-hook 'text-mode-hook
          (defalias 'my/fancy-mode-setup
            (let (some-closure-var)
              (lambda () ...))))

Advantage is that there can be only one alias, and it's added by name to the hook. So you can alter and re-run add-hook without duplication, and you can easily remove it by name. Best of all, you can create a closure (the let there) if you need that gives you "local storage in the function" that persists between runs (without cluttering the namespace with global variables).

[–] [email protected] 1 points 9 months ago (1 children)

but the documentation recommends against using lambdas in add-hook calls (which makes sense).

Why? I use lambdas in add-hook calls all the time, because it seems un-Lisp-ish to create a function to be called only once.

[–] [email protected] 1 points 9 months ago

Can't modify a lamdba hook once it's been created, however if you named it via defun you can redeclare it with new behaviour or remove it from the advice list.