I would like to set some buffers to be opened in insert mode in evil.
For example in read-only buffers there is no point in having a normal mode since i then can't use q to exit but i have to do i q. This is annoying for interactive modes that produce error buffers.
What I have been trying and failed with is as follows:
(evil-set-initial-state view-mode 'insert)
You can modify the variable evil-insert-state-modes, which holds a list of modes that should be started in insert state:
(add-to-list 'evil-insert-state-modes 'view-mode)
There are analogous variables for emacs state, normal state, etc.
Related
Let's assume the particular mode I'm working with is python-mode.
The Emacs manual specifies following for hooks:
Every major mode command is supposed to run a normal hook called the mode hook as one of the last steps of initialization.
From Major mode conventions:
Each major mode should have a normal mode hook named modename-mode-hook. The very last thing the major mode command should do is to call run-mode-hooks.
And with-eval-after-load executes code after code is loaded (e.g. required), and runs immediately if already required.
I have following in my init file:
(add-hook 'python-mode-hook 'my-post-python)
Also I have added
(with-eval-after-load 'python-mode
(setq-default python-basic-offset 7) ; setting some option
(add-to-list 'python-globals-list "console"))
Now assuming I open Emacs, followed by opening a Python file buffer, what are the loading/execution orders with respect to hooks and with-eval-after-load? From the docs specified at start, it seems mode hooks will run before with-eval-after-load code?
More specifically, are mode hooks are run every time a buffer is entered/ made the current buffer? (P.S. this is hard to find from the docs/manual, any links clarifying any of the above in the docs/manual are welcome).
Now assuming I open emacs, followed by opening a python file buffer, what are the loading/execution order with respect to hooks and with-eval-after-load?
Assuming python.el has not already been loaded, then:
You visit foo.py.
set-auto-mode is called and determines that python-mode is appropriate, and calls that.
The python-mode function is (at this point) an autoload definition for the python-mode library1, which is consequently loaded.
At the end of loading, your with-eval-after-load for the python-mode library is evaluated.
The real python-mode function (newly defined by loading the library) is called, at the end of which:
python-mode-hook runs.
Are mode hooks are run every time a buffer is entered/ made the current buffer?
No, they run every time the mode function is called.
1 The default library is python.el which uses (provide 'python), but from your with-eval-after-load you're evidentially using the python-mode.el library instead.
evil-mode has evil-emacs-state-modes var, which defines modes to open in emacs mode.
I have magit-diff-mode listed in that var (in fact, it is a default).
Running magit-diff opens correctly in emacs mode.
However, if I a run magit-status (opens in emacs mode), place the point on Head (first line in the buffer) and hit Ret, magit-visit-thing is called and and the commit info is shown in a new buffer. This buffer is in magit-diff-mode, however, evil-mode is active for this buffer.
How do I prevent this behaviour?
As we figured out in the comments - the buffer opens in magit-revision-mode, not in magit-diff-mode.
To find out the major mode of the buffer, describe variable major-mode - C-h v major-mode <RET>.
For you information, when you do describe-mode instead (C-h m) what you see as mode name is the string that appears in the status line. It may be difficult to guess from it what the actual mode name is - like Magit Rev is actually a string for magit-revision-mode - no way of knowing unless you look in magit-diff.el:
define-derived-mode magit-revision-mode magit-diff-mode "Magit Rev"
In Emacs how do I stop the auto-fill minor mode from loading when I start the idlwave major mode?
So far I have been completely unsuccessful at figuring out how to do this. I have tried to use remove-hook for both idl-mode-hook and text-mode-hook without success.
You might have enabled auto-fill-mode as a global minor mode, so it is on in all buffers by default. If that is the case, the task is not so much not turning it on in idlwave-mode but rather turning it off.
Most major modes provide a special hook variable: it's a list containing functions that are called whenever that major mode is invoked. For instance, with the following line you can make sure that auto-fill-mode will get turned off each time a buffer goes into idlwave-mode:
(add-hook 'idlwave-mode-hook (lambda () (auto-fill-mode 0)))
Put the above line in your init file (e.g. ~/.emacs or ~/.emacs.d/init.el) and auto-fill-mode should be turned off in idlwave mode after you restarted Emacs.
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?
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)
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...
I have an asynchronous process in Emacs, which creates a TAGS file.
This process creates a process buffer called *ctags*. If the process result is "finished\n", I kill the buffer.
If the process result is anything else I want to display the process buffer similar to the *compilation* status output when running M-x compile.
I.e. I want to vertically split the screen and show the *ctags* buffer at the bottom. Pressing q would preferably kill the bottom buffer and just show my original buffer.
I tried using this in my process sentinel callback:
(split-window-vertically)
(set-window-buffer (selected-window) (get-buffer "*ctags*"))
but aside from the fact that it puts the *ctags* buffer on top, the buffer does not have the same characteristics as the *compilation* output, e.g. pressing q inserts q.
How do I create a buffer like *compilation*?
EDIT:
Inspired by Trey Jackson's answer below, this does exactly what I want:
(pop-to-buffer (get-buffer "*ctags*"))
(compilation-mode)
It selects the *ctags* buffer, puts it into compilation mode and q will quit the window.
EDIT2:
Using (compilation-mode) (major mode instead of minor mode) since Emacs somehow doesn't like reapplying the minor mode to an exisiting buffer.
The Error message I get is:
Toggling compilation-minor-mode off; better pass explicit argument.
To get the behavior of the *compilation* buffer, add this to your script:
(compilation-mode)
It's better to derive your own mode from compilation-mode, and define error regex, etc.