Skip to content

Commit

Permalink
Make it possible to assign a key binding to evil-escape
Browse files Browse the repository at this point in the history
Fixes #29
  • Loading branch information
syl20bnr committed Sep 2, 2015
1 parent 5c5b1a6 commit 65756d4
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 26 deletions.
17 changes: 13 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
- [Delay between keys](#delay-between-keys)
- [Excluding a major mode](#excluding-a-major-mode)
- [Enable only for a list of major modes](#enable-only-for-a-list-of-major-modes)
- [Assign a key binding directly](#assign-a-key-binding-directly)

<!-- markdown-toc end -->

Expand Down Expand Up @@ -92,9 +93,17 @@ A major mode can be excluded by adding it to the list

### Enable only for a list of major modes

It is also possible to provide an inclusive list of major modes
with the variable `evil-escape-enable-only-for-major-modes`. When this list
non-nil then evil-escape is enabled only for the major-modes contained in the
list.
An inclusive list of major modes can defined with the variable
`evil-escape-enable-only-for-major-modes`. When this list is non-nil
then evil-escape is enabled only for the major-modes in the list.

### Assign a key binding directly

It is possible to bind `evil-escape' function directly`, for
instance to execute evil-escape with <kbd>C-c C-g</kbd>:

```elisp
(global-set-key (kbd "C-c C-g") 'evil-escape)
```

[MELPA]: http://melpa.org/
49 changes: 27 additions & 22 deletions evil-escape.el
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
;; Author: Sylvain Benner <[email protected]>
;; Keywords: convenience editing evil
;; Created: 22 Oct 2014
;; Version: 3.04
;; Version: 3.05
;; Package-Requires: ((emacs "24") (evil "1.0.9"))
;; URL: https://github.com/syl20bnr/evil-escape

Expand Down Expand Up @@ -58,10 +58,15 @@
;; A major mode can be excluded by adding it to the list
;; `evil-escape-excluded-major-modes'.

;; It is also possible to provide an inclusive list of major modes
;; with the variable `evil-escape-enable-only-for-major-modes'. When this list
;; non-nil then evil-escape is enabled only for the major-modes contained in the
;; list.
;; An inclusive list of major modes can defined with the variable
;; `evil-escape-enable-only-for-major-modes'. When this list is
;; non-nil then evil-escape is enabled only for the major-modes
;; in the list.

;; It is possible to bind `evil-escape' function directly, for
;; instance to execute evil-escape with `C-c C-g':

;; (global-set-key (kbd "C-c C-g") 'evil-escape)

;; More information in the readme of the repository:
;; https://github.com/syl20bnr/evil-escape
Expand Down Expand Up @@ -106,6 +111,21 @@ with a key sequence."
(add-hook 'pre-command-hook 'evil-escape-pre-command-hook)
(remove-hook 'pre-command-hook 'evil-escape-pre-command-hook)))

(defun evil-escape ()
"Escape from everything... well almost everything."
(interactive)
(pcase evil-state
(`normal (evil-escape--escape-normal-state))
(`motion (evil-escape--escape-motion-state))
(`insert (evil-normal-state))
(`emacs (evil-escape--escape-emacs-state))
(`evilified (evil-escape--escape-emacs-state))
(`visual (evil-exit-visual-state))
(`replace (evil-normal-state))
(`lisp (evil-normal-state))
(`iedit (evil-iedit-state/quit-iedit-mode))
(`iedit-insert (evil-iedit-state/quit-iedit-mode))))

(defun evil-escape-pre-command-hook ()
"evil-escape pre-command hook."
(when (evil-escape-p)
Expand All @@ -117,14 +137,14 @@ with a key sequence."
(set-buffer-modified-p modified)
(cond
((and (integerp evt) (char-equal evt skey))
(evil-escape--escape)
(evil-escape)
(setq this-command 'ignore))
((null evt))
(t (setq unread-command-events
(append unread-command-events (list evt))))))))

(defun evil-escape-p ()
"Return non-nil if evil-escape should run."
"Return non-nil if evil-escape can run."
(and (or (window-minibuffer-p)
(bound-and-true-p isearch-mode)
(and (fboundp 'helm-alive-p) (helm-alive-p))
Expand All @@ -134,21 +154,6 @@ with a key sequence."
(memq major-mode evil-escape-enable-only-for-major-modes))
(equal (this-command-keys) (evil-escape--first-key))))

(defun evil-escape--escape ()
"Escape from everything... well almost everything."
(pcase evil-state
(`normal (evil-escape--escape-normal-state))
(`motion (evil-escape--escape-motion-state))
(`insert (evil-normal-state))
(`emacs (evil-escape--escape-emacs-state))
(`evilified (evil-escape--escape-emacs-state))
(`visual (evil-exit-visual-state))
(`replace (evil-normal-state))
(`lisp (evil-normal-state))
(`iedit (evil-iedit-state/quit-iedit-mode))
(`iedit-insert (evil-iedit-state/quit-iedit-mode))
(_ (evil-escape--escape-normal-state))))

(defun evil-escape--escape-normal-state ()
"Escape from normal state."
(cond
Expand Down

0 comments on commit 65756d4

Please sign in to comment.