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