Merge term bindings from evil-special-modes

Previous, we had evil-ansi-term here.  I think the name evil-term is more
appropriate as it supports term, ansi-term and multi-term.

I've merged the hook that synchronizes normal/line mode and insert/char mode.
Because of this, there is no need to add bindings to send raw characters in
insert mode.

I've integrated the feature of not moving the cursor back: the new
implementation does not rebind <escape>, it sets `evil-move-cursor-back locally.
This commit is contained in:
Pierre Neidhardt 2017-11-06 14:55:39 +01:00
parent 62c48917b2
commit d936b5191c
2 changed files with 90 additions and 71 deletions

View File

@ -1,71 +0,0 @@
;;; evil-ansi-term.el --- Evil integration for ansi-term. -*- lexical-binding: t -*-
;; Copyright (C) 2017 James Nguyen
;; Author: James Nguyen <james@jojojames.com>
;; Maintainer: James Nguyen <james@jojojames.com>
;; URL: https://github.com/jojojames/evil-collection
;; Version: 0.0.1
;; Package-Requires: ((emacs "25.1"))
;; Keywords: evil, emacs, ansi-term
;; HomePage: https://github.com/jojojames/evil-collection
;; This program 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 of the License, or
;; (at your option) any later version.
;; This program 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.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;; Evil integration for `ansi-term' and `multi-term'.
;;; Code:
(require 'term)
(require 'evil-collection-util)
(defun +evil-escape-stay ()
"Go back to Normal State but don't move cursor backwards.
Moving cursor backwards when going back to Normal is default Vim behavior but
is not good default behavior in some cases. (Like Terminal)"
(interactive)
(evil-normal-state)
(evil-forward-char 1))
(defun +evil-term-send-tab ()
"Send tab in term mode."
(interactive)
(term-send-raw-string "\t"))
(defun evil-ansi-term-set-keys ()
(evil-define-key 'normal term-raw-map
(kbd "p") 'term-paste
(kbd "C-h") 'help-command)
(evil-define-key 'insert term-raw-map
(kbd "C-a") 'term-send-raw
(kbd "C-b") 'term-send-raw
(kbd "C-d") 'term-send-raw
(kbd "C-f") 'term-send-raw
(kbd "C-e") 'term-send-raw
(kbd "C-h") 'help-command
(kbd "C-k") 'term-send-raw
(kbd "C-l") 'term-send-raw
(kbd "C-r") 'term-send-raw
(kbd "C-u") 'term-send-raw
(kbd "C-w") 'term-send-raw
(kbd "C-y") 'term-send-raw
(kbd "<escape>") #'+evil-escape-stay
(kbd "ESC") #'+evil-escape-stay
(kbd "C-c C-d") 'term-send-eof
(kbd "C-c C-z") 'term-stop-subjob
(kbd "<tab>") '+evil-term-send-tab))
(provide 'evil-ansi-term)
;;; evil-ansi-term.el ends here

90
evil-term.el Normal file
View File

@ -0,0 +1,90 @@
;;; evil-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, emacs, term
;; HomePage: https://github.com/jojojames/evil-collection
;; 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 mode will automatically switch to line mode.
;; Conversely, switching to insert mode 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 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-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-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))
(defun evil-term-set-keys ()
(evil-set-initial-state 'term-mode 'insert)
(add-hook 'term-mode-hook 'evil-term-sync-state-and-mode)
(add-hook 'term-mode-hook 'evil-term-escape-stay)
(evil-define-key 'normal term-mode-map
(kbd "C-c C-k") 'evil-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
;; "0" 'term-bol ; "0" is meant to really go at the beginning of line.
"^" 'term-bol
"$" 'term-show-maximum-output))
(provide 'evil-term)
;;; evil-term.el ends here