(eshell-skip-prompt-function): Fix :type.
[emacs.git] / lisp / eshell / esh-mode.el
blobd0393df4153eaeb143d38bad693b5d6b12605f98
1 ;;; esh-mode --- user interface
3 ;; Copyright (C) 1999, 2000 Free Software Foundation
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 2, or (at your option)
12 ;; 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; see the file COPYING. If not, write to the
21 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 ;; Boston, MA 02111-1307, USA.
24 (provide 'esh-mode)
26 (eval-when-compile (require 'esh-maint))
28 (defgroup eshell-mode nil
29 "This module contains code for handling input from the user."
30 :tag "User interface"
31 :group 'eshell)
33 ;;; Commentary:
35 ;; Basically, Eshell is used just like shell mode (<M-x shell>). The
36 ;; keystrokes for navigating the buffer, and accessing the command
37 ;; history, are identical. Unlike shell mode, however, Eshell mode's
38 ;; governing process is Emacs itself. With shell mode, an inferior
39 ;; shell process is executed that communicates with Emacs via comint
40 ;; -- a mode for handling sub-process interaction. Eshell mode, on
41 ;; the other hand, is a truly native Emacs shell. No subprocess are
42 ;; invoked except the ones requested by the user at the prompt.
44 ;; After entering a command, use <RET> to invoke it ([Command
45 ;; invocation]) . If there is a command on disk, it will be executed
46 ;; as in a normal shell. If there is no command by that name on disk,
47 ;; but a Lisp function with that name is defined, the Lisp function
48 ;; will be called, using the arguments passed on the command line.
50 ;; Some of the other features of the command interaction mode are:
52 ;; @ <M-RET> can be used to accumulate further commands while a
53 ;; command is currently running. Since all input is passed to the
54 ;; subprocess being executed, there is no automatic input queueing
55 ;; as there is with other shells.
57 ;; @ <C-c C-t> can be used to truncate the buffer if it grows too
58 ;; large.
60 ;; @ <C-c C-r> will move point to the beginning of the output of the
61 ;; last command. With a prefix argument, it will narrow to view
62 ;; only that output.
64 ;; @ <C-c C-o> will delete the output from the last command.
66 ;; @ <C-c C-f> will move forward a complete shell argument.
68 ;; @ <C-c C-b> will move backward a complete shell argument.
70 (require 'esh-module)
71 (require 'esh-cmd)
72 (require 'esh-io)
73 (require 'esh-var)
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-handle-control-codes
168 eshell-watch-for-password-prompt)
169 "*Functions to call before output is displayed.
170 These functions are only called for output that is displayed
171 interactively, and not for output which is redirected."
172 :type 'hook
173 :group 'eshell-mode)
175 (defcustom eshell-preoutput-filter-functions nil
176 "*Functions to call before output is inserted into the buffer.
177 These functions get one argument, a string containing the text to be
178 inserted. They return the string as it should be inserted."
179 :type 'hook
180 :group 'eshell-mode)
182 (defcustom eshell-password-prompt-regexp
183 "[Pp]ass\\(word\\|phrase\\).*:\\s *\\'"
184 "*Regexp matching prompts for passwords in the inferior process.
185 This is used by `eshell-watch-for-password-prompt'."
186 :type 'regexp
187 :group 'eshell-mode)
189 (defcustom eshell-skip-prompt-function nil
190 "*A function called from beginning of line to skip the prompt."
191 :type '(choice (const nil) function)
192 :group 'eshell-mode)
194 (defcustom eshell-status-in-modeline t
195 "*If non-nil, let the user know a command is running in the modeline."
196 :type 'boolean
197 :group 'eshell-mode)
199 (defvar eshell-non-interactive-p nil
200 "A variable which is non-nil when Eshell is not running interactively.
201 Modules should use this variable so that they don't clutter non-interactive
202 sessions, such as when using `eshell-command'.")
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)
223 (defvar eshell-mode-syntax-table nil)
224 (defvar eshell-mode-abbrev-table nil)
226 (define-abbrev-table 'eshell-mode-abbrev-table ())
228 (eval-when-compile
229 (unless (eshell-under-xemacs-p)
230 (defalias 'characterp 'ignore)
231 (defalias 'char-int 'ignore)))
233 (if (not eshell-mode-syntax-table)
234 (let ((i 0))
235 (setq eshell-mode-syntax-table (make-syntax-table))
236 (while (< i ?0)
237 (modify-syntax-entry i "_ " eshell-mode-syntax-table)
238 (setq i (1+ i)))
239 (setq i (1+ ?9))
240 (while (< i ?A)
241 (modify-syntax-entry i "_ " eshell-mode-syntax-table)
242 (setq i (1+ i)))
243 (setq i (1+ ?Z))
244 (while (< i ?a)
245 (modify-syntax-entry i "_ " eshell-mode-syntax-table)
246 (setq i (1+ i)))
247 (setq i (1+ ?z))
248 (while (< i 128)
249 (modify-syntax-entry i "_ " eshell-mode-syntax-table)
250 (setq i (1+ i)))
251 (modify-syntax-entry ? " " eshell-mode-syntax-table)
252 (modify-syntax-entry ?\t " " eshell-mode-syntax-table)
253 (modify-syntax-entry ?\f " " eshell-mode-syntax-table)
254 (modify-syntax-entry ?\n "> " eshell-mode-syntax-table)
255 ;; Give CR the same syntax as newline, for selective-display.
256 (modify-syntax-entry ?\^m "> " 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 ;; Used to be singlequote; changed for flonums.
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 (modify-syntax-entry ?\" "\" " eshell-mode-syntax-table)
267 (modify-syntax-entry ?\\ "/ " eshell-mode-syntax-table)
268 (modify-syntax-entry ?\( "() " eshell-mode-syntax-table)
269 (modify-syntax-entry ?\) ")( " eshell-mode-syntax-table)
270 (modify-syntax-entry ?\{ "(} " eshell-mode-syntax-table)
271 (modify-syntax-entry ?\} "){ " eshell-mode-syntax-table)
272 (modify-syntax-entry ?\[ "(] " eshell-mode-syntax-table)
273 (modify-syntax-entry ?\] ")[ " eshell-mode-syntax-table)
274 ;; All non-word multibyte characters should be `symbol'.
275 (if (eshell-under-xemacs-p)
276 (map-char-table
277 (function
278 (lambda (key val)
279 (and (characterp key)
280 (>= (char-int key) 256)
281 (/= (char-syntax key) ?w)
282 (modify-syntax-entry key "_ "
283 eshell-mode-syntax-table))))
284 (standard-syntax-table))
285 (map-char-table
286 (function
287 (lambda (key val)
288 (and (>= key 256)
289 (/= (char-syntax key) ?w)
290 (modify-syntax-entry key "_ "
291 eshell-mode-syntax-table))))
292 (standard-syntax-table)))))
294 ;;; User Functions:
296 ;;;###autoload
297 (defun eshell-mode ()
298 "Emacs shell interactive mode.
300 \\{eshell-mode-map}"
301 (kill-all-local-variables)
303 (setq major-mode 'eshell-mode)
304 (setq mode-name "EShell")
305 (set (make-local-variable 'eshell-mode) t)
307 (make-local-variable 'eshell-mode-map)
308 (setq eshell-mode-map (make-sparse-keymap))
309 (use-local-map eshell-mode-map)
311 (when eshell-status-in-modeline
312 (make-local-variable 'eshell-command-running-string)
313 (let ((fmt (eshell-copy-list mode-line-format)))
314 (make-local-variable 'mode-line-format)
315 (setq mode-line-format fmt))
316 (let ((modeline (memq 'mode-line-modified mode-line-format)))
317 (if modeline
318 (setcar modeline 'eshell-command-running-string))))
320 (define-key eshell-mode-map [return] 'eshell-send-input)
321 (define-key eshell-mode-map [(control ?m)] 'eshell-send-input)
322 (define-key eshell-mode-map [(control ?j)] 'eshell-send-input)
323 (define-key eshell-mode-map [(meta return)] 'eshell-queue-input)
324 (define-key eshell-mode-map [(meta control ?m)] 'eshell-queue-input)
325 (define-key eshell-mode-map [(meta control ?l)] 'eshell-show-output)
327 (set (make-local-variable 'eshell-command-prefix)
328 (make-symbol "eshell-command-prefix"))
329 (fset eshell-command-prefix (make-sparse-keymap))
330 (set (make-local-variable 'eshell-command-map)
331 (symbol-function eshell-command-prefix))
332 (define-key eshell-mode-map [(control ?c)] eshell-command-prefix)
334 ;; without this, find-tag complains about read-only text being
335 ;; modified
336 (if (eq (key-binding [(meta ?.)]) 'find-tag)
337 (define-key eshell-mode-map [(meta ?.)] 'eshell-find-tag))
338 (define-key eshell-command-map [(meta ?o)] 'eshell-mark-output)
339 (define-key eshell-command-map [(meta ?d)] 'eshell-toggle-direct-send)
341 (define-key eshell-command-map [(control ?a)] 'eshell-bol)
342 (define-key eshell-command-map [(control ?b)] 'eshell-backward-argument)
343 (define-key eshell-command-map [(control ?e)] 'eshell-show-maximum-output)
344 (define-key eshell-command-map [(control ?f)] 'eshell-forward-argument)
345 (define-key eshell-command-map [return] 'eshell-copy-old-input)
346 (define-key eshell-command-map [(control ?m)] 'eshell-copy-old-input)
347 (define-key eshell-command-map [(control ?o)] 'eshell-kill-output)
348 (define-key eshell-command-map [(control ?r)] 'eshell-show-output)
349 (define-key eshell-command-map [(control ?t)] 'eshell-truncate-buffer)
350 (define-key eshell-command-map [(control ?u)] 'eshell-kill-input)
351 (define-key eshell-command-map [(control ?w)] 'backward-kill-word)
352 (define-key eshell-command-map [(control ?y)] 'eshell-repeat-argument)
354 (setq local-abbrev-table eshell-mode-abbrev-table)
355 (set-syntax-table eshell-mode-syntax-table)
357 (set (make-local-variable 'dired-directory) default-directory)
358 (set (make-local-variable 'list-buffers-directory)
359 (expand-file-name default-directory))
361 ;; always set the tab width to 8 in Eshell buffers, since external
362 ;; commands which do their own formatting almost always expect this
363 (set (make-local-variable 'tab-width) 8)
365 ;; don't ever use auto-fill in Eshell buffers
366 (setq auto-fill-function nil)
368 ;; always display everything from a return value
369 (if (boundp 'print-length)
370 (set (make-local-variable 'print-length) nil))
371 (if (boundp 'print-level)
372 (set (make-local-variable 'print-level) nil))
374 ;; set require-final-newline to nil; otherwise, all redirected
375 ;; output will end with a newline, whether or not the source
376 ;; indicated it!
377 (set (make-local-variable 'require-final-newline) nil)
379 (set (make-local-variable 'max-lisp-eval-depth)
380 (max 3000 max-lisp-eval-depth))
381 (set (make-local-variable 'max-specpdl-size)
382 (max 6000 max-lisp-eval-depth))
384 (set (make-local-variable 'eshell-last-input-start) (point-marker))
385 (set (make-local-variable 'eshell-last-input-end) (point-marker))
386 (set (make-local-variable 'eshell-last-output-start) (point-marker))
387 (set (make-local-variable 'eshell-last-output-end) (point-marker))
388 (set (make-local-variable 'eshell-last-output-block-begin) (point))
390 (let ((modules-list (eshell-copy-list eshell-modules-list)))
391 (make-local-variable 'eshell-modules-list)
392 (setq eshell-modules-list modules-list))
394 ;; load extension modules into memory. This will cause any global
395 ;; variables they define to be visible, since some of the core
396 ;; modules sometimes take advantage of their functionality if used.
397 (eshell-for module eshell-modules-list
398 (let ((module-fullname (symbol-name module))
399 module-shortname)
400 (if (string-match "^eshell-\\(.*\\)" module-fullname)
401 (setq module-shortname
402 (concat "em-" (match-string 1 module-fullname))))
403 (unless module-shortname
404 (error "Invalid Eshell module name: %s" module-fullname))
405 (unless (featurep (intern module-shortname))
406 (load module-shortname))))
408 (unless (file-exists-p eshell-directory-name)
409 (eshell-make-private-directory eshell-directory-name t))
411 ;; load core Eshell modules for this session
412 (eshell-for module (eshell-subgroups 'eshell)
413 (run-hooks (intern-soft (concat (symbol-name module)
414 "-load-hook"))))
416 ;; load extension modules for this session
417 (eshell-for module eshell-modules-list
418 (let ((load-hook (intern-soft (concat (symbol-name module)
419 "-load-hook"))))
420 (if (and load-hook (boundp load-hook))
421 (run-hooks load-hook))))
423 (make-local-hook 'pre-command-hook)
425 (if eshell-send-direct-to-subprocesses
426 (add-hook 'pre-command-hook 'eshell-intercept-commands t t))
428 (if eshell-scroll-to-bottom-on-input
429 (add-hook 'pre-command-hook 'eshell-preinput-scroll-to-bottom t t))
431 (when eshell-scroll-show-maximum-output
432 (set (make-local-variable 'scroll-conservatively) 1000))
434 (when eshell-status-in-modeline
435 (make-local-hook 'eshell-pre-command-hook)
436 (add-hook 'eshell-pre-command-hook 'eshell-command-started nil t)
437 (make-local-hook 'eshell-post-command-hook)
438 (add-hook 'eshell-post-command-hook 'eshell-command-finished nil t))
440 (make-local-hook 'kill-buffer-hook)
441 (add-hook 'kill-buffer-hook
442 (function
443 (lambda ()
444 (run-hooks 'eshell-exit-hook))) t t)
446 (if eshell-first-time-p
447 (run-hooks 'eshell-first-time-mode-hook))
448 (run-hooks 'eshell-mode-hook)
449 (run-hooks 'eshell-post-command-hook))
451 (put 'eshell-mode 'mode-class 'special)
453 (eshell-deftest mode major-mode
454 "Major mode is correct"
455 (eq major-mode 'eshell-mode))
457 (eshell-deftest mode eshell-mode-variable
458 "`eshell-mode' is true"
459 (eq eshell-mode t))
461 (eshell-deftest var window-height
462 "LINES equals window height"
463 (let ((eshell-stringify-t t))
464 (eshell-command-result-p "= $LINES (window-height)" "t\n")))
466 (defun eshell-command-started ()
467 "Indicate in the modeline that a command has started."
468 (setq eshell-command-running-string "**")
469 (force-mode-line-update))
471 (defun eshell-command-finished ()
472 "Indicate in the modeline that a command has finished."
473 (setq eshell-command-running-string "--")
474 (force-mode-line-update))
476 (eshell-deftest mode command-running-p
477 "Modeline shows no command running"
478 (or (eshell-under-xemacs-p)
479 (not eshell-status-in-modeline)
480 (and (memq 'eshell-command-running-string mode-line-format)
481 (equal eshell-command-running-string "--"))))
483 ;;; Internal Functions:
485 (defun eshell-toggle-direct-send ()
486 (interactive)
487 (if eshell-send-direct-to-subprocesses
488 (progn
489 (setq eshell-send-direct-to-subprocesses nil)
490 (remove-hook 'pre-command-hook 'eshell-intercept-commands t)
491 (message "Sending subprocess input on RET"))
492 (setq eshell-send-direct-to-subprocesses t)
493 (add-hook 'pre-command-hook 'eshell-intercept-commands t t)
494 (message "Sending subprocess input directly")))
496 (defun eshell-self-insert-command (N)
497 (interactive "i")
498 (process-send-string
499 (eshell-interactive-process)
500 (char-to-string (if (symbolp last-command-char)
501 (get last-command-char 'ascii-character)
502 last-command-char))))
504 (defun eshell-intercept-commands ()
505 (when (and (eshell-interactive-process)
506 (not (and (integerp last-input-event)
507 (memq last-input-event '(?\C-x ?\C-c)))))
508 (let ((possible-events (where-is-internal this-command))
509 (name (symbol-name this-command))
510 (intercept t))
511 ;; Assume that any multikey combination which does NOT target an
512 ;; Eshell command, is a combo the user wants invoked rather than
513 ;; sent to the underlying subprocess.
514 (unless (and (> (length name) 7)
515 (equal (substring name 0 7) "eshell-"))
516 (while possible-events
517 (if (> (length (car possible-events)) 1)
518 (setq intercept nil possible-events nil)
519 (setq possible-events (cdr possible-events)))))
520 (if intercept
521 (setq this-command 'eshell-self-insert-command)))))
523 (defun eshell-find-tag (&optional tagname next-p regexp-p)
524 "A special version of `find-tag' that ignores read-onlyness."
525 (interactive)
526 (require 'etags)
527 (let ((inhibit-read-only t)
528 (no-default (eobp))
529 (find-tag-default-function 'ignore))
530 (setq tagname (car (find-tag-interactive "Find tag: ")))
531 (find-tag tagname next-p regexp-p)))
533 (defun eshell-move-argument (limit func property arg)
534 "Move forward ARG arguments."
535 (catch 'eshell-incomplete
536 (eshell-parse-arguments (save-excursion (eshell-bol) (point))
537 (line-end-position)))
538 (let ((pos (save-excursion
539 (funcall func 1)
540 (while (and (> arg 0) (/= (point) limit))
541 (if (get-text-property (point) property)
542 (setq arg (1- arg)))
543 (if (> arg 0)
544 (funcall func 1)))
545 (point))))
546 (goto-char pos)
547 (if (and (eq func 'forward-char)
548 (= (1+ pos) limit))
549 (forward-char 1))))
551 (eshell-deftest arg forward-arg
552 "Move across command arguments"
553 (eshell-insert-command "echo $(+ 1 (- 4 3)) \"alpha beta\" file" 'ignore)
554 (let ((here (point)) begin valid)
555 (eshell-bol)
556 (setq begin (point))
557 (eshell-forward-argument 4)
558 (setq valid (= here (point)))
559 (eshell-backward-argument 4)
560 (prog1
561 (and valid (= begin (point)))
562 (eshell-bol)
563 (delete-region (point) (point-max)))))
565 (defun eshell-forward-argument (&optional arg)
566 "Move forward ARG arguments."
567 (interactive "p")
568 (eshell-move-argument (point-max) 'forward-char 'arg-end arg))
570 (defun eshell-backward-argument (&optional arg)
571 "Move backward ARG arguments."
572 (interactive "p")
573 (eshell-move-argument (point-min) 'backward-char 'arg-begin arg))
575 (defun eshell-repeat-argument (&optional arg)
576 (interactive "p")
577 (let ((begin (save-excursion
578 (eshell-backward-argument arg)
579 (point))))
580 (kill-ring-save begin (point))
581 (yank)))
583 (defun eshell-bol ()
584 "Goes to the beginning of line, then skips past the prompt, if any."
585 (interactive)
586 (beginning-of-line)
587 (and eshell-skip-prompt-function
588 (funcall eshell-skip-prompt-function)))
590 (defsubst eshell-push-command-mark ()
591 "Push a mark at the end of the last input text."
592 (push-mark (1- eshell-last-input-end) t))
594 (custom-add-option 'eshell-pre-command-hook 'eshell-push-command-mark)
596 (defsubst eshell-goto-input-start ()
597 "Goto the start of the last command input.
598 Putting this function on `eshell-pre-command-hook' will mimic Plan 9's
599 9term behavior."
600 (goto-char eshell-last-input-start))
602 (custom-add-option 'eshell-pre-command-hook 'eshell-push-command-mark)
604 (defsubst eshell-interactive-print (string)
605 "Print STRING to the eshell display buffer."
606 (eshell-output-filter nil string))
608 (defsubst eshell-begin-on-new-line ()
609 "This function outputs a newline if not at beginning of line."
610 (save-excursion
611 (goto-char eshell-last-output-end)
612 (or (bolp)
613 (eshell-interactive-print "\n"))))
615 (defsubst eshell-reset (&optional no-hooks)
616 "Output a prompt on a new line, aborting any current input.
617 If NO-HOOKS is non-nil, then `eshell-post-command-hook' won't be run."
618 (goto-char (point-max))
619 (setq eshell-last-input-start (point-marker)
620 eshell-last-input-end (point-marker)
621 eshell-last-output-start (point-marker)
622 eshell-last-output-block-begin (point)
623 eshell-last-output-end (point-marker))
624 (eshell-begin-on-new-line)
625 (unless no-hooks
626 (run-hooks 'eshell-post-command-hook)
627 (goto-char (point-max))))
629 (defun eshell-parse-command-input (beg end &optional args)
630 "Parse the command input from BEG to END.
631 The difference is that `eshell-parse-command' expects a complete
632 command string (and will error if it doesn't get one), whereas this
633 function will inform the caller whether more input is required.
635 If nil is returned, more input is necessary (probably because a
636 multi-line input string wasn't terminated properly). Otherwise, it
637 will return the parsed command."
638 (let (delim command)
639 (if (setq delim
640 (catch 'eshell-incomplete
641 (ignore
642 (setq command (eshell-parse-command (cons beg end)
643 args t)))))
644 (ignore
645 (message "Expecting completion of delimeter %c ..."
646 (if (listp delim)
647 (car delim)
648 delim)))
649 command)))
651 (defun eshell-update-markers (pmark)
652 "Update the input and output markers relative to point and PMARK."
653 (set-marker eshell-last-input-start pmark)
654 (set-marker eshell-last-input-end (point))
655 (set-marker eshell-last-output-end (point)))
657 (defun eshell-queue-input (&optional use-region)
658 "Queue the current input text for execution by Eshell.
659 Particularly, don't send the text to the current process, even if it's
660 waiting for input."
661 (interactive "P")
662 (eshell-send-input use-region t))
664 (eshell-deftest mode queue-input
665 "Queue command input"
666 (eshell-insert-command "sleep 2")
667 (eshell-insert-command "echo alpha" 'eshell-queue-input)
668 (let ((count 10))
669 (while (and eshell-current-command
670 (> count 0))
671 (sit-for 1 0)
672 (setq count (1- count))))
673 (eshell-match-result "alpha\n"))
675 (defun eshell-send-input (&optional use-region queue-p no-newline)
676 "Send the input received to Eshell for parsing and processing..
677 After `eshell-last-output-end', sends all text from that marker to
678 point as input. Before that marker, calls `eshell-get-old-input' to
679 retrieve old input, copies it to the end of the buffer, and sends it.
681 If USE-REGION is non-nil, the current region (between point and mark)
682 will be used as input.
684 If QUEUE-P is non-nil, input will be queued until the next prompt,
685 rather than sent to the currently active process. If no process, the
686 input is processed immediately.
688 If NO-NEWLINE is non-nil, the input is sent without an implied final
689 newline."
690 (interactive "P")
691 ;; Note that the input string does not include its terminal newline.
692 (let ((proc-running-p (and (eshell-interactive-process)
693 (not queue-p)))
694 (inhibit-point-motion-hooks t)
695 after-change-functions)
696 (unless (and proc-running-p
697 (not (eq (process-status
698 (eshell-interactive-process)) 'run)))
699 (if (or proc-running-p
700 (>= (point) eshell-last-output-end))
701 (goto-char (point-max))
702 (let ((copy (eshell-get-old-input use-region)))
703 (goto-char eshell-last-output-end)
704 (insert-and-inherit copy)))
705 (unless (or no-newline
706 (and eshell-send-direct-to-subprocesses
707 proc-running-p))
708 (insert-before-markers-and-inherit ?\n))
709 (if proc-running-p
710 (progn
711 (eshell-update-markers eshell-last-output-end)
712 (if (or eshell-send-direct-to-subprocesses
713 (= eshell-last-input-start eshell-last-input-end))
714 (unless no-newline
715 (process-send-string (eshell-interactive-process) "\n"))
716 (process-send-region (eshell-interactive-process)
717 eshell-last-input-start
718 eshell-last-input-end)))
719 (if (= eshell-last-output-end (point))
720 (run-hooks 'eshell-post-command-hook)
721 (let (input)
722 (eshell-condition-case err
723 (progn
724 (setq input (buffer-substring-no-properties
725 eshell-last-output-end (1- (point))))
726 (run-hook-with-args 'eshell-expand-input-functions
727 eshell-last-output-end (1- (point)))
728 (let ((cmd (eshell-parse-command-input
729 eshell-last-output-end (1- (point)))))
730 (when cmd
731 (eshell-update-markers eshell-last-output-end)
732 (setq input (buffer-substring-no-properties
733 eshell-last-input-start
734 (1- eshell-last-input-end)))
735 (run-hooks 'eshell-input-filter-functions)
736 (and (catch 'eshell-terminal
737 (ignore
738 (if (eshell-invoke-directly cmd input)
739 (eval cmd)
740 (eshell-eval-command cmd input))))
741 (eshell-life-is-too-much)))))
742 (quit
743 (eshell-reset t)
744 (run-hooks 'eshell-post-command-hook)
745 (signal 'quit nil))
746 (error
747 (eshell-reset t)
748 (eshell-interactive-print
749 (concat (error-message-string err) "\n"))
750 (run-hooks 'eshell-post-command-hook)
751 (insert-and-inherit input)))))))))
753 (eshell-deftest proc send-to-subprocess
754 "Send input to a subprocess"
755 ;; jww (1999-12-06): what about when bc is unavailable?
756 (if (not (eshell-search-path "bc"))
758 (eshell-insert-command "bc")
759 (eshell-insert-command "1 + 2")
760 (sit-for 1 0)
761 (forward-line -1)
762 (prog1
763 (looking-at "3\n")
764 (eshell-insert-command "quit")
765 (sit-for 1 0))))
767 (defsubst eshell-kill-new ()
768 "Add the last input text to the kill ring."
769 (kill-ring-save eshell-last-input-start eshell-last-input-end))
771 (custom-add-option 'eshell-input-filter-functions 'eshell-kill-new)
773 (defun eshell-output-filter (process string)
774 "Send the output from PROCESS (STRING) to the interactive display.
775 This is done after all necessary filtering has been done."
776 (let ((oprocbuf (if process (process-buffer process)
777 (current-buffer)))
778 (inhibit-point-motion-hooks t)
779 after-change-functions)
780 (let ((functions eshell-preoutput-filter-functions))
781 (while (and functions string)
782 (setq string (funcall (car functions) string))
783 (setq functions (cdr functions))))
784 (if (and string oprocbuf (buffer-name oprocbuf))
785 (let ((obuf (current-buffer))
786 opoint obeg oend)
787 (set-buffer oprocbuf)
788 (setq opoint (point))
789 (setq obeg (point-min))
790 (setq oend (point-max))
791 (let ((buffer-read-only nil)
792 (nchars (length string))
793 (ostart nil))
794 (widen)
795 (goto-char eshell-last-output-end)
796 (setq ostart (point))
797 (if (<= (point) opoint)
798 (setq opoint (+ opoint nchars)))
799 (if (< (point) obeg)
800 (setq obeg (+ obeg nchars)))
801 (if (<= (point) oend)
802 (setq oend (+ oend nchars)))
803 (insert-before-markers string)
804 (if (= (window-start (selected-window)) (point))
805 (set-window-start (selected-window)
806 (- (point) nchars)))
807 (if (= (point) eshell-last-input-end)
808 (set-marker eshell-last-input-end
809 (- eshell-last-input-end nchars)))
810 (set-marker eshell-last-output-start ostart)
811 (set-marker eshell-last-output-end (point))
812 (force-mode-line-update))
813 (narrow-to-region obeg oend)
814 (goto-char opoint)
815 (eshell-run-output-filters)
816 (set-buffer obuf)))))
818 (defun eshell-run-output-filters ()
819 "Run the `eshell-output-filter-functions' on the current output."
820 (save-current-buffer
821 (run-hooks 'eshell-output-filter-functions))
822 (setq eshell-last-output-block-begin
823 (marker-position eshell-last-output-end)))
825 ;;; jww (1999-10-23): this needs testing
826 (defun eshell-preinput-scroll-to-bottom ()
827 "Go to the end of buffer in all windows showing it.
828 Movement occurs if point in the selected window is not after the
829 process mark, and `this-command' is an insertion command. Insertion
830 commands recognised are `self-insert-command', `yank', and
831 `hilit-yank'. Depends on the value of
832 `eshell-scroll-to-bottom-on-input'.
834 This function should be a pre-command hook."
835 (if (memq this-command '(self-insert-command yank hilit-yank))
836 (let* ((selected (selected-window))
837 (current (current-buffer))
838 (scroll eshell-scroll-to-bottom-on-input))
839 (if (< (point) eshell-last-output-end)
840 (if (eq scroll 'this)
841 (goto-char (point-max))
842 (walk-windows
843 (function
844 (lambda (window)
845 (when (and (eq (window-buffer window) current)
846 (or (eq scroll t) (eq scroll 'all)))
847 (select-window window)
848 (goto-char (point-max))
849 (select-window selected))))
850 nil t))))))
852 ;;; jww (1999-10-23): this needs testing
853 (defun eshell-postoutput-scroll-to-bottom ()
854 "Go to the end of buffer in all windows showing it.
855 Does not scroll if the current line is the last line in the buffer.
856 Depends on the value of `eshell-scroll-to-bottom-on-output' and
857 `eshell-scroll-show-maximum-output'.
859 This function should be in the list `eshell-output-filter-functions'."
860 (let* ((selected (selected-window))
861 (current (current-buffer))
862 (scroll eshell-scroll-to-bottom-on-output))
863 (unwind-protect
864 (walk-windows
865 (function
866 (lambda (window)
867 (if (eq (window-buffer window) current)
868 (progn
869 (select-window window)
870 (if (and (< (point) eshell-last-output-end)
871 (or (eq scroll t) (eq scroll 'all)
872 ;; Maybe user wants point to jump to end.
873 (and (eq scroll 'this)
874 (eq selected window))
875 (and (eq scroll 'others)
876 (not (eq selected window)))
877 ;; If point was at the end, keep it at end.
878 (>= (point) eshell-last-output-start)))
879 (goto-char eshell-last-output-end))
880 ;; Optionally scroll so that the text
881 ;; ends at the bottom of the window.
882 (if (and eshell-scroll-show-maximum-output
883 (>= (point) eshell-last-output-end))
884 (save-excursion
885 (goto-char (point-max))
886 (recenter -1)))
887 (select-window selected)))))
888 nil t)
889 (set-buffer current))))
891 (custom-add-option 'eshell-output-filter-functions
892 'eshell-postoutput-scroll-to-bottom)
894 (defun eshell-beginning-of-input ()
895 "Return the location of the start of the previous input."
896 eshell-last-input-start)
898 (defun eshell-beginning-of-output ()
899 "Return the location of the end of the previous output block."
900 eshell-last-input-end)
902 (defun eshell-end-of-output ()
903 "Return the location of the end of the previous output block."
904 (if (eshell-using-module 'eshell-prompt)
905 eshell-last-output-start
906 eshell-last-output-end))
908 (defun eshell-kill-output ()
909 "Kill all output from interpreter since last input.
910 Does not delete the prompt."
911 (interactive)
912 (save-excursion
913 (goto-char (eshell-beginning-of-output))
914 (insert "*** output flushed ***\n")
915 (delete-region (point) (eshell-end-of-output))))
917 (eshell-deftest io flush-output
918 "Flush previous output"
919 (eshell-insert-command "echo alpha")
920 (eshell-kill-output)
921 (and (eshell-match-result (regexp-quote "*** output flushed ***\n"))
922 (forward-line)
923 (= (point) eshell-last-output-start)))
925 (defun eshell-show-output (&optional arg)
926 "Display start of this batch of interpreter output at top of window.
927 Sets mark to the value of point when this command is run.
928 With a prefix argument, narrows region to last command output."
929 (interactive "P")
930 (goto-char (eshell-beginning-of-output))
931 (set-window-start (selected-window)
932 (save-excursion
933 (goto-char (eshell-beginning-of-input))
934 (line-beginning-position)))
935 (if arg
936 (narrow-to-region (eshell-beginning-of-output)
937 (eshell-end-of-output)))
938 (eshell-end-of-output))
940 (defun eshell-mark-output (&optional arg)
941 "Display start of this batch of interpreter output at top of window.
942 Sets mark to the value of point when this command is run.
943 With a prefix argument, narrows region to last command output."
944 (interactive "P")
945 (push-mark (eshell-show-output arg)))
947 (defun eshell-kill-input ()
948 "Kill all text from last stuff output by interpreter to point."
949 (interactive)
950 (if (> (point) eshell-last-output-end)
951 (kill-region eshell-last-output-end (point))
952 (let ((here (point)))
953 (eshell-bol)
954 (kill-region (point) here))))
956 (defun eshell-show-maximum-output ()
957 "Put the end of the buffer at the bottom of the window."
958 (interactive)
959 (if (interactive-p)
960 (widen))
961 (goto-char (point-max))
962 (recenter -1))
964 (defun eshell-get-old-input (&optional use-current-region)
965 "Return the command input on the current line."
966 (if use-current-region
967 (buffer-substring (min (point) (mark))
968 (max (point) (mark)))
969 (save-excursion
970 (beginning-of-line)
971 (and eshell-skip-prompt-function
972 (funcall eshell-skip-prompt-function))
973 (let ((beg (point)))
974 (end-of-line)
975 (buffer-substring beg (point))))))
977 (defun eshell-copy-old-input ()
978 "Insert after prompt old input at point as new input to be edited."
979 (interactive)
980 (let ((input (eshell-get-old-input)))
981 (goto-char eshell-last-output-end)
982 (insert-and-inherit input)))
984 (eshell-deftest mode run-old-command
985 "Re-run an old command"
986 (eshell-insert-command "echo alpha")
987 (goto-char eshell-last-input-start)
988 (string= (eshell-get-old-input) "echo alpha"))
990 (defun eshell/exit ()
991 "Leave or kill the Eshell buffer, depending on `eshell-kill-on-exit'."
992 (throw 'eshell-terminal t))
994 (defun eshell-life-is-too-much ()
995 "Kill the current buffer (or bury it). Good-bye Eshell."
996 (interactive)
997 (if (not eshell-kill-on-exit)
998 (bury-buffer)
999 (kill-buffer (current-buffer))))
1001 (defun eshell-truncate-buffer ()
1002 "Truncate the buffer to `eshell-buffer-maximum-lines'.
1003 This function could be on `eshell-output-filter-functions' or bound to
1004 a key."
1005 (interactive)
1006 (save-excursion
1007 (goto-char eshell-last-output-end)
1008 (let ((lines (count-lines 1 (point)))
1009 (inhibit-read-only t))
1010 (forward-line (- eshell-buffer-maximum-lines))
1011 (beginning-of-line)
1012 (let ((pos (point)))
1013 (if (bobp)
1014 (if (interactive-p)
1015 (error "Buffer too short to truncate"))
1016 (delete-region (point-min) (point))
1017 (if (interactive-p)
1018 (message "Truncated buffer from %d to %d lines (%.1fk freed)"
1019 lines eshell-buffer-maximum-lines
1020 (/ pos 1024.0))))))))
1022 (custom-add-option 'eshell-output-filter-functions
1023 'eshell-truncate-buffer)
1025 (defun send-invisible (str)
1026 "Read a string without echoing.
1027 Then send it to the process running in the current buffer."
1028 (interactive "P") ; Defeat snooping via C-x ESC ESC
1029 (let ((str (read-passwd
1030 (format "Password: "
1031 (process-name (eshell-interactive-process))))))
1032 (if (stringp str)
1033 (process-send-string (eshell-interactive-process)
1034 (concat str "\n"))
1035 (message "Warning: text will be echoed"))))
1037 (defun eshell-watch-for-password-prompt ()
1038 "Prompt in the minibuffer for password and send without echoing.
1039 This function uses `send-invisible' to read and send a password to the
1040 buffer's process if STRING contains a password prompt defined by
1041 `eshell-password-prompt-regexp'.
1043 This function could be in the list `eshell-output-filter-functions'."
1044 (when (eshell-interactive-process)
1045 (save-excursion
1046 (goto-char eshell-last-output-block-begin)
1047 (beginning-of-line)
1048 (if (re-search-forward eshell-password-prompt-regexp
1049 eshell-last-output-end t)
1050 (send-invisible nil)))))
1052 (custom-add-option 'eshell-output-filter-functions
1053 'eshell-watch-for-password-prompt)
1055 (defun eshell-handle-control-codes ()
1056 "Act properly when certain control codes are seen."
1057 (save-excursion
1058 (let ((orig (point)))
1059 (goto-char eshell-last-output-block-begin)
1060 (unless (eolp)
1061 (beginning-of-line))
1062 (while (< (point) eshell-last-output-end)
1063 (let ((char (char-after)))
1064 (cond
1065 ((eq char ?\r)
1066 (if (< (1+ (point)) eshell-last-output-end)
1067 (if (memq (char-after (1+ (point)))
1068 '(?\n ?\r))
1069 (delete-char 1)
1070 (let ((end (1+ (point))))
1071 (beginning-of-line)
1072 (delete-region (point) end)))
1073 (add-text-properties (point) (1+ (point))
1074 '(invisible t))
1075 (forward-char)))
1076 ((eq char ?\a)
1077 (delete-char 1)
1078 (beep))
1079 ((eq char ?\C-h)
1080 (delete-backward-char 1)
1081 (delete-char 1))
1083 (forward-char))))))))
1085 (custom-add-option 'eshell-output-filter-functions
1086 'eshell-handle-control-codes)
1088 ;;; Code:
1090 ;;; esh-mode.el ends here