(comint-prompt-read-only): New variable.
[emacs.git] / lisp / comint.el
blob52217fa8ad6ec480321670d6dca75fdb91aaba80
1 ;;; comint.el --- general command interpreter in a window stuff
3 ;; Copyright (C) 1988,90,92,93,94,95,96,97,98,99,2000,01,02,03,2004
4 ;; Free Software Foundation, Inc.
6 ;; Author: Olin Shivers <shivers@cs.cmu.edu>
7 ;; Simon Marshall <simon@gnu.org>
8 ;; Maintainer: FSF
9 ;; Keywords: processes
11 ;; This file is part of GNU Emacs.
13 ;; GNU Emacs is free software; you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; any later version.
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs; see the file COPYING. If not, write to the
25 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
26 ;; Boston, MA 02111-1307, USA.
28 ;;; Commentary:
30 ;; This file defines a general command-interpreter-in-a-buffer package
31 ;; (comint mode). The idea is that you can build specific process-in-a-buffer
32 ;; modes on top of comint mode -- e.g., Lisp, shell, scheme, T, soar, ....
33 ;; This way, all these specific packages share a common base functionality,
34 ;; and a common set of bindings, which makes them easier to use (and
35 ;; saves code, implementation time, etc., etc.).
37 ;; Several packages are already defined using comint mode:
38 ;; - shell.el defines a shell-in-a-buffer mode.
39 ;; - cmulisp.el defines a simple lisp-in-a-buffer mode.
41 ;; - The file cmuscheme.el defines a scheme-in-a-buffer mode.
42 ;; - The file tea.el tunes scheme and inferior-scheme modes for T.
43 ;; - The file soar.el tunes Lisp and inferior-lisp modes for Soar.
44 ;; - cmutex.el defines TeX and LaTeX modes that invoke TeX, LaTeX, BibTeX,
45 ;; previewers, and printers from within Emacs.
46 ;; - background.el allows csh-like job control inside Emacs.
47 ;; It is pretty easy to make new derived modes for other processes.
49 ;; For documentation on the functionality provided by Comint mode, and
50 ;; the hooks available for customising it, see the comments below.
51 ;; For further information on the standard derived modes (shell,
52 ;; inferior-lisp, inferior-scheme, ...), see the relevant source files.
54 ;; For hints on converting existing process modes (e.g., tex-mode,
55 ;; background, dbx, gdb, kermit, prolog, telnet) to use comint-mode
56 ;; instead of shell-mode, see the notes at the end of this file.
59 ;; Brief Command Documentation:
60 ;;============================================================================
61 ;; Comint Mode Commands: (common to all derived modes, like shell & cmulisp
62 ;; mode)
64 ;; M-p comint-previous-input Cycle backwards in input history
65 ;; M-n comint-next-input Cycle forwards
66 ;; M-r comint-previous-matching-input Previous input matching a regexp
67 ;; M-s comint-next-matching-input Next input that matches
68 ;; M-C-l comint-show-output Show last batch of process output
69 ;; RET comint-send-input
70 ;; C-d comint-delchar-or-maybe-eof Delete char unless at end of buff
71 ;; C-c C-a comint-bol-or-process-mark First time, move point to bol;
72 ;; second time, move to process-mark.
73 ;; C-c C-u comint-kill-input ^u
74 ;; C-c C-w backward-kill-word ^w
75 ;; C-c C-c comint-interrupt-subjob ^c
76 ;; C-c C-z comint-stop-subjob ^z
77 ;; C-c C-\ comint-quit-subjob ^\
78 ;; C-c C-o comint-delete-output Delete last batch of process output
79 ;; C-c C-r comint-show-output Show last batch of process output
80 ;; C-c C-l comint-dynamic-list-input-ring List input history
82 ;; Not bound by default in comint-mode (some are in shell mode)
83 ;; comint-run Run a program under comint-mode
84 ;; send-invisible Read a line w/o echo, and send to proc
85 ;; comint-dynamic-complete-filename Complete filename at point.
86 ;; comint-dynamic-list-filename-completions List completions in help buffer.
87 ;; comint-replace-by-expanded-filename Expand and complete filename at point;
88 ;; replace with expanded/completed name.
89 ;; comint-replace-by-expanded-history Expand history at point;
90 ;; replace with expanded name.
91 ;; comint-magic-space Expand history and add (a) space(s).
92 ;; comint-kill-subjob No mercy.
93 ;; comint-show-maximum-output Show as much output as possible.
94 ;; comint-continue-subjob Send CONT signal to buffer's process
95 ;; group. Useful if you accidentally
96 ;; suspend your process (with C-c C-z).
97 ;; comint-get-next-from-history Fetch successive input history lines
98 ;; comint-accumulate Combine lines to send them together
99 ;; as input.
100 ;; comint-goto-process-mark Move point to where process-mark is.
101 ;; comint-set-process-mark Set process-mark to point.
103 ;; comint-mode-hook is the Comint mode hook. Basically for your keybindings.
105 ;;; Code:
107 (require 'ring)
109 ;; Buffer Local Variables:
110 ;;============================================================================
111 ;; Comint mode buffer local variables:
112 ;; comint-prompt-regexp string comint-bol uses to match prompt
113 ;; comint-delimiter-argument-list list For delimiters and arguments
114 ;; comint-last-input-start marker Handy if inferior always echoes
115 ;; comint-last-input-end marker For comint-delete-output command
116 ;; comint-input-ring-size integer For the input history
117 ;; comint-input-ring ring mechanism
118 ;; comint-input-ring-index number ...
119 ;; comint-save-input-ring-index number ...
120 ;; comint-input-autoexpand symbol ...
121 ;; comint-input-ignoredups boolean ...
122 ;; comint-dynamic-complete-functions hook For the completion mechanism
123 ;; comint-completion-fignore list ...
124 ;; comint-file-name-chars string ...
125 ;; comint-file-name-quote-list list ...
126 ;; comint-get-old-input function Hooks for specific
127 ;; comint-input-filter-functions hook process-in-a-buffer
128 ;; comint-output-filter-functions hook function modes.
129 ;; comint-preoutput-filter-functions hook
130 ;; comint-input-filter function ...
131 ;; comint-input-sender function ...
132 ;; comint-eol-on-send boolean ...
133 ;; comint-process-echoes boolean ...
134 ;; comint-scroll-to-bottom-on-input symbol For scroll behavior
135 ;; comint-scroll-to-bottom-on-output symbol ...
136 ;; comint-scroll-show-maximum-output boolean ...
137 ;; comint-accum-marker maker For comint-accumulate
139 ;; Comint mode non-buffer local variables:
140 ;; comint-completion-addsuffix boolean/cons For file name
141 ;; comint-completion-autolist boolean completion behavior
142 ;; comint-completion-recexact boolean ...
144 (defgroup comint nil
145 "General command interpreter in a window stuff."
146 :group 'processes)
148 (defgroup comint-completion nil
149 "Completion facilities in comint"
150 :group 'comint)
152 (defgroup comint-source nil
153 "Source finding facilities in comint"
154 :prefix "comint-"
155 :group 'comint)
157 (defvar comint-prompt-regexp "^"
158 "Regexp to recognise prompts in the inferior process.
159 Defaults to \"^\", the null string at BOL.
161 This variable is only used if the variable
162 `comint-use-prompt-regexp-instead-of-fields' is non-nil.
164 Good choices:
165 Canonical Lisp: \"^[^> \\n]*>+:? *\" (Lucid, franz, kcl, T, cscheme, oaklisp)
166 Lucid Common Lisp: \"^\\\\(>\\\\|\\\\(->\\\\)+\\\\) *\"
167 franz: \"^\\\\(->\\\\|<[0-9]*>:\\\\) *\"
168 kcl: \"^>+ *\"
169 shell: \"^[^#$%>\\n]*[#$%>] *\"
170 T: \"^>+ *\"
172 This is a good thing to set in mode hooks.")
174 (defcustom comint-prompt-read-only nil
175 "If non-nil, the comint prompt is read only.
176 This does not affect existing prompts.
177 Certain derived modes may override this option."
178 :type 'boolean
179 :group 'comint
180 :version "21.4")
182 (defvar comint-delimiter-argument-list ()
183 "List of characters to recognise as separate arguments in input.
184 Strings comprising a character in this list will separate the arguments
185 surrounding them, and also be regarded as arguments in their own right (unlike
186 whitespace). See `comint-arguments'.
187 Defaults to the empty list.
189 For shells, a good value is (?\\| ?& ?< ?> ?\\( ?\\) ?;).
191 This is a good thing to set in mode hooks.")
193 (defcustom comint-input-autoexpand nil
194 "*If non-nil, expand input command history references on completion.
195 This mirrors the optional behavior of tcsh (its autoexpand and histlist).
197 If the value is `input', then the expansion is seen on input.
198 If the value is `history', then the expansion is only when inserting
199 into the buffer's input ring. See also `comint-magic-space' and
200 `comint-dynamic-complete'.
202 This variable is buffer-local."
203 :type '(choice (const :tag "off" nil)
204 (const input)
205 (const history)
206 (other :tag "on" t))
207 :group 'comint)
209 (defface comint-highlight-input '((t (:weight bold)))
210 "Face to use to highlight user input."
211 :group 'comint)
213 (defface comint-highlight-prompt
214 '((((background dark)) (:foreground "cyan"))
215 (t (:foreground "dark blue")))
216 "Face to use to highlight prompts."
217 :group 'comint)
219 (defcustom comint-input-ignoredups nil
220 "*If non-nil, don't add input matching the last on the input ring.
221 This mirrors the optional behavior of bash.
223 This variable is buffer-local."
224 :type 'boolean
225 :group 'comint)
227 (defcustom comint-input-ring-file-name nil
228 "*If non-nil, name of the file to read/write input history.
229 See also `comint-read-input-ring' and `comint-write-input-ring'.
231 This variable is buffer-local, and is a good thing to set in mode hooks."
232 :type '(choice (const :tag "nil" nil)
233 file)
234 :group 'comint)
236 (defcustom comint-scroll-to-bottom-on-input nil
237 "*Controls whether input to interpreter causes window to scroll.
238 If nil, then do not scroll. If t or `all', scroll all windows showing buffer.
239 If `this', scroll only the selected window.
241 The default is nil.
243 See `comint-preinput-scroll-to-bottom'. This variable is buffer-local."
244 :type '(choice (const :tag "off" nil)
245 (const t)
246 (const all)
247 (const this))
248 :group 'comint)
250 (defcustom comint-move-point-for-output nil
251 "*Controls whether interpreter output moves point to the end of the output.
252 If nil, then output never moves point to the output.
253 (If the output occurs at point, it is inserted before point.)
254 If t or `all', move point in all windows showing the buffer.
255 If `this', move point only the selected window.
256 If `others', move point only in other windows, not in the selected window.
258 The default is nil.
260 See the variable `comint-scroll-show-maximum-output' and the function
261 `comint-postoutput-scroll-to-bottom'.
262 This variable is buffer-local in all Comint buffers."
263 :type '(choice (const :tag "off" nil)
264 (const t)
265 (const all)
266 (const this)
267 (const others))
268 :group 'comint)
270 (defvaralias 'comint-scroll-to-bottom-on-output 'comint-move-point-for-output)
272 (defcustom comint-scroll-show-maximum-output t
273 "*Controls how to scroll due to interpreter output.
274 This variable applies when point is at the end of the buffer
275 \(either because it was originally there, or because
276 `comint-move-point-for-output' said to move it there)
277 and output from the subprocess is inserted.
279 Non-nil means scroll so that the window is full of text
280 and point is on the last line. A value of nil
281 means don't do anything special--scroll normally.
283 See also the variable `comint-move-point-for-output' and the function
284 `comint-postoutput-scroll-to-bottom'.
285 This variable is buffer-local in all Comint buffers."
286 :type 'boolean
287 :group 'comint)
289 (defcustom comint-buffer-maximum-size 1024
290 "*The maximum size in lines for Comint buffers.
291 Comint buffers are truncated from the top to be no greater than this number, if
292 the function `comint-truncate-buffer' is on `comint-output-filter-functions'."
293 :type 'integer
294 :group 'comint)
296 (defvar comint-input-ring-size 32
297 "Size of input history ring.")
299 (defvar comint-input-ring-separator "\n"
300 "Separator between commands in the history file.")
302 (defvar comint-input-history-ignore "^#"
303 "Regexp for history entries that should be ignored when Comint initializes.")
305 (defcustom comint-process-echoes nil
306 "*If non-nil, assume that the subprocess echoes any input.
307 If so, delete one copy of the input so that only one copy eventually
308 appears in the buffer.
310 This variable is buffer-local."
311 :type 'boolean
312 :group 'comint)
314 ;; AIX puts the name of the person being su'd to in front of the prompt.
315 ;; kinit prints a prompt like `Password for devnull@GNU.ORG: '.
316 ;; ksu prints a prompt like `Kerberos password for devnull/root@GNU.ORG: '.
317 ;; ssh-add prints a prompt like `Enter passphrase: '.
318 ;; Some implementations of passwd use "Password (again)" as the 2nd prompt.
319 (defcustom comint-password-prompt-regexp
320 "\\(\\([Oo]ld \\|[Nn]ew \\|'s \\|login \\|\
321 Kerberos \\|CVS \\|UNIX \\| SMB \\|^\\)\
322 \[Pp]assword\\( (again)\\)?\\|\
323 pass phrase\\|\\(Enter\\|Repeat\\) passphrase\\)\
324 \\( for [^:]+\\)?:\\s *\\'"
325 "*Regexp matching prompts for passwords in the inferior process.
326 This is used by `comint-watch-for-password-prompt'."
327 :type 'regexp
328 :group 'comint)
330 ;; Here are the per-interpreter hooks.
331 (defvar comint-get-old-input (function comint-get-old-input-default)
332 "Function that returns old text in Comint mode.
333 This function is called when return is typed while the point is in old
334 text. It returns the text to be submitted as process input. The
335 default is `comint-get-old-input-default', which either grabs the
336 current input field or grabs the current line and strips off leading
337 text matching `comint-prompt-regexp', depending on the value of
338 `comint-use-prompt-regexp-instead-of-fields'.")
340 (defvar comint-dynamic-complete-functions
341 '(comint-replace-by-expanded-history comint-dynamic-complete-filename)
342 "List of functions called to perform completion.
343 Functions should return non-nil if completion was performed.
344 See also `comint-dynamic-complete'.
346 This is a good thing to set in mode hooks.")
348 (defvar comint-input-filter
349 (function (lambda (str) (not (string-match "\\`\\s *\\'" str))))
350 "Predicate for filtering additions to input history.
351 Takes one argument, the input. If non-nil, the input may be saved on the input
352 history list. Default is to save anything that isn't all whitespace.")
354 (defvar comint-input-filter-functions '()
355 "Functions to call before input is sent to the process.
356 These functions get one argument, a string containing the text to send.
358 You can use `add-hook' to add functions to this list
359 either globally or locally.")
361 (defvar comint-output-filter-functions '(comint-postoutput-scroll-to-bottom)
362 "Functions to call after output is inserted into the buffer.
363 One possible function is `comint-postoutput-scroll-to-bottom'.
364 These functions get one argument, a string containing the text as originally
365 inserted. Note that this might not be the same as the buffer contents between
366 `comint-last-output-start' and the buffer's `process-mark', if other filter
367 functions have already modified the buffer.
369 See also `comint-preoutput-filter-functions'.
371 You can use `add-hook' to add functions to this list
372 either globally or locally.")
374 (defvar comint-input-sender-no-newline nil
375 "Non-nil directs the `comint-input-sender' function not to send a newline.")
377 (defvar comint-input-sender (function comint-simple-send)
378 "Function to actually send to PROCESS the STRING submitted by user.
379 Usually this is just `comint-simple-send', but if your mode needs to
380 massage the input string, put a different function here.
381 `comint-simple-send' just sends the string plus a newline.
382 \(If `comint-input-sender-no-newline' is non-nil, it omits the newline.)
383 This is called from the user command `comint-send-input'.")
385 (defcustom comint-eol-on-send t
386 "*Non-nil means go to the end of the line before sending input.
387 See `comint-send-input'."
388 :type 'boolean
389 :group 'comint)
391 ;; Note: If it is decided to purge comint-prompt-regexp from the source
392 ;; entirely, searching for uses of this variable will help to identify
393 ;; places that need attention.
394 (defcustom comint-use-prompt-regexp-instead-of-fields nil
395 "*If non-nil, use `comint-prompt-regexp' to distinguish prompts from user-input.
396 If nil, then program output and user-input are given different `field'
397 properties, which emacs commands can use to distinguish them (in
398 particular, common movement commands such as begining-of-line respect
399 field boundaries in a natural way)."
400 :type 'boolean
401 :group 'comint)
403 (defcustom comint-mode-hook '(turn-on-font-lock)
404 "Hook run upon entry to `comint-mode'.
405 This is run before the process is cranked up."
406 :type 'hook
407 :group 'comint)
409 (defcustom comint-exec-hook '()
410 "Hook run each time a process is exec'd by `comint-exec'.
411 This is called after the process is cranked up. It is useful for things that
412 must be done each time a process is executed in a Comint mode buffer (e.g.,
413 `(process-kill-without-query)'). In contrast, the `comint-mode-hook' is only
414 executed once when the buffer is created."
415 :type 'hook
416 :group 'comint)
418 (defvar comint-mode-map nil)
420 ;; Fixme: Is this still relevant?
421 (defvar comint-ptyp t
422 "Non-nil if communications via pty; false if by pipe. Buffer local.
423 This is to work around a bug in Emacs process signaling.")
425 (defvar comint-input-ring nil)
426 (defvar comint-last-input-start nil)
427 (defvar comint-last-input-end nil)
428 (defvar comint-last-output-start nil)
429 (defvar comint-input-ring-index nil
430 "Index of last matched history element.")
431 (defvar comint-matching-input-from-input-string ""
432 "Input previously used to match input history.")
433 (defvar comint-save-input-ring-index
434 "Last input ring index which you copied.
435 This is to support the command \\[comint-get-next-from-history].")
437 (defvar comint-accum-marker nil
438 "Non-nil if you are accumulating input lines to send as input together.
439 The command \\[comint-accumulate] sets this.")
441 (put 'comint-replace-by-expanded-history 'menu-enable 'comint-input-autoexpand)
442 (put 'comint-input-ring 'permanent-local t)
443 (put 'comint-input-ring-index 'permanent-local t)
444 (put 'comint-save-input-ring-index 'permanent-local t)
445 (put 'comint-input-autoexpand 'permanent-local t)
446 (put 'comint-input-filter-functions 'permanent-local t)
447 (put 'comint-output-filter-functions 'permanent-local t)
448 (put 'comint-preoutput-filter-functions 'permanent-local t)
449 (put 'comint-scroll-to-bottom-on-input 'permanent-local t)
450 (put 'comint-move-point-for-output 'permanent-local t)
451 (put 'comint-scroll-show-maximum-output 'permanent-local t)
452 (put 'comint-ptyp 'permanent-local t)
454 (put 'comint-mode 'mode-class 'special)
456 (define-derived-mode comint-mode fundamental-mode "Comint"
457 "Major mode for interacting with an inferior interpreter.
458 Interpreter name is same as buffer name, sans the asterisks.
459 Return at end of buffer sends line as input.
460 Return not at end copies rest of line to end and sends it.
461 Setting variable `comint-eol-on-send' means jump to the end of the line
462 before submitting new input.
464 This mode is customised to create major modes such as Inferior Lisp
465 mode, Shell mode, etc. This can be done by setting the hooks
466 `comint-input-filter-functions', `comint-input-filter', `comint-input-sender'
467 and `comint-get-old-input' to appropriate functions, and the variable
468 `comint-prompt-regexp' to the appropriate regular expression.
470 An input history is maintained of size `comint-input-ring-size', and
471 can be accessed with the commands \\[comint-next-input], \\[comint-previous-input], and \\[comint-dynamic-list-input-ring].
472 Input ring history expansion can be achieved with the commands
473 \\[comint-replace-by-expanded-history] or \\[comint-magic-space].
474 Input ring expansion is controlled by the variable `comint-input-autoexpand',
475 and addition is controlled by the variable `comint-input-ignoredups'.
477 Commands with no default key bindings include `send-invisible',
478 `comint-dynamic-complete', `comint-dynamic-list-filename-completions', and
479 `comint-magic-space'.
481 Input to, and output from, the subprocess can cause the window to scroll to
482 the end of the buffer. See variables `comint-output-filter-functions',
483 `comint-preoutput-filter-functions', `comint-scroll-to-bottom-on-input',
484 and `comint-move-point-for-output'.
486 If you accidentally suspend your process, use \\[comint-continue-subjob]
487 to continue it.
489 \\{comint-mode-map}
491 Entry to this mode runs the hooks on `comint-mode-hook'."
492 (setq mode-line-process '(":%s"))
493 (set (make-local-variable 'comint-last-input-start) (point-min-marker))
494 (set (make-local-variable 'comint-last-input-end) (point-min-marker))
495 (set (make-local-variable 'comint-last-output-start) (make-marker))
496 (make-local-variable 'comint-last-prompt-overlay)
497 (make-local-variable 'comint-prompt-regexp) ; Don't set; default
498 (make-local-variable 'comint-input-ring-size) ; ...to global val.
499 (make-local-variable 'comint-input-ring)
500 (make-local-variable 'comint-input-ring-file-name)
501 (or (and (boundp 'comint-input-ring) comint-input-ring)
502 (setq comint-input-ring (make-ring comint-input-ring-size)))
503 (make-local-variable 'comint-input-ring-index)
504 (make-local-variable 'comint-save-input-ring-index)
505 (or (and (boundp 'comint-input-ring-index) comint-input-ring-index)
506 (setq comint-input-ring-index nil))
507 (or (and (boundp 'comint-save-input-ring-index) comint-save-input-ring-index)
508 (setq comint-save-input-ring-index nil))
509 (make-local-variable 'comint-matching-input-from-input-string)
510 (make-local-variable 'comint-input-autoexpand)
511 (make-local-variable 'comint-input-ignoredups)
512 (make-local-variable 'comint-delimiter-argument-list)
513 (make-local-variable 'comint-completion-fignore)
514 (make-local-variable 'comint-get-old-input)
515 (make-local-variable 'comint-input-filter)
516 (make-local-variable 'comint-input-sender)
517 (make-local-variable 'comint-eol-on-send)
518 (make-local-variable 'comint-scroll-to-bottom-on-input)
519 (make-local-variable 'comint-move-point-for-output)
520 (make-local-variable 'comint-scroll-show-maximum-output)
521 ;; This makes it really work to keep point at the bottom.
522 (make-local-variable 'scroll-conservatively)
523 (setq scroll-conservatively 10000)
524 (add-hook 'pre-command-hook 'comint-preinput-scroll-to-bottom t t)
525 (make-local-variable 'comint-ptyp)
526 (make-local-variable 'comint-process-echoes)
527 (make-local-variable 'comint-file-name-chars)
528 (make-local-variable 'comint-file-name-quote-list)
529 (set (make-local-variable 'comint-accum-marker) (make-marker))
530 (add-hook 'change-major-mode-hook 'font-lock-defontify nil t)
531 ;; This behavior is not useful in comint buffers, and is annoying
532 (set (make-local-variable 'next-line-add-newlines) nil))
534 (if comint-mode-map
536 ;; Keys:
537 (setq comint-mode-map (make-sparse-keymap))
538 (define-key comint-mode-map "\ep" 'comint-previous-input)
539 (define-key comint-mode-map "\en" 'comint-next-input)
540 (define-key comint-mode-map [C-up] 'comint-previous-input)
541 (define-key comint-mode-map [C-down] 'comint-next-input)
542 (define-key comint-mode-map "\er" 'comint-previous-matching-input)
543 (define-key comint-mode-map "\es" 'comint-next-matching-input)
544 (define-key comint-mode-map [?\C-c ?\M-r] 'comint-previous-matching-input-from-input)
545 (define-key comint-mode-map [?\C-c ?\M-s] 'comint-next-matching-input-from-input)
546 (define-key comint-mode-map "\e\C-l" 'comint-show-output)
547 (define-key comint-mode-map "\C-m" 'comint-send-input)
548 (define-key comint-mode-map "\C-d" 'comint-delchar-or-maybe-eof)
549 (define-key comint-mode-map "\C-c " 'comint-accumulate)
550 (define-key comint-mode-map "\C-c\C-x" 'comint-get-next-from-history)
551 (define-key comint-mode-map "\C-c\C-a" 'comint-bol-or-process-mark)
552 (define-key comint-mode-map "\C-c\C-u" 'comint-kill-input)
553 (define-key comint-mode-map "\C-c\C-w" 'backward-kill-word)
554 (define-key comint-mode-map "\C-c\C-c" 'comint-interrupt-subjob)
555 (define-key comint-mode-map "\C-c\C-z" 'comint-stop-subjob)
556 (define-key comint-mode-map "\C-c\C-\\" 'comint-quit-subjob)
557 (define-key comint-mode-map "\C-c\C-m" 'comint-copy-old-input)
558 (define-key comint-mode-map "\C-c\C-o" 'comint-delete-output)
559 (define-key comint-mode-map "\C-c\C-r" 'comint-show-output)
560 (define-key comint-mode-map "\C-c\C-e" 'comint-show-maximum-output)
561 (define-key comint-mode-map "\C-c\C-l" 'comint-dynamic-list-input-ring)
562 (define-key comint-mode-map "\C-c\C-n" 'comint-next-prompt)
563 (define-key comint-mode-map "\C-c\C-p" 'comint-previous-prompt)
564 (define-key comint-mode-map "\C-c\C-d" 'comint-send-eof)
565 (define-key comint-mode-map "\C-c\C-s" 'comint-write-output)
566 (define-key comint-mode-map "\C-c." 'comint-insert-previous-argument)
567 ;; Mouse Buttons:
568 (define-key comint-mode-map [mouse-2] 'comint-insert-clicked-input)
569 ;; Menu bars:
570 ;; completion:
571 (define-key comint-mode-map [menu-bar completion]
572 (cons "Complete" (make-sparse-keymap "Complete")))
573 (define-key comint-mode-map [menu-bar completion complete-expand]
574 '("Expand File Name" . comint-replace-by-expanded-filename))
575 (define-key comint-mode-map [menu-bar completion complete-listing]
576 '("File Completion Listing" . comint-dynamic-list-filename-completions))
577 (define-key comint-mode-map [menu-bar completion complete-file]
578 '("Complete File Name" . comint-dynamic-complete-filename))
579 (define-key comint-mode-map [menu-bar completion complete]
580 '("Complete Before Point" . comint-dynamic-complete))
581 ;; Input history:
582 (define-key comint-mode-map [menu-bar inout]
583 (cons "In/Out" (make-sparse-keymap "In/Out")))
584 (define-key comint-mode-map [menu-bar inout delete-output]
585 '("Delete Current Output Group" . comint-delete-output))
586 (define-key comint-mode-map [menu-bar inout append-output-to-file]
587 '("Append Current Output Group to File" . comint-append-output-to-file))
588 (define-key comint-mode-map [menu-bar inout write-output]
589 '("Write Current Output Group to File" . comint-write-output))
590 (define-key comint-mode-map [menu-bar inout next-prompt]
591 '("Forward Output Group" . comint-next-prompt))
592 (define-key comint-mode-map [menu-bar inout previous-prompt]
593 '("Backward Output Group" . comint-previous-prompt))
594 (define-key comint-mode-map [menu-bar inout show-maximum-output]
595 '("Show Maximum Output" . comint-show-maximum-output))
596 (define-key comint-mode-map [menu-bar inout show-output]
597 '("Show Current Output Group" . comint-show-output))
598 (define-key comint-mode-map [menu-bar inout kill-input]
599 '("Kill Current Input" . comint-kill-input))
600 (define-key comint-mode-map [menu-bar inout copy-input]
601 '("Copy Old Input" . comint-copy-old-input))
602 (define-key comint-mode-map [menu-bar inout forward-matching-history]
603 '("Forward Matching Input..." . comint-forward-matching-input))
604 (define-key comint-mode-map [menu-bar inout backward-matching-history]
605 '("Backward Matching Input..." . comint-backward-matching-input))
606 (define-key comint-mode-map [menu-bar inout next-matching-history]
607 '("Next Matching Input..." . comint-next-matching-input))
608 (define-key comint-mode-map [menu-bar inout previous-matching-history]
609 '("Previous Matching Input..." . comint-previous-matching-input))
610 (define-key comint-mode-map [menu-bar inout next-matching-history-from-input]
611 '("Next Matching Current Input" . comint-next-matching-input-from-input))
612 (define-key comint-mode-map [menu-bar inout previous-matching-history-from-input]
613 '("Previous Matching Current Input" . comint-previous-matching-input-from-input))
614 (define-key comint-mode-map [menu-bar inout next-history]
615 '("Next Input" . comint-next-input))
616 (define-key comint-mode-map [menu-bar inout previous-history]
617 '("Previous Input" . comint-previous-input))
618 (define-key comint-mode-map [menu-bar inout list-history]
619 '("List Input History" . comint-dynamic-list-input-ring))
620 (define-key comint-mode-map [menu-bar inout expand-history]
621 '("Expand History Before Point" . comint-replace-by-expanded-history))
622 ;; Signals
623 (define-key comint-mode-map [menu-bar signals]
624 (cons "Signals" (make-sparse-keymap "Signals")))
625 (define-key comint-mode-map [menu-bar signals eof]
626 '("EOF" . comint-send-eof))
627 (define-key comint-mode-map [menu-bar signals kill]
628 '("KILL" . comint-kill-subjob))
629 (define-key comint-mode-map [menu-bar signals quit]
630 '("QUIT" . comint-quit-subjob))
631 (define-key comint-mode-map [menu-bar signals cont]
632 '("CONT" . comint-continue-subjob))
633 (define-key comint-mode-map [menu-bar signals stop]
634 '("STOP" . comint-stop-subjob))
635 (define-key comint-mode-map [menu-bar signals break]
636 '("BREAK" . comint-interrupt-subjob))
637 ;; Put them in the menu bar:
638 (setq menu-bar-final-items (append '(completion inout signals)
639 menu-bar-final-items))
642 (defun comint-check-proc (buffer)
643 "Return t if there is a living process associated w/buffer BUFFER.
644 Living means the status is `open', `run', or `stop'.
645 BUFFER can be either a buffer or the name of one."
646 (let ((proc (get-buffer-process buffer)))
647 (and proc (memq (process-status proc) '(open run stop)))))
649 ;;;###autoload
650 (defun make-comint-in-buffer (name buffer program &optional startfile &rest switches)
651 "Make a Comint process NAME in BUFFER, running PROGRAM.
652 If BUFFER is nil, it defaults to NAME surrounded by `*'s.
653 PROGRAM should be either a string denoting an executable program to create
654 via `start-process', or a cons pair of the form (HOST . SERVICE) denoting a TCP
655 connection to be opened via `open-network-stream'. If there is already a
656 running process in that buffer, it is not restarted. Optional third arg
657 STARTFILE is the name of a file to send the contents of to the process.
659 If PROGRAM is a string, any more args are arguments to PROGRAM."
660 (or (fboundp 'start-process)
661 (error "Multi-processing is not supported for this system"))
662 (setq buffer (get-buffer-create (or buffer (concat "*" name "*"))))
663 ;; If no process, or nuked process, crank up a new one and put buffer in
664 ;; comint mode. Otherwise, leave buffer and existing process alone.
665 (unless (comint-check-proc buffer)
666 (with-current-buffer buffer
667 (unless (derived-mode-p 'comint-mode)
668 (comint-mode))) ; Install local vars, mode, keymap, ...
669 (comint-exec buffer name program startfile switches))
670 buffer)
672 ;;;###autoload
673 (defun make-comint (name program &optional startfile &rest switches)
674 "Make a Comint process NAME in a buffer, running PROGRAM.
675 The name of the buffer is made by surrounding NAME with `*'s.
676 PROGRAM should be either a string denoting an executable program to create
677 via `start-process', or a cons pair of the form (HOST . SERVICE) denoting a TCP
678 connection to be opened via `open-network-stream'. If there is already a
679 running process in that buffer, it is not restarted. Optional third arg
680 STARTFILE is the name of a file to send the contents of the process to.
682 If PROGRAM is a string, any more args are arguments to PROGRAM."
683 (apply #'make-comint-in-buffer name nil program startfile switches))
685 ;;;###autoload
686 (defun comint-run (program)
687 "Run PROGRAM in a Comint buffer and switch to it.
688 The buffer name is made by surrounding the file name of PROGRAM with `*'s.
689 The file name is used to make a symbol name, such as `comint-sh-hook', and any
690 hooks on this symbol are run in the buffer.
691 See `make-comint' and `comint-exec'."
692 (interactive "sRun program: ")
693 (let ((name (file-name-nondirectory program)))
694 (switch-to-buffer (make-comint name program))
695 (run-hooks (intern-soft (concat "comint-" name "-hook")))))
697 (defun comint-exec (buffer name command startfile switches)
698 "Start up a process named NAME in buffer BUFFER for Comint modes.
699 Runs the given COMMAND with SWITCHES with output to STARTFILE.
700 Blasts any old process running in the buffer. Doesn't set the buffer mode.
701 You can use this to cheaply run a series of processes in the same Comint
702 buffer. The hook `comint-exec-hook' is run after each exec."
703 (with-current-buffer buffer
704 (let ((proc (get-buffer-process buffer))) ; Blast any old process.
705 (if proc (delete-process proc)))
706 ;; Crank up a new process
707 (let ((proc
708 (if (consp command)
709 (open-network-stream name buffer (car command) (cdr command))
710 (comint-exec-1 name buffer command switches))))
711 (set-process-filter proc 'comint-output-filter)
712 (make-local-variable 'comint-ptyp)
713 (setq comint-ptyp process-connection-type) ; t if pty, nil if pipe.
714 ;; Jump to the end, and set the process mark.
715 (goto-char (point-max))
716 (set-marker (process-mark proc) (point))
717 ;; Feed it the startfile.
718 (cond (startfile
719 ;;This is guaranteed to wait long enough
720 ;;but has bad results if the comint does not prompt at all
721 ;; (while (= size (buffer-size))
722 ;; (sleep-for 1))
723 ;;I hope 1 second is enough!
724 (sleep-for 1)
725 (goto-char (point-max))
726 (insert-file-contents startfile)
727 (setq startfile (buffer-substring (point) (point-max)))
728 (delete-region (point) (point-max))
729 (comint-send-string proc startfile)))
730 (run-hooks 'comint-exec-hook)
731 buffer)))
733 ;; This auxiliary function cranks up the process for comint-exec in
734 ;; the appropriate environment.
736 (defun comint-exec-1 (name buffer command switches)
737 (let ((process-environment
738 (nconc
739 ;; If using termcap, we specify `emacs' as the terminal type
740 ;; because that lets us specify a width.
741 ;; If using terminfo, we specify `dumb' because that is
742 ;; a defined terminal type. `emacs' is not a defined terminal type
743 ;; and there is no way for us to define it here.
744 ;; Some programs that use terminfo get very confused
745 ;; if TERM is not a valid terminal type.
746 ;; ;; There is similar code in compile.el.
747 (if (and (boundp 'system-uses-terminfo) system-uses-terminfo)
748 (list "TERM=dumb" "TERMCAP="
749 (format "COLUMNS=%d" (window-width)))
750 (list "TERM=emacs"
751 (format "TERMCAP=emacs:co#%d:tc=unknown:" (window-width))))
752 (if (getenv "EMACS") nil (list "EMACS=t"))
753 process-environment))
754 (default-directory
755 (if (file-accessible-directory-p default-directory)
756 default-directory
757 "/"))
758 proc decoding encoding changed)
759 (let ((exec-path (if (file-name-directory command)
760 ;; If the command has slashes, make sure we
761 ;; first look relative to the current directory.
762 (cons default-directory exec-path) exec-path)))
763 (setq proc (apply 'start-process name buffer command switches)))
764 (let ((coding-systems (process-coding-system proc)))
765 (setq decoding (car coding-systems)
766 encoding (cdr coding-systems)))
767 ;; If start-process decided to use some coding system for decoding
768 ;; data sent from the process and the coding system doesn't
769 ;; specify EOL conversion, we had better convert CRLF to LF.
770 (if (vectorp (coding-system-eol-type decoding))
771 (setq decoding (coding-system-change-eol-conversion decoding 'dos)
772 changed t))
773 ;; Even if start-process left the coding system for encoding data
774 ;; sent from the process undecided, we had better use the same one
775 ;; as what we use for decoding. But, we should suppress EOL
776 ;; conversion.
777 (if (and decoding (not encoding))
778 (setq encoding (coding-system-change-eol-conversion decoding 'unix)
779 changed t))
780 (if changed
781 (set-process-coding-system proc decoding encoding))
782 proc))
785 (defun comint-insert-clicked-input (event)
786 "In a Comint buffer, set the current input to the clicked-on previous input."
787 (interactive "e")
788 (let ((pos (posn-point (event-end event))))
789 (if (not (eq (get-char-property pos 'field) 'input))
790 ;; No input at POS, fall back to the global definition.
791 (let* ((keys (this-command-keys))
792 (last-key (and (vectorp keys) (aref keys (1- (length keys)))))
793 (fun (and last-key (lookup-key global-map (vector last-key)))))
794 (and fun (call-interactively fun)))
795 ;; There's previous input at POS, insert it at the end of the buffer.
796 (goto-char (point-max))
797 ;; First delete any old unsent input at the end
798 (delete-region
799 (or (marker-position comint-accum-marker)
800 (process-mark (get-buffer-process (current-buffer))))
801 (point))
802 ;; Insert the clicked-upon input
803 (insert (buffer-substring-no-properties
804 (previous-single-char-property-change (1+ pos) 'field)
805 (next-single-char-property-change pos 'field))))))
809 ;; Input history processing in a buffer
810 ;; ===========================================================================
811 ;; Useful input history functions, courtesy of the Ergo group.
813 ;; Eleven commands:
814 ;; comint-dynamic-list-input-ring List history in help buffer.
815 ;; comint-previous-input Previous input...
816 ;; comint-previous-matching-input ...matching a string.
817 ;; comint-previous-matching-input-from-input ... matching the current input.
818 ;; comint-next-input Next input...
819 ;; comint-next-matching-input ...matching a string.
820 ;; comint-next-matching-input-from-input ... matching the current input.
821 ;; comint-backward-matching-input Backwards input...
822 ;; comint-forward-matching-input ...matching a string.
823 ;; comint-replace-by-expanded-history Expand history at point;
824 ;; replace with expanded history.
825 ;; comint-magic-space Expand history and insert space.
827 ;; Three functions:
828 ;; comint-read-input-ring Read into comint-input-ring...
829 ;; comint-write-input-ring Write to comint-input-ring-file-name.
830 ;; comint-replace-by-expanded-history-before-point Workhorse function.
832 (defun comint-read-input-ring (&optional silent)
833 "Set the buffer's `comint-input-ring' from a history file.
834 The name of the file is given by the variable `comint-input-ring-file-name'.
835 The history ring is of size `comint-input-ring-size', regardless of file size.
836 If `comint-input-ring-file-name' is nil this function does nothing.
838 If the optional argument SILENT is non-nil, we say nothing about a
839 failure to read the history file.
841 This function is useful for major mode commands and mode hooks.
843 The commands stored in the history file are separated by the
844 `comint-input-ring-separator', and entries that match
845 `comint-input-history-ignore' are ignored. The most recent command
846 comes last.
848 See also `comint-input-ignoredups' and `comint-write-input-ring'."
849 (cond ((or (null comint-input-ring-file-name)
850 (equal comint-input-ring-file-name ""))
851 nil)
852 ((not (file-readable-p comint-input-ring-file-name))
853 (or silent
854 (message "Cannot read history file %s"
855 comint-input-ring-file-name)))
857 (let* ((history-buf (get-buffer-create " *temp*"))
858 (file comint-input-ring-file-name)
859 (count 0)
860 (size comint-input-ring-size)
861 (ring (make-ring size)))
862 (unwind-protect
863 (save-excursion
864 (set-buffer history-buf)
865 (widen)
866 (erase-buffer)
867 (insert-file-contents file)
868 ;; Save restriction in case file is already visited...
869 ;; Watch for those date stamps in history files!
870 (goto-char (point-max))
871 (let (start end history)
872 (while (and (< count comint-input-ring-size)
873 (re-search-backward comint-input-ring-separator nil t)
874 (setq end (match-beginning 0)))
875 (if (re-search-backward comint-input-ring-separator nil t)
876 (setq start (match-end 0))
877 (setq start (point-min)))
878 (setq history (buffer-substring start end))
879 (goto-char start)
880 (if (and (not (string-match comint-input-history-ignore history))
881 (or (null comint-input-ignoredups)
882 (ring-empty-p ring)
883 (not (string-equal (ring-ref ring 0) history))))
884 (progn
885 (ring-insert-at-beginning ring history)
886 (setq count (1+ count)))))))
887 (kill-buffer history-buf))
888 (setq comint-input-ring ring
889 comint-input-ring-index nil)))))
891 (defun comint-write-input-ring ()
892 "Writes the buffer's `comint-input-ring' to a history file.
893 The name of the file is given by the variable `comint-input-ring-file-name'.
894 The original contents of the file are lost if `comint-input-ring' is not empty.
895 If `comint-input-ring-file-name' is nil this function does nothing.
897 Useful within process sentinels.
899 See also `comint-read-input-ring'."
900 (cond ((or (null comint-input-ring-file-name)
901 (equal comint-input-ring-file-name "")
902 (null comint-input-ring) (ring-empty-p comint-input-ring))
903 nil)
904 ((not (file-writable-p comint-input-ring-file-name))
905 (message "Cannot write history file %s" comint-input-ring-file-name))
907 (let* ((history-buf (get-buffer-create " *Temp Input History*"))
908 (ring comint-input-ring)
909 (file comint-input-ring-file-name)
910 (index (ring-length ring)))
911 ;; Write it all out into a buffer first. Much faster, but messier,
912 ;; than writing it one line at a time.
913 (save-excursion
914 (set-buffer history-buf)
915 (erase-buffer)
916 (while (> index 0)
917 (setq index (1- index))
918 (insert (ring-ref ring index) comint-input-ring-separator))
919 (write-region (buffer-string) nil file nil 'no-message)
920 (kill-buffer nil))))))
923 (defvar comint-dynamic-list-input-ring-window-conf)
925 (defun comint-dynamic-list-input-ring-select ()
926 "Choose the input history entry that point is in or next to."
927 (interactive)
928 (let (beg end completion (buffer completion-reference-buffer)
929 (base-size completion-base-size))
930 (if (and (not (eobp)) (get-text-property (point) 'mouse-face))
931 (setq end (point) beg (1+ (point))))
932 (if (and (not (bobp)) (get-text-property (1- (point)) 'mouse-face))
933 (setq end (1- (point)) beg (point)))
934 (if (null beg)
935 (error "No history entry here"))
936 (setq beg (previous-single-property-change beg 'mouse-face))
937 (setq end (or (next-single-property-change end 'mouse-face) (point-max)))
938 (setq completion (buffer-substring beg end))
939 (set-window-configuration comint-dynamic-list-input-ring-window-conf)
940 (choose-completion-string completion buffer base-size)))
942 (defun comint-dynamic-list-input-ring ()
943 "List in help buffer the buffer's input history."
944 (interactive)
945 (if (or (not (ring-p comint-input-ring))
946 (ring-empty-p comint-input-ring))
947 (message "No history")
948 (let ((history nil)
949 (history-buffer " *Input History*")
950 (index (1- (ring-length comint-input-ring)))
951 (conf (current-window-configuration)))
952 ;; We have to build up a list ourselves from the ring vector.
953 (while (>= index 0)
954 (setq history (cons (ring-ref comint-input-ring index) history)
955 index (1- index)))
956 ;; Change "completion" to "history reference"
957 ;; to make the display accurate.
958 (with-output-to-temp-buffer history-buffer
959 (display-completion-list history)
960 (set-buffer history-buffer)
961 (let ((keymap (make-sparse-keymap)))
962 (set-keymap-parent keymap (current-local-map))
963 (define-key keymap "\C-m" 'comint-dynamic-list-input-ring-select)
964 (use-local-map keymap))
965 (forward-line 3)
966 (while (search-backward "completion" nil 'move)
967 (replace-match "history reference")))
968 (sit-for 0)
969 (message "Hit space to flush")
970 (setq comint-dynamic-list-input-ring-window-conf conf)
971 (let ((ch (read-event)))
972 (if (eq ch ?\ )
973 (set-window-configuration conf)
974 (setq unread-command-events (list ch)))))))
977 (defun comint-regexp-arg (prompt)
978 "Return list of regexp and prefix arg using PROMPT."
979 (let* (;; Don't clobber this.
980 (last-command last-command)
981 (regexp (read-from-minibuffer prompt nil nil nil
982 'minibuffer-history-search-history)))
983 (list (if (string-equal regexp "")
984 (setcar minibuffer-history-search-history
985 (nth 1 minibuffer-history-search-history))
986 regexp)
987 (prefix-numeric-value current-prefix-arg))))
989 (defun comint-search-arg (arg)
990 ;; First make sure there is a ring and that we are after the process mark
991 (cond ((not (comint-after-pmark-p))
992 (error "Not at command line"))
993 ((or (null comint-input-ring)
994 (ring-empty-p comint-input-ring))
995 (error "Empty input ring"))
996 ((zerop arg)
997 ;; arg of zero resets search from beginning, and uses arg of 1
998 (setq comint-input-ring-index nil)
1001 arg)))
1003 (defun comint-search-start (arg)
1004 "Index to start a directional search, starting at `comint-input-ring-index'."
1005 (if comint-input-ring-index
1006 ;; If a search is running, offset by 1 in direction of arg
1007 (mod (+ comint-input-ring-index (if (> arg 0) 1 -1))
1008 (ring-length comint-input-ring))
1009 ;; For a new search, start from beginning or end, as appropriate
1010 (if (>= arg 0)
1011 0 ; First elt for forward search
1012 (1- (ring-length comint-input-ring))))) ; Last elt for backward search
1014 (defun comint-previous-input-string (arg)
1015 "Return the string ARG places along the input ring.
1016 Moves relative to `comint-input-ring-index'."
1017 (ring-ref comint-input-ring (if comint-input-ring-index
1018 (mod (+ arg comint-input-ring-index)
1019 (ring-length comint-input-ring))
1020 arg)))
1022 (defun comint-previous-input (arg)
1023 "Cycle backwards through input history."
1024 (interactive "*p")
1025 (comint-previous-matching-input "." arg))
1027 (defun comint-next-input (arg)
1028 "Cycle forwards through input history."
1029 (interactive "*p")
1030 (comint-previous-input (- arg)))
1032 (defun comint-previous-matching-input-string (regexp arg)
1033 "Return the string matching REGEXP ARG places along the input ring.
1034 Moves relative to `comint-input-ring-index'."
1035 (let* ((pos (comint-previous-matching-input-string-position regexp arg)))
1036 (if pos (ring-ref comint-input-ring pos))))
1038 (defun comint-previous-matching-input-string-position (regexp arg &optional start)
1039 "Return the index matching REGEXP ARG places along the input ring.
1040 Moves relative to START, or `comint-input-ring-index'."
1041 (if (or (not (ring-p comint-input-ring))
1042 (ring-empty-p comint-input-ring))
1043 (error "No history"))
1044 (let* ((len (ring-length comint-input-ring))
1045 (motion (if (> arg 0) 1 -1))
1046 (n (mod (- (or start (comint-search-start arg)) motion) len))
1047 (tried-each-ring-item nil)
1048 (prev nil))
1049 ;; Do the whole search as many times as the argument says.
1050 (while (and (/= arg 0) (not tried-each-ring-item))
1051 ;; Step once.
1052 (setq prev n
1053 n (mod (+ n motion) len))
1054 ;; If we haven't reached a match, step some more.
1055 (while (and (< n len) (not tried-each-ring-item)
1056 (not (string-match regexp (ring-ref comint-input-ring n))))
1057 (setq n (mod (+ n motion) len)
1058 ;; If we have gone all the way around in this search.
1059 tried-each-ring-item (= n prev)))
1060 (setq arg (if (> arg 0) (1- arg) (1+ arg))))
1061 ;; Now that we know which ring element to use, if we found it, return that.
1062 (if (string-match regexp (ring-ref comint-input-ring n))
1063 n)))
1065 (defun comint-previous-matching-input (regexp arg)
1066 "Search backwards through input history for match for REGEXP.
1067 \(Previous history elements are earlier commands.)
1068 With prefix argument N, search for Nth previous match.
1069 If N is negative, find the next or Nth next match."
1070 (interactive (comint-regexp-arg "Previous input matching (regexp): "))
1071 (setq arg (comint-search-arg arg))
1072 (let ((pos (comint-previous-matching-input-string-position regexp arg)))
1073 ;; Has a match been found?
1074 (if (null pos)
1075 (error "Not found")
1076 (setq comint-input-ring-index pos)
1077 (message "History item: %d" (1+ pos))
1078 (delete-region
1079 ;; Can't use kill-region as it sets this-command
1080 (or (marker-position comint-accum-marker)
1081 (process-mark (get-buffer-process (current-buffer))))
1082 (point))
1083 (insert (ring-ref comint-input-ring pos)))))
1085 (defun comint-next-matching-input (regexp arg)
1086 "Search forwards through input history for match for REGEXP.
1087 \(Later history elements are more recent commands.)
1088 With prefix argument N, search for Nth following match.
1089 If N is negative, find the previous or Nth previous match."
1090 (interactive (comint-regexp-arg "Next input matching (regexp): "))
1091 (comint-previous-matching-input regexp (- arg)))
1093 (defun comint-previous-matching-input-from-input (arg)
1094 "Search backwards through input history for match for current input.
1095 \(Previous history elements are earlier commands.)
1096 With prefix argument N, search for Nth previous match.
1097 If N is negative, search forwards for the -Nth following match."
1098 (interactive "p")
1099 (if (not (memq last-command '(comint-previous-matching-input-from-input
1100 comint-next-matching-input-from-input)))
1101 ;; Starting a new search
1102 (setq comint-matching-input-from-input-string
1103 (buffer-substring
1104 (or (marker-position comint-accum-marker)
1105 (process-mark (get-buffer-process (current-buffer))))
1106 (point))
1107 comint-input-ring-index nil))
1108 (comint-previous-matching-input
1109 (concat "^" (regexp-quote comint-matching-input-from-input-string))
1110 arg))
1112 (defun comint-next-matching-input-from-input (arg)
1113 "Search forwards through input history for match for current input.
1114 \(Following history elements are more recent commands.)
1115 With prefix argument N, search for Nth following match.
1116 If N is negative, search backwards for the -Nth previous match."
1117 (interactive "p")
1118 (comint-previous-matching-input-from-input (- arg)))
1121 (defun comint-replace-by-expanded-history (&optional silent start)
1122 "Expand input command history references before point.
1123 Expansion is dependent on the value of `comint-input-autoexpand'.
1125 This function depends on the buffer's idea of the input history, which may not
1126 match the command interpreter's idea, assuming it has one.
1128 Assumes history syntax is like typical Un*x shells'. However, since Emacs
1129 cannot know the interpreter's idea of input line numbers, assuming it has one,
1130 it cannot expand absolute input line number references.
1132 If the optional argument SILENT is non-nil, never complain
1133 even if history reference seems erroneous.
1135 If the optional argument START is non-nil, that specifies the
1136 start of the text to scan for history references, rather
1137 than the logical beginning of line.
1139 See `comint-magic-space' and `comint-replace-by-expanded-history-before-point'.
1141 Returns t if successful."
1142 (interactive)
1143 (if (and comint-input-autoexpand
1144 (if comint-use-prompt-regexp-instead-of-fields
1145 ;; Use comint-prompt-regexp
1146 (save-excursion
1147 (beginning-of-line)
1148 (looking-at (concat comint-prompt-regexp "!\\|\\^")))
1149 ;; Use input fields. User input that hasn't been entered
1150 ;; yet, at the end of the buffer, has a nil `field' property.
1151 (and (null (get-char-property (point) 'field))
1152 (string-match "!\\|^\\^" (field-string)))))
1153 ;; Looks like there might be history references in the command.
1154 (let ((previous-modified-tick (buffer-modified-tick)))
1155 (comint-replace-by-expanded-history-before-point silent start)
1156 (/= previous-modified-tick (buffer-modified-tick)))))
1159 (defun comint-replace-by-expanded-history-before-point (silent &optional start)
1160 "Expand directory stack reference before point.
1161 See `comint-replace-by-expanded-history'. Returns t if successful.
1163 If the optional argument START is non-nil, that specifies the
1164 start of the text to scan for history references, rather
1165 than the logical beginning of line."
1166 (save-excursion
1167 (let ((toend (- (line-end-position) (point)))
1168 (start (comint-line-beginning-position)))
1169 (goto-char start)
1170 (while (progn
1171 (skip-chars-forward "^!^" (- (line-end-position) toend))
1172 (< (point) (- (line-end-position) toend)))
1173 ;; This seems a bit complex. We look for references such as !!, !-num,
1174 ;; !foo, !?foo, !{bar}, !?{bar}, ^oh, ^my^, ^god^it, ^never^ends^.
1175 ;; If that wasn't enough, the plings can be suffixed with argument
1176 ;; range specifiers.
1177 ;; Argument ranges are complex too, so we hive off the input line,
1178 ;; referenced with plings, with the range string to `comint-args'.
1179 (setq comint-input-ring-index nil)
1180 (cond ((or (= (preceding-char) ?\\)
1181 (comint-within-quotes start (point)))
1182 ;; The history is quoted, or we're in quotes.
1183 (goto-char (1+ (point))))
1184 ((looking-at "![0-9]+\\($\\|[^-]\\)")
1185 ;; We cannot know the interpreter's idea of input line numbers.
1186 (goto-char (match-end 0))
1187 (message "Absolute reference cannot be expanded"))
1188 ((looking-at "!-\\([0-9]+\\)\\(:?[0-9^$*-]+\\)?")
1189 ;; Just a number of args from `number' lines backward.
1190 (let ((number (1- (string-to-number
1191 (buffer-substring (match-beginning 1)
1192 (match-end 1))))))
1193 (if (<= number (ring-length comint-input-ring))
1194 (progn
1195 (replace-match
1196 (comint-args (comint-previous-input-string number)
1197 (match-beginning 2) (match-end 2))
1198 t t)
1199 (setq comint-input-ring-index number)
1200 (message "History item: %d" (1+ number)))
1201 (goto-char (match-end 0))
1202 (message "Relative reference exceeds input history size"))))
1203 ((or (looking-at "!!?:?\\([0-9^$*-]+\\)") (looking-at "!!"))
1204 ;; Just a number of args from the previous input line.
1205 (replace-match
1206 (comint-args (comint-previous-input-string 0)
1207 (match-beginning 1) (match-end 1))
1208 t t)
1209 (message "History item: previous"))
1210 ((looking-at
1211 "!\\??\\({\\(.+\\)}\\|\\(\\sw+\\)\\)\\(:?[0-9^$*-]+\\)?")
1212 ;; Most recent input starting with or containing (possibly
1213 ;; protected) string, maybe just a number of args. Phew.
1214 (let* ((mb1 (match-beginning 1)) (me1 (match-end 1))
1215 (mb2 (match-beginning 2)) (me2 (match-end 2))
1216 (exp (buffer-substring (or mb2 mb1) (or me2 me1)))
1217 (pref (if (save-match-data (looking-at "!\\?")) "" "^"))
1218 (pos (save-match-data
1219 (comint-previous-matching-input-string-position
1220 (concat pref (regexp-quote exp)) 1))))
1221 (if (null pos)
1222 (progn
1223 (goto-char (match-end 0))
1224 (or silent
1225 (progn (message "Not found")
1226 (ding))))
1227 (setq comint-input-ring-index pos)
1228 (replace-match
1229 (comint-args (ring-ref comint-input-ring pos)
1230 (match-beginning 4) (match-end 4))
1231 t t)
1232 (message "History item: %d" (1+ pos)))))
1233 ((looking-at "\\^\\([^^]+\\)\\^?\\([^^]*\\)\\^?")
1234 ;; Quick substitution on the previous input line.
1235 (let ((old (buffer-substring (match-beginning 1) (match-end 1)))
1236 (new (buffer-substring (match-beginning 2) (match-end 2)))
1237 (pos nil))
1238 (replace-match (comint-previous-input-string 0) t t)
1239 (setq pos (point))
1240 (goto-char (match-beginning 0))
1241 (if (not (search-forward old pos t))
1242 (or silent
1243 (error "Not found"))
1244 (replace-match new t t)
1245 (message "History item: substituted"))))
1247 (forward-char 1)))))))
1250 (defun comint-magic-space (arg)
1251 "Expand input history references before point and insert ARG spaces.
1252 A useful command to bind to SPC. See `comint-replace-by-expanded-history'."
1253 (interactive "p")
1254 (comint-replace-by-expanded-history)
1255 (self-insert-command arg))
1257 (defun comint-within-quotes (beg end)
1258 "Return t if the number of quotes between BEG and END is odd.
1259 Quotes are single and double."
1260 (let ((countsq (comint-how-many-region "\\(^\\|[^\\\\]\\)\'" beg end))
1261 (countdq (comint-how-many-region "\\(^\\|[^\\\\]\\)\"" beg end)))
1262 (or (= (mod countsq 2) 1) (= (mod countdq 2) 1))))
1264 (defun comint-how-many-region (regexp beg end)
1265 "Return number of matches for REGEXP from BEG to END."
1266 (let ((count 0))
1267 (save-excursion
1268 (save-match-data
1269 (goto-char beg)
1270 (while (re-search-forward regexp end t)
1271 (setq count (1+ count)))))
1272 count))
1274 (defun comint-args (string begin end)
1275 ;; From STRING, return the args depending on the range specified in the text
1276 ;; from BEGIN to END. If BEGIN is nil, assume all args. Ignore leading `:'.
1277 ;; Range can be x-y, x-, -y, where x/y can be [0-9], *, ^, $.
1278 (save-match-data
1279 (if (null begin)
1280 (comint-arguments string 0 nil)
1281 (let* ((range (buffer-substring
1282 (if (eq (char-after begin) ?:) (1+ begin) begin) end))
1283 (nth (cond ((string-match "^[*^]" range) 1)
1284 ((string-match "^-" range) 0)
1285 ((string-equal range "$") nil)
1286 (t (string-to-number range))))
1287 (mth (cond ((string-match "[-*$]$" range) nil)
1288 ((string-match "-" range)
1289 (string-to-number (substring range (match-end 0))))
1290 (t nth))))
1291 (comint-arguments string nth mth)))))
1293 (defun comint-delim-arg (arg)
1294 "Return a list of arguments from ARG.
1295 Break it up at the delimiters in `comint-delimiter-argument-list'.
1296 Returned list is backwards."
1297 (if (null comint-delimiter-argument-list)
1298 (list arg)
1299 (let ((args nil)
1300 (pos 0)
1301 (len (length arg)))
1302 (while (< pos len)
1303 (let ((char (aref arg pos))
1304 (start pos))
1305 (if (memq char comint-delimiter-argument-list)
1306 (while (and (< pos len) (eq (aref arg pos) char))
1307 (setq pos (1+ pos)))
1308 (while (and (< pos len)
1309 (not (memq (aref arg pos)
1310 comint-delimiter-argument-list)))
1311 (setq pos (1+ pos))))
1312 (setq args (cons (substring arg start pos) args))))
1313 args)))
1315 (defun comint-arguments (string nth mth)
1316 "Return from STRING the NTH to MTH arguments.
1317 NTH and/or MTH can be nil, which means the last argument.
1318 Returned arguments are separated by single spaces.
1319 We assume whitespace separates arguments, except within quotes
1320 and except for a space or tab that immediately follows a backslash.
1321 Also, a run of one or more of a single character
1322 in `comint-delimiter-argument-list' is a separate argument.
1323 Argument 0 is the command name."
1324 ;; The first line handles ordinary characters and backslash-sequences
1325 ;; (except with w32 msdos-like shells, where backslashes are valid).
1326 ;; The second matches "-quoted strings.
1327 ;; The third matches '-quoted strings.
1328 ;; The fourth matches `-quoted strings.
1329 ;; This seems to fit the syntax of BASH 2.0.
1330 (let* ((first (if (if (fboundp 'w32-shell-dos-semantics)
1331 (w32-shell-dos-semantics))
1332 "[^ \n\t\"'`]+\\|"
1333 "[^ \n\t\"'`\\]+\\|\\\\[\"'`\\ \t]+\\|"))
1334 (argpart (concat first
1335 "\\(\"\\([^\"\\]\\|\\\\.\\)*\"\\|\
1336 '[^']*'\\|\
1337 `[^`]*`\\)"))
1338 (args ()) (pos 0)
1339 (count 0)
1340 beg str quotes)
1341 ;; Build a list of all the args until we have as many as we want.
1342 (while (and (or (null mth) (<= count mth))
1343 (string-match argpart string pos))
1344 (if (and beg (= pos (match-beginning 0)))
1345 ;; It's contiguous, part of the same arg.
1346 (setq pos (match-end 0)
1347 quotes (or quotes (match-beginning 1)))
1348 ;; It's a new separate arg.
1349 (if beg
1350 ;; Put the previous arg, if there was one, onto ARGS.
1351 (setq str (substring string beg pos)
1352 args (if quotes (cons str args)
1353 (nconc (comint-delim-arg str) args))))
1354 (setq count (length args))
1355 (setq quotes (match-beginning 1))
1356 (setq beg (match-beginning 0))
1357 (setq pos (match-end 0))))
1358 (if beg
1359 (setq str (substring string beg pos)
1360 args (if quotes (cons str args)
1361 (nconc (comint-delim-arg str) args))))
1362 (setq count (length args))
1363 (let ((n (or nth (1- count)))
1364 (m (if mth (1- (- count mth)) 0)))
1365 (mapconcat
1366 (function (lambda (a) a)) (nthcdr n (nreverse (nthcdr m args))) " "))))
1369 ;; Input processing stuff
1371 (defun comint-add-to-input-history (cmd)
1372 "Add CMD to the input history.
1373 Ignore duplicates if `comint-input-ignoredups' is non-nil."
1374 (if (and (funcall comint-input-filter cmd)
1375 (or (null comint-input-ignoredups)
1376 (not (ring-p comint-input-ring))
1377 (ring-empty-p comint-input-ring)
1378 (not (string-equal (ring-ref comint-input-ring 0)
1379 cmd))))
1380 (ring-insert comint-input-ring cmd)))
1382 (defun comint-send-input (&optional no-newline)
1383 "Send input to process.
1384 After the process output mark, sends all text from the process mark to
1385 point as input to the process. Before the process output mark, calls
1386 value of variable `comint-get-old-input' to retrieve old input, copies
1387 it to the process mark, and sends it. A terminal newline is also
1388 inserted into the buffer and sent to the process unless NO-NEWLINE is
1389 non-nil.
1391 Any history reference may be expanded depending on the value of the variable
1392 `comint-input-autoexpand'. The list of function names contained in the value
1393 of `comint-input-filter-functions' is called on the input before sending it.
1394 The input is entered into the input history ring, if the value of variable
1395 `comint-input-filter' returns non-nil when called on the input.
1397 If variable `comint-eol-on-send' is non-nil, then point is moved to the
1398 end of line before sending the input.
1400 After the input has been sent, if `comint-process-echoes' is non-nil,
1401 then `comint-send-input' waits to see if the process outputs a string
1402 matching the input, and if so, deletes that part of the output.
1404 The values of `comint-get-old-input', `comint-input-filter-functions', and
1405 `comint-input-filter' are chosen according to the command interpreter running
1406 in the buffer. E.g.,
1408 If the interpreter is the csh,
1409 `comint-get-old-input' is the default:
1410 If `comint-use-prompt-regexp-instead-of-fields' is nil, then
1411 either return the current input field, if point is on an input
1412 field, or the current line, if point is on an output field.
1413 If `comint-use-prompt-regexp-instead-of-fields' is non-nil, then
1414 return the current line with any initial string matching the
1415 regexp `comint-prompt-regexp' removed.
1416 `comint-input-filter-functions' monitors input for \"cd\", \"pushd\", and
1417 \"popd\" commands. When it sees one, it cd's the buffer.
1418 comint-input-filter is the default: returns t if the input isn't all white
1419 space.
1421 If the Comint is Lucid Common Lisp,
1422 comint-get-old-input snarfs the sexp ending at point.
1423 comint-input-filter-functions does nothing.
1424 comint-input-filter returns nil if the input matches input-filter-regexp,
1425 which matches (1) all whitespace (2) :a, :c, etc.
1427 Similarly for Soar, Scheme, etc."
1428 (interactive)
1429 ;; Note that the input string does not include its terminal newline.
1430 (let ((proc (get-buffer-process (current-buffer))))
1431 (if (not proc) (error "Current buffer has no process")
1432 (let* ((pmark (process-mark proc))
1433 (intxt (if (>= (point) (marker-position pmark))
1434 (progn (if comint-eol-on-send (end-of-line))
1435 (buffer-substring pmark (point)))
1436 (let ((copy (funcall comint-get-old-input)))
1437 (goto-char pmark)
1438 (insert copy)
1439 copy)))
1440 (input (if (not (eq comint-input-autoexpand 'input))
1441 ;; Just whatever's already there
1442 intxt
1443 ;; Expand and leave it visible in buffer
1444 (comint-replace-by-expanded-history t pmark)
1445 (buffer-substring pmark (point))))
1446 (history (if (not (eq comint-input-autoexpand 'history))
1447 input
1448 ;; This is messy 'cos ultimately the original
1449 ;; functions used do insertion, rather than return
1450 ;; strings. We have to expand, then insert back.
1451 (comint-replace-by-expanded-history t pmark)
1452 (let ((copy (buffer-substring pmark (point)))
1453 (start (point)))
1454 (insert input)
1455 (delete-region pmark start)
1456 copy))))
1458 (unless no-newline
1459 (insert ?\n))
1461 (comint-add-to-input-history history)
1463 (run-hook-with-args 'comint-input-filter-functions
1464 (if no-newline input
1465 (concat input "\n")))
1467 (let ((beg (marker-position pmark))
1468 (end (if no-newline (point) (1- (point)))))
1469 (when (> end beg)
1470 ;; Set text-properties for the input field
1471 (add-text-properties
1472 beg end
1473 '(front-sticky t
1474 font-lock-face comint-highlight-input
1475 mouse-face highlight
1476 help-echo "mouse-2: insert after prompt as new input"))
1477 (unless comint-use-prompt-regexp-instead-of-fields
1478 ;; Give old user input a field property of `input', to
1479 ;; distinguish it from both process output and unsent
1480 ;; input. The terminating newline is put into a special
1481 ;; `boundary' field to make cursor movement between input
1482 ;; and output fields smoother.
1483 (put-text-property beg end 'field 'input)))
1484 (unless (or no-newline comint-use-prompt-regexp-instead-of-fields)
1485 ;; Cover the terminating newline
1486 (add-text-properties end (1+ end)
1487 '(rear-nonsticky t
1488 field boundary
1489 inhibit-line-move-field-capture t))))
1491 (comint-snapshot-last-prompt)
1493 (setq comint-save-input-ring-index comint-input-ring-index)
1494 (setq comint-input-ring-index nil)
1495 ;; Update the markers before we send the input
1496 ;; in case we get output amidst sending the input.
1497 (set-marker comint-last-input-start pmark)
1498 (set-marker comint-last-input-end (point))
1499 (set-marker (process-mark proc) (point))
1500 ;; clear the "accumulation" marker
1501 (set-marker comint-accum-marker nil)
1502 (let ((comint-input-sender-no-newline no-newline))
1503 (funcall comint-input-sender proc input))
1505 ;; Optionally delete echoed input (after checking it).
1506 (when comint-process-echoes
1507 (let ((echo-len (- comint-last-input-end
1508 comint-last-input-start)))
1509 ;; Wait for all input to be echoed:
1510 (while (and (accept-process-output proc)
1511 (> (+ comint-last-input-end echo-len)
1512 (point-max))
1513 (zerop
1514 (compare-buffer-substrings
1515 nil comint-last-input-start
1516 (- (point-max) echo-len)
1517 ;; Above difference is equivalent to
1518 ;; (+ comint-last-input-start
1519 ;; (- (point-max) comint-last-input-end))
1520 nil comint-last-input-end (point-max)))))
1521 (if (and
1522 (<= (+ comint-last-input-end echo-len)
1523 (point-max))
1524 (zerop
1525 (compare-buffer-substrings
1526 nil comint-last-input-start comint-last-input-end
1527 nil comint-last-input-end
1528 (+ comint-last-input-end echo-len))))
1529 (delete-region comint-last-input-end
1530 (+ comint-last-input-end echo-len)))))
1532 ;; This used to call comint-output-filter-functions,
1533 ;; but that scrolled the buffer in undesirable ways.
1534 (run-hook-with-args 'comint-output-filter-functions "")))))
1536 (defvar comint-preoutput-filter-functions nil
1537 "List of functions to call before inserting Comint output into the buffer.
1538 Each function gets one argument, a string containing the text received
1539 from the subprocess. It should return the string to insert, perhaps
1540 the same string that was received, or perhaps a modified or transformed
1541 string.
1543 The functions on the list are called sequentially, and each one is
1544 given the string returned by the previous one. The string returned by
1545 the last function is the text that is actually inserted in the
1546 redirection buffer.
1548 You can use `add-hook' to add functions to this list
1549 either globally or locally.")
1551 (defvar comint-inhibit-carriage-motion nil
1552 "If nil, Comint will interpret `carriage control' characters in output.
1553 See `comint-carriage-motion' for details.")
1555 ;; When non-nil, this is an overlay over the last recognized prompt in
1556 ;; the buffer; it is used when highlighting the prompt.
1557 (defvar comint-last-prompt-overlay nil)
1559 (defun comint-snapshot-last-prompt ()
1560 "`snapshot' any current `comint-last-prompt-overlay'.
1561 freeze its attributes in place, even when more input comes a long
1562 and moves the prompt overlay."
1563 (when comint-last-prompt-overlay
1564 (let ((inhibit-read-only t))
1565 (add-text-properties (overlay-start comint-last-prompt-overlay)
1566 (overlay-end comint-last-prompt-overlay)
1567 (overlay-properties comint-last-prompt-overlay)))))
1569 (defun comint-carriage-motion (start end)
1570 "Interpret carriage control characters in the region from START to END.
1571 Translate carriage return/linefeed sequences to linefeeds.
1572 Make single carriage returns delete to the beginning of the line.
1573 Make backspaces delete the previous character."
1574 (save-excursion
1575 ;; First do a quick check to see if there are any applicable
1576 ;; characters, so we can avoid calling save-match-data and
1577 ;; save-restriction if not.
1578 (goto-char start)
1579 (when (< (skip-chars-forward "^\b\r" end) (- end start))
1580 (save-match-data
1581 (save-restriction
1582 (widen)
1583 (let ((inhibit-field-text-motion t)
1584 (inhibit-read-only t))
1585 ;; CR LF -> LF
1586 ;; Note that this won't work properly when the CR and LF
1587 ;; are in different output chunks, but this is probably an
1588 ;; exceedingly rare case (because they are generally
1589 ;; written as a unit), and to delay interpretation of a
1590 ;; trailing CR in a chunk would result in odd interactive
1591 ;; behavior (and this case is probably far more common).
1592 (while (re-search-forward "\r$" end t)
1593 (delete-char -1))
1594 ;; bare CR -> delete preceding line
1595 (goto-char start)
1596 (while (search-forward "\r" end t)
1597 (delete-region (point) (line-beginning-position)))
1598 ;; BS -> delete preceding character
1599 (goto-char start)
1600 (while (search-forward "\b" end t)
1601 (delete-char -2))))))))
1603 ;; The purpose of using this filter for comint processes
1604 ;; is to keep comint-last-input-end from moving forward
1605 ;; when output is inserted.
1606 (defun comint-output-filter (process string)
1607 (let ((oprocbuf (process-buffer process)))
1608 ;; First check for killed buffer or no input.
1609 (when (and string oprocbuf (buffer-name oprocbuf))
1610 (with-current-buffer oprocbuf
1611 ;; Run preoutput filters
1612 (let ((functions comint-preoutput-filter-functions))
1613 (while (and functions string)
1614 (if (eq (car functions) t)
1615 (let ((functions (default-value 'comint-preoutput-filter-functions)))
1616 (while (and functions string)
1617 (setq string (funcall (car functions) string))
1618 (setq functions (cdr functions))))
1619 (setq string (funcall (car functions) string)))
1620 (setq functions (cdr functions))))
1622 ;; Insert STRING
1623 (let ((inhibit-read-only t)
1624 ;; Avoid the overhead of save-excursion, since we just
1625 ;; fiddle with the point
1626 (saved-point (point-marker)))
1628 ;; The point should float after any insertion we do
1629 (set-marker-insertion-type saved-point t)
1631 ;; We temporarly remove any buffer narrowing, in case the
1632 ;; process mark is outside of the restriction
1633 (save-restriction
1634 (widen)
1636 (goto-char (process-mark process))
1637 (set-marker comint-last-output-start (point))
1639 ;; insert-before-markers is a bad thing. XXX
1641 ;; It is used here to force window-point markers (used to
1642 ;; store the value of point in non-selected windows) to
1643 ;; advance, but it also screws up any other markers that we
1644 ;; don't _want_ to advance, such as the start-marker of some
1645 ;; of the overlays we create.
1647 ;; We work around the problem with the overlays by
1648 ;; explicitly adjusting them after we do the insertion, but
1649 ;; in the future this problem should be solved correctly, by
1650 ;; using `insert', and making the insertion-type of
1651 ;; window-point markers settable (via a buffer-local
1652 ;; variable). In comint buffers, this variable would be set
1653 ;; to `t', to cause point in non-select windows to advance.
1654 (insert-before-markers string)
1655 ;; Fixup markers and overlays that got screwed up because we
1656 ;; used `insert-before-markers'.
1657 (let ((old-point (- (point) (length string))))
1658 ;; comint-last-output-start
1659 (set-marker comint-last-output-start old-point)
1660 ;; comint-last-input-end
1661 (when (and comint-last-input-end
1662 (equal (marker-position comint-last-input-end)
1663 (point)))
1664 (set-marker comint-last-input-end old-point))
1665 ;; No overlays we create are set to advance upon insertion
1666 ;; (at the start/end), so we assume that any overlay which
1667 ;; is at the current point was incorrectly advanced by
1668 ;; insert-before-markers. First fixup overlays that might
1669 ;; start at point:
1670 (dolist (over (overlays-at (point)))
1671 (when (= (overlay-start over) (point))
1672 (let ((end (overlay-end over)))
1673 (move-overlay over
1674 old-point
1675 (if (= end (point)) old-point end)))))
1676 ;; Then do overlays that might end at point:
1677 (dolist (over (overlays-at (1- (point))))
1678 (when (= (overlay-end over) (point))
1679 (move-overlay over
1680 (min (overlay-start over) old-point)
1681 old-point))))
1683 ;; Advance process-mark
1684 (set-marker (process-mark process) (point))
1686 (unless comint-inhibit-carriage-motion
1687 ;; Interpret any carriage motion characters (newline, backspace)
1688 (comint-carriage-motion comint-last-output-start (point)))
1690 (run-hook-with-args 'comint-output-filter-functions string)
1692 (goto-char (process-mark process)) ; in case a filter moved it
1694 (unless comint-use-prompt-regexp-instead-of-fields
1695 (let ((inhibit-read-only t))
1696 (add-text-properties comint-last-output-start (point)
1697 '(rear-nonsticky t
1698 field output
1699 inhibit-line-move-field-capture t))))
1701 ;; Highlight the prompt, where we define `prompt' to mean
1702 ;; the most recent output that doesn't end with a newline.
1703 (let ((prompt-start (save-excursion (forward-line 0) (point)))
1704 (inhibit-read-only t))
1705 (when comint-prompt-read-only
1706 (or (= (point-min) prompt-start)
1707 (get-text-property (1- prompt-start) 'read-only)
1708 (put-text-property
1709 (1- prompt-start) prompt-start 'read-only 'fence))
1710 (add-text-properties
1711 prompt-start (point)
1712 '(read-only t rear-non-sticky t front-sticky (read-only))))
1713 (unless (and (bolp) (null comint-last-prompt-overlay))
1714 ;; Need to create or move the prompt overlay (in the case
1715 ;; where there is no prompt ((bolp) == t), we still do
1716 ;; this if there's already an existing overlay).
1717 (if comint-last-prompt-overlay
1718 ;; Just move an existing overlay
1719 (move-overlay comint-last-prompt-overlay
1720 prompt-start (point))
1721 ;; Need to create the overlay
1722 (setq comint-last-prompt-overlay
1723 (make-overlay prompt-start (point)))
1724 (overlay-put comint-last-prompt-overlay
1725 'font-lock-face 'comint-highlight-prompt))))
1727 (goto-char saved-point)))))))
1729 (defun comint-preinput-scroll-to-bottom ()
1730 "Go to the end of buffer in all windows showing it.
1731 Movement occurs if point in the selected window is not after the process mark,
1732 and `this-command' is an insertion command. Insertion commands recognised
1733 are `self-insert-command', `comint-magic-space', `yank', and `hilit-yank'.
1734 Depends on the value of `comint-scroll-to-bottom-on-input'.
1736 This function should be a pre-command hook."
1737 (if (and comint-scroll-to-bottom-on-input
1738 (memq this-command '(self-insert-command comint-magic-space yank
1739 hilit-yank)))
1740 (let* ((selected (selected-window))
1741 (current (current-buffer))
1742 (process (get-buffer-process current))
1743 (scroll comint-scroll-to-bottom-on-input))
1744 (if (and process (< (point) (process-mark process)))
1745 (if (eq scroll 'this)
1746 (goto-char (point-max))
1747 (walk-windows
1748 (function (lambda (window)
1749 (if (and (eq (window-buffer window) current)
1750 (or (eq scroll t) (eq scroll 'all)))
1751 (progn
1752 (select-window window)
1753 (goto-char (point-max))
1754 (select-window selected)))))
1755 nil t))))))
1757 (defun comint-postoutput-scroll-to-bottom (string)
1758 "Go to the end of buffer in some or all windows showing it.
1759 Does not scroll if the current line is the last line in the buffer.
1760 Depends on the value of `comint-move-point-for-output' and
1761 `comint-scroll-show-maximum-output'.
1763 This function should be in the list `comint-output-filter-functions'."
1764 (let* ((selected (selected-window))
1765 (current (current-buffer))
1766 (process (get-buffer-process current))
1767 (scroll comint-move-point-for-output))
1768 (unwind-protect
1769 (if process
1770 (walk-windows
1771 (function (lambda (window)
1772 (if (eq (window-buffer window) current)
1773 (progn
1774 (select-window window)
1775 (if (and (< (point) (process-mark process))
1776 (or (eq scroll t) (eq scroll 'all)
1777 ;; Maybe user wants point to jump to end.
1778 (and (eq scroll 'this) (eq selected window))
1779 (and (eq scroll 'others) (not (eq selected window)))
1780 ;; If point was at the end, keep it at end.
1781 (and (marker-position comint-last-output-start)
1782 (>= (point) comint-last-output-start))))
1783 (goto-char (process-mark process)))
1784 ;; Optionally scroll so that the text
1785 ;; ends at the bottom of the window.
1786 (if (and comint-scroll-show-maximum-output
1787 (= (point) (point-max)))
1788 (save-excursion
1789 (goto-char (point-max))
1790 (recenter -1)))
1791 (select-window selected)))))
1792 nil t))
1793 (set-buffer current))))
1795 (defun comint-truncate-buffer (&optional string)
1796 "Truncate the buffer to `comint-buffer-maximum-size'.
1797 This function could be on `comint-output-filter-functions' or bound to a key."
1798 (interactive)
1799 (save-excursion
1800 (goto-char (process-mark (get-buffer-process (current-buffer))))
1801 (forward-line (- comint-buffer-maximum-size))
1802 (beginning-of-line)
1803 (let ((inhibit-read-only t))
1804 (delete-region (point-min) (point)))))
1806 (defun comint-strip-ctrl-m (&optional string)
1807 "Strip trailing `^M' characters from the current output group.
1808 This function could be on `comint-output-filter-functions' or bound to a key."
1809 (interactive)
1810 (let ((pmark (process-mark (get-buffer-process (current-buffer)))))
1811 (save-excursion
1812 (condition-case nil
1813 (goto-char
1814 (if (interactive-p) comint-last-input-end comint-last-output-start))
1815 (error nil))
1816 (while (re-search-forward "\r+$" pmark t)
1817 (replace-match "" t t)))))
1818 (defalias 'shell-strip-ctrl-m 'comint-strip-ctrl-m)
1820 (defun comint-show-maximum-output ()
1821 "Put the end of the buffer at the bottom of the window."
1822 (interactive)
1823 (goto-char (point-max))
1824 (recenter -1))
1826 (defun comint-get-old-input-default ()
1827 "Default for `comint-get-old-input'.
1828 If `comint-use-prompt-regexp-instead-of-fields' is nil, then either
1829 return the current input field, if point is on an input field, or the
1830 current line, if point is on an output field.
1831 If `comint-use-prompt-regexp-instead-of-fields' is non-nil, then return
1832 the current line with any initial string matching the regexp
1833 `comint-prompt-regexp' removed."
1834 (let ((bof (field-beginning)))
1835 (if (eq (get-char-property bof 'field) 'input)
1836 (field-string-no-properties bof)
1837 (comint-bol)
1838 (buffer-substring-no-properties (point) (line-end-position)))))
1840 (defun comint-copy-old-input ()
1841 "Insert after prompt old input at point as new input to be edited.
1842 Calls `comint-get-old-input' to get old input."
1843 (interactive)
1844 (let ((input (funcall comint-get-old-input))
1845 (process (get-buffer-process (current-buffer))))
1846 (if (not process)
1847 (error "Current buffer has no process")
1848 (goto-char (process-mark process))
1849 (insert input))))
1851 (defun comint-skip-prompt ()
1852 "Skip past the text matching regexp `comint-prompt-regexp'.
1853 If this takes us past the end of the current line, don't skip at all."
1854 (if (and (looking-at comint-prompt-regexp)
1855 (<= (match-end 0) (line-end-position)))
1856 (goto-char (match-end 0))))
1858 (defun comint-after-pmark-p ()
1859 "Return t if point is after the process output marker."
1860 (let ((pmark (process-mark (get-buffer-process (current-buffer)))))
1861 (<= (marker-position pmark) (point))))
1863 (defun comint-simple-send (proc string)
1864 "Default function for sending to PROC input STRING.
1865 This just sends STRING plus a newline. To override this,
1866 set the hook `comint-input-sender'."
1867 (comint-send-string proc string)
1868 (if comint-input-sender-no-newline
1869 (if (not (string-equal string ""))
1870 (process-send-eof))
1871 (comint-send-string proc "\n")))
1873 (defun comint-line-beginning-position ()
1874 "Return the buffer position of the beginning of the line, after any prompt.
1875 If `comint-use-prompt-regexp-instead-of-fields' is non-nil, then the
1876 prompt skip is done by skipping text matching the regular expression
1877 `comint-prompt-regexp', a buffer local variable."
1878 (if comint-use-prompt-regexp-instead-of-fields
1879 ;; Use comint-prompt-regexp
1880 (save-excursion
1881 (beginning-of-line)
1882 (comint-skip-prompt)
1883 (point))
1884 ;; Use input fields. Note that, unlike the behavior of
1885 ;; `line-beginning-position' inside a field, this function will
1886 ;; return the position of the end of a prompt, even if the point is
1887 ;; already inside the prompt. In order to do this, it assumes that
1888 ;; if there are two fields on a line, then the first one is the
1889 ;; prompt, and the second one is an input field, and is front-sticky
1890 ;; (as input fields should be).
1891 (constrain-to-field (line-beginning-position) (line-end-position))))
1893 (defun comint-bol (&optional arg)
1894 "Go to the beginning of line, then skip past the prompt, if any.
1895 If prefix argument is given (\\[universal-argument]) the prompt is not skipped.
1896 If `comint-use-prompt-regexp-instead-of-fields' is non-nil, then the
1897 prompt skip is done by skipping text matching the regular expression
1898 `comint-prompt-regexp', a buffer local variable."
1899 (interactive "P")
1900 (if arg
1901 ;; Unlike `beginning-of-line', forward-line ignores field boundaries
1902 (forward-line 0)
1903 (goto-char (comint-line-beginning-position))))
1905 ;; These three functions are for entering text you don't want echoed or
1906 ;; saved -- typically passwords to ftp, telnet, or somesuch.
1907 ;; Just enter m-x send-invisible and type in your line, or add
1908 ;; `comint-watch-for-password-prompt' to `comint-output-filter-functions'.
1910 (defun comint-read-noecho (prompt &optional stars)
1911 "Read a single line of text from user without echoing, and return it.
1912 Prompt with argument PROMPT, a string. Optional argument STARS causes
1913 input to be echoed with '*' characters on the prompt line. Input ends with
1914 RET, LFD, or ESC. DEL or C-h rubs out. C-u kills line. C-g aborts (if
1915 `inhibit-quit' is set because e.g. this function was called from a process
1916 filter and C-g is pressed, this function returns nil rather than a string).
1918 Note that the keystrokes comprising the text can still be recovered
1919 \(temporarily) with \\[view-lossage]. Some people find this worrisome (see,
1920 however, `clear-this-command-keys').
1921 Once the caller uses the password, it can erase the password
1922 by doing (clear-string STRING)."
1923 (let ((ans "")
1924 (newans nil)
1925 (c 0)
1926 (echo-keystrokes 0)
1927 (cursor-in-echo-area t)
1928 (message-log-max nil)
1929 (done nil))
1930 (while (not done)
1931 (if stars
1932 (message "%s%s" prompt (make-string (length ans) ?*))
1933 (message "%s" prompt))
1934 ;; Use this instead of `read-char' to avoid "Non-character input-event".
1935 (setq c (read-char-exclusive))
1936 (cond ((= c ?\C-g)
1937 ;; This function may get called from a process filter, where
1938 ;; inhibit-quit is set. In later versions of emacs read-char
1939 ;; may clear quit-flag itself and return C-g. That would make
1940 ;; it impossible to quit this loop in a simple way, so
1941 ;; re-enable it here (for backward-compatibility the check for
1942 ;; quit-flag below would still be necessary, so this seems
1943 ;; like the simplest way to do things).
1944 (setq quit-flag t
1945 done t))
1946 ((or (= c ?\r) (= c ?\n) (= c ?\e))
1947 (setq done t))
1948 ((= c ?\C-u)
1949 (clear-string ans)
1950 (setq ans ""))
1951 ((and (/= c ?\b) (/= c ?\177))
1952 (setq newans (concat ans (char-to-string c)))
1953 (clear-string ans)
1954 (setq ans newans))
1955 ((> (length ans) 0)
1956 (aset ans (1- (length ans)) 0)
1957 (setq ans (substring ans 0 -1)))))
1958 (if quit-flag
1959 ;; Emulate a true quit, except that we have to return a value.
1960 (prog1
1961 (setq quit-flag nil)
1962 (message "Quit")
1963 (beep t))
1964 (message "")
1965 ans)))
1967 (defun send-invisible (&optional prompt)
1968 "Read a string without echoing.
1969 Then send it to the process running in the current buffer.
1970 The string is sent using `comint-input-sender'.
1971 Security bug: your string can still be temporarily recovered with
1972 \\[view-lossage]; `clear-this-command-keys' can fix that."
1973 (interactive "P") ; Defeat snooping via C-x ESC ESC
1974 (let ((proc (get-buffer-process (current-buffer))))
1975 (if proc
1976 (let ((str (comint-read-noecho (or prompt "Non-echoed text: ") t)))
1977 (if (stringp str)
1978 (progn
1979 (comint-snapshot-last-prompt)
1980 (funcall comint-input-sender proc str))
1981 (message "Warning: text will be echoed")))
1982 (error "Current buffer has no process"))))
1984 (defun comint-watch-for-password-prompt (string)
1985 "Prompt in the minibuffer for password and send without echoing.
1986 This function uses `send-invisible' to read and send a password to the buffer's
1987 process if STRING contains a password prompt defined by
1988 `comint-password-prompt-regexp'.
1990 This function could be in the list `comint-output-filter-functions'."
1991 (when (string-match comint-password-prompt-regexp string)
1992 (when (string-match "^[ \n\r\t\v\f\b\a]+" string)
1993 (setq string (replace-match "" t t string)))
1994 (send-invisible string)))
1996 ;; Low-level process communication
1998 (defun comint-send-string (process string)
1999 "Like `process-send-string', but also does extra bookkeeping for Comint mode."
2000 (if process
2001 (with-current-buffer (if (processp process)
2002 (process-buffer process)
2003 (get-buffer process))
2004 (comint-snapshot-last-prompt))
2005 (comint-snapshot-last-prompt))
2006 (process-send-string process string))
2008 (defun comint-send-region (process start end)
2009 "Like `process-send-region', but also does extra bookkeeping for Comint mode."
2010 (if process
2011 (with-current-buffer (if (processp process)
2012 (process-buffer process)
2013 (get-buffer process))
2014 (comint-snapshot-last-prompt))
2015 (comint-snapshot-last-prompt))
2016 (process-send-region process start end))
2019 ;; Random input hackage
2021 (defun comint-delete-output ()
2022 "Delete all output from interpreter since last input.
2023 Does not delete the prompt."
2024 (interactive)
2025 (let ((proc (get-buffer-process (current-buffer)))
2026 (replacement nil)
2027 (inhibit-read-only t))
2028 (save-excursion
2029 (let ((pmark (progn (goto-char (process-mark proc))
2030 (forward-line 0)
2031 (point-marker))))
2032 (delete-region comint-last-input-end pmark)
2033 (goto-char (process-mark proc))
2034 (setq replacement (concat "*** output flushed ***\n"
2035 (buffer-substring pmark (point))))
2036 (delete-region pmark (point))))
2037 ;; Output message and put back prompt
2038 (comint-output-filter proc replacement)))
2039 (defalias 'comint-kill-output 'comint-delete-output)
2040 (make-obsolete 'comint-kill-output 'comint-delete-output "21.1")
2042 (defun comint-write-output (filename &optional append mustbenew)
2043 "Write output from interpreter since last input to FILENAME.
2044 Any prompt at the end of the output is not written.
2046 If the optional argument APPEND (the prefix argument when interactive)
2047 is non-nil, the output is appended to the file instead.
2049 If the optional argument MUSTBENEW is non-nil, check for an existing
2050 file with the same name. If MUSTBENEW is `excl', that means to get an
2051 error if the file already exists; never overwrite. If MUSTBENEW is
2052 neither nil nor `excl', that means ask for confirmation before
2053 overwriting, but do go ahead and overwrite the file if the user
2054 confirms. When interactive, MUSTBENEW is nil when appending, and t
2055 otherwise."
2056 (interactive
2057 (list (read-file-name
2058 (if current-prefix-arg
2059 "Append output to file: "
2060 "Write output to file: "))
2061 current-prefix-arg
2062 (not current-prefix-arg)))
2063 (save-excursion
2064 (goto-char (process-mark (get-buffer-process (current-buffer))))
2065 (forward-line 0)
2066 (write-region comint-last-input-end (point) filename
2067 append nil nil mustbenew)))
2069 ;; This function exists for the benefit of the menu; from the keyboard,
2070 ;; users can just use `comint-write-output' with a prefix arg.
2071 (defun comint-append-output-to-file (filename)
2072 "Append output from interpreter since last input to FILENAME.
2073 Any prompt at the end of the output is not written."
2074 (interactive "fAppend output to file: ")
2075 (comint-write-output filename t))
2077 (defun comint-show-output ()
2078 "Display start of this batch of interpreter output at top of window.
2079 Sets mark to the value of point when this command is run."
2080 (interactive)
2081 (push-mark)
2082 (let ((pos (or (marker-position comint-last-input-end) (point-max))))
2083 (cond (comint-use-prompt-regexp-instead-of-fields
2084 (goto-char pos)
2085 (beginning-of-line 0)
2086 (set-window-start (selected-window) (point))
2087 (comint-skip-prompt))
2089 (goto-char (field-beginning pos))
2090 (set-window-start (selected-window) (point))))))
2093 (defun comint-interrupt-subjob ()
2094 "Interrupt the current subjob.
2095 This command also kills the pending input
2096 between the process mark and point."
2097 (interactive)
2098 (comint-skip-input)
2099 (interrupt-process nil comint-ptyp)
2100 ;; (process-send-string nil "\n")
2103 (defun comint-kill-subjob ()
2104 "Send kill signal to the current subjob.
2105 This command also kills the pending input
2106 between the process mark and point."
2107 (interactive)
2108 (comint-skip-input)
2109 (kill-process nil comint-ptyp))
2111 (defun comint-quit-subjob ()
2112 "Send quit signal to the current subjob.
2113 This command also kills the pending input
2114 between the process mark and point."
2115 (interactive)
2116 (comint-skip-input)
2117 (quit-process nil comint-ptyp))
2119 (defun comint-stop-subjob ()
2120 "Stop the current subjob.
2121 This command also kills the pending input
2122 between the process mark and point.
2124 WARNING: if there is no current subjob, you can end up suspending
2125 the top-level process running in the buffer. If you accidentally do
2126 this, use \\[comint-continue-subjob] to resume the process. (This
2127 is not a problem with most shells, since they ignore this signal.)"
2128 (interactive)
2129 (comint-skip-input)
2130 (stop-process nil comint-ptyp))
2132 (defun comint-continue-subjob ()
2133 "Send CONT signal to process buffer's process group.
2134 Useful if you accidentally suspend the top-level process."
2135 (interactive)
2136 (continue-process nil comint-ptyp))
2138 (defun comint-skip-input ()
2139 "Skip all pending input, from last stuff output by interpreter to point.
2140 This means mark it as if it had been sent as input, without sending it."
2141 (let ((comint-input-sender 'ignore)
2142 (comint-input-filter-functions nil))
2143 (comint-send-input t))
2144 (end-of-line)
2145 (let ((pos (point))
2146 (marker (process-mark (get-buffer-process (current-buffer)))))
2147 (insert " " (key-description (this-command-keys)))
2148 (if (= marker pos)
2149 (set-marker marker (point)))))
2151 (defun comint-kill-input ()
2152 "Kill all text from last stuff output by interpreter to point."
2153 (interactive)
2154 (let ((pmark (process-mark (get-buffer-process (current-buffer)))))
2155 (if (> (point) (marker-position pmark))
2156 (kill-region pmark (point)))))
2158 (defun comint-delchar-or-maybe-eof (arg)
2159 "Delete ARG characters forward or send an EOF to subprocess.
2160 Sends an EOF only if point is at the end of the buffer and there is no input."
2161 (interactive "p")
2162 (let ((proc (get-buffer-process (current-buffer))))
2163 (if (and (eobp) proc (= (point) (marker-position (process-mark proc))))
2164 (comint-send-eof)
2165 (delete-char arg))))
2167 (defun comint-send-eof ()
2168 "Send an EOF to the current buffer's process."
2169 (interactive)
2170 (comint-send-input t)
2171 (process-send-eof))
2174 (defun comint-backward-matching-input (regexp n)
2175 "Search backward through buffer for input fields that match REGEXP.
2176 If `comint-use-prompt-regexp-instead-of-fields' is non-nil, then input
2177 fields are identified by lines that match `comint-prompt-regexp'.
2179 With prefix argument N, search for Nth previous match.
2180 If N is negative, find the next or Nth next match."
2181 (interactive (comint-regexp-arg "Backward input matching (regexp): "))
2182 (if comint-use-prompt-regexp-instead-of-fields
2183 ;; Use comint-prompt-regexp
2184 (let* ((re (concat comint-prompt-regexp ".*" regexp))
2185 (pos (save-excursion (end-of-line (if (> n 0) 0 1))
2186 (if (re-search-backward re nil t n)
2187 (point)))))
2188 (if (null pos)
2189 (progn (message "Not found")
2190 (ding))
2191 (goto-char pos)
2192 (comint-bol nil)))
2193 ;; Use input fields
2194 (let* ((dir (if (< n 0) -1 1))
2195 (pos
2196 (save-excursion
2197 (while (/= n 0)
2198 (unless (re-search-backward regexp nil t dir)
2199 (error "Not found"))
2200 (when (eq (get-char-property (point) 'field) 'input)
2201 (setq n (- n dir))))
2202 (field-beginning))))
2203 (goto-char pos))))
2206 (defun comint-forward-matching-input (regexp arg)
2207 "Search forward through buffer for input fields that match REGEXP.
2208 If `comint-use-prompt-regexp-instead-of-fields' is non-nil, then input
2209 fields are identified by lines that match `comint-prompt-regexp'.
2211 With prefix argument N, search for Nth following match.
2212 If N is negative, find the previous or Nth previous match."
2213 (interactive (comint-regexp-arg "Forward input matching (regexp): "))
2214 (comint-backward-matching-input regexp (- arg)))
2217 (defun comint-next-prompt (n)
2218 "Move to end of Nth next prompt in the buffer.
2219 If `comint-use-prompt-regexp-instead-of-fields' is nil, then this means
2220 the beginning of the Nth next `input' field, otherwise, it means the Nth
2221 occurrence of text matching `comint-prompt-regexp'."
2222 (interactive "p")
2223 (if comint-use-prompt-regexp-instead-of-fields
2224 ;; Use comint-prompt-regexp
2225 (let ((paragraph-start comint-prompt-regexp))
2226 (end-of-line (if (> n 0) 1 0))
2227 (forward-paragraph n)
2228 (comint-skip-prompt))
2229 ;; Use input fields
2230 (let ((pos (point))
2231 (input-pos nil)
2232 prev-pos)
2233 (while (/= n 0)
2234 (setq prev-pos pos)
2235 (setq pos
2236 (if (> n 0)
2237 (next-single-char-property-change pos 'field)
2238 (previous-single-char-property-change pos 'field)))
2239 (cond ((or (null pos) (= pos prev-pos))
2240 ;; Ran off the end of the buffer.
2241 (when (> n 0)
2242 ;; There's always an input field at the end of the
2243 ;; buffer, but it has a `field' property of nil.
2244 (setq input-pos (point-max)))
2245 ;; stop iterating
2246 (setq n 0))
2247 ((eq (get-char-property pos 'field) 'input)
2248 (setq n (if (< n 0) (1+ n) (1- n)))
2249 (setq input-pos pos))))
2250 (when input-pos
2251 (goto-char input-pos)))))
2254 (defun comint-previous-prompt (n)
2255 "Move to end of Nth previous prompt in the buffer.
2256 If `comint-use-prompt-regexp-instead-of-fields' is nil, then this means
2257 the beginning of the Nth previous `input' field, otherwise, it means the Nth
2258 occurrence of text matching `comint-prompt-regexp'."
2259 (interactive "p")
2260 (comint-next-prompt (- n)))
2262 ;; State used by `comint-insert-previous-argument' when cycling.
2263 (defvar comint-insert-previous-argument-last-start-pos nil)
2264 (make-variable-buffer-local 'comint-insert-previous-argument-last-start-pos)
2265 (defvar comint-insert-previous-argument-last-index nil)
2266 (make-variable-buffer-local 'comint-insert-previous-argument-last-index)
2268 ;; Needs fixing:
2269 ;; make comint-arguments understand negative indices as bash does
2270 (defun comint-insert-previous-argument (index)
2271 "Insert the INDEXth argument from the previous Comint command-line at point.
2272 Spaces are added at beginning and/or end of the inserted string if
2273 necessary to ensure that it's separated from adjacent arguments.
2274 Interactively, if no prefix argument is given, the last argument is inserted.
2275 Repeated interactive invocations will cycle through the same argument
2276 from progressively earlier commands (using the value of INDEX specified
2277 with the first command).
2278 This command is like `M-.' in bash."
2279 (interactive "P")
2280 (unless (null index)
2281 (setq index (prefix-numeric-value index)))
2282 (cond ((eq last-command this-command)
2283 ;; Delete last input inserted by this command.
2284 (delete-region comint-insert-previous-argument-last-start-pos (point))
2285 (setq index comint-insert-previous-argument-last-index))
2287 ;; This is a non-repeat invocation, so initialize state.
2288 (setq comint-input-ring-index nil)
2289 (setq comint-insert-previous-argument-last-index index)
2290 (when (null comint-insert-previous-argument-last-start-pos)
2291 ;; First usage; initialize to a marker
2292 (setq comint-insert-previous-argument-last-start-pos
2293 (make-marker)))))
2294 ;; Make sure we're not in the prompt, and add a beginning space if necess.
2295 (if (<= (point) (comint-line-beginning-position))
2296 (comint-bol)
2297 (just-one-space))
2298 ;; Remember the beginning of what we insert, so we can delete it if
2299 ;; the command is repeated.
2300 (set-marker comint-insert-previous-argument-last-start-pos (point))
2301 ;; Insert the argument.
2302 (let ((input-string (comint-previous-input-string 0)))
2303 (when (string-match "[ \t\n]*&" input-string)
2304 ;; strip terminating '&'
2305 (setq input-string (substring input-string 0 (match-beginning 0))))
2306 (insert (comint-arguments input-string index index)))
2307 ;; Make next invocation return arg from previous input
2308 (setq comint-input-ring-index (1+ (or comint-input-ring-index 0)))
2309 ;; Add a terminating space if necessary.
2310 (unless (eolp)
2311 (just-one-space)))
2314 ;; Support for source-file processing commands.
2315 ;;============================================================================
2316 ;; Many command-interpreters (e.g., Lisp, Scheme, Soar) have
2317 ;; commands that process files of source text (e.g. loading or compiling
2318 ;; files). So the corresponding process-in-a-buffer modes have commands
2319 ;; for doing this (e.g., lisp-load-file). The functions below are useful
2320 ;; for defining these commands.
2322 ;; Alas, these guys don't do exactly the right thing for Lisp, Scheme
2323 ;; and Soar, in that they don't know anything about file extensions.
2324 ;; So the compile/load interface gets the wrong default occasionally.
2325 ;; The load-file/compile-file default mechanism could be smarter -- it
2326 ;; doesn't know about the relationship between filename extensions and
2327 ;; whether the file is source or executable. If you compile foo.lisp
2328 ;; with compile-file, then the next load-file should use foo.bin for
2329 ;; the default, not foo.lisp. This is tricky to do right, particularly
2330 ;; because the extension for executable files varies so much (.o, .bin,
2331 ;; .lbin, .mo, .vo, .ao, ...).
2334 ;; COMINT-SOURCE-DEFAULT -- determines defaults for source-file processing
2335 ;; commands.
2337 ;; COMINT-CHECK-SOURCE -- if FNAME is in a modified buffer, asks you if you
2338 ;; want to save the buffer before issuing any process requests to the command
2339 ;; interpreter.
2341 ;; COMINT-GET-SOURCE -- used by the source-file processing commands to prompt
2342 ;; for the file to process.
2344 (defun comint-source-default (previous-dir/file source-modes)
2345 "Compute the defaults for `load-file' and `compile-file' commands.
2347 PREVIOUS-DIR/FILE is a pair (directory . filename) from the last
2348 source-file processing command. nil if there hasn't been one yet.
2349 SOURCE-MODES is a list used to determine what buffers contain source
2350 files: if the major mode of the buffer is in SOURCE-MODES, it's source.
2351 Typically, (lisp-mode) or (scheme-mode).
2353 If the command is given while the cursor is inside a string, *and*
2354 the string is an existing filename, *and* the filename is not a directory,
2355 then the string is taken as default. This allows you to just position
2356 your cursor over a string that's a filename and have it taken as default.
2358 If the command is given in a file buffer whose major mode is in
2359 SOURCE-MODES, then the filename is the default file, and the
2360 file's directory is the default directory.
2362 If the buffer isn't a source file buffer (e.g., it's the process buffer),
2363 then the default directory & file are what was used in the last source-file
2364 processing command (i.e., PREVIOUS-DIR/FILE). If this is the first time
2365 the command has been run (PREVIOUS-DIR/FILE is nil), the default directory
2366 is the cwd, with no default file. (\"no default file\" = nil)
2368 SOURCE-REGEXP is typically going to be something like (tea-mode)
2369 for T programs, (lisp-mode) for Lisp programs, (soar-mode lisp-mode)
2370 for Soar programs, etc.
2372 The function returns a pair: (default-directory . default-file)."
2373 (cond ((and buffer-file-name (memq major-mode source-modes))
2374 (cons (file-name-directory buffer-file-name)
2375 (file-name-nondirectory buffer-file-name)))
2376 (previous-dir/file)
2378 (cons default-directory nil))))
2381 (defun comint-check-source (fname)
2382 "Check whether to save buffers visiting file FNAME.
2383 Prior to loading or compiling (or otherwise processing) a file (in the CMU
2384 process-in-a-buffer modes), this function can be called on the filename.
2385 If the file is loaded into a buffer, and the buffer is modified, the user
2386 is queried to see if he wants to save the buffer before proceeding with
2387 the load or compile."
2388 (let ((buff (get-file-buffer fname)))
2389 (if (and buff
2390 (buffer-modified-p buff)
2391 (y-or-n-p (format "Save buffer %s first? " (buffer-name buff))))
2392 ;; save BUFF.
2393 (let ((old-buffer (current-buffer)))
2394 (set-buffer buff)
2395 (save-buffer)
2396 (set-buffer old-buffer)))))
2398 (defun comint-extract-string ()
2399 "Return string around POINT, or nil."
2400 (let ((syntax (syntax-ppss)))
2401 (when (nth 3 syntax)
2402 (condition-case ()
2403 (buffer-substring-no-properties (1+ (nth 8 syntax))
2404 (progn (goto-char (nth 8 syntax))
2405 (forward-sexp)
2406 (1- (point))))
2407 (error nil)))))
2409 (defun comint-get-source (prompt prev-dir/file source-modes mustmatch-p)
2410 "Prompt for filenames in commands that process source files,
2411 e.g. loading or compiling a file.
2412 Provides a default, if there is one, and returns the result filename.
2414 See `comint-source-default' for more on determining defaults.
2416 PROMPT is the prompt string. PREV-DIR/FILE is the (directory . file) pair
2417 from the last source processing command. SOURCE-MODES is a list of major
2418 modes used to determine what file buffers contain source files. (These
2419 two arguments are used for determining defaults). If MUSTMATCH-P is true,
2420 then the filename reader will only accept a file that exists.
2422 A typical use:
2423 (interactive (comint-get-source \"Compile file: \" prev-lisp-dir/file
2424 '(lisp-mode) t))"
2425 (let* ((def (comint-source-default prev-dir/file source-modes))
2426 (stringfile (comint-extract-string))
2427 (sfile-p (and stringfile
2428 (condition-case ()
2429 (file-exists-p stringfile)
2430 (error nil))
2431 (not (file-directory-p stringfile))))
2432 (defdir (if sfile-p (file-name-directory stringfile)
2433 (car def)))
2434 (deffile (if sfile-p (file-name-nondirectory stringfile)
2435 (cdr def)))
2436 (ans (read-file-name (if deffile (format "%s(default %s) "
2437 prompt deffile)
2438 prompt)
2439 defdir
2440 (concat defdir deffile)
2441 mustmatch-p)))
2442 (list (expand-file-name (substitute-in-file-name ans)))))
2444 ;; I am somewhat divided on this string-default feature. It seems
2445 ;; to violate the principle-of-least-astonishment, in that it makes
2446 ;; the default harder to predict, so you actually have to look and see
2447 ;; what the default really is before choosing it. This can trip you up.
2448 ;; On the other hand, it can be useful, I guess. I would appreciate feedback
2449 ;; on this.
2450 ;; -Olin
2453 ;; Simple process query facility.
2454 ;; ===========================================================================
2455 ;; This function is for commands that want to send a query to the process
2456 ;; and show the response to the user. For example, a command to get the
2457 ;; arglist for a Common Lisp function might send a "(arglist 'foo)" query
2458 ;; to an inferior Common Lisp process.
2460 ;; This simple facility just sends strings to the inferior process and pops
2461 ;; up a window for the process buffer so you can see what the process
2462 ;; responds with. We don't do anything fancy like try to intercept what the
2463 ;; process responds with and put it in a pop-up window or on the message
2464 ;; line. We just display the buffer. Low tech. Simple. Works good.
2466 (defun comint-proc-query (proc str)
2467 "Send to the inferior process PROC the string STR.
2468 Pop-up but do not select a window for the inferior process so that
2469 its response can be seen."
2470 (let* ((proc-buf (process-buffer proc))
2471 (proc-mark (process-mark proc)))
2472 (display-buffer proc-buf)
2473 (set-buffer proc-buf) ; but it's not the selected *window*
2474 (let ((proc-win (get-buffer-window proc-buf))
2475 (proc-pt (marker-position proc-mark)))
2476 (comint-send-string proc str) ; send the query
2477 (accept-process-output proc) ; wait for some output
2478 ;; Try to position the proc window so you can see the answer.
2479 ;; This is bogus code. If you delete the (sit-for 0), it breaks.
2480 ;; I don't know why. Wizards invited to improve it.
2481 (unless (pos-visible-in-window-p proc-pt proc-win)
2482 (let ((opoint (window-point proc-win)))
2483 (set-window-point proc-win proc-mark)
2484 (sit-for 0)
2485 (if (not (pos-visible-in-window-p opoint proc-win))
2486 (push-mark opoint)
2487 (set-window-point proc-win opoint)))))))
2490 ;; Filename/command/history completion in a buffer
2491 ;; ===========================================================================
2492 ;; Useful completion functions, courtesy of the Ergo group.
2494 ;; Six commands:
2495 ;; comint-dynamic-complete Complete or expand command, filename,
2496 ;; history at point.
2497 ;; comint-dynamic-complete-filename Complete filename at point.
2498 ;; comint-dynamic-list-filename-completions List completions in help buffer.
2499 ;; comint-replace-by-expanded-filename Expand and complete filename at point;
2500 ;; replace with expanded/completed name.
2501 ;; comint-dynamic-simple-complete Complete stub given candidates.
2503 ;; These are not installed in the comint-mode keymap. But they are
2504 ;; available for people who want them. Shell-mode installs them:
2505 ;; (define-key shell-mode-map "\t" 'comint-dynamic-complete)
2506 ;; (define-key shell-mode-map "\M-?"
2507 ;; 'comint-dynamic-list-filename-completions)))
2509 ;; Commands like this are fine things to put in load hooks if you
2510 ;; want them present in specific modes.
2512 (defcustom comint-completion-autolist nil
2513 "*If non-nil, automatically list possibilities on partial completion.
2514 This mirrors the optional behavior of tcsh."
2515 :type 'boolean
2516 :group 'comint-completion)
2518 (defcustom comint-completion-addsuffix t
2519 "*If non-nil, add a `/' to completed directories, ` ' to file names.
2520 If a cons pair, it should be of the form (DIRSUFFIX . FILESUFFIX) where
2521 DIRSUFFIX and FILESUFFIX are strings added on unambiguous or exact completion.
2522 This mirrors the optional behavior of tcsh."
2523 :type '(choice (const :tag "None" nil)
2524 (const :tag "Add /" t)
2525 (cons :tag "Suffix pair"
2526 (string :tag "Directory suffix")
2527 (string :tag "File suffix")))
2528 :group 'comint-completion)
2530 (defcustom comint-completion-recexact nil
2531 "*If non-nil, use shortest completion if characters cannot be added.
2532 This mirrors the optional behavior of tcsh.
2534 A non-nil value is useful if `comint-completion-autolist' is non-nil too."
2535 :type 'boolean
2536 :group 'comint-completion)
2538 (defcustom comint-completion-fignore nil
2539 "*List of suffixes to be disregarded during file completion.
2540 This mirrors the optional behavior of bash and tcsh.
2542 Note that this applies to `comint-dynamic-complete-filename' only."
2543 :type '(repeat (string :tag "Suffix"))
2544 :group 'comint-completion)
2546 (defvar comint-file-name-prefix ""
2547 "Prefix prepended to absolute file names taken from process input.
2548 This is used by Comint's and shell's completion functions, and by shell's
2549 directory tracking functions.")
2551 (defvar comint-file-name-chars
2552 (if (memq system-type '(ms-dos windows-nt cygwin))
2553 "~/A-Za-z0-9_^$!#%&{}@`'.,:()-"
2554 "[]~/A-Za-z0-9+@:_.$#%,={}-")
2555 "String of characters valid in a file name.
2556 Note that all non-ASCII characters are considered valid in a file name
2557 regardless of what this variable says.
2559 This is a good thing to set in mode hooks.")
2561 (defvar comint-file-name-quote-list nil
2562 "List of characters to quote with `\\' when in a file name.
2564 This is a good thing to set in mode hooks.")
2567 (defun comint-directory (directory)
2568 "Return expanded DIRECTORY, with `comint-file-name-prefix' if absolute."
2569 (expand-file-name (if (file-name-absolute-p directory)
2570 (concat comint-file-name-prefix directory)
2571 directory)))
2574 (defun comint-word (word-chars)
2575 "Return the word of WORD-CHARS at point, or nil if none is found.
2576 Word constituents are considered to be those in WORD-CHARS, which is like the
2577 inside of a \"[...]\" (see `skip-chars-forward'),
2578 plus all non-ASCII characters."
2579 (save-excursion
2580 (let ((here (point))
2581 giveup)
2582 (while (not giveup)
2583 (let ((startpoint (point)))
2584 (skip-chars-backward (concat "\\\\" word-chars))
2585 ;; Fixme: This isn't consistent with Bash, at least -- not
2586 ;; all non-ASCII chars should be word constituents.
2587 (if (and (> (- (point) 2) (point-min))
2588 (= (char-after (- (point) 2)) ?\\))
2589 (forward-char -2))
2590 (if (and (> (- (point) 1) (point-min))
2591 (>= (char-after (- (point) 1)) 128))
2592 (forward-char -1))
2593 (if (= (point) startpoint)
2594 (setq giveup t))))
2595 ;; Set match-data to match the entire string.
2596 (when (< (point) here)
2597 (set-match-data (list (point) here))
2598 (match-string 0)))))
2600 (defun comint-substitute-in-file-name (filename)
2601 "Return FILENAME with environment variables substituted.
2602 Supports additional environment variable syntax of the command
2603 interpreter (e.g., the percent notation of cmd.exe on NT)."
2604 (let ((name (substitute-in-file-name filename)))
2605 (if (memq system-type '(ms-dos windows-nt))
2606 (let (env-var-name
2607 env-var-val)
2608 (save-match-data
2609 (while (string-match "%\\([^\\\\/]*\\)%" name)
2610 (setq env-var-name
2611 (substring name (match-beginning 1) (match-end 1)))
2612 (setq env-var-val (if (getenv env-var-name)
2613 (getenv env-var-name)
2614 ""))
2615 (setq name (replace-match env-var-val t t name))))))
2616 name))
2618 (defun comint-match-partial-filename ()
2619 "Return the filename at point, or nil if non is found.
2620 Environment variables are substituted. See `comint-word'."
2621 (let ((filename (comint-word comint-file-name-chars)))
2622 (and filename (comint-substitute-in-file-name
2623 (comint-unquote-filename filename)))))
2626 (defun comint-quote-filename (filename)
2627 "Return FILENAME with magic characters quoted.
2628 Magic characters are those in `comint-file-name-quote-list'."
2629 (if (null comint-file-name-quote-list)
2630 filename
2631 (let ((regexp
2632 (format "[%s]"
2633 (mapconcat 'char-to-string comint-file-name-quote-list ""))))
2634 (save-match-data
2635 (let ((i 0))
2636 (while (string-match regexp filename i)
2637 (setq filename (replace-match "\\\\\\&" nil nil filename))
2638 (setq i (1+ (match-end 0)))))
2639 filename))))
2641 (defun comint-unquote-filename (filename)
2642 "Return FILENAME with quoted characters unquoted."
2643 (if (null comint-file-name-quote-list)
2644 filename
2645 (save-match-data
2646 (let ((i 0))
2647 (while (string-match "\\\\\\(.\\)" filename i)
2648 (setq filename (replace-match "\\1" nil nil filename))
2649 (setq i (+ 1 (match-beginning 0)))))
2650 filename)))
2653 (defun comint-dynamic-complete ()
2654 "Dynamically perform completion at point.
2655 Calls the functions in `comint-dynamic-complete-functions' to perform
2656 completion until a function returns non-nil, at which point completion is
2657 assumed to have occurred."
2658 (interactive)
2659 (run-hook-with-args-until-success 'comint-dynamic-complete-functions))
2662 (defun comint-dynamic-complete-filename ()
2663 "Dynamically complete the filename at point.
2664 Completes if after a filename. See `comint-match-partial-filename' and
2665 `comint-dynamic-complete-as-filename'.
2666 This function is similar to `comint-replace-by-expanded-filename', except that
2667 it won't change parts of the filename already entered in the buffer; it just
2668 adds completion characters to the end of the filename. A completions listing
2669 may be shown in a help buffer if completion is ambiguous.
2671 Completion is dependent on the value of `comint-completion-addsuffix',
2672 `comint-completion-recexact' and `comint-completion-fignore', and the timing of
2673 completions listing is dependent on the value of `comint-completion-autolist'.
2675 Returns t if successful."
2676 (interactive)
2677 (when (comint-match-partial-filename)
2678 (unless (window-minibuffer-p (selected-window))
2679 (message "Completing file name..."))
2680 (comint-dynamic-complete-as-filename)))
2682 (defun comint-dynamic-complete-as-filename ()
2683 "Dynamically complete at point as a filename.
2684 See `comint-dynamic-complete-filename'. Returns t if successful."
2685 (let* ((completion-ignore-case (memq system-type '(ms-dos windows-nt cygwin)))
2686 (completion-ignored-extensions comint-completion-fignore)
2687 ;; If we bind this, it breaks remote directory tracking in rlogin.el.
2688 ;; I think it was originally bound to solve file completion problems,
2689 ;; but subsequent changes may have made this unnecessary. sm.
2690 ;;(file-name-handler-alist nil)
2691 (minibuffer-p (window-minibuffer-p (selected-window)))
2692 (success t)
2693 (dirsuffix (cond ((not comint-completion-addsuffix)
2695 ((not (consp comint-completion-addsuffix))
2696 "/")
2698 (car comint-completion-addsuffix))))
2699 (filesuffix (cond ((not comint-completion-addsuffix)
2701 ((not (consp comint-completion-addsuffix))
2702 " ")
2704 (cdr comint-completion-addsuffix))))
2705 (filename (or (comint-match-partial-filename) ""))
2706 (filedir (file-name-directory filename))
2707 (filenondir (file-name-nondirectory filename))
2708 (directory (if filedir (comint-directory filedir) default-directory))
2709 (completion (file-name-completion filenondir directory)))
2710 (cond ((null completion)
2711 (message "No completions of %s" filename)
2712 (setq success nil))
2713 ((eq completion t) ; Means already completed "file".
2714 (insert filesuffix)
2715 (unless minibuffer-p
2716 (message "Sole completion")))
2717 ((string-equal completion "") ; Means completion on "directory/".
2718 (comint-dynamic-list-filename-completions))
2719 (t ; Completion string returned.
2720 (let ((file (concat (file-name-as-directory directory) completion)))
2721 (insert (comint-quote-filename
2722 (substring (directory-file-name completion)
2723 (length filenondir))))
2724 (cond ((symbolp (file-name-completion completion directory))
2725 ;; We inserted a unique completion.
2726 (insert (if (file-directory-p file) dirsuffix filesuffix))
2727 (unless minibuffer-p
2728 (message "Completed")))
2729 ((and comint-completion-recexact comint-completion-addsuffix
2730 (string-equal filenondir completion)
2731 (file-exists-p file))
2732 ;; It's not unique, but user wants shortest match.
2733 (insert (if (file-directory-p file) dirsuffix filesuffix))
2734 (unless minibuffer-p
2735 (message "Completed shortest")))
2736 ((or comint-completion-autolist
2737 (string-equal filenondir completion))
2738 ;; It's not unique, list possible completions.
2739 (comint-dynamic-list-filename-completions))
2741 (unless minibuffer-p
2742 (message "Partially completed")))))))
2743 success))
2746 (defun comint-replace-by-expanded-filename ()
2747 "Dynamically expand and complete the filename at point.
2748 Replace the filename with an expanded, canonicalised and completed replacement.
2749 \"Expanded\" means environment variables (e.g., $HOME) and `~'s are replaced
2750 with the corresponding directories. \"Canonicalised\" means `..' and `.' are
2751 removed, and the filename is made absolute instead of relative. For expansion
2752 see `expand-file-name' and `substitute-in-file-name'. For completion see
2753 `comint-dynamic-complete-filename'."
2754 (interactive)
2755 (let ((filename (comint-match-partial-filename)))
2756 (when filename
2757 (replace-match (expand-file-name filename) t t)
2758 (comint-dynamic-complete-filename))))
2761 (defun comint-dynamic-simple-complete (stub candidates)
2762 "Dynamically complete STUB from CANDIDATES list.
2763 This function inserts completion characters at point by completing STUB from
2764 the strings in CANDIDATES. A completions listing may be shown in a help buffer
2765 if completion is ambiguous.
2767 Returns nil if no completion was inserted.
2768 Returns `sole' if completed with the only completion match.
2769 Returns `shortest' if completed with the shortest of the completion matches.
2770 Returns `partial' if completed as far as possible with the completion matches.
2771 Returns `listed' if a completion listing was shown.
2773 See also `comint-dynamic-complete-filename'."
2774 (let* ((completion-ignore-case (memq system-type '(ms-dos windows-nt cygwin)))
2775 (suffix (cond ((not comint-completion-addsuffix) "")
2776 ((not (consp comint-completion-addsuffix)) " ")
2777 (t (cdr comint-completion-addsuffix))))
2778 (completions (all-completions stub candidates)))
2779 (cond ((null completions)
2780 (message "No completions of %s" stub)
2781 nil)
2782 ((= 1 (length completions)) ; Gotcha!
2783 (let ((completion (car completions)))
2784 (if (string-equal completion stub)
2785 (message "Sole completion")
2786 (insert (substring completion (length stub)))
2787 (message "Completed"))
2788 (insert suffix)
2789 'sole))
2790 (t ; There's no unique completion.
2791 (let ((completion (try-completion stub candidates)))
2792 ;; Insert the longest substring.
2793 (insert (substring completion (length stub)))
2794 (cond ((and comint-completion-recexact comint-completion-addsuffix
2795 (string-equal stub completion)
2796 (member completion completions))
2797 ;; It's not unique, but user wants shortest match.
2798 (insert suffix)
2799 (message "Completed shortest")
2800 'shortest)
2801 ((or comint-completion-autolist
2802 (string-equal stub completion))
2803 ;; It's not unique, list possible completions.
2804 (comint-dynamic-list-completions completions)
2805 'listed)
2807 (message "Partially completed")
2808 'partial)))))))
2811 (defun comint-dynamic-list-filename-completions ()
2812 "List in help buffer possible completions of the filename at point."
2813 (interactive)
2814 (let* ((completion-ignore-case (memq system-type '(ms-dos windows-nt cygwin)))
2815 ;; If we bind this, it breaks remote directory tracking in rlogin.el.
2816 ;; I think it was originally bound to solve file completion problems,
2817 ;; but subsequent changes may have made this unnecessary. sm.
2818 ;;(file-name-handler-alist nil)
2819 (filename (or (comint-match-partial-filename) ""))
2820 (filedir (file-name-directory filename))
2821 (filenondir (file-name-nondirectory filename))
2822 (directory (if filedir (comint-directory filedir) default-directory))
2823 (completions (file-name-all-completions filenondir directory)))
2824 (if (not completions)
2825 (message "No completions of %s" filename)
2826 (comint-dynamic-list-completions
2827 (mapcar 'comint-quote-filename completions)))))
2830 ;; This is bound locally in a *Completions* buffer to the list of
2831 ;; completions displayed, and is used to detect the case where the same
2832 ;; command is repeatedly used without the set of completions changing.
2833 (defvar comint-displayed-dynamic-completions nil)
2835 (defvar comint-dynamic-list-completions-config nil)
2837 (defun comint-dynamic-list-completions (completions)
2838 "List in help buffer sorted COMPLETIONS.
2839 Typing SPC flushes the help buffer."
2840 (let ((window (get-buffer-window "*Completions*")))
2841 (setq completions (sort completions 'string-lessp))
2842 (if (and (eq last-command this-command)
2843 window (window-live-p window) (window-buffer window)
2844 (buffer-name (window-buffer window))
2845 ;; The above tests are not sufficient to detect the case where we
2846 ;; should scroll, because the top-level interactive command may
2847 ;; not have displayed a completions window the last time it was
2848 ;; invoked, and there may be such a window left over from a
2849 ;; previous completion command with a different set of
2850 ;; completions. To detect that case, we also test that the set
2851 ;; of displayed completions is in fact the same as the previously
2852 ;; displayed set.
2853 (equal completions
2854 (buffer-local-value 'comint-displayed-dynamic-completions
2855 (window-buffer window))))
2856 ;; If this command was repeated, and
2857 ;; there's a fresh completion window with a live buffer,
2858 ;; and this command is repeated, scroll that window.
2859 (with-current-buffer (window-buffer window)
2860 (if (pos-visible-in-window-p (point-max) window)
2861 (set-window-start window (point-min))
2862 (save-selected-window
2863 (select-window window)
2864 (scroll-up))))
2866 ;; Display a completion list for the first time.
2867 (setq comint-dynamic-list-completions-config
2868 (current-window-configuration))
2869 (with-output-to-temp-buffer "*Completions*"
2870 (display-completion-list completions))
2871 (message "Type space to flush; repeat completion command to scroll"))
2873 ;; Read the next key, to process SPC.
2874 (let (key first)
2875 (if (save-excursion
2876 (set-buffer (get-buffer "*Completions*"))
2877 (set (make-local-variable
2878 'comint-displayed-dynamic-completions)
2879 completions)
2880 (setq key (read-key-sequence nil)
2881 first (aref key 0))
2882 (and (consp first) (consp (event-start first))
2883 (eq (window-buffer (posn-window (event-start first)))
2884 (get-buffer "*Completions*"))
2885 (eq (key-binding key) 'mouse-choose-completion)))
2886 ;; If the user does mouse-choose-completion with the mouse,
2887 ;; execute the command, then delete the completion window.
2888 (progn
2889 (mouse-choose-completion first)
2890 (set-window-configuration comint-dynamic-list-completions-config))
2891 (unless (eq first ?\ )
2892 (setq unread-command-events (listify-key-sequence key)))
2893 (unless (eq first ?\t)
2894 (set-window-configuration comint-dynamic-list-completions-config))))))
2897 (defun comint-get-next-from-history ()
2898 "After fetching a line from input history, this fetches the following line.
2899 In other words, this recalls the input line after the line you recalled last.
2900 You can use this to repeat a sequence of input lines."
2901 (interactive)
2902 (if comint-save-input-ring-index
2903 (progn
2904 (setq comint-input-ring-index (1+ comint-save-input-ring-index))
2905 (comint-next-input 1))
2906 (message "No previous history command")))
2908 (defun comint-accumulate ()
2909 "Accumulate a line to send as input along with more lines.
2910 This inserts a newline so that you can enter more text
2911 to be sent along with this line. Use \\[comint-send-input]
2912 to send all the accumulated input, at once.
2913 The entire accumulated text becomes one item in the input history
2914 when you send it."
2915 (interactive)
2916 (insert "\n")
2917 (set-marker comint-accum-marker (point))
2918 (if comint-input-ring-index
2919 (setq comint-save-input-ring-index
2920 (- comint-input-ring-index 1))))
2922 (defun comint-goto-process-mark ()
2923 "Move point to the process mark.
2924 The process mark separates output, and input already sent,
2925 from input that has not yet been sent."
2926 (interactive)
2927 (let ((proc (or (get-buffer-process (current-buffer))
2928 (error "Current buffer has no process"))))
2929 (goto-char (process-mark proc))
2930 (when (interactive-p)
2931 (message "Point is now at the process mark"))))
2933 (defun comint-bol-or-process-mark ()
2934 "Move point to beginning of line (after prompt) or to the process mark.
2935 The first time you use this command, it moves to the beginning of the line
2936 \(but after the prompt, if any). If you repeat it again immediately,
2937 it moves point to the process mark.
2939 The process mark separates the process output, along with input already sent,
2940 from input that has not yet been sent. Ordinarily, the process mark
2941 is at the beginning of the current input line; but if you have
2942 used \\[comint-accumulate] to send multiple lines at once,
2943 the process mark is at the beginning of the accumulated input."
2944 (interactive)
2945 (if (not (eq last-command 'comint-bol-or-process-mark))
2946 (comint-bol nil)
2947 (comint-goto-process-mark)))
2949 (defun comint-set-process-mark ()
2950 "Set the process mark at point."
2951 (interactive)
2952 (let ((proc (or (get-buffer-process (current-buffer))
2953 (error "Current buffer has no process"))))
2954 (set-marker (process-mark proc) (point))
2955 (message "Process mark set")))
2958 ;; Author: Peter Breton <pbreton@cs.umb.edu>
2960 ;; This little add-on for comint is intended to make it easy to get
2961 ;; output from currently active comint buffers into another buffer,
2962 ;; or buffers, and then go back to using the comint shell.
2964 ;; My particular use is SQL interpreters; I want to be able to execute a
2965 ;; query using the process associated with a comint-buffer, and save that
2966 ;; somewhere else. Because the process might have state (for example, it
2967 ;; could be in an uncommitted transaction), just running starting a new
2968 ;; process and having it execute the query and then finish, would not
2969 ;; work. I'm sure there are other uses as well, although in many cases
2970 ;; starting a new process is the simpler, and thus preferable, approach.
2972 ;; The basic implementation is as follows: comint-redirect changes the
2973 ;; preoutput filter functions (`comint-preoutput-filter-functions') to use
2974 ;; its own filter. The filter puts the output into the designated buffer,
2975 ;; or buffers, until it sees a regexp that tells it to stop (by default,
2976 ;; this is the prompt for the interpreter, `comint-prompt-regexp'). When it
2977 ;; sees the stop regexp, it restores the old filter functions, and runs
2978 ;; `comint-redirect-hook'.
2980 ;; Each comint buffer may only use one redirection at a time, but any number
2981 ;; of different comint buffers may be simultaneously redirected.
2983 ;; NOTE: It is EXTREMELY important that `comint-prompt-regexp' be set to the
2984 ;; correct prompt for your interpreter, or that you supply a regexp that says
2985 ;; when the redirection is finished. Otherwise, redirection will continue
2986 ;; indefinitely. The code now does a sanity check to ensure that it can find
2987 ;; a prompt in the comint buffer; however, it is still important to ensure that
2988 ;; this prompt is set correctly.
2990 ;; XXX: This doesn't work so well unless `comint-prompt-regexp' is set;
2991 ;; perhaps it should prompt for a terminating string (with an
2992 ;; appropriate magic default by examining what we think is the prompt)?
2994 ;; Fixme: look for appropriate fields, rather than regexp, if
2995 ;; `comint-use-prompt-regexp-instead-of-fields' is true.
2997 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2998 ;; Variables
2999 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3001 (defcustom comint-redirect-verbose nil
3002 "*If non-nil, print messages each time the redirection filter is invoked.
3003 Also print a message when redirection is completed."
3004 :group 'comint
3005 :type 'boolean)
3007 ;; Directly analagous to comint-preoutput-filter-functions
3008 (defvar comint-redirect-filter-functions nil
3009 "List of functions to call before inserting redirected process output.
3010 Each function gets one argument, a string containing the text received
3011 from the subprocess. It should return the string to insert, perhaps
3012 the same string that was received, or perhaps a modified or transformed
3013 string.
3015 The functions on the list are called sequentially, and each one is given
3016 the string returned by the previous one. The string returned by the
3017 last function is the text that is actually inserted in the redirection buffer.
3019 You can use `add-hook' to add functions to this list
3020 either globally or locally.")
3022 ;; Internal variables
3024 (defvar comint-redirect-output-buffer nil
3025 "The buffer or list of buffers to put output into.")
3027 (defvar comint-redirect-finished-regexp nil
3028 "Regular expression that determines when to stop redirection in Comint.
3029 When the redirection filter function is given output that matches this regexp,
3030 the output is inserted as usual, and redirection is completed.")
3032 (defvar comint-redirect-insert-matching-regexp nil
3033 "If non-nil, the text that ends a redirection is included in it.
3034 More precisely, the text that matches `comint-redirect-finished-regexp'
3035 and therefore terminates an output redirection is inserted in the
3036 redirection target buffer, along with the preceding output.")
3038 (defvar comint-redirect-echo-input nil
3039 "Non-nil means echo input in the process buffer even during redirection.")
3041 (defvar comint-redirect-completed nil
3042 "Non-nil if redirection has completed in the current buffer.")
3044 (defvar comint-redirect-original-mode-line-process nil
3045 "Original mode line for redirected process.")
3047 (defvar comint-redirect-perform-sanity-check t
3048 "If non-nil, check that redirection is likely to complete successfully.
3049 More precisely, before starting a redirection, verify that the
3050 regular expression `comint-redirect-finished-regexp' that controls
3051 when to terminate it actually matches some text already in the process
3052 buffer. The idea is that this regular expression should match a prompt
3053 string, and that there ought to be at least one copy of your prompt string
3054 in the process buffer already.")
3056 (defvar comint-redirect-original-filter-function nil
3057 "The process filter that was in place when redirection is started.
3058 When redirection is completed, the process filter is restored to
3059 this value.")
3061 (defvar comint-redirect-subvert-readonly nil
3062 "Non-nil means comint-redirect can insert into otherwise-readonly buffers.
3063 The readonly status is toggled around insertion.
3064 This is useful, for instance, for insertion into Help mode buffers.
3065 You probably want to set it locally to the output buffer.")
3067 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3068 ;; Functions
3069 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3071 (defun comint-redirect-setup (output-buffer
3072 comint-buffer
3073 finished-regexp
3074 &optional echo-input)
3075 "Set up for output redirection.
3076 This function sets local variables that are used by `comint-redirect-filter'
3077 to perform redirection.
3079 Output from COMINT-BUFFER is redirected to OUTPUT-BUFFER, until something
3080 in the output matches FINISHED-REGEXP.
3082 If optional argument ECHO-INPUT is non-nil, output is echoed to the
3083 original Comint buffer.
3085 This function is called by `comint-redirect-send-command-to-process',
3086 and does not normally need to be invoked by the end user or programmer."
3087 (with-current-buffer comint-buffer
3089 (make-local-variable 'comint-redirect-original-mode-line-process)
3090 (setq comint-redirect-original-mode-line-process mode-line-process)
3092 (make-local-variable 'comint-redirect-output-buffer)
3093 (setq comint-redirect-output-buffer output-buffer)
3095 (make-local-variable 'comint-redirect-finished-regexp)
3096 (setq comint-redirect-finished-regexp finished-regexp)
3098 (make-local-variable 'comint-redirect-echo-input)
3099 (setq comint-redirect-echo-input echo-input)
3101 (make-local-variable 'comint-redirect-completed)
3102 (setq comint-redirect-completed nil)
3104 (setq mode-line-process
3105 (if mode-line-process
3106 (list (concat (elt mode-line-process 0) " Redirection"))
3107 (list ":%s Redirection")))))
3109 (defun comint-redirect-cleanup ()
3110 "End a Comint redirection. See `comint-redirect-send-command'."
3111 (interactive)
3112 ;; Restore the process filter
3113 (set-process-filter (get-buffer-process (current-buffer))
3114 comint-redirect-original-filter-function)
3115 ;; Restore the mode line
3116 (setq mode-line-process comint-redirect-original-mode-line-process)
3117 ;; Set the completed flag
3118 (setq comint-redirect-completed t))
3120 ;; Because the cleanup happens as a callback, it's not easy to guarantee
3121 ;; that it really occurs.
3122 (defalias 'comint-redirect-remove-redirection 'comint-redirect-cleanup)
3124 (defun comint-redirect-filter (process input-string)
3125 "Filter function which redirects output from PROCESS to a buffer or buffers.
3126 The variable `comint-redirect-output-buffer' says which buffer(s) to
3127 place output in.
3129 INPUT-STRING is the input from the Comint process.
3131 This function runs as a process filter, and does not need to be invoked by the
3132 end user."
3133 (and process
3134 (with-current-buffer (process-buffer process)
3135 (comint-redirect-preoutput-filter input-string)
3136 ;; If we have to echo output, give it to the original filter function
3137 (and comint-redirect-echo-input
3138 comint-redirect-original-filter-function
3139 (funcall comint-redirect-original-filter-function
3140 process input-string)))))
3143 (defun comint-redirect-preoutput-filter (input-string)
3144 "Comint filter function which redirects Comint output to a buffer or buffers.
3145 The variable `comint-redirect-output-buffer' says which buffer(s) to
3146 place output in.
3148 INPUT-STRING is the input from the Comint process.
3150 This function does not need to be invoked by the end user."
3151 (let ((output-buffer-list
3152 (if (listp comint-redirect-output-buffer)
3153 comint-redirect-output-buffer
3154 (list comint-redirect-output-buffer)))
3155 (filtered-input-string input-string))
3157 ;; If there are any filter functions, give them a chance to modify the string
3158 (let ((functions comint-redirect-filter-functions))
3159 (while (and functions filtered-input-string)
3160 (if (eq (car functions) t)
3161 ;; If a local value says "use the default value too",
3162 ;; do that.
3163 (let ((functions (default-value 'comint-redirect-filter-functions)))
3164 (while (and functions filtered-input-string)
3165 (setq filtered-input-string
3166 (funcall (car functions) filtered-input-string))
3167 (setq functions (cdr functions))))
3168 (setq filtered-input-string
3169 (funcall (car functions) filtered-input-string)))
3170 (setq functions (cdr functions))))
3172 ;; Clobber `comint-redirect-finished-regexp'
3173 (or comint-redirect-insert-matching-regexp
3174 (and (string-match comint-redirect-finished-regexp filtered-input-string)
3175 (setq filtered-input-string
3176 (replace-match "" nil nil filtered-input-string))))
3178 ;; Send output to all registered buffers
3179 (save-excursion
3180 (dolist (buf output-buffer-list)
3181 ;; Set this buffer to the output buffer
3182 (set-buffer (get-buffer-create buf))
3183 ;; Go to the end of the buffer
3184 (goto-char (point-max))
3185 ;; Insert the output
3186 (let ((inhibit-read-only comint-redirect-subvert-readonly))
3187 (insert filtered-input-string))))
3189 ;; Message
3190 (and comint-redirect-verbose
3191 (message "Redirected output to buffer(s) %s"
3192 (mapconcat 'identity output-buffer-list " ")))
3194 ;; If we see the prompt, tidy up
3195 ;; We'll look for the prompt in the original string, so nobody can
3196 ;; clobber it
3197 (and (string-match comint-redirect-finished-regexp input-string)
3198 (progn
3199 (and comint-redirect-verbose
3200 (message "Redirection completed"))
3201 (comint-redirect-cleanup)
3202 (run-hooks 'comint-redirect-hook)))
3203 ;; Echo input?
3204 (if comint-redirect-echo-input
3205 filtered-input-string
3206 "")))
3208 ;;;###autoload
3209 (defun comint-redirect-send-command (command output-buffer echo &optional no-display)
3210 "Send COMMAND to process in current buffer, with output to OUTPUT-BUFFER.
3211 With prefix arg ECHO, echo output in process buffer.
3213 If NO-DISPLAY is non-nil, do not show the output buffer."
3214 (interactive "sCommand: \nBOutput Buffer: \nP")
3215 (let ((process (get-buffer-process (current-buffer))))
3216 (if process
3217 (comint-redirect-send-command-to-process
3218 command output-buffer (current-buffer) echo no-display)
3219 (error "No process for current buffer"))))
3221 ;;;###autoload
3222 (defun comint-redirect-send-command-to-process
3223 (command output-buffer process echo &optional no-display)
3224 "Send COMMAND to PROCESS, with output to OUTPUT-BUFFER.
3225 With prefix arg, echo output in process buffer.
3227 If NO-DISPLAY is non-nil, do not show the output buffer."
3228 (interactive "sCommand: \nBOutput Buffer: \nbProcess Buffer: \nP")
3229 (let* (;; The process buffer
3230 (process-buffer (if (processp process)
3231 (process-buffer process)
3232 process))
3233 (proc (get-buffer-process process-buffer)))
3234 ;; Change to the process buffer
3235 (with-current-buffer process-buffer
3237 ;; Make sure there's a prompt in the current process buffer
3238 (and comint-redirect-perform-sanity-check
3239 (save-excursion
3240 (goto-char (point-max))
3241 (or (re-search-backward comint-prompt-regexp nil t)
3242 (error "No prompt found or `comint-prompt-regexp' not set properly"))))
3244 ;;;;;;;;;;;;;;;;;;;;;
3245 ;; Set up for redirection
3246 ;;;;;;;;;;;;;;;;;;;;;
3247 (comint-redirect-setup
3248 ;; Output Buffer
3249 output-buffer
3250 ;; Comint Buffer
3251 (current-buffer)
3252 ;; Finished Regexp
3253 comint-prompt-regexp
3254 ;; Echo input
3255 echo)
3257 ;;;;;;;;;;;;;;;;;;;;;
3258 ;; Set the filter
3259 ;;;;;;;;;;;;;;;;;;;;;
3260 ;; Save the old filter
3261 (setq comint-redirect-original-filter-function
3262 (process-filter proc))
3263 (set-process-filter proc 'comint-redirect-filter)
3265 ;;;;;;;;;;;;;;;;;;;;;
3266 ;; Send the command
3267 ;;;;;;;;;;;;;;;;;;;;;
3268 (process-send-string
3269 (current-buffer)
3270 (concat command "\n"))
3272 ;;;;;;;;;;;;;;;;;;;;;
3273 ;; Show the output
3274 ;;;;;;;;;;;;;;;;;;;;;
3275 (or no-display
3276 (display-buffer
3277 (get-buffer-create
3278 (if (listp output-buffer)
3279 (car output-buffer)
3280 output-buffer)))))))
3282 ;;;###autoload
3283 (defun comint-redirect-results-list (command regexp regexp-group)
3284 "Send COMMAND to current process.
3285 Return a list of expressions in the output which match REGEXP.
3286 REGEXP-GROUP is the regular expression group in REGEXP to use."
3287 (comint-redirect-results-list-from-process
3288 (get-buffer-process (current-buffer))
3289 command regexp regexp-group))
3291 ;;;###autoload
3292 (defun comint-redirect-results-list-from-process (process command regexp regexp-group)
3293 "Send COMMAND to PROCESS.
3294 Return a list of expressions in the output which match REGEXP.
3295 REGEXP-GROUP is the regular expression group in REGEXP to use."
3296 (let ((output-buffer " *Comint Redirect Work Buffer*")
3297 results)
3298 (save-excursion
3299 (set-buffer (get-buffer-create output-buffer))
3300 (erase-buffer)
3301 (comint-redirect-send-command-to-process command
3302 output-buffer process nil t)
3303 ;; Wait for the process to complete
3304 (set-buffer (process-buffer process))
3305 (while (null comint-redirect-completed)
3306 (accept-process-output nil 1))
3307 ;; Collect the output
3308 (set-buffer output-buffer)
3309 (goto-char (point-min))
3310 ;; Skip past the command, if it was echoed
3311 (and (looking-at command)
3312 (forward-line))
3313 (while (re-search-forward regexp nil t)
3314 (setq results
3315 (cons (buffer-substring-no-properties
3316 (match-beginning regexp-group)
3317 (match-end regexp-group))
3318 results)))
3319 results)))
3321 (mapc (lambda (x)
3322 (add-to-list 'debug-ignored-errors x))
3323 '("^Not at command line$"
3324 "^Empty input ring$"
3325 "^No history$"
3326 "^Not found$" ; Too common?
3327 "^Current buffer has no process$"))
3330 ;; Converting process modes to use comint mode
3331 ;; ===========================================================================
3332 ;; The code in the Emacs 19 distribution has all been modified to use comint
3333 ;; where needed. However, there are `third-party' packages out there that
3334 ;; still use the old shell mode. Here's a guide to conversion.
3336 ;; Renaming variables
3337 ;; Most of the work is renaming variables and functions. These are the common
3338 ;; ones:
3339 ;; Local variables:
3340 ;; last-input-start comint-last-input-start
3341 ;; last-input-end comint-last-input-end
3342 ;; shell-prompt-pattern comint-prompt-regexp
3343 ;; shell-set-directory-error-hook <no equivalent>
3344 ;; Miscellaneous:
3345 ;; shell-set-directory <unnecessary>
3346 ;; shell-mode-map comint-mode-map
3347 ;; Commands:
3348 ;; shell-send-input comint-send-input
3349 ;; shell-send-eof comint-delchar-or-maybe-eof
3350 ;; kill-shell-input comint-kill-input
3351 ;; interrupt-shell-subjob comint-interrupt-subjob
3352 ;; stop-shell-subjob comint-stop-subjob
3353 ;; quit-shell-subjob comint-quit-subjob
3354 ;; kill-shell-subjob comint-kill-subjob
3355 ;; kill-output-from-shell comint-delete-output
3356 ;; show-output-from-shell comint-show-output
3357 ;; copy-last-shell-input Use comint-previous-input/comint-next-input
3359 ;; SHELL-SET-DIRECTORY is gone, its functionality taken over by
3360 ;; SHELL-DIRECTORY-TRACKER, the shell mode's comint-input-filter-functions.
3361 ;; Comint mode does not provide functionality equivalent to
3362 ;; shell-set-directory-error-hook; it is gone.
3364 ;; comint-last-input-start is provided for modes which want to munge
3365 ;; the buffer after input is sent, perhaps because the inferior
3366 ;; insists on echoing the input. The LAST-INPUT-START variable in
3367 ;; the old shell package was used to implement a history mechanism,
3368 ;; but you should think twice before using comint-last-input-start
3369 ;; for this; the input history ring often does the job better.
3371 ;; If you are implementing some process-in-a-buffer mode, called foo-mode, do
3372 ;; *not* create the comint-mode local variables in your foo-mode function.
3373 ;; This is not modular. Instead, call comint-mode, and let *it* create the
3374 ;; necessary comint-specific local variables. Then create the
3375 ;; foo-mode-specific local variables in foo-mode. Set the buffer's keymap to
3376 ;; be foo-mode-map, and its mode to be foo-mode. Set the comint-mode hooks
3377 ;; (comint-{prompt-regexp, input-filter, input-filter-functions,
3378 ;; get-old-input) that need to be different from the defaults. Call
3379 ;; foo-mode-hook, and you're done. Don't run the comint-mode hook yourself;
3380 ;; comint-mode will take care of it. The following example, from shell.el,
3381 ;; is typical:
3383 ;; (defvar shell-mode-map '())
3384 ;; (cond ((not shell-mode-map)
3385 ;; (setq shell-mode-map (copy-keymap comint-mode-map))
3386 ;; (define-key shell-mode-map "\C-c\C-f" 'shell-forward-command)
3387 ;; (define-key shell-mode-map "\C-c\C-b" 'shell-backward-command)
3388 ;; (define-key shell-mode-map "\t" 'comint-dynamic-complete)
3389 ;; (define-key shell-mode-map "\M-?"
3390 ;; 'comint-dynamic-list-filename-completions)))
3392 ;; (defun shell-mode ()
3393 ;; (interactive)
3394 ;; (comint-mode)
3395 ;; (setq comint-prompt-regexp shell-prompt-pattern)
3396 ;; (setq major-mode 'shell-mode)
3397 ;; (setq mode-name "Shell")
3398 ;; (use-local-map shell-mode-map)
3399 ;; (make-local-variable 'shell-directory-stack)
3400 ;; (setq shell-directory-stack nil)
3401 ;; (add-hook 'comint-input-filter-functions 'shell-directory-tracker)
3402 ;; (run-hooks 'shell-mode-hook))
3405 ;; Completion for comint-mode users
3407 ;; For modes that use comint-mode, comint-dynamic-complete-functions is the
3408 ;; hook to add completion functions to. Functions on this list should return
3409 ;; non-nil if completion occurs (i.e., further completion should not occur).
3410 ;; You could use comint-dynamic-simple-complete to do the bulk of the
3411 ;; completion job.
3414 (provide 'comint)
3416 ;;; arch-tag: 1793314c-09db-40be-9549-9aeae3e75164
3417 ;;; comint.el ends here