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