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