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