* admin/notes/copyright: Remove comments about deleted files in src/{m,s}.
[emacs.git] / lisp / eshell / esh-mode.el
blob10623dba8e31d34f5163033ff309cff7aa61c814
1 ;;; esh-mode.el --- user interface
3 ;; Copyright (C) 1999-2011 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 (eval-when-compile (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 "[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'."
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 (defcustom eshell-status-in-modeline t
197 "If non-nil, let the user know a command is running in the modeline."
198 :type 'boolean
199 :group 'eshell-mode)
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)
226 (let ((i 0))
227 (setq eshell-mode-syntax-table (make-syntax-table))
228 (while (< i ?0)
229 (modify-syntax-entry i "_ " eshell-mode-syntax-table)
230 (setq i (1+ i)))
231 (setq i (1+ ?9))
232 (while (< i ?A)
233 (modify-syntax-entry i "_ " eshell-mode-syntax-table)
234 (setq i (1+ i)))
235 (setq i (1+ ?Z))
236 (while (< i ?a)
237 (modify-syntax-entry i "_ " eshell-mode-syntax-table)
238 (setq i (1+ i)))
239 (setq i (1+ ?z))
240 (while (< i 128)
241 (modify-syntax-entry i "_ " eshell-mode-syntax-table)
242 (setq i (1+ i)))
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)
268 (map-char-table
269 (function
270 (lambda (key val)
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))
277 (map-char-table
278 (function
279 (lambda (key val)
280 (and (if (consp key)
281 (and (>= (car key) 128)
282 (/= (char-syntax (car key)) ?w))
283 (and (>= key 256)
284 (/= (char-syntax key) ?w)))
285 (modify-syntax-entry key "_ "
286 eshell-mode-syntax-table))))
287 (standard-syntax-table)))))
289 ;;; User Functions:
291 (defun eshell-kill-buffer-function ()
292 "Function added to `kill-buffer-hook' in Eshell buffers.
293 This runs the function `eshell-kill-processes-on-exit',
294 and the hook `eshell-exit-hook'."
295 ;; It's fine to run this unconditionally since it can be customized
296 ;; via the `eshell-kill-processes-on-exit' variable.
297 (and (fboundp 'eshell-query-kill-processes)
298 (not (memq 'eshell-query-kill-processes eshell-exit-hook))
299 (eshell-query-kill-processes))
300 (run-hooks 'eshell-exit-hook))
302 ;;;###autoload
303 (defun eshell-mode ()
304 "Emacs shell interactive mode.
306 \\{eshell-mode-map}"
307 (kill-all-local-variables)
309 (setq major-mode 'eshell-mode)
310 (setq mode-name "EShell")
311 (set (make-local-variable 'eshell-mode) t)
313 (make-local-variable 'eshell-mode-map)
314 (setq eshell-mode-map (make-sparse-keymap))
315 (use-local-map eshell-mode-map)
317 (when eshell-status-in-modeline
318 (make-local-variable 'eshell-command-running-string)
319 (let ((fmt (copy-sequence mode-line-format)))
320 (make-local-variable 'mode-line-format)
321 (setq mode-line-format fmt))
322 (let ((modeline (memq 'mode-line-modified mode-line-format)))
323 (if modeline
324 (setcar modeline 'eshell-command-running-string))))
326 (define-key eshell-mode-map [return] 'eshell-send-input)
327 (define-key eshell-mode-map [(control ?m)] 'eshell-send-input)
328 (define-key eshell-mode-map [(control ?j)] 'eshell-send-input)
329 (define-key eshell-mode-map [(meta return)] 'eshell-queue-input)
330 (define-key eshell-mode-map [(meta control ?m)] 'eshell-queue-input)
331 (define-key eshell-mode-map [(meta control ?l)] 'eshell-show-output)
332 (define-key eshell-mode-map [(control ?a)] 'eshell-bol)
334 (set (make-local-variable 'eshell-command-prefix)
335 (make-symbol "eshell-command-prefix"))
336 (fset eshell-command-prefix (make-sparse-keymap))
337 (set (make-local-variable 'eshell-command-map)
338 (symbol-function eshell-command-prefix))
339 (define-key eshell-mode-map [(control ?c)] eshell-command-prefix)
341 ;; without this, find-tag complains about read-only text being
342 ;; modified
343 (if (eq (key-binding [(meta ?.)]) 'find-tag)
344 (define-key eshell-mode-map [(meta ?.)] 'eshell-find-tag))
345 (define-key eshell-command-map [(meta ?o)] 'eshell-mark-output)
346 (define-key eshell-command-map [(meta ?d)] 'eshell-toggle-direct-send)
348 (define-key eshell-command-map [(control ?a)] 'eshell-bol)
349 (define-key eshell-command-map [(control ?b)] 'eshell-backward-argument)
350 (define-key eshell-command-map [(control ?e)] 'eshell-show-maximum-output)
351 (define-key eshell-command-map [(control ?f)] 'eshell-forward-argument)
352 (define-key eshell-command-map [return] 'eshell-copy-old-input)
353 (define-key eshell-command-map [(control ?m)] 'eshell-copy-old-input)
354 (define-key eshell-command-map [(control ?o)] 'eshell-kill-output)
355 (define-key eshell-command-map [(control ?r)] 'eshell-show-output)
356 (define-key eshell-command-map [(control ?t)] 'eshell-truncate-buffer)
357 (define-key eshell-command-map [(control ?u)] 'eshell-kill-input)
358 (define-key eshell-command-map [(control ?w)] 'backward-kill-word)
359 (define-key eshell-command-map [(control ?y)] 'eshell-repeat-argument)
361 (setq local-abbrev-table eshell-mode-abbrev-table)
362 (set-syntax-table eshell-mode-syntax-table)
364 (set (make-local-variable 'dired-directory) default-directory)
365 (set (make-local-variable 'list-buffers-directory)
366 (expand-file-name default-directory))
368 ;; always set the tab width to 8 in Eshell buffers, since external
369 ;; commands which do their own formatting almost always expect this
370 (set (make-local-variable 'tab-width) 8)
372 ;; don't ever use auto-fill in Eshell buffers
373 (setq auto-fill-function nil)
375 ;; always display everything from a return value
376 (if (boundp 'print-length)
377 (set (make-local-variable 'print-length) nil))
378 (if (boundp 'print-level)
379 (set (make-local-variable 'print-level) nil))
381 ;; set require-final-newline to nil; otherwise, all redirected
382 ;; output will end with a newline, whether or not the source
383 ;; indicated it!
384 (set (make-local-variable 'require-final-newline) nil)
386 (set (make-local-variable 'max-lisp-eval-depth)
387 (max 3000 max-lisp-eval-depth))
388 (set (make-local-variable 'max-specpdl-size)
389 (max 6000 max-lisp-eval-depth))
391 (set (make-local-variable 'eshell-last-input-start) (point-marker))
392 (set (make-local-variable 'eshell-last-input-end) (point-marker))
393 (set (make-local-variable 'eshell-last-output-start) (point-marker))
394 (set (make-local-variable 'eshell-last-output-end) (point-marker))
395 (set (make-local-variable 'eshell-last-output-block-begin) (point))
397 (let ((modules-list (copy-sequence eshell-modules-list)))
398 (make-local-variable 'eshell-modules-list)
399 (setq eshell-modules-list modules-list))
401 ;; load extension modules into memory. This will cause any global
402 ;; variables they define to be visible, since some of the core
403 ;; modules sometimes take advantage of their functionality if used.
404 (dolist (module eshell-modules-list)
405 (let ((module-fullname (symbol-name module))
406 module-shortname)
407 (if (string-match "^eshell-\\(.*\\)" module-fullname)
408 (setq module-shortname
409 (concat "em-" (match-string 1 module-fullname))))
410 (unless module-shortname
411 (error "Invalid Eshell module name: %s" module-fullname))
412 (unless (featurep (intern module-shortname))
413 (load module-shortname))))
415 (unless (file-exists-p eshell-directory-name)
416 (eshell-make-private-directory eshell-directory-name t))
418 ;; Load core Eshell modules, then extension modules, for this session.
419 (dolist (module (append (eshell-subgroups 'eshell) eshell-modules-list))
420 (let ((load-hook (intern-soft (format "%s-load-hook" module)))
421 (initfunc (intern-soft (format "%s-initialize" module))))
422 (when (and load-hook (boundp load-hook))
423 (if (memq initfunc (symbol-value load-hook)) (setq initfunc nil))
424 (run-hooks load-hook))
425 ;; So we don't need the -initialize functions on the hooks (b#5375).
426 (and initfunc (fboundp initfunc) (funcall initfunc))))
428 (if eshell-send-direct-to-subprocesses
429 (add-hook 'pre-command-hook 'eshell-intercept-commands t t))
431 (if eshell-scroll-to-bottom-on-input
432 (add-hook 'pre-command-hook 'eshell-preinput-scroll-to-bottom t t))
434 (when eshell-scroll-show-maximum-output
435 (set (make-local-variable 'scroll-conservatively) 1000))
437 (when eshell-status-in-modeline
438 (add-hook 'eshell-pre-command-hook 'eshell-command-started nil t)
439 (add-hook 'eshell-post-command-hook 'eshell-command-finished nil t))
441 (add-hook 'kill-buffer-hook 'eshell-kill-buffer-function t t)
443 (if eshell-first-time-p
444 (run-hooks 'eshell-first-time-mode-hook))
445 (run-mode-hooks 'eshell-mode-hook)
446 (run-hooks 'eshell-post-command-hook))
448 (put 'eshell-mode 'mode-class 'special)
450 (defun eshell-command-started ()
451 "Indicate in the modeline that a command has started."
452 (setq eshell-command-running-string "**")
453 (force-mode-line-update))
455 (defun eshell-command-finished ()
456 "Indicate in the modeline that a command has finished."
457 (setq eshell-command-running-string "--")
458 (force-mode-line-update))
460 ;;; Internal Functions:
462 (defun eshell-toggle-direct-send ()
463 (interactive)
464 (if eshell-send-direct-to-subprocesses
465 (progn
466 (setq eshell-send-direct-to-subprocesses nil)
467 (remove-hook 'pre-command-hook 'eshell-intercept-commands t)
468 (message "Sending subprocess input on RET"))
469 (setq eshell-send-direct-to-subprocesses t)
470 (add-hook 'pre-command-hook 'eshell-intercept-commands t t)
471 (message "Sending subprocess input directly")))
473 (defun eshell-self-insert-command (N)
474 (interactive "i")
475 (process-send-string
476 (eshell-interactive-process)
477 (char-to-string (if (symbolp last-command-event)
478 (get last-command-event 'ascii-character)
479 last-command-event))))
481 (defun eshell-intercept-commands ()
482 (when (and (eshell-interactive-process)
483 (not (and (integerp last-input-event)
484 (memq last-input-event '(?\C-x ?\C-c)))))
485 (let ((possible-events (where-is-internal this-command))
486 (name (symbol-name this-command))
487 (intercept t))
488 ;; Assume that any multikey combination which does NOT target an
489 ;; Eshell command, is a combo the user wants invoked rather than
490 ;; sent to the underlying subprocess.
491 (unless (and (> (length name) 7)
492 (equal (substring name 0 7) "eshell-"))
493 (while possible-events
494 (if (> (length (car possible-events)) 1)
495 (setq intercept nil possible-events nil)
496 (setq possible-events (cdr possible-events)))))
497 (if intercept
498 (setq this-command 'eshell-self-insert-command)))))
500 (defun eshell-find-tag (&optional tagname next-p regexp-p)
501 "A special version of `find-tag' that ignores read-onlyness."
502 (interactive)
503 (require 'etags)
504 (let ((inhibit-read-only t)
505 (no-default (eobp))
506 (find-tag-default-function 'ignore))
507 (with-no-warnings
508 (setq tagname (car (find-tag-interactive "Find tag: "))))
509 (find-tag tagname next-p regexp-p)))
511 (defun eshell-move-argument (limit func property arg)
512 "Move forward ARG arguments."
513 (catch 'eshell-incomplete
514 (eshell-parse-arguments (save-excursion (eshell-bol) (point))
515 (line-end-position)))
516 (let ((pos (save-excursion
517 (funcall func 1)
518 (while (and (> arg 0) (/= (point) limit))
519 (if (get-text-property (point) property)
520 (setq arg (1- arg)))
521 (if (> arg 0)
522 (funcall func 1)))
523 (point))))
524 (goto-char pos)
525 (if (and (eq func 'forward-char)
526 (= (1+ pos) limit))
527 (forward-char 1))))
529 (defun eshell-forward-argument (&optional arg)
530 "Move forward ARG arguments."
531 (interactive "p")
532 (eshell-move-argument (point-max) 'forward-char 'arg-end arg))
534 (defun eshell-backward-argument (&optional arg)
535 "Move backward ARG arguments."
536 (interactive "p")
537 (eshell-move-argument (point-min) 'backward-char 'arg-begin arg))
539 (defun eshell-repeat-argument (&optional arg)
540 (interactive "p")
541 (let ((begin (save-excursion
542 (eshell-backward-argument arg)
543 (point))))
544 (kill-ring-save begin (point))
545 (yank)))
547 (defun eshell-bol ()
548 "Goes to the beginning of line, then skips past the prompt, if any."
549 (interactive)
550 (beginning-of-line)
551 (and eshell-skip-prompt-function
552 (funcall eshell-skip-prompt-function)))
554 (defsubst eshell-push-command-mark ()
555 "Push a mark at the end of the last input text."
556 (push-mark (1- eshell-last-input-end) t))
558 (custom-add-option 'eshell-pre-command-hook 'eshell-push-command-mark)
560 (defsubst eshell-goto-input-start ()
561 "Goto the start of the last command input.
562 Putting this function on `eshell-pre-command-hook' will mimic Plan 9's
563 9term behavior."
564 (goto-char eshell-last-input-start))
566 (custom-add-option 'eshell-pre-command-hook 'eshell-push-command-mark)
568 (defsubst eshell-interactive-print (string)
569 "Print STRING to the eshell display buffer."
570 (eshell-output-filter nil string))
572 (defsubst eshell-begin-on-new-line ()
573 "This function outputs a newline if not at beginning of line."
574 (save-excursion
575 (goto-char eshell-last-output-end)
576 (or (bolp)
577 (eshell-interactive-print "\n"))))
579 (defsubst eshell-reset (&optional no-hooks)
580 "Output a prompt on a new line, aborting any current input.
581 If NO-HOOKS is non-nil, then `eshell-post-command-hook' won't be run."
582 (goto-char (point-max))
583 (setq eshell-last-input-start (point-marker)
584 eshell-last-input-end (point-marker)
585 eshell-last-output-start (point-marker)
586 eshell-last-output-block-begin (point)
587 eshell-last-output-end (point-marker))
588 (eshell-begin-on-new-line)
589 (unless no-hooks
590 (run-hooks 'eshell-post-command-hook)
591 (goto-char (point-max))))
593 (defun eshell-parse-command-input (beg end &optional args)
594 "Parse the command input from BEG to END.
595 The difference is that `eshell-parse-command' expects a complete
596 command string (and will error if it doesn't get one), whereas this
597 function will inform the caller whether more input is required.
599 If nil is returned, more input is necessary (probably because a
600 multi-line input string wasn't terminated properly). Otherwise, it
601 will return the parsed command."
602 (let (delim command)
603 (if (setq delim
604 (catch 'eshell-incomplete
605 (ignore
606 (setq command (eshell-parse-command (cons beg end)
607 args t)))))
608 (ignore
609 (message "Expecting completion of delimeter %c ..."
610 (if (listp delim)
611 (car delim)
612 delim)))
613 command)))
615 (defun eshell-update-markers (pmark)
616 "Update the input and output markers relative to point and PMARK."
617 (set-marker eshell-last-input-start pmark)
618 (set-marker eshell-last-input-end (point))
619 (set-marker eshell-last-output-end (point)))
621 (defun eshell-queue-input (&optional use-region)
622 "Queue the current input text for execution by Eshell.
623 Particularly, don't send the text to the current process, even if it's
624 waiting for input."
625 (interactive "P")
626 (eshell-send-input use-region t))
628 (defun eshell-send-input (&optional use-region queue-p no-newline)
629 "Send the input received to Eshell for parsing and processing.
630 After `eshell-last-output-end', sends all text from that marker to
631 point as input. Before that marker, calls `eshell-get-old-input' to
632 retrieve old input, copies it to the end of the buffer, and sends it.
634 If USE-REGION is non-nil, the current region (between point and mark)
635 will be used as input.
637 If QUEUE-P is non-nil, input will be queued until the next prompt,
638 rather than sent to the currently active process. If no process, the
639 input is processed immediately.
641 If NO-NEWLINE is non-nil, the input is sent without an implied final
642 newline."
643 (interactive "P")
644 ;; Note that the input string does not include its terminal newline.
645 (let ((proc-running-p (and (eshell-interactive-process)
646 (not queue-p)))
647 (inhibit-point-motion-hooks t)
648 after-change-functions)
649 (unless (and proc-running-p
650 (not (eq (process-status
651 (eshell-interactive-process)) 'run)))
652 (if (or proc-running-p
653 (>= (point) eshell-last-output-end))
654 (goto-char (point-max))
655 (let ((copy (eshell-get-old-input use-region)))
656 (goto-char eshell-last-output-end)
657 (insert-and-inherit copy)))
658 (unless (or no-newline
659 (and eshell-send-direct-to-subprocesses
660 proc-running-p))
661 (insert-before-markers-and-inherit ?\n))
662 (if proc-running-p
663 (progn
664 (eshell-update-markers eshell-last-output-end)
665 (if (or eshell-send-direct-to-subprocesses
666 (= eshell-last-input-start eshell-last-input-end))
667 (unless no-newline
668 (process-send-string (eshell-interactive-process) "\n"))
669 (process-send-region (eshell-interactive-process)
670 eshell-last-input-start
671 eshell-last-input-end)))
672 (if (= eshell-last-output-end (point))
673 (run-hooks 'eshell-post-command-hook)
674 (let (input)
675 (eshell-condition-case err
676 (progn
677 (setq input (buffer-substring-no-properties
678 eshell-last-output-end (1- (point))))
679 (run-hook-with-args 'eshell-expand-input-functions
680 eshell-last-output-end (1- (point)))
681 (let ((cmd (eshell-parse-command-input
682 eshell-last-output-end (1- (point)))))
683 (when cmd
684 (eshell-update-markers eshell-last-output-end)
685 (setq input (buffer-substring-no-properties
686 eshell-last-input-start
687 (1- eshell-last-input-end)))
688 (run-hooks 'eshell-input-filter-functions)
689 (and (catch 'eshell-terminal
690 (ignore
691 (if (eshell-invoke-directly cmd input)
692 (eval cmd)
693 (eshell-eval-command cmd input))))
694 (eshell-life-is-too-much)))))
695 (quit
696 (eshell-reset t)
697 (run-hooks 'eshell-post-command-hook)
698 (signal 'quit nil))
699 (error
700 (eshell-reset t)
701 (eshell-interactive-print
702 (concat (error-message-string err) "\n"))
703 (run-hooks 'eshell-post-command-hook)
704 (insert-and-inherit input)))))))))
706 (defsubst eshell-kill-new ()
707 "Add the last input text to the kill ring."
708 (kill-ring-save eshell-last-input-start eshell-last-input-end))
710 (custom-add-option 'eshell-input-filter-functions 'eshell-kill-new)
712 (defun eshell-output-filter (process string)
713 "Send the output from PROCESS (STRING) to the interactive display.
714 This is done after all necessary filtering has been done."
715 (let ((oprocbuf (if process (process-buffer process)
716 (current-buffer)))
717 (inhibit-point-motion-hooks t)
718 after-change-functions)
719 (let ((functions eshell-preoutput-filter-functions))
720 (while (and functions string)
721 (setq string (funcall (car functions) string))
722 (setq functions (cdr functions))))
723 (if (and string oprocbuf (buffer-name oprocbuf))
724 (let (opoint obeg oend)
725 (with-current-buffer oprocbuf
726 (setq opoint (point))
727 (setq obeg (point-min))
728 (setq oend (point-max))
729 (let ((buffer-read-only nil)
730 (nchars (length string))
731 (ostart nil))
732 (widen)
733 (goto-char eshell-last-output-end)
734 (setq ostart (point))
735 (if (<= (point) opoint)
736 (setq opoint (+ opoint nchars)))
737 (if (< (point) obeg)
738 (setq obeg (+ obeg nchars)))
739 (if (<= (point) oend)
740 (setq oend (+ oend nchars)))
741 (insert-before-markers string)
742 (if (= (window-start (selected-window)) (point))
743 (set-window-start (selected-window)
744 (- (point) nchars)))
745 (if (= (point) eshell-last-input-end)
746 (set-marker eshell-last-input-end
747 (- eshell-last-input-end nchars)))
748 (set-marker eshell-last-output-start ostart)
749 (set-marker eshell-last-output-end (point))
750 (force-mode-line-update))
751 (narrow-to-region obeg oend)
752 (goto-char opoint)
753 (eshell-run-output-filters))))))
755 (defun eshell-run-output-filters ()
756 "Run the `eshell-output-filter-functions' on the current output."
757 (save-current-buffer
758 (run-hooks 'eshell-output-filter-functions))
759 (setq eshell-last-output-block-begin
760 (marker-position eshell-last-output-end)))
762 ;;; jww (1999-10-23): this needs testing
763 (defun eshell-preinput-scroll-to-bottom ()
764 "Go to the end of buffer in all windows showing it.
765 Movement occurs if point in the selected window is not after the
766 process mark, and `this-command' is an insertion command. Insertion
767 commands recognized are `self-insert-command', `yank', and
768 `hilit-yank'. Depends on the value of
769 `eshell-scroll-to-bottom-on-input'.
771 This function should be a pre-command hook."
772 (if (memq this-command '(self-insert-command yank hilit-yank))
773 (let* ((selected (selected-window))
774 (current (current-buffer))
775 (scroll eshell-scroll-to-bottom-on-input))
776 (if (< (point) eshell-last-output-end)
777 (if (eq scroll 'this)
778 (goto-char (point-max))
779 (walk-windows
780 (function
781 (lambda (window)
782 (when (and (eq (window-buffer window) current)
783 (or (eq scroll t) (eq scroll 'all)))
784 (select-window window)
785 (goto-char (point-max))
786 (select-window selected))))
787 nil t))))))
789 ;;; jww (1999-10-23): this needs testing
790 (defun eshell-postoutput-scroll-to-bottom ()
791 "Go to the end of buffer in all windows showing it.
792 Does not scroll if the current line is the last line in the buffer.
793 Depends on the value of `eshell-scroll-to-bottom-on-output' and
794 `eshell-scroll-show-maximum-output'.
796 This function should be in the list `eshell-output-filter-functions'."
797 (let* ((selected (selected-window))
798 (current (current-buffer))
799 (scroll eshell-scroll-to-bottom-on-output))
800 (unwind-protect
801 (walk-windows
802 (function
803 (lambda (window)
804 (if (eq (window-buffer window) current)
805 (progn
806 (select-window window)
807 (if (and (< (point) eshell-last-output-end)
808 (or (eq scroll t) (eq scroll 'all)
809 ;; Maybe user wants point to jump to end.
810 (and (eq scroll 'this)
811 (eq selected window))
812 (and (eq scroll 'others)
813 (not (eq selected window)))
814 ;; If point was at the end, keep it at end.
815 (>= (point) eshell-last-output-start)))
816 (goto-char eshell-last-output-end))
817 ;; Optionally scroll so that the text
818 ;; ends at the bottom of the window.
819 (if (and eshell-scroll-show-maximum-output
820 (>= (point) eshell-last-output-end))
821 (save-excursion
822 (goto-char (point-max))
823 (recenter -1)))
824 (select-window selected)))))
825 nil t)
826 (set-buffer current))))
828 (defun eshell-beginning-of-input ()
829 "Return the location of the start of the previous input."
830 eshell-last-input-start)
832 (defun eshell-beginning-of-output ()
833 "Return the location of the end of the previous output block."
834 eshell-last-input-end)
836 (defun eshell-end-of-output ()
837 "Return the location of the end of the previous output block."
838 (if (eshell-using-module 'eshell-prompt)
839 eshell-last-output-start
840 eshell-last-output-end))
842 (defun eshell-kill-output ()
843 "Kill all output from interpreter since last input.
844 Does not delete the prompt."
845 (interactive)
846 (save-excursion
847 (goto-char (eshell-beginning-of-output))
848 (insert "*** output flushed ***\n")
849 (delete-region (point) (eshell-end-of-output))))
851 (defun eshell-show-output (&optional arg)
852 "Display start of this batch of interpreter output at top of window.
853 Sets mark to the value of point when this command is run.
854 With a prefix argument, narrows region to last command output."
855 (interactive "P")
856 (goto-char (eshell-beginning-of-output))
857 (set-window-start (selected-window)
858 (save-excursion
859 (goto-char (eshell-beginning-of-input))
860 (line-beginning-position)))
861 (if arg
862 (narrow-to-region (eshell-beginning-of-output)
863 (eshell-end-of-output)))
864 (eshell-end-of-output))
866 (defun eshell-mark-output (&optional arg)
867 "Display start of this batch of interpreter output at top of window.
868 Sets mark to the value of point when this command is run.
869 With a prefix argument, narrows region to last command output."
870 (interactive "P")
871 (push-mark (eshell-show-output arg)))
873 (defun eshell-kill-input ()
874 "Kill all text from last stuff output by interpreter to point."
875 (interactive)
876 (if (> (point) eshell-last-output-end)
877 (kill-region eshell-last-output-end (point))
878 (let ((here (point)))
879 (eshell-bol)
880 (kill-region (point) here))))
882 (defun eshell-show-maximum-output (&optional interactive)
883 "Put the end of the buffer at the bottom of the window.
884 When run interactively, widen the buffer first."
885 (interactive "p")
886 (if interactive
887 (widen))
888 (goto-char (point-max))
889 (recenter -1))
891 (defun eshell-get-old-input (&optional use-current-region)
892 "Return the command input on the current line."
893 (if use-current-region
894 (buffer-substring (min (point) (mark))
895 (max (point) (mark)))
896 (save-excursion
897 (beginning-of-line)
898 (and eshell-skip-prompt-function
899 (funcall eshell-skip-prompt-function))
900 (let ((beg (point)))
901 (end-of-line)
902 (buffer-substring beg (point))))))
904 (defun eshell-copy-old-input ()
905 "Insert after prompt old input at point as new input to be edited."
906 (interactive)
907 (let ((input (eshell-get-old-input)))
908 (goto-char eshell-last-output-end)
909 (insert-and-inherit input)))
911 (defun eshell/exit ()
912 "Leave or kill the Eshell buffer, depending on `eshell-kill-on-exit'."
913 (throw 'eshell-terminal t))
915 (defun eshell-life-is-too-much ()
916 "Kill the current buffer (or bury it). Good-bye Eshell."
917 (interactive)
918 (if (not eshell-kill-on-exit)
919 (bury-buffer)
920 (kill-buffer (current-buffer))))
922 (defun eshell-truncate-buffer ()
923 "Truncate the buffer to `eshell-buffer-maximum-lines'.
924 This function could be on `eshell-output-filter-functions' or bound to
925 a key."
926 (interactive)
927 (save-excursion
928 (goto-char eshell-last-output-end)
929 (let ((lines (count-lines 1 (point)))
930 (inhibit-read-only t))
931 (forward-line (- eshell-buffer-maximum-lines))
932 (beginning-of-line)
933 (let ((pos (point)))
934 (if (bobp)
935 (if (called-interactively-p 'interactive)
936 (message "Buffer too short to truncate"))
937 (delete-region (point-min) (point))
938 (if (called-interactively-p 'interactive)
939 (message "Truncated buffer from %d to %d lines (%.1fk freed)"
940 lines eshell-buffer-maximum-lines
941 (/ pos 1024.0))))))))
943 (custom-add-option 'eshell-output-filter-functions
944 'eshell-truncate-buffer)
946 (defun eshell-send-invisible (str)
947 "Read a string without echoing.
948 Then send it to the process running in the current buffer."
949 (interactive "P") ; Defeat snooping via C-x ESC ESC
950 (let ((str (read-passwd
951 (format "%s Password: "
952 (process-name (eshell-interactive-process))))))
953 (if (stringp str)
954 (process-send-string (eshell-interactive-process)
955 (concat str "\n"))
956 (message "Warning: text will be echoed"))))
958 (defun eshell-watch-for-password-prompt ()
959 "Prompt in the minibuffer for password and send without echoing.
960 This function uses `eshell-send-invisible' to read and send a password to the
961 buffer's process if STRING contains a password prompt defined by
962 `eshell-password-prompt-regexp'.
964 This function could be in the list `eshell-output-filter-functions'."
965 (when (eshell-interactive-process)
966 (save-excursion
967 (goto-char eshell-last-output-block-begin)
968 (beginning-of-line)
969 (if (re-search-forward eshell-password-prompt-regexp
970 eshell-last-output-end t)
971 (eshell-send-invisible nil)))))
973 (custom-add-option 'eshell-output-filter-functions
974 'eshell-watch-for-password-prompt)
976 (defun eshell-handle-control-codes ()
977 "Act properly when certain control codes are seen."
978 (save-excursion
979 (let ((orig (point)))
980 (goto-char eshell-last-output-block-begin)
981 (unless (eolp)
982 (beginning-of-line))
983 (while (< (point) eshell-last-output-end)
984 (let ((char (char-after)))
985 (cond
986 ((eq char ?\r)
987 (if (< (1+ (point)) eshell-last-output-end)
988 (if (memq (char-after (1+ (point)))
989 '(?\n ?\r))
990 (delete-char 1)
991 (let ((end (1+ (point))))
992 (beginning-of-line)
993 (delete-region (point) end)))
994 (add-text-properties (point) (1+ (point))
995 '(invisible t))
996 (forward-char)))
997 ((eq char ?\a)
998 (delete-char 1)
999 (beep))
1000 ((eq char ?\C-h)
1001 (delete-backward-char 1)
1002 (delete-char 1))
1004 (forward-char))))))))
1006 (custom-add-option 'eshell-output-filter-functions
1007 'eshell-handle-control-codes)
1009 (autoload 'ansi-color-apply-on-region "ansi-color")
1011 (defun eshell-handle-ansi-color ()
1012 "Handle ANSI color codes."
1013 (ansi-color-apply-on-region eshell-last-output-start
1014 eshell-last-output-end))
1016 (custom-add-option 'eshell-output-filter-functions
1017 'eshell-handle-ansi-color)
1019 ;;; esh-mode.el ends here