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