5

Emacs and I agree on many things. That's good. However, there is one thing we disagree on – the default value for fill-column for different major modes. In order to alleviate this, I added the following to my init file:

(let ((fill-column-mode-alist '((org-mode . 80)
                                (markdown-mode . 80)
                                (tex-mode . 80)
                                (python-mode . 79))))
  (defun modify-fill-column-maybe ()
    "Call `set-fill-column' with a value according to the major mode."
    (let ((value (assoc major-mode
                        fill-column-mode-alist
                        #'provided-mode-derived-p)))
      (when value
        (set-fill-column (cdr value))))))

(add-hook 'after-change-major-mode-hook #'modify-fill-column-maybe)

My question is whether there is something fundamentally wrong with my approach. Any pitfalls that I am overlooking? Is there maybe a built-in functionality for this that I am not aware of?

1 Answer 1

8

I don't think there's anything wrong with what you're doing. If it works I don't see any obvious pitfalls.

That said, it's not the usual way to apply mode-specific settings. This is usually done with a mode-hook. For example, I have the following in my init:

(defun my-org-src-mode-hook ()
  (setq fill-column 65)
  (auto-fill-mode t))

(add-hook 'org-src-mode-hook #'my-org-src-mode-hook)

If the only thing you're tweaking for each mode is the fill-column value, your way is probably more convenient. But if you've got other mode-specific config, it may be more convenient to organize it by mode (i.e., with mode hooks), rather than by feature.

6
  • Thank you. For some reason I have opted to organize things, as you say, by feature. For example, In my use-package declaration for flyspell, I have :hook ((org-mode markdown-mode TeX-mode) . flyspell-mode). Maybe I should consider doing things the other way around!
    – d125q
    Oct 24, 2018 at 15:18
  • Ha! I didn't know use-package had a :hook argument, that's handy.
    – Tyler
    Oct 24, 2018 at 17:56
  • 1
    @Tyler: please add a # before 'my-org-src-mode-hook so the compiler knows it is a function. Oct 25, 2018 at 15:08
  • 1
    @DamienCassou ok. I understand why, but the official docs don't cover this, and the #' syntax differs from the examples of add-hook in the manuals. It would be helpful if the official Emacs manual explicitly covered this. The only mention of #' is in relation to anonymous functions.
    – Tyler
    Oct 25, 2018 at 16:24
  • 1
    @alper you should be defining my-latex-mode-hook, and then adding it to LaTeX-mode-hook via (add-hook 'LaTeX-mode-hook #'my-latex-mode-hook)
    – Tyler
    Jan 6, 2022 at 15:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.