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