Commit | Line | Data |
---|---|---|
c0274f38 | 1 | ;;; comint.el --- general command interpreter in a window stuff |
e41b2db1 | 2 | |
17193f89 | 3 | ;; Copyright (C) 1988, 90, 92, 93, 94, 95, 96, 97, 98 Free Software Foundation, Inc. |
c0274f38 | 4 | |
f8604035 SM |
5 | ;; Author: Olin Shivers <shivers@cs.cmu.edu> then |
6 | ;; Simon Marshall <simon@gnu.ai.mit.edu> | |
7 | ;; Maintainer: FSF | |
e9571d2a | 8 | ;; Keywords: processes |
3a801d0c | 9 | |
1586b965 | 10 | ;; This file is part of GNU Emacs. |
be9b65ac | 11 | |
1586b965 RS |
12 | ;; GNU Emacs is free software; you can redistribute it and/or modify |
13 | ;; it under the terms of the GNU General Public License as published by | |
14 | ;; the Free Software Foundation; either version 2, or (at your option) | |
15 | ;; any later version. | |
be9b65ac | 16 | |
1586b965 RS |
17 | ;; GNU Emacs is distributed in the hope that it will be useful, |
18 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
19 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
20 | ;; GNU General Public License for more details. | |
be9b65ac | 21 | |
1586b965 | 22 | ;; You should have received a copy of the GNU General Public License |
b578f267 EN |
23 | ;; along with GNU Emacs; see the file COPYING. If not, write to the |
24 | ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
25 | ;; Boston, MA 02111-1307, USA. | |
be9b65ac | 26 | |
e5167999 ER |
27 | ;;; Commentary: |
28 | ||
b578f267 EN |
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) | |
32 | ;; - Simon Marshall (simon@gnu.ai.mit.edu) | |
33 | ||
34 | ;; This file defines a general command-interpreter-in-a-buffer package | |
35 | ;; (comint mode). The idea is that you can build specific process-in-a-buffer | |
36 | ;; modes on top of comint mode -- e.g., lisp, shell, scheme, T, soar, .... | |
37 | ;; This way, all these specific packages share a common base functionality, | |
38 | ;; and a common set of bindings, which makes them easier to use (and | |
39 | ;; saves code, implementation time, etc., etc.). | |
40 | ||
41 | ;; Several packages are already defined using comint mode: | |
42 | ;; - shell.el defines a shell-in-a-buffer mode. | |
43 | ;; - cmulisp.el defines a simple lisp-in-a-buffer mode. | |
44 | ;; | |
45 | ;; - The file cmuscheme.el defines a scheme-in-a-buffer mode. | |
46 | ;; - The file tea.el tunes scheme and inferior-scheme modes for T. | |
47 | ;; - The file soar.el tunes lisp and inferior-lisp modes for Soar. | |
48 | ;; - cmutex.el defines tex and latex modes that invoke tex, latex, bibtex, | |
49 | ;; previewers, and printers from within emacs. | |
50 | ;; - background.el allows csh-like job control inside emacs. | |
51 | ;; It is pretty easy to make new derived modes for other processes. | |
52 | ||
53 | ;; For documentation on the functionality provided by comint mode, and | |
54 | ;; the hooks available for customising it, see the comments below. | |
55 | ;; For further information on the standard derived modes (shell, | |
56 | ;; inferior-lisp, inferior-scheme, ...), see the relevant source files. | |
57 | ||
58 | ;; For hints on converting existing process modes (e.g., tex-mode, | |
59 | ;; background, dbx, gdb, kermit, prolog, telnet) to use comint-mode | |
60 | ;; instead of shell-mode, see the notes at the end of this file. | |
be9b65ac | 61 | |
be9b65ac | 62 | \f |
b578f267 EN |
63 | ;; Brief Command Documentation: |
64 | ;;============================================================================ | |
65 | ;; Comint Mode Commands: (common to all derived modes, like shell & cmulisp | |
66 | ;; mode) | |
67 | ;; | |
361ecbf2 RS |
68 | ;; m-p comint-previous-input Cycle backwards in input history |
69 | ;; m-n comint-next-input Cycle forwards | |
b578f267 EN |
70 | ;; m-r comint-previous-matching-input Previous input matching a regexp |
71 | ;; m-s comint-next-matching-input Next input that matches | |
361ecbf2 | 72 | ;; m-c-l comint-show-output Show last batch of process output |
b578f267 | 73 | ;; return comint-send-input |
361ecbf2 RS |
74 | ;; c-d comint-delchar-or-maybe-eof Delete char unless at end of buff |
75 | ;; c-c c-a comint-bol-or-process-mark First time, move point to bol; | |
76 | ;; second time, move to process-mark. | |
b578f267 EN |
77 | ;; c-c c-u comint-kill-input ^u |
78 | ;; c-c c-w backward-kill-word ^w | |
79 | ;; c-c c-c comint-interrupt-subjob ^c | |
80 | ;; c-c c-z comint-stop-subjob ^z | |
81 | ;; c-c c-\ comint-quit-subjob ^\ | |
82 | ;; c-c c-o comint-kill-output Delete last batch of process output | |
83 | ;; c-c c-r comint-show-output Show last batch of process output | |
84 | ;; c-c c-l comint-dynamic-list-input-ring List input history | |
85 | ;; | |
86 | ;; Not bound by default in comint-mode (some are in shell mode) | |
87 | ;; comint-run Run a program under comint-mode | |
88 | ;; send-invisible Read a line w/o echo, and send to proc | |
89 | ;; comint-dynamic-complete-filename Complete filename at point. | |
90 | ;; comint-dynamic-complete-variable Complete variable name at point. | |
91 | ;; comint-dynamic-list-filename-completions List completions in help buffer. | |
92 | ;; comint-replace-by-expanded-filename Expand and complete filename at point; | |
93 | ;; replace with expanded/completed name. | |
94 | ;; comint-replace-by-expanded-history Expand history at point; | |
95 | ;; replace with expanded name. | |
96 | ;; comint-magic-space Expand history and add (a) space(s). | |
97 | ;; comint-kill-subjob No mercy. | |
98 | ;; comint-show-maximum-output Show as much output as possible. | |
99 | ;; comint-continue-subjob Send CONT signal to buffer's process | |
100 | ;; group. Useful if you accidentally | |
101 | ;; suspend your process (with C-c C-z). | |
361ecbf2 RS |
102 | ;; comint-get-next-from-history Fetch successive input history lines |
103 | ;; comint-accumulate Combine lines to send them together | |
104 | ;; as input. | |
105 | ;; comint-goto-process-mark Move point to where process-mark is. | |
106 | ;; comint-set-process-mark Set process-mark to point. | |
b578f267 EN |
107 | |
108 | ;; comint-mode-hook is the comint mode hook. Basically for your keybindings. | |
be9b65ac | 109 | |
e5167999 ER |
110 | ;;; Code: |
111 | ||
c9706a52 | 112 | (require 'ring) |
be9b65ac | 113 | \f |
b578f267 EN |
114 | ;; Buffer Local Variables: |
115 | ;;============================================================================ | |
116 | ;; Comint mode buffer local variables: | |
107d7589 SM |
117 | ;; comint-prompt-regexp string comint-bol uses to match prompt |
118 | ;; comint-delimiter-argument-list list For delimiters and arguments | |
119 | ;; comint-last-input-start marker Handy if inferior always echoes | |
120 | ;; comint-last-input-end marker For comint-kill-output command | |
121 | ;; comint-input-ring-size integer For the input history | |
122 | ;; comint-input-ring ring mechanism | |
123 | ;; comint-input-ring-index number ... | |
361ecbf2 | 124 | ;; comint-save-input-ring-index number ... |
107d7589 SM |
125 | ;; comint-input-autoexpand symbol ... |
126 | ;; comint-input-ignoredups boolean ... | |
127 | ;; comint-last-input-match string ... | |
128 | ;; comint-dynamic-complete-functions hook For the completion mechanism | |
129 | ;; comint-completion-fignore list ... | |
2d4382e5 | 130 | ;; comint-file-name-chars string ... |
107d7589 SM |
131 | ;; comint-file-name-quote-list list ... |
132 | ;; comint-get-old-input function Hooks for specific | |
133 | ;; comint-input-filter-functions hook process-in-a-buffer | |
134 | ;; comint-output-filter-functions hook function modes. | |
d4321160 | 135 | ;; comint-preoutput-filter-functions hook |
107d7589 SM |
136 | ;; comint-input-filter function ... |
137 | ;; comint-input-sender function ... | |
138 | ;; comint-eol-on-send boolean ... | |
139 | ;; comint-process-echoes boolean ... | |
140 | ;; comint-scroll-to-bottom-on-input symbol For scroll behavior | |
141 | ;; comint-scroll-to-bottom-on-output symbol ... | |
142 | ;; comint-scroll-show-maximum-output boolean ... | |
361ecbf2 | 143 | ;; comint-accum-marker maker For comint-accumulate |
b578f267 EN |
144 | ;; |
145 | ;; Comint mode non-buffer local variables: | |
107d7589 SM |
146 | ;; comint-completion-addsuffix boolean/cons For file name |
147 | ;; comint-completion-autolist boolean completion behavior | |
148 | ;; comint-completion-recexact boolean ... | |
be9b65ac | 149 | |
ed366e87 RS |
150 | (defgroup comint nil |
151 | "General command interpreter in a window stuff." | |
152 | :group 'processes) | |
153 | ||
154 | (defgroup comint-completion nil | |
155 | "Completion facilities in comint" | |
156 | :group 'comint) | |
157 | ||
158 | (defgroup comint-source nil | |
159 | "Source finding facilities in comint" | |
160 | :prefix "comint-" | |
161 | :group 'comint) | |
162 | ||
163 | ||
49116ac0 JB |
164 | (defvar comint-prompt-regexp "^" |
165 | "Regexp to recognise prompts in the inferior process. | |
166 | Defaults to \"^\", the null string at BOL. | |
be9b65ac DL |
167 | |
168 | Good choices: | |
d637fa56 | 169 | Canonical Lisp: \"^[^> \\n]*>+:? *\" (Lucid, franz, kcl, T, cscheme, oaklisp) |
41752cf1 JB |
170 | Lucid Common Lisp: \"^\\\\(>\\\\|\\\\(->\\\\)+\\\\) *\" |
171 | franz: \"^\\\\(->\\\\|<[0-9]*>:\\\\) *\" | |
49116ac0 | 172 | kcl: \"^>+ *\" |
d637fa56 | 173 | shell: \"^[^#$%>\\n]*[#$%>] *\" |
49116ac0 | 174 | T: \"^>+ *\" |
be9b65ac | 175 | |
49116ac0 | 176 | This is a good thing to set in mode hooks.") |
be9b65ac | 177 | |
3ee91e68 RS |
178 | (defvar comint-delimiter-argument-list () |
179 | "List of characters to recognise as separate arguments in input. | |
180 | Strings comprising a character in this list will separate the arguments | |
181 | surrounding them, and also be regarded as arguments in their own right (unlike | |
182 | whitespace). See `comint-arguments'. | |
3f4b7c36 | 183 | Defaults to the empty list. |
3ee91e68 | 184 | |
3f4b7c36 | 185 | For shells, a good value is (?\\| ?& ?< ?> ?\\( ?\\) ?;). |
3ee91e68 RS |
186 | |
187 | This is a good thing to set in mode hooks.") | |
188 | ||
ed366e87 | 189 | (defcustom comint-input-autoexpand nil |
3ee91e68 | 190 | "*If non-nil, expand input command history references on completion. |
fdb7791d | 191 | This mirrors the optional behavior of tcsh (its autoexpand and histlit). |
3ee91e68 RS |
192 | |
193 | If the value is `input', then the expansion is seen on input. | |
194 | If the value is `history', then the expansion is only when inserting | |
195 | into the buffer's input ring. See also `comint-magic-space' and | |
196 | `comint-dynamic-complete'. | |
197 | ||
ed366e87 RS |
198 | This variable is buffer-local." |
199 | :type '(choice (const :tag "off" nil) | |
200 | (const :tag "on" t) | |
201 | (const input) | |
202 | (const history)) | |
203 | :group 'comint) | |
204 | ||
ed366e87 | 205 | (defcustom comint-input-ignoredups nil |
3ee91e68 | 206 | "*If non-nil, don't add input matching the last on the input ring. |
fdb7791d | 207 | This mirrors the optional behavior of bash. |
3ee91e68 | 208 | |
ed366e87 RS |
209 | This variable is buffer-local." |
210 | :type 'boolean | |
211 | :group 'comint) | |
3ee91e68 | 212 | |
ed366e87 | 213 | (defcustom comint-input-ring-file-name nil |
3ee91e68 RS |
214 | "*If non-nil, name of the file to read/write input history. |
215 | See also `comint-read-input-ring' and `comint-write-input-ring'. | |
216 | ||
ed366e87 RS |
217 | This variable is buffer-local, and is a good thing to set in mode hooks." |
218 | :type 'boolean | |
219 | :group 'comint) | |
3ee91e68 | 220 | |
ed366e87 | 221 | (defcustom comint-scroll-to-bottom-on-input nil |
3ee91e68 RS |
222 | "*Controls whether input to interpreter causes window to scroll. |
223 | If nil, then do not scroll. If t or `all', scroll all windows showing buffer. | |
224 | If `this', scroll only the selected window. | |
225 | ||
91d75282 | 226 | The default is nil. |
f4ef65c7 | 227 | |
ed366e87 RS |
228 | See `comint-preinput-scroll-to-bottom'. This variable is buffer-local." |
229 | :type '(choice (const :tag "off" nil) | |
230 | (const t) | |
231 | (const all) | |
232 | (const this)) | |
233 | :group 'comint) | |
3ee91e68 | 234 | |
ed366e87 | 235 | (defcustom comint-scroll-to-bottom-on-output nil |
3ee91e68 RS |
236 | "*Controls whether interpreter output causes window to scroll. |
237 | If nil, then do not scroll. If t or `all', scroll all windows showing buffer. | |
238 | If `this', scroll only the selected window. | |
239 | If `others', scroll only those that are not the selected window. | |
91d75282 RS |
240 | |
241 | The default is nil. | |
f4ef65c7 | 242 | |
3ee91e68 | 243 | See variable `comint-scroll-show-maximum-output' and function |
ed366e87 RS |
244 | `comint-postoutput-scroll-to-bottom'. This variable is buffer-local." |
245 | :type '(choice (const :tag "off" nil) | |
246 | (const t) | |
247 | (const all) | |
248 | (const this) | |
249 | (const others)) | |
250 | :group 'comint) | |
251 | ||
252 | (defcustom comint-scroll-show-maximum-output nil | |
3ee91e68 RS |
253 | "*Controls how interpreter output causes window to scroll. |
254 | If non-nil, then show the maximum output when the window is scrolled. | |
91d75282 | 255 | |
3ee91e68 | 256 | See variable `comint-scroll-to-bottom-on-output' and function |
ed366e87 RS |
257 | `comint-postoutput-scroll-to-bottom'. This variable is buffer-local." |
258 | :type 'boolean | |
259 | :group 'comint) | |
91d75282 | 260 | |
ed366e87 | 261 | (defcustom comint-buffer-maximum-size 1024 |
39a185a9 SM |
262 | "*The maximum size in lines for comint buffers. |
263 | Comint buffers are truncated from the top to be no greater than this number, if | |
ed366e87 RS |
264 | the function `comint-truncate-buffer' is on `comint-output-filter-functions'." |
265 | :type 'integer | |
266 | :group 'comint) | |
39a185a9 | 267 | |
3ee91e68 | 268 | (defvar comint-input-ring-size 32 |
49116ac0 | 269 | "Size of input history ring.") |
be9b65ac | 270 | |
ed366e87 | 271 | (defcustom comint-process-echoes nil |
cccd719e | 272 | "*If non-nil, assume that the subprocess echoes any input. |
8ae3bc9f | 273 | If so, delete one copy of the input so that only one copy eventually |
3ee91e68 | 274 | appears in the buffer. |
8ae3bc9f | 275 | |
ed366e87 RS |
276 | This variable is buffer-local." |
277 | :type 'boolean | |
278 | :group 'comint) | |
8ae3bc9f | 279 | |
ed366e87 RS |
280 | ;; AIX puts the name of the person being su'd to in from of the prompt. |
281 | (defcustom comint-password-prompt-regexp | |
f69925dd | 282 | "\\(\\([Oo]ld \\|[Nn]ew \\|'s \\|^\\)[Pp]assword\\|pass phrase\\):\\s *\\'" |
654d89d7 | 283 | "*Regexp matching prompts for passwords in the inferior process. |
ed366e87 RS |
284 | This is used by `comint-watch-for-password-prompt'." |
285 | :type 'regexp | |
286 | :group 'comint) | |
654d89d7 | 287 | |
b578f267 | 288 | ;; Here are the per-interpreter hooks. |
49116ac0 | 289 | (defvar comint-get-old-input (function comint-get-old-input-default) |
3ee91e68 | 290 | "Function that returns old text in comint mode. |
be9b65ac DL |
291 | This function is called when return is typed while the point is in old text. |
292 | It returns the text to be submitted as process input. The default is | |
cccd719e RS |
293 | `comint-get-old-input-default', which grabs the current line, and strips off |
294 | leading text matching `comint-prompt-regexp'.") | |
be9b65ac | 295 | |
1b421826 RS |
296 | (defvar comint-dynamic-complete-functions |
297 | '(comint-replace-by-expanded-history comint-dynamic-complete-filename) | |
298 | "List of functions called to perform completion. | |
299 | Functions should return non-nil if completion was performed. | |
300 | See also `comint-dynamic-complete'. | |
3ee91e68 RS |
301 | |
302 | This is a good thing to set in mode hooks.") | |
303 | ||
49116ac0 JB |
304 | (defvar comint-input-filter |
305 | (function (lambda (str) (not (string-match "\\`\\s *\\'" str)))) | |
306 | "Predicate for filtering additions to input history. | |
3ee91e68 RS |
307 | Takes one argument, the input. If non-nil, the input may be saved on the input |
308 | history list. Default is to save anything that isn't all whitespace.") | |
309 | ||
fb9ab28a | 310 | (defvar comint-input-filter-functions '() |
1b421826 RS |
311 | "Functions to call before input is sent to the process. |
312 | These functions get one argument, a string containing the text to send. | |
313 | ||
314 | This variable is buffer-local.") | |
315 | ||
fb9ab28a | 316 | (defvar comint-output-filter-functions '(comint-postoutput-scroll-to-bottom) |
3ee91e68 | 317 | "Functions to call after output is inserted into the buffer. |
c1da5c03 | 318 | One possible function is `comint-postoutput-scroll-to-bottom'. |
107d7589 SM |
319 | These functions get one argument, a string containing the text as originally |
320 | inserted. Note that this might not be the same as the buffer contents between | |
321 | `comint-last-output-start' and the buffer's `process-mark', if other filter | |
322 | functions have already modified the buffer. | |
3ee91e68 | 323 | |
d4321160 RS |
324 | See also `comint-preoutput-filter-functions'. |
325 | ||
3ee91e68 | 326 | This variable is buffer-local.") |
49116ac0 JB |
327 | |
328 | (defvar comint-input-sender (function comint-simple-send) | |
329 | "Function to actually send to PROCESS the STRING submitted by user. | |
cccd719e RS |
330 | Usually this is just `comint-simple-send', but if your mode needs to |
331 | massage the input string, put a different function here. | |
332 | `comint-simple-send' just sends the string plus a newline. | |
333 | This is called from the user command `comint-send-input'.") | |
49116ac0 | 334 | |
ed366e87 | 335 | (defcustom comint-eol-on-send t |
3ee91e68 | 336 | "*Non-nil means go to the end of the line before sending input. |
ed366e87 RS |
337 | See `comint-send-input'." |
338 | :type 'boolean | |
339 | :group 'comint) | |
be9b65ac | 340 | |
ed366e87 | 341 | (defcustom comint-mode-hook '() |
49116ac0 | 342 | "Called upon entry into comint-mode |
ed366e87 RS |
343 | This is run before the process is cranked up." |
344 | :type 'hook | |
345 | :group 'comint) | |
49116ac0 | 346 | |
ed366e87 | 347 | (defcustom comint-exec-hook '() |
cccd719e | 348 | "Called each time a process is exec'd by `comint-exec'. |
49116ac0 | 349 | This is called after the process is cranked up. It is useful for things that |
cccd719e RS |
350 | must be done each time a process is executed in a comint mode buffer (e.g., |
351 | `(process-kill-without-query)'). In contrast, the `comint-mode-hook' is only | |
ed366e87 RS |
352 | executed once when the buffer is created." |
353 | :type 'hook | |
354 | :group 'comint) | |
49116ac0 JB |
355 | |
356 | (defvar comint-mode-map nil) | |
be9b65ac | 357 | |
c9706a52 | 358 | (defvar comint-ptyp t |
3ee91e68 | 359 | "Non-nil if communications via pty; false if by pipe. Buffer local. |
5ef2f81f | 360 | This is to work around a bug in Emacs process signaling.") |
c9706a52 | 361 | |
0bcb5285 | 362 | (defvar comint-input-ring nil) |
e6dfdce5 RS |
363 | (defvar comint-last-input-start) |
364 | (defvar comint-last-input-end) | |
7d5d472e | 365 | (defvar comint-last-output-start) |
3ee91e68 RS |
366 | (defvar comint-input-ring-index nil |
367 | "Index of last matched history element.") | |
368 | (defvar comint-matching-input-from-input-string "" | |
369 | "Input previously used to match input history.") | |
361ecbf2 RS |
370 | (defvar comint-save-input-ring-index |
371 | "Last input ring index which you copied. | |
372 | This is to support the command \\[comint-get-next-from-history].") | |
373 | ||
374 | (defvar comint-accum-marker nil | |
375 | "Non-nil if you are accumulating input lines to send as input together. | |
376 | The command \\[comint-accumulate] sets this.") | |
21d9bf27 RS |
377 | |
378 | (put 'comint-replace-by-expanded-history 'menu-enable 'comint-input-autoexpand) | |
c9706a52 | 379 | (put 'comint-input-ring 'permanent-local t) |
3ee91e68 | 380 | (put 'comint-input-ring-index 'permanent-local t) |
361ecbf2 | 381 | (put 'comint-save-input-ring-index 'permanent-local t) |
3ee91e68 | 382 | (put 'comint-input-autoexpand 'permanent-local t) |
fb9ab28a RS |
383 | (put 'comint-input-filter-functions 'permanent-local t) |
384 | (put 'comint-output-filter-functions 'permanent-local t) | |
d4321160 | 385 | (put 'comint-preoutput-filter-functions 'permanent-local t) |
3ee91e68 RS |
386 | (put 'comint-scroll-to-bottom-on-input 'permanent-local t) |
387 | (put 'comint-scroll-to-bottom-on-output 'permanent-local t) | |
388 | (put 'comint-scroll-show-maximum-output 'permanent-local t) | |
c9706a52 | 389 | (put 'comint-ptyp 'permanent-local t) |
e6dfdce5 | 390 | |
158cfdd2 RS |
391 | (put 'comint-mode 'mode-class 'special) |
392 | ||
be9b65ac DL |
393 | (defun comint-mode () |
394 | "Major mode for interacting with an inferior interpreter. | |
395 | Interpreter name is same as buffer name, sans the asterisks. | |
396 | Return at end of buffer sends line as input. | |
397 | Return not at end copies rest of line to end and sends it. | |
cccd719e | 398 | Setting variable `comint-eol-on-send' means jump to the end of the line |
49116ac0 | 399 | before submitting new input. |
be9b65ac | 400 | |
3ee91e68 RS |
401 | This mode is customised to create major modes such as Inferior Lisp |
402 | mode, Shell mode, etc. This can be done by setting the hooks | |
fb9ab28a | 403 | `comint-input-filter-functions', `comint-input-filter', `comint-input-sender' |
3ee91e68 | 404 | and `comint-get-old-input' to appropriate functions, and the variable |
cccd719e | 405 | `comint-prompt-regexp' to the appropriate regular expression. |
be9b65ac | 406 | |
cccd719e | 407 | An input history is maintained of size `comint-input-ring-size', and |
3ee91e68 RS |
408 | can be accessed with the commands \\[comint-next-input], \\[comint-previous-input], and \\[comint-dynamic-list-input-ring]. |
409 | Input ring history expansion can be achieved with the commands | |
410 | \\[comint-replace-by-expanded-history] or \\[comint-magic-space]. | |
411 | Input ring expansion is controlled by the variable `comint-input-autoexpand', | |
412 | and addition is controlled by the variable `comint-input-ignoredups'. | |
413 | ||
cccd719e | 414 | Commands with no default key bindings include `send-invisible', |
21d9bf27 | 415 | `comint-dynamic-complete', `comint-dynamic-list-filename-completions', and |
3ee91e68 RS |
416 | `comint-magic-space'. |
417 | ||
418 | Input to, and output from, the subprocess can cause the window to scroll to | |
fb9ab28a | 419 | the end of the buffer. See variables `comint-output-filter-functions', |
d4321160 RS |
420 | `comint-preoutput-filter-functions', `comint-scroll-to-bottom-on-input', |
421 | and `comint-scroll-to-bottom-on-output'. | |
be9b65ac DL |
422 | |
423 | If you accidentally suspend your process, use \\[comint-continue-subjob] | |
424 | to continue it. | |
425 | ||
426 | \\{comint-mode-map} | |
427 | ||
3ee91e68 | 428 | Entry to this mode runs the hooks on `comint-mode-hook'." |
be9b65ac | 429 | (interactive) |
3ee91e68 RS |
430 | ;; Do not remove this. All major modes must do this. |
431 | (kill-all-local-variables) | |
432 | (setq major-mode 'comint-mode) | |
433 | (setq mode-name "Comint") | |
baae3c4e | 434 | (setq mode-line-process '(":%s")) |
3ee91e68 RS |
435 | (use-local-map comint-mode-map) |
436 | (make-local-variable 'comint-last-input-start) | |
437 | (setq comint-last-input-start (make-marker)) | |
a9a6f83f | 438 | (set-marker comint-last-input-start (point-min)) |
3ee91e68 RS |
439 | (make-local-variable 'comint-last-input-end) |
440 | (setq comint-last-input-end (make-marker)) | |
a9a6f83f | 441 | (set-marker comint-last-input-end (point-min)) |
3ee91e68 RS |
442 | (make-local-variable 'comint-last-output-start) |
443 | (setq comint-last-output-start (make-marker)) | |
444 | (make-local-variable 'comint-prompt-regexp) ; Don't set; default | |
445 | (make-local-variable 'comint-input-ring-size) ; ...to global val. | |
446 | (make-local-variable 'comint-input-ring) | |
447 | (make-local-variable 'comint-input-ring-file-name) | |
448 | (or (and (boundp 'comint-input-ring) comint-input-ring) | |
449 | (setq comint-input-ring (make-ring comint-input-ring-size))) | |
450 | (make-local-variable 'comint-input-ring-index) | |
361ecbf2 | 451 | (make-local-variable 'comint-save-input-ring-index) |
3ee91e68 RS |
452 | (or (and (boundp 'comint-input-ring-index) comint-input-ring-index) |
453 | (setq comint-input-ring-index nil)) | |
361ecbf2 RS |
454 | (or (and (boundp 'comint-save-input-ring-index) comint-save-input-ring-index) |
455 | (setq comint-save-input-ring-index nil)) | |
3ee91e68 RS |
456 | (make-local-variable 'comint-matching-input-from-input-string) |
457 | (make-local-variable 'comint-input-autoexpand) | |
458 | (make-local-variable 'comint-input-ignoredups) | |
459 | (make-local-variable 'comint-delimiter-argument-list) | |
155fd027 | 460 | (make-local-hook 'comint-dynamic-complete-functions) |
497513ad | 461 | (make-local-variable 'comint-completion-fignore) |
3ee91e68 | 462 | (make-local-variable 'comint-get-old-input) |
155fd027 | 463 | (make-local-hook 'comint-input-filter-functions) |
3ee91e68 RS |
464 | (make-local-variable 'comint-input-filter) |
465 | (make-local-variable 'comint-input-sender) | |
466 | (make-local-variable 'comint-eol-on-send) | |
467 | (make-local-variable 'comint-scroll-to-bottom-on-input) | |
468 | (make-local-variable 'comint-scroll-to-bottom-on-output) | |
469 | (make-local-variable 'comint-scroll-show-maximum-output) | |
c1da5c03 RS |
470 | (make-local-variable 'pre-command-hook) |
471 | (add-hook 'pre-command-hook 'comint-preinput-scroll-to-bottom) | |
155fd027 | 472 | (make-local-hook 'comint-output-filter-functions) |
3ee91e68 RS |
473 | (make-local-variable 'comint-ptyp) |
474 | (make-local-variable 'comint-exec-hook) | |
475 | (make-local-variable 'comint-process-echoes) | |
2d4382e5 | 476 | (make-local-variable 'comint-file-name-chars) |
59f36b08 | 477 | (make-local-variable 'comint-file-name-quote-list) |
361ecbf2 RS |
478 | (make-local-variable 'comint-accum-marker) |
479 | (setq comint-accum-marker (make-marker)) | |
480 | (set-marker comint-accum-marker nil) | |
3ee91e68 | 481 | (run-hooks 'comint-mode-hook)) |
be9b65ac | 482 | |
49116ac0 JB |
483 | (if comint-mode-map |
484 | nil | |
3ee91e68 | 485 | ;; Keys: |
49116ac0 JB |
486 | (setq comint-mode-map (make-sparse-keymap)) |
487 | (define-key comint-mode-map "\ep" 'comint-previous-input) | |
488 | (define-key comint-mode-map "\en" 'comint-next-input) | |
cf7c8463 RS |
489 | (define-key comint-mode-map [C-up] 'comint-previous-input) |
490 | (define-key comint-mode-map [C-down] 'comint-next-input) | |
0c5c1f34 RS |
491 | (define-key comint-mode-map "\er" 'comint-previous-matching-input) |
492 | (define-key comint-mode-map "\es" 'comint-next-matching-input) | |
fb597fe8 RS |
493 | (define-key comint-mode-map [?\C-c ?\M-r] 'comint-previous-matching-input-from-input) |
494 | (define-key comint-mode-map [?\C-c ?\M-s] 'comint-next-matching-input-from-input) | |
d637fa56 | 495 | (define-key comint-mode-map "\e\C-l" 'comint-show-output) |
49116ac0 JB |
496 | (define-key comint-mode-map "\C-m" 'comint-send-input) |
497 | (define-key comint-mode-map "\C-d" 'comint-delchar-or-maybe-eof) | |
361ecbf2 | 498 | (define-key comint-mode-map "\C-c " 'comint-accumulate) |
fb597fe8 | 499 | (define-key comint-mode-map "\C-c\C-x" 'comint-get-next-from-history) |
361ecbf2 | 500 | (define-key comint-mode-map "\C-c\C-a" 'comint-bol-or-process-mark) |
49116ac0 JB |
501 | (define-key comint-mode-map "\C-c\C-u" 'comint-kill-input) |
502 | (define-key comint-mode-map "\C-c\C-w" 'backward-kill-word) | |
503 | (define-key comint-mode-map "\C-c\C-c" 'comint-interrupt-subjob) | |
504 | (define-key comint-mode-map "\C-c\C-z" 'comint-stop-subjob) | |
505 | (define-key comint-mode-map "\C-c\C-\\" 'comint-quit-subjob) | |
3ee91e68 | 506 | (define-key comint-mode-map "\C-c\C-m" 'comint-copy-old-input) |
49116ac0 | 507 | (define-key comint-mode-map "\C-c\C-o" 'comint-kill-output) |
49116ac0 | 508 | (define-key comint-mode-map "\C-c\C-r" 'comint-show-output) |
3ee91e68 | 509 | (define-key comint-mode-map "\C-c\C-e" 'comint-show-maximum-output) |
4bec19b2 | 510 | (define-key comint-mode-map "\C-c\C-l" 'comint-dynamic-list-input-ring) |
1de80a34 | 511 | (define-key comint-mode-map "\C-c\C-n" 'comint-next-prompt) |
3ee91e68 | 512 | (define-key comint-mode-map "\C-c\C-p" 'comint-previous-prompt) |
1de80a34 | 513 | (define-key comint-mode-map "\C-c\C-d" 'comint-send-eof) |
3ee91e68 RS |
514 | ;; Menu bars: |
515 | ;; completion: | |
516 | (define-key comint-mode-map [menu-bar completion] | |
dcab7fbd | 517 | (cons "Complete" (make-sparse-keymap "Complete"))) |
3ee91e68 RS |
518 | (define-key comint-mode-map [menu-bar completion complete-expand] |
519 | '("Expand File Name" . comint-replace-by-expanded-filename)) | |
520 | (define-key comint-mode-map [menu-bar completion complete-listing] | |
521 | '("File Completion Listing" . comint-dynamic-list-filename-completions)) | |
3ee91e68 RS |
522 | (define-key comint-mode-map [menu-bar completion complete-file] |
523 | '("Complete File Name" . comint-dynamic-complete-filename)) | |
524 | (define-key comint-mode-map [menu-bar completion complete] | |
525 | '("Complete Before Point" . comint-dynamic-complete)) | |
526 | ;; Input history: | |
5c07a950 | 527 | (define-key comint-mode-map [menu-bar inout] |
dcab7fbd | 528 | (cons "In/Out" (make-sparse-keymap "In/Out"))) |
5c07a950 | 529 | (define-key comint-mode-map [menu-bar inout kill-output] |
dcab7fbd | 530 | '("Kill Current Output Group" . comint-kill-output)) |
5c07a950 | 531 | (define-key comint-mode-map [menu-bar inout next-prompt] |
dcab7fbd | 532 | '("Forward Output Group" . comint-next-prompt)) |
5c07a950 | 533 | (define-key comint-mode-map [menu-bar inout previous-prompt] |
dcab7fbd | 534 | '("Backward Output Group" . comint-previous-prompt)) |
5c07a950 | 535 | (define-key comint-mode-map [menu-bar inout show-maximum-output] |
dcab7fbd | 536 | '("Show Maximum Output" . comint-show-maximum-output)) |
5c07a950 | 537 | (define-key comint-mode-map [menu-bar inout show-output] |
dcab7fbd | 538 | '("Show Current Output Group" . comint-show-output)) |
5c07a950 | 539 | (define-key comint-mode-map [menu-bar inout kill-input] |
3ee91e68 | 540 | '("Kill Current Input" . comint-kill-input)) |
5c07a950 | 541 | (define-key comint-mode-map [menu-bar inout copy-input] |
3ee91e68 | 542 | '("Copy Old Input" . comint-copy-old-input)) |
5c07a950 | 543 | (define-key comint-mode-map [menu-bar inout forward-matching-history] |
dcab7fbd | 544 | '("Forward Matching Input..." . comint-forward-matching-input)) |
5c07a950 | 545 | (define-key comint-mode-map [menu-bar inout backward-matching-history] |
dcab7fbd | 546 | '("Backward Matching Input..." . comint-backward-matching-input)) |
5c07a950 | 547 | (define-key comint-mode-map [menu-bar inout next-matching-history] |
3ee91e68 | 548 | '("Next Matching Input..." . comint-next-matching-input)) |
5c07a950 | 549 | (define-key comint-mode-map [menu-bar inout previous-matching-history] |
3ee91e68 | 550 | '("Previous Matching Input..." . comint-previous-matching-input)) |
5c07a950 | 551 | (define-key comint-mode-map [menu-bar inout next-matching-history-from-input] |
3ee91e68 | 552 | '("Next Matching Current Input" . comint-next-matching-input-from-input)) |
5c07a950 | 553 | (define-key comint-mode-map [menu-bar inout previous-matching-history-from-input] |
3ee91e68 | 554 | '("Previous Matching Current Input" . comint-previous-matching-input-from-input)) |
5c07a950 | 555 | (define-key comint-mode-map [menu-bar inout next-history] |
3ee91e68 | 556 | '("Next Input" . comint-next-input)) |
5c07a950 | 557 | (define-key comint-mode-map [menu-bar inout previous-history] |
3ee91e68 | 558 | '("Previous Input" . comint-previous-input)) |
5c07a950 | 559 | (define-key comint-mode-map [menu-bar inout list-history] |
3ee91e68 | 560 | '("List Input History" . comint-dynamic-list-input-ring)) |
5c07a950 | 561 | (define-key comint-mode-map [menu-bar inout expand-history] |
3ee91e68 | 562 | '("Expand History Before Point" . comint-replace-by-expanded-history)) |
3ee91e68 RS |
563 | ;; Signals |
564 | (define-key comint-mode-map [menu-bar signals] | |
565 | (cons "Signals" (make-sparse-keymap "Signals"))) | |
566 | (define-key comint-mode-map [menu-bar signals eof] | |
567 | '("EOF" . comint-send-eof)) | |
568 | (define-key comint-mode-map [menu-bar signals kill] | |
569 | '("KILL" . comint-kill-subjob)) | |
570 | (define-key comint-mode-map [menu-bar signals quit] | |
571 | '("QUIT" . comint-quit-subjob)) | |
572 | (define-key comint-mode-map [menu-bar signals cont] | |
573 | '("CONT" . comint-continue-subjob)) | |
574 | (define-key comint-mode-map [menu-bar signals stop] | |
575 | '("STOP" . comint-stop-subjob)) | |
576 | (define-key comint-mode-map [menu-bar signals break] | |
577 | '("BREAK" . comint-interrupt-subjob)) | |
578 | ;; Put them in the menu bar: | |
5c07a950 | 579 | (setq menu-bar-final-items (append '(completion inout signals) |
3ee91e68 | 580 | menu-bar-final-items)) |
49116ac0 JB |
581 | ) |
582 | ||
49116ac0 | 583 | (defun comint-check-proc (buffer) |
3ee91e68 | 584 | "Return t if there is a living process associated w/buffer BUFFER. |
1a4086d2 | 585 | Living means the status is `open', `run', or `stop'. |
cccd719e | 586 | BUFFER can be either a buffer or the name of one." |
49116ac0 | 587 | (let ((proc (get-buffer-process buffer))) |
1a4086d2 | 588 | (and proc (memq (process-status proc) '(open run stop))))) |
be9b65ac | 589 | |
87206843 | 590 | ;;;###autoload |
be9b65ac | 591 | (defun make-comint (name program &optional startfile &rest switches) |
14a08856 RM |
592 | "Make a comint process NAME in a buffer, running PROGRAM. |
593 | The name of the buffer is made by surrounding NAME with `*'s. | |
1a4086d2 RS |
594 | PROGRAM should be either a string denoting an executable program to create |
595 | via `start-process', or a cons pair of the form (HOST . SERVICE) denoting a TCP | |
596 | connection to be opened via `open-network-stream'. If there is already a | |
597 | running process in that buffer, it is not restarted. Optional third arg | |
598 | STARTFILE is the name of a file to send the contents of to the process. | |
599 | ||
600 | If PROGRAM is a string, any more args are arguments to PROGRAM." | |
af37b2e6 RS |
601 | (or (fboundp 'start-process) |
602 | (error "Multi-processing is not supported for this system")) | |
49116ac0 | 603 | (let ((buffer (get-buffer-create (concat "*" name "*")))) |
be9b65ac | 604 | ;; If no process, or nuked process, crank up a new one and put buffer in |
14a08856 | 605 | ;; comint mode. Otherwise, leave buffer and existing process alone. |
1ff6cf10 | 606 | (cond ((not (comint-check-proc buffer)) |
49116ac0 | 607 | (save-excursion |
be9b65ac DL |
608 | (set-buffer buffer) |
609 | (comint-mode)) ; Install local vars, mode, keymap, ... | |
610 | (comint-exec buffer name program startfile switches))) | |
611 | buffer)) | |
612 | ||
87206843 | 613 | ;;;###autoload |
21d9bf27 RS |
614 | (defun comint-run (program) |
615 | "Run PROGRAM in a comint buffer and switch to it. | |
616 | The buffer name is made by surrounding the file name of PROGRAM with `*'s. | |
617 | The file name is used to make a symbol name, such as `comint-sh-hook', and any | |
618 | hooks on this symbol are run in the buffer. | |
619 | See `make-comint' and `comint-exec'." | |
620 | (interactive "sRun program: ") | |
621 | (let ((name (file-name-nondirectory program))) | |
622 | (switch-to-buffer (make-comint name program)) | |
623 | (run-hooks (intern-soft (concat "comint-" name "-hook"))))) | |
624 | ||
be9b65ac | 625 | (defun comint-exec (buffer name command startfile switches) |
cccd719e | 626 | "Start up a process in buffer BUFFER for comint modes. |
14a08856 | 627 | Blasts any old process running in the buffer. Doesn't set the buffer mode. |
49116ac0 | 628 | You can use this to cheaply run a series of processes in the same comint |
cccd719e | 629 | buffer. The hook `comint-exec-hook' is run after each exec." |
be9b65ac DL |
630 | (save-excursion |
631 | (set-buffer buffer) | |
632 | (let ((proc (get-buffer-process buffer))) ; Blast any old process. | |
633 | (if proc (delete-process proc))) | |
634 | ;; Crank up a new process | |
1a4086d2 RS |
635 | (let ((proc |
636 | (if (consp command) | |
637 | (open-network-stream name buffer (car command) (cdr command)) | |
638 | (comint-exec-1 name buffer command switches)))) | |
3ee91e68 | 639 | (set-process-filter proc 'comint-output-filter) |
49116ac0 JB |
640 | (make-local-variable 'comint-ptyp) |
641 | (setq comint-ptyp process-connection-type) ; T if pty, NIL if pipe. | |
be9b65ac | 642 | ;; Jump to the end, and set the process mark. |
49116ac0 | 643 | (goto-char (point-max)) |
87ef29fd | 644 | (set-marker (process-mark proc) (point)) |
be9b65ac DL |
645 | ;; Feed it the startfile. |
646 | (cond (startfile | |
647 | ;;This is guaranteed to wait long enough | |
648 | ;;but has bad results if the comint does not prompt at all | |
649 | ;; (while (= size (buffer-size)) | |
650 | ;; (sleep-for 1)) | |
651 | ;;I hope 1 second is enough! | |
652 | (sleep-for 1) | |
653 | (goto-char (point-max)) | |
654 | (insert-file-contents startfile) | |
655 | (setq startfile (buffer-substring (point) (point-max))) | |
656 | (delete-region (point) (point-max)) | |
49116ac0 JB |
657 | (comint-send-string proc startfile))) |
658 | (run-hooks 'comint-exec-hook) | |
87ef29fd | 659 | buffer))) |
be9b65ac | 660 | |
b578f267 EN |
661 | ;; This auxiliary function cranks up the process for comint-exec in |
662 | ;; the appropriate environment. | |
be9b65ac DL |
663 | |
664 | (defun comint-exec-1 (name buffer command switches) | |
1b421826 | 665 | (let ((process-environment |
1f9e761d | 666 | (nconc |
a4ab7e69 RS |
667 | ;; If using termcap, we specify `emacs' as the terminal type |
668 | ;; because that lets us specify a width. | |
36990caa | 669 | ;; If using terminfo, we specify `dumb' because that is |
a4ab7e69 RS |
670 | ;; a defined terminal type. `emacs' is not a defined terminal type |
671 | ;; and there is no way for us to define it here. | |
672 | ;; Some programs that use terminfo get very confused | |
673 | ;; if TERM is not a valid terminal type. | |
1f9e761d | 674 | (if (and (boundp 'system-uses-terminfo) system-uses-terminfo) |
4a11114d | 675 | (list "TERM=dumb" "TERMCAP=" |
1f9e761d | 676 | (format "COLUMNS=%d" (frame-width))) |
f3b0a431 | 677 | (list "TERM=emacs" |
9dbfa8cf | 678 | (format "TERMCAP=emacs:co#%d:tc=unknown:" (frame-width)))) |
f3b0a431 | 679 | (if (getenv "EMACS") nil (list "EMACS=t")) |
1347899b RS |
680 | process-environment)) |
681 | (default-directory | |
e1d409fe | 682 | (if (file-accessible-directory-p default-directory) |
1347899b | 683 | default-directory |
17193f89 | 684 | (char-to-string directory-sep-char)))) |
e065a56e | 685 | (apply 'start-process name buffer command switches))) |
be9b65ac | 686 | \f |
b578f267 EN |
687 | ;; Input history processing in a buffer |
688 | ;; =========================================================================== | |
689 | ;; Useful input history functions, courtesy of the Ergo group. | |
690 | ||
691 | ;; Eleven commands: | |
692 | ;; comint-dynamic-list-input-ring List history in help buffer. | |
693 | ;; comint-previous-input Previous input... | |
694 | ;; comint-previous-matching-input ...matching a string. | |
695 | ;; comint-previous-matching-input-from-input ... matching the current input. | |
696 | ;; comint-next-input Next input... | |
697 | ;; comint-next-matching-input ...matching a string. | |
698 | ;; comint-next-matching-input-from-input ... matching the current input. | |
699 | ;; comint-backward-matching-input Backwards input... | |
700 | ;; comint-forward-matching-input ...matching a string. | |
701 | ;; comint-replace-by-expanded-history Expand history at point; | |
702 | ;; replace with expanded history. | |
703 | ;; comint-magic-space Expand history and insert space. | |
704 | ;; | |
705 | ;; Three functions: | |
706 | ;; comint-read-input-ring Read into comint-input-ring... | |
707 | ;; comint-write-input-ring Write to comint-input-ring-file-name. | |
708 | ;; comint-replace-by-expanded-history-before-point Workhorse function. | |
3ee91e68 | 709 | |
d637fa56 | 710 | (defun comint-read-input-ring (&optional silent) |
3ee91e68 RS |
711 | "Sets the buffer's `comint-input-ring' from a history file. |
712 | The name of the file is given by the variable `comint-input-ring-file-name'. | |
713 | The history ring is of size `comint-input-ring-size', regardless of file size. | |
714 | If `comint-input-ring-file-name' is nil this function does nothing. | |
715 | ||
d637fa56 RS |
716 | If the optional argument SILENT is non-nil, we say nothing about a |
717 | failure to read the history file. | |
3ee91e68 | 718 | |
d637fa56 RS |
719 | This function is useful for major mode commands and mode hooks. |
720 | ||
721 | The structure of the history file should be one input command per line, | |
722 | with the most recent command last. | |
3ee91e68 | 723 | See also `comint-input-ignoredups' and `comint-write-input-ring'." |
07ccf0bc RS |
724 | (cond ((or (null comint-input-ring-file-name) |
725 | (equal comint-input-ring-file-name "")) | |
3ee91e68 RS |
726 | nil) |
727 | ((not (file-readable-p comint-input-ring-file-name)) | |
d637fa56 RS |
728 | (or silent |
729 | (message "Cannot read history file %s" | |
730 | comint-input-ring-file-name))) | |
3ee91e68 | 731 | (t |
dc0825db RS |
732 | (let ((history-buf (get-buffer-create " *temp*")) |
733 | (file comint-input-ring-file-name) | |
734 | (count 0) | |
3ee91e68 | 735 | (ring (make-ring comint-input-ring-size))) |
dc0825db RS |
736 | (unwind-protect |
737 | (save-excursion | |
738 | (set-buffer history-buf) | |
3ee91e68 | 739 | (widen) |
dc0825db RS |
740 | (erase-buffer) |
741 | (insert-file-contents file) | |
742 | ;; Save restriction in case file is already visited... | |
743 | ;; Watch for those date stamps in history files! | |
744 | (goto-char (point-max)) | |
745 | (while (and (< count comint-input-ring-size) | |
746 | (re-search-backward "^[ \t]*\\([^#\n].*\\)[ \t]*$" | |
747 | nil t)) | |
3ee91e68 RS |
748 | (let ((history (buffer-substring (match-beginning 1) |
749 | (match-end 1)))) | |
750 | (if (or (null comint-input-ignoredups) | |
751 | (ring-empty-p ring) | |
752 | (not (string-equal (ring-ref ring 0) history))) | |
dc0825db RS |
753 | (ring-insert-at-beginning ring history))) |
754 | (setq count (1+ count)))) | |
755 | (kill-buffer history-buf)) | |
3ee91e68 RS |
756 | (setq comint-input-ring ring |
757 | comint-input-ring-index nil))))) | |
758 | ||
759 | (defun comint-write-input-ring () | |
760 | "Writes the buffer's `comint-input-ring' to a history file. | |
761 | The name of the file is given by the variable `comint-input-ring-file-name'. | |
762 | The original contents of the file are lost if `comint-input-ring' is not empty. | |
763 | If `comint-input-ring-file-name' is nil this function does nothing. | |
764 | ||
765 | Useful within process sentinels. | |
766 | ||
767 | See also `comint-read-input-ring'." | |
768 | (cond ((or (null comint-input-ring-file-name) | |
07ccf0bc | 769 | (equal comint-input-ring-file-name "") |
3ee91e68 RS |
770 | (null comint-input-ring) (ring-empty-p comint-input-ring)) |
771 | nil) | |
772 | ((not (file-writable-p comint-input-ring-file-name)) | |
773 | (message "Cannot write history file %s" comint-input-ring-file-name)) | |
774 | (t | |
775 | (let* ((history-buf (get-buffer-create " *Temp Input History*")) | |
776 | (ring comint-input-ring) | |
777 | (file comint-input-ring-file-name) | |
778 | (index (ring-length ring))) | |
779 | ;; Write it all out into a buffer first. Much faster, but messier, | |
780 | ;; than writing it one line at a time. | |
781 | (save-excursion | |
782 | (set-buffer history-buf) | |
783 | (erase-buffer) | |
784 | (while (> index 0) | |
785 | (setq index (1- index)) | |
786 | (insert (ring-ref ring index) ?\n)) | |
787 | (write-region (buffer-string) nil file nil 'no-message) | |
788 | (kill-buffer nil)))))) | |
789 | ||
790 | ||
791 | (defun comint-dynamic-list-input-ring () | |
792 | "List in help buffer the buffer's input history." | |
793 | (interactive) | |
794 | (if (or (not (ring-p comint-input-ring)) | |
795 | (ring-empty-p comint-input-ring)) | |
796 | (message "No history") | |
797 | (let ((history nil) | |
798 | (history-buffer " *Input History*") | |
799 | (index (1- (ring-length comint-input-ring))) | |
800 | (conf (current-window-configuration))) | |
801 | ;; We have to build up a list ourselves from the ring vector. | |
802 | (while (>= index 0) | |
803 | (setq history (cons (ring-ref comint-input-ring index) history) | |
804 | index (1- index))) | |
805 | ;; Change "completion" to "history reference" | |
806 | ;; to make the display accurate. | |
807 | (with-output-to-temp-buffer history-buffer | |
808 | (display-completion-list history) | |
809 | (set-buffer history-buffer) | |
810 | (forward-line 3) | |
811 | (while (search-backward "completion" nil 'move) | |
812 | (replace-match "history reference"))) | |
813 | (sit-for 0) | |
814 | (message "Hit space to flush") | |
815 | (let ((ch (read-event))) | |
816 | (if (eq ch ?\ ) | |
817 | (set-window-configuration conf) | |
818 | (setq unread-command-events (list ch))))))) | |
819 | ||
820 | ||
821 | (defun comint-regexp-arg (prompt) | |
822 | ;; Return list of regexp and prefix arg using PROMPT. | |
9f8ac7f9 | 823 | (let* (;; Don't clobber this. |
3ee91e68 RS |
824 | (last-command last-command) |
825 | (regexp (read-from-minibuffer prompt nil nil nil | |
826 | 'minibuffer-history-search-history))) | |
827 | (list (if (string-equal regexp "") | |
828 | (setcar minibuffer-history-search-history | |
829 | (nth 1 minibuffer-history-search-history)) | |
830 | regexp) | |
831 | (prefix-numeric-value current-prefix-arg)))) | |
832 | ||
833 | (defun comint-search-arg (arg) | |
834 | ;; First make sure there is a ring and that we are after the process mark | |
835 | (cond ((not (comint-after-pmark-p)) | |
836 | (error "Not at command line")) | |
837 | ((or (null comint-input-ring) | |
838 | (ring-empty-p comint-input-ring)) | |
839 | (error "Empty input ring")) | |
840 | ((zerop arg) | |
841 | ;; arg of zero resets search from beginning, and uses arg of 1 | |
842 | (setq comint-input-ring-index nil) | |
843 | 1) | |
844 | (t | |
845 | arg))) | |
846 | ||
847 | (defun comint-search-start (arg) | |
848 | ;; Index to start a directional search, starting at comint-input-ring-index | |
849 | (if comint-input-ring-index | |
850 | ;; If a search is running, offset by 1 in direction of arg | |
851 | (mod (+ comint-input-ring-index (if (> arg 0) 1 -1)) | |
852 | (ring-length comint-input-ring)) | |
853 | ;; For a new search, start from beginning or end, as appropriate | |
854 | (if (>= arg 0) | |
855 | 0 ; First elt for forward search | |
856 | (1- (ring-length comint-input-ring))))) ; Last elt for backward search | |
857 | ||
858 | (defun comint-previous-input-string (arg) | |
859 | "Return the string ARG places along the input ring. | |
860 | Moves relative to `comint-input-ring-index'." | |
861 | (ring-ref comint-input-ring (if comint-input-ring-index | |
862 | (mod (+ arg comint-input-ring-index) | |
863 | (ring-length comint-input-ring)) | |
864 | arg))) | |
be9b65ac DL |
865 | |
866 | (defun comint-previous-input (arg) | |
867 | "Cycle backwards through input history." | |
868 | (interactive "*p") | |
3ee91e68 | 869 | (comint-previous-matching-input "." arg)) |
1586b965 | 870 | |
be9b65ac DL |
871 | (defun comint-next-input (arg) |
872 | "Cycle forwards through input history." | |
873 | (interactive "*p") | |
874 | (comint-previous-input (- arg))) | |
875 | ||
3ee91e68 RS |
876 | (defun comint-previous-matching-input-string (regexp arg) |
877 | "Return the string matching REGEXP ARG places along the input ring. | |
878 | Moves relative to `comint-input-ring-index'." | |
879 | (let* ((pos (comint-previous-matching-input-string-position regexp arg))) | |
880 | (if pos (ring-ref comint-input-ring pos)))) | |
881 | ||
882 | (defun comint-previous-matching-input-string-position (regexp arg &optional start) | |
883 | "Return the index matching REGEXP ARG places along the input ring. | |
884 | Moves relative to START, or `comint-input-ring-index'." | |
885 | (if (or (not (ring-p comint-input-ring)) | |
886 | (ring-empty-p comint-input-ring)) | |
887 | (error "No history")) | |
888 | (let* ((len (ring-length comint-input-ring)) | |
889 | (motion (if (> arg 0) 1 -1)) | |
890 | (n (mod (- (or start (comint-search-start arg)) motion) len)) | |
891 | (tried-each-ring-item nil) | |
892 | (prev nil)) | |
893 | ;; Do the whole search as many times as the argument says. | |
894 | (while (and (/= arg 0) (not tried-each-ring-item)) | |
895 | ;; Step once. | |
896 | (setq prev n | |
897 | n (mod (+ n motion) len)) | |
898 | ;; If we haven't reached a match, step some more. | |
899 | (while (and (< n len) (not tried-each-ring-item) | |
900 | (not (string-match regexp (ring-ref comint-input-ring n)))) | |
901 | (setq n (mod (+ n motion) len) | |
902 | ;; If we have gone all the way around in this search. | |
903 | tried-each-ring-item (= n prev))) | |
904 | (setq arg (if (> arg 0) (1- arg) (1+ arg)))) | |
905 | ;; Now that we know which ring element to use, if we found it, return that. | |
906 | (if (string-match regexp (ring-ref comint-input-ring n)) | |
907 | n))) | |
908 | ||
0c5c1f34 RS |
909 | (defun comint-previous-matching-input (regexp arg) |
910 | "Search backwards through input history for match for REGEXP. | |
911 | \(Previous history elements are earlier commands.) | |
912 | With prefix argument N, search for Nth previous match. | |
913 | If N is negative, find the next or Nth next match." | |
3ee91e68 RS |
914 | (interactive (comint-regexp-arg "Previous input matching (regexp): ")) |
915 | (setq arg (comint-search-arg arg)) | |
916 | (let ((pos (comint-previous-matching-input-string-position regexp arg))) | |
917 | ;; Has a match been found? | |
918 | (if (null pos) | |
919 | (error "Not found") | |
920 | (setq comint-input-ring-index pos) | |
921 | (message "History item: %d" (1+ pos)) | |
922 | (delete-region | |
923 | ;; Can't use kill-region as it sets this-command | |
361ecbf2 RS |
924 | (or (marker-position comint-accum-marker) |
925 | (process-mark (get-buffer-process (current-buffer)))) | |
926 | (point)) | |
3ee91e68 | 927 | (insert (ring-ref comint-input-ring pos))))) |
0c5c1f34 RS |
928 | |
929 | (defun comint-next-matching-input (regexp arg) | |
930 | "Search forwards through input history for match for REGEXP. | |
931 | \(Later history elements are more recent commands.) | |
932 | With prefix argument N, search for Nth following match. | |
933 | If N is negative, find the previous or Nth previous match." | |
3ee91e68 | 934 | (interactive (comint-regexp-arg "Next input matching (regexp): ")) |
0c5c1f34 | 935 | (comint-previous-matching-input regexp (- arg))) |
be9b65ac | 936 | |
3ee91e68 RS |
937 | (defun comint-previous-matching-input-from-input (arg) |
938 | "Search backwards through input history for match for current input. | |
939 | \(Previous history elements are earlier commands.) | |
940 | With prefix argument N, search for Nth previous match. | |
3f849529 | 941 | If N is negative, search forwards for the -Nth following match." |
3ee91e68 RS |
942 | (interactive "p") |
943 | (if (not (memq last-command '(comint-previous-matching-input-from-input | |
944 | comint-next-matching-input-from-input))) | |
945 | ;; Starting a new search | |
946 | (setq comint-matching-input-from-input-string | |
947 | (buffer-substring | |
361ecbf2 RS |
948 | (or (marker-position comint-accum-marker) |
949 | (process-mark (get-buffer-process (current-buffer)))) | |
3ee91e68 RS |
950 | (point)) |
951 | comint-input-ring-index nil)) | |
952 | (comint-previous-matching-input | |
953 | (concat "^" (regexp-quote comint-matching-input-from-input-string)) | |
954 | arg)) | |
955 | ||
956 | (defun comint-next-matching-input-from-input (arg) | |
957 | "Search forwards through input history for match for current input. | |
3f849529 RS |
958 | \(Following history elements are more recent commands.) |
959 | With prefix argument N, search for Nth following match. | |
960 | If N is negative, search backwards for the -Nth previous match." | |
3ee91e68 RS |
961 | (interactive "p") |
962 | (comint-previous-matching-input-from-input (- arg))) | |
963 | ||
964 | ||
17ac4966 | 965 | (defun comint-replace-by-expanded-history (&optional silent start) |
3ee91e68 | 966 | "Expand input command history references before point. |
1b421826 RS |
967 | Expansion is dependent on the value of `comint-input-autoexpand'. |
968 | ||
3ee91e68 RS |
969 | This function depends on the buffer's idea of the input history, which may not |
970 | match the command interpreter's idea, assuming it has one. | |
971 | ||
972 | Assumes history syntax is like typical Un*x shells'. However, since emacs | |
973 | cannot know the interpreter's idea of input line numbers, assuming it has one, | |
974 | it cannot expand absolute input line number references. | |
975 | ||
d637fa56 RS |
976 | If the optional argument SILENT is non-nil, never complain |
977 | even if history reference seems erroneous. | |
978 | ||
17ac4966 RS |
979 | If the optional argument START is non-nil, that specifies the |
980 | start of the text to scan for history references, rather | |
981 | than the logical beginning of line. | |
982 | ||
1b421826 RS |
983 | See `comint-magic-space' and `comint-replace-by-expanded-history-before-point'. |
984 | ||
985 | Returns t if successful." | |
3ee91e68 | 986 | (interactive) |
1b421826 | 987 | (if (and comint-input-autoexpand |
107d7589 | 988 | (string-match "!\\|^\\^" (funcall comint-get-old-input)) |
8d3d2a0c KH |
989 | (save-excursion (beginning-of-line) |
990 | (looking-at comint-prompt-regexp))) | |
1b421826 RS |
991 | ;; Looks like there might be history references in the command. |
992 | (let ((previous-modified-tick (buffer-modified-tick))) | |
17ac4966 | 993 | (comint-replace-by-expanded-history-before-point silent start) |
1b421826 RS |
994 | (/= previous-modified-tick (buffer-modified-tick))))) |
995 | ||
996 | ||
17ac4966 | 997 | (defun comint-replace-by-expanded-history-before-point (silent &optional start) |
1b421826 | 998 | "Expand directory stack reference before point. |
17ac4966 RS |
999 | See `comint-replace-by-expanded-history'. Returns t if successful. |
1000 | ||
1001 | If the optional argument START is non-nil, that specifies the | |
1002 | start of the text to scan for history references, rather | |
1003 | than the logical beginning of line." | |
3ee91e68 RS |
1004 | (save-excursion |
1005 | (let ((toend (- (save-excursion (end-of-line nil) (point)) (point))) | |
17ac4966 | 1006 | (start (or start (progn (comint-bol nil) (point))))) |
d637fa56 RS |
1007 | (while (progn |
1008 | (skip-chars-forward "^!^" | |
1009 | (save-excursion | |
1010 | (end-of-line nil) (- (point) toend))) | |
1011 | (< (point) | |
1012 | (save-excursion | |
1013 | (end-of-line nil) (- (point) toend)))) | |
3ee91e68 RS |
1014 | ;; This seems a bit complex. We look for references such as !!, !-num, |
1015 | ;; !foo, !?foo, !{bar}, !?{bar}, ^oh, ^my^, ^god^it, ^never^ends^. | |
1016 | ;; If that wasn't enough, the plings can be suffixed with argument | |
1017 | ;; range specifiers. | |
1018 | ;; Argument ranges are complex too, so we hive off the input line, | |
1019 | ;; referenced with plings, with the range string to `comint-args'. | |
1020 | (setq comint-input-ring-index nil) | |
3ee91e68 RS |
1021 | (cond ((or (= (preceding-char) ?\\) |
1022 | (comint-within-quotes start (point))) | |
1023 | ;; The history is quoted, or we're in quotes. | |
d637fa56 | 1024 | (goto-char (1+ (point)))) |
3ee91e68 RS |
1025 | ((looking-at "![0-9]+\\($\\|[^-]\\)") |
1026 | ;; We cannot know the interpreter's idea of input line numbers. | |
1027 | (goto-char (match-end 0)) | |
1028 | (message "Absolute reference cannot be expanded")) | |
1029 | ((looking-at "!-\\([0-9]+\\)\\(:?[0-9^$*-]+\\)?") | |
1030 | ;; Just a number of args from `number' lines backward. | |
1031 | (let ((number (1- (string-to-number | |
1032 | (buffer-substring (match-beginning 1) | |
1033 | (match-end 1)))))) | |
1034 | (if (<= number (ring-length comint-input-ring)) | |
1035 | (progn | |
1036 | (replace-match | |
1037 | (comint-args (comint-previous-input-string number) | |
fb9ab28a RS |
1038 | (match-beginning 2) (match-end 2)) |
1039 | t t) | |
3ee91e68 RS |
1040 | (setq comint-input-ring-index number) |
1041 | (message "History item: %d" (1+ number))) | |
1042 | (goto-char (match-end 0)) | |
1043 | (message "Relative reference exceeds input history size")))) | |
1044 | ((or (looking-at "!!?:?\\([0-9^$*-]+\\)") (looking-at "!!")) | |
1045 | ;; Just a number of args from the previous input line. | |
1046 | (replace-match | |
1047 | (comint-args (comint-previous-input-string 0) | |
fb9ab28a RS |
1048 | (match-beginning 1) (match-end 1)) |
1049 | t t) | |
1b421826 | 1050 | (message "History item: previous")) |
3ee91e68 RS |
1051 | ((looking-at |
1052 | "!\\??\\({\\(.+\\)}\\|\\(\\sw+\\)\\)\\(:?[0-9^$*-]+\\)?") | |
1053 | ;; Most recent input starting with or containing (possibly | |
1054 | ;; protected) string, maybe just a number of args. Phew. | |
1055 | (let* ((mb1 (match-beginning 1)) (me1 (match-end 1)) | |
1056 | (mb2 (match-beginning 2)) (me2 (match-end 2)) | |
1057 | (exp (buffer-substring (or mb2 mb1) (or me2 me1))) | |
1058 | (pref (if (save-match-data (looking-at "!\\?")) "" "^")) | |
1059 | (pos (save-match-data | |
1060 | (comint-previous-matching-input-string-position | |
1061 | (concat pref (regexp-quote exp)) 1)))) | |
1062 | (if (null pos) | |
5a336c13 KH |
1063 | (progn |
1064 | (goto-char (match-end 0)) | |
1065 | (or silent | |
1066 | (progn (message "Not found") | |
1067 | (ding)))) | |
3ee91e68 | 1068 | (setq comint-input-ring-index pos) |
3ee91e68 RS |
1069 | (replace-match |
1070 | (comint-args (ring-ref comint-input-ring pos) | |
fb9ab28a RS |
1071 | (match-beginning 4) (match-end 4)) |
1072 | t t) | |
1b421826 | 1073 | (message "History item: %d" (1+ pos))))) |
3ee91e68 RS |
1074 | ((looking-at "\\^\\([^^]+\\)\\^?\\([^^]*\\)\\^?") |
1075 | ;; Quick substitution on the previous input line. | |
1076 | (let ((old (buffer-substring (match-beginning 1) (match-end 1))) | |
1077 | (new (buffer-substring (match-beginning 2) (match-end 2))) | |
1078 | (pos nil)) | |
1079 | (replace-match (comint-previous-input-string 0) t t) | |
1080 | (setq pos (point)) | |
1081 | (goto-char (match-beginning 0)) | |
1b421826 RS |
1082 | (if (not (search-forward old pos t)) |
1083 | (or silent | |
1084 | (error "Not found")) | |
1085 | (replace-match new t t) | |
1086 | (message "History item: substituted")))) | |
3ee91e68 | 1087 | (t |
2ac86cc0 | 1088 | (forward-char 1))))))) |
3ee91e68 RS |
1089 | |
1090 | ||
1091 | (defun comint-magic-space (arg) | |
1092 | "Expand input history references before point and insert ARG spaces. | |
1093 | A useful command to bind to SPC. See `comint-replace-by-expanded-history'." | |
1094 | (interactive "p") | |
1095 | (comint-replace-by-expanded-history) | |
1096 | (self-insert-command arg)) | |
1097 | \f | |
1098 | (defun comint-within-quotes (beg end) | |
1099 | "Return t if the number of quotes between BEG and END is odd. | |
1100 | Quotes are single and double." | |
1101 | (let ((countsq (comint-how-many-region "\\(^\\|[^\\\\]\\)\'" beg end)) | |
1102 | (countdq (comint-how-many-region "\\(^\\|[^\\\\]\\)\"" beg end))) | |
1103 | (or (= (mod countsq 2) 1) (= (mod countdq 2) 1)))) | |
1104 | ||
1105 | (defun comint-how-many-region (regexp beg end) | |
1106 | "Return number of matches for REGEXP from BEG to END." | |
1107 | (let ((count 0)) | |
1108 | (save-excursion | |
1109 | (save-match-data | |
1110 | (goto-char beg) | |
1111 | (while (re-search-forward regexp end t) | |
1112 | (setq count (1+ count))))) | |
1113 | count)) | |
1114 | ||
1115 | (defun comint-args (string begin end) | |
1116 | ;; From STRING, return the args depending on the range specified in the text | |
1117 | ;; from BEGIN to END. If BEGIN is nil, assume all args. Ignore leading `:'. | |
1118 | ;; Range can be x-y, x-, -y, where x/y can be [0-9], *, ^, $. | |
1119 | (save-match-data | |
1120 | (if (null begin) | |
1121 | (comint-arguments string 0 nil) | |
1122 | (let* ((range (buffer-substring | |
1123 | (if (eq (char-after begin) ?:) (1+ begin) begin) end)) | |
1124 | (nth (cond ((string-match "^[*^]" range) 1) | |
1125 | ((string-match "^-" range) 0) | |
1126 | ((string-equal range "$") nil) | |
1127 | (t (string-to-number range)))) | |
1128 | (mth (cond ((string-match "[-*$]$" range) nil) | |
1129 | ((string-match "-" range) | |
1130 | (string-to-number (substring range (match-end 0)))) | |
1131 | (t nth)))) | |
1132 | (comint-arguments string nth mth))))) | |
1133 | ||
3f4b7c36 RS |
1134 | ;; Return a list of arguments from ARG. Break it up at the |
1135 | ;; delimiters in comint-delimiter-argument-list. Returned list is backwards. | |
3ee91e68 | 1136 | (defun comint-delim-arg (arg) |
3f4b7c36 | 1137 | (if (null comint-delimiter-argument-list) |
3ee91e68 | 1138 | (list arg) |
3f4b7c36 RS |
1139 | (let ((args nil) |
1140 | (pos 0) | |
1141 | (len (length arg))) | |
1142 | (while (< pos len) | |
1143 | (let ((char (aref arg pos)) | |
1144 | (start pos)) | |
1145 | (if (memq char comint-delimiter-argument-list) | |
1146 | (while (and (< pos len) (eq (aref arg pos) char)) | |
1147 | (setq pos (1+ pos))) | |
1148 | (while (and (< pos len) | |
1149 | (not (memq (aref arg pos) | |
1150 | comint-delimiter-argument-list))) | |
1151 | (setq pos (1+ pos)))) | |
1152 | (setq args (cons (substring arg start pos) args)))) | |
3ee91e68 RS |
1153 | args))) |
1154 | ||
1155 | (defun comint-arguments (string nth mth) | |
1156 | "Return from STRING the NTH to MTH arguments. | |
1157 | NTH and/or MTH can be nil, which means the last argument. | |
3f4b7c36 RS |
1158 | Returned arguments are separated by single spaces. |
1159 | We assume whitespace separates arguments, except within quotes. | |
1160 | Also, a run of one or more of a single character | |
1161 | in `comint-delimiter-argument-list' is a separate argument. | |
3ee91e68 | 1162 | Argument 0 is the command name." |
ec569127 RS |
1163 | ;; The first line handles ordinary characters and backslash-sequences. |
1164 | ;; The second matches "-quoted strings. | |
1165 | ;; The third matches '-quoted strings. | |
1166 | ;; The fourth matches `-quoted strings. | |
1167 | ;; This seems to fit the syntax of BASH 2.0. | |
1168 | (let ((argpart "[^ \n\t\"'`\\]+\\|\\\\[\"'`\\]+\\|\ | |
1169 | \\(\"\\([^\"\\]\\|\\\\.\\)*\"\\|\ | |
1170 | '[^']*'\\|\ | |
1171 | `[^`]*`\\)") | |
d91f490d RS |
1172 | (args ()) (pos 0) |
1173 | (count 0) | |
1174 | beg str value quotes) | |
1175 | ;; Build a list of all the args until we have as many as we want. | |
1176 | (while (and (or (null mth) (<= count mth)) | |
1177 | (string-match argpart string pos)) | |
1178 | (if (and beg (= pos (match-beginning 0))) | |
1179 | ;; It's contiguous, part of the same arg. | |
1180 | (setq pos (match-end 0) | |
1181 | quotes (or quotes (match-beginning 1))) | |
1182 | ;; It's a new separate arg. | |
1183 | (if beg | |
1184 | ;; Put the previous arg, if there was one, onto ARGS. | |
1185 | (setq str (substring string beg pos) | |
1186 | args (if quotes (cons str args) | |
1187 | (nconc (comint-delim-arg str) args)) | |
1188 | count (1+ count))) | |
1189 | (setq quotes (match-beginning 1)) | |
1190 | (setq beg (match-beginning 0)) | |
1191 | (setq pos (match-end 0)))) | |
1192 | (if beg | |
1193 | (setq str (substring string beg pos) | |
1194 | args (if quotes (cons str args) | |
1195 | (nconc (comint-delim-arg str) args)) | |
1196 | count (1+ count))) | |
1197 | (let ((n (or nth (1- count))) | |
1198 | (m (if mth (1- (- count mth)) 0))) | |
3ee91e68 RS |
1199 | (mapconcat |
1200 | (function (lambda (a) a)) (nthcdr n (nreverse (nthcdr m args))) " ")))) | |
1201 | \f | |
b578f267 EN |
1202 | ;; |
1203 | ;; Input processing stuff | |
1204 | ;; | |
3ee91e68 | 1205 | |
49116ac0 | 1206 | (defun comint-send-input () |
c2ced5d8 CZ |
1207 | "Send input to process. |
1208 | After the process output mark, sends all text from the process mark to | |
1209 | point as input to the process. Before the process output mark, calls value | |
8ae3bc9f | 1210 | of variable `comint-get-old-input' to retrieve old input, copies it to the |
cccd719e | 1211 | process mark, and sends it. If variable `comint-process-echoes' is nil, |
8ae3bc9f | 1212 | a terminal newline is also inserted into the buffer and sent to the process |
cccd719e RS |
1213 | \(if it is non-nil, all text from the process mark to point is deleted, |
1214 | since it is assumed the remote process will re-echo it). | |
8ae3bc9f | 1215 | |
3ee91e68 | 1216 | Any history reference may be expanded depending on the value of the variable |
1b421826 | 1217 | `comint-input-autoexpand'. The list of function names contained in the value |
fb9ab28a | 1218 | of `comint-input-filter-functions' is called on the input before sending it. |
1b421826 RS |
1219 | The input is entered into the input history ring, if the value of variable |
1220 | `comint-input-filter' returns non-nil when called on the input. | |
cccd719e RS |
1221 | |
1222 | If variable `comint-eol-on-send' is non-nil, then point is moved to the | |
8ae3bc9f NF |
1223 | end of line before sending the input. |
1224 | ||
fb9ab28a | 1225 | The values of `comint-get-old-input', `comint-input-filter-functions', and |
1b421826 RS |
1226 | `comint-input-filter' are chosen according to the command interpreter running |
1227 | in the buffer. E.g., | |
1228 | ||
be9b65ac | 1229 | If the interpreter is the csh, |
49116ac0 JB |
1230 | comint-get-old-input is the default: take the current line, discard any |
1231 | initial string matching regexp comint-prompt-regexp. | |
fb9ab28a | 1232 | comint-input-filter-functions monitors input for \"cd\", \"pushd\", and |
1b421826 | 1233 | \"popd\" commands. When it sees one, it cd's the buffer. |
3ee91e68 | 1234 | comint-input-filter is the default: returns t if the input isn't all white |
49116ac0 | 1235 | space. |
be9b65ac DL |
1236 | |
1237 | If the comint is Lucid Common Lisp, | |
49116ac0 | 1238 | comint-get-old-input snarfs the sexp ending at point. |
fb9ab28a | 1239 | comint-input-filter-functions does nothing. |
3ee91e68 | 1240 | comint-input-filter returns nil if the input matches input-filter-regexp, |
be9b65ac DL |
1241 | which matches (1) all whitespace (2) :a, :c, etc. |
1242 | ||
1586b965 | 1243 | Similarly for Soar, Scheme, etc." |
be9b65ac DL |
1244 | (interactive) |
1245 | ;; Note that the input string does not include its terminal newline. | |
49116ac0 JB |
1246 | (let ((proc (get-buffer-process (current-buffer)))) |
1247 | (if (not proc) (error "Current buffer has no process") | |
1248 | (let* ((pmark (process-mark proc)) | |
3ee91e68 | 1249 | (intxt (if (>= (point) (marker-position pmark)) |
49116ac0 JB |
1250 | (progn (if comint-eol-on-send (end-of-line)) |
1251 | (buffer-substring pmark (point))) | |
87ef29fd JB |
1252 | (let ((copy (funcall comint-get-old-input))) |
1253 | (goto-char pmark) | |
1254 | (insert copy) | |
3ee91e68 RS |
1255 | copy))) |
1256 | (input (if (not (eq comint-input-autoexpand 'input)) | |
1257 | ;; Just whatever's already there | |
1258 | intxt | |
1259 | ;; Expand and leave it visible in buffer | |
17ac4966 | 1260 | (comint-replace-by-expanded-history t pmark) |
3ee91e68 RS |
1261 | (buffer-substring pmark (point)))) |
1262 | (history (if (not (eq comint-input-autoexpand 'history)) | |
5f46b51a | 1263 | input |
3ee91e68 RS |
1264 | ;; This is messy 'cos ultimately the original |
1265 | ;; functions used do insertion, rather than return | |
1266 | ;; strings. We have to expand, then insert back. | |
17ac4966 | 1267 | (comint-replace-by-expanded-history t pmark) |
ff6318be SM |
1268 | (let ((copy (buffer-substring pmark (point))) |
1269 | (start (point))) | |
1270 | (insert input) | |
1271 | (delete-region pmark start) | |
5f46b51a | 1272 | copy)))) |
8ae3bc9f NF |
1273 | (if comint-process-echoes |
1274 | (delete-region pmark (point)) | |
d2a49350 | 1275 | (insert-before-markers ?\n)) |
3ee91e68 RS |
1276 | (if (and (funcall comint-input-filter history) |
1277 | (or (null comint-input-ignoredups) | |
1278 | (not (ring-p comint-input-ring)) | |
1279 | (ring-empty-p comint-input-ring) | |
1280 | (not (string-equal (ring-ref comint-input-ring 0) | |
1281 | history)))) | |
1282 | (ring-insert comint-input-ring history)) | |
155fd027 KH |
1283 | (run-hook-with-args 'comint-input-filter-functions |
1284 | (concat input "\n")) | |
361ecbf2 | 1285 | (setq comint-save-input-ring-index comint-input-ring-index) |
1586b965 | 1286 | (setq comint-input-ring-index nil) |
dcdc526c RS |
1287 | ;; Update the markers before we send the input |
1288 | ;; in case we get output amidst sending the input. | |
87ef29fd JB |
1289 | (set-marker comint-last-input-start pmark) |
1290 | (set-marker comint-last-input-end (point)) | |
3ee91e68 | 1291 | (set-marker (process-mark proc) (point)) |
361ecbf2 RS |
1292 | ;; clear the "accumulation" marker |
1293 | (set-marker comint-accum-marker nil) | |
dcdc526c | 1294 | (funcall comint-input-sender proc input) |
6aac2ab0 RS |
1295 | ;; This used to call comint-output-filter-functions, |
1296 | ;; but that scrolled the buffer in undesirable ways. | |
1297 | (run-hook-with-args 'comint-output-filter-functions ""))))) | |
be9b65ac | 1298 | |
d4321160 | 1299 | (defvar comint-preoutput-filter-functions nil |
e7654b4a | 1300 | "Functions to call before output is inserted into the buffer. |
d4321160 RS |
1301 | These functions get one argument, a string containing the text to be |
1302 | inserted. They return the string as it should be inserted. | |
1303 | ||
1304 | This variable is buffer-local.") | |
1305 | ||
3ee91e68 | 1306 | ;; The purpose of using this filter for comint processes |
5511e411 RS |
1307 | ;; is to keep comint-last-input-end from moving forward |
1308 | ;; when output is inserted. | |
3ee91e68 RS |
1309 | (defun comint-output-filter (process string) |
1310 | ;; First check for killed buffer | |
91d75282 | 1311 | (let ((oprocbuf (process-buffer process))) |
d4321160 RS |
1312 | (let ((functions comint-preoutput-filter-functions)) |
1313 | (while (and functions string) | |
1314 | (setq string (funcall (car functions) string)) | |
1315 | (setq functions (cdr functions)))) | |
1316 | (if (and string oprocbuf (buffer-name oprocbuf)) | |
91d75282 RS |
1317 | (let ((obuf (current-buffer)) |
1318 | (opoint nil) (obeg nil) (oend nil)) | |
1319 | (set-buffer oprocbuf) | |
1320 | (setq opoint (point)) | |
1321 | (setq obeg (point-min)) | |
1322 | (setq oend (point-max)) | |
1323 | (let ((buffer-read-only nil) | |
1324 | (nchars (length string)) | |
1325 | (ostart nil)) | |
1326 | (widen) | |
1327 | (goto-char (process-mark process)) | |
1328 | (setq ostart (point)) | |
1329 | (if (<= (point) opoint) | |
1330 | (setq opoint (+ opoint nchars))) | |
1331 | ;; Insert after old_begv, but before old_zv. | |
1332 | (if (< (point) obeg) | |
1333 | (setq obeg (+ obeg nchars))) | |
1334 | (if (<= (point) oend) | |
1335 | (setq oend (+ oend nchars))) | |
1336 | (insert-before-markers string) | |
1337 | ;; Don't insert initial prompt outside the top of the window. | |
1338 | (if (= (window-start (selected-window)) (point)) | |
1339 | (set-window-start (selected-window) (- (point) (length string)))) | |
1340 | (if (and comint-last-input-end | |
1341 | (marker-buffer comint-last-input-end) | |
1342 | (= (point) comint-last-input-end)) | |
1343 | (set-marker comint-last-input-end (- comint-last-input-end nchars))) | |
1344 | (set-marker comint-last-output-start ostart) | |
1345 | (set-marker (process-mark process) (point)) | |
1346 | (force-mode-line-update)) | |
1347 | ||
1348 | (narrow-to-region obeg oend) | |
1349 | (goto-char opoint) | |
155fd027 | 1350 | (run-hook-with-args 'comint-output-filter-functions string) |
91d75282 | 1351 | (set-buffer obuf))))) |
3ee91e68 | 1352 | |
c1da5c03 | 1353 | (defun comint-preinput-scroll-to-bottom () |
3ee91e68 RS |
1354 | "Go to the end of buffer in all windows showing it. |
1355 | Movement occurs if point in the selected window is not after the process mark, | |
1356 | and `this-command' is an insertion command. Insertion commands recognised | |
d637fa56 | 1357 | are `self-insert-command', `comint-magic-space', `yank', and `hilit-yank'. |
3ee91e68 RS |
1358 | Depends on the value of `comint-scroll-to-bottom-on-input'. |
1359 | ||
c1da5c03 | 1360 | This function should be a pre-command hook." |
3ee91e68 | 1361 | (if (and comint-scroll-to-bottom-on-input |
91d75282 | 1362 | (memq this-command '(self-insert-command comint-magic-space yank |
d637fa56 | 1363 | hilit-yank))) |
3ee91e68 RS |
1364 | (let* ((selected (selected-window)) |
1365 | (current (current-buffer)) | |
1366 | (process (get-buffer-process current)) | |
1367 | (scroll comint-scroll-to-bottom-on-input)) | |
d637fa56 | 1368 | (if (and process (< (point) (process-mark process))) |
0744bf64 RS |
1369 | (if (eq scroll 'this) |
1370 | (goto-char (point-max)) | |
1371 | (walk-windows | |
1372 | (function (lambda (window) | |
1373 | (if (and (eq (window-buffer window) current) | |
d637fa56 | 1374 | (or (eq scroll t) (eq scroll 'all))) |
0744bf64 RS |
1375 | (progn |
1376 | (select-window window) | |
1377 | (goto-char (point-max)) | |
1378 | (select-window selected))))) | |
d637fa56 | 1379 | nil t)))))) |
3ee91e68 | 1380 | |
c1da5c03 | 1381 | (defun comint-postoutput-scroll-to-bottom (string) |
3ee91e68 RS |
1382 | "Go to the end of buffer in all windows showing it. |
1383 | Does not scroll if the current line is the last line in the buffer. | |
1384 | Depends on the value of `comint-scroll-to-bottom-on-output' and | |
1385 | `comint-scroll-show-maximum-output'. | |
1386 | ||
fb9ab28a | 1387 | This function should be in the list `comint-output-filter-functions'." |
3ee91e68 RS |
1388 | (let* ((selected (selected-window)) |
1389 | (current (current-buffer)) | |
1390 | (process (get-buffer-process current)) | |
1391 | (scroll comint-scroll-to-bottom-on-output)) | |
d637fa56 RS |
1392 | (unwind-protect |
1393 | (if process | |
1394 | (walk-windows | |
1395 | (function (lambda (window) | |
1396 | (if (eq (window-buffer window) current) | |
1397 | (progn | |
1398 | (select-window window) | |
1399 | (if (and (< (point) (process-mark process)) | |
1400 | (or (eq scroll t) (eq scroll 'all) | |
107d7589 | 1401 | ;; Maybe user wants point to jump to end. |
d637fa56 RS |
1402 | (and (eq scroll 'this) (eq selected window)) |
1403 | (and (eq scroll 'others) (not (eq selected window))) | |
107d7589 SM |
1404 | ;; If point was at the end, keep it at end. |
1405 | (>= (point) comint-last-output-start))) | |
d637fa56 RS |
1406 | (goto-char (process-mark process))) |
1407 | ;; Optionally scroll so that the text | |
1408 | ;; ends at the bottom of the window. | |
1409 | (if (and comint-scroll-show-maximum-output | |
1410 | (>= (point) (process-mark process))) | |
1411 | (save-excursion | |
1412 | (goto-char (point-max)) | |
1413 | (recenter -1))) | |
1414 | (select-window selected))))) | |
1415 | nil t)) | |
1416 | (set-buffer current)))) | |
3ee91e68 | 1417 | |
39a185a9 SM |
1418 | (defun comint-truncate-buffer (&optional string) |
1419 | "Truncate the buffer to `comint-buffer-maximum-size'. | |
1420 | This function could be on `comint-output-filter-functions' or bound to a key." | |
1421 | (interactive) | |
1422 | (save-excursion | |
107d7589 | 1423 | (goto-char (process-mark (get-buffer-process (current-buffer)))) |
39a185a9 SM |
1424 | (forward-line (- comint-buffer-maximum-size)) |
1425 | (beginning-of-line) | |
1426 | (delete-region (point-min) (point)))) | |
1427 | ||
efcec33f RS |
1428 | (defun comint-strip-ctrl-m (&optional string) |
1429 | "Strip trailing `^M' characters from the current output group. | |
39a185a9 | 1430 | This function could be on `comint-output-filter-functions' or bound to a key." |
efcec33f RS |
1431 | (interactive) |
1432 | (let ((pmark (process-mark (get-buffer-process (current-buffer))))) | |
1433 | (save-excursion | |
71a895c6 KH |
1434 | (condition-case nil |
1435 | (goto-char | |
1436 | (if (interactive-p) comint-last-input-end comint-last-output-start)) | |
1437 | (error nil)) | |
efcec33f RS |
1438 | (while (re-search-forward "\r+$" pmark t) |
1439 | (replace-match "" t t))))) | |
1440 | (defalias 'shell-strip-ctrl-m 'comint-strip-ctrl-m) | |
1441 | ||
3ee91e68 RS |
1442 | (defun comint-show-maximum-output () |
1443 | "Put the end of the buffer at the bottom of the window." | |
1444 | (interactive) | |
1445 | (goto-char (point-max)) | |
1446 | (recenter -1)) | |
5511e411 | 1447 | |
be9b65ac | 1448 | (defun comint-get-old-input-default () |
cccd719e | 1449 | "Default for `comint-get-old-input'. |
c2ced5d8 | 1450 | Take the current line, and discard any initial text matching |
cccd719e | 1451 | `comint-prompt-regexp'." |
be9b65ac | 1452 | (save-excursion |
49116ac0 JB |
1453 | (beginning-of-line) |
1454 | (comint-skip-prompt) | |
1455 | (let ((beg (point))) | |
1456 | (end-of-line) | |
1457 | (buffer-substring beg (point))))) | |
1458 | ||
3ee91e68 RS |
1459 | (defun comint-copy-old-input () |
1460 | "Insert after prompt old input at point as new input to be edited. | |
1461 | Calls `comint-get-old-input' to get old input." | |
1462 | (interactive) | |
1463 | (let ((input (funcall comint-get-old-input)) | |
1464 | (process (get-buffer-process (current-buffer)))) | |
1465 | (if (not process) | |
1466 | (error "Current buffer has no process") | |
1467 | (goto-char (process-mark process)) | |
1468 | (insert input)))) | |
1469 | ||
49116ac0 | 1470 | (defun comint-skip-prompt () |
cccd719e | 1471 | "Skip past the text matching regexp `comint-prompt-regexp'. |
49116ac0 JB |
1472 | If this takes us past the end of the current line, don't skip at all." |
1473 | (let ((eol (save-excursion (end-of-line) (point)))) | |
1474 | (if (and (looking-at comint-prompt-regexp) | |
1475 | (<= (match-end 0) eol)) | |
1476 | (goto-char (match-end 0))))) | |
1477 | ||
49116ac0 | 1478 | (defun comint-after-pmark-p () |
3ee91e68 RS |
1479 | "Return t if point is after the process output marker." |
1480 | (let ((pmark (process-mark (get-buffer-process (current-buffer))))) | |
1481 | (<= (marker-position pmark) (point)))) | |
49116ac0 JB |
1482 | |
1483 | (defun comint-simple-send (proc string) | |
1484 | "Default function for sending to PROC input STRING. | |
1485 | This just sends STRING plus a newline. To override this, | |
cccd719e | 1486 | set the hook `comint-input-sender'." |
49116ac0 JB |
1487 | (comint-send-string proc string) |
1488 | (comint-send-string proc "\n")) | |
be9b65ac DL |
1489 | |
1490 | (defun comint-bol (arg) | |
1491 | "Goes to the beginning of line, then skips past the prompt, if any. | |
3ee91e68 | 1492 | If prefix argument is given (\\[universal-argument]) the prompt is not skipped. |
be9b65ac | 1493 | |
49116ac0 | 1494 | The prompt skip is done by skipping text matching the regular expression |
a77db92b | 1495 | `comint-prompt-regexp', a buffer local variable." |
be9b65ac DL |
1496 | (interactive "P") |
1497 | (beginning-of-line) | |
49116ac0 | 1498 | (if (null arg) (comint-skip-prompt))) |
be9b65ac | 1499 | |
b578f267 EN |
1500 | ;; These three functions are for entering text you don't want echoed or |
1501 | ;; saved -- typically passwords to ftp, telnet, or somesuch. | |
1502 | ;; Just enter m-x send-invisible and type in your line, or add | |
1503 | ;; `comint-watch-for-password-prompt' to `comint-output-filter-functions'. | |
49116ac0 | 1504 | |
87ef29fd | 1505 | (defun comint-read-noecho (prompt &optional stars) |
4be9a25f NF |
1506 | "Read a single line of text from user without echoing, and return it. |
1507 | Prompt with argument PROMPT, a string. Optional argument STARS causes | |
1508 | input to be echoed with '*' characters on the prompt line. Input ends with | |
1509 | RET, LFD, or ESC. DEL or C-h rubs out. C-u kills line. C-g aborts (if | |
cccd719e RS |
1510 | `inhibit-quit' is set because e.g. this function was called from a process |
1511 | filter and C-g is pressed, this function returns nil rather than a string). | |
4be9a25f NF |
1512 | |
1513 | Note that the keystrokes comprising the text can still be recovered | |
dc81e166 RS |
1514 | \(temporarily) with \\[view-lossage]. Some people find this worrysome. |
1515 | Once the caller uses the password, it can erase the password | |
1516 | by doing (fillarray STRING 0)." | |
4be9a25f | 1517 | (let ((ans "") |
dc81e166 | 1518 | (newans nil) |
4be9a25f NF |
1519 | (c 0) |
1520 | (echo-keystrokes 0) | |
87ef29fd | 1521 | (cursor-in-echo-area t) |
23cb053e KH |
1522 | (message-log-max nil) |
1523 | (done nil)) | |
4be9a25f NF |
1524 | (while (not done) |
1525 | (if stars | |
84a99e73 KH |
1526 | (message "%s%s" prompt (make-string (length ans) ?*)) |
1527 | (message "%s" prompt)) | |
af171fd0 RS |
1528 | ;; Use this instead of `read-char' to avoid "Non-character input-event". |
1529 | (setq c (read-char-exclusive)) | |
4be9a25f | 1530 | (cond ((= c ?\C-g) |
59f36b08 SM |
1531 | ;; This function may get called from a process filter, where |
1532 | ;; inhibit-quit is set. In later versions of emacs read-char | |
1533 | ;; may clear quit-flag itself and return C-g. That would make | |
1534 | ;; it impossible to quit this loop in a simple way, so | |
1535 | ;; re-enable it here (for backward-compatibility the check for | |
1536 | ;; quit-flag below would still be necessary, so this seems | |
1537 | ;; like the simplest way to do things). | |
1538 | (setq quit-flag t | |
1539 | done t)) | |
1540 | ((or (= c ?\r) (= c ?\n) (= c ?\e)) | |
1541 | (setq done t)) | |
1542 | ((= c ?\C-u) | |
dc81e166 | 1543 | (fillarray ans 0) |
59f36b08 SM |
1544 | (setq ans "")) |
1545 | ((and (/= c ?\b) (/= c ?\177)) | |
dc81e166 RS |
1546 | (setq newans (concat ans (char-to-string c))) |
1547 | (fillarray ans 0) | |
1548 | (setq ans newans)) | |
59f36b08 | 1549 | ((> (length ans) 0) |
dc81e166 | 1550 | (aset ans (1- (length ans)) 0) |
59f36b08 | 1551 | (setq ans (substring ans 0 -1))))) |
4be9a25f NF |
1552 | (if quit-flag |
1553 | ;; Emulate a true quit, except that we have to return a value. | |
1554 | (prog1 | |
1555 | (setq quit-flag nil) | |
1556 | (message "Quit") | |
1557 | (beep t)) | |
1558 | (message "") | |
1559 | ans))) | |
87ef29fd | 1560 | |
be9b65ac | 1561 | (defun send-invisible (str) |
c2ced5d8 | 1562 | "Read a string without echoing. |
6152f5f0 SM |
1563 | Then send it to the process running in the current buffer. |
1564 | The string is sent using `comint-input-sender'. | |
49116ac0 JB |
1565 | Security bug: your string can still be temporarily recovered with |
1566 | \\[view-lossage]." | |
4a11114d | 1567 | (interactive "P") ; Defeat snooping via C-x ESC ESC |
be9b65ac | 1568 | (let ((proc (get-buffer-process (current-buffer)))) |
4a11114d SM |
1569 | (cond ((not proc) |
1570 | (error "Current buffer has no process")) | |
1571 | ((stringp str) | |
1572 | (funcall comint-input-sender proc str)) | |
1573 | (t | |
1574 | (let ((str (comint-read-noecho "Non-echoed text: " t))) | |
1575 | (if (stringp str) | |
1576 | (send-invisible str) | |
1577 | (message "Warning: text will be echoed"))))))) | |
21d9bf27 RS |
1578 | |
1579 | (defun comint-watch-for-password-prompt (string) | |
1580 | "Prompt in the minibuffer for password and send without echoing. | |
1581 | This function uses `send-invisible' to read and send a password to the buffer's | |
654d89d7 NF |
1582 | process if STRING contains a password prompt defined by |
1583 | `comint-password-prompt-regexp'. | |
21d9bf27 RS |
1584 | |
1585 | This function could be in the list `comint-output-filter-functions'." | |
654d89d7 | 1586 | (if (string-match comint-password-prompt-regexp string) |
21d9bf27 | 1587 | (send-invisible nil))) |
be9b65ac | 1588 | \f |
b578f267 | 1589 | ;; Low-level process communication |
be9b65ac | 1590 | |
7df7d5b3 RS |
1591 | (defalias 'comint-send-string 'process-send-string) |
1592 | (defalias 'comint-send-region 'process-send-region) | |
be9b65ac | 1593 | \f |
b578f267 | 1594 | ;; Random input hackage |
be9b65ac | 1595 | |
49116ac0 | 1596 | (defun comint-kill-output () |
3ee91e68 RS |
1597 | "Kill all output from interpreter since last input. |
1598 | Does not delete the prompt." | |
be9b65ac | 1599 | (interactive) |
a9a6f83f RS |
1600 | (let ((proc (get-buffer-process (current-buffer))) |
1601 | (replacement nil)) | |
1602 | (save-excursion | |
1603 | (let ((pmark (progn (goto-char (process-mark proc)) | |
1604 | (beginning-of-line nil) | |
1605 | (point-marker)))) | |
1606 | (delete-region comint-last-input-end pmark) | |
37a9a675 | 1607 | (goto-char (process-mark proc)) |
a9a6f83f RS |
1608 | (setq replacement (concat "*** output flushed ***\n" |
1609 | (buffer-substring pmark (point)))) | |
1610 | (delete-region pmark (point)))) | |
1611 | ;; Output message and put back prompt | |
1612 | (comint-output-filter proc replacement))) | |
be9b65ac DL |
1613 | |
1614 | (defun comint-show-output () | |
49116ac0 | 1615 | "Display start of this batch of interpreter output at top of window. |
21d9bf27 | 1616 | Sets mark to the value of point when this command is run." |
be9b65ac | 1617 | (interactive) |
815c1a20 | 1618 | (push-mark) |
3ee91e68 | 1619 | (let ((pos (point))) |
42453245 | 1620 | (goto-char (or (marker-position comint-last-input-end) (point-max))) |
3ee91e68 RS |
1621 | (beginning-of-line 0) |
1622 | (set-window-start (selected-window) (point)) | |
815c1a20 | 1623 | (comint-skip-prompt))) |
be9b65ac DL |
1624 | |
1625 | (defun comint-interrupt-subjob () | |
1a94abea RS |
1626 | "Interrupt the current subjob. |
1627 | This command also kills the pending input | |
1628 | between the process-mark and point." | |
be9b65ac | 1629 | (interactive) |
1a94abea | 1630 | (comint-kill-input) |
49116ac0 | 1631 | (interrupt-process nil comint-ptyp)) |
be9b65ac DL |
1632 | |
1633 | (defun comint-kill-subjob () | |
1a94abea RS |
1634 | "Send kill signal to the current subjob. |
1635 | This command also kills the pending input | |
1636 | between the process-mark and point." | |
be9b65ac | 1637 | (interactive) |
1a94abea | 1638 | (comint-kill-input) |
49116ac0 | 1639 | (kill-process nil comint-ptyp)) |
be9b65ac DL |
1640 | |
1641 | (defun comint-quit-subjob () | |
1a94abea RS |
1642 | "Send quit signal to the current subjob. |
1643 | This command also kills the pending input | |
1644 | between the process-mark and point." | |
be9b65ac | 1645 | (interactive) |
1a94abea | 1646 | (comint-kill-input) |
49116ac0 | 1647 | (quit-process nil comint-ptyp)) |
be9b65ac DL |
1648 | |
1649 | (defun comint-stop-subjob () | |
49116ac0 | 1650 | "Stop the current subjob. |
1a94abea RS |
1651 | This command also kills the pending input |
1652 | between the process-mark and point. | |
1653 | ||
be9b65ac | 1654 | WARNING: if there is no current subjob, you can end up suspending |
49116ac0 JB |
1655 | the top-level process running in the buffer. If you accidentally do |
1656 | this, use \\[comint-continue-subjob] to resume the process. (This | |
1657 | is not a problem with most shells, since they ignore this signal.)" | |
be9b65ac | 1658 | (interactive) |
1a94abea | 1659 | (comint-kill-input) |
49116ac0 | 1660 | (stop-process nil comint-ptyp)) |
be9b65ac DL |
1661 | |
1662 | (defun comint-continue-subjob () | |
49116ac0 | 1663 | "Send CONT signal to process buffer's process group. |
be9b65ac DL |
1664 | Useful if you accidentally suspend the top-level process." |
1665 | (interactive) | |
49116ac0 | 1666 | (continue-process nil comint-ptyp)) |
be9b65ac DL |
1667 | |
1668 | (defun comint-kill-input () | |
49116ac0 | 1669 | "Kill all text from last stuff output by interpreter to point." |
be9b65ac | 1670 | (interactive) |
3ee91e68 RS |
1671 | (let ((pmark (process-mark (get-buffer-process (current-buffer))))) |
1672 | (if (> (point) (marker-position pmark)) | |
49116ac0 | 1673 | (kill-region pmark (point))))) |
be9b65ac DL |
1674 | |
1675 | (defun comint-delchar-or-maybe-eof (arg) | |
4a11114d SM |
1676 | "Delete ARG characters forward or send an EOF to subprocess. |
1677 | Sends an EOF only if point is at the end of the buffer and there is no input." | |
be9b65ac | 1678 | (interactive "p") |
90d6bfb9 KH |
1679 | (let ((proc (get-buffer-process (current-buffer)))) |
1680 | (if (and (eobp) proc (= (point) (marker-position (process-mark proc)))) | |
4a11114d SM |
1681 | (process-send-eof) |
1682 | (delete-char arg)))) | |
49116ac0 | 1683 | |
1de80a34 RM |
1684 | (defun comint-send-eof () |
1685 | "Send an EOF to the current buffer's process." | |
1686 | (interactive) | |
1687 | (process-send-eof)) | |
49116ac0 | 1688 | |
3ee91e68 RS |
1689 | |
1690 | (defun comint-backward-matching-input (regexp arg) | |
1691 | "Search backward through buffer for match for REGEXP. | |
1692 | Matches are searched for on lines that match `comint-prompt-regexp'. | |
1693 | With prefix argument N, search for Nth previous match. | |
1694 | If N is negative, find the next or Nth next match." | |
1695 | (interactive (comint-regexp-arg "Backward input matching (regexp): ")) | |
1696 | (let* ((re (concat comint-prompt-regexp ".*" regexp)) | |
1697 | (pos (save-excursion (end-of-line (if (> arg 0) 0 1)) | |
1698 | (if (re-search-backward re nil t arg) | |
1699 | (point))))) | |
1700 | (if (null pos) | |
1701 | (progn (message "Not found") | |
1702 | (ding)) | |
1703 | (goto-char pos) | |
1704 | (comint-bol nil)))) | |
1705 | ||
1706 | (defun comint-forward-matching-input (regexp arg) | |
1707 | "Search forward through buffer for match for REGEXP. | |
1708 | Matches are searched for on lines that match `comint-prompt-regexp'. | |
1709 | With prefix argument N, search for Nth following match. | |
1710 | If N is negative, find the previous or Nth previous match." | |
1711 | (interactive (comint-regexp-arg "Forward input matching (regexp): ")) | |
1712 | (comint-backward-matching-input regexp (- arg))) | |
1713 | ||
1714 | ||
1de80a34 | 1715 | (defun comint-next-prompt (n) |
3ee91e68 | 1716 | "Move to end of Nth next prompt in the buffer. |
1de80a34 RM |
1717 | See `comint-prompt-regexp'." |
1718 | (interactive "p") | |
3ee91e68 RS |
1719 | (let ((paragraph-start comint-prompt-regexp)) |
1720 | (end-of-line (if (> n 0) 1 0)) | |
1721 | (forward-paragraph n) | |
1722 | (comint-skip-prompt))) | |
49116ac0 | 1723 | |
3ee91e68 RS |
1724 | (defun comint-previous-prompt (n) |
1725 | "Move to end of Nth previous prompt in the buffer. | |
1de80a34 RM |
1726 | See `comint-prompt-regexp'." |
1727 | (interactive "p") | |
3ee91e68 | 1728 | (comint-next-prompt (- n))) |
be9b65ac | 1729 | \f |
b578f267 EN |
1730 | ;; Support for source-file processing commands. |
1731 | ;;============================================================================ | |
1732 | ;; Many command-interpreters (e.g., Lisp, Scheme, Soar) have | |
1733 | ;; commands that process files of source text (e.g. loading or compiling | |
1734 | ;; files). So the corresponding process-in-a-buffer modes have commands | |
1735 | ;; for doing this (e.g., lisp-load-file). The functions below are useful | |
1736 | ;; for defining these commands. | |
1737 | ;; | |
1738 | ;; Alas, these guys don't do exactly the right thing for Lisp, Scheme | |
1739 | ;; and Soar, in that they don't know anything about file extensions. | |
1740 | ;; So the compile/load interface gets the wrong default occasionally. | |
1741 | ;; The load-file/compile-file default mechanism could be smarter -- it | |
1742 | ;; doesn't know about the relationship between filename extensions and | |
1743 | ;; whether the file is source or executable. If you compile foo.lisp | |
1744 | ;; with compile-file, then the next load-file should use foo.bin for | |
1745 | ;; the default, not foo.lisp. This is tricky to do right, particularly | |
1746 | ;; because the extension for executable files varies so much (.o, .bin, | |
1747 | ;; .lbin, .mo, .vo, .ao, ...). | |
1748 | ||
1749 | ||
1750 | ;; COMINT-SOURCE-DEFAULT -- determines defaults for source-file processing | |
1751 | ;; commands. | |
1752 | ;; | |
1753 | ;; COMINT-CHECK-SOURCE -- if FNAME is in a modified buffer, asks you if you | |
1754 | ;; want to save the buffer before issuing any process requests to the command | |
1755 | ;; interpreter. | |
1756 | ;; | |
1757 | ;; COMINT-GET-SOURCE -- used by the source-file processing commands to prompt | |
1758 | ;; for the file to process. | |
1759 | ||
1760 | ;; (COMINT-SOURCE-DEFAULT previous-dir/file source-modes) | |
1761 | ;;============================================================================ | |
1762 | ;; This function computes the defaults for the load-file and compile-file | |
1763 | ;; commands for tea, soar, cmulisp, and cmuscheme modes. | |
1764 | ;; | |
1765 | ;; - PREVIOUS-DIR/FILE is a pair (directory . filename) from the last | |
1766 | ;; source-file processing command. NIL if there hasn't been one yet. | |
1767 | ;; - SOURCE-MODES is a list used to determine what buffers contain source | |
1768 | ;; files: if the major mode of the buffer is in SOURCE-MODES, it's source. | |
1769 | ;; Typically, (lisp-mode) or (scheme-mode). | |
1770 | ;; | |
1771 | ;; If the command is given while the cursor is inside a string, *and* | |
1772 | ;; the string is an existing filename, *and* the filename is not a directory, | |
1773 | ;; then the string is taken as default. This allows you to just position | |
1774 | ;; your cursor over a string that's a filename and have it taken as default. | |
1775 | ;; | |
1776 | ;; If the command is given in a file buffer whose major mode is in | |
1777 | ;; SOURCE-MODES, then the the filename is the default file, and the | |
1778 | ;; file's directory is the default directory. | |
1779 | ;; | |
1780 | ;; If the buffer isn't a source file buffer (e.g., it's the process buffer), | |
1781 | ;; then the default directory & file are what was used in the last source-file | |
1782 | ;; processing command (i.e., PREVIOUS-DIR/FILE). If this is the first time | |
1783 | ;; the command has been run (PREVIOUS-DIR/FILE is nil), the default directory | |
1784 | ;; is the cwd, with no default file. (\"no default file\" = nil) | |
1785 | ;; | |
1786 | ;; SOURCE-REGEXP is typically going to be something like (tea-mode) | |
1787 | ;; for T programs, (lisp-mode) for Lisp programs, (soar-mode lisp-mode) | |
1788 | ;; for Soar programs, etc. | |
1789 | ;; | |
1790 | ;; The function returns a pair: (default-directory . default-file). | |
be9b65ac DL |
1791 | |
1792 | (defun comint-source-default (previous-dir/file source-modes) | |
1793 | (cond ((and buffer-file-name (memq major-mode source-modes)) | |
1794 | (cons (file-name-directory buffer-file-name) | |
1795 | (file-name-nondirectory buffer-file-name))) | |
1796 | (previous-dir/file) | |
1797 | (t | |
1798 | (cons default-directory nil)))) | |
1799 | ||
49116ac0 | 1800 | |
b578f267 EN |
1801 | ;; (COMINT-CHECK-SOURCE fname) |
1802 | ;;============================================================================ | |
1803 | ;; Prior to loading or compiling (or otherwise processing) a file (in the CMU | |
1804 | ;; process-in-a-buffer modes), this function can be called on the filename. | |
1805 | ;; If the file is loaded into a buffer, and the buffer is modified, the user | |
1806 | ;; is queried to see if he wants to save the buffer before proceeding with | |
1807 | ;; the load or compile. | |
be9b65ac DL |
1808 | |
1809 | (defun comint-check-source (fname) | |
1810 | (let ((buff (get-file-buffer fname))) | |
1811 | (if (and buff | |
1812 | (buffer-modified-p buff) | |
3ee91e68 | 1813 | (y-or-n-p (format "Save buffer %s first? " (buffer-name buff)))) |
be9b65ac DL |
1814 | ;; save BUFF. |
1815 | (let ((old-buffer (current-buffer))) | |
1816 | (set-buffer buff) | |
1817 | (save-buffer) | |
1818 | (set-buffer old-buffer))))) | |
1819 | ||
49116ac0 | 1820 | |
b578f267 EN |
1821 | ;; (COMINT-GET-SOURCE prompt prev-dir/file source-modes mustmatch-p) |
1822 | ;;============================================================================ | |
1823 | ;; COMINT-GET-SOURCE is used to prompt for filenames in command-interpreter | |
1824 | ;; commands that process source files (like loading or compiling a file). | |
1825 | ;; It prompts for the filename, provides a default, if there is one, | |
1826 | ;; and returns the result filename. | |
1827 | ;; | |
1828 | ;; See COMINT-SOURCE-DEFAULT for more on determining defaults. | |
1829 | ;; | |
1830 | ;; PROMPT is the prompt string. PREV-DIR/FILE is the (directory . file) pair | |
1831 | ;; from the last source processing command. SOURCE-MODES is a list of major | |
1832 | ;; modes used to determine what file buffers contain source files. (These | |
1833 | ;; two arguments are used for determining defaults). If MUSTMATCH-P is true, | |
1834 | ;; then the filename reader will only accept a file that exists. | |
1835 | ;; | |
1836 | ;; A typical use: | |
1837 | ;; (interactive (comint-get-source "Compile file: " prev-lisp-dir/file | |
1838 | ;; '(lisp-mode) t)) | |
1839 | ||
1840 | ;; This is pretty stupid about strings. It decides we're in a string | |
1841 | ;; if there's a quote on both sides of point on the current line. | |
be9b65ac | 1842 | (defun comint-extract-string () |
cccd719e | 1843 | "Return string around POINT that starts the current line, or nil." |
be9b65ac DL |
1844 | (save-excursion |
1845 | (let* ((point (point)) | |
1846 | (bol (progn (beginning-of-line) (point))) | |
1847 | (eol (progn (end-of-line) (point))) | |
1848 | (start (progn (goto-char point) | |
1849 | (and (search-backward "\"" bol t) | |
1850 | (1+ (point))))) | |
1851 | (end (progn (goto-char point) | |
1852 | (and (search-forward "\"" eol t) | |
1853 | (1- (point)))))) | |
1854 | (and start end | |
1855 | (buffer-substring start end))))) | |
1856 | ||
1857 | (defun comint-get-source (prompt prev-dir/file source-modes mustmatch-p) | |
1858 | (let* ((def (comint-source-default prev-dir/file source-modes)) | |
1859 | (stringfile (comint-extract-string)) | |
1860 | (sfile-p (and stringfile | |
49116ac0 JB |
1861 | (condition-case () |
1862 | (file-exists-p stringfile) | |
1863 | (error nil)) | |
be9b65ac DL |
1864 | (not (file-directory-p stringfile)))) |
1865 | (defdir (if sfile-p (file-name-directory stringfile) | |
1866 | (car def))) | |
1867 | (deffile (if sfile-p (file-name-nondirectory stringfile) | |
1868 | (cdr def))) | |
1869 | (ans (read-file-name (if deffile (format "%s(default %s) " | |
1870 | prompt deffile) | |
1871 | prompt) | |
1872 | defdir | |
1873 | (concat defdir deffile) | |
1874 | mustmatch-p))) | |
1875 | (list (expand-file-name (substitute-in-file-name ans))))) | |
1876 | ||
b578f267 EN |
1877 | ;; I am somewhat divided on this string-default feature. It seems |
1878 | ;; to violate the principle-of-least-astonishment, in that it makes | |
1879 | ;; the default harder to predict, so you actually have to look and see | |
1880 | ;; what the default really is before choosing it. This can trip you up. | |
1881 | ;; On the other hand, it can be useful, I guess. I would appreciate feedback | |
1882 | ;; on this. | |
1883 | ;; -Olin | |
49116ac0 | 1884 | |
be9b65ac | 1885 | \f |
b578f267 EN |
1886 | ;; Simple process query facility. |
1887 | ;; =========================================================================== | |
1888 | ;; This function is for commands that want to send a query to the process | |
1889 | ;; and show the response to the user. For example, a command to get the | |
1890 | ;; arglist for a Common Lisp function might send a "(arglist 'foo)" query | |
1891 | ;; to an inferior Common Lisp process. | |
1892 | ;; | |
1893 | ;; This simple facility just sends strings to the inferior process and pops | |
1894 | ;; up a window for the process buffer so you can see what the process | |
1895 | ;; responds with. We don't do anything fancy like try to intercept what the | |
1896 | ;; process responds with and put it in a pop-up window or on the message | |
1897 | ;; line. We just display the buffer. Low tech. Simple. Works good. | |
1898 | ||
1899 | ;; Send to the inferior process PROC the string STR. Pop-up but do not select | |
1900 | ;; a window for the inferior process so that its response can be seen. | |
be9b65ac DL |
1901 | (defun comint-proc-query (proc str) |
1902 | (let* ((proc-buf (process-buffer proc)) | |
1903 | (proc-mark (process-mark proc))) | |
1904 | (display-buffer proc-buf) | |
1905 | (set-buffer proc-buf) ; but it's not the selected *window* | |
1906 | (let ((proc-win (get-buffer-window proc-buf)) | |
1907 | (proc-pt (marker-position proc-mark))) | |
1908 | (comint-send-string proc str) ; send the query | |
1909 | (accept-process-output proc) ; wait for some output | |
1910 | ;; Try to position the proc window so you can see the answer. | |
1911 | ;; This is bogus code. If you delete the (sit-for 0), it breaks. | |
1912 | ;; I don't know why. Wizards invited to improve it. | |
17193f89 SM |
1913 | (unless (pos-visible-in-window-p proc-pt proc-win) |
1914 | (let ((opoint (window-point proc-win))) | |
1915 | (set-window-point proc-win proc-mark) | |
1916 | (sit-for 0) | |
1917 | (if (not (pos-visible-in-window-p opoint proc-win)) | |
1918 | (push-mark opoint) | |
1919 | (set-window-point proc-win opoint))))))) | |
be9b65ac DL |
1920 | |
1921 | \f | |
b578f267 EN |
1922 | ;; Filename/command/history completion in a buffer |
1923 | ;; =========================================================================== | |
1924 | ;; Useful completion functions, courtesy of the Ergo group. | |
1925 | ||
1926 | ;; Six commands: | |
1927 | ;; comint-dynamic-complete Complete or expand command, filename, | |
1928 | ;; history at point. | |
1929 | ;; comint-dynamic-complete-filename Complete filename at point. | |
1930 | ;; comint-dynamic-list-filename-completions List completions in help buffer. | |
1931 | ;; comint-replace-by-expanded-filename Expand and complete filename at point; | |
1932 | ;; replace with expanded/completed name. | |
1933 | ;; comint-dynamic-simple-complete Complete stub given candidates. | |
1934 | ||
1935 | ;; These are not installed in the comint-mode keymap. But they are | |
1936 | ;; available for people who want them. Shell-mode installs them: | |
1937 | ;; (define-key shell-mode-map "\t" 'comint-dynamic-complete) | |
1938 | ;; (define-key shell-mode-map "\M-?" | |
1939 | ;; 'comint-dynamic-list-filename-completions))) | |
1940 | ;; | |
1941 | ;; Commands like this are fine things to put in load hooks if you | |
1942 | ;; want them present in specific modes. | |
49116ac0 | 1943 | |
ed366e87 | 1944 | (defcustom comint-completion-autolist nil |
5ef2f81f | 1945 | "*If non-nil, automatically list possibilities on partial completion. |
ed366e87 RS |
1946 | This mirrors the optional behavior of tcsh." |
1947 | :type 'boolean | |
1948 | :group 'comint-completion) | |
3ee91e68 | 1949 | |
ed366e87 | 1950 | (defcustom comint-completion-addsuffix t |
3ee91e68 | 1951 | "*If non-nil, add a `/' to completed directories, ` ' to file names. |
59f36b08 SM |
1952 | If a cons pair, it should be of the form (DIRSUFFIX . FILESUFFIX) where |
1953 | DIRSUFFIX and FILESUFFIX are strings added on unambiguous or exact completion. | |
ed366e87 RS |
1954 | This mirrors the optional behavior of tcsh." |
1955 | :type 'boolean | |
1956 | :group 'comint-completion) | |
3ee91e68 | 1957 | |
ed366e87 | 1958 | (defcustom comint-completion-recexact nil |
3ee91e68 | 1959 | "*If non-nil, use shortest completion if characters cannot be added. |
fdb7791d | 1960 | This mirrors the optional behavior of tcsh. |
3ee91e68 | 1961 | |
ed366e87 RS |
1962 | A non-nil value is useful if `comint-completion-autolist' is non-nil too." |
1963 | :type 'boolean | |
1964 | :group 'comint-completion) | |
3ee91e68 | 1965 | |
ed366e87 | 1966 | (defcustom comint-completion-fignore nil |
497513ad RS |
1967 | "*List of suffixes to be disregarded during file completion. |
1968 | This mirrors the optional behavior of bash and tcsh. | |
1969 | ||
ed366e87 RS |
1970 | Note that this applies to `comint-dynamic-complete-filename' only." |
1971 | :type '(repeat (string :tag "Suffix")) | |
1972 | :group 'comint-completion) | |
497513ad | 1973 | |
3ee91e68 RS |
1974 | (defvar comint-file-name-prefix "" |
1975 | "Prefix prepended to absolute file names taken from process input. | |
1976 | This is used by comint's and shell's completion functions, and by shell's | |
1977 | directory tracking functions.") | |
be9b65ac | 1978 | |
2d4382e5 | 1979 | (defvar comint-file-name-chars |
da418b28 GV |
1980 | (if (memq system-type '(ms-dos windows-nt)) |
1981 | "~/A-Za-z0-9_^$!#%&{}@`'.,:()-" | |
1982 | "~/A-Za-z0-9+@:_.$#%,={}-") | |
2d4382e5 | 1983 | "String of characters valid in a file name. |
af898b0f RS |
1984 | Note that all non-ASCII characters are considered valid in a file name |
1985 | regardless of what this variable says. | |
107d7589 SM |
1986 | |
1987 | This is a good thing to set in mode hooks.") | |
1988 | ||
59f36b08 | 1989 | (defvar comint-file-name-quote-list nil |
ed366e87 | 1990 | "List of characters to quote with `\' when in a file name. |
59f36b08 SM |
1991 | |
1992 | This is a good thing to set in mode hooks.") | |
1993 | ||
3ee91e68 RS |
1994 | |
1995 | (defun comint-directory (directory) | |
1996 | ;; Return expanded DIRECTORY, with `comint-file-name-prefix' if absolute. | |
1997 | (expand-file-name (if (file-name-absolute-p directory) | |
1998 | (concat comint-file-name-prefix directory) | |
1999 | directory))) | |
2000 | ||
2001 | ||
1b421826 RS |
2002 | (defun comint-word (word-chars) |
2003 | "Return the word of WORD-CHARS at point, or nil if non is found. | |
2004 | Word constituents are considered to be those in WORD-CHARS, which is like the | |
af898b0f RS |
2005 | inside of a \"[...]\" (see `skip-chars-forward'), |
2006 | plus all non-ASCII characters." | |
3ee91e68 | 2007 | (save-excursion |
59f36b08 SM |
2008 | (let ((non-word-chars (concat "[^\\\\" word-chars "]")) (here (point))) |
2009 | (while (and (re-search-backward non-word-chars nil 'move) | |
af898b0f RS |
2010 | ;;(memq (char-after (point)) shell-file-name-quote-list) |
2011 | (or (>= (following-char) 128) | |
2012 | (eq (preceding-char) ?\\))) | |
59f36b08 | 2013 | (backward-char 1)) |
4288965c | 2014 | ;; Don't go forward over a word-char (this can happen if we're at bob). |
17193f89 SM |
2015 | (when (or (not (bobp)) (looking-at non-word-chars)) |
2016 | (forward-char 1)) | |
4288965c | 2017 | ;; Set match-data to match the entire string. |
17193f89 | 2018 | (when (< (point) here) |
e4947876 | 2019 | (set-match-data (list (point) here)) |
17193f89 | 2020 | (match-string 0))))) |
3ee91e68 | 2021 | |
7a35a067 RS |
2022 | (defun comint-substitute-in-file-name (filename) |
2023 | "Return FILENAME with environment variables substituted. | |
2024 | Supports additional environment variable syntax of the command | |
2025 | interpreter (e.g., the percent notation of cmd.exe on NT)." | |
2026 | (let ((name (substitute-in-file-name filename))) | |
2027 | (if (memq system-type '(ms-dos windows-nt)) | |
2028 | (let (env-var-name | |
2029 | env-var-val) | |
2030 | (save-match-data | |
2031 | (while (string-match "%\\([^\\\\/]*\\)%" name) | |
2032 | (setq env-var-name | |
2033 | (substring name (match-beginning 1) (match-end 1))) | |
2034 | (setq env-var-val (if (getenv env-var-name) | |
2035 | (getenv env-var-name) | |
2036 | "")) | |
2037 | (setq name (replace-match env-var-val nil nil name)))))) | |
2038 | name)) | |
3ee91e68 | 2039 | |
1b421826 RS |
2040 | (defun comint-match-partial-filename () |
2041 | "Return the filename at point, or nil if non is found. | |
2042 | Environment variables are substituted. See `comint-word'." | |
2d4382e5 | 2043 | (let ((filename (comint-word comint-file-name-chars))) |
7a35a067 RS |
2044 | (and filename (comint-substitute-in-file-name |
2045 | (comint-unquote-filename filename))))) | |
59f36b08 SM |
2046 | |
2047 | ||
2048 | (defun comint-quote-filename (filename) | |
2049 | "Return FILENAME with magic characters quoted. | |
2050 | Magic characters are those in `comint-file-name-quote-list'." | |
2051 | (if (null comint-file-name-quote-list) | |
2052 | filename | |
2053 | (let ((regexp | |
2054 | (format "\\(^\\|[^\\]\\)\\([%s]\\)" | |
2055 | (mapconcat 'char-to-string comint-file-name-quote-list "")))) | |
2056 | (save-match-data | |
2057 | (while (string-match regexp filename) | |
2058 | (setq filename (replace-match "\\1\\\\\\2" nil nil filename))) | |
2059 | filename)))) | |
2060 | ||
2061 | (defun comint-unquote-filename (filename) | |
2062 | "Return FILENAME with quoted characters unquoted." | |
2063 | (if (null comint-file-name-quote-list) | |
2064 | filename | |
2065 | (save-match-data | |
dbdfff7f RS |
2066 | (let ((i 0)) |
2067 | (while (string-match "\\\\\\(.\\)" filename i) | |
2068 | (setq filename (replace-match "\\1" nil nil filename)) | |
2069 | (setq i (+ 1 (match-beginning 0))))) | |
59f36b08 | 2070 | filename))) |
be9b65ac | 2071 | |
49116ac0 | 2072 | |
be9b65ac | 2073 | (defun comint-dynamic-complete () |
1b421826 RS |
2074 | "Dynamically perform completion at point. |
2075 | Calls the functions in `comint-dynamic-complete-functions' to perform | |
2076 | completion until a function returns non-nil, at which point completion is | |
2077 | assumed to have occurred." | |
3ee91e68 | 2078 | (interactive) |
155fd027 | 2079 | (run-hook-with-args-until-success 'comint-dynamic-complete-functions)) |
3ee91e68 RS |
2080 | |
2081 | ||
2082 | (defun comint-dynamic-complete-filename () | |
49116ac0 | 2083 | "Dynamically complete the filename at point. |
1b421826 RS |
2084 | Completes if after a filename. See `comint-match-partial-filename' and |
2085 | `comint-dynamic-complete-as-filename'. | |
3ee91e68 RS |
2086 | This function is similar to `comint-replace-by-expanded-filename', except that |
2087 | it won't change parts of the filename already entered in the buffer; it just | |
2088 | adds completion characters to the end of the filename. A completions listing | |
2089 | may be shown in a help buffer if completion is ambiguous. | |
2090 | ||
497513ad RS |
2091 | Completion is dependent on the value of `comint-completion-addsuffix', |
2092 | `comint-completion-recexact' and `comint-completion-fignore', and the timing of | |
2093 | completions listing is dependent on the value of `comint-completion-autolist'. | |
1b421826 RS |
2094 | |
2095 | Returns t if successful." | |
3ee91e68 | 2096 | (interactive) |
17193f89 SM |
2097 | (when (comint-match-partial-filename) |
2098 | (unless (window-minibuffer-p (selected-window)) | |
2099 | (message "Completing file name...")) | |
2100 | (comint-dynamic-complete-as-filename))) | |
1b421826 RS |
2101 | |
2102 | (defun comint-dynamic-complete-as-filename () | |
2103 | "Dynamically complete at point as a filename. | |
2104 | See `comint-dynamic-complete-filename'. Returns t if successful." | |
107d7589 | 2105 | (let* ((completion-ignore-case (memq system-type '(ms-dos windows-nt))) |
497513ad | 2106 | (completion-ignored-extensions comint-completion-fignore) |
107d7589 SM |
2107 | ;; If we bind this, it breaks remote directory tracking in rlogin.el. |
2108 | ;; I think it was originally bound to solve file completion problems, | |
2109 | ;; but subsequent changes may have made this unnecessary. sm. | |
2110 | ;;(file-name-handler-alist nil) | |
81fb2771 | 2111 | (minibuffer-p (window-minibuffer-p (selected-window))) |
1b421826 | 2112 | (success t) |
17193f89 SM |
2113 | (dirsuffix (cond ((not comint-completion-addsuffix) |
2114 | "") | |
2115 | ((not (consp comint-completion-addsuffix)) | |
2116 | (char-to-string directory-sep-char)) | |
2117 | (t | |
2118 | (car comint-completion-addsuffix)))) | |
2119 | (filesuffix (cond ((not comint-completion-addsuffix) | |
2120 | "") | |
2121 | ((not (consp comint-completion-addsuffix)) | |
2122 | " ") | |
2123 | (t | |
2124 | (cdr comint-completion-addsuffix)))) | |
f79f58bc KH |
2125 | (filename (or (comint-match-partial-filename) "")) |
2126 | (pathdir (file-name-directory filename)) | |
2127 | (pathnondir (file-name-nondirectory filename)) | |
2128 | (directory (if pathdir (comint-directory pathdir) default-directory)) | |
81fb2771 | 2129 | (completion (file-name-completion pathnondir directory))) |
3ee91e68 RS |
2130 | (cond ((null completion) |
2131 | (message "No completions of %s" filename) | |
1b421826 | 2132 | (setq success nil)) |
3ee91e68 | 2133 | ((eq completion t) ; Means already completed "file". |
59f36b08 | 2134 | (insert filesuffix) |
17193f89 SM |
2135 | (unless minibuffer-p |
2136 | (message "Sole completion"))) | |
3ee91e68 RS |
2137 | ((string-equal completion "") ; Means completion on "directory/". |
2138 | (comint-dynamic-list-filename-completions)) | |
2139 | (t ; Completion string returned. | |
2140 | (let ((file (concat (file-name-as-directory directory) completion))) | |
59f36b08 SM |
2141 | (insert (comint-quote-filename |
2142 | (substring (directory-file-name completion) | |
2143 | (length pathnondir)))) | |
3ee91e68 RS |
2144 | (cond ((symbolp (file-name-completion completion directory)) |
2145 | ;; We inserted a unique completion. | |
59f36b08 | 2146 | (insert (if (file-directory-p file) dirsuffix filesuffix)) |
17193f89 SM |
2147 | (unless minibuffer-p |
2148 | (message "Completed"))) | |
3ee91e68 RS |
2149 | ((and comint-completion-recexact comint-completion-addsuffix |
2150 | (string-equal pathnondir completion) | |
2151 | (file-exists-p file)) | |
2152 | ;; It's not unique, but user wants shortest match. | |
59f36b08 | 2153 | (insert (if (file-directory-p file) dirsuffix filesuffix)) |
17193f89 SM |
2154 | (unless minibuffer-p |
2155 | (message "Completed shortest"))) | |
3ee91e68 RS |
2156 | ((or comint-completion-autolist |
2157 | (string-equal pathnondir completion)) | |
2158 | ;; It's not unique, list possible completions. | |
2159 | (comint-dynamic-list-filename-completions)) | |
2160 | (t | |
17193f89 SM |
2161 | (unless minibuffer-p |
2162 | (message "Partially completed"))))))) | |
1b421826 | 2163 | success)) |
3ee91e68 RS |
2164 | |
2165 | ||
2166 | (defun comint-replace-by-expanded-filename () | |
2167 | "Dynamically expand and complete the filename at point. | |
2168 | Replace the filename with an expanded, canonicalised and completed replacement. | |
2169 | \"Expanded\" means environment variables (e.g., $HOME) and `~'s are replaced | |
2170 | with the corresponding directories. \"Canonicalised\" means `..' and `.' are | |
2171 | removed, and the filename is made absolute instead of relative. For expansion | |
2172 | see `expand-file-name' and `substitute-in-file-name'. For completion see | |
2173 | `comint-dynamic-complete-filename'." | |
be9b65ac | 2174 | (interactive) |
3ee91e68 RS |
2175 | (replace-match (expand-file-name (comint-match-partial-filename)) t t) |
2176 | (comint-dynamic-complete-filename)) | |
2177 | ||
2178 | ||
3ee91e68 RS |
2179 | (defun comint-dynamic-simple-complete (stub candidates) |
2180 | "Dynamically complete STUB from CANDIDATES list. | |
2181 | This function inserts completion characters at point by completing STUB from | |
2182 | the strings in CANDIDATES. A completions listing may be shown in a help buffer | |
2183 | if completion is ambiguous. | |
2184 | ||
1b421826 RS |
2185 | Returns nil if no completion was inserted. |
2186 | Returns `sole' if completed with the only completion match. | |
2187 | Returns `shortest' if completed with the shortest of the completion matches. | |
2188 | Returns `partial' if completed as far as possible with the completion matches. | |
2189 | Returns `listed' if a completion listing was shown. | |
2190 | ||
3ee91e68 | 2191 | See also `comint-dynamic-complete-filename'." |
107d7589 | 2192 | (let* ((completion-ignore-case (memq system-type '(ms-dos windows-nt))) |
59f36b08 SM |
2193 | (suffix (cond ((not comint-completion-addsuffix) "") |
2194 | ((not (consp comint-completion-addsuffix)) " ") | |
2195 | (t (cdr comint-completion-addsuffix)))) | |
3ee91e68 RS |
2196 | (candidates (mapcar (function (lambda (x) (list x))) candidates)) |
2197 | (completions (all-completions stub candidates))) | |
be9b65ac | 2198 | (cond ((null completions) |
3ee91e68 | 2199 | (message "No completions of %s" stub) |
1b421826 | 2200 | nil) |
3ee91e68 RS |
2201 | ((= 1 (length completions)) ; Gotcha! |
2202 | (let ((completion (car completions))) | |
2203 | (if (string-equal completion stub) | |
2204 | (message "Sole completion") | |
2205 | (insert (substring completion (length stub))) | |
2206 | (message "Completed")) | |
59f36b08 | 2207 | (insert suffix) |
1b421826 | 2208 | 'sole)) |
3ee91e68 RS |
2209 | (t ; There's no unique completion. |
2210 | (let ((completion (try-completion stub candidates))) | |
2211 | ;; Insert the longest substring. | |
2212 | (insert (substring completion (length stub))) | |
2213 | (cond ((and comint-completion-recexact comint-completion-addsuffix | |
2214 | (string-equal stub completion) | |
2215 | (member completion completions)) | |
2216 | ;; It's not unique, but user wants shortest match. | |
59f36b08 | 2217 | (insert suffix) |
1b421826 RS |
2218 | (message "Completed shortest") |
2219 | 'shortest) | |
3ee91e68 RS |
2220 | ((or comint-completion-autolist |
2221 | (string-equal stub completion)) | |
2222 | ;; It's not unique, list possible completions. | |
1b421826 RS |
2223 | (comint-dynamic-list-completions completions) |
2224 | 'listed) | |
3ee91e68 | 2225 | (t |
1b421826 RS |
2226 | (message "Partially completed") |
2227 | 'partial))))))) | |
3ee91e68 RS |
2228 | |
2229 | ||
2230 | (defun comint-dynamic-list-filename-completions () | |
2231 | "List in help buffer possible completions of the filename at point." | |
2232 | (interactive) | |
107d7589 SM |
2233 | (let* ((completion-ignore-case (memq system-type '(ms-dos windows-nt))) |
2234 | ;; If we bind this, it breaks remote directory tracking in rlogin.el. | |
2235 | ;; I think it was originally bound to solve file completion problems, | |
2236 | ;; but subsequent changes may have made this unnecessary. sm. | |
2237 | ;;(file-name-handler-alist nil) | |
1b421826 | 2238 | (filename (or (comint-match-partial-filename) "")) |
3ee91e68 RS |
2239 | (pathdir (file-name-directory filename)) |
2240 | (pathnondir (file-name-nondirectory filename)) | |
2241 | (directory (if pathdir (comint-directory pathdir) default-directory)) | |
2242 | (completions (file-name-all-completions pathnondir directory))) | |
59f36b08 SM |
2243 | (if (not completions) |
2244 | (message "No completions of %s" filename) | |
2245 | (comint-dynamic-list-completions | |
2246 | (mapcar 'comint-quote-filename completions))))) | |
3ee91e68 RS |
2247 | |
2248 | ||
2249 | (defun comint-dynamic-list-completions (completions) | |
2250 | "List in help buffer sorted COMPLETIONS. | |
2251 | Typing SPC flushes the help buffer." | |
2252 | (let ((conf (current-window-configuration))) | |
dd3038f6 | 2253 | (with-output-to-temp-buffer "*Completions*" |
3ee91e68 | 2254 | (display-completion-list (sort completions 'string-lessp))) |
3ee91e68 | 2255 | (message "Hit space to flush") |
7d5d472e RS |
2256 | (let (key first) |
2257 | (if (save-excursion | |
dd3038f6 | 2258 | (set-buffer (get-buffer "*Completions*")) |
7d5d472e RS |
2259 | (setq key (read-key-sequence nil) |
2260 | first (aref key 0)) | |
eb6c110a | 2261 | (and (consp first) (consp (event-start first)) |
7d5d472e | 2262 | (eq (window-buffer (posn-window (event-start first))) |
dd3038f6 | 2263 | (get-buffer "*Completions*")) |
7d5d472e RS |
2264 | (eq (key-binding key) 'mouse-choose-completion))) |
2265 | ;; If the user does mouse-choose-completion with the mouse, | |
2266 | ;; execute the command, then delete the completion window. | |
2267 | (progn | |
2268 | (mouse-choose-completion first) | |
2269 | (set-window-configuration conf)) | |
2270 | (if (eq first ?\ ) | |
2271 | (set-window-configuration conf) | |
2b5a5ed3 | 2272 | (setq unread-command-events (listify-key-sequence key))))))) |
be9b65ac | 2273 | \f |
361ecbf2 RS |
2274 | (defun comint-get-next-from-history () |
2275 | "After fetching a line from input history, this fetches the following line. | |
2276 | In other words, this recalls the input line after the line you recalled last. | |
2277 | You can use this to repeat a sequence of input lines." | |
2278 | (interactive) | |
2279 | (if comint-save-input-ring-index | |
2280 | (progn | |
2281 | (setq comint-input-ring-index (1+ comint-save-input-ring-index)) | |
2282 | (comint-next-input 1)) | |
2283 | (message "No previous history command"))) | |
2284 | ||
2285 | (defun comint-accumulate () | |
2286 | "Accumulate a line to send as input along with more lines. | |
2287 | This inserts a newline so that you can enter more text | |
2288 | to be sent along with this line. Use \\[comint-send-input] | |
2289 | to send all the accumulated input, at once. | |
2290 | The entire accumulated text becomes one item in the input history | |
2291 | when you send it." | |
2292 | (interactive) | |
2293 | (insert "\n") | |
2294 | (set-marker comint-accum-marker (point)) | |
2295 | (if comint-input-ring-index | |
2296 | (setq comint-save-input-ring-index | |
2297 | (- comint-input-ring-index 1)))) | |
2298 | ||
2299 | (defun comint-goto-process-mark () | |
2300 | "Move point to the process mark. | |
2301 | The process mark separates output, and input already sent, | |
2302 | from input that has not yet been sent." | |
2303 | (interactive) | |
2304 | (let ((proc (or (get-buffer-process (current-buffer)) | |
2305 | (error "Current buffer has no process")))) | |
2306 | (goto-char (process-mark proc)) | |
2307 | (message "Point is now at the process mark"))) | |
2308 | ||
2309 | (defun comint-bol-or-process-mark () | |
2310 | "Move point beginning of line (after prompt) or to the process mark. | |
2311 | The first time you use this command, it moves to the beginning of the line | |
2312 | \(but after the prompt, if any). If you repeat it again immediately, | |
2313 | it moves point to the process mark. | |
2314 | ||
2315 | The process mark separates the process output, along with input already sent, | |
2316 | from input that has not yet been sent. Ordinarily, the process mark | |
2317 | is at the beginning of the current input line; but if you have | |
2318 | used \\[comint-accumulate] to send multiple lines at once, | |
2319 | the process mark is at the beginning of the accumulated input." | |
2320 | (interactive) | |
2321 | (if (not (eq last-command 'comint-bol-or-mark)) | |
2322 | (comint-bol nil) | |
2323 | (comint-goto-process-mark))) | |
2324 | ||
2325 | (defun comint-set-process-mark () | |
2326 | "Set the process mark at point." | |
2327 | (interactive) | |
2328 | (let ((proc (or (get-buffer-process (current-buffer)) | |
2329 | (error "Current buffer has no process")))) | |
2330 | (set-marker (process-mark proc) (point)) | |
2331 | (message "Process mark set"))) | |
2332 | ||
2333 | \f | |
b578f267 EN |
2334 | ;; Converting process modes to use comint mode |
2335 | ;; =========================================================================== | |
2336 | ;; The code in the Emacs 19 distribution has all been modified to use comint | |
2337 | ;; where needed. However, there are `third-party' packages out there that | |
2338 | ;; still use the old shell mode. Here's a guide to conversion. | |
2339 | ;; | |
2340 | ;; Renaming variables | |
2341 | ;; Most of the work is renaming variables and functions. These are the common | |
2342 | ;; ones: | |
2343 | ;; Local variables: | |
2344 | ;; last-input-start comint-last-input-start | |
2345 | ;; last-input-end comint-last-input-end | |
2346 | ;; shell-prompt-pattern comint-prompt-regexp | |
2347 | ;; shell-set-directory-error-hook <no equivalent> | |
2348 | ;; Miscellaneous: | |
2349 | ;; shell-set-directory <unnecessary> | |
2350 | ;; shell-mode-map comint-mode-map | |
2351 | ;; Commands: | |
2352 | ;; shell-send-input comint-send-input | |
2353 | ;; shell-send-eof comint-delchar-or-maybe-eof | |
2354 | ;; kill-shell-input comint-kill-input | |
2355 | ;; interrupt-shell-subjob comint-interrupt-subjob | |
2356 | ;; stop-shell-subjob comint-stop-subjob | |
2357 | ;; quit-shell-subjob comint-quit-subjob | |
2358 | ;; kill-shell-subjob comint-kill-subjob | |
2359 | ;; kill-output-from-shell comint-kill-output | |
2360 | ;; show-output-from-shell comint-show-output | |
2361 | ;; copy-last-shell-input Use comint-previous-input/comint-next-input | |
2362 | ;; | |
2363 | ;; SHELL-SET-DIRECTORY is gone, its functionality taken over by | |
2364 | ;; SHELL-DIRECTORY-TRACKER, the shell mode's comint-input-filter-functions. | |
2365 | ;; Comint mode does not provide functionality equivalent to | |
2366 | ;; shell-set-directory-error-hook; it is gone. | |
2367 | ;; | |
2368 | ;; comint-last-input-start is provided for modes which want to munge | |
2369 | ;; the buffer after input is sent, perhaps because the inferior | |
2370 | ;; insists on echoing the input. The LAST-INPUT-START variable in | |
2371 | ;; the old shell package was used to implement a history mechanism, | |
2372 | ;; but you should think twice before using comint-last-input-start | |
2373 | ;; for this; the input history ring often does the job better. | |
2374 | ;; | |
2375 | ;; If you are implementing some process-in-a-buffer mode, called foo-mode, do | |
2376 | ;; *not* create the comint-mode local variables in your foo-mode function. | |
2377 | ;; This is not modular. Instead, call comint-mode, and let *it* create the | |
2378 | ;; necessary comint-specific local variables. Then create the | |
2379 | ;; foo-mode-specific local variables in foo-mode. Set the buffer's keymap to | |
2380 | ;; be foo-mode-map, and its mode to be foo-mode. Set the comint-mode hooks | |
2381 | ;; (comint-{prompt-regexp, input-filter, input-filter-functions, | |
2382 | ;; get-old-input) that need to be different from the defaults. Call | |
2383 | ;; foo-mode-hook, and you're done. Don't run the comint-mode hook yourself; | |
2384 | ;; comint-mode will take care of it. The following example, from shell.el, | |
2385 | ;; is typical: | |
2386 | ;; | |
2387 | ;; (defvar shell-mode-map '()) | |
2388 | ;; (cond ((not shell-mode-map) | |
2389 | ;; (setq shell-mode-map (copy-keymap comint-mode-map)) | |
2390 | ;; (define-key shell-mode-map "\C-c\C-f" 'shell-forward-command) | |
2391 | ;; (define-key shell-mode-map "\C-c\C-b" 'shell-backward-command) | |
2392 | ;; (define-key shell-mode-map "\t" 'comint-dynamic-complete) | |
2393 | ;; (define-key shell-mode-map "\M-?" | |
2394 | ;; 'comint-dynamic-list-filename-completions))) | |
2395 | ;; | |
2396 | ;; (defun shell-mode () | |
2397 | ;; (interactive) | |
2398 | ;; (comint-mode) | |
2399 | ;; (setq comint-prompt-regexp shell-prompt-pattern) | |
2400 | ;; (setq major-mode 'shell-mode) | |
2401 | ;; (setq mode-name "Shell") | |
2402 | ;; (use-local-map shell-mode-map) | |
2403 | ;; (make-local-variable 'shell-directory-stack) | |
2404 | ;; (setq shell-directory-stack nil) | |
2405 | ;; (add-hook 'comint-input-filter-functions 'shell-directory-tracker) | |
2406 | ;; (run-hooks 'shell-mode-hook)) | |
2407 | ;; | |
2408 | ;; | |
b578f267 EN |
2409 | ;; Completion for comint-mode users |
2410 | ;; | |
2411 | ;; For modes that use comint-mode, comint-dynamic-complete-functions is the | |
2412 | ;; hook to add completion functions to. Functions on this list should return | |
2413 | ;; non-nil if completion occurs (i.e., further completion should not occur). | |
2414 | ;; You could use comint-dynamic-simple-complete to do the bulk of the | |
2415 | ;; completion job. | |
49116ac0 | 2416 | \f |
49116ac0 | 2417 | (provide 'comint) |
c0274f38 | 2418 | |
b578f267 | 2419 | ;; comint.el ends here |