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