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