(eshell-var): Fix typo in docstring.
[bpt/emacs.git] / lisp / eshell / esh-mode.el
CommitLineData
60370d40 1;;; esh-mode.el --- user interface
26b4dc84 2
db22de71 3;; Copyright (C) 1999, 2000, 2001, 2004 Free Software Foundation
26b4dc84 4
7de5b421
GM
5;; Author: John Wiegley <johnw@gnu.org>
6
26b4dc84
GM
7;; This file is part of GNU Emacs.
8
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.
13
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.
18
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.
23
24(provide 'esh-mode)
25
26(eval-when-compile (require 'esh-maint))
27
28(defgroup eshell-mode nil
29 "This module contains code for handling input from the user."
30 :tag "User interface"
31 :group 'eshell)
32
33;;; Commentary:
34
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.
43;;
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.
49;;
50;; Some of the other features of the command interaction mode are:
51;;
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.
56;;
57;; @ <C-c C-t> can be used to truncate the buffer if it grows too
58;; large.
59;;
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.
63;;
64;; @ <C-c C-o> will delete the output from the last command.
65;;
66;; @ <C-c C-f> will move forward a complete shell argument.
67;;
68;; @ <C-c C-b> will move backward a complete shell argument.
69
70(require 'esh-module)
71(require 'esh-cmd)
72(require 'esh-io)
73(require 'esh-var)
74
75;;; User Variables:
76
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)
81
82(defcustom eshell-mode-hook nil
83 "*A hook that gets run when `eshell-mode' is entered."
84 :type 'hook
85 :group 'eshell-mode)
86
87(defcustom eshell-first-time-mode-hook nil
88 "*A hook that gets run the first time `eshell-mode' is entered.
89That is to say, the first time during an Emacs session."
90 :type 'hook
91 :group 'eshell-mode)
92
93(defcustom eshell-exit-hook '(eshell-query-kill-processes)
94 "*A hook that is run whenever `eshell' is exited.
95This hook is only run if exiting actually kills the buffer."
96 :type 'hook
97 :group 'eshell-mode)
98
99(defcustom eshell-kill-on-exit t
100 "*If non-nil, kill the Eshell buffer on the `exit' command.
101Otherwise, the buffer will simply be buried."
102 :type 'boolean
103 :group 'eshell-mode)
104
105(defcustom eshell-input-filter-functions nil
106 "*Functions to call before input is processed.
107The input is contained in the region from `eshell-last-input-start' to
108`eshell-last-input-end'."
109 :type 'hook
110 :group 'eshell-mode)
111
79cf8e80
JW
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)
116
26b4dc84
GM
117(defcustom eshell-expand-input-functions nil
118 "*Functions to call before input is parsed.
119Each function is passed two arguments, which bounds the region of the
120current input text."
121 :type 'hook
122 :group 'eshell-mode)
123
124(defcustom eshell-scroll-to-bottom-on-input nil
125 "*Controls whether input to interpreter causes window to scroll.
126If nil, then do not scroll. If t or `all', scroll all windows showing
127buffer. If `this', scroll only the selected window.
128
129See `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)
134
135(defcustom eshell-scroll-to-bottom-on-output nil
136 "*Controls whether interpreter output causes window to scroll.
137If nil, then do not scroll. If t or `all', scroll all windows showing
138buffer. If `this', scroll only the selected window. If `others',
139scroll only those that are not the selected window.
140
141See 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)
148
149(defcustom eshell-scroll-show-maximum-output t
150 "*Controls how interpreter output causes window to scroll.
151If non-nil, then show the maximum output when the window is scrolled.
152
153See variable `eshell-scroll-to-bottom-on-output' and function
154`eshell-postoutput-scroll-to-bottom'."
155 :type 'boolean
156 :group 'eshell-mode)
157
158(defcustom eshell-buffer-maximum-lines 1024
159 "*The maximum size in lines for eshell buffers.
160Eshell buffers are truncated from the top to be no greater than this
161number, if the function `eshell-truncate-buffer' is on
162`eshell-output-filter-functions'."
163 :type 'integer
164 :group 'eshell-mode)
165
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.
170These functions are only called for output that is displayed
171interactively, and not for output which is redirected."
172 :type 'hook
173 :group 'eshell-mode)
174
175(defcustom eshell-preoutput-filter-functions nil
176 "*Functions to call before output is inserted into the buffer.
177These functions get one argument, a string containing the text to be
178inserted. They return the string as it should be inserted."
179 :type 'hook
180 :group 'eshell-mode)
181
182(defcustom eshell-password-prompt-regexp
dace60cf 183 "[Pp]ass\\(word\\|phrase\\).*:\\s *\\'"
26b4dc84
GM
184 "*Regexp matching prompts for passwords in the inferior process.
185This is used by `eshell-watch-for-password-prompt'."
186 :type 'regexp
187 :group 'eshell-mode)
188
189(defcustom eshell-skip-prompt-function nil
190 "*A function called from beginning of line to skip the prompt."
6a44f20f 191 :type '(choice (const nil) function)
26b4dc84
GM
192 :group 'eshell-mode)
193
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)
198
26b4dc84
GM
199(defvar eshell-first-time-p t
200 "A variable which is non-nil the first time Eshell is loaded.")
201
202;; Internal Variables:
203
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)
216
217(defvar eshell-currently-handling-window nil)
218(defvar eshell-mode-syntax-table nil)
219(defvar eshell-mode-abbrev-table nil)
220
221(define-abbrev-table 'eshell-mode-abbrev-table ())
222
223(eval-when-compile
224 (unless (eshell-under-xemacs-p)
225 (defalias 'characterp 'ignore)
226 (defalias 'char-int 'ignore)))
227
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)))))
288
289;;; User Functions:
290
291;;;###autoload
292(defun eshell-mode ()
293 "Emacs shell interactive mode.
294
295\\{eshell-mode-map}"
296 (kill-all-local-variables)
297
298 (setq major-mode 'eshell-mode)
299 (setq mode-name "EShell")
300 (set (make-local-variable 'eshell-mode) t)
301
302 (make-local-variable 'eshell-mode-map)
303 (setq eshell-mode-map (make-sparse-keymap))
304 (use-local-map eshell-mode-map)
305
306 (when eshell-status-in-modeline
307 (make-local-variable 'eshell-command-running-string)
48e889be 308 (let ((fmt (copy-sequence mode-line-format)))
26b4dc84
GM
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))))
314
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
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)
328
b4bd214e
JW
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))
26b4dc84 333 (define-key eshell-command-map [(meta ?o)] 'eshell-mark-output)
79cf8e80 334 (define-key eshell-command-map [(meta ?d)] 'eshell-toggle-direct-send)
26b4dc84
GM
335
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)
b4bd214e 347 (define-key eshell-command-map [(control ?y)] 'eshell-repeat-argument)
26b4dc84
GM
348
349 (setq local-abbrev-table eshell-mode-abbrev-table)
350 (set-syntax-table eshell-mode-syntax-table)
351
352 (set (make-local-variable 'dired-directory) default-directory)
353 (set (make-local-variable 'list-buffers-directory)
354 (expand-file-name default-directory))
355
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)
359
b4bd214e
JW
360 ;; don't ever use auto-fill in Eshell buffers
361 (setq auto-fill-function nil)
362
26b4dc84
GM
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))
368
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)
373
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))
378
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))
384
48e889be 385 (let ((modules-list (copy-sequence eshell-modules-list)))
26b4dc84
GM
386 (make-local-variable 'eshell-modules-list)
387 (setq eshell-modules-list modules-list))
388
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))))
402
403 (unless (file-exists-p eshell-directory-name)
404 (eshell-make-private-directory eshell-directory-name t))
405
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"))))
410
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))))
417
79cf8e80
JW
418 (if eshell-send-direct-to-subprocesses
419 (add-hook 'pre-command-hook 'eshell-intercept-commands t t))
420
421 (if eshell-scroll-to-bottom-on-input
422 (add-hook 'pre-command-hook 'eshell-preinput-scroll-to-bottom t t))
26b4dc84
GM
423
424 (when eshell-scroll-show-maximum-output
425 (set (make-local-variable 'scroll-conservatively) 1000))
426
427 (when eshell-status-in-modeline
26b4dc84 428 (add-hook 'eshell-pre-command-hook 'eshell-command-started nil t)
26b4dc84
GM
429 (add-hook 'eshell-post-command-hook 'eshell-command-finished nil t))
430
26b4dc84
GM
431 (add-hook 'kill-buffer-hook
432 (function
433 (lambda ()
434 (run-hooks 'eshell-exit-hook))) t t)
435
436 (if eshell-first-time-p
437 (run-hooks 'eshell-first-time-mode-hook))
5c3066b8 438 (run-mode-hooks 'eshell-mode-hook)
26b4dc84
GM
439 (run-hooks 'eshell-post-command-hook))
440
441(put 'eshell-mode 'mode-class 'special)
442
443(eshell-deftest mode major-mode
444 "Major mode is correct"
445 (eq major-mode 'eshell-mode))
446
447(eshell-deftest mode eshell-mode-variable
448 "`eshell-mode' is true"
449 (eq eshell-mode t))
450
451(eshell-deftest var window-height
452 "LINES equals window height"
dace60cf
JW
453 (let ((eshell-stringify-t t))
454 (eshell-command-result-p "= $LINES (window-height)" "t\n")))
26b4dc84
GM
455
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))
460
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))
465
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 "--"))))
472
473;;; Internal Functions:
474
79cf8e80
JW
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")))
485
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))))
493
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)))))
512
b4bd214e
JW
513(defun eshell-find-tag (&optional tagname next-p regexp-p)
514 "A special version of `find-tag' that ignores read-onlyness."
515 (interactive)
ca7aae91 516 (require 'etags)
b4bd214e 517 (let ((inhibit-read-only t)
8c6b1d83
JW
518 (no-default (eobp))
519 (find-tag-default-function 'ignore))
5f0fe8f1
RS
520 (with-no-warnings
521 (setq tagname (car (find-tag-interactive "Find tag: "))))
b4bd214e
JW
522 (find-tag tagname next-p regexp-p)))
523
26b4dc84
GM
524(defun eshell-move-argument (limit func property arg)
525 "Move forward ARG arguments."
526 (catch 'eshell-incomplete
527 (eshell-parse-arguments (save-excursion (eshell-bol) (point))
528 (line-end-position)))
b4bd214e
JW
529 (let ((pos (save-excursion
530 (funcall func 1)
531 (while (and (> arg 0) (/= (point) limit))
532 (if (get-text-property (point) property)
533 (setq arg (1- arg)))
534 (if (> arg 0)
535 (funcall func 1)))
536 (point))))
26b4dc84
GM
537 (goto-char pos)
538 (if (and (eq func 'forward-char)
539 (= (1+ pos) limit))
540 (forward-char 1))))
541
542(eshell-deftest arg forward-arg
543 "Move across command arguments"
544 (eshell-insert-command "echo $(+ 1 (- 4 3)) \"alpha beta\" file" 'ignore)
545 (let ((here (point)) begin valid)
546 (eshell-bol)
547 (setq begin (point))
548 (eshell-forward-argument 4)
549 (setq valid (= here (point)))
550 (eshell-backward-argument 4)
551 (prog1
552 (and valid (= begin (point)))
553 (eshell-bol)
554 (delete-region (point) (point-max)))))
555
556(defun eshell-forward-argument (&optional arg)
557 "Move forward ARG arguments."
558 (interactive "p")
559 (eshell-move-argument (point-max) 'forward-char 'arg-end arg))
560
561(defun eshell-backward-argument (&optional arg)
562 "Move backward ARG arguments."
563 (interactive "p")
564 (eshell-move-argument (point-min) 'backward-char 'arg-begin arg))
565
b4bd214e
JW
566(defun eshell-repeat-argument (&optional arg)
567 (interactive "p")
568 (let ((begin (save-excursion
569 (eshell-backward-argument arg)
570 (point))))
571 (kill-ring-save begin (point))
572 (yank)))
573
26b4dc84
GM
574(defun eshell-bol ()
575 "Goes to the beginning of line, then skips past the prompt, if any."
576 (interactive)
577 (beginning-of-line)
578 (and eshell-skip-prompt-function
579 (funcall eshell-skip-prompt-function)))
580
581(defsubst eshell-push-command-mark ()
582 "Push a mark at the end of the last input text."
583 (push-mark (1- eshell-last-input-end) t))
584
585(custom-add-option 'eshell-pre-command-hook 'eshell-push-command-mark)
586
587(defsubst eshell-goto-input-start ()
588 "Goto the start of the last command input.
589Putting this function on `eshell-pre-command-hook' will mimic Plan 9's
5909term behavior."
591 (goto-char eshell-last-input-start))
592
593(custom-add-option 'eshell-pre-command-hook 'eshell-push-command-mark)
594
595(defsubst eshell-interactive-print (string)
596 "Print STRING to the eshell display buffer."
597 (eshell-output-filter nil string))
598
599(defsubst eshell-begin-on-new-line ()
600 "This function outputs a newline if not at beginning of line."
601 (save-excursion
602 (goto-char eshell-last-output-end)
603 (or (bolp)
604 (eshell-interactive-print "\n"))))
605
606(defsubst eshell-reset (&optional no-hooks)
607 "Output a prompt on a new line, aborting any current input.
608If NO-HOOKS is non-nil, then `eshell-post-command-hook' won't be run."
609 (goto-char (point-max))
610 (setq eshell-last-input-start (point-marker)
611 eshell-last-input-end (point-marker)
612 eshell-last-output-start (point-marker)
613 eshell-last-output-block-begin (point)
614 eshell-last-output-end (point-marker))
615 (eshell-begin-on-new-line)
616 (unless no-hooks
617 (run-hooks 'eshell-post-command-hook)
618 (goto-char (point-max))))
619
620(defun eshell-parse-command-input (beg end &optional args)
621 "Parse the command input from BEG to END.
622The difference is that `eshell-parse-command' expects a complete
623command string (and will error if it doesn't get one), whereas this
624function will inform the caller whether more input is required.
625
626If nil is returned, more input is necessary (probably because a
627multi-line input string wasn't terminated properly). Otherwise, it
628will return the parsed command."
b4bd214e
JW
629 (let (delim command)
630 (if (setq delim
631 (catch 'eshell-incomplete
632 (ignore
633 (setq command (eshell-parse-command (cons beg end)
634 args t)))))
635 (ignore
636 (message "Expecting completion of delimeter %c ..."
637 (if (listp delim)
638 (car delim)
639 delim)))
26b4dc84
GM
640 command)))
641
642(defun eshell-update-markers (pmark)
643 "Update the input and output markers relative to point and PMARK."
644 (set-marker eshell-last-input-start pmark)
645 (set-marker eshell-last-input-end (point))
646 (set-marker eshell-last-output-end (point)))
647
648(defun eshell-queue-input (&optional use-region)
649 "Queue the current input text for execution by Eshell.
650Particularly, don't send the text to the current process, even if it's
651waiting for input."
652 (interactive "P")
653 (eshell-send-input use-region t))
654
655(eshell-deftest mode queue-input
656 "Queue command input"
657 (eshell-insert-command "sleep 2")
658 (eshell-insert-command "echo alpha" 'eshell-queue-input)
659 (let ((count 10))
660 (while (and eshell-current-command
661 (> count 0))
662 (sit-for 1 0)
663 (setq count (1- count))))
664 (eshell-match-result "alpha\n"))
665
666(defun eshell-send-input (&optional use-region queue-p no-newline)
e94dae7f 667 "Send the input received to Eshell for parsing and processing.
26b4dc84
GM
668After `eshell-last-output-end', sends all text from that marker to
669point as input. Before that marker, calls `eshell-get-old-input' to
670retrieve old input, copies it to the end of the buffer, and sends it.
671
672If USE-REGION is non-nil, the current region (between point and mark)
673will be used as input.
674
675If QUEUE-P is non-nil, input will be queued until the next prompt,
676rather than sent to the currently active process. If no process, the
677input is processed immediately.
678
679If NO-NEWLINE is non-nil, the input is sent without an implied final
680newline."
681 (interactive "P")
682 ;; Note that the input string does not include its terminal newline.
683 (let ((proc-running-p (and (eshell-interactive-process)
684 (not queue-p)))
685 (inhibit-point-motion-hooks t)
686 after-change-functions)
687 (unless (and proc-running-p
688 (not (eq (process-status
689 (eshell-interactive-process)) 'run)))
690 (if (or proc-running-p
691 (>= (point) eshell-last-output-end))
692 (goto-char (point-max))
693 (let ((copy (eshell-get-old-input use-region)))
694 (goto-char eshell-last-output-end)
695 (insert-and-inherit copy)))
79cf8e80
JW
696 (unless (or no-newline
697 (and eshell-send-direct-to-subprocesses
698 proc-running-p))
26b4dc84
GM
699 (insert-before-markers-and-inherit ?\n))
700 (if proc-running-p
701 (progn
702 (eshell-update-markers eshell-last-output-end)
79cf8e80
JW
703 (if (or eshell-send-direct-to-subprocesses
704 (= eshell-last-input-start eshell-last-input-end))
26b4dc84
GM
705 (unless no-newline
706 (process-send-string (eshell-interactive-process) "\n"))
707 (process-send-region (eshell-interactive-process)
708 eshell-last-input-start
709 eshell-last-input-end)))
710 (if (= eshell-last-output-end (point))
711 (run-hooks 'eshell-post-command-hook)
712 (let (input)
713 (eshell-condition-case err
714 (progn
715 (setq input (buffer-substring-no-properties
716 eshell-last-output-end (1- (point))))
717 (run-hook-with-args 'eshell-expand-input-functions
718 eshell-last-output-end (1- (point)))
719 (let ((cmd (eshell-parse-command-input
720 eshell-last-output-end (1- (point)))))
721 (when cmd
722 (eshell-update-markers eshell-last-output-end)
723 (setq input (buffer-substring-no-properties
724 eshell-last-input-start
725 (1- eshell-last-input-end)))
726 (run-hooks 'eshell-input-filter-functions)
727 (and (catch 'eshell-terminal
728 (ignore
dace60cf
JW
729 (if (eshell-invoke-directly cmd input)
730 (eval cmd)
731 (eshell-eval-command cmd input))))
26b4dc84
GM
732 (eshell-life-is-too-much)))))
733 (quit
734 (eshell-reset t)
735 (run-hooks 'eshell-post-command-hook)
736 (signal 'quit nil))
737 (error
738 (eshell-reset t)
739 (eshell-interactive-print
740 (concat (error-message-string err) "\n"))
741 (run-hooks 'eshell-post-command-hook)
742 (insert-and-inherit input)))))))))
743
0c00c5ac
JW
744; (eshell-deftest proc send-to-subprocess
745; "Send input to a subprocess"
746; ;; jww (1999-12-06): what about when bc is unavailable?
747; (if (not (eshell-search-path "bc"))
748; t
749; (eshell-insert-command "bc")
750; (eshell-insert-command "1 + 2")
751; (sit-for 1 0)
752; (forward-line -1)
753; (prog1
754; (looking-at "3\n")
755; (eshell-insert-command "quit")
756; (sit-for 1 0))))
26b4dc84
GM
757
758(defsubst eshell-kill-new ()
759 "Add the last input text to the kill ring."
760 (kill-ring-save eshell-last-input-start eshell-last-input-end))
761
762(custom-add-option 'eshell-input-filter-functions 'eshell-kill-new)
763
764(defun eshell-output-filter (process string)
765 "Send the output from PROCESS (STRING) to the interactive display.
766This is done after all necessary filtering has been done."
767 (let ((oprocbuf (if process (process-buffer process)
768 (current-buffer)))
769 (inhibit-point-motion-hooks t)
770 after-change-functions)
771 (let ((functions eshell-preoutput-filter-functions))
772 (while (and functions string)
773 (setq string (funcall (car functions) string))
774 (setq functions (cdr functions))))
775 (if (and string oprocbuf (buffer-name oprocbuf))
776 (let ((obuf (current-buffer))
777 opoint obeg oend)
778 (set-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)
807 (set-buffer obuf)))))
808
809(defun eshell-run-output-filters ()
810 "Run the `eshell-output-filter-functions' on the current output."
811 (save-current-buffer
812 (run-hooks 'eshell-output-filter-functions))
813 (setq eshell-last-output-block-begin
814 (marker-position eshell-last-output-end)))
815
816;;; jww (1999-10-23): this needs testing
817(defun eshell-preinput-scroll-to-bottom ()
818 "Go to the end of buffer in all windows showing it.
819Movement occurs if point in the selected window is not after the
820process mark, and `this-command' is an insertion command. Insertion
b7599623 821commands recognized are `self-insert-command', `yank', and
26b4dc84
GM
822`hilit-yank'. Depends on the value of
823`eshell-scroll-to-bottom-on-input'.
824
825This function should be a pre-command hook."
826 (if (memq this-command '(self-insert-command yank hilit-yank))
827 (let* ((selected (selected-window))
828 (current (current-buffer))
829 (scroll eshell-scroll-to-bottom-on-input))
830 (if (< (point) eshell-last-output-end)
831 (if (eq scroll 'this)
832 (goto-char (point-max))
833 (walk-windows
834 (function
835 (lambda (window)
836 (when (and (eq (window-buffer window) current)
837 (or (eq scroll t) (eq scroll 'all)))
838 (select-window window)
839 (goto-char (point-max))
840 (select-window selected))))
841 nil t))))))
842
843;;; jww (1999-10-23): this needs testing
844(defun eshell-postoutput-scroll-to-bottom ()
845 "Go to the end of buffer in all windows showing it.
846Does not scroll if the current line is the last line in the buffer.
847Depends on the value of `eshell-scroll-to-bottom-on-output' and
848`eshell-scroll-show-maximum-output'.
849
850This function should be in the list `eshell-output-filter-functions'."
851 (let* ((selected (selected-window))
852 (current (current-buffer))
853 (scroll eshell-scroll-to-bottom-on-output))
854 (unwind-protect
855 (walk-windows
856 (function
857 (lambda (window)
858 (if (eq (window-buffer window) current)
859 (progn
860 (select-window window)
861 (if (and (< (point) eshell-last-output-end)
862 (or (eq scroll t) (eq scroll 'all)
863 ;; Maybe user wants point to jump to end.
864 (and (eq scroll 'this)
865 (eq selected window))
866 (and (eq scroll 'others)
867 (not (eq selected window)))
868 ;; If point was at the end, keep it at end.
869 (>= (point) eshell-last-output-start)))
870 (goto-char eshell-last-output-end))
871 ;; Optionally scroll so that the text
872 ;; ends at the bottom of the window.
873 (if (and eshell-scroll-show-maximum-output
874 (>= (point) eshell-last-output-end))
875 (save-excursion
876 (goto-char (point-max))
877 (recenter -1)))
878 (select-window selected)))))
879 nil t)
880 (set-buffer current))))
881
882(custom-add-option 'eshell-output-filter-functions
883 'eshell-postoutput-scroll-to-bottom)
884
885(defun eshell-beginning-of-input ()
886 "Return the location of the start of the previous input."
887 eshell-last-input-start)
888
889(defun eshell-beginning-of-output ()
890 "Return the location of the end of the previous output block."
891 eshell-last-input-end)
892
893(defun eshell-end-of-output ()
894 "Return the location of the end of the previous output block."
895 (if (eshell-using-module 'eshell-prompt)
896 eshell-last-output-start
897 eshell-last-output-end))
898
899(defun eshell-kill-output ()
900 "Kill all output from interpreter since last input.
901Does not delete the prompt."
902 (interactive)
903 (save-excursion
904 (goto-char (eshell-beginning-of-output))
905 (insert "*** output flushed ***\n")
906 (delete-region (point) (eshell-end-of-output))))
907
908(eshell-deftest io flush-output
909 "Flush previous output"
910 (eshell-insert-command "echo alpha")
911 (eshell-kill-output)
912 (and (eshell-match-result (regexp-quote "*** output flushed ***\n"))
913 (forward-line)
914 (= (point) eshell-last-output-start)))
915
916(defun eshell-show-output (&optional arg)
917 "Display start of this batch of interpreter output at top of window.
918Sets mark to the value of point when this command is run.
919With a prefix argument, narrows region to last command output."
920 (interactive "P")
921 (goto-char (eshell-beginning-of-output))
922 (set-window-start (selected-window)
923 (save-excursion
924 (goto-char (eshell-beginning-of-input))
925 (line-beginning-position)))
926 (if arg
927 (narrow-to-region (eshell-beginning-of-output)
928 (eshell-end-of-output)))
929 (eshell-end-of-output))
930
931(defun eshell-mark-output (&optional arg)
932 "Display start of this batch of interpreter output at top of window.
933Sets mark to the value of point when this command is run.
934With a prefix argument, narrows region to last command output."
935 (interactive "P")
936 (push-mark (eshell-show-output arg)))
937
938(defun eshell-kill-input ()
939 "Kill all text from last stuff output by interpreter to point."
940 (interactive)
941 (if (> (point) eshell-last-output-end)
942 (kill-region eshell-last-output-end (point))
943 (let ((here (point)))
944 (eshell-bol)
945 (kill-region (point) here))))
946
3e80ba3c
RS
947(defun eshell-show-maximum-output (&optional interactive)
948 "Put the end of the buffer at the bottom of the window.
949When run interactively, widen the buffer first."
950 (interactive "p")
951 (if interactive
26b4dc84
GM
952 (widen))
953 (goto-char (point-max))
954 (recenter -1))
955
956(defun eshell-get-old-input (&optional use-current-region)
957 "Return the command input on the current line."
958 (if use-current-region
959 (buffer-substring (min (point) (mark))
960 (max (point) (mark)))
961 (save-excursion
962 (beginning-of-line)
963 (and eshell-skip-prompt-function
964 (funcall eshell-skip-prompt-function))
965 (let ((beg (point)))
966 (end-of-line)
967 (buffer-substring beg (point))))))
968
969(defun eshell-copy-old-input ()
970 "Insert after prompt old input at point as new input to be edited."
971 (interactive)
972 (let ((input (eshell-get-old-input)))
973 (goto-char eshell-last-output-end)
974 (insert-and-inherit input)))
975
976(eshell-deftest mode run-old-command
977 "Re-run an old command"
978 (eshell-insert-command "echo alpha")
979 (goto-char eshell-last-input-start)
980 (string= (eshell-get-old-input) "echo alpha"))
981
982(defun eshell/exit ()
983 "Leave or kill the Eshell buffer, depending on `eshell-kill-on-exit'."
984 (throw 'eshell-terminal t))
985
986(defun eshell-life-is-too-much ()
987 "Kill the current buffer (or bury it). Good-bye Eshell."
988 (interactive)
989 (if (not eshell-kill-on-exit)
990 (bury-buffer)
991 (kill-buffer (current-buffer))))
992
993(defun eshell-truncate-buffer ()
994 "Truncate the buffer to `eshell-buffer-maximum-lines'.
995This function could be on `eshell-output-filter-functions' or bound to
996a key."
997 (interactive)
998 (save-excursion
999 (goto-char eshell-last-output-end)
1000 (let ((lines (count-lines 1 (point)))
1001 (inhibit-read-only t))
1002 (forward-line (- eshell-buffer-maximum-lines))
1003 (beginning-of-line)
1004 (let ((pos (point)))
1005 (if (bobp)
1006 (if (interactive-p)
3e80ba3c 1007 (message "Buffer too short to truncate"))
26b4dc84
GM
1008 (delete-region (point-min) (point))
1009 (if (interactive-p)
1010 (message "Truncated buffer from %d to %d lines (%.1fk freed)"
1011 lines eshell-buffer-maximum-lines
1012 (/ pos 1024.0))))))))
1013
1014(custom-add-option 'eshell-output-filter-functions
1015 'eshell-truncate-buffer)
1016
a07c7ade 1017(defun eshell-send-invisible (str)
26b4dc84
GM
1018 "Read a string without echoing.
1019Then send it to the process running in the current buffer."
1020 (interactive "P") ; Defeat snooping via C-x ESC ESC
1021 (let ((str (read-passwd
db22de71 1022 (format "%s Password: "
26b4dc84
GM
1023 (process-name (eshell-interactive-process))))))
1024 (if (stringp str)
1025 (process-send-string (eshell-interactive-process)
1026 (concat str "\n"))
1027 (message "Warning: text will be echoed"))))
1028
1029(defun eshell-watch-for-password-prompt ()
1030 "Prompt in the minibuffer for password and send without echoing.
a07c7ade 1031This function uses `eshell-send-invisible' to read and send a password to the
26b4dc84
GM
1032buffer's process if STRING contains a password prompt defined by
1033`eshell-password-prompt-regexp'.
1034
1035This function could be in the list `eshell-output-filter-functions'."
1036 (when (eshell-interactive-process)
1037 (save-excursion
1038 (goto-char eshell-last-output-block-begin)
1039 (beginning-of-line)
1040 (if (re-search-forward eshell-password-prompt-regexp
1041 eshell-last-output-end t)
a07c7ade 1042 (eshell-send-invisible nil)))))
26b4dc84
GM
1043
1044(custom-add-option 'eshell-output-filter-functions
1045 'eshell-watch-for-password-prompt)
1046
1047(defun eshell-handle-control-codes ()
1048 "Act properly when certain control codes are seen."
1049 (save-excursion
1050 (let ((orig (point)))
1051 (goto-char eshell-last-output-block-begin)
1052 (unless (eolp)
1053 (beginning-of-line))
1054 (while (< (point) eshell-last-output-end)
1055 (let ((char (char-after)))
1056 (cond
1057 ((eq char ?\r)
1058 (if (< (1+ (point)) eshell-last-output-end)
1059 (if (memq (char-after (1+ (point)))
1060 '(?\n ?\r))
1061 (delete-char 1)
1062 (let ((end (1+ (point))))
1063 (beginning-of-line)
1064 (delete-region (point) end)))
1065 (add-text-properties (point) (1+ (point))
1066 '(invisible t))
1067 (forward-char)))
1068 ((eq char ?\a)
1069 (delete-char 1)
1070 (beep))
1071 ((eq char ?\C-h)
1072 (delete-backward-char 1)
1073 (delete-char 1))
1074 (t
1075 (forward-char))))))))
1076
1077(custom-add-option 'eshell-output-filter-functions
1078 'eshell-handle-control-codes)
1079
1080;;; Code:
1081
ab5796a9 1082;;; arch-tag: ec65bc2b-da14-4547-81d3-a32af3a4dc57
26b4dc84 1083;;; esh-mode.el ends here