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