]> code.delx.au - gnu-emacs/blob - lisp/eshell/em-rebind.el
Convert consecutive FSF copyright years to ranges.
[gnu-emacs] / lisp / eshell / em-rebind.el
1 ;;; em-rebind.el --- rebind keys when point is at current input
2
3 ;; Copyright (C) 1999-2011 Free Software Foundation, Inc.
4
5 ;; Author: John Wiegley <johnw@gnu.org>
6
7 ;; This file is part of GNU Emacs.
8
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.
13
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.
18
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/>.
21
22 ;;; Commentary:
23
24 ;;; Code:
25
26 (eval-when-compile (require 'eshell))
27
28 ;;;###autoload
29 (eshell-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)
41
42 ;;; User Variables:
43
44 (defcustom eshell-rebind-load-hook '(eshell-rebind-initialize)
45 "A list of functions to call when loading `eshell-rebind'."
46 :type 'hook
47 :group 'eshell-rebind)
48
49 (defcustom eshell-rebind-keys-alist
50 '(([(control ?a)] . eshell-bol)
51 ([home] . eshell-bol)
52 ([(control ?d)] . eshell-delchar-or-maybe-eof)
53 ([backspace] . eshell-delete-backward-char)
54 ([delete] . eshell-delete-backward-char)
55 ([(control ?w)] . backward-kill-word)
56 ([(control ?u)] . eshell-kill-input))
57 "Bind some keys differently if point is in input text."
58 :type '(repeat (cons (vector :tag "Keys to bind"
59 (repeat :inline t sexp))
60 (function :tag "Command")))
61 :group 'eshell-rebind)
62
63 (defcustom eshell-confine-point-to-input t
64 "If non-nil, do not allow the point to leave the current input.
65 This is more difficult to do nicely in Emacs than one might think.
66 Basically, the `point-left' attribute is added to the input text, and
67 a function is placed on that hook to take the point back to
68 `eshell-last-output-end' every time the user tries to move away. But
69 since there are many cases in which the point _ought_ to move away
70 \(for programmatic reasons), the variable
71 `eshell-cannot-leave-input-list' defines commands which are affected
72 from this rule. However, this list is by no means as complete as it
73 probably should be, so basically all one can hope for is that other
74 people will left the point alone in the Eshell buffer. Sigh."
75 :type 'boolean
76 :group 'eshell-rebind)
77
78 (defcustom eshell-error-if-move-away t
79 "If non-nil, consider it an error to try to move outside current input.
80 This is default behavior of shells like bash."
81 :type 'boolean
82 :group 'eshell-rebind)
83
84 (defcustom eshell-remap-previous-input t
85 "If non-nil, remap input keybindings on previous prompts as well."
86 :type 'boolean
87 :group 'eshell-rebind)
88
89 (defcustom eshell-cannot-leave-input-list
90 '(beginning-of-line-text
91 beginning-of-line
92 move-to-column
93 move-to-left-margin
94 move-to-tab-stop
95 forward-char
96 backward-char
97 delete-char
98 delete-backward-char
99 backward-delete-char
100 backward-delete-char-untabify
101 kill-paragraph
102 backward-kill-paragraph
103 kill-sentence
104 backward-kill-sentence
105 kill-sexp
106 backward-kill-sexp
107 kill-word
108 backward-kill-word
109 kill-region
110 forward-list
111 backward-list
112 forward-page
113 backward-page
114 forward-point
115 forward-paragraph
116 backward-paragraph
117 backward-prefix-chars
118 forward-sentence
119 backward-sentence
120 forward-sexp
121 backward-sexp
122 forward-to-indentation
123 backward-to-indentation
124 backward-up-list
125 forward-word
126 backward-word
127 forward-line
128 previous-line
129 next-line
130 forward-visible-line
131 forward-comment
132 forward-thing)
133 "A list of commands that cannot leave the input area."
134 :type '(repeat function)
135 :group 'eshell-rebind)
136
137 ;; Internal Variables:
138
139 (defvar eshell-input-keymap)
140 (defvar eshell-previous-point)
141 (defvar eshell-lock-keymap)
142
143 ;;; Functions:
144
145 (defun eshell-rebind-initialize ()
146 "Initialize the inputing code."
147 (unless eshell-non-interactive-p
148 (add-hook 'eshell-mode-hook 'eshell-setup-input-keymap nil t)
149 (make-local-variable 'eshell-previous-point)
150 (add-hook 'pre-command-hook 'eshell-save-previous-point nil t)
151 (make-local-variable 'overriding-local-map)
152 (add-hook 'post-command-hook 'eshell-rebind-input-map nil t)
153 (set (make-local-variable 'eshell-lock-keymap) nil)
154 (define-key eshell-command-map [(meta ?l)] 'eshell-lock-local-map)))
155
156 (defun eshell-lock-local-map (&optional arg)
157 "Lock or unlock the current local keymap.
158 Within a prefix arg, set the local keymap to its normal value, and
159 lock it at that."
160 (interactive "P")
161 (if (or arg (not eshell-lock-keymap))
162 (progn
163 (use-local-map eshell-mode-map)
164 (setq eshell-lock-keymap t)
165 (message "Local keymap locked in normal mode"))
166 (use-local-map eshell-input-keymap)
167 (setq eshell-lock-keymap nil)
168 (message "Local keymap unlocked: obey context")))
169
170 (defun eshell-save-previous-point ()
171 "Save the location of point before the next command is run."
172 (setq eshell-previous-point (point)))
173
174 (defsubst eshell-point-within-input-p (pos)
175 "Test whether POS is within an input range."
176 (let (begin)
177 (or (and (>= pos eshell-last-output-end)
178 eshell-last-output-end)
179 (and eshell-remap-previous-input
180 (setq begin
181 (save-excursion
182 (eshell-bol)
183 (and (not (bolp)) (point))))
184 (>= pos begin)
185 (<= pos (line-end-position))
186 begin))))
187
188 (defun eshell-rebind-input-map ()
189 "Rebind the input keymap based on the location of the cursor."
190 (ignore-errors
191 (unless eshell-lock-keymap
192 (if (eshell-point-within-input-p (point))
193 (use-local-map eshell-input-keymap)
194 (let (begin)
195 (if (and eshell-confine-point-to-input
196 (setq begin
197 (eshell-point-within-input-p eshell-previous-point))
198 (memq this-command eshell-cannot-leave-input-list))
199 (progn
200 (use-local-map eshell-input-keymap)
201 (goto-char begin)
202 (if (and eshell-error-if-move-away
203 (not (eq this-command 'kill-region)))
204 (beep)))
205 (use-local-map eshell-mode-map)))))))
206
207 (defun eshell-setup-input-keymap ()
208 "Setup the input keymap to be used during input editing."
209 (make-local-variable 'eshell-input-keymap)
210 (setq eshell-input-keymap (make-sparse-keymap))
211 (set-keymap-parent eshell-input-keymap eshell-mode-map)
212 (let ((bindings eshell-rebind-keys-alist))
213 (while bindings
214 (define-key eshell-input-keymap (caar bindings)
215 (cdar bindings))
216 (setq bindings (cdr bindings)))))
217
218 (defun eshell-delete-backward-char (n &optional killflag)
219 "Delete the last character, unless it's part of the output."
220 (interactive "P")
221 (let ((count (prefix-numeric-value n)))
222 (if (eshell-point-within-input-p (- (point) count))
223 (delete-backward-char count n)
224 (beep))))
225
226 (defun eshell-delchar-or-maybe-eof (arg)
227 "Delete ARG characters forward or send an EOF to subprocess.
228 Sends an EOF only if point is at the end of the buffer and there is no
229 input."
230 (interactive "p")
231 (let ((proc (eshell-interactive-process)))
232 (if (eobp)
233 (cond
234 ((/= (point) eshell-last-output-end)
235 (beep))
236 (proc
237 (process-send-eof))
238 (t
239 (eshell-life-is-too-much)))
240 (eshell-delete-backward-char (- arg)))))
241
242 (provide 'em-rebind)
243
244 ;; Local Variables:
245 ;; generated-autoload-file: "esh-groups.el"
246 ;; End:
247
248 ;;; em-rebind.el ends here