comint.el 141 KB
Newer Older
Eric S. Raymond's avatar
Eric S. Raymond committed
1
;;; comint.el --- general command interpreter in a window stuff
2

3
;; Copyright (C) 1988, 1990, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
4
;;   2000, 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
Eric S. Raymond's avatar
Eric S. Raymond committed
5

6
;; Author: Olin Shivers <shivers@cs.cmu.edu>
Karl Heuer's avatar
Karl Heuer committed
7
;;	Simon Marshall <simon@gnu.org>
Simon Marshall's avatar
Simon Marshall committed
8
;; Maintainer: FSF
Eric S. Raymond's avatar
Eric S. Raymond committed
9
;; Keywords: processes
Eric S. Raymond's avatar
Eric S. Raymond committed
10

11
;; This file is part of GNU Emacs.
David Lawrence's avatar
David Lawrence committed
12

13 14 15 16
;; GNU Emacs is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 2, or (at your option)
;; any later version.
David Lawrence's avatar
David Lawrence committed
17

18 19 20 21
;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.
David Lawrence's avatar
David Lawrence committed
22

23
;; You should have received a copy of the GNU General Public License
Erik Naggum's avatar
Erik Naggum committed
24
;; along with GNU Emacs; see the file COPYING.  If not, write to the
Lute Kamstra's avatar
Lute Kamstra committed
25 26
;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
;; Boston, MA 02110-1301, USA.
David Lawrence's avatar
David Lawrence committed
27

Eric S. Raymond's avatar
Eric S. Raymond committed
28 29
;;; Commentary:

Erik Naggum's avatar
Erik Naggum committed
30
;; This file defines a general command-interpreter-in-a-buffer package
Stefan Monnier's avatar
Stefan Monnier committed
31 32
;; (comint mode).  The idea is that you can build specific process-in-a-buffer
;; modes on top of comint mode -- e.g., Lisp, shell, scheme, T, soar, ....
Miles Bader's avatar
Miles Bader committed
33
;; This way, all these specific packages share a common base functionality,
Erik Naggum's avatar
Erik Naggum committed
34 35 36 37 38 39 40 41 42
;; and a common set of bindings, which makes them easier to use (and
;; saves code, implementation time, etc., etc.).

;; Several packages are already defined using comint mode:
;; - shell.el defines a shell-in-a-buffer mode.
;; - cmulisp.el defines a simple lisp-in-a-buffer mode.
;;
;; - The file cmuscheme.el defines a scheme-in-a-buffer mode.
;; - The file tea.el tunes scheme and inferior-scheme modes for T.
Stefan Monnier's avatar
Stefan Monnier committed
43 44 45 46
;; - The file soar.el tunes Lisp and inferior-lisp modes for Soar.
;; - cmutex.el defines TeX and LaTeX modes that invoke TeX, LaTeX, BibTeX,
;;   previewers, and printers from within Emacs.
;; - background.el allows csh-like job control inside Emacs.
Erik Naggum's avatar
Erik Naggum committed
47 48
;; It is pretty easy to make new derived modes for other processes.

Stefan Monnier's avatar
Stefan Monnier committed
49
;; For documentation on the functionality provided by Comint mode, and
Erik Naggum's avatar
Erik Naggum committed
50
;; the hooks available for customising it, see the comments below.
Miles Bader's avatar
Miles Bader committed
51
;; For further information on the standard derived modes (shell,
Erik Naggum's avatar
Erik Naggum committed
52 53 54 55 56
;; inferior-lisp, inferior-scheme, ...), see the relevant source files.

;; For hints on converting existing process modes (e.g., tex-mode,
;; background, dbx, gdb, kermit, prolog, telnet) to use comint-mode
;; instead of shell-mode, see the notes at the end of this file.
David Lawrence's avatar
David Lawrence committed
57 58


Erik Naggum's avatar
Erik Naggum committed
59 60 61 62 63
;; Brief Command Documentation:
;;============================================================================
;; Comint Mode Commands: (common to all derived modes, like shell & cmulisp
;; mode)
;;
Stefan Monnier's avatar
Stefan Monnier committed
64 65 66 67 68 69 70 71
;; M-p	   comint-previous-input	   Cycle backwards in input history
;; M-n	   comint-next-input		   Cycle forwards
;; M-r     comint-previous-matching-input  Previous input matching a regexp
;; M-s     comint-next-matching-input      Next input that matches
;; M-C-l   comint-show-output		   Show last batch of process output
;; RET	   comint-send-input
;; C-d	   comint-delchar-or-maybe-eof     Delete char unless at end of buff
;; C-c C-a comint-bol-or-process-mark      First time, move point to bol;
72
;;					    second time, move to process-mark.
Stefan Monnier's avatar
Stefan Monnier committed
73 74 75 76 77 78 79 80
;; C-c C-u comint-kill-input		    ^u
;; C-c C-w backward-kill-word		    ^w
;; C-c C-c comint-interrupt-subjob	    ^c
;; C-c C-z comint-stop-subjob		    ^z
;; C-c C-\ comint-quit-subjob		    ^\
;; C-c C-o comint-delete-output		    Delete last batch of process output
;; C-c C-r comint-show-output		    Show last batch of process output
;; C-c C-l comint-dynamic-list-input-ring  List input history
Erik Naggum's avatar
Erik Naggum committed
81 82 83 84 85 86 87 88 89 90 91 92 93 94
;;
;; Not bound by default in comint-mode (some are in shell mode)
;; comint-run				Run a program under comint-mode
;; send-invisible			Read a line w/o echo, and send to proc
;; comint-dynamic-complete-filename	Complete filename at point.
;; comint-dynamic-list-filename-completions List completions in help buffer.
;; comint-replace-by-expanded-filename	Expand and complete filename at point;
;;					replace with expanded/completed name.
;; comint-replace-by-expanded-history	Expand history at point;
;;					replace with expanded name.
;; comint-magic-space                  Expand history and add (a) space(s).
;; comint-kill-subjob			No mercy.
;; comint-show-maximum-output          Show as much output as possible.
;; comint-continue-subjob		Send CONT signal to buffer's process
Stefan Monnier's avatar
Stefan Monnier committed
95
;;					group.  Useful if you accidentally
Erik Naggum's avatar
Erik Naggum committed
96
;;					suspend your process (with C-c C-z).
97 98 99 100 101
;; comint-get-next-from-history        Fetch successive input history lines
;; comint-accumulate		       Combine lines to send them together
;;					as input.
;; comint-goto-process-mark	       Move point to where process-mark is.
;; comint-set-process-mark	       Set process-mark to point.
Erik Naggum's avatar
Erik Naggum committed
102

