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