0

I'm using evil (via spacemacs) and I want to bind <f2> to open and close the undo-tree (like vim-Gundo). I can successfully bind opening undo-tree-visualize, but I can't figure out how to quit. I tried the obvious:

(define-key evil-normal-state-map (kbd "<f2>") 'undo-tree-visualizer)
(define-key undo-tree-visualizer-mode-map (kbd "<f2>") 'undo-tree-visualizer-quit)

It correctly opens undo-tree but doesn't close it (I get a "no undo information" error). I also tried to hook on load:

(defun my-undo-tree-hook ()
    (define-key undo-tree-visualizer-mode-map (kbd "<f2>") 'undo-tree-visualizer-quit))
(add-hook 'undo-tree-visualizer-mode-hook 'my-undo-tree-hook)

But that also doesn't work.

What am I doing wrong?

1 Answer 1

1

To get the same key to do different things in different contexts, bind it to a function that checks if the undo tree is open. You can use get-buffer[1] [2] to check for the undo-tree window and use the variable for the buffer name:

(defun undo-tree-visualizer-toggle ()
  (interactive)
  (if (get-buffer undo-tree-visualizer-buffer-name)
      (undo-tree-visualizer-quit)
    (undo-tree-visualize)))
;; You're usually in normal mode.
(define-key evil-normal-state-map (kbd "<f2>") 'undo-tree-visualizer-toggle)
;; Inside the undo-tree, you're in motion mode.
(define-key evil-motion-state-map (kbd "<f2>") 'undo-tree-visualizer-toggle)

You can find variables with C-h v (with helm I incrementally searched for "undo-tree buffer" to find it).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.