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