1 ;;; em-rebind.el --- rebind keys when point is at current input -*- lexical-binding:t -*-
3 ;; Copyright (C) 1999-2015 Free Software Foundation, Inc.
5 ;; Author: John Wiegley <johnw@gnu.org>
7 ;; This file is part of GNU Emacs.
9 ;; GNU Emacs is free software: you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation, either version 3 of the License, or
12 ;; (at your option) any later version.
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
27 (eval-when-compile (require 'eshell
))
31 (defgroup eshell-rebind nil
32 "This module allows for special keybindings that only take effect
33 while the point is in a region of input text. By default, it binds
34 C-a to move to the beginning of the input text (rather than just the
35 beginning of the line), and C-p and C-n to move through the input
36 history, C-u kills the current input text, etc. It also, if
37 `eshell-confine-point-to-input' is non-nil, does not allow certain
38 commands to cause the point to leave the input area, such as
39 `backward-word', `previous-line', etc. This module intends to mimic
40 the behavior of normal shells while the user editing new input text."
41 :tag
"Rebind keys at input"
42 :group
'eshell-module
))
46 (defcustom eshell-rebind-load-hook nil
47 "A list of functions to call when loading `eshell-rebind'."
48 :version
"24.1" ; removed eshell-rebind-initialize
50 :group
'eshell-rebind
)
52 (defcustom eshell-rebind-keys-alist
53 '(([(control ?a
)] . eshell-bol
)
55 ([(control ?d)] . eshell-delchar-or-maybe-eof)
56 ([backspace] . eshell-delete-backward-char)
57 ([delete] . eshell-delete-backward-char)
58 ([(control ?w)] . backward-kill-word)
59 ([(control ?u)] . eshell-kill-input))
60 "Bind some keys differently if point is in input text."
61 :type '(repeat (cons (vector :tag "Keys to bind"
62 (repeat :inline t sexp))
63 (function :tag "Command")))
64 :group 'eshell-rebind)
66 (defcustom eshell-confine-point-to-input t
67 "If non-nil, do not allow the point to leave the current input.
68 This is more difficult to do nicely in Emacs than one might think.
69 Basically, the `point-left' attribute is added to the input text, and
70 a function is placed on that hook to take the point back to
71 `eshell-last-output-end' every time the user tries to move away. But
72 since there are many cases in which the point _ought_ to move away
73 \(for programmatic reasons), the variable
74 `eshell-cannot-leave-input-list' defines commands which are affected
75 from this rule. However, this list is by no means as complete as it
76 probably should be, so basically all one can hope for is that other
77 people will left the point alone in the Eshell buffer. Sigh."
79 :group 'eshell-rebind)
81 (defcustom eshell-error-if-move-away t
82 "If non-nil, consider it an error to try to move outside current input.
83 This is default behavior of shells like bash."
85 :group 'eshell-rebind)
87 (defcustom eshell-remap-previous-input t
88 "If non-nil, remap input keybindings on previous prompts as well."
90 :group 'eshell-rebind)
92 (defcustom eshell-cannot-leave-input-list
93 '(beginning-of-line-text
103 backward-delete-char-untabify
105 backward-kill-paragraph
107 backward-kill-sentence
120 backward-prefix-chars
125 forward-to-indentation
126 backward-to-indentation
136 "A list of commands that cannot leave the input area."
137 :type '(repeat function)
138 :group 'eshell-rebind)
140 ;; Internal Variables:
142 (defvar eshell-input-keymap)
143 (defvar eshell-previous-point)
144 (defvar eshell-lock-keymap)
148 (defun eshell-rebind-initialize ()
149 "Initialize the inputting code."
150 (unless eshell-non-interactive-p
151 (add-hook 'eshell-mode-hook 'eshell-setup-input-keymap nil t)
152 (make-local-variable 'eshell-previous-point)
153 (add-hook 'pre-command-hook 'eshell-save-previous-point nil t)
154 (make-local-variable 'overriding-local-map)
155 (add-hook 'post-command-hook 'eshell-rebind-input-map nil t)
156 (set (make-local-variable 'eshell-lock-keymap) nil)
157 (define-key eshell-command-map [(meta ?l)] 'eshell-lock-local-map)))
159 (defun eshell-lock-local-map (&optional arg)
160 "Lock or unlock the current local keymap.
161 Within a prefix arg, set the local keymap to its normal value, and
164 (if (or arg (not eshell-lock-keymap))
166 (use-local-map eshell-mode-map)
167 (setq eshell-lock-keymap t)
168 (message "Local keymap locked in normal mode"))
169 (use-local-map eshell-input-keymap)
170 (setq eshell-lock-keymap nil)
171 (message "Local keymap unlocked: obey context")))
173 (defun eshell-save-previous-point ()
174 "Save the location of point before the next command is run."
175 (setq eshell-previous-point (point)))
177 (defsubst eshell-point-within-input-p (pos)
178 "Test whether POS is within an input range."
180 (or (and (>= pos eshell-last-output-end)
181 eshell-last-output-end)
182 (and eshell-remap-previous-input
186 (and (not (bolp)) (point))))
188 (<= pos (line-end-position))
191 (defun eshell-rebind-input-map ()
192 "Rebind the input keymap based on the location of the cursor."
194 (unless eshell-lock-keymap
195 (if (eshell-point-within-input-p (point))
196 (use-local-map eshell-input-keymap)
198 (if (and eshell-confine-point-to-input
200 (eshell-point-within-input-p eshell-previous-point))
201 (memq this-command eshell-cannot-leave-input-list))
203 (use-local-map eshell-input-keymap)
205 (if (and eshell-error-if-move-away
206 (not (eq this-command 'kill-region)))
208 (use-local-map eshell-mode-map)))))))
210 (defun eshell-setup-input-keymap ()
211 "Setup the input keymap to be used during input editing."
212 (make-local-variable 'eshell-input-keymap)
213 (setq eshell-input-keymap (make-sparse-keymap))
214 (set-keymap-parent eshell-input-keymap eshell-mode-map)
215 (let ((bindings eshell-rebind-keys-alist))
217 (define-key eshell-input-keymap (caar bindings)
219 (setq bindings (cdr bindings)))))
221 (defun eshell-delete-backward-char (n)
222 "Delete the last character, unless it's part of the output."
224 (let ((count (prefix-numeric-value n)))
225 (if (eshell-point-within-input-p (- (point) count))
226 (delete-backward-char count n)
229 (defun eshell-delchar-or-maybe-eof (arg)
230 "Delete ARG characters forward or send an EOF to subprocess.
231 Sends an EOF only if point is at the end of the buffer and there is no
234 (let ((proc (eshell-interactive-process)))
237 ((/= (point) eshell-last-output-end)
242 (eshell-life-is-too-much)))
243 (eshell-delete-backward-char (- arg)))))
248 ;; generated-autoload-file: "esh-groups.el"
251 ;;; em-rebind.el ends here