(pascal-font-lock-keywords): New variable.
[bpt/emacs.git] / lisp / comint.el
CommitLineData
c0274f38 1;;; comint.el --- general command interpreter in a window stuff
e41b2db1 2
42453245 3;; Copyright (C) 1988, 1990, 1992, 1993, 1994 Free Software Foundation, Inc.
c0274f38 4
1586b965 5;; Author: Olin Shivers <shivers@cs.cmu.edu>
3ee91e68 6;; Adapted-by: Simon Marshall <s.marshall@dcs.hull.ac.uk>
e9571d2a 7;; Keywords: processes
3a801d0c 8
1586b965 9;; This file is part of GNU Emacs.
be9b65ac 10
1586b965
RS
11;; GNU Emacs is free software; you can redistribute it and/or modify
12;; it under the terms of the GNU General Public License as published by
13;; the Free Software Foundation; either version 2, or (at your option)
14;; any later version.
be9b65ac 15
1586b965
RS
16;; GNU Emacs is distributed in the hope that it will be useful,
17;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19;; GNU General Public License for more details.
be9b65ac 20
1586b965
RS
21;; You should have received a copy of the GNU General Public License
22;; along with GNU Emacs; see the file COPYING. If not, write to
23;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
be9b65ac 24
e5167999
ER
25;;; Commentary:
26
49116ac0
JB
27;;; Please send me bug reports, bug fixes, and extensions, so that I can
28;;; merge them into the master source.
29;;; - Olin Shivers (shivers@cs.cmu.edu)
3ee91e68 30;;; - Simon Marshall (s.marshall@dcs.hull.ac.uk)
49116ac0 31
be9b65ac
DL
32;;; This file defines a general command-interpreter-in-a-buffer package
33;;; (comint mode). The idea is that you can build specific process-in-a-buffer
34;;; modes on top of comint mode -- e.g., lisp, shell, scheme, T, soar, ....
35;;; This way, all these specific packages share a common base functionality,
36;;; and a common set of bindings, which makes them easier to use (and
37;;; saves code, implementation time, etc., etc.).
38
49116ac0 39;;; Several packages are already defined using comint mode:
735b3061 40;;; - shell.el defines a shell-in-a-buffer mode.
49116ac0 41;;; - cmulisp.el defines a simple lisp-in-a-buffer mode.
49116ac0
JB
42;;;
43;;; - The file cmuscheme.el defines a scheme-in-a-buffer mode.
44;;; - The file tea.el tunes scheme and inferior-scheme modes for T.
45;;; - The file soar.el tunes lisp and inferior-lisp modes for Soar.
46;;; - cmutex.el defines tex and latex modes that invoke tex, latex, bibtex,
47;;; previewers, and printers from within emacs.
48;;; - background.el allows csh-like job control inside emacs.
49;;; It is pretty easy to make new derived modes for other processes.
50
be9b65ac
DL
51;;; For documentation on the functionality provided by comint mode, and
52;;; the hooks available for customising it, see the comments below.
53;;; For further information on the standard derived modes (shell,
54;;; inferior-lisp, inferior-scheme, ...), see the relevant source files.
55
49116ac0
JB
56;;; For hints on converting existing process modes (e.g., tex-mode,
57;;; background, dbx, gdb, kermit, prolog, telnet) to use comint-mode
be9b65ac
DL
58;;; instead of shell-mode, see the notes at the end of this file.
59
be9b65ac 60\f
49116ac0
JB
61;;; Brief Command Documentation:
62;;;============================================================================
735b3061 63;;; Comint Mode Commands: (common to all derived modes, like shell & cmulisp
49116ac0
JB
64;;; mode)
65;;;
66;;; m-p comint-previous-input Cycle backwards in input history
67;;; m-n comint-next-input Cycle forwards
3ee91e68
RS
68;;; m-r comint-previous-matching-input Previous input matching a regexp
69;;; m-s comint-next-matching-input Next input that matches
d637fa56 70;;; m-c-l comint-show-output Show last batch of process output
49116ac0 71;;; return comint-send-input
3ee91e68 72;;; c-d comint-delchar-or-maybe-eof Delete char unless at end of buff
a77db92b 73;;; c-c c-a comint-bol Beginning of line; skip prompt
49116ac0
JB
74;;; c-c c-u comint-kill-input ^u
75;;; c-c c-w backward-kill-word ^w
76;;; c-c c-c comint-interrupt-subjob ^c
77;;; c-c c-z comint-stop-subjob ^z
78;;; c-c c-\ comint-quit-subjob ^\
79;;; c-c c-o comint-kill-output Delete last batch of process output
80;;; c-c c-r comint-show-output Show last batch of process output
3ee91e68 81;;; c-c c-h comint-dynamic-list-input-ring List input history
49116ac0 82;;;
3ee91e68 83;;; Not bound by default in comint-mode (some are in shell mode)
21d9bf27 84;;; comint-run Run a program under comint-mode
be9b65ac 85;;; send-invisible Read a line w/o echo, and send to proc
3ee91e68
RS
86;;; comint-dynamic-complete-filename Complete filename at point.
87;;; comint-dynamic-complete-variable Complete variable name at point.
88;;; comint-dynamic-list-filename-completions List completions in help buffer.
be9b65ac
DL
89;;; comint-replace-by-expanded-filename Expand and complete filename at point;
90;;; replace with expanded/completed name.
3ee91e68
RS
91;;; comint-replace-by-expanded-history Expand history at point;
92;;; replace with expanded name.
93;;; comint-magic-space Expand history and add (a) space(s).
49116ac0 94;;; comint-kill-subjob No mercy.
3ee91e68 95;;; comint-show-maximum-output Show as much output as possible.
49116ac0
JB
96;;; comint-continue-subjob Send CONT signal to buffer's process
97;;; group. Useful if you accidentally
98;;; suspend your process (with C-c C-z).
49116ac0
JB
99
100;;; comint-mode-hook is the comint mode hook. Basically for your keybindings.
be9b65ac 101
e5167999
ER
102;;; Code:
103
c9706a52 104(require 'ring)
be9b65ac
DL
105\f
106;;; Buffer Local Variables:
107;;;============================================================================
108;;; Comint mode buffer local variables:
3ee91e68
RS
109;;; comint-prompt-regexp - string comint-bol uses to match prompt
110;;; comint-delimiter-argument-list - list For delimiters and arguments
111;;; comint-last-input-start - marker Handy if inferior always echoes
49116ac0 112;;; comint-last-input-end - marker For comint-kill-output command
e6dfdce5
RS
113;;; comint-input-ring-size - integer For the input history
114;;; comint-input-ring - ring mechanism
0c5c1f34 115;;; comint-input-ring-index - number ...
3ee91e68
RS
116;;; comint-input-autoexpand - symbol ...
117;;; comint-input-ignoredups - boolean ...
be9b65ac 118;;; comint-last-input-match - string ...
1b421826 119;;; comint-dynamic-complete-functions - hook For the completion mechanism
497513ad 120;;; comint-completion-fignore - list ...
be9b65ac 121;;; comint-get-old-input - function Hooks for specific
fb9ab28a
RS
122;;; comint-input-filter-functions - hook process-in-a-buffer
123;;; comint-output-filter-functions - hook function modes.
3ee91e68
RS
124;;; comint-input-filter - function ...
125;;; comint-input-send - function ...
126;;; comint-eol-on-send - boolean ...
127;;; comint-process-echoes - boolean ...
fdb7791d 128;;; comint-scroll-to-bottom-on-input - symbol For scroll behavior
1b421826
RS
129;;; comint-scroll-to-bottom-on-output - symbol ...
130;;; comint-scroll-show-maximum-output - boolean...
3ee91e68
RS
131;;;
132;;; Comint mode non-buffer local variables:
1b421826
RS
133;;; comint-completion-addsuffix - boolean For file name completion
134;;; comint-completion-autolist - boolean behavior
135;;; comint-completion-recexact - boolean ...
be9b65ac 136
49116ac0
JB
137(defvar comint-prompt-regexp "^"
138 "Regexp to recognise prompts in the inferior process.
139Defaults to \"^\", the null string at BOL.
be9b65ac
DL
140
141Good choices:
d637fa56 142 Canonical Lisp: \"^[^> \\n]*>+:? *\" (Lucid, franz, kcl, T, cscheme, oaklisp)
41752cf1
JB
143 Lucid Common Lisp: \"^\\\\(>\\\\|\\\\(->\\\\)+\\\\) *\"
144 franz: \"^\\\\(->\\\\|<[0-9]*>:\\\\) *\"
49116ac0 145 kcl: \"^>+ *\"
d637fa56 146 shell: \"^[^#$%>\\n]*[#$%>] *\"
49116ac0 147 T: \"^>+ *\"
be9b65ac 148
49116ac0 149This is a good thing to set in mode hooks.")
be9b65ac 150
3ee91e68
RS
151(defvar comint-delimiter-argument-list ()
152 "List of characters to recognise as separate arguments in input.
153Strings comprising a character in this list will separate the arguments
154surrounding them, and also be regarded as arguments in their own right (unlike
155whitespace). See `comint-arguments'.
3f4b7c36 156Defaults to the empty list.
3ee91e68 157
3f4b7c36 158For shells, a good value is (?\\| ?& ?< ?> ?\\( ?\\) ?;).
3ee91e68
RS
159
160This is a good thing to set in mode hooks.")
161
94bd6573 162(defvar comint-input-autoexpand nil
3ee91e68 163 "*If non-nil, expand input command history references on completion.
fdb7791d 164This mirrors the optional behavior of tcsh (its autoexpand and histlit).
3ee91e68
RS
165
166If the value is `input', then the expansion is seen on input.
167If the value is `history', then the expansion is only when inserting
168into the buffer's input ring. See also `comint-magic-space' and
169`comint-dynamic-complete'.
170
171This variable is buffer-local.")
172
173(defvar comint-input-ignoredups nil
174 "*If non-nil, don't add input matching the last on the input ring.
fdb7791d 175This mirrors the optional behavior of bash.
3ee91e68
RS
176
177This variable is buffer-local.")
178
179(defvar comint-input-ring-file-name nil
180 "*If non-nil, name of the file to read/write input history.
181See also `comint-read-input-ring' and `comint-write-input-ring'.
182
183This variable is buffer-local, and is a good thing to set in mode hooks.")
184
91d75282 185(defvar comint-scroll-to-bottom-on-input nil
3ee91e68
RS
186 "*Controls whether input to interpreter causes window to scroll.
187If nil, then do not scroll. If t or `all', scroll all windows showing buffer.
188If `this', scroll only the selected window.
189
91d75282 190The default is nil.
f4ef65c7 191
3ee91e68
RS
192See `comint-preinput-scroll-to-bottom'. This variable is buffer-local.")
193
91d75282 194(defvar comint-scroll-to-bottom-on-output nil
3ee91e68
RS
195 "*Controls whether interpreter output causes window to scroll.
196If nil, then do not scroll. If t or `all', scroll all windows showing buffer.
197If `this', scroll only the selected window.
198If `others', scroll only those that are not the selected window.
91d75282
RS
199
200The default is nil.
f4ef65c7 201
3ee91e68
RS
202See variable `comint-scroll-show-maximum-output' and function
203`comint-postoutput-scroll-to-bottom'. This variable is buffer-local.")
91d75282 204
c541b39e 205(defvar comint-scroll-show-maximum-output nil
3ee91e68
RS
206 "*Controls how interpreter output causes window to scroll.
207If non-nil, then show the maximum output when the window is scrolled.
91d75282 208
3ee91e68
RS
209See variable `comint-scroll-to-bottom-on-output' and function
210`comint-postoutput-scroll-to-bottom'. This variable is buffer-local.")
91d75282 211
3ee91e68 212(defvar comint-input-ring-size 32
49116ac0 213 "Size of input history ring.")
be9b65ac 214
8ae3bc9f 215(defvar comint-process-echoes nil
cccd719e 216 "*If non-nil, assume that the subprocess echoes any input.
8ae3bc9f 217If so, delete one copy of the input so that only one copy eventually
3ee91e68 218appears in the buffer.
8ae3bc9f
NF
219
220This variable is buffer-local.")
221
7782a0c6
RS
222(defvar comint-password-prompt-regexp
223 "\\(^[Pp]assword\\|pass phrase\\):\\s *\\'"
654d89d7 224 "*Regexp matching prompts for passwords in the inferior process.
7782a0c6 225This is used by `comint-watch-for-password-prompt'.")
654d89d7 226
be9b65ac 227;;; Here are the per-interpreter hooks.
49116ac0 228(defvar comint-get-old-input (function comint-get-old-input-default)
3ee91e68 229 "Function that returns old text in comint mode.
be9b65ac
DL
230This function is called when return is typed while the point is in old text.
231It returns the text to be submitted as process input. The default is
cccd719e
RS
232`comint-get-old-input-default', which grabs the current line, and strips off
233leading text matching `comint-prompt-regexp'.")
be9b65ac 234
1b421826
RS
235(defvar comint-dynamic-complete-functions
236 '(comint-replace-by-expanded-history comint-dynamic-complete-filename)
237 "List of functions called to perform completion.
238Functions should return non-nil if completion was performed.
239See also `comint-dynamic-complete'.
3ee91e68
RS
240
241This is a good thing to set in mode hooks.")
242
49116ac0
JB
243(defvar comint-input-filter
244 (function (lambda (str) (not (string-match "\\`\\s *\\'" str))))
245 "Predicate for filtering additions to input history.
3ee91e68
RS
246Takes one argument, the input. If non-nil, the input may be saved on the input
247history list. Default is to save anything that isn't all whitespace.")
248
fb9ab28a 249(defvar comint-input-filter-functions '()
1b421826
RS
250 "Functions to call before input is sent to the process.
251These functions get one argument, a string containing the text to send.
252
253This variable is buffer-local.")
254
fb9ab28a 255(defvar comint-output-filter-functions '(comint-postoutput-scroll-to-bottom)
3ee91e68 256 "Functions to call after output is inserted into the buffer.
c1da5c03
RS
257One possible function is `comint-postoutput-scroll-to-bottom'.
258These functions get one argument, a string containing the text just inserted.
3ee91e68
RS
259
260This variable is buffer-local.")
49116ac0
JB
261
262(defvar comint-input-sender (function comint-simple-send)
263 "Function to actually send to PROCESS the STRING submitted by user.
cccd719e
RS
264Usually this is just `comint-simple-send', but if your mode needs to
265massage the input string, put a different function here.
266`comint-simple-send' just sends the string plus a newline.
267This is called from the user command `comint-send-input'.")
49116ac0 268
cccd719e 269(defvar comint-eol-on-send t
3ee91e68 270 "*Non-nil means go to the end of the line before sending input.
cccd719e 271See `comint-send-input'.")
be9b65ac
DL
272
273(defvar comint-mode-hook '()
49116ac0
JB
274 "Called upon entry into comint-mode
275This is run before the process is cranked up.")
276
277(defvar comint-exec-hook '()
cccd719e 278 "Called each time a process is exec'd by `comint-exec'.
49116ac0 279This is called after the process is cranked up. It is useful for things that
cccd719e
RS
280must be done each time a process is executed in a comint mode buffer (e.g.,
281`(process-kill-without-query)'). In contrast, the `comint-mode-hook' is only
49116ac0
JB
282executed once when the buffer is created.")
283
284(defvar comint-mode-map nil)
be9b65ac 285
c9706a52 286(defvar comint-ptyp t
3ee91e68 287 "Non-nil if communications via pty; false if by pipe. Buffer local.
cccd719e 288This is to work around a bug in Emacs process signalling.")
c9706a52 289
0bcb5285 290(defvar comint-input-ring nil)
e6dfdce5
RS
291(defvar comint-last-input-start)
292(defvar comint-last-input-end)
7d5d472e 293(defvar comint-last-output-start)
3ee91e68
RS
294(defvar comint-input-ring-index nil
295 "Index of last matched history element.")
296(defvar comint-matching-input-from-input-string ""
297 "Input previously used to match input history.")
21d9bf27
RS
298
299(put 'comint-replace-by-expanded-history 'menu-enable 'comint-input-autoexpand)
c9706a52 300(put 'comint-input-ring 'permanent-local t)
3ee91e68
RS
301(put 'comint-input-ring-index 'permanent-local t)
302(put 'comint-input-autoexpand 'permanent-local t)
fb9ab28a
RS
303(put 'comint-input-filter-functions 'permanent-local t)
304(put 'comint-output-filter-functions 'permanent-local t)
3ee91e68
RS
305(put 'comint-scroll-to-bottom-on-input 'permanent-local t)
306(put 'comint-scroll-to-bottom-on-output 'permanent-local t)
307(put 'comint-scroll-show-maximum-output 'permanent-local t)
c9706a52 308(put 'comint-ptyp 'permanent-local t)
e6dfdce5 309
be9b65ac
DL
310(defun comint-mode ()
311 "Major mode for interacting with an inferior interpreter.
312Interpreter name is same as buffer name, sans the asterisks.
313Return at end of buffer sends line as input.
314Return not at end copies rest of line to end and sends it.
cccd719e 315Setting variable `comint-eol-on-send' means jump to the end of the line
49116ac0 316before submitting new input.
be9b65ac 317
3ee91e68
RS
318This mode is customised to create major modes such as Inferior Lisp
319mode, Shell mode, etc. This can be done by setting the hooks
fb9ab28a 320`comint-input-filter-functions', `comint-input-filter', `comint-input-sender'
3ee91e68 321and `comint-get-old-input' to appropriate functions, and the variable
cccd719e 322`comint-prompt-regexp' to the appropriate regular expression.
be9b65ac 323
cccd719e 324An input history is maintained of size `comint-input-ring-size', and
3ee91e68
RS
325can be accessed with the commands \\[comint-next-input], \\[comint-previous-input], and \\[comint-dynamic-list-input-ring].
326Input ring history expansion can be achieved with the commands
327\\[comint-replace-by-expanded-history] or \\[comint-magic-space].
328Input ring expansion is controlled by the variable `comint-input-autoexpand',
329and addition is controlled by the variable `comint-input-ignoredups'.
330
cccd719e 331Commands with no default key bindings include `send-invisible',
21d9bf27 332`comint-dynamic-complete', `comint-dynamic-list-filename-completions', and
3ee91e68
RS
333`comint-magic-space'.
334
335Input to, and output from, the subprocess can cause the window to scroll to
fb9ab28a 336the end of the buffer. See variables `comint-output-filter-functions',
3ee91e68 337`comint-scroll-to-bottom-on-input', and `comint-scroll-to-bottom-on-output'.
be9b65ac
DL
338
339If you accidentally suspend your process, use \\[comint-continue-subjob]
340to continue it.
341
342\\{comint-mode-map}
343
3ee91e68 344Entry to this mode runs the hooks on `comint-mode-hook'."
be9b65ac 345 (interactive)
3ee91e68
RS
346 ;; Do not remove this. All major modes must do this.
347 (kill-all-local-variables)
348 (setq major-mode 'comint-mode)
349 (setq mode-name "Comint")
baae3c4e 350 (setq mode-line-process '(":%s"))
3ee91e68
RS
351 (use-local-map comint-mode-map)
352 (make-local-variable 'comint-last-input-start)
353 (setq comint-last-input-start (make-marker))
a9a6f83f 354 (set-marker comint-last-input-start (point-min))
3ee91e68
RS
355 (make-local-variable 'comint-last-input-end)
356 (setq comint-last-input-end (make-marker))
a9a6f83f 357 (set-marker comint-last-input-end (point-min))
3ee91e68
RS
358 (make-local-variable 'comint-last-output-start)
359 (setq comint-last-output-start (make-marker))
360 (make-local-variable 'comint-prompt-regexp) ; Don't set; default
361 (make-local-variable 'comint-input-ring-size) ; ...to global val.
362 (make-local-variable 'comint-input-ring)
363 (make-local-variable 'comint-input-ring-file-name)
364 (or (and (boundp 'comint-input-ring) comint-input-ring)
365 (setq comint-input-ring (make-ring comint-input-ring-size)))
366 (make-local-variable 'comint-input-ring-index)
367 (or (and (boundp 'comint-input-ring-index) comint-input-ring-index)
368 (setq comint-input-ring-index nil))
369 (make-local-variable 'comint-matching-input-from-input-string)
370 (make-local-variable 'comint-input-autoexpand)
371 (make-local-variable 'comint-input-ignoredups)
372 (make-local-variable 'comint-delimiter-argument-list)
1b421826 373 (make-local-variable 'comint-dynamic-complete-functions)
497513ad 374 (make-local-variable 'comint-completion-fignore)
3ee91e68 375 (make-local-variable 'comint-get-old-input)
fb9ab28a 376 (make-local-variable 'comint-input-filter-functions)
3ee91e68
RS
377 (make-local-variable 'comint-input-filter)
378 (make-local-variable 'comint-input-sender)
379 (make-local-variable 'comint-eol-on-send)
380 (make-local-variable 'comint-scroll-to-bottom-on-input)
381 (make-local-variable 'comint-scroll-to-bottom-on-output)
382 (make-local-variable 'comint-scroll-show-maximum-output)
c1da5c03
RS
383 (make-local-variable 'pre-command-hook)
384 (add-hook 'pre-command-hook 'comint-preinput-scroll-to-bottom)
fb9ab28a 385 (make-local-variable 'comint-output-filter-functions)
3ee91e68
RS
386 (make-local-variable 'comint-ptyp)
387 (make-local-variable 'comint-exec-hook)
388 (make-local-variable 'comint-process-echoes)
389 (run-hooks 'comint-mode-hook))
be9b65ac 390
49116ac0
JB
391(if comint-mode-map
392 nil
3ee91e68 393 ;; Keys:
49116ac0
JB
394 (setq comint-mode-map (make-sparse-keymap))
395 (define-key comint-mode-map "\ep" 'comint-previous-input)
396 (define-key comint-mode-map "\en" 'comint-next-input)
0c5c1f34
RS
397 (define-key comint-mode-map "\er" 'comint-previous-matching-input)
398 (define-key comint-mode-map "\es" 'comint-next-matching-input)
3ee91e68
RS
399 (define-key comint-mode-map [?\A-\M-r] 'comint-previous-matching-input-from-input)
400 (define-key comint-mode-map [?\A-\M-s] 'comint-next-matching-input-from-input)
d637fa56 401 (define-key comint-mode-map "\e\C-l" 'comint-show-output)
49116ac0
JB
402 (define-key comint-mode-map "\C-m" 'comint-send-input)
403 (define-key comint-mode-map "\C-d" 'comint-delchar-or-maybe-eof)
5bd43625 404 (define-key comint-mode-map "\C-c\C-a" 'comint-bol)
49116ac0
JB
405 (define-key comint-mode-map "\C-c\C-u" 'comint-kill-input)
406 (define-key comint-mode-map "\C-c\C-w" 'backward-kill-word)
407 (define-key comint-mode-map "\C-c\C-c" 'comint-interrupt-subjob)
408 (define-key comint-mode-map "\C-c\C-z" 'comint-stop-subjob)
409 (define-key comint-mode-map "\C-c\C-\\" 'comint-quit-subjob)
3ee91e68 410 (define-key comint-mode-map "\C-c\C-m" 'comint-copy-old-input)
49116ac0 411 (define-key comint-mode-map "\C-c\C-o" 'comint-kill-output)
49116ac0 412 (define-key comint-mode-map "\C-c\C-r" 'comint-show-output)
3ee91e68 413 (define-key comint-mode-map "\C-c\C-e" 'comint-show-maximum-output)
4bec19b2 414 (define-key comint-mode-map "\C-c\C-l" 'comint-dynamic-list-input-ring)
1de80a34 415 (define-key comint-mode-map "\C-c\C-n" 'comint-next-prompt)
3ee91e68 416 (define-key comint-mode-map "\C-c\C-p" 'comint-previous-prompt)
1de80a34 417 (define-key comint-mode-map "\C-c\C-d" 'comint-send-eof)
3ee91e68
RS
418 ;; Menu bars:
419 ;; completion:
420 (define-key comint-mode-map [menu-bar completion]
dcab7fbd 421 (cons "Complete" (make-sparse-keymap "Complete")))
3ee91e68
RS
422 (define-key comint-mode-map [menu-bar completion complete-expand]
423 '("Expand File Name" . comint-replace-by-expanded-filename))
424 (define-key comint-mode-map [menu-bar completion complete-listing]
425 '("File Completion Listing" . comint-dynamic-list-filename-completions))
3ee91e68
RS
426 (define-key comint-mode-map [menu-bar completion complete-file]
427 '("Complete File Name" . comint-dynamic-complete-filename))
428 (define-key comint-mode-map [menu-bar completion complete]
429 '("Complete Before Point" . comint-dynamic-complete))
430 ;; Input history:
5c07a950 431 (define-key comint-mode-map [menu-bar inout]
dcab7fbd 432 (cons "In/Out" (make-sparse-keymap "In/Out")))
5c07a950 433 (define-key comint-mode-map [menu-bar inout kill-output]
dcab7fbd 434 '("Kill Current Output Group" . comint-kill-output))
5c07a950 435 (define-key comint-mode-map [menu-bar inout next-prompt]
dcab7fbd 436 '("Forward Output Group" . comint-next-prompt))
5c07a950 437 (define-key comint-mode-map [menu-bar inout previous-prompt]
dcab7fbd 438 '("Backward Output Group" . comint-previous-prompt))
5c07a950 439 (define-key comint-mode-map [menu-bar inout show-maximum-output]
dcab7fbd 440 '("Show Maximum Output" . comint-show-maximum-output))
5c07a950 441 (define-key comint-mode-map [menu-bar inout show-output]
dcab7fbd 442 '("Show Current Output Group" . comint-show-output))
5c07a950 443 (define-key comint-mode-map [menu-bar inout kill-input]
3ee91e68 444 '("Kill Current Input" . comint-kill-input))
5c07a950 445 (define-key comint-mode-map [menu-bar inout copy-input]
3ee91e68 446 '("Copy Old Input" . comint-copy-old-input))
5c07a950 447 (define-key comint-mode-map [menu-bar inout forward-matching-history]
dcab7fbd 448 '("Forward Matching Input..." . comint-forward-matching-input))
5c07a950 449 (define-key comint-mode-map [menu-bar inout backward-matching-history]
dcab7fbd 450 '("Backward Matching Input..." . comint-backward-matching-input))
5c07a950 451 (define-key comint-mode-map [menu-bar inout next-matching-history]
3ee91e68 452 '("Next Matching Input..." . comint-next-matching-input))
5c07a950 453 (define-key comint-mode-map [menu-bar inout previous-matching-history]
3ee91e68 454 '("Previous Matching Input..." . comint-previous-matching-input))
5c07a950 455 (define-key comint-mode-map [menu-bar inout next-matching-history-from-input]
3ee91e68 456 '("Next Matching Current Input" . comint-next-matching-input-from-input))
5c07a950 457 (define-key comint-mode-map [menu-bar inout previous-matching-history-from-input]
3ee91e68 458 '("Previous Matching Current Input" . comint-previous-matching-input-from-input))
5c07a950 459 (define-key comint-mode-map [menu-bar inout next-history]
3ee91e68 460 '("Next Input" . comint-next-input))
5c07a950 461 (define-key comint-mode-map [menu-bar inout previous-history]
3ee91e68 462 '("Previous Input" . comint-previous-input))
5c07a950 463 (define-key comint-mode-map [menu-bar inout list-history]
3ee91e68 464 '("List Input History" . comint-dynamic-list-input-ring))
5c07a950 465 (define-key comint-mode-map [menu-bar inout expand-history]
3ee91e68 466 '("Expand History Before Point" . comint-replace-by-expanded-history))
3ee91e68
RS
467 ;; Signals
468 (define-key comint-mode-map [menu-bar signals]
469 (cons "Signals" (make-sparse-keymap "Signals")))
470 (define-key comint-mode-map [menu-bar signals eof]
471 '("EOF" . comint-send-eof))
472 (define-key comint-mode-map [menu-bar signals kill]
473 '("KILL" . comint-kill-subjob))
474 (define-key comint-mode-map [menu-bar signals quit]
475 '("QUIT" . comint-quit-subjob))
476 (define-key comint-mode-map [menu-bar signals cont]
477 '("CONT" . comint-continue-subjob))
478 (define-key comint-mode-map [menu-bar signals stop]
479 '("STOP" . comint-stop-subjob))
480 (define-key comint-mode-map [menu-bar signals break]
481 '("BREAK" . comint-interrupt-subjob))
482 ;; Put them in the menu bar:
5c07a950 483 (setq menu-bar-final-items (append '(completion inout signals)
3ee91e68 484 menu-bar-final-items))
49116ac0
JB
485 )
486
49116ac0 487(defun comint-check-proc (buffer)
3ee91e68 488 "Return t if there is a living process associated w/buffer BUFFER.
cccd719e
RS
489Living means the status is `run' or `stop'.
490BUFFER can be either a buffer or the name of one."
49116ac0
JB
491 (let ((proc (get-buffer-process buffer)))
492 (and proc (memq (process-status proc) '(run stop)))))
be9b65ac
DL
493
494;;; Note that this guy, unlike shell.el's make-shell, barfs if you pass it ()
495;;; for the second argument (program).
eb6e9899 496;;;###autoload
be9b65ac 497(defun make-comint (name program &optional startfile &rest switches)
14a08856
RM
498 "Make a comint process NAME in a buffer, running PROGRAM.
499The name of the buffer is made by surrounding NAME with `*'s.
500If there is already a running process in that buffer, it is not restarted.
501Optional third arg STARTFILE is the name of a file to send the contents of to
502the process. Any more args are arguments to PROGRAM."
49116ac0 503 (let ((buffer (get-buffer-create (concat "*" name "*"))))
be9b65ac 504 ;; If no process, or nuked process, crank up a new one and put buffer in
14a08856 505 ;; comint mode. Otherwise, leave buffer and existing process alone.
1ff6cf10 506 (cond ((not (comint-check-proc buffer))
49116ac0 507 (save-excursion
be9b65ac
DL
508 (set-buffer buffer)
509 (comint-mode)) ; Install local vars, mode, keymap, ...
510 (comint-exec buffer name program startfile switches)))
511 buffer))
512
21d9bf27
RS
513;;;###autoload
514(defun comint-run (program)
515 "Run PROGRAM in a comint buffer and switch to it.
516The buffer name is made by surrounding the file name of PROGRAM with `*'s.
517The file name is used to make a symbol name, such as `comint-sh-hook', and any
518hooks on this symbol are run in the buffer.
519See `make-comint' and `comint-exec'."
520 (interactive "sRun program: ")
521 (let ((name (file-name-nondirectory program)))
522 (switch-to-buffer (make-comint name program))
523 (run-hooks (intern-soft (concat "comint-" name "-hook")))))
524
be9b65ac 525(defun comint-exec (buffer name command startfile switches)
cccd719e 526 "Start up a process in buffer BUFFER for comint modes.
14a08856 527Blasts any old process running in the buffer. Doesn't set the buffer mode.
49116ac0 528You can use this to cheaply run a series of processes in the same comint
cccd719e 529buffer. The hook `comint-exec-hook' is run after each exec."
be9b65ac
DL
530 (save-excursion
531 (set-buffer buffer)
532 (let ((proc (get-buffer-process buffer))) ; Blast any old process.
533 (if proc (delete-process proc)))
534 ;; Crank up a new process
535 (let ((proc (comint-exec-1 name buffer command switches)))
3ee91e68 536 (set-process-filter proc 'comint-output-filter)
49116ac0
JB
537 (make-local-variable 'comint-ptyp)
538 (setq comint-ptyp process-connection-type) ; T if pty, NIL if pipe.
be9b65ac 539 ;; Jump to the end, and set the process mark.
49116ac0 540 (goto-char (point-max))
87ef29fd 541 (set-marker (process-mark proc) (point))
be9b65ac
DL
542 ;; Feed it the startfile.
543 (cond (startfile
544 ;;This is guaranteed to wait long enough
545 ;;but has bad results if the comint does not prompt at all
546 ;; (while (= size (buffer-size))
547 ;; (sleep-for 1))
548 ;;I hope 1 second is enough!
549 (sleep-for 1)
550 (goto-char (point-max))
551 (insert-file-contents startfile)
552 (setq startfile (buffer-substring (point) (point-max)))
553 (delete-region (point) (point-max))
49116ac0
JB
554 (comint-send-string proc startfile)))
555 (run-hooks 'comint-exec-hook)
87ef29fd 556 buffer)))
be9b65ac
DL
557
558;;; This auxiliary function cranks up the process for comint-exec in
e065a56e 559;;; the appropriate environment.
be9b65ac
DL
560
561(defun comint-exec-1 (name buffer command switches)
1b421826 562 (let ((process-environment
1f9e761d 563 (nconc
a4ab7e69
RS
564 ;; If using termcap, we specify `emacs' as the terminal type
565 ;; because that lets us specify a width.
566 ;; If using terminfo, we specify `unknown' because that is
567 ;; a defined terminal type. `emacs' is not a defined terminal type
568 ;; and there is no way for us to define it here.
569 ;; Some programs that use terminfo get very confused
570 ;; if TERM is not a valid terminal type.
1f9e761d
KH
571 (if (and (boundp 'system-uses-terminfo) system-uses-terminfo)
572 (list "EMACS=t" "TERM=unknown"
573 (format "COLUMNS=%d" (frame-width)))
574 (list "EMACS=t" "TERM=emacs"
575 (format "TERMCAP=emacs:co#%d:tc=unknown" (frame-width))))
576 process-environment)))
e065a56e 577 (apply 'start-process name buffer command switches)))
be9b65ac 578\f
3ee91e68 579;;; Input history processing in a buffer
be9b65ac 580;;; ===========================================================================
3ee91e68
RS
581;;; Useful input history functions, courtesy of the Ergo group.
582
583;;; Eleven commands:
584;;; comint-dynamic-list-input-ring List history in help buffer.
585;;; comint-previous-input Previous input...
586;;; comint-previous-matching-input ...matching a string.
587;;; comint-previous-matching-input-from-input ... matching the current input.
588;;; comint-next-input Next input...
589;;; comint-next-matching-input ...matching a string.
590;;; comint-next-matching-input-from-input ... matching the current input.
591;;; comint-backward-matching-input Backwards input...
592;;; comint-forward-matching-input ...matching a string.
593;;; comint-replace-by-expanded-history Expand history at point;
594;;; replace with expanded history.
595;;; comint-magic-space Expand history and insert space.
596;;;
1b421826 597;;; Three functions:
3ee91e68
RS
598;;; comint-read-input-ring Read into comint-input-ring...
599;;; comint-write-input-ring Write to comint-input-ring-file-name.
1b421826 600;;; comint-replace-by-expanded-history-before-point Workhorse function.
3ee91e68 601
d637fa56 602(defun comint-read-input-ring (&optional silent)
3ee91e68
RS
603 "Sets the buffer's `comint-input-ring' from a history file.
604The name of the file is given by the variable `comint-input-ring-file-name'.
605The history ring is of size `comint-input-ring-size', regardless of file size.
606If `comint-input-ring-file-name' is nil this function does nothing.
607
d637fa56
RS
608If the optional argument SILENT is non-nil, we say nothing about a
609failure to read the history file.
3ee91e68 610
d637fa56
RS
611This function is useful for major mode commands and mode hooks.
612
613The structure of the history file should be one input command per line,
614with the most recent command last.
3ee91e68 615See also `comint-input-ignoredups' and `comint-write-input-ring'."
07ccf0bc
RS
616 (cond ((or (null comint-input-ring-file-name)
617 (equal comint-input-ring-file-name ""))
3ee91e68
RS
618 nil)
619 ((not (file-readable-p comint-input-ring-file-name))
d637fa56
RS
620 (or silent
621 (message "Cannot read history file %s"
622 comint-input-ring-file-name)))
3ee91e68 623 (t
dc0825db
RS
624 (let ((history-buf (get-buffer-create " *temp*"))
625 (file comint-input-ring-file-name)
626 (count 0)
3ee91e68 627 (ring (make-ring comint-input-ring-size)))
dc0825db
RS
628 (unwind-protect
629 (save-excursion
630 (set-buffer history-buf)
3ee91e68 631 (widen)
dc0825db
RS
632 (erase-buffer)
633 (insert-file-contents file)
634 ;; Save restriction in case file is already visited...
635 ;; Watch for those date stamps in history files!
636 (goto-char (point-max))
637 (while (and (< count comint-input-ring-size)
638 (re-search-backward "^[ \t]*\\([^#\n].*\\)[ \t]*$"
639 nil t))
3ee91e68
RS
640 (let ((history (buffer-substring (match-beginning 1)
641 (match-end 1))))
642 (if (or (null comint-input-ignoredups)
643 (ring-empty-p ring)
644 (not (string-equal (ring-ref ring 0) history)))
dc0825db
RS
645 (ring-insert-at-beginning ring history)))
646 (setq count (1+ count))))
647 (kill-buffer history-buf))
3ee91e68
RS
648 (setq comint-input-ring ring
649 comint-input-ring-index nil)))))
650
651(defun comint-write-input-ring ()
652 "Writes the buffer's `comint-input-ring' to a history file.
653The name of the file is given by the variable `comint-input-ring-file-name'.
654The original contents of the file are lost if `comint-input-ring' is not empty.
655If `comint-input-ring-file-name' is nil this function does nothing.
656
657Useful within process sentinels.
658
659See also `comint-read-input-ring'."
660 (cond ((or (null comint-input-ring-file-name)
07ccf0bc 661 (equal comint-input-ring-file-name "")
3ee91e68
RS
662 (null comint-input-ring) (ring-empty-p comint-input-ring))
663 nil)
664 ((not (file-writable-p comint-input-ring-file-name))
665 (message "Cannot write history file %s" comint-input-ring-file-name))
666 (t
667 (let* ((history-buf (get-buffer-create " *Temp Input History*"))
668 (ring comint-input-ring)
669 (file comint-input-ring-file-name)
670 (index (ring-length ring)))
671 ;; Write it all out into a buffer first. Much faster, but messier,
672 ;; than writing it one line at a time.
673 (save-excursion
674 (set-buffer history-buf)
675 (erase-buffer)
676 (while (> index 0)
677 (setq index (1- index))
678 (insert (ring-ref ring index) ?\n))
679 (write-region (buffer-string) nil file nil 'no-message)
680 (kill-buffer nil))))))
681
682
683(defun comint-dynamic-list-input-ring ()
684 "List in help buffer the buffer's input history."
685 (interactive)
686 (if (or (not (ring-p comint-input-ring))
687 (ring-empty-p comint-input-ring))
688 (message "No history")
689 (let ((history nil)
690 (history-buffer " *Input History*")
691 (index (1- (ring-length comint-input-ring)))
692 (conf (current-window-configuration)))
693 ;; We have to build up a list ourselves from the ring vector.
694 (while (>= index 0)
695 (setq history (cons (ring-ref comint-input-ring index) history)
696 index (1- index)))
697 ;; Change "completion" to "history reference"
698 ;; to make the display accurate.
699 (with-output-to-temp-buffer history-buffer
700 (display-completion-list history)
701 (set-buffer history-buffer)
702 (forward-line 3)
703 (while (search-backward "completion" nil 'move)
704 (replace-match "history reference")))
705 (sit-for 0)
706 (message "Hit space to flush")
707 (let ((ch (read-event)))
708 (if (eq ch ?\ )
709 (set-window-configuration conf)
710 (setq unread-command-events (list ch)))))))
711
712
713(defun comint-regexp-arg (prompt)
714 ;; Return list of regexp and prefix arg using PROMPT.
715 (let* ((minibuffer-history-sexp-flag nil)
716 ;; Don't clobber this.
717 (last-command last-command)
718 (regexp (read-from-minibuffer prompt nil nil nil
719 'minibuffer-history-search-history)))
720 (list (if (string-equal regexp "")
721 (setcar minibuffer-history-search-history
722 (nth 1 minibuffer-history-search-history))
723 regexp)
724 (prefix-numeric-value current-prefix-arg))))
725
726(defun comint-search-arg (arg)
727 ;; First make sure there is a ring and that we are after the process mark
728 (cond ((not (comint-after-pmark-p))
729 (error "Not at command line"))
730 ((or (null comint-input-ring)
731 (ring-empty-p comint-input-ring))
732 (error "Empty input ring"))
733 ((zerop arg)
734 ;; arg of zero resets search from beginning, and uses arg of 1
735 (setq comint-input-ring-index nil)
736 1)
737 (t
738 arg)))
739
740(defun comint-search-start (arg)
741 ;; Index to start a directional search, starting at comint-input-ring-index
742 (if comint-input-ring-index
743 ;; If a search is running, offset by 1 in direction of arg
744 (mod (+ comint-input-ring-index (if (> arg 0) 1 -1))
745 (ring-length comint-input-ring))
746 ;; For a new search, start from beginning or end, as appropriate
747 (if (>= arg 0)
748 0 ; First elt for forward search
749 (1- (ring-length comint-input-ring))))) ; Last elt for backward search
750
751(defun comint-previous-input-string (arg)
752 "Return the string ARG places along the input ring.
753Moves relative to `comint-input-ring-index'."
754 (ring-ref comint-input-ring (if comint-input-ring-index
755 (mod (+ arg comint-input-ring-index)
756 (ring-length comint-input-ring))
757 arg)))
be9b65ac
DL
758
759(defun comint-previous-input (arg)
760 "Cycle backwards through input history."
761 (interactive "*p")
3ee91e68 762 (comint-previous-matching-input "." arg))
1586b965 763
be9b65ac
DL
764(defun comint-next-input (arg)
765 "Cycle forwards through input history."
766 (interactive "*p")
767 (comint-previous-input (- arg)))
768
3ee91e68
RS
769(defun comint-previous-matching-input-string (regexp arg)
770 "Return the string matching REGEXP ARG places along the input ring.
771Moves relative to `comint-input-ring-index'."
772 (let* ((pos (comint-previous-matching-input-string-position regexp arg)))
773 (if pos (ring-ref comint-input-ring pos))))
774
775(defun comint-previous-matching-input-string-position (regexp arg &optional start)
776 "Return the index matching REGEXP ARG places along the input ring.
777Moves relative to START, or `comint-input-ring-index'."
778 (if (or (not (ring-p comint-input-ring))
779 (ring-empty-p comint-input-ring))
780 (error "No history"))
781 (let* ((len (ring-length comint-input-ring))
782 (motion (if (> arg 0) 1 -1))
783 (n (mod (- (or start (comint-search-start arg)) motion) len))
784 (tried-each-ring-item nil)
785 (prev nil))
786 ;; Do the whole search as many times as the argument says.
787 (while (and (/= arg 0) (not tried-each-ring-item))
788 ;; Step once.
789 (setq prev n
790 n (mod (+ n motion) len))
791 ;; If we haven't reached a match, step some more.
792 (while (and (< n len) (not tried-each-ring-item)
793 (not (string-match regexp (ring-ref comint-input-ring n))))
794 (setq n (mod (+ n motion) len)
795 ;; If we have gone all the way around in this search.
796 tried-each-ring-item (= n prev)))
797 (setq arg (if (> arg 0) (1- arg) (1+ arg))))
798 ;; Now that we know which ring element to use, if we found it, return that.
799 (if (string-match regexp (ring-ref comint-input-ring n))
800 n)))
801
0c5c1f34
RS
802(defun comint-previous-matching-input (regexp arg)
803 "Search backwards through input history for match for REGEXP.
804\(Previous history elements are earlier commands.)
805With prefix argument N, search for Nth previous match.
806If N is negative, find the next or Nth next match."
3ee91e68
RS
807 (interactive (comint-regexp-arg "Previous input matching (regexp): "))
808 (setq arg (comint-search-arg arg))
809 (let ((pos (comint-previous-matching-input-string-position regexp arg)))
810 ;; Has a match been found?
811 (if (null pos)
812 (error "Not found")
813 (setq comint-input-ring-index pos)
814 (message "History item: %d" (1+ pos))
815 (delete-region
816 ;; Can't use kill-region as it sets this-command
817 (process-mark (get-buffer-process (current-buffer))) (point))
818 (insert (ring-ref comint-input-ring pos)))))
0c5c1f34
RS
819
820(defun comint-next-matching-input (regexp arg)
821 "Search forwards through input history for match for REGEXP.
822\(Later history elements are more recent commands.)
823With prefix argument N, search for Nth following match.
824If N is negative, find the previous or Nth previous match."
3ee91e68 825 (interactive (comint-regexp-arg "Next input matching (regexp): "))
0c5c1f34 826 (comint-previous-matching-input regexp (- arg)))
be9b65ac 827
3ee91e68
RS
828(defun comint-previous-matching-input-from-input (arg)
829 "Search backwards through input history for match for current input.
830\(Previous history elements are earlier commands.)
831With prefix argument N, search for Nth previous match.
3f849529 832If N is negative, search forwards for the -Nth following match."
3ee91e68
RS
833 (interactive "p")
834 (if (not (memq last-command '(comint-previous-matching-input-from-input
835 comint-next-matching-input-from-input)))
836 ;; Starting a new search
837 (setq comint-matching-input-from-input-string
838 (buffer-substring
839 (process-mark (get-buffer-process (current-buffer)))
840 (point))
841 comint-input-ring-index nil))
842 (comint-previous-matching-input
843 (concat "^" (regexp-quote comint-matching-input-from-input-string))
844 arg))
845
846(defun comint-next-matching-input-from-input (arg)
847 "Search forwards through input history for match for current input.
3f849529
RS
848\(Following history elements are more recent commands.)
849With prefix argument N, search for Nth following match.
850If N is negative, search backwards for the -Nth previous match."
3ee91e68
RS
851 (interactive "p")
852 (comint-previous-matching-input-from-input (- arg)))
853
854
d637fa56 855(defun comint-replace-by-expanded-history (&optional silent)
3ee91e68 856 "Expand input command history references before point.
1b421826
RS
857Expansion is dependent on the value of `comint-input-autoexpand'.
858
3ee91e68
RS
859This function depends on the buffer's idea of the input history, which may not
860match the command interpreter's idea, assuming it has one.
861
862Assumes history syntax is like typical Un*x shells'. However, since emacs
863cannot know the interpreter's idea of input line numbers, assuming it has one,
864it cannot expand absolute input line number references.
865
d637fa56
RS
866If the optional argument SILENT is non-nil, never complain
867even if history reference seems erroneous.
868
1b421826
RS
869See `comint-magic-space' and `comint-replace-by-expanded-history-before-point'.
870
871Returns t if successful."
3ee91e68 872 (interactive)
1b421826 873 (if (and comint-input-autoexpand
8d3d2a0c
KH
874 (string-match "[!^]" (funcall comint-get-old-input))
875 (save-excursion (beginning-of-line)
876 (looking-at comint-prompt-regexp)))
1b421826
RS
877 ;; Looks like there might be history references in the command.
878 (let ((previous-modified-tick (buffer-modified-tick)))
879 (message "Expanding history references...")
8d3d2a0c 880 (comint-replace-by-expanded-history-before-point silent)
1b421826
RS
881 (/= previous-modified-tick (buffer-modified-tick)))))
882
883
8d3d2a0c 884(defun comint-replace-by-expanded-history-before-point (silent)
1b421826
RS
885 "Expand directory stack reference before point.
886See `comint-replace-by-expanded-history'. Returns t if successful."
3ee91e68
RS
887 (save-excursion
888 (let ((toend (- (save-excursion (end-of-line nil) (point)) (point)))
889 (start (progn (comint-bol nil) (point))))
d637fa56
RS
890 (while (progn
891 (skip-chars-forward "^!^"
892 (save-excursion
893 (end-of-line nil) (- (point) toend)))
894 (< (point)
895 (save-excursion
896 (end-of-line nil) (- (point) toend))))
3ee91e68
RS
897 ;; This seems a bit complex. We look for references such as !!, !-num,
898 ;; !foo, !?foo, !{bar}, !?{bar}, ^oh, ^my^, ^god^it, ^never^ends^.
899 ;; If that wasn't enough, the plings can be suffixed with argument
900 ;; range specifiers.
901 ;; Argument ranges are complex too, so we hive off the input line,
902 ;; referenced with plings, with the range string to `comint-args'.
903 (setq comint-input-ring-index nil)
3ee91e68
RS
904 (cond ((or (= (preceding-char) ?\\)
905 (comint-within-quotes start (point)))
906 ;; The history is quoted, or we're in quotes.
d637fa56 907 (goto-char (1+ (point))))
3ee91e68
RS
908 ((looking-at "![0-9]+\\($\\|[^-]\\)")
909 ;; We cannot know the interpreter's idea of input line numbers.
910 (goto-char (match-end 0))
911 (message "Absolute reference cannot be expanded"))
912 ((looking-at "!-\\([0-9]+\\)\\(:?[0-9^$*-]+\\)?")
913 ;; Just a number of args from `number' lines backward.
914 (let ((number (1- (string-to-number
915 (buffer-substring (match-beginning 1)
916 (match-end 1))))))
917 (if (<= number (ring-length comint-input-ring))
918 (progn
919 (replace-match
920 (comint-args (comint-previous-input-string number)
fb9ab28a
RS
921 (match-beginning 2) (match-end 2))
922 t t)
3ee91e68
RS
923 (setq comint-input-ring-index number)
924 (message "History item: %d" (1+ number)))
925 (goto-char (match-end 0))
926 (message "Relative reference exceeds input history size"))))
927 ((or (looking-at "!!?:?\\([0-9^$*-]+\\)") (looking-at "!!"))
928 ;; Just a number of args from the previous input line.
929 (replace-match
930 (comint-args (comint-previous-input-string 0)
fb9ab28a
RS
931 (match-beginning 1) (match-end 1))
932 t t)
1b421826 933 (message "History item: previous"))
3ee91e68
RS
934 ((looking-at
935 "!\\??\\({\\(.+\\)}\\|\\(\\sw+\\)\\)\\(:?[0-9^$*-]+\\)?")
936 ;; Most recent input starting with or containing (possibly
937 ;; protected) string, maybe just a number of args. Phew.
938 (let* ((mb1 (match-beginning 1)) (me1 (match-end 1))
939 (mb2 (match-beginning 2)) (me2 (match-end 2))
940 (exp (buffer-substring (or mb2 mb1) (or me2 me1)))
941 (pref (if (save-match-data (looking-at "!\\?")) "" "^"))
942 (pos (save-match-data
943 (comint-previous-matching-input-string-position
944 (concat pref (regexp-quote exp)) 1))))
945 (if (null pos)
5a336c13
KH
946 (progn
947 (goto-char (match-end 0))
948 (or silent
949 (progn (message "Not found")
950 (ding))))
3ee91e68 951 (setq comint-input-ring-index pos)
3ee91e68
RS
952 (replace-match
953 (comint-args (ring-ref comint-input-ring pos)
fb9ab28a
RS
954 (match-beginning 4) (match-end 4))
955 t t)
1b421826 956 (message "History item: %d" (1+ pos)))))
3ee91e68
RS
957 ((looking-at "\\^\\([^^]+\\)\\^?\\([^^]*\\)\\^?")
958 ;; Quick substitution on the previous input line.
959 (let ((old (buffer-substring (match-beginning 1) (match-end 1)))
960 (new (buffer-substring (match-beginning 2) (match-end 2)))
961 (pos nil))
962 (replace-match (comint-previous-input-string 0) t t)
963 (setq pos (point))
964 (goto-char (match-beginning 0))
1b421826
RS
965 (if (not (search-forward old pos t))
966 (or silent
967 (error "Not found"))
968 (replace-match new t t)
969 (message "History item: substituted"))))
3ee91e68
RS
970 (t
971 (goto-char (match-end 0))))))))
972
973
974(defun comint-magic-space (arg)
975 "Expand input history references before point and insert ARG spaces.
976A useful command to bind to SPC. See `comint-replace-by-expanded-history'."
977 (interactive "p")
978 (comint-replace-by-expanded-history)
979 (self-insert-command arg))
980\f
981(defun comint-within-quotes (beg end)
982 "Return t if the number of quotes between BEG and END is odd.
983Quotes are single and double."
984 (let ((countsq (comint-how-many-region "\\(^\\|[^\\\\]\\)\'" beg end))
985 (countdq (comint-how-many-region "\\(^\\|[^\\\\]\\)\"" beg end)))
986 (or (= (mod countsq 2) 1) (= (mod countdq 2) 1))))
987
988(defun comint-how-many-region (regexp beg end)
989 "Return number of matches for REGEXP from BEG to END."
990 (let ((count 0))
991 (save-excursion
992 (save-match-data
993 (goto-char beg)
994 (while (re-search-forward regexp end t)
995 (setq count (1+ count)))))
996 count))
997
998(defun comint-args (string begin end)
999 ;; From STRING, return the args depending on the range specified in the text
1000 ;; from BEGIN to END. If BEGIN is nil, assume all args. Ignore leading `:'.
1001 ;; Range can be x-y, x-, -y, where x/y can be [0-9], *, ^, $.
1002 (save-match-data
1003 (if (null begin)
1004 (comint-arguments string 0 nil)
1005 (let* ((range (buffer-substring
1006 (if (eq (char-after begin) ?:) (1+ begin) begin) end))
1007 (nth (cond ((string-match "^[*^]" range) 1)
1008 ((string-match "^-" range) 0)
1009 ((string-equal range "$") nil)
1010 (t (string-to-number range))))
1011 (mth (cond ((string-match "[-*$]$" range) nil)
1012 ((string-match "-" range)
1013 (string-to-number (substring range (match-end 0))))
1014 (t nth))))
1015 (comint-arguments string nth mth)))))
1016
3f4b7c36
RS
1017;; Return a list of arguments from ARG. Break it up at the
1018;; delimiters in comint-delimiter-argument-list. Returned list is backwards.
3ee91e68 1019(defun comint-delim-arg (arg)
3f4b7c36 1020 (if (null comint-delimiter-argument-list)
3ee91e68 1021 (list arg)
3f4b7c36
RS
1022 (let ((args nil)
1023 (pos 0)
1024 (len (length arg)))
1025 (while (< pos len)
1026 (let ((char (aref arg pos))
1027 (start pos))
1028 (if (memq char comint-delimiter-argument-list)
1029 (while (and (< pos len) (eq (aref arg pos) char))
1030 (setq pos (1+ pos)))
1031 (while (and (< pos len)
1032 (not (memq (aref arg pos)
1033 comint-delimiter-argument-list)))
1034 (setq pos (1+ pos))))
1035 (setq args (cons (substring arg start pos) args))))
3ee91e68
RS
1036 args)))
1037
1038(defun comint-arguments (string nth mth)
1039 "Return from STRING the NTH to MTH arguments.
1040NTH and/or MTH can be nil, which means the last argument.
3f4b7c36
RS
1041Returned arguments are separated by single spaces.
1042We assume whitespace separates arguments, except within quotes.
1043Also, a run of one or more of a single character
1044in `comint-delimiter-argument-list' is a separate argument.
3ee91e68 1045Argument 0 is the command name."
5e778cd1 1046 (let ((argpart "[^ \n\t\"'`]+\\|\\(\"[^\"]*\"\\|'[^']*'\\|`[^`]*`\\)")
d91f490d
RS
1047 (args ()) (pos 0)
1048 (count 0)
1049 beg str value quotes)
1050 ;; Build a list of all the args until we have as many as we want.
1051 (while (and (or (null mth) (<= count mth))
1052 (string-match argpart string pos))
1053 (if (and beg (= pos (match-beginning 0)))
1054 ;; It's contiguous, part of the same arg.
1055 (setq pos (match-end 0)
1056 quotes (or quotes (match-beginning 1)))
1057 ;; It's a new separate arg.
1058 (if beg
1059 ;; Put the previous arg, if there was one, onto ARGS.
1060 (setq str (substring string beg pos)
1061 args (if quotes (cons str args)
1062 (nconc (comint-delim-arg str) args))
1063 count (1+ count)))
1064 (setq quotes (match-beginning 1))
1065 (setq beg (match-beginning 0))
1066 (setq pos (match-end 0))))
1067 (if beg
1068 (setq str (substring string beg pos)
1069 args (if quotes (cons str args)
1070 (nconc (comint-delim-arg str) args))
1071 count (1+ count)))
1072 (let ((n (or nth (1- count)))
1073 (m (if mth (1- (- count mth)) 0)))
3ee91e68
RS
1074 (mapconcat
1075 (function (lambda (a) a)) (nthcdr n (nreverse (nthcdr m args))) " "))))
1076\f
be9b65ac 1077;;;
3ee91e68 1078;;; Input processing stuff
be9b65ac 1079;;;
3ee91e68 1080
49116ac0 1081(defun comint-send-input ()
c2ced5d8
CZ
1082 "Send input to process.
1083After the process output mark, sends all text from the process mark to
1084point as input to the process. Before the process output mark, calls value
8ae3bc9f 1085of variable `comint-get-old-input' to retrieve old input, copies it to the
cccd719e 1086process mark, and sends it. If variable `comint-process-echoes' is nil,
8ae3bc9f 1087a terminal newline is also inserted into the buffer and sent to the process
cccd719e
RS
1088\(if it is non-nil, all text from the process mark to point is deleted,
1089since it is assumed the remote process will re-echo it).
8ae3bc9f 1090
3ee91e68 1091Any history reference may be expanded depending on the value of the variable
1b421826 1092`comint-input-autoexpand'. The list of function names contained in the value
fb9ab28a 1093of `comint-input-filter-functions' is called on the input before sending it.
1b421826
RS
1094The input is entered into the input history ring, if the value of variable
1095`comint-input-filter' returns non-nil when called on the input.
cccd719e
RS
1096
1097If variable `comint-eol-on-send' is non-nil, then point is moved to the
8ae3bc9f
NF
1098end of line before sending the input.
1099
fb9ab28a 1100The values of `comint-get-old-input', `comint-input-filter-functions', and
1b421826
RS
1101`comint-input-filter' are chosen according to the command interpreter running
1102in the buffer. E.g.,
1103
be9b65ac 1104If the interpreter is the csh,
49116ac0
JB
1105 comint-get-old-input is the default: take the current line, discard any
1106 initial string matching regexp comint-prompt-regexp.
fb9ab28a 1107 comint-input-filter-functions monitors input for \"cd\", \"pushd\", and
1b421826 1108 \"popd\" commands. When it sees one, it cd's the buffer.
3ee91e68 1109 comint-input-filter is the default: returns t if the input isn't all white
49116ac0 1110 space.
be9b65ac
DL
1111
1112If the comint is Lucid Common Lisp,
49116ac0 1113 comint-get-old-input snarfs the sexp ending at point.
fb9ab28a 1114 comint-input-filter-functions does nothing.
3ee91e68 1115 comint-input-filter returns nil if the input matches input-filter-regexp,
be9b65ac
DL
1116 which matches (1) all whitespace (2) :a, :c, etc.
1117
1586b965 1118Similarly for Soar, Scheme, etc."
be9b65ac
DL
1119 (interactive)
1120 ;; Note that the input string does not include its terminal newline.
49116ac0
JB
1121 (let ((proc (get-buffer-process (current-buffer))))
1122 (if (not proc) (error "Current buffer has no process")
1123 (let* ((pmark (process-mark proc))
3ee91e68 1124 (intxt (if (>= (point) (marker-position pmark))
49116ac0
JB
1125 (progn (if comint-eol-on-send (end-of-line))
1126 (buffer-substring pmark (point)))
87ef29fd
JB
1127 (let ((copy (funcall comint-get-old-input)))
1128 (goto-char pmark)
1129 (insert copy)
3ee91e68
RS
1130 copy)))
1131 (input (if (not (eq comint-input-autoexpand 'input))
1132 ;; Just whatever's already there
1133 intxt
1134 ;; Expand and leave it visible in buffer
d637fa56 1135 (comint-replace-by-expanded-history t)
3ee91e68
RS
1136 (buffer-substring pmark (point))))
1137 (history (if (not (eq comint-input-autoexpand 'history))
5f46b51a 1138 input
3ee91e68
RS
1139 ;; This is messy 'cos ultimately the original
1140 ;; functions used do insertion, rather than return
1141 ;; strings. We have to expand, then insert back.
d637fa56 1142 (comint-replace-by-expanded-history t)
5f46b51a
RS
1143 (let ((copy (buffer-substring pmark (point))))
1144 (delete-region pmark (point))
1145 (insert input)
1146 copy))))
8ae3bc9f
NF
1147 (if comint-process-echoes
1148 (delete-region pmark (point))
3ee91e68
RS
1149 (insert ?\n))
1150 (if (and (funcall comint-input-filter history)
1151 (or (null comint-input-ignoredups)
1152 (not (ring-p comint-input-ring))
1153 (ring-empty-p comint-input-ring)
1154 (not (string-equal (ring-ref comint-input-ring 0)
1155 history))))
1156 (ring-insert comint-input-ring history))
fb9ab28a 1157 (let ((functions comint-input-filter-functions))
1b421826
RS
1158 (while functions
1159 (funcall (car functions) (concat input "\n"))
1160 (setq functions (cdr functions))))
1586b965 1161 (setq comint-input-ring-index nil)
dcdc526c
RS
1162 ;; Update the markers before we send the input
1163 ;; in case we get output amidst sending the input.
87ef29fd
JB
1164 (set-marker comint-last-input-start pmark)
1165 (set-marker comint-last-input-end (point))
3ee91e68 1166 (set-marker (process-mark proc) (point))
dcdc526c 1167 (funcall comint-input-sender proc input)
a9a6f83f 1168 (comint-output-filter proc "")))))
be9b65ac 1169
3ee91e68 1170;; The purpose of using this filter for comint processes
5511e411
RS
1171;; is to keep comint-last-input-end from moving forward
1172;; when output is inserted.
3ee91e68
RS
1173(defun comint-output-filter (process string)
1174 ;; First check for killed buffer
91d75282
RS
1175 (let ((oprocbuf (process-buffer process)))
1176 (if (and oprocbuf (buffer-name oprocbuf))
1177 (let ((obuf (current-buffer))
1178 (opoint nil) (obeg nil) (oend nil))
1179 (set-buffer oprocbuf)
1180 (setq opoint (point))
1181 (setq obeg (point-min))
1182 (setq oend (point-max))
1183 (let ((buffer-read-only nil)
1184 (nchars (length string))
1185 (ostart nil))
1186 (widen)
1187 (goto-char (process-mark process))
1188 (setq ostart (point))
1189 (if (<= (point) opoint)
1190 (setq opoint (+ opoint nchars)))
1191 ;; Insert after old_begv, but before old_zv.
1192 (if (< (point) obeg)
1193 (setq obeg (+ obeg nchars)))
1194 (if (<= (point) oend)
1195 (setq oend (+ oend nchars)))
1196 (insert-before-markers string)
1197 ;; Don't insert initial prompt outside the top of the window.
1198 (if (= (window-start (selected-window)) (point))
1199 (set-window-start (selected-window) (- (point) (length string))))
1200 (if (and comint-last-input-end
1201 (marker-buffer comint-last-input-end)
1202 (= (point) comint-last-input-end))
1203 (set-marker comint-last-input-end (- comint-last-input-end nchars)))
1204 (set-marker comint-last-output-start ostart)
1205 (set-marker (process-mark process) (point))
1206 (force-mode-line-update))
1207
1208 (narrow-to-region obeg oend)
1209 (goto-char opoint)
fb9ab28a 1210 (let ((functions comint-output-filter-functions))
91d75282
RS
1211 (while functions
1212 (funcall (car functions) string)
1213 (setq functions (cdr functions))))
1214 (set-buffer obuf)))))
3ee91e68 1215
c1da5c03 1216(defun comint-preinput-scroll-to-bottom ()
3ee91e68
RS
1217 "Go to the end of buffer in all windows showing it.
1218Movement occurs if point in the selected window is not after the process mark,
1219and `this-command' is an insertion command. Insertion commands recognised
d637fa56 1220are `self-insert-command', `comint-magic-space', `yank', and `hilit-yank'.
3ee91e68
RS
1221Depends on the value of `comint-scroll-to-bottom-on-input'.
1222
c1da5c03 1223This function should be a pre-command hook."
3ee91e68 1224 (if (and comint-scroll-to-bottom-on-input
91d75282 1225 (memq this-command '(self-insert-command comint-magic-space yank
d637fa56 1226 hilit-yank)))
3ee91e68
RS
1227 (let* ((selected (selected-window))
1228 (current (current-buffer))
1229 (process (get-buffer-process current))
1230 (scroll comint-scroll-to-bottom-on-input))
d637fa56 1231 (if (and process (< (point) (process-mark process)))
0744bf64
RS
1232 (if (eq scroll 'this)
1233 (goto-char (point-max))
1234 (walk-windows
1235 (function (lambda (window)
1236 (if (and (eq (window-buffer window) current)
d637fa56 1237 (or (eq scroll t) (eq scroll 'all)))
0744bf64
RS
1238 (progn
1239 (select-window window)
1240 (goto-char (point-max))
1241 (select-window selected)))))
d637fa56 1242 nil t))))))
3ee91e68 1243
c1da5c03 1244(defun comint-postoutput-scroll-to-bottom (string)
3ee91e68
RS
1245 "Go to the end of buffer in all windows showing it.
1246Does not scroll if the current line is the last line in the buffer.
1247Depends on the value of `comint-scroll-to-bottom-on-output' and
1248`comint-scroll-show-maximum-output'.
1249
fb9ab28a 1250This function should be in the list `comint-output-filter-functions'."
3ee91e68
RS
1251 (let* ((selected (selected-window))
1252 (current (current-buffer))
1253 (process (get-buffer-process current))
1254 (scroll comint-scroll-to-bottom-on-output))
d637fa56
RS
1255 (unwind-protect
1256 (if process
1257 (walk-windows
1258 (function (lambda (window)
1259 (if (eq (window-buffer window) current)
1260 (progn
1261 (select-window window)
1262 (if (and (< (point) (process-mark process))
1263 (or (eq scroll t) (eq scroll 'all)
1264 ;; Maybe user wants point to jump to the end.
1265 (and (eq scroll 'this) (eq selected window))
1266 (and (eq scroll 'others) (not (eq selected window)))
1267 ;; If point was at the end, keep it at the end.
1268 (>= (point)
1269 (- (process-mark process) (length string)))))
1270 (goto-char (process-mark process)))
1271 ;; Optionally scroll so that the text
1272 ;; ends at the bottom of the window.
1273 (if (and comint-scroll-show-maximum-output
1274 (>= (point) (process-mark process)))
1275 (save-excursion
1276 (goto-char (point-max))
1277 (recenter -1)))
1278 (select-window selected)))))
1279 nil t))
1280 (set-buffer current))))
3ee91e68 1281
efcec33f
RS
1282(defun comint-strip-ctrl-m (&optional string)
1283 "Strip trailing `^M' characters from the current output group.
1284
1285This function could be in the list `comint-output-filter-functions' or bound to
1286a key."
1287 (interactive)
1288 (let ((pmark (process-mark (get-buffer-process (current-buffer)))))
1289 (save-excursion
1290 (goto-char
1291 (if (interactive-p) comint-last-input-end comint-last-output-start))
1292 (while (re-search-forward "\r+$" pmark t)
1293 (replace-match "" t t)))))
1294(defalias 'shell-strip-ctrl-m 'comint-strip-ctrl-m)
1295
3ee91e68
RS
1296(defun comint-show-maximum-output ()
1297 "Put the end of the buffer at the bottom of the window."
1298 (interactive)
1299 (goto-char (point-max))
1300 (recenter -1))
5511e411 1301
be9b65ac 1302(defun comint-get-old-input-default ()
cccd719e 1303 "Default for `comint-get-old-input'.
c2ced5d8 1304Take the current line, and discard any initial text matching
cccd719e 1305`comint-prompt-regexp'."
be9b65ac 1306 (save-excursion
49116ac0
JB
1307 (beginning-of-line)
1308 (comint-skip-prompt)
1309 (let ((beg (point)))
1310 (end-of-line)
1311 (buffer-substring beg (point)))))
1312
3ee91e68
RS
1313(defun comint-copy-old-input ()
1314 "Insert after prompt old input at point as new input to be edited.
1315Calls `comint-get-old-input' to get old input."
1316 (interactive)
1317 (let ((input (funcall comint-get-old-input))
1318 (process (get-buffer-process (current-buffer))))
1319 (if (not process)
1320 (error "Current buffer has no process")
1321 (goto-char (process-mark process))
1322 (insert input))))
1323
49116ac0 1324(defun comint-skip-prompt ()
cccd719e 1325 "Skip past the text matching regexp `comint-prompt-regexp'.
49116ac0
JB
1326If this takes us past the end of the current line, don't skip at all."
1327 (let ((eol (save-excursion (end-of-line) (point))))
1328 (if (and (looking-at comint-prompt-regexp)
1329 (<= (match-end 0) eol))
1330 (goto-char (match-end 0)))))
1331
49116ac0 1332(defun comint-after-pmark-p ()
3ee91e68
RS
1333 "Return t if point is after the process output marker."
1334 (let ((pmark (process-mark (get-buffer-process (current-buffer)))))
1335 (<= (marker-position pmark) (point))))
49116ac0
JB
1336
1337(defun comint-simple-send (proc string)
1338 "Default function for sending to PROC input STRING.
1339This just sends STRING plus a newline. To override this,
cccd719e 1340set the hook `comint-input-sender'."
49116ac0
JB
1341 (comint-send-string proc string)
1342 (comint-send-string proc "\n"))
be9b65ac
DL
1343
1344(defun comint-bol (arg)
1345 "Goes to the beginning of line, then skips past the prompt, if any.
3ee91e68 1346If prefix argument is given (\\[universal-argument]) the prompt is not skipped.
be9b65ac 1347
49116ac0 1348The prompt skip is done by skipping text matching the regular expression
a77db92b 1349`comint-prompt-regexp', a buffer local variable."
be9b65ac
DL
1350 (interactive "P")
1351 (beginning-of-line)
49116ac0 1352 (if (null arg) (comint-skip-prompt)))
be9b65ac 1353
21d9bf27 1354;;; These three functions are for entering text you don't want echoed or
be9b65ac 1355;;; saved -- typically passwords to ftp, telnet, or somesuch.
21d9bf27
RS
1356;;; Just enter m-x send-invisible and type in your line, or add
1357;;; `comint-watch-for-password-prompt' to `comint-output-filter-functions'.
49116ac0 1358
87ef29fd 1359(defun comint-read-noecho (prompt &optional stars)
4be9a25f
NF
1360 "Read a single line of text from user without echoing, and return it.
1361Prompt with argument PROMPT, a string. Optional argument STARS causes
1362input to be echoed with '*' characters on the prompt line. Input ends with
1363RET, LFD, or ESC. DEL or C-h rubs out. C-u kills line. C-g aborts (if
cccd719e
RS
1364`inhibit-quit' is set because e.g. this function was called from a process
1365filter and C-g is pressed, this function returns nil rather than a string).
4be9a25f
NF
1366
1367Note that the keystrokes comprising the text can still be recovered
cccd719e 1368\(temporarily) with \\[view-lossage]. This may be a security bug for some
4be9a25f
NF
1369applications."
1370 (let ((ans "")
1371 (c 0)
1372 (echo-keystrokes 0)
87ef29fd 1373 (cursor-in-echo-area t)
4be9a25f
NF
1374 (done nil))
1375 (while (not done)
1376 (if stars
1377 (message "%s%s" prompt (make-string (length ans) ?*))
1378 (message prompt))
1379 (setq c (read-char))
1380 (cond ((= c ?\C-g)
1381 ;; This function may get called from a process filter, where
1382 ;; inhibit-quit is set. In later versions of emacs read-char
1383 ;; may clear quit-flag itself and return C-g. That would make
1384 ;; it impossible to quit this loop in a simple way, so
1385 ;; re-enable it here (for backward-compatibility the check for
acdf173a 1386 ;; quit-flag below would still be necessary, so this seems
4be9a25f
NF
1387 ;; like the simplest way to do things).
1388 (setq quit-flag t
1389 done t))
1390 ((or (= c ?\r) (= c ?\n) (= c ?\e))
1391 (setq done t))
1392 ((= c ?\C-u)
1393 (setq ans ""))
1394 ((and (/= c ?\b) (/= c ?\177))
1395 (setq ans (concat ans (char-to-string c))))
1396 ((> (length ans) 0)
1397 (setq ans (substring ans 0 -1)))))
1398 (if quit-flag
1399 ;; Emulate a true quit, except that we have to return a value.
1400 (prog1
1401 (setq quit-flag nil)
1402 (message "Quit")
1403 (beep t))
1404 (message "")
1405 ans)))
87ef29fd 1406
be9b65ac 1407(defun send-invisible (str)
c2ced5d8 1408 "Read a string without echoing.
cccd719e
RS
1409Then send it to the process running in the current buffer. A new-line
1410is additionally sent. String is not saved on comint input history list.
49116ac0
JB
1411Security bug: your string can still be temporarily recovered with
1412\\[view-lossage]."
be9b65ac
DL
1413 (interactive "P") ; Defeat snooping via C-x esc
1414 (let ((proc (get-buffer-process (current-buffer))))
21d9bf27
RS
1415 (if (not proc)
1416 (error "Current buffer has no process")
1417 (comint-send-string
1418 proc (if (stringp str) str (comint-read-noecho "Non-echoed text: " t)))
1419 (comint-send-string proc "\n"))))
1420
1421(defun comint-watch-for-password-prompt (string)
1422 "Prompt in the minibuffer for password and send without echoing.
1423This function uses `send-invisible' to read and send a password to the buffer's
654d89d7
NF
1424process if STRING contains a password prompt defined by
1425`comint-password-prompt-regexp'.
21d9bf27
RS
1426
1427This function could be in the list `comint-output-filter-functions'."
654d89d7 1428 (if (string-match comint-password-prompt-regexp string)
21d9bf27 1429 (send-invisible nil)))
be9b65ac
DL
1430\f
1431;;; Low-level process communication
1432
7df7d5b3
RS
1433(defalias 'comint-send-string 'process-send-string)
1434(defalias 'comint-send-region 'process-send-region)
be9b65ac
DL
1435\f
1436;;; Random input hackage
1437
49116ac0 1438(defun comint-kill-output ()
3ee91e68
RS
1439 "Kill all output from interpreter since last input.
1440Does not delete the prompt."
be9b65ac 1441 (interactive)
a9a6f83f
RS
1442 (let ((proc (get-buffer-process (current-buffer)))
1443 (replacement nil))
1444 (save-excursion
1445 (let ((pmark (progn (goto-char (process-mark proc))
1446 (beginning-of-line nil)
1447 (point-marker))))
1448 (delete-region comint-last-input-end pmark)
1449 (comint-skip-prompt)
1450 (setq replacement (concat "*** output flushed ***\n"
1451 (buffer-substring pmark (point))))
1452 (delete-region pmark (point))))
1453 ;; Output message and put back prompt
1454 (comint-output-filter proc replacement)))
be9b65ac
DL
1455
1456(defun comint-show-output ()
49116ac0 1457 "Display start of this batch of interpreter output at top of window.
21d9bf27 1458Sets mark to the value of point when this command is run."
be9b65ac 1459 (interactive)
815c1a20 1460 (push-mark)
3ee91e68 1461 (let ((pos (point)))
42453245 1462 (goto-char (or (marker-position comint-last-input-end) (point-max)))
3ee91e68
RS
1463 (beginning-of-line 0)
1464 (set-window-start (selected-window) (point))
815c1a20 1465 (comint-skip-prompt)))
be9b65ac
DL
1466
1467(defun comint-interrupt-subjob ()
49116ac0 1468 "Interrupt the current subjob."
be9b65ac 1469 (interactive)
49116ac0 1470 (interrupt-process nil comint-ptyp))
be9b65ac
DL
1471
1472(defun comint-kill-subjob ()
49116ac0 1473 "Send kill signal to the current subjob."
be9b65ac 1474 (interactive)
49116ac0 1475 (kill-process nil comint-ptyp))
be9b65ac
DL
1476
1477(defun comint-quit-subjob ()
49116ac0 1478 "Send quit signal to the current subjob."
be9b65ac 1479 (interactive)
49116ac0 1480 (quit-process nil comint-ptyp))
be9b65ac
DL
1481
1482(defun comint-stop-subjob ()
49116ac0 1483 "Stop the current subjob.
be9b65ac 1484WARNING: if there is no current subjob, you can end up suspending
49116ac0
JB
1485the top-level process running in the buffer. If you accidentally do
1486this, use \\[comint-continue-subjob] to resume the process. (This
1487is not a problem with most shells, since they ignore this signal.)"
be9b65ac 1488 (interactive)
49116ac0 1489 (stop-process nil comint-ptyp))
be9b65ac
DL
1490
1491(defun comint-continue-subjob ()
49116ac0 1492 "Send CONT signal to process buffer's process group.
be9b65ac
DL
1493Useful if you accidentally suspend the top-level process."
1494 (interactive)
49116ac0 1495 (continue-process nil comint-ptyp))
be9b65ac
DL
1496
1497(defun comint-kill-input ()
49116ac0 1498 "Kill all text from last stuff output by interpreter to point."
be9b65ac 1499 (interactive)
3ee91e68
RS
1500 (let ((pmark (process-mark (get-buffer-process (current-buffer)))))
1501 (if (> (point) (marker-position pmark))
49116ac0 1502 (kill-region pmark (point)))))
be9b65ac
DL
1503
1504(defun comint-delchar-or-maybe-eof (arg)
3ee91e68 1505 "Delete ARG characters forward, or (if at eob) send an EOF to subprocess."
be9b65ac
DL
1506 (interactive "p")
1507 (if (eobp)
1508 (process-send-eof)
3ee91e68 1509 (delete-char arg)))
49116ac0 1510
1de80a34
RM
1511(defun comint-send-eof ()
1512 "Send an EOF to the current buffer's process."
1513 (interactive)
1514 (process-send-eof))
49116ac0 1515
3ee91e68
RS
1516
1517(defun comint-backward-matching-input (regexp arg)
1518 "Search backward through buffer for match for REGEXP.
1519Matches are searched for on lines that match `comint-prompt-regexp'.
1520With prefix argument N, search for Nth previous match.
1521If N is negative, find the next or Nth next match."
1522 (interactive (comint-regexp-arg "Backward input matching (regexp): "))
1523 (let* ((re (concat comint-prompt-regexp ".*" regexp))
1524 (pos (save-excursion (end-of-line (if (> arg 0) 0 1))
1525 (if (re-search-backward re nil t arg)
1526 (point)))))
1527 (if (null pos)
1528 (progn (message "Not found")
1529 (ding))
1530 (goto-char pos)
1531 (comint-bol nil))))
1532
1533(defun comint-forward-matching-input (regexp arg)
1534 "Search forward through buffer for match for REGEXP.
1535Matches are searched for on lines that match `comint-prompt-regexp'.
1536With prefix argument N, search for Nth following match.
1537If N is negative, find the previous or Nth previous match."
1538 (interactive (comint-regexp-arg "Forward input matching (regexp): "))
1539 (comint-backward-matching-input regexp (- arg)))
1540
1541
1de80a34 1542(defun comint-next-prompt (n)
3ee91e68 1543 "Move to end of Nth next prompt in the buffer.
1de80a34
RM
1544See `comint-prompt-regexp'."
1545 (interactive "p")
3ee91e68
RS
1546 (let ((paragraph-start comint-prompt-regexp))
1547 (end-of-line (if (> n 0) 1 0))
1548 (forward-paragraph n)
1549 (comint-skip-prompt)))
49116ac0 1550
3ee91e68
RS
1551(defun comint-previous-prompt (n)
1552 "Move to end of Nth previous prompt in the buffer.
1de80a34
RM
1553See `comint-prompt-regexp'."
1554 (interactive "p")
3ee91e68 1555 (comint-next-prompt (- n)))
be9b65ac
DL
1556\f
1557;;; Support for source-file processing commands.
1558;;;============================================================================
1559;;; Many command-interpreters (e.g., Lisp, Scheme, Soar) have
1560;;; commands that process files of source text (e.g. loading or compiling
1561;;; files). So the corresponding process-in-a-buffer modes have commands
1562;;; for doing this (e.g., lisp-load-file). The functions below are useful
1563;;; for defining these commands.
1564;;;
1565;;; Alas, these guys don't do exactly the right thing for Lisp, Scheme
1566;;; and Soar, in that they don't know anything about file extensions.
1567;;; So the compile/load interface gets the wrong default occasionally.
1568;;; The load-file/compile-file default mechanism could be smarter -- it
1569;;; doesn't know about the relationship between filename extensions and
1570;;; whether the file is source or executable. If you compile foo.lisp
1571;;; with compile-file, then the next load-file should use foo.bin for
1572;;; the default, not foo.lisp. This is tricky to do right, particularly
1573;;; because the extension for executable files varies so much (.o, .bin,
1574;;; .lbin, .mo, .vo, .ao, ...).
1575
1576
1577;;; COMINT-SOURCE-DEFAULT -- determines defaults for source-file processing
1578;;; commands.
1579;;;
1580;;; COMINT-CHECK-SOURCE -- if FNAME is in a modified buffer, asks you if you
1581;;; want to save the buffer before issuing any process requests to the command
1582;;; interpreter.
1583;;;
1584;;; COMINT-GET-SOURCE -- used by the source-file processing commands to prompt
1585;;; for the file to process.
1586
1587;;; (COMINT-SOURCE-DEFAULT previous-dir/file source-modes)
1588;;;============================================================================
1589;;; This function computes the defaults for the load-file and compile-file
49116ac0 1590;;; commands for tea, soar, cmulisp, and cmuscheme modes.
be9b65ac
DL
1591;;;
1592;;; - PREVIOUS-DIR/FILE is a pair (directory . filename) from the last
1593;;; source-file processing command. NIL if there hasn't been one yet.
1594;;; - SOURCE-MODES is a list used to determine what buffers contain source
1595;;; files: if the major mode of the buffer is in SOURCE-MODES, it's source.
1596;;; Typically, (lisp-mode) or (scheme-mode).
1597;;;
1598;;; If the command is given while the cursor is inside a string, *and*
1599;;; the string is an existing filename, *and* the filename is not a directory,
1600;;; then the string is taken as default. This allows you to just position
1601;;; your cursor over a string that's a filename and have it taken as default.
1602;;;
1603;;; If the command is given in a file buffer whose major mode is in
1604;;; SOURCE-MODES, then the the filename is the default file, and the
1605;;; file's directory is the default directory.
1606;;;
1607;;; If the buffer isn't a source file buffer (e.g., it's the process buffer),
1608;;; then the default directory & file are what was used in the last source-file
1609;;; processing command (i.e., PREVIOUS-DIR/FILE). If this is the first time
1610;;; the command has been run (PREVIOUS-DIR/FILE is nil), the default directory
1611;;; is the cwd, with no default file. (\"no default file\" = nil)
1612;;;
1613;;; SOURCE-REGEXP is typically going to be something like (tea-mode)
1614;;; for T programs, (lisp-mode) for Lisp programs, (soar-mode lisp-mode)
1615;;; for Soar programs, etc.
1616;;;
1617;;; The function returns a pair: (default-directory . default-file).
1618
1619(defun comint-source-default (previous-dir/file source-modes)
1620 (cond ((and buffer-file-name (memq major-mode source-modes))
1621 (cons (file-name-directory buffer-file-name)
1622 (file-name-nondirectory buffer-file-name)))
1623 (previous-dir/file)
1624 (t
1625 (cons default-directory nil))))
1626
49116ac0 1627
be9b65ac
DL
1628;;; (COMINT-CHECK-SOURCE fname)
1629;;;============================================================================
49116ac0 1630;;; Prior to loading or compiling (or otherwise processing) a file (in the CMU
be9b65ac
DL
1631;;; process-in-a-buffer modes), this function can be called on the filename.
1632;;; If the file is loaded into a buffer, and the buffer is modified, the user
1633;;; is queried to see if he wants to save the buffer before proceeding with
1634;;; the load or compile.
1635
1636(defun comint-check-source (fname)
1637 (let ((buff (get-file-buffer fname)))
1638 (if (and buff
1639 (buffer-modified-p buff)
3ee91e68 1640 (y-or-n-p (format "Save buffer %s first? " (buffer-name buff))))
be9b65ac
DL
1641 ;; save BUFF.
1642 (let ((old-buffer (current-buffer)))
1643 (set-buffer buff)
1644 (save-buffer)
1645 (set-buffer old-buffer)))))
1646
49116ac0 1647
be9b65ac
DL
1648;;; (COMINT-GET-SOURCE prompt prev-dir/file source-modes mustmatch-p)
1649;;;============================================================================
1650;;; COMINT-GET-SOURCE is used to prompt for filenames in command-interpreter
1651;;; commands that process source files (like loading or compiling a file).
1652;;; It prompts for the filename, provides a default, if there is one,
1653;;; and returns the result filename.
1654;;;
1655;;; See COMINT-SOURCE-DEFAULT for more on determining defaults.
1656;;;
1657;;; PROMPT is the prompt string. PREV-DIR/FILE is the (directory . file) pair
1658;;; from the last source processing command. SOURCE-MODES is a list of major
1659;;; modes used to determine what file buffers contain source files. (These
1660;;; two arguments are used for determining defaults). If MUSTMATCH-P is true,
1661;;; then the filename reader will only accept a file that exists.
1662;;;
1663;;; A typical use:
1664;;; (interactive (comint-get-source "Compile file: " prev-lisp-dir/file
1665;;; '(lisp-mode) t))
1666
1667;;; This is pretty stupid about strings. It decides we're in a string
1668;;; if there's a quote on both sides of point on the current line.
1669(defun comint-extract-string ()
cccd719e 1670 "Return string around POINT that starts the current line, or nil."
be9b65ac
DL
1671 (save-excursion
1672 (let* ((point (point))
1673 (bol (progn (beginning-of-line) (point)))
1674 (eol (progn (end-of-line) (point)))
1675 (start (progn (goto-char point)
1676 (and (search-backward "\"" bol t)
1677 (1+ (point)))))
1678 (end (progn (goto-char point)
1679 (and (search-forward "\"" eol t)
1680 (1- (point))))))
1681 (and start end
1682 (buffer-substring start end)))))
1683
1684(defun comint-get-source (prompt prev-dir/file source-modes mustmatch-p)
1685 (let* ((def (comint-source-default prev-dir/file source-modes))
1686 (stringfile (comint-extract-string))
1687 (sfile-p (and stringfile
49116ac0
JB
1688 (condition-case ()
1689 (file-exists-p stringfile)
1690 (error nil))
be9b65ac
DL
1691 (not (file-directory-p stringfile))))
1692 (defdir (if sfile-p (file-name-directory stringfile)
1693 (car def)))
1694 (deffile (if sfile-p (file-name-nondirectory stringfile)
1695 (cdr def)))
1696 (ans (read-file-name (if deffile (format "%s(default %s) "
1697 prompt deffile)
1698 prompt)
1699 defdir
1700 (concat defdir deffile)
1701 mustmatch-p)))
1702 (list (expand-file-name (substitute-in-file-name ans)))))
1703
49116ac0
JB
1704;;; I am somewhat divided on this string-default feature. It seems
1705;;; to violate the principle-of-least-astonishment, in that it makes
1706;;; the default harder to predict, so you actually have to look and see
1707;;; what the default really is before choosing it. This can trip you up.
1708;;; On the other hand, it can be useful, I guess. I would appreciate feedback
1709;;; on this.
1710;;; -Olin
1711
be9b65ac
DL
1712\f
1713;;; Simple process query facility.
1714;;; ===========================================================================
1715;;; This function is for commands that want to send a query to the process
1716;;; and show the response to the user. For example, a command to get the
1717;;; arglist for a Common Lisp function might send a "(arglist 'foo)" query
1718;;; to an inferior Common Lisp process.
1719;;;
1720;;; This simple facility just sends strings to the inferior process and pops
1721;;; up a window for the process buffer so you can see what the process
1722;;; responds with. We don't do anything fancy like try to intercept what the
1723;;; process responds with and put it in a pop-up window or on the message
1724;;; line. We just display the buffer. Low tech. Simple. Works good.
1725
1726;;; Send to the inferior process PROC the string STR. Pop-up but do not select
1727;;; a window for the inferior process so that its response can be seen.
1728(defun comint-proc-query (proc str)
1729 (let* ((proc-buf (process-buffer proc))
1730 (proc-mark (process-mark proc)))
1731 (display-buffer proc-buf)
1732 (set-buffer proc-buf) ; but it's not the selected *window*
1733 (let ((proc-win (get-buffer-window proc-buf))
1734 (proc-pt (marker-position proc-mark)))
1735 (comint-send-string proc str) ; send the query
1736 (accept-process-output proc) ; wait for some output
1737 ;; Try to position the proc window so you can see the answer.
1738 ;; This is bogus code. If you delete the (sit-for 0), it breaks.
1739 ;; I don't know why. Wizards invited to improve it.
1740 (if (not (pos-visible-in-window-p proc-pt proc-win))
1741 (let ((opoint (window-point proc-win)))
3ee91e68
RS
1742 (set-window-point proc-win proc-mark)
1743 (sit-for 0)
be9b65ac
DL
1744 (if (not (pos-visible-in-window-p opoint proc-win))
1745 (push-mark opoint)
3ee91e68 1746 (set-window-point proc-win opoint)))))))
be9b65ac
DL
1747
1748\f
3ee91e68 1749;;; Filename/command/history completion in a buffer
be9b65ac
DL
1750;;; ===========================================================================
1751;;; Useful completion functions, courtesy of the Ergo group.
be9b65ac 1752
1b421826 1753;;; Six commands:
3ee91e68
RS
1754;;; comint-dynamic-complete Complete or expand command, filename,
1755;;; history at point.
1756;;; comint-dynamic-complete-filename Complete filename at point.
3ee91e68 1757;;; comint-dynamic-list-filename-completions List completions in help buffer.
be9b65ac
DL
1758;;; comint-replace-by-expanded-filename Expand and complete filename at point;
1759;;; replace with expanded/completed name.
3ee91e68
RS
1760;;; comint-dynamic-simple-complete Complete stub given candidates.
1761
be9b65ac 1762;;; These are not installed in the comint-mode keymap. But they are
49116ac0 1763;;; available for people who want them. Shell-mode installs them:
735b3061 1764;;; (define-key shell-mode-map "\t" 'comint-dynamic-complete)
3ee91e68
RS
1765;;; (define-key shell-mode-map "\M-?"
1766;;; 'comint-dynamic-list-filename-completions)))
49116ac0
JB
1767;;;
1768;;; Commands like this are fine things to put in load hooks if you
1586b965 1769;;; want them present in specific modes.
49116ac0 1770
3ee91e68
RS
1771(defvar comint-completion-autolist nil
1772 "*If non-nil, automatically list possiblities on partial completion.
fdb7791d 1773This mirrors the optional behavior of tcsh.")
3ee91e68
RS
1774
1775(defvar comint-completion-addsuffix t
1776 "*If non-nil, add a `/' to completed directories, ` ' to file names.
fdb7791d 1777This mirrors the optional behavior of tcsh.")
3ee91e68
RS
1778
1779(defvar comint-completion-recexact nil
1780 "*If non-nil, use shortest completion if characters cannot be added.
fdb7791d 1781This mirrors the optional behavior of tcsh.
3ee91e68
RS
1782
1783A non-nil value is useful if `comint-completion-autolist' is non-nil too.")
1784
497513ad
RS
1785(defvar comint-completion-fignore nil
1786 "*List of suffixes to be disregarded during file completion.
1787This mirrors the optional behavior of bash and tcsh.
1788
1789Note that this applies to `comint-dynamic-complete-filename' only.")
1790
3ee91e68
RS
1791(defvar comint-file-name-prefix ""
1792 "Prefix prepended to absolute file names taken from process input.
1793This is used by comint's and shell's completion functions, and by shell's
1794directory tracking functions.")
be9b65ac 1795
3ee91e68
RS
1796
1797(defun comint-directory (directory)
1798 ;; Return expanded DIRECTORY, with `comint-file-name-prefix' if absolute.
1799 (expand-file-name (if (file-name-absolute-p directory)
1800 (concat comint-file-name-prefix directory)
1801 directory)))
1802
1803
1b421826
RS
1804(defun comint-word (word-chars)
1805 "Return the word of WORD-CHARS at point, or nil if non is found.
1806Word constituents are considered to be those in WORD-CHARS, which is like the
1807inside of a \"[...]\" (see `skip-chars-forward')."
3ee91e68 1808 (save-excursion
1b421826
RS
1809 (let ((limit (point))
1810 (word (concat "[" word-chars "]"))
1811 (non-word (concat "[^" word-chars "]")))
1812 (if (re-search-backward non-word nil 'move)
1813 (forward-char 1))
1814 ;; Anchor the search forwards.
1815 (if (or (eolp) (looking-at non-word))
1816 nil
1817 (re-search-forward (concat word "+") limit)
1818 (buffer-substring (match-beginning 0) (match-end 0))))))
3ee91e68 1819
3ee91e68 1820
1b421826
RS
1821(defun comint-match-partial-filename ()
1822 "Return the filename at point, or nil if non is found.
1823Environment variables are substituted. See `comint-word'."
1824 (let ((filename (comint-word "~/A-Za-z0-9+@:_.$#,={}-")))
1825 (and filename (substitute-in-file-name filename))))
be9b65ac 1826
49116ac0 1827
be9b65ac 1828(defun comint-dynamic-complete ()
1b421826
RS
1829 "Dynamically perform completion at point.
1830Calls the functions in `comint-dynamic-complete-functions' to perform
1831completion until a function returns non-nil, at which point completion is
1832assumed to have occurred."
3ee91e68 1833 (interactive)
1b421826
RS
1834 (let ((functions comint-dynamic-complete-functions))
1835 (while (and functions (null (funcall (car functions))))
1836 (setq functions (cdr functions)))))
3ee91e68
RS
1837
1838
1839(defun comint-dynamic-complete-filename ()
49116ac0 1840 "Dynamically complete the filename at point.
1b421826
RS
1841Completes if after a filename. See `comint-match-partial-filename' and
1842`comint-dynamic-complete-as-filename'.
3ee91e68
RS
1843This function is similar to `comint-replace-by-expanded-filename', except that
1844it won't change parts of the filename already entered in the buffer; it just
1845adds completion characters to the end of the filename. A completions listing
1846may be shown in a help buffer if completion is ambiguous.
1847
497513ad
RS
1848Completion is dependent on the value of `comint-completion-addsuffix',
1849`comint-completion-recexact' and `comint-completion-fignore', and the timing of
1850completions listing is dependent on the value of `comint-completion-autolist'.
1b421826
RS
1851
1852Returns t if successful."
3ee91e68 1853 (interactive)
1b421826 1854 (if (comint-match-partial-filename)
22f33dd9
RS
1855 (prog2 (or (eq (selected-window) (minibuffer-window))
1856 (message "Completing file name..."))
1b421826
RS
1857 (comint-dynamic-complete-as-filename))))
1858
1859
1860(defun comint-dynamic-complete-as-filename ()
1861 "Dynamically complete at point as a filename.
1862See `comint-dynamic-complete-filename'. Returns t if successful."
3ee91e68 1863 (let* ((completion-ignore-case nil)
497513ad 1864 (completion-ignored-extensions comint-completion-fignore)
1b421826 1865 (success t)
f79f58bc
KH
1866 (filename (or (comint-match-partial-filename) ""))
1867 (pathdir (file-name-directory filename))
1868 (pathnondir (file-name-nondirectory filename))
1869 (directory (if pathdir (comint-directory pathdir) default-directory))
22f33dd9
RS
1870 (completion (file-name-completion pathnondir directory))
1871 (mini-flag (eq (selected-window) (minibuffer-window))))
3ee91e68
RS
1872 (cond ((null completion)
1873 (message "No completions of %s" filename)
1b421826 1874 (setq success nil))
3ee91e68
RS
1875 ((eq completion t) ; Means already completed "file".
1876 (if comint-completion-addsuffix (insert " "))
22f33dd9 1877 (or mini-flag (message "Sole completion")))
3ee91e68
RS
1878 ((string-equal completion "") ; Means completion on "directory/".
1879 (comint-dynamic-list-filename-completions))
1880 (t ; Completion string returned.
1881 (let ((file (concat (file-name-as-directory directory) completion)))
3ee91e68
RS
1882 (insert (substring (directory-file-name completion)
1883 (length pathnondir)))
1884 (cond ((symbolp (file-name-completion completion directory))
1885 ;; We inserted a unique completion.
1886 (if comint-completion-addsuffix
1887 (insert (if (file-directory-p file) "/" " ")))
22f33dd9 1888 (or mini-flag (message "Completed")))
3ee91e68
RS
1889 ((and comint-completion-recexact comint-completion-addsuffix
1890 (string-equal pathnondir completion)
1891 (file-exists-p file))
1892 ;; It's not unique, but user wants shortest match.
1893 (insert (if (file-directory-p file) "/" " "))
22f33dd9 1894 (or mini-flag (message "Completed shortest")))
3ee91e68
RS
1895 ((or comint-completion-autolist
1896 (string-equal pathnondir completion))
1897 ;; It's not unique, list possible completions.
1898 (comint-dynamic-list-filename-completions))
1899 (t
22f33dd9 1900 (or mini-flag (message "Partially completed")))))))
1b421826 1901 success))
3ee91e68
RS
1902
1903
1904(defun comint-replace-by-expanded-filename ()
1905 "Dynamically expand and complete the filename at point.
1906Replace the filename with an expanded, canonicalised and completed replacement.
1907\"Expanded\" means environment variables (e.g., $HOME) and `~'s are replaced
1908with the corresponding directories. \"Canonicalised\" means `..' and `.' are
1909removed, and the filename is made absolute instead of relative. For expansion
1910see `expand-file-name' and `substitute-in-file-name'. For completion see
1911`comint-dynamic-complete-filename'."
be9b65ac 1912 (interactive)
3ee91e68
RS
1913 (replace-match (expand-file-name (comint-match-partial-filename)) t t)
1914 (comint-dynamic-complete-filename))
1915
1916
3ee91e68
RS
1917(defun comint-dynamic-simple-complete (stub candidates)
1918 "Dynamically complete STUB from CANDIDATES list.
1919This function inserts completion characters at point by completing STUB from
1920the strings in CANDIDATES. A completions listing may be shown in a help buffer
1921if completion is ambiguous.
1922
1b421826
RS
1923Returns nil if no completion was inserted.
1924Returns `sole' if completed with the only completion match.
1925Returns `shortest' if completed with the shortest of the completion matches.
1926Returns `partial' if completed as far as possible with the completion matches.
1927Returns `listed' if a completion listing was shown.
1928
3ee91e68
RS
1929See also `comint-dynamic-complete-filename'."
1930 (let* ((completion-ignore-case nil)
1931 (candidates (mapcar (function (lambda (x) (list x))) candidates))
1932 (completions (all-completions stub candidates)))
be9b65ac 1933 (cond ((null completions)
3ee91e68 1934 (message "No completions of %s" stub)
1b421826 1935 nil)
3ee91e68
RS
1936 ((= 1 (length completions)) ; Gotcha!
1937 (let ((completion (car completions)))
1938 (if (string-equal completion stub)
1939 (message "Sole completion")
1940 (insert (substring completion (length stub)))
1941 (message "Completed"))
1b421826
RS
1942 (if comint-completion-addsuffix (insert " "))
1943 'sole))
3ee91e68
RS
1944 (t ; There's no unique completion.
1945 (let ((completion (try-completion stub candidates)))
1946 ;; Insert the longest substring.
1947 (insert (substring completion (length stub)))
1948 (cond ((and comint-completion-recexact comint-completion-addsuffix
1949 (string-equal stub completion)
1950 (member completion completions))
1951 ;; It's not unique, but user wants shortest match.
1952 (insert " ")
1b421826
RS
1953 (message "Completed shortest")
1954 'shortest)
3ee91e68
RS
1955 ((or comint-completion-autolist
1956 (string-equal stub completion))
1957 ;; It's not unique, list possible completions.
1b421826
RS
1958 (comint-dynamic-list-completions completions)
1959 'listed)
3ee91e68 1960 (t
1b421826
RS
1961 (message "Partially completed")
1962 'partial)))))))
3ee91e68
RS
1963
1964
1965(defun comint-dynamic-list-filename-completions ()
1966 "List in help buffer possible completions of the filename at point."
1967 (interactive)
1968 (let* ((completion-ignore-case nil)
1b421826 1969 (filename (or (comint-match-partial-filename) ""))
3ee91e68
RS
1970 (pathdir (file-name-directory filename))
1971 (pathnondir (file-name-nondirectory filename))
1972 (directory (if pathdir (comint-directory pathdir) default-directory))
1973 (completions (file-name-all-completions pathnondir directory)))
1974 (if completions
1975 (comint-dynamic-list-completions completions)
1b421826 1976 (message "No completions of %s" filename))))
3ee91e68
RS
1977
1978
1979(defun comint-dynamic-list-completions (completions)
1980 "List in help buffer sorted COMPLETIONS.
1981Typing SPC flushes the help buffer."
1982 (let ((conf (current-window-configuration)))
dd3038f6 1983 (with-output-to-temp-buffer "*Completions*"
3ee91e68 1984 (display-completion-list (sort completions 'string-lessp)))
3ee91e68 1985 (message "Hit space to flush")
7d5d472e
RS
1986 (let (key first)
1987 (if (save-excursion
dd3038f6 1988 (set-buffer (get-buffer "*Completions*"))
7d5d472e
RS
1989 (setq key (read-key-sequence nil)
1990 first (aref key 0))
1991 (and (consp first)
1992 (eq (window-buffer (posn-window (event-start first)))
dd3038f6 1993 (get-buffer "*Completions*"))
7d5d472e
RS
1994 (eq (key-binding key) 'mouse-choose-completion)))
1995 ;; If the user does mouse-choose-completion with the mouse,
1996 ;; execute the command, then delete the completion window.
1997 (progn
1998 (mouse-choose-completion first)
1999 (set-window-configuration conf))
2000 (if (eq first ?\ )
2001 (set-window-configuration conf)
2b5a5ed3 2002 (setq unread-command-events (listify-key-sequence key)))))))
be9b65ac
DL
2003\f
2004;;; Converting process modes to use comint mode
2005;;; ===========================================================================
be14d312
ER
2006;;; The code in the Emacs 19 distribution has all been modified to use comint
2007;;; where needed. However, there are `third-party' packages out there that
2008;;; still use the old shell mode. Here's a guide to conversion.
49116ac0 2009;;;
be9b65ac 2010;;; Renaming variables
49116ac0
JB
2011;;; Most of the work is renaming variables and functions. These are the common
2012;;; ones:
2013;;; Local variables:
87ef29fd 2014;;; last-input-start comint-last-input-start
49116ac0 2015;;; last-input-end comint-last-input-end
49116ac0
JB
2016;;; shell-prompt-pattern comint-prompt-regexp
2017;;; shell-set-directory-error-hook <no equivalent>
2018;;; Miscellaneous:
be9b65ac
DL
2019;;; shell-set-directory <unnecessary>
2020;;; shell-mode-map comint-mode-map
49116ac0 2021;;; Commands:
be9b65ac
DL
2022;;; shell-send-input comint-send-input
2023;;; shell-send-eof comint-delchar-or-maybe-eof
2024;;; kill-shell-input comint-kill-input
2025;;; interrupt-shell-subjob comint-interrupt-subjob
2026;;; stop-shell-subjob comint-stop-subjob
2027;;; quit-shell-subjob comint-quit-subjob
2028;;; kill-shell-subjob comint-kill-subjob
2029;;; kill-output-from-shell comint-kill-output
2030;;; show-output-from-shell comint-show-output
2031;;; copy-last-shell-input Use comint-previous-input/comint-next-input
2032;;;
87ef29fd 2033;;; SHELL-SET-DIRECTORY is gone, its functionality taken over by
fb9ab28a 2034;;; SHELL-DIRECTORY-TRACKER, the shell mode's comint-input-filter-functions.
87ef29fd 2035;;; Comint mode does not provide functionality equivalent to
be9b65ac 2036;;; shell-set-directory-error-hook; it is gone.
87ef29fd
JB
2037;;;
2038;;; comint-last-input-start is provided for modes which want to munge
2039;;; the buffer after input is sent, perhaps because the inferior
2040;;; insists on echoing the input. The LAST-INPUT-START variable in
2041;;; the old shell package was used to implement a history mechanism,
2042;;; but you should think twice before using comint-last-input-start
2043;;; for this; the input history ring often does the job better.
be9b65ac
DL
2044;;;
2045;;; If you are implementing some process-in-a-buffer mode, called foo-mode, do
2046;;; *not* create the comint-mode local variables in your foo-mode function.
2047;;; This is not modular. Instead, call comint-mode, and let *it* create the
2048;;; necessary comint-specific local variables. Then create the
2049;;; foo-mode-specific local variables in foo-mode. Set the buffer's keymap to
2050;;; be foo-mode-map, and its mode to be foo-mode. Set the comint-mode hooks
fb9ab28a 2051;;; (comint-{prompt-regexp, input-filter, input-filter-functions,
1b421826 2052;;; get-old-input) that need to be different from the defaults. Call
be9b65ac 2053;;; foo-mode-hook, and you're done. Don't run the comint-mode hook yourself;
735b3061 2054;;; comint-mode will take care of it. The following example, from shell.el,
49116ac0
JB
2055;;; is typical:
2056;;;
d55d5ec2
RS
2057;;; (defvar shell-mode-map '())
2058;;; (cond ((not shell-mode-map)
63ab7e8f 2059;;; (setq shell-mode-map (copy-keymap comint-mode-map))
d55d5ec2
RS
2060;;; (define-key shell-mode-map "\C-c\C-f" 'shell-forward-command)
2061;;; (define-key shell-mode-map "\C-c\C-b" 'shell-backward-command)
2062;;; (define-key shell-mode-map "\t" 'comint-dynamic-complete)
2063;;; (define-key shell-mode-map "\M-?"
2064;;; 'comint-dynamic-list-filename-completions)))
2065;;;
49116ac0
JB
2066;;; (defun shell-mode ()
2067;;; (interactive)
2068;;; (comint-mode)
2069;;; (setq comint-prompt-regexp shell-prompt-pattern)
2070;;; (setq major-mode 'shell-mode)
2071;;; (setq mode-name "Shell")
49116ac0
JB
2072;;; (use-local-map shell-mode-map)
2073;;; (make-local-variable 'shell-directory-stack)
2074;;; (setq shell-directory-stack nil)
fb9ab28a 2075;;; (add-hook 'comint-input-filter-functions 'shell-directory-tracker)
49116ac0
JB
2076;;; (run-hooks 'shell-mode-hook))
2077;;;
be9b65ac
DL
2078;;;
2079;;; Note that make-comint is different from make-shell in that it
2080;;; doesn't have a default program argument. If you give make-shell
2081;;; a program name of NIL, it cleverly chooses one of explicit-shell-name,
2082;;; $ESHELL, $SHELL, or /bin/sh. If you give make-comint a program argument
2083;;; of NIL, it barfs. Adjust your code accordingly...
49116ac0 2084;;;
3ee91e68
RS
2085;;; Completion for comint-mode users
2086;;;
1b421826
RS
2087;;; For modes that use comint-mode, comint-dynamic-complete-functions is the
2088;;; hook to add completion functions to. Functions on this list should return
2089;;; non-nil if completion occurs (i.e., further completion should not occur).
3ee91e68
RS
2090;;; You could use comint-dynamic-simple-complete to do the bulk of the
2091;;; completion job.
49116ac0 2092\f
49116ac0 2093(provide 'comint)
c0274f38
ER
2094
2095;;; comint.el ends here