*** empty log message ***
[emacs.git] / lisp / eshell / esh-mode.el
blobf76900bf48211c9e6df5fe26ecc44f7da650d9a6
1 ;;; esh-mode.el --- user interface
3 ;; Copyright (C) 1999, 2000, 2001, 2004 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-first-time-p t
200 "A variable which is non-nil the first time Eshell is loaded.")
202 ;; Internal Variables:
204 ;; these are only set to `nil' initially for the sake of the
205 ;; byte-compiler, when compiling other files which `require' this one
206 (defvar eshell-mode nil)
207 (defvar eshell-mode-map nil)
208 (defvar eshell-command-running-string "--")
209 (defvar eshell-command-map nil)
210 (defvar eshell-command-prefix nil)
211 (defvar eshell-last-input-start nil)
212 (defvar eshell-last-input-end nil)
213 (defvar eshell-last-output-start nil)
214 (defvar eshell-last-output-block-begin nil)
215 (defvar eshell-last-output-end nil)
217 (defvar eshell-currently-handling-window nil)
218 (defvar eshell-mode-syntax-table nil)
219 (defvar eshell-mode-abbrev-table nil)
221 (define-abbrev-table 'eshell-mode-abbrev-table ())
223 (eval-when-compile
224 (unless (eshell-under-xemacs-p)
225 (defalias 'characterp 'ignore)
226 (defalias 'char-int 'ignore)))
228 (if (not eshell-mode-syntax-table)
229 (let ((i 0))
230 (setq eshell-mode-syntax-table (make-syntax-table))
231 (while (< i ?0)
232 (modify-syntax-entry i "_ " eshell-mode-syntax-table)
233 (setq i (1+ i)))
234 (setq i (1+ ?9))
235 (while (< i ?A)
236 (modify-syntax-entry i "_ " eshell-mode-syntax-table)
237 (setq i (1+ i)))
238 (setq i (1+ ?Z))
239 (while (< i ?a)
240 (modify-syntax-entry i "_ " eshell-mode-syntax-table)
241 (setq i (1+ i)))
242 (setq i (1+ ?z))
243 (while (< i 128)
244 (modify-syntax-entry i "_ " eshell-mode-syntax-table)
245 (setq i (1+ i)))
246 (modify-syntax-entry ? " " eshell-mode-syntax-table)
247 (modify-syntax-entry ?\t " " eshell-mode-syntax-table)
248 (modify-syntax-entry ?\f " " eshell-mode-syntax-table)
249 (modify-syntax-entry ?\n "> " eshell-mode-syntax-table)
250 ;; Give CR the same syntax as newline, for selective-display.
251 (modify-syntax-entry ?\^m "> " eshell-mode-syntax-table)
252 ;;; (modify-syntax-entry ?\; "< " eshell-mode-syntax-table)
253 (modify-syntax-entry ?` "' " eshell-mode-syntax-table)
254 (modify-syntax-entry ?' "' " eshell-mode-syntax-table)
255 (modify-syntax-entry ?, "' " eshell-mode-syntax-table)
256 ;; Used to be singlequote; changed for flonums.
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 (modify-syntax-entry ?\} "){ " eshell-mode-syntax-table)
267 (modify-syntax-entry ?\[ "(] " eshell-mode-syntax-table)
268 (modify-syntax-entry ?\] ")[ " eshell-mode-syntax-table)
269 ;; All non-word multibyte characters should be `symbol'.
270 (if (eshell-under-xemacs-p)
271 (map-char-table
272 (function
273 (lambda (key val)
274 (and (characterp key)
275 (>= (char-int key) 256)
276 (/= (char-syntax key) ?w)
277 (modify-syntax-entry key "_ "
278 eshell-mode-syntax-table))))
279 (standard-syntax-table))
280 (map-char-table
281 (function
282 (lambda (key val)
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)
322 (set (make-local-variable 'eshell-command-prefix)
323 (make-symbol "eshell-command-prefix"))
324 (fset eshell-command-prefix (make-sparse-keymap))
325 (set (make-local-variable 'eshell-command-map)
326 (symbol-function eshell-command-prefix))
327 (define-key eshell-mode-map [(control ?c)] eshell-command-prefix)
329 ;; without this, find-tag complains about read-only text being
330 ;; modified
331 (if (eq (key-binding [(meta ?.)]) 'find-tag)
332 (define-key eshell-mode-map [(meta ?.)] 'eshell-find-tag))
333 (define-key eshell-command-map [(meta ?o)] 'eshell-mark-output)
334 (define-key eshell-command-map [(meta ?d)] 'eshell-toggle-direct-send)
336 (define-key eshell-command-map [(control ?a)] 'eshell-bol)
337 (define-key eshell-command-map [(control ?b)] 'eshell-backward-argument)
338 (define-key eshell-command-map [(control ?e)] 'eshell-show-maximum-output)
339 (define-key eshell-command-map [(control ?f)] 'eshell-forward-argument)
340 (define-key eshell-command-map [return] 'eshell-copy-old-input)
341 (define-key eshell-command-map [(control ?m)] 'eshell-copy-old-input)
342 (define-key eshell-command-map [(control ?o)] 'eshell-kill-output)
343 (define-key eshell-command-map [(control ?r)] 'eshell-show-output)
344 (define-key eshell-command-map [(control ?t)] 'eshell-truncate-buffer)
345 (define-key eshell-command-map [(control ?u)] 'eshell-kill-input)
346 (define-key eshell-command-map [(control ?w)] 'backward-kill-word)
347 (define-key eshell-command-map [(control ?y)] 'eshell-repeat-argument)
349 (setq local-abbrev-table eshell-mode-abbrev-table)
350 (set-syntax-table eshell-mode-syntax-table)
352 (set (make-local-variable 'dired-directory) default-directory)
353 (set (make-local-variable 'list-buffers-directory)
354 (expand-file-name default-directory))
356 ;; always set the tab width to 8 in Eshell buffers, since external
357 ;; commands which do their own formatting almost always expect this
358 (set (make-local-variable 'tab-width) 8)
360 ;; don't ever use auto-fill in Eshell buffers
361 (setq auto-fill-function nil)
363 ;; always display everything from a return value
364 (if (boundp 'print-length)
365 (set (make-local-variable 'print-length) nil))
366 (if (boundp 'print-level)
367 (set (make-local-variable 'print-level) nil))
369 ;; set require-final-newline to nil; otherwise, all redirected
370 ;; output will end with a newline, whether or not the source
371 ;; indicated it!
372 (set (make-local-variable 'require-final-newline) nil)
374 (set (make-local-variable 'max-lisp-eval-depth)
375 (max 3000 max-lisp-eval-depth))
376 (set (make-local-variable 'max-specpdl-size)
377 (max 6000 max-lisp-eval-depth))
379 (set (make-local-variable 'eshell-last-input-start) (point-marker))
380 (set (make-local-variable 'eshell-last-input-end) (point-marker))
381 (set (make-local-variable 'eshell-last-output-start) (point-marker))
382 (set (make-local-variable 'eshell-last-output-end) (point-marker))
383 (set (make-local-variable 'eshell-last-output-block-begin) (point))
385 (let ((modules-list (copy-sequence eshell-modules-list)))
386 (make-local-variable 'eshell-modules-list)
387 (setq eshell-modules-list modules-list))
389 ;; load extension modules into memory. This will cause any global
390 ;; variables they define to be visible, since some of the core
391 ;; modules sometimes take advantage of their functionality if used.
392 (eshell-for module eshell-modules-list
393 (let ((module-fullname (symbol-name module))
394 module-shortname)
395 (if (string-match "^eshell-\\(.*\\)" module-fullname)
396 (setq module-shortname
397 (concat "em-" (match-string 1 module-fullname))))
398 (unless module-shortname
399 (error "Invalid Eshell module name: %s" module-fullname))
400 (unless (featurep (intern module-shortname))
401 (load module-shortname))))
403 (unless (file-exists-p eshell-directory-name)
404 (eshell-make-private-directory eshell-directory-name t))
406 ;; load core Eshell modules for this session
407 (eshell-for module (eshell-subgroups 'eshell)
408 (run-hooks (intern-soft (concat (symbol-name module)
409 "-load-hook"))))
411 ;; load extension modules for this session
412 (eshell-for module eshell-modules-list
413 (let ((load-hook (intern-soft (concat (symbol-name module)
414 "-load-hook"))))
415 (if (and load-hook (boundp load-hook))
416 (run-hooks load-hook))))
418 (if eshell-send-direct-to-subprocesses
419 (add-hook 'pre-command-hook 'eshell-intercept-commands t t))
421 (if eshell-scroll-to-bottom-on-input
422 (add-hook 'pre-command-hook 'eshell-preinput-scroll-to-bottom t t))
424 (when eshell-scroll-show-maximum-output
425 (set (make-local-variable 'scroll-conservatively) 1000))
427 (when eshell-status-in-modeline
428 (add-hook 'eshell-pre-command-hook 'eshell-command-started nil t)
429 (add-hook 'eshell-post-command-hook 'eshell-command-finished nil t))
431 (add-hook 'kill-buffer-hook
432 (function
433 (lambda ()
434 (run-hooks 'eshell-exit-hook))) t t)
436 (if eshell-first-time-p
437 (run-hooks 'eshell-first-time-mode-hook))
438 (run-hooks 'eshell-mode-hook)
439 (run-hooks 'eshell-post-command-hook))
441 (put 'eshell-mode 'mode-class 'special)
443 (eshell-deftest mode major-mode
444 "Major mode is correct"
445 (eq major-mode 'eshell-mode))
447 (eshell-deftest mode eshell-mode-variable
448 "`eshell-mode' is true"
449 (eq eshell-mode t))
451 (eshell-deftest var window-height
452 "LINES equals window height"
453 (let ((eshell-stringify-t t))
454 (eshell-command-result-p "= $LINES (window-height)" "t\n")))
456 (defun eshell-command-started ()
457 "Indicate in the modeline that a command has started."
458 (setq eshell-command-running-string "**")
459 (force-mode-line-update))
461 (defun eshell-command-finished ()
462 "Indicate in the modeline that a command has finished."
463 (setq eshell-command-running-string "--")
464 (force-mode-line-update))
466 (eshell-deftest mode command-running-p
467 "Modeline shows no command running"
468 (or (eshell-under-xemacs-p)
469 (not eshell-status-in-modeline)
470 (and (memq 'eshell-command-running-string mode-line-format)
471 (equal eshell-command-running-string "--"))))
473 ;;; Internal Functions:
475 (defun eshell-toggle-direct-send ()
476 (interactive)
477 (if eshell-send-direct-to-subprocesses
478 (progn
479 (setq eshell-send-direct-to-subprocesses nil)
480 (remove-hook 'pre-command-hook 'eshell-intercept-commands t)
481 (message "Sending subprocess input on RET"))
482 (setq eshell-send-direct-to-subprocesses t)
483 (add-hook 'pre-command-hook 'eshell-intercept-commands t t)
484 (message "Sending subprocess input directly")))
486 (defun eshell-self-insert-command (N)
487 (interactive "i")
488 (process-send-string
489 (eshell-interactive-process)
490 (char-to-string (if (symbolp last-command-char)
491 (get last-command-char 'ascii-character)
492 last-command-char))))
494 (defun eshell-intercept-commands ()
495 (when (and (eshell-interactive-process)
496 (not (and (integerp last-input-event)
497 (memq last-input-event '(?\C-x ?\C-c)))))
498 (let ((possible-events (where-is-internal this-command))
499 (name (symbol-name this-command))
500 (intercept t))
501 ;; Assume that any multikey combination which does NOT target an
502 ;; Eshell command, is a combo the user wants invoked rather than
503 ;; sent to the underlying subprocess.
504 (unless (and (> (length name) 7)
505 (equal (substring name 0 7) "eshell-"))
506 (while possible-events
507 (if (> (length (car possible-events)) 1)
508 (setq intercept nil possible-events nil)
509 (setq possible-events (cdr possible-events)))))
510 (if intercept
511 (setq this-command 'eshell-self-insert-command)))))
513 (defun eshell-find-tag (&optional tagname next-p regexp-p)
514 "A special version of `find-tag' that ignores read-onlyness."
515 (interactive)
516 (require 'etags)
517 (let ((inhibit-read-only t)
518 (no-default (eobp))
519 (find-tag-default-function 'ignore))
520 (setq tagname (car (find-tag-interactive "Find tag: ")))
521 (find-tag tagname next-p regexp-p)))
523 (defun eshell-move-argument (limit func property arg)
524 "Move forward ARG arguments."
525 (catch 'eshell-incomplete
526 (eshell-parse-arguments (save-excursion (eshell-bol) (point))
527 (line-end-position)))
528 (let ((pos (save-excursion
529 (funcall func 1)
530 (while (and (> arg 0) (/= (point) limit))
531 (if (get-text-property (point) property)
532 (setq arg (1- arg)))
533 (if (> arg 0)
534 (funcall func 1)))
535 (point))))
536 (goto-char pos)
537 (if (and (eq func 'forward-char)
538 (= (1+ pos) limit))
539 (forward-char 1))))
541 (eshell-deftest arg forward-arg
542 "Move across command arguments"
543 (eshell-insert-command "echo $(+ 1 (- 4 3)) \"alpha beta\" file" 'ignore)
544 (let ((here (point)) begin valid)
545 (eshell-bol)
546 (setq begin (point))
547 (eshell-forward-argument 4)
548 (setq valid (= here (point)))
549 (eshell-backward-argument 4)
550 (prog1
551 (and valid (= begin (point)))
552 (eshell-bol)
553 (delete-region (point) (point-max)))))
555 (defun eshell-forward-argument (&optional arg)
556 "Move forward ARG arguments."
557 (interactive "p")
558 (eshell-move-argument (point-max) 'forward-char 'arg-end arg))
560 (defun eshell-backward-argument (&optional arg)
561 "Move backward ARG arguments."
562 (interactive "p")
563 (eshell-move-argument (point-min) 'backward-char 'arg-begin arg))
565 (defun eshell-repeat-argument (&optional arg)
566 (interactive "p")
567 (let ((begin (save-excursion
568 (eshell-backward-argument arg)
569 (point))))
570 (kill-ring-save begin (point))
571 (yank)))
573 (defun eshell-bol ()
574 "Goes to the beginning of line, then skips past the prompt, if any."
575 (interactive)
576 (beginning-of-line)
577 (and eshell-skip-prompt-function
578 (funcall eshell-skip-prompt-function)))
580 (defsubst eshell-push-command-mark ()
581 "Push a mark at the end of the last input text."
582 (push-mark (1- eshell-last-input-end) t))
584 (custom-add-option 'eshell-pre-command-hook 'eshell-push-command-mark)
586 (defsubst eshell-goto-input-start ()
587 "Goto the start of the last command input.
588 Putting this function on `eshell-pre-command-hook' will mimic Plan 9's
589 9term behavior."
590 (goto-char eshell-last-input-start))
592 (custom-add-option 'eshell-pre-command-hook 'eshell-push-command-mark)
594 (defsubst eshell-interactive-print (string)
595 "Print STRING to the eshell display buffer."
596 (eshell-output-filter nil string))
598 (defsubst eshell-begin-on-new-line ()
599 "This function outputs a newline if not at beginning of line."
600 (save-excursion
601 (goto-char eshell-last-output-end)
602 (or (bolp)
603 (eshell-interactive-print "\n"))))
605 (defsubst eshell-reset (&optional no-hooks)
606 "Output a prompt on a new line, aborting any current input.
607 If NO-HOOKS is non-nil, then `eshell-post-command-hook' won't be run."
608 (goto-char (point-max))
609 (setq eshell-last-input-start (point-marker)
610 eshell-last-input-end (point-marker)
611 eshell-last-output-start (point-marker)
612 eshell-last-output-block-begin (point)
613 eshell-last-output-end (point-marker))
614 (eshell-begin-on-new-line)
615 (unless no-hooks
616 (run-hooks 'eshell-post-command-hook)
617 (goto-char (point-max))))
619 (defun eshell-parse-command-input (beg end &optional args)
620 "Parse the command input from BEG to END.
621 The difference is that `eshell-parse-command' expects a complete
622 command string (and will error if it doesn't get one), whereas this
623 function will inform the caller whether more input is required.
625 If nil is returned, more input is necessary (probably because a
626 multi-line input string wasn't terminated properly). Otherwise, it
627 will return the parsed command."
628 (let (delim command)
629 (if (setq delim
630 (catch 'eshell-incomplete
631 (ignore
632 (setq command (eshell-parse-command (cons beg end)
633 args t)))))
634 (ignore
635 (message "Expecting completion of delimeter %c ..."
636 (if (listp delim)
637 (car delim)
638 delim)))
639 command)))
641 (defun eshell-update-markers (pmark)
642 "Update the input and output markers relative to point and PMARK."
643 (set-marker eshell-last-input-start pmark)
644 (set-marker eshell-last-input-end (point))
645 (set-marker eshell-last-output-end (point)))
647 (defun eshell-queue-input (&optional use-region)
648 "Queue the current input text for execution by Eshell.
649 Particularly, don't send the text to the current process, even if it's
650 waiting for input."
651 (interactive "P")
652 (eshell-send-input use-region t))
654 (eshell-deftest mode queue-input
655 "Queue command input"
656 (eshell-insert-command "sleep 2")
657 (eshell-insert-command "echo alpha" 'eshell-queue-input)
658 (let ((count 10))
659 (while (and eshell-current-command
660 (> count 0))
661 (sit-for 1 0)
662 (setq count (1- count))))
663 (eshell-match-result "alpha\n"))
665 (defun eshell-send-input (&optional use-region queue-p no-newline)
666 "Send the input received to Eshell for parsing and processing..
667 After `eshell-last-output-end', sends all text from that marker to
668 point as input. Before that marker, calls `eshell-get-old-input' to
669 retrieve old input, copies it to the end of the buffer, and sends it.
671 If USE-REGION is non-nil, the current region (between point and mark)
672 will be used as input.
674 If QUEUE-P is non-nil, input will be queued until the next prompt,
675 rather than sent to the currently active process. If no process, the
676 input is processed immediately.
678 If NO-NEWLINE is non-nil, the input is sent without an implied final
679 newline."
680 (interactive "P")
681 ;; Note that the input string does not include its terminal newline.
682 (let ((proc-running-p (and (eshell-interactive-process)
683 (not queue-p)))
684 (inhibit-point-motion-hooks t)
685 after-change-functions)
686 (unless (and proc-running-p
687 (not (eq (process-status
688 (eshell-interactive-process)) 'run)))
689 (if (or proc-running-p
690 (>= (point) eshell-last-output-end))
691 (goto-char (point-max))
692 (let ((copy (eshell-get-old-input use-region)))
693 (goto-char eshell-last-output-end)
694 (insert-and-inherit copy)))
695 (unless (or no-newline
696 (and eshell-send-direct-to-subprocesses
697 proc-running-p))
698 (insert-before-markers-and-inherit ?\n))
699 (if proc-running-p
700 (progn
701 (eshell-update-markers eshell-last-output-end)
702 (if (or eshell-send-direct-to-subprocesses
703 (= eshell-last-input-start eshell-last-input-end))
704 (unless no-newline
705 (process-send-string (eshell-interactive-process) "\n"))
706 (process-send-region (eshell-interactive-process)
707 eshell-last-input-start
708 eshell-last-input-end)))
709 (if (= eshell-last-output-end (point))
710 (run-hooks 'eshell-post-command-hook)
711 (let (input)
712 (eshell-condition-case err
713 (progn
714 (setq input (buffer-substring-no-properties
715 eshell-last-output-end (1- (point))))
716 (run-hook-with-args 'eshell-expand-input-functions
717 eshell-last-output-end (1- (point)))
718 (let ((cmd (eshell-parse-command-input
719 eshell-last-output-end (1- (point)))))
720 (when cmd
721 (eshell-update-markers eshell-last-output-end)
722 (setq input (buffer-substring-no-properties
723 eshell-last-input-start
724 (1- eshell-last-input-end)))
725 (run-hooks 'eshell-input-filter-functions)
726 (and (catch 'eshell-terminal
727 (ignore
728 (if (eshell-invoke-directly cmd input)
729 (eval cmd)
730 (eshell-eval-command cmd input))))
731 (eshell-life-is-too-much)))))
732 (quit
733 (eshell-reset t)
734 (run-hooks 'eshell-post-command-hook)
735 (signal 'quit nil))
736 (error
737 (eshell-reset t)
738 (eshell-interactive-print
739 (concat (error-message-string err) "\n"))
740 (run-hooks 'eshell-post-command-hook)
741 (insert-and-inherit input)))))))))
743 ; (eshell-deftest proc send-to-subprocess
744 ; "Send input to a subprocess"
745 ; ;; jww (1999-12-06): what about when bc is unavailable?
746 ; (if (not (eshell-search-path "bc"))
748 ; (eshell-insert-command "bc")
749 ; (eshell-insert-command "1 + 2")
750 ; (sit-for 1 0)
751 ; (forward-line -1)
752 ; (prog1
753 ; (looking-at "3\n")
754 ; (eshell-insert-command "quit")
755 ; (sit-for 1 0))))
757 (defsubst eshell-kill-new ()
758 "Add the last input text to the kill ring."
759 (kill-ring-save eshell-last-input-start eshell-last-input-end))
761 (custom-add-option 'eshell-input-filter-functions 'eshell-kill-new)
763 (defun eshell-output-filter (process string)
764 "Send the output from PROCESS (STRING) to the interactive display.
765 This is done after all necessary filtering has been done."
766 (let ((oprocbuf (if process (process-buffer process)
767 (current-buffer)))
768 (inhibit-point-motion-hooks t)
769 after-change-functions)
770 (let ((functions eshell-preoutput-filter-functions))
771 (while (and functions string)
772 (setq string (funcall (car functions) string))
773 (setq functions (cdr functions))))
774 (if (and string oprocbuf (buffer-name oprocbuf))
775 (let ((obuf (current-buffer))
776 opoint obeg oend)
777 (set-buffer oprocbuf)
778 (setq opoint (point))
779 (setq obeg (point-min))
780 (setq oend (point-max))
781 (let ((buffer-read-only nil)
782 (nchars (length string))
783 (ostart nil))
784 (widen)
785 (goto-char eshell-last-output-end)
786 (setq ostart (point))
787 (if (<= (point) opoint)
788 (setq opoint (+ opoint nchars)))
789 (if (< (point) obeg)
790 (setq obeg (+ obeg nchars)))
791 (if (<= (point) oend)
792 (setq oend (+ oend nchars)))
793 (insert-before-markers string)
794 (if (= (window-start (selected-window)) (point))
795 (set-window-start (selected-window)
796 (- (point) nchars)))
797 (if (= (point) eshell-last-input-end)
798 (set-marker eshell-last-input-end
799 (- eshell-last-input-end nchars)))
800 (set-marker eshell-last-output-start ostart)
801 (set-marker eshell-last-output-end (point))
802 (force-mode-line-update))
803 (narrow-to-region obeg oend)
804 (goto-char opoint)
805 (eshell-run-output-filters)
806 (set-buffer obuf)))))
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 recognised 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 (custom-add-option 'eshell-output-filter-functions
882 'eshell-postoutput-scroll-to-bottom)
884 (defun eshell-beginning-of-input ()
885 "Return the location of the start of the previous input."
886 eshell-last-input-start)
888 (defun eshell-beginning-of-output ()
889 "Return the location of the end of the previous output block."
890 eshell-last-input-end)
892 (defun eshell-end-of-output ()
893 "Return the location of the end of the previous output block."
894 (if (eshell-using-module 'eshell-prompt)
895 eshell-last-output-start
896 eshell-last-output-end))
898 (defun eshell-kill-output ()
899 "Kill all output from interpreter since last input.
900 Does not delete the prompt."
901 (interactive)
902 (save-excursion
903 (goto-char (eshell-beginning-of-output))
904 (insert "*** output flushed ***\n")
905 (delete-region (point) (eshell-end-of-output))))
907 (eshell-deftest io flush-output
908 "Flush previous output"
909 (eshell-insert-command "echo alpha")
910 (eshell-kill-output)
911 (and (eshell-match-result (regexp-quote "*** output flushed ***\n"))
912 (forward-line)
913 (= (point) eshell-last-output-start)))
915 (defun eshell-show-output (&optional arg)
916 "Display start of this batch of interpreter output at top of window.
917 Sets mark to the value of point when this command is run.
918 With a prefix argument, narrows region to last command output."
919 (interactive "P")
920 (goto-char (eshell-beginning-of-output))
921 (set-window-start (selected-window)
922 (save-excursion
923 (goto-char (eshell-beginning-of-input))
924 (line-beginning-position)))
925 (if arg
926 (narrow-to-region (eshell-beginning-of-output)
927 (eshell-end-of-output)))
928 (eshell-end-of-output))
930 (defun eshell-mark-output (&optional arg)
931 "Display start of this batch of interpreter output at top of window.
932 Sets mark to the value of point when this command is run.
933 With a prefix argument, narrows region to last command output."
934 (interactive "P")
935 (push-mark (eshell-show-output arg)))
937 (defun eshell-kill-input ()
938 "Kill all text from last stuff output by interpreter to point."
939 (interactive)
940 (if (> (point) eshell-last-output-end)
941 (kill-region eshell-last-output-end (point))
942 (let ((here (point)))
943 (eshell-bol)
944 (kill-region (point) here))))
946 (defun eshell-show-maximum-output ()
947 "Put the end of the buffer at the bottom of the window."
948 (interactive)
949 (if (interactive-p)
950 (widen))
951 (goto-char (point-max))
952 (recenter -1))
954 (defun eshell-get-old-input (&optional use-current-region)
955 "Return the command input on the current line."
956 (if use-current-region
957 (buffer-substring (min (point) (mark))
958 (max (point) (mark)))
959 (save-excursion
960 (beginning-of-line)
961 (and eshell-skip-prompt-function
962 (funcall eshell-skip-prompt-function))
963 (let ((beg (point)))
964 (end-of-line)
965 (buffer-substring beg (point))))))
967 (defun eshell-copy-old-input ()
968 "Insert after prompt old input at point as new input to be edited."
969 (interactive)
970 (let ((input (eshell-get-old-input)))
971 (goto-char eshell-last-output-end)
972 (insert-and-inherit input)))
974 (eshell-deftest mode run-old-command
975 "Re-run an old command"
976 (eshell-insert-command "echo alpha")
977 (goto-char eshell-last-input-start)
978 (string= (eshell-get-old-input) "echo alpha"))
980 (defun eshell/exit ()
981 "Leave or kill the Eshell buffer, depending on `eshell-kill-on-exit'."
982 (throw 'eshell-terminal t))
984 (defun eshell-life-is-too-much ()
985 "Kill the current buffer (or bury it). Good-bye Eshell."
986 (interactive)
987 (if (not eshell-kill-on-exit)
988 (bury-buffer)
989 (kill-buffer (current-buffer))))
991 (defun eshell-truncate-buffer ()
992 "Truncate the buffer to `eshell-buffer-maximum-lines'.
993 This function could be on `eshell-output-filter-functions' or bound to
994 a key."
995 (interactive)
996 (save-excursion
997 (goto-char eshell-last-output-end)
998 (let ((lines (count-lines 1 (point)))
999 (inhibit-read-only t))
1000 (forward-line (- eshell-buffer-maximum-lines))
1001 (beginning-of-line)
1002 (let ((pos (point)))
1003 (if (bobp)
1004 (if (interactive-p)
1005 (error "Buffer too short to truncate"))
1006 (delete-region (point-min) (point))
1007 (if (interactive-p)
1008 (message "Truncated buffer from %d to %d lines (%.1fk freed)"
1009 lines eshell-buffer-maximum-lines
1010 (/ pos 1024.0))))))))
1012 (custom-add-option 'eshell-output-filter-functions
1013 'eshell-truncate-buffer)
1015 (defun eshell-send-invisible (str)
1016 "Read a string without echoing.
1017 Then send it to the process running in the current buffer."
1018 (interactive "P") ; Defeat snooping via C-x ESC ESC
1019 (let ((str (read-passwd
1020 (format "%s Password: "
1021 (process-name (eshell-interactive-process))))))
1022 (if (stringp str)
1023 (process-send-string (eshell-interactive-process)
1024 (concat str "\n"))
1025 (message "Warning: text will be echoed"))))
1027 (defun eshell-watch-for-password-prompt ()
1028 "Prompt in the minibuffer for password and send without echoing.
1029 This function uses `eshell-send-invisible' to read and send a password to the
1030 buffer's process if STRING contains a password prompt defined by
1031 `eshell-password-prompt-regexp'.
1033 This function could be in the list `eshell-output-filter-functions'."
1034 (when (eshell-interactive-process)
1035 (save-excursion
1036 (goto-char eshell-last-output-block-begin)
1037 (beginning-of-line)
1038 (if (re-search-forward eshell-password-prompt-regexp
1039 eshell-last-output-end t)
1040 (eshell-send-invisible nil)))))
1042 (custom-add-option 'eshell-output-filter-functions
1043 'eshell-watch-for-password-prompt)
1045 (defun eshell-handle-control-codes ()
1046 "Act properly when certain control codes are seen."
1047 (save-excursion
1048 (let ((orig (point)))
1049 (goto-char eshell-last-output-block-begin)
1050 (unless (eolp)
1051 (beginning-of-line))
1052 (while (< (point) eshell-last-output-end)
1053 (let ((char (char-after)))
1054 (cond
1055 ((eq char ?\r)
1056 (if (< (1+ (point)) eshell-last-output-end)
1057 (if (memq (char-after (1+ (point)))
1058 '(?\n ?\r))
1059 (delete-char 1)
1060 (let ((end (1+ (point))))
1061 (beginning-of-line)
1062 (delete-region (point) end)))
1063 (add-text-properties (point) (1+ (point))
1064 '(invisible t))
1065 (forward-char)))
1066 ((eq char ?\a)
1067 (delete-char 1)
1068 (beep))
1069 ((eq char ?\C-h)
1070 (delete-backward-char 1)
1071 (delete-char 1))
1073 (forward-char))))))))
1075 (custom-add-option 'eshell-output-filter-functions
1076 'eshell-handle-control-codes)
1078 ;;; Code:
1080 ;;; arch-tag: ec65bc2b-da14-4547-81d3-a32af3a4dc57
1081 ;;; esh-mode.el ends here