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