1 ;;; esh-mode.el --- user interface
3 ;; Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007,
4 ;; 2008, 2009, 2010 Free Software Foundation, Inc.
6 ;; Author: John Wiegley <johnw@gnu.org>
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;; Basically, Eshell is used just like shell mode (<M-x shell>). The
26 ;; keystrokes for navigating the buffer, and accessing the command
27 ;; history, are identical. Unlike shell mode, however, Eshell mode's
28 ;; governing process is Emacs itself. With shell mode, an inferior
29 ;; shell process is executed that communicates with Emacs via comint
30 ;; -- a mode for handling sub-process interaction. Eshell mode, on
31 ;; the other hand, is a truly native Emacs shell. No subprocess are
32 ;; invoked except the ones requested by the user at the prompt.
34 ;; After entering a command, use <RET> to invoke it ([Command
35 ;; invocation]) . If there is a command on disk, it will be executed
36 ;; as in a normal shell. If there is no command by that name on disk,
37 ;; but a Lisp function with that name is defined, the Lisp function
38 ;; will be called, using the arguments passed on the command line.
40 ;; Some of the other features of the command interaction mode are:
42 ;; @ <M-RET> can be used to accumulate further commands while a
43 ;; command is currently running. Since all input is passed to the
44 ;; subprocess being executed, there is no automatic input queueing
45 ;; as there is with other shells.
47 ;; @ <C-c C-t> can be used to truncate the buffer if it grows too
50 ;; @ <C-c C-r> will move point to the beginning of the output of the
51 ;; last command. With a prefix argument, it will narrow to view
54 ;; @ <C-c C-o> will delete the output from the last command.
56 ;; @ <C-c C-f> will move forward a complete shell argument.
58 ;; @ <C-c C-b> will move backward a complete shell argument.
64 (eval-when-compile (require 'esh-util
))
70 (defgroup eshell-mode nil
71 "This module contains code for handling input from the user."
77 (defcustom eshell-mode-unload-hook nil
78 "*A hook that gets run when `eshell-mode' is unloaded."
82 (defcustom eshell-mode-hook nil
83 "*A hook that gets run when `eshell-mode' is entered."
87 (defcustom eshell-first-time-mode-hook nil
88 "*A hook that gets run the first time `eshell-mode' is entered.
89 That is to say, the first time during an Emacs session."
93 (defcustom eshell-exit-hook
'(eshell-query-kill-processes)
94 "*A hook that is run whenever `eshell' is exited.
95 This hook is only run if exiting actually kills the buffer."
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 "[Pp]ass\\(word\\|phrase\\).*:\\s *\\'"
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 (defcustom eshell-status-in-modeline t
197 "*If non-nil, let the user know a command is running in the modeline."
201 (defvar eshell-first-time-p t
202 "A variable which is non-nil the first time Eshell is loaded.")
204 ;; Internal Variables:
206 ;; these are only set to `nil' initially for the sake of the
207 ;; byte-compiler, when compiling other files which `require' this one
208 (defvar eshell-mode nil
)
209 (defvar eshell-mode-map nil
)
210 (defvar eshell-command-running-string
"--")
211 (defvar eshell-command-map nil
)
212 (defvar eshell-command-prefix nil
)
213 (defvar eshell-last-input-start nil
)
214 (defvar eshell-last-input-end nil
)
215 (defvar eshell-last-output-start nil
)
216 (defvar eshell-last-output-block-begin nil
)
217 (defvar eshell-last-output-end nil
)
219 (defvar eshell-currently-handling-window nil
)
220 (defvar eshell-mode-syntax-table nil
)
221 (defvar eshell-mode-abbrev-table nil
)
223 (define-abbrev-table 'eshell-mode-abbrev-table
())
225 (if (not eshell-mode-syntax-table
)
227 (setq eshell-mode-syntax-table
(make-syntax-table))
229 (modify-syntax-entry i
"_ " eshell-mode-syntax-table
)
233 (modify-syntax-entry i
"_ " eshell-mode-syntax-table
)
237 (modify-syntax-entry i
"_ " eshell-mode-syntax-table
)
241 (modify-syntax-entry i
"_ " eshell-mode-syntax-table
)
243 (modify-syntax-entry ?
" " eshell-mode-syntax-table
)
244 (modify-syntax-entry ?
\t " " eshell-mode-syntax-table
)
245 (modify-syntax-entry ?
\f " " eshell-mode-syntax-table
)
246 (modify-syntax-entry ?
\n "> " eshell-mode-syntax-table
)
247 ;; Give CR the same syntax as newline, for selective-display.
248 (modify-syntax-entry ?\^m
"> " 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 (modify-syntax-entry ?
, "' " eshell-mode-syntax-table
)
253 ;; Used to be singlequote; changed for flonums.
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 (modify-syntax-entry ?\
] ")[ " eshell-mode-syntax-table
)
266 ;; All non-word multibyte characters should be `symbol'.
267 (if (featurep 'xemacs
)
271 (and (characterp key
)
272 (>= (char-int key
) 256)
273 (/= (char-syntax key
) ?w
)
274 (modify-syntax-entry key
"_ "
275 eshell-mode-syntax-table
))))
276 (standard-syntax-table))
281 (and (>= (car key
) 128)
282 (/= (char-syntax (car key
)) ?w
))
284 (/= (char-syntax key
) ?w
)))
285 (modify-syntax-entry key
"_ "
286 eshell-mode-syntax-table
))))
287 (standard-syntax-table)))))
292 (defun eshell-mode ()
293 "Emacs shell interactive mode.
296 (kill-all-local-variables)
298 (setq major-mode
'eshell-mode
)
299 (setq mode-name
"EShell")
300 (set (make-local-variable 'eshell-mode
) t
)
302 (make-local-variable 'eshell-mode-map
)
303 (setq eshell-mode-map
(make-sparse-keymap))
304 (use-local-map eshell-mode-map
)
306 (when eshell-status-in-modeline
307 (make-local-variable 'eshell-command-running-string
)
308 (let ((fmt (copy-sequence mode-line-format
)))
309 (make-local-variable 'mode-line-format
)
310 (setq mode-line-format fmt
))
311 (let ((modeline (memq 'mode-line-modified mode-line-format
)))
313 (setcar modeline
'eshell-command-running-string
))))
315 (define-key eshell-mode-map
[return] 'eshell-send-input)
316 (define-key eshell-mode-map [(control ?m)] 'eshell-send-input)
317 (define-key eshell-mode-map [(control ?j)] 'eshell-send-input)
318 (define-key eshell-mode-map [(meta return)] 'eshell-queue-input)
319 (define-key eshell-mode-map [(meta control ?m)] 'eshell-queue-input)
320 (define-key eshell-mode-map [(meta control ?l)] 'eshell-show-output)
321 (define-key eshell-mode-map [(control ?a)] 'eshell-bol)
323 (set (make-local-variable 'eshell-command-prefix)
324 (make-symbol "eshell-command-prefix"))
325 (fset eshell-command-prefix (make-sparse-keymap))
326 (set (make-local-variable 'eshell-command-map)
327 (symbol-function eshell-command-prefix))
328 (define-key eshell-mode-map [(control ?c)] eshell-command-prefix)
330 ;; without this, find-tag complains about read-only text being
332 (if (eq (key-binding [(meta ?.)]) 'find-tag)
333 (define-key eshell-mode-map [(meta ?.)] 'eshell-find-tag))
334 (define-key eshell-command-map [(meta ?o)] 'eshell-mark-output)
335 (define-key eshell-command-map [(meta ?d)] 'eshell-toggle-direct-send)
337 (define-key eshell-command-map [(control ?a)] 'eshell-bol)
338 (define-key eshell-command-map [(control ?b)] 'eshell-backward-argument)
339 (define-key eshell-command-map [(control ?e)] 'eshell-show-maximum-output)
340 (define-key eshell-command-map [(control ?f)] 'eshell-forward-argument)
341 (define-key eshell-command-map [return] 'eshell-copy-old-input
)
342 (define-key eshell-command-map
[(control ?m
)] 'eshell-copy-old-input
)
343 (define-key eshell-command-map
[(control ?o
)] 'eshell-kill-output
)
344 (define-key eshell-command-map
[(control ?r
)] 'eshell-show-output
)
345 (define-key eshell-command-map
[(control ?t
)] 'eshell-truncate-buffer
)
346 (define-key eshell-command-map
[(control ?u
)] 'eshell-kill-input
)
347 (define-key eshell-command-map
[(control ?w
)] 'backward-kill-word
)
348 (define-key eshell-command-map
[(control ?y
)] 'eshell-repeat-argument
)
350 (setq local-abbrev-table eshell-mode-abbrev-table
)
351 (set-syntax-table eshell-mode-syntax-table
)
353 (set (make-local-variable 'dired-directory
) default-directory
)
354 (set (make-local-variable 'list-buffers-directory
)
355 (expand-file-name default-directory
))
357 ;; always set the tab width to 8 in Eshell buffers, since external
358 ;; commands which do their own formatting almost always expect this
359 (set (make-local-variable 'tab-width
) 8)
361 ;; don't ever use auto-fill in Eshell buffers
362 (setq auto-fill-function nil
)
364 ;; always display everything from a return value
365 (if (boundp 'print-length
)
366 (set (make-local-variable 'print-length
) nil
))
367 (if (boundp 'print-level
)
368 (set (make-local-variable 'print-level
) nil
))
370 ;; set require-final-newline to nil; otherwise, all redirected
371 ;; output will end with a newline, whether or not the source
373 (set (make-local-variable 'require-final-newline
) nil
)
375 (set (make-local-variable 'max-lisp-eval-depth
)
376 (max 3000 max-lisp-eval-depth
))
377 (set (make-local-variable 'max-specpdl-size
)
378 (max 6000 max-lisp-eval-depth
))
380 (set (make-local-variable 'eshell-last-input-start
) (point-marker))
381 (set (make-local-variable 'eshell-last-input-end
) (point-marker))
382 (set (make-local-variable 'eshell-last-output-start
) (point-marker))
383 (set (make-local-variable 'eshell-last-output-end
) (point-marker))
384 (set (make-local-variable 'eshell-last-output-block-begin
) (point))
386 (let ((modules-list (copy-sequence eshell-modules-list
)))
387 (make-local-variable 'eshell-modules-list
)
388 (setq eshell-modules-list modules-list
))
390 ;; load extension modules into memory. This will cause any global
391 ;; variables they define to be visible, since some of the core
392 ;; modules sometimes take advantage of their functionality if used.
393 (eshell-for module eshell-modules-list
394 (let ((module-fullname (symbol-name module
))
396 (if (string-match "^eshell-\\(.*\\)" module-fullname
)
397 (setq module-shortname
398 (concat "em-" (match-string 1 module-fullname
))))
399 (unless module-shortname
400 (error "Invalid Eshell module name: %s" module-fullname
))
401 (unless (featurep (intern module-shortname
))
402 (load module-shortname
))))
404 (unless (file-exists-p eshell-directory-name
)
405 (eshell-make-private-directory eshell-directory-name t
))
407 ;; load core Eshell modules for this session
408 (eshell-for module
(eshell-subgroups 'eshell
)
409 (run-hooks (intern-soft (concat (symbol-name module
)
412 ;; load extension modules for this session
413 (eshell-for module eshell-modules-list
414 (let ((load-hook (intern-soft (concat (symbol-name module
)
416 (if (and load-hook
(boundp load-hook
))
417 (run-hooks load-hook
))))
419 (if eshell-send-direct-to-subprocesses
420 (add-hook 'pre-command-hook
'eshell-intercept-commands t t
))
422 (if eshell-scroll-to-bottom-on-input
423 (add-hook 'pre-command-hook
'eshell-preinput-scroll-to-bottom t t
))
425 (when eshell-scroll-show-maximum-output
426 (set (make-local-variable 'scroll-conservatively
) 1000))
428 (when eshell-status-in-modeline
429 (add-hook 'eshell-pre-command-hook
'eshell-command-started nil t
)
430 (add-hook 'eshell-post-command-hook
'eshell-command-finished nil t
))
432 (add-hook 'kill-buffer-hook
435 (run-hooks 'eshell-exit-hook
))) t t
)
437 (if eshell-first-time-p
438 (run-hooks 'eshell-first-time-mode-hook
))
439 (run-mode-hooks 'eshell-mode-hook
)
440 (run-hooks 'eshell-post-command-hook
))
442 (put 'eshell-mode
'mode-class
'special
)
444 (eshell-deftest mode major-mode
445 "Major mode is correct"
446 (eq major-mode
'eshell-mode
))
448 (eshell-deftest mode eshell-mode-variable
449 "`eshell-mode' is true"
452 (eshell-deftest var window-height
453 "LINES equals window height"
454 (let ((eshell-stringify-t t
))
455 (eshell-command-result-p "= $LINES (window-height)" "t\n")))
457 (defun eshell-command-started ()
458 "Indicate in the modeline that a command has started."
459 (setq eshell-command-running-string
"**")
460 (force-mode-line-update))
462 (defun eshell-command-finished ()
463 "Indicate in the modeline that a command has finished."
464 (setq eshell-command-running-string
"--")
465 (force-mode-line-update))
467 (eshell-deftest mode command-running-p
468 "Modeline shows no command running"
469 (or (featurep 'xemacs
)
470 (not eshell-status-in-modeline
)
471 (and (memq 'eshell-command-running-string mode-line-format
)
472 (equal eshell-command-running-string
"--"))))
474 ;;; Internal Functions:
476 (defun eshell-toggle-direct-send ()
478 (if eshell-send-direct-to-subprocesses
480 (setq eshell-send-direct-to-subprocesses nil
)
481 (remove-hook 'pre-command-hook
'eshell-intercept-commands t
)
482 (message "Sending subprocess input on RET"))
483 (setq eshell-send-direct-to-subprocesses t
)
484 (add-hook 'pre-command-hook
'eshell-intercept-commands t t
)
485 (message "Sending subprocess input directly")))
487 (defun eshell-self-insert-command (N)
490 (eshell-interactive-process)
491 (char-to-string (if (symbolp last-command-event
)
492 (get last-command-event
'ascii-character
)
493 last-command-event
))))
495 (defun eshell-intercept-commands ()
496 (when (and (eshell-interactive-process)
497 (not (and (integerp last-input-event
)
498 (memq last-input-event
'(?\C-x ?\C-c
)))))
499 (let ((possible-events (where-is-internal this-command
))
500 (name (symbol-name this-command
))
502 ;; Assume that any multikey combination which does NOT target an
503 ;; Eshell command, is a combo the user wants invoked rather than
504 ;; sent to the underlying subprocess.
505 (unless (and (> (length name
) 7)
506 (equal (substring name
0 7) "eshell-"))
507 (while possible-events
508 (if (> (length (car possible-events
)) 1)
509 (setq intercept nil possible-events nil
)
510 (setq possible-events
(cdr possible-events
)))))
512 (setq this-command
'eshell-self-insert-command
)))))
514 (defun eshell-find-tag (&optional tagname next-p regexp-p
)
515 "A special version of `find-tag' that ignores read-onlyness."
518 (let ((inhibit-read-only t
)
520 (find-tag-default-function 'ignore
))
522 (setq tagname
(car (find-tag-interactive "Find tag: "))))
523 (find-tag tagname next-p regexp-p
)))
525 (defun eshell-move-argument (limit func property arg
)
526 "Move forward ARG arguments."
527 (catch 'eshell-incomplete
528 (eshell-parse-arguments (save-excursion (eshell-bol) (point))
529 (line-end-position)))
530 (let ((pos (save-excursion
532 (while (and (> arg
0) (/= (point) limit
))
533 (if (get-text-property (point) property
)
539 (if (and (eq func
'forward-char
)
543 (eshell-deftest arg forward-arg
544 "Move across command arguments"
545 (eshell-insert-command "echo $(+ 1 (- 4 3)) \"alpha beta\" file" 'ignore
)
546 (let ((here (point)) begin valid
)
549 (eshell-forward-argument 4)
550 (setq valid
(= here
(point)))
551 (eshell-backward-argument 4)
553 (and valid
(= begin
(point)))
555 (delete-region (point) (point-max)))))
557 (defun eshell-forward-argument (&optional arg
)
558 "Move forward ARG arguments."
560 (eshell-move-argument (point-max) 'forward-char
'arg-end arg
))
562 (defun eshell-backward-argument (&optional arg
)
563 "Move backward ARG arguments."
565 (eshell-move-argument (point-min) 'backward-char
'arg-begin arg
))
567 (defun eshell-repeat-argument (&optional arg
)
569 (let ((begin (save-excursion
570 (eshell-backward-argument arg
)
572 (kill-ring-save begin
(point))
576 "Goes to the beginning of line, then skips past the prompt, if any."
579 (and eshell-skip-prompt-function
580 (funcall eshell-skip-prompt-function
)))
582 (defsubst eshell-push-command-mark
()
583 "Push a mark at the end of the last input text."
584 (push-mark (1- eshell-last-input-end
) t
))
586 (custom-add-option 'eshell-pre-command-hook
'eshell-push-command-mark
)
588 (defsubst eshell-goto-input-start
()
589 "Goto the start of the last command input.
590 Putting this function on `eshell-pre-command-hook' will mimic Plan 9's
592 (goto-char eshell-last-input-start
))
594 (custom-add-option 'eshell-pre-command-hook
'eshell-push-command-mark
)
596 (defsubst eshell-interactive-print
(string)
597 "Print STRING to the eshell display buffer."
598 (eshell-output-filter nil string
))
600 (defsubst eshell-begin-on-new-line
()
601 "This function outputs a newline if not at beginning of line."
603 (goto-char eshell-last-output-end
)
605 (eshell-interactive-print "\n"))))
607 (defsubst eshell-reset
(&optional no-hooks
)
608 "Output a prompt on a new line, aborting any current input.
609 If NO-HOOKS is non-nil, then `eshell-post-command-hook' won't be run."
610 (goto-char (point-max))
611 (setq eshell-last-input-start
(point-marker)
612 eshell-last-input-end
(point-marker)
613 eshell-last-output-start
(point-marker)
614 eshell-last-output-block-begin
(point)
615 eshell-last-output-end
(point-marker))
616 (eshell-begin-on-new-line)
618 (run-hooks 'eshell-post-command-hook
)
619 (goto-char (point-max))))
621 (defun eshell-parse-command-input (beg end
&optional args
)
622 "Parse the command input from BEG to END.
623 The difference is that `eshell-parse-command' expects a complete
624 command string (and will error if it doesn't get one), whereas this
625 function will inform the caller whether more input is required.
627 If nil is returned, more input is necessary (probably because a
628 multi-line input string wasn't terminated properly). Otherwise, it
629 will return the parsed command."
632 (catch 'eshell-incomplete
634 (setq command
(eshell-parse-command (cons beg end
)
637 (message "Expecting completion of delimeter %c ..."
643 (defun eshell-update-markers (pmark)
644 "Update the input and output markers relative to point and PMARK."
645 (set-marker eshell-last-input-start pmark
)
646 (set-marker eshell-last-input-end
(point))
647 (set-marker eshell-last-output-end
(point)))
649 (defun eshell-queue-input (&optional use-region
)
650 "Queue the current input text for execution by Eshell.
651 Particularly, don't send the text to the current process, even if it's
654 (eshell-send-input use-region t
))
656 (eshell-deftest mode queue-input
657 "Queue command input"
658 (eshell-insert-command "sleep 2")
659 (eshell-insert-command "echo alpha" 'eshell-queue-input
)
661 (while (and eshell-current-command
664 (setq count
(1- count
))))
665 (eshell-match-result "alpha\n"))
667 (defun eshell-send-input (&optional use-region queue-p no-newline
)
668 "Send the input received to Eshell for parsing and processing.
669 After `eshell-last-output-end', sends all text from that marker to
670 point as input. Before that marker, calls `eshell-get-old-input' to
671 retrieve old input, copies it to the end of the buffer, and sends it.
673 If USE-REGION is non-nil, the current region (between point and mark)
674 will be used as input.
676 If QUEUE-P is non-nil, input will be queued until the next prompt,
677 rather than sent to the currently active process. If no process, the
678 input is processed immediately.
680 If NO-NEWLINE is non-nil, the input is sent without an implied final
683 ;; Note that the input string does not include its terminal newline.
684 (let ((proc-running-p (and (eshell-interactive-process)
686 (inhibit-point-motion-hooks t
)
687 after-change-functions
)
688 (unless (and proc-running-p
689 (not (eq (process-status
690 (eshell-interactive-process)) 'run
)))
691 (if (or proc-running-p
692 (>= (point) eshell-last-output-end
))
693 (goto-char (point-max))
694 (let ((copy (eshell-get-old-input use-region
)))
695 (goto-char eshell-last-output-end
)
696 (insert-and-inherit copy
)))
697 (unless (or no-newline
698 (and eshell-send-direct-to-subprocesses
700 (insert-before-markers-and-inherit ?
\n))
703 (eshell-update-markers eshell-last-output-end
)
704 (if (or eshell-send-direct-to-subprocesses
705 (= eshell-last-input-start eshell-last-input-end
))
707 (process-send-string (eshell-interactive-process) "\n"))
708 (process-send-region (eshell-interactive-process)
709 eshell-last-input-start
710 eshell-last-input-end
)))
711 (if (= eshell-last-output-end
(point))
712 (run-hooks 'eshell-post-command-hook
)
714 (eshell-condition-case err
716 (setq input
(buffer-substring-no-properties
717 eshell-last-output-end
(1- (point))))
718 (run-hook-with-args 'eshell-expand-input-functions
719 eshell-last-output-end
(1- (point)))
720 (let ((cmd (eshell-parse-command-input
721 eshell-last-output-end
(1- (point)))))
723 (eshell-update-markers eshell-last-output-end
)
724 (setq input
(buffer-substring-no-properties
725 eshell-last-input-start
726 (1- eshell-last-input-end
)))
727 (run-hooks 'eshell-input-filter-functions
)
728 (and (catch 'eshell-terminal
730 (if (eshell-invoke-directly cmd input
)
732 (eshell-eval-command cmd input
))))
733 (eshell-life-is-too-much)))))
736 (run-hooks 'eshell-post-command-hook
)
740 (eshell-interactive-print
741 (concat (error-message-string err
) "\n"))
742 (run-hooks 'eshell-post-command-hook
)
743 (insert-and-inherit input
)))))))))
745 ; (eshell-deftest proc send-to-subprocess
746 ; "Send input to a subprocess"
747 ; ;; jww (1999-12-06): what about when bc is unavailable?
748 ; (if (not (eshell-search-path "bc"))
750 ; (eshell-insert-command "bc")
751 ; (eshell-insert-command "1 + 2")
756 ; (eshell-insert-command "quit")
759 (defsubst eshell-kill-new
()
760 "Add the last input text to the kill ring."
761 (kill-ring-save eshell-last-input-start eshell-last-input-end
))
763 (custom-add-option 'eshell-input-filter-functions
'eshell-kill-new
)
765 (defun eshell-output-filter (process string
)
766 "Send the output from PROCESS (STRING) to the interactive display.
767 This is done after all necessary filtering has been done."
768 (let ((oprocbuf (if process
(process-buffer process
)
770 (inhibit-point-motion-hooks t
)
771 after-change-functions
)
772 (let ((functions eshell-preoutput-filter-functions
))
773 (while (and functions string
)
774 (setq string
(funcall (car functions
) string
))
775 (setq functions
(cdr functions
))))
776 (if (and string oprocbuf
(buffer-name oprocbuf
))
777 (let (opoint obeg oend
)
778 (with-current-buffer oprocbuf
779 (setq opoint
(point))
780 (setq obeg
(point-min))
781 (setq oend
(point-max))
782 (let ((buffer-read-only nil
)
783 (nchars (length string
))
786 (goto-char eshell-last-output-end
)
787 (setq ostart
(point))
788 (if (<= (point) opoint
)
789 (setq opoint
(+ opoint nchars
)))
791 (setq obeg
(+ obeg nchars
)))
792 (if (<= (point) oend
)
793 (setq oend
(+ oend nchars
)))
794 (insert-before-markers string
)
795 (if (= (window-start (selected-window)) (point))
796 (set-window-start (selected-window)
798 (if (= (point) eshell-last-input-end
)
799 (set-marker eshell-last-input-end
800 (- eshell-last-input-end nchars
)))
801 (set-marker eshell-last-output-start ostart
)
802 (set-marker eshell-last-output-end
(point))
803 (force-mode-line-update))
804 (narrow-to-region obeg oend
)
806 (eshell-run-output-filters))))))
808 (defun eshell-run-output-filters ()
809 "Run the `eshell-output-filter-functions' on the current output."
811 (run-hooks 'eshell-output-filter-functions
))
812 (setq eshell-last-output-block-begin
813 (marker-position eshell-last-output-end
)))
815 ;;; jww (1999-10-23): this needs testing
816 (defun eshell-preinput-scroll-to-bottom ()
817 "Go to the end of buffer in all windows showing it.
818 Movement occurs if point in the selected window is not after the
819 process mark, and `this-command' is an insertion command. Insertion
820 commands recognized are `self-insert-command', `yank', and
821 `hilit-yank'. Depends on the value of
822 `eshell-scroll-to-bottom-on-input'.
824 This function should be a pre-command hook."
825 (if (memq this-command
'(self-insert-command yank hilit-yank
))
826 (let* ((selected (selected-window))
827 (current (current-buffer))
828 (scroll eshell-scroll-to-bottom-on-input
))
829 (if (< (point) eshell-last-output-end
)
830 (if (eq scroll
'this
)
831 (goto-char (point-max))
835 (when (and (eq (window-buffer window
) current
)
836 (or (eq scroll t
) (eq scroll
'all
)))
837 (select-window window
)
838 (goto-char (point-max))
839 (select-window selected
))))
842 ;;; jww (1999-10-23): this needs testing
843 (defun eshell-postoutput-scroll-to-bottom ()
844 "Go to the end of buffer in all windows showing it.
845 Does not scroll if the current line is the last line in the buffer.
846 Depends on the value of `eshell-scroll-to-bottom-on-output' and
847 `eshell-scroll-show-maximum-output'.
849 This function should be in the list `eshell-output-filter-functions'."
850 (let* ((selected (selected-window))
851 (current (current-buffer))
852 (scroll eshell-scroll-to-bottom-on-output
))
857 (if (eq (window-buffer window
) current
)
859 (select-window window
)
860 (if (and (< (point) eshell-last-output-end
)
861 (or (eq scroll t
) (eq scroll
'all
)
862 ;; Maybe user wants point to jump to end.
863 (and (eq scroll
'this
)
864 (eq selected window
))
865 (and (eq scroll
'others
)
866 (not (eq selected window
)))
867 ;; If point was at the end, keep it at end.
868 (>= (point) eshell-last-output-start
)))
869 (goto-char eshell-last-output-end
))
870 ;; Optionally scroll so that the text
871 ;; ends at the bottom of the window.
872 (if (and eshell-scroll-show-maximum-output
873 (>= (point) eshell-last-output-end
))
875 (goto-char (point-max))
877 (select-window selected
)))))
879 (set-buffer current
))))
881 (defun eshell-beginning-of-input ()
882 "Return the location of the start of the previous input."
883 eshell-last-input-start
)
885 (defun eshell-beginning-of-output ()
886 "Return the location of the end of the previous output block."
887 eshell-last-input-end
)
889 (defun eshell-end-of-output ()
890 "Return the location of the end of the previous output block."
891 (if (eshell-using-module 'eshell-prompt
)
892 eshell-last-output-start
893 eshell-last-output-end
))
895 (defun eshell-kill-output ()
896 "Kill all output from interpreter since last input.
897 Does not delete the prompt."
900 (goto-char (eshell-beginning-of-output))
901 (insert "*** output flushed ***\n")
902 (delete-region (point) (eshell-end-of-output))))
904 (eshell-deftest io flush-output
905 "Flush previous output"
906 (eshell-insert-command "echo alpha")
908 (and (eshell-match-result (regexp-quote "*** output flushed ***\n"))
910 (= (point) eshell-last-output-start
)))
912 (defun eshell-show-output (&optional arg
)
913 "Display start of this batch of interpreter output at top of window.
914 Sets mark to the value of point when this command is run.
915 With a prefix argument, narrows region to last command output."
917 (goto-char (eshell-beginning-of-output))
918 (set-window-start (selected-window)
920 (goto-char (eshell-beginning-of-input))
921 (line-beginning-position)))
923 (narrow-to-region (eshell-beginning-of-output)
924 (eshell-end-of-output)))
925 (eshell-end-of-output))
927 (defun eshell-mark-output (&optional arg
)
928 "Display start of this batch of interpreter output at top of window.
929 Sets mark to the value of point when this command is run.
930 With a prefix argument, narrows region to last command output."
932 (push-mark (eshell-show-output arg
)))
934 (defun eshell-kill-input ()
935 "Kill all text from last stuff output by interpreter to point."
937 (if (> (point) eshell-last-output-end
)
938 (kill-region eshell-last-output-end
(point))
939 (let ((here (point)))
941 (kill-region (point) here
))))
943 (defun eshell-show-maximum-output (&optional interactive
)
944 "Put the end of the buffer at the bottom of the window.
945 When run interactively, widen the buffer first."
949 (goto-char (point-max))
952 (defun eshell-get-old-input (&optional use-current-region
)
953 "Return the command input on the current line."
954 (if use-current-region
955 (buffer-substring (min (point) (mark))
956 (max (point) (mark)))
959 (and eshell-skip-prompt-function
960 (funcall eshell-skip-prompt-function
))
963 (buffer-substring beg
(point))))))
965 (defun eshell-copy-old-input ()
966 "Insert after prompt old input at point as new input to be edited."
968 (let ((input (eshell-get-old-input)))
969 (goto-char eshell-last-output-end
)
970 (insert-and-inherit input
)))
972 (eshell-deftest mode run-old-command
973 "Re-run an old command"
974 (eshell-insert-command "echo alpha")
975 (goto-char eshell-last-input-start
)
976 (string= (eshell-get-old-input) "echo alpha"))
978 (defun eshell/exit
()
979 "Leave or kill the Eshell buffer, depending on `eshell-kill-on-exit'."
980 (throw 'eshell-terminal t
))
982 (defun eshell-life-is-too-much ()
983 "Kill the current buffer (or bury it). Good-bye Eshell."
985 (if (not eshell-kill-on-exit
)
987 (kill-buffer (current-buffer))))
989 (defun eshell-truncate-buffer ()
990 "Truncate the buffer to `eshell-buffer-maximum-lines'.
991 This function could be on `eshell-output-filter-functions' or bound to
995 (goto-char eshell-last-output-end
)
996 (let ((lines (count-lines 1 (point)))
997 (inhibit-read-only t
))
998 (forward-line (- eshell-buffer-maximum-lines
))
1000 (let ((pos (point)))
1002 (if (called-interactively-p 'interactive
)
1003 (message "Buffer too short to truncate"))
1004 (delete-region (point-min) (point))
1005 (if (called-interactively-p 'interactive
)
1006 (message "Truncated buffer from %d to %d lines (%.1fk freed)"
1007 lines eshell-buffer-maximum-lines
1008 (/ pos
1024.0))))))))
1010 (custom-add-option 'eshell-output-filter-functions
1011 'eshell-truncate-buffer
)
1013 (defun eshell-send-invisible (str)
1014 "Read a string without echoing.
1015 Then send it to the process running in the current buffer."
1016 (interactive "P") ; Defeat snooping via C-x ESC ESC
1017 (let ((str (read-passwd
1018 (format "%s Password: "
1019 (process-name (eshell-interactive-process))))))
1021 (process-send-string (eshell-interactive-process)
1023 (message "Warning: text will be echoed"))))
1025 (defun eshell-watch-for-password-prompt ()
1026 "Prompt in the minibuffer for password and send without echoing.
1027 This function uses `eshell-send-invisible' to read and send a password to the
1028 buffer's process if STRING contains a password prompt defined by
1029 `eshell-password-prompt-regexp'.
1031 This function could be in the list `eshell-output-filter-functions'."
1032 (when (eshell-interactive-process)
1034 (goto-char eshell-last-output-block-begin
)
1036 (if (re-search-forward eshell-password-prompt-regexp
1037 eshell-last-output-end t
)
1038 (eshell-send-invisible nil
)))))
1040 (custom-add-option 'eshell-output-filter-functions
1041 'eshell-watch-for-password-prompt
)
1043 (defun eshell-handle-control-codes ()
1044 "Act properly when certain control codes are seen."
1046 (let ((orig (point)))
1047 (goto-char eshell-last-output-block-begin
)
1049 (beginning-of-line))
1050 (while (< (point) eshell-last-output-end
)
1051 (let ((char (char-after)))
1054 (if (< (1+ (point)) eshell-last-output-end
)
1055 (if (memq (char-after (1+ (point)))
1058 (let ((end (1+ (point))))
1060 (delete-region (point) end
)))
1061 (add-text-properties (point) (1+ (point))
1068 (delete-backward-char 1)
1071 (forward-char))))))))
1073 (custom-add-option 'eshell-output-filter-functions
1074 'eshell-handle-control-codes
)
1076 (autoload 'ansi-color-apply-on-region
"ansi-color")
1078 (defun eshell-handle-ansi-color ()
1079 "Handle ANSI color codes."
1080 (ansi-color-apply-on-region eshell-last-output-start
1081 eshell-last-output-end
))
1083 (custom-add-option 'eshell-output-filter-functions
1084 'eshell-handle-ansi-color
)
1086 ;; arch-tag: ec65bc2b-da14-4547-81d3-a32af3a4dc57
1087 ;;; esh-mode.el ends here