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