(ad-start-advice-on-load): Initialize to t.
[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)
41752cf1
JB
131 Lucid Common Lisp: \"^\\\\(>\\\\|\\\\(->\\\\)+\\\\) *\"
132 franz: \"^\\\\(->\\\\|<[0-9]*>:\\\\) *\"
49116ac0
JB
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 142(defvar comint-process-echoes nil
cccd719e 143 "*If non-nil, assume that the subprocess echoes any input.
8ae3bc9f
NF
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
cccd719e
RS
154`comint-get-old-input-default', which grabs the current line, and strips off
155leading text matching `comint-prompt-regexp'.")
be9b65ac 156
49116ac0 157(defvar comint-input-sentinel (function ignore)
cccd719e 158 "Called on each input submitted to comint mode process by `comint-send-input'.
49116ac0 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
cccd719e 165history list. Default is to save anything that isn't all whitespace")
49116ac0
JB
166
167(defvar comint-input-sender (function comint-simple-send)
168 "Function to actually send to PROCESS the STRING submitted by user.
cccd719e
RS
169Usually this is just `comint-simple-send', but if your mode needs to
170massage the input string, put a different function here.
171`comint-simple-send' just sends the string plus a newline.
172This is called from the user command `comint-send-input'.")
49116ac0 173
cccd719e
RS
174(defvar comint-eol-on-send t
175 "*Non-nil means go 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 '()
cccd719e 183 "Called each time a process is exec'd by `comint-exec'.
49116ac0 184This is called after the process is cranked up. It is useful for things that
cccd719e
RS
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
49116ac0
JB
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.
cccd719e 193This is to work around a bug in Emacs process signalling.")
c9706a52 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.
cccd719e 211Setting variable `comint-eol-on-send' means jump to the end of the line
49116ac0 212before submitting new input.
be9b65ac 213
cccd719e
RS
214This mode is typically customised to create Inferior Lisp mode,
215Shell mode, etc. This can be done by setting the hooks
216`comint-input-sentinel', `comint-input-filter', `comint-input-sender' and
217`comint-get-old-input' to appropriate functions, and the variable
218`comint-prompt-regexp' to the appropriate regular expression.
be9b65ac 219
cccd719e
RS
220An input history is maintained of size `comint-input-ring-size', and
221can be accessed with the commands \\[comint-next-input] and \\[comint-previous-input].
222Commands with no default key bindings include `send-invisible',
223`comint-dynamic-complete', and `comint-list-dynamic-completions'.
be9b65ac
DL
224
225If you accidentally suspend your process, use \\[comint-continue-subjob]
226to continue it.
227
228\\{comint-mode-map}
229
49116ac0 230Entry to this mode runs the hooks on comint-mode-hook"
be9b65ac 231 (interactive)
c9706a52
RS
232 ;; Do not remove this. All major modes must do this.
233 (kill-all-local-variables)
49116ac0
JB
234 (setq major-mode 'comint-mode)
235 (setq mode-name "Comint")
236 (setq mode-line-process '(": %s"))
237 (use-local-map comint-mode-map)
87ef29fd
JB
238 (make-local-variable 'comint-last-input-start)
239 (setq comint-last-input-start (make-marker))
49116ac0
JB
240 (make-local-variable 'comint-last-input-end)
241 (setq comint-last-input-end (make-marker))
0c5c1f34
RS
242;;; (make-local-variable 'comint-last-input-match)
243;;; (setq comint-last-input-match "")
49116ac0 244 (make-local-variable 'comint-prompt-regexp) ; Don't set; default
e6dfdce5
RS
245 (make-local-variable 'comint-input-ring-size) ; ...to global val.
246 (make-local-variable 'comint-input-ring)
247 (make-local-variable 'comint-input-ring-index)
248 (setq comint-input-ring-index 0)
49116ac0
JB
249 (make-local-variable 'comint-get-old-input)
250 (make-local-variable 'comint-input-sentinel)
251 (make-local-variable 'comint-input-filter)
252 (make-local-variable 'comint-input-sender)
253 (make-local-variable 'comint-eol-on-send)
254 (make-local-variable 'comint-ptyp)
87ef29fd 255 (make-local-variable 'comint-exec-hook)
8ae3bc9f 256 (make-local-variable 'comint-process-echoes)
49116ac0 257 (run-hooks 'comint-mode-hook)
c9706a52
RS
258 (or comint-input-ring
259 (setq comint-input-ring (make-ring comint-input-ring-size))))
be9b65ac 260
49116ac0
JB
261(if comint-mode-map
262 nil
263 (setq comint-mode-map (make-sparse-keymap))
264 (define-key comint-mode-map "\ep" 'comint-previous-input)
265 (define-key comint-mode-map "\en" 'comint-next-input)
0c5c1f34
RS
266 (define-key comint-mode-map "\er" 'comint-previous-matching-input)
267 (define-key comint-mode-map "\es" 'comint-next-matching-input)
49116ac0
JB
268 (define-key comint-mode-map "\C-m" 'comint-send-input)
269 (define-key comint-mode-map "\C-d" 'comint-delchar-or-maybe-eof)
270 (define-key comint-mode-map "\C-a" 'comint-bol)
271 (define-key comint-mode-map "\C-c\C-u" 'comint-kill-input)
272 (define-key comint-mode-map "\C-c\C-w" 'backward-kill-word)
273 (define-key comint-mode-map "\C-c\C-c" 'comint-interrupt-subjob)
274 (define-key comint-mode-map "\C-c\C-z" 'comint-stop-subjob)
275 (define-key comint-mode-map "\C-c\C-\\" 'comint-quit-subjob)
276 (define-key comint-mode-map "\C-c\C-o" 'comint-kill-output)
49116ac0
JB
277 (define-key comint-mode-map "\C-c\C-r" 'comint-show-output)
278 ;;; prompt-search commands commented out 3/90 -Olin
279; (define-key comint-mode-map "\eP" 'comint-msearch-input)
280; (define-key comint-mode-map "\eN" 'comint-psearch-input)
281; (define-key comint-mode-map "\C-cR" 'comint-msearch-input-matching)
282 )
283
284
285;;; This function is used to make a full copy of the comint mode map,
286;;; so that client modes won't interfere with each other. This function
287;;; isn't necessary in emacs 18.5x, but we keep it around for 18.4x versions.
288(defun full-copy-sparse-keymap (km)
cccd719e 289 "Recursively copy the sparse keymap KM."
49116ac0
JB
290 (cond ((consp km)
291 (cons (full-copy-sparse-keymap (car km))
292 (full-copy-sparse-keymap (cdr km))))
293 (t km)))
294
295(defun comint-check-proc (buffer)
cccd719e
RS
296 "True if there is a living process associated w/buffer BUFFER.
297Living means the status is `run' or `stop'.
298BUFFER can be either a buffer or the name of one."
49116ac0
JB
299 (let ((proc (get-buffer-process buffer)))
300 (and proc (memq (process-status proc) '(run stop)))))
be9b65ac
DL
301
302;;; Note that this guy, unlike shell.el's make-shell, barfs if you pass it ()
303;;; for the second argument (program).
eb6e9899 304;;;###autoload
be9b65ac 305(defun make-comint (name program &optional startfile &rest switches)
14a08856
RM
306 "Make a comint process NAME in a buffer, running PROGRAM.
307The name of the buffer is made by surrounding NAME with `*'s.
308If there is already a running process in that buffer, it is not restarted.
309Optional third arg STARTFILE is the name of a file to send the contents of to
310the process. Any more args are arguments to PROGRAM."
49116ac0 311 (let ((buffer (get-buffer-create (concat "*" name "*"))))
be9b65ac 312 ;; If no process, or nuked process, crank up a new one and put buffer in
14a08856 313 ;; comint mode. Otherwise, leave buffer and existing process alone.
1ff6cf10 314 (cond ((not (comint-check-proc buffer))
49116ac0 315 (save-excursion
be9b65ac
DL
316 (set-buffer buffer)
317 (comint-mode)) ; Install local vars, mode, keymap, ...
318 (comint-exec buffer name program startfile switches)))
319 buffer))
320
321(defun comint-exec (buffer name command startfile switches)
cccd719e 322 "Start up a process in buffer BUFFER for comint modes.
14a08856 323Blasts any old process running in the buffer. Doesn't set the buffer mode.
49116ac0 324You can use this to cheaply run a series of processes in the same comint
cccd719e 325buffer. The hook `comint-exec-hook' is run after each exec."
be9b65ac
DL
326 (save-excursion
327 (set-buffer buffer)
328 (let ((proc (get-buffer-process buffer))) ; Blast any old process.
329 (if proc (delete-process proc)))
330 ;; Crank up a new process
331 (let ((proc (comint-exec-1 name buffer command switches)))
5511e411 332 (set-process-filter proc 'comint-filter)
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 594of variable `comint-get-old-input' to retrieve old input, copies it to the
cccd719e 595process mark, and sends it. If variable `comint-process-echoes' is nil,
8ae3bc9f 596a terminal newline is also inserted into the buffer and sent to the process
cccd719e
RS
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).
8ae3bc9f 599
cccd719e
RS
600The value of variable `comint-input-sentinel' is called on the input
601before sending it. The input is entered into the input history ring,
602if the value of variable `comint-input-filter' returns non-nil when
603called on the input.
604
605If variable `comint-eol-on-send' is non-nil, then point is moved to the
8ae3bc9f
NF
606end of line before sending the input.
607
608`comint-get-old-input', `comint-input-sentinel', and `comint-input-filter'
609are chosen according to the command interpreter running in the buffer. E.g.,
be9b65ac 610If the interpreter is the csh,
49116ac0
JB
611 comint-get-old-input is the default: take the current line, discard any
612 initial string matching regexp comint-prompt-regexp.
613 comint-input-sentinel monitors input for \"cd\", \"pushd\", and \"popd\"
614 commands. When it sees one, it cd's the buffer.
615 comint-input-filter is the default: returns T if the input isn't all white
616 space.
be9b65ac
DL
617
618If the comint is Lucid Common Lisp,
49116ac0
JB
619 comint-get-old-input snarfs the sexp ending at point.
620 comint-input-sentinel does nothing.
621 comint-input-filter returns NIL if the input matches input-filter-regexp,
be9b65ac
DL
622 which matches (1) all whitespace (2) :a, :c, etc.
623
1586b965 624Similarly for Soar, Scheme, etc."
be9b65ac
DL
625 (interactive)
626 ;; Note that the input string does not include its terminal newline.
49116ac0
JB
627 (let ((proc (get-buffer-process (current-buffer))))
628 (if (not proc) (error "Current buffer has no process")
629 (let* ((pmark (process-mark proc))
630 (pmark-val (marker-position pmark))
631 (input (if (>= (point) pmark-val)
632 (progn (if comint-eol-on-send (end-of-line))
633 (buffer-substring pmark (point)))
87ef29fd
JB
634 (let ((copy (funcall comint-get-old-input)))
635 (goto-char pmark)
636 (insert copy)
637 copy))))
8ae3bc9f
NF
638 (if comint-process-echoes
639 (delete-region pmark (point))
640 (insert ?\n))
e6dfdce5
RS
641 (if (funcall comint-input-filter input)
642 (ring-insert comint-input-ring input))
49116ac0
JB
643 (funcall comint-input-sentinel input)
644 (funcall comint-input-sender proc input)
1586b965 645 (setq comint-input-ring-index nil)
87ef29fd
JB
646 (set-marker comint-last-input-start pmark)
647 (set-marker comint-last-input-end (point))
648 (set-marker (process-mark proc) (point))))))
be9b65ac 649
5511e411
RS
650;; The sole purpose of using this filter for comint processes
651;; is to keep comint-last-input-end from moving forward
652;; when output is inserted.
653(defun comint-filter (process string)
654 (let ((obuf (current-buffer))
5511e411
RS
655 opoint obeg oend)
656 (set-buffer (process-buffer process))
657 (setq opoint (point))
658 (setq obeg (point-min))
659 (setq oend (point-max))
5511e411
RS
660 (let ((buffer-read-only nil)
661 (nchars (length string)))
662 (widen)
663 (goto-char (process-mark process))
c64cc467
RS
664 (if (<= (point) opoint)
665 (setq opoint (+ opoint nchars)))
5511e411
RS
666 ;; Insert after old_begv, but before old_zv.
667 (if (< (point) obeg)
668 (setq obeg (+ obeg nchars)))
669 (if (<= (point) oend)
670 (setq oend (+ oend nchars)))
671
672 (insert-before-markers string)
cccd719e
RS
673 ;; Don't insert initial prompt outside the top of the window.
674 (if (= (window-start (selected-window)) (point))
675 (set-window-start (selected-window) (- (point) (length string))))
676
5511e411
RS
677 (and comint-last-input-end
678 (marker-buffer comint-last-input-end)
679 (= (point) comint-last-input-end)
680 (set-marker comint-last-input-end
681 (- comint-last-input-end nchars)))
682 (set-marker (process-mark process) (point) nil)
afa721bf 683 (force-mode-line-update))
5511e411 684
afa721bf
RS
685 (narrow-to-region obeg oend)
686 (goto-char opoint)
687 (set-buffer obuf)))
5511e411 688
be9b65ac 689(defun comint-get-old-input-default ()
cccd719e 690 "Default for `comint-get-old-input'.
c2ced5d8 691Take the current line, and discard any initial text matching
cccd719e 692`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 ()
cccd719e 701 "Skip past the text matching regexp `comint-prompt-regexp'.
49116ac0
JB
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,
cccd719e 722set the hook `comint-input-sender'."
49116ac0
JB
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 731The prompt skip is done by skipping text matching the regular expression
cccd719e 732`comint-prompt-regexp', a buffer local variable.
49116ac0 733
cccd719e
RS
734If you don't like this command, bind 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
87ef29fd 744(defun comint-read-noecho (prompt &optional stars)
4be9a25f
NF
745 "Read a single line of text from user without echoing, and return it.
746Prompt with argument PROMPT, a string. Optional argument STARS causes
747input to be echoed with '*' characters on the prompt line. Input ends with
748RET, LFD, or ESC. DEL or C-h rubs out. C-u kills line. C-g aborts (if
cccd719e
RS
749`inhibit-quit' is set because e.g. this function was called from a process
750filter and C-g is pressed, this function returns nil rather than a string).
4be9a25f
NF
751
752Note that the keystrokes comprising the text can still be recovered
cccd719e 753\(temporarily) with \\[view-lossage]. This may be a security bug for some
4be9a25f
NF
754applications."
755 (let ((ans "")
756 (c 0)
757 (echo-keystrokes 0)
87ef29fd 758 (cursor-in-echo-area t)
4be9a25f
NF
759 (done nil))
760 (while (not done)
761 (if stars
762 (message "%s%s" prompt (make-string (length ans) ?*))
763 (message prompt))
764 (setq c (read-char))
765 (cond ((= c ?\C-g)
766 ;; This function may get called from a process filter, where
767 ;; inhibit-quit is set. In later versions of emacs read-char
768 ;; may clear quit-flag itself and return C-g. That would make
769 ;; it impossible to quit this loop in a simple way, so
770 ;; re-enable it here (for backward-compatibility the check for
771 ;; quit-flag below would still be necessary, so this is seems
772 ;; like the simplest way to do things).
773 (setq quit-flag t
774 done t))
775 ((or (= c ?\r) (= c ?\n) (= c ?\e))
776 (setq done t))
777 ((= c ?\C-u)
778 (setq ans ""))
779 ((and (/= c ?\b) (/= c ?\177))
780 (setq ans (concat ans (char-to-string c))))
781 ((> (length ans) 0)
782 (setq ans (substring ans 0 -1)))))
783 (if quit-flag
784 ;; Emulate a true quit, except that we have to return a value.
785 (prog1
786 (setq quit-flag nil)
787 (message "Quit")
788 (beep t))
789 (message "")
790 ans)))
87ef29fd 791
be9b65ac 792(defun send-invisible (str)
c2ced5d8 793 "Read a string without echoing.
cccd719e
RS
794Then send it to the process running in the current buffer. A new-line
795is additionally sent. String is not saved on comint input history list.
49116ac0
JB
796Security bug: your string can still be temporarily recovered with
797\\[view-lossage]."
be9b65ac
DL
798; (interactive (list (comint-read-noecho "Enter non-echoed text")))
799 (interactive "P") ; Defeat snooping via C-x esc
800 (let ((proc (get-buffer-process (current-buffer))))
801 (if (not proc) (error "Current buffer has no process")
802 (comint-send-string proc
803 (if (stringp str) str
87ef29fd 804 (comint-read-noecho "Non-echoed text: " t)))
be9b65ac
DL
805 (comint-send-string proc "\n"))))
806
807\f
808;;; Low-level process communication
809
810(defvar comint-input-chunk-size 512
cccd719e 811 "*Long inputs are sent to comint processes in chunks of this size.
be9b65ac
DL
812If your process is choking on big inputs, try lowering the value.")
813
814(defun comint-send-string (proc str)
815 "Send PROCESS the contents of STRING as input.
cccd719e
RS
816This is equivalent to `process-send-string', except that long input strings
817are broken up into chunks of size `comint-input-chunk-size'. Processes
818are given a chance to output between chunks. This can help prevent processes
be9b65ac
DL
819from hanging when you send them long inputs on some OS's."
820 (let* ((len (length str))
821 (i (min len comint-input-chunk-size)))
822 (process-send-string proc (substring str 0 i))
823 (while (< i len)
824 (let ((next-i (+ i comint-input-chunk-size)))
825 (accept-process-output)
826 (process-send-string proc (substring str i (min len next-i)))
827 (setq i next-i)))))
828
829(defun comint-send-region (proc start end)
830 "Sends to PROC the region delimited by START and END.
cccd719e
RS
831This is a replacement for `process-send-region' that tries to keep
832your process from hanging on long inputs. See `comint-send-string'."
be9b65ac
DL
833 (comint-send-string proc (buffer-substring start end)))
834
835\f
836;;; Random input hackage
837
49116ac0 838(defun comint-kill-output ()
be9b65ac
DL
839 "Kill all output from interpreter since last input."
840 (interactive)
49116ac0
JB
841 (let ((pmark (process-mark (get-buffer-process (current-buffer)))))
842 (kill-region comint-last-input-end pmark)
843 (goto-char pmark)
844 (insert "*** output flushed ***\n")
845 (set-marker pmark (point))))
be9b65ac
DL
846
847(defun comint-show-output ()
49116ac0
JB
848 "Display start of this batch of interpreter output at top of window.
849Also put cursor there."
be9b65ac
DL
850 (interactive)
851 (goto-char comint-last-input-end)
49116ac0 852 (backward-char)
be9b65ac
DL
853 (beginning-of-line)
854 (set-window-start (selected-window) (point))
49116ac0 855 (end-of-line))
be9b65ac
DL
856
857(defun comint-interrupt-subjob ()
49116ac0 858 "Interrupt the current subjob."
be9b65ac 859 (interactive)
49116ac0 860 (interrupt-process nil comint-ptyp))
be9b65ac
DL
861
862(defun comint-kill-subjob ()
49116ac0 863 "Send kill signal to the current subjob."
be9b65ac 864 (interactive)
49116ac0 865 (kill-process nil comint-ptyp))
be9b65ac
DL
866
867(defun comint-quit-subjob ()
49116ac0 868 "Send quit signal to the current subjob."
be9b65ac 869 (interactive)
49116ac0 870 (quit-process nil comint-ptyp))
be9b65ac
DL
871
872(defun comint-stop-subjob ()
49116ac0 873 "Stop the current subjob.
be9b65ac 874WARNING: if there is no current subjob, you can end up suspending
49116ac0
JB
875the top-level process running in the buffer. If you accidentally do
876this, use \\[comint-continue-subjob] to resume the process. (This
877is not a problem with most shells, since they ignore this signal.)"
be9b65ac 878 (interactive)
49116ac0 879 (stop-process nil comint-ptyp))
be9b65ac
DL
880
881(defun comint-continue-subjob ()
49116ac0 882 "Send CONT signal to process buffer's process group.
be9b65ac
DL
883Useful if you accidentally suspend the top-level process."
884 (interactive)
49116ac0 885 (continue-process nil comint-ptyp))
be9b65ac
DL
886
887(defun comint-kill-input ()
49116ac0 888 "Kill all text from last stuff output by interpreter to point."
be9b65ac 889 (interactive)
49116ac0
JB
890 (let* ((pmark (process-mark (get-buffer-process (current-buffer))))
891 (p-pos (marker-position pmark)))
892 (if (> (point) p-pos)
893 (kill-region pmark (point)))))
be9b65ac
DL
894
895(defun comint-delchar-or-maybe-eof (arg)
896 "Delete ARG characters forward, or send an EOF to process if at end of buffer."
897 (interactive "p")
898 (if (eobp)
899 (process-send-eof)
49116ac0
JB
900 (delete-char arg)))
901
902
903
be9b65ac
DL
904\f
905;;; Support for source-file processing commands.
906;;;============================================================================
907;;; Many command-interpreters (e.g., Lisp, Scheme, Soar) have
908;;; commands that process files of source text (e.g. loading or compiling
909;;; files). So the corresponding process-in-a-buffer modes have commands
910;;; for doing this (e.g., lisp-load-file). The functions below are useful
911;;; for defining these commands.
912;;;
913;;; Alas, these guys don't do exactly the right thing for Lisp, Scheme
914;;; and Soar, in that they don't know anything about file extensions.
915;;; So the compile/load interface gets the wrong default occasionally.
916;;; The load-file/compile-file default mechanism could be smarter -- it
917;;; doesn't know about the relationship between filename extensions and
918;;; whether the file is source or executable. If you compile foo.lisp
919;;; with compile-file, then the next load-file should use foo.bin for
920;;; the default, not foo.lisp. This is tricky to do right, particularly
921;;; because the extension for executable files varies so much (.o, .bin,
922;;; .lbin, .mo, .vo, .ao, ...).
923
924
925;;; COMINT-SOURCE-DEFAULT -- determines defaults for source-file processing
926;;; commands.
927;;;
928;;; COMINT-CHECK-SOURCE -- if FNAME is in a modified buffer, asks you if you
929;;; want to save the buffer before issuing any process requests to the command
930;;; interpreter.
931;;;
932;;; COMINT-GET-SOURCE -- used by the source-file processing commands to prompt
933;;; for the file to process.
934
935;;; (COMINT-SOURCE-DEFAULT previous-dir/file source-modes)
936;;;============================================================================
937;;; This function computes the defaults for the load-file and compile-file
49116ac0 938;;; commands for tea, soar, cmulisp, and cmuscheme modes.
be9b65ac
DL
939;;;
940;;; - PREVIOUS-DIR/FILE is a pair (directory . filename) from the last
941;;; source-file processing command. NIL if there hasn't been one yet.
942;;; - SOURCE-MODES is a list used to determine what buffers contain source
943;;; files: if the major mode of the buffer is in SOURCE-MODES, it's source.
944;;; Typically, (lisp-mode) or (scheme-mode).
945;;;
946;;; If the command is given while the cursor is inside a string, *and*
947;;; the string is an existing filename, *and* the filename is not a directory,
948;;; then the string is taken as default. This allows you to just position
949;;; your cursor over a string that's a filename and have it taken as default.
950;;;
951;;; If the command is given in a file buffer whose major mode is in
952;;; SOURCE-MODES, then the the filename is the default file, and the
953;;; file's directory is the default directory.
954;;;
955;;; If the buffer isn't a source file buffer (e.g., it's the process buffer),
956;;; then the default directory & file are what was used in the last source-file
957;;; processing command (i.e., PREVIOUS-DIR/FILE). If this is the first time
958;;; the command has been run (PREVIOUS-DIR/FILE is nil), the default directory
959;;; is the cwd, with no default file. (\"no default file\" = nil)
960;;;
961;;; SOURCE-REGEXP is typically going to be something like (tea-mode)
962;;; for T programs, (lisp-mode) for Lisp programs, (soar-mode lisp-mode)
963;;; for Soar programs, etc.
964;;;
965;;; The function returns a pair: (default-directory . default-file).
966
967(defun comint-source-default (previous-dir/file source-modes)
968 (cond ((and buffer-file-name (memq major-mode source-modes))
969 (cons (file-name-directory buffer-file-name)
970 (file-name-nondirectory buffer-file-name)))
971 (previous-dir/file)
972 (t
973 (cons default-directory nil))))
974
49116ac0 975
be9b65ac
DL
976;;; (COMINT-CHECK-SOURCE fname)
977;;;============================================================================
49116ac0 978;;; Prior to loading or compiling (or otherwise processing) a file (in the CMU
be9b65ac
DL
979;;; process-in-a-buffer modes), this function can be called on the filename.
980;;; If the file is loaded into a buffer, and the buffer is modified, the user
981;;; is queried to see if he wants to save the buffer before proceeding with
982;;; the load or compile.
983
984(defun comint-check-source (fname)
985 (let ((buff (get-file-buffer fname)))
986 (if (and buff
987 (buffer-modified-p buff)
988 (y-or-n-p (format "Save buffer %s first? "
989 (buffer-name buff))))
990 ;; save BUFF.
991 (let ((old-buffer (current-buffer)))
992 (set-buffer buff)
993 (save-buffer)
994 (set-buffer old-buffer)))))
995
49116ac0 996
be9b65ac
DL
997;;; (COMINT-GET-SOURCE prompt prev-dir/file source-modes mustmatch-p)
998;;;============================================================================
999;;; COMINT-GET-SOURCE is used to prompt for filenames in command-interpreter
1000;;; commands that process source files (like loading or compiling a file).
1001;;; It prompts for the filename, provides a default, if there is one,
1002;;; and returns the result filename.
1003;;;
1004;;; See COMINT-SOURCE-DEFAULT for more on determining defaults.
1005;;;
1006;;; PROMPT is the prompt string. PREV-DIR/FILE is the (directory . file) pair
1007;;; from the last source processing command. SOURCE-MODES is a list of major
1008;;; modes used to determine what file buffers contain source files. (These
1009;;; two arguments are used for determining defaults). If MUSTMATCH-P is true,
1010;;; then the filename reader will only accept a file that exists.
1011;;;
1012;;; A typical use:
1013;;; (interactive (comint-get-source "Compile file: " prev-lisp-dir/file
1014;;; '(lisp-mode) t))
1015
1016;;; This is pretty stupid about strings. It decides we're in a string
1017;;; if there's a quote on both sides of point on the current line.
1018(defun comint-extract-string ()
cccd719e 1019 "Return string around POINT that starts the current line, or nil."
be9b65ac
DL
1020 (save-excursion
1021 (let* ((point (point))
1022 (bol (progn (beginning-of-line) (point)))
1023 (eol (progn (end-of-line) (point)))
1024 (start (progn (goto-char point)
1025 (and (search-backward "\"" bol t)
1026 (1+ (point)))))
1027 (end (progn (goto-char point)
1028 (and (search-forward "\"" eol t)
1029 (1- (point))))))
1030 (and start end
1031 (buffer-substring start end)))))
1032
1033(defun comint-get-source (prompt prev-dir/file source-modes mustmatch-p)
1034 (let* ((def (comint-source-default prev-dir/file source-modes))
1035 (stringfile (comint-extract-string))
1036 (sfile-p (and stringfile
49116ac0
JB
1037 (condition-case ()
1038 (file-exists-p stringfile)
1039 (error nil))
be9b65ac
DL
1040 (not (file-directory-p stringfile))))
1041 (defdir (if sfile-p (file-name-directory stringfile)
1042 (car def)))
1043 (deffile (if sfile-p (file-name-nondirectory stringfile)
1044 (cdr def)))
1045 (ans (read-file-name (if deffile (format "%s(default %s) "
1046 prompt deffile)
1047 prompt)
1048 defdir
1049 (concat defdir deffile)
1050 mustmatch-p)))
1051 (list (expand-file-name (substitute-in-file-name ans)))))
1052
49116ac0
JB
1053;;; I am somewhat divided on this string-default feature. It seems
1054;;; to violate the principle-of-least-astonishment, in that it makes
1055;;; the default harder to predict, so you actually have to look and see
1056;;; what the default really is before choosing it. This can trip you up.
1057;;; On the other hand, it can be useful, I guess. I would appreciate feedback
1058;;; on this.
1059;;; -Olin
1060
be9b65ac
DL
1061\f
1062;;; Simple process query facility.
1063;;; ===========================================================================
1064;;; This function is for commands that want to send a query to the process
1065;;; and show the response to the user. For example, a command to get the
1066;;; arglist for a Common Lisp function might send a "(arglist 'foo)" query
1067;;; to an inferior Common Lisp process.
1068;;;
1069;;; This simple facility just sends strings to the inferior process and pops
1070;;; up a window for the process buffer so you can see what the process
1071;;; responds with. We don't do anything fancy like try to intercept what the
1072;;; process responds with and put it in a pop-up window or on the message
1073;;; line. We just display the buffer. Low tech. Simple. Works good.
1074
1075;;; Send to the inferior process PROC the string STR. Pop-up but do not select
1076;;; a window for the inferior process so that its response can be seen.
1077(defun comint-proc-query (proc str)
1078 (let* ((proc-buf (process-buffer proc))
1079 (proc-mark (process-mark proc)))
1080 (display-buffer proc-buf)
1081 (set-buffer proc-buf) ; but it's not the selected *window*
1082 (let ((proc-win (get-buffer-window proc-buf))
1083 (proc-pt (marker-position proc-mark)))
1084 (comint-send-string proc str) ; send the query
1085 (accept-process-output proc) ; wait for some output
1086 ;; Try to position the proc window so you can see the answer.
1087 ;; This is bogus code. If you delete the (sit-for 0), it breaks.
1088 ;; I don't know why. Wizards invited to improve it.
1089 (if (not (pos-visible-in-window-p proc-pt proc-win))
1090 (let ((opoint (window-point proc-win)))
1091 (set-window-point proc-win proc-mark) (sit-for 0)
1092 (if (not (pos-visible-in-window-p opoint proc-win))
1093 (push-mark opoint)
1094 (set-window-point proc-win opoint)))))))
1095
1096\f
1097;;; Filename completion in a buffer
1098;;; ===========================================================================
1099;;; Useful completion functions, courtesy of the Ergo group.
be9b65ac
DL
1100
1101;;; Three commands:
1102;;; comint-dynamic-complete Complete filename at point.
1103;;; comint-dynamic-list-completions List completions in help buffer.
1104;;; comint-replace-by-expanded-filename Expand and complete filename at point;
1105;;; replace with expanded/completed name.
1106
1107;;; These are not installed in the comint-mode keymap. But they are
49116ac0 1108;;; available for people who want them. Shell-mode installs them:
735b3061
ER
1109;;; (define-key shell-mode-map "\t" 'comint-dynamic-complete)
1110;;; (define-key shell-mode-map "\M-?" 'comint-dynamic-list-completions)))
49116ac0
JB
1111;;;
1112;;; Commands like this are fine things to put in load hooks if you
1586b965 1113;;; want them present in specific modes.
49116ac0 1114
be9b65ac
DL
1115
1116(defun comint-match-partial-pathname ()
cccd719e 1117 "Return the filename at point, or signal an error."
49116ac0 1118 (save-excursion
f4c0acdb 1119 (if (re-search-backward "[^~/A-Za-z0-9_.$#,=-]" nil 'move)
49116ac0
JB
1120 (forward-char 1))
1121 ;; Anchor the search forwards.
f4c0acdb
JB
1122 (if (not (looking-at "[~/A-Za-z0-9_.$#,=-]")) (error ""))
1123 (re-search-forward "[~/A-Za-z0-9_.$#,=-]+")
49116ac0
JB
1124 (substitute-in-file-name
1125 (buffer-substring (match-beginning 0) (match-end 0)))))
1126
be9b65ac
DL
1127
1128(defun comint-replace-by-expanded-filename ()
cccd719e 1129 "Expand the filename at point.
c2ced5d8
CZ
1130Replace the filename with an expanded, canonicalised, and completed
1131 replacement.
be9b65ac
DL
1132\"Expanded\" means environment variables (e.g., $HOME) and ~'s are
1133replaced with the corresponding directories. \"Canonicalised\" means ..
49116ac0 1134and \. are removed, and the filename is made absolute instead of relative.
cccd719e
RS
1135See functions `expand-file-name' and `substitute-in-file-name'. See also
1136`comint-dynamic-complete'."
be9b65ac
DL
1137 (interactive)
1138 (let* ((pathname (comint-match-partial-pathname))
1139 (pathdir (file-name-directory pathname))
1140 (pathnondir (file-name-nondirectory pathname))
1141 (completion (file-name-completion pathnondir
1142 (or pathdir default-directory))))
1143 (cond ((null completion)
1586b965 1144 (message "No completions of %s" pathname)
49116ac0 1145 (ding))
be9b65ac 1146 ((eql completion t)
1586b965 1147 (message "Unique completion"))
be9b65ac
DL
1148 (t ; this means a string was returned.
1149 (delete-region (match-beginning 0) (match-end 0))
1150 (insert (expand-file-name (concat pathdir completion)))))))
1151
49116ac0 1152
be9b65ac 1153(defun comint-dynamic-complete ()
49116ac0 1154 "Dynamically complete the filename at point.
9c106b25 1155This function is similar to `comint-replace-by-expanded-filename', except
be9b65ac
DL
1156that it won't change parts of the filename already entered in the buffer;
1157it just adds completion characters to the end of the filename."
1158 (interactive)
1159 (let* ((pathname (comint-match-partial-pathname))
1160 (pathdir (file-name-directory pathname))
1161 (pathnondir (file-name-nondirectory pathname))
49116ac0 1162 (completion (file-name-completion pathnondir
be9b65ac
DL
1163 (or pathdir default-directory))))
1164 (cond ((null completion)
1586b965 1165 (message "No completions of %s" pathname)
49116ac0 1166 (ding))
be9b65ac 1167 ((eql completion t)
1586b965 1168 (message "Unique completion"))
be9b65ac
DL
1169 (t ; this means a string was returned.
1170 (goto-char (match-end 0))
1171 (insert (substring completion (length pathnondir)))))))
1172
1173(defun comint-dynamic-list-completions ()
49116ac0 1174 "List in help buffer all possible completions of the filename at point."
be9b65ac
DL
1175 (interactive)
1176 (let* ((pathname (comint-match-partial-pathname))
1177 (pathdir (file-name-directory pathname))
1178 (pathnondir (file-name-nondirectory pathname))
1179 (completions
1180 (file-name-all-completions pathnondir
1181 (or pathdir default-directory))))
1182 (cond ((null completions)
1586b965 1183 (message "No completions of %s" pathname)
49116ac0 1184 (ding))
be9b65ac
DL
1185 (t
1186 (let ((conf (current-window-configuration)))
49116ac0 1187 (with-output-to-temp-buffer "*Help*"
be9b65ac
DL
1188 (display-completion-list completions))
1189 (sit-for 0)
1586b965 1190 (message "Hit space to flush")
bdc2ae8d 1191 (let ((ch (read-event)))
561a73d7 1192 (if (eq ch ?\ )
be9b65ac 1193 (set-window-configuration conf)
bdc2ae8d 1194 (setq unread-command-events (list ch)))))))))
be9b65ac
DL
1195\f
1196;;; Converting process modes to use comint mode
1197;;; ===========================================================================
be14d312
ER
1198;;; The code in the Emacs 19 distribution has all been modified to use comint
1199;;; where needed. However, there are `third-party' packages out there that
1200;;; still use the old shell mode. Here's a guide to conversion.
49116ac0 1201;;;
be9b65ac 1202;;; Renaming variables
49116ac0
JB
1203;;; Most of the work is renaming variables and functions. These are the common
1204;;; ones:
1205;;; Local variables:
87ef29fd 1206;;; last-input-start comint-last-input-start
49116ac0 1207;;; last-input-end comint-last-input-end
49116ac0
JB
1208;;; shell-prompt-pattern comint-prompt-regexp
1209;;; shell-set-directory-error-hook <no equivalent>
1210;;; Miscellaneous:
be9b65ac
DL
1211;;; shell-set-directory <unnecessary>
1212;;; shell-mode-map comint-mode-map
49116ac0 1213;;; Commands:
be9b65ac
DL
1214;;; shell-send-input comint-send-input
1215;;; shell-send-eof comint-delchar-or-maybe-eof
1216;;; kill-shell-input comint-kill-input
1217;;; interrupt-shell-subjob comint-interrupt-subjob
1218;;; stop-shell-subjob comint-stop-subjob
1219;;; quit-shell-subjob comint-quit-subjob
1220;;; kill-shell-subjob comint-kill-subjob
1221;;; kill-output-from-shell comint-kill-output
1222;;; show-output-from-shell comint-show-output
1223;;; copy-last-shell-input Use comint-previous-input/comint-next-input
1224;;;
87ef29fd
JB
1225;;; SHELL-SET-DIRECTORY is gone, its functionality taken over by
1226;;; SHELL-DIRECTORY-TRACKER, the shell mode's comint-input-sentinel.
1227;;; Comint mode does not provide functionality equivalent to
be9b65ac 1228;;; shell-set-directory-error-hook; it is gone.
87ef29fd
JB
1229;;;
1230;;; comint-last-input-start is provided for modes which want to munge
1231;;; the buffer after input is sent, perhaps because the inferior
1232;;; insists on echoing the input. The LAST-INPUT-START variable in
1233;;; the old shell package was used to implement a history mechanism,
1234;;; but you should think twice before using comint-last-input-start
1235;;; for this; the input history ring often does the job better.
be9b65ac
DL
1236;;;
1237;;; If you are implementing some process-in-a-buffer mode, called foo-mode, do
1238;;; *not* create the comint-mode local variables in your foo-mode function.
1239;;; This is not modular. Instead, call comint-mode, and let *it* create the
1240;;; necessary comint-specific local variables. Then create the
1241;;; foo-mode-specific local variables in foo-mode. Set the buffer's keymap to
1242;;; be foo-mode-map, and its mode to be foo-mode. Set the comint-mode hooks
1243;;; (comint-prompt-regexp, comint-input-filter, comint-input-sentinel,
1244;;; comint-get-old-input) that need to be different from the defaults. Call
1245;;; foo-mode-hook, and you're done. Don't run the comint-mode hook yourself;
735b3061 1246;;; comint-mode will take care of it. The following example, from shell.el,
49116ac0
JB
1247;;; is typical:
1248;;;
1249;;; (defun shell-mode ()
1250;;; (interactive)
1251;;; (comint-mode)
1252;;; (setq comint-prompt-regexp shell-prompt-pattern)
1253;;; (setq major-mode 'shell-mode)
1254;;; (setq mode-name "Shell")
1255;;; (cond ((not shell-mode-map)
1256;;; (setq shell-mode-map (full-copy-sparse-keymap comint-mode-map))
1257;;; (define-key shell-mode-map "\M-\t" 'comint-dynamic-complete)
1258;;; (define-key shell-mode-map "\M-?"
1259;;; 'comint-dynamic-list-completions)))
1260;;; (use-local-map shell-mode-map)
1261;;; (make-local-variable 'shell-directory-stack)
1262;;; (setq shell-directory-stack nil)
1263;;; (setq comint-input-sentinel 'shell-directory-tracker)
1264;;; (run-hooks 'shell-mode-hook))
1265;;;
be9b65ac
DL
1266;;;
1267;;; Note that make-comint is different from make-shell in that it
1268;;; doesn't have a default program argument. If you give make-shell
1269;;; a program name of NIL, it cleverly chooses one of explicit-shell-name,
1270;;; $ESHELL, $SHELL, or /bin/sh. If you give make-comint a program argument
1271;;; of NIL, it barfs. Adjust your code accordingly...
49116ac0
JB
1272;;;
1273\f
1274;;; Do the user's customisation...
1275
1276(defvar comint-load-hook nil
1277 "This hook is run when comint is loaded in.
1278This is a good place to put keybindings.")
1279
1280(run-hooks 'comint-load-hook)
1281
1282;;; Change log:
1283;;; 9/12/89
1284;;; - Souped up the filename expansion procedures.
1285;;; Doc strings are much clearer and more detailed.
1286;;; Fixed a bug where doing a filename completion when the point
1287;;; was in the middle of the filename instead of at the end would lose.
1288;;;
1289;;; 2/17/90
1290;;; - Souped up the command history stuff so that text inserted
1291;;; by comint-previous-input-matching is removed by following
1292;;; command history recalls. comint-next/previous-input-matching
1293;;; is now much more smoothly integrated w/the command history stuff.
1294;;; - Added comint-eol-on-send flag and comint-input-sender hook.
1295;;; Comint-input-sender based on code contributed by Jeff Peck
1296;;; (peck@sun.com).
1297;;;
1298;;; 3/13/90 ccm@cmu.cs.edu
1299;;; - Added comint-previous-similar-input for looking up similar inputs.
1300;;; - Added comint-send-and-get-output to allow snarfing input from
1301;;; buffer.
1302;;; - Added the ability to pick up a source file by positioning over
1303;;; a string in comint-get-source.
1304;;; - Added add-hook to make it a little easier for the user to use
1305;;; multiple hooks.
1306;;;
1307;;; 5/22/90 shivers
1308;;; - Moved Chris' multiplexed ipc stuff to comint-ipc.el.
1309;;; - Altered Chris' comint-get-source string feature. The string
1310;;; is only offered as a default if it names an existing file.
1311;;; - Changed comint-exec to directly crank up the process, instead
1312;;; of calling the env program. This made background.el happy.
1313;;; - Added new buffer-local var comint-ptyp. The problem is that
1314;;; the signalling functions don't work as advertised. If you are
1315;;; communicating via pipes, the CURRENT-GROUP arg is supposed to
1316;;; be ignored, but, unfortunately it seems to be the case that you
1317;;; must pass a NIL for this arg in the pipe case. COMINT-PTYP
1318;;; is a flag that tells whether the process is communicating
1319;;; via pipes or a pty. The comint signalling functions use it
1320;;; to determine the necessary CURRENT-GROUP arg value. The bug
1321;;; has been reported to the Gnu folks.
1322;;; - comint-dynamic-complete flushes the help window if you hit space
1323;;; after you execute it.
1324;;; - Added functions comint-send-string, comint-send-region and var
1325;;; comint-input-chunk-size. comint-send-string tries to prevent processes
1326;;; from hanging when you send them long strings by breaking them into
1327;;; chunks and allowing process output between chunks. I got the idea from
1328;;; Eero Simoncelli's Common Lisp package. Note that using
1329;;; comint-send-string means that the process buffer's contents can change
1330;;; during a call! If you depend on process output only happening between
1331;;; toplevel commands, this could be a problem. In such a case, use
1332;;; process-send-string instead. If this is a problem for people, I'd like
1333;;; to hear about it.
1334;;; - Added comint-proc-query as a simple mechanism for commands that
1335;;; want to query an inferior process and display its response. For a
1336;;; typical use, see lisp-show-arglist in cmulisp.el.
1337;;; - Added constant comint-version, which is now "2.01".
1338;;;
1339;;; 6/14/90 shivers
1340;;; - Had comint-update-env defined twice. Removed extra copy. Also
1341;;; renamed mem to be comint-mem, for modularity. The duplication
1342;;; was reported by Michael Meissner.
1343;;; 6/16/90 shivers
1344;;; - Emacs has two different mechanisms for maintaining the process
1345;;; environment, determined at compile time by the MAINTAIN-ENVIRONMENT
1346;;; #define. One uses the process-environment global variable, and
1347;;; one uses a getenv/setenv interface. comint-exec assumed the
1348;;; process-environment interface; it has been generalised (with
1349;;; comint-exec-1) to handle both cases. Pretty bogus. We could,
1350;;; of course, skip all this and just use the etc/env program to
1351;;; handle the environment tweaking, but that obscures process
1352;;; queries that other modules (like background.el) depend on. etc/env
1353;;; is also fairly bogus. This bug, and some of the fix code was
1354;;; reported by Dan Pierson.
1355;;;
1356;;; 9/5/90 shivers
1357;;; - Changed make-variable-buffer-local's to make-local-variable's.
1358;;; This leaves non-comint-mode buffers alone. Stephane Payrard
eb8c3be9 1359;;; reported the sloppy usage.
49116ac0
JB
1360;;; - You can now go from comint-previous-similar-input to
1361;;; comint-previous-input with no problem.
1362;;;
1363;;; 12/21/90 shivers
1364;;; - Added a condition-case to comint-get-source. Bogus strings
1365;;; beginning with ~ were making the file-exists-p barf.
1366;;; - Added "=" to the set of chars recognised by file completion
1367;;; as constituting a filename.
1368;;;
1369;;; 1/90 shivers
1370;;; These changes comprise release 2.02:
1371;;; - Removed the kill-all-local-variables in comint-mode. This
1372;;; made it impossible for client modes to set things before calling
1373;;; comint-mode. (In particular, it messed up ilisp.el) In general,
1374;;; the client mode should be responsible for a k-a-l-v's.
1375;;; - Fixed comint-match-partial-pathname so that it works in
1376;;; more cases: if the filename begins at the start-of-buffer;
1377;;; if point is on the first char of the filename. Just a question
1378;;; of getting the tricky bits right.
1379;;; - Added a hook, comint-exec-hook that is run each time a process
1380;;; is cranked up. Useful for things like process-kill-without-query.
1381;;;
87ef29fd 1382;;; These two were pointed out by tale:
49116ac0
JB
1383;;; - Improved the doc string in comint-send-input a little bit.
1384;;; - Tweaked make-comint to check process status with comint-check-proc
1385;;; instead of equivalent inline code.
87ef29fd 1386;;;
49116ac0
JB
1387;;; - Prompt-search history commands have been commented out. I never
1388;;; liked them; I don't think anyone used them.
87ef29fd 1389;;; - Made comint-exec-hook a local var, as it should have been.
735b3061 1390;;; (This way, for instance, you can have shell procs kill-w/o-query,
87ef29fd
JB
1391;;; but let Scheme procs be default.)
1392;;;
1393;;; 7/91 Shivers
1394;;; - Souped up comint-read-noecho with an optional argument, STARS.
1395;;; Suggested by mjlx@EAGLE.CNSF.CORNELL.EDU.
1396;;; - Moved comint-previous-input-matching from C-c r to C-M-r.
1397;;; C-c <letter> bindings are reserved for the user.
1398;;; These bindings were done by Jim Blandy.
1399;;; These changes comprise version 2.03.
49116ac0
JB
1400
1401(provide 'comint)
c0274f38
ER
1402
1403;;; comint.el ends here