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