blob: fb275e8bba0eeafec1821a73fb04f8ee7e31ae17 (
plain) (
blame)
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
|
;;; evil-collection-term.el --- Evil bindings for term and ansi-term -*- lexical-binding: t -*-
;; Copyright (C) 2017 Pierre Neidhardt
;; Author: Pierre Neidhardt <mail@ambrevar.xyz>
;; Maintainer: James Nguyen <james@jojojames.com>
;; Pierre Neidhardt <mail@ambrevar.xyz>
;; URL: https://github.com/emacs-evil/evil-collection
;; Version: 0.0.1
;; Package-Requires: ((emacs "25.1"))
;; Keywords: evil, term, tools
;; 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:
;; Evil integration for `term' and `ansi-term'.
;; This should also work for other terminal emulators such as `multi-term'.
;;
;; Switching to normal state will automatically switch to line mode.
;; Conversely, switching to insert state will automatically switch to char mode.
;;; Code:
(require 'evil-collection)
(require 'term)
(defcustom evil-collection-term-sync-state-and-mode-p t
"Synchronize insert/normal state with char/line-mode respectively.
When non-nil, going to normal state will automatically switch to
line-mode. Conversely, going to insert state on the last
commandline will automatically switch to char-mode.
Warning: This feature is experimental."
:group 'evil-collection-term
:type 'boolean)
(defcustom evil-collection-term-sync-state-function
'evil-collection-term-switch-to-char-mode-on-insert
"Function used when synchronizing insert/normal state with char/line-mode.
This is only used if `evil-collection-term-sync-state-and-mode-p' is true."
:group 'evil-collection-term
:type 'function)
;; TODO: Rebinding ESC has the drawback that programs like vi cannot use it anymore.
;; Workaround: switch to Emacs state and double-press ESC.
;; Otherwise leave ESC to "C-c C-j".
;; Or bind char-mode ESC to "C-c C-x"?
;; TODO: Add support for normal-state editing.
(defconst evil-collection-term-maps '(term-raw-map
term-mode-map))
(defun evil-collection-term-escape-stay ()
"Go back to normal state but don't move cursor backwards.
Moving cursor backwards is the default Vim behavior but
it is not appropriate in some cases like terminals."
(setq-local evil-move-cursor-back nil))
(defun evil-collection-term-char-mode-insert ()
"Switch to `term-char-mode' and enter insert state."
(interactive)
(term-char-mode)
(evil-insert-state))
(defun evil-collection-term-char-mode-entry-function ()
"Maybe switch to `term-char-mode' on insert state."
(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-collection-term-switch-to-char-mode-on-insert ()
"Switch to `term-char-mode' on insert state."
(when (get-buffer-process (current-buffer))
(term-char-mode)))
(defun evil-collection-term-sync-state-and-mode ()
"Sync `term-char-mode' and `term-line-mode' with insert and normal state."
(add-hook 'evil-insert-state-entry-hook
evil-collection-term-sync-state-function nil t)
(add-hook 'evil-insert-state-exit-hook 'term-line-mode nil t))
(defun evil-collection-term-send-tab ()
"Send tab in term mode."
(interactive)
(term-send-raw-string "\t"))
(defun evil-collection-term-setup ()
"Set up `evil' bindings for `term'."
(evil-set-initial-state 'term-mode 'insert)
(if evil-collection-term-sync-state-and-mode-p
(add-hook 'term-mode-hook 'evil-collection-term-sync-state-and-mode)
(remove-hook 'term-mode-hook 'evil-collection-term-sync-state-and-mode))
(add-hook 'term-mode-hook 'evil-collection-term-escape-stay)
;; Evil has some "C-" bindings in insert state that shadow regular terminal bindings.
;; Don't raw-send "C-c" (prefix key) nor "C-h" (help prefix).
(evil-collection-define-key 'insert 'term-raw-map
(kbd "C-a") 'term-send-raw
(kbd "C-b") 'term-send-raw ; Should not be necessary.
(kbd "C-d") 'term-send-raw
(kbd "C-e") 'term-send-raw
(kbd "C-f") 'term-send-raw ; Should not be necessary.
(kbd "C-k") 'term-send-raw
(kbd "C-l") 'term-send-raw ; Should not be necessary.
(kbd "C-n") 'term-send-raw
(kbd "C-o") 'term-send-raw
(kbd "C-p") 'term-send-raw
(kbd "C-q") 'term-send-raw ; Should not be necessary.
(kbd "C-r") 'term-send-raw
(kbd "C-s") 'term-send-raw ; Should not be necessary.
(kbd "C-t") 'term-send-raw
(kbd "C-u") 'term-send-raw ; Should not be necessary.
(kbd "C-v") 'term-send-raw ; Should not be necessary.
(kbd "C-w") 'term-send-raw
(kbd "C-y") 'term-send-raw
(kbd "C-z") 'term-send-raw
(kbd "<tab>") 'evil-collection-term-send-tab ; Should not be necessary.
(kbd "C-c C-d") 'term-send-eof
(kbd "C-c C-z") 'term-stop-subjob)
(evil-collection-define-key 'normal 'term-mode-map
(kbd "C-c C-k") 'evil-collection-term-char-mode-insert
(kbd "<return>") 'term-send-input
(kbd "p") 'term-paste
;; motion
"[" 'term-previous-prompt
"]" 'term-next-prompt
(kbd "C-k") 'term-previous-prompt
(kbd "C-j") 'term-next-prompt
"gk" 'term-previous-prompt
"gj" 'term-next-prompt
;; "0" 'term-bol ; "0" is meant to really go at the beginning of line.
"^" 'term-bol
"$" 'term-show-maximum-output))
(provide 'evil-collection-term)
;;; evil-collection-term.el ends here
|