1 ;;; esh-mode.el --- user interface -*- lexical-binding:t -*-
3 ;; Copyright (C) 1999-2014 Free Software Foundation, Inc.
5 ;; Author: John Wiegley <johnw@gnu.org>
7 ;; This file is part of GNU Emacs.
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.
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.
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/>.
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.
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.
39 ;; Some of the other features of the command interaction mode are:
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.
46 ;; @ <C-c C-t> can be used to truncate the buffer if it grows too
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
53 ;; @ <C-c C-o> will delete the output from the last command.
55 ;; @ <C-c C-f> will move forward a complete shell argument.
57 ;; @ <C-c C-b> will move backward a complete shell argument.
69 (defgroup eshell-mode nil
70 "This module contains code for handling input from the user."
76 (defcustom eshell-mode-unload-hook nil
77 "A hook that gets run when `eshell-mode' is unloaded."
81 (defcustom eshell-mode-hook nil
82 "A hook that gets run when `eshell-mode' is entered."
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."
92 (defcustom eshell-exit-hook nil
93 "A hook that is run whenever `eshell' is exited.
94 This hook is only run if exiting actually kills the buffer."
95 :version
"24.1" ; removed eshell-query-kill-processes
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."
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'."
112 (defcustom eshell-send-direct-to-subprocesses nil
113 "If t, send any input immediately to a subprocess."
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
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.
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
))
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.
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
))
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.
153 See variable `eshell-scroll-to-bottom-on-output' and function
154 `eshell-postoutput-scroll-to-bottom'."
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'."
166 (defcustom eshell-output-filter-functions
167 '(eshell-postoutput-scroll-to-bottom
168 eshell-handle-control-codes
169 eshell-handle-ansi-color
170 eshell-watch-for-password-prompt
)
171 "Functions to call before output is displayed.
172 These functions are only called for output that is displayed
173 interactively, and not for output which is redirected."
177 (defcustom eshell-preoutput-filter-functions nil
178 "Functions to call before output is inserted into the buffer.
179 These functions get one argument, a string containing the text to be
180 inserted. They return the string as it should be inserted."
184 (defcustom eshell-password-prompt-regexp
185 (format "\\(%s\\).*:\\s *\\'" (regexp-opt password-word-equivalents
))
186 "Regexp matching prompts for passwords in the inferior process.
187 This is used by `eshell-watch-for-password-prompt'."
191 (defcustom eshell-skip-prompt-function nil
192 "A function called from beginning of line to skip the prompt."
193 :type
'(choice (const nil
) function
)
196 (define-obsolete-variable-alias 'eshell-status-in-modeline
197 'eshell-status-in-mode-line
"24.3")
199 (defcustom eshell-status-in-mode-line t
200 "If non-nil, let the user know a command is running in the mode line."
204 (defvar eshell-first-time-p t
205 "A variable which is non-nil the first time Eshell is loaded.")
207 ;; Internal Variables:
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
)
222 (defvar eshell-currently-handling-window nil
)
224 (define-abbrev-table 'eshell-mode-abbrev-table
())
226 (defvar eshell-mode-syntax-table
227 (let ((st (make-syntax-table))
230 (modify-syntax-entry i
"_ " st
)
234 (modify-syntax-entry i
"_ " st
)
238 (modify-syntax-entry i
"_ " st
)
242 (modify-syntax-entry i
"_ " st
)
244 (modify-syntax-entry ?
" " st
)
245 (modify-syntax-entry ?
\t " " st
)
246 (modify-syntax-entry ?
\f " " st
)
247 (modify-syntax-entry ?
\n "> " st
)
248 ;; Give CR the same syntax as newline, for selective-display.
249 (modify-syntax-entry ?\^m
"> " st
)
250 ;; (modify-syntax-entry ?\; "< " st)
251 (modify-syntax-entry ?
` "' " st
)
252 (modify-syntax-entry ?
' "' " st
)
253 (modify-syntax-entry ?
, "' " st
)
254 ;; Used to be singlequote; changed for flonums.
255 (modify-syntax-entry ?.
"_ " st
)
256 (modify-syntax-entry ?-
"_ " st
)
257 (modify-syntax-entry ?|
". " st
)
258 (modify-syntax-entry ?
# "' " st
)
259 (modify-syntax-entry ?
\" "\" " st
)
260 (modify-syntax-entry ?
\\ "/ " st
)
261 (modify-syntax-entry ?\
( "() " st
)
262 (modify-syntax-entry ?\
) ")( " st
)
263 (modify-syntax-entry ?\
{ "(} " st
)
264 (modify-syntax-entry ?\
} "){ " st
)
265 (modify-syntax-entry ?\
[ "(] " st
)
266 (modify-syntax-entry ?\
] ")[ " st
)
267 ;; All non-word multibyte characters should be `symbol'.
269 (if (featurep 'xemacs
)
271 (and (characterp key
)
272 (>= (char-int key
) 256)
273 (/= (char-syntax key
) ?w
)
274 (modify-syntax-entry key
"_ " st
)))
277 (and (>= (car key
) 128)
278 (/= (char-syntax (car key
)) ?w
))
280 (/= (char-syntax key
) ?w
)))
281 (modify-syntax-entry key
"_ " st
))))
282 (standard-syntax-table))
287 (defun eshell-kill-buffer-function ()
288 "Function added to `kill-buffer-hook' in Eshell buffers.
289 This runs the function `eshell-kill-processes-on-exit',
290 and the hook `eshell-exit-hook'."
291 ;; It's fine to run this unconditionally since it can be customized
292 ;; via the `eshell-kill-processes-on-exit' variable.
293 (and (fboundp 'eshell-query-kill-processes
)
294 (not (memq 'eshell-query-kill-processes eshell-exit-hook
))
295 (eshell-query-kill-processes))
296 (run-hooks 'eshell-exit-hook
))
299 (define-derived-mode eshell-mode fundamental-mode
"EShell"
300 "Emacs shell interactive mode."
301 (setq-local eshell-mode t
)
303 ;; FIXME: What the hell!?
304 (setq-local eshell-mode-map
(make-sparse-keymap))
305 (use-local-map eshell-mode-map
)
307 (when eshell-status-in-mode-line
308 (make-local-variable 'eshell-command-running-string
)
309 (let ((fmt (copy-sequence mode-line-format
)))
310 (setq-local mode-line-format fmt
))
311 (let ((mode-line-elt (memq 'mode-line-modified mode-line-format
)))
313 (setcar mode-line-elt
'eshell-command-running-string
))))
315 (define-key eshell-mode-map
"\r" 'eshell-send-input
)
316 (define-key eshell-mode-map
"\M-\r" 'eshell-queue-input
)
317 (define-key eshell-mode-map
[(meta control ?l
)] 'eshell-show-output
)
318 (define-key eshell-mode-map
[(control ?a
)] 'eshell-bol
)
320 (setq-local eshell-command-prefix
(make-symbol "eshell-command-prefix"))
321 (fset eshell-command-prefix
(make-sparse-keymap))
322 (setq-local eshell-command-map
(symbol-function eshell-command-prefix
))
323 (define-key eshell-mode-map
[(control ?c
)] eshell-command-prefix
)
325 ;; without this, find-tag complains about read-only text being
327 (if (eq (key-binding [(meta ?.
)]) 'find-tag
)
328 (define-key eshell-mode-map
[(meta ?.
)] 'eshell-find-tag
))
329 (define-key eshell-command-map
[(meta ?o
)] 'eshell-mark-output
)
330 (define-key eshell-command-map
[(meta ?d
)] 'eshell-toggle-direct-send
)
332 (define-key eshell-command-map
[(control ?a
)] 'eshell-bol
)
333 (define-key eshell-command-map
[(control ?b
)] 'eshell-backward-argument
)
334 (define-key eshell-command-map
[(control ?e
)] 'eshell-show-maximum-output
)
335 (define-key eshell-command-map
[(control ?f
)] 'eshell-forward-argument
)
336 (define-key eshell-command-map
[return] 'eshell-copy-old-input)
337 (define-key eshell-command-map [(control ?m)] 'eshell-copy-old-input)
338 (define-key eshell-command-map [(control ?o)] 'eshell-kill-output)
339 (define-key eshell-command-map [(control ?r)] 'eshell-show-output)
340 (define-key eshell-command-map [(control ?t)] 'eshell-truncate-buffer)
341 (define-key eshell-command-map [(control ?u)] 'eshell-kill-input)
342 (define-key eshell-command-map [(control ?w)] 'backward-kill-word)
343 (define-key eshell-command-map [(control ?y)] 'eshell-repeat-argument)
345 (setq local-abbrev-table eshell-mode-abbrev-table)
347 (set (make-local-variable 'dired-directory) default-directory)
348 (set (make-local-variable 'list-buffers-directory)
349 (expand-file-name default-directory))
351 ;; always set the tab width to 8 in Eshell buffers, since external
352 ;; commands which do their own formatting almost always expect this
353 (set (make-local-variable 'tab-width) 8)
355 ;; don't ever use auto-fill in Eshell buffers
356 (setq auto-fill-function nil)
358 ;; always display everything from a return value
359 (if (boundp 'print-length)
360 (set (make-local-variable 'print-length) nil))
361 (if (boundp 'print-level)
362 (set (make-local-variable 'print-level) nil))
364 ;; set require-final-newline to nil; otherwise, all redirected
365 ;; output will end with a newline, whether or not the source
367 (set (make-local-variable 'require-final-newline) nil)
369 (set (make-local-variable 'max-lisp-eval-depth)
370 (max 3000 max-lisp-eval-depth))
371 (set (make-local-variable 'max-specpdl-size)
372 (max 6000 max-lisp-eval-depth))
374 (set (make-local-variable 'eshell-last-input-start) (point-marker))
375 (set (make-local-variable 'eshell-last-input-end) (point-marker))
376 (set (make-local-variable 'eshell-last-output-start) (point-marker))
377 (set (make-local-variable 'eshell-last-output-end) (point-marker))
378 (set (make-local-variable 'eshell-last-output-block-begin) (point))
380 (let ((modules-list (copy-sequence eshell-modules-list)))
381 (make-local-variable 'eshell-modules-list)
382 (setq eshell-modules-list modules-list))
384 ;; load extension modules into memory. This will cause any global
385 ;; variables they define to be visible, since some of the core
386 ;; modules sometimes take advantage of their functionality if used.
387 (dolist (module eshell-modules-list)
388 (let ((module-fullname (symbol-name module))
390 (if (string-match "^eshell-\\(.*\\)" module-fullname)
391 (setq module-shortname
392 (concat "em-" (match-string 1 module-fullname))))
393 (unless module-shortname
394 (error "Invalid Eshell module name: %s" module-fullname))
395 (unless (featurep (intern module-shortname))
396 (load module-shortname))))
398 (unless (file-exists-p eshell-directory-name)
399 (eshell-make-private-directory eshell-directory-name t))
401 ;; Load core Eshell modules, then extension modules, for this session.
402 (dolist (module (append (eshell-subgroups 'eshell) eshell-modules-list))
403 (let ((load-hook (intern-soft (format "%s-load-hook" module)))
404 (initfunc (intern-soft (format "%s-initialize" module))))
405 (when (and load-hook (boundp load-hook))
406 (if (memq initfunc (symbol-value load-hook)) (setq initfunc nil))
407 (run-hooks load-hook))
408 ;; So we don't need the -initialize functions on the hooks (b#5375).
409 (and initfunc (fboundp initfunc) (funcall initfunc))))
411 (if eshell-send-direct-to-subprocesses
412 (add-hook 'pre-command-hook 'eshell-intercept-commands t t))
414 (if eshell-scroll-to-bottom-on-input
415 (add-hook 'pre-command-hook 'eshell-preinput-scroll-to-bottom t t))
417 (when eshell-scroll-show-maximum-output
418 (set (make-local-variable 'scroll-conservatively) 1000))
420 (when eshell-status-in-mode-line
421 (add-hook 'eshell-pre-command-hook 'eshell-command-started nil t)
422 (add-hook 'eshell-post-command-hook 'eshell-command-finished nil t))
424 (add-hook 'kill-buffer-hook 'eshell-kill-buffer-function t t)
426 (if eshell-first-time-p
427 (run-hooks 'eshell-first-time-mode-hook))
428 (run-hooks 'eshell-post-command-hook))
430 (put 'eshell-mode 'mode-class 'special)
432 (defun eshell-command-started ()
433 "Indicate in the mode line that a command has started."
434 (setq eshell-command-running-string "**")
435 (force-mode-line-update))
437 (defun eshell-command-finished ()
438 "Indicate in the mode line that a command has finished."
439 (setq eshell-command-running-string "--")
440 (force-mode-line-update))
442 ;;; Internal Functions:
444 (defun eshell-toggle-direct-send ()
446 (if eshell-send-direct-to-subprocesses
448 (setq eshell-send-direct-to-subprocesses nil)
449 (remove-hook 'pre-command-hook 'eshell-intercept-commands t)
450 (message "Sending subprocess input on RET"))
451 (setq eshell-send-direct-to-subprocesses t)
452 (add-hook 'pre-command-hook 'eshell-intercept-commands t t)
453 (message "Sending subprocess input directly")))
455 (defun eshell-self-insert-command ()
458 (eshell-interactive-process)
459 (char-to-string (if (symbolp last-command-event)
460 (get last-command-event 'ascii-character)
461 last-command-event))))
463 (defun eshell-intercept-commands ()
464 (when (and (eshell-interactive-process)
465 (not (and (integerp last-input-event)
466 (memq last-input-event '(?\C-x ?\C-c)))))
467 (let ((possible-events (where-is-internal this-command))
468 (name (symbol-name this-command))
470 ;; Assume that any multikey combination which does NOT target an
471 ;; Eshell command, is a combo the user wants invoked rather than
472 ;; sent to the underlying subprocess.
473 (unless (and (> (length name) 7)
474 (equal (substring name 0 7) "eshell-"))
475 (while possible-events
476 (if (> (length (car possible-events)) 1)
477 (setq intercept nil possible-events nil)
478 (setq possible-events (cdr possible-events)))))
480 (setq this-command 'eshell-self-insert-command)))))
482 (declare-function find-tag-interactive "etags" (prompt &optional no-default))
484 (defun eshell-find-tag (&optional tagname next-p regexp-p)
485 "A special version of `find-tag' that ignores whether the text is read-only."
488 (let ((inhibit-read-only t)
490 (find-tag-default-function 'ignore))
491 (setq tagname (car (find-tag-interactive "Find tag: " no-default)))
492 (find-tag tagname next-p regexp-p)))
494 (defun eshell-move-argument (limit func property arg)
495 "Move forward ARG arguments."
496 (catch 'eshell-incomplete
497 (eshell-parse-arguments (save-excursion (eshell-bol) (point))
498 (line-end-position)))
499 (let ((pos (save-excursion
501 (while (and (> arg 0) (/= (point) limit))
502 (if (get-text-property (point) property)
508 (if (and (eq func 'forward-char)
512 (defun eshell-forward-argument (&optional arg)
513 "Move forward ARG arguments."
515 (eshell-move-argument (point-max) 'forward-char 'arg-end arg))
517 (defun eshell-backward-argument (&optional arg)
518 "Move backward ARG arguments."
520 (eshell-move-argument (point-min) 'backward-char 'arg-begin arg))
522 (defun eshell-repeat-argument (&optional arg)
524 (let ((begin (save-excursion
525 (eshell-backward-argument arg)
527 (kill-ring-save begin (point))
531 "Goes to the beginning of line, then skips past the prompt, if any."
534 (and eshell-skip-prompt-function
535 (funcall eshell-skip-prompt-function)))
537 (defsubst eshell-push-command-mark ()
538 "Push a mark at the end of the last input text."
539 (push-mark (1- eshell-last-input-end) t))
541 (custom-add-option 'eshell-pre-command-hook 'eshell-push-command-mark)
543 (defsubst eshell-goto-input-start ()
544 "Goto the start of the last command input.
545 Putting this function on `eshell-pre-command-hook' will mimic Plan 9's
547 (goto-char eshell-last-input-start))
549 (custom-add-option 'eshell-pre-command-hook 'eshell-push-command-mark)
551 (defsubst eshell-interactive-print (string)
552 "Print STRING to the eshell display buffer."
553 (eshell-output-filter nil string))
555 (defsubst eshell-begin-on-new-line ()
556 "This function outputs a newline if not at beginning of line."
558 (goto-char eshell-last-output-end)
560 (eshell-interactive-print "\n"))))
562 (defsubst eshell-reset (&optional no-hooks)
563 "Output a prompt on a new line, aborting any current input.
564 If NO-HOOKS is non-nil, then `eshell-post-command-hook' won't be run."
565 (goto-char (point-max))
566 (setq eshell-last-input-start (point-marker)
567 eshell-last-input-end (point-marker)
568 eshell-last-output-start (point-marker)
569 eshell-last-output-block-begin (point)
570 eshell-last-output-end (point-marker))
571 (eshell-begin-on-new-line)
573 (run-hooks 'eshell-post-command-hook)
574 (goto-char (point-max))))
576 (defun eshell-parse-command-input (beg end &optional args)
577 "Parse the command input from BEG to END.
578 The difference is that `eshell-parse-command' expects a complete
579 command string (and will error if it doesn't get one), whereas this
580 function will inform the caller whether more input is required.
582 If nil is returned, more input is necessary (probably because a
583 multi-line input string wasn't terminated properly). Otherwise, it
584 will return the parsed command."
587 (catch 'eshell-incomplete
589 (setq command (eshell-parse-command (cons beg end)
592 (message "Expecting completion of delimiter %c ..."
598 (defun eshell-update-markers (pmark)
599 "Update the input and output markers relative to point and PMARK."
600 (set-marker eshell-last-input-start pmark)
601 (set-marker eshell-last-input-end (point))
602 (set-marker eshell-last-output-end (point)))
604 (defun eshell-queue-input (&optional use-region)
605 "Queue the current input text for execution by Eshell.
606 Particularly, don't send the text to the current process, even if it's
609 (eshell-send-input use-region t))
611 (defun eshell-send-input (&optional use-region queue-p no-newline)
612 "Send the input received to Eshell for parsing and processing.
613 After `eshell-last-output-end', sends all text from that marker to
614 point as input. Before that marker, calls `eshell-get-old-input' to
615 retrieve old input, copies it to the end of the buffer, and sends it.
617 If USE-REGION is non-nil, the current region (between point and mark)
618 will be used as input.
620 If QUEUE-P is non-nil, input will be queued until the next prompt,
621 rather than sent to the currently active process. If no process, the
622 input is processed immediately.
624 If NO-NEWLINE is non-nil, the input is sent without an implied final
627 ;; Note that the input string does not include its terminal newline.
628 (let ((proc-running-p (and (eshell-interactive-process)
630 (inhibit-point-motion-hooks t)
631 after-change-functions)
632 (unless (and proc-running-p
633 (not (eq (process-status
634 (eshell-interactive-process)) 'run)))
635 (if (or proc-running-p
636 (>= (point) eshell-last-output-end))
637 (goto-char (point-max))
638 (let ((copy (eshell-get-old-input use-region)))
639 (goto-char eshell-last-output-end)
640 (insert-and-inherit copy)))
641 (unless (or no-newline
642 (and eshell-send-direct-to-subprocesses
644 (insert-before-markers-and-inherit ?\n))
647 (eshell-update-markers eshell-last-output-end)
648 (if (or eshell-send-direct-to-subprocesses
649 (= eshell-last-input-start eshell-last-input-end))
651 (process-send-string (eshell-interactive-process) "\n"))
652 (process-send-region (eshell-interactive-process)
653 eshell-last-input-start
654 eshell-last-input-end)))
655 (if (= eshell-last-output-end (point))
656 (run-hooks 'eshell-post-command-hook)
658 (eshell-condition-case err
660 (setq input (buffer-substring-no-properties
661 eshell-last-output-end (1- (point))))
662 (run-hook-with-args 'eshell-expand-input-functions
663 eshell-last-output-end (1- (point)))
664 (let ((cmd (eshell-parse-command-input
665 eshell-last-output-end (1- (point)))))
667 (eshell-update-markers eshell-last-output-end)
668 (setq input (buffer-substring-no-properties
669 eshell-last-input-start
670 (1- eshell-last-input-end)))
671 (run-hooks 'eshell-input-filter-functions)
672 (and (catch 'eshell-terminal
674 (if (eshell-invoke-directly cmd)
676 (eshell-eval-command cmd input))))
677 (eshell-life-is-too-much)))))
680 (run-hooks 'eshell-post-command-hook)
684 (eshell-interactive-print
685 (concat (error-message-string err) "\n"))
686 (run-hooks 'eshell-post-command-hook)
687 (insert-and-inherit input)))))))))
689 (defsubst eshell-kill-new ()
690 "Add the last input text to the kill ring."
691 (kill-ring-save eshell-last-input-start eshell-last-input-end))
693 (custom-add-option 'eshell-input-filter-functions 'eshell-kill-new)
695 (defun eshell-output-filter (process string)
696 "Send the output from PROCESS (STRING) to the interactive display.
697 This is done after all necessary filtering has been done."
698 (let ((oprocbuf (if process (process-buffer process)
700 (inhibit-point-motion-hooks t)
701 after-change-functions)
702 (let ((functions eshell-preoutput-filter-functions))
703 (while (and functions string)
704 (setq string (funcall (car functions) string))
705 (setq functions (cdr functions))))
706 (if (and string oprocbuf (buffer-name oprocbuf))
707 (let (opoint obeg oend)
708 (with-current-buffer oprocbuf
709 (setq opoint (point))
710 (setq obeg (point-min))
711 (setq oend (point-max))
712 (let ((buffer-read-only nil)
713 (nchars (length string))
716 (goto-char eshell-last-output-end)
717 (setq ostart (point))
718 (if (<= (point) opoint)
719 (setq opoint (+ opoint nchars)))
721 (setq obeg (+ obeg nchars)))
722 (if (<= (point) oend)
723 (setq oend (+ oend nchars)))
724 (insert-before-markers string)
725 (if (= (window-start) (point))
726 (set-window-start (selected-window)
728 (if (= (point) eshell-last-input-end)
729 (set-marker eshell-last-input-end
730 (- eshell-last-input-end nchars)))
731 (set-marker eshell-last-output-start ostart)
732 (set-marker eshell-last-output-end (point))
733 (force-mode-line-update))
734 (narrow-to-region obeg oend)
736 (eshell-run-output-filters))))))
738 (defun eshell-run-output-filters ()
739 "Run the `eshell-output-filter-functions' on the current output."
741 (run-hooks 'eshell-output-filter-functions))
742 (setq eshell-last-output-block-begin
743 (marker-position eshell-last-output-end)))
745 ;;; jww (1999-10-23): this needs testing
746 (defun eshell-preinput-scroll-to-bottom ()
747 "Go to the end of buffer in all windows showing it.
748 Movement occurs if point in the selected window is not after the
749 process mark, and `this-command' is an insertion command. Insertion
750 commands recognized are `self-insert-command', `yank', and
751 `hilit-yank'. Depends on the value of
752 `eshell-scroll-to-bottom-on-input'.
754 This function should be a pre-command hook."
755 (if (memq this-command '(self-insert-command yank hilit-yank))
756 (let* ((selected (selected-window))
757 (current (current-buffer))
758 (scroll eshell-scroll-to-bottom-on-input))
759 (if (< (point) eshell-last-output-end)
760 (if (eq scroll 'this)
761 (goto-char (point-max))
765 (when (and (eq (window-buffer window) current)
766 (or (eq scroll t) (eq scroll 'all)))
767 (select-window window)
768 (goto-char (point-max))
769 (select-window selected))))
772 ;;; jww (1999-10-23): this needs testing
773 (defun eshell-postoutput-scroll-to-bottom ()
774 "Go to the end of buffer in all windows showing it.
775 Does not scroll if the current line is the last line in the buffer.
776 Depends on the value of `eshell-scroll-to-bottom-on-output' and
777 `eshell-scroll-show-maximum-output'.
779 This function should be in the list `eshell-output-filter-functions'."
780 (let* ((selected (selected-window))
781 (current (current-buffer))
782 (scroll eshell-scroll-to-bottom-on-output))
787 (if (eq (window-buffer window) current)
789 (select-window window)
790 (if (and (< (point) eshell-last-output-end)
791 (or (eq scroll t) (eq scroll 'all)
792 ;; Maybe user wants point to jump to end.
793 (and (eq scroll 'this)
794 (eq selected window))
795 (and (eq scroll 'others)
796 (not (eq selected window)))
797 ;; If point was at the end, keep it at end.
798 (>= (point) eshell-last-output-start)))
799 (goto-char eshell-last-output-end))
800 ;; Optionally scroll so that the text
801 ;; ends at the bottom of the window.
802 (if (and eshell-scroll-show-maximum-output
803 (>= (point) eshell-last-output-end))
805 (goto-char (point-max))
807 (select-window selected)))))
809 (set-buffer current))))
811 (defun eshell-beginning-of-input ()
812 "Return the location of the start of the previous input."
813 eshell-last-input-start)
815 (defun eshell-beginning-of-output ()
816 "Return the location of the end of the previous output block."
817 eshell-last-input-end)
819 (defun eshell-end-of-output ()
820 "Return the location of the end of the previous output block."
821 (if (eshell-using-module 'eshell-prompt)
822 eshell-last-output-start
823 eshell-last-output-end))
825 (defun eshell-kill-output ()
826 "Kill all output from interpreter since last input.
827 Does not delete the prompt."
830 (goto-char (eshell-beginning-of-output))
831 (insert "*** output flushed ***\n")
832 (delete-region (point) (eshell-end-of-output))))
834 (defun eshell-show-output (&optional arg)
835 "Display start of this batch of interpreter output at top of window.
836 Sets mark to the value of point when this command is run.
837 With a prefix argument, narrows region to last command output."
839 (goto-char (eshell-beginning-of-output))
840 (set-window-start (selected-window)
842 (goto-char (eshell-beginning-of-input))
843 (line-beginning-position)))
845 (narrow-to-region (eshell-beginning-of-output)
846 (eshell-end-of-output)))
847 (eshell-end-of-output))
849 (defun eshell-mark-output (&optional arg)
850 "Display start of this batch of interpreter output at top of window.
851 Sets mark to the value of point when this command is run.
852 With a prefix argument, narrows region to last command output."
854 (push-mark (eshell-show-output arg)))
856 (defun eshell-kill-input ()
857 "Kill all text from last stuff output by interpreter to point."
859 (if (> (point) eshell-last-output-end)
860 (kill-region eshell-last-output-end (point))
861 (let ((here (point)))
863 (kill-region (point) here))))
865 (defun eshell-show-maximum-output (&optional interactive)
866 "Put the end of the buffer at the bottom of the window.
867 When run interactively, widen the buffer first."
871 (goto-char (point-max))
874 (defun eshell-get-old-input (&optional use-current-region)
875 "Return the command input on the current line."
876 (if use-current-region
877 (buffer-substring (min (point) (mark))
878 (max (point) (mark)))
881 (and eshell-skip-prompt-function
882 (funcall eshell-skip-prompt-function))
885 (buffer-substring beg (point))))))
887 (defun eshell-copy-old-input ()
888 "Insert after prompt old input at point as new input to be edited."
890 (let ((input (eshell-get-old-input)))
891 (goto-char eshell-last-output-end)
892 (insert-and-inherit input)))
894 (defun eshell/exit ()
895 "Leave or kill the Eshell buffer, depending on `eshell-kill-on-exit'."
896 (throw 'eshell-terminal t))
898 (defun eshell-life-is-too-much ()
899 "Kill the current buffer (or bury it). Good-bye Eshell."
901 (if (not eshell-kill-on-exit)
903 (kill-buffer (current-buffer))))
905 (defun eshell-truncate-buffer ()
906 "Truncate the buffer to `eshell-buffer-maximum-lines'.
907 This function could be on `eshell-output-filter-functions' or bound to
911 (goto-char eshell-last-output-end)
912 (let ((lines (count-lines 1 (point)))
913 (inhibit-read-only t))
914 (forward-line (- eshell-buffer-maximum-lines))
918 (if (called-interactively-p 'interactive)
919 (message "Buffer too short to truncate"))
920 (delete-region (point-min) (point))
921 (if (called-interactively-p 'interactive)
922 (message "Truncated buffer from %d to %d lines (%.1fk freed)"
923 lines eshell-buffer-maximum-lines
924 (/ pos 1024.0))))))))
926 (custom-add-option 'eshell-output-filter-functions
927 'eshell-truncate-buffer)
929 (defun eshell-send-invisible ()
930 "Read a string without echoing.
931 Then send it to the process running in the current buffer."
932 (interactive) ; Don't pass str as argument, to avoid snooping via C-x ESC ESC
933 (let ((str (read-passwd
934 (format "%s Password: "
935 (process-name (eshell-interactive-process))))))
937 (process-send-string (eshell-interactive-process)
939 (message "Warning: text will be echoed"))))
941 (defun eshell-watch-for-password-prompt ()
942 "Prompt in the minibuffer for password and send without echoing.
943 This function uses `eshell-send-invisible' to read and send a password to the
944 buffer's process if STRING contains a password prompt defined by
945 `eshell-password-prompt-regexp'.
947 This function could be in the list `eshell-output-filter-functions'."
948 (when (eshell-interactive-process)
950 (let ((case-fold-search t))
951 (goto-char eshell-last-output-block-begin)
953 (if (re-search-forward eshell-password-prompt-regexp
954 eshell-last-output-end t)
955 (eshell-send-invisible))))))
957 (custom-add-option 'eshell-output-filter-functions
958 'eshell-watch-for-password-prompt)
960 (defun eshell-handle-control-codes ()
961 "Act properly when certain control codes are seen."
963 (goto-char eshell-last-output-block-begin)
966 (while (< (point) eshell-last-output-end)
967 (let ((char (char-after)))
970 (if (< (1+ (point)) eshell-last-output-end)
971 (if (memq (char-after (1+ (point)))
974 (let ((end (1+ (point))))
976 (delete-region (point) end)))
977 (add-text-properties (point) (1+ (point))
984 (delete-region (1- (point)) (1+ (point))))
988 (custom-add-option 'eshell-output-filter-functions
989 'eshell-handle-control-codes)
991 (autoload 'ansi-color-apply-on-region "ansi-color")
993 (defun eshell-handle-ansi-color ()
994 "Handle ANSI color codes."
995 (ansi-color-apply-on-region eshell-last-output-start
996 eshell-last-output-end))
998 (custom-add-option 'eshell-output-filter-functions
999 'eshell-handle-ansi-color)
1001 ;;; esh-mode.el ends here