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