I have associated a file extension with org mode as follows:

(add-to-list 'auto-mode-alist '("\\.hy\\'" . org-mode))

I have also created certain hooks for org-mode and text mode as follows:

(add-hook 'org-mode-hook (lambda () 
               (flyspell-mode -1)
               (column-marker-3 -1)
               (smart-quotes-mode -1)))
(add-hook 'text-mode-hook (lambda () 
                (column-marker-3 70)
                (setq-default fill-column 70)))

I would like to add hooks now for the specific file type .hy which differ from the default hooks I have for org-mode.

I would like the following modes activated when I work with a .hy file: flyspell-mode, column-marker-mode, auto-fill-mode, line-numbers-mode. I would also like global-visual-line-mode turned off.

I am not sure if this is possible. Would appreciate some help.

2 Answers 2

(define-derived-mode hy-mode org-mode "Hy Mode"
  "Major mode for editing *.hy files."

(add-to-list 'auto-mode-alist '("\\.hy\\'" . hy-mode))

Note that hy-mode-hook is automatically defined as well, if you prefer (or need) to use a hook.

Your existing hooks will run as usual, as you now have:

text-mode -> outline-mode -> org-mode -> hy-mode

See the heading "Derived modes, and mode hooks" in https://stackoverflow.com/a/19295380/324105 if you wish to understand in more detail how derived modes and mode hooks fit together. The do-whatever-you-want-here above would be the child-body in that other answer.

  • 1
    Why are you calling text-mode in your mode body?? text-mode is a major mode -- and one which your hy-mode is already a derivative of. You should derive directly from text-mode if that's what you really want.
    – phils
    Commented Apr 10, 2022 at 12:35
  • 1
    setq-default fill-column is a bug. You just want setq there.
    – phils
    Commented Apr 10, 2022 at 12:36
  • 1
    And although nowadays you don't need to do this, I really recommend that when calling a minor mode directly in code, you pass it a positive numeric argument: (abbrev-mode 1) rather than (abbrev-mode), and likewise for the others. The reason is that this provides a distinction between calling major modes and minor modes which wouldn't otherwise exist, which makes the code clearer.
    – phils
    Commented Apr 10, 2022 at 12:40
  • 1
    Oh, you've messed up the syntax. Please refer to my example. Your mode should be hy-mode derived from org-mode, and not Hy derived from Mode which clearly won't do what you wanted. I've no idea why you changed that (in both the define-derived-mode and the add-to-list call), but it's wrong. Use the format I showed.
    – phils
    Commented Apr 10, 2022 at 12:45
  • 1
    It is buffer-local, but the setq is running in the buffer in question. If you use setq-default then you're affecting more than just hy-mode buffers.
    – phils
    Commented Apr 10, 2022 at 13:58

@phils gives you an elegant solution. A cruder one would be to add another function to org-mode-hook which tests (buffer-file-name) to see whether it ends in .hy:

(add-hook 'org-mode-hook
  (lambda ()
    (when (string-match-p "\\.hy\\'" (buffer-file-name))

In your context, I think phils' solution is better.

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.