1 ;;; esh-mode.el --- user interface
3 ;; Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007, 2008 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.
62 (eval-when-compile (require 'esh-util
))
68 (defgroup eshell-mode nil
69 "This module contains code for handling input from the user."
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-postoutput-scroll-to-bottom
166 eshell-handle-control-codes
167 eshell-watch-for-password-prompt
)
168 "*Functions to call before output is displayed.
169 These functions are only called for output that is displayed
170 interactively, and not for output which is redirected."
174 (defcustom eshell-preoutput-filter-functions nil
175 "*Functions to call before output is inserted into the buffer.
176 These functions get one argument, a string containing the text to be
177 inserted. They return the string as it should be inserted."
181 (defcustom eshell-password-prompt-regexp
182 "[Pp]ass\\(word\\|phrase\\).*:\\s *\\'"
183 "*Regexp matching prompts for passwords in the inferior process.
184 This is used by `eshell-watch-for-password-prompt'."
188 (defcustom eshell-skip-prompt-function nil
189 "*A function called from beginning of line to skip the prompt."
190 :type
'(choice (const nil
) function
)
193 (defcustom eshell-status-in-modeline t
194 "*If non-nil, let the user know a command is running in the modeline."
198 (defvar eshell-first-time-p t
199 "A variable which is non-nil the first time Eshell is loaded.")
201 ;; Internal Variables:
203 ;; these are only set to `nil' initially for the sake of the
204 ;; byte-compiler, when compiling other files which `require' this one
205 (defvar eshell-mode nil
)
206 (defvar eshell-mode-map nil
)
207 (defvar eshell-command-running-string
"--")
208 (defvar eshell-command-map nil
)
209 (defvar eshell-command-prefix nil
)
210 (defvar eshell-last-input-start nil
)
211 (defvar eshell-last-input-end nil
)
212 (defvar eshell-last-output-start nil
)
213 (defvar eshell-last-output-block-begin nil
)
214 (defvar eshell-last-output-end nil
)
216 (defvar eshell-currently-handling-window nil
)
217 (defvar eshell-mode-syntax-table nil
)
218 (defvar eshell-mode-abbrev-table nil
)
220 (define-abbrev-table 'eshell-mode-abbrev-table
())
222 (if (not eshell-mode-syntax-table
)
224 (setq eshell-mode-syntax-table
(make-syntax-table))
226 (modify-syntax-entry i
"_ " eshell-mode-syntax-table
)
230 (modify-syntax-entry i
"_ " eshell-mode-syntax-table
)
234 (modify-syntax-entry i
"_ " eshell-mode-syntax-table
)
238 (modify-syntax-entry i
"_ " eshell-mode-syntax-table
)
240 (modify-syntax-entry ?
" " eshell-mode-syntax-table
)
241 (modify-syntax-entry ?
\t " " eshell-mode-syntax-table
)
242 (modify-syntax-entry ?
\f " " eshell-mode-syntax-table
)
243 (modify-syntax-entry ?
\n "> " eshell-mode-syntax-table
)
244 ;; Give CR the same syntax as newline, for selective-display.
245 (modify-syntax-entry ?\^m
"> " eshell-mode-syntax-table
)
246 ;;; (modify-syntax-entry ?\; "< " eshell-mode-syntax-table)
247 (modify-syntax-entry ?
` "' " eshell-mode-syntax-table
)
248 (modify-syntax-entry ?
' "' " eshell-mode-syntax-table
)
249 (modify-syntax-entry ?
, "' " eshell-mode-syntax-table
)
250 ;; Used to be singlequote; changed for flonums.
251 (modify-syntax-entry ?.
"_ " eshell-mode-syntax-table
)
252 (modify-syntax-entry ?-
"_ " eshell-mode-syntax-table
)
253 (modify-syntax-entry ?|
". " eshell-mode-syntax-table
)
254 (modify-syntax-entry ?
# "' " eshell-mode-syntax-table
)
255 (modify-syntax-entry ?
\" "\" " eshell-mode-syntax-table
)
256 (modify-syntax-entry ?
\\ "/ " eshell-mode-syntax-table
)
257 (modify-syntax-entry ?\
( "() " eshell-mode-syntax-table
)
258 (modify-syntax-entry ?\
) ")( " eshell-mode-syntax-table
)
259 (modify-syntax-entry ?\
{ "(} " eshell-mode-syntax-table
)
260 (modify-syntax-entry ?\
} "){ " eshell-mode-syntax-table
)
261 (modify-syntax-entry ?\
[ "(] " eshell-mode-syntax-table
)
262 (modify-syntax-entry ?\
] ")[ " eshell-mode-syntax-table
)
263 ;; All non-word multibyte characters should be `symbol'.
264 (if (featurep 'xemacs
)
268 (and (characterp key
)
269 (>= (char-int key
) 256)
270 (/= (char-syntax key
) ?w
)
271 (modify-syntax-entry key
"_ "
272 eshell-mode-syntax-table
))))
273 (standard-syntax-table))
278 (and (>= (car key
) 128)
279 (/= (char-syntax (car key
)) ?w
))
281 (/= (char-syntax key
) ?w
)))
282 (modify-syntax-entry key
"_ "
283 eshell-mode-syntax-table
))))
284 (standard-syntax-table)))))
289 (defun eshell-mode ()
290 "Emacs shell interactive mode.
293 (kill-all-local-variables)
295 (setq major-mode
'eshell-mode
)
296 (setq mode-name
"EShell")
297 (set (make-local-variable 'eshell-mode
) t
)
299 (make-local-variable 'eshell-mode-map
)
300 (setq eshell-mode-map
(make-sparse-keymap))
301 (use-local-map eshell-mode-map
)
303 (when eshell-status-in-modeline
304 (make-local-variable 'eshell-command-running-string
)
305 (let ((fmt (copy-sequence mode-line-format
)))
306 (make-local-variable 'mode-line-format
)
307 (setq mode-line-format fmt
))
308 (let ((modeline (memq 'mode-line-modified mode-line-format
)))
310 (setcar modeline
'eshell-command-running-string
))))
312 (define-key eshell-mode-map
[return] 'eshell-send-input)
313 (define-key eshell-mode-map [(control ?m)] 'eshell-send-input)
314 (define-key eshell-mode-map [(control ?j)] 'eshell-send-input)
315 (define-key eshell-mode-map [(meta return)] 'eshell-queue-input)
316 (define-key eshell-mode-map [(meta control ?m)] 'eshell-queue-input)
317 (define-key eshell-mode-map [(meta control ?l)] 'eshell-show-output)
319 (set (make-local-variable 'eshell-command-prefix)
320 (make-symbol "eshell-command-prefix"))
321 (fset eshell-command-prefix (make-sparse-keymap))
322 (set (make-local-variable 'eshell-command-map)
323 (symbol-function eshell-command-prefix))
324 (define-key eshell-mode-map [(control ?c)] eshell-command-prefix)
326 ;; without this, find-tag complains about read-only text being
328 (if (eq (key-binding [(meta ?.)]) 'find-tag)
329 (define-key eshell-mode-map [(meta ?.)] 'eshell-find-tag))
330 (define-key eshell-command-map [(meta ?o)] 'eshell-mark-output)
331 (define-key eshell-command-map [(meta ?d)] 'eshell-toggle-direct-send)
333 (define-key eshell-command-map [(control ?a)] 'eshell-bol)
334 (define-key eshell-command-map [(control ?b)] 'eshell-backward-argument)
335 (define-key eshell-command-map [(control ?e)] 'eshell-show-maximum-output)
336 (define-key eshell-command-map [(control ?f)] 'eshell-forward-argument)
337 (define-key eshell-command-map [return] 'eshell-copy-old-input
)
338 (define-key eshell-command-map
[(control ?m
)] 'eshell-copy-old-input
)
339 (define-key eshell-command-map
[(control ?o
)] 'eshell-kill-output
)
340 (define-key eshell-command-map
[(control ?r
)] 'eshell-show-output
)
341 (define-key eshell-command-map
[(control ?t
)] 'eshell-truncate-buffer
)
342 (define-key eshell-command-map
[(control ?u
)] 'eshell-kill-input
)
343 (define-key eshell-command-map
[(control ?w
)] 'backward-kill-word
)
344 (define-key eshell-command-map
[(control ?y
)] 'eshell-repeat-argument
)
346 (setq local-abbrev-table eshell-mode-abbrev-table
)
347 (set-syntax-table eshell-mode-syntax-table
)
349 (set (make-local-variable 'dired-directory
) default-directory
)
350 (set (make-local-variable 'list-buffers-directory
)
351 (expand-file-name default-directory
))
353 ;; always set the tab width to 8 in Eshell buffers, since external
354 ;; commands which do their own formatting almost always expect this
355 (set (make-local-variable 'tab-width
) 8)
357 ;; don't ever use auto-fill in Eshell buffers
358 (setq auto-fill-function nil
)
360 ;; always display everything from a return value
361 (if (boundp 'print-length
)
362 (set (make-local-variable 'print-length
) nil
))
363 (if (boundp 'print-level
)
364 (set (make-local-variable 'print-level
) nil
))
366 ;; set require-final-newline to nil; otherwise, all redirected
367 ;; output will end with a newline, whether or not the source
369 (set (make-local-variable 'require-final-newline
) nil
)
371 (set (make-local-variable 'max-lisp-eval-depth
)
372 (max 3000 max-lisp-eval-depth
))
373 (set (make-local-variable 'max-specpdl-size
)
374 (max 6000 max-lisp-eval-depth
))
376 (set (make-local-variable 'eshell-last-input-start
) (point-marker))
377 (set (make-local-variable 'eshell-last-input-end
) (point-marker))
378 (set (make-local-variable 'eshell-last-output-start
) (point-marker))
379 (set (make-local-variable 'eshell-last-output-end
) (point-marker))
380 (set (make-local-variable 'eshell-last-output-block-begin
) (point))
382 (let ((modules-list (copy-sequence eshell-modules-list
)))
383 (make-local-variable 'eshell-modules-list
)
384 (setq eshell-modules-list modules-list
))
386 ;; load extension modules into memory. This will cause any global
387 ;; variables they define to be visible, since some of the core
388 ;; modules sometimes take advantage of their functionality if used.
389 (eshell-for module eshell-modules-list
390 (let ((module-fullname (symbol-name module
))
392 (if (string-match "^eshell-\\(.*\\)" module-fullname
)
393 (setq module-shortname
394 (concat "em-" (match-string 1 module-fullname
))))
395 (unless module-shortname
396 (error "Invalid Eshell module name: %s" module-fullname
))
397 (unless (featurep (intern module-shortname
))
398 (load module-shortname
))))
400 (unless (file-exists-p eshell-directory-name
)
401 (eshell-make-private-directory eshell-directory-name t
))
403 ;; load core Eshell modules for this session
404 (eshell-for module
(eshell-subgroups 'eshell
)
405 (run-hooks (intern-soft (concat (symbol-name module
)
408 ;; load extension modules for this session
409 (eshell-for module eshell-modules-list
410 (let ((load-hook (intern-soft (concat (symbol-name module
)
412 (if (and load-hook
(boundp load-hook
))
413 (run-hooks load-hook
))))
415 (if eshell-send-direct-to-subprocesses
416 (add-hook 'pre-command-hook
'eshell-intercept-commands t t
))
418 (if eshell-scroll-to-bottom-on-input
419 (add-hook 'pre-command-hook
'eshell-preinput-scroll-to-bottom t t
))
421 (when eshell-scroll-show-maximum-output
422 (set (make-local-variable 'scroll-conservatively
) 1000))
424 (when eshell-status-in-modeline
425 (add-hook 'eshell-pre-command-hook
'eshell-command-started nil t
)
426 (add-hook 'eshell-post-command-hook
'eshell-command-finished nil t
))
428 (add-hook 'kill-buffer-hook
431 (run-hooks 'eshell-exit-hook
))) t t
)
433 (if eshell-first-time-p
434 (run-hooks 'eshell-first-time-mode-hook
))
435 (run-mode-hooks 'eshell-mode-hook
)
436 (run-hooks 'eshell-post-command-hook
))
438 (put 'eshell-mode
'mode-class
'special
)
440 (eshell-deftest mode major-mode
441 "Major mode is correct"
442 (eq major-mode
'eshell-mode
))
444 (eshell-deftest mode eshell-mode-variable
445 "`eshell-mode' is true"
448 (eshell-deftest var window-height
449 "LINES equals window height"
450 (let ((eshell-stringify-t t
))
451 (eshell-command-result-p "= $LINES (window-height)" "t\n")))
453 (defun eshell-command-started ()
454 "Indicate in the modeline that a command has started."
455 (setq eshell-command-running-string
"**")
456 (force-mode-line-update))
458 (defun eshell-command-finished ()
459 "Indicate in the modeline that a command has finished."
460 (setq eshell-command-running-string
"--")
461 (force-mode-line-update))
463 (eshell-deftest mode command-running-p
464 "Modeline shows no command running"
465 (or (featurep 'xemacs
)
466 (not eshell-status-in-modeline
)
467 (and (memq 'eshell-command-running-string mode-line-format
)
468 (equal eshell-command-running-string
"--"))))
470 ;;; Internal Functions:
472 (defun eshell-toggle-direct-send ()
474 (if eshell-send-direct-to-subprocesses
476 (setq eshell-send-direct-to-subprocesses nil
)
477 (remove-hook 'pre-command-hook
'eshell-intercept-commands t
)
478 (message "Sending subprocess input on RET"))
479 (setq eshell-send-direct-to-subprocesses t
)
480 (add-hook 'pre-command-hook
'eshell-intercept-commands t t
)
481 (message "Sending subprocess input directly")))
483 (defun eshell-self-insert-command (N)
486 (eshell-interactive-process)
487 (char-to-string (if (symbolp last-command-char
)
488 (get last-command-char
'ascii-character
)
489 last-command-char
))))
491 (defun eshell-intercept-commands ()
492 (when (and (eshell-interactive-process)
493 (not (and (integerp last-input-event
)
494 (memq last-input-event
'(?\C-x ?\C-c
)))))
495 (let ((possible-events (where-is-internal this-command
))
496 (name (symbol-name this-command
))
498 ;; Assume that any multikey combination which does NOT target an
499 ;; Eshell command, is a combo the user wants invoked rather than
500 ;; sent to the underlying subprocess.
501 (unless (and (> (length name
) 7)
502 (equal (substring name
0 7) "eshell-"))
503 (while possible-events
504 (if (> (length (car possible-events
)) 1)
505 (setq intercept nil possible-events nil
)
506 (setq possible-events
(cdr possible-events
)))))
508 (setq this-command
'eshell-self-insert-command
)))))
510 (defun eshell-find-tag (&optional tagname next-p regexp-p
)
511 "A special version of `find-tag' that ignores read-onlyness."
514 (let ((inhibit-read-only t
)
516 (find-tag-default-function 'ignore
))
518 (setq tagname
(car (find-tag-interactive "Find tag: "))))
519 (find-tag tagname next-p regexp-p
)))
521 (defun eshell-move-argument (limit func property arg
)
522 "Move forward ARG arguments."
523 (catch 'eshell-incomplete
524 (eshell-parse-arguments (save-excursion (eshell-bol) (point))
525 (line-end-position)))
526 (let ((pos (save-excursion
528 (while (and (> arg
0) (/= (point) limit
))
529 (if (get-text-property (point) property
)
535 (if (and (eq func
'forward-char
)
539 (eshell-deftest arg forward-arg
540 "Move across command arguments"
541 (eshell-insert-command "echo $(+ 1 (- 4 3)) \"alpha beta\" file" 'ignore
)
542 (let ((here (point)) begin valid
)
545 (eshell-forward-argument 4)
546 (setq valid
(= here
(point)))
547 (eshell-backward-argument 4)
549 (and valid
(= begin
(point)))
551 (delete-region (point) (point-max)))))
553 (defun eshell-forward-argument (&optional arg
)
554 "Move forward ARG arguments."
556 (eshell-move-argument (point-max) 'forward-char
'arg-end arg
))
558 (defun eshell-backward-argument (&optional arg
)
559 "Move backward ARG arguments."
561 (eshell-move-argument (point-min) 'backward-char
'arg-begin arg
))
563 (defun eshell-repeat-argument (&optional arg
)
565 (let ((begin (save-excursion
566 (eshell-backward-argument arg
)
568 (kill-ring-save begin
(point))
572 "Goes to the beginning of line, then skips past the prompt, if any."
575 (and eshell-skip-prompt-function
576 (funcall eshell-skip-prompt-function
)))
578 (defsubst eshell-push-command-mark
()
579 "Push a mark at the end of the last input text."
580 (push-mark (1- eshell-last-input-end
) t
))
582 (custom-add-option 'eshell-pre-command-hook
'eshell-push-command-mark
)
584 (defsubst eshell-goto-input-start
()
585 "Goto the start of the last command input.
586 Putting this function on `eshell-pre-command-hook' will mimic Plan 9's
588 (goto-char eshell-last-input-start
))
590 (custom-add-option 'eshell-pre-command-hook
'eshell-push-command-mark
)
592 (defsubst eshell-interactive-print
(string)
593 "Print STRING to the eshell display buffer."
594 (eshell-output-filter nil string
))
596 (defsubst eshell-begin-on-new-line
()
597 "This function outputs a newline if not at beginning of line."
599 (goto-char eshell-last-output-end
)
601 (eshell-interactive-print "\n"))))
603 (defsubst eshell-reset
(&optional no-hooks
)
604 "Output a prompt on a new line, aborting any current input.
605 If NO-HOOKS is non-nil, then `eshell-post-command-hook' won't be run."
606 (goto-char (point-max))
607 (setq eshell-last-input-start
(point-marker)
608 eshell-last-input-end
(point-marker)
609 eshell-last-output-start
(point-marker)
610 eshell-last-output-block-begin
(point)
611 eshell-last-output-end
(point-marker))
612 (eshell-begin-on-new-line)
614 (run-hooks 'eshell-post-command-hook
)
615 (goto-char (point-max))))
617 (defun eshell-parse-command-input (beg end
&optional args
)
618 "Parse the command input from BEG to END.
619 The difference is that `eshell-parse-command' expects a complete
620 command string (and will error if it doesn't get one), whereas this
621 function will inform the caller whether more input is required.
623 If nil is returned, more input is necessary (probably because a
624 multi-line input string wasn't terminated properly). Otherwise, it
625 will return the parsed command."
628 (catch 'eshell-incomplete
630 (setq command
(eshell-parse-command (cons beg end
)
633 (message "Expecting completion of delimeter %c ..."
639 (defun eshell-update-markers (pmark)
640 "Update the input and output markers relative to point and PMARK."
641 (set-marker eshell-last-input-start pmark
)
642 (set-marker eshell-last-input-end
(point))
643 (set-marker eshell-last-output-end
(point)))
645 (defun eshell-queue-input (&optional use-region
)
646 "Queue the current input text for execution by Eshell.
647 Particularly, don't send the text to the current process, even if it's
650 (eshell-send-input use-region t
))
652 (eshell-deftest mode queue-input
653 "Queue command input"
654 (eshell-insert-command "sleep 2")
655 (eshell-insert-command "echo alpha" 'eshell-queue-input
)
657 (while (and eshell-current-command
660 (setq count
(1- count
))))
661 (eshell-match-result "alpha\n"))
663 (defun eshell-send-input (&optional use-region queue-p no-newline
)
664 "Send the input received to Eshell for parsing and processing.
665 After `eshell-last-output-end', sends all text from that marker to
666 point as input. Before that marker, calls `eshell-get-old-input' to
667 retrieve old input, copies it to the end of the buffer, and sends it.
669 If USE-REGION is non-nil, the current region (between point and mark)
670 will be used as input.
672 If QUEUE-P is non-nil, input will be queued until the next prompt,
673 rather than sent to the currently active process. If no process, the
674 input is processed immediately.
676 If NO-NEWLINE is non-nil, the input is sent without an implied final
679 ;; Note that the input string does not include its terminal newline.
680 (let ((proc-running-p (and (eshell-interactive-process)
682 (inhibit-point-motion-hooks t
)
683 after-change-functions
)
684 (unless (and proc-running-p
685 (not (eq (process-status
686 (eshell-interactive-process)) 'run
)))
687 (if (or proc-running-p
688 (>= (point) eshell-last-output-end
))
689 (goto-char (point-max))
690 (let ((copy (eshell-get-old-input use-region
)))
691 (goto-char eshell-last-output-end
)
692 (insert-and-inherit copy
)))
693 (unless (or no-newline
694 (and eshell-send-direct-to-subprocesses
696 (insert-before-markers-and-inherit ?
\n))
699 (eshell-update-markers eshell-last-output-end
)
700 (if (or eshell-send-direct-to-subprocesses
701 (= eshell-last-input-start eshell-last-input-end
))
703 (process-send-string (eshell-interactive-process) "\n"))
704 (process-send-region (eshell-interactive-process)
705 eshell-last-input-start
706 eshell-last-input-end
)))
707 (if (= eshell-last-output-end
(point))
708 (run-hooks 'eshell-post-command-hook
)
710 (eshell-condition-case err
712 (setq input
(buffer-substring-no-properties
713 eshell-last-output-end
(1- (point))))
714 (run-hook-with-args 'eshell-expand-input-functions
715 eshell-last-output-end
(1- (point)))
716 (let ((cmd (eshell-parse-command-input
717 eshell-last-output-end
(1- (point)))))
719 (eshell-update-markers eshell-last-output-end
)
720 (setq input
(buffer-substring-no-properties
721 eshell-last-input-start
722 (1- eshell-last-input-end
)))
723 (run-hooks 'eshell-input-filter-functions
)
724 (and (catch 'eshell-terminal
726 (if (eshell-invoke-directly cmd input
)
728 (eshell-eval-command cmd input
))))
729 (eshell-life-is-too-much)))))
732 (run-hooks 'eshell-post-command-hook
)
736 (eshell-interactive-print
737 (concat (error-message-string err
) "\n"))
738 (run-hooks 'eshell-post-command-hook
)
739 (insert-and-inherit input
)))))))))
741 ; (eshell-deftest proc send-to-subprocess
742 ; "Send input to a subprocess"
743 ; ;; jww (1999-12-06): what about when bc is unavailable?
744 ; (if (not (eshell-search-path "bc"))
746 ; (eshell-insert-command "bc")
747 ; (eshell-insert-command "1 + 2")
752 ; (eshell-insert-command "quit")
755 (defsubst eshell-kill-new
()
756 "Add the last input text to the kill ring."
757 (kill-ring-save eshell-last-input-start eshell-last-input-end
))
759 (custom-add-option 'eshell-input-filter-functions
'eshell-kill-new
)
761 (defun eshell-output-filter (process string
)
762 "Send the output from PROCESS (STRING) to the interactive display.
763 This is done after all necessary filtering has been done."
764 (let ((oprocbuf (if process
(process-buffer process
)
766 (inhibit-point-motion-hooks t
)
767 after-change-functions
)
768 (let ((functions eshell-preoutput-filter-functions
))
769 (while (and functions string
)
770 (setq string
(funcall (car functions
) string
))
771 (setq functions
(cdr functions
))))
772 (if (and string oprocbuf
(buffer-name oprocbuf
))
773 (let (opoint obeg oend
)
774 (with-current-buffer oprocbuf
775 (setq opoint
(point))
776 (setq obeg
(point-min))
777 (setq oend
(point-max))
778 (let ((buffer-read-only nil
)
779 (nchars (length string
))
782 (goto-char eshell-last-output-end
)
783 (setq ostart
(point))
784 (if (<= (point) opoint
)
785 (setq opoint
(+ opoint nchars
)))
787 (setq obeg
(+ obeg nchars
)))
788 (if (<= (point) oend
)
789 (setq oend
(+ oend nchars
)))
790 (insert-before-markers string
)
791 (if (= (window-start (selected-window)) (point))
792 (set-window-start (selected-window)
794 (if (= (point) eshell-last-input-end
)
795 (set-marker eshell-last-input-end
796 (- eshell-last-input-end nchars
)))
797 (set-marker eshell-last-output-start ostart
)
798 (set-marker eshell-last-output-end
(point))
799 (force-mode-line-update))
800 (narrow-to-region obeg oend
)
802 (eshell-run-output-filters))))))
804 (defun eshell-run-output-filters ()
805 "Run the `eshell-output-filter-functions' on the current output."
807 (run-hooks 'eshell-output-filter-functions
))
808 (setq eshell-last-output-block-begin
809 (marker-position eshell-last-output-end
)))
811 ;;; jww (1999-10-23): this needs testing
812 (defun eshell-preinput-scroll-to-bottom ()
813 "Go to the end of buffer in all windows showing it.
814 Movement occurs if point in the selected window is not after the
815 process mark, and `this-command' is an insertion command. Insertion
816 commands recognized are `self-insert-command', `yank', and
817 `hilit-yank'. Depends on the value of
818 `eshell-scroll-to-bottom-on-input'.
820 This function should be a pre-command hook."
821 (if (memq this-command
'(self-insert-command yank hilit-yank
))
822 (let* ((selected (selected-window))
823 (current (current-buffer))
824 (scroll eshell-scroll-to-bottom-on-input
))
825 (if (< (point) eshell-last-output-end
)
826 (if (eq scroll
'this
)
827 (goto-char (point-max))
831 (when (and (eq (window-buffer window
) current
)
832 (or (eq scroll t
) (eq scroll
'all
)))
833 (select-window window
)
834 (goto-char (point-max))
835 (select-window selected
))))
838 ;;; jww (1999-10-23): this needs testing
839 (defun eshell-postoutput-scroll-to-bottom ()
840 "Go to the end of buffer in all windows showing it.
841 Does not scroll if the current line is the last line in the buffer.
842 Depends on the value of `eshell-scroll-to-bottom-on-output' and
843 `eshell-scroll-show-maximum-output'.
845 This function should be in the list `eshell-output-filter-functions'."
846 (let* ((selected (selected-window))
847 (current (current-buffer))
848 (scroll eshell-scroll-to-bottom-on-output
))
853 (if (eq (window-buffer window
) current
)
855 (select-window window
)
856 (if (and (< (point) eshell-last-output-end
)
857 (or (eq scroll t
) (eq scroll
'all
)
858 ;; Maybe user wants point to jump to end.
859 (and (eq scroll
'this
)
860 (eq selected window
))
861 (and (eq scroll
'others
)
862 (not (eq selected window
)))
863 ;; If point was at the end, keep it at end.
864 (>= (point) eshell-last-output-start
)))
865 (goto-char eshell-last-output-end
))
866 ;; Optionally scroll so that the text
867 ;; ends at the bottom of the window.
868 (if (and eshell-scroll-show-maximum-output
869 (>= (point) eshell-last-output-end
))
871 (goto-char (point-max))
873 (select-window selected
)))))
875 (set-buffer current
))))
877 (defun eshell-beginning-of-input ()
878 "Return the location of the start of the previous input."
879 eshell-last-input-start
)
881 (defun eshell-beginning-of-output ()
882 "Return the location of the end of the previous output block."
883 eshell-last-input-end
)
885 (defun eshell-end-of-output ()
886 "Return the location of the end of the previous output block."
887 (if (eshell-using-module 'eshell-prompt
)
888 eshell-last-output-start
889 eshell-last-output-end
))
891 (defun eshell-kill-output ()
892 "Kill all output from interpreter since last input.
893 Does not delete the prompt."
896 (goto-char (eshell-beginning-of-output))
897 (insert "*** output flushed ***\n")
898 (delete-region (point) (eshell-end-of-output))))
900 (eshell-deftest io flush-output
901 "Flush previous output"
902 (eshell-insert-command "echo alpha")
904 (and (eshell-match-result (regexp-quote "*** output flushed ***\n"))
906 (= (point) eshell-last-output-start
)))
908 (defun eshell-show-output (&optional arg
)
909 "Display start of this batch of interpreter output at top of window.
910 Sets mark to the value of point when this command is run.
911 With a prefix argument, narrows region to last command output."
913 (goto-char (eshell-beginning-of-output))
914 (set-window-start (selected-window)
916 (goto-char (eshell-beginning-of-input))
917 (line-beginning-position)))
919 (narrow-to-region (eshell-beginning-of-output)
920 (eshell-end-of-output)))
921 (eshell-end-of-output))
923 (defun eshell-mark-output (&optional arg
)
924 "Display start of this batch of interpreter output at top of window.
925 Sets mark to the value of point when this command is run.
926 With a prefix argument, narrows region to last command output."
928 (push-mark (eshell-show-output arg
)))
930 (defun eshell-kill-input ()
931 "Kill all text from last stuff output by interpreter to point."
933 (if (> (point) eshell-last-output-end
)
934 (kill-region eshell-last-output-end
(point))
935 (let ((here (point)))
937 (kill-region (point) here
))))
939 (defun eshell-show-maximum-output (&optional interactive
)
940 "Put the end of the buffer at the bottom of the window.
941 When run interactively, widen the buffer first."
945 (goto-char (point-max))
948 (defun eshell-get-old-input (&optional use-current-region
)
949 "Return the command input on the current line."
950 (if use-current-region
951 (buffer-substring (min (point) (mark))
952 (max (point) (mark)))
955 (and eshell-skip-prompt-function
956 (funcall eshell-skip-prompt-function
))
959 (buffer-substring beg
(point))))))
961 (defun eshell-copy-old-input ()
962 "Insert after prompt old input at point as new input to be edited."
964 (let ((input (eshell-get-old-input)))
965 (goto-char eshell-last-output-end
)
966 (insert-and-inherit input
)))
968 (eshell-deftest mode run-old-command
969 "Re-run an old command"
970 (eshell-insert-command "echo alpha")
971 (goto-char eshell-last-input-start
)
972 (string= (eshell-get-old-input) "echo alpha"))
974 (defun eshell/exit
()
975 "Leave or kill the Eshell buffer, depending on `eshell-kill-on-exit'."
976 (throw 'eshell-terminal t
))
978 (defun eshell-life-is-too-much ()
979 "Kill the current buffer (or bury it). Good-bye Eshell."
981 (if (not eshell-kill-on-exit
)
983 (kill-buffer (current-buffer))))
985 (defun eshell-truncate-buffer ()
986 "Truncate the buffer to `eshell-buffer-maximum-lines'.
987 This function could be on `eshell-output-filter-functions' or bound to
991 (goto-char eshell-last-output-end
)
992 (let ((lines (count-lines 1 (point)))
993 (inhibit-read-only t
))
994 (forward-line (- eshell-buffer-maximum-lines
))
999 (message "Buffer too short to truncate"))
1000 (delete-region (point-min) (point))
1002 (message "Truncated buffer from %d to %d lines (%.1fk freed)"
1003 lines eshell-buffer-maximum-lines
1004 (/ pos
1024.0))))))))
1006 (custom-add-option 'eshell-output-filter-functions
1007 'eshell-truncate-buffer
)
1009 (defun eshell-send-invisible (str)
1010 "Read a string without echoing.
1011 Then send it to the process running in the current buffer."
1012 (interactive "P") ; Defeat snooping via C-x ESC ESC
1013 (let ((str (read-passwd
1014 (format "%s Password: "
1015 (process-name (eshell-interactive-process))))))
1017 (process-send-string (eshell-interactive-process)
1019 (message "Warning: text will be echoed"))))
1021 (defun eshell-watch-for-password-prompt ()
1022 "Prompt in the minibuffer for password and send without echoing.
1023 This function uses `eshell-send-invisible' to read and send a password to the
1024 buffer's process if STRING contains a password prompt defined by
1025 `eshell-password-prompt-regexp'.
1027 This function could be in the list `eshell-output-filter-functions'."
1028 (when (eshell-interactive-process)
1030 (goto-char eshell-last-output-block-begin
)
1032 (if (re-search-forward eshell-password-prompt-regexp
1033 eshell-last-output-end t
)
1034 (eshell-send-invisible nil
)))))
1036 (custom-add-option 'eshell-output-filter-functions
1037 'eshell-watch-for-password-prompt
)
1039 (defun eshell-handle-control-codes ()
1040 "Act properly when certain control codes are seen."
1042 (let ((orig (point)))
1043 (goto-char eshell-last-output-block-begin
)
1045 (beginning-of-line))
1046 (while (< (point) eshell-last-output-end
)
1047 (let ((char (char-after)))
1050 (if (< (1+ (point)) eshell-last-output-end
)
1051 (if (memq (char-after (1+ (point)))
1054 (let ((end (1+ (point))))
1056 (delete-region (point) end
)))
1057 (add-text-properties (point) (1+ (point))
1064 (delete-backward-char 1)
1067 (forward-char))))))))
1069 (custom-add-option 'eshell-output-filter-functions
1070 'eshell-handle-control-codes
)
1072 (defun eshell-handle-ansi-color ()
1073 "Handle ANSI color codes."
1074 (eval-and-compile (require 'ansi-color
))
1075 (ansi-color-apply-on-region eshell-last-output-start
1076 eshell-last-output-end
))
1078 (custom-add-option 'eshell-output-filter-functions
1079 'eshell-handle-ansi-color
)
1083 ;; arch-tag: ec65bc2b-da14-4547-81d3-a32af3a4dc57
1084 ;;; esh-mode.el ends here