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