Mercurial > emacs
annotate lisp/eshell/esh-mode.el @ 31314:16b334e9c404
*** empty log message ***
author | Peter Breton <pbreton@attbi.com> |
---|---|
date | Thu, 31 Aug 2000 23:53:09 +0000 |
parents | 3099993cba0f |
children | a36c3cf583d4 |
rev | line source |
---|---|
29870 | 1 ;;; esh-mode --- user interface |
2 | |
29934
34b1ab9d583d
Change spelling of the Free Software Foundation.
Gerd Moellmann <gerd@gnu.org>
parents:
29872
diff
changeset
|
3 ;; Copyright (C) 1999, 2000 Free Software Foundation |
29870 | 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 'esh-mode) | |
23 | |
24 (eval-when-compile (require 'esh-maint)) | |
25 | |
26 (defgroup eshell-mode nil | |
27 "This module contains code for handling input from the user." | |
28 :tag "User interface" | |
29 :group 'eshell) | |
30 | |
31 ;;; Commentary: | |
32 | |
33 ;; Basically, Eshell is used just like shell mode (<M-x shell>). The | |
34 ;; keystrokes for navigating the buffer, and accessing the command | |
35 ;; history, are identical. Unlike shell mode, however, Eshell mode's | |
36 ;; governing process is Emacs itself. With shell mode, an inferior | |
37 ;; shell process is executed that communicates with Emacs via comint | |
38 ;; -- a mode for handling sub-process interaction. Eshell mode, on | |
39 ;; the other hand, is a truly native Emacs shell. No subprocess are | |
40 ;; invoked except the ones requested by the user at the prompt. | |
41 ;; | |
42 ;; After entering a command, use <RET> to invoke it ([Command | |
43 ;; invocation]) . If there is a command on disk, it will be executed | |
44 ;; as in a normal shell. If there is no command by that name on disk, | |
45 ;; but a Lisp function with that name is defined, the Lisp function | |
46 ;; will be called, using the arguments passed on the command line. | |
47 ;; | |
48 ;; Some of the other features of the command interaction mode are: | |
49 ;; | |
50 ;; @ <M-RET> can be used to accumulate further commands while a | |
51 ;; command is currently running. Since all input is passed to the | |
52 ;; subprocess being executed, there is no automatic input queueing | |
53 ;; as there is with other shells. | |
54 ;; | |
55 ;; @ <C-c C-t> can be used to truncate the buffer if it grows too | |
56 ;; large. | |
57 ;; | |
58 ;; @ <C-c C-r> will move point to the beginning of the output of the | |
59 ;; last command. With a prefix argument, it will narrow to view | |
60 ;; only that output. | |
61 ;; | |
62 ;; @ <C-c C-o> will delete the output from the last command. | |
63 ;; | |
64 ;; @ <C-c C-f> will move forward a complete shell argument. | |
65 ;; | |
66 ;; @ <C-c C-b> will move backward a complete shell argument. | |
67 | |
68 (require 'esh-module) | |
69 (require 'esh-cmd) | |
70 (require 'esh-io) | |
71 (require 'esh-var) | |
72 | |
73 ;;; User Variables: | |
74 | |
75 (defcustom eshell-mode-unload-hook nil | |
76 "*A hook that gets run when `eshell-mode' is unloaded." | |
77 :type 'hook | |
78 :group 'eshell-mode) | |
79 | |
80 (defcustom eshell-mode-hook nil | |
81 "*A hook that gets run when `eshell-mode' is entered." | |
82 :type 'hook | |
83 :group 'eshell-mode) | |
84 | |
85 (defcustom eshell-first-time-mode-hook nil | |
86 "*A hook that gets run the first time `eshell-mode' is entered. | |
87 That is to say, the first time during an Emacs session." | |
88 :type 'hook | |
89 :group 'eshell-mode) | |
90 | |
91 (defcustom eshell-exit-hook '(eshell-query-kill-processes) | |
92 "*A hook that is run whenever `eshell' is exited. | |
93 This hook is only run if exiting actually kills the buffer." | |
94 :type 'hook | |
95 :group 'eshell-mode) | |
96 | |
97 (defcustom eshell-kill-on-exit t | |
98 "*If non-nil, kill the Eshell buffer on the `exit' command. | |
99 Otherwise, the buffer will simply be buried." | |
100 :type 'boolean | |
101 :group 'eshell-mode) | |
102 | |
103 (defcustom eshell-input-filter-functions nil | |
104 "*Functions to call before input is processed. | |
105 The input is contained in the region from `eshell-last-input-start' to | |
106 `eshell-last-input-end'." | |
107 :type 'hook | |
108 :group 'eshell-mode) | |
109 | |
110 (defcustom eshell-expand-input-functions nil | |
111 "*Functions to call before input is parsed. | |
112 Each function is passed two arguments, which bounds the region of the | |
113 current input text." | |
114 :type 'hook | |
115 :group 'eshell-mode) | |
116 | |
117 (defcustom eshell-scroll-to-bottom-on-input nil | |
118 "*Controls whether input to interpreter causes window to scroll. | |
119 If nil, then do not scroll. If t or `all', scroll all windows showing | |
120 buffer. If `this', scroll only the selected window. | |
121 | |
122 See `eshell-preinput-scroll-to-bottom'." | |
123 :type '(radio (const :tag "Do not scroll Eshell windows" nil) | |
124 (const :tag "Scroll all windows showing the buffer" all) | |
125 (const :tag "Scroll only the selected window" this)) | |
126 :group 'eshell-mode) | |
127 | |
128 (defcustom eshell-scroll-to-bottom-on-output nil | |
129 "*Controls whether interpreter output causes window to scroll. | |
130 If nil, then do not scroll. If t or `all', scroll all windows showing | |
131 buffer. If `this', scroll only the selected window. If `others', | |
132 scroll only those that are not the selected window. | |
133 | |
134 See variable `eshell-scroll-show-maximum-output' and function | |
135 `eshell-postoutput-scroll-to-bottom'." | |
136 :type '(radio (const :tag "Do not scroll Eshell windows" nil) | |
137 (const :tag "Scroll all windows showing the buffer" all) | |
138 (const :tag "Scroll only the selected window" this) | |
139 (const :tag "Scroll all windows other than selected" this)) | |
140 :group 'eshell-mode) | |
141 | |
142 (defcustom eshell-scroll-show-maximum-output t | |
143 "*Controls how interpreter output causes window to scroll. | |
144 If non-nil, then show the maximum output when the window is scrolled. | |
145 | |
146 See variable `eshell-scroll-to-bottom-on-output' and function | |
147 `eshell-postoutput-scroll-to-bottom'." | |
148 :type 'boolean | |
149 :group 'eshell-mode) | |
150 | |
151 (defcustom eshell-buffer-maximum-lines 1024 | |
152 "*The maximum size in lines for eshell buffers. | |
153 Eshell buffers are truncated from the top to be no greater than this | |
154 number, if the function `eshell-truncate-buffer' is on | |
155 `eshell-output-filter-functions'." | |
156 :type 'integer | |
157 :group 'eshell-mode) | |
158 | |
159 (defcustom eshell-output-filter-functions | |
160 '(eshell-handle-control-codes | |
161 eshell-watch-for-password-prompt) | |
162 "*Functions to call before output is displayed. | |
163 These functions are only called for output that is displayed | |
164 interactively, and not for output which is redirected." | |
165 :type 'hook | |
166 :group 'eshell-mode) | |
167 | |
168 (defcustom eshell-preoutput-filter-functions nil | |
169 "*Functions to call before output is inserted into the buffer. | |
170 These functions get one argument, a string containing the text to be | |
171 inserted. They return the string as it should be inserted." | |
172 :type 'hook | |
173 :group 'eshell-mode) | |
174 | |
175 (defcustom eshell-password-prompt-regexp | |
176 "\\(\\([Oo]ld \\|[Nn]ew \\|Kerberos \\|CVS \\|'s \\|login \\|^\\)\ | |
177 [Pp]assword\\|pass phrase\\|\\(Enter\\|Repeat\\) passphrase\\)\ | |
178 \\( for [^@ \t\n]+@[^@ \t\n]+\\)?:\\s *\\'" | |
179 "*Regexp matching prompts for passwords in the inferior process. | |
180 This is used by `eshell-watch-for-password-prompt'." | |
181 :type 'regexp | |
182 :group 'eshell-mode) | |
183 | |
184 (defcustom eshell-skip-prompt-function nil | |
185 "*A function called from beginning of line to skip the prompt." | |
186 :type 'function | |
187 :group 'eshell-mode) | |
188 | |
189 (defcustom eshell-status-in-modeline t | |
190 "*If non-nil, let the user know a command is running in the modeline." | |
191 :type 'boolean | |
192 :group 'eshell-mode) | |
193 | |
194 (defvar eshell-non-interactive-p nil | |
195 "A variable which is non-nil when Eshell is not running interactively. | |
196 Modules should use this variable so that they don't clutter non-interactive | |
197 sessions, such as when using `eshell-command'.") | |
198 | |
199 (defvar eshell-first-time-p t | |
200 "A variable which is non-nil the first time Eshell is loaded.") | |
201 | |
202 ;; Internal Variables: | |
203 | |
204 ;; these are only set to `nil' initially for the sake of the | |
205 ;; byte-compiler, when compiling other files which `require' this one | |
206 (defvar eshell-mode nil) | |
207 (defvar eshell-mode-map nil) | |
208 (defvar eshell-command-running-string "--") | |
209 (defvar eshell-command-map nil) | |
210 (defvar eshell-command-prefix nil) | |
211 (defvar eshell-last-input-start nil) | |
212 (defvar eshell-last-input-end nil) | |
213 (defvar eshell-last-output-start nil) | |
214 (defvar eshell-last-output-block-begin nil) | |
215 (defvar eshell-last-output-end nil) | |
216 | |
217 (defvar eshell-currently-handling-window nil) | |
218 (defvar eshell-mode-syntax-table nil) | |
219 (defvar eshell-mode-abbrev-table nil) | |
220 | |
221 (define-abbrev-table 'eshell-mode-abbrev-table ()) | |
222 | |
223 (eval-when-compile | |
224 (unless (eshell-under-xemacs-p) | |
225 (defalias 'characterp 'ignore) | |
226 (defalias 'char-int 'ignore))) | |
227 | |
228 (if (not eshell-mode-syntax-table) | |
229 (let ((i 0)) | |
230 (setq eshell-mode-syntax-table (make-syntax-table)) | |
231 (while (< i ?0) | |
232 (modify-syntax-entry i "_ " eshell-mode-syntax-table) | |
233 (setq i (1+ i))) | |
234 (setq i (1+ ?9)) | |
235 (while (< i ?A) | |
236 (modify-syntax-entry i "_ " eshell-mode-syntax-table) | |
237 (setq i (1+ i))) | |
238 (setq i (1+ ?Z)) | |
239 (while (< i ?a) | |
240 (modify-syntax-entry i "_ " eshell-mode-syntax-table) | |
241 (setq i (1+ i))) | |
242 (setq i (1+ ?z)) | |
243 (while (< i 128) | |
244 (modify-syntax-entry i "_ " eshell-mode-syntax-table) | |
245 (setq i (1+ i))) | |
246 (modify-syntax-entry ? " " eshell-mode-syntax-table) | |
247 (modify-syntax-entry ?\t " " eshell-mode-syntax-table) | |
248 (modify-syntax-entry ?\f " " eshell-mode-syntax-table) | |
249 (modify-syntax-entry ?\n "> " eshell-mode-syntax-table) | |
250 ;; Give CR the same syntax as newline, for selective-display. | |
251 (modify-syntax-entry ?\^m "> " eshell-mode-syntax-table) | |
252 ;;; (modify-syntax-entry ?\; "< " eshell-mode-syntax-table) | |
253 (modify-syntax-entry ?` "' " eshell-mode-syntax-table) | |
254 (modify-syntax-entry ?' "' " eshell-mode-syntax-table) | |
255 (modify-syntax-entry ?, "' " eshell-mode-syntax-table) | |
256 ;; Used to be singlequote; changed for flonums. | |
257 (modify-syntax-entry ?. "_ " eshell-mode-syntax-table) | |
258 (modify-syntax-entry ?- "_ " eshell-mode-syntax-table) | |
259 (modify-syntax-entry ?| ". " eshell-mode-syntax-table) | |
260 (modify-syntax-entry ?# "' " eshell-mode-syntax-table) | |
261 (modify-syntax-entry ?\" "\" " eshell-mode-syntax-table) | |
262 (modify-syntax-entry ?\\ "/ " eshell-mode-syntax-table) | |
263 (modify-syntax-entry ?\( "() " eshell-mode-syntax-table) | |
264 (modify-syntax-entry ?\) ")( " eshell-mode-syntax-table) | |
265 (modify-syntax-entry ?\{ "(} " eshell-mode-syntax-table) | |
266 (modify-syntax-entry ?\} "){ " eshell-mode-syntax-table) | |
267 (modify-syntax-entry ?\[ "(] " eshell-mode-syntax-table) | |
268 (modify-syntax-entry ?\] ")[ " eshell-mode-syntax-table) | |
269 ;; All non-word multibyte characters should be `symbol'. | |
270 (if (eshell-under-xemacs-p) | |
271 (map-char-table | |
272 (function | |
273 (lambda (key val) | |
274 (and (characterp key) | |
275 (>= (char-int key) 256) | |
276 (/= (char-syntax key) ?w) | |
277 (modify-syntax-entry key "_ " | |
278 eshell-mode-syntax-table)))) | |
279 (standard-syntax-table)) | |
280 (map-char-table | |
281 (function | |
282 (lambda (key val) | |
283 (and (>= key 256) | |
284 (/= (char-syntax key) ?w) | |
285 (modify-syntax-entry key "_ " | |
286 eshell-mode-syntax-table)))) | |
287 (standard-syntax-table))))) | |
288 | |
289 ;;; User Functions: | |
290 | |
291 ;;;###autoload | |
292 (defun eshell-mode () | |
293 "Emacs shell interactive mode. | |
294 | |
295 \\{eshell-mode-map}" | |
296 (kill-all-local-variables) | |
297 | |
298 (setq major-mode 'eshell-mode) | |
299 (setq mode-name "EShell") | |
300 (set (make-local-variable 'eshell-mode) t) | |
301 | |
302 (make-local-variable 'eshell-mode-map) | |
303 (setq eshell-mode-map (make-sparse-keymap)) | |
304 (use-local-map eshell-mode-map) | |
305 | |
306 (when eshell-status-in-modeline | |
307 (make-local-variable 'eshell-command-running-string) | |
29872
b95f1796ee1a
(eshell-mode): Use eshell-copy-list instead
Gerd Moellmann <gerd@gnu.org>
parents:
29870
diff
changeset
|
308 (let ((fmt (eshell-copy-list mode-line-format))) |
29870 | 309 (make-local-variable 'mode-line-format) |
310 (setq mode-line-format fmt)) | |
311 (let ((modeline (memq 'mode-line-modified mode-line-format))) | |
312 (if modeline | |
313 (setcar modeline 'eshell-command-running-string)))) | |
314 | |
315 (define-key eshell-mode-map [return] 'eshell-send-input) | |
316 (define-key eshell-mode-map [(control ?m)] 'eshell-send-input) | |
317 (define-key eshell-mode-map [(control ?j)] 'eshell-send-input) | |
318 (define-key eshell-mode-map [(meta return)] 'eshell-queue-input) | |
319 (define-key eshell-mode-map [(meta control ?m)] 'eshell-queue-input) | |
320 (define-key eshell-mode-map [(meta control ?l)] 'eshell-show-output) | |
321 | |
322 (set (make-local-variable 'eshell-command-prefix) | |
323 (make-symbol "eshell-command-prefix")) | |
324 (fset eshell-command-prefix (make-sparse-keymap)) | |
325 (set (make-local-variable 'eshell-command-map) | |
326 (symbol-function eshell-command-prefix)) | |
327 (define-key eshell-mode-map [(control ?c)] eshell-command-prefix) | |
328 | |
31240 | 329 ;; without this, find-tag complains about read-only text being |
330 ;; modified | |
331 (if (eq (key-binding [(meta ?.)]) 'find-tag) | |
332 (define-key eshell-mode-map [(meta ?.)] 'eshell-find-tag)) | |
29870 | 333 (define-key eshell-command-map [(meta ?o)] 'eshell-mark-output) |
334 | |
335 (define-key eshell-command-map [(control ?a)] 'eshell-bol) | |
336 (define-key eshell-command-map [(control ?b)] 'eshell-backward-argument) | |
337 (define-key eshell-command-map [(control ?e)] 'eshell-show-maximum-output) | |
338 (define-key eshell-command-map [(control ?f)] 'eshell-forward-argument) | |
339 (define-key eshell-command-map [return] 'eshell-copy-old-input) | |
340 (define-key eshell-command-map [(control ?m)] 'eshell-copy-old-input) | |
341 (define-key eshell-command-map [(control ?o)] 'eshell-kill-output) | |
342 (define-key eshell-command-map [(control ?r)] 'eshell-show-output) | |
343 (define-key eshell-command-map [(control ?t)] 'eshell-truncate-buffer) | |
344 (define-key eshell-command-map [(control ?u)] 'eshell-kill-input) | |
345 (define-key eshell-command-map [(control ?w)] 'backward-kill-word) | |
31240 | 346 (define-key eshell-command-map [(control ?y)] 'eshell-repeat-argument) |
29870 | 347 |
348 (setq local-abbrev-table eshell-mode-abbrev-table) | |
349 (set-syntax-table eshell-mode-syntax-table) | |
350 | |
351 (set (make-local-variable 'dired-directory) default-directory) | |
352 (set (make-local-variable 'list-buffers-directory) | |
353 (expand-file-name default-directory)) | |
354 | |
355 ;; always set the tab width to 8 in Eshell buffers, since external | |
356 ;; commands which do their own formatting almost always expect this | |
357 (set (make-local-variable 'tab-width) 8) | |
358 | |
31240 | 359 ;; don't ever use auto-fill in Eshell buffers |
360 (setq auto-fill-function nil) | |
361 | |
29870 | 362 ;; always display everything from a return value |
363 (if (boundp 'print-length) | |
364 (set (make-local-variable 'print-length) nil)) | |
365 (if (boundp 'print-level) | |
366 (set (make-local-variable 'print-level) nil)) | |
367 | |
368 ;; set require-final-newline to nil; otherwise, all redirected | |
369 ;; output will end with a newline, whether or not the source | |
370 ;; indicated it! | |
371 (set (make-local-variable 'require-final-newline) nil) | |
372 | |
373 (set (make-local-variable 'max-lisp-eval-depth) | |
374 (max 3000 max-lisp-eval-depth)) | |
375 (set (make-local-variable 'max-specpdl-size) | |
376 (max 6000 max-lisp-eval-depth)) | |
377 | |
378 (set (make-local-variable 'eshell-last-input-start) (point-marker)) | |
379 (set (make-local-variable 'eshell-last-input-end) (point-marker)) | |
380 (set (make-local-variable 'eshell-last-output-start) (point-marker)) | |
381 (set (make-local-variable 'eshell-last-output-end) (point-marker)) | |
382 (set (make-local-variable 'eshell-last-output-block-begin) (point)) | |
383 | |
29872
b95f1796ee1a
(eshell-mode): Use eshell-copy-list instead
Gerd Moellmann <gerd@gnu.org>
parents:
29870
diff
changeset
|
384 (let ((modules-list (eshell-copy-list eshell-modules-list))) |
29870 | 385 (make-local-variable 'eshell-modules-list) |
386 (setq eshell-modules-list modules-list)) | |
387 | |
388 ;; load extension modules into memory. This will cause any global | |
389 ;; variables they define to be visible, since some of the core | |
390 ;; modules sometimes take advantage of their functionality if used. | |
391 (eshell-for module eshell-modules-list | |
392 (let ((module-fullname (symbol-name module)) | |
393 module-shortname) | |
394 (if (string-match "^eshell-\\(.*\\)" module-fullname) | |
395 (setq module-shortname | |
396 (concat "em-" (match-string 1 module-fullname)))) | |
397 (unless module-shortname | |
398 (error "Invalid Eshell module name: %s" module-fullname)) | |
399 (unless (featurep (intern module-shortname)) | |
400 (load module-shortname)))) | |
401 | |
402 (unless (file-exists-p eshell-directory-name) | |
403 (eshell-make-private-directory eshell-directory-name t)) | |
404 | |
405 ;; load core Eshell modules for this session | |
406 (eshell-for module (eshell-subgroups 'eshell) | |
407 (run-hooks (intern-soft (concat (symbol-name module) | |
408 "-load-hook")))) | |
409 | |
410 ;; load extension modules for this session | |
411 (eshell-for module eshell-modules-list | |
412 (let ((load-hook (intern-soft (concat (symbol-name module) | |
413 "-load-hook")))) | |
414 (if (and load-hook (boundp load-hook)) | |
415 (run-hooks load-hook)))) | |
416 | |
417 (when eshell-scroll-to-bottom-on-input | |
418 (make-local-hook 'pre-command-hook) | |
419 (add-hook 'pre-command-hook 'eshell-preinput-scroll-to-bottom t t)) | |
420 | |
421 (when eshell-scroll-show-maximum-output | |
422 (set (make-local-variable 'scroll-conservatively) 1000)) | |
423 | |
424 (when eshell-status-in-modeline | |
425 (make-local-hook 'eshell-pre-command-hook) | |
426 (add-hook 'eshell-pre-command-hook 'eshell-command-started nil t) | |
427 (make-local-hook 'eshell-post-command-hook) | |
428 (add-hook 'eshell-post-command-hook 'eshell-command-finished nil t)) | |
429 | |
430 (make-local-hook 'kill-buffer-hook) | |
431 (add-hook 'kill-buffer-hook | |
432 (function | |
433 (lambda () | |
434 (run-hooks 'eshell-exit-hook))) t t) | |
435 | |
436 (if eshell-first-time-p | |
437 (run-hooks 'eshell-first-time-mode-hook)) | |
438 (run-hooks 'eshell-mode-hook) | |
439 (run-hooks 'eshell-post-command-hook)) | |
440 | |
441 (put 'eshell-mode 'mode-class 'special) | |
442 | |
443 (eshell-deftest mode major-mode | |
444 "Major mode is correct" | |
445 (eq major-mode 'eshell-mode)) | |
446 | |
447 (eshell-deftest mode eshell-mode-variable | |
448 "`eshell-mode' is true" | |
449 (eq eshell-mode t)) | |
450 | |
451 (eshell-deftest var window-height | |
452 "LINES equals window height" | |
453 (eshell-command-result-p "= $LINES (window-height)" "t\n")) | |
454 | |
455 (defun eshell-command-started () | |
456 "Indicate in the modeline that a command has started." | |
457 (setq eshell-command-running-string "**") | |
458 (force-mode-line-update)) | |
459 | |
460 (defun eshell-command-finished () | |
461 "Indicate in the modeline that a command has finished." | |
462 (setq eshell-command-running-string "--") | |
463 (force-mode-line-update)) | |
464 | |
465 (eshell-deftest mode command-running-p | |
466 "Modeline shows no command running" | |
467 (or (eshell-under-xemacs-p) | |
468 (not eshell-status-in-modeline) | |
469 (and (memq 'eshell-command-running-string mode-line-format) | |
470 (equal eshell-command-running-string "--")))) | |
471 | |
472 ;;; Internal Functions: | |
473 | |
31240 | 474 (defun eshell-find-tag (&optional tagname next-p regexp-p) |
475 "A special version of `find-tag' that ignores read-onlyness." | |
476 (interactive) | |
31241 | 477 (require 'etags) |
31240 | 478 (let ((inhibit-read-only t) |
479 (no-default (eobp))) | |
480 (setq tagname (find-tag-interactive "Find tag: " no-default)) | |
481 (find-tag tagname next-p regexp-p))) | |
482 | |
29870 | 483 (defun eshell-move-argument (limit func property arg) |
484 "Move forward ARG arguments." | |
485 (catch 'eshell-incomplete | |
486 (eshell-parse-arguments (save-excursion (eshell-bol) (point)) | |
487 (line-end-position))) | |
31240 | 488 (let ((pos (save-excursion |
489 (funcall func 1) | |
490 (while (and (> arg 0) (/= (point) limit)) | |
491 (if (get-text-property (point) property) | |
492 (setq arg (1- arg))) | |
493 (if (> arg 0) | |
494 (funcall func 1))) | |
495 (point)))) | |
29870 | 496 (goto-char pos) |
497 (if (and (eq func 'forward-char) | |
498 (= (1+ pos) limit)) | |
499 (forward-char 1)))) | |
500 | |
501 (eshell-deftest arg forward-arg | |
502 "Move across command arguments" | |
503 (eshell-insert-command "echo $(+ 1 (- 4 3)) \"alpha beta\" file" 'ignore) | |
504 (let ((here (point)) begin valid) | |
505 (eshell-bol) | |
506 (setq begin (point)) | |
507 (eshell-forward-argument 4) | |
508 (setq valid (= here (point))) | |
509 (eshell-backward-argument 4) | |
510 (prog1 | |
511 (and valid (= begin (point))) | |
512 (eshell-bol) | |
513 (delete-region (point) (point-max))))) | |
514 | |
515 (defun eshell-forward-argument (&optional arg) | |
516 "Move forward ARG arguments." | |
517 (interactive "p") | |
518 (eshell-move-argument (point-max) 'forward-char 'arg-end arg)) | |
519 | |
520 (defun eshell-backward-argument (&optional arg) | |
521 "Move backward ARG arguments." | |
522 (interactive "p") | |
523 (eshell-move-argument (point-min) 'backward-char 'arg-begin arg)) | |
524 | |
31240 | 525 (defun eshell-repeat-argument (&optional arg) |
526 (interactive "p") | |
527 (let ((begin (save-excursion | |
528 (eshell-backward-argument arg) | |
529 (point)))) | |
530 (kill-ring-save begin (point)) | |
531 (yank))) | |
532 | |
29870 | 533 (defun eshell-bol () |
534 "Goes to the beginning of line, then skips past the prompt, if any." | |
535 (interactive) | |
536 (beginning-of-line) | |
537 (and eshell-skip-prompt-function | |
538 (funcall eshell-skip-prompt-function))) | |
539 | |
540 (defsubst eshell-push-command-mark () | |
541 "Push a mark at the end of the last input text." | |
542 (push-mark (1- eshell-last-input-end) t)) | |
543 | |
544 (custom-add-option 'eshell-pre-command-hook 'eshell-push-command-mark) | |
545 | |
546 (defsubst eshell-goto-input-start () | |
547 "Goto the start of the last command input. | |
548 Putting this function on `eshell-pre-command-hook' will mimic Plan 9's | |
549 9term behavior." | |
550 (goto-char eshell-last-input-start)) | |
551 | |
552 (custom-add-option 'eshell-pre-command-hook 'eshell-push-command-mark) | |
553 | |
554 (defsubst eshell-interactive-print (string) | |
555 "Print STRING to the eshell display buffer." | |
556 (eshell-output-filter nil string)) | |
557 | |
558 (defsubst eshell-begin-on-new-line () | |
559 "This function outputs a newline if not at beginning of line." | |
560 (save-excursion | |
561 (goto-char eshell-last-output-end) | |
562 (or (bolp) | |
563 (eshell-interactive-print "\n")))) | |
564 | |
565 (defsubst eshell-reset (&optional no-hooks) | |
566 "Output a prompt on a new line, aborting any current input. | |
567 If NO-HOOKS is non-nil, then `eshell-post-command-hook' won't be run." | |
568 (goto-char (point-max)) | |
569 (setq eshell-last-input-start (point-marker) | |
570 eshell-last-input-end (point-marker) | |
571 eshell-last-output-start (point-marker) | |
572 eshell-last-output-block-begin (point) | |
573 eshell-last-output-end (point-marker)) | |
574 (eshell-begin-on-new-line) | |
575 (unless no-hooks | |
576 (run-hooks 'eshell-post-command-hook) | |
577 (goto-char (point-max)))) | |
578 | |
579 (defun eshell-parse-command-input (beg end &optional args) | |
580 "Parse the command input from BEG to END. | |
581 The difference is that `eshell-parse-command' expects a complete | |
582 command string (and will error if it doesn't get one), whereas this | |
583 function will inform the caller whether more input is required. | |
584 | |
585 If nil is returned, more input is necessary (probably because a | |
586 multi-line input string wasn't terminated properly). Otherwise, it | |
587 will return the parsed command." | |
31240 | 588 (let (delim command) |
589 (if (setq delim | |
590 (catch 'eshell-incomplete | |
591 (ignore | |
592 (setq command (eshell-parse-command (cons beg end) | |
593 args t))))) | |
594 (ignore | |
595 (message "Expecting completion of delimeter %c ..." | |
596 (if (listp delim) | |
597 (car delim) | |
598 delim))) | |
29870 | 599 command))) |
600 | |
601 (defun eshell-update-markers (pmark) | |
602 "Update the input and output markers relative to point and PMARK." | |
603 (set-marker eshell-last-input-start pmark) | |
604 (set-marker eshell-last-input-end (point)) | |
605 (set-marker eshell-last-output-end (point))) | |
606 | |
607 (defun eshell-queue-input (&optional use-region) | |
608 "Queue the current input text for execution by Eshell. | |
609 Particularly, don't send the text to the current process, even if it's | |
610 waiting for input." | |
611 (interactive "P") | |
612 (eshell-send-input use-region t)) | |
613 | |
614 (eshell-deftest mode queue-input | |
615 "Queue command input" | |
616 (eshell-insert-command "sleep 2") | |
617 (eshell-insert-command "echo alpha" 'eshell-queue-input) | |
618 (let ((count 10)) | |
619 (while (and eshell-current-command | |
620 (> count 0)) | |
621 (sit-for 1 0) | |
622 (setq count (1- count)))) | |
623 (eshell-match-result "alpha\n")) | |
624 | |
625 (defun eshell-send-input (&optional use-region queue-p no-newline) | |
626 "Send the input received to Eshell for parsing and processing.. | |
627 After `eshell-last-output-end', sends all text from that marker to | |
628 point as input. Before that marker, calls `eshell-get-old-input' to | |
629 retrieve old input, copies it to the end of the buffer, and sends it. | |
630 | |
631 If USE-REGION is non-nil, the current region (between point and mark) | |
632 will be used as input. | |
633 | |
634 If QUEUE-P is non-nil, input will be queued until the next prompt, | |
635 rather than sent to the currently active process. If no process, the | |
636 input is processed immediately. | |
637 | |
638 If NO-NEWLINE is non-nil, the input is sent without an implied final | |
639 newline." | |
640 (interactive "P") | |
641 ;; Note that the input string does not include its terminal newline. | |
642 (let ((proc-running-p (and (eshell-interactive-process) | |
643 (not queue-p))) | |
644 (inhibit-point-motion-hooks t) | |
645 after-change-functions) | |
646 (unless (and proc-running-p | |
647 (not (eq (process-status | |
648 (eshell-interactive-process)) 'run))) | |
649 (if (or proc-running-p | |
650 (>= (point) eshell-last-output-end)) | |
651 (goto-char (point-max)) | |
652 (let ((copy (eshell-get-old-input use-region))) | |
653 (goto-char eshell-last-output-end) | |
654 (insert-and-inherit copy))) | |
655 (unless no-newline | |
656 (insert-before-markers-and-inherit ?\n)) | |
657 (if proc-running-p | |
658 (progn | |
659 (eshell-update-markers eshell-last-output-end) | |
660 (if (= eshell-last-input-start eshell-last-input-end) | |
661 (unless no-newline | |
662 (process-send-string (eshell-interactive-process) "\n")) | |
663 (process-send-region (eshell-interactive-process) | |
664 eshell-last-input-start | |
665 eshell-last-input-end))) | |
666 (if (= eshell-last-output-end (point)) | |
667 (run-hooks 'eshell-post-command-hook) | |
668 (let (input) | |
669 (eshell-condition-case err | |
670 (progn | |
671 (setq input (buffer-substring-no-properties | |
672 eshell-last-output-end (1- (point)))) | |
673 (run-hook-with-args 'eshell-expand-input-functions | |
674 eshell-last-output-end (1- (point))) | |
675 (let ((cmd (eshell-parse-command-input | |
676 eshell-last-output-end (1- (point))))) | |
677 (when cmd | |
678 (eshell-update-markers eshell-last-output-end) | |
679 (setq input (buffer-substring-no-properties | |
680 eshell-last-input-start | |
681 (1- eshell-last-input-end))) | |
682 (run-hooks 'eshell-input-filter-functions) | |
683 (and (catch 'eshell-terminal | |
684 (ignore | |
685 (eshell-eval-command cmd input))) | |
686 (eshell-life-is-too-much))))) | |
687 (quit | |
688 (eshell-reset t) | |
689 (run-hooks 'eshell-post-command-hook) | |
690 (signal 'quit nil)) | |
691 (error | |
692 (eshell-reset t) | |
693 (eshell-interactive-print | |
694 (concat (error-message-string err) "\n")) | |
695 (run-hooks 'eshell-post-command-hook) | |
696 (insert-and-inherit input))))))))) | |
697 | |
698 (eshell-deftest proc send-to-subprocess | |
699 "Send input to a subprocess" | |
700 ;; jww (1999-12-06): what about when bc is unavailable? | |
701 (if (not (eshell-search-path "bc")) | |
702 t | |
703 (eshell-insert-command "bc") | |
704 (eshell-insert-command "1 + 2") | |
705 (sit-for 1 0) | |
706 (forward-line -1) | |
707 (prog1 | |
708 (looking-at "3\n") | |
709 (eshell-insert-command "quit") | |
710 (sit-for 1 0)))) | |
711 | |
712 (defsubst eshell-kill-new () | |
713 "Add the last input text to the kill ring." | |
714 (kill-ring-save eshell-last-input-start eshell-last-input-end)) | |
715 | |
716 (custom-add-option 'eshell-input-filter-functions 'eshell-kill-new) | |
717 | |
718 (defun eshell-output-filter (process string) | |
719 "Send the output from PROCESS (STRING) to the interactive display. | |
720 This is done after all necessary filtering has been done." | |
721 (let ((oprocbuf (if process (process-buffer process) | |
722 (current-buffer))) | |
723 (inhibit-point-motion-hooks t) | |
724 after-change-functions) | |
725 (let ((functions eshell-preoutput-filter-functions)) | |
726 (while (and functions string) | |
727 (setq string (funcall (car functions) string)) | |
728 (setq functions (cdr functions)))) | |
729 (if (and string oprocbuf (buffer-name oprocbuf)) | |
730 (let ((obuf (current-buffer)) | |
731 opoint obeg oend) | |
732 (set-buffer oprocbuf) | |
733 (setq opoint (point)) | |
734 (setq obeg (point-min)) | |
735 (setq oend (point-max)) | |
736 (let ((buffer-read-only nil) | |
737 (nchars (length string)) | |
738 (ostart nil)) | |
739 (widen) | |
740 (goto-char eshell-last-output-end) | |
741 (setq ostart (point)) | |
742 (if (<= (point) opoint) | |
743 (setq opoint (+ opoint nchars))) | |
744 (if (< (point) obeg) | |
745 (setq obeg (+ obeg nchars))) | |
746 (if (<= (point) oend) | |
747 (setq oend (+ oend nchars))) | |
748 (insert-before-markers string) | |
749 (if (= (window-start (selected-window)) (point)) | |
750 (set-window-start (selected-window) | |
751 (- (point) nchars))) | |
752 (if (= (point) eshell-last-input-end) | |
753 (set-marker eshell-last-input-end | |
754 (- eshell-last-input-end nchars))) | |
755 (set-marker eshell-last-output-start ostart) | |
756 (set-marker eshell-last-output-end (point)) | |
757 (force-mode-line-update)) | |
758 (narrow-to-region obeg oend) | |
759 (goto-char opoint) | |
760 (eshell-run-output-filters) | |
761 (set-buffer obuf))))) | |
762 | |
763 (defun eshell-run-output-filters () | |
764 "Run the `eshell-output-filter-functions' on the current output." | |
765 (save-current-buffer | |
766 (run-hooks 'eshell-output-filter-functions)) | |
767 (setq eshell-last-output-block-begin | |
768 (marker-position eshell-last-output-end))) | |
769 | |
770 ;;; jww (1999-10-23): this needs testing | |
771 (defun eshell-preinput-scroll-to-bottom () | |
772 "Go to the end of buffer in all windows showing it. | |
773 Movement occurs if point in the selected window is not after the | |
774 process mark, and `this-command' is an insertion command. Insertion | |
775 commands recognised are `self-insert-command', `yank', and | |
776 `hilit-yank'. Depends on the value of | |
777 `eshell-scroll-to-bottom-on-input'. | |
778 | |
779 This function should be a pre-command hook." | |
780 (if (memq this-command '(self-insert-command yank hilit-yank)) | |
781 (let* ((selected (selected-window)) | |
782 (current (current-buffer)) | |
783 (scroll eshell-scroll-to-bottom-on-input)) | |
784 (if (< (point) eshell-last-output-end) | |
785 (if (eq scroll 'this) | |
786 (goto-char (point-max)) | |
787 (walk-windows | |
788 (function | |
789 (lambda (window) | |
790 (when (and (eq (window-buffer window) current) | |
791 (or (eq scroll t) (eq scroll 'all))) | |
792 (select-window window) | |
793 (goto-char (point-max)) | |
794 (select-window selected)))) | |
795 nil t)))))) | |
796 | |
797 ;;; jww (1999-10-23): this needs testing | |
798 (defun eshell-postoutput-scroll-to-bottom () | |
799 "Go to the end of buffer in all windows showing it. | |
800 Does not scroll if the current line is the last line in the buffer. | |
801 Depends on the value of `eshell-scroll-to-bottom-on-output' and | |
802 `eshell-scroll-show-maximum-output'. | |
803 | |
804 This function should be in the list `eshell-output-filter-functions'." | |
805 (let* ((selected (selected-window)) | |
806 (current (current-buffer)) | |
807 (scroll eshell-scroll-to-bottom-on-output)) | |
808 (unwind-protect | |
809 (walk-windows | |
810 (function | |
811 (lambda (window) | |
812 (if (eq (window-buffer window) current) | |
813 (progn | |
814 (select-window window) | |
815 (if (and (< (point) eshell-last-output-end) | |
816 (or (eq scroll t) (eq scroll 'all) | |
817 ;; Maybe user wants point to jump to end. | |
818 (and (eq scroll 'this) | |
819 (eq selected window)) | |
820 (and (eq scroll 'others) | |
821 (not (eq selected window))) | |
822 ;; If point was at the end, keep it at end. | |
823 (>= (point) eshell-last-output-start))) | |
824 (goto-char eshell-last-output-end)) | |
825 ;; Optionally scroll so that the text | |
826 ;; ends at the bottom of the window. | |
827 (if (and eshell-scroll-show-maximum-output | |
828 (>= (point) eshell-last-output-end)) | |
829 (save-excursion | |
830 (goto-char (point-max)) | |
831 (recenter -1))) | |
832 (select-window selected))))) | |
833 nil t) | |
834 (set-buffer current)))) | |
835 | |
836 (custom-add-option 'eshell-output-filter-functions | |
837 'eshell-postoutput-scroll-to-bottom) | |
838 | |
839 (defun eshell-beginning-of-input () | |
840 "Return the location of the start of the previous input." | |
841 eshell-last-input-start) | |
842 | |
843 (defun eshell-beginning-of-output () | |
844 "Return the location of the end of the previous output block." | |
845 eshell-last-input-end) | |
846 | |
847 (defun eshell-end-of-output () | |
848 "Return the location of the end of the previous output block." | |
849 (if (eshell-using-module 'eshell-prompt) | |
850 eshell-last-output-start | |
851 eshell-last-output-end)) | |
852 | |
853 (defun eshell-kill-output () | |
854 "Kill all output from interpreter since last input. | |
855 Does not delete the prompt." | |
856 (interactive) | |
857 (save-excursion | |
858 (goto-char (eshell-beginning-of-output)) | |
859 (insert "*** output flushed ***\n") | |
860 (delete-region (point) (eshell-end-of-output)))) | |
861 | |
862 (eshell-deftest io flush-output | |
863 "Flush previous output" | |
864 (eshell-insert-command "echo alpha") | |
865 (eshell-kill-output) | |
866 (and (eshell-match-result (regexp-quote "*** output flushed ***\n")) | |
867 (forward-line) | |
868 (= (point) eshell-last-output-start))) | |
869 | |
870 (defun eshell-show-output (&optional arg) | |
871 "Display start of this batch of interpreter output at top of window. | |
872 Sets mark to the value of point when this command is run. | |
873 With a prefix argument, narrows region to last command output." | |
874 (interactive "P") | |
875 (goto-char (eshell-beginning-of-output)) | |
876 (set-window-start (selected-window) | |
877 (save-excursion | |
878 (goto-char (eshell-beginning-of-input)) | |
879 (line-beginning-position))) | |
880 (if arg | |
881 (narrow-to-region (eshell-beginning-of-output) | |
882 (eshell-end-of-output))) | |
883 (eshell-end-of-output)) | |
884 | |
885 (defun eshell-mark-output (&optional arg) | |
886 "Display start of this batch of interpreter output at top of window. | |
887 Sets mark to the value of point when this command is run. | |
888 With a prefix argument, narrows region to last command output." | |
889 (interactive "P") | |
890 (push-mark (eshell-show-output arg))) | |
891 | |
892 (defun eshell-kill-input () | |
893 "Kill all text from last stuff output by interpreter to point." | |
894 (interactive) | |
895 (if (> (point) eshell-last-output-end) | |
896 (kill-region eshell-last-output-end (point)) | |
897 (let ((here (point))) | |
898 (eshell-bol) | |
899 (kill-region (point) here)))) | |
900 | |
901 (defun eshell-show-maximum-output () | |
902 "Put the end of the buffer at the bottom of the window." | |
903 (interactive) | |
904 (if (interactive-p) | |
905 (widen)) | |
906 (goto-char (point-max)) | |
907 (recenter -1)) | |
908 | |
909 (defun eshell-get-old-input (&optional use-current-region) | |
910 "Return the command input on the current line." | |
911 (if use-current-region | |
912 (buffer-substring (min (point) (mark)) | |
913 (max (point) (mark))) | |
914 (save-excursion | |
915 (beginning-of-line) | |
916 (and eshell-skip-prompt-function | |
917 (funcall eshell-skip-prompt-function)) | |
918 (let ((beg (point))) | |
919 (end-of-line) | |
920 (buffer-substring beg (point)))))) | |
921 | |
922 (defun eshell-copy-old-input () | |
923 "Insert after prompt old input at point as new input to be edited." | |
924 (interactive) | |
925 (let ((input (eshell-get-old-input))) | |
926 (goto-char eshell-last-output-end) | |
927 (insert-and-inherit input))) | |
928 | |
929 (eshell-deftest mode run-old-command | |
930 "Re-run an old command" | |
931 (eshell-insert-command "echo alpha") | |
932 (goto-char eshell-last-input-start) | |
933 (string= (eshell-get-old-input) "echo alpha")) | |
934 | |
935 (defun eshell/exit () | |
936 "Leave or kill the Eshell buffer, depending on `eshell-kill-on-exit'." | |
937 (throw 'eshell-terminal t)) | |
938 | |
939 (defun eshell-life-is-too-much () | |
940 "Kill the current buffer (or bury it). Good-bye Eshell." | |
941 (interactive) | |
942 (if (not eshell-kill-on-exit) | |
943 (bury-buffer) | |
944 (kill-buffer (current-buffer)))) | |
945 | |
946 (defun eshell-truncate-buffer () | |
947 "Truncate the buffer to `eshell-buffer-maximum-lines'. | |
948 This function could be on `eshell-output-filter-functions' or bound to | |
949 a key." | |
950 (interactive) | |
951 (save-excursion | |
952 (goto-char eshell-last-output-end) | |
953 (let ((lines (count-lines 1 (point))) | |
954 (inhibit-read-only t)) | |
955 (forward-line (- eshell-buffer-maximum-lines)) | |
956 (beginning-of-line) | |
957 (let ((pos (point))) | |
958 (if (bobp) | |
959 (if (interactive-p) | |
960 (error "Buffer too short to truncate")) | |
961 (delete-region (point-min) (point)) | |
962 (if (interactive-p) | |
963 (message "Truncated buffer from %d to %d lines (%.1fk freed)" | |
964 lines eshell-buffer-maximum-lines | |
965 (/ pos 1024.0)))))))) | |
966 | |
967 (custom-add-option 'eshell-output-filter-functions | |
968 'eshell-truncate-buffer) | |
969 | |
970 (defun send-invisible (str) | |
971 "Read a string without echoing. | |
972 Then send it to the process running in the current buffer." | |
973 (interactive "P") ; Defeat snooping via C-x ESC ESC | |
974 (let ((str (read-passwd | |
975 (format "Password: " | |
976 (process-name (eshell-interactive-process)))))) | |
977 (if (stringp str) | |
978 (process-send-string (eshell-interactive-process) | |
979 (concat str "\n")) | |
980 (message "Warning: text will be echoed")))) | |
981 | |
982 (defun eshell-watch-for-password-prompt () | |
983 "Prompt in the minibuffer for password and send without echoing. | |
984 This function uses `send-invisible' to read and send a password to the | |
985 buffer's process if STRING contains a password prompt defined by | |
986 `eshell-password-prompt-regexp'. | |
987 | |
988 This function could be in the list `eshell-output-filter-functions'." | |
989 (when (eshell-interactive-process) | |
990 (save-excursion | |
991 (goto-char eshell-last-output-block-begin) | |
992 (beginning-of-line) | |
993 (if (re-search-forward eshell-password-prompt-regexp | |
994 eshell-last-output-end t) | |
995 (send-invisible nil))))) | |
996 | |
997 (custom-add-option 'eshell-output-filter-functions | |
998 'eshell-watch-for-password-prompt) | |
999 | |
1000 (defun eshell-handle-control-codes () | |
1001 "Act properly when certain control codes are seen." | |
1002 (save-excursion | |
1003 (let ((orig (point))) | |
1004 (goto-char eshell-last-output-block-begin) | |
1005 (unless (eolp) | |
1006 (beginning-of-line)) | |
1007 (while (< (point) eshell-last-output-end) | |
1008 (let ((char (char-after))) | |
1009 (cond | |
1010 ((eq char ?\r) | |
1011 (if (< (1+ (point)) eshell-last-output-end) | |
1012 (if (memq (char-after (1+ (point))) | |
1013 '(?\n ?\r)) | |
1014 (delete-char 1) | |
1015 (let ((end (1+ (point)))) | |
1016 (beginning-of-line) | |
1017 (delete-region (point) end))) | |
1018 (add-text-properties (point) (1+ (point)) | |
1019 '(invisible t)) | |
1020 (forward-char))) | |
1021 ((eq char ?\a) | |
1022 (delete-char 1) | |
1023 (beep)) | |
1024 ((eq char ?\C-h) | |
1025 (delete-backward-char 1) | |
1026 (delete-char 1)) | |
1027 (t | |
1028 (forward-char)))))))) | |
1029 | |
1030 (custom-add-option 'eshell-output-filter-functions | |
1031 'eshell-handle-control-codes) | |
1032 | |
1033 ;;; Code: | |
1034 | |
1035 ;;; esh-mode.el ends here |