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