]> code.delx.au - gnu-emacs/blob - lisp/eshell/esh-mode.el
Dired recognize dirs when file size in human units
[gnu-emacs] / lisp / eshell / esh-mode.el
1 ;;; esh-mode.el --- user interface -*- lexical-binding:t -*-
2
3 ;; Copyright (C) 1999-2016 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 ;; This is to avoid making the paragraph base direction
384 ;; right-to-left if the first word just happens to start with a
385 ;; strong R2L character.
386 (setq bidi-paragraph-direction 'left-to-right)
387
388 ;; load extension modules into memory. This will cause any global
389 ;; variables they define to be visible, since some of the core
390 ;; modules sometimes take advantage of their functionality if used.
391 (dolist (module eshell-modules-list)
392 (let ((module-fullname (symbol-name module))
393 module-shortname)
394 (if (string-match "^eshell-\\(.*\\)" module-fullname)
395 (setq module-shortname
396 (concat "em-" (match-string 1 module-fullname))))
397 (unless module-shortname
398 (error "Invalid Eshell module name: %s" module-fullname))
399 (unless (featurep (intern module-shortname))
400 (load module-shortname))))
401
402 (unless (file-exists-p eshell-directory-name)
403 (eshell-make-private-directory eshell-directory-name t))
404
405 ;; Load core Eshell modules, then extension modules, for this session.
406 (dolist (module (append (eshell-subgroups 'eshell) eshell-modules-list))
407 (let ((load-hook (intern-soft (format "%s-load-hook" module)))
408 (initfunc (intern-soft (format "%s-initialize" module))))
409 (when (and load-hook (boundp load-hook))
410 (if (memq initfunc (symbol-value load-hook)) (setq initfunc nil))
411 (run-hooks load-hook))
412 ;; So we don't need the -initialize functions on the hooks (b#5375).
413 (and initfunc (fboundp initfunc) (funcall initfunc))))
414
415 (if eshell-send-direct-to-subprocesses
416 (add-hook 'pre-command-hook 'eshell-intercept-commands t t))
417
418 (if eshell-scroll-to-bottom-on-input
419 (add-hook 'pre-command-hook 'eshell-preinput-scroll-to-bottom t t))
420
421 (when eshell-scroll-show-maximum-output
422 (set (make-local-variable 'scroll-conservatively) 1000))
423
424 (when eshell-status-in-mode-line
425 (add-hook 'eshell-pre-command-hook 'eshell-command-started nil t)
426 (add-hook 'eshell-post-command-hook 'eshell-command-finished nil t))
427
428 (add-hook 'kill-buffer-hook 'eshell-kill-buffer-function t t)
429
430 (if eshell-first-time-p
431 (run-hooks 'eshell-first-time-mode-hook))
432 (run-hooks 'eshell-post-command-hook))
433
434 (put 'eshell-mode 'mode-class 'special)
435
436 (defun eshell-command-started ()
437 "Indicate in the mode line that a command has started."
438 (setq eshell-command-running-string "**")
439 (force-mode-line-update))
440
441 (defun eshell-command-finished ()
442 "Indicate in the mode line that a command has finished."
443 (setq eshell-command-running-string "--")
444 (force-mode-line-update))
445
446 ;;; Internal Functions:
447
448 (defun eshell-toggle-direct-send ()
449 (interactive)
450 (if eshell-send-direct-to-subprocesses
451 (progn
452 (setq eshell-send-direct-to-subprocesses nil)
453 (remove-hook 'pre-command-hook 'eshell-intercept-commands t)
454 (message "Sending subprocess input on RET"))
455 (setq eshell-send-direct-to-subprocesses t)
456 (add-hook 'pre-command-hook 'eshell-intercept-commands t t)
457 (message "Sending subprocess input directly")))
458
459 (defun eshell-self-insert-command ()
460 (interactive)
461 (process-send-string
462 (eshell-interactive-process)
463 (char-to-string (if (symbolp last-command-event)
464 (get last-command-event 'ascii-character)
465 last-command-event))))
466
467 (defun eshell-intercept-commands ()
468 (when (and (eshell-interactive-process)
469 (not (and (integerp last-input-event)
470 (memq last-input-event '(?\C-x ?\C-c)))))
471 (let ((possible-events (where-is-internal this-command))
472 (name (symbol-name this-command))
473 (intercept t))
474 ;; Assume that any multikey combination which does NOT target an
475 ;; Eshell command, is a combo the user wants invoked rather than
476 ;; sent to the underlying subprocess.
477 (unless (and (> (length name) 7)
478 (equal (substring name 0 7) "eshell-"))
479 (while possible-events
480 (if (> (length (car possible-events)) 1)
481 (setq intercept nil possible-events nil)
482 (setq possible-events (cdr possible-events)))))
483 (if intercept
484 (setq this-command 'eshell-self-insert-command)))))
485
486 (declare-function find-tag-interactive "etags" (prompt &optional no-default))
487
488 (defun eshell-find-tag (&optional tagname next-p regexp-p)
489 "A special version of `find-tag' that ignores whether the text is read-only."
490 (interactive)
491 (require 'etags)
492 (let ((inhibit-read-only t)
493 (no-default (eobp))
494 (find-tag-default-function 'ignore))
495 (setq tagname (car (find-tag-interactive "Find tag: " no-default)))
496 (find-tag tagname next-p regexp-p)))
497
498 (defun eshell-move-argument (limit func property arg)
499 "Move forward ARG arguments."
500 (catch 'eshell-incomplete
501 (eshell-parse-arguments (save-excursion (eshell-bol) (point))
502 (line-end-position)))
503 (let ((pos (save-excursion
504 (funcall func 1)
505 (while (and (> arg 0) (/= (point) limit))
506 (if (get-text-property (point) property)
507 (setq arg (1- arg)))
508 (if (> arg 0)
509 (funcall func 1)))
510 (point))))
511 (goto-char pos)
512 (if (and (eq func 'forward-char)
513 (= (1+ pos) limit))
514 (forward-char 1))))
515
516 (defun eshell-forward-argument (&optional arg)
517 "Move forward ARG arguments."
518 (interactive "p")
519 (eshell-move-argument (point-max) 'forward-char 'arg-end arg))
520
521 (defun eshell-backward-argument (&optional arg)
522 "Move backward ARG arguments."
523 (interactive "p")
524 (eshell-move-argument (point-min) 'backward-char 'arg-begin arg))
525
526 (defun eshell-repeat-argument (&optional arg)
527 (interactive "p")
528 (let ((begin (save-excursion
529 (eshell-backward-argument arg)
530 (point))))
531 (kill-ring-save begin (point))
532 (yank)))
533
534 (defun eshell-bol ()
535 "Goes to the beginning of line, then skips past the prompt, if any."
536 (interactive)
537 (beginning-of-line)
538 (and eshell-skip-prompt-function
539 (funcall eshell-skip-prompt-function)))
540
541 (defsubst eshell-push-command-mark ()
542 "Push a mark at the end of the last input text."
543 (push-mark (1- eshell-last-input-end) t))
544
545 (custom-add-option 'eshell-pre-command-hook 'eshell-push-command-mark)
546
547 (defsubst eshell-goto-input-start ()
548 "Goto the start of the last command input.
549 Putting this function on `eshell-pre-command-hook' will mimic Plan 9's
550 9term behavior."
551 (goto-char eshell-last-input-start))
552
553 (custom-add-option 'eshell-pre-command-hook 'eshell-push-command-mark)
554
555 (defsubst eshell-interactive-print (string)
556 "Print STRING to the eshell display buffer."
557 (eshell-output-filter nil string))
558
559 (defsubst eshell-begin-on-new-line ()
560 "This function outputs a newline if not at beginning of line."
561 (save-excursion
562 (goto-char eshell-last-output-end)
563 (or (bolp)
564 (eshell-interactive-print "\n"))))
565
566 (defsubst eshell-reset (&optional no-hooks)
567 "Output a prompt on a new line, aborting any current input.
568 If NO-HOOKS is non-nil, then `eshell-post-command-hook' won't be run."
569 (goto-char (point-max))
570 (setq eshell-last-input-start (point-marker)
571 eshell-last-input-end (point-marker)
572 eshell-last-output-start (point-marker)
573 eshell-last-output-block-begin (point)
574 eshell-last-output-end (point-marker))
575 (eshell-begin-on-new-line)
576 (unless no-hooks
577 (run-hooks 'eshell-post-command-hook)
578 (goto-char (point-max))))
579
580 (defun eshell-parse-command-input (beg end &optional args)
581 "Parse the command input from BEG to END.
582 The difference is that `eshell-parse-command' expects a complete
583 command string (and will error if it doesn't get one), whereas this
584 function will inform the caller whether more input is required.
585
586 If nil is returned, more input is necessary (probably because a
587 multi-line input string wasn't terminated properly). Otherwise, it
588 will return the parsed command."
589 (let (delim command)
590 (if (setq delim
591 (catch 'eshell-incomplete
592 (ignore
593 (setq command (eshell-parse-command (cons beg end)
594 args t)))))
595 (ignore
596 (message "Expecting completion of delimiter %c ..."
597 (if (listp delim)
598 (car delim)
599 delim)))
600 command)))
601
602 (defun eshell-update-markers (pmark)
603 "Update the input and output markers relative to point and PMARK."
604 (set-marker eshell-last-input-start pmark)
605 (set-marker eshell-last-input-end (point))
606 (set-marker eshell-last-output-end (point)))
607
608 (defun eshell-queue-input (&optional use-region)
609 "Queue the current input text for execution by Eshell.
610 Particularly, don't send the text to the current process, even if it's
611 waiting for input."
612 (interactive "P")
613 (eshell-send-input use-region t))
614
615 (defun eshell-send-input (&optional use-region queue-p no-newline)
616 "Send the input received to Eshell for parsing and processing.
617 After `eshell-last-output-end', sends all text from that marker to
618 point as input. Before that marker, calls `eshell-get-old-input' to
619 retrieve old input, copies it to the end of the buffer, and sends it.
620
621 If USE-REGION is non-nil, the current region (between point and mark)
622 will be used as input.
623
624 If QUEUE-P is non-nil, input will be queued until the next prompt,
625 rather than sent to the currently active process. If no process, the
626 input is processed immediately.
627
628 If NO-NEWLINE is non-nil, the input is sent without an implied final
629 newline."
630 (interactive "P")
631 ;; Note that the input string does not include its terminal newline.
632 (let ((proc-running-p (and (eshell-interactive-process)
633 (not queue-p)))
634 (inhibit-point-motion-hooks t)
635 (inhibit-modification-hooks t))
636 (unless (and proc-running-p
637 (not (eq (process-status
638 (eshell-interactive-process))
639 'run)))
640 (if (or proc-running-p
641 (>= (point) eshell-last-output-end))
642 (goto-char (point-max))
643 (let ((copy (eshell-get-old-input use-region)))
644 (goto-char eshell-last-output-end)
645 (insert-and-inherit copy)))
646 (unless (or no-newline
647 (and eshell-send-direct-to-subprocesses
648 proc-running-p))
649 (insert-before-markers-and-inherit ?\n))
650 (if proc-running-p
651 (progn
652 (eshell-update-markers eshell-last-output-end)
653 (if (or eshell-send-direct-to-subprocesses
654 (= eshell-last-input-start eshell-last-input-end))
655 (unless no-newline
656 (process-send-string (eshell-interactive-process) "\n"))
657 (process-send-region (eshell-interactive-process)
658 eshell-last-input-start
659 eshell-last-input-end)))
660 (if (= eshell-last-output-end (point))
661 (run-hooks 'eshell-post-command-hook)
662 (let (input)
663 (eshell-condition-case err
664 (progn
665 (setq input (buffer-substring-no-properties
666 eshell-last-output-end (1- (point))))
667 (run-hook-with-args 'eshell-expand-input-functions
668 eshell-last-output-end (1- (point)))
669 (let ((cmd (eshell-parse-command-input
670 eshell-last-output-end (1- (point)))))
671 (when cmd
672 (eshell-update-markers eshell-last-output-end)
673 (setq input (buffer-substring-no-properties
674 eshell-last-input-start
675 (1- eshell-last-input-end)))
676 (run-hooks 'eshell-input-filter-functions)
677 (and (catch 'eshell-terminal
678 (ignore
679 (if (eshell-invoke-directly cmd)
680 (eval cmd)
681 (eshell-eval-command cmd input))))
682 (eshell-life-is-too-much)))))
683 (quit
684 (eshell-reset t)
685 (run-hooks 'eshell-post-command-hook)
686 (signal 'quit nil))
687 (error
688 (eshell-reset t)
689 (eshell-interactive-print
690 (concat (error-message-string err) "\n"))
691 (run-hooks 'eshell-post-command-hook)
692 (insert-and-inherit input)))))))))
693
694 (defsubst eshell-kill-new ()
695 "Add the last input text to the kill ring."
696 (kill-ring-save eshell-last-input-start eshell-last-input-end))
697
698 (custom-add-option 'eshell-input-filter-functions 'eshell-kill-new)
699
700 (defun eshell-output-filter (process string)
701 "Send the output from PROCESS (STRING) to the interactive display.
702 This is done after all necessary filtering has been done."
703 (let ((oprocbuf (if process (process-buffer process)
704 (current-buffer)))
705 (inhibit-point-motion-hooks t)
706 (inhibit-modification-hooks t))
707 (let ((functions eshell-preoutput-filter-functions))
708 (while (and functions string)
709 (setq string (funcall (car functions) string))
710 (setq functions (cdr functions))))
711 (if (and string oprocbuf (buffer-name oprocbuf))
712 (let (opoint obeg oend)
713 (with-current-buffer oprocbuf
714 (setq opoint (point))
715 (setq obeg (point-min))
716 (setq oend (point-max))
717 (let ((buffer-read-only nil)
718 (nchars (length string))
719 (ostart nil))
720 (widen)
721 (goto-char eshell-last-output-end)
722 (setq ostart (point))
723 (if (<= (point) opoint)
724 (setq opoint (+ opoint nchars)))
725 (if (< (point) obeg)
726 (setq obeg (+ obeg nchars)))
727 (if (<= (point) oend)
728 (setq oend (+ oend nchars)))
729 (insert-before-markers string)
730 (if (= (window-start) (point))
731 (set-window-start (selected-window)
732 (- (point) nchars)))
733 (if (= (point) eshell-last-input-end)
734 (set-marker eshell-last-input-end
735 (- eshell-last-input-end nchars)))
736 (set-marker eshell-last-output-start ostart)
737 (set-marker eshell-last-output-end (point))
738 (force-mode-line-update))
739 (narrow-to-region obeg oend)
740 (goto-char opoint)
741 (eshell-run-output-filters))))))
742
743 (defun eshell-run-output-filters ()
744 "Run the `eshell-output-filter-functions' on the current output."
745 (save-current-buffer
746 (run-hooks 'eshell-output-filter-functions))
747 (setq eshell-last-output-block-begin
748 (marker-position eshell-last-output-end)))
749
750 ;;; jww (1999-10-23): this needs testing
751 (defun eshell-preinput-scroll-to-bottom ()
752 "Go to the end of buffer in all windows showing it.
753 Movement occurs if point in the selected window is not after the
754 process mark, and `this-command' is an insertion command. Insertion
755 commands recognized are `self-insert-command', `yank', and
756 `hilit-yank'. Depends on the value of
757 `eshell-scroll-to-bottom-on-input'.
758
759 This function should be a pre-command hook."
760 (if (memq this-command '(self-insert-command yank hilit-yank))
761 (let* ((selected (selected-window))
762 (current (current-buffer))
763 (scroll eshell-scroll-to-bottom-on-input))
764 (if (< (point) eshell-last-output-end)
765 (if (eq scroll 'this)
766 (goto-char (point-max))
767 (walk-windows
768 (function
769 (lambda (window)
770 (when (and (eq (window-buffer window) current)
771 (or (eq scroll t) (eq scroll 'all)))
772 (select-window window)
773 (goto-char (point-max))
774 (select-window selected))))
775 nil t))))))
776
777 ;;; jww (1999-10-23): this needs testing
778 (defun eshell-postoutput-scroll-to-bottom ()
779 "Go to the end of buffer in all windows showing it.
780 Does not scroll if the current line is the last line in the buffer.
781 Depends on the value of `eshell-scroll-to-bottom-on-output' and
782 `eshell-scroll-show-maximum-output'.
783
784 This function should be in the list `eshell-output-filter-functions'."
785 (let* ((selected (selected-window))
786 (current (current-buffer))
787 (scroll eshell-scroll-to-bottom-on-output))
788 (unwind-protect
789 (walk-windows
790 (function
791 (lambda (window)
792 (if (eq (window-buffer window) current)
793 (progn
794 (select-window window)
795 (if (and (< (point) eshell-last-output-end)
796 (or (eq scroll t) (eq scroll 'all)
797 ;; Maybe user wants point to jump to end.
798 (and (eq scroll 'this)
799 (eq selected window))
800 (and (eq scroll 'others)
801 (not (eq selected window)))
802 ;; If point was at the end, keep it at end.
803 (>= (point) eshell-last-output-start)))
804 (goto-char eshell-last-output-end))
805 ;; Optionally scroll so that the text
806 ;; ends at the bottom of the window.
807 (if (and eshell-scroll-show-maximum-output
808 (>= (point) eshell-last-output-end))
809 (save-excursion
810 (goto-char (point-max))
811 (recenter -1)))
812 (select-window selected)))))
813 nil t)
814 (set-buffer current))))
815
816 (defun eshell-beginning-of-input ()
817 "Return the location of the start of the previous input."
818 eshell-last-input-start)
819
820 (defun eshell-beginning-of-output ()
821 "Return the location of the end of the previous output block."
822 eshell-last-input-end)
823
824 (defun eshell-end-of-output ()
825 "Return the location of the end of the previous output block."
826 (if (eshell-using-module 'eshell-prompt)
827 eshell-last-output-start
828 eshell-last-output-end))
829
830 (defun eshell-kill-output ()
831 "Kill all output from interpreter since last input.
832 Does not delete the prompt."
833 (interactive)
834 (save-excursion
835 (goto-char (eshell-beginning-of-output))
836 (insert "*** output flushed ***\n")
837 (delete-region (point) (eshell-end-of-output))))
838
839 (defun eshell-show-output (&optional arg)
840 "Display start of this batch of interpreter output at top of window.
841 Sets mark to the value of point when this command is run.
842 With a prefix argument, narrows region to last command output."
843 (interactive "P")
844 (goto-char (eshell-beginning-of-output))
845 (set-window-start (selected-window)
846 (save-excursion
847 (goto-char (eshell-beginning-of-input))
848 (line-beginning-position)))
849 (if arg
850 (narrow-to-region (eshell-beginning-of-output)
851 (eshell-end-of-output)))
852 (eshell-end-of-output))
853
854 (defun eshell-mark-output (&optional arg)
855 "Display start of this batch of interpreter output at top of window.
856 Sets mark to the value of point when this command is run.
857 With a prefix argument, narrows region to last command output."
858 (interactive "P")
859 (push-mark (eshell-show-output arg)))
860
861 (defun eshell-kill-input ()
862 "Kill all text from last stuff output by interpreter to point."
863 (interactive)
864 (if (> (point) eshell-last-output-end)
865 (kill-region eshell-last-output-end (point))
866 (let ((here (point)))
867 (eshell-bol)
868 (kill-region (point) here))))
869
870 (defun eshell-show-maximum-output (&optional interactive)
871 "Put the end of the buffer at the bottom of the window.
872 When run interactively, widen the buffer first."
873 (interactive "p")
874 (if interactive
875 (widen))
876 (goto-char (point-max))
877 (recenter -1))
878
879 (defun eshell/clear (&optional scrollback)
880 "Scroll contents of eshell window out of sight, leaving a blank window.
881 If SCROLLBACK is non-nil, clear the scrollback contents."
882 (interactive)
883 (if scrollback
884 (eshell/clear-scrollback)
885 (insert (make-string (window-size) ?\n))
886 (eshell-send-input)))
887
888 (defun eshell/clear-scrollback ()
889 "Clear the scrollback content of the eshell window."
890 (let ((inhibit-read-only t))
891 (erase-buffer)))
892
893 (defun eshell-get-old-input (&optional use-current-region)
894 "Return the command input on the current line."
895 (if use-current-region
896 (buffer-substring (min (point) (mark))
897 (max (point) (mark)))
898 (save-excursion
899 (beginning-of-line)
900 (and eshell-skip-prompt-function
901 (funcall eshell-skip-prompt-function))
902 (let ((beg (point)))
903 (end-of-line)
904 (buffer-substring beg (point))))))
905
906 (defun eshell-copy-old-input ()
907 "Insert after prompt old input at point as new input to be edited."
908 (interactive)
909 (let ((input (eshell-get-old-input)))
910 (goto-char eshell-last-output-end)
911 (insert-and-inherit input)))
912
913 (defun eshell/exit ()
914 "Leave or kill the Eshell buffer, depending on `eshell-kill-on-exit'."
915 (throw 'eshell-terminal t))
916
917 (defun eshell-life-is-too-much ()
918 "Kill the current buffer (or bury it). Good-bye Eshell."
919 (interactive)
920 (if (not eshell-kill-on-exit)
921 (bury-buffer)
922 (kill-buffer (current-buffer))))
923
924 (defun eshell-truncate-buffer ()
925 "Truncate the buffer to `eshell-buffer-maximum-lines'.
926 This function could be on `eshell-output-filter-functions' or bound to
927 a key."
928 (interactive)
929 (save-excursion
930 (goto-char eshell-last-output-end)
931 (let ((lines (count-lines 1 (point)))
932 (inhibit-read-only t))
933 (forward-line (- eshell-buffer-maximum-lines))
934 (beginning-of-line)
935 (let ((pos (point)))
936 (if (bobp)
937 (if (called-interactively-p 'interactive)
938 (message "Buffer too short to truncate"))
939 (delete-region (point-min) (point))
940 (if (called-interactively-p 'interactive)
941 (message "Truncated buffer from %d to %d lines (%.1fk freed)"
942 lines eshell-buffer-maximum-lines
943 (/ pos 1024.0))))))))
944
945 (custom-add-option 'eshell-output-filter-functions
946 'eshell-truncate-buffer)
947
948 (defun eshell-send-invisible ()
949 "Read a string without echoing.
950 Then send it to the process running in the current buffer."
951 (interactive) ; Don't pass str as argument, to avoid snooping via C-x ESC ESC
952 (let ((str (read-passwd
953 (format "%s Password: "
954 (process-name (eshell-interactive-process))))))
955 (if (stringp str)
956 (process-send-string (eshell-interactive-process)
957 (concat str "\n"))
958 (message "Warning: text will be echoed"))))
959
960 (defun eshell-watch-for-password-prompt ()
961 "Prompt in the minibuffer for password and send without echoing.
962 This function uses `eshell-send-invisible' to read and send a password to the
963 buffer's process if STRING contains a password prompt defined by
964 `eshell-password-prompt-regexp'.
965
966 This function could be in the list `eshell-output-filter-functions'."
967 (when (eshell-interactive-process)
968 (save-excursion
969 (let ((case-fold-search t))
970 (goto-char eshell-last-output-block-begin)
971 (beginning-of-line)
972 (if (re-search-forward eshell-password-prompt-regexp
973 eshell-last-output-end t)
974 (eshell-send-invisible))))))
975
976 (custom-add-option 'eshell-output-filter-functions
977 'eshell-watch-for-password-prompt)
978
979 (defun eshell-handle-control-codes ()
980 "Act properly when certain control codes are seen."
981 (save-excursion
982 (goto-char eshell-last-output-block-begin)
983 (unless (eolp)
984 (beginning-of-line))
985 (while (< (point) eshell-last-output-end)
986 (let ((char (char-after)))
987 (cond
988 ((eq char ?\r)
989 (if (< (1+ (point)) eshell-last-output-end)
990 (if (memq (char-after (1+ (point)))
991 '(?\n ?\r))
992 (delete-char 1)
993 (let ((end (1+ (point))))
994 (beginning-of-line)
995 (delete-region (point) end)))
996 (add-text-properties (point) (1+ (point))
997 '(invisible t))
998 (forward-char)))
999 ((eq char ?\a)
1000 (delete-char 1)
1001 (beep))
1002 ((eq char ?\C-h)
1003 (delete-region (1- (point)) (1+ (point))))
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