]> code.delx.au - gnu-emacs/blob - lisp/eshell/esh-mode.el
Merge from emacs--devo--0
[gnu-emacs] / lisp / eshell / esh-mode.el
1 ;;; esh-mode.el --- user interface
2
3 ;; Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007 Free Software Foundation, Inc.
5
6 ;; Author: John Wiegley <johnw@gnu.org>
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 3, or (at your option)
13 ;; any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
23 ;; Boston, MA 02110-1301, USA.
24
25 (provide 'esh-mode)
26
27 (eval-when-compile (require 'esh-maint))
28
29 (defgroup eshell-mode nil
30 "This module contains code for handling input from the user."
31 :tag "User interface"
32 :group 'eshell)
33
34 ;;; Commentary:
35
36 ;; Basically, Eshell is used just like shell mode (<M-x shell>). The
37 ;; keystrokes for navigating the buffer, and accessing the command
38 ;; history, are identical. Unlike shell mode, however, Eshell mode's
39 ;; governing process is Emacs itself. With shell mode, an inferior
40 ;; shell process is executed that communicates with Emacs via comint
41 ;; -- a mode for handling sub-process interaction. Eshell mode, on
42 ;; the other hand, is a truly native Emacs shell. No subprocess are
43 ;; invoked except the ones requested by the user at the prompt.
44 ;;
45 ;; After entering a command, use <RET> to invoke it ([Command
46 ;; invocation]) . If there is a command on disk, it will be executed
47 ;; as in a normal shell. If there is no command by that name on disk,
48 ;; but a Lisp function with that name is defined, the Lisp function
49 ;; will be called, using the arguments passed on the command line.
50 ;;
51 ;; Some of the other features of the command interaction mode are:
52 ;;
53 ;; @ <M-RET> can be used to accumulate further commands while a
54 ;; command is currently running. Since all input is passed to the
55 ;; subprocess being executed, there is no automatic input queueing
56 ;; as there is with other shells.
57 ;;
58 ;; @ <C-c C-t> can be used to truncate the buffer if it grows too
59 ;; large.
60 ;;
61 ;; @ <C-c C-r> will move point to the beginning of the output of the
62 ;; last command. With a prefix argument, it will narrow to view
63 ;; only that output.
64 ;;
65 ;; @ <C-c C-o> will delete the output from the last command.
66 ;;
67 ;; @ <C-c C-f> will move forward a complete shell argument.
68 ;;
69 ;; @ <C-c C-b> will move backward a complete shell argument.
70
71 (require 'esh-module)
72 (require 'esh-cmd)
73 (require 'esh-io)
74 (require 'esh-var)
75
76 ;;; User Variables:
77
78 (defcustom eshell-mode-unload-hook nil
79 "*A hook that gets run when `eshell-mode' is unloaded."
80 :type 'hook
81 :group 'eshell-mode)
82
83 (defcustom eshell-mode-hook nil
84 "*A hook that gets run when `eshell-mode' is entered."
85 :type 'hook
86 :group 'eshell-mode)
87
88 (defcustom eshell-first-time-mode-hook nil
89 "*A hook that gets run the first time `eshell-mode' is entered.
90 That is to say, the first time during an Emacs session."
91 :type 'hook
92 :group 'eshell-mode)
93
94 (defcustom eshell-exit-hook '(eshell-query-kill-processes)
95 "*A hook that is run whenever `eshell' is exited.
96 This hook is only run if exiting actually kills the buffer."
97 :type 'hook
98 :group 'eshell-mode)
99
100 (defcustom eshell-kill-on-exit t
101 "*If non-nil, kill the Eshell buffer on the `exit' command.
102 Otherwise, the buffer will simply be buried."
103 :type 'boolean
104 :group 'eshell-mode)
105
106 (defcustom eshell-input-filter-functions nil
107 "*Functions to call before input is processed.
108 The input is contained in the region from `eshell-last-input-start' to
109 `eshell-last-input-end'."
110 :type 'hook
111 :group 'eshell-mode)
112
113 (defcustom eshell-send-direct-to-subprocesses nil
114 "*If t, send any input immediately to a subprocess."
115 :type 'boolean
116 :group 'eshell-mode)
117
118 (defcustom eshell-expand-input-functions nil
119 "*Functions to call before input is parsed.
120 Each function is passed two arguments, which bounds the region of the
121 current input text."
122 :type 'hook
123 :group 'eshell-mode)
124
125 (defcustom eshell-scroll-to-bottom-on-input nil
126 "*Controls whether input to interpreter causes window to scroll.
127 If nil, then do not scroll. If t or `all', scroll all windows showing
128 buffer. If `this', scroll only the selected window.
129
130 See `eshell-preinput-scroll-to-bottom'."
131 :type '(radio (const :tag "Do not scroll Eshell windows" nil)
132 (const :tag "Scroll all windows showing the buffer" all)
133 (const :tag "Scroll only the selected window" this))
134 :group 'eshell-mode)
135
136 (defcustom eshell-scroll-to-bottom-on-output nil
137 "*Controls whether interpreter output causes window to scroll.
138 If nil, then do not scroll. If t or `all', scroll all windows showing
139 buffer. If `this', scroll only the selected window. If `others',
140 scroll only those that are not the selected window.
141
142 See variable `eshell-scroll-show-maximum-output' and function
143 `eshell-postoutput-scroll-to-bottom'."
144 :type '(radio (const :tag "Do not scroll Eshell windows" nil)
145 (const :tag "Scroll all windows showing the buffer" all)
146 (const :tag "Scroll only the selected window" this)
147 (const :tag "Scroll all windows other than selected" this))
148 :group 'eshell-mode)
149
150 (defcustom eshell-scroll-show-maximum-output t
151 "*Controls how interpreter output causes window to scroll.
152 If non-nil, then show the maximum output when the window is scrolled.
153
154 See variable `eshell-scroll-to-bottom-on-output' and function
155 `eshell-postoutput-scroll-to-bottom'."
156 :type 'boolean
157 :group 'eshell-mode)
158
159 (defcustom eshell-buffer-maximum-lines 1024
160 "*The maximum size in lines for eshell buffers.
161 Eshell buffers are truncated from the top to be no greater than this
162 number, if the function `eshell-truncate-buffer' is on
163 `eshell-output-filter-functions'."
164 :type 'integer
165 :group 'eshell-mode)
166
167 (defcustom eshell-output-filter-functions
168 '(eshell-postoutput-scroll-to-bottom
169 eshell-handle-control-codes
170 eshell-watch-for-password-prompt)
171 "*Functions to call before output is displayed.
172 These functions are only called for output that is displayed
173 interactively, and not for output which is redirected."
174 :type 'hook
175 :group 'eshell-mode)
176
177 (defcustom eshell-preoutput-filter-functions nil
178 "*Functions to call before output is inserted into the buffer.
179 These functions get one argument, a string containing the text to be
180 inserted. They return the string as it should be inserted."
181 :type 'hook
182 :group 'eshell-mode)
183
184 (defcustom eshell-password-prompt-regexp
185 "[Pp]ass\\(word\\|phrase\\).*:\\s *\\'"
186 "*Regexp matching prompts for passwords in the inferior process.
187 This is used by `eshell-watch-for-password-prompt'."
188 :type 'regexp
189 :group 'eshell-mode)
190
191 (defcustom eshell-skip-prompt-function nil
192 "*A function called from beginning of line to skip the prompt."
193 :type '(choice (const nil) function)
194 :group 'eshell-mode)
195
196 (defcustom eshell-status-in-modeline t
197 "*If non-nil, let the user know a command is running in the modeline."
198 :type 'boolean
199 :group 'eshell-mode)
200
201 (defvar eshell-first-time-p t
202 "A variable which is non-nil the first time Eshell is loaded.")
203
204 ;; Internal Variables:
205
206 ;; these are only set to `nil' initially for the sake of the
207 ;; byte-compiler, when compiling other files which `require' this one
208 (defvar eshell-mode nil)
209 (defvar eshell-mode-map nil)
210 (defvar eshell-command-running-string "--")
211 (defvar eshell-command-map nil)
212 (defvar eshell-command-prefix nil)
213 (defvar eshell-last-input-start nil)
214 (defvar eshell-last-input-end nil)
215 (defvar eshell-last-output-start nil)
216 (defvar eshell-last-output-block-begin nil)
217 (defvar eshell-last-output-end nil)
218
219 (defvar eshell-currently-handling-window nil)
220 (defvar eshell-mode-syntax-table nil)
221 (defvar eshell-mode-abbrev-table nil)
222
223 (define-abbrev-table 'eshell-mode-abbrev-table ())
224
225 (eval-when-compile
226 (unless (eshell-under-xemacs-p)
227 (defalias 'characterp 'ignore)
228 (defalias 'char-int 'ignore)))
229
230 (if (not eshell-mode-syntax-table)
231 (let ((i 0))
232 (setq eshell-mode-syntax-table (make-syntax-table))
233 (while (< i ?0)
234 (modify-syntax-entry i "_ " eshell-mode-syntax-table)
235 (setq i (1+ i)))
236 (setq i (1+ ?9))
237 (while (< i ?A)
238 (modify-syntax-entry i "_ " eshell-mode-syntax-table)
239 (setq i (1+ i)))
240 (setq i (1+ ?Z))
241 (while (< i ?a)
242 (modify-syntax-entry i "_ " eshell-mode-syntax-table)
243 (setq i (1+ i)))
244 (setq i (1+ ?z))
245 (while (< i 128)
246 (modify-syntax-entry i "_ " eshell-mode-syntax-table)
247 (setq i (1+ i)))
248 (modify-syntax-entry ? " " eshell-mode-syntax-table)
249 (modify-syntax-entry ?\t " " eshell-mode-syntax-table)
250 (modify-syntax-entry ?\f " " eshell-mode-syntax-table)
251 (modify-syntax-entry ?\n "> " eshell-mode-syntax-table)
252 ;; Give CR the same syntax as newline, for selective-display.
253 (modify-syntax-entry ?\^m "> " eshell-mode-syntax-table)
254 ;;; (modify-syntax-entry ?\; "< " eshell-mode-syntax-table)
255 (modify-syntax-entry ?` "' " eshell-mode-syntax-table)
256 (modify-syntax-entry ?' "' " eshell-mode-syntax-table)
257 (modify-syntax-entry ?, "' " eshell-mode-syntax-table)
258 ;; Used to be singlequote; changed for flonums.
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 (modify-syntax-entry ?\[ "(] " eshell-mode-syntax-table)
270 (modify-syntax-entry ?\] ")[ " eshell-mode-syntax-table)
271 ;; All non-word multibyte characters should be `symbol'.
272 (if (eshell-under-xemacs-p)
273 (map-char-table
274 (function
275 (lambda (key val)
276 (and (characterp key)
277 (>= (char-int key) 256)
278 (/= (char-syntax key) ?w)
279 (modify-syntax-entry key "_ "
280 eshell-mode-syntax-table))))
281 (standard-syntax-table))
282 (map-char-table
283 (function
284 (lambda (key val)
285 (and (if (consp key)
286 (and (>= (car key) 128)
287 (/= (char-syntax (car key)) ?w))
288 (and (>= key 256)
289 (/= (char-syntax key) ?w)))
290 (modify-syntax-entry key "_ "
291 eshell-mode-syntax-table))))
292 (standard-syntax-table)))))
293
294 ;;; User Functions:
295
296 ;;;###autoload
297 (defun eshell-mode ()
298 "Emacs shell interactive mode.
299
300 \\{eshell-mode-map}"
301 (kill-all-local-variables)
302
303 (setq major-mode 'eshell-mode)
304 (setq mode-name "EShell")
305 (set (make-local-variable 'eshell-mode) t)
306
307 (make-local-variable 'eshell-mode-map)
308 (setq eshell-mode-map (make-sparse-keymap))
309 (use-local-map eshell-mode-map)
310
311 (when eshell-status-in-modeline
312 (make-local-variable 'eshell-command-running-string)
313 (let ((fmt (copy-sequence mode-line-format)))
314 (make-local-variable 'mode-line-format)
315 (setq mode-line-format fmt))
316 (let ((modeline (memq 'mode-line-modified mode-line-format)))
317 (if modeline
318 (setcar modeline 'eshell-command-running-string))))
319
320 (define-key eshell-mode-map [return] 'eshell-send-input)
321 (define-key eshell-mode-map [(control ?m)] 'eshell-send-input)
322 (define-key eshell-mode-map [(control ?j)] 'eshell-send-input)
323 (define-key eshell-mode-map [(meta return)] 'eshell-queue-input)
324 (define-key eshell-mode-map [(meta control ?m)] 'eshell-queue-input)
325 (define-key eshell-mode-map [(meta control ?l)] 'eshell-show-output)
326
327 (set (make-local-variable 'eshell-command-prefix)
328 (make-symbol "eshell-command-prefix"))
329 (fset eshell-command-prefix (make-sparse-keymap))
330 (set (make-local-variable 'eshell-command-map)
331 (symbol-function eshell-command-prefix))
332 (define-key eshell-mode-map [(control ?c)] eshell-command-prefix)
333
334 ;; without this, find-tag complains about read-only text being
335 ;; modified
336 (if (eq (key-binding [(meta ?.)]) 'find-tag)
337 (define-key eshell-mode-map [(meta ?.)] 'eshell-find-tag))
338 (define-key eshell-command-map [(meta ?o)] 'eshell-mark-output)
339 (define-key eshell-command-map [(meta ?d)] 'eshell-toggle-direct-send)
340
341 (define-key eshell-command-map [(control ?a)] 'eshell-bol)
342 (define-key eshell-command-map [(control ?b)] 'eshell-backward-argument)
343 (define-key eshell-command-map [(control ?e)] 'eshell-show-maximum-output)
344 (define-key eshell-command-map [(control ?f)] 'eshell-forward-argument)
345 (define-key eshell-command-map [return] 'eshell-copy-old-input)
346 (define-key eshell-command-map [(control ?m)] 'eshell-copy-old-input)
347 (define-key eshell-command-map [(control ?o)] 'eshell-kill-output)
348 (define-key eshell-command-map [(control ?r)] 'eshell-show-output)
349 (define-key eshell-command-map [(control ?t)] 'eshell-truncate-buffer)
350 (define-key eshell-command-map [(control ?u)] 'eshell-kill-input)
351 (define-key eshell-command-map [(control ?w)] 'backward-kill-word)
352 (define-key eshell-command-map [(control ?y)] 'eshell-repeat-argument)
353
354 (setq local-abbrev-table eshell-mode-abbrev-table)
355 (set-syntax-table eshell-mode-syntax-table)
356
357 (set (make-local-variable 'dired-directory) default-directory)
358 (set (make-local-variable 'list-buffers-directory)
359 (expand-file-name default-directory))
360
361 ;; always set the tab width to 8 in Eshell buffers, since external
362 ;; commands which do their own formatting almost always expect this
363 (set (make-local-variable 'tab-width) 8)
364
365 ;; don't ever use auto-fill in Eshell buffers
366 (setq auto-fill-function nil)
367
368 ;; always display everything from a return value
369 (if (boundp 'print-length)
370 (set (make-local-variable 'print-length) nil))
371 (if (boundp 'print-level)
372 (set (make-local-variable 'print-level) nil))
373
374 ;; set require-final-newline to nil; otherwise, all redirected
375 ;; output will end with a newline, whether or not the source
376 ;; indicated it!
377 (set (make-local-variable 'require-final-newline) nil)
378
379 (set (make-local-variable 'max-lisp-eval-depth)
380 (max 3000 max-lisp-eval-depth))
381 (set (make-local-variable 'max-specpdl-size)
382 (max 6000 max-lisp-eval-depth))
383
384 (set (make-local-variable 'eshell-last-input-start) (point-marker))
385 (set (make-local-variable 'eshell-last-input-end) (point-marker))
386 (set (make-local-variable 'eshell-last-output-start) (point-marker))
387 (set (make-local-variable 'eshell-last-output-end) (point-marker))
388 (set (make-local-variable 'eshell-last-output-block-begin) (point))
389
390 (let ((modules-list (copy-sequence eshell-modules-list)))
391 (make-local-variable 'eshell-modules-list)
392 (setq eshell-modules-list modules-list))
393
394 ;; load extension modules into memory. This will cause any global
395 ;; variables they define to be visible, since some of the core
396 ;; modules sometimes take advantage of their functionality if used.
397 (eshell-for module eshell-modules-list
398 (let ((module-fullname (symbol-name module))
399 module-shortname)
400 (if (string-match "^eshell-\\(.*\\)" module-fullname)
401 (setq module-shortname
402 (concat "em-" (match-string 1 module-fullname))))
403 (unless module-shortname
404 (error "Invalid Eshell module name: %s" module-fullname))
405 (unless (featurep (intern module-shortname))
406 (load module-shortname))))
407
408 (unless (file-exists-p eshell-directory-name)
409 (eshell-make-private-directory eshell-directory-name t))
410
411 ;; load core Eshell modules for this session
412 (eshell-for module (eshell-subgroups 'eshell)
413 (run-hooks (intern-soft (concat (symbol-name module)
414 "-load-hook"))))
415
416 ;; load extension modules for this session
417 (eshell-for module eshell-modules-list
418 (let ((load-hook (intern-soft (concat (symbol-name module)
419 "-load-hook"))))
420 (if (and load-hook (boundp load-hook))
421 (run-hooks load-hook))))
422
423 (if eshell-send-direct-to-subprocesses
424 (add-hook 'pre-command-hook 'eshell-intercept-commands t t))
425
426 (if eshell-scroll-to-bottom-on-input
427 (add-hook 'pre-command-hook 'eshell-preinput-scroll-to-bottom t t))
428
429 (when eshell-scroll-show-maximum-output
430 (set (make-local-variable 'scroll-conservatively) 1000))
431
432 (when eshell-status-in-modeline
433 (add-hook 'eshell-pre-command-hook 'eshell-command-started nil t)
434 (add-hook 'eshell-post-command-hook 'eshell-command-finished nil t))
435
436 (add-hook 'kill-buffer-hook
437 (function
438 (lambda ()
439 (run-hooks 'eshell-exit-hook))) t t)
440
441 (if eshell-first-time-p
442 (run-hooks 'eshell-first-time-mode-hook))
443 (run-mode-hooks 'eshell-mode-hook)
444 (run-hooks 'eshell-post-command-hook))
445
446 (put 'eshell-mode 'mode-class 'special)
447
448 (eshell-deftest mode major-mode
449 "Major mode is correct"
450 (eq major-mode 'eshell-mode))
451
452 (eshell-deftest mode eshell-mode-variable
453 "`eshell-mode' is true"
454 (eq eshell-mode t))
455
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")))
460
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))
465
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))
470
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 "--"))))
477
478 ;;; Internal Functions:
479
480 (defun eshell-toggle-direct-send ()
481 (interactive)
482 (if eshell-send-direct-to-subprocesses
483 (progn
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")))
490
491 (defun eshell-self-insert-command (N)
492 (interactive "i")
493 (process-send-string
494 (eshell-interactive-process)
495 (char-to-string (if (symbolp last-command-char)
496 (get last-command-char 'ascii-character)
497 last-command-char))))
498
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))
505 (intercept t))
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)))))
515 (if intercept
516 (setq this-command 'eshell-self-insert-command)))))
517
518 (defun eshell-find-tag (&optional tagname next-p regexp-p)
519 "A special version of `find-tag' that ignores read-onlyness."
520 (interactive)
521 (require 'etags)
522 (let ((inhibit-read-only t)
523 (no-default (eobp))
524 (find-tag-default-function 'ignore))
525 (with-no-warnings
526 (setq tagname (car (find-tag-interactive "Find tag: "))))
527 (find-tag tagname next-p regexp-p)))
528
529 (defun eshell-move-argument (limit func property arg)
530 "Move forward ARG arguments."
531 (catch 'eshell-incomplete
532 (eshell-parse-arguments (save-excursion (eshell-bol) (point))
533 (line-end-position)))
534 (let ((pos (save-excursion
535 (funcall func 1)
536 (while (and (> arg 0) (/= (point) limit))
537 (if (get-text-property (point) property)
538 (setq arg (1- arg)))
539 (if (> arg 0)
540 (funcall func 1)))
541 (point))))
542 (goto-char pos)
543 (if (and (eq func 'forward-char)
544 (= (1+ pos) limit))
545 (forward-char 1))))
546
547 (eshell-deftest arg forward-arg
548 "Move across command arguments"
549 (eshell-insert-command "echo $(+ 1 (- 4 3)) \"alpha beta\" file" 'ignore)
550 (let ((here (point)) begin valid)
551 (eshell-bol)
552 (setq begin (point))
553 (eshell-forward-argument 4)
554 (setq valid (= here (point)))
555 (eshell-backward-argument 4)
556 (prog1
557 (and valid (= begin (point)))
558 (eshell-bol)
559 (delete-region (point) (point-max)))))
560
561 (defun eshell-forward-argument (&optional arg)
562 "Move forward ARG arguments."
563 (interactive "p")
564 (eshell-move-argument (point-max) 'forward-char 'arg-end arg))
565
566 (defun eshell-backward-argument (&optional arg)
567 "Move backward ARG arguments."
568 (interactive "p")
569 (eshell-move-argument (point-min) 'backward-char 'arg-begin arg))
570
571 (defun eshell-repeat-argument (&optional arg)
572 (interactive "p")
573 (let ((begin (save-excursion
574 (eshell-backward-argument arg)
575 (point))))
576 (kill-ring-save begin (point))
577 (yank)))
578
579 (defun eshell-bol ()
580 "Goes to the beginning of line, then skips past the prompt, if any."
581 (interactive)
582 (beginning-of-line)
583 (and eshell-skip-prompt-function
584 (funcall eshell-skip-prompt-function)))
585
586 (defsubst eshell-push-command-mark ()
587 "Push a mark at the end of the last input text."
588 (push-mark (1- eshell-last-input-end) t))
589
590 (custom-add-option 'eshell-pre-command-hook 'eshell-push-command-mark)
591
592 (defsubst eshell-goto-input-start ()
593 "Goto the start of the last command input.
594 Putting this function on `eshell-pre-command-hook' will mimic Plan 9's
595 9term behavior."
596 (goto-char eshell-last-input-start))
597
598 (custom-add-option 'eshell-pre-command-hook 'eshell-push-command-mark)
599
600 (defsubst eshell-interactive-print (string)
601 "Print STRING to the eshell display buffer."
602 (eshell-output-filter nil string))
603
604 (defsubst eshell-begin-on-new-line ()
605 "This function outputs a newline if not at beginning of line."
606 (save-excursion
607 (goto-char eshell-last-output-end)
608 (or (bolp)
609 (eshell-interactive-print "\n"))))
610
611 (defsubst eshell-reset (&optional no-hooks)
612 "Output a prompt on a new line, aborting any current input.
613 If NO-HOOKS is non-nil, then `eshell-post-command-hook' won't be run."
614 (goto-char (point-max))
615 (setq eshell-last-input-start (point-marker)
616 eshell-last-input-end (point-marker)
617 eshell-last-output-start (point-marker)
618 eshell-last-output-block-begin (point)
619 eshell-last-output-end (point-marker))
620 (eshell-begin-on-new-line)
621 (unless no-hooks
622 (run-hooks 'eshell-post-command-hook)
623 (goto-char (point-max))))
624
625 (defun eshell-parse-command-input (beg end &optional args)
626 "Parse the command input from BEG to END.
627 The difference is that `eshell-parse-command' expects a complete
628 command string (and will error if it doesn't get one), whereas this
629 function will inform the caller whether more input is required.
630
631 If nil is returned, more input is necessary (probably because a
632 multi-line input string wasn't terminated properly). Otherwise, it
633 will return the parsed command."
634 (let (delim command)
635 (if (setq delim
636 (catch 'eshell-incomplete
637 (ignore
638 (setq command (eshell-parse-command (cons beg end)
639 args t)))))
640 (ignore
641 (message "Expecting completion of delimeter %c ..."
642 (if (listp delim)
643 (car delim)
644 delim)))
645 command)))
646
647 (defun eshell-update-markers (pmark)
648 "Update the input and output markers relative to point and PMARK."
649 (set-marker eshell-last-input-start pmark)
650 (set-marker eshell-last-input-end (point))
651 (set-marker eshell-last-output-end (point)))
652
653 (defun eshell-queue-input (&optional use-region)
654 "Queue the current input text for execution by Eshell.
655 Particularly, don't send the text to the current process, even if it's
656 waiting for input."
657 (interactive "P")
658 (eshell-send-input use-region t))
659
660 (eshell-deftest mode queue-input
661 "Queue command input"
662 (eshell-insert-command "sleep 2")
663 (eshell-insert-command "echo alpha" 'eshell-queue-input)
664 (let ((count 10))
665 (while (and eshell-current-command
666 (> count 0))
667 (sit-for 1 0)
668 (setq count (1- count))))
669 (eshell-match-result "alpha\n"))
670
671 (defun eshell-send-input (&optional use-region queue-p no-newline)
672 "Send the input received to Eshell for parsing and processing.
673 After `eshell-last-output-end', sends all text from that marker to
674 point as input. Before that marker, calls `eshell-get-old-input' to
675 retrieve old input, copies it to the end of the buffer, and sends it.
676
677 If USE-REGION is non-nil, the current region (between point and mark)
678 will be used as input.
679
680 If QUEUE-P is non-nil, input will be queued until the next prompt,
681 rather than sent to the currently active process. If no process, the
682 input is processed immediately.
683
684 If NO-NEWLINE is non-nil, the input is sent without an implied final
685 newline."
686 (interactive "P")
687 ;; Note that the input string does not include its terminal newline.
688 (let ((proc-running-p (and (eshell-interactive-process)
689 (not queue-p)))
690 (inhibit-point-motion-hooks t)
691 after-change-functions)
692 (unless (and proc-running-p
693 (not (eq (process-status
694 (eshell-interactive-process)) 'run)))
695 (if (or proc-running-p
696 (>= (point) eshell-last-output-end))
697 (goto-char (point-max))
698 (let ((copy (eshell-get-old-input use-region)))
699 (goto-char eshell-last-output-end)
700 (insert-and-inherit copy)))
701 (unless (or no-newline
702 (and eshell-send-direct-to-subprocesses
703 proc-running-p))
704 (insert-before-markers-and-inherit ?\n))
705 (if proc-running-p
706 (progn
707 (eshell-update-markers eshell-last-output-end)
708 (if (or eshell-send-direct-to-subprocesses
709 (= eshell-last-input-start eshell-last-input-end))
710 (unless no-newline
711 (process-send-string (eshell-interactive-process) "\n"))
712 (process-send-region (eshell-interactive-process)
713 eshell-last-input-start
714 eshell-last-input-end)))
715 (if (= eshell-last-output-end (point))
716 (run-hooks 'eshell-post-command-hook)
717 (let (input)
718 (eshell-condition-case err
719 (progn
720 (setq input (buffer-substring-no-properties
721 eshell-last-output-end (1- (point))))
722 (run-hook-with-args 'eshell-expand-input-functions
723 eshell-last-output-end (1- (point)))
724 (let ((cmd (eshell-parse-command-input
725 eshell-last-output-end (1- (point)))))
726 (when cmd
727 (eshell-update-markers eshell-last-output-end)
728 (setq input (buffer-substring-no-properties
729 eshell-last-input-start
730 (1- eshell-last-input-end)))
731 (run-hooks 'eshell-input-filter-functions)
732 (and (catch 'eshell-terminal
733 (ignore
734 (if (eshell-invoke-directly cmd input)
735 (eval cmd)
736 (eshell-eval-command cmd input))))
737 (eshell-life-is-too-much)))))
738 (quit
739 (eshell-reset t)
740 (run-hooks 'eshell-post-command-hook)
741 (signal 'quit nil))
742 (error
743 (eshell-reset t)
744 (eshell-interactive-print
745 (concat (error-message-string err) "\n"))
746 (run-hooks 'eshell-post-command-hook)
747 (insert-and-inherit input)))))))))
748
749 ; (eshell-deftest proc send-to-subprocess
750 ; "Send input to a subprocess"
751 ; ;; jww (1999-12-06): what about when bc is unavailable?
752 ; (if (not (eshell-search-path "bc"))
753 ; t
754 ; (eshell-insert-command "bc")
755 ; (eshell-insert-command "1 + 2")
756 ; (sit-for 1 0)
757 ; (forward-line -1)
758 ; (prog1
759 ; (looking-at "3\n")
760 ; (eshell-insert-command "quit")
761 ; (sit-for 1 0))))
762
763 (defsubst eshell-kill-new ()
764 "Add the last input text to the kill ring."
765 (kill-ring-save eshell-last-input-start eshell-last-input-end))
766
767 (custom-add-option 'eshell-input-filter-functions 'eshell-kill-new)
768
769 (defun eshell-output-filter (process string)
770 "Send the output from PROCESS (STRING) to the interactive display.
771 This is done after all necessary filtering has been done."
772 (let ((oprocbuf (if process (process-buffer process)
773 (current-buffer)))
774 (inhibit-point-motion-hooks t)
775 after-change-functions)
776 (let ((functions eshell-preoutput-filter-functions))
777 (while (and functions string)
778 (setq string (funcall (car functions) string))
779 (setq functions (cdr functions))))
780 (if (and string oprocbuf (buffer-name oprocbuf))
781 (let (opoint obeg oend)
782 (with-current-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))
788 (ostart nil))
789 (widen)
790 (goto-char eshell-last-output-end)
791 (setq ostart (point))
792 (if (<= (point) opoint)
793 (setq opoint (+ opoint nchars)))
794 (if (< (point) obeg)
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)
801 (- (point) nchars)))
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)
809 (goto-char opoint)
810 (eshell-run-output-filters))))))
811
812 (defun eshell-run-output-filters ()
813 "Run the `eshell-output-filter-functions' on the current output."
814 (save-current-buffer
815 (run-hooks 'eshell-output-filter-functions))
816 (setq eshell-last-output-block-begin
817 (marker-position eshell-last-output-end)))
818
819 ;;; jww (1999-10-23): this needs testing
820 (defun eshell-preinput-scroll-to-bottom ()
821 "Go to the end of buffer in all windows showing it.
822 Movement occurs if point in the selected window is not after the
823 process mark, and `this-command' is an insertion command. Insertion
824 commands recognized are `self-insert-command', `yank', and
825 `hilit-yank'. Depends on the value of
826 `eshell-scroll-to-bottom-on-input'.
827
828 This function should be a pre-command hook."
829 (if (memq this-command '(self-insert-command yank hilit-yank))
830 (let* ((selected (selected-window))
831 (current (current-buffer))
832 (scroll eshell-scroll-to-bottom-on-input))
833 (if (< (point) eshell-last-output-end)
834 (if (eq scroll 'this)
835 (goto-char (point-max))
836 (walk-windows
837 (function
838 (lambda (window)
839 (when (and (eq (window-buffer window) current)
840 (or (eq scroll t) (eq scroll 'all)))
841 (select-window window)
842 (goto-char (point-max))
843 (select-window selected))))
844 nil t))))))
845
846 ;;; jww (1999-10-23): this needs testing
847 (defun eshell-postoutput-scroll-to-bottom ()
848 "Go to the end of buffer in all windows showing it.
849 Does not scroll if the current line is the last line in the buffer.
850 Depends on the value of `eshell-scroll-to-bottom-on-output' and
851 `eshell-scroll-show-maximum-output'.
852
853 This function should be in the list `eshell-output-filter-functions'."
854 (let* ((selected (selected-window))
855 (current (current-buffer))
856 (scroll eshell-scroll-to-bottom-on-output))
857 (unwind-protect
858 (walk-windows
859 (function
860 (lambda (window)
861 (if (eq (window-buffer window) current)
862 (progn
863 (select-window window)
864 (if (and (< (point) eshell-last-output-end)
865 (or (eq scroll t) (eq scroll 'all)
866 ;; Maybe user wants point to jump to end.
867 (and (eq scroll 'this)
868 (eq selected window))
869 (and (eq scroll 'others)
870 (not (eq selected window)))
871 ;; If point was at the end, keep it at end.
872 (>= (point) eshell-last-output-start)))
873 (goto-char eshell-last-output-end))
874 ;; Optionally scroll so that the text
875 ;; ends at the bottom of the window.
876 (if (and eshell-scroll-show-maximum-output
877 (>= (point) eshell-last-output-end))
878 (save-excursion
879 (goto-char (point-max))
880 (recenter -1)))
881 (select-window selected)))))
882 nil t)
883 (set-buffer current))))
884
885 (defun eshell-beginning-of-input ()
886 "Return the location of the start of the previous input."
887 eshell-last-input-start)
888
889 (defun eshell-beginning-of-output ()
890 "Return the location of the end of the previous output block."
891 eshell-last-input-end)
892
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))
898
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))))
907
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)))
915
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))
930
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)))
937
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))))
946
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))
955
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))))))
968
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)))
975
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"))
981
982 (defun eshell/exit ()
983 "Leave or kill the Eshell buffer, depending on `eshell-kill-on-exit'."
984 (throw 'eshell-terminal t))
985
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))))
992
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))))))))
1013
1014 (custom-add-option 'eshell-output-filter-functions
1015 'eshell-truncate-buffer)
1016
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"))))
1028
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'.
1034
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)))))
1043
1044 (custom-add-option 'eshell-output-filter-functions
1045 'eshell-watch-for-password-prompt)
1046
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))
1074 (t
1075 (forward-char))))))))
1076
1077 (custom-add-option 'eshell-output-filter-functions
1078 'eshell-handle-control-codes)
1079
1080 (defun eshell-handle-ansi-color ()
1081 "Handle ANSI color codes."
1082 (eval-and-compile (require 'ansi-color))
1083 (ansi-color-apply-on-region eshell-last-output-start
1084 eshell-last-output-end))
1085
1086 (custom-add-option 'eshell-output-filter-functions
1087 'eshell-handle-ansi-color)
1088
1089 ;;; Code:
1090
1091 ;;; arch-tag: ec65bc2b-da14-4547-81d3-a32af3a4dc57
1092 ;;; esh-mode.el ends here