Stefan Monnier's avatar
Stefan Monnier committed
103
;; comint-mode-hook is the Comint mode hook.  Basically for your keybindings.
David Lawrence's avatar
David Lawrence committed
104

Eric S. Raymond's avatar
Eric S. Raymond committed
105 106
;;; Code:

Richard M. Stallman's avatar
Richard M. Stallman committed
107
(require 'ring)
David Lawrence's avatar
David Lawrence committed
108

Erik Naggum's avatar
Erik Naggum committed
109 110 111
;; Buffer Local Variables:
;;============================================================================
;; Comint mode buffer local variables:
Miles Bader's avatar
Miles Bader committed
112
;;  comint-prompt-regexp		string	comint-bol uses to match prompt
113 114
;;  comint-delimiter-argument-list	list	For delimiters and arguments
;;  comint-last-input-start		marker	Handy if inferior always echoes
115
;;  comint-last-input-end		marker	For comint-delete-output command
116 117 118
;;  comint-input-ring-size		integer	For the input history
;;  comint-input-ring			ring	mechanism
;;  comint-input-ring-index		number	...
119
;;  comint-save-input-ring-index	number	...
120 121 122 123
;;  comint-input-autoexpand		symbol	...
;;  comint-input-ignoredups		boolean	...
;;  comint-dynamic-complete-functions	hook   For the completion mechanism
;;  comint-completion-fignore		list	...
124
;;  comint-file-name-chars		string	...
125
;;  comint-file-name-quote-list		list	...
Miles Bader's avatar
Miles Bader committed
126
;;  comint-get-old-input		function Hooks for specific
127 128
;;  comint-input-filter-functions	hook	process-in-a-buffer
;;  comint-output-filter-functions	hook	function modes.
129
;;  comint-preoutput-filter-functions   hook
130 131 132 133 134 135
;;  comint-input-filter			function ...
;;  comint-input-sender			function ...
;;  comint-eol-on-send			boolean	...
;;  comint-process-echoes		boolean	...
;;  comint-scroll-to-bottom-on-input	symbol	For scroll behavior
;;  comint-scroll-to-bottom-on-output	symbol	...
Miles Bader's avatar
Miles Bader committed
136
;;  comint-scroll-show-maximum-output	boolean	...
137
;;  comint-accum-marker			maker	  For comint-accumulate
Erik Naggum's avatar
Erik Naggum committed
138 139
;;
;; Comint mode non-buffer local variables:
140 141 142
;;  comint-completion-addsuffix		boolean/cons	For file name
;;  comint-completion-autolist		boolean		completion behavior
;;  comint-completion-recexact		boolean		...
David Lawrence's avatar
David Lawrence committed
143

144 145 146 147 148
(defgroup comint nil
  "General command interpreter in a window stuff."
  :group 'processes)

(defgroup comint-completion nil
149
  "Completion facilities in comint."
150 151 152
  :group 'comint)

(defgroup comint-source nil
153
  "Source finding facilities in comint."
154 155 156
  :prefix "comint-"
  :group 'comint)

Jim Blandy's avatar
Jim Blandy committed
157
(defvar comint-prompt-regexp "^"
158
  "Regexp to recognize prompts in the inferior process.
Jim Blandy's avatar
Jim Blandy committed
159
Defaults to \"^\", the null string at BOL.
David Lawrence's avatar
David Lawrence committed
160

161
This variable is only used if the variable
162
`comint-use-prompt-regexp' is non-nil.
163

David Lawrence's avatar
David Lawrence committed
164
Good choices:
165
  Canonical Lisp: \"^[^> \\n]*>+:? *\" (Lucid, franz, kcl, T, cscheme, oaklisp)
166 167
  Lucid Common Lisp: \"^\\\\(>\\\\|\\\\(->\\\\)+\\\\) *\"
  franz: \"^\\\\(->\\\\|<[0-9]*>:\\\\) *\"
Jim Blandy's avatar
Jim Blandy committed
168
  kcl: \"^>+ *\"
169
  shell: \"^[^#$%>\\n]*[#$%>] *\"
Jim Blandy's avatar
Jim Blandy committed
170
  T: \"^>+ *\"
David Lawrence's avatar
David Lawrence committed
171

Jim Blandy's avatar
Jim Blandy committed
172
This is a good thing to set in mode hooks.")
David Lawrence's avatar
David Lawrence committed
173

174 175
(defcustom comint-prompt-read-only nil
  "If non-nil, the comint prompt is read only.
176
The read only region includes the newline before the prompt.
177
This does not affect existing prompts.
178 179 180 181 182 183 184 185 186 187
Certain derived modes may override this option.

If you set this option to t, then the safe way to temporarily
override the read-only-ness of comint prompts is to call
`comint-kill-whole-line' or `comint-kill-region' with no
narrowing in effect.  This way you will be certain that none of
the remaining prompts will be accidentally messed up.  You may
wish to put something like the following in your `.emacs' file:

\(add-hook 'comint-mode-hook
188
	  (lambda ()
189
	    (define-key comint-mode-map \"\\C-w\" 'comint-kill-region)
190 191
	    (define-key comint-mode-map [C-S-backspace]
	      'comint-kill-whole-line)))
192

193
If you sometimes use comint-mode on text-only terminals or with `emacs -nw',
194
you might wish to use another binding for `comint-kill-whole-line'."
195 196
  :type 'boolean
  :group 'comint
197
  :version "22.1")
198

199
(defvar comint-delimiter-argument-list ()
200
  "List of characters to recognize as separate arguments in input.
201 202 203
Strings comprising a character in this list will separate the arguments
surrounding them, and also be regarded as arguments in their own right (unlike
whitespace).  See `comint-arguments'.
204
Defaults to the empty list.
205

206
For shells, a good value is (?\\| ?& ?< ?> ?\\( ?\\) ?;).
207 208 209

This is a good thing to set in mode hooks.")

210
(defcustom comint-input-autoexpand nil
211
  "*If non-nil, expand input command history references on completion.
Stefan Monnier's avatar
Stefan Monnier committed
212
This mirrors the optional behavior of tcsh (its autoexpand and histlist).
213 214 215 216 217 218

If the value is `input', then the expansion is seen on input.
If the value is `history', then the expansion is only when inserting
into the buffer's input ring.  See also `comint-magic-space' and
`comint-dynamic-complete'.

219 220 221
This variable is buffer-local."
  :type '(choice (const :tag "off" nil)
		 (const input)
222 223
		 (const history)
		 (other :tag "on" t))
224 225
  :group 'comint)

226
(defface comint-highlight-input '((t (:weight bold)))
227
  "Face to use to highlight user input."
228 229
  :group 'comint)

230
(defface comint-highlight-prompt
231
  '((((min-colors 88) (background dark)) (:foreground "cyan1"))
232
    (((background dark)) (:foreground "cyan"))
233
    (t (:foreground "dark blue")))
234
  "Face to use to highlight prompts."
235 236
  :group 'comint)

237
(defcustom comint-input-ignoredups nil
238
  "*If non-nil, don't add input matching the last on the input ring.
239
This mirrors the optional behavior of bash.
240

241 242 243
This variable is buffer-local."
  :type 'boolean
  :group 'comint)
244

245
(defcustom comint-input-ring-file-name nil
246 247 248
  "*If non-nil, name of the file to read/write input history.
See also `comint-read-input-ring' and `comint-write-input-ring'.

249
This variable is buffer-local, and is a good thing to set in mode hooks."
250 251
  :type '(choice (const :tag "nil" nil)
		 file)
252
  :group 'comint)
253

254
(defcustom comint-scroll-to-bottom-on-input nil
255 256 257 258
  "*Controls whether input to interpreter causes window to scroll.
If nil, then do not scroll.  If t or `all', scroll all windows showing buffer.
If `this', scroll only the selected window.

259
The default is nil.
260

261 262 263 264 265 266
See `comint-preinput-scroll-to-bottom'.  This variable is buffer-local."
  :type '(choice (const :tag "off" nil)
		 (const t)
		 (const all)
		 (const this))
  :group 'comint)
267

268 269 270 271 272 273 274
(defcustom comint-move-point-for-output nil
  "*Controls whether interpreter output moves point to the end of the output.
If nil, then output never moves point to the output.
 (If the output occurs at point, it is inserted before point.)
If t or `all', move point in all windows showing the buffer.
If `this', move point only the selected window.
If `others', move point only in other windows, not in the selected window.
275 276

The default is nil.
277

278 279
See the variable `comint-scroll-show-maximum-output' and the function
`comint-postoutput-scroll-to-bottom'.
Stefan Monnier's avatar
Stefan Monnier committed
280
This variable is buffer-local in all Comint buffers."
281 282 283 284 285 286 287
  :type '(choice (const :tag "off" nil)
		 (const t)
		 (const all)
		 (const this)
		 (const others))
  :group 'comint)

288
(defvaralias 'comint-scroll-to-bottom-on-output 'comint-move-point-for-output)
289

290 291 292
(defcustom comint-scroll-show-maximum-output t
  "*Controls how to scroll due to interpreter output.
This variable applies when point is at the end of the buffer
293
\(either because it was originally there, or because
294 295 296 297 298
`comint-move-point-for-output' said to move it there)
and output from the subprocess is inserted.

Non-nil means scroll so that the window is full of text
and point is on the last line.  A value of nil
Stefan Monnier's avatar
Stefan Monnier committed
299
means don't do anything special--scroll normally.
300 301 302

See also the variable `comint-move-point-for-output' and the function
`comint-postoutput-scroll-to-bottom'.
Stefan Monnier's avatar
Stefan Monnier committed
303
This variable is buffer-local in all Comint buffers."
304 305
  :type 'boolean
  :group 'comint)
306

307
(defcustom comint-buffer-maximum-size 1024
Stefan Monnier's avatar
Stefan Monnier committed
308
  "*The maximum size in lines for Comint buffers.
309
Comint buffers are truncated from the top to be no greater than this number, if
310 311 312
the function `comint-truncate-buffer' is on `comint-output-filter-functions'."
  :type 'integer
  :group 'comint)
313

314
(defvar comint-input-ring-size 150
Jim Blandy's avatar
Jim Blandy committed
315
  "Size of input history ring.")
David Lawrence's avatar
David Lawrence committed
316

317 318 319
(defvar comint-input-ring-separator "\n"
  "Separator between commands in the history file.")

320
(defvar comint-input-history-ignore "^#"
Stefan Monnier's avatar
Stefan Monnier committed
321
  "Regexp for history entries that should be ignored when Comint initializes.")
322

323
(defcustom comint-process-echoes nil
324
  "*If non-nil, assume that the subprocess echoes any input.
Noah Friedman's avatar
Noah Friedman committed
325
If so, delete one copy of the input so that only one copy eventually
326
appears in the buffer.
Noah Friedman's avatar
Noah Friedman committed
327

328 329 330
This variable is buffer-local."
  :type 'boolean
  :group 'comint)
Noah Friedman's avatar
Noah Friedman committed
331

332
;; AIX puts the name of the person being su'd to in front of the prompt.
333
;; kinit prints a prompt like `Password for devnull@GNU.ORG: '.
334 335
;; ksu prints a prompt like `Kerberos password for devnull/root@GNU.ORG: '.
;; ssh-add prints a prompt like `Enter passphrase: '.
336
;; Some implementations of passwd use "Password (again)" as the 2nd prompt.
337
(defcustom comint-password-prompt-regexp
338
  "\\(\\([Oo]ld \\|[Nn]ew \\|'s \\|login \\|\
339
Kerberos \\|CVS \\|UNIX \\| SMB \\|^\\)\
Stefan Monnier's avatar
Stefan Monnier committed
340
\[Pp]assword\\( (again)\\)?\\|\
341 342
pass phrase\\|\\(Enter\\|Repeat\\|Bad\\) passphrase\\)\
\\(?:, try again\\)?\\(?: for [^:]+\\)?:\\s *\\'"
343
  "*Regexp matching prompts for passwords in the inferior process.
344 345 346
This is used by `comint-watch-for-password-prompt'."
  :type 'regexp
  :group 'comint)
347

Erik Naggum's avatar
Erik Naggum committed
348
;; Here are the per-interpreter hooks.
Jim Blandy's avatar
Jim Blandy committed
349
(defvar comint-get-old-input (function comint-get-old-input-default)
Stefan Monnier's avatar
Stefan Monnier committed
350
  "Function that returns old text in Comint mode.
351 352 353 354 355
This function is called when return is typed while the point is in old
text.  It returns the text to be submitted as process input.  The
default is `comint-get-old-input-default', which either grabs the
current input field or grabs the current line and strips off leading
text matching `comint-prompt-regexp', depending on the value of
356
`comint-use-prompt-regexp'.")
David Lawrence's avatar
David Lawrence committed
357

358 359 360 361 362
(defvar comint-dynamic-complete-functions
  '(comint-replace-by-expanded-history comint-dynamic-complete-filename)
  "List of functions called to perform completion.
Functions should return non-nil if completion was performed.
See also `comint-dynamic-complete'.
363 364 365

This is a good thing to set in mode hooks.")

Jim Blandy's avatar
Jim Blandy committed
366 367 368
(defvar comint-input-filter
  (function (lambda (str) (not (string-match "\\`\\s *\\'" str))))
  "Predicate for filtering additions to input history.
369 370 371
Takes one argument, the input.  If non-nil, the input may be saved on the input
history list.  Default is to save anything that isn't all whitespace.")

372
(defvar comint-input-filter-functions '()
373
  "Abnormal hook run before input is sent to the process.
374
These functions get one argument, a string containing the text to send.")
375

376
;;;###autoload
377
(defvar comint-output-filter-functions '(comint-postoutput-scroll-to-bottom comint-watch-for-password-prompt)
378
  "Functions to call after output is inserted into the buffer.
379
One possible function is `comint-postoutput-scroll-to-bottom'.
380 381 382 383
These functions get one argument, a string containing the text as originally
inserted.  Note that this might not be the same as the buffer contents between
`comint-last-output-start' and the buffer's `process-mark', if other filter
functions have already modified the buffer.
384

385 386
See also `comint-preoutput-filter-functions'.

387 388
You can use `add-hook' to add functions to this list
either globally or locally.")
Jim Blandy's avatar
Jim Blandy committed
389

390 391 392
(defvar comint-input-sender-no-newline nil
  "Non-nil directs the `comint-input-sender' function not to send a newline.")

Jim Blandy's avatar
Jim Blandy committed
393 394
(defvar comint-input-sender (function comint-simple-send)
  "Function to actually send to PROCESS the STRING submitted by user.
Miles Bader's avatar
Miles Bader committed
395
Usually this is just `comint-simple-send', but if your mode needs to
396 397
massage the input string, put a different function here.
`comint-simple-send' just sends the string plus a newline.
398
\(If `comint-input-sender-no-newline' is non-nil, it omits the newline.)
399
This is called from the user command `comint-send-input'.")
Jim Blandy's avatar
Jim Blandy committed
400

401
(defcustom comint-eol-on-send t
402
  "*Non-nil means go to the end of the line before sending input.
403 404 405
See `comint-send-input'."
  :type 'boolean
  :group 'comint)
David Lawrence's avatar
David Lawrence committed
406

407 408 409
;; Note: If it is decided to purge comint-prompt-regexp from the source
;; entirely, searching for uses of this variable will help to identify
;; places that need attention.
410 411
(defcustom comint-use-prompt-regexp nil
  "*If non-nil, use `comint-prompt-regexp' to recognize prompts.
412
If nil, then program output and user-input are given different `field'
413
properties, which Emacs commands can use to distinguish them (in
414 415
particular, common movement commands such as `beginning-of-line'
respect field boundaries in a natural way)."
416 417 418
  :type 'boolean
  :group 'comint)

419 420
;; Autoload is necessary for Custom to recognize old alias.
;;;###autoload
421 422
(define-obsolete-variable-alias 'comint-use-prompt-regexp-instead-of-fields
    'comint-use-prompt-regexp "22.1")
423

424
(defcustom comint-mode-hook nil
Stefan Monnier's avatar
Stefan Monnier committed
425
  "Hook run upon entry to `comint-mode'.
426 427 428
This is run before the process is cranked up."
  :type 'hook
  :group 'comint)
Jim Blandy's avatar
Jim Blandy committed
429

430
(defcustom comint-exec-hook '()
Stefan Monnier's avatar
Stefan Monnier committed
431
  "Hook run each time a process is exec'd by `comint-exec'.
Jim Blandy's avatar
Jim Blandy committed
432
This is called after the process is cranked up.  It is useful for things that
Stefan Monnier's avatar
Stefan Monnier committed
433
must be done each time a process is executed in a Comint mode buffer (e.g.,
434
`(process-kill-without-query)').  In contrast, the `comint-mode-hook' is only
435 436 437
executed once when the buffer is created."
  :type 'hook
  :group 'comint)
Jim Blandy's avatar
Jim Blandy committed
438

439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538
(defvar comint-mode-map
  (let ((map (make-sparse-keymap)))
    ;; Keys:
    (define-key map "\ep" 	  'comint-previous-input)
    (define-key map "\en" 	  'comint-next-input)
    (define-key map [C-up] 	  'comint-previous-input)
    (define-key map [C-down] 	  'comint-next-input)
    (define-key map "\er" 	  'comint-previous-matching-input)
    (define-key map "\es" 	  'comint-next-matching-input)
    (define-key map [?\C-c ?\M-r] 'comint-previous-matching-input-from-input)
    (define-key map [?\C-c ?\M-s] 'comint-next-matching-input-from-input)
    (define-key map "\e\C-l" 	  'comint-show-output)
    (define-key map "\C-m" 	  'comint-send-input)
    (define-key map "\C-d" 	  'comint-delchar-or-maybe-eof)
    (define-key map "\C-c " 	  'comint-accumulate)
    (define-key map "\C-c\C-x" 	  'comint-get-next-from-history)
    (define-key map "\C-c\C-a" 	  'comint-bol-or-process-mark)
    (define-key map "\C-c\C-u" 	  'comint-kill-input)
    (define-key map "\C-c\C-w" 	  'backward-kill-word)
    (define-key map "\C-c\C-c" 	  'comint-interrupt-subjob)
    (define-key map "\C-c\C-z" 	  'comint-stop-subjob)
    (define-key map "\C-c\C-\\"   'comint-quit-subjob)
    (define-key map "\C-c\C-m" 	  'comint-insert-input)
    (define-key map "\C-c\C-o" 	  'comint-delete-output)
    (define-key map "\C-c\C-r" 	  'comint-show-output)
    (define-key map "\C-c\C-e" 	  'comint-show-maximum-output)
    (define-key map "\C-c\C-l" 	  'comint-dynamic-list-input-ring)
    (define-key map "\C-c\C-n" 	  'comint-next-prompt)
    (define-key map "\C-c\C-p" 	  'comint-previous-prompt)
    (define-key map "\C-c\C-d" 	  'comint-send-eof)
    (define-key map "\C-c\C-s" 	  'comint-write-output)
    (define-key map "\C-c." 	  'comint-insert-previous-argument)
    ;; Mouse Buttons:
    (define-key map [mouse-2]     'comint-insert-input)
    ;; Menu bars:
    ;; completion:
    (define-key map [menu-bar completion]
      (cons "Complete" (make-sparse-keymap "Complete")))
    (define-key map [menu-bar completion complete-expand]
      '("Expand File Name" . comint-replace-by-expanded-filename))
    (define-key map [menu-bar completion complete-listing]
      '("File Completion Listing" . comint-dynamic-list-filename-completions))
    (define-key map [menu-bar completion complete-file]
      '("Complete File Name" . comint-dynamic-complete-filename))
    (define-key map [menu-bar completion complete]
      '("Complete Before Point" . comint-dynamic-complete))
    ;; Input history:
    (define-key map [menu-bar inout]
      (cons "In/Out" (make-sparse-keymap "In/Out")))
    (define-key map [menu-bar inout delete-output]
      '("Delete Current Output Group" . comint-delete-output))
    (define-key map [menu-bar inout append-output-to-file]
      '("Append Current Output Group to File" . comint-append-output-to-file))
    (define-key map [menu-bar inout write-output]
      '("Write Current Output Group to File" . comint-write-output))
    (define-key map [menu-bar inout next-prompt]
      '("Forward Output Group" . comint-next-prompt))
    (define-key map [menu-bar inout previous-prompt]
      '("Backward Output Group" . comint-previous-prompt))
    (define-key map [menu-bar inout show-maximum-output]
      '("Show Maximum Output" . comint-show-maximum-output))
    (define-key map [menu-bar inout show-output]
      '("Show Current Output Group" . comint-show-output))
    (define-key map [menu-bar inout kill-input]
      '("Kill Current Input" . comint-kill-input))
    (define-key map [menu-bar inout copy-input]
      '("Copy Old Input" . comint-insert-input))
    (define-key map [menu-bar inout forward-matching-history]
      '("Forward Matching Input..." . comint-forward-matching-input))
    (define-key map [menu-bar inout backward-matching-history]
      '("Backward Matching Input..." . comint-backward-matching-input))
    (define-key map [menu-bar inout next-matching-history]
      '("Next Matching Input..." . comint-next-matching-input))
    (define-key map [menu-bar inout previous-matching-history]
      '("Previous Matching Input..." . comint-previous-matching-input))
    (define-key map [menu-bar inout next-matching-history-from-input]
      '("Next Matching Current Input" . comint-next-matching-input-from-input))
    (define-key map [menu-bar inout previous-matching-history-from-input]
      '("Previous Matching Current Input" . comint-previous-matching-input-from-input))
    (define-key map [menu-bar inout next-history]
      '("Next Input" . comint-next-input))
    (define-key map [menu-bar inout previous-history]
      '("Previous Input" . comint-previous-input))
    (define-key map [menu-bar inout list-history]
      '("List Input History" . comint-dynamic-list-input-ring))
    (define-key map [menu-bar inout expand-history]
      '("Expand History Before Point" . comint-replace-by-expanded-history))
    ;; Signals
    (let ((signals-map (make-sparse-keymap "Signals")))
      (define-key map [menu-bar signals] (cons "Signals" signals-map))
      (define-key signals-map [eof]   '("EOF"   . comint-send-eof))
      (define-key signals-map [kill]  '("KILL"  . comint-kill-subjob))
      (define-key signals-map [quit]  '("QUIT"  . comint-quit-subjob))
      (define-key signals-map [cont]  '("CONT"  . comint-continue-subjob))
      (define-key signals-map [stop]  '("STOP"  . comint-stop-subjob))
      (define-key signals-map [break] '("BREAK" . comint-interrupt-subjob)))
    ;; Put them in the menu bar:
    (setq menu-bar-final-items (append '(completion inout signals)
				       menu-bar-final-items))
    map))
David Lawrence's avatar
David Lawrence committed
539

Stefan Monnier's avatar
Stefan Monnier committed
540
;; Fixme: Is this still relevant?
Richard M. Stallman's avatar
Richard M. Stallman committed
541
(defvar comint-ptyp t
542
  "Non-nil if communications via pty; false if by pipe.  Buffer local.
543
This is to work around a bug in Emacs process signaling.")
Richard M. Stallman's avatar
Richard M. Stallman committed
544

545
(defvar comint-input-ring nil)
Stefan Monnier's avatar
Stefan Monnier committed
546 547 548
(defvar comint-last-input-start nil)
(defvar comint-last-input-end nil)
(defvar comint-last-output-start nil)
549 550 551 552
(defvar comint-input-ring-index nil
  "Index of last matched history element.")
(defvar comint-matching-input-from-input-string ""
  "Input previously used to match input history.")
553 554 555 556 557 558 559
(defvar comint-save-input-ring-index
  "Last input ring index which you copied.
This is to support the command \\[comint-get-next-from-history].")

(defvar comint-accum-marker nil
  "Non-nil if you are accumulating input lines to send as input together.
The command \\[comint-accumulate] sets this.")
560 561

(put 'comint-replace-by-expanded-history 'menu-enable 'comint-input-autoexpand)
Richard M. Stallman's avatar
Richard M. Stallman committed
562
(put 'comint-input-ring 'permanent-local t)
563
(put 'comint-input-ring-index 'permanent-local t)
564
(put 'comint-save-input-ring-index 'permanent-local t)
565
(put 'comint-input-autoexpand 'permanent-local t)
566 567
(put 'comint-input-filter-functions 'permanent-local t)
(put 'comint-output-filter-functions 'permanent-local t)
568
(put 'comint-preoutput-filter-functions 'permanent-local t)
569
(put 'comint-scroll-to-bottom-on-input 'permanent-local t)
570
(put 'comint-move-point-for-output 'permanent-local t)
571
(put 'comint-scroll-show-maximum-output 'permanent-local t)
Richard M. Stallman's avatar
Richard M. Stallman committed
572
(put 'comint-ptyp 'permanent-local t)
573

574 575
(put 'comint-mode 'mode-class 'special)

576
(define-derived-mode comint-mode fundamental-mode "Comint"
David Lawrence's avatar
David Lawrence committed
577 578 579 580
  "Major mode for interacting with an inferior interpreter.
Interpreter name is same as buffer name, sans the asterisks.
Return at end of buffer sends line as input.
Return not at end copies rest of line to end and sends it.
581
Setting variable `comint-eol-on-send' means jump to the end of the line
Jim Blandy's avatar
Jim Blandy committed
582
before submitting new input.
David Lawrence's avatar
David Lawrence committed
583

584
This mode is customized to create major modes such as Inferior Lisp
585
mode, Shell mode, etc.  This can be done by setting the hooks
586
`comint-input-filter-functions', `comint-input-filter', `comint-input-sender'
587
and `comint-get-old-input' to appropriate functions, and the variable
588
`comint-prompt-regexp' to the appropriate regular expression.
David Lawrence's avatar
David Lawrence committed
589

590
An input history is maintained of size `comint-input-ring-size', and
591 592 593 594 595 596
can be accessed with the commands \\[comint-next-input], \\[comint-previous-input], and \\[comint-dynamic-list-input-ring].
Input ring history expansion can be achieved with the commands
\\[comint-replace-by-expanded-history] or \\[comint-magic-space].
Input ring expansion is controlled by the variable `comint-input-autoexpand',
and addition is controlled by the variable `comint-input-ignoredups'.

597
Commands with no default key bindings include `send-invisible',
Miles Bader's avatar
Miles Bader committed
598
`comint-dynamic-complete', `comint-dynamic-list-filename-completions', and
599 600 601
`comint-magic-space'.

Input to, and output from, the subprocess can cause the window to scroll to
602
the end of the buffer.  See variables `comint-output-filter-functions',
603
`comint-preoutput-filter-functions', `comint-scroll-to-bottom-on-input',
604
and `comint-move-point-for-output'.
David Lawrence's avatar
David Lawrence committed
605 606 607 608 609 610

If you accidentally suspend your process, use \\[comint-continue-subjob]
to continue it.

\\{comint-mode-map}

611
Entry to this mode runs the hooks on `comint-mode-hook'."
612
  (setq mode-line-process '(":%s"))
Stefan Monnier's avatar
Stefan Monnier committed
613 614 615
  (set (make-local-variable 'comint-last-input-start) (point-min-marker))
  (set (make-local-variable 'comint-last-input-end) (point-min-marker))
  (set (make-local-variable 'comint-last-output-start) (make-marker))
616
  (make-local-variable 'comint-last-prompt-overlay)
617 618 619 620 621 622 623
  (make-local-variable 'comint-prompt-regexp)        ; Don't set; default
  (make-local-variable 'comint-input-ring-size)      ; ...to global val.
  (make-local-variable 'comint-input-ring)
  (make-local-variable 'comint-input-ring-file-name)
  (or (and (boundp 'comint-input-ring) comint-input-ring)
      (setq comint-input-ring (make-ring comint-input-ring-size)))
  (make-local-variable 'comint-input-ring-index)
624
  (make-local-variable 'comint-save-input-ring-index)
625 626
  (or (and (boundp 'comint-input-ring-index) comint-input-ring-index)
      (setq comint-input-ring-index nil))
627 628
  (or (and (boundp 'comint-save-input-ring-index) comint-save-input-ring-index)
      (setq comint-save-input-ring-index nil))
629 630 631 632
  (make-local-variable 'comint-matching-input-from-input-string)
  (make-local-variable 'comint-input-autoexpand)
  (make-local-variable 'comint-input-ignoredups)
  (make-local-variable 'comint-delimiter-argument-list)
633
  (make-local-variable 'comint-completion-fignore)
634 635 636 637 638
  (make-local-variable 'comint-get-old-input)
  (make-local-variable 'comint-input-filter)
  (make-local-variable 'comint-input-sender)
  (make-local-variable 'comint-eol-on-send)
  (make-local-variable 'comint-scroll-to-bottom-on-input)
639
  (make-local-variable 'comint-move-point-for-output)
640
  (make-local-variable 'comint-scroll-show-maximum-output)
641 642 643
  ;; This makes it really work to keep point at the bottom.
  (make-local-variable 'scroll-conservatively)
  (setq scroll-conservatively 10000)
644
  (add-hook 'pre-command-hook 'comint-preinput-scroll-to-bottom t t)
645 646
  (make-local-variable 'comint-ptyp)
  (make-local-variable 'comint-process-echoes)
647
  (make-local-variable 'comint-file-name-chars)
648
  (make-local-variable 'comint-file-name-quote-list)
Stefan Monnier's avatar
Stefan Monnier committed
649
  (set (make-local-variable 'comint-accum-marker) (make-marker))
650
  (add-hook 'change-major-mode-hook 'font-lock-defontify nil t)
651 652
  ;; This behavior is not useful in comint buffers, and is annoying
  (set (make-local-variable 'next-line-add-newlines) nil))
David Lawrence's avatar
David Lawrence committed
653

Jim Blandy's avatar
Jim Blandy committed
654
(defun comint-check-proc (buffer)
655
  "Return non-nil if there is a living process associated w/buffer BUFFER.
656
Living means the status is `open', `run', or `stop'.
657
BUFFER can be either a buffer or the name of one."
Jim Blandy's avatar
Jim Blandy committed
658
  (let ((proc (get-buffer-process buffer)))
659
    (and proc (memq (process-status proc) '(open run stop)))))
David Lawrence's avatar
David Lawrence committed
660

661 662
;;;###autoload
(defun make-comint-in-buffer (name buffer program &optional startfile &rest switches)
Stefan Monnier's avatar
Stefan Monnier committed
663
  "Make a Comint process NAME in BUFFER, running PROGRAM.
664 665 666 667
If BUFFER is nil, it defaults to NAME surrounded by `*'s.
PROGRAM should be either a string denoting an executable program to create
via `start-process', or a cons pair of the form (HOST . SERVICE) denoting a TCP
connection to be opened via `open-network-stream'.  If there is already a
668
running process in that buffer, it is not restarted.  Optional fourth arg
669 670 671 672 673 674 675 676 677 678
STARTFILE is the name of a file to send the contents of to the process.

If PROGRAM is a string, any more args are arguments to PROGRAM."
  (or (fboundp 'start-process)
      (error "Multi-processing is not supported for this system"))
  (setq buffer (get-buffer-create (or buffer (concat "*" name "*"))))
  ;; If no process, or nuked process, crank up a new one and put buffer in
  ;; comint mode.  Otherwise, leave buffer and existing process alone.
  (unless (comint-check-proc buffer)
    (with-current-buffer buffer
Stefan Monnier's avatar
Stefan Monnier committed
679 680
      (unless (derived-mode-p 'comint-mode)
	(comint-mode))) ; Install local vars, mode, keymap, ...
681 682 683
    (comint-exec buffer name program startfile switches))
  buffer)

684
;;;###autoload
David Lawrence's avatar
David Lawrence committed
685
(defun make-comint (name program &optional startfile &rest switches)
Stefan Monnier's avatar
Stefan Monnier committed
686
  "Make a Comint process NAME in a buffer, running PROGRAM.
687
The name of the buffer is made by surrounding NAME with `*'s.
688 689 690 691
PROGRAM should be either a string denoting an executable program to create
via `start-process', or a cons pair of the form (HOST . SERVICE) denoting a TCP
connection to be opened via `open-network-stream'.  If there is already a
running process in that buffer, it is not restarted.  Optional third arg
Stefan Monnier's avatar
Stefan Monnier committed
692
STARTFILE is the name of a file to send the contents of the process to.
693 694

If PROGRAM is a string, any more args are arguments to PROGRAM."
695
  (apply #'make-comint-in-buffer name nil program startfile switches))
David Lawrence's avatar
David Lawrence committed
696

697
;;;###autoload
698
(defun comint-run (program)
Stefan Monnier's avatar
Stefan Monnier committed
699
  "Run PROGRAM in a Comint buffer and switch to it.
700 701 702 703 704 705 706 707 708
The buffer name is made by surrounding the file name of PROGRAM with `*'s.
The file name is used to make a symbol name, such as `comint-sh-hook', and any
hooks on this symbol are run in the buffer.
See `make-comint' and `comint-exec'."
  (interactive "sRun program: ")
  (let ((name (file-name-nondirectory program)))
    (switch-to-buffer (make-comint name program))
    (run-hooks (intern-soft (concat "comint-" name "-hook")))))

David Lawrence's avatar
David Lawrence committed
709
(defun comint-exec (buffer name command startfile switches)
Stefan Monnier's avatar
Stefan Monnier committed
710 711
  "Start up a process named NAME in buffer BUFFER for Comint modes.
Runs the given COMMAND with SWITCHES with output to STARTFILE.
712
Blasts any old process running in the buffer.  Doesn't set the buffer mode.
Stefan Monnier's avatar
Stefan Monnier committed
713
You can use this to cheaply run a series of processes in the same Comint
714
buffer.  The hook `comint-exec-hook' is run after each exec."
715
  (with-current-buffer buffer
David Lawrence's avatar
David Lawrence committed
716 717 718
    (let ((proc (get-buffer-process buffer)))	; Blast any old process.
      (if proc (delete-process proc)))
    ;; Crank up a new process
719 720 721 722
    (let ((proc
	   (if (consp command)
	       (open-network-stream name buffer (car command) (cdr command))
	     (comint-exec-1 name buffer command switches))))
723
      (set-process-filter proc 'comint-output-filter)
Jim Blandy's avatar
Jim Blandy committed
724
      (make-local-variable 'comint-ptyp)
Pavel Janík's avatar
Pavel Janík committed
725
      (setq comint-ptyp process-connection-type) ; t if pty, nil if pipe.
David Lawrence's avatar
David Lawrence committed
726
      ;; Jump to the end, and set the process mark.
Jim Blandy's avatar
Jim Blandy committed
727
      (goto-char (point-max))
Jim Blandy's avatar
Jim Blandy committed
728
      (set-marker (process-mark proc) (point))
David Lawrence's avatar
David Lawrence committed
729 730 731 732 733 734 735 736 737 738 739 740
      ;; Feed it the startfile.
      (cond (startfile
	     ;;This is guaranteed to wait long enough
	     ;;but has bad results if the comint does not prompt at all
	     ;;	     (while (= size (buffer-size))
	     ;;	       (sleep-for 1))
	     ;;I hope 1 second is enough!
	     (sleep-for 1)
	     (goto-char (point-max))
	     (insert-file-contents startfile)
	     (setq startfile (buffer-substring (point) (point-max)))
	     (delete-region (point) (point-max))
Jim Blandy's avatar
Jim Blandy committed
741 742
	     (comint-send-string proc startfile)))
    (run-hooks 'comint-exec-hook)
Jim Blandy's avatar
Jim Blandy committed
743
    buffer)))
David Lawrence's avatar
David Lawrence committed
744

Erik Naggum's avatar
Erik Naggum committed
745 746
;; This auxiliary function cranks up the process for comint-exec in
;; the appropriate environment.
David Lawrence's avatar
David Lawrence committed
747 748

(defun comint-exec-1 (name buffer command switches)
749
  (let ((process-environment
750
	 (nconc
Richard M. Stallman's avatar
Richard M. Stallman committed
751 752
	  ;; If using termcap, we specify `emacs' as the terminal type
	  ;; because that lets us specify a width.
753
	  ;; If using terminfo, we specify `dumb' because that is
Richard M. Stallman's avatar
Richard M. Stallman committed
754 755 756 757
	  ;; a defined terminal type.  `emacs' is not a defined terminal type
	  ;; and there is no way for us to define it here.
	  ;; Some programs that use terminfo get very confused
	  ;; if TERM is not a valid terminal type.
Richard M. Stallman's avatar
Richard M. Stallman committed
758
	  ;; ;; There is similar code in compile.el.
759
	  (if (and (boundp 'system-uses-terminfo) system-uses-terminfo)
760
	      (list "TERM=dumb" "TERMCAP="
761
		    (format "COLUMNS=%d" (window-width)))
762
	    (list "TERM=emacs"
763
		  (format "TERMCAP=emacs:co#%d:tc=unknown:" (window-width))))
764
	  (if (getenv "EMACS") nil (list "EMACS=t"))
765 766
	  process-environment))
	(default-directory
767
	  (if (file-accessible-directory-p default-directory)
768
	      default-directory
769
	    "/"))
770
	proc decoding encoding changed)
771 772 773 774 775
    (let ((exec-path (if (file-name-directory command)
			 ;; If the command has slashes, make sure we
			 ;; first look relative to the current directory.
			 (cons default-directory exec-path) exec-path)))
      (setq proc (apply 'start-process name buffer command switches)))
776 777 778 779
    (let ((coding-systems (process-coding-system proc)))
      (setq decoding (car coding-systems)
	    encoding (cdr coding-systems)))
    ;; If start-process decided to use some coding system for decoding
780
    ;; data sent from the process and the coding system doesn't
781 782 783 784 785 786 787 788 789 790 791 792 793 794
    ;; specify EOL conversion, we had better convert CRLF to LF.
    (if (vectorp (coding-system-eol-type decoding))
	(setq decoding (coding-system-change-eol-conversion decoding 'dos)
	      changed t))
    ;; Even if start-process left the coding system for encoding data
    ;; sent from the process undecided, we had better use the same one
    ;; as what we use for decoding.  But, we should suppress EOL
    ;; conversion.
    (if (and decoding (not encoding))
	(setq encoding (coding-system-change-eol-conversion decoding 'unix)
	      changed t))
    (if changed
	(set-process-coding-system proc decoding encoding))
    proc))
795

796
(defun comint-insert-input (&optional event)
797
  "In a Comint buffer, set the current input to the previous input at point."
798 799 800
  ;; This doesn't use "e" because it is supposed to work
  ;; for events without parameters.
  (interactive (list last-input-event))
801
  (let ((pos (point)))
802 803
    (if event (mouse-set-point event))
    (if (not (eq (get-char-property (point) 'field) 'input))
804 805 806 807
	;; No input at POS, fall back to the global definition.
	(let* ((keys (this-command-keys))
	       (last-key (and (vectorp keys) (aref keys (1- (length keys)))))
	       (fun (and last-key (lookup-key global-map (vector last-key)))))
808
	  (goto-char pos)
809
	  (and fun (call-interactively fun)))
810
      (setq pos (point))
811 812 813 814 815 816 817
      ;; There's previous input at POS, insert it at the end of the buffer.
      (goto-char (point-max))
      ;; First delete any old unsent input at the end
      (delete-region
       (or (marker-position comint-accum-marker)
	   (process-mark (get-buffer-process (current-buffer))))
       (point))
818
      ;; Insert the input at point
819 820
      (insert (buffer-substring-no-properties
	       (previous-single-char-property-change (1+ pos) 'field)
821
	       (next-single-char-property-change pos 'field))))))
Miles Bader's avatar
Miles Bader committed
822

David Lawrence's avatar
David Lawrence committed
823

Erik Naggum's avatar
Erik Naggum committed
824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845
;; Input history processing in a buffer
;; ===========================================================================
;; Useful input history functions, courtesy of the Ergo group.

;; Eleven commands:
;; comint-dynamic-list-input-ring	List history in help buffer.
;; comint-previous-input		Previous input...
;; comint-previous-matching-input	...matching a string.
;; comint-previous-matching-input-from-input ... matching the current input.
;; comint-next-input			Next input...
;; comint-next-matching-input		...matching a string.
;; comint-next-matching-input-from-input     ... matching the current input.
;; comint-backward-matching-input      Backwards input...
;; comint-forward-matching-input       ...matching a string.
;; comint-replace-by-expanded-history	Expand history at point;
;;					replace with expanded history.
;; comint-magic-space			Expand history and insert space.
;;
;; Three functions:
;; comint-read-input-ring              Read into comint-input-ring...
;; comint-write-input-ring             Write to comint-input-ring-file-name.
;; comint-replace-by-expanded-history-before-point Workhorse function.
846

847
(defun comint-read-input-ring (&optional silent)
Stefan Monnier's avatar
Stefan Monnier committed
848
  "Set the buffer's `comint-input-ring' from a history file.
849 850 851 852
The name of the file is given by the variable `comint-input-ring-file-name'.
The history ring is of size `comint-input-ring-size', regardless of file size.
If `comint-input-ring-file-name' is nil this function does nothing.

853 854
If the optional argument SILENT is non-nil, we say nothing about a
failure to read the history file.
855

856 857
This function is useful for major mode commands and mode hooks.

858
The commands stored in the history file are separated by the
859 860 861
`comint-input-ring-separator', and entries that match
`comint-input-history-ignore' are ignored.  The most recent command
comes last.
862

863
See also `comint-input-ignoredups' and `comint-write-input-ring'."
864