summaryrefslogtreecommitdiff
path: root/evil-collection-term.el
diff options
context:
space:
mode:
authorJames Nguyen <james@jojojames.com>2017-12-06 17:41:09 -0800
committerJames Nguyen <james@jojojames.com>2017-12-06 17:50:37 -0800
commit9eb7d3db0d4b7ffbc6ea6c137b0f2ec21f907d53 (patch)
treefee440d522a6270fca6b4989f654e10da8d293a1 /evil-collection-term.el
parent4992d5fb0fd310d73ddc8a8301120552eb9a136f (diff)
Change namespace to evil-collection
Diffstat (limited to 'evil-collection-term.el')
-rw-r--r--evil-collection-term.el127
1 files changed, 127 insertions, 0 deletions
diff --git a/evil-collection-term.el b/evil-collection-term.el
new file mode 100644
index 0000000..45c78db
--- /dev/null
+++ b/evil-collection-term.el
@@ -0,0 +1,127 @@
+;;; evil-collection-term.el --- Evil bindings for term and ansi-term -*- lexical-binding: t -*-
+
+;; Copyright (C) 2017 Pierre Neidhardt
+
+;; Author: Pierre Neidhardt <ambrevar@gmail.com>
+;; Maintainer: James Nguyen <james@jojojames.com>
+;; Pierre Neidhardt <ambrevar@gmail.com>
+;; URL: https://github.com/jojojames/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)
+(require 'term)
+
+;;; 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"?
+
+(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-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-term-char-mode-entry-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)
+ (add-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-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-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