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.

  • astoff1@alien.topB
    link
    fedilink
    English
    arrow-up
    1
    ·
    11 months ago

    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.

    • 7890yuiop@alien.topB
      link
      fedilink
      English
      arrow-up
      1
      ·
      11 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.

  • 0lMon@alien.topB
    link
    fedilink
    English
    arrow-up
    1
    ·
    11 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!

  • JDRiverRun@alien.topB
    link
    fedilink
    English
    arrow-up
    1
    ·
    11 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).

  • arensb@alien.topB
    link
    fedilink
    English
    arrow-up
    1
    ·
    11 months ago

    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.

    • accoil@alien.topB
      link
      fedilink
      English
      arrow-up
      1
      ·
      11 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.