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