(latexenc-find-file-coding-system): Don't inherit the EOL part of the
[emacs.git] / lisp / eshell / esh-mode.el
blob51b9f71b75da18698872fcb12e52bbe2f00de0a1
1 ;;; esh-mode.el --- user interface
3 ;; Copyright (C) 1999, 2000, 2001, 2004 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)
12 ;; any later version.
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.
24 (provide 'esh-mode)
26 (eval-when-compile (require 'esh-maint))
28 (defgroup eshell-mode nil
29 "This module contains code for handling input from the user."
30 :tag "User interface"
31 :group 'eshell)
33 ;;; Commentary:
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
58 ;; large.
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
62 ;; only that output.
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.
70 (require 'esh-module)
71 (require 'esh-cmd)
72 (require 'esh-io)
73 (require 'esh-var)
75 ;;; User Variables:
77 (defcustom eshell-mode-unload-hook nil
78 "*A hook that gets run when `eshell-mode' is unloaded."
79 :type 'hook
80 :group 'eshell-mode)
82 (defcustom eshell-mode-hook nil
83 "*A hook that gets run when `eshell-mode' is entered."
84 :type 'hook
85 :group 'eshell-mode)
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."
90 :type 'hook
91 :group 'eshell-mode)
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."
96 :type 'hook
97 :group 'eshell-mode)
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)
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)
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)
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)
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))
133 :group 'eshell-mode)
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))
147 :group 'eshell-mode)
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'."
155 :type 'boolean
156 :group 'eshell-mode)
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)
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."
172 :type 'hook
173 :group 'eshell-mode)
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."
179 :type 'hook
180 :group 'eshell-mode)
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'."
186 :type 'regexp
187 :group 'eshell-mode)
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)
192 :group 'eshell-mode)
194 (defcustom eshell-status-in-modeline t
195 "*If non-nil, let the user know a command is running in the modeline."
196 :type 'boolean
197 :group 'eshell-mode)
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 ())
223 (eval-when-compile
224 (unless (eshell-under-xemacs-p)
225 (defalias 'characterp 'ignore)
226 (defalias 'char-int 'ignore)))
228 (if (not eshell-mode-syntax-table)
229 (let ((i 0))
230 (setq eshell-mode-syntax-table (make-syntax-table))
231 (while (< i ?0)
232 (modify-syntax-entry i "_ " eshell-mode-syntax-table)
233 (setq i (1+ i)))
234 (setq i (1+ ?9))
235 (while (< i ?A)
236 (modify-syntax-entry i "_ " eshell-mode-syntax-table)
237 (setq i (1+ i)))
238 (setq i (1+ ?Z))
239 (while (< i ?a)
240 (modify-syntax-entry i "_ " eshell-mode-syntax-table)
241 (setq i (1+ i)))
242 (setq i (1+ ?z))
243 (while (< i 128)
244 (modify-syntax-entry i "_ " eshell-mode-syntax-table)
245 (setq i (1+ i)))
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)
271 (map-char-table
272 (function
273 (lambda (key val)
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))
280 (map-char-table
281 (function
282 (lambda (key val)
283 (and (>= key 256)
284 (/= (char-syntax key) ?w)
285 (modify-syntax-entry key "_ "
286 eshell-mode-syntax-table))))
287 (standard-syntax-table)))))
289 ;;; User Functions:
291 ;;;###autoload
292 (defun eshell-mode ()
293 "Emacs shell interactive mode.
295 \\{eshell-mode-map}"
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 (copy-sequence 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)))
312 (if modeline
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
330 ;; modified
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
371 ;; indicated it!
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 (copy-sequence 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))
394 module-shortname)
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)
409 "-load-hook"))))
411 ;; load extension modules for this session
412 (eshell-for module eshell-modules-list
413 (let ((load-hook (intern-soft (concat (symbol-name module)
414 "-load-hook"))))
415 (if (and load-hook (boundp load-hook))
416 (run-hooks load-hook))))
418 (if eshell-send-direct-to-subprocesses
419 (add-hook 'pre-command-hook 'eshell-intercept-commands t t))
421 (if eshell-scroll-to-bottom-on-input
422 (add-hook 'pre-command-hook 'eshell-preinput-scroll-to-bottom t t))
424 (when eshell-scroll-show-maximum-output
425 (set (make-local-variable 'scroll-conservatively) 1000))
427 (when eshell-status-in-modeline
428 (add-hook 'eshell-pre-command-hook 'eshell-command-started nil t)
429 (add-hook 'eshell-post-command-hook 'eshell-command-finished nil t))
431 (add-hook 'kill-buffer-hook
432 (function
433 (lambda ()
434 (run-hooks 'eshell-exit-hook))) t t)
436 (if eshell-first-time-p
437 (run-hooks 'eshell-first-time-mode-hook))
438 (run-mode-hooks 'eshell-mode-hook)
439 (run-hooks 'eshell-post-command-hook))
441 (put 'eshell-mode 'mode-class 'special)
443 (eshell-deftest mode major-mode
444 "Major mode is correct"
445 (eq major-mode 'eshell-mode))
447 (eshell-deftest mode eshell-mode-variable
448 "`eshell-mode' is true"
449 (eq eshell-mode t))
451 (eshell-deftest var window-height
452 "LINES equals window height"
453 (let ((eshell-stringify-t t))
454 (eshell-command-result-p "= $LINES (window-height)" "t\n")))
456 (defun eshell-command-started ()
457 "Indicate in the modeline that a command has started."
458 (setq eshell-command-running-string "**")
459 (force-mode-line-update))
461 (defun eshell-command-finished ()
462 "Indicate in the modeline that a command has finished."
463 (setq eshell-command-running-string "--")
464 (force-mode-line-update))
466 (eshell-deftest mode command-running-p
467 "Modeline shows no command running"
468 (or (eshell-under-xemacs-p)
469 (not eshell-status-in-modeline)
470 (and (memq 'eshell-command-running-string mode-line-format)
471 (equal eshell-command-running-string "--"))))
473 ;;; Internal Functions:
475 (defun eshell-toggle-direct-send ()
476 (interactive)
477 (if eshell-send-direct-to-subprocesses
478 (progn
479 (setq eshell-send-direct-to-subprocesses nil)
480 (remove-hook 'pre-command-hook 'eshell-intercept-commands t)
481 (message "Sending subprocess input on RET"))
482 (setq eshell-send-direct-to-subprocesses t)
483 (add-hook 'pre-command-hook 'eshell-intercept-commands t t)
484 (message "Sending subprocess input directly")))
486 (defun eshell-self-insert-command (N)
487 (interactive "i")
488 (process-send-string
489 (eshell-interactive-process)
490 (char-to-string (if (symbolp last-command-char)
491 (get last-command-char 'ascii-character)
492 last-command-char))))
494 (defun eshell-intercept-commands ()
495 (when (and (eshell-interactive-process)
496 (not (and (integerp last-input-event)
497 (memq last-input-event '(?\C-x ?\C-c)))))
498 (let ((possible-events (where-is-internal this-command))
499 (name (symbol-name this-command))
500 (intercept t))
501 ;; Assume that any multikey combination which does NOT target an
502 ;; Eshell command, is a combo the user wants invoked rather than
503 ;; sent to the underlying subprocess.
504 (unless (and (> (length name) 7)
505 (equal (substring name 0 7) "eshell-"))
506 (while possible-events
507 (if (> (length (car possible-events)) 1)
508 (setq intercept nil possible-events nil)
509 (setq possible-events (cdr possible-events)))))
510 (if intercept
511 (setq this-command 'eshell-self-insert-command)))))
513 (defun eshell-find-tag (&optional tagname next-p regexp-p)
514 "A special version of `find-tag' that ignores read-onlyness."
515 (interactive)
516 (require 'etags)
517 (let ((inhibit-read-only t)
518 (no-default (eobp))
519 (find-tag-default-function 'ignore))
520 (with-no-warnings
521 (setq tagname (car (find-tag-interactive "Find tag: "))))
522 (find-tag tagname next-p regexp-p)))
524 (defun eshell-move-argument (limit func property arg)
525 "Move forward ARG arguments."
526 (catch 'eshell-incomplete
527 (eshell-parse-arguments (save-excursion (eshell-bol) (point))
528 (line-end-position)))
529 (let ((pos (save-excursion
530 (funcall func 1)
531 (while (and (> arg 0) (/= (point) limit))
532 (if (get-text-property (point) property)
533 (setq arg (1- arg)))
534 (if (> arg 0)
535 (funcall func 1)))
536 (point))))
537 (goto-char pos)
538 (if (and (eq func 'forward-char)
539 (= (1+ pos) limit))
540 (forward-char 1))))
542 (eshell-deftest arg forward-arg
543 "Move across command arguments"
544 (eshell-insert-command "echo $(+ 1 (- 4 3)) \"alpha beta\" file" 'ignore)
545 (let ((here (point)) begin valid)
546 (eshell-bol)
547 (setq begin (point))
548 (eshell-forward-argument 4)
549 (setq valid (= here (point)))
550 (eshell-backward-argument 4)
551 (prog1
552 (and valid (= begin (point)))
553 (eshell-bol)
554 (delete-region (point) (point-max)))))
556 (defun eshell-forward-argument (&optional arg)
557 "Move forward ARG arguments."
558 (interactive "p")
559 (eshell-move-argument (point-max) 'forward-char 'arg-end arg))
561 (defun eshell-backward-argument (&optional arg)
562 "Move backward ARG arguments."
563 (interactive "p")
564 (eshell-move-argument (point-min) 'backward-char 'arg-begin arg))
566 (defun eshell-repeat-argument (&optional arg)
567 (interactive "p")
568 (let ((begin (save-excursion
569 (eshell-backward-argument arg)
570 (point))))
571 (kill-ring-save begin (point))
572 (yank)))
574 (defun eshell-bol ()
575 "Goes to the beginning of line, then skips past the prompt, if any."
576 (interactive)
577 (beginning-of-line)
578 (and eshell-skip-prompt-function
579 (funcall eshell-skip-prompt-function)))
581 (defsubst eshell-push-command-mark ()
582 "Push a mark at the end of the last input text."
583 (push-mark (1- eshell-last-input-end) t))
585 (custom-add-option 'eshell-pre-command-hook 'eshell-push-command-mark)
587 (defsubst eshell-goto-input-start ()
588 "Goto the start of the last command input.
589 Putting this function on `eshell-pre-command-hook' will mimic Plan 9's
590 9term behavior."
591 (goto-char eshell-last-input-start))
593 (custom-add-option 'eshell-pre-command-hook 'eshell-push-command-mark)
595 (defsubst eshell-interactive-print (string)
596 "Print STRING to the eshell display buffer."
597 (eshell-output-filter nil string))
599 (defsubst eshell-begin-on-new-line ()
600 "This function outputs a newline if not at beginning of line."
601 (save-excursion
602 (goto-char eshell-last-output-end)
603 (or (bolp)
604 (eshell-interactive-print "\n"))))
606 (defsubst eshell-reset (&optional no-hooks)
607 "Output a prompt on a new line, aborting any current input.
608 If NO-HOOKS is non-nil, then `eshell-post-command-hook' won't be run."
609 (goto-char (point-max))
610 (setq eshell-last-input-start (point-marker)
611 eshell-last-input-end (point-marker)
612 eshell-last-output-start (point-marker)
613 eshell-last-output-block-begin (point)
614 eshell-last-output-end (point-marker))
615 (eshell-begin-on-new-line)
616 (unless no-hooks
617 (run-hooks 'eshell-post-command-hook)
618 (goto-char (point-max))))
620 (defun eshell-parse-command-input (beg end &optional args)
621 "Parse the command input from BEG to END.
622 The difference is that `eshell-parse-command' expects a complete
623 command string (and will error if it doesn't get one), whereas this
624 function will inform the caller whether more input is required.
626 If nil is returned, more input is necessary (probably because a
627 multi-line input string wasn't terminated properly). Otherwise, it
628 will return the parsed command."
629 (let (delim command)
630 (if (setq delim
631 (catch 'eshell-incomplete
632 (ignore
633 (setq command (eshell-parse-command (cons beg end)
634 args t)))))
635 (ignore
636 (message "Expecting completion of delimeter %c ..."
637 (if (listp delim)
638 (car delim)
639 delim)))
640 command)))
642 (defun eshell-update-markers (pmark)
643 "Update the input and output markers relative to point and PMARK."
644 (set-marker eshell-last-input-start pmark)
645 (set-marker eshell-last-input-end (point))
646 (set-marker eshell-last-output-end (point)))
648 (defun eshell-queue-input (&optional use-region)
649 "Queue the current input text for execution by Eshell.
650 Particularly, don't send the text to the current process, even if it's
651 waiting for input."
652 (interactive "P")
653 (eshell-send-input use-region t))
655 (eshell-deftest mode queue-input
656 "Queue command input"
657 (eshell-insert-command "sleep 2")
658 (eshell-insert-command "echo alpha" 'eshell-queue-input)
659 (let ((count 10))
660 (while (and eshell-current-command
661 (> count 0))
662 (sit-for 1 0)
663 (setq count (1- count))))
664 (eshell-match-result "alpha\n"))
666 (defun eshell-send-input (&optional use-region queue-p no-newline)
667 "Send the input received to Eshell for parsing and processing.
668 After `eshell-last-output-end', sends all text from that marker to
669 point as input. Before that marker, calls `eshell-get-old-input' to
670 retrieve old input, copies it to the end of the buffer, and sends it.
672 If USE-REGION is non-nil, the current region (between point and mark)
673 will be used as input.
675 If QUEUE-P is non-nil, input will be queued until the next prompt,
676 rather than sent to the currently active process. If no process, the
677 input is processed immediately.
679 If NO-NEWLINE is non-nil, the input is sent without an implied final
680 newline."
681 (interactive "P")
682 ;; Note that the input string does not include its terminal newline.
683 (let ((proc-running-p (and (eshell-interactive-process)
684 (not queue-p)))
685 (inhibit-point-motion-hooks t)
686 after-change-functions)
687 (unless (and proc-running-p
688 (not (eq (process-status
689 (eshell-interactive-process)) 'run)))
690 (if (or proc-running-p
691 (>= (point) eshell-last-output-end))
692 (goto-char (point-max))
693 (let ((copy (eshell-get-old-input use-region)))
694 (goto-char eshell-last-output-end)
695 (insert-and-inherit copy)))
696 (unless (or no-newline
697 (and eshell-send-direct-to-subprocesses
698 proc-running-p))
699 (insert-before-markers-and-inherit ?\n))
700 (if proc-running-p
701 (progn
702 (eshell-update-markers eshell-last-output-end)
703 (if (or eshell-send-direct-to-subprocesses
704 (= eshell-last-input-start eshell-last-input-end))
705 (unless no-newline
706 (process-send-string (eshell-interactive-process) "\n"))
707 (process-send-region (eshell-interactive-process)
708 eshell-last-input-start
709 eshell-last-input-end)))
710 (if (= eshell-last-output-end (point))
711 (run-hooks 'eshell-post-command-hook)
712 (let (input)
713 (eshell-condition-case err
714 (progn
715 (setq input (buffer-substring-no-properties
716 eshell-last-output-end (1- (point))))
717 (run-hook-with-args 'eshell-expand-input-functions
718 eshell-last-output-end (1- (point)))
719 (let ((cmd (eshell-parse-command-input
720 eshell-last-output-end (1- (point)))))
721 (when cmd
722 (eshell-update-markers eshell-last-output-end)
723 (setq input (buffer-substring-no-properties
724 eshell-last-input-start
725 (1- eshell-last-input-end)))
726 (run-hooks 'eshell-input-filter-functions)
727 (and (catch 'eshell-terminal
728 (ignore
729 (if (eshell-invoke-directly cmd input)
730 (eval cmd)
731 (eshell-eval-command cmd input))))
732 (eshell-life-is-too-much)))))
733 (quit
734 (eshell-reset t)
735 (run-hooks 'eshell-post-command-hook)
736 (signal 'quit nil))
737 (error
738 (eshell-reset t)
739 (eshell-interactive-print
740 (concat (error-message-string err) "\n"))
741 (run-hooks 'eshell-post-command-hook)
742 (insert-and-inherit input)))))))))
744 ; (eshell-deftest proc send-to-subprocess
745 ; "Send input to a subprocess"
746 ; ;; jww (1999-12-06): what about when bc is unavailable?
747 ; (if (not (eshell-search-path "bc"))
749 ; (eshell-insert-command "bc")
750 ; (eshell-insert-command "1 + 2")
751 ; (sit-for 1 0)
752 ; (forward-line -1)
753 ; (prog1
754 ; (looking-at "3\n")
755 ; (eshell-insert-command "quit")
756 ; (sit-for 1 0))))
758 (defsubst eshell-kill-new ()
759 "Add the last input text to the kill ring."
760 (kill-ring-save eshell-last-input-start eshell-last-input-end))
762 (custom-add-option 'eshell-input-filter-functions 'eshell-kill-new)
764 (defun eshell-output-filter (process string)
765 "Send the output from PROCESS (STRING) to the interactive display.
766 This is done after all necessary filtering has been done."
767 (let ((oprocbuf (if process (process-buffer process)
768 (current-buffer)))
769 (inhibit-point-motion-hooks t)
770 after-change-functions)
771 (let ((functions eshell-preoutput-filter-functions))
772 (while (and functions string)
773 (setq string (funcall (car functions) string))
774 (setq functions (cdr functions))))
775 (if (and string oprocbuf (buffer-name oprocbuf))
776 (let ((obuf (current-buffer))
777 opoint obeg oend)
778 (set-buffer oprocbuf)
779 (setq opoint (point))
780 (setq obeg (point-min))
781 (setq oend (point-max))
782 (let ((buffer-read-only nil)
783 (nchars (length string))
784 (ostart nil))
785 (widen)
786 (goto-char eshell-last-output-end)
787 (setq ostart (point))
788 (if (<= (point) opoint)
789 (setq opoint (+ opoint nchars)))
790 (if (< (point) obeg)
791 (setq obeg (+ obeg nchars)))
792 (if (<= (point) oend)
793 (setq oend (+ oend nchars)))
794 (insert-before-markers string)
795 (if (= (window-start (selected-window)) (point))
796 (set-window-start (selected-window)
797 (- (point) nchars)))
798 (if (= (point) eshell-last-input-end)
799 (set-marker eshell-last-input-end
800 (- eshell-last-input-end nchars)))
801 (set-marker eshell-last-output-start ostart)
802 (set-marker eshell-last-output-end (point))
803 (force-mode-line-update))
804 (narrow-to-region obeg oend)
805 (goto-char opoint)
806 (eshell-run-output-filters)
807 (set-buffer obuf)))))
809 (defun eshell-run-output-filters ()
810 "Run the `eshell-output-filter-functions' on the current output."
811 (save-current-buffer
812 (run-hooks 'eshell-output-filter-functions))
813 (setq eshell-last-output-block-begin
814 (marker-position eshell-last-output-end)))
816 ;;; jww (1999-10-23): this needs testing
817 (defun eshell-preinput-scroll-to-bottom ()
818 "Go to the end of buffer in all windows showing it.
819 Movement occurs if point in the selected window is not after the
820 process mark, and `this-command' is an insertion command. Insertion
821 commands recognised are `self-insert-command', `yank', and
822 `hilit-yank'. Depends on the value of
823 `eshell-scroll-to-bottom-on-input'.
825 This function should be a pre-command hook."
826 (if (memq this-command '(self-insert-command yank hilit-yank))
827 (let* ((selected (selected-window))
828 (current (current-buffer))
829 (scroll eshell-scroll-to-bottom-on-input))
830 (if (< (point) eshell-last-output-end)
831 (if (eq scroll 'this)
832 (goto-char (point-max))
833 (walk-windows
834 (function
835 (lambda (window)
836 (when (and (eq (window-buffer window) current)
837 (or (eq scroll t) (eq scroll 'all)))
838 (select-window window)
839 (goto-char (point-max))
840 (select-window selected))))
841 nil t))))))
843 ;;; jww (1999-10-23): this needs testing
844 (defun eshell-postoutput-scroll-to-bottom ()
845 "Go to the end of buffer in all windows showing it.
846 Does not scroll if the current line is the last line in the buffer.
847 Depends on the value of `eshell-scroll-to-bottom-on-output' and
848 `eshell-scroll-show-maximum-output'.
850 This function should be in the list `eshell-output-filter-functions'."
851 (let* ((selected (selected-window))
852 (current (current-buffer))
853 (scroll eshell-scroll-to-bottom-on-output))
854 (unwind-protect
855 (walk-windows
856 (function
857 (lambda (window)
858 (if (eq (window-buffer window) current)
859 (progn
860 (select-window window)
861 (if (and (< (point) eshell-last-output-end)
862 (or (eq scroll t) (eq scroll 'all)
863 ;; Maybe user wants point to jump to end.
864 (and (eq scroll 'this)
865 (eq selected window))
866 (and (eq scroll 'others)
867 (not (eq selected window)))
868 ;; If point was at the end, keep it at end.
869 (>= (point) eshell-last-output-start)))
870 (goto-char eshell-last-output-end))
871 ;; Optionally scroll so that the text
872 ;; ends at the bottom of the window.
873 (if (and eshell-scroll-show-maximum-output
874 (>= (point) eshell-last-output-end))
875 (save-excursion
876 (goto-char (point-max))
877 (recenter -1)))
878 (select-window selected)))))
879 nil t)
880 (set-buffer current))))
882 (custom-add-option 'eshell-output-filter-functions
883 'eshell-postoutput-scroll-to-bottom)
885 (defun eshell-beginning-of-input ()
886 "Return the location of the start of the previous input."
887 eshell-last-input-start)
889 (defun eshell-beginning-of-output ()
890 "Return the location of the end of the previous output block."
891 eshell-last-input-end)
893 (defun eshell-end-of-output ()
894 "Return the location of the end of the previous output block."
895 (if (eshell-using-module 'eshell-prompt)
896 eshell-last-output-start
897 eshell-last-output-end))
899 (defun eshell-kill-output ()
900 "Kill all output from interpreter since last input.
901 Does not delete the prompt."
902 (interactive)
903 (save-excursion
904 (goto-char (eshell-beginning-of-output))
905 (insert "*** output flushed ***\n")
906 (delete-region (point) (eshell-end-of-output))))
908 (eshell-deftest io flush-output
909 "Flush previous output"
910 (eshell-insert-command "echo alpha")
911 (eshell-kill-output)
912 (and (eshell-match-result (regexp-quote "*** output flushed ***\n"))
913 (forward-line)
914 (= (point) eshell-last-output-start)))
916 (defun eshell-show-output (&optional arg)
917 "Display start of this batch of interpreter output at top of window.
918 Sets mark to the value of point when this command is run.
919 With a prefix argument, narrows region to last command output."
920 (interactive "P")
921 (goto-char (eshell-beginning-of-output))
922 (set-window-start (selected-window)
923 (save-excursion
924 (goto-char (eshell-beginning-of-input))
925 (line-beginning-position)))
926 (if arg
927 (narrow-to-region (eshell-beginning-of-output)
928 (eshell-end-of-output)))
929 (eshell-end-of-output))
931 (defun eshell-mark-output (&optional arg)
932 "Display start of this batch of interpreter output at top of window.
933 Sets mark to the value of point when this command is run.
934 With a prefix argument, narrows region to last command output."
935 (interactive "P")
936 (push-mark (eshell-show-output arg)))
938 (defun eshell-kill-input ()
939 "Kill all text from last stuff output by interpreter to point."
940 (interactive)
941 (if (> (point) eshell-last-output-end)
942 (kill-region eshell-last-output-end (point))
943 (let ((here (point)))
944 (eshell-bol)
945 (kill-region (point) here))))
947 (defun eshell-show-maximum-output (&optional interactive)
948 "Put the end of the buffer at the bottom of the window.
949 When run interactively, widen the buffer first."
950 (interactive "p")
951 (if interactive
952 (widen))
953 (goto-char (point-max))
954 (recenter -1))
956 (defun eshell-get-old-input (&optional use-current-region)
957 "Return the command input on the current line."
958 (if use-current-region
959 (buffer-substring (min (point) (mark))
960 (max (point) (mark)))
961 (save-excursion
962 (beginning-of-line)
963 (and eshell-skip-prompt-function
964 (funcall eshell-skip-prompt-function))
965 (let ((beg (point)))
966 (end-of-line)
967 (buffer-substring beg (point))))))
969 (defun eshell-copy-old-input ()
970 "Insert after prompt old input at point as new input to be edited."
971 (interactive)
972 (let ((input (eshell-get-old-input)))
973 (goto-char eshell-last-output-end)
974 (insert-and-inherit input)))
976 (eshell-deftest mode run-old-command
977 "Re-run an old command"
978 (eshell-insert-command "echo alpha")
979 (goto-char eshell-last-input-start)
980 (string= (eshell-get-old-input) "echo alpha"))
982 (defun eshell/exit ()
983 "Leave or kill the Eshell buffer, depending on `eshell-kill-on-exit'."
984 (throw 'eshell-terminal t))
986 (defun eshell-life-is-too-much ()
987 "Kill the current buffer (or bury it). Good-bye Eshell."
988 (interactive)
989 (if (not eshell-kill-on-exit)
990 (bury-buffer)
991 (kill-buffer (current-buffer))))
993 (defun eshell-truncate-buffer ()
994 "Truncate the buffer to `eshell-buffer-maximum-lines'.
995 This function could be on `eshell-output-filter-functions' or bound to
996 a key."
997 (interactive)
998 (save-excursion
999 (goto-char eshell-last-output-end)
1000 (let ((lines (count-lines 1 (point)))
1001 (inhibit-read-only t))
1002 (forward-line (- eshell-buffer-maximum-lines))
1003 (beginning-of-line)
1004 (let ((pos (point)))
1005 (if (bobp)
1006 (if (interactive-p)
1007 (message "Buffer too short to truncate"))
1008 (delete-region (point-min) (point))
1009 (if (interactive-p)
1010 (message "Truncated buffer from %d to %d lines (%.1fk freed)"
1011 lines eshell-buffer-maximum-lines
1012 (/ pos 1024.0))))))))
1014 (custom-add-option 'eshell-output-filter-functions
1015 'eshell-truncate-buffer)
1017 (defun eshell-send-invisible (str)
1018 "Read a string without echoing.
1019 Then send it to the process running in the current buffer."
1020 (interactive "P") ; Defeat snooping via C-x ESC ESC
1021 (let ((str (read-passwd
1022 (format "%s Password: "
1023 (process-name (eshell-interactive-process))))))
1024 (if (stringp str)
1025 (process-send-string (eshell-interactive-process)
1026 (concat str "\n"))
1027 (message "Warning: text will be echoed"))))
1029 (defun eshell-watch-for-password-prompt ()
1030 "Prompt in the minibuffer for password and send without echoing.
1031 This function uses `eshell-send-invisible' to read and send a password to the
1032 buffer's process if STRING contains a password prompt defined by
1033 `eshell-password-prompt-regexp'.
1035 This function could be in the list `eshell-output-filter-functions'."
1036 (when (eshell-interactive-process)
1037 (save-excursion
1038 (goto-char eshell-last-output-block-begin)
1039 (beginning-of-line)
1040 (if (re-search-forward eshell-password-prompt-regexp
1041 eshell-last-output-end t)
1042 (eshell-send-invisible nil)))))
1044 (custom-add-option 'eshell-output-filter-functions
1045 'eshell-watch-for-password-prompt)
1047 (defun eshell-handle-control-codes ()
1048 "Act properly when certain control codes are seen."
1049 (save-excursion
1050 (let ((orig (point)))
1051 (goto-char eshell-last-output-block-begin)
1052 (unless (eolp)
1053 (beginning-of-line))
1054 (while (< (point) eshell-last-output-end)
1055 (let ((char (char-after)))
1056 (cond
1057 ((eq char ?\r)
1058 (if (< (1+ (point)) eshell-last-output-end)
1059 (if (memq (char-after (1+ (point)))
1060 '(?\n ?\r))
1061 (delete-char 1)
1062 (let ((end (1+ (point))))
1063 (beginning-of-line)
1064 (delete-region (point) end)))
1065 (add-text-properties (point) (1+ (point))
1066 '(invisible t))
1067 (forward-char)))
1068 ((eq char ?\a)
1069 (delete-char 1)
1070 (beep))
1071 ((eq char ?\C-h)
1072 (delete-backward-char 1)
1073 (delete-char 1))
1075 (forward-char))))))))
1077 (custom-add-option 'eshell-output-filter-functions
1078 'eshell-handle-control-codes)
1080 ;;; Code:
1082 ;;; arch-tag: ec65bc2b-da14-4547-81d3-a32af3a4dc57
1083 ;;; esh-mode.el ends here