summaryrefslogtreecommitdiffstats
path: root/quick-normal-state.post
blob: 6dfe67a776f6315f53ddd23fe0c7064046a3158c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
;;;;;
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.