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