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