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