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