blog/quick-normal-state.post

48 lines
1.7 KiB
Text
Raw Permalink Normal View History

2015-01-02 21:04:36 +01:00
;;;;;
title: Quick normal-state
tags: emacs, evil
date: 2014-07-11 00:47
format: md
;;;;;
I realized today that most of the time (over 90%) of the time I save a
file I feel I have finished typing something. Typing this out makes it
sound very obvious, actually. One other thing I noticed is that I
still forget to return to normal-state after I'm done editing. Emacs
being hackable and not being content with twisting my brain to suit my
editor but rather twisting my editor to suit me, I thought I might
automate it a little.
My first idea was to have evil automatically revert to normal-state
whenever I save and also whenever I switch buffers. I haven't found a
hook that runs when switching buffers, so I'll need to brush up on my
advising skills and have it run just before switching, perhaps even
when `execute-extended-command` or `smex` run, so any explicit minibuffer
action returns to normal state.
For now it's just the save file, and only for buffers that aren't in
the emacs-state as default list:
``` emacs-lisp
(defun modes-starting-in (state)
"Get a list of modes whose default state is STATE."
(symbol-value (evil-state-property state :modes)))
(defun maybe-switch-to-normal-state ()
"Switch the current buffer to normal state.
Only do this when the mode is not in emacs state by
default."
(unless (memql major-mode (modes-starting-in 'emacs))
(evil-normal-state)))
(with-eval-after-load 'evil
(add-hook 'after-save-hook
#'maybe-switch-to-normal-state))
```
I personally only use either normal-state or emacs-state as default
states when a mode loads, if you want to check more you'll have to add
some more calls to `memq` and change `emacs` to, for example `insert` or
`visual` or whichever you need.