* lisp/emacs-lisp/package.el (package--with-work-buffer-async):
[emacs.git] / lisp / eshell / esh-mode.el
blob22ccf4e91f95379e7a696eb9b4035378d3d39212
1 ;;; esh-mode.el --- user interface -*- lexical-binding:t -*-
3 ;; Copyright (C) 1999-2015 Free Software Foundation, Inc.
5 ;; Author: John Wiegley <johnw@gnu.org>
7 ;; This file is part of GNU Emacs.
9 ;; GNU Emacs is free software: you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation, either version 3 of the License, or
12 ;; (at your option) any later version.
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
22 ;;; Commentary:
24 ;; Basically, Eshell is used just like shell mode (<M-x shell>). The
25 ;; keystrokes for navigating the buffer, and accessing the command
26 ;; history, are identical. Unlike shell mode, however, Eshell mode's
27 ;; governing process is Emacs itself. With shell mode, an inferior
28 ;; shell process is executed that communicates with Emacs via comint
29 ;; -- a mode for handling sub-process interaction. Eshell mode, on
30 ;; the other hand, is a truly native Emacs shell. No subprocess are
31 ;; invoked except the ones requested by the user at the prompt.
33 ;; After entering a command, use <RET> to invoke it ([Command
34 ;; invocation]) . If there is a command on disk, it will be executed
35 ;; as in a normal shell. If there is no command by that name on disk,
36 ;; but a Lisp function with that name is defined, the Lisp function
37 ;; will be called, using the arguments passed on the command line.
39 ;; Some of the other features of the command interaction mode are:
41 ;; @ <M-RET> can be used to accumulate further commands while a
42 ;; command is currently running. Since all input is passed to the
43 ;; subprocess being executed, there is no automatic input queueing
44 ;; as there is with other shells.
46 ;; @ <C-c C-t> can be used to truncate the buffer if it grows too
47 ;; large.
49 ;; @ <C-c C-r> will move point to the beginning of the output of the
50 ;; last command. With a prefix argument, it will narrow to view
51 ;; only that output.
53 ;; @ <C-c C-o> will delete the output from the last command.
55 ;; @ <C-c C-f> will move forward a complete shell argument.
57 ;; @ <C-c C-b> will move backward a complete shell argument.
59 ;;; Code:
61 (provide 'esh-mode)
63 (require 'esh-util)
64 (require 'esh-module)
65 (require 'esh-cmd)
66 (require 'esh-io)
67 (require 'esh-var)
69 (defgroup eshell-mode nil
70 "This module contains code for handling input from the user."
71 :tag "User interface"
72 :group 'eshell)
74 ;;; User Variables:
76 (defcustom eshell-mode-unload-hook nil
77 "A hook that gets run when `eshell-mode' is unloaded."
78 :type 'hook
79 :group 'eshell-mode)
81 (defcustom eshell-mode-hook nil
82 "A hook that gets run when `eshell-mode' is entered."
83 :type 'hook
84 :group 'eshell-mode)
86 (defcustom eshell-first-time-mode-hook nil
87 "A hook that gets run the first time `eshell-mode' is entered.
88 That is to say, the first time during an Emacs session."
89 :type 'hook
90 :group 'eshell-mode)
92 (defcustom eshell-exit-hook nil
93 "A hook that is run whenever `eshell' is exited.
94 This hook is only run if exiting actually kills the buffer."
95 :version "24.1" ; removed eshell-query-kill-processes
96 :type 'hook
97 :group 'eshell-mode)
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."
102 :type 'boolean
103 :group 'eshell-mode)
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'."
109 :type 'hook
110 :group 'eshell-mode)
112 (defcustom eshell-send-direct-to-subprocesses nil
113 "If t, send any input immediately to a subprocess."
114 :type 'boolean
115 :group 'eshell-mode)
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
120 current input text."
121 :type 'hook
122 :group 'eshell-mode)
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))
133 :group 'eshell-mode)
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))
147 :group 'eshell-mode)
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'."
155 :type 'boolean
156 :group 'eshell-mode)
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'."
163 :type 'integer
164 :group 'eshell-mode)
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."
174 :type 'hook
175 :group 'eshell-mode)
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."
181 :type 'hook
182 :group 'eshell-mode)
184 (defcustom eshell-password-prompt-regexp
185 (format "\\(%s\\).*:\\s *\\'" (regexp-opt password-word-equivalents))
186 "Regexp matching prompts for passwords in the inferior process.
187 This is used by `eshell-watch-for-password-prompt'."
188 :type 'regexp
189 :group 'eshell-mode)
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)
194 :group 'eshell-mode)
196 (define-obsolete-variable-alias 'eshell-status-in-modeline
197 'eshell-status-in-mode-line "24.3")
199 (defcustom eshell-status-in-mode-line t
200 "If non-nil, let the user know a command is running in the mode line."
201 :type 'boolean
202 :group 'eshell-mode)
204 (defvar eshell-first-time-p t
205 "A variable which is non-nil the first time Eshell is loaded.")
207 ;; Internal Variables:
209 ;; these are only set to nil initially for the sake of the
210 ;; byte-compiler, when compiling other files which `require' this one
211 (defvar eshell-mode nil)
212 (defvar eshell-mode-map nil)
213 (defvar eshell-command-running-string "--")
214 (defvar eshell-command-map nil)
215 (defvar eshell-command-prefix nil)
216 (defvar eshell-last-input-start nil)
217 (defvar eshell-last-input-end nil)
218 (defvar eshell-last-output-start nil)
219 (defvar eshell-last-output-block-begin nil)
220 (defvar eshell-last-output-end nil)
222 (defvar eshell-currently-handling-window nil)
224 (define-abbrev-table 'eshell-mode-abbrev-table ())
226 (defvar eshell-mode-syntax-table
227 (let ((st (make-syntax-table))
228 (i 0))
229 (while (< i ?0)
230 (modify-syntax-entry i "_ " st)
231 (setq i (1+ i)))
232 (setq i (1+ ?9))
233 (while (< i ?A)
234 (modify-syntax-entry i "_ " st)
235 (setq i (1+ i)))
236 (setq i (1+ ?Z))
237 (while (< i ?a)
238 (modify-syntax-entry i "_ " st)
239 (setq i (1+ i)))
240 (setq i (1+ ?z))
241 (while (< i 128)
242 (modify-syntax-entry i "_ " st)
243 (setq i (1+ i)))
244 (modify-syntax-entry ? " " st)
245 (modify-syntax-entry ?\t " " st)
246 (modify-syntax-entry ?\f " " st)
247 (modify-syntax-entry ?\n "> " st)
248 ;; Give CR the same syntax as newline, for selective-display.
249 (modify-syntax-entry ?\^m "> " st)
250 ;; (modify-syntax-entry ?\; "< " st)
251 (modify-syntax-entry ?` "' " st)
252 (modify-syntax-entry ?' "' " st)
253 (modify-syntax-entry ?, "' " st)
254 ;; Used to be singlequote; changed for flonums.
255 (modify-syntax-entry ?. "_ " st)
256 (modify-syntax-entry ?- "_ " st)
257 (modify-syntax-entry ?| ". " st)
258 (modify-syntax-entry ?# "' " st)
259 (modify-syntax-entry ?\" "\" " st)
260 (modify-syntax-entry ?\\ "/ " st)
261 (modify-syntax-entry ?\( "() " st)
262 (modify-syntax-entry ?\) ")( " st)
263 (modify-syntax-entry ?\{ "(} " st)
264 (modify-syntax-entry ?\} "){ " st)
265 (modify-syntax-entry ?\[ "(] " st)
266 (modify-syntax-entry ?\] ")[ " st)
267 ;; All non-word multibyte characters should be `symbol'.
268 (map-char-table
269 (if (featurep 'xemacs)
270 (lambda (key _val)
271 (and (characterp key)
272 (>= (char-int key) 256)
273 (/= (char-syntax key) ?w)
274 (modify-syntax-entry key "_ " st)))
275 (lambda (key _val)
276 (and (if (consp key)
277 (and (>= (car key) 128)
278 (/= (char-syntax (car key)) ?w))
279 (and (>= key 256)
280 (/= (char-syntax key) ?w)))
281 (modify-syntax-entry key "_ " st))))
282 (standard-syntax-table))
283 st))
285 ;;; User Functions:
287 (defun eshell-kill-buffer-function ()
288 "Function added to `kill-buffer-hook' in Eshell buffers.
289 This runs the function `eshell-kill-processes-on-exit',
290 and the hook `eshell-exit-hook'."
291 ;; It's fine to run this unconditionally since it can be customized
292 ;; via the `eshell-kill-processes-on-exit' variable.
293 (and (fboundp 'eshell-query-kill-processes)
294 (not (memq 'eshell-query-kill-processes eshell-exit-hook))
295 (eshell-query-kill-processes))
296 (run-hooks 'eshell-exit-hook))
298 ;;;###autoload
299 (define-derived-mode eshell-mode fundamental-mode "EShell"
300 "Emacs shell interactive mode."
301 (setq-local eshell-mode t)
303 ;; FIXME: What the hell!?
304 (setq-local eshell-mode-map (make-sparse-keymap))
305 (use-local-map eshell-mode-map)
307 (when eshell-status-in-mode-line
308 (make-local-variable 'eshell-command-running-string)
309 (let ((fmt (copy-sequence mode-line-format)))
310 (setq-local mode-line-format fmt))
311 (let ((mode-line-elt (memq 'mode-line-modified mode-line-format)))
312 (if mode-line-elt
313 (setcar mode-line-elt 'eshell-command-running-string))))
315 (define-key eshell-mode-map "\r" 'eshell-send-input)
316 (define-key eshell-mode-map "\M-\r" 'eshell-queue-input)
317 (define-key eshell-mode-map [(meta control ?l)] 'eshell-show-output)
318 (define-key eshell-mode-map [(control ?a)] 'eshell-bol)
320 (setq-local eshell-command-prefix (make-symbol "eshell-command-prefix"))
321 (fset eshell-command-prefix (make-sparse-keymap))
322 (setq-local eshell-command-map (symbol-function eshell-command-prefix))
323 (define-key eshell-mode-map [(control ?c)] eshell-command-prefix)
325 ;; without this, find-tag complains about read-only text being
326 ;; modified
327 (if (eq (key-binding [(meta ?.)]) 'find-tag)
328 (define-key eshell-mode-map [(meta ?.)] 'eshell-find-tag))
329 (define-key eshell-command-map [(meta ?o)] 'eshell-mark-output)
330 (define-key eshell-command-map [(meta ?d)] 'eshell-toggle-direct-send)
332 (define-key eshell-command-map [(control ?a)] 'eshell-bol)
333 (define-key eshell-command-map [(control ?b)] 'eshell-backward-argument)
334 (define-key eshell-command-map [(control ?e)] 'eshell-show-maximum-output)
335 (define-key eshell-command-map [(control ?f)] 'eshell-forward-argument)
336 (define-key eshell-command-map [return] 'eshell-copy-old-input)
337 (define-key eshell-command-map [(control ?m)] 'eshell-copy-old-input)
338 (define-key eshell-command-map [(control ?o)] 'eshell-kill-output)
339 (define-key eshell-command-map [(control ?r)] 'eshell-show-output)
340 (define-key eshell-command-map [(control ?t)] 'eshell-truncate-buffer)
341 (define-key eshell-command-map [(control ?u)] 'eshell-kill-input)
342 (define-key eshell-command-map [(control ?w)] 'backward-kill-word)
343 (define-key eshell-command-map [(control ?y)] 'eshell-repeat-argument)
345 (setq local-abbrev-table eshell-mode-abbrev-table)
347 (set (make-local-variable 'list-buffers-directory)
348 (expand-file-name default-directory))
350 ;; always set the tab width to 8 in Eshell buffers, since external
351 ;; commands which do their own formatting almost always expect this
352 (set (make-local-variable 'tab-width) 8)
354 ;; don't ever use auto-fill in Eshell buffers
355 (setq auto-fill-function nil)
357 ;; always display everything from a return value
358 (if (boundp 'print-length)
359 (set (make-local-variable 'print-length) nil))
360 (if (boundp 'print-level)
361 (set (make-local-variable 'print-level) nil))
363 ;; set require-final-newline to nil; otherwise, all redirected
364 ;; output will end with a newline, whether or not the source
365 ;; indicated it!
366 (set (make-local-variable 'require-final-newline) nil)
368 (set (make-local-variable 'max-lisp-eval-depth)
369 (max 3000 max-lisp-eval-depth))
370 (set (make-local-variable 'max-specpdl-size)
371 (max 6000 max-lisp-eval-depth))
373 (set (make-local-variable 'eshell-last-input-start) (point-marker))
374 (set (make-local-variable 'eshell-last-input-end) (point-marker))
375 (set (make-local-variable 'eshell-last-output-start) (point-marker))
376 (set (make-local-variable 'eshell-last-output-end) (point-marker))
377 (set (make-local-variable 'eshell-last-output-block-begin) (point))
379 (let ((modules-list (copy-sequence eshell-modules-list)))
380 (make-local-variable 'eshell-modules-list)
381 (setq eshell-modules-list modules-list))
383 ;; load extension modules into memory. This will cause any global
384 ;; variables they define to be visible, since some of the core
385 ;; modules sometimes take advantage of their functionality if used.
386 (dolist (module eshell-modules-list)
387 (let ((module-fullname (symbol-name module))
388 module-shortname)
389 (if (string-match "^eshell-\\(.*\\)" module-fullname)
390 (setq module-shortname
391 (concat "em-" (match-string 1 module-fullname))))
392 (unless module-shortname
393 (error "Invalid Eshell module name: %s" module-fullname))
394 (unless (featurep (intern module-shortname))
395 (load module-shortname))))
397 (unless (file-exists-p eshell-directory-name)
398 (eshell-make-private-directory eshell-directory-name t))
400 ;; Load core Eshell modules, then extension modules, for this session.
401 (dolist (module (append (eshell-subgroups 'eshell) eshell-modules-list))
402 (let ((load-hook (intern-soft (format "%s-load-hook" module)))
403 (initfunc (intern-soft (format "%s-initialize" module))))
404 (when (and load-hook (boundp load-hook))
405 (if (memq initfunc (symbol-value load-hook)) (setq initfunc nil))
406 (run-hooks load-hook))
407 ;; So we don't need the -initialize functions on the hooks (b#5375).
408 (and initfunc (fboundp initfunc) (funcall initfunc))))
410 (if eshell-send-direct-to-subprocesses
411 (add-hook 'pre-command-hook 'eshell-intercept-commands t t))
413 (if eshell-scroll-to-bottom-on-input
414 (add-hook 'pre-command-hook 'eshell-preinput-scroll-to-bottom t t))
416 (when eshell-scroll-show-maximum-output
417 (set (make-local-variable 'scroll-conservatively) 1000))
419 (when eshell-status-in-mode-line
420 (add-hook 'eshell-pre-command-hook 'eshell-command-started nil t)
421 (add-hook 'eshell-post-command-hook 'eshell-command-finished nil t))
423 (add-hook 'kill-buffer-hook 'eshell-kill-buffer-function t t)
425 (if eshell-first-time-p
426 (run-hooks 'eshell-first-time-mode-hook))
427 (run-hooks 'eshell-post-command-hook))
429 (put 'eshell-mode 'mode-class 'special)
431 (defun eshell-command-started ()
432 "Indicate in the mode line that a command has started."
433 (setq eshell-command-running-string "**")
434 (force-mode-line-update))
436 (defun eshell-command-finished ()
437 "Indicate in the mode line that a command has finished."
438 (setq eshell-command-running-string "--")
439 (force-mode-line-update))
441 ;;; Internal Functions:
443 (defun eshell-toggle-direct-send ()
444 (interactive)
445 (if eshell-send-direct-to-subprocesses
446 (progn
447 (setq eshell-send-direct-to-subprocesses nil)
448 (remove-hook 'pre-command-hook 'eshell-intercept-commands t)
449 (message "Sending subprocess input on RET"))
450 (setq eshell-send-direct-to-subprocesses t)
451 (add-hook 'pre-command-hook 'eshell-intercept-commands t t)
452 (message "Sending subprocess input directly")))
454 (defun eshell-self-insert-command ()
455 (interactive)
456 (process-send-string
457 (eshell-interactive-process)
458 (char-to-string (if (symbolp last-command-event)
459 (get last-command-event 'ascii-character)
460 last-command-event))))
462 (defun eshell-intercept-commands ()
463 (when (and (eshell-interactive-process)
464 (not (and (integerp last-input-event)
465 (memq last-input-event '(?\C-x ?\C-c)))))
466 (let ((possible-events (where-is-internal this-command))
467 (name (symbol-name this-command))
468 (intercept t))
469 ;; Assume that any multikey combination which does NOT target an
470 ;; Eshell command, is a combo the user wants invoked rather than
471 ;; sent to the underlying subprocess.
472 (unless (and (> (length name) 7)
473 (equal (substring name 0 7) "eshell-"))
474 (while possible-events
475 (if (> (length (car possible-events)) 1)
476 (setq intercept nil possible-events nil)
477 (setq possible-events (cdr possible-events)))))
478 (if intercept
479 (setq this-command 'eshell-self-insert-command)))))
481 (declare-function find-tag-interactive "etags" (prompt &optional no-default))
483 (defun eshell-find-tag (&optional tagname next-p regexp-p)
484 "A special version of `find-tag' that ignores whether the text is read-only."
485 (interactive)
486 (require 'etags)
487 (let ((inhibit-read-only t)
488 (no-default (eobp))
489 (find-tag-default-function 'ignore))
490 (setq tagname (car (find-tag-interactive "Find tag: " no-default)))
491 (find-tag tagname next-p regexp-p)))
493 (defun eshell-move-argument (limit func property arg)
494 "Move forward ARG arguments."
495 (catch 'eshell-incomplete
496 (eshell-parse-arguments (save-excursion (eshell-bol) (point))
497 (line-end-position)))
498 (let ((pos (save-excursion
499 (funcall func 1)
500 (while (and (> arg 0) (/= (point) limit))
501 (if (get-text-property (point) property)
502 (setq arg (1- arg)))
503 (if (> arg 0)
504 (funcall func 1)))
505 (point))))
506 (goto-char pos)
507 (if (and (eq func 'forward-char)
508 (= (1+ pos) limit))
509 (forward-char 1))))
511 (defun eshell-forward-argument (&optional arg)
512 "Move forward ARG arguments."
513 (interactive "p")
514 (eshell-move-argument (point-max) 'forward-char 'arg-end arg))
516 (defun eshell-backward-argument (&optional arg)
517 "Move backward ARG arguments."
518 (interactive "p")
519 (eshell-move-argument (point-min) 'backward-char 'arg-begin arg))
521 (defun eshell-repeat-argument (&optional arg)
522 (interactive "p")
523 (let ((begin (save-excursion
524 (eshell-backward-argument arg)
525 (point))))
526 (kill-ring-save begin (point))
527 (yank)))
529 (defun eshell-bol ()
530 "Goes to the beginning of line, then skips past the prompt, if any."
531 (interactive)
532 (beginning-of-line)
533 (and eshell-skip-prompt-function
534 (funcall eshell-skip-prompt-function)))
536 (defsubst eshell-push-command-mark ()
537 "Push a mark at the end of the last input text."
538 (push-mark (1- eshell-last-input-end) t))
540 (custom-add-option 'eshell-pre-command-hook 'eshell-push-command-mark)
542 (defsubst eshell-goto-input-start ()
543 "Goto the start of the last command input.
544 Putting this function on `eshell-pre-command-hook' will mimic Plan 9's
545 9term behavior."
546 (goto-char eshell-last-input-start))
548 (custom-add-option 'eshell-pre-command-hook 'eshell-push-command-mark)
550 (defsubst eshell-interactive-print (string)
551 "Print STRING to the eshell display buffer."
552 (eshell-output-filter nil string))
554 (defsubst eshell-begin-on-new-line ()
555 "This function outputs a newline if not at beginning of line."
556 (save-excursion
557 (goto-char eshell-last-output-end)
558 (or (bolp)
559 (eshell-interactive-print "\n"))))
561 (defsubst eshell-reset (&optional no-hooks)
562 "Output a prompt on a new line, aborting any current input.
563 If NO-HOOKS is non-nil, then `eshell-post-command-hook' won't be run."
564 (goto-char (point-max))
565 (setq eshell-last-input-start (point-marker)
566 eshell-last-input-end (point-marker)
567 eshell-last-output-start (point-marker)
568 eshell-last-output-block-begin (point)
569 eshell-last-output-end (point-marker))
570 (eshell-begin-on-new-line)
571 (unless no-hooks
572 (run-hooks 'eshell-post-command-hook)
573 (goto-char (point-max))))
575 (defun eshell-parse-command-input (beg end &optional args)
576 "Parse the command input from BEG to END.
577 The difference is that `eshell-parse-command' expects a complete
578 command string (and will error if it doesn't get one), whereas this
579 function will inform the caller whether more input is required.
581 If nil is returned, more input is necessary (probably because a
582 multi-line input string wasn't terminated properly). Otherwise, it
583 will return the parsed command."
584 (let (delim command)
585 (if (setq delim
586 (catch 'eshell-incomplete
587 (ignore
588 (setq command (eshell-parse-command (cons beg end)
589 args t)))))
590 (ignore
591 (message "Expecting completion of delimiter %c ..."
592 (if (listp delim)
593 (car delim)
594 delim)))
595 command)))
597 (defun eshell-update-markers (pmark)
598 "Update the input and output markers relative to point and PMARK."
599 (set-marker eshell-last-input-start pmark)
600 (set-marker eshell-last-input-end (point))
601 (set-marker eshell-last-output-end (point)))
603 (defun eshell-queue-input (&optional use-region)
604 "Queue the current input text for execution by Eshell.
605 Particularly, don't send the text to the current process, even if it's
606 waiting for input."
607 (interactive "P")
608 (eshell-send-input use-region t))
610 (defun eshell-send-input (&optional use-region queue-p no-newline)
611 "Send the input received to Eshell for parsing and processing.
612 After `eshell-last-output-end', sends all text from that marker to
613 point as input. Before that marker, calls `eshell-get-old-input' to
614 retrieve old input, copies it to the end of the buffer, and sends it.
616 If USE-REGION is non-nil, the current region (between point and mark)
617 will be used as input.
619 If QUEUE-P is non-nil, input will be queued until the next prompt,
620 rather than sent to the currently active process. If no process, the
621 input is processed immediately.
623 If NO-NEWLINE is non-nil, the input is sent without an implied final
624 newline."
625 (interactive "P")
626 ;; Note that the input string does not include its terminal newline.
627 (let ((proc-running-p (and (eshell-interactive-process)
628 (not queue-p)))
629 (inhibit-point-motion-hooks t)
630 after-change-functions)
631 (unless (and proc-running-p
632 (not (eq (process-status
633 (eshell-interactive-process)) 'run)))
634 (if (or proc-running-p
635 (>= (point) eshell-last-output-end))
636 (goto-char (point-max))
637 (let ((copy (eshell-get-old-input use-region)))
638 (goto-char eshell-last-output-end)
639 (insert-and-inherit copy)))
640 (unless (or no-newline
641 (and eshell-send-direct-to-subprocesses
642 proc-running-p))
643 (insert-before-markers-and-inherit ?\n))
644 (if proc-running-p
645 (progn
646 (eshell-update-markers eshell-last-output-end)
647 (if (or eshell-send-direct-to-subprocesses
648 (= eshell-last-input-start eshell-last-input-end))
649 (unless no-newline
650 (process-send-string (eshell-interactive-process) "\n"))
651 (process-send-region (eshell-interactive-process)
652 eshell-last-input-start
653 eshell-last-input-end)))
654 (if (= eshell-last-output-end (point))
655 (run-hooks 'eshell-post-command-hook)
656 (let (input)
657 (eshell-condition-case err
658 (progn
659 (setq input (buffer-substring-no-properties
660 eshell-last-output-end (1- (point))))
661 (run-hook-with-args 'eshell-expand-input-functions
662 eshell-last-output-end (1- (point)))
663 (let ((cmd (eshell-parse-command-input
664 eshell-last-output-end (1- (point)))))
665 (when cmd
666 (eshell-update-markers eshell-last-output-end)
667 (setq input (buffer-substring-no-properties
668 eshell-last-input-start
669 (1- eshell-last-input-end)))
670 (run-hooks 'eshell-input-filter-functions)
671 (and (catch 'eshell-terminal
672 (ignore
673 (if (eshell-invoke-directly cmd)
674 (eval cmd)
675 (eshell-eval-command cmd input))))
676 (eshell-life-is-too-much)))))
677 (quit
678 (eshell-reset t)
679 (run-hooks 'eshell-post-command-hook)
680 (signal 'quit nil))
681 (error
682 (eshell-reset t)
683 (eshell-interactive-print
684 (concat (error-message-string err) "\n"))
685 (run-hooks 'eshell-post-command-hook)
686 (insert-and-inherit input)))))))))
688 (defsubst eshell-kill-new ()
689 "Add the last input text to the kill ring."
690 (kill-ring-save eshell-last-input-start eshell-last-input-end))
692 (custom-add-option 'eshell-input-filter-functions 'eshell-kill-new)
694 (defun eshell-output-filter (process string)
695 "Send the output from PROCESS (STRING) to the interactive display.
696 This is done after all necessary filtering has been done."
697 (let ((oprocbuf (if process (process-buffer process)
698 (current-buffer)))
699 (inhibit-point-motion-hooks t)
700 after-change-functions)
701 (let ((functions eshell-preoutput-filter-functions))
702 (while (and functions string)
703 (setq string (funcall (car functions) string))
704 (setq functions (cdr functions))))
705 (if (and string oprocbuf (buffer-name oprocbuf))
706 (let (opoint obeg oend)
707 (with-current-buffer oprocbuf
708 (setq opoint (point))
709 (setq obeg (point-min))
710 (setq oend (point-max))
711 (let ((buffer-read-only nil)
712 (nchars (length string))
713 (ostart nil))
714 (widen)
715 (goto-char eshell-last-output-end)
716 (setq ostart (point))
717 (if (<= (point) opoint)
718 (setq opoint (+ opoint nchars)))
719 (if (< (point) obeg)
720 (setq obeg (+ obeg nchars)))
721 (if (<= (point) oend)
722 (setq oend (+ oend nchars)))
723 (insert-before-markers string)
724 (if (= (window-start) (point))
725 (set-window-start (selected-window)
726 (- (point) nchars)))
727 (if (= (point) eshell-last-input-end)
728 (set-marker eshell-last-input-end
729 (- eshell-last-input-end nchars)))
730 (set-marker eshell-last-output-start ostart)
731 (set-marker eshell-last-output-end (point))
732 (force-mode-line-update))
733 (narrow-to-region obeg oend)
734 (goto-char opoint)
735 (eshell-run-output-filters))))))
737 (defun eshell-run-output-filters ()
738 "Run the `eshell-output-filter-functions' on the current output."
739 (save-current-buffer
740 (run-hooks 'eshell-output-filter-functions))
741 (setq eshell-last-output-block-begin
742 (marker-position eshell-last-output-end)))
744 ;;; jww (1999-10-23): this needs testing
745 (defun eshell-preinput-scroll-to-bottom ()
746 "Go to the end of buffer in all windows showing it.
747 Movement occurs if point in the selected window is not after the
748 process mark, and `this-command' is an insertion command. Insertion
749 commands recognized are `self-insert-command', `yank', and
750 `hilit-yank'. Depends on the value of
751 `eshell-scroll-to-bottom-on-input'.
753 This function should be a pre-command hook."
754 (if (memq this-command '(self-insert-command yank hilit-yank))
755 (let* ((selected (selected-window))
756 (current (current-buffer))
757 (scroll eshell-scroll-to-bottom-on-input))
758 (if (< (point) eshell-last-output-end)
759 (if (eq scroll 'this)
760 (goto-char (point-max))
761 (walk-windows
762 (function
763 (lambda (window)
764 (when (and (eq (window-buffer window) current)
765 (or (eq scroll t) (eq scroll 'all)))
766 (select-window window)
767 (goto-char (point-max))
768 (select-window selected))))
769 nil t))))))
771 ;;; jww (1999-10-23): this needs testing
772 (defun eshell-postoutput-scroll-to-bottom ()
773 "Go to the end of buffer in all windows showing it.
774 Does not scroll if the current line is the last line in the buffer.
775 Depends on the value of `eshell-scroll-to-bottom-on-output' and
776 `eshell-scroll-show-maximum-output'.
778 This function should be in the list `eshell-output-filter-functions'."
779 (let* ((selected (selected-window))
780 (current (current-buffer))
781 (scroll eshell-scroll-to-bottom-on-output))
782 (unwind-protect
783 (walk-windows
784 (function
785 (lambda (window)
786 (if (eq (window-buffer window) current)
787 (progn
788 (select-window window)
789 (if (and (< (point) eshell-last-output-end)
790 (or (eq scroll t) (eq scroll 'all)
791 ;; Maybe user wants point to jump to end.
792 (and (eq scroll 'this)
793 (eq selected window))
794 (and (eq scroll 'others)
795 (not (eq selected window)))
796 ;; If point was at the end, keep it at end.
797 (>= (point) eshell-last-output-start)))
798 (goto-char eshell-last-output-end))
799 ;; Optionally scroll so that the text
800 ;; ends at the bottom of the window.
801 (if (and eshell-scroll-show-maximum-output
802 (>= (point) eshell-last-output-end))
803 (save-excursion
804 (goto-char (point-max))
805 (recenter -1)))
806 (select-window selected)))))
807 nil t)
808 (set-buffer current))))
810 (defun eshell-beginning-of-input ()
811 "Return the location of the start of the previous input."
812 eshell-last-input-start)
814 (defun eshell-beginning-of-output ()
815 "Return the location of the end of the previous output block."
816 eshell-last-input-end)
818 (defun eshell-end-of-output ()
819 "Return the location of the end of the previous output block."
820 (if (eshell-using-module 'eshell-prompt)
821 eshell-last-output-start
822 eshell-last-output-end))
824 (defun eshell-kill-output ()
825 "Kill all output from interpreter since last input.
826 Does not delete the prompt."
827 (interactive)
828 (save-excursion
829 (goto-char (eshell-beginning-of-output))
830 (insert "*** output flushed ***\n")
831 (delete-region (point) (eshell-end-of-output))))
833 (defun eshell-show-output (&optional arg)
834 "Display start of this batch of interpreter output at top of window.
835 Sets mark to the value of point when this command is run.
836 With a prefix argument, narrows region to last command output."
837 (interactive "P")
838 (goto-char (eshell-beginning-of-output))
839 (set-window-start (selected-window)
840 (save-excursion
841 (goto-char (eshell-beginning-of-input))
842 (line-beginning-position)))
843 (if arg
844 (narrow-to-region (eshell-beginning-of-output)
845 (eshell-end-of-output)))
846 (eshell-end-of-output))
848 (defun eshell-mark-output (&optional arg)
849 "Display start of this batch of interpreter output at top of window.
850 Sets mark to the value of point when this command is run.
851 With a prefix argument, narrows region to last command output."
852 (interactive "P")
853 (push-mark (eshell-show-output arg)))
855 (defun eshell-kill-input ()
856 "Kill all text from last stuff output by interpreter to point."
857 (interactive)
858 (if (> (point) eshell-last-output-end)
859 (kill-region eshell-last-output-end (point))
860 (let ((here (point)))
861 (eshell-bol)
862 (kill-region (point) here))))
864 (defun eshell-show-maximum-output (&optional interactive)
865 "Put the end of the buffer at the bottom of the window.
866 When run interactively, widen the buffer first."
867 (interactive "p")
868 (if interactive
869 (widen))
870 (goto-char (point-max))
871 (recenter -1))
873 (defun eshell/clear (&optional scrollback)
874 "Scroll contents of eshell window out of sight, leaving a blank window.
875 If SCROLLBACK is non-nil, clear the scrollback contents."
876 (interactive)
877 (if scrollback
878 (eshell/clear-scrollback)
879 (let ((number-newlines (count-lines (window-start) (point))))
880 (insert (make-string number-newlines ?\n))
881 (eshell-send-input))))
883 (defun eshell/clear-scrollback ()
884 "Clear the scrollback content of the eshell window."
885 (let ((inhibit-read-only t))
886 (erase-buffer)))
888 (defun eshell-get-old-input (&optional use-current-region)
889 "Return the command input on the current line."
890 (if use-current-region
891 (buffer-substring (min (point) (mark))
892 (max (point) (mark)))
893 (save-excursion
894 (beginning-of-line)
895 (and eshell-skip-prompt-function
896 (funcall eshell-skip-prompt-function))
897 (let ((beg (point)))
898 (end-of-line)
899 (buffer-substring beg (point))))))
901 (defun eshell-copy-old-input ()
902 "Insert after prompt old input at point as new input to be edited."
903 (interactive)
904 (let ((input (eshell-get-old-input)))
905 (goto-char eshell-last-output-end)
906 (insert-and-inherit input)))
908 (defun eshell/exit ()
909 "Leave or kill the Eshell buffer, depending on `eshell-kill-on-exit'."
910 (throw 'eshell-terminal t))
912 (defun eshell-life-is-too-much ()
913 "Kill the current buffer (or bury it). Good-bye Eshell."
914 (interactive)
915 (if (not eshell-kill-on-exit)
916 (bury-buffer)
917 (kill-buffer (current-buffer))))
919 (defun eshell-truncate-buffer ()
920 "Truncate the buffer to `eshell-buffer-maximum-lines'.
921 This function could be on `eshell-output-filter-functions' or bound to
922 a key."
923 (interactive)
924 (save-excursion
925 (goto-char eshell-last-output-end)
926 (let ((lines (count-lines 1 (point)))
927 (inhibit-read-only t))
928 (forward-line (- eshell-buffer-maximum-lines))
929 (beginning-of-line)
930 (let ((pos (point)))
931 (if (bobp)
932 (if (called-interactively-p 'interactive)
933 (message "Buffer too short to truncate"))
934 (delete-region (point-min) (point))
935 (if (called-interactively-p 'interactive)
936 (message "Truncated buffer from %d to %d lines (%.1fk freed)"
937 lines eshell-buffer-maximum-lines
938 (/ pos 1024.0))))))))
940 (custom-add-option 'eshell-output-filter-functions
941 'eshell-truncate-buffer)
943 (defun eshell-send-invisible ()
944 "Read a string without echoing.
945 Then send it to the process running in the current buffer."
946 (interactive) ; Don't pass str as argument, to avoid snooping via C-x ESC ESC
947 (let ((str (read-passwd
948 (format "%s Password: "
949 (process-name (eshell-interactive-process))))))
950 (if (stringp str)
951 (process-send-string (eshell-interactive-process)
952 (concat str "\n"))
953 (message "Warning: text will be echoed"))))
955 (defun eshell-watch-for-password-prompt ()
956 "Prompt in the minibuffer for password and send without echoing.
957 This function uses `eshell-send-invisible' to read and send a password to the
958 buffer's process if STRING contains a password prompt defined by
959 `eshell-password-prompt-regexp'.
961 This function could be in the list `eshell-output-filter-functions'."
962 (when (eshell-interactive-process)
963 (save-excursion
964 (let ((case-fold-search t))
965 (goto-char eshell-last-output-block-begin)
966 (beginning-of-line)
967 (if (re-search-forward eshell-password-prompt-regexp
968 eshell-last-output-end t)
969 (eshell-send-invisible))))))
971 (custom-add-option 'eshell-output-filter-functions
972 'eshell-watch-for-password-prompt)
974 (defun eshell-handle-control-codes ()
975 "Act properly when certain control codes are seen."
976 (save-excursion
977 (goto-char eshell-last-output-block-begin)
978 (unless (eolp)
979 (beginning-of-line))
980 (while (< (point) eshell-last-output-end)
981 (let ((char (char-after)))
982 (cond
983 ((eq char ?\r)
984 (if (< (1+ (point)) eshell-last-output-end)
985 (if (memq (char-after (1+ (point)))
986 '(?\n ?\r))
987 (delete-char 1)
988 (let ((end (1+ (point))))
989 (beginning-of-line)
990 (delete-region (point) end)))
991 (add-text-properties (point) (1+ (point))
992 '(invisible t))
993 (forward-char)))
994 ((eq char ?\a)
995 (delete-char 1)
996 (beep))
997 ((eq char ?\C-h)
998 (delete-region (1- (point)) (1+ (point))))
1000 (forward-char)))))))
1002 (custom-add-option 'eshell-output-filter-functions
1003 'eshell-handle-control-codes)
1005 (autoload 'ansi-color-apply-on-region "ansi-color")
1007 (defun eshell-handle-ansi-color ()
1008 "Handle ANSI color codes."
1009 (ansi-color-apply-on-region eshell-last-output-start
1010 eshell-last-output-end))
1012 (custom-add-option 'eshell-output-filter-functions
1013 'eshell-handle-ansi-color)
1015 ;;; esh-mode.el ends here