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