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