1 ;;; esh-mode --- user interface
3 ;; Copyright (C) 1999, 2000 Free Software Foundation
5 ;; This file is part of GNU Emacs.
7 ;; GNU Emacs is free software; you can redistribute it and/or modify
8 ;; it under the terms of the GNU General Public License as published by
9 ;; the Free Software Foundation; either version 2, or (at your option)
12 ;; GNU Emacs is distributed in the hope that it will be useful,
13 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ;; GNU General Public License for more details.
17 ;; You should have received a copy of the GNU General Public License
18 ;; along with GNU Emacs; see the file COPYING. If not, write to the
19 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 ;; Boston, MA 02111-1307, USA.
24 (eval-when-compile (require 'esh-maint))
26 (defgroup eshell-mode nil
27 "This module contains code for handling input from the user."
33 ;; Basically, Eshell is used just like shell mode (<M-x shell>). The
34 ;; keystrokes for navigating the buffer, and accessing the command
35 ;; history, are identical. Unlike shell mode, however, Eshell mode's
36 ;; governing process is Emacs itself. With shell mode, an inferior
37 ;; shell process is executed that communicates with Emacs via comint
38 ;; -- a mode for handling sub-process interaction. Eshell mode, on
39 ;; the other hand, is a truly native Emacs shell. No subprocess are
40 ;; invoked except the ones requested by the user at the prompt.
42 ;; After entering a command, use <RET> to invoke it ([Command
43 ;; invocation]) . If there is a command on disk, it will be executed
44 ;; as in a normal shell. If there is no command by that name on disk,
45 ;; but a Lisp function with that name is defined, the Lisp function
46 ;; will be called, using the arguments passed on the command line.
48 ;; Some of the other features of the command interaction mode are:
50 ;; @ <M-RET> can be used to accumulate further commands while a
51 ;; command is currently running. Since all input is passed to the
52 ;; subprocess being executed, there is no automatic input queueing
53 ;; as there is with other shells.
55 ;; @ <C-c C-t> can be used to truncate the buffer if it grows too
58 ;; @ <C-c C-r> will move point to the beginning of the output of the
59 ;; last command. With a prefix argument, it will narrow to view
62 ;; @ <C-c C-o> will delete the output from the last command.
64 ;; @ <C-c C-f> will move forward a complete shell argument.
66 ;; @ <C-c C-b> will move backward a complete shell argument.
75 (defcustom eshell-mode-unload-hook nil
76 "*A hook that gets run when `eshell-mode' is unloaded."
80 (defcustom eshell-mode-hook nil
81 "*A hook that gets run when `eshell-mode' is entered."
85 (defcustom eshell-first-time-mode-hook nil
86 "*A hook that gets run the first time `eshell-mode' is entered.
87 That is to say, the first time during an Emacs session."
91 (defcustom eshell-exit-hook '(eshell-query-kill-processes)
92 "*A hook that is run whenever `eshell' is exited.
93 This hook is only run if exiting actually kills the buffer."
97 (defcustom eshell-kill-on-exit t
98 "*If non-nil, kill the Eshell buffer on the `exit' command.
99 Otherwise, the buffer will simply be buried."
103 (defcustom eshell-input-filter-functions nil
104 "*Functions to call before input is processed.
105 The input is contained in the region from `eshell-last-input-start' to
106 `eshell-last-input-end'."
110 (defcustom eshell-send-direct-to-subprocesses nil
111 "*If t, send any input immediately to a subprocess."
115 (defcustom eshell-expand-input-functions nil
116 "*Functions to call before input is parsed.
117 Each function is passed two arguments, which bounds the region of the
122 (defcustom eshell-scroll-to-bottom-on-input nil
123 "*Controls whether input to interpreter causes window to scroll.
124 If nil, then do not scroll. If t or `all', scroll all windows showing
125 buffer. If `this', scroll only the selected window.
127 See `eshell-preinput-scroll-to-bottom'."
128 :type '(radio (const :tag "Do not scroll Eshell windows" nil)
129 (const :tag "Scroll all windows showing the buffer" all)
130 (const :tag "Scroll only the selected window" this))
133 (defcustom eshell-scroll-to-bottom-on-output nil
134 "*Controls whether interpreter output causes window to scroll.
135 If nil, then do not scroll. If t or `all', scroll all windows showing
136 buffer. If `this', scroll only the selected window. If `others',
137 scroll only those that are not the selected window.
139 See variable `eshell-scroll-show-maximum-output' and function
140 `eshell-postoutput-scroll-to-bottom'."
141 :type '(radio (const :tag "Do not scroll Eshell windows" nil)
142 (const :tag "Scroll all windows showing the buffer" all)
143 (const :tag "Scroll only the selected window" this)
144 (const :tag "Scroll all windows other than selected" this))
147 (defcustom eshell-scroll-show-maximum-output t
148 "*Controls how interpreter output causes window to scroll.
149 If non-nil, then show the maximum output when the window is scrolled.
151 See variable `eshell-scroll-to-bottom-on-output' and function
152 `eshell-postoutput-scroll-to-bottom'."
156 (defcustom eshell-buffer-maximum-lines 1024
157 "*The maximum size in lines for eshell buffers.
158 Eshell buffers are truncated from the top to be no greater than this
159 number, if the function `eshell-truncate-buffer' is on
160 `eshell-output-filter-functions'."
164 (defcustom eshell-output-filter-functions
165 '(eshell-handle-control-codes
166 eshell-watch-for-password-prompt)
167 "*Functions to call before output is displayed.
168 These functions are only called for output that is displayed
169 interactively, and not for output which is redirected."
173 (defcustom eshell-preoutput-filter-functions nil
174 "*Functions to call before output is inserted into the buffer.
175 These functions get one argument, a string containing the text to be
176 inserted. They return the string as it should be inserted."
180 (defcustom eshell-password-prompt-regexp
181 "\\(\\([Oo]ld \\|[Nn]ew \\|Kerberos \\|CVS \\|'s \\|login \\|^\\)\
182 [Pp]assword\\|pass phrase\\|\\(Enter\\|Repeat\\) passphrase\\)\
183 \\( for [^@ \t\n]+@[^@ \t\n]+\\)?:\\s *\\'"
184 "*Regexp matching prompts for passwords in the inferior process.
185 This is used by `eshell-watch-for-password-prompt'."
189 (defcustom eshell-skip-prompt-function nil
190 "*A function called from beginning of line to skip the prompt."
194 (defcustom eshell-status-in-modeline t
195 "*If non-nil, let the user know a command is running in the modeline."
199 (defvar eshell-non-interactive-p nil
200 "A variable which is non-nil when Eshell is not running interactively.
201 Modules should use this variable so that they don't clutter non-interactive
202 sessions, such as when using `eshell-command'.")
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)
223 (defvar eshell-mode-syntax-table nil)
224 (defvar eshell-mode-abbrev-table nil)
226 (define-abbrev-table 'eshell-mode-abbrev-table ())
229 (unless (eshell-under-xemacs-p)
230 (defalias 'characterp 'ignore)
231 (defalias 'char-int 'ignore)))
233 (if (not eshell-mode-syntax-table)
235 (setq eshell-mode-syntax-table (make-syntax-table))
237 (modify-syntax-entry i "_ " eshell-mode-syntax-table)
241 (modify-syntax-entry i "_ " eshell-mode-syntax-table)
245 (modify-syntax-entry i "_ " eshell-mode-syntax-table)
249 (modify-syntax-entry i "_ " eshell-mode-syntax-table)
251 (modify-syntax-entry ? " " eshell-mode-syntax-table)
252 (modify-syntax-entry ?\t " " eshell-mode-syntax-table)
253 (modify-syntax-entry ?\f " " eshell-mode-syntax-table)
254 (modify-syntax-entry ?\n "> " eshell-mode-syntax-table)
255 ;; Give CR the same syntax as newline, for selective-display.
256 (modify-syntax-entry ?\^m "> " eshell-mode-syntax-table)
257 ;;; (modify-syntax-entry ?\; "< " eshell-mode-syntax-table)
258 (modify-syntax-entry ?` "' " eshell-mode-syntax-table)
259 (modify-syntax-entry ?' "' " eshell-mode-syntax-table)
260 (modify-syntax-entry ?, "' " eshell-mode-syntax-table)
261 ;; Used to be singlequote; changed for flonums.
262 (modify-syntax-entry ?. "_ " eshell-mode-syntax-table)
263 (modify-syntax-entry ?- "_ " eshell-mode-syntax-table)
264 (modify-syntax-entry ?| ". " eshell-mode-syntax-table)
265 (modify-syntax-entry ?# "' " eshell-mode-syntax-table)
266 (modify-syntax-entry ?\" "\" " eshell-mode-syntax-table)
267 (modify-syntax-entry ?\\ "/ " eshell-mode-syntax-table)
268 (modify-syntax-entry ?\( "() " eshell-mode-syntax-table)
269 (modify-syntax-entry ?\) ")( " eshell-mode-syntax-table)
270 (modify-syntax-entry ?\{ "(} " eshell-mode-syntax-table)
271 (modify-syntax-entry ?\} "){ " eshell-mode-syntax-table)
272 (modify-syntax-entry ?\[ "(] " eshell-mode-syntax-table)
273 (modify-syntax-entry ?\] ")[ " eshell-mode-syntax-table)
274 ;; All non-word multibyte characters should be `symbol'.
275 (if (eshell-under-xemacs-p)
279 (and (characterp key)
280 (>= (char-int key) 256)
281 (/= (char-syntax key) ?w)
282 (modify-syntax-entry key "_ "
283 eshell-mode-syntax-table))))
284 (standard-syntax-table))
289 (/= (char-syntax key) ?w)
290 (modify-syntax-entry key "_ "
291 eshell-mode-syntax-table))))
292 (standard-syntax-table)))))
297 (defun eshell-mode ()
298 "Emacs shell interactive mode.
301 (kill-all-local-variables)
303 (setq major-mode 'eshell-mode)
304 (setq mode-name "EShell")
305 (set (make-local-variable 'eshell-mode) t)
307 (make-local-variable 'eshell-mode-map)
308 (setq eshell-mode-map (make-sparse-keymap))
309 (use-local-map eshell-mode-map)
311 (when eshell-status-in-modeline
312 (make-local-variable 'eshell-command-running-string)
313 (let ((fmt (eshell-copy-list mode-line-format)))
314 (make-local-variable 'mode-line-format)
315 (setq mode-line-format fmt))
316 (let ((modeline (memq 'mode-line-modified mode-line-format)))
318 (setcar modeline 'eshell-command-running-string))))
320 (define-key eshell-mode-map [return] 'eshell-send-input)
321 (define-key eshell-mode-map [(control ?m)] 'eshell-send-input)
322 (define-key eshell-mode-map [(control ?j)] 'eshell-send-input)
323 (define-key eshell-mode-map [(meta return)] 'eshell-queue-input)
324 (define-key eshell-mode-map [(meta control ?m)] 'eshell-queue-input)
325 (define-key eshell-mode-map [(meta control ?l)] 'eshell-show-output)
327 (set (make-local-variable 'eshell-command-prefix)
328 (make-symbol "eshell-command-prefix"))
329 (fset eshell-command-prefix (make-sparse-keymap))
330 (set (make-local-variable 'eshell-command-map)
331 (symbol-function eshell-command-prefix))
332 (define-key eshell-mode-map [(control ?c)] eshell-command-prefix)
334 ;; without this, find-tag complains about read-only text being
336 (if (eq (key-binding [(meta ?.)]) 'find-tag)
337 (define-key eshell-mode-map [(meta ?.)] 'eshell-find-tag))
338 (define-key eshell-command-map [(meta ?o)] 'eshell-mark-output)
339 (define-key eshell-command-map [(meta ?d)] 'eshell-toggle-direct-send)
341 (define-key eshell-command-map [(control ?a)] 'eshell-bol)
342 (define-key eshell-command-map [(control ?b)] 'eshell-backward-argument)
343 (define-key eshell-command-map [(control ?e)] 'eshell-show-maximum-output)
344 (define-key eshell-command-map [(control ?f)] 'eshell-forward-argument)
345 (define-key eshell-command-map [return] 'eshell-copy-old-input)
346 (define-key eshell-command-map [(control ?m)] 'eshell-copy-old-input)
347 (define-key eshell-command-map [(control ?o)] 'eshell-kill-output)
348 (define-key eshell-command-map [(control ?r)] 'eshell-show-output)
349 (define-key eshell-command-map [(control ?t)] 'eshell-truncate-buffer)
350 (define-key eshell-command-map [(control ?u)] 'eshell-kill-input)
351 (define-key eshell-command-map [(control ?w)] 'backward-kill-word)
352 (define-key eshell-command-map [(control ?y)] 'eshell-repeat-argument)
354 (setq local-abbrev-table eshell-mode-abbrev-table)
355 (set-syntax-table eshell-mode-syntax-table)
357 (set (make-local-variable 'dired-directory) default-directory)
358 (set (make-local-variable 'list-buffers-directory)
359 (expand-file-name default-directory))
361 ;; always set the tab width to 8 in Eshell buffers, since external
362 ;; commands which do their own formatting almost always expect this
363 (set (make-local-variable 'tab-width) 8)
365 ;; don't ever use auto-fill in Eshell buffers
366 (setq auto-fill-function nil)
368 ;; always display everything from a return value
369 (if (boundp 'print-length)
370 (set (make-local-variable 'print-length) nil))
371 (if (boundp 'print-level)
372 (set (make-local-variable 'print-level) nil))
374 ;; set require-final-newline to nil; otherwise, all redirected
375 ;; output will end with a newline, whether or not the source
377 (set (make-local-variable 'require-final-newline) nil)
379 (set (make-local-variable 'max-lisp-eval-depth)
380 (max 3000 max-lisp-eval-depth))
381 (set (make-local-variable 'max-specpdl-size)
382 (max 6000 max-lisp-eval-depth))
384 (set (make-local-variable 'eshell-last-input-start) (point-marker))
385 (set (make-local-variable 'eshell-last-input-end) (point-marker))
386 (set (make-local-variable 'eshell-last-output-start) (point-marker))
387 (set (make-local-variable 'eshell-last-output-end) (point-marker))
388 (set (make-local-variable 'eshell-last-output-block-begin) (point))
390 (let ((modules-list (eshell-copy-list eshell-modules-list)))
391 (make-local-variable 'eshell-modules-list)
392 (setq eshell-modules-list modules-list))
394 ;; load extension modules into memory. This will cause any global
395 ;; variables they define to be visible, since some of the core
396 ;; modules sometimes take advantage of their functionality if used.
397 (eshell-for module eshell-modules-list
398 (let ((module-fullname (symbol-name module))
400 (if (string-match "^eshell-\\(.*\\)" module-fullname)
401 (setq module-shortname
402 (concat "em-" (match-string 1 module-fullname))))
403 (unless module-shortname
404 (error "Invalid Eshell module name: %s" module-fullname))
405 (unless (featurep (intern module-shortname))
406 (load module-shortname))))
408 (unless (file-exists-p eshell-directory-name)
409 (eshell-make-private-directory eshell-directory-name t))
411 ;; load core Eshell modules for this session
412 (eshell-for module (eshell-subgroups 'eshell)
413 (run-hooks (intern-soft (concat (symbol-name module)
416 ;; load extension modules for this session
417 (eshell-for module eshell-modules-list
418 (let ((load-hook (intern-soft (concat (symbol-name module)
420 (if (and load-hook (boundp load-hook))
421 (run-hooks load-hook))))
423 (make-local-hook 'pre-command-hook)
425 (if eshell-send-direct-to-subprocesses
426 (add-hook 'pre-command-hook 'eshell-intercept-commands t t))
428 (if eshell-scroll-to-bottom-on-input
429 (add-hook 'pre-command-hook 'eshell-preinput-scroll-to-bottom t t))
431 (when eshell-scroll-show-maximum-output
432 (set (make-local-variable 'scroll-conservatively) 1000))
434 (when eshell-status-in-modeline
435 (make-local-hook 'eshell-pre-command-hook)
436 (add-hook 'eshell-pre-command-hook 'eshell-command-started nil t)
437 (make-local-hook 'eshell-post-command-hook)
438 (add-hook 'eshell-post-command-hook 'eshell-command-finished nil t))
440 (make-local-hook 'kill-buffer-hook)
441 (add-hook 'kill-buffer-hook
444 (run-hooks 'eshell-exit-hook))) t t)
446 (if eshell-first-time-p
447 (run-hooks 'eshell-first-time-mode-hook))
448 (run-hooks 'eshell-mode-hook)
449 (run-hooks 'eshell-post-command-hook))
451 (put 'eshell-mode 'mode-class 'special)
453 (eshell-deftest mode major-mode
454 "Major mode is correct"
455 (eq major-mode 'eshell-mode))
457 (eshell-deftest mode eshell-mode-variable
458 "`eshell-mode' is true"
461 (eshell-deftest var window-height
462 "LINES equals window height"
463 (eshell-command-result-p "= $LINES (window-height)" "t\n"))
465 (defun eshell-command-started ()
466 "Indicate in the modeline that a command has started."
467 (setq eshell-command-running-string "**")
468 (force-mode-line-update))
470 (defun eshell-command-finished ()
471 "Indicate in the modeline that a command has finished."
472 (setq eshell-command-running-string "--")
473 (force-mode-line-update))
475 (eshell-deftest mode command-running-p
476 "Modeline shows no command running"
477 (or (eshell-under-xemacs-p)
478 (not eshell-status-in-modeline)
479 (and (memq 'eshell-command-running-string mode-line-format)
480 (equal eshell-command-running-string "--"))))
482 ;;; Internal Functions:
484 (defun eshell-toggle-direct-send ()
486 (if eshell-send-direct-to-subprocesses
488 (setq eshell-send-direct-to-subprocesses nil)
489 (remove-hook 'pre-command-hook 'eshell-intercept-commands t)
490 (message "Sending subprocess input on RET"))
491 (setq eshell-send-direct-to-subprocesses t)
492 (add-hook 'pre-command-hook 'eshell-intercept-commands t t)
493 (message "Sending subprocess input directly")))
495 (defun eshell-self-insert-command (N)
498 (eshell-interactive-process)
499 (char-to-string (if (symbolp last-command-char)
500 (get last-command-char 'ascii-character)
501 last-command-char))))
503 (defun eshell-intercept-commands ()
504 (when (and (eshell-interactive-process)
505 (not (and (integerp last-input-event)
506 (memq last-input-event '(?\C-x ?\C-c)))))
507 (let ((possible-events (where-is-internal this-command))
508 (name (symbol-name this-command))
510 ;; Assume that any multikey combination which does NOT target an
511 ;; Eshell command, is a combo the user wants invoked rather than
512 ;; sent to the underlying subprocess.
513 (unless (and (> (length name) 7)
514 (equal (substring name 0 7) "eshell-"))
515 (while possible-events
516 (if (> (length (car possible-events)) 1)
517 (setq intercept nil possible-events nil)
518 (setq possible-events (cdr possible-events)))))
520 (setq this-command 'eshell-self-insert-command)))))
522 (defun eshell-find-tag (&optional tagname next-p regexp-p)
523 "A special version of `find-tag' that ignores read-onlyness."
526 (let ((inhibit-read-only t)
528 (setq tagname (find-tag-interactive "Find tag: " no-default))
529 (find-tag tagname next-p regexp-p)))
531 (defun eshell-move-argument (limit func property arg)
532 "Move forward ARG arguments."
533 (catch 'eshell-incomplete
534 (eshell-parse-arguments (save-excursion (eshell-bol) (point))
535 (line-end-position)))
536 (let ((pos (save-excursion
538 (while (and (> arg 0) (/= (point) limit))
539 (if (get-text-property (point) property)
545 (if (and (eq func 'forward-char)
549 (eshell-deftest arg forward-arg
550 "Move across command arguments"
551 (eshell-insert-command "echo $(+ 1 (- 4 3)) \"alpha beta\" file" 'ignore)
552 (let ((here (point)) begin valid)
555 (eshell-forward-argument 4)
556 (setq valid (= here (point)))
557 (eshell-backward-argument 4)
559 (and valid (= begin (point)))
561 (delete-region (point) (point-max)))))
563 (defun eshell-forward-argument (&optional arg)
564 "Move forward ARG arguments."
566 (eshell-move-argument (point-max) 'forward-char 'arg-end arg))
568 (defun eshell-backward-argument (&optional arg)
569 "Move backward ARG arguments."
571 (eshell-move-argument (point-min) 'backward-char 'arg-begin arg))
573 (defun eshell-repeat-argument (&optional arg)
575 (let ((begin (save-excursion
576 (eshell-backward-argument arg)
578 (kill-ring-save begin (point))
582 "Goes to the beginning of line, then skips past the prompt, if any."
585 (and eshell-skip-prompt-function
586 (funcall eshell-skip-prompt-function)))
588 (defsubst eshell-push-command-mark ()
589 "Push a mark at the end of the last input text."
590 (push-mark (1- eshell-last-input-end) t))
592 (custom-add-option 'eshell-pre-command-hook 'eshell-push-command-mark)
594 (defsubst eshell-goto-input-start ()
595 "Goto the start of the last command input.
596 Putting this function on `eshell-pre-command-hook' will mimic Plan 9's
598 (goto-char eshell-last-input-start))
600 (custom-add-option 'eshell-pre-command-hook 'eshell-push-command-mark)
602 (defsubst eshell-interactive-print (string)
603 "Print STRING to the eshell display buffer."
604 (eshell-output-filter nil string))
606 (defsubst eshell-begin-on-new-line ()
607 "This function outputs a newline if not at beginning of line."
609 (goto-char eshell-last-output-end)
611 (eshell-interactive-print "\n"))))
613 (defsubst eshell-reset (&optional no-hooks)
614 "Output a prompt on a new line, aborting any current input.
615 If NO-HOOKS is non-nil, then `eshell-post-command-hook' won't be run."
616 (goto-char (point-max))
617 (setq eshell-last-input-start (point-marker)
618 eshell-last-input-end (point-marker)
619 eshell-last-output-start (point-marker)
620 eshell-last-output-block-begin (point)
621 eshell-last-output-end (point-marker))
622 (eshell-begin-on-new-line)
624 (run-hooks 'eshell-post-command-hook)
625 (goto-char (point-max))))
627 (defun eshell-parse-command-input (beg end &optional args)
628 "Parse the command input from BEG to END.
629 The difference is that `eshell-parse-command' expects a complete
630 command string (and will error if it doesn't get one), whereas this
631 function will inform the caller whether more input is required.
633 If nil is returned, more input is necessary (probably because a
634 multi-line input string wasn't terminated properly). Otherwise, it
635 will return the parsed command."
638 (catch 'eshell-incomplete
640 (setq command (eshell-parse-command (cons beg end)
643 (message "Expecting completion of delimeter %c ..."
649 (defun eshell-update-markers (pmark)
650 "Update the input and output markers relative to point and PMARK."
651 (set-marker eshell-last-input-start pmark)
652 (set-marker eshell-last-input-end (point))
653 (set-marker eshell-last-output-end (point)))
655 (defun eshell-queue-input (&optional use-region)
656 "Queue the current input text for execution by Eshell.
657 Particularly, don't send the text to the current process, even if it's
660 (eshell-send-input use-region t))
662 (eshell-deftest mode queue-input
663 "Queue command input"
664 (eshell-insert-command "sleep 2")
665 (eshell-insert-command "echo alpha" 'eshell-queue-input)
667 (while (and eshell-current-command
670 (setq count (1- count))))
671 (eshell-match-result "alpha\n"))
673 (defun eshell-send-input (&optional use-region queue-p no-newline)
674 "Send the input received to Eshell for parsing and processing..
675 After `eshell-last-output-end', sends all text from that marker to
676 point as input. Before that marker, calls `eshell-get-old-input' to
677 retrieve old input, copies it to the end of the buffer, and sends it.
679 If USE-REGION is non-nil, the current region (between point and mark)
680 will be used as input.
682 If QUEUE-P is non-nil, input will be queued until the next prompt,
683 rather than sent to the currently active process. If no process, the
684 input is processed immediately.
686 If NO-NEWLINE is non-nil, the input is sent without an implied final
689 ;; Note that the input string does not include its terminal newline.
690 (let ((proc-running-p (and (eshell-interactive-process)
692 (inhibit-point-motion-hooks t)
693 after-change-functions)
694 (unless (and proc-running-p
695 (not (eq (process-status
696 (eshell-interactive-process)) 'run)))
697 (if (or proc-running-p
698 (>= (point) eshell-last-output-end))
699 (goto-char (point-max))
700 (let ((copy (eshell-get-old-input use-region)))
701 (goto-char eshell-last-output-end)
702 (insert-and-inherit copy)))
703 (unless (or no-newline
704 (and eshell-send-direct-to-subprocesses
706 (insert-before-markers-and-inherit ?\n))
709 (eshell-update-markers eshell-last-output-end)
710 (if (or eshell-send-direct-to-subprocesses
711 (= eshell-last-input-start eshell-last-input-end))
713 (process-send-string (eshell-interactive-process) "\n"))
714 (process-send-region (eshell-interactive-process)
715 eshell-last-input-start
716 eshell-last-input-end)))
717 (if (= eshell-last-output-end (point))
718 (run-hooks 'eshell-post-command-hook)
720 (eshell-condition-case err
722 (setq input (buffer-substring-no-properties
723 eshell-last-output-end (1- (point))))
724 (run-hook-with-args 'eshell-expand-input-functions
725 eshell-last-output-end (1- (point)))
726 (let ((cmd (eshell-parse-command-input
727 eshell-last-output-end (1- (point)))))
729 (eshell-update-markers eshell-last-output-end)
730 (setq input (buffer-substring-no-properties
731 eshell-last-input-start
732 (1- eshell-last-input-end)))
733 (run-hooks 'eshell-input-filter-functions)
734 (and (catch 'eshell-terminal
736 (eshell-eval-command cmd input)))
737 (eshell-life-is-too-much)))))
740 (run-hooks 'eshell-post-command-hook)
744 (eshell-interactive-print
745 (concat (error-message-string err) "\n"))
746 (run-hooks 'eshell-post-command-hook)
747 (insert-and-inherit input)))))))))
749 (eshell-deftest proc send-to-subprocess
750 "Send input to a subprocess"
751 ;; jww (1999-12-06): what about when bc is unavailable?
752 (if (not (eshell-search-path "bc"))
754 (eshell-insert-command "bc")
755 (eshell-insert-command "1 + 2")
760 (eshell-insert-command "quit")
763 (defsubst eshell-kill-new ()
764 "Add the last input text to the kill ring."
765 (kill-ring-save eshell-last-input-start eshell-last-input-end))
767 (custom-add-option 'eshell-input-filter-functions 'eshell-kill-new)
769 (defun eshell-output-filter (process string)
770 "Send the output from PROCESS (STRING) to the interactive display.
771 This is done after all necessary filtering has been done."
772 (let ((oprocbuf (if process (process-buffer process)
774 (inhibit-point-motion-hooks t)
775 after-change-functions)
776 (let ((functions eshell-preoutput-filter-functions))
777 (while (and functions string)
778 (setq string (funcall (car functions) string))
779 (setq functions (cdr functions))))
780 (if (and string oprocbuf (buffer-name oprocbuf))
781 (let ((obuf (current-buffer))
783 (set-buffer oprocbuf)
784 (setq opoint (point))
785 (setq obeg (point-min))
786 (setq oend (point-max))
787 (let ((buffer-read-only nil)
788 (nchars (length string))
791 (goto-char eshell-last-output-end)
792 (setq ostart (point))
793 (if (<= (point) opoint)
794 (setq opoint (+ opoint nchars)))
796 (setq obeg (+ obeg nchars)))
797 (if (<= (point) oend)
798 (setq oend (+ oend nchars)))
799 (insert-before-markers string)
800 (if (= (window-start (selected-window)) (point))
801 (set-window-start (selected-window)
803 (if (= (point) eshell-last-input-end)
804 (set-marker eshell-last-input-end
805 (- eshell-last-input-end nchars)))
806 (set-marker eshell-last-output-start ostart)
807 (set-marker eshell-last-output-end (point))
808 (force-mode-line-update))
809 (narrow-to-region obeg oend)
811 (eshell-run-output-filters)
812 (set-buffer obuf)))))
814 (defun eshell-run-output-filters ()
815 "Run the `eshell-output-filter-functions' on the current output."
817 (run-hooks 'eshell-output-filter-functions))
818 (setq eshell-last-output-block-begin
819 (marker-position eshell-last-output-end)))
821 ;;; jww (1999-10-23): this needs testing
822 (defun eshell-preinput-scroll-to-bottom ()
823 "Go to the end of buffer in all windows showing it.
824 Movement occurs if point in the selected window is not after the
825 process mark, and `this-command' is an insertion command. Insertion
826 commands recognised are `self-insert-command', `yank', and
827 `hilit-yank'. Depends on the value of
828 `eshell-scroll-to-bottom-on-input'.
830 This function should be a pre-command hook."
831 (if (memq this-command '(self-insert-command yank hilit-yank))
832 (let* ((selected (selected-window))
833 (current (current-buffer))
834 (scroll eshell-scroll-to-bottom-on-input))
835 (if (< (point) eshell-last-output-end)
836 (if (eq scroll 'this)
837 (goto-char (point-max))
841 (when (and (eq (window-buffer window) current)
842 (or (eq scroll t) (eq scroll 'all)))
843 (select-window window)
844 (goto-char (point-max))
845 (select-window selected))))
848 ;;; jww (1999-10-23): this needs testing
849 (defun eshell-postoutput-scroll-to-bottom ()
850 "Go to the end of buffer in all windows showing it.
851 Does not scroll if the current line is the last line in the buffer.
852 Depends on the value of `eshell-scroll-to-bottom-on-output' and
853 `eshell-scroll-show-maximum-output'.
855 This function should be in the list `eshell-output-filter-functions'."
856 (let* ((selected (selected-window))
857 (current (current-buffer))
858 (scroll eshell-scroll-to-bottom-on-output))
863 (if (eq (window-buffer window) current)
865 (select-window window)
866 (if (and (< (point) eshell-last-output-end)
867 (or (eq scroll t) (eq scroll 'all)
868 ;; Maybe user wants point to jump to end.
869 (and (eq scroll 'this)
870 (eq selected window))
871 (and (eq scroll 'others)
872 (not (eq selected window)))
873 ;; If point was at the end, keep it at end.
874 (>= (point) eshell-last-output-start)))
875 (goto-char eshell-last-output-end))
876 ;; Optionally scroll so that the text
877 ;; ends at the bottom of the window.
878 (if (and eshell-scroll-show-maximum-output
879 (>= (point) eshell-last-output-end))
881 (goto-char (point-max))
883 (select-window selected)))))
885 (set-buffer current))))
887 (custom-add-option 'eshell-output-filter-functions
888 'eshell-postoutput-scroll-to-bottom)
890 (defun eshell-beginning-of-input ()
891 "Return the location of the start of the previous input."
892 eshell-last-input-start)
894 (defun eshell-beginning-of-output ()
895 "Return the location of the end of the previous output block."
896 eshell-last-input-end)
898 (defun eshell-end-of-output ()
899 "Return the location of the end of the previous output block."
900 (if (eshell-using-module 'eshell-prompt)
901 eshell-last-output-start
902 eshell-last-output-end))
904 (defun eshell-kill-output ()
905 "Kill all output from interpreter since last input.
906 Does not delete the prompt."
909 (goto-char (eshell-beginning-of-output))
910 (insert "*** output flushed ***\n")
911 (delete-region (point) (eshell-end-of-output))))
913 (eshell-deftest io flush-output
914 "Flush previous output"
915 (eshell-insert-command "echo alpha")
917 (and (eshell-match-result (regexp-quote "*** output flushed ***\n"))
919 (= (point) eshell-last-output-start)))
921 (defun eshell-show-output (&optional arg)
922 "Display start of this batch of interpreter output at top of window.
923 Sets mark to the value of point when this command is run.
924 With a prefix argument, narrows region to last command output."
926 (goto-char (eshell-beginning-of-output))
927 (set-window-start (selected-window)
929 (goto-char (eshell-beginning-of-input))
930 (line-beginning-position)))
932 (narrow-to-region (eshell-beginning-of-output)
933 (eshell-end-of-output)))
934 (eshell-end-of-output))
936 (defun eshell-mark-output (&optional arg)
937 "Display start of this batch of interpreter output at top of window.
938 Sets mark to the value of point when this command is run.
939 With a prefix argument, narrows region to last command output."
941 (push-mark (eshell-show-output arg)))
943 (defun eshell-kill-input ()
944 "Kill all text from last stuff output by interpreter to point."
946 (if (> (point) eshell-last-output-end)
947 (kill-region eshell-last-output-end (point))
948 (let ((here (point)))
950 (kill-region (point) here))))
952 (defun eshell-show-maximum-output ()
953 "Put the end of the buffer at the bottom of the window."
957 (goto-char (point-max))
960 (defun eshell-get-old-input (&optional use-current-region)
961 "Return the command input on the current line."
962 (if use-current-region
963 (buffer-substring (min (point) (mark))
964 (max (point) (mark)))
967 (and eshell-skip-prompt-function
968 (funcall eshell-skip-prompt-function))
971 (buffer-substring beg (point))))))
973 (defun eshell-copy-old-input ()
974 "Insert after prompt old input at point as new input to be edited."
976 (let ((input (eshell-get-old-input)))
977 (goto-char eshell-last-output-end)
978 (insert-and-inherit input)))
980 (eshell-deftest mode run-old-command
981 "Re-run an old command"
982 (eshell-insert-command "echo alpha")
983 (goto-char eshell-last-input-start)
984 (string= (eshell-get-old-input) "echo alpha"))
986 (defun eshell/exit ()
987 "Leave or kill the Eshell buffer, depending on `eshell-kill-on-exit'."
988 (throw 'eshell-terminal t))
990 (defun eshell-life-is-too-much ()
991 "Kill the current buffer (or bury it). Good-bye Eshell."
993 (if (not eshell-kill-on-exit)
995 (kill-buffer (current-buffer))))
997 (defun eshell-truncate-buffer ()
998 "Truncate the buffer to `eshell-buffer-maximum-lines'.
999 This function could be on `eshell-output-filter-functions' or bound to
1003 (goto-char eshell-last-output-end)
1004 (let ((lines (count-lines 1 (point)))
1005 (inhibit-read-only t))
1006 (forward-line (- eshell-buffer-maximum-lines))
1008 (let ((pos (point)))
1011 (error "Buffer too short to truncate"))
1012 (delete-region (point-min) (point))
1014 (message "Truncated buffer from %d to %d lines (%.1fk freed)"
1015 lines eshell-buffer-maximum-lines
1016 (/ pos 1024.0))))))))
1018 (custom-add-option 'eshell-output-filter-functions
1019 'eshell-truncate-buffer)
1021 (defun send-invisible (str)
1022 "Read a string without echoing.
1023 Then send it to the process running in the current buffer."
1024 (interactive "P") ; Defeat snooping via C-x ESC ESC
1025 (let ((str (read-passwd
1026 (format "Password: "
1027 (process-name (eshell-interactive-process))))))
1029 (process-send-string (eshell-interactive-process)
1031 (message "Warning: text will be echoed"))))
1033 (defun eshell-watch-for-password-prompt ()
1034 "Prompt in the minibuffer for password and send without echoing.
1035 This function uses `send-invisible' to read and send a password to the
1036 buffer's process if STRING contains a password prompt defined by
1037 `eshell-password-prompt-regexp'.
1039 This function could be in the list `eshell-output-filter-functions'."
1040 (when (eshell-interactive-process)
1042 (goto-char eshell-last-output-block-begin)
1044 (if (re-search-forward eshell-password-prompt-regexp
1045 eshell-last-output-end t)
1046 (send-invisible nil)))))
1048 (custom-add-option 'eshell-output-filter-functions
1049 'eshell-watch-for-password-prompt)
1051 (defun eshell-handle-control-codes ()
1052 "Act properly when certain control codes are seen."
1054 (let ((orig (point)))
1055 (goto-char eshell-last-output-block-begin)
1057 (beginning-of-line))
1058 (while (< (point) eshell-last-output-end)
1059 (let ((char (char-after)))
1062 (if (< (1+ (point)) eshell-last-output-end)
1063 (if (memq (char-after (1+ (point)))
1066 (let ((end (1+ (point))))
1068 (delete-region (point) end)))
1069 (add-text-properties (point) (1+ (point))
1076 (delete-backward-char 1)
1079 (forward-char))))))))
1081 (custom-add-option 'eshell-output-filter-functions
1082 'eshell-handle-control-codes)
1086 ;;; esh-mode.el ends here