noctuid/general.el

Leader key only works in messages buffer after changing modes (EVIL)

zloom31 opened this issue · 4 comments

I have configured my leader key to work with evil & evil-collection but it still refuses to work properly with the messages buffer. Only after changing modes does it recognise the leader key, otherwise it tries to execute evil-forward-char function.

This issue was mentioned here but even after adding motion as state it does not seem to work.

@bakku Keybindings will work after a state change. I'm not exactly sure why this is (you can look for or open a bug report on the evil repo if you want). `messages-buffer-mode` starts in normal state, but the motion state keybindings are used initially, and running `evil-normalize-keymaps` in `messages-buffer-mode-hook` does not seem to help. As a workaround you can probably also specify `motion` as a state.

#190 (comment)

My general.el config section:

(use-package general
    :init
    (setq general-override-states '(insert
				    emacs
				    hybrid
				    normal
				    visual
				    motion
				    operator
				    replace))
    :config
    (general-evil-setup t)

    (general-create-definer my/leader-key-def
      :states '(motion normal insert visual emacs)
      :keymaps 'override
      :prefix "SPC"
      :global-prefix "C-SPC")

    (general-create-definer my/local-leader-def
      ;; :prefix my-local-leader
      :prefix "SPC m"))

Try this:

(general-with 'evil
  ;; (evil-set-initial-state 'messages-buffer-mode 'normal)
  ;; this won't work (runs too soon it seems)
  ;; (general-add-hook 'messages-buffer-mode-hook #'evil-normalize-keymaps)
  ;; this doesn't work
  ;; (general-add-advice 'after-find-file
  ;;                     :after (lambda (&rest _)
  ;;                              (when (eq major-mode 'messages-buffer-mode)
  ;;                                (evil-normalize-keymaps)
  ;;                                t))
  ;;                     nil
  ;;                     #'identity)
  ;; this is what fixes things
  (general-add-hook 'post-command-hook
                    (lambda (&rest _)
                      (when (eq major-mode 'messages-buffer-mode)
                        (evil-normalize-keymaps)
                        t))
                    nil
                    nil
                    #'identity))

Thanks for the reply. This seems to work but only if another buffer is visited before messages. Otherwise I need to press SPC (reads it as evil-forward-char) once before it will start using the leader properly.

Do you not have a scratch buffer and visit Messages immediately? Maybe try this:

(general-add-hook 'after-init-hook
                    (lambda (&rest _)
                      (when-let ((messages-buffer (get-buffer "*Messages*")))
                        (with-current-buffer messages-buffer
                          (evil-normalize-keymaps))))
                    nil
                    nil
                    t)

Thanks a lot! This works perfectly.
The reason I do not have a scratch buffer on launch is because I launch my emacs with key bound to this command:
emacsclient -c -n -e '(switch-to-buffer nil)'

It opens the Messages buffer immediately on the first startup but it's very handy since it reopens the last buffer I had open when I close my emacs frame.