11

By design, the fundamental-mode is bare bones as other major modes are supposed to derive from this one.

From the info page (elisp) Major Modes, we have:

This is the major mode command for Fundamental mode. Unlike other mode commands, it does not run any mode hooks (*note Major Mode Conventions::), since you are not supposed to customize this mode.

For that reason,

  • We do not have a fundamental-mode-hook.
  • Also my-minor-mode does not get enabled ONLY in this major mode buffers inspite of having (global-my-minor-mode 1) in my config.

Below would work but only if I manually typed M-x fundamental-mode:

(add-hook 'after-change-major-mode-hook #'my-minor-mode)

It does not help in the below case where a fundamental-mode buffer (*abcd*) is created by default:

(with-current-buffer (get-buffer-create "*abcd*")
  (insert "Hello world"))

Why I want to enable a minor mode in fundamental-mode?

I like to keep my custom key bindings in my minor mode map. I have this minor mode enabled globally (but it does not get enabled only in fundamental-mode buffers).

Here's a use case that made this ask this question here. On doing M-x esup (esup package), it creates an *esup-log* buffer with fundamental-mode as its major mode. But each time I need to enable my minor mode manually in it to use my convenient bindings.

I'd like to enable my-minor-mode in a truly global fashion, fundamental-mode or not.

Update 1:

Here is my minor mode definition: modi-mode.

Update 2:

Based on @npostavs' comment, I reviewed what's different between my minor mode modi-mode and override-global-mode in use-package/bind-key.el. And it turns out that the init value of my mode is nil and that of override-global-mode is t.

If I change the init value of my mode to t, it too gets enabled in the fundamental-mode buffers! But the act of (global-modi-mode 1) does not end with the same result. Is that odd? Probably a bug?

12
  • A similar custom key binding mode does get enabled in fundamental-mode buffers for me, so perhaps the problem is in your mode's definition. override-global-mode
    – npostavs
    Commented Sep 17, 2015 at 14:03
  • @npostavs Thanks. I will review how use-package implements that. Commented Sep 17, 2015 at 14:08
  • @npostavs Please check the updated question.. turns out I am facing this problem simply because the init value of my mode is nil; but I do enable it globally. Commented Sep 17, 2015 at 14:56
  • 1
    In buffer.c, it looks like get-buffer-create runs the buffer-list-update-hook at the very end of its function, perhaps you could attach your own function with conditions to that hook?
    – lawlist
    Commented Sep 17, 2015 at 15:47
  • 1
    Emacs uses "internal" temporary buffers for all kinds of purposes (e.g. doing coding-system conversion for I/O to/from a file or process may use a tempoary buffer). These buffers use fundamental-mode and I think it's right that they don't enable your minor-mode. As for other buffers, the rule for me is that: if you ever want to see this buffer (other than for debugging purposes) then it should probably not use fundamental-mode.
    – Stefan
    Commented Sep 20, 2015 at 1:42

3 Answers 3

5

The only way I can get a minor mode enabled by default in fundamental-mode buffers is to set the init value of that minor mode to t.

(define-minor-mode my-minor-mode
 "My minor mode"
 :init-value t)

Note that simply toggling ON a globalized minor mode (see C-h i g (elisp) Defining Minor Modes) with init value nil does not do the same thing. In this case, a fundamental-mode buffer will be created with this minor mode disabled.


The above, of course, will work only if you control the minor mode source. For all other minor modes that come with different packages, if you'd like to enable a minor mode by default in fundamental-mode buffers, you would need to do

(setq-default the-minor-mode t)
4
  • 1
    I think for other minor modes you could do (setq-default 'the-minor-mode t) to get the same effect.
    – npostavs
    Commented Sep 17, 2015 at 15:38
  • Good point! I will update the answer (removed that single quote). Thanks! Commented Sep 17, 2015 at 15:45
  • If you added (setq-default modi-mode 1) to the function turn-on-modi-mode and (setq-default modi-mode nil) to the function turn-off-modi-mode`, then you would be one step closer to actually globally enabling and disabling the mode.
    – nispio
    Commented Apr 16, 2016 at 23:09
  • @nispio That's a good point. I haven't yet needed to do this. But now I know what I need to do if I need to toggle my minor mode in a fundamental-mode buffer. Commented Apr 17, 2016 at 0:25
3

Do not use fundamental-mode -- at least not interactively. You rarely want to be in fundamental-mode. There is almost always something better.

The whole point of fundamental-mode is to not have the usual major-mode handling (hooks etc.). You can think of fundamental-mode as kind of like an abstract class.

If some package puts you in a buffer that is in fundamental-mode then do what you need to do to prevent this -- use some other mode for it.

Consider customizing major-mode so that it defaults to something other than fundamental-mode. (I use indented-text-mode.)


Formerly there was default-major-mode for this, which I think was a bit clearer, but they decided to deprecate that (it still works, of course).

default-major-mode is a variable defined in `C source code`.
Its value is indented-text-mode

  This variable is obsolete since 23.2;
  use `major-mode' instead.

Documentation:
Value of `major-mode` for new buffers.
10
  • 2
    I am not using the fundamental-mode. The crux of the matter is that when any package uses get-buffer-create to generate a temporary buffer, that buffer gets created with fundamental-mode as the major mode. In those buffers, my minor mode won't be enabled by default (because its init value is nil; even though I enabled it globally). Commented Sep 17, 2015 at 15:43
  • Few such packages I can list: esup, keyfreq Commented Sep 17, 2015 at 15:45
  • A package that just calls get-buffer-create does not do anything to establish the major mode (or any minor mode). Should it (for the package in question)? If it should, consider contacting the package maintainer. If not, consider turning on your preferred major or minor mode yourself. IOW, if it's about what you want, and not what the package should set up generally, then it's up to you to set it up the way you want.
    – Drew
    Commented Sep 17, 2015 at 22:43
  • I'd be curious to know why this was downvoted.
    – Drew
    Commented Apr 13, 2016 at 22:57
  • 1
    @Drew because it does not address the issue. The OP explicitly states that other modes are creating buffers in fundamental mode, and your proposed solution is to not use fundamental mode.
    – nispio
    Commented Apr 15, 2016 at 17:51
0

The function get-buffer-create is defined in buffer.c, and at the very end of that function it calls the buffer-list-update-hook. Such a hook could be used to run a custom function, but certain conditions may need to be added to prevent the custom function from firing at inopportune moments whenever that hook is called by other functions.

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.