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