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