1 ;;; em-rebind.el --- rebind keys when point is at current input
3 ;; Copyright (C) 1999, 2000, 2002, 2003, 2004,
4 ;; 2005 Free Software Foundation, Inc.
6 ;; Author: John Wiegley <johnw@gnu.org>
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
23 ;; Boston, MA 02110-1301, USA.
27 (eval-when-compile (require 'esh-maint
))
29 (defgroup eshell-rebind nil
30 "This module allows for special keybindings that only take effect
31 while the point is in a region of input text. By default, it binds
32 C-a to move to the beginning of the input text (rather than just the
33 beginning of the line), and C-p and C-n to move through the input
34 history, C-u kills the current input text, etc. It also, if
35 `eshell-confine-point-to-input' is non-nil, does not allow certain
36 commands to cause the point to leave the input area, such as
37 `backward-word', `previous-line', etc. This module intends to mimic
38 the behavior of normal shells while the user editing new input text."
39 :tag
"Rebind keys at input"
40 :group
'eshell-module
)
46 (defcustom eshell-rebind-load-hook
'(eshell-rebind-initialize)
47 "*A list of functions to call when loading `eshell-rebind'."
49 :group
'eshell-rebind
)
51 (defcustom eshell-rebind-keys-alist
52 '(([(control ?a
)] . eshell-bol
)
54 ([(control ?d)] . eshell-delchar-or-maybe-eof)
55 ([backspace] . eshell-delete-backward-char)
56 ([delete] . eshell-delete-backward-char)
57 ([(control ?w)] . backward-kill-word)
58 ([(control ?u)] . eshell-kill-input))
59 "*Bind some keys differently if point is in input text."
60 :type '(repeat (cons (vector :tag "Keys to bind"
61 (repeat :inline t sexp))
62 (function :tag "Command")))
63 :group 'eshell-rebind)
65 (defcustom eshell-confine-point-to-input t
66 "*If non-nil, do not allow the point to leave the current input.
67 This is more difficult to do nicely in Emacs than one might think.
68 Basically, the `point-left' attribute is added to the input text, and
69 a function is placed on that hook to take the point back to
70 `eshell-last-output-end' every time the user tries to move away. But
71 since there are many cases in which the point _ought_ to move away
72 \(for programmatic reasons), the variable
73 `eshell-cannot-leave-input-list' defines commands which are affected
74 from this rule. However, this list is by no means as complete as it
75 probably should be, so basically all one can hope for is that other
76 people will left the point alone in the Eshell buffer. Sigh."
78 :group 'eshell-rebind)
80 (defcustom eshell-error-if-move-away t
81 "*If non-nil, consider it an error to try to move outside current input.
82 This is default behavior of shells like bash."
84 :group 'eshell-rebind)
86 (defcustom eshell-remap-previous-input t
87 "*If non-nil, remap input keybindings on previous prompts as well."
89 :group 'eshell-rebind)
91 (defcustom eshell-cannot-leave-input-list
92 '(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 inputing 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 &optional killflag)
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)))))
247 ;;; arch-tag: 76d84f12-cc56-4d67-9b7d-c6b44ad20530
248 ;;; em-rebind.el ends here