Sync to HEAD
[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,2000,01,02,03,2004
4 ;; Free Software Foundation, Inc.
5
6 ;; Author: Olin Shivers <shivers@cs.cmu.edu>
7 ;; Simon Marshall <simon@gnu.org>
8 ;; Maintainer: FSF
9 ;; Keywords: processes
10
11 ;; This file is part of GNU Emacs.
12
13 ;; GNU Emacs is free software; you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; any later version.
17
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
22
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs; see the file COPYING. If not, write to the
25 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
26 ;; Boston, MA 02111-1307, USA.
27
28 ;;; Commentary:
29
30 ;; This file defines a general command-interpreter-in-a-buffer package
31 ;; (comint mode). The idea is that you can build specific process-in-a-buffer
32 ;; modes on top of comint mode -- e.g., Lisp, shell, scheme, T, soar, ....
33 ;; This way, all these specific packages share a common base functionality,
34 ;; and a common set of bindings, which makes them easier to use (and
35 ;; saves code, implementation time, etc., etc.).
36
37 ;; Several packages are already defined using comint mode:
38 ;; - shell.el defines a shell-in-a-buffer mode.
39 ;; - cmulisp.el defines a simple lisp-in-a-buffer mode.
40 ;;
41 ;; - The file cmuscheme.el defines a scheme-in-a-buffer mode.
42 ;; - The file tea.el tunes scheme and inferior-scheme modes for T.
43 ;; - The file soar.el tunes Lisp and inferior-lisp modes for Soar.
44 ;; - cmutex.el defines TeX and LaTeX modes that invoke TeX, LaTeX, BibTeX,
45 ;; previewers, and printers from within Emacs.
46 ;; - background.el allows csh-like job control inside Emacs.
47 ;; It is pretty easy to make new derived modes for other processes.
48
49 ;; For documentation on the functionality provided by Comint mode, and
50 ;; the hooks available for customising it, see the comments below.
51 ;; For further information on the standard derived modes (shell,
52 ;; inferior-lisp, inferior-scheme, ...), see the relevant source files.
53
54 ;; For hints on converting existing process modes (e.g., tex-mode,
55 ;; background, dbx, gdb, kermit, prolog, telnet) to use comint-mode
56 ;; instead of shell-mode, see the notes at the end of this file.
57
58 \f
59 ;; Brief Command Documentation:
60 ;;============================================================================
61 ;; Comint Mode Commands: (common to all derived modes, like shell & cmulisp
62 ;; mode)
63 ;;
64 ;; M-p comint-previous-input Cycle backwards in input history
65 ;; M-n comint-next-input Cycle forwards
66 ;; M-r comint-previous-matching-input Previous input matching a regexp
67 ;; M-s comint-next-matching-input Next input that matches
68 ;; M-C-l comint-show-output Show last batch of process output
69 ;; RET comint-send-input
70 ;; C-d comint-delchar-or-maybe-eof Delete char unless at end of buff
71 ;; C-c C-a comint-bol-or-process-mark First time, move point to bol;
72 ;; second time, move to process-mark.
73 ;; C-c C-u comint-kill-input ^u
74 ;; C-c C-w backward-kill-word ^w
75 ;; C-c C-c comint-interrupt-subjob ^c
76 ;; C-c C-z comint-stop-subjob ^z
77 ;; C-c C-\ comint-quit-subjob ^\
78 ;; C-c C-o comint-delete-output Delete last batch of process output
79 ;; C-c C-r comint-show-output Show last batch of process output
80 ;; C-c C-l comint-dynamic-list-input-ring List input history
81 ;;
82 ;; Not bound by default in comint-mode (some are in shell mode)
83 ;; comint-run Run a program under comint-mode
84 ;; send-invisible Read a line w/o echo, and send to proc
85 ;; comint-dynamic-complete-filename Complete filename at point.
86 ;; comint-dynamic-list-filename-completions List completions in help buffer.
87 ;; comint-replace-by-expanded-filename Expand and complete filename at point;
88 ;; replace with expanded/completed name.
89 ;; comint-replace-by-expanded-history Expand history at point;
90 ;; replace with expanded name.
91 ;; comint-magic-space Expand history and add (a) space(s).
92 ;; comint-kill-subjob No mercy.
93 ;; comint-show-maximum-output Show as much output as possible.
94 ;; comint-continue-subjob Send CONT signal to buffer's process
95 ;; group. Useful if you accidentally
96 ;; suspend your process (with C-c C-z).
97 ;; comint-get-next-from-history Fetch successive input history lines
98 ;; comint-accumulate Combine lines to send them together
99 ;; as input.
100 ;; comint-goto-process-mark Move point to where process-mark is.
101 ;; comint-set-process-mark Set process-mark to point.
102
103 ;; comint-mode-hook is the Comint mode hook. Basically for your keybindings.
104
105 ;;; Code:
106
107 (require 'ring)
108 \f
109 ;; Buffer Local Variables:
110 ;;============================================================================
111 ;; Comint mode buffer local variables:
112 ;; comint-prompt-regexp string comint-bol uses to match prompt
113 ;; comint-delimiter-argument-list list For delimiters and arguments
114 ;; comint-last-input-start marker Handy if inferior always echoes
115 ;; comint-last-input-end marker For comint-delete-output command
116 ;; comint-input-ring-size integer For the input history
117 ;; comint-input-ring ring mechanism
118 ;; comint-input-ring-index number ...
119 ;; comint-save-input-ring-index number ...
120 ;; comint-input-autoexpand symbol ...
121 ;; comint-input-ignoredups boolean ...
122 ;; comint-dynamic-complete-functions hook For the completion mechanism
123 ;; comint-completion-fignore list ...
124 ;; comint-file-name-chars string ...
125 ;; comint-file-name-quote-list list ...
126 ;; comint-get-old-input function Hooks for specific
127 ;; comint-input-filter-functions hook process-in-a-buffer
128 ;; comint-output-filter-functions hook function modes.
129 ;; comint-preoutput-filter-functions hook
130 ;; comint-input-filter function ...
131 ;; comint-input-sender function ...
132 ;; comint-eol-on-send boolean ...
133 ;; comint-process-echoes boolean ...
134 ;; comint-scroll-to-bottom-on-input symbol For scroll behavior
135 ;; comint-scroll-to-bottom-on-output symbol ...
136 ;; comint-scroll-show-maximum-output boolean ...
137 ;; comint-accum-marker maker For comint-accumulate
138 ;;
139 ;; Comint mode non-buffer local variables:
140 ;; comint-completion-addsuffix boolean/cons For file name
141 ;; comint-completion-autolist boolean completion behavior
142 ;; comint-completion-recexact boolean ...
143
144 (defgroup comint nil
145 "General command interpreter in a window stuff."
146 :group 'processes)
147
148 (defgroup comint-completion nil
149 "Completion facilities in comint"
150 :group 'comint)
151
152 (defgroup comint-source nil
153 "Source finding facilities in comint"
154 :prefix "comint-"
155 :group 'comint)
156
157 (defvar comint-prompt-regexp "^"
158 "Regexp to recognise prompts in the inferior process.
159 Defaults to \"^\", the null string at BOL.
160
161 This variable is only used if the variable
162 `comint-use-prompt-regexp-instead-of-fields' is non-nil.
163
164 Good choices:
165 Canonical Lisp: \"^[^> \\n]*>+:? *\" (Lucid, franz, kcl, T, cscheme, oaklisp)
166 Lucid Common Lisp: \"^\\\\(>\\\\|\\\\(->\\\\)+\\\\) *\"
167 franz: \"^\\\\(->\\\\|<[0-9]*>:\\\\) *\"
168 kcl: \"^>+ *\"
169 shell: \"^[^#$%>\\n]*[#$%>] *\"
170 T: \"^>+ *\"
171
172 This is a good thing to set in mode hooks.")
173
174 (defvar comint-delimiter-argument-list ()
175 "List of characters to recognise as separate arguments in input.
176 Strings comprising a character in this list will separate the arguments
177 surrounding them, and also be regarded as arguments in their own right (unlike
178 whitespace). See `comint-arguments'.
179 Defaults to the empty list.
180
181 For shells, a good value is (?\\| ?& ?< ?> ?\\( ?\\) ?;).
182
183 This is a good thing to set in mode hooks.")
184
185 (defcustom comint-input-autoexpand nil
186 "*If non-nil, expand input command history references on completion.
187 This mirrors the optional behavior of tcsh (its autoexpand and histlist).
188
189 If the value is `input', then the expansion is seen on input.
190 If the value is `history', then the expansion is only when inserting
191 into the buffer's input ring. See also `comint-magic-space' and
192 `comint-dynamic-complete'.
193
194 This variable is buffer-local."
195 :type '(choice (const :tag "off" nil)
196 (const input)
197 (const history)
198 (other :tag "on" t))
199 :group 'comint)
200
201 (defface comint-highlight-input '((t (:weight bold)))
202 "Face to use to highlight user input."
203 :group 'comint)
204
205 (defface comint-highlight-prompt
206 '((((background dark)) (:foreground "cyan"))
207 (t (:foreground "dark blue")))
208 "Face to use to highlight prompts."
209 :group 'comint)
210
211 (defcustom comint-input-ignoredups nil
212 "*If non-nil, don't add input matching the last on the input ring.
213 This mirrors the optional behavior of bash.
214
215 This variable is buffer-local."
216 :type 'boolean
217 :group 'comint)
218
219 (defcustom comint-input-ring-file-name nil
220 "*If non-nil, name of the file to read/write input history.
221 See also `comint-read-input-ring' and `comint-write-input-ring'.
222
223 This variable is buffer-local, and is a good thing to set in mode hooks."
224 :type '(choice (const :tag "nil" nil)
225 file)
226 :group 'comint)
227
228 (defcustom comint-scroll-to-bottom-on-input nil
229 "*Controls whether input to interpreter causes window to scroll.
230 If nil, then do not scroll. If t or `all', scroll all windows showing buffer.
231 If `this', scroll only the selected window.
232
233 The default is nil.
234
235 See `comint-preinput-scroll-to-bottom'. This variable is buffer-local."
236 :type '(choice (const :tag "off" nil)
237 (const t)
238 (const all)
239 (const this))
240 :group 'comint)
241
242 (defcustom comint-move-point-for-output nil
243 "*Controls whether interpreter output moves point to the end of the output.
244 If nil, then output never moves point to the output.
245 (If the output occurs at point, it is inserted before point.)
246 If t or `all', move point in all windows showing the buffer.
247 If `this', move point only the selected window.
248 If `others', move point only in other windows, not in the selected window.
249
250 The default is nil.
251
252 See the variable `comint-scroll-show-maximum-output' and the function
253 `comint-postoutput-scroll-to-bottom'.
254 This variable is buffer-local in all Comint buffers."
255 :type '(choice (const :tag "off" nil)
256 (const t)
257 (const all)
258 (const this)
259 (const others))
260 :group 'comint)
261
262 (defvaralias 'comint-scroll-to-bottom-on-output 'comint-move-point-for-output)
263
264 (defcustom comint-scroll-show-maximum-output t
265 "*Controls how to scroll due to interpreter output.
266 This variable applies when point is at the end of the buffer
267 \(either because it was originally there, or because
268 `comint-move-point-for-output' said to move it there)
269 and output from the subprocess is inserted.
270
271 Non-nil means scroll so that the window is full of text
272 and point is on the last line. A value of nil
273 means don't do anything special--scroll normally.
274
275 See also the variable `comint-move-point-for-output' and the function
276 `comint-postoutput-scroll-to-bottom'.
277 This variable is buffer-local in all Comint buffers."
278 :type 'boolean
279 :group 'comint)
280
281 (defcustom comint-buffer-maximum-size 1024
282 "*The maximum size in lines for Comint buffers.
283 Comint buffers are truncated from the top to be no greater than this number, if
284 the function `comint-truncate-buffer' is on `comint-output-filter-functions'."
285 :type 'integer
286 :group 'comint)
287
288 (defvar comint-input-ring-size 32
289 "Size of input history ring.")
290
291 (defvar comint-input-ring-separator "\n"
292 "Separator between commands in the history file.")
293
294 (defvar comint-input-history-ignore "^#"
295 "Regexp for history entries that should be ignored when Comint initializes.")
296
297 (defcustom comint-process-echoes nil
298 "*If non-nil, assume that the subprocess echoes any input.
299 If so, delete one copy of the input so that only one copy eventually
300 appears in the buffer.
301
302 This variable is buffer-local."
303 :type 'boolean
304 :group 'comint)
305
306 ;; AIX puts the name of the person being su'd to in front of the prompt.
307 ;; kinit prints a prompt like `Password for devnull@GNU.ORG: '.
308 ;; ksu prints a prompt like `Kerberos password for devnull/root@GNU.ORG: '.
309 ;; ssh-add prints a prompt like `Enter passphrase: '.
310 ;; Some implementations of passwd use "Password (again)" as the 2nd prompt.
311 (defcustom comint-password-prompt-regexp
312 "\\(\\([Oo]ld \\|[Nn]ew \\|'s \\|login \\|\
313 Kerberos \\|CVS \\|UNIX \\| SMB \\|^\\)\
314 \[Pp]assword\\( (again)\\)?\\|\
315 pass phrase\\|\\(Enter\\|Repeat\\) passphrase\\)\
316 \\( for [^:]+\\)?:\\s *\\'"
317 "*Regexp matching prompts for passwords in the inferior process.
318 This is used by `comint-watch-for-password-prompt'."
319 :type 'regexp
320 :group 'comint)
321
322 ;; Here are the per-interpreter hooks.
323 (defvar comint-get-old-input (function comint-get-old-input-default)
324 "Function that returns old text in Comint mode.
325 This function is called when return is typed while the point is in old
326 text. It returns the text to be submitted as process input. The
327 default is `comint-get-old-input-default', which either grabs the
328 current input field or grabs the current line and strips off leading
329 text matching `comint-prompt-regexp', depending on the value of
330 `comint-use-prompt-regexp-instead-of-fields'.")
331
332 (defvar comint-dynamic-complete-functions
333 '(comint-replace-by-expanded-history comint-dynamic-complete-filename)
334 "List of functions called to perform completion.
335 Functions should return non-nil if completion was performed.
336 See also `comint-dynamic-complete'.
337
338 This is a good thing to set in mode hooks.")
339
340 (defvar comint-input-filter
341 (function (lambda (str) (not (string-match "\\`\\s *\\'" str))))
342 "Predicate for filtering additions to input history.
343 Takes one argument, the input. If non-nil, the input may be saved on the input
344 history list. Default is to save anything that isn't all whitespace.")
345
346 (defvar comint-input-filter-functions '()
347 "Functions to call before input is sent to the process.
348 These functions get one argument, a string containing the text to send.
349
350 You can use `add-hook' to add functions to this list
351 either globally or locally.")
352
353 (defvar comint-output-filter-functions '(comint-postoutput-scroll-to-bottom)
354 "Functions to call after output is inserted into the buffer.
355 One possible function is `comint-postoutput-scroll-to-bottom'.
356 These functions get one argument, a string containing the text as originally
357 inserted. Note that this might not be the same as the buffer contents between
358 `comint-last-output-start' and the buffer's `process-mark', if other filter
359 functions have already modified the buffer.
360
361 See also `comint-preoutput-filter-functions'.
362
363 You can use `add-hook' to add functions to this list
364 either globally or locally.")
365
366 (defvar comint-input-sender-no-newline nil
367 "Non-nil directs the `comint-input-sender' function not to send a newline.")
368
369 (defvar comint-input-sender (function comint-simple-send)
370 "Function to actually send to PROCESS the STRING submitted by user.
371 Usually this is just `comint-simple-send', but if your mode needs to
372 massage the input string, put a different function here.
373 `comint-simple-send' just sends the string plus a newline.
374 \(If `comint-input-sender-no-newline' is non-nil, it omits the newline.)
375 This is called from the user command `comint-send-input'.")
376
377 (defcustom comint-eol-on-send t
378 "*Non-nil means go to the end of the line before sending input.
379 See `comint-send-input'."
380 :type 'boolean
381 :group 'comint)
382
383 ;; Note: If it is decided to purge comint-prompt-regexp from the source
384 ;; entirely, searching for uses of this variable will help to identify
385 ;; places that need attention.
386 (defcustom comint-use-prompt-regexp-instead-of-fields nil
387 "*If non-nil, use `comint-prompt-regexp' to distinguish prompts from user-input.
388 If nil, then program output and user-input are given different `field'
389 properties, which emacs commands can use to distinguish them (in
390 particular, common movement commands such as begining-of-line respect
391 field boundaries in a natural way)."
392 :type 'boolean
393 :group 'comint)
394
395 (defcustom comint-mode-hook '(turn-on-font-lock)
396 "Hook run upon entry to `comint-mode'.
397 This is run before the process is cranked up."
398 :type 'hook
399 :group 'comint)
400
401 (defcustom comint-exec-hook '()
402 "Hook run each time a process is exec'd by `comint-exec'.
403 This is called after the process is cranked up. It is useful for things that
404 must be done each time a process is executed in a Comint mode buffer (e.g.,
405 `(process-kill-without-query)'). In contrast, the `comint-mode-hook' is only
406 executed once when the buffer is created."
407 :type 'hook
408 :group 'comint)
409
410 (defvar comint-mode-map nil)
411
412 ;; Fixme: Is this still relevant?
413 (defvar comint-ptyp t
414 "Non-nil if communications via pty; false if by pipe. Buffer local.
415 This is to work around a bug in Emacs process signaling.")
416
417 (defvar comint-input-ring nil)
418 (defvar comint-last-input-start nil)
419 (defvar comint-last-input-end nil)
420 (defvar comint-last-output-start nil)
421 (defvar comint-input-ring-index nil
422 "Index of last matched history element.")
423 (defvar comint-matching-input-from-input-string ""
424 "Input previously used to match input history.")
425 (defvar comint-save-input-ring-index
426 "Last input ring index which you copied.
427 This is to support the command \\[comint-get-next-from-history].")
428
429 (defvar comint-accum-marker nil
430 "Non-nil if you are accumulating input lines to send as input together.
431 The command \\[comint-accumulate] sets this.")
432
433 (put 'comint-replace-by-expanded-history 'menu-enable 'comint-input-autoexpand)
434 (put 'comint-input-ring 'permanent-local t)
435 (put 'comint-input-ring-index 'permanent-local t)
436 (put 'comint-save-input-ring-index 'permanent-local t)
437 (put 'comint-input-autoexpand 'permanent-local t)
438 (put 'comint-input-filter-functions 'permanent-local t)
439 (put 'comint-output-filter-functions 'permanent-local t)
440 (put 'comint-preoutput-filter-functions 'permanent-local t)
441 (put 'comint-scroll-to-bottom-on-input 'permanent-local t)
442 (put 'comint-move-point-for-output 'permanent-local t)
443 (put 'comint-scroll-show-maximum-output 'permanent-local t)
444 (put 'comint-ptyp 'permanent-local t)
445
446 (put 'comint-mode 'mode-class 'special)
447
448 (define-derived-mode comint-mode fundamental-mode "Comint"
449 "Major mode for interacting with an inferior interpreter.
450 Interpreter name is same as buffer name, sans the asterisks.
451 Return at end of buffer sends line as input.
452 Return not at end copies rest of line to end and sends it.
453 Setting variable `comint-eol-on-send' means jump to the end of the line
454 before submitting new input.
455
456 This mode is customised to create major modes such as Inferior Lisp
457 mode, Shell mode, etc. This can be done by setting the hooks
458 `comint-input-filter-functions', `comint-input-filter', `comint-input-sender'
459 and `comint-get-old-input' to appropriate functions, and the variable
460 `comint-prompt-regexp' to the appropriate regular expression.
461
462 An input history is maintained of size `comint-input-ring-size', and
463 can be accessed with the commands \\[comint-next-input], \\[comint-previous-input], and \\[comint-dynamic-list-input-ring].
464 Input ring history expansion can be achieved with the commands
465 \\[comint-replace-by-expanded-history] or \\[comint-magic-space].
466 Input ring expansion is controlled by the variable `comint-input-autoexpand',
467 and addition is controlled by the variable `comint-input-ignoredups'.
468
469 Commands with no default key bindings include `send-invisible',
470 `comint-dynamic-complete', `comint-dynamic-list-filename-completions', and
471 `comint-magic-space'.
472
473 Input to, and output from, the subprocess can cause the window to scroll to
474 the end of the buffer. See variables `comint-output-filter-functions',
475 `comint-preoutput-filter-functions', `comint-scroll-to-bottom-on-input',
476 and `comint-move-point-for-output'.
477
478 If you accidentally suspend your process, use \\[comint-continue-subjob]
479 to continue it.
480
481 \\{comint-mode-map}
482
483 Entry to this mode runs the hooks on `comint-mode-hook'."
484 (setq mode-line-process '(":%s"))
485 (set (make-local-variable 'comint-last-input-start) (point-min-marker))
486 (set (make-local-variable 'comint-last-input-end) (point-min-marker))
487 (set (make-local-variable 'comint-last-output-start) (make-marker))
488 (make-local-variable 'comint-last-prompt-overlay)
489 (make-local-variable 'comint-prompt-regexp) ; Don't set; default
490 (make-local-variable 'comint-input-ring-size) ; ...to global val.
491 (make-local-variable 'comint-input-ring)
492 (make-local-variable 'comint-input-ring-file-name)
493 (or (and (boundp 'comint-input-ring) comint-input-ring)
494 (setq comint-input-ring (make-ring comint-input-ring-size)))
495 (make-local-variable 'comint-input-ring-index)
496 (make-local-variable 'comint-save-input-ring-index)
497 (or (and (boundp 'comint-input-ring-index) comint-input-ring-index)
498 (setq comint-input-ring-index nil))
499 (or (and (boundp 'comint-save-input-ring-index) comint-save-input-ring-index)
500 (setq comint-save-input-ring-index nil))
501 (make-local-variable 'comint-matching-input-from-input-string)
502 (make-local-variable 'comint-input-autoexpand)
503 (make-local-variable 'comint-input-ignoredups)
504 (make-local-variable 'comint-delimiter-argument-list)
505 (make-local-variable 'comint-completion-fignore)
506 (make-local-variable 'comint-get-old-input)
507 (make-local-variable 'comint-input-filter)
508 (make-local-variable 'comint-input-sender)
509 (make-local-variable 'comint-eol-on-send)
510 (make-local-variable 'comint-scroll-to-bottom-on-input)
511 (make-local-variable 'comint-move-point-for-output)
512 (make-local-variable 'comint-scroll-show-maximum-output)
513 ;; This makes it really work to keep point at the bottom.
514 (make-local-variable 'scroll-conservatively)
515 (setq scroll-conservatively 10000)
516 (add-hook 'pre-command-hook 'comint-preinput-scroll-to-bottom t t)
517 (make-local-variable 'comint-ptyp)
518 (make-local-variable 'comint-process-echoes)
519 (make-local-variable 'comint-file-name-chars)
520 (make-local-variable 'comint-file-name-quote-list)
521 (set (make-local-variable 'comint-accum-marker) (make-marker))
522 (add-hook 'change-major-mode-hook 'font-lock-defontify nil t)
523 ;; This behavior is not useful in comint buffers, and is annoying
524 (set (make-local-variable 'next-line-add-newlines) nil))
525
526 (if comint-mode-map
527 nil
528 ;; Keys:
529 (setq comint-mode-map (make-sparse-keymap))
530 (define-key comint-mode-map "\ep" 'comint-previous-input)
531 (define-key comint-mode-map "\en" 'comint-next-input)
532 (define-key comint-mode-map [C-up] 'comint-previous-input)
533 (define-key comint-mode-map [C-down] 'comint-next-input)
534 (define-key comint-mode-map "\er" 'comint-previous-matching-input)
535 (define-key comint-mode-map "\es" 'comint-next-matching-input)
536 (define-key comint-mode-map [?\C-c ?\M-r] 'comint-previous-matching-input-from-input)
537 (define-key comint-mode-map [?\C-c ?\M-s] 'comint-next-matching-input-from-input)
538 (define-key comint-mode-map "\e\C-l" 'comint-show-output)
539 (define-key comint-mode-map "\C-m" 'comint-send-input)
540 (define-key comint-mode-map "\C-d" 'comint-delchar-or-maybe-eof)
541 (define-key comint-mode-map "\C-c " 'comint-accumulate)
542 (define-key comint-mode-map "\C-c\C-x" 'comint-get-next-from-history)
543 (define-key comint-mode-map "\C-c\C-a" 'comint-bol-or-process-mark)
544 (define-key comint-mode-map "\C-c\C-u" 'comint-kill-input)
545 (define-key comint-mode-map "\C-c\C-w" 'backward-kill-word)
546 (define-key comint-mode-map "\C-c\C-c" 'comint-interrupt-subjob)
547 (define-key comint-mode-map "\C-c\C-z" 'comint-stop-subjob)
548 (define-key comint-mode-map "\C-c\C-\\" 'comint-quit-subjob)
549 (define-key comint-mode-map "\C-c\C-m" 'comint-copy-old-input)
550 (define-key comint-mode-map "\C-c\C-o" 'comint-delete-output)
551 (define-key comint-mode-map "\C-c\C-r" 'comint-show-output)
552 (define-key comint-mode-map "\C-c\C-e" 'comint-show-maximum-output)
553 (define-key comint-mode-map "\C-c\C-l" 'comint-dynamic-list-input-ring)
554 (define-key comint-mode-map "\C-c\C-n" 'comint-next-prompt)
555 (define-key comint-mode-map "\C-c\C-p" 'comint-previous-prompt)
556 (define-key comint-mode-map "\C-c\C-d" 'comint-send-eof)
557 (define-key comint-mode-map "\C-c\C-s" 'comint-write-output)
558 (define-key comint-mode-map "\C-c." 'comint-insert-previous-argument)
559 ;; Mouse Buttons:
560 (define-key comint-mode-map [mouse-2] 'comint-insert-clicked-input)
561 ;; Menu bars:
562 ;; completion:
563 (define-key comint-mode-map [menu-bar completion]
564 (cons "Complete" (make-sparse-keymap "Complete")))
565 (define-key comint-mode-map [menu-bar completion complete-expand]
566 '("Expand File Name" . comint-replace-by-expanded-filename))
567 (define-key comint-mode-map [menu-bar completion complete-listing]
568 '("File Completion Listing" . comint-dynamic-list-filename-completions))
569 (define-key comint-mode-map [menu-bar completion complete-file]
570 '("Complete File Name" . comint-dynamic-complete-filename))
571 (define-key comint-mode-map [menu-bar completion complete]
572 '("Complete Before Point" . comint-dynamic-complete))
573 ;; Input history:
574 (define-key comint-mode-map [menu-bar inout]
575 (cons "In/Out" (make-sparse-keymap "In/Out")))
576 (define-key comint-mode-map [menu-bar inout delete-output]
577 '("Delete Current Output Group" . comint-delete-output))
578 (define-key comint-mode-map [menu-bar inout append-output-to-file]
579 '("Append Current Output Group to File" . comint-append-output-to-file))
580 (define-key comint-mode-map [menu-bar inout write-output]
581 '("Write Current Output Group to File" . comint-write-output))
582 (define-key comint-mode-map [menu-bar inout next-prompt]
583 '("Forward Output Group" . comint-next-prompt))
584 (define-key comint-mode-map [menu-bar inout previous-prompt]
585 '("Backward Output Group" . comint-previous-prompt))
586 (define-key comint-mode-map [menu-bar inout show-maximum-output]
587 '("Show Maximum Output" . comint-show-maximum-output))
588 (define-key comint-mode-map [menu-bar inout show-output]
589 '("Show Current Output Group" . comint-show-output))
590 (define-key comint-mode-map [menu-bar inout kill-input]
591 '("Kill Current Input" . comint-kill-input))
592 (define-key comint-mode-map [menu-bar inout copy-input]
593 '("Copy Old Input" . comint-copy-old-input))
594 (define-key comint-mode-map [menu-bar inout forward-matching-history]
595 '("Forward Matching Input..." . comint-forward-matching-input))
596 (define-key comint-mode-map [menu-bar inout backward-matching-history]
597 '("Backward Matching Input..." . comint-backward-matching-input))
598 (define-key comint-mode-map [menu-bar inout next-matching-history]
599 '("Next Matching Input..." . comint-next-matching-input))
600 (define-key comint-mode-map [menu-bar inout previous-matching-history]
601 '("Previous Matching Input..." . comint-previous-matching-input))
602 (define-key comint-mode-map [menu-bar inout next-matching-history-from-input]
603 '("Next Matching Current Input" . comint-next-matching-input-from-input))
604 (define-key comint-mode-map [menu-bar inout previous-matching-history-from-input]
605 '("Previous Matching Current Input" . comint-previous-matching-input-from-input))
606 (define-key comint-mode-map [menu-bar inout next-history]
607 '("Next Input" . comint-next-input))
608 (define-key comint-mode-map [menu-bar inout previous-history]
609 '("Previous Input" . comint-previous-input))
610 (define-key comint-mode-map [menu-bar inout list-history]
611 '("List Input History" . comint-dynamic-list-input-ring))
612 (define-key comint-mode-map [menu-bar inout expand-history]
613 '("Expand History Before Point" . comint-replace-by-expanded-history))
614 ;; Signals
615 (define-key comint-mode-map [menu-bar signals]
616 (cons "Signals" (make-sparse-keymap "Signals")))
617 (define-key comint-mode-map [menu-bar signals eof]
618 '("EOF" . comint-send-eof))
619 (define-key comint-mode-map [menu-bar signals kill]
620 '("KILL" . comint-kill-subjob))
621 (define-key comint-mode-map [menu-bar signals quit]
622 '("QUIT" . comint-quit-subjob))
623 (define-key comint-mode-map [menu-bar signals cont]
624 '("CONT" . comint-continue-subjob))
625 (define-key comint-mode-map [menu-bar signals stop]
626 '("STOP" . comint-stop-subjob))
627 (define-key comint-mode-map [menu-bar signals break]
628 '("BREAK" . comint-interrupt-subjob))
629 ;; Put them in the menu bar:
630 (setq menu-bar-final-items (append '(completion inout signals)
631 menu-bar-final-items))
632 )
633
634 (defun comint-check-proc (buffer)
635 "Return t if there is a living process associated w/buffer BUFFER.
636 Living means the status is `open', `run', or `stop'.
637 BUFFER can be either a buffer or the name of one."
638 (let ((proc (get-buffer-process buffer)))
639 (and proc (memq (process-status proc) '(open run stop)))))
640
641 ;;;###autoload
642 (defun make-comint-in-buffer (name buffer program &optional startfile &rest switches)
643 "Make a Comint process NAME in BUFFER, running PROGRAM.
644 If BUFFER is nil, it defaults to NAME surrounded by `*'s.
645 PROGRAM should be either a string denoting an executable program to create
646 via `start-process', or a cons pair of the form (HOST . SERVICE) denoting a TCP
647 connection to be opened via `open-network-stream'. If there is already a
648 running process in that buffer, it is not restarted. Optional third arg
649 STARTFILE is the name of a file to send the contents of to the process.
650
651 If PROGRAM is a string, any more args are arguments to PROGRAM."
652 (or (fboundp 'start-process)
653 (error "Multi-processing is not supported for this system"))
654 (setq buffer (get-buffer-create (or buffer (concat "*" name "*"))))
655 ;; If no process, or nuked process, crank up a new one and put buffer in
656 ;; comint mode. Otherwise, leave buffer and existing process alone.
657 (unless (comint-check-proc buffer)
658 (with-current-buffer buffer
659 (unless (derived-mode-p 'comint-mode)
660 (comint-mode))) ; Install local vars, mode, keymap, ...
661 (comint-exec buffer name program startfile switches))
662 buffer)
663
664 ;;;###autoload
665 (defun make-comint (name program &optional startfile &rest switches)
666 "Make a Comint process NAME in a buffer, running PROGRAM.
667 The name of the buffer is made by surrounding NAME with `*'s.
668 PROGRAM should be either a string denoting an executable program to create
669 via `start-process', or a cons pair of the form (HOST . SERVICE) denoting a TCP
670 connection to be opened via `open-network-stream'. If there is already a
671 running process in that buffer, it is not restarted. Optional third arg
672 STARTFILE is the name of a file to send the contents of the process to.
673
674 If PROGRAM is a string, any more args are arguments to PROGRAM."
675 (apply #'make-comint-in-buffer name nil program startfile switches))
676
677 ;;;###autoload
678 (defun comint-run (program)
679 "Run PROGRAM in a Comint buffer and switch to it.
680 The buffer name is made by surrounding the file name of PROGRAM with `*'s.
681 The file name is used to make a symbol name, such as `comint-sh-hook', and any
682 hooks on this symbol are run in the buffer.
683 See `make-comint' and `comint-exec'."
684 (interactive "sRun program: ")
685 (let ((name (file-name-nondirectory program)))
686 (switch-to-buffer (make-comint name program))
687 (run-hooks (intern-soft (concat "comint-" name "-hook")))))
688
689 (defun comint-exec (buffer name command startfile switches)
690 "Start up a process named NAME in buffer BUFFER for Comint modes.
691 Runs the given COMMAND with SWITCHES with output to STARTFILE.
692 Blasts any old process running in the buffer. Doesn't set the buffer mode.
693 You can use this to cheaply run a series of processes in the same Comint
694 buffer. The hook `comint-exec-hook' is run after each exec."
695 (with-current-buffer buffer
696 (let ((proc (get-buffer-process buffer))) ; Blast any old process.
697 (if proc (delete-process proc)))
698 ;; Crank up a new process
699 (let ((proc
700 (if (consp command)
701 (open-network-stream name buffer (car command) (cdr command))
702 (comint-exec-1 name buffer command switches))))
703 (set-process-filter proc 'comint-output-filter)
704 (make-local-variable 'comint-ptyp)
705 (setq comint-ptyp process-connection-type) ; t if pty, nil if pipe.
706 ;; Jump to the end, and set the process mark.
707 (goto-char (point-max))
708 (set-marker (process-mark proc) (point))
709 ;; Feed it the startfile.
710 (cond (startfile
711 ;;This is guaranteed to wait long enough
712 ;;but has bad results if the comint does not prompt at all
713 ;; (while (= size (buffer-size))
714 ;; (sleep-for 1))
715 ;;I hope 1 second is enough!
716 (sleep-for 1)
717 (goto-char (point-max))
718 (insert-file-contents startfile)
719 (setq startfile (buffer-substring (point) (point-max)))
720 (delete-region (point) (point-max))
721 (comint-send-string proc startfile)))
722 (run-hooks 'comint-exec-hook)
723 buffer)))
724
725 ;; This auxiliary function cranks up the process for comint-exec in
726 ;; the appropriate environment.
727
728 (defun comint-exec-1 (name buffer command switches)
729 (let ((process-environment
730 (nconc
731 ;; If using termcap, we specify `emacs' as the terminal type
732 ;; because that lets us specify a width.
733 ;; If using terminfo, we specify `dumb' because that is
734 ;; a defined terminal type. `emacs' is not a defined terminal type
735 ;; and there is no way for us to define it here.
736 ;; Some programs that use terminfo get very confused
737 ;; if TERM is not a valid terminal type.
738 ;; ;; There is similar code in compile.el.
739 (if (and (boundp 'system-uses-terminfo) system-uses-terminfo)
740 (list "TERM=dumb" "TERMCAP="
741 (format "COLUMNS=%d" (window-width)))
742 (list "TERM=emacs"
743 (format "TERMCAP=emacs:co#%d:tc=unknown:" (window-width))))
744 (if (getenv "EMACS") nil (list "EMACS=t"))
745 process-environment))
746 (default-directory
747 (if (file-accessible-directory-p default-directory)
748 default-directory
749 "/"))
750 proc decoding encoding changed)
751 (let ((exec-path (if (file-name-directory command)
752 ;; If the command has slashes, make sure we
753 ;; first look relative to the current directory.
754 (cons default-directory exec-path) exec-path)))
755 (setq proc (apply 'start-process name buffer command switches)))
756 (let ((coding-systems (process-coding-system proc)))
757 (setq decoding (car coding-systems)
758 encoding (cdr coding-systems)))
759 ;; If start-process decided to use some coding system for decoding
760 ;; data sent from the process and the coding system doesn't
761 ;; specify EOL conversion, we had better convert CRLF to LF.
762 (if (vectorp (coding-system-eol-type decoding))
763 (setq decoding (coding-system-change-eol-conversion decoding 'dos)
764 changed t))
765 ;; Even if start-process left the coding system for encoding data
766 ;; sent from the process undecided, we had better use the same one
767 ;; as what we use for decoding. But, we should suppress EOL
768 ;; conversion.
769 (if (and decoding (not encoding))
770 (setq encoding (coding-system-change-eol-conversion decoding 'unix)
771 changed t))
772 (if changed
773 (set-process-coding-system proc decoding encoding))
774 proc))
775
776
777 (defun comint-insert-clicked-input (event)
778 "In a Comint buffer, set the current input to the clicked-on previous input."
779 (interactive "e")
780 (let ((pos (posn-point (event-end event))))
781 (if (not (eq (get-char-property pos 'field) 'input))
782 ;; No input at POS, fall back to the global definition.
783 (let* ((keys (this-command-keys))
784 (last-key (and (vectorp keys) (aref keys (1- (length keys)))))
785 (fun (and last-key (lookup-key global-map (vector last-key)))))
786 (and fun (call-interactively fun)))
787 ;; There's previous input at POS, insert it at the end of the buffer.
788 (goto-char (point-max))
789 ;; First delete any old unsent input at the end
790 (delete-region
791 (or (marker-position comint-accum-marker)
792 (process-mark (get-buffer-process (current-buffer))))
793 (point))
794 ;; Insert the clicked-upon input
795 (insert (buffer-substring-no-properties
796 (previous-single-char-property-change (1+ pos) 'field)
797 (next-single-char-property-change pos 'field))))))
798
799
800 \f
801 ;; Input history processing in a buffer
802 ;; ===========================================================================
803 ;; Useful input history functions, courtesy of the Ergo group.
804
805 ;; Eleven commands:
806 ;; comint-dynamic-list-input-ring List history in help buffer.
807 ;; comint-previous-input Previous input...
808 ;; comint-previous-matching-input ...matching a string.
809 ;; comint-previous-matching-input-from-input ... matching the current input.
810 ;; comint-next-input Next input...
811 ;; comint-next-matching-input ...matching a string.
812 ;; comint-next-matching-input-from-input ... matching the current input.
813 ;; comint-backward-matching-input Backwards input...
814 ;; comint-forward-matching-input ...matching a string.
815 ;; comint-replace-by-expanded-history Expand history at point;
816 ;; replace with expanded history.
817 ;; comint-magic-space Expand history and insert space.
818 ;;
819 ;; Three functions:
820 ;; comint-read-input-ring Read into comint-input-ring...
821 ;; comint-write-input-ring Write to comint-input-ring-file-name.
822 ;; comint-replace-by-expanded-history-before-point Workhorse function.
823
824 (defun comint-read-input-ring (&optional silent)
825 "Set the buffer's `comint-input-ring' from a history file.
826 The name of the file is given by the variable `comint-input-ring-file-name'.
827 The history ring is of size `comint-input-ring-size', regardless of file size.
828 If `comint-input-ring-file-name' is nil this function does nothing.
829
830 If the optional argument SILENT is non-nil, we say nothing about a
831 failure to read the history file.
832
833 This function is useful for major mode commands and mode hooks.
834
835 The commands stored in the history file are separated by the
836 `comint-input-ring-separator', and entries that match
837 `comint-input-history-ignore' are ignored. The most recent command
838 comes last.
839
840 See also `comint-input-ignoredups' and `comint-write-input-ring'."
841 (cond ((or (null comint-input-ring-file-name)
842 (equal comint-input-ring-file-name ""))
843 nil)
844 ((not (file-readable-p comint-input-ring-file-name))
845 (or silent
846 (message "Cannot read history file %s"
847 comint-input-ring-file-name)))
848 (t
849 (let* ((history-buf (get-buffer-create " *temp*"))
850 (file comint-input-ring-file-name)
851 (count 0)
852 (size comint-input-ring-size)
853 (ring (make-ring size)))
854 (unwind-protect
855 (save-excursion
856 (set-buffer history-buf)
857 (widen)
858 (erase-buffer)
859 (insert-file-contents file)
860 ;; Save restriction in case file is already visited...
861 ;; Watch for those date stamps in history files!
862 (goto-char (point-max))
863 (let (start end history)
864 (while (and (< count comint-input-ring-size)
865 (re-search-backward comint-input-ring-separator nil t)
866 (setq end (match-beginning 0)))
867 (if (re-search-backward comint-input-ring-separator nil t)
868 (setq start (match-end 0))
869 (setq start (point-min)))
870 (setq history (buffer-substring start end))
871 (goto-char start)
872 (if (and (not (string-match comint-input-history-ignore history))
873 (or (null comint-input-ignoredups)
874 (ring-empty-p ring)
875 (not (string-equal (ring-ref ring 0) history))))
876 (progn
877 (ring-insert-at-beginning ring history)
878 (setq count (1+ count)))))))
879 (kill-buffer history-buf))
880 (setq comint-input-ring ring
881 comint-input-ring-index nil)))))
882
883 (defun comint-write-input-ring ()
884 "Writes the buffer's `comint-input-ring' to a history file.
885 The name of the file is given by the variable `comint-input-ring-file-name'.
886 The original contents of the file are lost if `comint-input-ring' is not empty.
887 If `comint-input-ring-file-name' is nil this function does nothing.
888
889 Useful within process sentinels.
890
891 See also `comint-read-input-ring'."
892 (cond ((or (null comint-input-ring-file-name)
893 (equal comint-input-ring-file-name "")
894 (null comint-input-ring) (ring-empty-p comint-input-ring))
895 nil)
896 ((not (file-writable-p comint-input-ring-file-name))
897 (message "Cannot write history file %s" comint-input-ring-file-name))
898 (t
899 (let* ((history-buf (get-buffer-create " *Temp Input History*"))
900 (ring comint-input-ring)
901 (file comint-input-ring-file-name)
902 (index (ring-length ring)))
903 ;; Write it all out into a buffer first. Much faster, but messier,
904 ;; than writing it one line at a time.
905 (save-excursion
906 (set-buffer history-buf)
907 (erase-buffer)
908 (while (> index 0)
909 (setq index (1- index))
910 (insert (ring-ref ring index) comint-input-ring-separator))
911 (write-region (buffer-string) nil file nil 'no-message)
912 (kill-buffer nil))))))
913
914
915 (defvar comint-dynamic-list-input-ring-window-conf)
916
917 (defun comint-dynamic-list-input-ring-select ()
918 "Choose the input history entry that point is in or next to."
919 (interactive)
920 (let (beg end completion (buffer completion-reference-buffer)
921 (base-size completion-base-size))
922 (if (and (not (eobp)) (get-text-property (point) 'mouse-face))
923 (setq end (point) beg (1+ (point))))
924 (if (and (not (bobp)) (get-text-property (1- (point)) 'mouse-face))
925 (setq end (1- (point)) beg (point)))
926 (if (null beg)
927 (error "No history entry here"))
928 (setq beg (previous-single-property-change beg 'mouse-face))
929 (setq end (or (next-single-property-change end 'mouse-face) (point-max)))
930 (setq completion (buffer-substring beg end))
931 (set-window-configuration comint-dynamic-list-input-ring-window-conf)
932 (choose-completion-string completion buffer base-size)))
933
934 (defun comint-dynamic-list-input-ring ()
935 "List in help buffer the buffer's input history."
936 (interactive)
937 (if (or (not (ring-p comint-input-ring))
938 (ring-empty-p comint-input-ring))
939 (message "No history")
940 (let ((history nil)
941 (history-buffer " *Input History*")
942 (index (1- (ring-length comint-input-ring)))
943 (conf (current-window-configuration)))
944 ;; We have to build up a list ourselves from the ring vector.
945 (while (>= index 0)
946 (setq history (cons (ring-ref comint-input-ring index) history)
947 index (1- index)))
948 ;; Change "completion" to "history reference"
949 ;; to make the display accurate.
950 (with-output-to-temp-buffer history-buffer
951 (display-completion-list history)
952 (set-buffer history-buffer)
953 (let ((keymap (make-sparse-keymap)))
954 (set-keymap-parent keymap (current-local-map))
955 (define-key keymap "\C-m" 'comint-dynamic-list-input-ring-select)
956 (use-local-map keymap))
957 (forward-line 3)
958 (while (search-backward "completion" nil 'move)
959 (replace-match "history reference")))
960 (sit-for 0)
961 (message "Hit space to flush")
962 (setq comint-dynamic-list-input-ring-window-conf conf)
963 (let ((ch (read-event)))
964 (if (eq ch ?\ )
965 (set-window-configuration conf)
966 (setq unread-command-events (list ch)))))))
967
968
969 (defun comint-regexp-arg (prompt)
970 "Return list of regexp and prefix arg using PROMPT."
971 (let* (;; Don't clobber this.
972 (last-command last-command)
973 (regexp (read-from-minibuffer prompt nil nil nil
974 'minibuffer-history-search-history)))
975 (list (if (string-equal regexp "")
976 (setcar minibuffer-history-search-history
977 (nth 1 minibuffer-history-search-history))
978 regexp)
979 (prefix-numeric-value current-prefix-arg))))
980
981 (defun comint-search-arg (arg)
982 ;; First make sure there is a ring and that we are after the process mark
983 (cond ((not (comint-after-pmark-p))
984 (error "Not at command line"))
985 ((or (null comint-input-ring)
986 (ring-empty-p comint-input-ring))
987 (error "Empty input ring"))
988 ((zerop arg)
989 ;; arg of zero resets search from beginning, and uses arg of 1
990 (setq comint-input-ring-index nil)
991 1)
992 (t
993 arg)))
994
995 (defun comint-search-start (arg)
996 "Index to start a directional search, starting at `comint-input-ring-index'."
997 (if comint-input-ring-index
998 ;; If a search is running, offset by 1 in direction of arg
999 (mod (+ comint-input-ring-index (if (> arg 0) 1 -1))
1000 (ring-length comint-input-ring))
1001 ;; For a new search, start from beginning or end, as appropriate
1002 (if (>= arg 0)
1003 0 ; First elt for forward search
1004 (1- (ring-length comint-input-ring))))) ; Last elt for backward search
1005
1006 (defun comint-previous-input-string (arg)
1007 "Return the string ARG places along the input ring.
1008 Moves relative to `comint-input-ring-index'."
1009 (ring-ref comint-input-ring (if comint-input-ring-index
1010 (mod (+ arg comint-input-ring-index)
1011 (ring-length comint-input-ring))
1012 arg)))
1013
1014 (defun comint-previous-input (arg)
1015 "Cycle backwards through input history."
1016 (interactive "*p")
1017 (comint-previous-matching-input "." arg))
1018
1019 (defun comint-next-input (arg)
1020 "Cycle forwards through input history."
1021 (interactive "*p")
1022 (comint-previous-input (- arg)))
1023
1024 (defun comint-previous-matching-input-string (regexp arg)
1025 "Return the string matching REGEXP ARG places along the input ring.
1026 Moves relative to `comint-input-ring-index'."
1027 (let* ((pos (comint-previous-matching-input-string-position regexp arg)))
1028 (if pos (ring-ref comint-input-ring pos))))
1029
1030 (defun comint-previous-matching-input-string-position (regexp arg &optional start)
1031 "Return the index matching REGEXP ARG places along the input ring.
1032 Moves relative to START, or `comint-input-ring-index'."
1033 (if (or (not (ring-p comint-input-ring))
1034 (ring-empty-p comint-input-ring))
1035 (error "No history"))
1036 (let* ((len (ring-length comint-input-ring))
1037 (motion (if (> arg 0) 1 -1))
1038 (n (mod (- (or start (comint-search-start arg)) motion) len))
1039 (tried-each-ring-item nil)
1040 (prev nil))
1041 ;; Do the whole search as many times as the argument says.
1042 (while (and (/= arg 0) (not tried-each-ring-item))
1043 ;; Step once.
1044 (setq prev n
1045 n (mod (+ n motion) len))
1046 ;; If we haven't reached a match, step some more.
1047 (while (and (< n len) (not tried-each-ring-item)
1048 (not (string-match regexp (ring-ref comint-input-ring n))))
1049 (setq n (mod (+ n motion) len)
1050 ;; If we have gone all the way around in this search.
1051 tried-each-ring-item (= n prev)))
1052 (setq arg (if (> arg 0) (1- arg) (1+ arg))))
1053 ;; Now that we know which ring element to use, if we found it, return that.
1054 (if (string-match regexp (ring-ref comint-input-ring n))
1055 n)))
1056
1057 (defun comint-previous-matching-input (regexp arg)
1058 "Search backwards through input history for match for REGEXP.
1059 \(Previous history elements are earlier commands.)
1060 With prefix argument N, search for Nth previous match.
1061 If N is negative, find the next or Nth next match."
1062 (interactive (comint-regexp-arg "Previous input matching (regexp): "))
1063 (setq arg (comint-search-arg arg))
1064 (let ((pos (comint-previous-matching-input-string-position regexp arg)))
1065 ;; Has a match been found?
1066 (if (null pos)
1067 (error "Not found")
1068 (setq comint-input-ring-index pos)
1069 (message "History item: %d" (1+ pos))
1070 (delete-region
1071 ;; Can't use kill-region as it sets this-command
1072 (or (marker-position comint-accum-marker)
1073 (process-mark (get-buffer-process (current-buffer))))
1074 (point))
1075 (insert (ring-ref comint-input-ring pos)))))
1076
1077 (defun comint-next-matching-input (regexp arg)
1078 "Search forwards through input history for match for REGEXP.
1079 \(Later history elements are more recent commands.)
1080 With prefix argument N, search for Nth following match.
1081 If N is negative, find the previous or Nth previous match."
1082 (interactive (comint-regexp-arg "Next input matching (regexp): "))
1083 (comint-previous-matching-input regexp (- arg)))
1084
1085 (defun comint-previous-matching-input-from-input (arg)
1086 "Search backwards through input history for match for current input.
1087 \(Previous history elements are earlier commands.)
1088 With prefix argument N, search for Nth previous match.
1089 If N is negative, search forwards for the -Nth following match."
1090 (interactive "p")
1091 (if (not (memq last-command '(comint-previous-matching-input-from-input
1092 comint-next-matching-input-from-input)))
1093 ;; Starting a new search
1094 (setq comint-matching-input-from-input-string
1095 (buffer-substring
1096 (or (marker-position comint-accum-marker)
1097 (process-mark (get-buffer-process (current-buffer))))
1098 (point))
1099 comint-input-ring-index nil))
1100 (comint-previous-matching-input
1101 (concat "^" (regexp-quote comint-matching-input-from-input-string))
1102 arg))
1103
1104 (defun comint-next-matching-input-from-input (arg)
1105 "Search forwards through input history for match for current input.
1106 \(Following history elements are more recent commands.)
1107 With prefix argument N, search for Nth following match.
1108 If N is negative, search backwards for the -Nth previous match."
1109 (interactive "p")
1110 (comint-previous-matching-input-from-input (- arg)))
1111
1112
1113 (defun comint-replace-by-expanded-history (&optional silent start)
1114 "Expand input command history references before point.
1115 Expansion is dependent on the value of `comint-input-autoexpand'.
1116
1117 This function depends on the buffer's idea of the input history, which may not
1118 match the command interpreter's idea, assuming it has one.
1119
1120 Assumes history syntax is like typical Un*x shells'. However, since Emacs
1121 cannot know the interpreter's idea of input line numbers, assuming it has one,
1122 it cannot expand absolute input line number references.
1123
1124 If the optional argument SILENT is non-nil, never complain
1125 even if history reference seems erroneous.
1126
1127 If the optional argument START is non-nil, that specifies the
1128 start of the text to scan for history references, rather
1129 than the logical beginning of line.
1130
1131 See `comint-magic-space' and `comint-replace-by-expanded-history-before-point'.
1132
1133 Returns t if successful."
1134 (interactive)
1135 (if (and comint-input-autoexpand
1136 (if comint-use-prompt-regexp-instead-of-fields
1137 ;; Use comint-prompt-regexp
1138 (save-excursion
1139 (beginning-of-line)
1140 (looking-at (concat comint-prompt-regexp "!\\|\\^")))
1141 ;; Use input fields. User input that hasn't been entered
1142 ;; yet, at the end of the buffer, has a nil `field' property.
1143 (and (null (get-char-property (point) 'field))
1144 (string-match "!\\|^\\^" (field-string)))))
1145 ;; Looks like there might be history references in the command.
1146 (let ((previous-modified-tick (buffer-modified-tick)))
1147 (comint-replace-by-expanded-history-before-point silent start)
1148 (/= previous-modified-tick (buffer-modified-tick)))))
1149
1150
1151 (defun comint-replace-by-expanded-history-before-point (silent &optional start)
1152 "Expand directory stack reference before point.
1153 See `comint-replace-by-expanded-history'. Returns t if successful.
1154
1155 If the optional argument START is non-nil, that specifies the
1156 start of the text to scan for history references, rather
1157 than the logical beginning of line."
1158 (save-excursion
1159 (let ((toend (- (line-end-position) (point)))
1160 (start (comint-line-beginning-position)))
1161 (goto-char start)
1162 (while (progn
1163 (skip-chars-forward "^!^" (- (line-end-position) toend))
1164 (< (point) (- (line-end-position) toend)))
1165 ;; This seems a bit complex. We look for references such as !!, !-num,
1166 ;; !foo, !?foo, !{bar}, !?{bar}, ^oh, ^my^, ^god^it, ^never^ends^.
1167 ;; If that wasn't enough, the plings can be suffixed with argument
1168 ;; range specifiers.
1169 ;; Argument ranges are complex too, so we hive off the input line,
1170 ;; referenced with plings, with the range string to `comint-args'.
1171 (setq comint-input-ring-index nil)
1172 (cond ((or (= (preceding-char) ?\\)
1173 (comint-within-quotes start (point)))
1174 ;; The history is quoted, or we're in quotes.
1175 (goto-char (1+ (point))))
1176 ((looking-at "![0-9]+\\($\\|[^-]\\)")
1177 ;; We cannot know the interpreter's idea of input line numbers.
1178 (goto-char (match-end 0))
1179 (message "Absolute reference cannot be expanded"))
1180 ((looking-at "!-\\([0-9]+\\)\\(:?[0-9^$*-]+\\)?")
1181 ;; Just a number of args from `number' lines backward.
1182 (let ((number (1- (string-to-number
1183 (buffer-substring (match-beginning 1)
1184 (match-end 1))))))
1185 (if (<= number (ring-length comint-input-ring))
1186 (progn
1187 (replace-match
1188 (comint-args (comint-previous-input-string number)
1189 (match-beginning 2) (match-end 2))
1190 t t)
1191 (setq comint-input-ring-index number)
1192 (message "History item: %d" (1+ number)))
1193 (goto-char (match-end 0))
1194 (message "Relative reference exceeds input history size"))))
1195 ((or (looking-at "!!?:?\\([0-9^$*-]+\\)") (looking-at "!!"))
1196 ;; Just a number of args from the previous input line.
1197 (replace-match
1198 (comint-args (comint-previous-input-string 0)
1199 (match-beginning 1) (match-end 1))
1200 t t)
1201 (message "History item: previous"))
1202 ((looking-at
1203 "!\\??\\({\\(.+\\)}\\|\\(\\sw+\\)\\)\\(:?[0-9^$*-]+\\)?")
1204 ;; Most recent input starting with or containing (possibly
1205 ;; protected) string, maybe just a number of args. Phew.
1206 (let* ((mb1 (match-beginning 1)) (me1 (match-end 1))
1207 (mb2 (match-beginning 2)) (me2 (match-end 2))
1208 (exp (buffer-substring (or mb2 mb1) (or me2 me1)))
1209 (pref (if (save-match-data (looking-at "!\\?")) "" "^"))
1210 (pos (save-match-data
1211 (comint-previous-matching-input-string-position
1212 (concat pref (regexp-quote exp)) 1))))
1213 (if (null pos)
1214 (progn
1215 (goto-char (match-end 0))
1216 (or silent
1217 (progn (message "Not found")
1218 (ding))))
1219 (setq comint-input-ring-index pos)
1220 (replace-match
1221 (comint-args (ring-ref comint-input-ring pos)
1222 (match-beginning 4) (match-end 4))
1223 t t)
1224 (message "History item: %d" (1+ pos)))))
1225 ((looking-at "\\^\\([^^]+\\)\\^?\\([^^]*\\)\\^?")
1226 ;; Quick substitution on the previous input line.
1227 (let ((old (buffer-substring (match-beginning 1) (match-end 1)))
1228 (new (buffer-substring (match-beginning 2) (match-end 2)))
1229 (pos nil))
1230 (replace-match (comint-previous-input-string 0) t t)
1231 (setq pos (point))
1232 (goto-char (match-beginning 0))
1233 (if (not (search-forward old pos t))
1234 (or silent
1235 (error "Not found"))
1236 (replace-match new t t)
1237 (message "History item: substituted"))))
1238 (t
1239 (forward-char 1)))))))
1240
1241
1242 (defun comint-magic-space (arg)
1243 "Expand input history references before point and insert ARG spaces.
1244 A useful command to bind to SPC. See `comint-replace-by-expanded-history'."
1245 (interactive "p")
1246 (comint-replace-by-expanded-history)
1247 (self-insert-command arg))
1248 \f
1249 (defun comint-within-quotes (beg end)
1250 "Return t if the number of quotes between BEG and END is odd.
1251 Quotes are single and double."
1252 (let ((countsq (comint-how-many-region "\\(^\\|[^\\\\]\\)\'" beg end))
1253 (countdq (comint-how-many-region "\\(^\\|[^\\\\]\\)\"" beg end)))
1254 (or (= (mod countsq 2) 1) (= (mod countdq 2) 1))))
1255
1256 (defun comint-how-many-region (regexp beg end)
1257 "Return number of matches for REGEXP from BEG to END."
1258 (let ((count 0))
1259 (save-excursion
1260 (save-match-data
1261 (goto-char beg)
1262 (while (re-search-forward regexp end t)
1263 (setq count (1+ count)))))
1264 count))
1265
1266 (defun comint-args (string begin end)
1267 ;; From STRING, return the args depending on the range specified in the text
1268 ;; from BEGIN to END. If BEGIN is nil, assume all args. Ignore leading `:'.
1269 ;; Range can be x-y, x-, -y, where x/y can be [0-9], *, ^, $.
1270 (save-match-data
1271 (if (null begin)
1272 (comint-arguments string 0 nil)
1273 (let* ((range (buffer-substring
1274 (if (eq (char-after begin) ?:) (1+ begin) begin) end))
1275 (nth (cond ((string-match "^[*^]" range) 1)
1276 ((string-match "^-" range) 0)
1277 ((string-equal range "$") nil)
1278 (t (string-to-number range))))
1279 (mth (cond ((string-match "[-*$]$" range) nil)
1280 ((string-match "-" range)
1281 (string-to-number (substring range (match-end 0))))
1282 (t nth))))
1283 (comint-arguments string nth mth)))))
1284
1285 (defun comint-delim-arg (arg)
1286 "Return a list of arguments from ARG.
1287 Break it up at the delimiters in `comint-delimiter-argument-list'.
1288 Returned list is backwards."
1289 (if (null comint-delimiter-argument-list)
1290 (list arg)
1291 (let ((args nil)
1292 (pos 0)
1293 (len (length arg)))
1294 (while (< pos len)
1295 (let ((char (aref arg pos))
1296 (start pos))
1297 (if (memq char comint-delimiter-argument-list)
1298 (while (and (< pos len) (eq (aref arg pos) char))
1299 (setq pos (1+ pos)))
1300 (while (and (< pos len)
1301 (not (memq (aref arg pos)
1302 comint-delimiter-argument-list)))
1303 (setq pos (1+ pos))))
1304 (setq args (cons (substring arg start pos) args))))
1305 args)))
1306
1307 (defun comint-arguments (string nth mth)
1308 "Return from STRING the NTH to MTH arguments.
1309 NTH and/or MTH can be nil, which means the last argument.
1310 Returned arguments are separated by single spaces.
1311 We assume whitespace separates arguments, except within quotes
1312 and except for a space or tab that immediately follows a backslash.
1313 Also, a run of one or more of a single character
1314 in `comint-delimiter-argument-list' is a separate argument.
1315 Argument 0 is the command name."
1316 ;; The first line handles ordinary characters and backslash-sequences
1317 ;; (except with w32 msdos-like shells, where backslashes are valid).
1318 ;; The second matches "-quoted strings.
1319 ;; The third matches '-quoted strings.
1320 ;; The fourth matches `-quoted strings.
1321 ;; This seems to fit the syntax of BASH 2.0.
1322 (let* ((first (if (if (fboundp 'w32-shell-dos-semantics)
1323 (w32-shell-dos-semantics))
1324 "[^ \n\t\"'`]+\\|"
1325 "[^ \n\t\"'`\\]+\\|\\\\[\"'`\\ \t]+\\|"))
1326 (argpart (concat first
1327 "\\(\"\\([^\"\\]\\|\\\\.\\)*\"\\|\
1328 '[^']*'\\|\
1329 `[^`]*`\\)"))
1330 (args ()) (pos 0)
1331 (count 0)
1332 beg str quotes)
1333 ;; Build a list of all the args until we have as many as we want.
1334 (while (and (or (null mth) (<= count mth))
1335 (string-match argpart string pos))
1336 (if (and beg (= pos (match-beginning 0)))
1337 ;; It's contiguous, part of the same arg.
1338 (setq pos (match-end 0)
1339 quotes (or quotes (match-beginning 1)))
1340 ;; It's a new separate arg.
1341 (if beg
1342 ;; Put the previous arg, if there was one, onto ARGS.
1343 (setq str (substring string beg pos)
1344 args (if quotes (cons str args)
1345 (nconc (comint-delim-arg str) args))))
1346 (setq count (length args))
1347 (setq quotes (match-beginning 1))
1348 (setq beg (match-beginning 0))
1349 (setq pos (match-end 0))))
1350 (if beg
1351 (setq str (substring string beg pos)
1352 args (if quotes (cons str args)
1353 (nconc (comint-delim-arg str) args))))
1354 (setq count (length args))
1355 (let ((n (or nth (1- count)))
1356 (m (if mth (1- (- count mth)) 0)))
1357 (mapconcat
1358 (function (lambda (a) a)) (nthcdr n (nreverse (nthcdr m args))) " "))))
1359 \f
1360 ;;
1361 ;; Input processing stuff
1362 ;;
1363 (defun comint-add-to-input-history (cmd)
1364 "Add CMD to the input history.
1365 Ignore duplicates if `comint-input-ignoredups' is non-nil."
1366 (if (and (funcall comint-input-filter cmd)
1367 (or (null comint-input-ignoredups)
1368 (not (ring-p comint-input-ring))
1369 (ring-empty-p comint-input-ring)
1370 (not (string-equal (ring-ref comint-input-ring 0)
1371 cmd))))
1372 (ring-insert comint-input-ring cmd)))
1373
1374 (defun comint-send-input (&optional no-newline)
1375 "Send input to process.
1376 After the process output mark, sends all text from the process mark to
1377 point as input to the process. Before the process output mark, calls
1378 value of variable `comint-get-old-input' to retrieve old input, copies
1379 it to the process mark, and sends it. A terminal newline is also
1380 inserted into the buffer and sent to the process unless NO-NEWLINE is
1381 non-nil.
1382
1383 Any history reference may be expanded depending on the value of the variable
1384 `comint-input-autoexpand'. The list of function names contained in the value
1385 of `comint-input-filter-functions' is called on the input before sending it.
1386 The input is entered into the input history ring, if the value of variable
1387 `comint-input-filter' returns non-nil when called on the input.
1388
1389 If variable `comint-eol-on-send' is non-nil, then point is moved to the
1390 end of line before sending the input.
1391
1392 After the input has been sent, if `comint-process-echoes' is non-nil,
1393 then `comint-send-input' waits to see if the process outputs a string
1394 matching the input, and if so, deletes that part of the output.
1395
1396 The values of `comint-get-old-input', `comint-input-filter-functions', and
1397 `comint-input-filter' are chosen according to the command interpreter running
1398 in the buffer. E.g.,
1399
1400 If the interpreter is the csh,
1401 `comint-get-old-input' is the default:
1402 If `comint-use-prompt-regexp-instead-of-fields' is nil, then
1403 either return the current input field, if point is on an input
1404 field, or the current line, if point is on an output field.
1405 If `comint-use-prompt-regexp-instead-of-fields' is non-nil, then
1406 return the current line with any initial string matching the
1407 regexp `comint-prompt-regexp' removed.
1408 `comint-input-filter-functions' monitors input for \"cd\", \"pushd\", and
1409 \"popd\" commands. When it sees one, it cd's the buffer.
1410 comint-input-filter is the default: returns t if the input isn't all white
1411 space.
1412
1413 If the Comint is Lucid Common Lisp,
1414 comint-get-old-input snarfs the sexp ending at point.
1415 comint-input-filter-functions does nothing.
1416 comint-input-filter returns nil if the input matches input-filter-regexp,
1417 which matches (1) all whitespace (2) :a, :c, etc.
1418
1419 Similarly for Soar, Scheme, etc."
1420 (interactive)
1421 ;; Note that the input string does not include its terminal newline.
1422 (let ((proc (get-buffer-process (current-buffer))))
1423 (if (not proc) (error "Current buffer has no process")
1424 (let* ((pmark (process-mark proc))
1425 (intxt (if (>= (point) (marker-position pmark))
1426 (progn (if comint-eol-on-send (end-of-line))
1427 (buffer-substring pmark (point)))
1428 (let ((copy (funcall comint-get-old-input)))
1429 (goto-char pmark)
1430 (insert copy)
1431 copy)))
1432 (input (if (not (eq comint-input-autoexpand 'input))
1433 ;; Just whatever's already there
1434 intxt
1435 ;; Expand and leave it visible in buffer
1436 (comint-replace-by-expanded-history t pmark)
1437 (buffer-substring pmark (point))))
1438 (history (if (not (eq comint-input-autoexpand 'history))
1439 input
1440 ;; This is messy 'cos ultimately the original
1441 ;; functions used do insertion, rather than return
1442 ;; strings. We have to expand, then insert back.
1443 (comint-replace-by-expanded-history t pmark)
1444 (let ((copy (buffer-substring pmark (point)))
1445 (start (point)))
1446 (insert input)
1447 (delete-region pmark start)
1448 copy))))
1449
1450 (unless no-newline
1451 (insert ?\n))
1452
1453 (comint-add-to-input-history history)
1454
1455 (run-hook-with-args 'comint-input-filter-functions
1456 (if no-newline input
1457 (concat input "\n")))
1458
1459 (let ((beg (marker-position pmark))
1460 (end (if no-newline (point) (1- (point)))))
1461 (when (> end beg)
1462 ;; Set text-properties for the input field
1463 (add-text-properties
1464 beg end
1465 '(front-sticky t
1466 font-lock-face comint-highlight-input
1467 mouse-face highlight
1468 help-echo "mouse-2: insert after prompt as new input"))
1469 (unless comint-use-prompt-regexp-instead-of-fields
1470 ;; Give old user input a field property of `input', to
1471 ;; distinguish it from both process output and unsent
1472 ;; input. The terminating newline is put into a special
1473 ;; `boundary' field to make cursor movement between input
1474 ;; and output fields smoother.
1475 (put-text-property beg end 'field 'input)))
1476 (unless (or no-newline comint-use-prompt-regexp-instead-of-fields)
1477 ;; Cover the terminating newline
1478 (add-text-properties end (1+ end)
1479 '(rear-nonsticky t
1480 field boundary
1481 inhibit-line-move-field-capture t))))
1482
1483 (comint-snapshot-last-prompt)
1484
1485 (setq comint-save-input-ring-index comint-input-ring-index)
1486 (setq comint-input-ring-index nil)
1487 ;; Update the markers before we send the input
1488 ;; in case we get output amidst sending the input.
1489 (set-marker comint-last-input-start pmark)
1490 (set-marker comint-last-input-end (point))
1491 (set-marker (process-mark proc) (point))
1492 ;; clear the "accumulation" marker
1493 (set-marker comint-accum-marker nil)
1494 (let ((comint-input-sender-no-newline no-newline))
1495 (funcall comint-input-sender proc input))
1496
1497 ;; Optionally delete echoed input (after checking it).
1498 (when comint-process-echoes
1499 (let ((echo-len (- comint-last-input-end
1500 comint-last-input-start)))
1501 ;; Wait for all input to be echoed:
1502 (while (and (accept-process-output proc)
1503 (> (+ comint-last-input-end echo-len)
1504 (point-max))
1505 (zerop
1506 (compare-buffer-substrings
1507 nil comint-last-input-start
1508 (- (point-max) echo-len)
1509 ;; Above difference is equivalent to
1510 ;; (+ comint-last-input-start
1511 ;; (- (point-max) comint-last-input-end))
1512 nil comint-last-input-end (point-max)))))
1513 (if (and
1514 (<= (+ comint-last-input-end echo-len)
1515 (point-max))
1516 (zerop
1517 (compare-buffer-substrings
1518 nil comint-last-input-start comint-last-input-end
1519 nil comint-last-input-end
1520 (+ comint-last-input-end echo-len))))
1521 (delete-region comint-last-input-end
1522 (+ comint-last-input-end echo-len)))))
1523
1524 ;; This used to call comint-output-filter-functions,
1525 ;; but that scrolled the buffer in undesirable ways.
1526 (run-hook-with-args 'comint-output-filter-functions "")))))
1527
1528 (defvar comint-preoutput-filter-functions nil
1529 "List of functions to call before inserting Comint output into the buffer.
1530 Each function gets one argument, a string containing the text received
1531 from the subprocess. It should return the string to insert, perhaps
1532 the same string that was received, or perhaps a modified or transformed
1533 string.
1534
1535 The functions on the list are called sequentially, and each one is
1536 given the string returned by the previous one. The string returned by
1537 the last function is the text that is actually inserted in the
1538 redirection buffer.
1539
1540 You can use `add-hook' to add functions to this list
1541 either globally or locally.")
1542
1543 (defvar comint-inhibit-carriage-motion nil
1544 "If nil, Comint will interpret `carriage control' characters in output.
1545 See `comint-carriage-motion' for details.")
1546
1547 ;; When non-nil, this is an overlay over the last recognized prompt in
1548 ;; the buffer; it is used when highlighting the prompt.
1549 (defvar comint-last-prompt-overlay nil)
1550
1551 (defun comint-snapshot-last-prompt ()
1552 "`snapshot' any current `comint-last-prompt-overlay'.
1553 freeze its attributes in place, even when more input comes a long
1554 and moves the prompt overlay."
1555 (when comint-last-prompt-overlay
1556 (let ((inhibit-read-only t))
1557 (add-text-properties (overlay-start comint-last-prompt-overlay)
1558 (overlay-end comint-last-prompt-overlay)
1559 (overlay-properties comint-last-prompt-overlay)))))
1560
1561 (defun comint-carriage-motion (start end)
1562 "Interpret carriage control characters in the region from START to END.
1563 Translate carriage return/linefeed sequences to linefeeds.
1564 Make single carriage returns delete to the beginning of the line.
1565 Make backspaces delete the previous character."
1566 (save-excursion
1567 ;; First do a quick check to see if there are any applicable
1568 ;; characters, so we can avoid calling save-match-data and
1569 ;; save-restriction if not.
1570 (goto-char start)
1571 (when (< (skip-chars-forward "^\b\r" end) (- end start))
1572 (save-match-data
1573 (save-restriction
1574 (widen)
1575 (let ((inhibit-field-text-motion t)
1576 (inhibit-read-only t))
1577 ;; CR LF -> LF
1578 ;; Note that this won't work properly when the CR and LF
1579 ;; are in different output chunks, but this is probably an
1580 ;; exceedingly rare case (because they are generally
1581 ;; written as a unit), and to delay interpretation of a
1582 ;; trailing CR in a chunk would result in odd interactive
1583 ;; behavior (and this case is probably far more common).
1584 (while (re-search-forward "\r$" end t)
1585 (delete-char -1))
1586 ;; bare CR -> delete preceding line
1587 (goto-char start)
1588 (while (search-forward "\r" end t)
1589 (delete-region (point) (line-beginning-position)))
1590 ;; BS -> delete preceding character
1591 (goto-char start)
1592 (while (search-forward "\b" end t)
1593 (delete-char -2))))))))
1594
1595 ;; The purpose of using this filter for comint processes
1596 ;; is to keep comint-last-input-end from moving forward
1597 ;; when output is inserted.
1598 (defun comint-output-filter (process string)
1599 (let ((oprocbuf (process-buffer process)))
1600 ;; First check for killed buffer or no input.
1601 (when (and string oprocbuf (buffer-name oprocbuf))
1602 (with-current-buffer oprocbuf
1603 ;; Run preoutput filters
1604 (let ((functions comint-preoutput-filter-functions))
1605 (while (and functions string)
1606 (if (eq (car functions) t)
1607 (let ((functions (default-value 'comint-preoutput-filter-functions)))
1608 (while (and functions string)
1609 (setq string (funcall (car functions) string))
1610 (setq functions (cdr functions))))
1611 (setq string (funcall (car functions) string)))
1612 (setq functions (cdr functions))))
1613
1614 ;; Insert STRING
1615 (let ((inhibit-read-only t)
1616 ;; Avoid the overhead of save-excursion, since we just
1617 ;; fiddle with the point
1618 (saved-point (point-marker)))
1619
1620 ;; The point should float after any insertion we do
1621 (set-marker-insertion-type saved-point t)
1622
1623 ;; We temporarly remove any buffer narrowing, in case the
1624 ;; process mark is outside of the restriction
1625 (save-restriction
1626 (widen)
1627
1628 (goto-char (process-mark process))
1629 (set-marker comint-last-output-start (point))
1630
1631 ;; insert-before-markers is a bad thing. XXX
1632 ;;
1633 ;; It is used here to force window-point markers (used to
1634 ;; store the value of point in non-selected windows) to
1635 ;; advance, but it also screws up any other markers that we
1636 ;; don't _want_ to advance, such as the start-marker of some
1637 ;; of the overlays we create.
1638 ;;
1639 ;; We work around the problem with the overlays by
1640 ;; explicitly adjusting them after we do the insertion, but
1641 ;; in the future this problem should be solved correctly, by
1642 ;; using `insert', and making the insertion-type of
1643 ;; window-point markers settable (via a buffer-local
1644 ;; variable). In comint buffers, this variable would be set
1645 ;; to `t', to cause point in non-select windows to advance.
1646 (insert-before-markers string)
1647 ;; Fixup markers and overlays that got screwed up because we
1648 ;; used `insert-before-markers'.
1649 (let ((old-point (- (point) (length string))))
1650 ;; comint-last-output-start
1651 (set-marker comint-last-output-start old-point)
1652 ;; comint-last-input-end
1653 (when (and comint-last-input-end
1654 (equal (marker-position comint-last-input-end)
1655 (point)))
1656 (set-marker comint-last-input-end old-point))
1657 ;; No overlays we create are set to advance upon insertion
1658 ;; (at the start/end), so we assume that any overlay which
1659 ;; is at the current point was incorrectly advanced by
1660 ;; insert-before-markers. First fixup overlays that might
1661 ;; start at point:
1662 (dolist (over (overlays-at (point)))
1663 (when (= (overlay-start over) (point))
1664 (let ((end (overlay-end over)))
1665 (move-overlay over
1666 old-point
1667 (if (= end (point)) old-point end)))))
1668 ;; Then do overlays that might end at point:
1669 (dolist (over (overlays-at (1- (point))))
1670 (when (= (overlay-end over) (point))
1671 (move-overlay over
1672 (min (overlay-start over) old-point)
1673 old-point))))
1674
1675 ;; Advance process-mark
1676 (set-marker (process-mark process) (point))
1677
1678 (unless comint-inhibit-carriage-motion
1679 ;; Interpret any carriage motion characters (newline, backspace)
1680 (comint-carriage-motion comint-last-output-start (point)))
1681
1682 (run-hook-with-args 'comint-output-filter-functions string)
1683
1684 (goto-char (process-mark process)) ; in case a filter moved it
1685
1686 (unless comint-use-prompt-regexp-instead-of-fields
1687 (let ((inhibit-read-only t))
1688 (add-text-properties comint-last-output-start (point)
1689 '(rear-nonsticky t
1690 field output
1691 inhibit-line-move-field-capture t))))
1692
1693 ;; Highlight the prompt, where we define `prompt' to mean
1694 ;; the most recent output that doesn't end with a newline.
1695 (unless (and (bolp) (null comint-last-prompt-overlay))
1696 ;; Need to create or move the prompt overlay (in the case
1697 ;; where there is no prompt ((bolp) == t), we still do
1698 ;; this if there's already an existing overlay).
1699 (let ((prompt-start (save-excursion (forward-line 0) (point))))
1700 (if comint-last-prompt-overlay
1701 ;; Just move an existing overlay
1702 (move-overlay comint-last-prompt-overlay
1703 prompt-start (point))
1704 ;; Need to create the overlay
1705 (setq comint-last-prompt-overlay
1706 (make-overlay prompt-start (point)))
1707 (overlay-put comint-last-prompt-overlay
1708 'font-lock-face 'comint-highlight-prompt))))
1709
1710 (goto-char saved-point)))))))
1711
1712 (defun comint-preinput-scroll-to-bottom ()
1713 "Go to the end of buffer in all windows showing it.
1714 Movement occurs if point in the selected window is not after the process mark,
1715 and `this-command' is an insertion command. Insertion commands recognised
1716 are `self-insert-command', `comint-magic-space', `yank', and `hilit-yank'.
1717 Depends on the value of `comint-scroll-to-bottom-on-input'.
1718
1719 This function should be a pre-command hook."
1720 (if (and comint-scroll-to-bottom-on-input
1721 (memq this-command '(self-insert-command comint-magic-space yank
1722 hilit-yank)))
1723 (let* ((selected (selected-window))
1724 (current (current-buffer))
1725 (process (get-buffer-process current))
1726 (scroll comint-scroll-to-bottom-on-input))
1727 (if (and process (< (point) (process-mark process)))
1728 (if (eq scroll 'this)
1729 (goto-char (point-max))
1730 (walk-windows
1731 (function (lambda (window)
1732 (if (and (eq (window-buffer window) current)
1733 (or (eq scroll t) (eq scroll 'all)))
1734 (progn
1735 (select-window window)
1736 (goto-char (point-max))
1737 (select-window selected)))))
1738 nil t))))))
1739
1740 (defun comint-postoutput-scroll-to-bottom (string)
1741 "Go to the end of buffer in some or all windows showing it.
1742 Does not scroll if the current line is the last line in the buffer.
1743 Depends on the value of `comint-move-point-for-output' and
1744 `comint-scroll-show-maximum-output'.
1745
1746 This function should be in the list `comint-output-filter-functions'."
1747 (let* ((selected (selected-window))
1748 (current (current-buffer))
1749 (process (get-buffer-process current))
1750 (scroll comint-move-point-for-output))
1751 (unwind-protect
1752 (if process
1753 (walk-windows
1754 (function (lambda (window)
1755 (if (eq (window-buffer window) current)
1756 (progn
1757 (select-window window)
1758 (if (and (< (point) (process-mark process))
1759 (or (eq scroll t) (eq scroll 'all)
1760 ;; Maybe user wants point to jump to end.
1761 (and (eq scroll 'this) (eq selected window))
1762 (and (eq scroll 'others) (not (eq selected window)))
1763 ;; If point was at the end, keep it at end.
1764 (and (marker-position comint-last-output-start)
1765 (>= (point) comint-last-output-start))))
1766 (goto-char (process-mark process)))
1767 ;; Optionally scroll so that the text
1768 ;; ends at the bottom of the window.
1769 (if (and comint-scroll-show-maximum-output
1770 (= (point) (point-max)))
1771 (save-excursion
1772 (goto-char (point-max))
1773 (recenter -1)))
1774 (select-window selected)))))
1775 nil t))
1776 (set-buffer current))))
1777
1778 (defun comint-truncate-buffer (&optional string)
1779 "Truncate the buffer to `comint-buffer-maximum-size'.
1780 This function could be on `comint-output-filter-functions' or bound to a key."
1781 (interactive)
1782 (save-excursion
1783 (goto-char (process-mark (get-buffer-process (current-buffer))))
1784 (forward-line (- comint-buffer-maximum-size))
1785 (beginning-of-line)
1786 (let ((inhibit-read-only t))
1787 (delete-region (point-min) (point)))))
1788
1789 (defun comint-strip-ctrl-m (&optional string)
1790 "Strip trailing `^M' characters from the current output group.
1791 This function could be on `comint-output-filter-functions' or bound to a key."
1792 (interactive)
1793 (let ((pmark (process-mark (get-buffer-process (current-buffer)))))
1794 (save-excursion
1795 (condition-case nil
1796 (goto-char
1797 (if (interactive-p) comint-last-input-end comint-last-output-start))
1798 (error nil))
1799 (while (re-search-forward "\r+$" pmark t)
1800 (replace-match "" t t)))))
1801 (defalias 'shell-strip-ctrl-m 'comint-strip-ctrl-m)
1802
1803 (defun comint-show-maximum-output ()
1804 "Put the end of the buffer at the bottom of the window."
1805 (interactive)
1806 (goto-char (point-max))
1807 (recenter -1))
1808
1809 (defun comint-get-old-input-default ()
1810 "Default for `comint-get-old-input'.
1811 If `comint-use-prompt-regexp-instead-of-fields' is nil, then either
1812 return the current input field, if point is on an input field, or the
1813 current line, if point is on an output field.
1814 If `comint-use-prompt-regexp-instead-of-fields' is non-nil, then return
1815 the current line with any initial string matching the regexp
1816 `comint-prompt-regexp' removed."
1817 (let ((bof (field-beginning)))
1818 (if (eq (get-char-property bof 'field) 'input)
1819 (field-string-no-properties bof)
1820 (comint-bol)
1821 (buffer-substring-no-properties (point) (line-end-position)))))
1822
1823 (defun comint-copy-old-input ()
1824 "Insert after prompt old input at point as new input to be edited.
1825 Calls `comint-get-old-input' to get old input."
1826 (interactive)
1827 (let ((input (funcall comint-get-old-input))
1828 (process (get-buffer-process (current-buffer))))
1829 (if (not process)
1830 (error "Current buffer has no process")
1831 (goto-char (process-mark process))
1832 (insert input))))
1833
1834 (defun comint-skip-prompt ()
1835 "Skip past the text matching regexp `comint-prompt-regexp'.
1836 If this takes us past the end of the current line, don't skip at all."
1837 (if (and (looking-at comint-prompt-regexp)
1838 (<= (match-end 0) (line-end-position)))
1839 (goto-char (match-end 0))))
1840
1841 (defun comint-after-pmark-p ()
1842 "Return t if point is after the process output marker."
1843 (let ((pmark (process-mark (get-buffer-process (current-buffer)))))
1844 (<= (marker-position pmark) (point))))
1845
1846 (defun comint-simple-send (proc string)
1847 "Default function for sending to PROC input STRING.
1848 This just sends STRING plus a newline. To override this,
1849 set the hook `comint-input-sender'."
1850 (comint-send-string proc string)
1851 (if comint-input-sender-no-newline
1852 (if (not (string-equal string ""))
1853 (process-send-eof))
1854 (comint-send-string proc "\n")))
1855
1856 (defun comint-line-beginning-position ()
1857 "Return the buffer position of the beginning of the line, after any prompt.
1858 If `comint-use-prompt-regexp-instead-of-fields' is non-nil, then the
1859 prompt skip is done by skipping text matching the regular expression
1860 `comint-prompt-regexp', a buffer local variable."
1861 (if comint-use-prompt-regexp-instead-of-fields
1862 ;; Use comint-prompt-regexp
1863 (save-excursion
1864 (beginning-of-line)
1865 (comint-skip-prompt)
1866 (point))
1867 ;; Use input fields. Note that, unlike the behavior of
1868 ;; `line-beginning-position' inside a field, this function will
1869 ;; return the position of the end of a prompt, even if the point is
1870 ;; already inside the prompt. In order to do this, it assumes that
1871 ;; if there are two fields on a line, then the first one is the
1872 ;; prompt, and the second one is an input field, and is front-sticky
1873 ;; (as input fields should be).
1874 (constrain-to-field (line-beginning-position) (line-end-position))))
1875
1876 (defun comint-bol (&optional arg)
1877 "Go to the beginning of line, then skip past the prompt, if any.
1878 If prefix argument is given (\\[universal-argument]) the prompt is not skipped.
1879 If `comint-use-prompt-regexp-instead-of-fields' is non-nil, then the
1880 prompt skip is done by skipping text matching the regular expression
1881 `comint-prompt-regexp', a buffer local variable."
1882 (interactive "P")
1883 (if arg
1884 ;; Unlike `beginning-of-line', forward-line ignores field boundaries
1885 (forward-line 0)
1886 (goto-char (comint-line-beginning-position))))
1887
1888 ;; These three functions are for entering text you don't want echoed or
1889 ;; saved -- typically passwords to ftp, telnet, or somesuch.
1890 ;; Just enter m-x send-invisible and type in your line, or add
1891 ;; `comint-watch-for-password-prompt' to `comint-output-filter-functions'.
1892
1893 (defun comint-read-noecho (prompt &optional stars)
1894 "Read a single line of text from user without echoing, and return it.
1895 Prompt with argument PROMPT, a string. Optional argument STARS causes
1896 input to be echoed with '*' characters on the prompt line. Input ends with
1897 RET, LFD, or ESC. DEL or C-h rubs out. C-u kills line. C-g aborts (if
1898 `inhibit-quit' is set because e.g. this function was called from a process
1899 filter and C-g is pressed, this function returns nil rather than a string).
1900
1901 Note that the keystrokes comprising the text can still be recovered
1902 \(temporarily) with \\[view-lossage]. Some people find this worrisome (see,
1903 however, `clear-this-command-keys').
1904 Once the caller uses the password, it can erase the password
1905 by doing (clear-string STRING)."
1906 (let ((ans "")
1907 (newans nil)
1908 (c 0)
1909 (echo-keystrokes 0)
1910 (cursor-in-echo-area t)
1911 (message-log-max nil)
1912 (done nil))
1913 (while (not done)
1914 (if stars
1915 (message "%s%s" prompt (make-string (length ans) ?*))
1916 (message "%s" prompt))
1917 ;; Use this instead of `read-char' to avoid "Non-character input-event".
1918 (setq c (read-char-exclusive))
1919 (cond ((= c ?\C-g)
1920 ;; This function may get called from a process filter, where
1921 ;; inhibit-quit is set. In later versions of emacs read-char
1922 ;; may clear quit-flag itself and return C-g. That would make
1923 ;; it impossible to quit this loop in a simple way, so
1924 ;; re-enable it here (for backward-compatibility the check for
1925 ;; quit-flag below would still be necessary, so this seems
1926 ;; like the simplest way to do things).
1927 (setq quit-flag t
1928 done t))
1929 ((or (= c ?\r) (= c ?\n) (= c ?\e))
1930 (setq done t))
1931 ((= c ?\C-u)
1932 (clear-string ans)
1933 (setq ans ""))
1934 ((and (/= c ?\b) (/= c ?\177))
1935 (setq newans (concat ans (char-to-string c)))
1936 (clear-string ans)
1937 (setq ans newans))
1938 ((> (length ans) 0)
1939 (aset ans (1- (length ans)) 0)
1940 (setq ans (substring ans 0 -1)))))
1941 (if quit-flag
1942 ;; Emulate a true quit, except that we have to return a value.
1943 (prog1
1944 (setq quit-flag nil)
1945 (message "Quit")
1946 (beep t))
1947 (message "")
1948 ans)))
1949
1950 (defun send-invisible (&optional prompt)
1951 "Read a string without echoing.
1952 Then send it to the process running in the current buffer.
1953 The string is sent using `comint-input-sender'.
1954 Security bug: your string can still be temporarily recovered with
1955 \\[view-lossage]; `clear-this-command-keys' can fix that."
1956 (interactive "P") ; Defeat snooping via C-x ESC ESC
1957 (let ((proc (get-buffer-process (current-buffer))))
1958 (if proc
1959 (let ((str (comint-read-noecho (or prompt "Non-echoed text: ") t)))
1960 (if (stringp str)
1961 (progn
1962 (comint-snapshot-last-prompt)
1963 (funcall comint-input-sender proc str))
1964 (message "Warning: text will be echoed")))
1965 (error "Current buffer has no process"))))
1966
1967 (defun comint-watch-for-password-prompt (string)
1968 "Prompt in the minibuffer for password and send without echoing.
1969 This function uses `send-invisible' to read and send a password to the buffer's
1970 process if STRING contains a password prompt defined by
1971 `comint-password-prompt-regexp'.
1972
1973 This function could be in the list `comint-output-filter-functions'."
1974 (when (string-match comint-password-prompt-regexp string)
1975 (when (string-match "^[ \n\r\t\v\f\b\a]+" string)
1976 (setq string (replace-match "" t t string)))
1977 (send-invisible string)))
1978 \f
1979 ;; Low-level process communication
1980
1981 (defun comint-send-string (process string)
1982 "Like `process-send-string', but also does extra bookkeeping for Comint mode."
1983 (if process
1984 (with-current-buffer (if (processp process)
1985 (process-buffer process)
1986 (get-buffer process))
1987 (comint-snapshot-last-prompt))
1988 (comint-snapshot-last-prompt))
1989 (process-send-string process string))
1990
1991 (defun comint-send-region (process start end)
1992 "Like `process-send-region', but also does extra bookkeeping for Comint mode."
1993 (if process
1994 (with-current-buffer (if (processp process)
1995 (process-buffer process)
1996 (get-buffer process))
1997 (comint-snapshot-last-prompt))
1998 (comint-snapshot-last-prompt))
1999 (process-send-region process start end))
2000
2001 \f
2002 ;; Random input hackage
2003
2004 (defun comint-delete-output ()
2005 "Delete all output from interpreter since last input.
2006 Does not delete the prompt."
2007 (interactive)
2008 (let ((proc (get-buffer-process (current-buffer)))
2009 (replacement nil))
2010 (save-excursion
2011 (let ((pmark (progn (goto-char (process-mark proc))
2012 (forward-line 0)
2013 (point-marker))))
2014 (delete-region comint-last-input-end pmark)
2015 (goto-char (process-mark proc))
2016 (setq replacement (concat "*** output flushed ***\n"
2017 (buffer-substring pmark (point))))
2018 (delete-region pmark (point))))
2019 ;; Output message and put back prompt
2020 (comint-output-filter proc replacement)))
2021 (defalias 'comint-kill-output 'comint-delete-output)
2022 (make-obsolete 'comint-kill-output 'comint-delete-output "21.1")
2023
2024 (defun comint-write-output (filename &optional append mustbenew)
2025 "Write output from interpreter since last input to FILENAME.
2026 Any prompt at the end of the output is not written.
2027
2028 If the optional argument APPEND (the prefix argument when interactive)
2029 is non-nil, the output is appended to the file instead.
2030
2031 If the optional argument MUSTBENEW is non-nil, check for an existing
2032 file with the same name. If MUSTBENEW is `excl', that means to get an
2033 error if the file already exists; never overwrite. If MUSTBENEW is
2034 neither nil nor `excl', that means ask for confirmation before
2035 overwriting, but do go ahead and overwrite the file if the user
2036 confirms. When interactive, MUSTBENEW is nil when appending, and t
2037 otherwise."
2038 (interactive
2039 (list (read-file-name
2040 (if current-prefix-arg
2041 "Append output to file: "
2042 "Write output to file: "))
2043 current-prefix-arg
2044 (not current-prefix-arg)))
2045 (save-excursion
2046 (goto-char (process-mark (get-buffer-process (current-buffer))))
2047 (forward-line 0)
2048 (write-region comint-last-input-end (point) filename
2049 append nil nil mustbenew)))
2050
2051 ;; This function exists for the benefit of the menu; from the keyboard,
2052 ;; users can just use `comint-write-output' with a prefix arg.
2053 (defun comint-append-output-to-file (filename)
2054 "Append output from interpreter since last input to FILENAME.
2055 Any prompt at the end of the output is not written."
2056 (interactive "fAppend output to file: ")
2057 (comint-write-output filename t))
2058
2059 (defun comint-show-output ()
2060 "Display start of this batch of interpreter output at top of window.
2061 Sets mark to the value of point when this command is run."
2062 (interactive)
2063 (push-mark)
2064 (let ((pos (or (marker-position comint-last-input-end) (point-max))))
2065 (cond (comint-use-prompt-regexp-instead-of-fields
2066 (goto-char pos)
2067 (beginning-of-line 0)
2068 (set-window-start (selected-window) (point))
2069 (comint-skip-prompt))
2070 (t
2071 (goto-char (field-beginning pos))
2072 (set-window-start (selected-window) (point))))))
2073
2074
2075 (defun comint-interrupt-subjob ()
2076 "Interrupt the current subjob.
2077 This command also kills the pending input
2078 between the process mark and point."
2079 (interactive)
2080 (comint-skip-input)
2081 (interrupt-process nil comint-ptyp)
2082 ;; (process-send-string nil "\n")
2083 )
2084
2085 (defun comint-kill-subjob ()
2086 "Send kill signal to the current subjob.
2087 This command also kills the pending input
2088 between the process mark and point."
2089 (interactive)
2090 (comint-skip-input)
2091 (kill-process nil comint-ptyp))
2092
2093 (defun comint-quit-subjob ()
2094 "Send quit signal to the current subjob.
2095 This command also kills the pending input
2096 between the process mark and point."
2097 (interactive)
2098 (comint-skip-input)
2099 (quit-process nil comint-ptyp))
2100
2101 (defun comint-stop-subjob ()
2102 "Stop the current subjob.
2103 This command also kills the pending input
2104 between the process mark and point.
2105
2106 WARNING: if there is no current subjob, you can end up suspending
2107 the top-level process running in the buffer. If you accidentally do
2108 this, use \\[comint-continue-subjob] to resume the process. (This
2109 is not a problem with most shells, since they ignore this signal.)"
2110 (interactive)
2111 (comint-skip-input)
2112 (stop-process nil comint-ptyp))
2113
2114 (defun comint-continue-subjob ()
2115 "Send CONT signal to process buffer's process group.
2116 Useful if you accidentally suspend the top-level process."
2117 (interactive)
2118 (continue-process nil comint-ptyp))
2119
2120 (defun comint-skip-input ()
2121 "Skip all pending input, from last stuff output by interpreter to point.
2122 This means mark it as if it had been sent as input, without sending it."
2123 (let ((comint-input-sender 'ignore)
2124 (comint-input-filter-functions nil))
2125 (comint-send-input t))
2126 (end-of-line)
2127 (let ((pos (point))
2128 (marker (process-mark (get-buffer-process (current-buffer)))))
2129 (insert " " (key-description (this-command-keys)))
2130 (if (= marker pos)
2131 (set-marker marker (point)))))
2132
2133 (defun comint-kill-input ()
2134 "Kill all text from last stuff output by interpreter to point."
2135 (interactive)
2136 (let ((pmark (process-mark (get-buffer-process (current-buffer)))))
2137 (if (> (point) (marker-position pmark))
2138 (kill-region pmark (point)))))
2139
2140 (defun comint-delchar-or-maybe-eof (arg)
2141 "Delete ARG characters forward or send an EOF to subprocess.
2142 Sends an EOF only if point is at the end of the buffer and there is no input."
2143 (interactive "p")
2144 (let ((proc (get-buffer-process (current-buffer))))
2145 (if (and (eobp) proc (= (point) (marker-position (process-mark proc))))
2146 (comint-send-eof)
2147 (delete-char arg))))
2148
2149 (defun comint-send-eof ()
2150 "Send an EOF to the current buffer's process."
2151 (interactive)
2152 (comint-send-input t)
2153 (process-send-eof))
2154
2155
2156 (defun comint-backward-matching-input (regexp n)
2157 "Search backward through buffer for input fields that match REGEXP.
2158 If `comint-use-prompt-regexp-instead-of-fields' is non-nil, then input
2159 fields are identified by lines that match `comint-prompt-regexp'.
2160
2161 With prefix argument N, search for Nth previous match.
2162 If N is negative, find the next or Nth next match."
2163 (interactive (comint-regexp-arg "Backward input matching (regexp): "))
2164 (if comint-use-prompt-regexp-instead-of-fields
2165 ;; Use comint-prompt-regexp
2166 (let* ((re (concat comint-prompt-regexp ".*" regexp))
2167 (pos (save-excursion (end-of-line (if (> n 0) 0 1))
2168 (if (re-search-backward re nil t n)
2169 (point)))))
2170 (if (null pos)
2171 (progn (message "Not found")
2172 (ding))
2173 (goto-char pos)
2174 (comint-bol nil)))
2175 ;; Use input fields
2176 (let* ((dir (if (< n 0) -1 1))
2177 (pos
2178 (save-excursion
2179 (while (/= n 0)
2180 (unless (re-search-backward regexp nil t dir)
2181 (error "Not found"))
2182 (when (eq (get-char-property (point) 'field) 'input)
2183 (setq n (- n dir))))
2184 (field-beginning))))
2185 (goto-char pos))))
2186
2187
2188 (defun comint-forward-matching-input (regexp arg)
2189 "Search forward through buffer for input fields that match REGEXP.
2190 If `comint-use-prompt-regexp-instead-of-fields' is non-nil, then input
2191 fields are identified by lines that match `comint-prompt-regexp'.
2192
2193 With prefix argument N, search for Nth following match.
2194 If N is negative, find the previous or Nth previous match."
2195 (interactive (comint-regexp-arg "Forward input matching (regexp): "))
2196 (comint-backward-matching-input regexp (- arg)))
2197
2198
2199 (defun comint-next-prompt (n)
2200 "Move to end of Nth next prompt in the buffer.
2201 If `comint-use-prompt-regexp-instead-of-fields' is nil, then this means
2202 the beginning of the Nth next `input' field, otherwise, it means the Nth
2203 occurrence of text matching `comint-prompt-regexp'."
2204 (interactive "p")
2205 (if comint-use-prompt-regexp-instead-of-fields
2206 ;; Use comint-prompt-regexp
2207 (let ((paragraph-start comint-prompt-regexp))
2208 (end-of-line (if (> n 0) 1 0))
2209 (forward-paragraph n)
2210 (comint-skip-prompt))
2211 ;; Use input fields
2212 (let ((pos (point))
2213 (input-pos nil)
2214 prev-pos)
2215 (while (/= n 0)
2216 (setq prev-pos pos)
2217 (setq pos
2218 (if (> n 0)
2219 (next-single-char-property-change pos 'field)
2220 (previous-single-char-property-change pos 'field)))
2221 (cond ((or (null pos) (= pos prev-pos))
2222 ;; Ran off the end of the buffer.
2223 (when (> n 0)
2224 ;; There's always an input field at the end of the
2225 ;; buffer, but it has a `field' property of nil.
2226 (setq input-pos (point-max)))
2227 ;; stop iterating
2228 (setq n 0))
2229 ((eq (get-char-property pos 'field) 'input)
2230 (setq n (if (< n 0) (1+ n) (1- n)))
2231 (setq input-pos pos))))
2232 (when input-pos
2233 (goto-char input-pos)))))
2234
2235
2236 (defun comint-previous-prompt (n)
2237 "Move to end of Nth previous prompt in the buffer.
2238 If `comint-use-prompt-regexp-instead-of-fields' is nil, then this means
2239 the beginning of the Nth previous `input' field, otherwise, it means the Nth
2240 occurrence of text matching `comint-prompt-regexp'."
2241 (interactive "p")
2242 (comint-next-prompt (- n)))
2243
2244 ;; State used by `comint-insert-previous-argument' when cycling.
2245 (defvar comint-insert-previous-argument-last-start-pos nil)
2246 (make-variable-buffer-local 'comint-insert-previous-argument-last-start-pos)
2247 (defvar comint-insert-previous-argument-last-index nil)
2248 (make-variable-buffer-local 'comint-insert-previous-argument-last-index)
2249
2250 ;; Needs fixing:
2251 ;; make comint-arguments understand negative indices as bash does
2252 (defun comint-insert-previous-argument (index)
2253 "Insert the INDEXth argument from the previous Comint command-line at point.
2254 Spaces are added at beginning and/or end of the inserted string if
2255 necessary to ensure that it's separated from adjacent arguments.
2256 Interactively, if no prefix argument is given, the last argument is inserted.
2257 Repeated interactive invocations will cycle through the same argument
2258 from progressively earlier commands (using the value of INDEX specified
2259 with the first command).
2260 This command is like `M-.' in bash."
2261 (interactive "P")
2262 (unless (null index)
2263 (setq index (prefix-numeric-value index)))
2264 (cond ((eq last-command this-command)
2265 ;; Delete last input inserted by this command.
2266 (delete-region comint-insert-previous-argument-last-start-pos (point))
2267 (setq index comint-insert-previous-argument-last-index))
2268 (t
2269 ;; This is a non-repeat invocation, so initialize state.
2270 (setq comint-input-ring-index nil)
2271 (setq comint-insert-previous-argument-last-index index)
2272 (when (null comint-insert-previous-argument-last-start-pos)
2273 ;; First usage; initialize to a marker
2274 (setq comint-insert-previous-argument-last-start-pos
2275 (make-marker)))))
2276 ;; Make sure we're not in the prompt, and add a beginning space if necess.
2277 (if (<= (point) (comint-line-beginning-position))
2278 (comint-bol)
2279 (just-one-space))
2280 ;; Remember the beginning of what we insert, so we can delete it if
2281 ;; the command is repeated.
2282 (set-marker comint-insert-previous-argument-last-start-pos (point))
2283 ;; Insert the argument.
2284 (let ((input-string (comint-previous-input-string 0)))
2285 (when (string-match "[ \t\n]*&" input-string)
2286 ;; strip terminating '&'
2287 (setq input-string (substring input-string 0 (match-beginning 0))))
2288 (insert (comint-arguments input-string index index)))
2289 ;; Make next invocation return arg from previous input
2290 (setq comint-input-ring-index (1+ (or comint-input-ring-index 0)))
2291 ;; Add a terminating space if necessary.
2292 (unless (eolp)
2293 (just-one-space)))
2294
2295 \f
2296 ;; Support for source-file processing commands.
2297 ;;============================================================================
2298 ;; Many command-interpreters (e.g., Lisp, Scheme, Soar) have
2299 ;; commands that process files of source text (e.g. loading or compiling
2300 ;; files). So the corresponding process-in-a-buffer modes have commands
2301 ;; for doing this (e.g., lisp-load-file). The functions below are useful
2302 ;; for defining these commands.
2303 ;;
2304 ;; Alas, these guys don't do exactly the right thing for Lisp, Scheme
2305 ;; and Soar, in that they don't know anything about file extensions.
2306 ;; So the compile/load interface gets the wrong default occasionally.
2307 ;; The load-file/compile-file default mechanism could be smarter -- it
2308 ;; doesn't know about the relationship between filename extensions and
2309 ;; whether the file is source or executable. If you compile foo.lisp
2310 ;; with compile-file, then the next load-file should use foo.bin for
2311 ;; the default, not foo.lisp. This is tricky to do right, particularly
2312 ;; because the extension for executable files varies so much (.o, .bin,
2313 ;; .lbin, .mo, .vo, .ao, ...).
2314
2315
2316 ;; COMINT-SOURCE-DEFAULT -- determines defaults for source-file processing
2317 ;; commands.
2318 ;;
2319 ;; COMINT-CHECK-SOURCE -- if FNAME is in a modified buffer, asks you if you
2320 ;; want to save the buffer before issuing any process requests to the command
2321 ;; interpreter.
2322 ;;
2323 ;; COMINT-GET-SOURCE -- used by the source-file processing commands to prompt
2324 ;; for the file to process.
2325
2326 (defun comint-source-default (previous-dir/file source-modes)
2327 "Compute the defaults for `load-file' and `compile-file' commands.
2328
2329 PREVIOUS-DIR/FILE is a pair (directory . filename) from the last
2330 source-file processing command. nil if there hasn't been one yet.
2331 SOURCE-MODES is a list used to determine what buffers contain source
2332 files: if the major mode of the buffer is in SOURCE-MODES, it's source.
2333 Typically, (lisp-mode) or (scheme-mode).
2334
2335 If the command is given while the cursor is inside a string, *and*
2336 the string is an existing filename, *and* the filename is not a directory,
2337 then the string is taken as default. This allows you to just position
2338 your cursor over a string that's a filename and have it taken as default.
2339
2340 If the command is given in a file buffer whose major mode is in
2341 SOURCE-MODES, then the filename is the default file, and the
2342 file's directory is the default directory.
2343
2344 If the buffer isn't a source file buffer (e.g., it's the process buffer),
2345 then the default directory & file are what was used in the last source-file
2346 processing command (i.e., PREVIOUS-DIR/FILE). If this is the first time
2347 the command has been run (PREVIOUS-DIR/FILE is nil), the default directory
2348 is the cwd, with no default file. (\"no default file\" = nil)
2349
2350 SOURCE-REGEXP is typically going to be something like (tea-mode)
2351 for T programs, (lisp-mode) for Lisp programs, (soar-mode lisp-mode)
2352 for Soar programs, etc.
2353
2354 The function returns a pair: (default-directory . default-file)."
2355 (cond ((and buffer-file-name (memq major-mode source-modes))
2356 (cons (file-name-directory buffer-file-name)
2357 (file-name-nondirectory buffer-file-name)))
2358 (previous-dir/file)
2359 (t
2360 (cons default-directory nil))))
2361
2362
2363 (defun comint-check-source (fname)
2364 "Check whether to save buffers visiting file FNAME.
2365 Prior to loading or compiling (or otherwise processing) a file (in the CMU
2366 process-in-a-buffer modes), this function can be called on the filename.
2367 If the file is loaded into a buffer, and the buffer is modified, the user
2368 is queried to see if he wants to save the buffer before proceeding with
2369 the load or compile."
2370 (let ((buff (get-file-buffer fname)))
2371 (if (and buff
2372 (buffer-modified-p buff)
2373 (y-or-n-p (format "Save buffer %s first? " (buffer-name buff))))
2374 ;; save BUFF.
2375 (let ((old-buffer (current-buffer)))
2376 (set-buffer buff)
2377 (save-buffer)
2378 (set-buffer old-buffer)))))
2379
2380 (defun comint-extract-string ()
2381 "Return string around POINT, or nil."
2382 (let ((syntax (syntax-ppss)))
2383 (when (nth 3 syntax)
2384 (condition-case ()
2385 (buffer-substring-no-properties (1+ (nth 8 syntax))
2386 (progn (goto-char (nth 8 syntax))
2387 (forward-sexp)
2388 (1- (point))))
2389 (error nil)))))
2390
2391 (defun comint-get-source (prompt prev-dir/file source-modes mustmatch-p)
2392 "Prompt for filenames in commands that process source files,
2393 e.g. loading or compiling a file.
2394 Provides a default, if there is one, and returns the result filename.
2395
2396 See `comint-source-default' for more on determining defaults.
2397
2398 PROMPT is the prompt string. PREV-DIR/FILE is the (directory . file) pair
2399 from the last source processing command. SOURCE-MODES is a list of major
2400 modes used to determine what file buffers contain source files. (These
2401 two arguments are used for determining defaults). If MUSTMATCH-P is true,
2402 then the filename reader will only accept a file that exists.
2403
2404 A typical use:
2405 (interactive (comint-get-source \"Compile file: \" prev-lisp-dir/file
2406 '(lisp-mode) t))"
2407 (let* ((def (comint-source-default prev-dir/file source-modes))
2408 (stringfile (comint-extract-string))
2409 (sfile-p (and stringfile
2410 (condition-case ()
2411 (file-exists-p stringfile)
2412 (error nil))
2413 (not (file-directory-p stringfile))))
2414 (defdir (if sfile-p (file-name-directory stringfile)
2415 (car def)))
2416 (deffile (if sfile-p (file-name-nondirectory stringfile)
2417 (cdr def)))
2418 (ans (read-file-name (if deffile (format "%s(default %s) "
2419 prompt deffile)
2420 prompt)
2421 defdir
2422 (concat defdir deffile)
2423 mustmatch-p)))
2424 (list (expand-file-name (substitute-in-file-name ans)))))
2425
2426 ;; I am somewhat divided on this string-default feature. It seems
2427 ;; to violate the principle-of-least-astonishment, in that it makes
2428 ;; the default harder to predict, so you actually have to look and see
2429 ;; what the default really is before choosing it. This can trip you up.
2430 ;; On the other hand, it can be useful, I guess. I would appreciate feedback
2431 ;; on this.
2432 ;; -Olin
2433
2434 \f
2435 ;; Simple process query facility.
2436 ;; ===========================================================================
2437 ;; This function is for commands that want to send a query to the process
2438 ;; and show the response to the user. For example, a command to get the
2439 ;; arglist for a Common Lisp function might send a "(arglist 'foo)" query
2440 ;; to an inferior Common Lisp process.
2441 ;;
2442 ;; This simple facility just sends strings to the inferior process and pops
2443 ;; up a window for the process buffer so you can see what the process
2444 ;; responds with. We don't do anything fancy like try to intercept what the
2445 ;; process responds with and put it in a pop-up window or on the message
2446 ;; line. We just display the buffer. Low tech. Simple. Works good.
2447
2448 (defun comint-proc-query (proc str)
2449 "Send to the inferior process PROC the string STR.
2450 Pop-up but do not select a window for the inferior process so that
2451 its response can be seen."
2452 (let* ((proc-buf (process-buffer proc))
2453 (proc-mark (process-mark proc)))
2454 (display-buffer proc-buf)
2455 (set-buffer proc-buf) ; but it's not the selected *window*
2456 (let ((proc-win (get-buffer-window proc-buf))
2457 (proc-pt (marker-position proc-mark)))
2458 (comint-send-string proc str) ; send the query
2459 (accept-process-output proc) ; wait for some output
2460 ;; Try to position the proc window so you can see the answer.
2461 ;; This is bogus code. If you delete the (sit-for 0), it breaks.
2462 ;; I don't know why. Wizards invited to improve it.
2463 (unless (pos-visible-in-window-p proc-pt proc-win)
2464 (let ((opoint (window-point proc-win)))
2465 (set-window-point proc-win proc-mark)
2466 (sit-for 0)
2467 (if (not (pos-visible-in-window-p opoint proc-win))
2468 (push-mark opoint)
2469 (set-window-point proc-win opoint)))))))
2470
2471 \f
2472 ;; Filename/command/history completion in a buffer
2473 ;; ===========================================================================
2474 ;; Useful completion functions, courtesy of the Ergo group.
2475
2476 ;; Six commands:
2477 ;; comint-dynamic-complete Complete or expand command, filename,
2478 ;; history at point.
2479 ;; comint-dynamic-complete-filename Complete filename at point.
2480 ;; comint-dynamic-list-filename-completions List completions in help buffer.
2481 ;; comint-replace-by-expanded-filename Expand and complete filename at point;
2482 ;; replace with expanded/completed name.
2483 ;; comint-dynamic-simple-complete Complete stub given candidates.
2484
2485 ;; These are not installed in the comint-mode keymap. But they are
2486 ;; available for people who want them. Shell-mode installs them:
2487 ;; (define-key shell-mode-map "\t" 'comint-dynamic-complete)
2488 ;; (define-key shell-mode-map "\M-?"
2489 ;; 'comint-dynamic-list-filename-completions)))
2490 ;;
2491 ;; Commands like this are fine things to put in load hooks if you
2492 ;; want them present in specific modes.
2493
2494 (defcustom comint-completion-autolist nil
2495 "*If non-nil, automatically list possibilities on partial completion.
2496 This mirrors the optional behavior of tcsh."
2497 :type 'boolean
2498 :group 'comint-completion)
2499
2500 (defcustom comint-completion-addsuffix t
2501 "*If non-nil, add a `/' to completed directories, ` ' to file names.
2502 If a cons pair, it should be of the form (DIRSUFFIX . FILESUFFIX) where
2503 DIRSUFFIX and FILESUFFIX are strings added on unambiguous or exact completion.
2504 This mirrors the optional behavior of tcsh."
2505 :type '(choice (const :tag "None" nil)
2506 (const :tag "Add /" t)
2507 (cons :tag "Suffix pair"
2508 (string :tag "Directory suffix")
2509 (string :tag "File suffix")))
2510 :group 'comint-completion)
2511
2512 (defcustom comint-completion-recexact nil
2513 "*If non-nil, use shortest completion if characters cannot be added.
2514 This mirrors the optional behavior of tcsh.
2515
2516 A non-nil value is useful if `comint-completion-autolist' is non-nil too."
2517 :type 'boolean
2518 :group 'comint-completion)
2519
2520 (defcustom comint-completion-fignore nil
2521 "*List of suffixes to be disregarded during file completion.
2522 This mirrors the optional behavior of bash and tcsh.
2523
2524 Note that this applies to `comint-dynamic-complete-filename' only."
2525 :type '(repeat (string :tag "Suffix"))
2526 :group 'comint-completion)
2527
2528 (defvar comint-file-name-prefix ""
2529 "Prefix prepended to absolute file names taken from process input.
2530 This is used by Comint's and shell's completion functions, and by shell's
2531 directory tracking functions.")
2532
2533 (defvar comint-file-name-chars
2534 (if (memq system-type '(ms-dos windows-nt cygwin))
2535 "~/A-Za-z0-9_^$!#%&{}@`'.,:()-"
2536 "[]~/A-Za-z0-9+@:_.$#%,={}-")
2537 "String of characters valid in a file name.
2538 Note that all non-ASCII characters are considered valid in a file name
2539 regardless of what this variable says.
2540
2541 This is a good thing to set in mode hooks.")
2542
2543 (defvar comint-file-name-quote-list nil
2544 "List of characters to quote with `\\' when in a file name.
2545
2546 This is a good thing to set in mode hooks.")
2547
2548
2549 (defun comint-directory (directory)
2550 "Return expanded DIRECTORY, with `comint-file-name-prefix' if absolute."
2551 (expand-file-name (if (file-name-absolute-p directory)
2552 (concat comint-file-name-prefix directory)
2553 directory)))
2554
2555
2556 (defun comint-word (word-chars)
2557 "Return the word of WORD-CHARS at point, or nil if none is found.
2558 Word constituents are considered to be those in WORD-CHARS, which is like the
2559 inside of a \"[...]\" (see `skip-chars-forward'),
2560 plus all non-ASCII characters."
2561 (save-excursion
2562 (let ((here (point))
2563 giveup)
2564 (while (not giveup)
2565 (let ((startpoint (point)))
2566 (skip-chars-backward (concat "\\\\" word-chars))
2567 ;; Fixme: This isn't consistent with Bash, at least -- not
2568 ;; all non-ASCII chars should be word constituents.
2569 (if (and (> (- (point) 2) (point-min))
2570 (= (char-after (- (point) 2)) ?\\))
2571 (forward-char -2))
2572 (if (and (> (- (point) 1) (point-min))
2573 (>= (char-after (- (point) 1)) 128))
2574 (forward-char -1))
2575 (if (= (point) startpoint)
2576 (setq giveup t))))
2577 ;; Set match-data to match the entire string.
2578 (when (< (point) here)
2579 (set-match-data (list (point) here))
2580 (match-string 0)))))
2581
2582 (defun comint-substitute-in-file-name (filename)
2583 "Return FILENAME with environment variables substituted.
2584 Supports additional environment variable syntax of the command
2585 interpreter (e.g., the percent notation of cmd.exe on NT)."
2586 (let ((name (substitute-in-file-name filename)))
2587 (if (memq system-type '(ms-dos windows-nt))
2588 (let (env-var-name
2589 env-var-val)
2590 (save-match-data
2591 (while (string-match "%\\([^\\\\/]*\\)%" name)
2592 (setq env-var-name
2593 (substring name (match-beginning 1) (match-end 1)))
2594 (setq env-var-val (if (getenv env-var-name)
2595 (getenv env-var-name)
2596 ""))
2597 (setq name (replace-match env-var-val t t name))))))
2598 name))
2599
2600 (defun comint-match-partial-filename ()
2601 "Return the filename at point, or nil if non is found.
2602 Environment variables are substituted. See `comint-word'."
2603 (let ((filename (comint-word comint-file-name-chars)))
2604 (and filename (comint-substitute-in-file-name
2605 (comint-unquote-filename filename)))))
2606
2607
2608 (defun comint-quote-filename (filename)
2609 "Return FILENAME with magic characters quoted.
2610 Magic characters are those in `comint-file-name-quote-list'."
2611 (if (null comint-file-name-quote-list)
2612 filename
2613 (let ((regexp
2614 (format "[%s]"
2615 (mapconcat 'char-to-string comint-file-name-quote-list ""))))
2616 (save-match-data
2617 (let ((i 0))
2618 (while (string-match regexp filename i)
2619 (setq filename (replace-match "\\\\\\&" nil nil filename))
2620 (setq i (1+ (match-end 0)))))
2621 filename))))
2622
2623 (defun comint-unquote-filename (filename)
2624 "Return FILENAME with quoted characters unquoted."
2625 (if (null comint-file-name-quote-list)
2626 filename
2627 (save-match-data
2628 (let ((i 0))
2629 (while (string-match "\\\\\\(.\\)" filename i)
2630 (setq filename (replace-match "\\1" nil nil filename))
2631 (setq i (+ 1 (match-beginning 0)))))
2632 filename)))
2633
2634
2635 (defun comint-dynamic-complete ()
2636 "Dynamically perform completion at point.
2637 Calls the functions in `comint-dynamic-complete-functions' to perform
2638 completion until a function returns non-nil, at which point completion is
2639 assumed to have occurred."
2640 (interactive)
2641 (run-hook-with-args-until-success 'comint-dynamic-complete-functions))
2642
2643
2644 (defun comint-dynamic-complete-filename ()
2645 "Dynamically complete the filename at point.
2646 Completes if after a filename. See `comint-match-partial-filename' and
2647 `comint-dynamic-complete-as-filename'.
2648 This function is similar to `comint-replace-by-expanded-filename', except that
2649 it won't change parts of the filename already entered in the buffer; it just
2650 adds completion characters to the end of the filename. A completions listing
2651 may be shown in a help buffer if completion is ambiguous.
2652
2653 Completion is dependent on the value of `comint-completion-addsuffix',
2654 `comint-completion-recexact' and `comint-completion-fignore', and the timing of
2655 completions listing is dependent on the value of `comint-completion-autolist'.
2656
2657 Returns t if successful."
2658 (interactive)
2659 (when (comint-match-partial-filename)
2660 (unless (window-minibuffer-p (selected-window))
2661 (message "Completing file name..."))
2662 (comint-dynamic-complete-as-filename)))
2663
2664 (defun comint-dynamic-complete-as-filename ()
2665 "Dynamically complete at point as a filename.
2666 See `comint-dynamic-complete-filename'. Returns t if successful."
2667 (let* ((completion-ignore-case (memq system-type '(ms-dos windows-nt cygwin)))
2668 (completion-ignored-extensions comint-completion-fignore)
2669 ;; If we bind this, it breaks remote directory tracking in rlogin.el.
2670 ;; I think it was originally bound to solve file completion problems,
2671 ;; but subsequent changes may have made this unnecessary. sm.
2672 ;;(file-name-handler-alist nil)
2673 (minibuffer-p (window-minibuffer-p (selected-window)))
2674 (success t)
2675 (dirsuffix (cond ((not comint-completion-addsuffix)
2676 "")
2677 ((not (consp comint-completion-addsuffix))
2678 "/")
2679 (t
2680 (car comint-completion-addsuffix))))
2681 (filesuffix (cond ((not comint-completion-addsuffix)
2682 "")
2683 ((not (consp comint-completion-addsuffix))
2684 " ")
2685 (t
2686 (cdr comint-completion-addsuffix))))
2687 (filename (or (comint-match-partial-filename) ""))
2688 (filedir (file-name-directory filename))
2689 (filenondir (file-name-nondirectory filename))
2690 (directory (if filedir (comint-directory filedir) default-directory))
2691 (completion (file-name-completion filenondir directory)))
2692 (cond ((null completion)
2693 (message "No completions of %s" filename)
2694 (setq success nil))
2695 ((eq completion t) ; Means already completed "file".
2696 (insert filesuffix)
2697 (unless minibuffer-p
2698 (message "Sole completion")))
2699 ((string-equal completion "") ; Means completion on "directory/".
2700 (comint-dynamic-list-filename-completions))
2701 (t ; Completion string returned.
2702 (let ((file (concat (file-name-as-directory directory) completion)))
2703 (insert (comint-quote-filename
2704 (substring (directory-file-name completion)
2705 (length filenondir))))
2706 (cond ((symbolp (file-name-completion completion directory))
2707 ;; We inserted a unique completion.
2708 (insert (if (file-directory-p file) dirsuffix filesuffix))
2709 (unless minibuffer-p
2710 (message "Completed")))
2711 ((and comint-completion-recexact comint-completion-addsuffix
2712 (string-equal filenondir completion)
2713 (file-exists-p file))
2714 ;; It's not unique, but user wants shortest match.
2715 (insert (if (file-directory-p file) dirsuffix filesuffix))
2716 (unless minibuffer-p
2717 (message "Completed shortest")))
2718 ((or comint-completion-autolist
2719 (string-equal filenondir completion))
2720 ;; It's not unique, list possible completions.
2721 (comint-dynamic-list-filename-completions))
2722 (t
2723 (unless minibuffer-p
2724 (message "Partially completed")))))))
2725 success))
2726
2727
2728 (defun comint-replace-by-expanded-filename ()
2729 "Dynamically expand and complete the filename at point.
2730 Replace the filename with an expanded, canonicalised and completed replacement.
2731 \"Expanded\" means environment variables (e.g., $HOME) and `~'s are replaced
2732 with the corresponding directories. \"Canonicalised\" means `..' and `.' are
2733 removed, and the filename is made absolute instead of relative. For expansion
2734 see `expand-file-name' and `substitute-in-file-name'. For completion see
2735 `comint-dynamic-complete-filename'."
2736 (interactive)
2737 (let ((filename (comint-match-partial-filename)))
2738 (when filename
2739 (replace-match (expand-file-name filename) t t)
2740 (comint-dynamic-complete-filename))))
2741
2742
2743 (defun comint-dynamic-simple-complete (stub candidates)
2744 "Dynamically complete STUB from CANDIDATES list.
2745 This function inserts completion characters at point by completing STUB from
2746 the strings in CANDIDATES. A completions listing may be shown in a help buffer
2747 if completion is ambiguous.
2748
2749 Returns nil if no completion was inserted.
2750 Returns `sole' if completed with the only completion match.
2751 Returns `shortest' if completed with the shortest of the completion matches.
2752 Returns `partial' if completed as far as possible with the completion matches.
2753 Returns `listed' if a completion listing was shown.
2754
2755 See also `comint-dynamic-complete-filename'."
2756 (let* ((completion-ignore-case (memq system-type '(ms-dos windows-nt cygwin)))
2757 (suffix (cond ((not comint-completion-addsuffix) "")
2758 ((not (consp comint-completion-addsuffix)) " ")
2759 (t (cdr comint-completion-addsuffix))))
2760 (completions (all-completions stub candidates)))
2761 (cond ((null completions)
2762 (message "No completions of %s" stub)
2763 nil)
2764 ((= 1 (length completions)) ; Gotcha!
2765 (let ((completion (car completions)))
2766 (if (string-equal completion stub)
2767 (message "Sole completion")
2768 (insert (substring completion (length stub)))
2769 (message "Completed"))
2770 (insert suffix)
2771 'sole))
2772 (t ; There's no unique completion.
2773 (let ((completion (try-completion stub candidates)))
2774 ;; Insert the longest substring.
2775 (insert (substring completion (length stub)))
2776 (cond ((and comint-completion-recexact comint-completion-addsuffix
2777 (string-equal stub completion)
2778 (member completion completions))
2779 ;; It's not unique, but user wants shortest match.
2780 (insert suffix)
2781 (message "Completed shortest")
2782 'shortest)
2783 ((or comint-completion-autolist
2784 (string-equal stub completion))
2785 ;; It's not unique, list possible completions.
2786 (comint-dynamic-list-completions completions)
2787 'listed)
2788 (t
2789 (message "Partially completed")
2790 'partial)))))))
2791
2792
2793 (defun comint-dynamic-list-filename-completions ()
2794 "List in help buffer possible completions of the filename at point."
2795 (interactive)
2796 (let* ((completion-ignore-case (memq system-type '(ms-dos windows-nt cygwin)))
2797 ;; If we bind this, it breaks remote directory tracking in rlogin.el.
2798 ;; I think it was originally bound to solve file completion problems,
2799 ;; but subsequent changes may have made this unnecessary. sm.
2800 ;;(file-name-handler-alist nil)
2801 (filename (or (comint-match-partial-filename) ""))
2802 (filedir (file-name-directory filename))
2803 (filenondir (file-name-nondirectory filename))
2804 (directory (if filedir (comint-directory filedir) default-directory))
2805 (completions (file-name-all-completions filenondir directory)))
2806 (if (not completions)
2807 (message "No completions of %s" filename)
2808 (comint-dynamic-list-completions
2809 (mapcar 'comint-quote-filename completions)))))
2810
2811
2812 ;; This is bound locally in a *Completions* buffer to the list of
2813 ;; completions displayed, and is used to detect the case where the same
2814 ;; command is repeatedly used without the set of completions changing.
2815 (defvar comint-displayed-dynamic-completions nil)
2816
2817 (defvar comint-dynamic-list-completions-config nil)
2818
2819 (defun comint-dynamic-list-completions (completions)
2820 "List in help buffer sorted COMPLETIONS.
2821 Typing SPC flushes the help buffer."
2822 (let ((window (get-buffer-window "*Completions*")))
2823 (setq completions (sort completions 'string-lessp))
2824 (if (and (eq last-command this-command)
2825 window (window-live-p window) (window-buffer window)
2826 (buffer-name (window-buffer window))
2827 ;; The above tests are not sufficient to detect the case where we
2828 ;; should scroll, because the top-level interactive command may
2829 ;; not have displayed a completions window the last time it was
2830 ;; invoked, and there may be such a window left over from a
2831 ;; previous completion command with a different set of
2832 ;; completions. To detect that case, we also test that the set
2833 ;; of displayed completions is in fact the same as the previously
2834 ;; displayed set.
2835 (equal completions
2836 (buffer-local-value 'comint-displayed-dynamic-completions
2837 (window-buffer window))))
2838 ;; If this command was repeated, and
2839 ;; there's a fresh completion window with a live buffer,
2840 ;; and this command is repeated, scroll that window.
2841 (with-current-buffer (window-buffer window)
2842 (if (pos-visible-in-window-p (point-max) window)
2843 (set-window-start window (point-min))
2844 (save-selected-window
2845 (select-window window)
2846 (scroll-up))))
2847
2848 ;; Display a completion list for the first time.
2849 (setq comint-dynamic-list-completions-config
2850 (current-window-configuration))
2851 (with-output-to-temp-buffer "*Completions*"
2852 (display-completion-list completions))
2853 (message "Type space to flush; repeat completion command to scroll"))
2854
2855 ;; Read the next key, to process SPC.
2856 (let (key first)
2857 (if (save-excursion
2858 (set-buffer (get-buffer "*Completions*"))
2859 (set (make-local-variable
2860 'comint-displayed-dynamic-completions)
2861 completions)
2862 (setq key (read-key-sequence nil)
2863 first (aref key 0))
2864 (and (consp first) (consp (event-start first))
2865 (eq (window-buffer (posn-window (event-start first)))
2866 (get-buffer "*Completions*"))
2867 (eq (key-binding key) 'mouse-choose-completion)))
2868 ;; If the user does mouse-choose-completion with the mouse,
2869 ;; execute the command, then delete the completion window.
2870 (progn
2871 (mouse-choose-completion first)
2872 (set-window-configuration comint-dynamic-list-completions-config))
2873 (unless (eq first ?\ )
2874 (setq unread-command-events (listify-key-sequence key)))
2875 (unless (eq first ?\t)
2876 (set-window-configuration comint-dynamic-list-completions-config))))))
2877
2878 \f
2879 (defun comint-get-next-from-history ()
2880 "After fetching a line from input history, this fetches the following line.
2881 In other words, this recalls the input line after the line you recalled last.
2882 You can use this to repeat a sequence of input lines."
2883 (interactive)
2884 (if comint-save-input-ring-index
2885 (progn
2886 (setq comint-input-ring-index (1+ comint-save-input-ring-index))
2887 (comint-next-input 1))
2888 (message "No previous history command")))
2889
2890 (defun comint-accumulate ()
2891 "Accumulate a line to send as input along with more lines.
2892 This inserts a newline so that you can enter more text
2893 to be sent along with this line. Use \\[comint-send-input]
2894 to send all the accumulated input, at once.
2895 The entire accumulated text becomes one item in the input history
2896 when you send it."
2897 (interactive)
2898 (insert "\n")
2899 (set-marker comint-accum-marker (point))
2900 (if comint-input-ring-index
2901 (setq comint-save-input-ring-index
2902 (- comint-input-ring-index 1))))
2903
2904 (defun comint-goto-process-mark ()
2905 "Move point to the process mark.
2906 The process mark separates output, and input already sent,
2907 from input that has not yet been sent."
2908 (interactive)
2909 (let ((proc (or (get-buffer-process (current-buffer))
2910 (error "Current buffer has no process"))))
2911 (goto-char (process-mark proc))
2912 (when (interactive-p)
2913 (message "Point is now at the process mark"))))
2914
2915 (defun comint-bol-or-process-mark ()
2916 "Move point to beginning of line (after prompt) or to the process mark.
2917 The first time you use this command, it moves to the beginning of the line
2918 \(but after the prompt, if any). If you repeat it again immediately,
2919 it moves point to the process mark.
2920
2921 The process mark separates the process output, along with input already sent,
2922 from input that has not yet been sent. Ordinarily, the process mark
2923 is at the beginning of the current input line; but if you have
2924 used \\[comint-accumulate] to send multiple lines at once,
2925 the process mark is at the beginning of the accumulated input."
2926 (interactive)
2927 (if (not (eq last-command 'comint-bol-or-process-mark))
2928 (comint-bol nil)
2929 (comint-goto-process-mark)))
2930
2931 (defun comint-set-process-mark ()
2932 "Set the process mark at point."
2933 (interactive)
2934 (let ((proc (or (get-buffer-process (current-buffer))
2935 (error "Current buffer has no process"))))
2936 (set-marker (process-mark proc) (point))
2937 (message "Process mark set")))
2938
2939 \f
2940 ;; Author: Peter Breton <pbreton@cs.umb.edu>
2941
2942 ;; This little add-on for comint is intended to make it easy to get
2943 ;; output from currently active comint buffers into another buffer,
2944 ;; or buffers, and then go back to using the comint shell.
2945 ;;
2946 ;; My particular use is SQL interpreters; I want to be able to execute a
2947 ;; query using the process associated with a comint-buffer, and save that
2948 ;; somewhere else. Because the process might have state (for example, it
2949 ;; could be in an uncommitted transaction), just running starting a new
2950 ;; process and having it execute the query and then finish, would not
2951 ;; work. I'm sure there are other uses as well, although in many cases
2952 ;; starting a new process is the simpler, and thus preferable, approach.
2953 ;;
2954 ;; The basic implementation is as follows: comint-redirect changes the
2955 ;; preoutput filter functions (`comint-preoutput-filter-functions') to use
2956 ;; its own filter. The filter puts the output into the designated buffer,
2957 ;; or buffers, until it sees a regexp that tells it to stop (by default,
2958 ;; this is the prompt for the interpreter, `comint-prompt-regexp'). When it
2959 ;; sees the stop regexp, it restores the old filter functions, and runs
2960 ;; `comint-redirect-hook'.
2961 ;;
2962 ;; Each comint buffer may only use one redirection at a time, but any number
2963 ;; of different comint buffers may be simultaneously redirected.
2964 ;;
2965 ;; NOTE: It is EXTREMELY important that `comint-prompt-regexp' be set to the
2966 ;; correct prompt for your interpreter, or that you supply a regexp that says
2967 ;; when the redirection is finished. Otherwise, redirection will continue
2968 ;; indefinitely. The code now does a sanity check to ensure that it can find
2969 ;; a prompt in the comint buffer; however, it is still important to ensure that
2970 ;; this prompt is set correctly.
2971 ;;
2972 ;; XXX: This doesn't work so well unless `comint-prompt-regexp' is set;
2973 ;; perhaps it should prompt for a terminating string (with an
2974 ;; appropriate magic default by examining what we think is the prompt)?
2975 ;;
2976 ;; Fixme: look for appropriate fields, rather than regexp, if
2977 ;; `comint-use-prompt-regexp-instead-of-fields' is true.
2978
2979 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2980 ;; Variables
2981 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2982
2983 (defcustom comint-redirect-verbose nil
2984 "*If non-nil, print messages each time the redirection filter is invoked.
2985 Also print a message when redirection is completed."
2986 :group 'comint
2987 :type 'boolean)
2988
2989 ;; Directly analagous to comint-preoutput-filter-functions
2990 (defvar comint-redirect-filter-functions nil
2991 "List of functions to call before inserting redirected process output.
2992 Each function gets one argument, a string containing the text received
2993 from the subprocess. It should return the string to insert, perhaps
2994 the same string that was received, or perhaps a modified or transformed
2995 string.
2996
2997 The functions on the list are called sequentially, and each one is given
2998 the string returned by the previous one. The string returned by the
2999 last function is the text that is actually inserted in the redirection buffer.
3000
3001 You can use `add-hook' to add functions to this list
3002 either globally or locally.")
3003
3004 ;; Internal variables
3005
3006 (defvar comint-redirect-output-buffer nil
3007 "The buffer or list of buffers to put output into.")
3008
3009 (defvar comint-redirect-finished-regexp nil
3010 "Regular expression that determines when to stop redirection in Comint.
3011 When the redirection filter function is given output that matches this regexp,
3012 the output is inserted as usual, and redirection is completed.")
3013
3014 (defvar comint-redirect-insert-matching-regexp nil
3015 "If non-nil, the text that ends a redirection is included in it.
3016 More precisely, the text that matches `comint-redirect-finished-regexp'
3017 and therefore terminates an output redirection is inserted in the
3018 redirection target buffer, along with the preceding output.")
3019
3020 (defvar comint-redirect-echo-input nil
3021 "Non-nil means echo input in the process buffer even during redirection.")
3022
3023 (defvar comint-redirect-completed nil
3024 "Non-nil if redirection has completed in the current buffer.")
3025
3026 (defvar comint-redirect-original-mode-line-process nil
3027 "Original mode line for redirected process.")
3028
3029 (defvar comint-redirect-perform-sanity-check t
3030 "If non-nil, check that redirection is likely to complete successfully.
3031 More precisely, before starting a redirection, verify that the
3032 regular expression `comint-redirect-finished-regexp' that controls
3033 when to terminate it actually matches some text already in the process
3034 buffer. The idea is that this regular expression should match a prompt
3035 string, and that there ought to be at least one copy of your prompt string
3036 in the process buffer already.")
3037
3038 (defvar comint-redirect-original-filter-function nil
3039 "The process filter that was in place when redirection is started.
3040 When redirection is completed, the process filter is restored to
3041 this value.")
3042
3043 (defvar comint-redirect-subvert-readonly nil
3044 "Non-nil means comint-redirect can insert into otherwise-readonly buffers.
3045 The readonly status is toggled around insertion.
3046 This is useful, for instance, for insertion into Help mode buffers.
3047 You probably want to set it locally to the output buffer.")
3048
3049 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3050 ;; Functions
3051 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3052
3053 (defun comint-redirect-setup (output-buffer
3054 comint-buffer
3055 finished-regexp
3056 &optional echo-input)
3057 "Set up for output redirection.
3058 This function sets local variables that are used by `comint-redirect-filter'
3059 to perform redirection.
3060
3061 Output from COMINT-BUFFER is redirected to OUTPUT-BUFFER, until something
3062 in the output matches FINISHED-REGEXP.
3063
3064 If optional argument ECHO-INPUT is non-nil, output is echoed to the
3065 original Comint buffer.
3066
3067 This function is called by `comint-redirect-send-command-to-process',
3068 and does not normally need to be invoked by the end user or programmer."
3069 (with-current-buffer comint-buffer
3070
3071 (make-local-variable 'comint-redirect-original-mode-line-process)
3072 (setq comint-redirect-original-mode-line-process mode-line-process)
3073
3074 (make-local-variable 'comint-redirect-output-buffer)
3075 (setq comint-redirect-output-buffer output-buffer)
3076
3077 (make-local-variable 'comint-redirect-finished-regexp)
3078 (setq comint-redirect-finished-regexp finished-regexp)
3079
3080 (make-local-variable 'comint-redirect-echo-input)
3081 (setq comint-redirect-echo-input echo-input)
3082
3083 (make-local-variable 'comint-redirect-completed)
3084 (setq comint-redirect-completed nil)
3085
3086 (setq mode-line-process
3087 (if mode-line-process
3088 (list (concat (elt mode-line-process 0) " Redirection"))
3089 (list ":%s Redirection")))))
3090
3091 (defun comint-redirect-cleanup ()
3092 "End a Comint redirection. See `comint-redirect-send-command'."
3093 (interactive)
3094 ;; Restore the process filter
3095 (set-process-filter (get-buffer-process (current-buffer))
3096 comint-redirect-original-filter-function)
3097 ;; Restore the mode line
3098 (setq mode-line-process comint-redirect-original-mode-line-process)
3099 ;; Set the completed flag
3100 (setq comint-redirect-completed t))
3101
3102 ;; Because the cleanup happens as a callback, it's not easy to guarantee
3103 ;; that it really occurs.
3104 (defalias 'comint-redirect-remove-redirection 'comint-redirect-cleanup)
3105
3106 (defun comint-redirect-filter (process input-string)
3107 "Filter function which redirects output from PROCESS to a buffer or buffers.
3108 The variable `comint-redirect-output-buffer' says which buffer(s) to
3109 place output in.
3110
3111 INPUT-STRING is the input from the Comint process.
3112
3113 This function runs as a process filter, and does not need to be invoked by the
3114 end user."
3115 (and process
3116 (with-current-buffer (process-buffer process)
3117 (comint-redirect-preoutput-filter input-string)
3118 ;; If we have to echo output, give it to the original filter function
3119 (and comint-redirect-echo-input
3120 comint-redirect-original-filter-function
3121 (funcall comint-redirect-original-filter-function
3122 process input-string)))))
3123
3124
3125 (defun comint-redirect-preoutput-filter (input-string)
3126 "Comint filter function which redirects Comint output to a buffer or buffers.
3127 The variable `comint-redirect-output-buffer' says which buffer(s) to
3128 place output in.
3129
3130 INPUT-STRING is the input from the Comint process.
3131
3132 This function does not need to be invoked by the end user."
3133 (let ((output-buffer-list
3134 (if (listp comint-redirect-output-buffer)
3135 comint-redirect-output-buffer
3136 (list comint-redirect-output-buffer)))
3137 (filtered-input-string input-string))
3138
3139 ;; If there are any filter functions, give them a chance to modify the string
3140 (let ((functions comint-redirect-filter-functions))
3141 (while (and functions filtered-input-string)
3142 (if (eq (car functions) t)
3143 ;; If a local value says "use the default value too",
3144 ;; do that.
3145 (let ((functions (default-value 'comint-redirect-filter-functions)))
3146 (while (and functions filtered-input-string)
3147 (setq filtered-input-string
3148 (funcall (car functions) filtered-input-string))
3149 (setq functions (cdr functions))))
3150 (setq filtered-input-string
3151 (funcall (car functions) filtered-input-string)))
3152 (setq functions (cdr functions))))
3153
3154 ;; Clobber `comint-redirect-finished-regexp'
3155 (or comint-redirect-insert-matching-regexp
3156 (and (string-match comint-redirect-finished-regexp filtered-input-string)
3157 (setq filtered-input-string
3158 (replace-match "" nil nil filtered-input-string))))
3159
3160 ;; Send output to all registered buffers
3161 (save-excursion
3162 (dolist (buf output-buffer-list)
3163 ;; Set this buffer to the output buffer
3164 (set-buffer (get-buffer-create buf))
3165 ;; Go to the end of the buffer
3166 (goto-char (point-max))
3167 ;; Insert the output
3168 (let ((inhibit-read-only comint-redirect-subvert-readonly))
3169 (insert filtered-input-string))))
3170
3171 ;; Message
3172 (and comint-redirect-verbose
3173 (message "Redirected output to buffer(s) %s"
3174 (mapconcat 'identity output-buffer-list " ")))
3175
3176 ;; If we see the prompt, tidy up
3177 ;; We'll look for the prompt in the original string, so nobody can
3178 ;; clobber it
3179 (and (string-match comint-redirect-finished-regexp input-string)
3180 (progn
3181 (and comint-redirect-verbose
3182 (message "Redirection completed"))
3183 (comint-redirect-cleanup)
3184 (run-hooks 'comint-redirect-hook)))
3185 ;; Echo input?
3186 (if comint-redirect-echo-input
3187 filtered-input-string
3188 "")))
3189
3190 ;;;###autoload
3191 (defun comint-redirect-send-command (command output-buffer echo &optional no-display)
3192 "Send COMMAND to process in current buffer, with output to OUTPUT-BUFFER.
3193 With prefix arg ECHO, echo output in process buffer.
3194
3195 If NO-DISPLAY is non-nil, do not show the output buffer."
3196 (interactive "sCommand: \nBOutput Buffer: \nP")
3197 (let ((process (get-buffer-process (current-buffer))))
3198 (if process
3199 (comint-redirect-send-command-to-process
3200 command output-buffer (current-buffer) echo no-display)
3201 (error "No process for current buffer"))))
3202
3203 ;;;###autoload
3204 (defun comint-redirect-send-command-to-process
3205 (command output-buffer process echo &optional no-display)
3206 "Send COMMAND to PROCESS, with output to OUTPUT-BUFFER.
3207 With prefix arg, echo output in process buffer.
3208
3209 If NO-DISPLAY is non-nil, do not show the output buffer."
3210 (interactive "sCommand: \nBOutput Buffer: \nbProcess Buffer: \nP")
3211 (let* (;; The process buffer
3212 (process-buffer (if (processp process)
3213 (process-buffer process)
3214 process))
3215 (proc (get-buffer-process process-buffer)))
3216 ;; Change to the process buffer
3217 (with-current-buffer process-buffer
3218
3219 ;; Make sure there's a prompt in the current process buffer
3220 (and comint-redirect-perform-sanity-check
3221 (save-excursion
3222 (goto-char (point-max))
3223 (or (re-search-backward comint-prompt-regexp nil t)
3224 (error "No prompt found or `comint-prompt-regexp' not set properly"))))
3225
3226 ;;;;;;;;;;;;;;;;;;;;;
3227 ;; Set up for redirection
3228 ;;;;;;;;;;;;;;;;;;;;;
3229 (comint-redirect-setup
3230 ;; Output Buffer
3231 output-buffer
3232 ;; Comint Buffer
3233 (current-buffer)
3234 ;; Finished Regexp
3235 comint-prompt-regexp
3236 ;; Echo input
3237 echo)
3238
3239 ;;;;;;;;;;;;;;;;;;;;;
3240 ;; Set the filter
3241 ;;;;;;;;;;;;;;;;;;;;;
3242 ;; Save the old filter
3243 (setq comint-redirect-original-filter-function
3244 (process-filter proc))
3245 (set-process-filter proc 'comint-redirect-filter)
3246
3247 ;;;;;;;;;;;;;;;;;;;;;
3248 ;; Send the command
3249 ;;;;;;;;;;;;;;;;;;;;;
3250 (process-send-string
3251 (current-buffer)
3252 (concat command "\n"))
3253
3254 ;;;;;;;;;;;;;;;;;;;;;
3255 ;; Show the output
3256 ;;;;;;;;;;;;;;;;;;;;;
3257 (or no-display
3258 (display-buffer
3259 (get-buffer-create
3260 (if (listp output-buffer)
3261 (car output-buffer)
3262 output-buffer)))))))
3263
3264 ;;;###autoload
3265 (defun comint-redirect-results-list (command regexp regexp-group)
3266 "Send COMMAND to current process.
3267 Return a list of expressions in the output which match REGEXP.
3268 REGEXP-GROUP is the regular expression group in REGEXP to use."
3269 (comint-redirect-results-list-from-process
3270 (get-buffer-process (current-buffer))
3271 command regexp regexp-group))
3272
3273 ;;;###autoload
3274 (defun comint-redirect-results-list-from-process (process command regexp regexp-group)
3275 "Send COMMAND to PROCESS.
3276 Return a list of expressions in the output which match REGEXP.
3277 REGEXP-GROUP is the regular expression group in REGEXP to use."
3278 (let ((output-buffer " *Comint Redirect Work Buffer*")
3279 results)
3280 (save-excursion
3281 (set-buffer (get-buffer-create output-buffer))
3282 (erase-buffer)
3283 (comint-redirect-send-command-to-process command
3284 output-buffer process nil t)
3285 ;; Wait for the process to complete
3286 (set-buffer (process-buffer process))
3287 (while (null comint-redirect-completed)
3288 (accept-process-output nil 1))
3289 ;; Collect the output
3290 (set-buffer output-buffer)
3291 (goto-char (point-min))
3292 ;; Skip past the command, if it was echoed
3293 (and (looking-at command)
3294 (forward-line))
3295 (while (re-search-forward regexp nil t)
3296 (setq results
3297 (cons (buffer-substring-no-properties
3298 (match-beginning regexp-group)
3299 (match-end regexp-group))
3300 results)))
3301 results)))
3302
3303 (mapc (lambda (x)
3304 (add-to-list 'debug-ignored-errors x))
3305 '("^Not at command line$"
3306 "^Empty input ring$"
3307 "^No history$"
3308 "^Not found$" ; Too common?
3309 "^Current buffer has no process$"))
3310
3311 \f
3312 ;; Converting process modes to use comint mode
3313 ;; ===========================================================================
3314 ;; The code in the Emacs 19 distribution has all been modified to use comint
3315 ;; where needed. However, there are `third-party' packages out there that
3316 ;; still use the old shell mode. Here's a guide to conversion.
3317 ;;
3318 ;; Renaming variables
3319 ;; Most of the work is renaming variables and functions. These are the common
3320 ;; ones:
3321 ;; Local variables:
3322 ;; last-input-start comint-last-input-start
3323 ;; last-input-end comint-last-input-end
3324 ;; shell-prompt-pattern comint-prompt-regexp
3325 ;; shell-set-directory-error-hook <no equivalent>
3326 ;; Miscellaneous:
3327 ;; shell-set-directory <unnecessary>
3328 ;; shell-mode-map comint-mode-map
3329 ;; Commands:
3330 ;; shell-send-input comint-send-input
3331 ;; shell-send-eof comint-delchar-or-maybe-eof
3332 ;; kill-shell-input comint-kill-input
3333 ;; interrupt-shell-subjob comint-interrupt-subjob
3334 ;; stop-shell-subjob comint-stop-subjob
3335 ;; quit-shell-subjob comint-quit-subjob
3336 ;; kill-shell-subjob comint-kill-subjob
3337 ;; kill-output-from-shell comint-delete-output
3338 ;; show-output-from-shell comint-show-output
3339 ;; copy-last-shell-input Use comint-previous-input/comint-next-input
3340 ;;
3341 ;; SHELL-SET-DIRECTORY is gone, its functionality taken over by
3342 ;; SHELL-DIRECTORY-TRACKER, the shell mode's comint-input-filter-functions.
3343 ;; Comint mode does not provide functionality equivalent to
3344 ;; shell-set-directory-error-hook; it is gone.
3345 ;;
3346 ;; comint-last-input-start is provided for modes which want to munge
3347 ;; the buffer after input is sent, perhaps because the inferior
3348 ;; insists on echoing the input. The LAST-INPUT-START variable in
3349 ;; the old shell package was used to implement a history mechanism,
3350 ;; but you should think twice before using comint-last-input-start
3351 ;; for this; the input history ring often does the job better.
3352 ;;
3353 ;; If you are implementing some process-in-a-buffer mode, called foo-mode, do
3354 ;; *not* create the comint-mode local variables in your foo-mode function.
3355 ;; This is not modular. Instead, call comint-mode, and let *it* create the
3356 ;; necessary comint-specific local variables. Then create the
3357 ;; foo-mode-specific local variables in foo-mode. Set the buffer's keymap to
3358 ;; be foo-mode-map, and its mode to be foo-mode. Set the comint-mode hooks
3359 ;; (comint-{prompt-regexp, input-filter, input-filter-functions,
3360 ;; get-old-input) that need to be different from the defaults. Call
3361 ;; foo-mode-hook, and you're done. Don't run the comint-mode hook yourself;
3362 ;; comint-mode will take care of it. The following example, from shell.el,
3363 ;; is typical:
3364 ;;
3365 ;; (defvar shell-mode-map '())
3366 ;; (cond ((not shell-mode-map)
3367 ;; (setq shell-mode-map (copy-keymap comint-mode-map))
3368 ;; (define-key shell-mode-map "\C-c\C-f" 'shell-forward-command)
3369 ;; (define-key shell-mode-map "\C-c\C-b" 'shell-backward-command)
3370 ;; (define-key shell-mode-map "\t" 'comint-dynamic-complete)
3371 ;; (define-key shell-mode-map "\M-?"
3372 ;; 'comint-dynamic-list-filename-completions)))
3373 ;;
3374 ;; (defun shell-mode ()
3375 ;; (interactive)
3376 ;; (comint-mode)
3377 ;; (setq comint-prompt-regexp shell-prompt-pattern)
3378 ;; (setq major-mode 'shell-mode)
3379 ;; (setq mode-name "Shell")
3380 ;; (use-local-map shell-mode-map)
3381 ;; (make-local-variable 'shell-directory-stack)
3382 ;; (setq shell-directory-stack nil)
3383 ;; (add-hook 'comint-input-filter-functions 'shell-directory-tracker)
3384 ;; (run-hooks 'shell-mode-hook))
3385 ;;
3386 ;;
3387 ;; Completion for comint-mode users
3388 ;;
3389 ;; For modes that use comint-mode, comint-dynamic-complete-functions is the
3390 ;; hook to add completion functions to. Functions on this list should return
3391 ;; non-nil if completion occurs (i.e., further completion should not occur).
3392 ;; You could use comint-dynamic-simple-complete to do the bulk of the
3393 ;; completion job.
3394 \f
3395
3396 (provide 'comint)
3397
3398 ;;; arch-tag: 1793314c-09db-40be-9549-9aeae3e75164
3399 ;;; comint.el ends here