In Emacs, how to automatically enable a minor mode based on buffer name?
Asked Answered
D

3

14

I have a Emacs extension that creates a buffer named *erl-output*. This buffer gets created with only fundamental-mode by default. Is there any way to automatically enable compilation-minor-mode on that buffer?

Develop answered 19/8, 2009 at 11:5 Comment(3)
I looked at compile.el but didn't find anything useful there.Develop
Why do you want compilation-minor-mode? Why not the major mode?Anderegg
It's just that my extension says something along the lines of "Enable compilation-minor-mode to be able to click the links". I'll take a look at just using compilation-modeDevelop
G
21

To automatically change major modes you can add the following to your .emacs file:

(add-to-list 'auto-mode-alist '("^\\*erl-output\\*$" . my-major-mode))

This won't work for you; it's for major mode selection and you're after minor mode selection.

Instead you could try a Hook. The manual says:

A hook is a Lisp variable which holds a list of functions, to be called on some well-defined occasion.

So you should be able to write a function which sets the minor mode when required. Looking at the List of Standard Hooks I think you should be trying temp-buffer-setup-hook or temp-buffer-show-hook.

You'll have to write a function which checks the buffer name and sets the mode if required, and add it to the hook using something like the following in your .emacs:

(add-hook 'temp-buffer-setup-hook 'my-func-to-set-mode)
Guadiana answered 19/8, 2009 at 11:15 Comment(5)
Yes, that is exactly the problem I have. :-)Develop
@Adam - I've updated my answer to cover Hooks as well as auto-mode-alist; hopefully they'll work for you.Guadiana
I tried enabling the major mode with no success. In .emacs I put: (add-to-list 'auto-mode-alist '("^\\*erl-output\\*$" . compilation-mode)) It is not created with compilation-mode. If I manually create a buffer with `(get-buffer-create "erl-output") the mode still doesn't get setDevelop
Did get it to work finally, but I have to maintain patches against the original package. I've notified the authors as to include them. Thanks for your help!Develop
Name patterns in auto-mode-alist are applied to file names, not buffer names. So even if a major mode were required here, the proposed solution would not help.Spiritism
A
4

Since your extension is creating the buffer, why not just add:

(compilation-mode)

(or (compilation-minor-mode) if you're really set on the minor mode idea) in the code that's creating the *erl-output* buffer. You can edit the source for the mode, or use advice around the creation routine...

Anderegg answered 19/8, 2009 at 15:4 Comment(1)
compilation-mode seems to make the buffer read only. The extension writes to the buffer occasionally.Develop
M
0

A related case, where the buffer is created from a file. For example, a build system that creates a file such as results.compile.log, you can add a new major mode,

(define-derived-mode compile-log-mode fundamental-mode
  "CompilateLog"
  (compilation-minor-mode))

then update `auto-mode-alist' with something like

(add-to-list 'auto-mode-alist '("\\.compile\\.log\\'" . compile-log-mode))
Maineetloire answered 31/7 at 16:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.