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