-
Notifications
You must be signed in to change notification settings - Fork 4
/
evil-term.el
77 lines (62 loc) · 2.45 KB
/
evil-term.el
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
;;; evil-term.el --- Add Evil bindings to term-mode
;; Copyright (C) 2017 Pierre Neidhardt
;; Author: Pierre Neidhardt <[email protected]>
;; Package-Requires: ((evil "1.2.3"))
;; Package-Version: 20170724.1223
;; Homepage: https://github.com/Ambrevar/evil-special-modes
;; Version: 0
;; This file is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published
;; by the Free Software Foundation; either version 3, or (at your
;; option) any later version.
;;
;; This file is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;;
;; For a full copy of the GNU General Public License
;; see <http://www.gnu.org/licenses/>.
;;; Commentary:
;;; Code:
(require 'evil)
(require 'term)
;;; TODO: Rebinding ESC has the drawback that programs like vi cannot use it anymore.
;;; Workaround: switch to Emacs mode and double-press ESC.
;;; Otherwise leave ESC to "C-c C-j".
;;; Or bind char-mode ESC to "C-c C-x"?
(defun evil-term-char-mode-insert ()
(interactive)
(term-char-mode)
(evil-insert-state))
(defun evil-term-char-mode-entry-function ()
(when (get-buffer-process (current-buffer))
(let (last-prompt)
(save-excursion
(goto-char (point-max))
(when (= (line-beginning-position) (line-end-position))
(ignore-errors (backward-char)))
(setq last-prompt (max (term-bol nil) (line-beginning-position))))
(when (>= (point) last-prompt)
(term-char-mode)))))
(defun evil-term-sync-state-and-mode ()
(add-hook 'evil-insert-state-entry-hook 'evil-term-char-mode-entry-function)
(add-hook 'evil-insert-state-exit-hook 'term-line-mode))
;;;###autoload
(defun evil-term-set-keys ()
(evil-set-initial-state 'term-mode 'insert)
(add-hook 'term-mode-hook 'evil-term-sync-state-and-mode)
(evil-define-key 'normal term-mode-map
(kbd "C-c C-k") 'evil-term-char-mode-insert
(kbd "RET") 'term-send-input
;; motion
"[" 'term-previous-prompt
"]" 'term-next-prompt
(kbd "C-k") 'term-previous-prompt
(kbd "C-j") 'term-next-prompt
;; "0" 'term-bol ; "0" is meant to really go at the beginning of line.
"^" 'term-bol
"$" 'term-show-maximum-output)
(evil-define-key 'insert term-mode-map (kbd "C-c C-k") 'term-char-mode))
(provide 'evil-term)
;;; evil-term.el ends here