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