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