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