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