comparison lisp/eshell/em-rebind.el @ 29876:edfec1c0d511

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