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