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