]> code.delx.au - gnu-emacs/blob - lisp/eshell/esh-mode.el
Merge from emacs--devo--0
[gnu-emacs] / lisp / eshell / esh-mode.el
1 ;;; esh-mode.el --- user interface
2
3 ;; Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007 Free Software Foundation, Inc.
5
6 ;; Author: John Wiegley <johnw@gnu.org>
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 3, or (at your option)
13 ;; any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
23 ;; Boston, MA 02110-1301, USA.
24
25 ;;; Commentary:
26
27 ;; Basically, Eshell is used just like shell mode (<M-x shell>). The
28 ;; keystrokes for navigating the buffer, and accessing the command
29 ;; history, are identical. Unlike shell mode, however, Eshell mode's
30 ;; governing process is Emacs itself. With shell mode, an inferior
31 ;; shell process is executed that communicates with Emacs via comint
32 ;; -- a mode for handling sub-process interaction. Eshell mode, on
33 ;; the other hand, is a truly native Emacs shell. No subprocess are
34 ;; invoked except the ones requested by the user at the prompt.
35 ;;
36 ;; After entering a command, use <RET> to invoke it ([Command
37 ;; invocation]) . If there is a command on disk, it will be executed
38 ;; as in a normal shell. If there is no command by that name on disk,
39 ;; but a Lisp function with that name is defined, the Lisp function
40 ;; will be called, using the arguments passed on the command line.
41 ;;
42 ;; Some of the other features of the command interaction mode are:
43 ;;
44 ;; @ <M-RET> can be used to accumulate further commands while a
45 ;; command is currently running. Since all input is passed to the
46 ;; subprocess being executed, there is no automatic input queueing
47 ;; as there is with other shells.
48 ;;
49 ;; @ <C-c C-t> can be used to truncate the buffer if it grows too
50 ;; large.
51 ;;
52 ;; @ <C-c C-r> will move point to the beginning of the output of the
53 ;; last command. With a prefix argument, it will narrow to view
54 ;; only that output.
55 ;;
56 ;; @ <C-c C-o> will delete the output from the last command.
57 ;;
58 ;; @ <C-c C-f> will move forward a complete shell argument.
59 ;;
60 ;; @ <C-c C-b> will move backward a complete shell argument.
61
62 (provide 'esh-mode)
63
64 (eval-when-compile (require 'esh-util))
65 (require 'esh-module)
66 (require 'esh-cmd)
67 (require 'esh-io)
68 (require 'esh-var)
69
70 (defgroup eshell-mode nil
71 "This module contains code for handling input from the user."
72 :tag "User interface"
73 :group 'eshell)
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-postoutput-scroll-to-bottom
168 eshell-handle-control-codes
169 eshell-watch-for-password-prompt)
170 "*Functions to call before output is displayed.
171 These functions are only called for output that is displayed
172 interactively, and not for output which is redirected."
173 :type 'hook
174 :group 'eshell-mode)
175
176 (defcustom eshell-preoutput-filter-functions nil
177 "*Functions to call before output is inserted into the buffer.
178 These functions get one argument, a string containing the text to be
179 inserted. They return the string as it should be inserted."
180 :type 'hook
181 :group 'eshell-mode)
182
183 (defcustom eshell-password-prompt-regexp
184 "[Pp]ass\\(word\\|phrase\\).*:\\s *\\'"
185 "*Regexp matching prompts for passwords in the inferior process.
186 This is used by `eshell-watch-for-password-prompt'."
187 :type 'regexp
188 :group 'eshell-mode)
189
190 (defcustom eshell-skip-prompt-function nil
191 "*A function called from beginning of line to skip the prompt."
192 :type '(choice (const nil) function)
193 :group 'eshell-mode)
194
195 (defcustom eshell-status-in-modeline t
196 "*If non-nil, let the user know a command is running in the modeline."
197 :type 'boolean
198 :group 'eshell-mode)
199
200 (defvar eshell-first-time-p t
201 "A variable which is non-nil the first time Eshell is loaded.")
202
203 ;; Internal Variables:
204
205 ;; these are only set to `nil' initially for the sake of the
206 ;; byte-compiler, when compiling other files which `require' this one
207 (defvar eshell-mode nil)
208 (defvar eshell-mode-map nil)
209 (defvar eshell-command-running-string "--")
210 (defvar eshell-command-map nil)
211 (defvar eshell-command-prefix nil)
212 (defvar eshell-last-input-start nil)
213 (defvar eshell-last-input-end nil)
214 (defvar eshell-last-output-start nil)
215 (defvar eshell-last-output-block-begin nil)
216 (defvar eshell-last-output-end nil)
217
218 (defvar eshell-currently-handling-window nil)
219 (defvar eshell-mode-syntax-table nil)
220 (defvar eshell-mode-abbrev-table nil)
221
222 (define-abbrev-table 'eshell-mode-abbrev-table ())
223
224 (if (not eshell-mode-syntax-table)
225 (let ((i 0))
226 (setq eshell-mode-syntax-table (make-syntax-table))
227 (while (< i ?0)
228 (modify-syntax-entry i "_ " eshell-mode-syntax-table)
229 (setq i (1+ i)))
230 (setq i (1+ ?9))
231 (while (< i ?A)
232 (modify-syntax-entry i "_ " eshell-mode-syntax-table)
233 (setq i (1+ i)))
234 (setq i (1+ ?Z))
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 128)
240 (modify-syntax-entry i "_ " eshell-mode-syntax-table)
241 (setq i (1+ i)))
242 (modify-syntax-entry ? " " eshell-mode-syntax-table)
243 (modify-syntax-entry ?\t " " eshell-mode-syntax-table)
244 (modify-syntax-entry ?\f " " eshell-mode-syntax-table)
245 (modify-syntax-entry ?\n "> " eshell-mode-syntax-table)
246 ;; Give CR the same syntax as newline, for selective-display.
247 (modify-syntax-entry ?\^m "> " eshell-mode-syntax-table)
248 ;;; (modify-syntax-entry ?\; "< " eshell-mode-syntax-table)
249 (modify-syntax-entry ?` "' " eshell-mode-syntax-table)
250 (modify-syntax-entry ?' "' " eshell-mode-syntax-table)
251 (modify-syntax-entry ?, "' " eshell-mode-syntax-table)
252 ;; Used to be singlequote; changed for flonums.
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 (modify-syntax-entry ?# "' " 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 (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 ;; All non-word multibyte characters should be `symbol'.
266 (if (featurep 'xemacs)
267 (map-char-table
268 (function
269 (lambda (key val)
270 (and (characterp key)
271 (>= (char-int key) 256)
272 (/= (char-syntax key) ?w)
273 (modify-syntax-entry key "_ "
274 eshell-mode-syntax-table))))
275 (standard-syntax-table))
276 (map-char-table
277 (function
278 (lambda (key val)
279 (and (if (consp key)
280 (and (>= (car key) 128)
281 (/= (char-syntax (car key)) ?w))
282 (and (>= key 256)
283 (/= (char-syntax key) ?w)))
284 (modify-syntax-entry key "_ "
285 eshell-mode-syntax-table))))
286 (standard-syntax-table)))))
287
288 ;;; User Functions:
289
290 ;;;###autoload
291 (defun eshell-mode ()
292 "Emacs shell interactive mode.
293
294 \\{eshell-mode-map}"
295 (kill-all-local-variables)
296
297 (setq major-mode 'eshell-mode)
298 (setq mode-name "EShell")
299 (set (make-local-variable 'eshell-mode) t)
300
301 (make-local-variable 'eshell-mode-map)
302 (setq eshell-mode-map (make-sparse-keymap))
303 (use-local-map eshell-mode-map)
304
305 (when eshell-status-in-modeline
306 (make-local-variable 'eshell-command-running-string)
307 (let ((fmt (copy-sequence mode-line-format)))
308 (make-local-variable 'mode-line-format)
309 (setq mode-line-format fmt))
310 (let ((modeline (memq 'mode-line-modified mode-line-format)))
311 (if modeline
312 (setcar modeline 'eshell-command-running-string))))
313
314 (define-key eshell-mode-map [return] 'eshell-send-input)
315 (define-key eshell-mode-map [(control ?m)] 'eshell-send-input)
316 (define-key eshell-mode-map [(control ?j)] 'eshell-send-input)
317 (define-key eshell-mode-map [(meta return)] 'eshell-queue-input)
318 (define-key eshell-mode-map [(meta control ?m)] 'eshell-queue-input)
319 (define-key eshell-mode-map [(meta control ?l)] 'eshell-show-output)
320
321 (set (make-local-variable 'eshell-command-prefix)
322 (make-symbol "eshell-command-prefix"))
323 (fset eshell-command-prefix (make-sparse-keymap))
324 (set (make-local-variable 'eshell-command-map)
325 (symbol-function eshell-command-prefix))
326 (define-key eshell-mode-map [(control ?c)] eshell-command-prefix)
327
328 ;; without this, find-tag complains about read-only text being
329 ;; modified
330 (if (eq (key-binding [(meta ?.)]) 'find-tag)
331 (define-key eshell-mode-map [(meta ?.)] 'eshell-find-tag))
332 (define-key eshell-command-map [(meta ?o)] 'eshell-mark-output)
333 (define-key eshell-command-map [(meta ?d)] 'eshell-toggle-direct-send)
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)
346 (define-key eshell-command-map [(control ?y)] 'eshell-repeat-argument)
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
359 ;; don't ever use auto-fill in Eshell buffers
360 (setq auto-fill-function nil)
361
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
384 (let ((modules-list (copy-sequence eshell-modules-list)))
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 (if eshell-send-direct-to-subprocesses
418 (add-hook 'pre-command-hook 'eshell-intercept-commands t t))
419
420 (if eshell-scroll-to-bottom-on-input
421 (add-hook 'pre-command-hook 'eshell-preinput-scroll-to-bottom t t))
422
423 (when eshell-scroll-show-maximum-output
424 (set (make-local-variable 'scroll-conservatively) 1000))
425
426 (when eshell-status-in-modeline
427 (add-hook 'eshell-pre-command-hook 'eshell-command-started nil t)
428 (add-hook 'eshell-post-command-hook 'eshell-command-finished nil t))
429
430 (add-hook 'kill-buffer-hook
431 (function
432 (lambda ()
433 (run-hooks 'eshell-exit-hook))) t t)
434
435 (if eshell-first-time-p
436 (run-hooks 'eshell-first-time-mode-hook))
437 (run-mode-hooks 'eshell-mode-hook)
438 (run-hooks 'eshell-post-command-hook))
439
440 (put 'eshell-mode 'mode-class 'special)
441
442 (eshell-deftest mode major-mode
443 "Major mode is correct"
444 (eq major-mode 'eshell-mode))
445
446 (eshell-deftest mode eshell-mode-variable
447 "`eshell-mode' is true"
448 (eq eshell-mode t))
449
450 (eshell-deftest var window-height
451 "LINES equals window height"
452 (let ((eshell-stringify-t t))
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 (featurep 'xemacs)
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
474 (defun eshell-toggle-direct-send ()
475 (interactive)
476 (if eshell-send-direct-to-subprocesses
477 (progn
478 (setq eshell-send-direct-to-subprocesses nil)
479 (remove-hook 'pre-command-hook 'eshell-intercept-commands t)
480 (message "Sending subprocess input on RET"))
481 (setq eshell-send-direct-to-subprocesses t)
482 (add-hook 'pre-command-hook 'eshell-intercept-commands t t)
483 (message "Sending subprocess input directly")))
484
485 (defun eshell-self-insert-command (N)
486 (interactive "i")
487 (process-send-string
488 (eshell-interactive-process)
489 (char-to-string (if (symbolp last-command-char)
490 (get last-command-char 'ascii-character)
491 last-command-char))))
492
493 (defun eshell-intercept-commands ()
494 (when (and (eshell-interactive-process)
495 (not (and (integerp last-input-event)
496 (memq last-input-event '(?\C-x ?\C-c)))))
497 (let ((possible-events (where-is-internal this-command))
498 (name (symbol-name this-command))
499 (intercept t))
500 ;; Assume that any multikey combination which does NOT target an
501 ;; Eshell command, is a combo the user wants invoked rather than
502 ;; sent to the underlying subprocess.
503 (unless (and (> (length name) 7)
504 (equal (substring name 0 7) "eshell-"))
505 (while possible-events
506 (if (> (length (car possible-events)) 1)
507 (setq intercept nil possible-events nil)
508 (setq possible-events (cdr possible-events)))))
509 (if intercept
510 (setq this-command 'eshell-self-insert-command)))))
511
512 (defun eshell-find-tag (&optional tagname next-p regexp-p)
513 "A special version of `find-tag' that ignores read-onlyness."
514 (interactive)
515 (require 'etags)
516 (let ((inhibit-read-only t)
517 (no-default (eobp))
518 (find-tag-default-function 'ignore))
519 (with-no-warnings
520 (setq tagname (car (find-tag-interactive "Find tag: "))))
521 (find-tag tagname next-p regexp-p)))
522
523 (defun eshell-move-argument (limit func property arg)
524 "Move forward ARG arguments."
525 (catch 'eshell-incomplete
526 (eshell-parse-arguments (save-excursion (eshell-bol) (point))
527 (line-end-position)))
528 (let ((pos (save-excursion
529 (funcall func 1)
530 (while (and (> arg 0) (/= (point) limit))
531 (if (get-text-property (point) property)
532 (setq arg (1- arg)))
533 (if (> arg 0)
534 (funcall func 1)))
535 (point))))
536 (goto-char pos)
537 (if (and (eq func 'forward-char)
538 (= (1+ pos) limit))
539 (forward-char 1))))
540
541 (eshell-deftest arg forward-arg
542 "Move across command arguments"
543 (eshell-insert-command "echo $(+ 1 (- 4 3)) \"alpha beta\" file" 'ignore)
544 (let ((here (point)) begin valid)
545 (eshell-bol)
546 (setq begin (point))
547 (eshell-forward-argument 4)
548 (setq valid (= here (point)))
549 (eshell-backward-argument 4)
550 (prog1
551 (and valid (= begin (point)))
552 (eshell-bol)
553 (delete-region (point) (point-max)))))
554
555 (defun eshell-forward-argument (&optional arg)
556 "Move forward ARG arguments."
557 (interactive "p")
558 (eshell-move-argument (point-max) 'forward-char 'arg-end arg))
559
560 (defun eshell-backward-argument (&optional arg)
561 "Move backward ARG arguments."
562 (interactive "p")
563 (eshell-move-argument (point-min) 'backward-char 'arg-begin arg))
564
565 (defun eshell-repeat-argument (&optional arg)
566 (interactive "p")
567 (let ((begin (save-excursion
568 (eshell-backward-argument arg)
569 (point))))
570 (kill-ring-save begin (point))
571 (yank)))
572
573 (defun eshell-bol ()
574 "Goes to the beginning of line, then skips past the prompt, if any."
575 (interactive)
576 (beginning-of-line)
577 (and eshell-skip-prompt-function
578 (funcall eshell-skip-prompt-function)))
579
580 (defsubst eshell-push-command-mark ()
581 "Push a mark at the end of the last input text."
582 (push-mark (1- eshell-last-input-end) t))
583
584 (custom-add-option 'eshell-pre-command-hook 'eshell-push-command-mark)
585
586 (defsubst eshell-goto-input-start ()
587 "Goto the start of the last command input.
588 Putting this function on `eshell-pre-command-hook' will mimic Plan 9's
589 9term behavior."
590 (goto-char eshell-last-input-start))
591
592 (custom-add-option 'eshell-pre-command-hook 'eshell-push-command-mark)
593
594 (defsubst eshell-interactive-print (string)
595 "Print STRING to the eshell display buffer."
596 (eshell-output-filter nil string))
597
598 (defsubst eshell-begin-on-new-line ()
599 "This function outputs a newline if not at beginning of line."
600 (save-excursion
601 (goto-char eshell-last-output-end)
602 (or (bolp)
603 (eshell-interactive-print "\n"))))
604
605 (defsubst eshell-reset (&optional no-hooks)
606 "Output a prompt on a new line, aborting any current input.
607 If NO-HOOKS is non-nil, then `eshell-post-command-hook' won't be run."
608 (goto-char (point-max))
609 (setq eshell-last-input-start (point-marker)
610 eshell-last-input-end (point-marker)
611 eshell-last-output-start (point-marker)
612 eshell-last-output-block-begin (point)
613 eshell-last-output-end (point-marker))
614 (eshell-begin-on-new-line)
615 (unless no-hooks
616 (run-hooks 'eshell-post-command-hook)
617 (goto-char (point-max))))
618
619 (defun eshell-parse-command-input (beg end &optional args)
620 "Parse the command input from BEG to END.
621 The difference is that `eshell-parse-command' expects a complete
622 command string (and will error if it doesn't get one), whereas this
623 function will inform the caller whether more input is required.
624
625 If nil is returned, more input is necessary (probably because a
626 multi-line input string wasn't terminated properly). Otherwise, it
627 will return the parsed command."
628 (let (delim command)
629 (if (setq delim
630 (catch 'eshell-incomplete
631 (ignore
632 (setq command (eshell-parse-command (cons beg end)
633 args t)))))
634 (ignore
635 (message "Expecting completion of delimeter %c ..."
636 (if (listp delim)
637 (car delim)
638 delim)))
639 command)))
640
641 (defun eshell-update-markers (pmark)
642 "Update the input and output markers relative to point and PMARK."
643 (set-marker eshell-last-input-start pmark)
644 (set-marker eshell-last-input-end (point))
645 (set-marker eshell-last-output-end (point)))
646
647 (defun eshell-queue-input (&optional use-region)
648 "Queue the current input text for execution by Eshell.
649 Particularly, don't send the text to the current process, even if it's
650 waiting for input."
651 (interactive "P")
652 (eshell-send-input use-region t))
653
654 (eshell-deftest mode queue-input
655 "Queue command input"
656 (eshell-insert-command "sleep 2")
657 (eshell-insert-command "echo alpha" 'eshell-queue-input)
658 (let ((count 10))
659 (while (and eshell-current-command
660 (> count 0))
661 (sit-for 1 0)
662 (setq count (1- count))))
663 (eshell-match-result "alpha\n"))
664
665 (defun eshell-send-input (&optional use-region queue-p no-newline)
666 "Send the input received to Eshell for parsing and processing.
667 After `eshell-last-output-end', sends all text from that marker to
668 point as input. Before that marker, calls `eshell-get-old-input' to
669 retrieve old input, copies it to the end of the buffer, and sends it.
670
671 If USE-REGION is non-nil, the current region (between point and mark)
672 will be used as input.
673
674 If QUEUE-P is non-nil, input will be queued until the next prompt,
675 rather than sent to the currently active process. If no process, the
676 input is processed immediately.
677
678 If NO-NEWLINE is non-nil, the input is sent without an implied final
679 newline."
680 (interactive "P")
681 ;; Note that the input string does not include its terminal newline.
682 (let ((proc-running-p (and (eshell-interactive-process)
683 (not queue-p)))
684 (inhibit-point-motion-hooks t)
685 after-change-functions)
686 (unless (and proc-running-p
687 (not (eq (process-status
688 (eshell-interactive-process)) 'run)))
689 (if (or proc-running-p
690 (>= (point) eshell-last-output-end))
691 (goto-char (point-max))
692 (let ((copy (eshell-get-old-input use-region)))
693 (goto-char eshell-last-output-end)
694 (insert-and-inherit copy)))
695 (unless (or no-newline
696 (and eshell-send-direct-to-subprocesses
697 proc-running-p))
698 (insert-before-markers-and-inherit ?\n))
699 (if proc-running-p
700 (progn
701 (eshell-update-markers eshell-last-output-end)
702 (if (or eshell-send-direct-to-subprocesses
703 (= eshell-last-input-start eshell-last-input-end))
704 (unless no-newline
705 (process-send-string (eshell-interactive-process) "\n"))
706 (process-send-region (eshell-interactive-process)
707 eshell-last-input-start
708 eshell-last-input-end)))
709 (if (= eshell-last-output-end (point))
710 (run-hooks 'eshell-post-command-hook)
711 (let (input)
712 (eshell-condition-case err
713 (progn
714 (setq input (buffer-substring-no-properties
715 eshell-last-output-end (1- (point))))
716 (run-hook-with-args 'eshell-expand-input-functions
717 eshell-last-output-end (1- (point)))
718 (let ((cmd (eshell-parse-command-input
719 eshell-last-output-end (1- (point)))))
720 (when cmd
721 (eshell-update-markers eshell-last-output-end)
722 (setq input (buffer-substring-no-properties
723 eshell-last-input-start
724 (1- eshell-last-input-end)))
725 (run-hooks 'eshell-input-filter-functions)
726 (and (catch 'eshell-terminal
727 (ignore
728 (if (eshell-invoke-directly cmd input)
729 (eval cmd)
730 (eshell-eval-command cmd input))))
731 (eshell-life-is-too-much)))))
732 (quit
733 (eshell-reset t)
734 (run-hooks 'eshell-post-command-hook)
735 (signal 'quit nil))
736 (error
737 (eshell-reset t)
738 (eshell-interactive-print
739 (concat (error-message-string err) "\n"))
740 (run-hooks 'eshell-post-command-hook)
741 (insert-and-inherit input)))))))))
742
743 ; (eshell-deftest proc send-to-subprocess
744 ; "Send input to a subprocess"
745 ; ;; jww (1999-12-06): what about when bc is unavailable?
746 ; (if (not (eshell-search-path "bc"))
747 ; t
748 ; (eshell-insert-command "bc")
749 ; (eshell-insert-command "1 + 2")
750 ; (sit-for 1 0)
751 ; (forward-line -1)
752 ; (prog1
753 ; (looking-at "3\n")
754 ; (eshell-insert-command "quit")
755 ; (sit-for 1 0))))
756
757 (defsubst eshell-kill-new ()
758 "Add the last input text to the kill ring."
759 (kill-ring-save eshell-last-input-start eshell-last-input-end))
760
761 (custom-add-option 'eshell-input-filter-functions 'eshell-kill-new)
762
763 (defun eshell-output-filter (process string)
764 "Send the output from PROCESS (STRING) to the interactive display.
765 This is done after all necessary filtering has been done."
766 (let ((oprocbuf (if process (process-buffer process)
767 (current-buffer)))
768 (inhibit-point-motion-hooks t)
769 after-change-functions)
770 (let ((functions eshell-preoutput-filter-functions))
771 (while (and functions string)
772 (setq string (funcall (car functions) string))
773 (setq functions (cdr functions))))
774 (if (and string oprocbuf (buffer-name oprocbuf))
775 (let (opoint obeg oend)
776 (with-current-buffer oprocbuf
777 (setq opoint (point))
778 (setq obeg (point-min))
779 (setq oend (point-max))
780 (let ((buffer-read-only nil)
781 (nchars (length string))
782 (ostart nil))
783 (widen)
784 (goto-char eshell-last-output-end)
785 (setq ostart (point))
786 (if (<= (point) opoint)
787 (setq opoint (+ opoint nchars)))
788 (if (< (point) obeg)
789 (setq obeg (+ obeg nchars)))
790 (if (<= (point) oend)
791 (setq oend (+ oend nchars)))
792 (insert-before-markers string)
793 (if (= (window-start (selected-window)) (point))
794 (set-window-start (selected-window)
795 (- (point) nchars)))
796 (if (= (point) eshell-last-input-end)
797 (set-marker eshell-last-input-end
798 (- eshell-last-input-end nchars)))
799 (set-marker eshell-last-output-start ostart)
800 (set-marker eshell-last-output-end (point))
801 (force-mode-line-update))
802 (narrow-to-region obeg oend)
803 (goto-char opoint)
804 (eshell-run-output-filters))))))
805
806 (defun eshell-run-output-filters ()
807 "Run the `eshell-output-filter-functions' on the current output."
808 (save-current-buffer
809 (run-hooks 'eshell-output-filter-functions))
810 (setq eshell-last-output-block-begin
811 (marker-position eshell-last-output-end)))
812
813 ;;; jww (1999-10-23): this needs testing
814 (defun eshell-preinput-scroll-to-bottom ()
815 "Go to the end of buffer in all windows showing it.
816 Movement occurs if point in the selected window is not after the
817 process mark, and `this-command' is an insertion command. Insertion
818 commands recognized are `self-insert-command', `yank', and
819 `hilit-yank'. Depends on the value of
820 `eshell-scroll-to-bottom-on-input'.
821
822 This function should be a pre-command hook."
823 (if (memq this-command '(self-insert-command yank hilit-yank))
824 (let* ((selected (selected-window))
825 (current (current-buffer))
826 (scroll eshell-scroll-to-bottom-on-input))
827 (if (< (point) eshell-last-output-end)
828 (if (eq scroll 'this)
829 (goto-char (point-max))
830 (walk-windows
831 (function
832 (lambda (window)
833 (when (and (eq (window-buffer window) current)
834 (or (eq scroll t) (eq scroll 'all)))
835 (select-window window)
836 (goto-char (point-max))
837 (select-window selected))))
838 nil t))))))
839
840 ;;; jww (1999-10-23): this needs testing
841 (defun eshell-postoutput-scroll-to-bottom ()
842 "Go to the end of buffer in all windows showing it.
843 Does not scroll if the current line is the last line in the buffer.
844 Depends on the value of `eshell-scroll-to-bottom-on-output' and
845 `eshell-scroll-show-maximum-output'.
846
847 This function should be in the list `eshell-output-filter-functions'."
848 (let* ((selected (selected-window))
849 (current (current-buffer))
850 (scroll eshell-scroll-to-bottom-on-output))
851 (unwind-protect
852 (walk-windows
853 (function
854 (lambda (window)
855 (if (eq (window-buffer window) current)
856 (progn
857 (select-window window)
858 (if (and (< (point) eshell-last-output-end)
859 (or (eq scroll t) (eq scroll 'all)
860 ;; Maybe user wants point to jump to end.
861 (and (eq scroll 'this)
862 (eq selected window))
863 (and (eq scroll 'others)
864 (not (eq selected window)))
865 ;; If point was at the end, keep it at end.
866 (>= (point) eshell-last-output-start)))
867 (goto-char eshell-last-output-end))
868 ;; Optionally scroll so that the text
869 ;; ends at the bottom of the window.
870 (if (and eshell-scroll-show-maximum-output
871 (>= (point) eshell-last-output-end))
872 (save-excursion
873 (goto-char (point-max))
874 (recenter -1)))
875 (select-window selected)))))
876 nil t)
877 (set-buffer current))))
878
879 (defun eshell-beginning-of-input ()
880 "Return the location of the start of the previous input."
881 eshell-last-input-start)
882
883 (defun eshell-beginning-of-output ()
884 "Return the location of the end of the previous output block."
885 eshell-last-input-end)
886
887 (defun eshell-end-of-output ()
888 "Return the location of the end of the previous output block."
889 (if (eshell-using-module 'eshell-prompt)
890 eshell-last-output-start
891 eshell-last-output-end))
892
893 (defun eshell-kill-output ()
894 "Kill all output from interpreter since last input.
895 Does not delete the prompt."
896 (interactive)
897 (save-excursion
898 (goto-char (eshell-beginning-of-output))
899 (insert "*** output flushed ***\n")
900 (delete-region (point) (eshell-end-of-output))))
901
902 (eshell-deftest io flush-output
903 "Flush previous output"
904 (eshell-insert-command "echo alpha")
905 (eshell-kill-output)
906 (and (eshell-match-result (regexp-quote "*** output flushed ***\n"))
907 (forward-line)
908 (= (point) eshell-last-output-start)))
909
910 (defun eshell-show-output (&optional arg)
911 "Display start of this batch of interpreter output at top of window.
912 Sets mark to the value of point when this command is run.
913 With a prefix argument, narrows region to last command output."
914 (interactive "P")
915 (goto-char (eshell-beginning-of-output))
916 (set-window-start (selected-window)
917 (save-excursion
918 (goto-char (eshell-beginning-of-input))
919 (line-beginning-position)))
920 (if arg
921 (narrow-to-region (eshell-beginning-of-output)
922 (eshell-end-of-output)))
923 (eshell-end-of-output))
924
925 (defun eshell-mark-output (&optional arg)
926 "Display start of this batch of interpreter output at top of window.
927 Sets mark to the value of point when this command is run.
928 With a prefix argument, narrows region to last command output."
929 (interactive "P")
930 (push-mark (eshell-show-output arg)))
931
932 (defun eshell-kill-input ()
933 "Kill all text from last stuff output by interpreter to point."
934 (interactive)
935 (if (> (point) eshell-last-output-end)
936 (kill-region eshell-last-output-end (point))
937 (let ((here (point)))
938 (eshell-bol)
939 (kill-region (point) here))))
940
941 (defun eshell-show-maximum-output (&optional interactive)
942 "Put the end of the buffer at the bottom of the window.
943 When run interactively, widen the buffer first."
944 (interactive "p")
945 (if interactive
946 (widen))
947 (goto-char (point-max))
948 (recenter -1))
949
950 (defun eshell-get-old-input (&optional use-current-region)
951 "Return the command input on the current line."
952 (if use-current-region
953 (buffer-substring (min (point) (mark))
954 (max (point) (mark)))
955 (save-excursion
956 (beginning-of-line)
957 (and eshell-skip-prompt-function
958 (funcall eshell-skip-prompt-function))
959 (let ((beg (point)))
960 (end-of-line)
961 (buffer-substring beg (point))))))
962
963 (defun eshell-copy-old-input ()
964 "Insert after prompt old input at point as new input to be edited."
965 (interactive)
966 (let ((input (eshell-get-old-input)))
967 (goto-char eshell-last-output-end)
968 (insert-and-inherit input)))
969
970 (eshell-deftest mode run-old-command
971 "Re-run an old command"
972 (eshell-insert-command "echo alpha")
973 (goto-char eshell-last-input-start)
974 (string= (eshell-get-old-input) "echo alpha"))
975
976 (defun eshell/exit ()
977 "Leave or kill the Eshell buffer, depending on `eshell-kill-on-exit'."
978 (throw 'eshell-terminal t))
979
980 (defun eshell-life-is-too-much ()
981 "Kill the current buffer (or bury it). Good-bye Eshell."
982 (interactive)
983 (if (not eshell-kill-on-exit)
984 (bury-buffer)
985 (kill-buffer (current-buffer))))
986
987 (defun eshell-truncate-buffer ()
988 "Truncate the buffer to `eshell-buffer-maximum-lines'.
989 This function could be on `eshell-output-filter-functions' or bound to
990 a key."
991 (interactive)
992 (save-excursion
993 (goto-char eshell-last-output-end)
994 (let ((lines (count-lines 1 (point)))
995 (inhibit-read-only t))
996 (forward-line (- eshell-buffer-maximum-lines))
997 (beginning-of-line)
998 (let ((pos (point)))
999 (if (bobp)
1000 (if (interactive-p)
1001 (message "Buffer too short to truncate"))
1002 (delete-region (point-min) (point))
1003 (if (interactive-p)
1004 (message "Truncated buffer from %d to %d lines (%.1fk freed)"
1005 lines eshell-buffer-maximum-lines
1006 (/ pos 1024.0))))))))
1007
1008 (custom-add-option 'eshell-output-filter-functions
1009 'eshell-truncate-buffer)
1010
1011 (defun eshell-send-invisible (str)
1012 "Read a string without echoing.
1013 Then send it to the process running in the current buffer."
1014 (interactive "P") ; Defeat snooping via C-x ESC ESC
1015 (let ((str (read-passwd
1016 (format "%s Password: "
1017 (process-name (eshell-interactive-process))))))
1018 (if (stringp str)
1019 (process-send-string (eshell-interactive-process)
1020 (concat str "\n"))
1021 (message "Warning: text will be echoed"))))
1022
1023 (defun eshell-watch-for-password-prompt ()
1024 "Prompt in the minibuffer for password and send without echoing.
1025 This function uses `eshell-send-invisible' to read and send a password to the
1026 buffer's process if STRING contains a password prompt defined by
1027 `eshell-password-prompt-regexp'.
1028
1029 This function could be in the list `eshell-output-filter-functions'."
1030 (when (eshell-interactive-process)
1031 (save-excursion
1032 (goto-char eshell-last-output-block-begin)
1033 (beginning-of-line)
1034 (if (re-search-forward eshell-password-prompt-regexp
1035 eshell-last-output-end t)
1036 (eshell-send-invisible nil)))))
1037
1038 (custom-add-option 'eshell-output-filter-functions
1039 'eshell-watch-for-password-prompt)
1040
1041 (defun eshell-handle-control-codes ()
1042 "Act properly when certain control codes are seen."
1043 (save-excursion
1044 (let ((orig (point)))
1045 (goto-char eshell-last-output-block-begin)
1046 (unless (eolp)
1047 (beginning-of-line))
1048 (while (< (point) eshell-last-output-end)
1049 (let ((char (char-after)))
1050 (cond
1051 ((eq char ?\r)
1052 (if (< (1+ (point)) eshell-last-output-end)
1053 (if (memq (char-after (1+ (point)))
1054 '(?\n ?\r))
1055 (delete-char 1)
1056 (let ((end (1+ (point))))
1057 (beginning-of-line)
1058 (delete-region (point) end)))
1059 (add-text-properties (point) (1+ (point))
1060 '(invisible t))
1061 (forward-char)))
1062 ((eq char ?\a)
1063 (delete-char 1)
1064 (beep))
1065 ((eq char ?\C-h)
1066 (delete-backward-char 1)
1067 (delete-char 1))
1068 (t
1069 (forward-char))))))))
1070
1071 (custom-add-option 'eshell-output-filter-functions
1072 'eshell-handle-control-codes)
1073
1074 (defun eshell-handle-ansi-color ()
1075 "Handle ANSI color codes."
1076 (eval-and-compile (require 'ansi-color))
1077 (ansi-color-apply-on-region eshell-last-output-start
1078 eshell-last-output-end))
1079
1080 (custom-add-option 'eshell-output-filter-functions
1081 'eshell-handle-ansi-color)
1082
1083 ;;; Code:
1084
1085 ;;; arch-tag: ec65bc2b-da14-4547-81d3-a32af3a4dc57
1086 ;;; esh-mode.el ends here