Mercurial > emacs
annotate lisp/comint.el @ 2563:c8ae06bda9fa
(global-map): Remove function key bindings that duplicate stuff in
loaddefs.el.
author | Eric S. Raymond <esr@snark.thyrsus.com> |
---|---|
date | Fri, 23 Apr 1993 02:21:02 +0000 |
parents | 577c77cfd199 |
children | 308da27928f2 |
rev | line source |
---|---|
662
8a533acedb77
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
638
diff
changeset
|
1 ;;; comint.el --- general command interpreter in a window stuff |
2307
10e417efb12a
Added or corrected Commentary sections
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2247
diff
changeset
|
2 |
1839
e10cdcb857d2
(make-comint): Added docstring.
Roland McGrath <roland@gnu.org>
parents:
1821
diff
changeset
|
3 ;; Copyright (C) 1988, 1990, 1992, 1993 Free Software Foundation, Inc. |
662
8a533acedb77
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
638
diff
changeset
|
4 |
909
4c6cdb66c74c
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
870
diff
changeset
|
5 ;; Author: Olin Shivers <shivers@cs.cmu.edu> |
2247
2c7997f249eb
Add or correct keywords
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2025
diff
changeset
|
6 ;; Keywords: processes |
846
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
811
diff
changeset
|
7 |
909
4c6cdb66c74c
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
870
diff
changeset
|
8 ;; This file is part of GNU Emacs. |
114 | 9 |
909
4c6cdb66c74c
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
870
diff
changeset
|
10 ;; GNU Emacs is free software; you can redistribute it and/or modify |
4c6cdb66c74c
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
870
diff
changeset
|
11 ;; it under the terms of the GNU General Public License as published by |
4c6cdb66c74c
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
870
diff
changeset
|
12 ;; the Free Software Foundation; either version 2, or (at your option) |
4c6cdb66c74c
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
870
diff
changeset
|
13 ;; any later version. |
114 | 14 |
909
4c6cdb66c74c
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
870
diff
changeset
|
15 ;; GNU Emacs is distributed in the hope that it will be useful, |
4c6cdb66c74c
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
870
diff
changeset
|
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of |
4c6cdb66c74c
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
870
diff
changeset
|
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
4c6cdb66c74c
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
870
diff
changeset
|
18 ;; GNU General Public License for more details. |
114 | 19 |
909
4c6cdb66c74c
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
870
diff
changeset
|
20 ;; You should have received a copy of the GNU General Public License |
4c6cdb66c74c
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
870
diff
changeset
|
21 ;; along with GNU Emacs; see the file COPYING. If not, write to |
4c6cdb66c74c
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
870
diff
changeset
|
22 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. |
114 | 23 |
807
4f28bd14272c
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
793
diff
changeset
|
24 ;;; Commentary: |
4f28bd14272c
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
793
diff
changeset
|
25 |
584 | 26 ;;; The changelog is at the end of this file. |
27 | |
28 ;;; Please send me bug reports, bug fixes, and extensions, so that I can | |
29 ;;; merge them into the master source. | |
30 ;;; - Olin Shivers (shivers@cs.cmu.edu) | |
31 | |
114 | 32 ;;; This file defines a general command-interpreter-in-a-buffer package |
33 ;;; (comint mode). The idea is that you can build specific process-in-a-buffer | |
34 ;;; modes on top of comint mode -- e.g., lisp, shell, scheme, T, soar, .... | |
35 ;;; This way, all these specific packages share a common base functionality, | |
36 ;;; and a common set of bindings, which makes them easier to use (and | |
37 ;;; saves code, implementation time, etc., etc.). | |
38 | |
584 | 39 ;;; Several packages are already defined using comint mode: |
2518
10509afe2588
Clean up cmu* uses in header comments.
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2455
diff
changeset
|
40 ;;; - shell.el defines a shell-in-a-buffer mode. |
584 | 41 ;;; - cmulisp.el defines a simple lisp-in-a-buffer mode. |
42 ;;; | |
43 ;;; - The file cmuscheme.el defines a scheme-in-a-buffer mode. | |
44 ;;; - The file tea.el tunes scheme and inferior-scheme modes for T. | |
45 ;;; - The file soar.el tunes lisp and inferior-lisp modes for Soar. | |
46 ;;; - cmutex.el defines tex and latex modes that invoke tex, latex, bibtex, | |
47 ;;; previewers, and printers from within emacs. | |
48 ;;; - background.el allows csh-like job control inside emacs. | |
49 ;;; It is pretty easy to make new derived modes for other processes. | |
50 | |
114 | 51 ;;; For documentation on the functionality provided by comint mode, and |
52 ;;; the hooks available for customising it, see the comments below. | |
53 ;;; For further information on the standard derived modes (shell, | |
54 ;;; inferior-lisp, inferior-scheme, ...), see the relevant source files. | |
55 | |
584 | 56 ;;; For hints on converting existing process modes (e.g., tex-mode, |
57 ;;; background, dbx, gdb, kermit, prolog, telnet) to use comint-mode | |
114 | 58 ;;; instead of shell-mode, see the notes at the end of this file. |
59 | |
60 | |
584 | 61 ;;; Brief Command Documentation: |
62 ;;;============================================================================ | |
2518
10509afe2588
Clean up cmu* uses in header comments.
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2455
diff
changeset
|
63 ;;; Comint Mode Commands: (common to all derived modes, like shell & cmulisp |
584 | 64 ;;; mode) |
65 ;;; | |
66 ;;; m-p comint-previous-input Cycle backwards in input history | |
67 ;;; m-n comint-next-input Cycle forwards | |
909
4c6cdb66c74c
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
870
diff
changeset
|
68 ;;; m-r comint-previous-similar-input Previous similar input |
4c6cdb66c74c
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
870
diff
changeset
|
69 ;;; m-s comint-next-similar-input Next similar input |
705 | 70 ;;; c-m-r comint-previous-input-matching Search backwards in input history |
584 | 71 ;;; return comint-send-input |
72 ;;; c-a comint-bol Beginning of line; skip prompt. | |
73 ;;; c-d comint-delchar-or-maybe-eof Delete char unless at end of buff. | |
74 ;;; c-c c-u comint-kill-input ^u | |
75 ;;; c-c c-w backward-kill-word ^w | |
76 ;;; c-c c-c comint-interrupt-subjob ^c | |
77 ;;; c-c c-z comint-stop-subjob ^z | |
78 ;;; c-c c-\ comint-quit-subjob ^\ | |
79 ;;; c-c c-o comint-kill-output Delete last batch of process output | |
80 ;;; c-c c-r comint-show-output Show last batch of process output | |
81 ;;; | |
114 | 82 ;;; Not bound by default in comint-mode |
83 ;;; send-invisible Read a line w/o echo, and send to proc | |
84 ;;; (These are bound in shell-mode) | |
85 ;;; comint-dynamic-complete Complete filename at point. | |
86 ;;; comint-dynamic-list-completions List completions in help buffer. | |
87 ;;; comint-replace-by-expanded-filename Expand and complete filename at point; | |
88 ;;; replace with expanded/completed name. | |
584 | 89 ;;; comint-kill-subjob No mercy. |
90 ;;; comint-continue-subjob Send CONT signal to buffer's process | |
91 ;;; group. Useful if you accidentally | |
92 ;;; suspend your process (with C-c C-z). | |
93 ;;; | |
94 ;;; These used to be bound for RMS -- I prefer the input history stuff, | |
95 ;;; but you might like 'em. | |
96 ;;; m-P comint-msearch-input Search backwards for prompt | |
97 ;;; m-N comint-psearch-input Search forwards for prompt | |
98 ;;; C-cR comint-msearch-input-matching Search backwards for prompt & string | |
114 | 99 |
584 | 100 ;;; comint-mode-hook is the comint mode hook. Basically for your keybindings. |
101 ;;; comint-load-hook is run after loading in this package. | |
102 | |
807
4f28bd14272c
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
793
diff
changeset
|
103 ;;; Code: |
4f28bd14272c
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
793
diff
changeset
|
104 |
4f28bd14272c
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
793
diff
changeset
|
105 (defconst comint-version "2.03") |
4f28bd14272c
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
793
diff
changeset
|
106 |
1131 | 107 (require 'ring) |
114 | 108 |
109 ;;; Buffer Local Variables: | |
110 ;;;============================================================================ | |
111 ;;; Comint mode buffer local variables: | |
112 ;;; comint-prompt-regexp - string comint-bol uses to match prompt. | |
705 | 113 ;;; comint-last-input-start - marker Handy if inferior always echos |
584 | 114 ;;; comint-last-input-end - marker For comint-kill-output command |
861
345296f94a1e
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
846
diff
changeset
|
115 ;;; comint-input-ring-size - integer For the input history |
345296f94a1e
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
846
diff
changeset
|
116 ;;; comint-input-ring - ring mechanism |
345296f94a1e
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
846
diff
changeset
|
117 ;;; comint-input-ring-index - marker ... |
114 | 118 ;;; comint-last-input-match - string ... |
119 ;;; comint-get-old-input - function Hooks for specific | |
120 ;;; comint-input-sentinel - function process-in-a-buffer | |
121 ;;; comint-input-filter - function modes. | |
122 ;;; comint-input-send - function | |
123 ;;; comint-eol-on-send - boolean | |
2539
577c77cfd199
*** empty log message ***
Noah Friedman <friedman@splode.com>
parents:
2518
diff
changeset
|
124 ;;; comint-process-echoes - boolean |
114 | 125 |
584 | 126 (defvar comint-prompt-regexp "^" |
127 "Regexp to recognise prompts in the inferior process. | |
128 Defaults to \"^\", the null string at BOL. | |
114 | 129 |
130 Good choices: | |
584 | 131 Canonical Lisp: \"^[^> ]*>+:? *\" (Lucid, franz, kcl, T, cscheme, oaklisp) |
114 | 132 Lucid Common Lisp: \"^\\(>\\|\\(->\\)+\\) *\" |
584 | 133 franz: \"^\\(->\\|<[0-9]*>:\\) *\" |
134 kcl: \"^>+ *\" | |
135 shell: \"^[^#$%>]*[#$%>] *\" | |
136 T: \"^>+ *\" | |
114 | 137 |
584 | 138 This is a good thing to set in mode hooks.") |
114 | 139 |
861
345296f94a1e
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
846
diff
changeset
|
140 (defvar comint-input-ring-size 30 |
584 | 141 "Size of input history ring.") |
114 | 142 |
2539
577c77cfd199
*** empty log message ***
Noah Friedman <friedman@splode.com>
parents:
2518
diff
changeset
|
143 (defvar comint-process-echoes nil |
577c77cfd199
*** empty log message ***
Noah Friedman <friedman@splode.com>
parents:
2518
diff
changeset
|
144 "*If non-`nil', assume that the subprocess echoes any input. |
577c77cfd199
*** empty log message ***
Noah Friedman <friedman@splode.com>
parents:
2518
diff
changeset
|
145 If so, delete one copy of the input so that only one copy eventually |
577c77cfd199
*** empty log message ***
Noah Friedman <friedman@splode.com>
parents:
2518
diff
changeset
|
146 appears in the buffer. |
577c77cfd199
*** empty log message ***
Noah Friedman <friedman@splode.com>
parents:
2518
diff
changeset
|
147 |
577c77cfd199
*** empty log message ***
Noah Friedman <friedman@splode.com>
parents:
2518
diff
changeset
|
148 This variable is buffer-local.") |
577c77cfd199
*** empty log message ***
Noah Friedman <friedman@splode.com>
parents:
2518
diff
changeset
|
149 |
114 | 150 ;;; Here are the per-interpreter hooks. |
584 | 151 (defvar comint-get-old-input (function comint-get-old-input-default) |
152 "Function that submits old text in comint mode. | |
114 | 153 This function is called when return is typed while the point is in old text. |
154 It returns the text to be submitted as process input. The default is | |
155 comint-get-old-input-default, which grabs the current line, and strips off | |
584 | 156 leading text matching comint-prompt-regexp") |
114 | 157 |
584 | 158 (defvar comint-input-sentinel (function ignore) |
159 "Called on each input submitted to comint mode process by comint-send-input. | |
160 Thus it can, for instance, track cd/pushd/popd commands issued to the csh.") | |
114 | 161 |
584 | 162 (defvar comint-input-filter |
163 (function (lambda (str) (not (string-match "\\`\\s *\\'" str)))) | |
164 "Predicate for filtering additions to input history. | |
114 | 165 Only inputs answering true to this function are saved on the input |
584 | 166 history list. Default is to save anything that isn't all whitespace") |
167 | |
168 (defvar comint-input-sender (function comint-simple-send) | |
169 "Function to actually send to PROCESS the STRING submitted by user. | |
170 Usually this is just 'comint-simple-send, but if your mode needs to | |
171 massage the input string, this is your hook. This is called from | |
172 the user command comint-send-input. comint-simple-send just sends | |
173 the string plus a newline.") | |
174 | |
175 (defvar comint-eol-on-send 'T | |
176 "If non-nil, then jump to the end of the line before sending input to process. | |
177 See comint-send-input") | |
114 | 178 |
179 (defvar comint-mode-hook '() | |
584 | 180 "Called upon entry into comint-mode |
181 This is run before the process is cranked up.") | |
182 | |
183 (defvar comint-exec-hook '() | |
184 "Called each time a process is exec'd by comint-exec. | |
185 This is called after the process is cranked up. It is useful for things that | |
186 must be done each time a process is executed in a comint-mode buffer (e.g., | |
187 (process-kill-without-query)). In contrast, the comint-mode-hook is only | |
188 executed once when the buffer is created.") | |
189 | |
190 (defvar comint-mode-map nil) | |
114 | 191 |
1131 | 192 (defvar comint-ptyp t |
193 "True if communications via pty; false if by pipe. Buffer local. | |
194 This is to work around a bug in emacs process signalling.") | |
195 | |
1376
dd83ec6103fe
(comint-last-input-match): defvar moved up.
Richard M. Stallman <rms@gnu.org>
parents:
1373
diff
changeset
|
196 (defvar comint-last-input-match "" |
dd83ec6103fe
(comint-last-input-match): defvar moved up.
Richard M. Stallman <rms@gnu.org>
parents:
1373
diff
changeset
|
197 "Last string searched for by comint input history search, for defaulting. |
dd83ec6103fe
(comint-last-input-match): defvar moved up.
Richard M. Stallman <rms@gnu.org>
parents:
1373
diff
changeset
|
198 Buffer local variable.") |
dd83ec6103fe
(comint-last-input-match): defvar moved up.
Richard M. Stallman <rms@gnu.org>
parents:
1373
diff
changeset
|
199 |
932
a93f0b1ba909
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
925
diff
changeset
|
200 (defvar comint-input-ring nil) |
861
345296f94a1e
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
846
diff
changeset
|
201 (defvar comint-last-input-start) |
345296f94a1e
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
846
diff
changeset
|
202 (defvar comint-last-input-end) |
1131 | 203 (defvar comint-input-ring-index) |
204 (put 'comint-input-ring 'permanent-local t) | |
205 (put 'comint-ptyp 'permanent-local t) | |
861
345296f94a1e
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
846
diff
changeset
|
206 |
114 | 207 (defun comint-mode () |
208 "Major mode for interacting with an inferior interpreter. | |
209 Interpreter name is same as buffer name, sans the asterisks. | |
210 Return at end of buffer sends line as input. | |
211 Return not at end copies rest of line to end and sends it. | |
584 | 212 Setting mode variable comint-eol-on-send means jump to the end of the line |
213 before submitting new input. | |
114 | 214 |
215 This mode is typically customised to create inferior-lisp-mode, | |
584 | 216 shell-mode, etc.. This can be done by setting the hooks |
217 comint-input-sentinel, comint-input-filter, comint-input-sender and | |
218 comint-get-old-input to appropriate functions, and the variable | |
219 comint-prompt-regexp to the appropriate regular expression. | |
114 | 220 |
861
345296f94a1e
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
846
diff
changeset
|
221 An input history is maintained of size comint-input-ring-size, and |
114 | 222 can be accessed with the commands comint-next-input [\\[comint-next-input]] and |
223 comint-previous-input [\\[comint-previous-input]]. Commands not keybound by | |
224 default are send-invisible, comint-dynamic-complete, and | |
225 comint-list-dynamic-completions. | |
226 | |
227 If you accidentally suspend your process, use \\[comint-continue-subjob] | |
228 to continue it. | |
229 | |
230 \\{comint-mode-map} | |
231 | |
584 | 232 Entry to this mode runs the hooks on comint-mode-hook" |
114 | 233 (interactive) |
1131 | 234 ;; Do not remove this. All major modes must do this. |
235 (kill-all-local-variables) | |
584 | 236 (setq major-mode 'comint-mode) |
237 (setq mode-name "Comint") | |
238 (setq mode-line-process '(": %s")) | |
239 (use-local-map comint-mode-map) | |
705 | 240 (make-local-variable 'comint-last-input-start) |
241 (setq comint-last-input-start (make-marker)) | |
584 | 242 (make-local-variable 'comint-last-input-end) |
243 (setq comint-last-input-end (make-marker)) | |
244 (make-local-variable 'comint-last-input-match) | |
245 (setq comint-last-input-match "") | |
246 (make-local-variable 'comint-prompt-regexp) ; Don't set; default | |
861
345296f94a1e
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
846
diff
changeset
|
247 (make-local-variable 'comint-input-ring-size) ; ...to global val. |
345296f94a1e
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
846
diff
changeset
|
248 (make-local-variable 'comint-input-ring) |
345296f94a1e
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
846
diff
changeset
|
249 (make-local-variable 'comint-input-ring-index) |
345296f94a1e
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
846
diff
changeset
|
250 (setq comint-input-ring-index 0) |
584 | 251 (make-local-variable 'comint-get-old-input) |
252 (make-local-variable 'comint-input-sentinel) | |
253 (make-local-variable 'comint-input-filter) | |
254 (make-local-variable 'comint-input-sender) | |
255 (make-local-variable 'comint-eol-on-send) | |
256 (make-local-variable 'comint-ptyp) | |
705 | 257 (make-local-variable 'comint-exec-hook) |
2539
577c77cfd199
*** empty log message ***
Noah Friedman <friedman@splode.com>
parents:
2518
diff
changeset
|
258 (make-local-variable 'comint-process-echoes) |
584 | 259 (run-hooks 'comint-mode-hook) |
1131 | 260 (or comint-input-ring |
261 (setq comint-input-ring (make-ring comint-input-ring-size)))) | |
114 | 262 |
584 | 263 (if comint-mode-map |
264 nil | |
265 (setq comint-mode-map (make-sparse-keymap)) | |
266 (define-key comint-mode-map "\ep" 'comint-previous-input) | |
267 (define-key comint-mode-map "\en" 'comint-next-input) | |
909
4c6cdb66c74c
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
870
diff
changeset
|
268 (define-key comint-mode-map "\er" 'comint-previous-similar-input) |
4c6cdb66c74c
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
870
diff
changeset
|
269 (define-key comint-mode-map "\es" 'comint-next-similar-input) |
584 | 270 (define-key comint-mode-map "\C-m" 'comint-send-input) |
271 (define-key comint-mode-map "\C-d" 'comint-delchar-or-maybe-eof) | |
272 (define-key comint-mode-map "\C-a" 'comint-bol) | |
273 (define-key comint-mode-map "\C-c\C-u" 'comint-kill-input) | |
274 (define-key comint-mode-map "\C-c\C-w" 'backward-kill-word) | |
275 (define-key comint-mode-map "\C-c\C-c" 'comint-interrupt-subjob) | |
276 (define-key comint-mode-map "\C-c\C-z" 'comint-stop-subjob) | |
277 (define-key comint-mode-map "\C-c\C-\\" 'comint-quit-subjob) | |
278 (define-key comint-mode-map "\C-c\C-o" 'comint-kill-output) | |
279 (define-key comint-mode-map "\C-\M-r" 'comint-previous-input-matching) | |
280 (define-key comint-mode-map "\C-c\C-r" 'comint-show-output) | |
281 ;;; prompt-search commands commented out 3/90 -Olin | |
282 ; (define-key comint-mode-map "\eP" 'comint-msearch-input) | |
283 ; (define-key comint-mode-map "\eN" 'comint-psearch-input) | |
284 ; (define-key comint-mode-map "\C-cR" 'comint-msearch-input-matching) | |
285 ) | |
286 | |
287 | |
288 ;;; This function is used to make a full copy of the comint mode map, | |
289 ;;; so that client modes won't interfere with each other. This function | |
290 ;;; isn't necessary in emacs 18.5x, but we keep it around for 18.4x versions. | |
291 (defun full-copy-sparse-keymap (km) | |
292 "Recursively copy the sparse keymap KM" | |
293 (cond ((consp km) | |
294 (cons (full-copy-sparse-keymap (car km)) | |
295 (full-copy-sparse-keymap (cdr km)))) | |
296 (t km))) | |
297 | |
298 (defun comint-check-proc (buffer) | |
299 "True if there is a process associated w/buffer BUFFER, and | |
300 it is alive (status RUN or STOP). BUFFER can be either a buffer or the | |
301 name of one" | |
302 (let ((proc (get-buffer-process buffer))) | |
114 | 303 (and proc (memq (process-status proc) '(run stop))))) |
304 | |
305 ;;; Note that this guy, unlike shell.el's make-shell, barfs if you pass it () | |
306 ;;; for the second argument (program). | |
870
1b3af6ad85be
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
864
diff
changeset
|
307 ;;;###autoload |
114 | 308 (defun make-comint (name program &optional startfile &rest switches) |
1839
e10cdcb857d2
(make-comint): Added docstring.
Roland McGrath <roland@gnu.org>
parents:
1821
diff
changeset
|
309 "Make a comint process NAME in a buffer, running PROGRAM. |
e10cdcb857d2
(make-comint): Added docstring.
Roland McGrath <roland@gnu.org>
parents:
1821
diff
changeset
|
310 The name of the buffer is made by surrounding NAME with `*'s. |
e10cdcb857d2
(make-comint): Added docstring.
Roland McGrath <roland@gnu.org>
parents:
1821
diff
changeset
|
311 If there is already a running process in that buffer, it is not restarted. |
e10cdcb857d2
(make-comint): Added docstring.
Roland McGrath <roland@gnu.org>
parents:
1821
diff
changeset
|
312 Optional third arg STARTFILE is the name of a file to send the contents of to |
e10cdcb857d2
(make-comint): Added docstring.
Roland McGrath <roland@gnu.org>
parents:
1821
diff
changeset
|
313 the process. Any more args are arguments to PROGRAM." |
584 | 314 (let ((buffer (get-buffer-create (concat "*" name "*")))) |
114 | 315 ;; If no process, or nuked process, crank up a new one and put buffer in |
1839
e10cdcb857d2
(make-comint): Added docstring.
Roland McGrath <roland@gnu.org>
parents:
1821
diff
changeset
|
316 ;; comint mode. Otherwise, leave buffer and existing process alone. |
182 | 317 (cond ((not (comint-check-proc buffer)) |
584 | 318 (save-excursion |
114 | 319 (set-buffer buffer) |
320 (comint-mode)) ; Install local vars, mode, keymap, ... | |
321 (comint-exec buffer name program startfile switches))) | |
322 buffer)) | |
323 | |
324 (defun comint-exec (buffer name command startfile switches) | |
325 "Fires up a process in buffer for comint modes. | |
1839
e10cdcb857d2
(make-comint): Added docstring.
Roland McGrath <roland@gnu.org>
parents:
1821
diff
changeset
|
326 Blasts any old process running in the buffer. Doesn't set the buffer mode. |
584 | 327 You can use this to cheaply run a series of processes in the same comint |
1839
e10cdcb857d2
(make-comint): Added docstring.
Roland McGrath <roland@gnu.org>
parents:
1821
diff
changeset
|
328 buffer. The hook comint-exec-hook is run after each exec." |
114 | 329 (save-excursion |
330 (set-buffer buffer) | |
331 (let ((proc (get-buffer-process buffer))) ; Blast any old process. | |
332 (if proc (delete-process proc))) | |
333 ;; Crank up a new process | |
334 (let ((proc (comint-exec-1 name buffer command switches))) | |
584 | 335 (make-local-variable 'comint-ptyp) |
336 (setq comint-ptyp process-connection-type) ; T if pty, NIL if pipe. | |
114 | 337 ;; Jump to the end, and set the process mark. |
584 | 338 (goto-char (point-max)) |
705 | 339 (set-marker (process-mark proc) (point)) |
114 | 340 ;; Feed it the startfile. |
341 (cond (startfile | |
342 ;;This is guaranteed to wait long enough | |
343 ;;but has bad results if the comint does not prompt at all | |
344 ;; (while (= size (buffer-size)) | |
345 ;; (sleep-for 1)) | |
346 ;;I hope 1 second is enough! | |
347 (sleep-for 1) | |
348 (goto-char (point-max)) | |
349 (insert-file-contents startfile) | |
350 (setq startfile (buffer-substring (point) (point-max))) | |
351 (delete-region (point) (point-max)) | |
584 | 352 (comint-send-string proc startfile))) |
353 (run-hooks 'comint-exec-hook) | |
705 | 354 buffer))) |
114 | 355 |
356 ;;; This auxiliary function cranks up the process for comint-exec in | |
638 | 357 ;;; the appropriate environment. |
114 | 358 |
359 (defun comint-exec-1 (name buffer command switches) | |
638 | 360 (let ((process-environment |
361 (comint-update-env process-environment | |
362 (list (format "TERMCAP=emacs:co#%d:tc=unknown" | |
778 | 363 (frame-width)) |
638 | 364 "TERM=emacs" |
365 "EMACS=t")))) | |
366 (apply 'start-process name buffer command switches))) | |
584 | 367 |
368 | |
114 | 369 |
370 ;; This is just (append new old-env) that compresses out shadowed entries. | |
793
6fb68a1460a6
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
778
diff
changeset
|
371 ;; It's also pretty ugly, mostly due to lisp's horrible iteration structures. |
114 | 372 (defun comint-update-env (old-env new) |
373 (let ((ans (reverse new)) | |
374 (vars (mapcar (function (lambda (vv) | |
375 (and (string-match "^[^=]*=" vv) | |
376 (substring vv 0 (match-end 0))))) | |
377 new))) | |
378 (while old-env | |
379 (let* ((vv (car old-env)) ; vv is var=value | |
380 (var (and (string-match "^[^=]*=" vv) | |
381 (substring vv 0 (match-end 0))))) | |
382 (setq old-env (cdr old-env)) | |
584 | 383 (cond ((not (and var (comint-mem var vars))) |
114 | 384 (if var (setq var (cons var vars))) |
385 (setq ans (cons vv ans)))))) | |
386 (nreverse ans))) | |
584 | 387 |
388 ;;; This should be in emacs, but it isn't. | |
389 (defun comint-mem (item list &optional elt=) | |
390 "Test to see if ITEM is equal to an item in LIST. | |
391 Option comparison function ELT= defaults to equal." | |
392 (let ((elt= (or elt= (function equal))) | |
393 (done nil)) | |
394 (while (and list (not done)) | |
395 (if (funcall elt= item (car list)) | |
396 (setq done list) | |
397 (setq list (cdr list)))) | |
398 done)) | |
399 | |
114 | 400 |
401 ;;; Input history retrieval commands | |
402 ;;; M-p -- previous input M-n -- next input | |
584 | 403 ;;; M-C-r -- previous input matching |
114 | 404 ;;; =========================================================================== |
405 | |
406 (defun comint-previous-input (arg) | |
407 "Cycle backwards through input history." | |
408 (interactive "*p") | |
861
345296f94a1e
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
846
diff
changeset
|
409 (let ((len (ring-length comint-input-ring))) |
584 | 410 (cond ((<= len 0) |
411 (message "Empty input ring") | |
412 (ding)) | |
413 ((not (comint-after-pmark-p)) | |
414 (message "Not after process mark") | |
415 (ding)) | |
416 (t | |
909
4c6cdb66c74c
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
870
diff
changeset
|
417 (delete-region (point) |
4c6cdb66c74c
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
870
diff
changeset
|
418 (process-mark (get-buffer-process (current-buffer)))) |
4c6cdb66c74c
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
870
diff
changeset
|
419 ;; Initialize the index on the first use of this command |
4c6cdb66c74c
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
870
diff
changeset
|
420 ;; so that the first M-p gets index 0, and the first M-n gets |
4c6cdb66c74c
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
870
diff
changeset
|
421 ;; index -1. |
4c6cdb66c74c
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
870
diff
changeset
|
422 (if (null comint-input-ring-index) |
4c6cdb66c74c
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
870
diff
changeset
|
423 (setq comint-input-ring-index |
4c6cdb66c74c
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
870
diff
changeset
|
424 (if (> arg 0) -1 |
4c6cdb66c74c
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
870
diff
changeset
|
425 (if (< arg 0) 1 0)))) |
4c6cdb66c74c
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
870
diff
changeset
|
426 (setq comint-input-ring-index |
4c6cdb66c74c
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
870
diff
changeset
|
427 (comint-mod (+ comint-input-ring-index arg) len)) |
861
345296f94a1e
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
846
diff
changeset
|
428 (message "%d" (1+ comint-input-ring-index)) |
909
4c6cdb66c74c
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
870
diff
changeset
|
429 (insert (ring-ref comint-input-ring comint-input-ring-index)))))) |
4c6cdb66c74c
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
870
diff
changeset
|
430 |
4c6cdb66c74c
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
870
diff
changeset
|
431 (defun comint-mod (n m) |
4c6cdb66c74c
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
870
diff
changeset
|
432 "Returns N mod M. M is positive. |
4c6cdb66c74c
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
870
diff
changeset
|
433 Answer is guaranteed to be non-negative, and less than m." |
4c6cdb66c74c
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
870
diff
changeset
|
434 (let ((n (% n m))) |
4c6cdb66c74c
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
870
diff
changeset
|
435 (if (>= n 0) n |
4c6cdb66c74c
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
870
diff
changeset
|
436 (+ n |
4c6cdb66c74c
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
870
diff
changeset
|
437 (if (>= m 0) m (- m)))))) ; (abs m) |
4c6cdb66c74c
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
870
diff
changeset
|
438 |
114 | 439 (defun comint-next-input (arg) |
440 "Cycle forwards through input history." | |
441 (interactive "*p") | |
442 (comint-previous-input (- arg))) | |
443 | |
444 (defun comint-previous-input-matching (str) | |
445 "Searches backwards through input history for substring match." | |
446 (interactive (let* ((last-command last-command) ; preserve around r-f-m | |
447 (s (read-from-minibuffer | |
864
fe5f6b7c9727
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
861
diff
changeset
|
448 (format "Command substring (default %s): " |
fe5f6b7c9727
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
861
diff
changeset
|
449 comint-last-input-match)))) |
114 | 450 (list (if (string= s "") comint-last-input-match s)))) |
451 ; (interactive "sCommand substring: ") | |
452 (setq comint-last-input-match str) ; update default | |
909
4c6cdb66c74c
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
870
diff
changeset
|
453 (if (null comint-input-ring-index) |
861
345296f94a1e
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
846
diff
changeset
|
454 (setq comint-input-ring-index -1)) |
114 | 455 (let ((str (regexp-quote str)) |
861
345296f94a1e
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
846
diff
changeset
|
456 (len (ring-length comint-input-ring)) |
345296f94a1e
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
846
diff
changeset
|
457 (n (+ comint-input-ring-index 1))) |
345296f94a1e
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
846
diff
changeset
|
458 (while (and (< n len) (not (string-match str (ring-ref comint-input-ring n)))) |
114 | 459 (setq n (+ n 1))) |
460 (cond ((< n len) | |
861
345296f94a1e
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
846
diff
changeset
|
461 (comint-previous-input (- n comint-input-ring-index))) |
909
4c6cdb66c74c
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
870
diff
changeset
|
462 (t (error "Not found"))))) |
584 | 463 |
464 | |
465 ;;; These next three commands are alternatives to the input history commands | |
466 ;;; -- comint-next-input, comint-previous-input and | |
467 ;;; comint-previous-input-matching. They search through the process buffer | |
468 ;;; text looking for occurrences of the prompt. Bound to M-P, M-N, and C-c R | |
469 ;;; (uppercase P, N, and R) for now. Try'em out. Go with what you like... | |
470 | |
471 ;;; comint-msearch-input-matching prompts for a string, not a regexp. | |
472 ;;; This could be considered to be the wrong thing. I decided to keep it | |
473 ;;; simple, and not make the user worry about regexps. This, of course, | |
474 ;;; limits functionality. | |
475 | |
476 ;;; These commands were deemed non-winning and have been commented out. | |
477 ;;; Feel free to re-enable them if you like. -Olin 3/91 | |
478 | |
479 ;(defun comint-psearch-input () | |
480 ; "Search forwards for next occurrence of prompt and skip to end of line. | |
481 ;\(prompt is anything matching regexp comint-prompt-regexp)" | |
482 ; (interactive) | |
483 ; (if (re-search-forward comint-prompt-regexp (point-max) t) | |
484 ; (end-of-line) | |
485 ; (error "No occurrence of prompt found"))) | |
486 ; | |
487 ;(defun comint-msearch-input () | |
488 ; "Search backwards for previous occurrence of prompt and skip to end of line. | |
489 ;Search starts from beginning of current line." | |
490 ; (interactive) | |
491 ; (let ((p (save-excursion | |
492 ; (beginning-of-line) | |
493 ; (cond ((re-search-backward comint-prompt-regexp (point-min) t) | |
494 ; (end-of-line) | |
495 ; (point)) | |
496 ; (t nil))))) | |
497 ; (if p (goto-char p) | |
498 ; (error "No occurrence of prompt found")))) | |
499 ; | |
500 ;(defun comint-msearch-input-matching (str) | |
501 ; "Search backwards for occurrence of prompt followed by STRING. | |
502 ;STRING is prompted for, and is NOT a regular expression." | |
503 ; (interactive (let ((s (read-from-minibuffer | |
504 ; (format "Command (default %s): " | |
505 ; comint-last-input-match)))) | |
506 ; (list (if (string= s "") comint-last-input-match s)))) | |
507 ;; (interactive "sCommand: ") | |
508 ; (setq comint-last-input-match str) ; update default | |
509 ; (let* ((r (concat comint-prompt-regexp (regexp-quote str))) | |
510 ; (p (save-excursion | |
511 ; (beginning-of-line) | |
512 ; (cond ((re-search-backward r (point-min) t) | |
513 ; (end-of-line) | |
514 ; (point)) | |
515 ; (t nil))))) | |
516 ; (if p (goto-char p) | |
517 ; (error "No match")))) | |
114 | 518 |
519 ;;; | |
520 ;;; Similar input -- contributed by ccm and highly winning. | |
521 ;;; | |
522 ;;; Reenter input, removing back to the last insert point if it exists. | |
523 ;;; | |
584 | 524 (defvar comint-last-similar-string "" |
525 "The string last used in a similar string search.") | |
114 | 526 (defun comint-previous-similar-input (arg) |
909
4c6cdb66c74c
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
870
diff
changeset
|
527 "Fetch the previous (older) input that matches the string typed so far. |
4c6cdb66c74c
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
870
diff
changeset
|
528 Successive repetitions find successively older matching inputs. |
4c6cdb66c74c
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
870
diff
changeset
|
529 A prefix argument serves as a repeat count; a negative argument |
4c6cdb66c74c
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
870
diff
changeset
|
530 fetches following (more recent) inputs." |
114 | 531 (interactive "p") |
584 | 532 (if (not (comint-after-pmark-p)) |
114 | 533 (error "Not after process mark")) |
909
4c6cdb66c74c
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
870
diff
changeset
|
534 (if (null comint-input-ring-index) |
4c6cdb66c74c
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
870
diff
changeset
|
535 (setq comint-input-ring-index |
4c6cdb66c74c
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
870
diff
changeset
|
536 (if (> arg 0) -1 |
4c6cdb66c74c
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
870
diff
changeset
|
537 (if (< arg 0) 1 0)))) |
4c6cdb66c74c
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
870
diff
changeset
|
538 (if (not (or (eq last-command 'comint-previous-similar-input) |
4c6cdb66c74c
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
870
diff
changeset
|
539 (eq last-command 'comint-next-similar-input))) |
4c6cdb66c74c
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
870
diff
changeset
|
540 (setq comint-last-similar-string |
584 | 541 (buffer-substring |
542 (process-mark (get-buffer-process (current-buffer))) | |
543 (point)))) | |
114 | 544 (let* ((size (length comint-last-similar-string)) |
861
345296f94a1e
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
846
diff
changeset
|
545 (len (ring-length comint-input-ring)) |
345296f94a1e
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
846
diff
changeset
|
546 (n (+ comint-input-ring-index arg)) |
114 | 547 entry) |
548 (while (and (< n len) | |
861
345296f94a1e
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
846
diff
changeset
|
549 (or (< (length (setq entry (ring-ref comint-input-ring n))) size) |
114 | 550 (not (equal comint-last-similar-string |
551 (substring entry 0 size))))) | |
552 (setq n (+ n arg))) | |
553 (cond ((< n len) | |
861
345296f94a1e
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
846
diff
changeset
|
554 (setq comint-input-ring-index n) |
909
4c6cdb66c74c
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
870
diff
changeset
|
555 (if (or (eq last-command 'comint-previous-similar-input) |
4c6cdb66c74c
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
870
diff
changeset
|
556 (eq last-command 'comint-next-similar-input)) |
584 | 557 (delete-region (mark) (point)) ; repeat |
558 (push-mark (point))) ; 1st time | |
114 | 559 (insert (substring entry size))) |
909
4c6cdb66c74c
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
870
diff
changeset
|
560 (t (error "Not found"))) |
861
345296f94a1e
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
846
diff
changeset
|
561 (message "%d" (1+ comint-input-ring-index)))) |
584 | 562 |
909
4c6cdb66c74c
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
870
diff
changeset
|
563 (defun comint-next-similar-input (arg) |
4c6cdb66c74c
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
870
diff
changeset
|
564 "Fetch the next (newer) input that matches the string typed so far. |
4c6cdb66c74c
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
870
diff
changeset
|
565 Successive repetitions find successively newer matching inputs. |
4c6cdb66c74c
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
870
diff
changeset
|
566 A prefix argument serves as a repeat count; a negative argument |
4c6cdb66c74c
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
870
diff
changeset
|
567 fetches previous (older) inputs." |
4c6cdb66c74c
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
870
diff
changeset
|
568 (interactive "p") |
4c6cdb66c74c
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
870
diff
changeset
|
569 (comint-previous-similar-input (- arg))) |
114 | 570 |
584 | 571 (defun comint-send-input () |
1356 | 572 "Send input to process. |
573 After the process output mark, sends all text from the process mark to | |
574 point as input to the process. Before the process output mark, calls value | |
2539
577c77cfd199
*** empty log message ***
Noah Friedman <friedman@splode.com>
parents:
2518
diff
changeset
|
575 of variable `comint-get-old-input' to retrieve old input, copies it to the |
577c77cfd199
*** empty log message ***
Noah Friedman <friedman@splode.com>
parents:
2518
diff
changeset
|
576 process mark, and sends it. If variable `comint-process-echoes' is `nil', |
577c77cfd199
*** empty log message ***
Noah Friedman <friedman@splode.com>
parents:
2518
diff
changeset
|
577 a terminal newline is also inserted into the buffer and sent to the process |
577c77cfd199
*** empty log message ***
Noah Friedman <friedman@splode.com>
parents:
2518
diff
changeset
|
578 (if it is non-`nil', all text from the process mark to point is deleted, |
577c77cfd199
*** empty log message ***
Noah Friedman <friedman@splode.com>
parents:
2518
diff
changeset
|
579 since it is assumed the remote process will re-echo it). The value of |
577c77cfd199
*** empty log message ***
Noah Friedman <friedman@splode.com>
parents:
2518
diff
changeset
|
580 variable `comint-input-sentinel' is called on the input before sending it. |
577c77cfd199
*** empty log message ***
Noah Friedman <friedman@splode.com>
parents:
2518
diff
changeset
|
581 The input is entered into the input history ring, if the value of variable |
577c77cfd199
*** empty log message ***
Noah Friedman <friedman@splode.com>
parents:
2518
diff
changeset
|
582 `comint-input-filter' returns non-`nil' when called on the input. |
114 | 583 |
2539
577c77cfd199
*** empty log message ***
Noah Friedman <friedman@splode.com>
parents:
2518
diff
changeset
|
584 If variable `comint-eol-on-send' is non-`nil', then point is moved to the |
577c77cfd199
*** empty log message ***
Noah Friedman <friedman@splode.com>
parents:
2518
diff
changeset
|
585 end of line before sending the input. |
114 | 586 |
2539
577c77cfd199
*** empty log message ***
Noah Friedman <friedman@splode.com>
parents:
2518
diff
changeset
|
587 `comint-get-old-input', `comint-input-sentinel', and `comint-input-filter' |
577c77cfd199
*** empty log message ***
Noah Friedman <friedman@splode.com>
parents:
2518
diff
changeset
|
588 are chosen according to the command interpreter running in the buffer. E.g., |
114 | 589 If the interpreter is the csh, |
584 | 590 comint-get-old-input is the default: take the current line, discard any |
591 initial string matching regexp comint-prompt-regexp. | |
592 comint-input-sentinel monitors input for \"cd\", \"pushd\", and \"popd\" | |
593 commands. When it sees one, it cd's the buffer. | |
594 comint-input-filter is the default: returns T if the input isn't all white | |
595 space. | |
114 | 596 |
597 If the comint is Lucid Common Lisp, | |
584 | 598 comint-get-old-input snarfs the sexp ending at point. |
599 comint-input-sentinel does nothing. | |
600 comint-input-filter returns NIL if the input matches input-filter-regexp, | |
114 | 601 which matches (1) all whitespace (2) :a, :c, etc. |
602 | |
909
4c6cdb66c74c
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
870
diff
changeset
|
603 Similarly for Soar, Scheme, etc." |
114 | 604 (interactive) |
605 ;; Note that the input string does not include its terminal newline. | |
584 | 606 (let ((proc (get-buffer-process (current-buffer)))) |
607 (if (not proc) (error "Current buffer has no process") | |
608 (let* ((pmark (process-mark proc)) | |
609 (pmark-val (marker-position pmark)) | |
610 (input (if (>= (point) pmark-val) | |
611 (progn (if comint-eol-on-send (end-of-line)) | |
612 (buffer-substring pmark (point))) | |
705 | 613 (let ((copy (funcall comint-get-old-input))) |
614 (goto-char pmark) | |
615 (insert copy) | |
616 copy)))) | |
2539
577c77cfd199
*** empty log message ***
Noah Friedman <friedman@splode.com>
parents:
2518
diff
changeset
|
617 (if comint-process-echoes |
577c77cfd199
*** empty log message ***
Noah Friedman <friedman@splode.com>
parents:
2518
diff
changeset
|
618 (delete-region pmark (point)) |
577c77cfd199
*** empty log message ***
Noah Friedman <friedman@splode.com>
parents:
2518
diff
changeset
|
619 (insert ?\n)) |
861
345296f94a1e
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
846
diff
changeset
|
620 (if (funcall comint-input-filter input) |
345296f94a1e
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
846
diff
changeset
|
621 (ring-insert comint-input-ring input)) |
584 | 622 (funcall comint-input-sentinel input) |
623 (funcall comint-input-sender proc input) | |
909
4c6cdb66c74c
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
870
diff
changeset
|
624 (setq comint-input-ring-index nil) |
705 | 625 (set-marker comint-last-input-start pmark) |
626 (set-marker comint-last-input-end (point)) | |
627 (set-marker (process-mark proc) (point)))))) | |
114 | 628 |
629 (defun comint-get-old-input-default () | |
1356 | 630 "Default for comint-get-old-input. |
631 Take the current line, and discard any initial text matching | |
632 comint-prompt-regexp." | |
114 | 633 (save-excursion |
584 | 634 (beginning-of-line) |
635 (comint-skip-prompt) | |
636 (let ((beg (point))) | |
637 (end-of-line) | |
638 (buffer-substring beg (point))))) | |
639 | |
640 (defun comint-skip-prompt () | |
641 "Skip past the text matching regexp comint-prompt-regexp. | |
642 If this takes us past the end of the current line, don't skip at all." | |
643 (let ((eol (save-excursion (end-of-line) (point)))) | |
644 (if (and (looking-at comint-prompt-regexp) | |
645 (<= (match-end 0) eol)) | |
646 (goto-char (match-end 0))))) | |
647 | |
648 | |
649 (defun comint-after-pmark-p () | |
650 "Is point after the process output marker?" | |
651 ;; Since output could come into the buffer after we looked at the point | |
652 ;; but before we looked at the process marker's value, we explicitly | |
653 ;; serialise. This is just because I don't know whether or not emacs | |
654 ;; services input during execution of lisp commands. | |
655 (let ((proc-pos (marker-position | |
656 (process-mark (get-buffer-process (current-buffer)))))) | |
657 (<= proc-pos (point)))) | |
658 | |
659 (defun comint-simple-send (proc string) | |
660 "Default function for sending to PROC input STRING. | |
661 This just sends STRING plus a newline. To override this, | |
662 set the hook COMINT-INPUT-SENDER." | |
663 (comint-send-string proc string) | |
664 (comint-send-string proc "\n")) | |
114 | 665 |
666 (defun comint-bol (arg) | |
667 "Goes to the beginning of line, then skips past the prompt, if any. | |
584 | 668 If a prefix argument is given (\\[universal-argument]), then no prompt skip |
669 -- go straight to column 0. | |
114 | 670 |
584 | 671 The prompt skip is done by skipping text matching the regular expression |
672 comint-prompt-regexp, a buffer local variable. | |
673 | |
674 If you don't like this command, reset c-a to beginning-of-line | |
675 in your hook, comint-mode-hook." | |
114 | 676 (interactive "P") |
677 (beginning-of-line) | |
584 | 678 (if (null arg) (comint-skip-prompt))) |
114 | 679 |
680 ;;; These two functions are for entering text you don't want echoed or | |
681 ;;; saved -- typically passwords to ftp, telnet, or somesuch. | |
584 | 682 ;;; Just enter m-x send-invisible and type in your line. |
683 | |
705 | 684 (defun comint-read-noecho (prompt &optional stars) |
2455
27d28402ee59
new comint-read-noecho.
Noah Friedman <friedman@splode.com>
parents:
2307
diff
changeset
|
685 "Read a single line of text from user without echoing, and return it. |
27d28402ee59
new comint-read-noecho.
Noah Friedman <friedman@splode.com>
parents:
2307
diff
changeset
|
686 Prompt with argument PROMPT, a string. Optional argument STARS causes |
27d28402ee59
new comint-read-noecho.
Noah Friedman <friedman@splode.com>
parents:
2307
diff
changeset
|
687 input to be echoed with '*' characters on the prompt line. Input ends with |
27d28402ee59
new comint-read-noecho.
Noah Friedman <friedman@splode.com>
parents:
2307
diff
changeset
|
688 RET, LFD, or ESC. DEL or C-h rubs out. C-u kills line. C-g aborts (if |
27d28402ee59
new comint-read-noecho.
Noah Friedman <friedman@splode.com>
parents:
2307
diff
changeset
|
689 inhibit-quit is set because e.g. this function was called from a process |
27d28402ee59
new comint-read-noecho.
Noah Friedman <friedman@splode.com>
parents:
2307
diff
changeset
|
690 filter and C-g is pressed, this function will return `nil', rather than a |
27d28402ee59
new comint-read-noecho.
Noah Friedman <friedman@splode.com>
parents:
2307
diff
changeset
|
691 string). |
27d28402ee59
new comint-read-noecho.
Noah Friedman <friedman@splode.com>
parents:
2307
diff
changeset
|
692 |
27d28402ee59
new comint-read-noecho.
Noah Friedman <friedman@splode.com>
parents:
2307
diff
changeset
|
693 Note that the keystrokes comprising the text can still be recovered |
27d28402ee59
new comint-read-noecho.
Noah Friedman <friedman@splode.com>
parents:
2307
diff
changeset
|
694 (temporarily) with \\[view-lossage]. This may be a security bug for some |
27d28402ee59
new comint-read-noecho.
Noah Friedman <friedman@splode.com>
parents:
2307
diff
changeset
|
695 applications." |
27d28402ee59
new comint-read-noecho.
Noah Friedman <friedman@splode.com>
parents:
2307
diff
changeset
|
696 (let ((ans "") |
27d28402ee59
new comint-read-noecho.
Noah Friedman <friedman@splode.com>
parents:
2307
diff
changeset
|
697 (c 0) |
27d28402ee59
new comint-read-noecho.
Noah Friedman <friedman@splode.com>
parents:
2307
diff
changeset
|
698 (echo-keystrokes 0) |
705 | 699 (cursor-in-echo-area t) |
2455
27d28402ee59
new comint-read-noecho.
Noah Friedman <friedman@splode.com>
parents:
2307
diff
changeset
|
700 (done nil)) |
27d28402ee59
new comint-read-noecho.
Noah Friedman <friedman@splode.com>
parents:
2307
diff
changeset
|
701 (while (not done) |
27d28402ee59
new comint-read-noecho.
Noah Friedman <friedman@splode.com>
parents:
2307
diff
changeset
|
702 (if stars |
27d28402ee59
new comint-read-noecho.
Noah Friedman <friedman@splode.com>
parents:
2307
diff
changeset
|
703 (message "%s%s" prompt (make-string (length ans) ?*)) |
27d28402ee59
new comint-read-noecho.
Noah Friedman <friedman@splode.com>
parents:
2307
diff
changeset
|
704 (message prompt)) |
27d28402ee59
new comint-read-noecho.
Noah Friedman <friedman@splode.com>
parents:
2307
diff
changeset
|
705 (setq c (read-char)) |
27d28402ee59
new comint-read-noecho.
Noah Friedman <friedman@splode.com>
parents:
2307
diff
changeset
|
706 (cond ((= c ?\C-g) |
27d28402ee59
new comint-read-noecho.
Noah Friedman <friedman@splode.com>
parents:
2307
diff
changeset
|
707 ;; This function may get called from a process filter, where |
27d28402ee59
new comint-read-noecho.
Noah Friedman <friedman@splode.com>
parents:
2307
diff
changeset
|
708 ;; inhibit-quit is set. In later versions of emacs read-char |
27d28402ee59
new comint-read-noecho.
Noah Friedman <friedman@splode.com>
parents:
2307
diff
changeset
|
709 ;; may clear quit-flag itself and return C-g. That would make |
27d28402ee59
new comint-read-noecho.
Noah Friedman <friedman@splode.com>
parents:
2307
diff
changeset
|
710 ;; it impossible to quit this loop in a simple way, so |
27d28402ee59
new comint-read-noecho.
Noah Friedman <friedman@splode.com>
parents:
2307
diff
changeset
|
711 ;; re-enable it here (for backward-compatibility the check for |
27d28402ee59
new comint-read-noecho.
Noah Friedman <friedman@splode.com>
parents:
2307
diff
changeset
|
712 ;; quit-flag below would still be necessary, so this is seems |
27d28402ee59
new comint-read-noecho.
Noah Friedman <friedman@splode.com>
parents:
2307
diff
changeset
|
713 ;; like the simplest way to do things). |
27d28402ee59
new comint-read-noecho.
Noah Friedman <friedman@splode.com>
parents:
2307
diff
changeset
|
714 (setq quit-flag t |
27d28402ee59
new comint-read-noecho.
Noah Friedman <friedman@splode.com>
parents:
2307
diff
changeset
|
715 done t)) |
27d28402ee59
new comint-read-noecho.
Noah Friedman <friedman@splode.com>
parents:
2307
diff
changeset
|
716 ((or (= c ?\r) (= c ?\n) (= c ?\e)) |
27d28402ee59
new comint-read-noecho.
Noah Friedman <friedman@splode.com>
parents:
2307
diff
changeset
|
717 (setq done t)) |
27d28402ee59
new comint-read-noecho.
Noah Friedman <friedman@splode.com>
parents:
2307
diff
changeset
|
718 ((= c ?\C-u) |
27d28402ee59
new comint-read-noecho.
Noah Friedman <friedman@splode.com>
parents:
2307
diff
changeset
|
719 (setq ans "")) |
27d28402ee59
new comint-read-noecho.
Noah Friedman <friedman@splode.com>
parents:
2307
diff
changeset
|
720 ((and (/= c ?\b) (/= c ?\177)) |
27d28402ee59
new comint-read-noecho.
Noah Friedman <friedman@splode.com>
parents:
2307
diff
changeset
|
721 (setq ans (concat ans (char-to-string c)))) |
27d28402ee59
new comint-read-noecho.
Noah Friedman <friedman@splode.com>
parents:
2307
diff
changeset
|
722 ((> (length ans) 0) |
27d28402ee59
new comint-read-noecho.
Noah Friedman <friedman@splode.com>
parents:
2307
diff
changeset
|
723 (setq ans (substring ans 0 -1))))) |
27d28402ee59
new comint-read-noecho.
Noah Friedman <friedman@splode.com>
parents:
2307
diff
changeset
|
724 (if quit-flag |
27d28402ee59
new comint-read-noecho.
Noah Friedman <friedman@splode.com>
parents:
2307
diff
changeset
|
725 ;; Emulate a true quit, except that we have to return a value. |
27d28402ee59
new comint-read-noecho.
Noah Friedman <friedman@splode.com>
parents:
2307
diff
changeset
|
726 (prog1 |
27d28402ee59
new comint-read-noecho.
Noah Friedman <friedman@splode.com>
parents:
2307
diff
changeset
|
727 (setq quit-flag nil) |
27d28402ee59
new comint-read-noecho.
Noah Friedman <friedman@splode.com>
parents:
2307
diff
changeset
|
728 (message "Quit") |
27d28402ee59
new comint-read-noecho.
Noah Friedman <friedman@splode.com>
parents:
2307
diff
changeset
|
729 (beep t)) |
27d28402ee59
new comint-read-noecho.
Noah Friedman <friedman@splode.com>
parents:
2307
diff
changeset
|
730 (message "") |
27d28402ee59
new comint-read-noecho.
Noah Friedman <friedman@splode.com>
parents:
2307
diff
changeset
|
731 ans))) |
705 | 732 |
114 | 733 (defun send-invisible (str) |
1356 | 734 "Read a string without echoing. |
735 Then send it to the process running in the current buffer. A new-line | |
736 is additionally sent. String is not saved on comint input history list. | |
584 | 737 Security bug: your string can still be temporarily recovered with |
738 \\[view-lossage]." | |
114 | 739 ; (interactive (list (comint-read-noecho "Enter non-echoed text"))) |
740 (interactive "P") ; Defeat snooping via C-x esc | |
741 (let ((proc (get-buffer-process (current-buffer)))) | |
742 (if (not proc) (error "Current buffer has no process") | |
743 (comint-send-string proc | |
744 (if (stringp str) str | |
705 | 745 (comint-read-noecho "Non-echoed text: " t))) |
114 | 746 (comint-send-string proc "\n")))) |
747 | |
748 | |
749 ;;; Low-level process communication | |
750 | |
751 (defvar comint-input-chunk-size 512 | |
752 "*Long inputs send to comint processes are broken up into chunks of this size. | |
753 If your process is choking on big inputs, try lowering the value.") | |
754 | |
755 (defun comint-send-string (proc str) | |
756 "Send PROCESS the contents of STRING as input. | |
757 This is equivalent to process-send-string, except that long input strings | |
758 are broken up into chunks of size comint-input-chunk-size. Processes | |
759 are given a chance to output between chunks. This can help prevent processes | |
760 from hanging when you send them long inputs on some OS's." | |
761 (let* ((len (length str)) | |
762 (i (min len comint-input-chunk-size))) | |
763 (process-send-string proc (substring str 0 i)) | |
764 (while (< i len) | |
765 (let ((next-i (+ i comint-input-chunk-size))) | |
766 (accept-process-output) | |
767 (process-send-string proc (substring str i (min len next-i))) | |
768 (setq i next-i))))) | |
769 | |
770 (defun comint-send-region (proc start end) | |
771 "Sends to PROC the region delimited by START and END. | |
772 This is a replacement for process-send-region that tries to keep | |
773 your process from hanging on long inputs. See comint-send-string." | |
774 (comint-send-string proc (buffer-substring start end))) | |
775 | |
776 | |
777 ;;; Random input hackage | |
778 | |
584 | 779 (defun comint-kill-output () |
114 | 780 "Kill all output from interpreter since last input." |
781 (interactive) | |
584 | 782 (let ((pmark (process-mark (get-buffer-process (current-buffer))))) |
783 (kill-region comint-last-input-end pmark) | |
784 (goto-char pmark) | |
785 (insert "*** output flushed ***\n") | |
786 (set-marker pmark (point)))) | |
114 | 787 |
788 (defun comint-show-output () | |
584 | 789 "Display start of this batch of interpreter output at top of window. |
790 Also put cursor there." | |
114 | 791 (interactive) |
792 (goto-char comint-last-input-end) | |
584 | 793 (backward-char) |
114 | 794 (beginning-of-line) |
795 (set-window-start (selected-window) (point)) | |
584 | 796 (end-of-line)) |
114 | 797 |
798 (defun comint-interrupt-subjob () | |
584 | 799 "Interrupt the current subjob." |
114 | 800 (interactive) |
584 | 801 (interrupt-process nil comint-ptyp)) |
114 | 802 |
803 (defun comint-kill-subjob () | |
584 | 804 "Send kill signal to the current subjob." |
114 | 805 (interactive) |
584 | 806 (kill-process nil comint-ptyp)) |
114 | 807 |
808 (defun comint-quit-subjob () | |
584 | 809 "Send quit signal to the current subjob." |
114 | 810 (interactive) |
584 | 811 (quit-process nil comint-ptyp)) |
114 | 812 |
813 (defun comint-stop-subjob () | |
584 | 814 "Stop the current subjob. |
114 | 815 WARNING: if there is no current subjob, you can end up suspending |
584 | 816 the top-level process running in the buffer. If you accidentally do |
817 this, use \\[comint-continue-subjob] to resume the process. (This | |
818 is not a problem with most shells, since they ignore this signal.)" | |
114 | 819 (interactive) |
584 | 820 (stop-process nil comint-ptyp)) |
114 | 821 |
822 (defun comint-continue-subjob () | |
584 | 823 "Send CONT signal to process buffer's process group. |
114 | 824 Useful if you accidentally suspend the top-level process." |
825 (interactive) | |
584 | 826 (continue-process nil comint-ptyp)) |
114 | 827 |
828 (defun comint-kill-input () | |
584 | 829 "Kill all text from last stuff output by interpreter to point." |
114 | 830 (interactive) |
584 | 831 (let* ((pmark (process-mark (get-buffer-process (current-buffer)))) |
832 (p-pos (marker-position pmark))) | |
833 (if (> (point) p-pos) | |
834 (kill-region pmark (point))))) | |
114 | 835 |
836 (defun comint-delchar-or-maybe-eof (arg) | |
837 "Delete ARG characters forward, or send an EOF to process if at end of buffer." | |
838 (interactive "p") | |
839 (if (eobp) | |
840 (process-send-eof) | |
584 | 841 (delete-char arg))) |
842 | |
843 | |
844 | |
114 | 845 |
846 ;;; Support for source-file processing commands. | |
847 ;;;============================================================================ | |
848 ;;; Many command-interpreters (e.g., Lisp, Scheme, Soar) have | |
849 ;;; commands that process files of source text (e.g. loading or compiling | |
850 ;;; files). So the corresponding process-in-a-buffer modes have commands | |
851 ;;; for doing this (e.g., lisp-load-file). The functions below are useful | |
852 ;;; for defining these commands. | |
853 ;;; | |
854 ;;; Alas, these guys don't do exactly the right thing for Lisp, Scheme | |
855 ;;; and Soar, in that they don't know anything about file extensions. | |
856 ;;; So the compile/load interface gets the wrong default occasionally. | |
857 ;;; The load-file/compile-file default mechanism could be smarter -- it | |
858 ;;; doesn't know about the relationship between filename extensions and | |
859 ;;; whether the file is source or executable. If you compile foo.lisp | |
860 ;;; with compile-file, then the next load-file should use foo.bin for | |
861 ;;; the default, not foo.lisp. This is tricky to do right, particularly | |
862 ;;; because the extension for executable files varies so much (.o, .bin, | |
863 ;;; .lbin, .mo, .vo, .ao, ...). | |
864 | |
865 | |
866 ;;; COMINT-SOURCE-DEFAULT -- determines defaults for source-file processing | |
867 ;;; commands. | |
868 ;;; | |
869 ;;; COMINT-CHECK-SOURCE -- if FNAME is in a modified buffer, asks you if you | |
870 ;;; want to save the buffer before issuing any process requests to the command | |
871 ;;; interpreter. | |
872 ;;; | |
873 ;;; COMINT-GET-SOURCE -- used by the source-file processing commands to prompt | |
874 ;;; for the file to process. | |
875 | |
876 ;;; (COMINT-SOURCE-DEFAULT previous-dir/file source-modes) | |
877 ;;;============================================================================ | |
878 ;;; This function computes the defaults for the load-file and compile-file | |
584 | 879 ;;; commands for tea, soar, cmulisp, and cmuscheme modes. |
114 | 880 ;;; |
881 ;;; - PREVIOUS-DIR/FILE is a pair (directory . filename) from the last | |
882 ;;; source-file processing command. NIL if there hasn't been one yet. | |
883 ;;; - SOURCE-MODES is a list used to determine what buffers contain source | |
884 ;;; files: if the major mode of the buffer is in SOURCE-MODES, it's source. | |
885 ;;; Typically, (lisp-mode) or (scheme-mode). | |
886 ;;; | |
887 ;;; If the command is given while the cursor is inside a string, *and* | |
888 ;;; the string is an existing filename, *and* the filename is not a directory, | |
889 ;;; then the string is taken as default. This allows you to just position | |
890 ;;; your cursor over a string that's a filename and have it taken as default. | |
891 ;;; | |
892 ;;; If the command is given in a file buffer whose major mode is in | |
893 ;;; SOURCE-MODES, then the the filename is the default file, and the | |
894 ;;; file's directory is the default directory. | |
895 ;;; | |
896 ;;; If the buffer isn't a source file buffer (e.g., it's the process buffer), | |
897 ;;; then the default directory & file are what was used in the last source-file | |
898 ;;; processing command (i.e., PREVIOUS-DIR/FILE). If this is the first time | |
899 ;;; the command has been run (PREVIOUS-DIR/FILE is nil), the default directory | |
900 ;;; is the cwd, with no default file. (\"no default file\" = nil) | |
901 ;;; | |
902 ;;; SOURCE-REGEXP is typically going to be something like (tea-mode) | |
903 ;;; for T programs, (lisp-mode) for Lisp programs, (soar-mode lisp-mode) | |
904 ;;; for Soar programs, etc. | |
905 ;;; | |
906 ;;; The function returns a pair: (default-directory . default-file). | |
907 | |
908 (defun comint-source-default (previous-dir/file source-modes) | |
909 (cond ((and buffer-file-name (memq major-mode source-modes)) | |
910 (cons (file-name-directory buffer-file-name) | |
911 (file-name-nondirectory buffer-file-name))) | |
912 (previous-dir/file) | |
913 (t | |
914 (cons default-directory nil)))) | |
915 | |
584 | 916 |
114 | 917 ;;; (COMINT-CHECK-SOURCE fname) |
918 ;;;============================================================================ | |
584 | 919 ;;; Prior to loading or compiling (or otherwise processing) a file (in the CMU |
114 | 920 ;;; process-in-a-buffer modes), this function can be called on the filename. |
921 ;;; If the file is loaded into a buffer, and the buffer is modified, the user | |
922 ;;; is queried to see if he wants to save the buffer before proceeding with | |
923 ;;; the load or compile. | |
924 | |
925 (defun comint-check-source (fname) | |
926 (let ((buff (get-file-buffer fname))) | |
927 (if (and buff | |
928 (buffer-modified-p buff) | |
929 (y-or-n-p (format "Save buffer %s first? " | |
930 (buffer-name buff)))) | |
931 ;; save BUFF. | |
932 (let ((old-buffer (current-buffer))) | |
933 (set-buffer buff) | |
934 (save-buffer) | |
935 (set-buffer old-buffer))))) | |
936 | |
584 | 937 |
114 | 938 ;;; (COMINT-GET-SOURCE prompt prev-dir/file source-modes mustmatch-p) |
939 ;;;============================================================================ | |
940 ;;; COMINT-GET-SOURCE is used to prompt for filenames in command-interpreter | |
941 ;;; commands that process source files (like loading or compiling a file). | |
942 ;;; It prompts for the filename, provides a default, if there is one, | |
943 ;;; and returns the result filename. | |
944 ;;; | |
945 ;;; See COMINT-SOURCE-DEFAULT for more on determining defaults. | |
946 ;;; | |
947 ;;; PROMPT is the prompt string. PREV-DIR/FILE is the (directory . file) pair | |
948 ;;; from the last source processing command. SOURCE-MODES is a list of major | |
949 ;;; modes used to determine what file buffers contain source files. (These | |
950 ;;; two arguments are used for determining defaults). If MUSTMATCH-P is true, | |
951 ;;; then the filename reader will only accept a file that exists. | |
952 ;;; | |
953 ;;; A typical use: | |
954 ;;; (interactive (comint-get-source "Compile file: " prev-lisp-dir/file | |
955 ;;; '(lisp-mode) t)) | |
956 | |
957 ;;; This is pretty stupid about strings. It decides we're in a string | |
958 ;;; if there's a quote on both sides of point on the current line. | |
959 (defun comint-extract-string () | |
1373 | 960 "Returns string around POINT that starts the current line or nil." |
114 | 961 (save-excursion |
962 (let* ((point (point)) | |
963 (bol (progn (beginning-of-line) (point))) | |
964 (eol (progn (end-of-line) (point))) | |
965 (start (progn (goto-char point) | |
966 (and (search-backward "\"" bol t) | |
967 (1+ (point))))) | |
968 (end (progn (goto-char point) | |
969 (and (search-forward "\"" eol t) | |
970 (1- (point)))))) | |
971 (and start end | |
972 (buffer-substring start end))))) | |
973 | |
974 (defun comint-get-source (prompt prev-dir/file source-modes mustmatch-p) | |
975 (let* ((def (comint-source-default prev-dir/file source-modes)) | |
976 (stringfile (comint-extract-string)) | |
977 (sfile-p (and stringfile | |
584 | 978 (condition-case () |
979 (file-exists-p stringfile) | |
980 (error nil)) | |
114 | 981 (not (file-directory-p stringfile)))) |
982 (defdir (if sfile-p (file-name-directory stringfile) | |
983 (car def))) | |
984 (deffile (if sfile-p (file-name-nondirectory stringfile) | |
985 (cdr def))) | |
986 (ans (read-file-name (if deffile (format "%s(default %s) " | |
987 prompt deffile) | |
988 prompt) | |
989 defdir | |
990 (concat defdir deffile) | |
991 mustmatch-p))) | |
992 (list (expand-file-name (substitute-in-file-name ans))))) | |
993 | |
584 | 994 ;;; I am somewhat divided on this string-default feature. It seems |
995 ;;; to violate the principle-of-least-astonishment, in that it makes | |
996 ;;; the default harder to predict, so you actually have to look and see | |
997 ;;; what the default really is before choosing it. This can trip you up. | |
998 ;;; On the other hand, it can be useful, I guess. I would appreciate feedback | |
999 ;;; on this. | |
1000 ;;; -Olin | |
1001 | |
114 | 1002 |
1003 ;;; Simple process query facility. | |
1004 ;;; =========================================================================== | |
1005 ;;; This function is for commands that want to send a query to the process | |
1006 ;;; and show the response to the user. For example, a command to get the | |
1007 ;;; arglist for a Common Lisp function might send a "(arglist 'foo)" query | |
1008 ;;; to an inferior Common Lisp process. | |
1009 ;;; | |
1010 ;;; This simple facility just sends strings to the inferior process and pops | |
1011 ;;; up a window for the process buffer so you can see what the process | |
1012 ;;; responds with. We don't do anything fancy like try to intercept what the | |
1013 ;;; process responds with and put it in a pop-up window or on the message | |
1014 ;;; line. We just display the buffer. Low tech. Simple. Works good. | |
1015 | |
1016 ;;; Send to the inferior process PROC the string STR. Pop-up but do not select | |
1017 ;;; a window for the inferior process so that its response can be seen. | |
1018 (defun comint-proc-query (proc str) | |
1019 (let* ((proc-buf (process-buffer proc)) | |
1020 (proc-mark (process-mark proc))) | |
1021 (display-buffer proc-buf) | |
1022 (set-buffer proc-buf) ; but it's not the selected *window* | |
1023 (let ((proc-win (get-buffer-window proc-buf)) | |
1024 (proc-pt (marker-position proc-mark))) | |
1025 (comint-send-string proc str) ; send the query | |
1026 (accept-process-output proc) ; wait for some output | |
1027 ;; Try to position the proc window so you can see the answer. | |
1028 ;; This is bogus code. If you delete the (sit-for 0), it breaks. | |
1029 ;; I don't know why. Wizards invited to improve it. | |
1030 (if (not (pos-visible-in-window-p proc-pt proc-win)) | |
1031 (let ((opoint (window-point proc-win))) | |
1032 (set-window-point proc-win proc-mark) (sit-for 0) | |
1033 (if (not (pos-visible-in-window-p opoint proc-win)) | |
1034 (push-mark opoint) | |
1035 (set-window-point proc-win opoint))))))) | |
1036 | |
1037 | |
1038 ;;; Filename completion in a buffer | |
1039 ;;; =========================================================================== | |
1040 ;;; Useful completion functions, courtesy of the Ergo group. | |
1041 | |
1042 ;;; Three commands: | |
1043 ;;; comint-dynamic-complete Complete filename at point. | |
1044 ;;; comint-dynamic-list-completions List completions in help buffer. | |
1045 ;;; comint-replace-by-expanded-filename Expand and complete filename at point; | |
1046 ;;; replace with expanded/completed name. | |
1047 | |
1048 ;;; These are not installed in the comint-mode keymap. But they are | |
584 | 1049 ;;; available for people who want them. Shell-mode installs them: |
2518
10509afe2588
Clean up cmu* uses in header comments.
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2455
diff
changeset
|
1050 ;;; (define-key shell-mode-map "\t" 'comint-dynamic-complete) |
10509afe2588
Clean up cmu* uses in header comments.
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2455
diff
changeset
|
1051 ;;; (define-key shell-mode-map "\M-?" 'comint-dynamic-list-completions))) |
584 | 1052 ;;; |
1053 ;;; Commands like this are fine things to put in load hooks if you | |
909
4c6cdb66c74c
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
870
diff
changeset
|
1054 ;;; want them present in specific modes. |
584 | 1055 |
114 | 1056 |
1057 (defun comint-match-partial-pathname () | |
584 | 1058 "Returns the filename at point or causes an error." |
1059 (save-excursion | |
1060 (if (re-search-backward "[^~/A-Za-z0-9---_.$#,=]" nil 'move) | |
1061 (forward-char 1)) | |
1062 ;; Anchor the search forwards. | |
1063 (if (not (looking-at "[~/A-Za-z0-9---_.$#,=]")) (error "")) | |
1064 (re-search-forward "[~/A-Za-z0-9---_.$#,=]+") | |
1065 (substitute-in-file-name | |
1066 (buffer-substring (match-beginning 0) (match-end 0))))) | |
1067 | |
114 | 1068 |
1069 (defun comint-replace-by-expanded-filename () | |
1356 | 1070 "Expand the filename at point. |
1071 Replace the filename with an expanded, canonicalised, and completed | |
1072 replacement. | |
114 | 1073 \"Expanded\" means environment variables (e.g., $HOME) and ~'s are |
1074 replaced with the corresponding directories. \"Canonicalised\" means .. | |
584 | 1075 and \. are removed, and the filename is made absolute instead of relative. |
1076 See functions expand-file-name and substitute-in-file-name. See also | |
114 | 1077 comint-dynamic-complete." |
1078 (interactive) | |
1079 (let* ((pathname (comint-match-partial-pathname)) | |
1080 (pathdir (file-name-directory pathname)) | |
1081 (pathnondir (file-name-nondirectory pathname)) | |
1082 (completion (file-name-completion pathnondir | |
1083 (or pathdir default-directory)))) | |
1084 (cond ((null completion) | |
909
4c6cdb66c74c
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
870
diff
changeset
|
1085 (message "No completions of %s" pathname) |
584 | 1086 (ding)) |
114 | 1087 ((eql completion t) |
909
4c6cdb66c74c
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
870
diff
changeset
|
1088 (message "Unique completion")) |
114 | 1089 (t ; this means a string was returned. |
1090 (delete-region (match-beginning 0) (match-end 0)) | |
1091 (insert (expand-file-name (concat pathdir completion))))))) | |
1092 | |
584 | 1093 |
114 | 1094 (defun comint-dynamic-complete () |
584 | 1095 "Dynamically complete the filename at point. |
1373 | 1096 This function is similar to `comint-replace-by-expanded-filename', except |
114 | 1097 that it won't change parts of the filename already entered in the buffer; |
1098 it just adds completion characters to the end of the filename." | |
1099 (interactive) | |
1100 (let* ((pathname (comint-match-partial-pathname)) | |
1101 (pathdir (file-name-directory pathname)) | |
1102 (pathnondir (file-name-nondirectory pathname)) | |
584 | 1103 (completion (file-name-completion pathnondir |
114 | 1104 (or pathdir default-directory)))) |
1105 (cond ((null completion) | |
909
4c6cdb66c74c
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
870
diff
changeset
|
1106 (message "No completions of %s" pathname) |
584 | 1107 (ding)) |
114 | 1108 ((eql completion t) |
909
4c6cdb66c74c
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
870
diff
changeset
|
1109 (message "Unique completion")) |
114 | 1110 (t ; this means a string was returned. |
1111 (goto-char (match-end 0)) | |
1112 (insert (substring completion (length pathnondir))))))) | |
1113 | |
1114 (defun comint-dynamic-list-completions () | |
584 | 1115 "List in help buffer all possible completions of the filename at point." |
114 | 1116 (interactive) |
1117 (let* ((pathname (comint-match-partial-pathname)) | |
1118 (pathdir (file-name-directory pathname)) | |
1119 (pathnondir (file-name-nondirectory pathname)) | |
1120 (completions | |
1121 (file-name-all-completions pathnondir | |
1122 (or pathdir default-directory)))) | |
1123 (cond ((null completions) | |
909
4c6cdb66c74c
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
870
diff
changeset
|
1124 (message "No completions of %s" pathname) |
584 | 1125 (ding)) |
114 | 1126 (t |
1127 (let ((conf (current-window-configuration))) | |
584 | 1128 (with-output-to-temp-buffer "*Help*" |
114 | 1129 (display-completion-list completions)) |
1130 (sit-for 0) | |
909
4c6cdb66c74c
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
870
diff
changeset
|
1131 (message "Hit space to flush") |
2022
9a765951a9b9
(comint-dynamic-list-completions): Handle any event when flushing the display.
Richard M. Stallman <rms@gnu.org>
parents:
1839
diff
changeset
|
1132 (let ((ch (read-event))) |
2025
3f02dc3e10a8
Fix bug in previous change.
Richard M. Stallman <rms@gnu.org>
parents:
2022
diff
changeset
|
1133 (if (eq ch ?\ ) |
114 | 1134 (set-window-configuration conf) |
2022
9a765951a9b9
(comint-dynamic-list-completions): Handle any event when flushing the display.
Richard M. Stallman <rms@gnu.org>
parents:
1839
diff
changeset
|
1135 (setq unread-command-events (list ch))))))))) |
114 | 1136 |
1137 ;;; Converting process modes to use comint mode | |
1138 ;;; =========================================================================== | |
768
4abf65b235d9
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
705
diff
changeset
|
1139 ;;; The code in the Emacs 19 distribution has all been modified to use comint |
4abf65b235d9
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
705
diff
changeset
|
1140 ;;; where needed. However, there are `third-party' packages out there that |
4abf65b235d9
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
705
diff
changeset
|
1141 ;;; still use the old shell mode. Here's a guide to conversion. |
584 | 1142 ;;; |
114 | 1143 ;;; Renaming variables |
584 | 1144 ;;; Most of the work is renaming variables and functions. These are the common |
1145 ;;; ones: | |
1146 ;;; Local variables: | |
705 | 1147 ;;; last-input-start comint-last-input-start |
584 | 1148 ;;; last-input-end comint-last-input-end |
1149 ;;; shell-prompt-pattern comint-prompt-regexp | |
1150 ;;; shell-set-directory-error-hook <no equivalent> | |
1151 ;;; Miscellaneous: | |
114 | 1152 ;;; shell-set-directory <unnecessary> |
1153 ;;; shell-mode-map comint-mode-map | |
584 | 1154 ;;; Commands: |
114 | 1155 ;;; shell-send-input comint-send-input |
1156 ;;; shell-send-eof comint-delchar-or-maybe-eof | |
1157 ;;; kill-shell-input comint-kill-input | |
1158 ;;; interrupt-shell-subjob comint-interrupt-subjob | |
1159 ;;; stop-shell-subjob comint-stop-subjob | |
1160 ;;; quit-shell-subjob comint-quit-subjob | |
1161 ;;; kill-shell-subjob comint-kill-subjob | |
1162 ;;; kill-output-from-shell comint-kill-output | |
1163 ;;; show-output-from-shell comint-show-output | |
1164 ;;; copy-last-shell-input Use comint-previous-input/comint-next-input | |
1165 ;;; | |
705 | 1166 ;;; SHELL-SET-DIRECTORY is gone, its functionality taken over by |
1167 ;;; SHELL-DIRECTORY-TRACKER, the shell mode's comint-input-sentinel. | |
1168 ;;; Comint mode does not provide functionality equivalent to | |
114 | 1169 ;;; shell-set-directory-error-hook; it is gone. |
705 | 1170 ;;; |
1171 ;;; comint-last-input-start is provided for modes which want to munge | |
1172 ;;; the buffer after input is sent, perhaps because the inferior | |
1173 ;;; insists on echoing the input. The LAST-INPUT-START variable in | |
1174 ;;; the old shell package was used to implement a history mechanism, | |
1175 ;;; but you should think twice before using comint-last-input-start | |
1176 ;;; for this; the input history ring often does the job better. | |
114 | 1177 ;;; |
1178 ;;; If you are implementing some process-in-a-buffer mode, called foo-mode, do | |
1179 ;;; *not* create the comint-mode local variables in your foo-mode function. | |
1180 ;;; This is not modular. Instead, call comint-mode, and let *it* create the | |
1181 ;;; necessary comint-specific local variables. Then create the | |
1182 ;;; foo-mode-specific local variables in foo-mode. Set the buffer's keymap to | |
1183 ;;; be foo-mode-map, and its mode to be foo-mode. Set the comint-mode hooks | |
1184 ;;; (comint-prompt-regexp, comint-input-filter, comint-input-sentinel, | |
1185 ;;; comint-get-old-input) that need to be different from the defaults. Call | |
1186 ;;; foo-mode-hook, and you're done. Don't run the comint-mode hook yourself; | |
2518
10509afe2588
Clean up cmu* uses in header comments.
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2455
diff
changeset
|
1187 ;;; comint-mode will take care of it. The following example, from shell.el, |
584 | 1188 ;;; is typical: |
1189 ;;; | |
1190 ;;; (defun shell-mode () | |
1191 ;;; (interactive) | |
1192 ;;; (comint-mode) | |
1193 ;;; (setq comint-prompt-regexp shell-prompt-pattern) | |
1194 ;;; (setq major-mode 'shell-mode) | |
1195 ;;; (setq mode-name "Shell") | |
1196 ;;; (cond ((not shell-mode-map) | |
1197 ;;; (setq shell-mode-map (full-copy-sparse-keymap comint-mode-map)) | |
1198 ;;; (define-key shell-mode-map "\M-\t" 'comint-dynamic-complete) | |
1199 ;;; (define-key shell-mode-map "\M-?" | |
1200 ;;; 'comint-dynamic-list-completions))) | |
1201 ;;; (use-local-map shell-mode-map) | |
1202 ;;; (make-local-variable 'shell-directory-stack) | |
1203 ;;; (setq shell-directory-stack nil) | |
1204 ;;; (setq comint-input-sentinel 'shell-directory-tracker) | |
1205 ;;; (run-hooks 'shell-mode-hook)) | |
1206 ;;; | |
114 | 1207 ;;; |
1208 ;;; Note that make-comint is different from make-shell in that it | |
1209 ;;; doesn't have a default program argument. If you give make-shell | |
1210 ;;; a program name of NIL, it cleverly chooses one of explicit-shell-name, | |
1211 ;;; $ESHELL, $SHELL, or /bin/sh. If you give make-comint a program argument | |
1212 ;;; of NIL, it barfs. Adjust your code accordingly... | |
584 | 1213 ;;; |
1214 | |
1215 ;;; Do the user's customisation... | |
1216 | |
1217 (defvar comint-load-hook nil | |
1218 "This hook is run when comint is loaded in. | |
1219 This is a good place to put keybindings.") | |
1220 | |
1221 (run-hooks 'comint-load-hook) | |
1222 | |
1223 ;;; Change log: | |
1224 ;;; 9/12/89 | |
1225 ;;; - Souped up the filename expansion procedures. | |
1226 ;;; Doc strings are much clearer and more detailed. | |
1227 ;;; Fixed a bug where doing a filename completion when the point | |
1228 ;;; was in the middle of the filename instead of at the end would lose. | |
1229 ;;; | |
1230 ;;; 2/17/90 | |
1231 ;;; - Souped up the command history stuff so that text inserted | |
1232 ;;; by comint-previous-input-matching is removed by following | |
1233 ;;; command history recalls. comint-next/previous-input-matching | |
1234 ;;; is now much more smoothly integrated w/the command history stuff. | |
1235 ;;; - Added comint-eol-on-send flag and comint-input-sender hook. | |
1236 ;;; Comint-input-sender based on code contributed by Jeff Peck | |
1237 ;;; (peck@sun.com). | |
1238 ;;; | |
1239 ;;; 3/13/90 ccm@cmu.cs.edu | |
1240 ;;; - Added comint-previous-similar-input for looking up similar inputs. | |
1241 ;;; - Added comint-send-and-get-output to allow snarfing input from | |
1242 ;;; buffer. | |
1243 ;;; - Added the ability to pick up a source file by positioning over | |
1244 ;;; a string in comint-get-source. | |
1245 ;;; - Added add-hook to make it a little easier for the user to use | |
1246 ;;; multiple hooks. | |
1247 ;;; | |
1248 ;;; 5/22/90 shivers | |
1249 ;;; - Moved Chris' multiplexed ipc stuff to comint-ipc.el. | |
1250 ;;; - Altered Chris' comint-get-source string feature. The string | |
1251 ;;; is only offered as a default if it names an existing file. | |
1252 ;;; - Changed comint-exec to directly crank up the process, instead | |
1253 ;;; of calling the env program. This made background.el happy. | |
1254 ;;; - Added new buffer-local var comint-ptyp. The problem is that | |
1255 ;;; the signalling functions don't work as advertised. If you are | |
1256 ;;; communicating via pipes, the CURRENT-GROUP arg is supposed to | |
1257 ;;; be ignored, but, unfortunately it seems to be the case that you | |
1258 ;;; must pass a NIL for this arg in the pipe case. COMINT-PTYP | |
1259 ;;; is a flag that tells whether the process is communicating | |
1260 ;;; via pipes or a pty. The comint signalling functions use it | |
1261 ;;; to determine the necessary CURRENT-GROUP arg value. The bug | |
1262 ;;; has been reported to the Gnu folks. | |
1263 ;;; - comint-dynamic-complete flushes the help window if you hit space | |
1264 ;;; after you execute it. | |
1265 ;;; - Added functions comint-send-string, comint-send-region and var | |
1266 ;;; comint-input-chunk-size. comint-send-string tries to prevent processes | |
1267 ;;; from hanging when you send them long strings by breaking them into | |
1268 ;;; chunks and allowing process output between chunks. I got the idea from | |
1269 ;;; Eero Simoncelli's Common Lisp package. Note that using | |
1270 ;;; comint-send-string means that the process buffer's contents can change | |
1271 ;;; during a call! If you depend on process output only happening between | |
1272 ;;; toplevel commands, this could be a problem. In such a case, use | |
1273 ;;; process-send-string instead. If this is a problem for people, I'd like | |
1274 ;;; to hear about it. | |
1275 ;;; - Added comint-proc-query as a simple mechanism for commands that | |
1276 ;;; want to query an inferior process and display its response. For a | |
1277 ;;; typical use, see lisp-show-arglist in cmulisp.el. | |
1278 ;;; - Added constant comint-version, which is now "2.01". | |
1279 ;;; | |
1280 ;;; 6/14/90 shivers | |
1281 ;;; - Had comint-update-env defined twice. Removed extra copy. Also | |
1282 ;;; renamed mem to be comint-mem, for modularity. The duplication | |
1283 ;;; was reported by Michael Meissner. | |
1284 ;;; 6/16/90 shivers | |
1285 ;;; - Emacs has two different mechanisms for maintaining the process | |
1286 ;;; environment, determined at compile time by the MAINTAIN-ENVIRONMENT | |
1287 ;;; #define. One uses the process-environment global variable, and | |
1288 ;;; one uses a getenv/setenv interface. comint-exec assumed the | |
1289 ;;; process-environment interface; it has been generalised (with | |
1290 ;;; comint-exec-1) to handle both cases. Pretty bogus. We could, | |
1291 ;;; of course, skip all this and just use the etc/env program to | |
1292 ;;; handle the environment tweaking, but that obscures process | |
1293 ;;; queries that other modules (like background.el) depend on. etc/env | |
1294 ;;; is also fairly bogus. This bug, and some of the fix code was | |
1295 ;;; reported by Dan Pierson. | |
1296 ;;; | |
1297 ;;; 9/5/90 shivers | |
1298 ;;; - Changed make-variable-buffer-local's to make-local-variable's. | |
1299 ;;; This leaves non-comint-mode buffers alone. Stephane Payrard | |
1300 ;;; reported the sloppy useage. | |
1301 ;;; - You can now go from comint-previous-similar-input to | |
1302 ;;; comint-previous-input with no problem. | |
1303 ;;; | |
1304 ;;; 12/21/90 shivers | |
1305 ;;; - Added a condition-case to comint-get-source. Bogus strings | |
1306 ;;; beginning with ~ were making the file-exists-p barf. | |
1307 ;;; - Added "=" to the set of chars recognised by file completion | |
1308 ;;; as constituting a filename. | |
1309 ;;; | |
1310 ;;; 1/90 shivers | |
1311 ;;; These changes comprise release 2.02: | |
1312 ;;; - Removed the kill-all-local-variables in comint-mode. This | |
1313 ;;; made it impossible for client modes to set things before calling | |
1314 ;;; comint-mode. (In particular, it messed up ilisp.el) In general, | |
1315 ;;; the client mode should be responsible for a k-a-l-v's. | |
1316 ;;; - Fixed comint-match-partial-pathname so that it works in | |
1317 ;;; more cases: if the filename begins at the start-of-buffer; | |
1318 ;;; if point is on the first char of the filename. Just a question | |
1319 ;;; of getting the tricky bits right. | |
1320 ;;; - Added a hook, comint-exec-hook that is run each time a process | |
1321 ;;; is cranked up. Useful for things like process-kill-without-query. | |
1322 ;;; | |
705 | 1323 ;;; These two were pointed out by tale: |
584 | 1324 ;;; - Improved the doc string in comint-send-input a little bit. |
1325 ;;; - Tweaked make-comint to check process status with comint-check-proc | |
1326 ;;; instead of equivalent inline code. | |
705 | 1327 ;;; |
584 | 1328 ;;; - Prompt-search history commands have been commented out. I never |
1329 ;;; liked them; I don't think anyone used them. | |
705 | 1330 ;;; - Made comint-exec-hook a local var, as it should have been. |
2518
10509afe2588
Clean up cmu* uses in header comments.
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2455
diff
changeset
|
1331 ;;; (This way, for instance, you can have shell procs kill-w/o-query, |
705 | 1332 ;;; but let Scheme procs be default.) |
1333 ;;; | |
1334 ;;; 7/91 Shivers | |
1335 ;;; - Souped up comint-read-noecho with an optional argument, STARS. | |
1336 ;;; Suggested by mjlx@EAGLE.CNSF.CORNELL.EDU. | |
1337 ;;; - Moved comint-previous-input-matching from C-c r to C-M-r. | |
1338 ;;; C-c <letter> bindings are reserved for the user. | |
1339 ;;; These bindings were done by Jim Blandy. | |
1340 ;;; These changes comprise version 2.03. | |
584 | 1341 |
1342 (provide 'comint) | |
662
8a533acedb77
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
638
diff
changeset
|
1343 |
8a533acedb77
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
638
diff
changeset
|
1344 ;;; comint.el ends here |