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