entered into RCS
[bpt/emacs.git] / lisp / comint.el
1 ;;; comint.el --- general command interpreter in a window stuff
2
3 ;;; Copyright Olin Shivers (1988).
4
5 ;; Maintainer: Olin Shivers <shivers@cs.cmu.edu>
6 ;; Version: 2.03
7 ;; Keyword: estensions, processes
8
9 ;;; This file is part of GNU Emacs.
10
11 ;;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;;; it under the terms of the GNU General Public License as published by
13 ;;; the Free Software Foundation; either version 2, or (at your option)
14 ;;; any later version.
15
16 ;;; GNU Emacs is distributed in the hope that it will be useful,
17 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;;; GNU General Public License for more details.
20
21 ;;; You should have received a copy of the GNU General Public License
22 ;;; along with GNU Emacs; see the file COPYING. If not, write to
23 ;;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
24
25 ;;; Commentary:
26
27 ;;; The changelog is at the end of this file.
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
33 ;;; This file defines a general command-interpreter-in-a-buffer package
34 ;;; (comint mode). The idea is that you can build specific process-in-a-buffer
35 ;;; modes on top of comint mode -- e.g., lisp, shell, scheme, T, soar, ....
36 ;;; This way, all these specific packages share a common base functionality,
37 ;;; and a common set of bindings, which makes them easier to use (and
38 ;;; saves code, implementation time, etc., etc.).
39
40 ;;; Several packages are already defined using comint mode:
41 ;;; - cmushell.el defines a shell-in-a-buffer mode.
42 ;;; - cmulisp.el defines a simple lisp-in-a-buffer mode.
43 ;;; Cmushell and cmulisp mode are similar to, and intended to replace,
44 ;;; their counterparts in the standard gnu emacs release (in shell.el).
45 ;;; These replacements are more featureful, robust, and uniform than the
46 ;;; released versions. The key bindings in lisp mode are also more compatible
47 ;;; with the bindings of Hemlock and Zwei (the Lisp Machine emacs).
48 ;;;
49 ;;; - The file cmuscheme.el defines a scheme-in-a-buffer mode.
50 ;;; - The file tea.el tunes scheme and inferior-scheme modes for T.
51 ;;; - The file soar.el tunes lisp and inferior-lisp modes for Soar.
52 ;;; - cmutex.el defines tex and latex modes that invoke tex, latex, bibtex,
53 ;;; previewers, and printers from within emacs.
54 ;;; - background.el allows csh-like job control inside emacs.
55 ;;; It is pretty easy to make new derived modes for other processes.
56
57 ;;; For documentation on the functionality provided by comint mode, and
58 ;;; the hooks available for customising it, see the comments below.
59 ;;; For further information on the standard derived modes (shell,
60 ;;; inferior-lisp, inferior-scheme, ...), see the relevant source files.
61
62 ;;; For hints on converting existing process modes (e.g., tex-mode,
63 ;;; background, dbx, gdb, kermit, prolog, telnet) to use comint-mode
64 ;;; instead of shell-mode, see the notes at the end of this file.
65
66 \f
67 ;;; Brief Command Documentation:
68 ;;;============================================================================
69 ;;; Comint Mode Commands: (common to all derived modes, like cmushell & cmulisp
70 ;;; mode)
71 ;;;
72 ;;; m-p comint-previous-input Cycle backwards in input history
73 ;;; m-n comint-next-input Cycle forwards
74 ;;; m-s comint-previous-similar-input Previous similar input
75 ;;; c-m-r comint-previous-input-matching Search backwards in input history
76 ;;; return comint-send-input
77 ;;; c-a comint-bol Beginning of line; skip prompt.
78 ;;; c-d comint-delchar-or-maybe-eof Delete char unless at end of buff.
79 ;;; c-c c-u comint-kill-input ^u
80 ;;; c-c c-w backward-kill-word ^w
81 ;;; c-c c-c comint-interrupt-subjob ^c
82 ;;; c-c c-z comint-stop-subjob ^z
83 ;;; c-c c-\ comint-quit-subjob ^\
84 ;;; c-c c-o comint-kill-output Delete last batch of process output
85 ;;; c-c c-r comint-show-output Show last batch of process output
86 ;;;
87 ;;; Not bound by default in comint-mode
88 ;;; send-invisible Read a line w/o echo, and send to proc
89 ;;; (These are bound in shell-mode)
90 ;;; comint-dynamic-complete Complete filename at point.
91 ;;; comint-dynamic-list-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-kill-subjob No mercy.
95 ;;; comint-continue-subjob Send CONT signal to buffer's process
96 ;;; group. Useful if you accidentally
97 ;;; suspend your process (with C-c C-z).
98 ;;;
99 ;;; These used to be bound for RMS -- I prefer the input history stuff,
100 ;;; but you might like 'em.
101 ;;; m-P comint-msearch-input Search backwards for prompt
102 ;;; m-N comint-psearch-input Search forwards for prompt
103 ;;; C-cR comint-msearch-input-matching Search backwards for prompt & string
104
105 ;;; comint-mode-hook is the comint mode hook. Basically for your keybindings.
106 ;;; comint-load-hook is run after loading in this package.
107
108 ;;; Code:
109
110 (defconst comint-version "2.03")
111
112 \f
113 ;;; Buffer Local Variables:
114 ;;;============================================================================
115 ;;; Comint mode buffer local variables:
116 ;;; comint-prompt-regexp - string comint-bol uses to match prompt.
117 ;;; comint-last-input-start - marker Handy if inferior always echos
118 ;;; comint-last-input-end - marker For comint-kill-output command
119 ;;; comint-input-ring-size - integer For the input history
120 ;;; comint-input-ring - ring mechanism
121 ;;; comint-input-ring-index - marker ...
122 ;;; comint-last-input-match - string ...
123 ;;; comint-get-old-input - function Hooks for specific
124 ;;; comint-input-sentinel - function process-in-a-buffer
125 ;;; comint-input-filter - function modes.
126 ;;; comint-input-send - function
127 ;;; comint-eol-on-send - boolean
128
129 (defvar comint-prompt-regexp "^"
130 "Regexp to recognise prompts in the inferior process.
131 Defaults to \"^\", the null string at BOL.
132
133 Good choices:
134 Canonical Lisp: \"^[^> ]*>+:? *\" (Lucid, franz, kcl, T, cscheme, oaklisp)
135 Lucid Common Lisp: \"^\\(>\\|\\(->\\)+\\) *\"
136 franz: \"^\\(->\\|<[0-9]*>:\\) *\"
137 kcl: \"^>+ *\"
138 shell: \"^[^#$%>]*[#$%>] *\"
139 T: \"^>+ *\"
140
141 This is a good thing to set in mode hooks.")
142
143 (defvar comint-input-ring-size 30
144 "Size of input history ring.")
145
146 ;;; Here are the per-interpreter hooks.
147 (defvar comint-get-old-input (function comint-get-old-input-default)
148 "Function that submits old text in comint mode.
149 This function is called when return is typed while the point is in old text.
150 It returns the text to be submitted as process input. The default is
151 comint-get-old-input-default, which grabs the current line, and strips off
152 leading text matching comint-prompt-regexp")
153
154 (defvar comint-input-sentinel (function ignore)
155 "Called on each input submitted to comint mode process by comint-send-input.
156 Thus it can, for instance, track cd/pushd/popd commands issued to the csh.")
157
158 (defvar comint-input-filter
159 (function (lambda (str) (not (string-match "\\`\\s *\\'" str))))
160 "Predicate for filtering additions to input history.
161 Only inputs answering true to this function are saved on the input
162 history list. Default is to save anything that isn't all whitespace")
163
164 (defvar comint-input-sender (function comint-simple-send)
165 "Function to actually send to PROCESS the STRING submitted by user.
166 Usually this is just 'comint-simple-send, but if your mode needs to
167 massage the input string, this is your hook. This is called from
168 the user command comint-send-input. comint-simple-send just sends
169 the string plus a newline.")
170
171 (defvar comint-eol-on-send 'T
172 "If non-nil, then jump to the end of the line before sending input to process.
173 See comint-send-input")
174
175 (defvar comint-mode-hook '()
176 "Called upon entry into comint-mode
177 This is run before the process is cranked up.")
178
179 (defvar comint-exec-hook '()
180 "Called each time a process is exec'd by comint-exec.
181 This is called after the process is cranked up. It is useful for things that
182 must be done each time a process is executed in a comint-mode buffer (e.g.,
183 (process-kill-without-query)). In contrast, the comint-mode-hook is only
184 executed once when the buffer is created.")
185
186 (defvar comint-mode-map nil)
187
188 (defvar comint-last-input-start)
189 (defvar comint-last-input-end)
190
191 (defun comint-mode ()
192 "Major mode for interacting with an inferior interpreter.
193 Interpreter name is same as buffer name, sans the asterisks.
194 Return at end of buffer sends line as input.
195 Return not at end copies rest of line to end and sends it.
196 Setting mode variable comint-eol-on-send means jump to the end of the line
197 before submitting new input.
198
199 This mode is typically customised to create inferior-lisp-mode,
200 shell-mode, etc.. This can be done by setting the hooks
201 comint-input-sentinel, comint-input-filter, comint-input-sender and
202 comint-get-old-input to appropriate functions, and the variable
203 comint-prompt-regexp to the appropriate regular expression.
204
205 An input history is maintained of size comint-input-ring-size, and
206 can be accessed with the commands comint-next-input [\\[comint-next-input]] and
207 comint-previous-input [\\[comint-previous-input]]. Commands not keybound by
208 default are send-invisible, comint-dynamic-complete, and
209 comint-list-dynamic-completions.
210
211 If you accidentally suspend your process, use \\[comint-continue-subjob]
212 to continue it.
213
214 \\{comint-mode-map}
215
216 Entry to this mode runs the hooks on comint-mode-hook"
217 (interactive)
218 (let ((old-ring (and (assq 'comint-input-ring (buffer-local-variables))
219 (boundp 'comint-input-ring)
220 comint-input-ring))
221 (old-ptyp comint-ptyp)) ; preserve across local var kill. gross.
222 ; (kill-all-local-variables) ; Removed 1/15/90 Olin
223 (setq major-mode 'comint-mode)
224 (setq mode-name "Comint")
225 (setq mode-line-process '(": %s"))
226 (use-local-map comint-mode-map)
227 (make-local-variable 'comint-last-input-start)
228 (setq comint-last-input-start (make-marker))
229 (make-local-variable 'comint-last-input-end)
230 (setq comint-last-input-end (make-marker))
231 (make-local-variable 'comint-last-input-match)
232 (setq comint-last-input-match "")
233 (make-local-variable 'comint-prompt-regexp) ; Don't set; default
234 (make-local-variable 'comint-input-ring-size) ; ...to global val.
235 (make-local-variable 'comint-input-ring)
236 (make-local-variable 'comint-input-ring-index)
237 (setq comint-input-ring-index 0)
238 (make-local-variable 'comint-get-old-input)
239 (make-local-variable 'comint-input-sentinel)
240 (make-local-variable 'comint-input-filter)
241 (make-local-variable 'comint-input-sender)
242 (make-local-variable 'comint-eol-on-send)
243 (make-local-variable 'comint-ptyp)
244 (setq comint-ptyp old-ptyp)
245 (make-local-variable 'comint-exec-hook)
246 (run-hooks 'comint-mode-hook)
247 ;Do this after the hook so the user can mung COMINT-INPUT-RING-SIZE w/his hook.
248 ;The test is so we don't lose history if we run comint-mode twice in
249 ;a buffer.
250 (setq comint-input-ring (if (ring-p old-ring) old-ring
251 (make-ring comint-input-ring-size)))))
252
253 ;;; The old-ptyp stuff above is because we have to preserve the value of
254 ;;; comint-ptyp across calls to comint-mode, in spite of the
255 ;;; kill-all-local-variables that it does. Blech. Hopefully, this will all
256 ;;; go away when a later release fixes the signalling bug.
257 ;;; (Later: I removed the kill-all-local-variables, but have left this
258 ;;; other code in place just in case I reverse myself.)
259
260 (if comint-mode-map
261 nil
262 (setq comint-mode-map (make-sparse-keymap))
263 (define-key comint-mode-map "\ep" 'comint-previous-input)
264 (define-key comint-mode-map "\en" 'comint-next-input)
265 (define-key comint-mode-map "\es" 'comint-previous-similar-input)
266 (define-key comint-mode-map "\C-m" 'comint-send-input)
267 (define-key comint-mode-map "\C-d" 'comint-delchar-or-maybe-eof)
268 (define-key comint-mode-map "\C-a" 'comint-bol)
269 (define-key comint-mode-map "\C-c\C-u" 'comint-kill-input)
270 (define-key comint-mode-map "\C-c\C-w" 'backward-kill-word)
271 (define-key comint-mode-map "\C-c\C-c" 'comint-interrupt-subjob)
272 (define-key comint-mode-map "\C-c\C-z" 'comint-stop-subjob)
273 (define-key comint-mode-map "\C-c\C-\\" 'comint-quit-subjob)
274 (define-key comint-mode-map "\C-c\C-o" 'comint-kill-output)
275 (define-key comint-mode-map "\C-\M-r" 'comint-previous-input-matching)
276 (define-key comint-mode-map "\C-c\C-r" 'comint-show-output)
277 ;;; prompt-search commands commented out 3/90 -Olin
278 ; (define-key comint-mode-map "\eP" 'comint-msearch-input)
279 ; (define-key comint-mode-map "\eN" 'comint-psearch-input)
280 ; (define-key comint-mode-map "\C-cR" 'comint-msearch-input-matching)
281 )
282
283
284 ;;; This function is used to make a full copy of the comint mode map,
285 ;;; so that client modes won't interfere with each other. This function
286 ;;; isn't necessary in emacs 18.5x, but we keep it around for 18.4x versions.
287 (defun full-copy-sparse-keymap (km)
288 "Recursively copy the sparse keymap KM"
289 (cond ((consp km)
290 (cons (full-copy-sparse-keymap (car km))
291 (full-copy-sparse-keymap (cdr km))))
292 (t km)))
293
294 (defun comint-check-proc (buffer)
295 "True if there is a process associated w/buffer BUFFER, and
296 it is alive (status RUN or STOP). BUFFER can be either a buffer or the
297 name of one"
298 (let ((proc (get-buffer-process buffer)))
299 (and proc (memq (process-status proc) '(run stop)))))
300
301 ;;; Note that this guy, unlike shell.el's make-shell, barfs if you pass it ()
302 ;;; for the second argument (program).
303 (defun make-comint (name program &optional startfile &rest switches)
304 (let ((buffer (get-buffer-create (concat "*" name "*"))))
305 ;; If no process, or nuked process, crank up a new one and put buffer in
306 ;; comint mode. Otherwise, leave buffer and existing process alone.
307 (cond ((not (comint-check-proc buffer))
308 (save-excursion
309 (set-buffer buffer)
310 (comint-mode)) ; Install local vars, mode, keymap, ...
311 (comint-exec buffer name program startfile switches)))
312 buffer))
313
314 (defvar comint-ptyp t
315 "True if communications via pty; false if by pipe. Buffer local.
316 This is to work around a bug in emacs process signalling.")
317
318 (defun comint-exec (buffer name command startfile switches)
319 "Fires up a process in buffer for comint modes.
320 Blasts any old process running in the buffer. Doesn't set the buffer mode.
321 You can use this to cheaply run a series of processes in the same comint
322 buffer. The hook comint-exec-hook is run after each exec."
323 (save-excursion
324 (set-buffer buffer)
325 (let ((proc (get-buffer-process buffer))) ; Blast any old process.
326 (if proc (delete-process proc)))
327 ;; Crank up a new process
328 (let ((proc (comint-exec-1 name buffer command switches)))
329 (make-local-variable 'comint-ptyp)
330 (setq comint-ptyp process-connection-type) ; T if pty, NIL if pipe.
331 ;; Jump to the end, and set the process mark.
332 (goto-char (point-max))
333 (set-marker (process-mark proc) (point))
334 ;; Feed it the startfile.
335 (cond (startfile
336 ;;This is guaranteed to wait long enough
337 ;;but has bad results if the comint does not prompt at all
338 ;; (while (= size (buffer-size))
339 ;; (sleep-for 1))
340 ;;I hope 1 second is enough!
341 (sleep-for 1)
342 (goto-char (point-max))
343 (insert-file-contents startfile)
344 (setq startfile (buffer-substring (point) (point-max)))
345 (delete-region (point) (point-max))
346 (comint-send-string proc startfile)))
347 (run-hooks 'comint-exec-hook)
348 buffer)))
349
350 ;;; This auxiliary function cranks up the process for comint-exec in
351 ;;; the appropriate environment.
352
353 (defun comint-exec-1 (name buffer command switches)
354 (let ((process-environment
355 (comint-update-env process-environment
356 (list (format "TERMCAP=emacs:co#%d:tc=unknown"
357 (frame-width))
358 "TERM=emacs"
359 "EMACS=t"))))
360 (apply 'start-process name buffer command switches)))
361
362
363
364 ;; This is just (append new old-env) that compresses out shadowed entries.
365 ;; It's also pretty ugly, mostly due to lisp's horrible iteration structures.
366 (defun comint-update-env (old-env new)
367 (let ((ans (reverse new))
368 (vars (mapcar (function (lambda (vv)
369 (and (string-match "^[^=]*=" vv)
370 (substring vv 0 (match-end 0)))))
371 new)))
372 (while old-env
373 (let* ((vv (car old-env)) ; vv is var=value
374 (var (and (string-match "^[^=]*=" vv)
375 (substring vv 0 (match-end 0)))))
376 (setq old-env (cdr old-env))
377 (cond ((not (and var (comint-mem var vars)))
378 (if var (setq var (cons var vars)))
379 (setq ans (cons vv ans))))))
380 (nreverse ans)))
381
382 ;;; This should be in emacs, but it isn't.
383 (defun comint-mem (item list &optional elt=)
384 "Test to see if ITEM is equal to an item in LIST.
385 Option comparison function ELT= defaults to equal."
386 (let ((elt= (or elt= (function equal)))
387 (done nil))
388 (while (and list (not done))
389 (if (funcall elt= item (car list))
390 (setq done list)
391 (setq list (cdr list))))
392 done))
393
394 \f
395 ;;; Ring Code
396 ;;;============================================================================
397 ;;; This code defines a ring data structure. A ring is a
398 ;;; (hd-index tl-index . vector)
399 ;;; list. You can insert to, remove from, and rotate a ring. When the ring
400 ;;; fills up, insertions cause the oldest elts to be quietly dropped.
401 ;;;
402 ;;; HEAD = index of the newest item on the ring.
403 ;;; TAIL = index of the oldest item on the ring.
404 ;;;
405 ;;; These functions are used by the input history mechanism, but they can
406 ;;; be used for other purposes as well.
407
408 (defun ring-p (x)
409 "T if X is a ring; NIL otherwise."
410 (and (consp x) (integerp (car x))
411 (consp (cdr x)) (integerp (car (cdr x)))
412 (vectorp (cdr (cdr x)))))
413
414 (defun make-ring (size)
415 "Make a ring that can contain SIZE elts"
416 (cons 1 (cons 0 (make-vector (+ size 1) nil))))
417
418 (defun ring-plus1 (index veclen)
419 "INDEX+1, with wraparound"
420 (let ((new-index (+ index 1)))
421 (if (= new-index veclen) 0 new-index)))
422
423 (defun ring-minus1 (index veclen)
424 "INDEX-1, with wraparound"
425 (- (if (= 0 index) veclen index) 1))
426
427 (defun ring-length (ring)
428 "Number of elts in the ring."
429 (let ((hd (car ring)) (tl (car (cdr ring))) (siz (length (cdr (cdr ring)))))
430 (let ((len (if (<= hd tl) (+ 1 (- tl hd)) (+ 1 tl (- siz hd)))))
431 (if (= len siz) 0 len))))
432
433 (defun ring-empty-p (ring)
434 (= 0 (ring-length ring)))
435
436 (defun ring-insert (ring item)
437 "Insert a new item onto the ring. If the ring is full, dump the oldest
438 item to make room."
439 (let* ((vec (cdr (cdr ring))) (len (length vec))
440 (new-hd (ring-minus1 (car ring) len)))
441 (setcar ring new-hd)
442 (aset vec new-hd item)
443 (if (ring-empty-p ring) ;overflow -- dump one off the tail.
444 (setcar (cdr ring) (ring-minus1 (car (cdr ring)) len)))))
445
446 (defun ring-remove (ring)
447 "Remove the oldest item retained on the ring."
448 (if (ring-empty-p ring) (error "Ring empty")
449 (let ((tl (car (cdr ring))) (vec (cdr (cdr ring))))
450 (setcar (cdr ring) (ring-minus1 tl (length vec)))
451 (aref vec tl))))
452
453 ;;; This isn't actually used in this package. I just threw it in in case
454 ;;; someone else wanted it. If you want rotating-ring behavior on your history
455 ;;; retrieval (analagous to kill ring behavior), this function is what you
456 ;;; need. I should write the yank-input and yank-pop-input-or-kill to go with
457 ;;; this, and not bind it to a key by default, so it would be available to
458 ;;; people who want to bind it to a key. But who would want it? Blech.
459 (defun ring-rotate (ring n)
460 (if (not (= n 0))
461 (if (ring-empty-p ring) ;Is this the right error check?
462 (error "ring empty")
463 (let ((hd (car ring)) (tl (car (cdr ring))) (vec (cdr (cdr ring))))
464 (let ((len (length vec)))
465 (while (> n 0)
466 (setq tl (ring-plus1 tl len))
467 (aset ring tl (aref ring hd))
468 (setq hd (ring-plus1 hd len))
469 (setq n (- n 1)))
470 (while (< n 0)
471 (setq hd (ring-minus1 hd len))
472 (aset vec hd (aref vec tl))
473 (setq tl (ring-minus1 tl len))
474 (setq n (- n 1))))
475 (setcar ring hd)
476 (setcar (cdr ring) tl)))))
477
478 (defun comint-mod (n m)
479 "Returns N mod M. M is positive. Answer is guaranteed to be non-negative,
480 and less than m."
481 (let ((n (% n m)))
482 (if (>= n 0) n
483 (+ n
484 (if (>= m 0) m (- m)))))) ; (abs m)
485
486 (defun ring-ref (ring index)
487 (let ((numelts (ring-length ring)))
488 (if (= numelts 0) (error "indexed empty ring")
489 (let* ((hd (car ring)) (tl (car (cdr ring))) (vec (cdr (cdr ring)))
490 (index (comint-mod index numelts))
491 (vec-index (comint-mod (+ index hd)
492 (length vec))))
493 (aref vec vec-index)))))
494
495
496 ;;; Input history retrieval commands
497 ;;; M-p -- previous input M-n -- next input
498 ;;; M-C-r -- previous input matching
499 ;;; ===========================================================================
500
501 (defun comint-previous-input (arg)
502 "Cycle backwards through input history."
503 (interactive "*p")
504 (let ((len (ring-length comint-input-ring)))
505 (cond ((<= len 0)
506 (message "Empty input ring")
507 (ding))
508 ((not (comint-after-pmark-p))
509 (message "Not after process mark")
510 (ding))
511 (t
512 (cond ((eq last-command 'comint-previous-input)
513 (delete-region (mark) (point)))
514 ((eq last-command 'comint-previous-similar-input)
515 (delete-region
516 (process-mark (get-buffer-process (current-buffer)))
517 (point)))
518 (t
519 (setq comint-input-ring-index
520 (if (> arg 0) -1
521 (if (< arg 0) 1 0)))
522 (push-mark (point))))
523 (setq comint-input-ring-index (comint-mod (+ comint-input-ring-index arg) len))
524 (message "%d" (1+ comint-input-ring-index))
525 (insert (ring-ref comint-input-ring comint-input-ring-index))
526 (setq this-command 'comint-previous-input)))))
527
528 (defun comint-next-input (arg)
529 "Cycle forwards through input history."
530 (interactive "*p")
531 (comint-previous-input (- arg)))
532
533 (defvar comint-last-input-match ""
534 "Last string searched for by comint input history search, for defaulting.
535 Buffer local variable.")
536
537 (defun comint-previous-input-matching (str)
538 "Searches backwards through input history for substring match."
539 (interactive (let* ((last-command last-command) ; preserve around r-f-m
540 (s (read-from-minibuffer
541 (format "Command substring (default %s): "
542 comint-last-input-match))))
543 (list (if (string= s "") comint-last-input-match s))))
544 ; (interactive "sCommand substring: ")
545 (setq comint-last-input-match str) ; update default
546 (if (not (eq last-command 'comint-previous-input))
547 (setq comint-input-ring-index -1))
548 (let ((str (regexp-quote str))
549 (len (ring-length comint-input-ring))
550 (n (+ comint-input-ring-index 1)))
551 (while (and (< n len) (not (string-match str (ring-ref comint-input-ring n))))
552 (setq n (+ n 1)))
553 (cond ((< n len)
554 (comint-previous-input (- n comint-input-ring-index)))
555 (t (if (eq last-command 'comint-previous-input)
556 (setq this-command 'comint-previous-input))
557 (message "Not found.")
558 (ding)))))
559
560
561 ;;; These next three commands are alternatives to the input history commands
562 ;;; -- comint-next-input, comint-previous-input and
563 ;;; comint-previous-input-matching. They search through the process buffer
564 ;;; text looking for occurrences of the prompt. Bound to M-P, M-N, and C-c R
565 ;;; (uppercase P, N, and R) for now. Try'em out. Go with what you like...
566
567 ;;; comint-msearch-input-matching prompts for a string, not a regexp.
568 ;;; This could be considered to be the wrong thing. I decided to keep it
569 ;;; simple, and not make the user worry about regexps. This, of course,
570 ;;; limits functionality.
571
572 ;;; These commands were deemed non-winning and have been commented out.
573 ;;; Feel free to re-enable them if you like. -Olin 3/91
574
575 ;(defun comint-psearch-input ()
576 ; "Search forwards for next occurrence of prompt and skip to end of line.
577 ;\(prompt is anything matching regexp comint-prompt-regexp)"
578 ; (interactive)
579 ; (if (re-search-forward comint-prompt-regexp (point-max) t)
580 ; (end-of-line)
581 ; (error "No occurrence of prompt found")))
582 ;
583 ;(defun comint-msearch-input ()
584 ; "Search backwards for previous occurrence of prompt and skip to end of line.
585 ;Search starts from beginning of current line."
586 ; (interactive)
587 ; (let ((p (save-excursion
588 ; (beginning-of-line)
589 ; (cond ((re-search-backward comint-prompt-regexp (point-min) t)
590 ; (end-of-line)
591 ; (point))
592 ; (t nil)))))
593 ; (if p (goto-char p)
594 ; (error "No occurrence of prompt found"))))
595 ;
596 ;(defun comint-msearch-input-matching (str)
597 ; "Search backwards for occurrence of prompt followed by STRING.
598 ;STRING is prompted for, and is NOT a regular expression."
599 ; (interactive (let ((s (read-from-minibuffer
600 ; (format "Command (default %s): "
601 ; comint-last-input-match))))
602 ; (list (if (string= s "") comint-last-input-match s))))
603 ;; (interactive "sCommand: ")
604 ; (setq comint-last-input-match str) ; update default
605 ; (let* ((r (concat comint-prompt-regexp (regexp-quote str)))
606 ; (p (save-excursion
607 ; (beginning-of-line)
608 ; (cond ((re-search-backward r (point-min) t)
609 ; (end-of-line)
610 ; (point))
611 ; (t nil)))))
612 ; (if p (goto-char p)
613 ; (error "No match"))))
614
615 ;;;
616 ;;; Similar input -- contributed by ccm and highly winning.
617 ;;;
618 ;;; Reenter input, removing back to the last insert point if it exists.
619 ;;;
620 (defvar comint-last-similar-string ""
621 "The string last used in a similar string search.")
622 (defun comint-previous-similar-input (arg)
623 "Reenters the last input that matches the string typed so far. If repeated
624 successively older inputs are reentered. If arg is 1, it will go back
625 in the history, if -1 it will go forward."
626 (interactive "p")
627 (if (not (comint-after-pmark-p))
628 (error "Not after process mark"))
629 (if (not (eq last-command 'comint-previous-similar-input))
630 (setq comint-input-ring-index -1
631 comint-last-similar-string
632 (buffer-substring
633 (process-mark (get-buffer-process (current-buffer)))
634 (point))))
635 (let* ((size (length comint-last-similar-string))
636 (len (ring-length comint-input-ring))
637 (n (+ comint-input-ring-index arg))
638 entry)
639 (while (and (< n len)
640 (or (< (length (setq entry (ring-ref comint-input-ring n))) size)
641 (not (equal comint-last-similar-string
642 (substring entry 0 size)))))
643 (setq n (+ n arg)))
644 (cond ((< n len)
645 (setq comint-input-ring-index n)
646 (if (eq last-command 'comint-previous-similar-input)
647 (delete-region (mark) (point)) ; repeat
648 (push-mark (point))) ; 1st time
649 (insert (substring entry size)))
650 (t (message "Not found.") (ding) (sit-for 1)))
651 (message "%d" (1+ comint-input-ring-index))))
652
653 \f
654 (defun comint-send-input ()
655 "Send input to process. After the process output mark, sends all text
656 from the process mark to point as input to the process. Before the process
657 output mark, calls value of variable comint-get-old-input to retrieve old
658 input, copies it to the process mark, and sends it. A terminal newline is
659 also inserted into the buffer and sent to the process. In either case, value
660 of variable comint-input-sentinel is called on the input before sending it.
661 The input is entered into the input history ring, if the value of variable
662 comint-input-filter returns non-nil when called on the input.
663
664 If variable comint-eol-on-send is non-nil, then point is moved to the end of
665 line before sending the input.
666
667 comint-get-old-input, comint-input-sentinel, and comint-input-filter are chosen
668 according to the command interpreter running in the buffer. E.g.,
669 If the interpreter is the csh,
670 comint-get-old-input is the default: take the current line, discard any
671 initial string matching regexp comint-prompt-regexp.
672 comint-input-sentinel monitors input for \"cd\", \"pushd\", and \"popd\"
673 commands. When it sees one, it cd's the buffer.
674 comint-input-filter is the default: returns T if the input isn't all white
675 space.
676
677 If the comint is Lucid Common Lisp,
678 comint-get-old-input snarfs the sexp ending at point.
679 comint-input-sentinel does nothing.
680 comint-input-filter returns NIL if the input matches input-filter-regexp,
681 which matches (1) all whitespace (2) :a, :c, etc.
682
683 Similarly for Soar, Scheme, etc.."
684 (interactive)
685 ;; Note that the input string does not include its terminal newline.
686 (let ((proc (get-buffer-process (current-buffer))))
687 (if (not proc) (error "Current buffer has no process")
688 (let* ((pmark (process-mark proc))
689 (pmark-val (marker-position pmark))
690 (input (if (>= (point) pmark-val)
691 (progn (if comint-eol-on-send (end-of-line))
692 (buffer-substring pmark (point)))
693 (let ((copy (funcall comint-get-old-input)))
694 (goto-char pmark)
695 (insert copy)
696 copy))))
697 (insert ?\n)
698 (if (funcall comint-input-filter input)
699 (ring-insert comint-input-ring input))
700 (funcall comint-input-sentinel input)
701 (funcall comint-input-sender proc input)
702 (set-marker comint-last-input-start pmark)
703 (set-marker comint-last-input-end (point))
704 (set-marker (process-mark proc) (point))))))
705
706 (defun comint-get-old-input-default ()
707 "Default for comint-get-old-input: take the current line, and discard
708 any initial text matching comint-prompt-regexp."
709 (save-excursion
710 (beginning-of-line)
711 (comint-skip-prompt)
712 (let ((beg (point)))
713 (end-of-line)
714 (buffer-substring beg (point)))))
715
716 (defun comint-skip-prompt ()
717 "Skip past the text matching regexp comint-prompt-regexp.
718 If this takes us past the end of the current line, don't skip at all."
719 (let ((eol (save-excursion (end-of-line) (point))))
720 (if (and (looking-at comint-prompt-regexp)
721 (<= (match-end 0) eol))
722 (goto-char (match-end 0)))))
723
724
725 (defun comint-after-pmark-p ()
726 "Is point after the process output marker?"
727 ;; Since output could come into the buffer after we looked at the point
728 ;; but before we looked at the process marker's value, we explicitly
729 ;; serialise. This is just because I don't know whether or not emacs
730 ;; services input during execution of lisp commands.
731 (let ((proc-pos (marker-position
732 (process-mark (get-buffer-process (current-buffer))))))
733 (<= proc-pos (point))))
734
735 (defun comint-simple-send (proc string)
736 "Default function for sending to PROC input STRING.
737 This just sends STRING plus a newline. To override this,
738 set the hook COMINT-INPUT-SENDER."
739 (comint-send-string proc string)
740 (comint-send-string proc "\n"))
741
742 (defun comint-bol (arg)
743 "Goes to the beginning of line, then skips past the prompt, if any.
744 If a prefix argument is given (\\[universal-argument]), then no prompt skip
745 -- go straight to column 0.
746
747 The prompt skip is done by skipping text matching the regular expression
748 comint-prompt-regexp, a buffer local variable.
749
750 If you don't like this command, reset c-a to beginning-of-line
751 in your hook, comint-mode-hook."
752 (interactive "P")
753 (beginning-of-line)
754 (if (null arg) (comint-skip-prompt)))
755
756 ;;; These two functions are for entering text you don't want echoed or
757 ;;; saved -- typically passwords to ftp, telnet, or somesuch.
758 ;;; Just enter m-x send-invisible and type in your line.
759
760 (defun comint-read-noecho (prompt &optional stars)
761 "Prompt the user with argument PROMPT. Read a single line of text
762 without echoing, and return it. Note that the keystrokes comprising
763 the text can still be recovered (temporarily) with \\[view-lossage]. This
764 may be a security bug for some applications. Optional argument STARS
765 causes input to be echoed with '*' characters on the prompt line."
766 (let ((echo-keystrokes 0)
767 (cursor-in-echo-area t)
768 (answ "")
769 tem)
770 (if (not (stringp prompt)) (setq prompt ""))
771 (message prompt)
772 (while (not(or (= (setq tem (read-char)) ?\^m)
773 (= tem ?\n)))
774 (setq answ (concat answ (char-to-string tem)))
775 (if stars (setq prompt (concat prompt "*")))
776 (message prompt))
777 (message "")
778 answ))
779
780
781 (defun send-invisible (str)
782 "Read a string without echoing, and send it to the process running
783 in the current buffer. A new-line is additionally sent. String is not
784 saved on comint input history list.
785 Security bug: your string can still be temporarily recovered with
786 \\[view-lossage]."
787 ; (interactive (list (comint-read-noecho "Enter non-echoed text")))
788 (interactive "P") ; Defeat snooping via C-x esc
789 (let ((proc (get-buffer-process (current-buffer))))
790 (if (not proc) (error "Current buffer has no process")
791 (comint-send-string proc
792 (if (stringp str) str
793 (comint-read-noecho "Non-echoed text: " t)))
794 (comint-send-string proc "\n"))))
795
796 \f
797 ;;; Low-level process communication
798
799 (defvar comint-input-chunk-size 512
800 "*Long inputs send to comint processes are broken up into chunks of this size.
801 If your process is choking on big inputs, try lowering the value.")
802
803 (defun comint-send-string (proc str)
804 "Send PROCESS the contents of STRING as input.
805 This is equivalent to process-send-string, except that long input strings
806 are broken up into chunks of size comint-input-chunk-size. Processes
807 are given a chance to output between chunks. This can help prevent processes
808 from hanging when you send them long inputs on some OS's."
809 (let* ((len (length str))
810 (i (min len comint-input-chunk-size)))
811 (process-send-string proc (substring str 0 i))
812 (while (< i len)
813 (let ((next-i (+ i comint-input-chunk-size)))
814 (accept-process-output)
815 (process-send-string proc (substring str i (min len next-i)))
816 (setq i next-i)))))
817
818 (defun comint-send-region (proc start end)
819 "Sends to PROC the region delimited by START and END.
820 This is a replacement for process-send-region that tries to keep
821 your process from hanging on long inputs. See comint-send-string."
822 (comint-send-string proc (buffer-substring start end)))
823
824 \f
825 ;;; Random input hackage
826
827 (defun comint-kill-output ()
828 "Kill all output from interpreter since last input."
829 (interactive)
830 (let ((pmark (process-mark (get-buffer-process (current-buffer)))))
831 (kill-region comint-last-input-end pmark)
832 (goto-char pmark)
833 (insert "*** output flushed ***\n")
834 (set-marker pmark (point))))
835
836 (defun comint-show-output ()
837 "Display start of this batch of interpreter output at top of window.
838 Also put cursor there."
839 (interactive)
840 (goto-char comint-last-input-end)
841 (backward-char)
842 (beginning-of-line)
843 (set-window-start (selected-window) (point))
844 (end-of-line))
845
846 (defun comint-interrupt-subjob ()
847 "Interrupt the current subjob."
848 (interactive)
849 (interrupt-process nil comint-ptyp))
850
851 (defun comint-kill-subjob ()
852 "Send kill signal to the current subjob."
853 (interactive)
854 (kill-process nil comint-ptyp))
855
856 (defun comint-quit-subjob ()
857 "Send quit signal to the current subjob."
858 (interactive)
859 (quit-process nil comint-ptyp))
860
861 (defun comint-stop-subjob ()
862 "Stop the current subjob.
863 WARNING: if there is no current subjob, you can end up suspending
864 the top-level process running in the buffer. If you accidentally do
865 this, use \\[comint-continue-subjob] to resume the process. (This
866 is not a problem with most shells, since they ignore this signal.)"
867 (interactive)
868 (stop-process nil comint-ptyp))
869
870 (defun comint-continue-subjob ()
871 "Send CONT signal to process buffer's process group.
872 Useful if you accidentally suspend the top-level process."
873 (interactive)
874 (continue-process nil comint-ptyp))
875
876 (defun comint-kill-input ()
877 "Kill all text from last stuff output by interpreter to point."
878 (interactive)
879 (let* ((pmark (process-mark (get-buffer-process (current-buffer))))
880 (p-pos (marker-position pmark)))
881 (if (> (point) p-pos)
882 (kill-region pmark (point)))))
883
884 (defun comint-delchar-or-maybe-eof (arg)
885 "Delete ARG characters forward, or send an EOF to process if at end of buffer."
886 (interactive "p")
887 (if (eobp)
888 (process-send-eof)
889 (delete-char arg)))
890
891
892
893 \f
894 ;;; Support for source-file processing commands.
895 ;;;============================================================================
896 ;;; Many command-interpreters (e.g., Lisp, Scheme, Soar) have
897 ;;; commands that process files of source text (e.g. loading or compiling
898 ;;; files). So the corresponding process-in-a-buffer modes have commands
899 ;;; for doing this (e.g., lisp-load-file). The functions below are useful
900 ;;; for defining these commands.
901 ;;;
902 ;;; Alas, these guys don't do exactly the right thing for Lisp, Scheme
903 ;;; and Soar, in that they don't know anything about file extensions.
904 ;;; So the compile/load interface gets the wrong default occasionally.
905 ;;; The load-file/compile-file default mechanism could be smarter -- it
906 ;;; doesn't know about the relationship between filename extensions and
907 ;;; whether the file is source or executable. If you compile foo.lisp
908 ;;; with compile-file, then the next load-file should use foo.bin for
909 ;;; the default, not foo.lisp. This is tricky to do right, particularly
910 ;;; because the extension for executable files varies so much (.o, .bin,
911 ;;; .lbin, .mo, .vo, .ao, ...).
912
913
914 ;;; COMINT-SOURCE-DEFAULT -- determines defaults for source-file processing
915 ;;; commands.
916 ;;;
917 ;;; COMINT-CHECK-SOURCE -- if FNAME is in a modified buffer, asks you if you
918 ;;; want to save the buffer before issuing any process requests to the command
919 ;;; interpreter.
920 ;;;
921 ;;; COMINT-GET-SOURCE -- used by the source-file processing commands to prompt
922 ;;; for the file to process.
923
924 ;;; (COMINT-SOURCE-DEFAULT previous-dir/file source-modes)
925 ;;;============================================================================
926 ;;; This function computes the defaults for the load-file and compile-file
927 ;;; commands for tea, soar, cmulisp, and cmuscheme modes.
928 ;;;
929 ;;; - PREVIOUS-DIR/FILE is a pair (directory . filename) from the last
930 ;;; source-file processing command. NIL if there hasn't been one yet.
931 ;;; - SOURCE-MODES is a list used to determine what buffers contain source
932 ;;; files: if the major mode of the buffer is in SOURCE-MODES, it's source.
933 ;;; Typically, (lisp-mode) or (scheme-mode).
934 ;;;
935 ;;; If the command is given while the cursor is inside a string, *and*
936 ;;; the string is an existing filename, *and* the filename is not a directory,
937 ;;; then the string is taken as default. This allows you to just position
938 ;;; your cursor over a string that's a filename and have it taken as default.
939 ;;;
940 ;;; If the command is given in a file buffer whose major mode is in
941 ;;; SOURCE-MODES, then the the filename is the default file, and the
942 ;;; file's directory is the default directory.
943 ;;;
944 ;;; If the buffer isn't a source file buffer (e.g., it's the process buffer),
945 ;;; then the default directory & file are what was used in the last source-file
946 ;;; processing command (i.e., PREVIOUS-DIR/FILE). If this is the first time
947 ;;; the command has been run (PREVIOUS-DIR/FILE is nil), the default directory
948 ;;; is the cwd, with no default file. (\"no default file\" = nil)
949 ;;;
950 ;;; SOURCE-REGEXP is typically going to be something like (tea-mode)
951 ;;; for T programs, (lisp-mode) for Lisp programs, (soar-mode lisp-mode)
952 ;;; for Soar programs, etc.
953 ;;;
954 ;;; The function returns a pair: (default-directory . default-file).
955
956 (defun comint-source-default (previous-dir/file source-modes)
957 (cond ((and buffer-file-name (memq major-mode source-modes))
958 (cons (file-name-directory buffer-file-name)
959 (file-name-nondirectory buffer-file-name)))
960 (previous-dir/file)
961 (t
962 (cons default-directory nil))))
963
964
965 ;;; (COMINT-CHECK-SOURCE fname)
966 ;;;============================================================================
967 ;;; Prior to loading or compiling (or otherwise processing) a file (in the CMU
968 ;;; process-in-a-buffer modes), this function can be called on the filename.
969 ;;; If the file is loaded into a buffer, and the buffer is modified, the user
970 ;;; is queried to see if he wants to save the buffer before proceeding with
971 ;;; the load or compile.
972
973 (defun comint-check-source (fname)
974 (let ((buff (get-file-buffer fname)))
975 (if (and buff
976 (buffer-modified-p buff)
977 (y-or-n-p (format "Save buffer %s first? "
978 (buffer-name buff))))
979 ;; save BUFF.
980 (let ((old-buffer (current-buffer)))
981 (set-buffer buff)
982 (save-buffer)
983 (set-buffer old-buffer)))))
984
985
986 ;;; (COMINT-GET-SOURCE prompt prev-dir/file source-modes mustmatch-p)
987 ;;;============================================================================
988 ;;; COMINT-GET-SOURCE is used to prompt for filenames in command-interpreter
989 ;;; commands that process source files (like loading or compiling a file).
990 ;;; It prompts for the filename, provides a default, if there is one,
991 ;;; and returns the result filename.
992 ;;;
993 ;;; See COMINT-SOURCE-DEFAULT for more on determining defaults.
994 ;;;
995 ;;; PROMPT is the prompt string. PREV-DIR/FILE is the (directory . file) pair
996 ;;; from the last source processing command. SOURCE-MODES is a list of major
997 ;;; modes used to determine what file buffers contain source files. (These
998 ;;; two arguments are used for determining defaults). If MUSTMATCH-P is true,
999 ;;; then the filename reader will only accept a file that exists.
1000 ;;;
1001 ;;; A typical use:
1002 ;;; (interactive (comint-get-source "Compile file: " prev-lisp-dir/file
1003 ;;; '(lisp-mode) t))
1004
1005 ;;; This is pretty stupid about strings. It decides we're in a string
1006 ;;; if there's a quote on both sides of point on the current line.
1007 (defun comint-extract-string ()
1008 "Returns string around point that starts the current line or nil."
1009 (save-excursion
1010 (let* ((point (point))
1011 (bol (progn (beginning-of-line) (point)))
1012 (eol (progn (end-of-line) (point)))
1013 (start (progn (goto-char point)
1014 (and (search-backward "\"" bol t)
1015 (1+ (point)))))
1016 (end (progn (goto-char point)
1017 (and (search-forward "\"" eol t)
1018 (1- (point))))))
1019 (and start end
1020 (buffer-substring start end)))))
1021
1022 (defun comint-get-source (prompt prev-dir/file source-modes mustmatch-p)
1023 (let* ((def (comint-source-default prev-dir/file source-modes))
1024 (stringfile (comint-extract-string))
1025 (sfile-p (and stringfile
1026 (condition-case ()
1027 (file-exists-p stringfile)
1028 (error nil))
1029 (not (file-directory-p stringfile))))
1030 (defdir (if sfile-p (file-name-directory stringfile)
1031 (car def)))
1032 (deffile (if sfile-p (file-name-nondirectory stringfile)
1033 (cdr def)))
1034 (ans (read-file-name (if deffile (format "%s(default %s) "
1035 prompt deffile)
1036 prompt)
1037 defdir
1038 (concat defdir deffile)
1039 mustmatch-p)))
1040 (list (expand-file-name (substitute-in-file-name ans)))))
1041
1042 ;;; I am somewhat divided on this string-default feature. It seems
1043 ;;; to violate the principle-of-least-astonishment, in that it makes
1044 ;;; the default harder to predict, so you actually have to look and see
1045 ;;; what the default really is before choosing it. This can trip you up.
1046 ;;; On the other hand, it can be useful, I guess. I would appreciate feedback
1047 ;;; on this.
1048 ;;; -Olin
1049
1050 \f
1051 ;;; Simple process query facility.
1052 ;;; ===========================================================================
1053 ;;; This function is for commands that want to send a query to the process
1054 ;;; and show the response to the user. For example, a command to get the
1055 ;;; arglist for a Common Lisp function might send a "(arglist 'foo)" query
1056 ;;; to an inferior Common Lisp process.
1057 ;;;
1058 ;;; This simple facility just sends strings to the inferior process and pops
1059 ;;; up a window for the process buffer so you can see what the process
1060 ;;; responds with. We don't do anything fancy like try to intercept what the
1061 ;;; process responds with and put it in a pop-up window or on the message
1062 ;;; line. We just display the buffer. Low tech. Simple. Works good.
1063
1064 ;;; Send to the inferior process PROC the string STR. Pop-up but do not select
1065 ;;; a window for the inferior process so that its response can be seen.
1066 (defun comint-proc-query (proc str)
1067 (let* ((proc-buf (process-buffer proc))
1068 (proc-mark (process-mark proc)))
1069 (display-buffer proc-buf)
1070 (set-buffer proc-buf) ; but it's not the selected *window*
1071 (let ((proc-win (get-buffer-window proc-buf))
1072 (proc-pt (marker-position proc-mark)))
1073 (comint-send-string proc str) ; send the query
1074 (accept-process-output proc) ; wait for some output
1075 ;; Try to position the proc window so you can see the answer.
1076 ;; This is bogus code. If you delete the (sit-for 0), it breaks.
1077 ;; I don't know why. Wizards invited to improve it.
1078 (if (not (pos-visible-in-window-p proc-pt proc-win))
1079 (let ((opoint (window-point proc-win)))
1080 (set-window-point proc-win proc-mark) (sit-for 0)
1081 (if (not (pos-visible-in-window-p opoint proc-win))
1082 (push-mark opoint)
1083 (set-window-point proc-win opoint)))))))
1084
1085 \f
1086 ;;; Filename completion in a buffer
1087 ;;; ===========================================================================
1088 ;;; Useful completion functions, courtesy of the Ergo group.
1089 ;;; M-<Tab> will complete the filename at the cursor as much as possible
1090 ;;; M-? will display a list of completions in the help buffer.
1091
1092 ;;; Three commands:
1093 ;;; comint-dynamic-complete Complete filename at point.
1094 ;;; comint-dynamic-list-completions List completions in help buffer.
1095 ;;; comint-replace-by-expanded-filename Expand and complete filename at point;
1096 ;;; replace with expanded/completed name.
1097
1098 ;;; These are not installed in the comint-mode keymap. But they are
1099 ;;; available for people who want them. Shell-mode installs them:
1100 ;;; (define-key cmushell-mode-map "\M-\t" 'comint-dynamic-complete)
1101 ;;; (define-key cmushell-mode-map "\M-?" 'comint-dynamic-list-completions)))
1102 ;;;
1103 ;;; Commands like this are fine things to put in load hooks if you
1104 ;;; want them present in specific modes. Example:
1105 ;;; (setq cmushell-load-hook
1106 ;;; '((lambda () (define-key lisp-mode-map "\M-\t"
1107 ;;; 'comint-replace-by-expanded-filename))))
1108 ;;;
1109
1110
1111 (defun comint-match-partial-pathname ()
1112 "Returns the filename at point or causes an error."
1113 (save-excursion
1114 (if (re-search-backward "[^~/A-Za-z0-9---_.$#,=]" nil 'move)
1115 (forward-char 1))
1116 ;; Anchor the search forwards.
1117 (if (not (looking-at "[~/A-Za-z0-9---_.$#,=]")) (error ""))
1118 (re-search-forward "[~/A-Za-z0-9---_.$#,=]+")
1119 (substitute-in-file-name
1120 (buffer-substring (match-beginning 0) (match-end 0)))))
1121
1122
1123 (defun comint-replace-by-expanded-filename ()
1124 "Replace the filename at point with an expanded, canonicalised, and
1125 completed replacement.
1126 \"Expanded\" means environment variables (e.g., $HOME) and ~'s are
1127 replaced with the corresponding directories. \"Canonicalised\" means ..
1128 and \. are removed, and the filename is made absolute instead of relative.
1129 See functions expand-file-name and substitute-in-file-name. See also
1130 comint-dynamic-complete."
1131 (interactive)
1132 (let* ((pathname (comint-match-partial-pathname))
1133 (pathdir (file-name-directory pathname))
1134 (pathnondir (file-name-nondirectory pathname))
1135 (completion (file-name-completion pathnondir
1136 (or pathdir default-directory))))
1137 (cond ((null completion)
1138 (message "No completions of %s." pathname)
1139 (ding))
1140 ((eql completion t)
1141 (message "Unique completion."))
1142 (t ; this means a string was returned.
1143 (delete-region (match-beginning 0) (match-end 0))
1144 (insert (expand-file-name (concat pathdir completion)))))))
1145
1146
1147 (defun comint-dynamic-complete ()
1148 "Dynamically complete the filename at point.
1149 This function is similar to comint-replace-by-expanded-filename, except
1150 that it won't change parts of the filename already entered in the buffer;
1151 it just adds completion characters to the end of the filename."
1152 (interactive)
1153 (let* ((pathname (comint-match-partial-pathname))
1154 (pathdir (file-name-directory pathname))
1155 (pathnondir (file-name-nondirectory pathname))
1156 (completion (file-name-completion pathnondir
1157 (or pathdir default-directory))))
1158 (cond ((null completion)
1159 (message "No completions of %s." pathname)
1160 (ding))
1161 ((eql completion t)
1162 (message "Unique completion."))
1163 (t ; this means a string was returned.
1164 (goto-char (match-end 0))
1165 (insert (substring completion (length pathnondir)))))))
1166
1167 (defun comint-dynamic-list-completions ()
1168 "List in help buffer all possible completions of the filename at point."
1169 (interactive)
1170 (let* ((pathname (comint-match-partial-pathname))
1171 (pathdir (file-name-directory pathname))
1172 (pathnondir (file-name-nondirectory pathname))
1173 (completions
1174 (file-name-all-completions pathnondir
1175 (or pathdir default-directory))))
1176 (cond ((null completions)
1177 (message "No completions of %s." pathname)
1178 (ding))
1179 (t
1180 (let ((conf (current-window-configuration)))
1181 (with-output-to-temp-buffer "*Help*"
1182 (display-completion-list completions))
1183 (sit-for 0)
1184 (message "Hit space to flush.")
1185 (let ((ch (read-char)))
1186 (if (= ch ?\ )
1187 (set-window-configuration conf)
1188 (setq unread-command-char ch))))))))
1189
1190 ; Ergo bindings
1191 ; (global-set-key "\M-\t" 'comint-replace-by-expanded-filename)
1192 ; (global-set-key "\M-?" 'comint-dynamic-list-completions)
1193 ; (define-key shell-mode-map "\M-\t" 'comint-dynamic-complete)
1194 \f
1195 ;;; Converting process modes to use comint mode
1196 ;;; ===========================================================================
1197 ;;; The code in the Emacs 19 distribution has all been modified to use comint
1198 ;;; where needed. However, there are `third-party' packages out there that
1199 ;;; still use the old shell mode. Here's a guide to conversion.
1200 ;;;
1201 ;;; Renaming variables
1202 ;;; Most of the work is renaming variables and functions. These are the common
1203 ;;; ones:
1204 ;;; Local variables:
1205 ;;; last-input-start comint-last-input-start
1206 ;;; last-input-end comint-last-input-end
1207 ;;; shell-prompt-pattern comint-prompt-regexp
1208 ;;; shell-set-directory-error-hook <no equivalent>
1209 ;;; Miscellaneous:
1210 ;;; shell-set-directory <unnecessary>
1211 ;;; shell-mode-map comint-mode-map
1212 ;;; Commands:
1213 ;;; shell-send-input comint-send-input
1214 ;;; shell-send-eof comint-delchar-or-maybe-eof
1215 ;;; kill-shell-input comint-kill-input
1216 ;;; interrupt-shell-subjob comint-interrupt-subjob
1217 ;;; stop-shell-subjob comint-stop-subjob
1218 ;;; quit-shell-subjob comint-quit-subjob
1219 ;;; kill-shell-subjob comint-kill-subjob
1220 ;;; kill-output-from-shell comint-kill-output
1221 ;;; show-output-from-shell comint-show-output
1222 ;;; copy-last-shell-input Use comint-previous-input/comint-next-input
1223 ;;;
1224 ;;; SHELL-SET-DIRECTORY is gone, its functionality taken over by
1225 ;;; SHELL-DIRECTORY-TRACKER, the shell mode's comint-input-sentinel.
1226 ;;; Comint mode does not provide functionality equivalent to
1227 ;;; shell-set-directory-error-hook; it is gone.
1228 ;;;
1229 ;;; comint-last-input-start is provided for modes which want to munge
1230 ;;; the buffer after input is sent, perhaps because the inferior
1231 ;;; insists on echoing the input. The LAST-INPUT-START variable in
1232 ;;; the old shell package was used to implement a history mechanism,
1233 ;;; but you should think twice before using comint-last-input-start
1234 ;;; for this; the input history ring often does the job better.
1235 ;;;
1236 ;;; If you are implementing some process-in-a-buffer mode, called foo-mode, do
1237 ;;; *not* create the comint-mode local variables in your foo-mode function.
1238 ;;; This is not modular. Instead, call comint-mode, and let *it* create the
1239 ;;; necessary comint-specific local variables. Then create the
1240 ;;; foo-mode-specific local variables in foo-mode. Set the buffer's keymap to
1241 ;;; be foo-mode-map, and its mode to be foo-mode. Set the comint-mode hooks
1242 ;;; (comint-prompt-regexp, comint-input-filter, comint-input-sentinel,
1243 ;;; comint-get-old-input) that need to be different from the defaults. Call
1244 ;;; foo-mode-hook, and you're done. Don't run the comint-mode hook yourself;
1245 ;;; comint-mode will take care of it. The following example, from cmushell.el,
1246 ;;; is typical:
1247 ;;;
1248 ;;; (defun shell-mode ()
1249 ;;; (interactive)
1250 ;;; (comint-mode)
1251 ;;; (setq comint-prompt-regexp shell-prompt-pattern)
1252 ;;; (setq major-mode 'shell-mode)
1253 ;;; (setq mode-name "Shell")
1254 ;;; (cond ((not shell-mode-map)
1255 ;;; (setq shell-mode-map (full-copy-sparse-keymap comint-mode-map))
1256 ;;; (define-key shell-mode-map "\M-\t" 'comint-dynamic-complete)
1257 ;;; (define-key shell-mode-map "\M-?"
1258 ;;; 'comint-dynamic-list-completions)))
1259 ;;; (use-local-map shell-mode-map)
1260 ;;; (make-local-variable 'shell-directory-stack)
1261 ;;; (setq shell-directory-stack nil)
1262 ;;; (setq comint-input-sentinel 'shell-directory-tracker)
1263 ;;; (run-hooks 'shell-mode-hook))
1264 ;;;
1265 ;;;
1266 ;;; Note that make-comint is different from make-shell in that it
1267 ;;; doesn't have a default program argument. If you give make-shell
1268 ;;; a program name of NIL, it cleverly chooses one of explicit-shell-name,
1269 ;;; $ESHELL, $SHELL, or /bin/sh. If you give make-comint a program argument
1270 ;;; of NIL, it barfs. Adjust your code accordingly...
1271 ;;;
1272 \f
1273 ;;; Do the user's customisation...
1274
1275 (defvar comint-load-hook nil
1276 "This hook is run when comint is loaded in.
1277 This is a good place to put keybindings.")
1278
1279 (run-hooks 'comint-load-hook)
1280
1281 ;;; Change log:
1282 ;;; 9/12/89
1283 ;;; - Souped up the filename expansion procedures.
1284 ;;; Doc strings are much clearer and more detailed.
1285 ;;; Fixed a bug where doing a filename completion when the point
1286 ;;; was in the middle of the filename instead of at the end would lose.
1287 ;;;
1288 ;;; 2/17/90
1289 ;;; - Souped up the command history stuff so that text inserted
1290 ;;; by comint-previous-input-matching is removed by following
1291 ;;; command history recalls. comint-next/previous-input-matching
1292 ;;; is now much more smoothly integrated w/the command history stuff.
1293 ;;; - Added comint-eol-on-send flag and comint-input-sender hook.
1294 ;;; Comint-input-sender based on code contributed by Jeff Peck
1295 ;;; (peck@sun.com).
1296 ;;;
1297 ;;; 3/13/90 ccm@cmu.cs.edu
1298 ;;; - Added comint-previous-similar-input for looking up similar inputs.
1299 ;;; - Added comint-send-and-get-output to allow snarfing input from
1300 ;;; buffer.
1301 ;;; - Added the ability to pick up a source file by positioning over
1302 ;;; a string in comint-get-source.
1303 ;;; - Added add-hook to make it a little easier for the user to use
1304 ;;; multiple hooks.
1305 ;;;
1306 ;;; 5/22/90 shivers
1307 ;;; - Moved Chris' multiplexed ipc stuff to comint-ipc.el.
1308 ;;; - Altered Chris' comint-get-source string feature. The string
1309 ;;; is only offered as a default if it names an existing file.
1310 ;;; - Changed comint-exec to directly crank up the process, instead
1311 ;;; of calling the env program. This made background.el happy.
1312 ;;; - Added new buffer-local var comint-ptyp. The problem is that
1313 ;;; the signalling functions don't work as advertised. If you are
1314 ;;; communicating via pipes, the CURRENT-GROUP arg is supposed to
1315 ;;; be ignored, but, unfortunately it seems to be the case that you
1316 ;;; must pass a NIL for this arg in the pipe case. COMINT-PTYP
1317 ;;; is a flag that tells whether the process is communicating
1318 ;;; via pipes or a pty. The comint signalling functions use it
1319 ;;; to determine the necessary CURRENT-GROUP arg value. The bug
1320 ;;; has been reported to the Gnu folks.
1321 ;;; - comint-dynamic-complete flushes the help window if you hit space
1322 ;;; after you execute it.
1323 ;;; - Added functions comint-send-string, comint-send-region and var
1324 ;;; comint-input-chunk-size. comint-send-string tries to prevent processes
1325 ;;; from hanging when you send them long strings by breaking them into
1326 ;;; chunks and allowing process output between chunks. I got the idea from
1327 ;;; Eero Simoncelli's Common Lisp package. Note that using
1328 ;;; comint-send-string means that the process buffer's contents can change
1329 ;;; during a call! If you depend on process output only happening between
1330 ;;; toplevel commands, this could be a problem. In such a case, use
1331 ;;; process-send-string instead. If this is a problem for people, I'd like
1332 ;;; to hear about it.
1333 ;;; - Added comint-proc-query as a simple mechanism for commands that
1334 ;;; want to query an inferior process and display its response. For a
1335 ;;; typical use, see lisp-show-arglist in cmulisp.el.
1336 ;;; - Added constant comint-version, which is now "2.01".
1337 ;;;
1338 ;;; 6/14/90 shivers
1339 ;;; - Had comint-update-env defined twice. Removed extra copy. Also
1340 ;;; renamed mem to be comint-mem, for modularity. The duplication
1341 ;;; was reported by Michael Meissner.
1342 ;;; 6/16/90 shivers
1343 ;;; - Emacs has two different mechanisms for maintaining the process
1344 ;;; environment, determined at compile time by the MAINTAIN-ENVIRONMENT
1345 ;;; #define. One uses the process-environment global variable, and
1346 ;;; one uses a getenv/setenv interface. comint-exec assumed the
1347 ;;; process-environment interface; it has been generalised (with
1348 ;;; comint-exec-1) to handle both cases. Pretty bogus. We could,
1349 ;;; of course, skip all this and just use the etc/env program to
1350 ;;; handle the environment tweaking, but that obscures process
1351 ;;; queries that other modules (like background.el) depend on. etc/env
1352 ;;; is also fairly bogus. This bug, and some of the fix code was
1353 ;;; reported by Dan Pierson.
1354 ;;;
1355 ;;; 9/5/90 shivers
1356 ;;; - Changed make-variable-buffer-local's to make-local-variable's.
1357 ;;; This leaves non-comint-mode buffers alone. Stephane Payrard
1358 ;;; reported the sloppy useage.
1359 ;;; - You can now go from comint-previous-similar-input to
1360 ;;; comint-previous-input with no problem.
1361 ;;;
1362 ;;; 12/21/90 shivers
1363 ;;; - Added a condition-case to comint-get-source. Bogus strings
1364 ;;; beginning with ~ were making the file-exists-p barf.
1365 ;;; - Added "=" to the set of chars recognised by file completion
1366 ;;; as constituting a filename.
1367 ;;;
1368 ;;; 1/90 shivers
1369 ;;; These changes comprise release 2.02:
1370 ;;; - Removed the kill-all-local-variables in comint-mode. This
1371 ;;; made it impossible for client modes to set things before calling
1372 ;;; comint-mode. (In particular, it messed up ilisp.el) In general,
1373 ;;; the client mode should be responsible for a k-a-l-v's.
1374 ;;; - Fixed comint-match-partial-pathname so that it works in
1375 ;;; more cases: if the filename begins at the start-of-buffer;
1376 ;;; if point is on the first char of the filename. Just a question
1377 ;;; of getting the tricky bits right.
1378 ;;; - Added a hook, comint-exec-hook that is run each time a process
1379 ;;; is cranked up. Useful for things like process-kill-without-query.
1380 ;;;
1381 ;;; These two were pointed out by tale:
1382 ;;; - Improved the doc string in comint-send-input a little bit.
1383 ;;; - Tweaked make-comint to check process status with comint-check-proc
1384 ;;; instead of equivalent inline code.
1385 ;;;
1386 ;;; - Prompt-search history commands have been commented out. I never
1387 ;;; liked them; I don't think anyone used them.
1388 ;;; - Made comint-exec-hook a local var, as it should have been.
1389 ;;; (This way, for instance, you can have cmushell procs kill-w/o-query,
1390 ;;; but let Scheme procs be default.)
1391 ;;;
1392 ;;; 7/91 Shivers
1393 ;;; - Souped up comint-read-noecho with an optional argument, STARS.
1394 ;;; Suggested by mjlx@EAGLE.CNSF.CORNELL.EDU.
1395 ;;; - Moved comint-previous-input-matching from C-c r to C-M-r.
1396 ;;; C-c <letter> bindings are reserved for the user.
1397 ;;; These bindings were done by Jim Blandy.
1398 ;;; These changes comprise version 2.03.
1399
1400 (provide 'comint)
1401
1402 ;;; comint.el ends here