3

Here is the definition of fundamental-mode:

(defun fundamental-mode ()
  "Major mode not specialized for anything in particular.
Other major modes are defined by comparison with this one."
  (interactive)
  (kill-all-local-variables)
  (run-mode-hooks))

There does not seem to be a fundamental-mode-hook defined.

I would like to call some code for all "fundamental-mode" buffers and all derived modes from it, without having to itemize all such modes and explicitly call add-hook on all of their mode functions.

Is there a way or am I stuck paying the itemization cost? If yes, then how do I do this inside all modes:

(modify-syntax-entry ?_ "w")

because after decades of Emacs usage, I've finally grown tired of my cursor getting hung up on every single underscore for every time I use M-f (ALT+f), across all new modes that are invented and that I install into my setup.

2
  • Consider wrapping your cursor movement function(s) with a temporary / custom syntax table (or modification thereof) that will be in effect for only the duration of the cursor movement, and forget about hooks and modifications of fundamental-mode and the like. E.g., with-syntax-table gnu.org/software/emacs/manual/html_node/elisp/…
    – lawlist
    May 25, 2021 at 17:49
  • Not posting as an asnwer because the question is more general, but global-superword-mode can be a solution of the issue that you describe at the end
    – matteol
    May 26, 2021 at 6:23

1 Answer 1

4

1st: Dealing with your actual problem of setting the syntax for ?_ in modes derived from fundamental-mode.

There are no commands setting the syntax table in fundamental-mode. Therefore, it uses the (standard-syntax-table). If the syntax for ?_ is not defined in a mode it uses the entry in its inherited syntax table. By default, (standard-syntax-table) is inherited. Since there are no circular references for syntax tables (standard-syntax-table) is eventually the fallback for all modes.

If we assume that modes setting the syntax for ?_ do it for a good reason it should be sufficient to use the following line in your init files:

(modify-syntax-entry ?_ " " (standard-syntax-table))

(This works at least in Emacs 27.1.90.)

2nd: The user is not supposed to customize fundamental-mode. So there is no hook for that mode.
Furthermore, fundamental-mode does not really play a role in the inheritance hierarchy. For all modes but fundamental-mode itself (derived-mode-p 'fundamental-mode) evaluates to nil.

Nevertheless, there is after-change-major-mode-hook that is run at the very end of major mode functions. That allows you to do general settings for all modes.

1
  • +500: For my goal, treating underscores as a part of words globally was facilitated by (modify-syntax-entry ?_ "w" (standard-syntax-table)). Thanks Tobias!
    – bgoodr
    May 27, 2021 at 18:35

Your Answer

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

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