1 ;;; esh-mode.el --- user interface
3 ;; Copyright (C) 1999, 2000 Free Software Foundation
5 ;; Author: John Wiegley <johnw@gnu.org>
7 ;; This file is part of GNU Emacs.
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 2, or (at your option)
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.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING. If not, write to the
21 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 ;; Boston, MA 02111-1307, USA.
26 (eval-when-compile (require 'esh-maint
))
28 (defgroup eshell-mode nil
29 "This module contains code for handling input from the user."
35 ;; Basically, Eshell is used just like shell mode (<M-x shell>). The
36 ;; keystrokes for navigating the buffer, and accessing the command
37 ;; history, are identical. Unlike shell mode, however, Eshell mode's
38 ;; governing process is Emacs itself. With shell mode, an inferior
39 ;; shell process is executed that communicates with Emacs via comint
40 ;; -- a mode for handling sub-process interaction. Eshell mode, on
41 ;; the other hand, is a truly native Emacs shell. No subprocess are
42 ;; invoked except the ones requested by the user at the prompt.
44 ;; After entering a command, use <RET> to invoke it ([Command
45 ;; invocation]) . If there is a command on disk, it will be executed
46 ;; as in a normal shell. If there is no command by that name on disk,
47 ;; but a Lisp function with that name is defined, the Lisp function
48 ;; will be called, using the arguments passed on the command line.
50 ;; Some of the other features of the command interaction mode are:
52 ;; @ <M-RET> can be used to accumulate further commands while a
53 ;; command is currently running. Since all input is passed to the
54 ;; subprocess being executed, there is no automatic input queueing
55 ;; as there is with other shells.
57 ;; @ <C-c C-t> can be used to truncate the buffer if it grows too
60 ;; @ <C-c C-r> will move point to the beginning of the output of the
61 ;; last command. With a prefix argument, it will narrow to view
64 ;; @ <C-c C-o> will delete the output from the last command.
66 ;; @ <C-c C-f> will move forward a complete shell argument.
68 ;; @ <C-c C-b> will move backward a complete shell argument.
77 (defcustom eshell-mode-unload-hook nil
78 "*A hook that gets run when `eshell-mode' is unloaded."
82 (defcustom eshell-mode-hook nil
83 "*A hook that gets run when `eshell-mode' is entered."
87 (defcustom eshell-first-time-mode-hook nil
88 "*A hook that gets run the first time `eshell-mode' is entered.
89 That is to say, the first time during an Emacs session."
93 (defcustom eshell-exit-hook
'(eshell-query-kill-processes)
94 "*A hook that is run whenever `eshell' is exited.
95 This hook is only run if exiting actually kills the buffer."
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."
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'."
112 (defcustom eshell-send-direct-to-subprocesses nil
113 "*If t, send any input immediately to a subprocess."
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
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.
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
))
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.
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
))
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.
153 See variable `eshell-scroll-to-bottom-on-output' and function
154 `eshell-postoutput-scroll-to-bottom'."
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'."
166 (defcustom eshell-output-filter-functions
167 '(eshell-handle-control-codes
168 eshell-watch-for-password-prompt
)
169 "*Functions to call before output is displayed.
170 These functions are only called for output that is displayed
171 interactively, and not for output which is redirected."
175 (defcustom eshell-preoutput-filter-functions nil
176 "*Functions to call before output is inserted into the buffer.
177 These functions get one argument, a string containing the text to be
178 inserted. They return the string as it should be inserted."
182 (defcustom eshell-password-prompt-regexp
183 "[Pp]ass\\(word\\|phrase\\).*:\\s *\\'"
184 "*Regexp matching prompts for passwords in the inferior process.
185 This is used by `eshell-watch-for-password-prompt'."
189 (defcustom eshell-skip-prompt-function nil
190 "*A function called from beginning of line to skip the prompt."
191 :type
'(choice (const nil
) function
)
194 (defcustom eshell-status-in-modeline t
195 "*If non-nil, let the user know a command is running in the modeline."
199 (defvar eshell-first-time-p t
200 "A variable which is non-nil the first time Eshell is loaded.")
202 ;; Internal Variables:
204 ;; these are only set to `nil' initially for the sake of the
205 ;; byte-compiler, when compiling other files which `require' this one
206 (defvar eshell-mode nil
)
207 (defvar eshell-mode-map nil
)
208 (defvar eshell-command-running-string
"--")
209 (defvar eshell-command-map nil
)
210 (defvar eshell-command-prefix nil
)
211 (defvar eshell-last-input-start nil
)
212 (defvar eshell-last-input-end nil
)
213 (defvar eshell-last-output-start nil
)
214 (defvar eshell-last-output-block-begin nil
)
215 (defvar eshell-last-output-end nil
)
217 (defvar eshell-currently-handling-window nil
)
218 (defvar eshell-mode-syntax-table nil
)
219 (defvar eshell-mode-abbrev-table nil
)
221 (define-abbrev-table 'eshell-mode-abbrev-table
())
224 (unless (eshell-under-xemacs-p)
225 (defalias 'characterp
'ignore
)
226 (defalias 'char-int
'ignore
)))
228 (if (not eshell-mode-syntax-table
)
230 (setq eshell-mode-syntax-table
(make-syntax-table))
232 (modify-syntax-entry i
"_ " eshell-mode-syntax-table
)
236 (modify-syntax-entry i
"_ " eshell-mode-syntax-table
)
240 (modify-syntax-entry i
"_ " eshell-mode-syntax-table
)
244 (modify-syntax-entry i
"_ " eshell-mode-syntax-table
)
246 (modify-syntax-entry ?
" " eshell-mode-syntax-table
)
247 (modify-syntax-entry ?
\t " " eshell-mode-syntax-table
)
248 (modify-syntax-entry ?
\f " " eshell-mode-syntax-table
)
249 (modify-syntax-entry ?
\n "> " eshell-mode-syntax-table
)
250 ;; Give CR the same syntax as newline, for selective-display.
251 (modify-syntax-entry ?\^m
"> " eshell-mode-syntax-table
)
252 ;;; (modify-syntax-entry ?\; "< " eshell-mode-syntax-table)
253 (modify-syntax-entry ?
` "' " eshell-mode-syntax-table
)
254 (modify-syntax-entry ?
' "' " eshell-mode-syntax-table
)
255 (modify-syntax-entry ?
, "' " eshell-mode-syntax-table
)
256 ;; Used to be singlequote; changed for flonums.
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 (modify-syntax-entry ?\
} "){ " eshell-mode-syntax-table
)
267 (modify-syntax-entry ?\
[ "(] " eshell-mode-syntax-table
)
268 (modify-syntax-entry ?\
] ")[ " eshell-mode-syntax-table
)
269 ;; All non-word multibyte characters should be `symbol'.
270 (if (eshell-under-xemacs-p)
274 (and (characterp key
)
275 (>= (char-int key
) 256)
276 (/= (char-syntax key
) ?w
)
277 (modify-syntax-entry key
"_ "
278 eshell-mode-syntax-table
))))
279 (standard-syntax-table))
284 (/= (char-syntax key
) ?w
)
285 (modify-syntax-entry key
"_ "
286 eshell-mode-syntax-table
))))
287 (standard-syntax-table)))))
292 (defun eshell-mode ()
293 "Emacs shell interactive mode.
296 (kill-all-local-variables)
298 (setq major-mode
'eshell-mode
)
299 (setq mode-name
"EShell")
300 (set (make-local-variable 'eshell-mode
) t
)
302 (make-local-variable 'eshell-mode-map
)
303 (setq eshell-mode-map
(make-sparse-keymap))
304 (use-local-map eshell-mode-map
)
306 (when eshell-status-in-modeline
307 (make-local-variable 'eshell-command-running-string
)
308 (let ((fmt (eshell-copy-list mode-line-format
)))
309 (make-local-variable 'mode-line-format
)
310 (setq mode-line-format fmt
))
311 (let ((modeline (memq 'mode-line-modified mode-line-format
)))
313 (setcar modeline
'eshell-command-running-string
))))
315 (define-key eshell-mode-map
[return] 'eshell-send-input)
316 (define-key eshell-mode-map [(control ?m)] 'eshell-send-input)
317 (define-key eshell-mode-map [(control ?j)] 'eshell-send-input)
318 (define-key eshell-mode-map [(meta return)] 'eshell-queue-input)
319 (define-key eshell-mode-map [(meta control ?m)] 'eshell-queue-input)
320 (define-key eshell-mode-map [(meta control ?l)] 'eshell-show-output)
322 (set (make-local-variable 'eshell-command-prefix)
323 (make-symbol "eshell-command-prefix"))
324 (fset eshell-command-prefix (make-sparse-keymap))
325 (set (make-local-variable 'eshell-command-map)
326 (symbol-function eshell-command-prefix))
327 (define-key eshell-mode-map [(control ?c)] eshell-command-prefix)
329 ;; without this, find-tag complains about read-only text being
331 (if (eq (key-binding [(meta ?.)]) 'find-tag)
332 (define-key eshell-mode-map [(meta ?.)] 'eshell-find-tag))
333 (define-key eshell-command-map [(meta ?o)] 'eshell-mark-output)
334 (define-key eshell-command-map [(meta ?d)] 'eshell-toggle-direct-send)
336 (define-key eshell-command-map [(control ?a)] 'eshell-bol)
337 (define-key eshell-command-map [(control ?b)] 'eshell-backward-argument)
338 (define-key eshell-command-map [(control ?e)] 'eshell-show-maximum-output)
339 (define-key eshell-command-map [(control ?f)] 'eshell-forward-argument)
340 (define-key eshell-command-map [return] 'eshell-copy-old-input
)
341 (define-key eshell-command-map
[(control ?m
)] 'eshell-copy-old-input
)
342 (define-key eshell-command-map
[(control ?o
)] 'eshell-kill-output
)
343 (define-key eshell-command-map
[(control ?r
)] 'eshell-show-output
)
344 (define-key eshell-command-map
[(control ?t
)] 'eshell-truncate-buffer
)
345 (define-key eshell-command-map
[(control ?u
)] 'eshell-kill-input
)
346 (define-key eshell-command-map
[(control ?w
)] 'backward-kill-word
)
347 (define-key eshell-command-map
[(control ?y
)] 'eshell-repeat-argument
)
349 (setq local-abbrev-table eshell-mode-abbrev-table
)
350 (set-syntax-table eshell-mode-syntax-table
)
352 (set (make-local-variable 'dired-directory
) default-directory
)
353 (set (make-local-variable 'list-buffers-directory
)
354 (expand-file-name default-directory
))
356 ;; always set the tab width to 8 in Eshell buffers, since external
357 ;; commands which do their own formatting almost always expect this
358 (set (make-local-variable 'tab-width
) 8)
360 ;; don't ever use auto-fill in Eshell buffers
361 (setq auto-fill-function nil
)
363 ;; always display everything from a return value
364 (if (boundp 'print-length
)
365 (set (make-local-variable 'print-length
) nil
))
366 (if (boundp 'print-level
)
367 (set (make-local-variable 'print-level
) nil
))
369 ;; set require-final-newline to nil; otherwise, all redirected
370 ;; output will end with a newline, whether or not the source
372 (set (make-local-variable 'require-final-newline
) nil
)
374 (set (make-local-variable 'max-lisp-eval-depth
)
375 (max 3000 max-lisp-eval-depth
))
376 (set (make-local-variable 'max-specpdl-size
)
377 (max 6000 max-lisp-eval-depth
))
379 (set (make-local-variable 'eshell-last-input-start
) (point-marker))
380 (set (make-local-variable 'eshell-last-input-end
) (point-marker))
381 (set (make-local-variable 'eshell-last-output-start
) (point-marker))
382 (set (make-local-variable 'eshell-last-output-end
) (point-marker))
383 (set (make-local-variable 'eshell-last-output-block-begin
) (point))
385 (let ((modules-list (eshell-copy-list eshell-modules-list
)))
386 (make-local-variable 'eshell-modules-list
)
387 (setq eshell-modules-list modules-list
))
389 ;; load extension modules into memory. This will cause any global
390 ;; variables they define to be visible, since some of the core
391 ;; modules sometimes take advantage of their functionality if used.
392 (eshell-for module eshell-modules-list
393 (let ((module-fullname (symbol-name module
))
395 (if (string-match "^eshell-\\(.*\\)" module-fullname
)
396 (setq module-shortname
397 (concat "em-" (match-string 1 module-fullname
))))
398 (unless module-shortname
399 (error "Invalid Eshell module name: %s" module-fullname
))
400 (unless (featurep (intern module-shortname
))
401 (load module-shortname
))))
403 (unless (file-exists-p eshell-directory-name
)
404 (eshell-make-private-directory eshell-directory-name t
))
406 ;; load core Eshell modules for this session
407 (eshell-for module
(eshell-subgroups 'eshell
)
408 (run-hooks (intern-soft (concat (symbol-name module
)
411 ;; load extension modules for this session
412 (eshell-for module eshell-modules-list
413 (let ((load-hook (intern-soft (concat (symbol-name module
)
415 (if (and load-hook
(boundp load-hook
))
416 (run-hooks load-hook
))))
418 (make-local-hook 'pre-command-hook
)
420 (if eshell-send-direct-to-subprocesses
421 (add-hook 'pre-command-hook
'eshell-intercept-commands t t
))
423 (if eshell-scroll-to-bottom-on-input
424 (add-hook 'pre-command-hook
'eshell-preinput-scroll-to-bottom t t
))
426 (when eshell-scroll-show-maximum-output
427 (set (make-local-variable 'scroll-conservatively
) 1000))
429 (when eshell-status-in-modeline
430 (make-local-hook 'eshell-pre-command-hook
)
431 (add-hook 'eshell-pre-command-hook
'eshell-command-started nil t
)
432 (make-local-hook 'eshell-post-command-hook
)
433 (add-hook 'eshell-post-command-hook
'eshell-command-finished nil t
))
435 (make-local-hook 'kill-buffer-hook
)
436 (add-hook 'kill-buffer-hook
439 (run-hooks 'eshell-exit-hook
))) t t
)
441 (if eshell-first-time-p
442 (run-hooks 'eshell-first-time-mode-hook
))
443 (run-hooks 'eshell-mode-hook
)
444 (run-hooks 'eshell-post-command-hook
))
446 (put 'eshell-mode
'mode-class
'special
)
448 (eshell-deftest mode major-mode
449 "Major mode is correct"
450 (eq major-mode
'eshell-mode
))
452 (eshell-deftest mode eshell-mode-variable
453 "`eshell-mode' is true"
456 (eshell-deftest var window-height
457 "LINES equals window height"
458 (let ((eshell-stringify-t t
))
459 (eshell-command-result-p "= $LINES (window-height)" "t\n")))
461 (defun eshell-command-started ()
462 "Indicate in the modeline that a command has started."
463 (setq eshell-command-running-string
"**")
464 (force-mode-line-update))
466 (defun eshell-command-finished ()
467 "Indicate in the modeline that a command has finished."
468 (setq eshell-command-running-string
"--")
469 (force-mode-line-update))
471 (eshell-deftest mode command-running-p
472 "Modeline shows no command running"
473 (or (eshell-under-xemacs-p)
474 (not eshell-status-in-modeline
)
475 (and (memq 'eshell-command-running-string mode-line-format
)
476 (equal eshell-command-running-string
"--"))))
478 ;;; Internal Functions:
480 (defun eshell-toggle-direct-send ()
482 (if eshell-send-direct-to-subprocesses
484 (setq eshell-send-direct-to-subprocesses nil
)
485 (remove-hook 'pre-command-hook
'eshell-intercept-commands t
)
486 (message "Sending subprocess input on RET"))
487 (setq eshell-send-direct-to-subprocesses t
)
488 (add-hook 'pre-command-hook
'eshell-intercept-commands t t
)
489 (message "Sending subprocess input directly")))
491 (defun eshell-self-insert-command (N)
494 (eshell-interactive-process)
495 (char-to-string (if (symbolp last-command-char
)
496 (get last-command-char
'ascii-character
)
497 last-command-char
))))
499 (defun eshell-intercept-commands ()
500 (when (and (eshell-interactive-process)
501 (not (and (integerp last-input-event
)
502 (memq last-input-event
'(?\C-x ?\C-c
)))))
503 (let ((possible-events (where-is-internal this-command
))
504 (name (symbol-name this-command
))
506 ;; Assume that any multikey combination which does NOT target an
507 ;; Eshell command, is a combo the user wants invoked rather than
508 ;; sent to the underlying subprocess.
509 (unless (and (> (length name
) 7)
510 (equal (substring name
0 7) "eshell-"))
511 (while possible-events
512 (if (> (length (car possible-events
)) 1)
513 (setq intercept nil possible-events nil
)
514 (setq possible-events
(cdr possible-events
)))))
516 (setq this-command
'eshell-self-insert-command
)))))
518 (defun eshell-find-tag (&optional tagname next-p regexp-p
)
519 "A special version of `find-tag' that ignores read-onlyness."
522 (let ((inhibit-read-only t
)
524 (find-tag-default-function 'ignore
))
525 (setq tagname
(car (find-tag-interactive "Find tag: ")))
526 (find-tag tagname next-p regexp-p
)))
528 (defun eshell-move-argument (limit func property arg
)
529 "Move forward ARG arguments."
530 (catch 'eshell-incomplete
531 (eshell-parse-arguments (save-excursion (eshell-bol) (point))
532 (line-end-position)))
533 (let ((pos (save-excursion
535 (while (and (> arg
0) (/= (point) limit
))
536 (if (get-text-property (point) property
)
542 (if (and (eq func
'forward-char
)
546 (eshell-deftest arg forward-arg
547 "Move across command arguments"
548 (eshell-insert-command "echo $(+ 1 (- 4 3)) \"alpha beta\" file" 'ignore
)
549 (let ((here (point)) begin valid
)
552 (eshell-forward-argument 4)
553 (setq valid
(= here
(point)))
554 (eshell-backward-argument 4)
556 (and valid
(= begin
(point)))
558 (delete-region (point) (point-max)))))
560 (defun eshell-forward-argument (&optional arg
)
561 "Move forward ARG arguments."
563 (eshell-move-argument (point-max) 'forward-char
'arg-end arg
))
565 (defun eshell-backward-argument (&optional arg
)
566 "Move backward ARG arguments."
568 (eshell-move-argument (point-min) 'backward-char
'arg-begin arg
))
570 (defun eshell-repeat-argument (&optional arg
)
572 (let ((begin (save-excursion
573 (eshell-backward-argument arg
)
575 (kill-ring-save begin
(point))
579 "Goes to the beginning of line, then skips past the prompt, if any."
582 (and eshell-skip-prompt-function
583 (funcall eshell-skip-prompt-function
)))
585 (defsubst eshell-push-command-mark
()
586 "Push a mark at the end of the last input text."
587 (push-mark (1- eshell-last-input-end
) t
))
589 (custom-add-option 'eshell-pre-command-hook
'eshell-push-command-mark
)
591 (defsubst eshell-goto-input-start
()
592 "Goto the start of the last command input.
593 Putting this function on `eshell-pre-command-hook' will mimic Plan 9's
595 (goto-char eshell-last-input-start
))
597 (custom-add-option 'eshell-pre-command-hook
'eshell-push-command-mark
)
599 (defsubst eshell-interactive-print
(string)
600 "Print STRING to the eshell display buffer."
601 (eshell-output-filter nil string
))
603 (defsubst eshell-begin-on-new-line
()
604 "This function outputs a newline if not at beginning of line."
606 (goto-char eshell-last-output-end
)
608 (eshell-interactive-print "\n"))))
610 (defsubst eshell-reset
(&optional no-hooks
)
611 "Output a prompt on a new line, aborting any current input.
612 If NO-HOOKS is non-nil, then `eshell-post-command-hook' won't be run."
613 (goto-char (point-max))
614 (setq eshell-last-input-start
(point-marker)
615 eshell-last-input-end
(point-marker)
616 eshell-last-output-start
(point-marker)
617 eshell-last-output-block-begin
(point)
618 eshell-last-output-end
(point-marker))
619 (eshell-begin-on-new-line)
621 (run-hooks 'eshell-post-command-hook
)
622 (goto-char (point-max))))
624 (defun eshell-parse-command-input (beg end
&optional args
)
625 "Parse the command input from BEG to END.
626 The difference is that `eshell-parse-command' expects a complete
627 command string (and will error if it doesn't get one), whereas this
628 function will inform the caller whether more input is required.
630 If nil is returned, more input is necessary (probably because a
631 multi-line input string wasn't terminated properly). Otherwise, it
632 will return the parsed command."
635 (catch 'eshell-incomplete
637 (setq command
(eshell-parse-command (cons beg end
)
640 (message "Expecting completion of delimeter %c ..."
646 (defun eshell-update-markers (pmark)
647 "Update the input and output markers relative to point and PMARK."
648 (set-marker eshell-last-input-start pmark
)
649 (set-marker eshell-last-input-end
(point))
650 (set-marker eshell-last-output-end
(point)))
652 (defun eshell-queue-input (&optional use-region
)
653 "Queue the current input text for execution by Eshell.
654 Particularly, don't send the text to the current process, even if it's
657 (eshell-send-input use-region t
))
659 (eshell-deftest mode queue-input
660 "Queue command input"
661 (eshell-insert-command "sleep 2")
662 (eshell-insert-command "echo alpha" 'eshell-queue-input
)
664 (while (and eshell-current-command
667 (setq count
(1- count
))))
668 (eshell-match-result "alpha\n"))
670 (defun eshell-send-input (&optional use-region queue-p no-newline
)
671 "Send the input received to Eshell for parsing and processing..
672 After `eshell-last-output-end', sends all text from that marker to
673 point as input. Before that marker, calls `eshell-get-old-input' to
674 retrieve old input, copies it to the end of the buffer, and sends it.
676 If USE-REGION is non-nil, the current region (between point and mark)
677 will be used as input.
679 If QUEUE-P is non-nil, input will be queued until the next prompt,
680 rather than sent to the currently active process. If no process, the
681 input is processed immediately.
683 If NO-NEWLINE is non-nil, the input is sent without an implied final
686 ;; Note that the input string does not include its terminal newline.
687 (let ((proc-running-p (and (eshell-interactive-process)
689 (inhibit-point-motion-hooks t
)
690 after-change-functions
)
691 (unless (and proc-running-p
692 (not (eq (process-status
693 (eshell-interactive-process)) 'run
)))
694 (if (or proc-running-p
695 (>= (point) eshell-last-output-end
))
696 (goto-char (point-max))
697 (let ((copy (eshell-get-old-input use-region
)))
698 (goto-char eshell-last-output-end
)
699 (insert-and-inherit copy
)))
700 (unless (or no-newline
701 (and eshell-send-direct-to-subprocesses
703 (insert-before-markers-and-inherit ?
\n))
706 (eshell-update-markers eshell-last-output-end
)
707 (if (or eshell-send-direct-to-subprocesses
708 (= eshell-last-input-start eshell-last-input-end
))
710 (process-send-string (eshell-interactive-process) "\n"))
711 (process-send-region (eshell-interactive-process)
712 eshell-last-input-start
713 eshell-last-input-end
)))
714 (if (= eshell-last-output-end
(point))
715 (run-hooks 'eshell-post-command-hook
)
717 (eshell-condition-case err
719 (setq input
(buffer-substring-no-properties
720 eshell-last-output-end
(1- (point))))
721 (run-hook-with-args 'eshell-expand-input-functions
722 eshell-last-output-end
(1- (point)))
723 (let ((cmd (eshell-parse-command-input
724 eshell-last-output-end
(1- (point)))))
726 (eshell-update-markers eshell-last-output-end
)
727 (setq input
(buffer-substring-no-properties
728 eshell-last-input-start
729 (1- eshell-last-input-end
)))
730 (run-hooks 'eshell-input-filter-functions
)
731 (and (catch 'eshell-terminal
733 (if (eshell-invoke-directly cmd input
)
735 (eshell-eval-command cmd input
))))
736 (eshell-life-is-too-much)))))
739 (run-hooks 'eshell-post-command-hook
)
743 (eshell-interactive-print
744 (concat (error-message-string err
) "\n"))
745 (run-hooks 'eshell-post-command-hook
)
746 (insert-and-inherit input
)))))))))
748 ; (eshell-deftest proc send-to-subprocess
749 ; "Send input to a subprocess"
750 ; ;; jww (1999-12-06): what about when bc is unavailable?
751 ; (if (not (eshell-search-path "bc"))
753 ; (eshell-insert-command "bc")
754 ; (eshell-insert-command "1 + 2")
759 ; (eshell-insert-command "quit")
762 (defsubst eshell-kill-new
()
763 "Add the last input text to the kill ring."
764 (kill-ring-save eshell-last-input-start eshell-last-input-end
))
766 (custom-add-option 'eshell-input-filter-functions
'eshell-kill-new
)
768 (defun eshell-output-filter (process string
)
769 "Send the output from PROCESS (STRING) to the interactive display.
770 This is done after all necessary filtering has been done."
771 (let ((oprocbuf (if process
(process-buffer process
)
773 (inhibit-point-motion-hooks t
)
774 after-change-functions
)
775 (let ((functions eshell-preoutput-filter-functions
))
776 (while (and functions string
)
777 (setq string
(funcall (car functions
) string
))
778 (setq functions
(cdr functions
))))
779 (if (and string oprocbuf
(buffer-name oprocbuf
))
780 (let ((obuf (current-buffer))
782 (set-buffer oprocbuf
)
783 (setq opoint
(point))
784 (setq obeg
(point-min))
785 (setq oend
(point-max))
786 (let ((buffer-read-only nil
)
787 (nchars (length string
))
790 (goto-char eshell-last-output-end
)
791 (setq ostart
(point))
792 (if (<= (point) opoint
)
793 (setq opoint
(+ opoint nchars
)))
795 (setq obeg
(+ obeg nchars
)))
796 (if (<= (point) oend
)
797 (setq oend
(+ oend nchars
)))
798 (insert-before-markers string
)
799 (if (= (window-start (selected-window)) (point))
800 (set-window-start (selected-window)
802 (if (= (point) eshell-last-input-end
)
803 (set-marker eshell-last-input-end
804 (- eshell-last-input-end nchars
)))
805 (set-marker eshell-last-output-start ostart
)
806 (set-marker eshell-last-output-end
(point))
807 (force-mode-line-update))
808 (narrow-to-region obeg oend
)
810 (eshell-run-output-filters)
811 (set-buffer obuf
)))))
813 (defun eshell-run-output-filters ()
814 "Run the `eshell-output-filter-functions' on the current output."
816 (run-hooks 'eshell-output-filter-functions
))
817 (setq eshell-last-output-block-begin
818 (marker-position eshell-last-output-end
)))
820 ;;; jww (1999-10-23): this needs testing
821 (defun eshell-preinput-scroll-to-bottom ()
822 "Go to the end of buffer in all windows showing it.
823 Movement occurs if point in the selected window is not after the
824 process mark, and `this-command' is an insertion command. Insertion
825 commands recognised are `self-insert-command', `yank', and
826 `hilit-yank'. Depends on the value of
827 `eshell-scroll-to-bottom-on-input'.
829 This function should be a pre-command hook."
830 (if (memq this-command
'(self-insert-command yank hilit-yank
))
831 (let* ((selected (selected-window))
832 (current (current-buffer))
833 (scroll eshell-scroll-to-bottom-on-input
))
834 (if (< (point) eshell-last-output-end
)
835 (if (eq scroll
'this
)
836 (goto-char (point-max))
840 (when (and (eq (window-buffer window
) current
)
841 (or (eq scroll t
) (eq scroll
'all
)))
842 (select-window window
)
843 (goto-char (point-max))
844 (select-window selected
))))
847 ;;; jww (1999-10-23): this needs testing
848 (defun eshell-postoutput-scroll-to-bottom ()
849 "Go to the end of buffer in all windows showing it.
850 Does not scroll if the current line is the last line in the buffer.
851 Depends on the value of `eshell-scroll-to-bottom-on-output' and
852 `eshell-scroll-show-maximum-output'.
854 This function should be in the list `eshell-output-filter-functions'."
855 (let* ((selected (selected-window))
856 (current (current-buffer))
857 (scroll eshell-scroll-to-bottom-on-output
))
862 (if (eq (window-buffer window
) current
)
864 (select-window window
)
865 (if (and (< (point) eshell-last-output-end
)
866 (or (eq scroll t
) (eq scroll
'all
)
867 ;; Maybe user wants point to jump to end.
868 (and (eq scroll
'this
)
869 (eq selected window
))
870 (and (eq scroll
'others
)
871 (not (eq selected window
)))
872 ;; If point was at the end, keep it at end.
873 (>= (point) eshell-last-output-start
)))
874 (goto-char eshell-last-output-end
))
875 ;; Optionally scroll so that the text
876 ;; ends at the bottom of the window.
877 (if (and eshell-scroll-show-maximum-output
878 (>= (point) eshell-last-output-end
))
880 (goto-char (point-max))
882 (select-window selected
)))))
884 (set-buffer current
))))
886 (custom-add-option 'eshell-output-filter-functions
887 'eshell-postoutput-scroll-to-bottom
)
889 (defun eshell-beginning-of-input ()
890 "Return the location of the start of the previous input."
891 eshell-last-input-start
)
893 (defun eshell-beginning-of-output ()
894 "Return the location of the end of the previous output block."
895 eshell-last-input-end
)
897 (defun eshell-end-of-output ()
898 "Return the location of the end of the previous output block."
899 (if (eshell-using-module 'eshell-prompt
)
900 eshell-last-output-start
901 eshell-last-output-end
))
903 (defun eshell-kill-output ()
904 "Kill all output from interpreter since last input.
905 Does not delete the prompt."
908 (goto-char (eshell-beginning-of-output))
909 (insert "*** output flushed ***\n")
910 (delete-region (point) (eshell-end-of-output))))
912 (eshell-deftest io flush-output
913 "Flush previous output"
914 (eshell-insert-command "echo alpha")
916 (and (eshell-match-result (regexp-quote "*** output flushed ***\n"))
918 (= (point) eshell-last-output-start
)))
920 (defun eshell-show-output (&optional arg
)
921 "Display start of this batch of interpreter output at top of window.
922 Sets mark to the value of point when this command is run.
923 With a prefix argument, narrows region to last command output."
925 (goto-char (eshell-beginning-of-output))
926 (set-window-start (selected-window)
928 (goto-char (eshell-beginning-of-input))
929 (line-beginning-position)))
931 (narrow-to-region (eshell-beginning-of-output)
932 (eshell-end-of-output)))
933 (eshell-end-of-output))
935 (defun eshell-mark-output (&optional arg
)
936 "Display start of this batch of interpreter output at top of window.
937 Sets mark to the value of point when this command is run.
938 With a prefix argument, narrows region to last command output."
940 (push-mark (eshell-show-output arg
)))
942 (defun eshell-kill-input ()
943 "Kill all text from last stuff output by interpreter to point."
945 (if (> (point) eshell-last-output-end
)
946 (kill-region eshell-last-output-end
(point))
947 (let ((here (point)))
949 (kill-region (point) here
))))
951 (defun eshell-show-maximum-output ()
952 "Put the end of the buffer at the bottom of the window."
956 (goto-char (point-max))
959 (defun eshell-get-old-input (&optional use-current-region
)
960 "Return the command input on the current line."
961 (if use-current-region
962 (buffer-substring (min (point) (mark))
963 (max (point) (mark)))
966 (and eshell-skip-prompt-function
967 (funcall eshell-skip-prompt-function
))
970 (buffer-substring beg
(point))))))
972 (defun eshell-copy-old-input ()
973 "Insert after prompt old input at point as new input to be edited."
975 (let ((input (eshell-get-old-input)))
976 (goto-char eshell-last-output-end
)
977 (insert-and-inherit input
)))
979 (eshell-deftest mode run-old-command
980 "Re-run an old command"
981 (eshell-insert-command "echo alpha")
982 (goto-char eshell-last-input-start
)
983 (string= (eshell-get-old-input) "echo alpha"))
985 (defun eshell/exit
()
986 "Leave or kill the Eshell buffer, depending on `eshell-kill-on-exit'."
987 (throw 'eshell-terminal t
))
989 (defun eshell-life-is-too-much ()
990 "Kill the current buffer (or bury it). Good-bye Eshell."
992 (if (not eshell-kill-on-exit
)
994 (kill-buffer (current-buffer))))
996 (defun eshell-truncate-buffer ()
997 "Truncate the buffer to `eshell-buffer-maximum-lines'.
998 This function could be on `eshell-output-filter-functions' or bound to
1002 (goto-char eshell-last-output-end
)
1003 (let ((lines (count-lines 1 (point)))
1004 (inhibit-read-only t
))
1005 (forward-line (- eshell-buffer-maximum-lines
))
1007 (let ((pos (point)))
1010 (error "Buffer too short to truncate"))
1011 (delete-region (point-min) (point))
1013 (message "Truncated buffer from %d to %d lines (%.1fk freed)"
1014 lines eshell-buffer-maximum-lines
1015 (/ pos
1024.0))))))))
1017 (custom-add-option 'eshell-output-filter-functions
1018 'eshell-truncate-buffer
)
1020 (defun send-invisible (str)
1021 "Read a string without echoing.
1022 Then send it to the process running in the current buffer."
1023 (interactive "P") ; Defeat snooping via C-x ESC ESC
1024 (let ((str (read-passwd
1025 (format "Password: "
1026 (process-name (eshell-interactive-process))))))
1028 (process-send-string (eshell-interactive-process)
1030 (message "Warning: text will be echoed"))))
1032 (defun eshell-watch-for-password-prompt ()
1033 "Prompt in the minibuffer for password and send without echoing.
1034 This function uses `send-invisible' to read and send a password to the
1035 buffer's process if STRING contains a password prompt defined by
1036 `eshell-password-prompt-regexp'.
1038 This function could be in the list `eshell-output-filter-functions'."
1039 (when (eshell-interactive-process)
1041 (goto-char eshell-last-output-block-begin
)
1043 (if (re-search-forward eshell-password-prompt-regexp
1044 eshell-last-output-end t
)
1045 (send-invisible nil
)))))
1047 (custom-add-option 'eshell-output-filter-functions
1048 'eshell-watch-for-password-prompt
)
1050 (defun eshell-handle-control-codes ()
1051 "Act properly when certain control codes are seen."
1053 (let ((orig (point)))
1054 (goto-char eshell-last-output-block-begin
)
1056 (beginning-of-line))
1057 (while (< (point) eshell-last-output-end
)
1058 (let ((char (char-after)))
1061 (if (< (1+ (point)) eshell-last-output-end
)
1062 (if (memq (char-after (1+ (point)))
1065 (let ((end (1+ (point))))
1067 (delete-region (point) end
)))
1068 (add-text-properties (point) (1+ (point))
1075 (delete-backward-char 1)
1078 (forward-char))))))))
1080 (custom-add-option 'eshell-output-filter-functions
1081 'eshell-handle-control-codes
)
1085 ;;; esh-mode.el ends here