Nuke arch-tags.
[emacs.git] / lisp / eshell / esh-mode.el
bloba344b4a00d7dd22da0a6a9dc080b2cd165742323
1 ;;; esh-mode.el --- user interface
3 ;; Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007,
4 ;; 2008, 2009, 2010, 2011 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/>.
23 ;;; Commentary:
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
48 ;; large.
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
52 ;; only that output.
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.
60 ;;; Code:
62 (provide 'esh-mode)
64 (eval-when-compile (require 'esh-util))
65 (require 'esh-module)
66 (require 'esh-cmd)
67 (require 'esh-io)
68 (require 'esh-var)
70 (defgroup eshell-mode nil
71 "This module contains code for handling input from the user."
72 :tag "User interface"
73 :group 'eshell)
75 ;;; User Variables:
77 (defcustom eshell-mode-unload-hook nil
78 "A hook that gets run when `eshell-mode' is unloaded."
79 :type 'hook
80 :group 'eshell-mode)
82 (defcustom eshell-mode-hook nil
83 "A hook that gets run when `eshell-mode' is entered."
84 :type 'hook
85 :group 'eshell-mode)
87 (defcustom eshell-first-time-mode-hook nil
88 "A hook that gets run the first time `eshell-mode' is entered.
89 That is to say, the first time during an Emacs session."
90 :type 'hook
91 :group 'eshell-mode)
93 (defcustom eshell-exit-hook '(eshell-query-kill-processes)
94 "A hook that is run whenever `eshell' is exited.
95 This hook is only run if exiting actually kills the buffer."
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 ;;;###autoload
292 (defun eshell-mode ()
293 "Emacs shell interactive mode.
295 \\{eshell-mode-map}"
296 (kill-all-local-variables)
298 (setq major-mode 'eshell-mode)
299 (setq mode-name "EShell")
300 (set (make-local-variable 'eshell-mode) t)
302 (make-local-variable 'eshell-mode-map)
303 (setq eshell-mode-map (make-sparse-keymap))
304 (use-local-map eshell-mode-map)
306 (when eshell-status-in-modeline
307 (make-local-variable 'eshell-command-running-string)
308 (let ((fmt (copy-sequence mode-line-format)))
309 (make-local-variable 'mode-line-format)
310 (setq mode-line-format fmt))
311 (let ((modeline (memq 'mode-line-modified mode-line-format)))
312 (if modeline
313 (setcar modeline 'eshell-command-running-string))))
315 (define-key eshell-mode-map [return] 'eshell-send-input)
316 (define-key eshell-mode-map [(control ?m)] 'eshell-send-input)
317 (define-key eshell-mode-map [(control ?j)] 'eshell-send-input)
318 (define-key eshell-mode-map [(meta return)] 'eshell-queue-input)
319 (define-key eshell-mode-map [(meta control ?m)] 'eshell-queue-input)
320 (define-key eshell-mode-map [(meta control ?l)] 'eshell-show-output)
321 (define-key eshell-mode-map [(control ?a)] 'eshell-bol)
323 (set (make-local-variable 'eshell-command-prefix)
324 (make-symbol "eshell-command-prefix"))
325 (fset eshell-command-prefix (make-sparse-keymap))
326 (set (make-local-variable 'eshell-command-map)
327 (symbol-function eshell-command-prefix))
328 (define-key eshell-mode-map [(control ?c)] eshell-command-prefix)
330 ;; without this, find-tag complains about read-only text being
331 ;; modified
332 (if (eq (key-binding [(meta ?.)]) 'find-tag)
333 (define-key eshell-mode-map [(meta ?.)] 'eshell-find-tag))
334 (define-key eshell-command-map [(meta ?o)] 'eshell-mark-output)
335 (define-key eshell-command-map [(meta ?d)] 'eshell-toggle-direct-send)
337 (define-key eshell-command-map [(control ?a)] 'eshell-bol)
338 (define-key eshell-command-map [(control ?b)] 'eshell-backward-argument)
339 (define-key eshell-command-map [(control ?e)] 'eshell-show-maximum-output)
340 (define-key eshell-command-map [(control ?f)] 'eshell-forward-argument)
341 (define-key eshell-command-map [return] 'eshell-copy-old-input)
342 (define-key eshell-command-map [(control ?m)] 'eshell-copy-old-input)
343 (define-key eshell-command-map [(control ?o)] 'eshell-kill-output)
344 (define-key eshell-command-map [(control ?r)] 'eshell-show-output)
345 (define-key eshell-command-map [(control ?t)] 'eshell-truncate-buffer)
346 (define-key eshell-command-map [(control ?u)] 'eshell-kill-input)
347 (define-key eshell-command-map [(control ?w)] 'backward-kill-word)
348 (define-key eshell-command-map [(control ?y)] 'eshell-repeat-argument)
350 (setq local-abbrev-table eshell-mode-abbrev-table)
351 (set-syntax-table eshell-mode-syntax-table)
353 (set (make-local-variable 'dired-directory) default-directory)
354 (set (make-local-variable 'list-buffers-directory)
355 (expand-file-name default-directory))
357 ;; always set the tab width to 8 in Eshell buffers, since external
358 ;; commands which do their own formatting almost always expect this
359 (set (make-local-variable 'tab-width) 8)
361 ;; don't ever use auto-fill in Eshell buffers
362 (setq auto-fill-function nil)
364 ;; always display everything from a return value
365 (if (boundp 'print-length)
366 (set (make-local-variable 'print-length) nil))
367 (if (boundp 'print-level)
368 (set (make-local-variable 'print-level) nil))
370 ;; set require-final-newline to nil; otherwise, all redirected
371 ;; output will end with a newline, whether or not the source
372 ;; indicated it!
373 (set (make-local-variable 'require-final-newline) nil)
375 (set (make-local-variable 'max-lisp-eval-depth)
376 (max 3000 max-lisp-eval-depth))
377 (set (make-local-variable 'max-specpdl-size)
378 (max 6000 max-lisp-eval-depth))
380 (set (make-local-variable 'eshell-last-input-start) (point-marker))
381 (set (make-local-variable 'eshell-last-input-end) (point-marker))
382 (set (make-local-variable 'eshell-last-output-start) (point-marker))
383 (set (make-local-variable 'eshell-last-output-end) (point-marker))
384 (set (make-local-variable 'eshell-last-output-block-begin) (point))
386 (let ((modules-list (copy-sequence eshell-modules-list)))
387 (make-local-variable 'eshell-modules-list)
388 (setq eshell-modules-list modules-list))
390 ;; load extension modules into memory. This will cause any global
391 ;; variables they define to be visible, since some of the core
392 ;; modules sometimes take advantage of their functionality if used.
393 (eshell-for module eshell-modules-list
394 (let ((module-fullname (symbol-name module))
395 module-shortname)
396 (if (string-match "^eshell-\\(.*\\)" module-fullname)
397 (setq module-shortname
398 (concat "em-" (match-string 1 module-fullname))))
399 (unless module-shortname
400 (error "Invalid Eshell module name: %s" module-fullname))
401 (unless (featurep (intern module-shortname))
402 (load module-shortname))))
404 (unless (file-exists-p eshell-directory-name)
405 (eshell-make-private-directory eshell-directory-name t))
407 ;; load core Eshell modules for this session
408 (eshell-for module (eshell-subgroups 'eshell)
409 (run-hooks (intern-soft (concat (symbol-name module)
410 "-load-hook"))))
412 ;; load extension modules for this session
413 (eshell-for module eshell-modules-list
414 (let ((load-hook (intern-soft (concat (symbol-name module)
415 "-load-hook"))))
416 (if (and load-hook (boundp load-hook))
417 (run-hooks load-hook))))
419 (if eshell-send-direct-to-subprocesses
420 (add-hook 'pre-command-hook 'eshell-intercept-commands t t))
422 (if eshell-scroll-to-bottom-on-input
423 (add-hook 'pre-command-hook 'eshell-preinput-scroll-to-bottom t t))
425 (when eshell-scroll-show-maximum-output
426 (set (make-local-variable 'scroll-conservatively) 1000))
428 (when eshell-status-in-modeline
429 (add-hook 'eshell-pre-command-hook 'eshell-command-started nil t)
430 (add-hook 'eshell-post-command-hook 'eshell-command-finished nil t))
432 (add-hook 'kill-buffer-hook
433 (function
434 (lambda ()
435 (run-hooks 'eshell-exit-hook))) t t)
437 (if eshell-first-time-p
438 (run-hooks 'eshell-first-time-mode-hook))
439 (run-mode-hooks 'eshell-mode-hook)
440 (run-hooks 'eshell-post-command-hook))
442 (put 'eshell-mode 'mode-class 'special)
444 (eshell-deftest mode major-mode
445 "Major mode is correct"
446 (eq major-mode 'eshell-mode))
448 (eshell-deftest mode eshell-mode-variable
449 "`eshell-mode' is true"
450 (eq eshell-mode t))
452 (eshell-deftest var window-height
453 "LINES equals window height"
454 (let ((eshell-stringify-t t))
455 (eshell-command-result-p "= $LINES (window-height)" "t\n")))
457 (defun eshell-command-started ()
458 "Indicate in the modeline that a command has started."
459 (setq eshell-command-running-string "**")
460 (force-mode-line-update))
462 (defun eshell-command-finished ()
463 "Indicate in the modeline that a command has finished."
464 (setq eshell-command-running-string "--")
465 (force-mode-line-update))
467 (eshell-deftest mode command-running-p
468 "Modeline shows no command running"
469 (or (featurep 'xemacs)
470 (not eshell-status-in-modeline)
471 (and (memq 'eshell-command-running-string mode-line-format)
472 (equal eshell-command-running-string "--"))))
474 ;;; Internal Functions:
476 (defun eshell-toggle-direct-send ()
477 (interactive)
478 (if eshell-send-direct-to-subprocesses
479 (progn
480 (setq eshell-send-direct-to-subprocesses nil)
481 (remove-hook 'pre-command-hook 'eshell-intercept-commands t)
482 (message "Sending subprocess input on RET"))
483 (setq eshell-send-direct-to-subprocesses t)
484 (add-hook 'pre-command-hook 'eshell-intercept-commands t t)
485 (message "Sending subprocess input directly")))
487 (defun eshell-self-insert-command (N)
488 (interactive "i")
489 (process-send-string
490 (eshell-interactive-process)
491 (char-to-string (if (symbolp last-command-event)
492 (get last-command-event 'ascii-character)
493 last-command-event))))
495 (defun eshell-intercept-commands ()
496 (when (and (eshell-interactive-process)
497 (not (and (integerp last-input-event)
498 (memq last-input-event '(?\C-x ?\C-c)))))
499 (let ((possible-events (where-is-internal this-command))
500 (name (symbol-name this-command))
501 (intercept t))
502 ;; Assume that any multikey combination which does NOT target an
503 ;; Eshell command, is a combo the user wants invoked rather than
504 ;; sent to the underlying subprocess.
505 (unless (and (> (length name) 7)
506 (equal (substring name 0 7) "eshell-"))
507 (while possible-events
508 (if (> (length (car possible-events)) 1)
509 (setq intercept nil possible-events nil)
510 (setq possible-events (cdr possible-events)))))
511 (if intercept
512 (setq this-command 'eshell-self-insert-command)))))
514 (defun eshell-find-tag (&optional tagname next-p regexp-p)
515 "A special version of `find-tag' that ignores read-onlyness."
516 (interactive)
517 (require 'etags)
518 (let ((inhibit-read-only t)
519 (no-default (eobp))
520 (find-tag-default-function 'ignore))
521 (with-no-warnings
522 (setq tagname (car (find-tag-interactive "Find tag: "))))
523 (find-tag tagname next-p regexp-p)))
525 (defun eshell-move-argument (limit func property arg)
526 "Move forward ARG arguments."
527 (catch 'eshell-incomplete
528 (eshell-parse-arguments (save-excursion (eshell-bol) (point))
529 (line-end-position)))
530 (let ((pos (save-excursion
531 (funcall func 1)
532 (while (and (> arg 0) (/= (point) limit))
533 (if (get-text-property (point) property)
534 (setq arg (1- arg)))
535 (if (> arg 0)
536 (funcall func 1)))
537 (point))))
538 (goto-char pos)
539 (if (and (eq func 'forward-char)
540 (= (1+ pos) limit))
541 (forward-char 1))))
543 (eshell-deftest arg forward-arg
544 "Move across command arguments"
545 (eshell-insert-command "echo $(+ 1 (- 4 3)) \"alpha beta\" file" 'ignore)
546 (let ((here (point)) begin valid)
547 (eshell-bol)
548 (setq begin (point))
549 (eshell-forward-argument 4)
550 (setq valid (= here (point)))
551 (eshell-backward-argument 4)
552 (prog1
553 (and valid (= begin (point)))
554 (eshell-bol)
555 (delete-region (point) (point-max)))))
557 (defun eshell-forward-argument (&optional arg)
558 "Move forward ARG arguments."
559 (interactive "p")
560 (eshell-move-argument (point-max) 'forward-char 'arg-end arg))
562 (defun eshell-backward-argument (&optional arg)
563 "Move backward ARG arguments."
564 (interactive "p")
565 (eshell-move-argument (point-min) 'backward-char 'arg-begin arg))
567 (defun eshell-repeat-argument (&optional arg)
568 (interactive "p")
569 (let ((begin (save-excursion
570 (eshell-backward-argument arg)
571 (point))))
572 (kill-ring-save begin (point))
573 (yank)))
575 (defun eshell-bol ()
576 "Goes to the beginning of line, then skips past the prompt, if any."
577 (interactive)
578 (beginning-of-line)
579 (and eshell-skip-prompt-function
580 (funcall eshell-skip-prompt-function)))
582 (defsubst eshell-push-command-mark ()
583 "Push a mark at the end of the last input text."
584 (push-mark (1- eshell-last-input-end) t))
586 (custom-add-option 'eshell-pre-command-hook 'eshell-push-command-mark)
588 (defsubst eshell-goto-input-start ()
589 "Goto the start of the last command input.
590 Putting this function on `eshell-pre-command-hook' will mimic Plan 9's
591 9term behavior."
592 (goto-char eshell-last-input-start))
594 (custom-add-option 'eshell-pre-command-hook 'eshell-push-command-mark)
596 (defsubst eshell-interactive-print (string)
597 "Print STRING to the eshell display buffer."
598 (eshell-output-filter nil string))
600 (defsubst eshell-begin-on-new-line ()
601 "This function outputs a newline if not at beginning of line."
602 (save-excursion
603 (goto-char eshell-last-output-end)
604 (or (bolp)
605 (eshell-interactive-print "\n"))))
607 (defsubst eshell-reset (&optional no-hooks)
608 "Output a prompt on a new line, aborting any current input.
609 If NO-HOOKS is non-nil, then `eshell-post-command-hook' won't be run."
610 (goto-char (point-max))
611 (setq eshell-last-input-start (point-marker)
612 eshell-last-input-end (point-marker)
613 eshell-last-output-start (point-marker)
614 eshell-last-output-block-begin (point)
615 eshell-last-output-end (point-marker))
616 (eshell-begin-on-new-line)
617 (unless no-hooks
618 (run-hooks 'eshell-post-command-hook)
619 (goto-char (point-max))))
621 (defun eshell-parse-command-input (beg end &optional args)
622 "Parse the command input from BEG to END.
623 The difference is that `eshell-parse-command' expects a complete
624 command string (and will error if it doesn't get one), whereas this
625 function will inform the caller whether more input is required.
627 If nil is returned, more input is necessary (probably because a
628 multi-line input string wasn't terminated properly). Otherwise, it
629 will return the parsed command."
630 (let (delim command)
631 (if (setq delim
632 (catch 'eshell-incomplete
633 (ignore
634 (setq command (eshell-parse-command (cons beg end)
635 args t)))))
636 (ignore
637 (message "Expecting completion of delimeter %c ..."
638 (if (listp delim)
639 (car delim)
640 delim)))
641 command)))
643 (defun eshell-update-markers (pmark)
644 "Update the input and output markers relative to point and PMARK."
645 (set-marker eshell-last-input-start pmark)
646 (set-marker eshell-last-input-end (point))
647 (set-marker eshell-last-output-end (point)))
649 (defun eshell-queue-input (&optional use-region)
650 "Queue the current input text for execution by Eshell.
651 Particularly, don't send the text to the current process, even if it's
652 waiting for input."
653 (interactive "P")
654 (eshell-send-input use-region t))
656 (eshell-deftest mode queue-input
657 "Queue command input"
658 (eshell-insert-command "sleep 2")
659 (eshell-insert-command "echo alpha" 'eshell-queue-input)
660 (let ((count 10))
661 (while (and eshell-current-command
662 (> count 0))
663 (sit-for 1 0)
664 (setq count (1- count))))
665 (eshell-match-result "alpha\n"))
667 (defun eshell-send-input (&optional use-region queue-p no-newline)
668 "Send the input received to Eshell for parsing and processing.
669 After `eshell-last-output-end', sends all text from that marker to
670 point as input. Before that marker, calls `eshell-get-old-input' to
671 retrieve old input, copies it to the end of the buffer, and sends it.
673 If USE-REGION is non-nil, the current region (between point and mark)
674 will be used as input.
676 If QUEUE-P is non-nil, input will be queued until the next prompt,
677 rather than sent to the currently active process. If no process, the
678 input is processed immediately.
680 If NO-NEWLINE is non-nil, the input is sent without an implied final
681 newline."
682 (interactive "P")
683 ;; Note that the input string does not include its terminal newline.
684 (let ((proc-running-p (and (eshell-interactive-process)
685 (not queue-p)))
686 (inhibit-point-motion-hooks t)
687 after-change-functions)
688 (unless (and proc-running-p
689 (not (eq (process-status
690 (eshell-interactive-process)) 'run)))
691 (if (or proc-running-p
692 (>= (point) eshell-last-output-end))
693 (goto-char (point-max))
694 (let ((copy (eshell-get-old-input use-region)))
695 (goto-char eshell-last-output-end)
696 (insert-and-inherit copy)))
697 (unless (or no-newline
698 (and eshell-send-direct-to-subprocesses
699 proc-running-p))
700 (insert-before-markers-and-inherit ?\n))
701 (if proc-running-p
702 (progn
703 (eshell-update-markers eshell-last-output-end)
704 (if (or eshell-send-direct-to-subprocesses
705 (= eshell-last-input-start eshell-last-input-end))
706 (unless no-newline
707 (process-send-string (eshell-interactive-process) "\n"))
708 (process-send-region (eshell-interactive-process)
709 eshell-last-input-start
710 eshell-last-input-end)))
711 (if (= eshell-last-output-end (point))
712 (run-hooks 'eshell-post-command-hook)
713 (let (input)
714 (eshell-condition-case err
715 (progn
716 (setq input (buffer-substring-no-properties
717 eshell-last-output-end (1- (point))))
718 (run-hook-with-args 'eshell-expand-input-functions
719 eshell-last-output-end (1- (point)))
720 (let ((cmd (eshell-parse-command-input
721 eshell-last-output-end (1- (point)))))
722 (when cmd
723 (eshell-update-markers eshell-last-output-end)
724 (setq input (buffer-substring-no-properties
725 eshell-last-input-start
726 (1- eshell-last-input-end)))
727 (run-hooks 'eshell-input-filter-functions)
728 (and (catch 'eshell-terminal
729 (ignore
730 (if (eshell-invoke-directly cmd input)
731 (eval cmd)
732 (eshell-eval-command cmd input))))
733 (eshell-life-is-too-much)))))
734 (quit
735 (eshell-reset t)
736 (run-hooks 'eshell-post-command-hook)
737 (signal 'quit nil))
738 (error
739 (eshell-reset t)
740 (eshell-interactive-print
741 (concat (error-message-string err) "\n"))
742 (run-hooks 'eshell-post-command-hook)
743 (insert-and-inherit input)))))))))
745 ; (eshell-deftest proc send-to-subprocess
746 ; "Send input to a subprocess"
747 ; ;; jww (1999-12-06): what about when bc is unavailable?
748 ; (if (not (eshell-search-path "bc"))
750 ; (eshell-insert-command "bc")
751 ; (eshell-insert-command "1 + 2")
752 ; (sit-for 1 0)
753 ; (forward-line -1)
754 ; (prog1
755 ; (looking-at "3\n")
756 ; (eshell-insert-command "quit")
757 ; (sit-for 1 0))))
759 (defsubst eshell-kill-new ()
760 "Add the last input text to the kill ring."
761 (kill-ring-save eshell-last-input-start eshell-last-input-end))
763 (custom-add-option 'eshell-input-filter-functions 'eshell-kill-new)
765 (defun eshell-output-filter (process string)
766 "Send the output from PROCESS (STRING) to the interactive display.
767 This is done after all necessary filtering has been done."
768 (let ((oprocbuf (if process (process-buffer process)
769 (current-buffer)))
770 (inhibit-point-motion-hooks t)
771 after-change-functions)
772 (let ((functions eshell-preoutput-filter-functions))
773 (while (and functions string)
774 (setq string (funcall (car functions) string))
775 (setq functions (cdr functions))))
776 (if (and string oprocbuf (buffer-name oprocbuf))
777 (let (opoint obeg oend)
778 (with-current-buffer oprocbuf
779 (setq opoint (point))
780 (setq obeg (point-min))
781 (setq oend (point-max))
782 (let ((buffer-read-only nil)
783 (nchars (length string))
784 (ostart nil))
785 (widen)
786 (goto-char eshell-last-output-end)
787 (setq ostart (point))
788 (if (<= (point) opoint)
789 (setq opoint (+ opoint nchars)))
790 (if (< (point) obeg)
791 (setq obeg (+ obeg nchars)))
792 (if (<= (point) oend)
793 (setq oend (+ oend nchars)))
794 (insert-before-markers string)
795 (if (= (window-start (selected-window)) (point))
796 (set-window-start (selected-window)
797 (- (point) nchars)))
798 (if (= (point) eshell-last-input-end)
799 (set-marker eshell-last-input-end
800 (- eshell-last-input-end nchars)))
801 (set-marker eshell-last-output-start ostart)
802 (set-marker eshell-last-output-end (point))
803 (force-mode-line-update))
804 (narrow-to-region obeg oend)
805 (goto-char opoint)
806 (eshell-run-output-filters))))))
808 (defun eshell-run-output-filters ()
809 "Run the `eshell-output-filter-functions' on the current output."
810 (save-current-buffer
811 (run-hooks 'eshell-output-filter-functions))
812 (setq eshell-last-output-block-begin
813 (marker-position eshell-last-output-end)))
815 ;;; jww (1999-10-23): this needs testing
816 (defun eshell-preinput-scroll-to-bottom ()
817 "Go to the end of buffer in all windows showing it.
818 Movement occurs if point in the selected window is not after the
819 process mark, and `this-command' is an insertion command. Insertion
820 commands recognized are `self-insert-command', `yank', and
821 `hilit-yank'. Depends on the value of
822 `eshell-scroll-to-bottom-on-input'.
824 This function should be a pre-command hook."
825 (if (memq this-command '(self-insert-command yank hilit-yank))
826 (let* ((selected (selected-window))
827 (current (current-buffer))
828 (scroll eshell-scroll-to-bottom-on-input))
829 (if (< (point) eshell-last-output-end)
830 (if (eq scroll 'this)
831 (goto-char (point-max))
832 (walk-windows
833 (function
834 (lambda (window)
835 (when (and (eq (window-buffer window) current)
836 (or (eq scroll t) (eq scroll 'all)))
837 (select-window window)
838 (goto-char (point-max))
839 (select-window selected))))
840 nil t))))))
842 ;;; jww (1999-10-23): this needs testing
843 (defun eshell-postoutput-scroll-to-bottom ()
844 "Go to the end of buffer in all windows showing it.
845 Does not scroll if the current line is the last line in the buffer.
846 Depends on the value of `eshell-scroll-to-bottom-on-output' and
847 `eshell-scroll-show-maximum-output'.
849 This function should be in the list `eshell-output-filter-functions'."
850 (let* ((selected (selected-window))
851 (current (current-buffer))
852 (scroll eshell-scroll-to-bottom-on-output))
853 (unwind-protect
854 (walk-windows
855 (function
856 (lambda (window)
857 (if (eq (window-buffer window) current)
858 (progn
859 (select-window window)
860 (if (and (< (point) eshell-last-output-end)
861 (or (eq scroll t) (eq scroll 'all)
862 ;; Maybe user wants point to jump to end.
863 (and (eq scroll 'this)
864 (eq selected window))
865 (and (eq scroll 'others)
866 (not (eq selected window)))
867 ;; If point was at the end, keep it at end.
868 (>= (point) eshell-last-output-start)))
869 (goto-char eshell-last-output-end))
870 ;; Optionally scroll so that the text
871 ;; ends at the bottom of the window.
872 (if (and eshell-scroll-show-maximum-output
873 (>= (point) eshell-last-output-end))
874 (save-excursion
875 (goto-char (point-max))
876 (recenter -1)))
877 (select-window selected)))))
878 nil t)
879 (set-buffer current))))
881 (defun eshell-beginning-of-input ()
882 "Return the location of the start of the previous input."
883 eshell-last-input-start)
885 (defun eshell-beginning-of-output ()
886 "Return the location of the end of the previous output block."
887 eshell-last-input-end)
889 (defun eshell-end-of-output ()
890 "Return the location of the end of the previous output block."
891 (if (eshell-using-module 'eshell-prompt)
892 eshell-last-output-start
893 eshell-last-output-end))
895 (defun eshell-kill-output ()
896 "Kill all output from interpreter since last input.
897 Does not delete the prompt."
898 (interactive)
899 (save-excursion
900 (goto-char (eshell-beginning-of-output))
901 (insert "*** output flushed ***\n")
902 (delete-region (point) (eshell-end-of-output))))
904 (eshell-deftest io flush-output
905 "Flush previous output"
906 (eshell-insert-command "echo alpha")
907 (eshell-kill-output)
908 (and (eshell-match-result (regexp-quote "*** output flushed ***\n"))
909 (forward-line)
910 (= (point) eshell-last-output-start)))
912 (defun eshell-show-output (&optional arg)
913 "Display start of this batch of interpreter output at top of window.
914 Sets mark to the value of point when this command is run.
915 With a prefix argument, narrows region to last command output."
916 (interactive "P")
917 (goto-char (eshell-beginning-of-output))
918 (set-window-start (selected-window)
919 (save-excursion
920 (goto-char (eshell-beginning-of-input))
921 (line-beginning-position)))
922 (if arg
923 (narrow-to-region (eshell-beginning-of-output)
924 (eshell-end-of-output)))
925 (eshell-end-of-output))
927 (defun eshell-mark-output (&optional arg)
928 "Display start of this batch of interpreter output at top of window.
929 Sets mark to the value of point when this command is run.
930 With a prefix argument, narrows region to last command output."
931 (interactive "P")
932 (push-mark (eshell-show-output arg)))
934 (defun eshell-kill-input ()
935 "Kill all text from last stuff output by interpreter to point."
936 (interactive)
937 (if (> (point) eshell-last-output-end)
938 (kill-region eshell-last-output-end (point))
939 (let ((here (point)))
940 (eshell-bol)
941 (kill-region (point) here))))
943 (defun eshell-show-maximum-output (&optional interactive)
944 "Put the end of the buffer at the bottom of the window.
945 When run interactively, widen the buffer first."
946 (interactive "p")
947 (if interactive
948 (widen))
949 (goto-char (point-max))
950 (recenter -1))
952 (defun eshell-get-old-input (&optional use-current-region)
953 "Return the command input on the current line."
954 (if use-current-region
955 (buffer-substring (min (point) (mark))
956 (max (point) (mark)))
957 (save-excursion
958 (beginning-of-line)
959 (and eshell-skip-prompt-function
960 (funcall eshell-skip-prompt-function))
961 (let ((beg (point)))
962 (end-of-line)
963 (buffer-substring beg (point))))))
965 (defun eshell-copy-old-input ()
966 "Insert after prompt old input at point as new input to be edited."
967 (interactive)
968 (let ((input (eshell-get-old-input)))
969 (goto-char eshell-last-output-end)
970 (insert-and-inherit input)))
972 (eshell-deftest mode run-old-command
973 "Re-run an old command"
974 (eshell-insert-command "echo alpha")
975 (goto-char eshell-last-input-start)
976 (string= (eshell-get-old-input) "echo alpha"))
978 (defun eshell/exit ()
979 "Leave or kill the Eshell buffer, depending on `eshell-kill-on-exit'."
980 (throw 'eshell-terminal t))
982 (defun eshell-life-is-too-much ()
983 "Kill the current buffer (or bury it). Good-bye Eshell."
984 (interactive)
985 (if (not eshell-kill-on-exit)
986 (bury-buffer)
987 (kill-buffer (current-buffer))))
989 (defun eshell-truncate-buffer ()
990 "Truncate the buffer to `eshell-buffer-maximum-lines'.
991 This function could be on `eshell-output-filter-functions' or bound to
992 a key."
993 (interactive)
994 (save-excursion
995 (goto-char eshell-last-output-end)
996 (let ((lines (count-lines 1 (point)))
997 (inhibit-read-only t))
998 (forward-line (- eshell-buffer-maximum-lines))
999 (beginning-of-line)
1000 (let ((pos (point)))
1001 (if (bobp)
1002 (if (called-interactively-p 'interactive)
1003 (message "Buffer too short to truncate"))
1004 (delete-region (point-min) (point))
1005 (if (called-interactively-p 'interactive)
1006 (message "Truncated buffer from %d to %d lines (%.1fk freed)"
1007 lines eshell-buffer-maximum-lines
1008 (/ pos 1024.0))))))))
1010 (custom-add-option 'eshell-output-filter-functions
1011 'eshell-truncate-buffer)
1013 (defun eshell-send-invisible (str)
1014 "Read a string without echoing.
1015 Then send it to the process running in the current buffer."
1016 (interactive "P") ; Defeat snooping via C-x ESC ESC
1017 (let ((str (read-passwd
1018 (format "%s Password: "
1019 (process-name (eshell-interactive-process))))))
1020 (if (stringp str)
1021 (process-send-string (eshell-interactive-process)
1022 (concat str "\n"))
1023 (message "Warning: text will be echoed"))))
1025 (defun eshell-watch-for-password-prompt ()
1026 "Prompt in the minibuffer for password and send without echoing.
1027 This function uses `eshell-send-invisible' to read and send a password to the
1028 buffer's process if STRING contains a password prompt defined by
1029 `eshell-password-prompt-regexp'.
1031 This function could be in the list `eshell-output-filter-functions'."
1032 (when (eshell-interactive-process)
1033 (save-excursion
1034 (goto-char eshell-last-output-block-begin)
1035 (beginning-of-line)
1036 (if (re-search-forward eshell-password-prompt-regexp
1037 eshell-last-output-end t)
1038 (eshell-send-invisible nil)))))
1040 (custom-add-option 'eshell-output-filter-functions
1041 'eshell-watch-for-password-prompt)
1043 (defun eshell-handle-control-codes ()
1044 "Act properly when certain control codes are seen."
1045 (save-excursion
1046 (let ((orig (point)))
1047 (goto-char eshell-last-output-block-begin)
1048 (unless (eolp)
1049 (beginning-of-line))
1050 (while (< (point) eshell-last-output-end)
1051 (let ((char (char-after)))
1052 (cond
1053 ((eq char ?\r)
1054 (if (< (1+ (point)) eshell-last-output-end)
1055 (if (memq (char-after (1+ (point)))
1056 '(?\n ?\r))
1057 (delete-char 1)
1058 (let ((end (1+ (point))))
1059 (beginning-of-line)
1060 (delete-region (point) end)))
1061 (add-text-properties (point) (1+ (point))
1062 '(invisible t))
1063 (forward-char)))
1064 ((eq char ?\a)
1065 (delete-char 1)
1066 (beep))
1067 ((eq char ?\C-h)
1068 (delete-backward-char 1)
1069 (delete-char 1))
1071 (forward-char))))))))
1073 (custom-add-option 'eshell-output-filter-functions
1074 'eshell-handle-control-codes)
1076 (autoload 'ansi-color-apply-on-region "ansi-color")
1078 (defun eshell-handle-ansi-color ()
1079 "Handle ANSI color codes."
1080 (ansi-color-apply-on-region eshell-last-output-start
1081 eshell-last-output-end))
1083 (custom-add-option 'eshell-output-filter-functions
1084 'eshell-handle-ansi-color)
1086 ;;; esh-mode.el ends here