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