Mercurial > emacs
annotate lisp/term.el @ 9738:511563c17f9c
(LD_SWITCH_MACHINE): New definition.
author | Richard M. Stallman <rms@gnu.org> |
---|---|
date | Fri, 28 Oct 1994 18:54:40 +0000 |
parents | 8792d17812fb |
children | c840b3eb9015 |
rev | line source |
---|---|
9509 | 1 ;; term.el --- general command interpreter in a window stuff |
2 ;; Copyright (C) 1988, 1990, 1992, 1992, 1994 Free Software Foundation, Inc. | |
3 | |
4 ;; Author: Per Bothner <bothner@cygnus.com> | |
5 ;; Based on comint mode written by: Olin Shivers <shivers@cs.cmu.edu> | |
6 ;; Keyword: processes | |
7 | |
8 ;; This file is part of GNU Emacs. | |
9 | |
10 ;; GNU Emacs is free software; you can redistribute it and/or modify | |
11 ;; it under the terms of the GNU General Public License as published by | |
12 ;; the Free Software Foundation; either version 2, or (at your option) | |
13 ;; any later version. | |
14 | |
15 ;; GNU Emacs is distributed in the hope that it will be useful, | |
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
18 ;; GNU General Public License for more details. | |
19 | |
20 ;; You should have received a copy of the GNU General Public License | |
21 ;; along with GNU Emacs; see the file COPYING. If not, write to | |
22 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. | |
23 | |
24 ;;; Commentary: | |
25 | |
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 ;;; - Per Bothner (bothner@cygnus.com) | |
31 | |
32 ;;; This file defines a general command-interpreter-in-a-buffer package | |
33 ;;; (term mode). The idea is that you can build specific process-in-a-buffer | |
34 ;;; modes on top of term 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 | |
39 ;;; For hints on converting existing process modes (e.g., tex-mode, | |
40 ;;; background, dbx, gdb, kermit, prolog, telnet) to use term-mode | |
41 ;;; instead of shell-mode, see the notes at the end of this file. | |
42 | |
43 | |
44 ;;; Brief Command Documentation: | |
45 ;;;============================================================================ | |
46 ;;; Term Mode Commands: (common to all derived modes, like cmushell & cmulisp | |
47 ;;; mode) | |
48 ;;; | |
49 ;;; m-p term-previous-input Cycle backwards in input history | |
50 ;;; m-n term-next-input Cycle forwards | |
51 ;;; m-r term-previous-matching-input Previous input matching a regexp | |
52 ;;; m-s comint-next-matching-input Next input that matches | |
53 ;;; return term-send-input | |
54 ;;; c-c c-a term-bol Beginning of line; skip prompt. | |
55 ;;; c-d term-delchar-or-maybe-eof Delete char unless at end of buff. | |
56 ;;; c-c c-u term-kill-input ^u | |
57 ;;; c-c c-w backward-kill-word ^w | |
58 ;;; c-c c-c term-interrupt-subjob ^c | |
59 ;;; c-c c-z term-stop-subjob ^z | |
60 ;;; c-c c-\ term-quit-subjob ^\ | |
61 ;;; c-c c-o term-kill-output Delete last batch of process output | |
62 ;;; c-c c-r term-show-output Show last batch of process output | |
63 ;;; c-c c-h term-dynamic-list-input-ring List input history | |
64 ;;; | |
65 ;;; Not bound by default in term-mode | |
66 ;;; term-send-invisible Read a line w/o echo, and send to proc | |
67 ;;; (These are bound in shell-mode) | |
68 ;;; term-dynamic-complete Complete filename at point. | |
69 ;;; term-dynamic-list-completions List completions in help buffer. | |
70 ;;; term-replace-by-expanded-filename Expand and complete filename at point; | |
71 ;;; replace with expanded/completed name. | |
72 ;;; term-kill-subjob No mercy. | |
73 ;;; term-show-maximum-output Show as much output as possible. | |
74 ;;; term-continue-subjob Send CONT signal to buffer's process | |
75 ;;; group. Useful if you accidentally | |
76 ;;; suspend your process (with C-c C-z). | |
77 | |
78 ;;; term-mode-hook is the term mode hook. Basically for your keybindings. | |
79 ;;; term-load-hook is run after loading in this package. | |
80 | |
81 ;;; Code: | |
82 | |
83 (defconst term-version "0.93") | |
84 | |
85 (require 'ring) | |
86 (require 'ehelp) | |
87 | |
88 ;;; Buffer Local Variables: | |
89 ;;;============================================================================ | |
90 ;;; Term mode buffer local variables: | |
91 ;;; term-prompt-regexp - string term-bol uses to match prompt. | |
92 ;;; term-delimiter-argument-list - list For delimiters and arguments | |
93 ;;; term-last-input-start - marker Handy if inferior always echoes | |
94 ;;; term-last-input-end - marker For term-kill-output command | |
95 ;;; term-input-ring-size - integer For the input history | |
96 ;;; term-input-ring - ring mechanism | |
97 ;;; term-input-ring-index - number ... | |
98 ;;; term-input-autoexpand - symbol ... | |
99 ;;; term-input-ignoredups - boolean ... | |
100 ;;; term-last-input-match - string ... | |
101 ;;; term-dynamic-complete-functions - hook For the completion mechanism | |
102 ;;; term-completion-fignore - list ... | |
103 ;;; term-get-old-input - function Hooks for specific | |
104 ;;; term-input-filter-functions - hook process-in-a-buffer | |
105 ;;; term-input-filter - function modes. | |
106 ;;; term-input-send - function | |
107 ;;; term-scroll-to-bottom-on-output - symbol ... | |
108 ;;; term-scroll-show-maximum-output - boolean... | |
109 | |
110 (defvar explicit-shell-file-name nil | |
111 "*If non-nil, is file name to use for explicitly requested inferior shell.") | |
112 | |
113 (defvar term-prompt-regexp "^" | |
114 "Regexp to recognise prompts in the inferior process. | |
115 Defaults to \"^\", the null string at BOL. | |
116 | |
117 Good choices: | |
118 Canonical Lisp: \"^[^> \\n]*>+:? *\" (Lucid, franz, kcl, T, cscheme, oaklisp) | |
119 Lucid Common Lisp: \"^\\\\(>\\\\|\\\\(->\\\\)+\\\\) *\" | |
120 franz: \"^\\\\(->\\\\|<[0-9]*>:\\\\) *\" | |
121 kcl: \"^>+ *\" | |
122 shell: \"^[^#$%>\\n]*[#$%>] *\" | |
123 T: \"^>+ *\" | |
124 | |
125 This is a good thing to set in mode hooks.") | |
126 | |
127 (defvar term-delimiter-argument-list () | |
128 "List of characters to recognise as separate arguments in input. | |
129 Strings comprising a character in this list will separate the arguments | |
130 surrounding them, and also be regarded as arguments in their own right (unlike | |
131 whitespace). See `term-arguments'. | |
132 Defaults to the empty list. | |
133 | |
134 For shells, a good value is (?\\| ?& ?< ?> ?\\( ?\\) ?;). | |
135 | |
136 This is a good thing to set in mode hooks.") | |
137 | |
138 (defvar term-input-autoexpand nil | |
139 "*If non-nil, expand input command history references on completion. | |
140 This mirrors the optional behavior of tcsh (its autoexpand and histlit). | |
141 | |
142 If the value is `input', then the expansion is seen on input. | |
143 If the value is `history', then the expansion is only when inserting | |
144 into the buffer's input ring. See also `term-magic-space' and | |
145 `term-dynamic-complete'. | |
146 | |
147 This variable is buffer-local.") | |
148 | |
149 (defvar term-input-ignoredups nil | |
150 "*If non-nil, don't add input matching the last on the input ring. | |
151 This mirrors the optional behavior of bash. | |
152 | |
153 This variable is buffer-local.") | |
154 | |
155 (defvar term-input-ring-file-name nil | |
156 "*If non-nil, name of the file to read/write input history. | |
157 See also `term-read-input-ring' and `term-write-input-ring'. | |
158 | |
159 This variable is buffer-local, and is a good thing to set in mode hooks.") | |
160 | |
161 (defvar term-scroll-to-bottom-on-output nil | |
162 "*Controls whether interpreter output causes window to scroll. | |
163 If nil, then do not scroll. If t or `all', scroll all windows showing buffer. | |
164 If `this', scroll only the selected window. | |
165 If `others', scroll only those that are not the selected window. | |
166 | |
167 The default is nil. | |
168 | |
169 See variable `term-scroll-show-maximum-output'. | |
170 This variable is buffer-local.") | |
171 | |
172 (defvar term-scroll-show-maximum-output nil | |
173 "*Controls how interpreter output causes window to scroll. | |
174 If non-nil, then show the maximum output when the window is scrolled. | |
175 | |
176 See variable `term-scroll-to-bottom-on-output'. | |
177 This variable is buffer-local.") | |
178 | |
179 (defvar term-input-ring-size 32 | |
180 "Size of input history ring.") | |
181 | |
182 ;; Where gud-display-frame should put the debugging arrow. This is | |
183 ;; set by the marker-filter, which scans the debugger's output for | |
184 ;; indications of the current pc. | |
185 (defvar term-pending-frame nil) | |
186 | |
187 ;;; Here are the per-interpreter hooks. | |
188 (defvar term-get-old-input (function term-get-old-input-default) | |
189 "Function that submits old text in term mode. | |
190 This function is called when return is typed while the point is in old text. | |
191 It returns the text to be submitted as process input. The default is | |
192 term-get-old-input-default, which grabs the current line, and strips off | |
193 leading text matching term-prompt-regexp") | |
194 | |
195 (defvar term-dynamic-complete-functions | |
196 '(term-replace-by-expanded-history term-dynamic-complete-filename) | |
197 "List of functions called to perform completion. | |
198 Functions should return non-nil if completion was performed. | |
199 See also `term-dynamic-complete'. | |
200 | |
201 This is a good thing to set in mode hooks.") | |
202 | |
203 (defvar term-input-filter | |
204 (function (lambda (str) (not (string-match "\\`\\s *\\'" str)))) | |
205 "Predicate for filtering additions to input history. | |
206 Only inputs answering true to this function are saved on the input | |
207 history list. Default is to save anything that isn't all whitespace") | |
208 | |
209 (defvar term-input-filter-functions '() | |
210 "Functions to call before input is sent to the process. | |
211 These functions get one argument, a string containing the text to send. | |
212 | |
213 This variable is buffer-local.") | |
214 | |
215 (defvar term-input-sender (function term-simple-send) | |
216 "Function to actually send to PROCESS the STRING submitted by user. | |
217 Usually this is just 'term-simple-send, but if your mode needs to | |
218 massage the input string, this is your hook. This is called from | |
219 the user command term-send-input. term-simple-send just sends | |
220 the string plus a newline.") | |
221 | |
222 (defvar term-mode-hook '() | |
223 "Called upon entry into term-mode | |
224 This is run before the process is cranked up.") | |
225 | |
226 (defvar term-exec-hook '() | |
227 "Called each time a process is exec'd by term-exec. | |
228 This is called after the process is cranked up. It is useful for things that | |
229 must be done each time a process is executed in a term-mode buffer (e.g., | |
230 (process-kill-without-query)). In contrast, the term-mode-hook is only | |
231 executed once when the buffer is created.") | |
232 | |
233 (defvar term-mode-map nil) | |
234 (defvar term-raw-map nil | |
235 "Keyboard map for sending characters directly to the inferior process.") | |
236 (defvar term-escape-char nil) | |
237 (defvar term-raw-escape-map nil) | |
238 | |
239 (defvar term-pager-break-map nil) | |
240 | |
241 (defvar term-ptyp t | |
242 "True if communications via pty; false if by pipe. Buffer local. | |
243 This is to work around a bug in emacs process signalling.") | |
244 | |
245 (defvar term-last-input-match "" | |
246 "Last string searched for by term input history search, for defaulting. | |
247 Buffer local variable.") | |
248 | |
249 (defvar term-input-ring nil) | |
250 (defvar term-last-input-start) | |
251 (defvar term-last-input-end) | |
252 (defvar term-input-ring-index nil | |
253 "Index of last matched history element.") | |
254 (defvar term-matching-input-from-input-string "" | |
255 "Input previously used to match input history.") | |
256 ; This argument to set-process-filter disables reading from the process, | |
257 ; assuming this is emacs-19.20 or newer. | |
258 (defvar term-pager-filter t) | |
259 | |
260 (put 'term-replace-by-expanded-history 'menu-enable 'term-input-autoexpand) | |
261 (put 'term-input-ring 'permanent-local t) | |
262 (put 'term-input-ring-index 'permanent-local t) | |
263 (put 'term-input-autoexpand 'permanent-local t) | |
264 (put 'term-input-filter-functions 'permanent-local t) | |
265 (put 'term-scroll-to-bottom-on-output 'permanent-local t) | |
266 (put 'term-scroll-show-maximum-output 'permanent-local t) | |
267 (put 'term-ptyp 'permanent-local t) | |
268 | |
269 (defmacro term-is-emacs19 () '(string-match "^19" emacs-version)) | |
270 ;; True if running under XEmacs (perviously Lucid emacs). | |
271 (defmacro term-is-xemacs () '(string-match "Lucid" emacs-version)) | |
272 | |
273 (defmacro term-in-char-mode () '(eq (current-local-map) term-raw-map)) | |
274 (defmacro term-in-line-mode () '(not (term-in-char-mode))) | |
275 | |
276 (if (term-is-xemacs) | |
277 (defvar term-terminal-menu | |
278 '("Terminal" | |
279 [ "Character mode" term-char-mode (term-in-line-mode)] | |
280 [ "Line mode" term-line-mode (term-in-char-mode)] | |
281 [ "Enable paging" term-pager-enable (not term-pager-count)] | |
282 [ "Disable paging" term-pager-disable term-pager-count])) | |
283 ) | |
284 | |
285 (put 'term-char-mode 'menu-enable '(term-in-line-mode)) | |
286 (put 'term-line-mode 'menu-enable '(term-in-char-mode)) | |
287 (put 'term-pager-enable 'menu-enable '(not term-pager-count)) | |
288 (put 'term-pager-disable 'menu-enable 'term-pager-count) | |
289 | |
290 (defun term-mode () | |
291 "Major mode for interacting with an inferior interpreter. | |
292 Interpreter name is same as buffer name, sans the asterisks. | |
293 In line sub-mode, return at end of buffer sends line as input, | |
294 while return not at end copies rest of line to end and sends it. | |
295 In char sub-mode, each character (except `term-escape-char`) is | |
296 set immediately. | |
297 | |
298 This mode is typically customised to create inferior-lisp-mode, | |
299 shell-mode, etc.. This can be done by setting the hooks | |
300 term-input-filter-functions, term-input-filter, term-input-sender and | |
301 term-get-old-input to appropriate functions, and the variable | |
302 term-prompt-regexp to the appropriate regular expression. | |
303 | |
304 An input history is maintained of size `term-input-ring-size', and | |
305 can be accessed with the commands \\[term-next-input], \\[term-previous-input], and \\[term-dynamic-list-input-ring]. | |
306 Input ring history expansion can be achieved with the commands | |
307 \\[term-replace-by-expanded-history] or \\[term-magic-space]. | |
308 Input ring expansion is controlled by the variable `term-input-autoexpand', | |
309 and addition is controlled by the variable `term-input-ignoredups'. | |
310 | |
311 Input to, and output from, the subprocess can cause the window to scroll to | |
312 the end of the buffer. See variables `term-scroll-to-bottom-on-input', | |
313 and `term-scroll-to-bottom-on-output'. | |
314 | |
315 If you accidentally suspend your process, use \\[term-continue-subjob] | |
316 to continue it. | |
317 | |
318 \\{term-mode-map} | |
319 | |
320 Entry to this mode runs the hooks on term-mode-hook" | |
321 (interactive) | |
322 ;; Do not remove this. All major modes must do this. | |
323 (kill-all-local-variables) | |
324 (setq major-mode 'term-mode) | |
325 (setq mode-name "Term") | |
326 (setq mode-line-process '(": line %s")) | |
327 (use-local-map term-mode-map) | |
328 (make-local-variable 'term-home-marker) | |
329 (setq term-home-marker (copy-marker 0)) | |
330 (make-local-variable 'term-saved-home-marker) | |
331 (setq term-saved-home-marker nil) | |
332 (make-local-variable 'term-height) | |
333 (make-local-variable 'term-width) | |
334 (setq term-width (1- (window-width))) | |
335 (setq term-height (1- (window-height))) | |
336 (make-local-variable 'term-terminal-parameter) | |
337 (make-local-variable 'term-saved-cursor) | |
338 (setq term-saved-cursor nil) | |
339 (make-local-variable 'term-last-input-start) | |
340 (setq term-last-input-start (make-marker)) | |
341 (make-local-variable 'term-last-input-end) | |
342 (setq term-last-input-end (make-marker)) | |
343 (make-local-variable 'term-last-input-match) | |
344 (setq term-last-input-match "") | |
345 (make-local-variable 'term-prompt-regexp) ; Don't set; default | |
346 (make-local-variable 'term-input-ring-size) ; ...to global val. | |
347 (make-local-variable 'term-input-ring) | |
348 (make-local-variable 'term-input-ring-file-name) | |
349 (or (and (boundp 'term-input-ring) term-input-ring) | |
350 (setq term-input-ring (make-ring term-input-ring-size))) | |
351 (make-local-variable 'term-input-ring-index) | |
352 (or (and (boundp 'term-input-ring-index) term-input-ring-index) | |
353 (setq term-input-ring-index nil)) | |
354 | |
355 (make-local-variable 'term-command-hook) | |
356 (setq term-command-hook (symbol-function 'term-command-hook)) | |
357 | |
358 ;; state 0: Normal state | |
359 ;; state 1: Last character was a graphic in the last column. | |
360 ;; If next char is graphic, first move one column right | |
361 ;; (and line warp) before displaying it. | |
362 ;; This emulates (more or less) the behavior of xterm. | |
363 ;; state 2: seen ESC | |
364 ;; state 3: seen ESC [ (or ESC [ ?) | |
365 ;; state 4: term-terminal-parameter contains pending output. | |
366 (make-local-variable 'term-terminal-state) | |
367 (setq term-terminal-state 0) | |
368 | |
369 ;; A queue of strings whose echo we want suppressed. | |
370 (make-local-variable 'term-kill-echo-list) | |
371 (setq term-kill-echo-list nil) | |
372 | |
373 ;; (current-column) at start of screen line, or nil if unknown. | |
374 (make-local-variable 'term-start-line-column) | |
375 (setq term-start-line-column 0) | |
376 ;; Cache for (current-column), or nil if unknown. | |
377 (make-local-variable 'term-current-column) | |
378 (setq term-current-column 0) | |
379 ;; Current vertical row (from home-marker) or nil if unknown. | |
380 (make-local-variable 'term-current-row) | |
381 (setq term-current-row 0) | |
382 (make-local-variable 'term-log-buffer) | |
383 (setq term-log-buffer nil) | |
384 (make-local-variable 'term-scroll-start) | |
385 (setq term-scroll-start 0) | |
386 (make-local-variable 'term-scroll-end) | |
387 (setq term-scroll-end term-height) | |
388 ;; term-scroll-with-delete is t if forward scrolling should | |
389 ;; be implemented by delete to top-most line(s); and nil if | |
390 ;; scrolling should be implemented by moving term-home-marker. | |
391 ;; It is set to t iff there is a (non-default) scroll-region | |
392 ;; OR the alternate buffer is used. | |
393 (make-local-variable 'term-scroll-with-delete) | |
394 (setq term-scroll-with-delete nil) | |
395 (make-local-variable 'term-pager-count) | |
396 ;;(setq term-pager-count 0) | |
397 (setq term-pager-count nil) | |
398 ;; Used to save the old keymap when doing PAGER processing. | |
399 (make-local-variable 'term-pager-old-local-map) | |
400 (setq term-pager-old-local-map nil) | |
401 ;; Used to save the old keymap when in char mode. | |
402 (make-local-variable 'term-old-mode-map) | |
403 (setq term-old-mode-map nil) | |
404 (make-local-variable 'term-insert-mode) | |
405 (setq term-insert-mode nil) | |
406 (make-local-variable 'term-dynamic-complete-functions) | |
407 (make-local-variable 'term-completion-fignore) | |
408 (make-local-variable 'term-get-old-input) | |
409 (make-local-variable 'term-matching-input-from-input-string) | |
410 (make-local-variable 'term-input-autoexpand) | |
411 (make-local-variable 'term-input-ignoredups) | |
412 (make-local-variable 'term-delimiter-argument-list) | |
413 (make-local-variable 'term-input-filter-functions) | |
414 (make-local-variable 'term-input-filter) | |
415 (make-local-variable 'term-input-sender) | |
416 (make-local-variable 'term-scroll-to-bottom-on-output) | |
417 (make-local-variable 'term-scroll-show-maximum-output) | |
418 (make-local-variable 'term-ptyp) | |
419 (make-local-variable 'term-exec-hook) | |
420 (make-local-variable 'term-vertical-motion) | |
421 (make-local-variable 'term-pending-delete-marker) | |
422 (setq term-pending-delete-marker (make-marker)) | |
423 (make-local-variable 'term-current-face) | |
424 (setq term-current-face 'default) | |
425 (make-local-variable 'term-pending-frame) | |
426 (setq term-pending-frame nil) | |
427 (make-local-variable 'term-chars-mode) | |
428 (setq term-chars-mode nil) | |
429 (run-hooks 'term-mode-hook) | |
430 (if (term-is-xemacs) | |
431 (set-buffer-menubar | |
432 (append current-menubar (list term-terminal-menu)))) | |
433 (or term-input-ring | |
434 (setq term-input-ring (make-ring term-input-ring-size)))) | |
435 | |
436 (if term-mode-map | |
437 nil | |
438 (setq term-mode-map (make-sparse-keymap)) | |
439 (define-key term-mode-map "\ep" 'term-previous-input) | |
440 (define-key term-mode-map "\en" 'term-next-input) | |
441 (define-key term-mode-map "\er" 'term-previous-matching-input) | |
442 (define-key term-mode-map "\es" 'term-next-matching-input) | |
443 (if (term-is-xemacs) | |
444 t | |
445 (define-key term-mode-map [?\A-\M-r] 'term-previous-matching-input-from-input) | |
446 (define-key term-mode-map [?\A-\M-s] 'term-next-matching-input-from-input)) | |
447 (define-key term-mode-map "\e\C-l" 'term-show-output) | |
448 (define-key term-mode-map "\C-m" 'term-send-input) | |
449 (define-key term-mode-map "\C-d" 'term-delchar-or-maybe-eof) | |
450 (define-key term-mode-map "\C-c\C-a" 'term-bol) | |
451 (define-key term-mode-map "\C-c\C-u" 'term-kill-input) | |
452 (define-key term-mode-map "\C-c\C-w" 'backward-kill-word) | |
453 (define-key term-mode-map "\C-c\C-c" 'term-interrupt-subjob) | |
454 (define-key term-mode-map "\C-c\C-z" 'term-stop-subjob) | |
455 (define-key term-mode-map "\C-c\C-\\" 'term-quit-subjob) | |
456 (define-key term-mode-map "\C-c\C-m" 'term-copy-old-input) | |
457 (define-key term-mode-map "\C-c\C-o" 'term-kill-output) | |
458 (define-key term-mode-map "\C-c\C-r" 'term-show-output) | |
459 (define-key term-mode-map "\C-c\C-e" 'term-show-maximum-output) | |
460 (define-key term-mode-map "\C-c\C-l" 'term-dynamic-list-input-ring) | |
461 (define-key term-mode-map "\C-c\C-n" 'term-next-prompt) | |
462 (define-key term-mode-map "\C-c\C-p" 'term-previous-prompt) | |
463 (define-key term-mode-map "\C-c\C-d" 'term-send-eof) | |
464 (define-key term-mode-map "\C-cc" 'term-char-mode) | |
465 (define-key term-mode-map "\C-cp" 'term-pager-enable) | |
466 (define-key term-mode-map "\C-cD" 'term-pager-disable) | |
467 | |
468 (copy-face 'default 'term-underline-face) | |
469 (set-face-underline-p 'term-underline-face t) | |
470 | |
471 ; ;; completion: | |
472 ; (define-key term-mode-map [menu-bar completion] | |
473 ; (cons "Complete" (make-sparse-keymap "Complete"))) | |
474 ; (define-key term-mode-map [menu-bar completion complete-expand] | |
475 ; '("Expand File Name" . term-replace-by-expanded-filename)) | |
476 ; (define-key term-mode-map [menu-bar completion complete-listing] | |
477 ; '("File Completion Listing" . term-dynamic-list-filename-completions)) | |
478 ; (define-key term-mode-map [menu-bar completion complete-file] | |
479 ; '("Complete File Name" . term-dynamic-complete-filename)) | |
480 ; (define-key term-mode-map [menu-bar completion complete] | |
481 ; '("Complete Before Point" . term-dynamic-complete)) | |
482 ; ;; Put them in the menu bar: | |
483 ; (setq menu-bar-final-items (append '(terminal completion inout signals) | |
484 ; menu-bar-final-items)) | |
485 ) | |
486 | |
487 ;; Menu bars: | |
488 (if (and (not (boundp 'term-terminal-menu)) | |
489 (term-is-emacs19) (not (term-is-xemacs))) | |
490 (progn | |
491 ;; terminal: | |
492 (defvar term-terminal-menu (make-sparse-keymap "Terminal")) | |
493 (define-key term-mode-map [menu-bar terminal] | |
494 (cons "Terminal" term-terminal-menu)) | |
495 (define-key term-terminal-menu [terminal-char-mode] | |
496 '("Character mode" . term-char-mode)) | |
497 (define-key term-terminal-menu [terminal-line-mode] | |
498 '("Line mode" . term-line-mode)) | |
499 (define-key term-terminal-menu [terminal-pager-enable] | |
500 '("Enable paging" . term-pager-enable)) | |
501 (define-key term-terminal-menu [terminal-pager-disable] | |
502 '("Disable paging" . term-pager-disable)) | |
503 | |
504 ;; completion: (line mode only) | |
505 (defvar term-completion-menu (make-sparse-keymap "Complete")) | |
506 (define-key term-mode-map [menu-bar completion] | |
507 (cons "Complete" term-completion-menu)) | |
508 (define-key term-completion-menu [complete-expand] | |
509 '("Expand File Name" . term-replace-by-expanded-filename)) | |
510 (define-key term-completion-menu [complete-listing] | |
511 '("File Completion Listing" . term-dynamic-list-filename-completions)) | |
512 (define-key term-completion-menu [menu-bar completion complete-file] | |
513 '("Complete File Name" . term-dynamic-complete-filename)) | |
514 (define-key term-completion-menu [menu-bar completion complete] | |
515 '("Complete Before Point" . term-dynamic-complete)) | |
516 | |
517 ;; Input history: (line mode only) | |
518 (defvar term-inout-menu (make-sparse-keymap "In/Out")) | |
519 (define-key term-mode-map [menu-bar inout] | |
520 (cons "In/Out" term-inout-menu)) | |
521 (define-key term-inout-menu [kill-output] | |
522 '("Kill Current Output Group" . term-kill-output)) | |
523 (define-key term-inout-menu [next-prompt] | |
524 '("Forward Output Group" . term-next-prompt)) | |
525 (define-key term-inout-menu [previous-prompt] | |
526 '("Backward Output Group" . term-previous-prompt)) | |
527 (define-key term-inout-menu [show-maximum-output] | |
528 '("Show Maximum Output" . term-show-maximum-output)) | |
529 (define-key term-inout-menu [show-output] | |
530 '("Show Current Output Group" . term-show-output)) | |
531 (define-key term-inout-menu [kill-input] | |
532 '("Kill Current Input" . term-kill-input)) | |
533 (define-key term-inout-menu [copy-input] | |
534 '("Copy Old Input" . term-copy-old-input)) | |
535 (define-key term-inout-menu [forward-matching-history] | |
536 '("Forward Matching Input..." . term-forward-matching-input)) | |
537 (define-key term-inout-menu [backward-matching-history] | |
538 '("Backward Matching Input..." . term-backward-matching-input)) | |
539 (define-key term-inout-menu [next-matching-history] | |
540 '("Next Matching Input..." . term-next-matching-input)) | |
541 (define-key term-inout-menu [previous-matching-history] | |
542 '("Previous Matching Input..." . term-previous-matching-input)) | |
543 (define-key term-inout-menu [next-matching-history-from-input] | |
544 '("Next Matching Current Input" . term-next-matching-input-from-input)) | |
545 (define-key term-inout-menu [previous-matching-history-from-input] | |
546 '("Previous Matching Current Input" . term-previous-matching-input-from-input)) | |
547 (define-key term-inout-menu [next-history] | |
548 '("Next Input" . term-next-input)) | |
549 (define-key term-inout-menu [previous-history] | |
550 '("Previous Input" . term-previous-input)) | |
551 (define-key term-inout-menu [list-history] | |
552 '("List Input History" . term-dynamic-list-input-ring)) | |
553 (define-key term-inout-menu [expand-history] | |
554 '("Expand History Before Point" . term-replace-by-expanded-history)) | |
555 | |
556 ;; Signals | |
557 (defvar term-signals-menu (make-sparse-keymap "Signals")) | |
558 (define-key term-mode-map [menu-bar signals] | |
559 (cons "Signals" term-signals-menu)) | |
560 (define-key term-signals-menu [eof] '("EOF" . term-send-eof)) | |
561 (define-key term-signals-menu [kill] '("KILL" . term-kill-subjob)) | |
562 (define-key term-signals-menu [quit] '("QUIT" . term-quit-subjob)) | |
563 (define-key term-signals-menu [cont] '("CONT" . term-continue-subjob)) | |
564 (define-key term-signals-menu [stop] '("STOP" . term-stop-subjob)) | |
565 (define-key term-signals-menu [] '("BREAK" . term-interrupt-subjob)) | |
566 )) | |
567 | |
568 (defun term-reset-size (height width) | |
569 (setq term-height height) | |
570 (setq term-width width) | |
571 (setq term-start-line-column nil) | |
572 (setq term-current-row nil) | |
573 (setq term-current-column nil) | |
574 (term-scroll-region 0 height)) | |
575 | |
576 ;; Recursive routine used to check if any string in term-kill-echo-list | |
577 ;; matches part of the buffer before point. | |
578 ;; If so, delete that matched part of the buffer - this suppresses echo. | |
579 ;; Also, remove that string from the term-kill-echo-list. | |
580 ;; We *also* remove any older string on the list, as a sanity measure, | |
581 ;; in case something gets out of sync. (Except for type-ahead, there | |
582 ;; should only be one element in the list.) | |
583 | |
584 (defun term-check-kill-echo-list () | |
585 (let ((cur term-kill-echo-list) (found nil) (save-point (point))) | |
586 (unwind-protect | |
587 (progn | |
588 (end-of-line) | |
589 (while cur | |
590 (let* ((str (car cur)) (len (length str)) (start (- (point) len))) | |
591 (if (and (>= start (point-min)) | |
592 (string= str (buffer-substring start (point)))) | |
593 (progn (delete-backward-char len) | |
594 (setq term-kill-echo-list (cdr cur)) | |
595 (setq term-current-column nil) | |
596 (setq term-current-row nil) | |
597 (setq term-start-line-column nil) | |
598 (setq cur nil found t)) | |
599 (setq cur (cdr cur)))))) | |
600 (if (not found) | |
601 (goto-char save-point))) | |
602 found)) | |
603 | |
604 (defun term-check-size (process) | |
605 (if (or (/= term-height (1- (window-height))) | |
606 (/= term-width (1- (window-width)))) | |
607 (progn | |
608 (term-reset-size (1- (window-height)) (1- (window-width))) | |
609 (set-process-window-size process term-height term-width)))) | |
610 | |
611 (defun term-send-raw-string (chars) | |
612 (let ((proc (get-buffer-process (current-buffer)))) | |
613 (if (not proc) | |
614 (error "Current buffer has no process") | |
615 ;; Note that (term-current-row) must be called *after* | |
616 ;; (point) has been updated to (process-mark proc). | |
617 (goto-char (process-mark proc)) | |
618 (if term-pager-count | |
619 (setq term-pager-count (term-current-row))) | |
620 (send-string proc chars)))) | |
621 | |
622 (defun term-send-raw () | |
623 "Send the last character typed through the terminal-emulator | |
624 without any interpretation." | |
625 (interactive) | |
626 ;; Convert `return' to C-m, etc. | |
627 (if (and (symbolp last-input-char) | |
628 (get last-input-char 'ascii-character)) | |
629 (setq last-input-char (get last-input-char 'ascii-character))) | |
630 (term-send-raw-string (make-string 1 last-input-char))) | |
631 | |
632 (defun term-send-raw-meta () | |
633 (interactive) | |
634 ;; Convert `return' to C-m, etc. | |
635 (if (and (symbolp last-input-char) | |
636 (get last-input-char 'ascii-character)) | |
637 (setq last-input-char (get last-input-char 'ascii-character))) | |
638 (term-send-raw-string (if (> last-input-char 127) | |
639 (make-string 1 last-input-char) | |
640 (format "\e%c" last-input-char)))) | |
641 | |
642 (defun term-mouse-paste (click arg) | |
643 "Insert the last stretch of killed text at the position clicked on." | |
644 (interactive "e\nP") | |
645 (mouse-set-point click) | |
646 (setq this-command 'yank) | |
647 (term-send-raw-string (current-kill (cond | |
648 ((listp arg) 0) | |
649 ((eq arg '-) -1) | |
650 (t (1- arg)))))) | |
651 | |
652 ;; Which would be better: "\e[A" or "\eOA"? readline accepts either. | |
653 (defun term-send-up () (interactive) (term-send-raw-string "\e[A")) | |
654 (defun term-send-down () (interactive) (term-send-raw-string "\e[B")) | |
655 (defun term-send-right () (interactive) (term-send-raw-string "\e[C")) | |
656 (defun term-send-left () (interactive) (term-send-raw-string "\e[D")) | |
657 | |
658 (defun term-set-escape-char (c) | |
659 (if term-escape-char | |
660 (define-key term-raw-map term-escape-char 'term-send-raw)) | |
661 (setq c (make-string 1 c)) | |
662 (define-key term-raw-map c term-raw-escape-map) | |
663 ;; Define standard binings in term-raw-escape-map | |
664 (define-key term-raw-escape-map "\C-x" | |
665 (lookup-key (current-global-map) "\C-x")) | |
666 (define-key term-raw-escape-map "\C-v" | |
667 (lookup-key (current-global-map) "\C-v")) | |
668 (define-key term-raw-escape-map "\C-u" | |
669 (lookup-key (current-global-map) "\C-u")) | |
670 (define-key term-raw-escape-map c 'term-send-raw) | |
671 (define-key term-raw-escape-map "p" 'term-pager-enable) | |
672 (define-key term-raw-escape-map "D" 'term-pager-disable) | |
673 (define-key term-raw-escape-map "l" 'term-line-mode)) | |
674 | |
675 (defun term-char-mode () | |
676 "Start using raw keyboard mode to send each character | |
677 to inferior process until a key bound to term-line-mode is encountered." | |
678 (interactive) | |
679 (if (not term-raw-map) | |
680 (let* ((map (make-keymap)) | |
681 (esc-map (make-keymap)) | |
682 (i 0)) | |
683 (while (< i 128) | |
684 (define-key map (make-string 1 i) 'term-send-raw) | |
685 (define-key esc-map (make-string 1 i) 'term-send-raw-meta) | |
686 (setq i (1+ i))) | |
687 (define-key map "\e" esc-map) | |
688 (setq term-raw-map map) | |
689 (setq term-raw-escape-map | |
690 (copy-keymap (lookup-key (current-global-map) "\C-x"))) | |
691 (if (term-is-emacs19) | |
692 (progn | |
693 (if (term-is-xemacs) | |
694 (define-key term-raw-map [(button2)] 'term-mouse-paste) | |
695 (progn | |
696 (define-key term-raw-map [mouse-2] 'term-mouse-paste) | |
697 (define-key term-raw-map [menu-bar terminal] | |
698 (cons "Terminal" term-terminal-menu)) | |
699 (define-key term-raw-map [menu-bar signals] | |
700 (cons "Signals" term-signals-menu)) )) | |
701 (define-key term-raw-map [up] 'term-send-up) | |
702 (define-key term-raw-map [down] 'term-send-down) | |
703 (define-key term-raw-map [right] 'term-send-right) | |
704 (define-key term-raw-map [left] 'term-send-left))) | |
705 (term-set-escape-char ?\C-c))) | |
706 ;; FIXME: Emit message? Cfr ilisp-raw-message | |
707 (setq term-old-mode-map (current-local-map)) | |
708 (use-local-map term-raw-map) | |
709 | |
710 ;; Send existing partial line to inferior (without newline). | |
711 (let ((pmark (process-mark (get-buffer-process (current-buffer)))) | |
712 (save-input-sender term-input-sender)) | |
713 (if (> (point) pmark) | |
714 (unwind-protect | |
715 (progn | |
716 (setq term-input-sender (symbol-function 'term-send-string)) | |
717 (end-of-line) | |
718 (term-send-input)) | |
719 (setq term-input-sender save-input-sender)))) | |
720 | |
721 (setq mode-line-process '(": char %s")) | |
722 (set-buffer-modified-p (buffer-modified-p))) ;;No-op, but updates mode line. | |
723 | |
724 (defun term-line-mode () | |
725 (interactive) | |
726 (use-local-map term-old-mode-map) | |
727 (setq mode-line-process '(": line %s")) | |
728 (set-buffer-modified-p (buffer-modified-p))) ;;No-op, but updates mode line. | |
729 | |
730 (defun term-check-proc (buffer) | |
731 "True if there is a process associated w/buffer BUFFER, and | |
732 it is alive (status RUN or STOP). BUFFER can be either a buffer or the | |
733 name of one" | |
734 (let ((proc (get-buffer-process buffer))) | |
735 (and proc (memq (process-status proc) '(run stop))))) | |
736 | |
737 ;;; Note that this guy, unlike shell.el's make-shell, barfs if you pass it () | |
738 ;;; for the second argument (program). | |
739 ;;;###autoload | |
740 (defun make-term (name program &optional startfile &rest switches) | |
741 "Make a term process NAME in a buffer, running PROGRAM. | |
742 The name of the buffer is made by surrounding NAME with `*'s. | |
743 If there is already a running process in that buffer, it is not restarted. | |
744 Optional third arg STARTFILE is the name of a file to send the contents of to | |
745 the process. Any more args are arguments to PROGRAM." | |
746 (let ((buffer (get-buffer-create (concat "*" name "*")))) | |
747 ;; If no process, or nuked process, crank up a new one and put buffer in | |
748 ;; term mode. Otherwise, leave buffer and existing process alone. | |
749 (cond ((not (term-check-proc buffer)) | |
750 (save-excursion | |
751 (set-buffer buffer) | |
752 (term-mode)) ; Install local vars, mode, keymap, ... | |
753 (term-exec buffer name program startfile switches))) | |
754 buffer)) | |
755 | |
756 ;;;###autoload | |
757 (defun term (program) | |
758 "Start a terminal-emulator in a new buffer." | |
759 (interactive (list (read-from-minibuffer "Run program: " | |
760 (or explicit-shell-file-name | |
761 (getenv "ESHELL") | |
762 (getenv "SHELL") | |
763 "/bin/sh")))) | |
764 (set-buffer (make-term "terminal" program)) | |
765 (term-mode) | |
766 (term-char-mode) | |
767 (switch-to-buffer "*terminal*")) | |
768 | |
769 (defun term-exec (buffer name command startfile switches) | |
770 "Start up a process in buffer for term modes. | |
771 Blasts any old process running in the buffer. Doesn't set the buffer mode. | |
772 You can use this to cheaply run a series of processes in the same term | |
773 buffer. The hook term-exec-hook is run after each exec." | |
774 (save-excursion | |
775 (set-buffer buffer) | |
776 (let ((proc (get-buffer-process buffer))) ; Blast any old process. | |
777 (if proc (delete-process proc))) | |
778 ;; Crank up a new process | |
779 (let ((proc (term-exec-1 name buffer command switches))) | |
780 (make-local-variable 'term-ptyp) | |
781 (setq term-ptyp process-connection-type) ; T if pty, NIL if pipe. | |
782 ;; Jump to the end, and set the process mark. | |
783 (goto-char (point-max)) | |
784 (set-marker (process-mark proc) (point)) | |
785 (set-process-filter proc 'term-emulate-terminal) | |
786 ;; Feed it the startfile. | |
787 (cond (startfile | |
788 ;;This is guaranteed to wait long enough | |
789 ;;but has bad results if the term does not prompt at all | |
790 ;; (while (= size (buffer-size)) | |
791 ;; (sleep-for 1)) | |
792 ;;I hope 1 second is enough! | |
793 (sleep-for 1) | |
794 (goto-char (point-max)) | |
795 (insert-file-contents startfile) | |
796 (setq startfile (buffer-substring (point) (point-max))) | |
797 (delete-region (point) (point-max)) | |
798 (term-send-string proc startfile))) | |
799 (run-hooks 'term-exec-hook) | |
800 buffer))) | |
801 | |
802 ;;; Name to use for TERM. | |
803 ;;; Using "emacs" loses, because bash disables editing if TERM == emacs. | |
804 (defvar term-term-name "eterm") | |
805 ; Format string, usage: (format term-termcap-string emacs-term-name "TERMCAP=" 24 80) | |
806 (defvar term-termcap-format | |
807 "%s%s:li#%d:co#%d:cl=\\E[;H\\E[2J:bs:am:xn:cm=\\E[%%i%%d;%%dH\ | |
808 :nd=\\E[C:up=\\E[A:ce=\\E[K:ho=\\E[H:pt\ | |
809 :al=\\E[L:dl=\\E[M:DL=\\E[%%dM:AL=\\E[%%dL:cs=\\E[%%i%%d;%%dr:sf=\\n\ | |
810 :te=\\E[2J\\E[?47l\\E8:ti=\\E7\\E[?47h\ | |
811 :dc=\\E[P:DC=\\E[%%dP:IC=\\E[%%d@:im=\\E[4h:ei=\E4l:mi:\ | |
812 :so=\\E[7m:se=\\E[m:us=\\E[4m:ue=\\E[m:md=\\E[1m:mr=\\E[7m:me=\\E[m\ | |
813 :UP=\\E[%%dA:DO=\\E[%%dB:LE=\\E[%%dD:RI=\\E[%%dC" | |
814 ;;; : -undefine ic | |
815 "termcap capabilties supported") | |
816 | |
817 ;;; This auxiliary function cranks up the process for term-exec in | |
818 ;;; the appropriate environment. | |
819 | |
820 (defun term-exec-1 (name buffer command switches) | |
821 ;; The 'if ...; then shift; fi' hack is because Bourne shell | |
822 ;; loses one arg when called with -c, and newer shells (bash, ksh) don't. | |
823 ;; Thus we add an extra dummy argument "..", and then remove it. | |
824 (apply 'start-process name buffer | |
9603
8792d17812fb
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
9509
diff
changeset
|
825 "/bin/sh" "-c" (format "stty -nl echo rows %d columns %d sane 2>/dev/null; if [ $1 = .. ]; then shift; fi;\ |
9509 | 826 TERM=$1; export TERM; shift;\ |
827 TERMCAP=$1; export TERMCAP; shift;\ | |
828 EMACS=t; export EMACS;\ | |
829 exec \"$@\"" term-height term-width) | |
830 ".." | |
831 term-term-name | |
832 (format term-termcap-format "" term-term-name term-height term-width) | |
833 command switches)) | |
834 | |
835 ;;; This should be in emacs, but it isn't. | |
836 (defun term-mem (item list &optional elt=) | |
837 "Test to see if ITEM is equal to an item in LIST. | |
838 Option comparison function ELT= defaults to equal." | |
839 (let ((elt= (or elt= (function equal))) | |
840 (done nil)) | |
841 (while (and list (not done)) | |
842 (if (funcall elt= item (car list)) | |
843 (setq done list) | |
844 (setq list (cdr list)))) | |
845 done)) | |
846 | |
847 | |
848 ;;; Input history processing in a buffer | |
849 ;;; =========================================================================== | |
850 ;;; Useful input history functions, courtesy of the Ergo group. | |
851 | |
852 ;;; Eleven commands: | |
853 ;;; term-dynamic-list-input-ring List history in help buffer. | |
854 ;;; term-previous-input Previous input... | |
855 ;;; term-previous-matching-input ...matching a string. | |
856 ;;; term-previous-matching-input-from-input ... matching the current input. | |
857 ;;; term-next-input Next input... | |
858 ;;; term-next-matching-input ...matching a string. | |
859 ;;; term-next-matching-input-from-input ... matching the current input. | |
860 ;;; term-backward-matching-input Backwards input... | |
861 ;;; term-forward-matching-input ...matching a string. | |
862 ;;; term-replace-by-expanded-history Expand history at point; | |
863 ;;; replace with expanded history. | |
864 ;;; term-magic-space Expand history and insert space. | |
865 ;;; | |
866 ;;; Three functions: | |
867 ;;; term-read-input-ring Read into term-input-ring... | |
868 ;;; term-write-input-ring Write to term-input-ring-file-name. | |
869 ;;; term-replace-by-expanded-history-before-point Workhorse function. | |
870 | |
871 (defun term-read-input-ring (&optional silent) | |
872 "Sets the buffer's `term-input-ring' from a history file. | |
873 The name of the file is given by the variable `term-input-ring-file-name'. | |
874 The history ring is of size `term-input-ring-size', regardless of file size. | |
875 If `term-input-ring-file-name' is nil this function does nothing. | |
876 | |
877 If the optional argument SILENT is non-nil, we say nothing about a | |
878 failure to read the history file. | |
879 | |
880 This function is useful for major mode commands and mode hooks. | |
881 | |
882 The structure of the history file should be one input command per line, | |
883 with the most recent command last. | |
884 See also `term-input-ignoredups' and `term-write-input-ring'." | |
885 (cond ((or (null term-input-ring-file-name) | |
886 (equal term-input-ring-file-name "")) | |
887 nil) | |
888 ((not (file-readable-p term-input-ring-file-name)) | |
889 (or silent | |
890 (message "Cannot read history file %s" | |
891 term-input-ring-file-name))) | |
892 (t | |
893 (let ((history-buf (get-buffer-create " *temp*")) | |
894 (file term-input-ring-file-name) | |
895 (count 0) | |
896 (ring (make-ring term-input-ring-size))) | |
897 (unwind-protect | |
898 (save-excursion | |
899 (set-buffer history-buf) | |
900 (widen) | |
901 (erase-buffer) | |
902 (insert-file-contents file) | |
903 ;; Save restriction in case file is already visited... | |
904 ;; Watch for those date stamps in history files! | |
905 (goto-char (point-max)) | |
906 (while (and (< count term-input-ring-size) | |
907 (re-search-backward "^[ \t]*\\([^#\n].*\\)[ \t]*$" | |
908 nil t)) | |
909 (let ((history (buffer-substring (match-beginning 1) | |
910 (match-end 1)))) | |
911 (if (or (null term-input-ignoredups) | |
912 (ring-empty-p ring) | |
913 (not (string-equal (ring-ref ring 0) history))) | |
914 (ring-insert-at-beginning ring history))) | |
915 (setq count (1+ count)))) | |
916 (kill-buffer history-buf)) | |
917 (setq term-input-ring ring | |
918 term-input-ring-index nil))))) | |
919 | |
920 (defun term-write-input-ring () | |
921 "Writes the buffer's `term-input-ring' to a history file. | |
922 The name of the file is given by the variable `term-input-ring-file-name'. | |
923 The original contents of the file are lost if `term-input-ring' is not empty. | |
924 If `term-input-ring-file-name' is nil this function does nothing. | |
925 | |
926 Useful within process sentinels. | |
927 | |
928 See also `term-read-input-ring'." | |
929 (cond ((or (null term-input-ring-file-name) | |
930 (equal term-input-ring-file-name "") | |
931 (null term-input-ring) (ring-empty-p term-input-ring)) | |
932 nil) | |
933 ((not (file-writable-p term-input-ring-file-name)) | |
934 (message "Cannot write history file %s" term-input-ring-file-name)) | |
935 (t | |
936 (let* ((history-buf (get-buffer-create " *Temp Input History*")) | |
937 (ring term-input-ring) | |
938 (file term-input-ring-file-name) | |
939 (index (ring-length ring))) | |
940 ;; Write it all out into a buffer first. Much faster, but messier, | |
941 ;; than writing it one line at a time. | |
942 (save-excursion | |
943 (set-buffer history-buf) | |
944 (erase-buffer) | |
945 (while (> index 0) | |
946 (setq index (1- index)) | |
947 (insert (ring-ref ring index) ?\n)) | |
948 (write-region (buffer-string) nil file nil 'no-message) | |
949 (kill-buffer nil)))))) | |
950 | |
951 | |
952 (defun term-dynamic-list-input-ring () | |
953 "List in help buffer the buffer's input history." | |
954 (interactive) | |
955 (if (or (not (ring-p term-input-ring)) | |
956 (ring-empty-p term-input-ring)) | |
957 (message "No history") | |
958 (let ((history nil) | |
959 (history-buffer " *Input History*") | |
960 (index (1- (ring-length term-input-ring))) | |
961 (conf (current-window-configuration))) | |
962 ;; We have to build up a list ourselves from the ring vector. | |
963 (while (>= index 0) | |
964 (setq history (cons (ring-ref term-input-ring index) history) | |
965 index (1- index))) | |
966 ;; Change "completion" to "history reference" | |
967 ;; to make the display accurate. | |
968 (with-output-to-temp-buffer history-buffer | |
969 (display-completion-list history) | |
970 (set-buffer history-buffer) | |
971 (forward-line 3) | |
972 (while (search-backward "completion" nil 'move) | |
973 (replace-match "history reference"))) | |
974 (sit-for 0) | |
975 (message "Hit space to flush") | |
976 (let ((ch (read-event))) | |
977 (if (eq ch ?\ ) | |
978 (set-window-configuration conf) | |
979 (setq unread-command-events (list ch))))))) | |
980 | |
981 | |
982 (defun term-regexp-arg (prompt) | |
983 ;; Return list of regexp and prefix arg using PROMPT. | |
984 (let* ((minibuffer-history-sexp-flag nil) | |
985 ;; Don't clobber this. | |
986 (last-command last-command) | |
987 (regexp (read-from-minibuffer prompt nil nil nil | |
988 'minibuffer-history-search-history))) | |
989 (list (if (string-equal regexp "") | |
990 (setcar minibuffer-history-search-history | |
991 (nth 1 minibuffer-history-search-history)) | |
992 regexp) | |
993 (prefix-numeric-value current-prefix-arg)))) | |
994 | |
995 (defun term-search-arg (arg) | |
996 ;; First make sure there is a ring and that we are after the process mark | |
997 (cond ((not (term-after-pmark-p)) | |
998 (error "Not at command line")) | |
999 ((or (null term-input-ring) | |
1000 (ring-empty-p term-input-ring)) | |
1001 (error "Empty input ring")) | |
1002 ((zerop arg) | |
1003 ;; arg of zero resets search from beginning, and uses arg of 1 | |
1004 (setq term-input-ring-index nil) | |
1005 1) | |
1006 (t | |
1007 arg))) | |
1008 | |
1009 (defun term-search-start (arg) | |
1010 ;; Index to start a directional search, starting at term-input-ring-index | |
1011 (if term-input-ring-index | |
1012 ;; If a search is running, offset by 1 in direction of arg | |
1013 (mod (+ term-input-ring-index (if (> arg 0) 1 -1)) | |
1014 (ring-length term-input-ring)) | |
1015 ;; For a new search, start from beginning or end, as appropriate | |
1016 (if (>= arg 0) | |
1017 0 ; First elt for forward search | |
1018 (1- (ring-length term-input-ring))))) ; Last elt for backward search | |
1019 | |
1020 (defun term-previous-input-string (arg) | |
1021 "Return the string ARG places along the input ring. | |
1022 Moves relative to `term-input-ring-index'." | |
1023 (ring-ref term-input-ring (if term-input-ring-index | |
1024 (mod (+ arg term-input-ring-index) | |
1025 (ring-length term-input-ring)) | |
1026 arg))) | |
1027 | |
1028 (defun term-previous-input (arg) | |
1029 "Cycle backwards through input history." | |
1030 (interactive "*p") | |
1031 (term-previous-matching-input "." arg)) | |
1032 | |
1033 (defun term-next-input (arg) | |
1034 "Cycle forwards through input history." | |
1035 (interactive "*p") | |
1036 (term-previous-input (- arg))) | |
1037 | |
1038 (defun term-previous-matching-input-string (regexp arg) | |
1039 "Return the string matching REGEXP ARG places along the input ring. | |
1040 Moves relative to `term-input-ring-index'." | |
1041 (let* ((pos (term-previous-matching-input-string-position regexp arg))) | |
1042 (if pos (ring-ref term-input-ring pos)))) | |
1043 | |
1044 (defun term-previous-matching-input-string-position (regexp arg &optional start) | |
1045 "Return the index matching REGEXP ARG places along the input ring. | |
1046 Moves relative to START, or `term-input-ring-index'." | |
1047 (if (or (not (ring-p term-input-ring)) | |
1048 (ring-empty-p term-input-ring)) | |
1049 (error "No history")) | |
1050 (let* ((len (ring-length term-input-ring)) | |
1051 (motion (if (> arg 0) 1 -1)) | |
1052 (n (mod (- (or start (term-search-start arg)) motion) len)) | |
1053 (tried-each-ring-item nil) | |
1054 (prev nil)) | |
1055 ;; Do the whole search as many times as the argument says. | |
1056 (while (and (/= arg 0) (not tried-each-ring-item)) | |
1057 ;; Step once. | |
1058 (setq prev n | |
1059 n (mod (+ n motion) len)) | |
1060 ;; If we haven't reached a match, step some more. | |
1061 (while (and (< n len) (not tried-each-ring-item) | |
1062 (not (string-match regexp (ring-ref term-input-ring n)))) | |
1063 (setq n (mod (+ n motion) len) | |
1064 ;; If we have gone all the way around in this search. | |
1065 tried-each-ring-item (= n prev))) | |
1066 (setq arg (if (> arg 0) (1- arg) (1+ arg)))) | |
1067 ;; Now that we know which ring element to use, if we found it, return that. | |
1068 (if (string-match regexp (ring-ref term-input-ring n)) | |
1069 n))) | |
1070 | |
1071 (defun term-previous-matching-input (regexp arg) | |
1072 "Search backwards through input history for match for REGEXP. | |
1073 \(Previous history elements are earlier commands.) | |
1074 With prefix argument N, search for Nth previous match. | |
1075 If N is negative, find the next or Nth next match." | |
1076 (interactive (term-regexp-arg "Previous input matching (regexp): ")) | |
1077 (setq arg (term-search-arg arg)) | |
1078 (let ((pos (term-previous-matching-input-string-position regexp arg))) | |
1079 ;; Has a match been found? | |
1080 (if (null pos) | |
1081 (error "Not found") | |
1082 (setq term-input-ring-index pos) | |
1083 (message "History item: %d" (1+ pos)) | |
1084 (delete-region | |
1085 ;; Can't use kill-region as it sets this-command | |
1086 (process-mark (get-buffer-process (current-buffer))) (point)) | |
1087 (insert (ring-ref term-input-ring pos))))) | |
1088 | |
1089 (defun term-next-matching-input (regexp arg) | |
1090 "Search forwards through input history for match for REGEXP. | |
1091 \(Later history elements are more recent commands.) | |
1092 With prefix argument N, search for Nth following match. | |
1093 If N is negative, find the previous or Nth previous match." | |
1094 (interactive (term-regexp-arg "Next input matching (regexp): ")) | |
1095 (term-previous-matching-input regexp (- arg))) | |
1096 | |
1097 (defun term-previous-matching-input-from-input (arg) | |
1098 "Search backwards through input history for match for current input. | |
1099 \(Previous history elements are earlier commands.) | |
1100 With prefix argument N, search for Nth previous match. | |
1101 If N is negative, search forwards for the -Nth following match." | |
1102 (interactive "p") | |
1103 (if (not (memq last-command '(term-previous-matching-input-from-input | |
1104 term-next-matching-input-from-input))) | |
1105 ;; Starting a new search | |
1106 (setq term-matching-input-from-input-string | |
1107 (buffer-substring | |
1108 (process-mark (get-buffer-process (current-buffer))) | |
1109 (point)) | |
1110 term-input-ring-index nil)) | |
1111 (term-previous-matching-input | |
1112 (concat "^" (regexp-quote term-matching-input-from-input-string)) | |
1113 arg)) | |
1114 | |
1115 (defun term-next-matching-input-from-input (arg) | |
1116 "Search forwards through input history for match for current input. | |
1117 \(Following history elements are more recent commands.) | |
1118 With prefix argument N, search for Nth following match. | |
1119 If N is negative, search backwards for the -Nth previous match." | |
1120 (interactive "p") | |
1121 (term-previous-matching-input-from-input (- arg))) | |
1122 | |
1123 | |
1124 (defun term-replace-by-expanded-history (&optional silent) | |
1125 "Expand input command history references before point. | |
1126 Expansion is dependent on the value of `term-input-autoexpand'. | |
1127 | |
1128 This function depends on the buffer's idea of the input history, which may not | |
1129 match the command interpreter's idea, assuming it has one. | |
1130 | |
1131 Assumes history syntax is like typical Un*x shells'. However, since emacs | |
1132 cannot know the interpreter's idea of input line numbers, assuming it has one, | |
1133 it cannot expand absolute input line number references. | |
1134 | |
1135 If the optional argument SILENT is non-nil, never complain | |
1136 even if history reference seems erroneous. | |
1137 | |
1138 See `term-magic-space' and `term-replace-by-expanded-history-before-point'. | |
1139 | |
1140 Returns t if successful." | |
1141 (interactive) | |
1142 (if (and term-input-autoexpand | |
1143 (string-match "[!^]" (funcall term-get-old-input)) | |
1144 (save-excursion (beginning-of-line) | |
1145 (looking-at term-prompt-regexp))) | |
1146 ;; Looks like there might be history references in the command. | |
1147 (let ((previous-modified-tick (buffer-modified-tick))) | |
1148 (message "Expanding history references...") | |
1149 (term-replace-by-expanded-history-before-point silent) | |
1150 (/= previous-modified-tick (buffer-modified-tick))))) | |
1151 | |
1152 | |
1153 (defun term-replace-by-expanded-history-before-point (silent) | |
1154 "Expand directory stack reference before point. | |
1155 See `term-replace-by-expanded-history'. Returns t if successful." | |
1156 (save-excursion | |
1157 (let ((toend (- (save-excursion (end-of-line nil) (point)) (point))) | |
1158 (start (progn (term-bol nil) (point)))) | |
1159 (while (progn | |
1160 (skip-chars-forward "^!^" | |
1161 (save-excursion | |
1162 (end-of-line nil) (- (point) toend))) | |
1163 (< (point) | |
1164 (save-excursion | |
1165 (end-of-line nil) (- (point) toend)))) | |
1166 ;; This seems a bit complex. We look for references such as !!, !-num, | |
1167 ;; !foo, !?foo, !{bar}, !?{bar}, ^oh, ^my^, ^god^it, ^never^ends^. | |
1168 ;; If that wasn't enough, the plings can be suffixed with argument | |
1169 ;; range specifiers. | |
1170 ;; Argument ranges are complex too, so we hive off the input line, | |
1171 ;; referenced with plings, with the range string to `term-args'. | |
1172 (setq term-input-ring-index nil) | |
1173 (cond ((or (= (preceding-char) ?\\) | |
1174 (term-within-quotes start (point))) | |
1175 ;; The history is quoted, or we're in quotes. | |
1176 (goto-char (1+ (point)))) | |
1177 ((looking-at "![0-9]+\\($\\|[^-]\\)") | |
1178 ;; We cannot know the interpreter's idea of input line numbers. | |
1179 (goto-char (match-end 0)) | |
1180 (message "Absolute reference cannot be expanded")) | |
1181 ((looking-at "!-\\([0-9]+\\)\\(:?[0-9^$*-]+\\)?") | |
1182 ;; Just a number of args from `number' lines backward. | |
1183 (let ((number (1- (string-to-number | |
1184 (buffer-substring (match-beginning 1) | |
1185 (match-end 1)))))) | |
1186 (if (<= number (ring-length term-input-ring)) | |
1187 (progn | |
1188 (replace-match | |
1189 (term-args (term-previous-input-string number) | |
1190 (match-beginning 2) (match-end 2)) | |
1191 t t) | |
1192 (setq term-input-ring-index number) | |
1193 (message "History item: %d" (1+ number))) | |
1194 (goto-char (match-end 0)) | |
1195 (message "Relative reference exceeds input history size")))) | |
1196 ((or (looking-at "!!?:?\\([0-9^$*-]+\\)") (looking-at "!!")) | |
1197 ;; Just a number of args from the previous input line. | |
1198 (replace-match | |
1199 (term-args (term-previous-input-string 0) | |
1200 (match-beginning 1) (match-end 1)) | |
1201 t t) | |
1202 (message "History item: previous")) | |
1203 ((looking-at | |
1204 "!\\??\\({\\(.+\\)}\\|\\(\\sw+\\)\\)\\(:?[0-9^$*-]+\\)?") | |
1205 ;; Most recent input starting with or containing (possibly | |
1206 ;; protected) string, maybe just a number of args. Phew. | |
1207 (let* ((mb1 (match-beginning 1)) (me1 (match-end 1)) | |
1208 (mb2 (match-beginning 2)) (me2 (match-end 2)) | |
1209 (exp (buffer-substring (or mb2 mb1) (or me2 me1))) | |
1210 (pref (if (save-match-data (looking-at "!\\?")) "" "^")) | |
1211 (pos (save-match-data | |
1212 (term-previous-matching-input-string-position | |
1213 (concat pref (regexp-quote exp)) 1)))) | |
1214 (if (null pos) | |
1215 (progn | |
1216 (goto-char (match-end 0)) | |
1217 (or silent | |
1218 (progn (message "Not found") | |
1219 (ding)))) | |
1220 (setq term-input-ring-index pos) | |
1221 (replace-match | |
1222 (term-args (ring-ref term-input-ring pos) | |
1223 (match-beginning 4) (match-end 4)) | |
1224 t t) | |
1225 (message "History item: %d" (1+ pos))))) | |
1226 ((looking-at "\\^\\([^^]+\\)\\^?\\([^^]*\\)\\^?") | |
1227 ;; Quick substitution on the previous input line. | |
1228 (let ((old (buffer-substring (match-beginning 1) (match-end 1))) | |
1229 (new (buffer-substring (match-beginning 2) (match-end 2))) | |
1230 (pos nil)) | |
1231 (replace-match (term-previous-input-string 0) t t) | |
1232 (setq pos (point)) | |
1233 (goto-char (match-beginning 0)) | |
1234 (if (not (search-forward old pos t)) | |
1235 (or silent | |
1236 (error "Not found")) | |
1237 (replace-match new t t) | |
1238 (message "History item: substituted")))) | |
1239 (t | |
1240 (goto-char (match-end 0)))))))) | |
1241 | |
1242 | |
1243 (defun term-magic-space (arg) | |
1244 "Expand input history references before point and insert ARG spaces. | |
1245 A useful command to bind to SPC. See `term-replace-by-expanded-history'." | |
1246 (interactive "p") | |
1247 (term-replace-by-expanded-history) | |
1248 (self-insert-command arg)) | |
1249 | |
1250 (defun term-within-quotes (beg end) | |
1251 "Return t if the number of quotes between BEG and END is odd. | |
1252 Quotes are single and double." | |
1253 (let ((countsq (term-how-many-region "\\(^\\|[^\\\\]\\)\'" beg end)) | |
1254 (countdq (term-how-many-region "\\(^\\|[^\\\\]\\)\"" beg end))) | |
1255 (or (= (mod countsq 2) 1) (= (mod countdq 2) 1)))) | |
1256 | |
1257 (defun term-how-many-region (regexp beg end) | |
1258 "Return number of matches for REGEXP from BEG to END." | |
1259 (let ((count 0)) | |
1260 (save-excursion | |
1261 (save-match-data | |
1262 (goto-char beg) | |
1263 (while (re-search-forward regexp end t) | |
1264 (setq count (1+ count))))) | |
1265 count)) | |
1266 | |
1267 (defun term-args (string begin end) | |
1268 ;; From STRING, return the args depending on the range specified in the text | |
1269 ;; from BEGIN to END. If BEGIN is nil, assume all args. Ignore leading `:'. | |
1270 ;; Range can be x-y, x-, -y, where x/y can be [0-9], *, ^, $. | |
1271 (save-match-data | |
1272 (if (null begin) | |
1273 (term-arguments string 0 nil) | |
1274 (let* ((range (buffer-substring | |
1275 (if (eq (char-after begin) ?:) (1+ begin) begin) end)) | |
1276 (nth (cond ((string-match "^[*^]" range) 1) | |
1277 ((string-match "^-" range) 0) | |
1278 ((string-equal range "$") nil) | |
1279 (t (string-to-number range)))) | |
1280 (mth (cond ((string-match "[-*$]$" range) nil) | |
1281 ((string-match "-" range) | |
1282 (string-to-number (substring range (match-end 0)))) | |
1283 (t nth)))) | |
1284 (term-arguments string nth mth))))) | |
1285 | |
1286 ;; Return a list of arguments from ARG. Break it up at the | |
1287 ;; delimiters in term-delimiter-argument-list. Returned list is backwards. | |
1288 (defun term-delim-arg (arg) | |
1289 (if (null term-delimiter-argument-list) | |
1290 (list arg) | |
1291 (let ((args nil) | |
1292 (pos 0) | |
1293 (len (length arg))) | |
1294 (while (< pos len) | |
1295 (let ((char (aref arg pos)) | |
1296 (start pos)) | |
1297 (if (memq char term-delimiter-argument-list) | |
1298 (while (and (< pos len) (eq (aref arg pos) char)) | |
1299 (setq pos (1+ pos))) | |
1300 (while (and (< pos len) | |
1301 (not (memq (aref arg pos) | |
1302 term-delimiter-argument-list))) | |
1303 (setq pos (1+ pos)))) | |
1304 (setq args (cons (substring arg start pos) args)))) | |
1305 args))) | |
1306 | |
1307 (defun term-arguments (string nth mth) | |
1308 "Return from STRING the NTH to MTH arguments. | |
1309 NTH and/or MTH can be nil, which means the last argument. | |
1310 Returned arguments are separated by single spaces. | |
1311 We assume whitespace separates arguments, except within quotes. | |
1312 Also, a run of one or more of a single character | |
1313 in `term-delimiter-argument-list' is a separate argument. | |
1314 Argument 0 is the command name." | |
1315 (let ((argpart "[^ \n\t\"'`]+\\|\\(\"[^\"]*\"\\|'[^']*'\\|`[^`]*`\\)") | |
1316 (args ()) (pos 0) | |
1317 (count 0) | |
1318 beg str value quotes) | |
1319 ;; Build a list of all the args until we have as many as we want. | |
1320 (while (and (or (null mth) (<= count mth)) | |
1321 (string-match argpart string pos)) | |
1322 (if (and beg (= pos (match-beginning 0))) | |
1323 ;; It's contiguous, part of the same arg. | |
1324 (setq pos (match-end 0) | |
1325 quotes (or quotes (match-beginning 1))) | |
1326 ;; It's a new separate arg. | |
1327 (if beg | |
1328 ;; Put the previous arg, if there was one, onto ARGS. | |
1329 (setq str (substring string beg pos) | |
1330 args (if quotes (cons str args) | |
1331 (nconc (term-delim-arg str) args)) | |
1332 count (1+ count))) | |
1333 (setq quotes (match-beginning 1)) | |
1334 (setq beg (match-beginning 0)) | |
1335 (setq pos (match-end 0)))) | |
1336 (if beg | |
1337 (setq str (substring string beg pos) | |
1338 args (if quotes (cons str args) | |
1339 (nconc (term-delim-arg str) args)) | |
1340 count (1+ count))) | |
1341 (let ((n (or nth (1- count))) | |
1342 (m (if mth (1- (- count mth)) 0))) | |
1343 (mapconcat | |
1344 (function (lambda (a) a)) (nthcdr n (nreverse (nthcdr m args))) " ")))) | |
1345 | |
1346 ;;; | |
1347 ;;; Input processing stuff [line mode] | |
1348 ;;; | |
1349 | |
1350 (defun term-send-input () | |
1351 "Send input to process. | |
1352 After the process output mark, sends all text from the process mark to | |
1353 point as input to the process. Before the process output mark, calls value | |
1354 of variable term-get-old-input to retrieve old input, copies it to the | |
1355 process mark, and sends it. A terminal newline is also inserted into the | |
1356 buffer and sent to the process. The list of function names contained in the | |
1357 value of `term-input-filter-functions' is called on the input before sending | |
1358 it. The input is entered into the input history ring, if the value of variable | |
1359 term-input-filter returns non-nil when called on the input. | |
1360 | |
1361 Any history reference may be expanded depending on the value of the variable | |
1362 `term-input-autoexpand'. The list of function names contained in the value | |
1363 of `term-input-filter-functions' is called on the input before sending it. | |
1364 The input is entered into the input history ring, if the value of variable | |
1365 `term-input-filter' returns non-nil when called on the input. | |
1366 | |
1367 The values of `term-get-old-input', `term-input-filter-functions', and | |
1368 `term-input-filter' are chosen according to the command interpreter running | |
1369 in the buffer. E.g., | |
1370 | |
1371 If the interpreter is the csh, | |
1372 term-get-old-input is the default: take the current line, discard any | |
1373 initial string matching regexp term-prompt-regexp. | |
1374 term-input-filter-functions monitors input for \"cd\", \"pushd\", and | |
1375 \"popd\" commands. When it sees one, it cd's the buffer. | |
1376 term-input-filter is the default: returns T if the input isn't all white | |
1377 space. | |
1378 | |
1379 If the term is Lucid Common Lisp, | |
1380 term-get-old-input snarfs the sexp ending at point. | |
1381 term-input-filter-functions does nothing. | |
1382 term-input-filter returns NIL if the input matches input-filter-regexp, | |
1383 which matches (1) all whitespace (2) :a, :c, etc. | |
1384 | |
1385 Similarly for Soar, Scheme, etc." | |
1386 (interactive) | |
1387 ;; Note that the input string does not include its terminal newline. | |
1388 (let ((proc (get-buffer-process (current-buffer)))) | |
1389 (if (not proc) (error "Current buffer has no process") | |
1390 (let* ((pmark (process-mark proc)) | |
1391 (pmark-val (marker-position pmark)) | |
1392 (intxt (if (>= (point) pmark-val) | |
1393 (progn (end-of-line) | |
1394 (let ((copy (buffer-substring pmark (point)))) | |
1395 ;; Delete, because inferior should echo. | |
1396 (set-marker term-pending-delete-marker | |
1397 pmark-val) | |
1398 (set-marker (process-mark proc) (point)) | |
1399 copy)) | |
1400 (funcall term-get-old-input))) | |
1401 (input (if (not (eq term-input-autoexpand 'input)) | |
1402 ;; Just whatever's already there | |
1403 intxt | |
1404 ;; Expand and leave it visible in buffer | |
1405 (term-replace-by-expanded-history t) | |
1406 (buffer-substring pmark (point)))) | |
1407 (history (if (not (eq term-input-autoexpand 'history)) | |
1408 input | |
1409 ;; This is messy 'cos ultimately the original | |
1410 ;; functions used do insertion, rather than return | |
1411 ;; strings. We have to expand, then insert back. | |
1412 (term-replace-by-expanded-history t) | |
1413 (let ((copy (buffer-substring pmark (point)))) | |
1414 (delete-region pmark (point)) | |
1415 (insert input) | |
1416 copy)))) | |
1417 (if term-pager-count | |
1418 (save-excursion | |
1419 (goto-char (process-mark proc)) | |
1420 (setq term-pager-count (term-current-row)))) | |
1421 (if (and (funcall term-input-filter history) | |
1422 (or (null term-input-ignoredups) | |
1423 (not (ring-p term-input-ring)) | |
1424 (ring-empty-p term-input-ring) | |
1425 (not (string-equal (ring-ref term-input-ring 0) | |
1426 history)))) | |
1427 (ring-insert term-input-ring history)) | |
1428 (let ((functions term-input-filter-functions)) | |
1429 (while functions | |
1430 (funcall (car functions) (concat input "\n")) | |
1431 (setq functions (cdr functions)))) | |
1432 (setq term-input-ring-index nil) | |
1433 (goto-char pmark) | |
1434 ;; Update the markers before we send the input | |
1435 ;; in case we get output amidst sending the input. | |
1436 (set-marker term-last-input-start pmark) | |
1437 (set-marker term-last-input-end (point)) | |
1438 (funcall term-input-sender proc input))))) | |
1439 | |
1440 (defun term-get-old-input-default () | |
1441 "Default for term-get-old-input. | |
1442 Take the current line, and discard any initial text matching | |
1443 term-prompt-regexp." | |
1444 (save-excursion | |
1445 (beginning-of-line) | |
1446 (term-skip-prompt) | |
1447 (let ((beg (point))) | |
1448 (end-of-line) | |
1449 (buffer-substring beg (point))))) | |
1450 | |
1451 (defun term-copy-old-input () | |
1452 "Insert after prompt old input at point as new input to be edited. | |
1453 Calls `term-get-old-input' to get old input." | |
1454 (interactive) | |
1455 (let ((input (funcall term-get-old-input)) | |
1456 (process (get-buffer-process (current-buffer)))) | |
1457 (if (not process) | |
1458 (error "Current buffer has no process") | |
1459 (goto-char (process-mark process)) | |
1460 (insert input)))) | |
1461 | |
1462 (defun term-skip-prompt () | |
1463 "Skip past the text matching regexp term-prompt-regexp. | |
1464 If this takes us past the end of the current line, don't skip at all." | |
1465 (let ((eol (save-excursion (end-of-line) (point)))) | |
1466 (if (and (looking-at term-prompt-regexp) | |
1467 (<= (match-end 0) eol)) | |
1468 (goto-char (match-end 0))))) | |
1469 | |
1470 | |
1471 (defun term-after-pmark-p () | |
1472 "Is point after the process output marker?" | |
1473 ;; Since output could come into the buffer after we looked at the point | |
1474 ;; but before we looked at the process marker's value, we explicitly | |
1475 ;; serialise. This is just because I don't know whether or not emacs | |
1476 ;; services input during execution of lisp commands. | |
1477 (let ((proc-pos (marker-position | |
1478 (process-mark (get-buffer-process (current-buffer)))))) | |
1479 (<= proc-pos (point)))) | |
1480 | |
1481 (defun term-simple-send (proc string) | |
1482 "Default function for sending to PROC input STRING. | |
1483 This just sends STRING plus a newline. To override this, | |
1484 set the hook TERM-INPUT-SENDER." | |
1485 (term-send-string proc string) | |
1486 (term-send-string proc "\n")) | |
1487 | |
1488 (defun term-bol (arg) | |
1489 "Goes to the beginning of line, then skips past the prompt, if any. | |
1490 If a prefix argument is given (\\[universal-argument]), then no prompt skip | |
1491 -- go straight to column 0. | |
1492 | |
1493 The prompt skip is done by skipping text matching the regular expression | |
1494 term-prompt-regexp, a buffer local variable." | |
1495 (interactive "P") | |
1496 (beginning-of-line) | |
1497 (if (null arg) (term-skip-prompt))) | |
1498 | |
1499 ;;; These two functions are for entering text you don't want echoed or | |
1500 ;;; saved -- typically passwords to ftp, telnet, or somesuch. | |
1501 ;;; Just enter m-x term-send-invisible and type in your line. | |
1502 | |
1503 (defun term-read-noecho (prompt &optional stars) | |
1504 "Read a single line of text from user without echoing, and return it. | |
1505 Prompt with argument PROMPT, a string. Optional argument STARS causes | |
1506 input to be echoed with '*' characters on the prompt line. Input ends with | |
1507 RET, LFD, or ESC. DEL or C-h rubs out. C-u kills line. C-g aborts (if | |
1508 `inhibit-quit' is set because e.g. this function was called from a process | |
1509 filter and C-g is pressed, this function returns nil rather than a string). | |
1510 | |
1511 Note that the keystrokes comprising the text can still be recovered | |
1512 \(temporarily) with \\[view-lossage]. This may be a security bug for some | |
1513 applications." | |
1514 (let ((ans "") | |
1515 (c 0) | |
1516 (echo-keystrokes 0) | |
1517 (cursor-in-echo-area t) | |
1518 (done nil)) | |
1519 (while (not done) | |
1520 (if stars | |
1521 (message "%s%s" prompt (make-string (length ans) ?*)) | |
1522 (message prompt)) | |
1523 (setq c (read-char)) | |
1524 (cond ((= c ?\C-g) | |
1525 ;; This function may get called from a process filter, where | |
1526 ;; inhibit-quit is set. In later versions of emacs read-char | |
1527 ;; may clear quit-flag itself and return C-g. That would make | |
1528 ;; it impossible to quit this loop in a simple way, so | |
1529 ;; re-enable it here (for backward-compatibility the check for | |
1530 ;; quit-flag below would still be necessary, so this seems | |
1531 ;; like the simplest way to do things). | |
1532 (setq quit-flag t | |
1533 done t)) | |
1534 ((or (= c ?\r) (= c ?\n) (= c ?\e)) | |
1535 (setq done t)) | |
1536 ((= c ?\C-u) | |
1537 (setq ans "")) | |
1538 ((and (/= c ?\b) (/= c ?\177)) | |
1539 (setq ans (concat ans (char-to-string c)))) | |
1540 ((> (length ans) 0) | |
1541 (setq ans (substring ans 0 -1))))) | |
1542 (if quit-flag | |
1543 ;; Emulate a true quit, except that we have to return a value. | |
1544 (prog1 | |
1545 (setq quit-flag nil) | |
1546 (message "Quit") | |
1547 (beep t)) | |
1548 (message "") | |
1549 ans))) | |
1550 | |
1551 (defun term-send-invisible (str &optional proc) | |
1552 "Read a string without echoing. | |
1553 Then send it to the process running in the current buffer. A new-line | |
1554 is additionally sent. String is not saved on term input history list. | |
1555 Security bug: your string can still be temporarily recovered with | |
1556 \\[view-lossage]." | |
1557 (interactive (list (term-read-noecho "Enter non-echoed text"))) | |
1558 (interactive "P") ; Defeat snooping via C-x esc | |
1559 (if (not (stringp str)) | |
1560 (setq str (term-read-noecho "Non-echoed text: " t))) | |
1561 (if (not proc) | |
1562 (setq proc (get-buffer-process (current-buffer)))) | |
1563 (if (not proc) (error "Current buffer has no process") | |
1564 (setq term-kill-echo-list (nconc term-kill-echo-list | |
1565 (cons str nil))) | |
1566 (term-send-string proc str) | |
1567 (term-send-string proc "\n"))) | |
1568 | |
1569 | |
1570 ;;; Low-level process communication | |
1571 | |
1572 (defvar term-input-chunk-size 512 | |
1573 "*Long inputs send to term processes are broken up into chunks of this size. | |
1574 If your process is choking on big inputs, try lowering the value.") | |
1575 | |
1576 (defun term-send-string (proc str) | |
1577 "Send PROCESS the contents of STRING as input. | |
1578 This is equivalent to process-send-string, except that long input strings | |
1579 are broken up into chunks of size term-input-chunk-size. Processes | |
1580 are given a chance to output between chunks. This can help prevent processes | |
1581 from hanging when you send them long inputs on some OS's." | |
1582 (let* ((len (length str)) | |
1583 (i (min len term-input-chunk-size))) | |
1584 (process-send-string proc (substring str 0 i)) | |
1585 (while (< i len) | |
1586 (let ((next-i (+ i term-input-chunk-size))) | |
1587 (accept-process-output) | |
1588 (process-send-string proc (substring str i (min len next-i))) | |
1589 (setq i next-i))))) | |
1590 | |
1591 (defun term-send-region (proc start end) | |
1592 "Sends to PROC the region delimited by START and END. | |
1593 This is a replacement for process-send-region that tries to keep | |
1594 your process from hanging on long inputs. See term-send-string." | |
1595 (term-send-string proc (buffer-substring start end))) | |
1596 | |
1597 | |
1598 ;;; Random input hackage | |
1599 | |
1600 (defun term-kill-output () | |
1601 "Kill all output from interpreter since last input." | |
1602 (interactive) | |
1603 (let ((pmark (process-mark (get-buffer-process (current-buffer))))) | |
1604 (kill-region term-last-input-end pmark) | |
1605 (goto-char pmark) | |
1606 (insert "*** output flushed ***\n") | |
1607 (set-marker pmark (point)))) | |
1608 | |
1609 (defun term-show-output () | |
1610 "Display start of this batch of interpreter output at top of window. | |
1611 Sets mark to the value of point when this command is run." | |
1612 (interactive) | |
1613 (goto-char term-last-input-end) | |
1614 (backward-char) | |
1615 (beginning-of-line) | |
1616 (set-window-start (selected-window) (point)) | |
1617 (end-of-line)) | |
1618 | |
1619 (defun term-interrupt-subjob () | |
1620 "Interrupt the current subjob." | |
1621 (interactive) | |
1622 (interrupt-process nil term-ptyp)) | |
1623 | |
1624 (defun term-kill-subjob () | |
1625 "Send kill signal to the current subjob." | |
1626 (interactive) | |
1627 (kill-process nil term-ptyp)) | |
1628 | |
1629 (defun term-quit-subjob () | |
1630 "Send quit signal to the current subjob." | |
1631 (interactive) | |
1632 (quit-process nil term-ptyp)) | |
1633 | |
1634 (defun term-stop-subjob () | |
1635 "Stop the current subjob. | |
1636 WARNING: if there is no current subjob, you can end up suspending | |
1637 the top-level process running in the buffer. If you accidentally do | |
1638 this, use \\[term-continue-subjob] to resume the process. (This | |
1639 is not a problem with most shells, since they ignore this signal.)" | |
1640 (interactive) | |
1641 (stop-process nil term-ptyp)) | |
1642 | |
1643 (defun term-continue-subjob () | |
1644 "Send CONT signal to process buffer's process group. | |
1645 Useful if you accidentally suspend the top-level process." | |
1646 (interactive) | |
1647 (continue-process nil term-ptyp)) | |
1648 | |
1649 (defun term-kill-input () | |
1650 "Kill all text from last stuff output by interpreter to point." | |
1651 (interactive) | |
1652 (let* ((pmark (process-mark (get-buffer-process (current-buffer)))) | |
1653 (p-pos (marker-position pmark))) | |
1654 (if (> (point) p-pos) | |
1655 (kill-region pmark (point))))) | |
1656 | |
1657 (defun term-delchar-or-maybe-eof (arg) | |
1658 "Delete ARG characters forward, or send an EOF to process if at end of buffer." | |
1659 (interactive "p") | |
1660 (if (eobp) | |
1661 (process-send-eof) | |
1662 (delete-char arg))) | |
1663 | |
1664 (defun term-send-eof () | |
1665 "Send an EOF to the current buffer's process." | |
1666 (interactive) | |
1667 (process-send-eof)) | |
1668 | |
1669 (defun term-backward-matching-input (regexp arg) | |
1670 "Search backward through buffer for match for REGEXP. | |
1671 Matches are searched for on lines that match `term-prompt-regexp'. | |
1672 With prefix argument N, search for Nth previous match. | |
1673 If N is negative, find the next or Nth next match." | |
1674 (interactive (term-regexp-arg "Backward input matching (regexp): ")) | |
1675 (let* ((re (concat term-prompt-regexp ".*" regexp)) | |
1676 (pos (save-excursion (end-of-line (if (> arg 0) 0 1)) | |
1677 (if (re-search-backward re nil t arg) | |
1678 (point))))) | |
1679 (if (null pos) | |
1680 (progn (message "Not found") | |
1681 (ding)) | |
1682 (goto-char pos) | |
1683 (term-bol nil)))) | |
1684 | |
1685 (defun term-forward-matching-input (regexp arg) | |
1686 "Search forward through buffer for match for REGEXP. | |
1687 Matches are searched for on lines that match `term-prompt-regexp'. | |
1688 With prefix argument N, search for Nth following match. | |
1689 If N is negative, find the previous or Nth previous match." | |
1690 (interactive (term-regexp-arg "Forward input matching (regexp): ")) | |
1691 (term-backward-matching-input regexp (- arg))) | |
1692 | |
1693 | |
1694 (defun term-next-prompt (n) | |
1695 "Move to end of Nth next prompt in the buffer. | |
1696 See `term-prompt-regexp'." | |
1697 (interactive "p") | |
1698 (let ((paragraph-start term-prompt-regexp)) | |
1699 (end-of-line (if (> n 0) 1 0)) | |
1700 (forward-paragraph n) | |
1701 (term-skip-prompt))) | |
1702 | |
1703 (defun term-previous-prompt (n) | |
1704 "Move to end of Nth previous prompt in the buffer. | |
1705 See `term-prompt-regexp'." | |
1706 (interactive "p") | |
1707 (term-next-prompt (- n))) | |
1708 | |
1709 ;;; Support for source-file processing commands. | |
1710 ;;;============================================================================ | |
1711 ;;; Many command-interpreters (e.g., Lisp, Scheme, Soar) have | |
1712 ;;; commands that process files of source text (e.g. loading or compiling | |
1713 ;;; files). So the corresponding process-in-a-buffer modes have commands | |
1714 ;;; for doing this (e.g., lisp-load-file). The functions below are useful | |
1715 ;;; for defining these commands. | |
1716 ;;; | |
1717 ;;; Alas, these guys don't do exactly the right thing for Lisp, Scheme | |
1718 ;;; and Soar, in that they don't know anything about file extensions. | |
1719 ;;; So the compile/load interface gets the wrong default occasionally. | |
1720 ;;; The load-file/compile-file default mechanism could be smarter -- it | |
1721 ;;; doesn't know about the relationship between filename extensions and | |
1722 ;;; whether the file is source or executable. If you compile foo.lisp | |
1723 ;;; with compile-file, then the next load-file should use foo.bin for | |
1724 ;;; the default, not foo.lisp. This is tricky to do right, particularly | |
1725 ;;; because the extension for executable files varies so much (.o, .bin, | |
1726 ;;; .lbin, .mo, .vo, .ao, ...). | |
1727 | |
1728 | |
1729 ;;; TERM-SOURCE-DEFAULT -- determines defaults for source-file processing | |
1730 ;;; commands. | |
1731 ;;; | |
1732 ;;; TERM-CHECK-SOURCE -- if FNAME is in a modified buffer, asks you if you | |
1733 ;;; want to save the buffer before issuing any process requests to the command | |
1734 ;;; interpreter. | |
1735 ;;; | |
1736 ;;; TERM-GET-SOURCE -- used by the source-file processing commands to prompt | |
1737 ;;; for the file to process. | |
1738 | |
1739 ;;; (TERM-SOURCE-DEFAULT previous-dir/file source-modes) | |
1740 ;;;============================================================================ | |
1741 ;;; This function computes the defaults for the load-file and compile-file | |
1742 ;;; commands for tea, soar, cmulisp, and cmuscheme modes. | |
1743 ;;; | |
1744 ;;; - PREVIOUS-DIR/FILE is a pair (directory . filename) from the last | |
1745 ;;; source-file processing command. NIL if there hasn't been one yet. | |
1746 ;;; - SOURCE-MODES is a list used to determine what buffers contain source | |
1747 ;;; files: if the major mode of the buffer is in SOURCE-MODES, it's source. | |
1748 ;;; Typically, (lisp-mode) or (scheme-mode). | |
1749 ;;; | |
1750 ;;; If the command is given while the cursor is inside a string, *and* | |
1751 ;;; the string is an existing filename, *and* the filename is not a directory, | |
1752 ;;; then the string is taken as default. This allows you to just position | |
1753 ;;; your cursor over a string that's a filename and have it taken as default. | |
1754 ;;; | |
1755 ;;; If the command is given in a file buffer whose major mode is in | |
1756 ;;; SOURCE-MODES, then the the filename is the default file, and the | |
1757 ;;; file's directory is the default directory. | |
1758 ;;; | |
1759 ;;; If the buffer isn't a source file buffer (e.g., it's the process buffer), | |
1760 ;;; then the default directory & file are what was used in the last source-file | |
1761 ;;; processing command (i.e., PREVIOUS-DIR/FILE). If this is the first time | |
1762 ;;; the command has been run (PREVIOUS-DIR/FILE is nil), the default directory | |
1763 ;;; is the cwd, with no default file. (\"no default file\" = nil) | |
1764 ;;; | |
1765 ;;; SOURCE-REGEXP is typically going to be something like (tea-mode) | |
1766 ;;; for T programs, (lisp-mode) for Lisp programs, (soar-mode lisp-mode) | |
1767 ;;; for Soar programs, etc. | |
1768 ;;; | |
1769 ;;; The function returns a pair: (default-directory . default-file). | |
1770 | |
1771 (defun term-source-default (previous-dir/file source-modes) | |
1772 (cond ((and buffer-file-name (memq major-mode source-modes)) | |
1773 (cons (file-name-directory buffer-file-name) | |
1774 (file-name-nondirectory buffer-file-name))) | |
1775 (previous-dir/file) | |
1776 (t | |
1777 (cons default-directory nil)))) | |
1778 | |
1779 | |
1780 ;;; (TERM-CHECK-SOURCE fname) | |
1781 ;;;============================================================================ | |
1782 ;;; Prior to loading or compiling (or otherwise processing) a file (in the CMU | |
1783 ;;; process-in-a-buffer modes), this function can be called on the filename. | |
1784 ;;; If the file is loaded into a buffer, and the buffer is modified, the user | |
1785 ;;; is queried to see if he wants to save the buffer before proceeding with | |
1786 ;;; the load or compile. | |
1787 | |
1788 (defun term-check-source (fname) | |
1789 (let ((buff (get-file-buffer fname))) | |
1790 (if (and buff | |
1791 (buffer-modified-p buff) | |
1792 (y-or-n-p (format "Save buffer %s first? " | |
1793 (buffer-name buff)))) | |
1794 ;; save BUFF. | |
1795 (let ((old-buffer (current-buffer))) | |
1796 (set-buffer buff) | |
1797 (save-buffer) | |
1798 (set-buffer old-buffer))))) | |
1799 | |
1800 | |
1801 ;;; (TERM-GET-SOURCE prompt prev-dir/file source-modes mustmatch-p) | |
1802 ;;;============================================================================ | |
1803 ;;; TERM-GET-SOURCE is used to prompt for filenames in command-interpreter | |
1804 ;;; commands that process source files (like loading or compiling a file). | |
1805 ;;; It prompts for the filename, provides a default, if there is one, | |
1806 ;;; and returns the result filename. | |
1807 ;;; | |
1808 ;;; See TERM-SOURCE-DEFAULT for more on determining defaults. | |
1809 ;;; | |
1810 ;;; PROMPT is the prompt string. PREV-DIR/FILE is the (directory . file) pair | |
1811 ;;; from the last source processing command. SOURCE-MODES is a list of major | |
1812 ;;; modes used to determine what file buffers contain source files. (These | |
1813 ;;; two arguments are used for determining defaults). If MUSTMATCH-P is true, | |
1814 ;;; then the filename reader will only accept a file that exists. | |
1815 ;;; | |
1816 ;;; A typical use: | |
1817 ;;; (interactive (term-get-source "Compile file: " prev-lisp-dir/file | |
1818 ;;; '(lisp-mode) t)) | |
1819 | |
1820 ;;; This is pretty stupid about strings. It decides we're in a string | |
1821 ;;; if there's a quote on both sides of point on the current line. | |
1822 (defun term-extract-string () | |
1823 "Returns string around POINT that starts the current line or nil." | |
1824 (save-excursion | |
1825 (let* ((point (point)) | |
1826 (bol (progn (beginning-of-line) (point))) | |
1827 (eol (progn (end-of-line) (point))) | |
1828 (start (progn (goto-char point) | |
1829 (and (search-backward "\"" bol t) | |
1830 (1+ (point))))) | |
1831 (end (progn (goto-char point) | |
1832 (and (search-forward "\"" eol t) | |
1833 (1- (point)))))) | |
1834 (and start end | |
1835 (buffer-substring start end))))) | |
1836 | |
1837 (defun term-get-source (prompt prev-dir/file source-modes mustmatch-p) | |
1838 (let* ((def (term-source-default prev-dir/file source-modes)) | |
1839 (stringfile (term-extract-string)) | |
1840 (sfile-p (and stringfile | |
1841 (condition-case () | |
1842 (file-exists-p stringfile) | |
1843 (error nil)) | |
1844 (not (file-directory-p stringfile)))) | |
1845 (defdir (if sfile-p (file-name-directory stringfile) | |
1846 (car def))) | |
1847 (deffile (if sfile-p (file-name-nondirectory stringfile) | |
1848 (cdr def))) | |
1849 (ans (read-file-name (if deffile (format "%s(default %s) " | |
1850 prompt deffile) | |
1851 prompt) | |
1852 defdir | |
1853 (concat defdir deffile) | |
1854 mustmatch-p))) | |
1855 (list (expand-file-name (substitute-in-file-name ans))))) | |
1856 | |
1857 ;;; I am somewhat divided on this string-default feature. It seems | |
1858 ;;; to violate the principle-of-least-astonishment, in that it makes | |
1859 ;;; the default harder to predict, so you actually have to look and see | |
1860 ;;; what the default really is before choosing it. This can trip you up. | |
1861 ;;; On the other hand, it can be useful, I guess. I would appreciate feedback | |
1862 ;;; on this. | |
1863 ;;; -Olin | |
1864 | |
1865 | |
1866 ;;; Simple process query facility. | |
1867 ;;; =========================================================================== | |
1868 ;;; This function is for commands that want to send a query to the process | |
1869 ;;; and show the response to the user. For example, a command to get the | |
1870 ;;; arglist for a Common Lisp function might send a "(arglist 'foo)" query | |
1871 ;;; to an inferior Common Lisp process. | |
1872 ;;; | |
1873 ;;; This simple facility just sends strings to the inferior process and pops | |
1874 ;;; up a window for the process buffer so you can see what the process | |
1875 ;;; responds with. We don't do anything fancy like try to intercept what the | |
1876 ;;; process responds with and put it in a pop-up window or on the message | |
1877 ;;; line. We just display the buffer. Low tech. Simple. Works good. | |
1878 | |
1879 ;;; Send to the inferior process PROC the string STR. Pop-up but do not select | |
1880 ;;; a window for the inferior process so that its response can be seen. | |
1881 (defun term-proc-query (proc str) | |
1882 (let* ((proc-buf (process-buffer proc)) | |
1883 (proc-mark (process-mark proc))) | |
1884 (display-buffer proc-buf) | |
1885 (set-buffer proc-buf) ; but it's not the selected *window* | |
1886 (let ((proc-win (get-buffer-window proc-buf)) | |
1887 (proc-pt (marker-position proc-mark))) | |
1888 (term-send-string proc str) ; send the query | |
1889 (accept-process-output proc) ; wait for some output | |
1890 ;; Try to position the proc window so you can see the answer. | |
1891 ;; This is bogus code. If you delete the (sit-for 0), it breaks. | |
1892 ;; I don't know why. Wizards invited to improve it. | |
1893 (if (not (pos-visible-in-window-p proc-pt proc-win)) | |
1894 (let ((opoint (window-point proc-win))) | |
1895 (set-window-point proc-win proc-mark) (sit-for 0) | |
1896 (if (not (pos-visible-in-window-p opoint proc-win)) | |
1897 (push-mark opoint) | |
1898 (set-window-point proc-win opoint))))))) | |
1899 | |
1900 ;;; Returns the current column in the current screen line. | |
1901 ;;; Note: (current-column) yields column in buffer line. | |
1902 | |
1903 (defun term-horizontal-column () | |
1904 (- (term-current-column) (term-start-line-column))) | |
1905 | |
1906 ;; Calls either vertical-motion or buffer-vertical-motion | |
1907 (defmacro term-vertical-motion (count) | |
1908 (list 'funcall 'term-vertical-motion count)) | |
1909 | |
1910 ;; An emulation of vertical-motion that is independent of having a window. | |
1911 ;; Instead, it uses the term-width variable as the logical window width. | |
1912 | |
1913 (defun buffer-vertical-motion (count) | |
1914 (cond ((= count 0) | |
1915 (move-to-column (* term-width (/ (current-column) term-width))) | |
1916 0) | |
1917 ((> count 0) | |
1918 (let ((H) | |
1919 (todo (+ count (/ (current-column) term-width)))) | |
1920 (end-of-line) | |
1921 ;; The loop interates over buffer lines; | |
1922 ;; H is the number of screen lines in the current line, i.e. | |
1923 ;; the ceiling of dividing the buffer line width by term-width. | |
1924 (while (and (<= (setq H (max (/ (+ (current-column) term-width -1) | |
1925 term-width) | |
1926 1)) | |
1927 todo) | |
1928 (not (eobp))) | |
1929 (setq todo (- todo H)) | |
1930 (forward-char) ;; Move past the ?\n | |
1931 (end-of-line)) ;; and on to the end of the next line. | |
1932 (if (and (>= todo H) (> todo 0)) | |
1933 (+ (- count todo) H -1) ;; Hit end of buffer. | |
1934 (move-to-column (* todo term-width)) | |
1935 count))) | |
1936 (t ;; (< count 0) ;; Similar algorithm, but for upward motion. | |
1937 (let ((H) | |
1938 (todo (- count))) | |
1939 (while (and (<= (setq H (max (/ (+ (current-column) term-width -1) | |
1940 term-width) | |
1941 1)) | |
1942 todo) | |
1943 (progn (beginning-of-line) | |
1944 (not (bobp)))) | |
1945 (setq todo (- todo H)) | |
1946 (backward-char)) ;; Move to end of previos line | |
1947 (if (and (>= todo H) (> todo 0)) | |
1948 (+ count todo (- 1 H)) ;; Hit beginning of buffer. | |
1949 (move-to-column (* (- H todo 1) term-width)) | |
1950 count))))) | |
1951 | |
1952 ;;; The term-start-line-column variable is used as a cache. | |
1953 (defun term-start-line-column () | |
1954 (cond (term-start-line-column) | |
1955 ((let ((save-pos (point))) | |
1956 (term-vertical-motion 0) | |
1957 (setq term-start-line-column (current-column)) | |
1958 (goto-char save-pos) | |
1959 term-start-line-column)))) | |
1960 | |
1961 ;;; Same as (current-column), but uses term-current-column as a cache. | |
1962 (defun term-current-column () | |
1963 (cond (term-current-column) | |
1964 ((setq term-current-column (current-column))))) | |
1965 | |
1966 ;;; Move DELTA column right (or left if delta < 0). | |
1967 | |
1968 (defun term-move-columns (delta) | |
1969 (setq term-current-column (+ (term-current-column) delta)) | |
1970 (move-to-column term-current-column t)) | |
1971 | |
1972 ;; Insert COUNT copies of CHAR in the default face. | |
1973 (defun term-insert-char (char count) | |
1974 (let ((old-point (point))) | |
1975 (insert-char char count) | |
1976 (put-text-property old-point (point) 'face 'default))) | |
1977 | |
1978 (defun term-current-row () | |
1979 (cond (term-current-row) | |
1980 ((setq term-current-row | |
1981 (save-restriction | |
1982 (save-excursion | |
1983 (narrow-to-region term-home-marker (point-max)) | |
1984 (- (term-vertical-motion -9999)))))))) | |
1985 | |
1986 (defun term-adjust-current-row-cache (delta) | |
1987 (if term-current-row | |
1988 (setq term-current-row (+ term-current-row delta)))) | |
1989 | |
1990 ;; True if currently doing PAGER handling. | |
1991 (defmacro term-handling-pager () 'term-pager-old-local-map) | |
1992 | |
1993 (defmacro term-using-alternate-sub-buffer () 'term-saved-home-marker) | |
1994 | |
1995 (defun term-terminal-pos () | |
1996 (save-excursion ; save-restriction | |
1997 (let ((save-col (term-current-column)) | |
1998 (x)) | |
1999 (term-vertical-motion 0) | |
2000 (setq x (- save-col (current-column))) | |
2001 (setq y (term-vertical-motion term-height)) | |
2002 (cons x y)))) | |
2003 | |
2004 ;;; Terminal emulation | |
2005 ;;; This is the standard process filter for term buffers. | |
2006 ;;; It emulates (most of the features of) a VT100/ANSI-style terminal. | |
2007 | |
2008 (defun term-emulate-terminal (proc str) | |
2009 (let* ((previous-buffer (current-buffer)) | |
2010 (i 0) char funny count save-point save-marker old-point temp win | |
2011 (selected (selected-window)) | |
2012 (str-length (length str))) | |
2013 (unwind-protect | |
2014 (progn | |
2015 (set-buffer (process-buffer proc)) | |
2016 | |
2017 (if (marker-buffer term-pending-delete-marker) | |
2018 (progn | |
2019 ;; Delete text following term-pending-delete-marker. | |
2020 (delete-region term-pending-delete-marker (process-mark proc)) | |
2021 (set-marker term-pending-delete-marker nil))) | |
2022 | |
2023 (if (eq (window-buffer) (current-buffer)) | |
2024 (progn | |
2025 (setq term-vertical-motion (symbol-function 'vertical-motion)) | |
2026 (term-check-size proc)) | |
2027 (setq term-vertical-motion | |
2028 (symbol-function 'buffer-vertical-motion))) | |
2029 | |
2030 (setq save-marker (copy-marker (process-mark proc))) | |
2031 | |
2032 (if (/= (point) (process-mark proc)) | |
2033 (progn (setq save-point (point-marker)) | |
2034 (goto-char (process-mark proc)))) | |
2035 | |
2036 (save-restriction | |
2037 ;; If the buffer is in line mode, and there is a partial | |
2038 ;; input line, save the line (by narrowing to leave it | |
2039 ;; outside the restriction ) until we're done with output. | |
2040 (if (and (> (point-max) (process-mark proc)) | |
2041 (term-in-line-mode)) | |
2042 (narrow-to-region (point-min) (process-mark proc))) | |
2043 | |
2044 (if term-log-buffer | |
2045 (princ str term-log-buffer)) | |
2046 (cond ((eq term-terminal-state 4) ;; Have saved pending output. | |
2047 (setq str (concat term-terminal-parameter str)) | |
2048 (setq term-terminal-parameter nil) | |
2049 (setq str-length (length str)) | |
2050 (setq term-terminal-state 0))) | |
2051 | |
2052 (while (< i str-length) | |
2053 (setq char (aref str i)) | |
2054 (cond ((< term-terminal-state 2) | |
2055 ;; Look for prefix of regular chars | |
2056 (setq funny | |
2057 (string-match "[\r\n\000\007\033\t\b\032\016\017]" | |
2058 str i)) | |
2059 (if (not funny) (setq funny str-length)) | |
2060 (cond ((> funny i) | |
2061 (cond ((eq term-terminal-state 1) | |
2062 (term-move-columns 1) | |
2063 (setq term-terminal-state 0))) | |
2064 (setq count (- funny i)) | |
2065 (setq temp (- (+ (term-horizontal-column) count) | |
2066 term-width)) | |
2067 (cond ((<= temp 0)) ;; All count chars fit in line. | |
2068 ((> count temp) ;; Some chars fit. | |
2069 ;; This iteration, handle only what fits. | |
2070 (setq count (- count temp)) | |
2071 (setq funny (+ count i))) | |
2072 ((> (term-handle-scroll 1) 0) | |
2073 (setq count (min count term-width)) | |
2074 (setq funny (+ count i)) | |
2075 (term-adjust-current-row-cache 1) | |
2076 (setq term-start-line-column | |
2077 term-current-column)) | |
2078 (t ;; Doing PAGER processing. | |
2079 (setq count 0 funny i) | |
2080 (setq term-current-column nil) | |
2081 (setq term-start-line-column nil))) | |
2082 (if term-insert-mode | |
2083 ;; Inserting spaces, then deleting them, then | |
2084 ;; inserting the actual text seems clumsy, but | |
2085 ;; it is simple, and the overhead is miniscule. | |
2086 (term-insert-spaces count)) | |
2087 (setq old-point (point)) | |
2088 (term-move-columns count) | |
2089 (delete-region old-point (point)) | |
2090 (insert (substring str i funny)) | |
2091 (put-text-property old-point (point) | |
2092 'face term-current-face) | |
2093 ;; If the last char was written in last column, | |
2094 ;; back up one column, but remember we did so. | |
2095 ;; Thus we emulate xterm/vt100-style line-wrapping. | |
2096 (cond ((eq temp 0) | |
2097 (term-move-columns -1) | |
2098 (setq term-terminal-state 1))) | |
2099 (setq i (1- funny))) | |
2100 ((and (setq term-terminal-state 0) | |
2101 (eq char ?\^I)) ; TAB | |
2102 ;; FIXME: Does not handle line wrap! | |
2103 (setq count (term-current-column)) | |
2104 (setq count (+ count 8 (- (mod count 8)))) | |
2105 (if (< (move-to-column count nil) count) | |
2106 (term-insert-char char 1)) | |
2107 (setq term-current-column count) | |
2108 (setq term-start-line-column nil)) | |
2109 ((eq char ?\b) | |
2110 (term-move-columns -1)) | |
2111 ((eq char ?\r) | |
2112 (term-vertical-motion 0) | |
2113 (setq term-current-column nil)) | |
2114 ((eq char ?\n) | |
2115 (if (not (and term-kill-echo-list | |
2116 (term-check-kill-echo-list))) | |
2117 (term-down 1 0 t))) | |
2118 ((eq char ?\033) ; Escape | |
2119 (setq term-terminal-state 2)) | |
2120 ((eq char 0)) ; NUL: Do nothing | |
2121 ((eq char ?\016)) ; Shift Out - ignored | |
2122 ((eq char ?\017)) ; Shift In - ignored | |
2123 ((eq char ?\^G) | |
2124 (beep t)) ; Bell | |
2125 ((eq char ?\032) | |
2126 (let ((end (string-match "\n" str i))) | |
2127 (if end | |
2128 (progn (funcall term-command-hook | |
2129 (substring str (1+ i) (1- end))) | |
2130 (setq i end)) | |
2131 (setq term-terminal-parameter | |
2132 (substring str i)) | |
2133 (setq term-terminal-state 4) | |
2134 (setq i str-length)))) | |
2135 (t ; insert char FIXME: Should never happen | |
2136 (term-move-columns 1) | |
2137 (backward-delete-char 1) | |
2138 (insert char)))) | |
2139 ((eq term-terminal-state 2) ; Seen Esc | |
2140 (cond ((eq char ?\133) ;; ?\133 = ?[ | |
2141 (make-local-variable 'term-terminal-parameter) | |
2142 (make-local-variable 'term-terminal-previous-parameter) | |
2143 (setq term-terminal-parameter 0) | |
2144 (setq term-terminal-previous-parameter 0) | |
2145 (setq term-terminal-state 3)) | |
2146 ((eq char ?D) ;; scroll forward | |
2147 (term-down 1 0 t) | |
2148 (setq term-terminal-state 0)) | |
2149 ((eq char ?M) ;; scroll reversed | |
2150 (term-insert-lines 1) | |
2151 (setq term-terminal-state 0)) | |
2152 ((eq char ?7) ;; Save cursor | |
2153 (setq term-saved-cursor | |
2154 (cons (term-current-row) | |
2155 (term-horizontal-column))) | |
2156 (setq term-terminal-state 0)) | |
2157 ((eq char ?8) ;; Restore cursor | |
2158 (if term-saved-cursor | |
2159 (term-goto (car term-saved-cursor) | |
2160 (cdr term-saved-cursor))) | |
2161 (setq term-terminal-state 0)) | |
2162 ((setq term-terminal-state 0)))) | |
2163 ((eq term-terminal-state 3) ; Seen Esc [ | |
2164 (cond ((and (>= char ?0) (<= char ?9)) | |
2165 (setq term-terminal-parameter | |
2166 (+ (* 10 term-terminal-parameter) (- char ?0)))) | |
2167 ((eq char ?\073 ) ; ?; | |
2168 (setq term-terminal-previous-parameter | |
2169 term-terminal-parameter) | |
2170 (setq term-terminal-parameter 0)) | |
2171 ((eq char ??)) ; Ignore ? | |
2172 (t | |
2173 (term-handle-ansi-escape char) | |
2174 (setq term-terminal-state 0))))) | |
2175 (if (term-handling-pager) | |
2176 ;; Finish stuff to get ready to handle PAGER. | |
2177 (progn | |
2178 (if (> (% (current-column) term-width) 0) | |
2179 (setq term-terminal-parameter | |
2180 (substring str i)) | |
2181 ;; We're at column 0. Goto end of buffer; to compensate, | |
2182 ;; prepend a ?\r for later. This looks more consistent. | |
2183 (if (zerop i) | |
2184 (setq term-terminal-parameter | |
2185 (concat "\r" (substring str i))) | |
2186 (setq term-terminal-parameter (substring str (1- i))) | |
2187 (aset term-terminal-parameter 0 ?\r)) | |
2188 (goto-char (point-max))) | |
2189 (setq term-terminal-state 4) | |
2190 (make-local-variable 'term-pager-old-filter) | |
2191 (setq term-pager-old-filter (process-filter proc)) | |
2192 (set-process-filter proc term-pager-filter) | |
2193 (setq i str-length))) | |
2194 (setq i (1+ i)))) | |
2195 | |
2196 (set-marker (process-mark proc) (point)) | |
2197 (if save-point | |
2198 (progn (goto-char save-point) | |
2199 (set-marker save-point nil))) | |
2200 | |
2201 ;; Check for a pending filename-and-line number to display. | |
2202 ;; We do this before scrolling, because we might create a new window. | |
2203 (if (and term-pending-frame | |
2204 (eq (window-buffer selected) (current-buffer))) | |
2205 (progn (term-display-line (car term-pending-frame) | |
2206 (cdr term-pending-frame)) | |
2207 (setq term-pending-frame nil) | |
2208 ;; We have created a new window, so check the window size. | |
2209 (term-check-size proc))) | |
2210 | |
2211 ;; Scroll each window displaying the buffer but (by default) | |
2212 ;; only if the point matches the process-mark we started with. | |
2213 (setq win selected) | |
2214 (while (progn | |
2215 (setq win (next-window win nil t)) | |
2216 (if (eq (window-buffer win) (process-buffer proc)) | |
2217 (let ((scroll term-scroll-to-bottom-on-output)) | |
2218 (select-window win) | |
2219 (if (or (= (point) save-marker) | |
2220 (eq scroll t) (eq scroll 'all) | |
2221 ;; Maybe user wants point to jump to the end. | |
2222 (and (eq selected win) | |
2223 (or (eq scroll 'this) (not save-point))) | |
2224 (and (eq scroll 'others) | |
2225 (not (eq selected win)))) | |
2226 (progn | |
2227 (goto-char term-home-marker) | |
2228 (recenter 0) | |
2229 (goto-char (process-mark proc)) | |
2230 (if (not (pos-visible-in-window-p (point) win)) | |
2231 (recenter -1)))) | |
2232 ;; Optionally scroll so that the text | |
2233 ;; ends at the bottom of the window. | |
2234 (if (and term-scroll-show-maximum-output | |
2235 (>= (point) (process-mark proc))) | |
2236 (save-excursion | |
2237 (goto-char (point-max)) | |
2238 (recenter -1))))) | |
2239 (not (eq win selected)))) | |
2240 | |
2241 (set-marker save-marker nil)) | |
2242 ;; unwind-protect cleanup-forms follow: | |
2243 (set-buffer previous-buffer) | |
2244 (select-window selected)))) | |
2245 | |
2246 ;;; Handle a character assuming (eq terminal-state 2) - | |
2247 ;;; i.e. we have previousely seen Escape followed by ?[. | |
2248 | |
2249 (defun term-handle-ansi-escape (char) | |
2250 (cond | |
2251 ((eq char ?H) ; cursor motion | |
2252 (if (<= term-terminal-parameter 0) | |
2253 (setq term-terminal-parameter 1)) | |
2254 (if (<= term-terminal-previous-parameter 0) | |
2255 (setq term-terminal-previous-parameter 1)) | |
2256 (term-goto | |
2257 (1- term-terminal-previous-parameter) | |
2258 (1- term-terminal-parameter))) | |
2259 ;; \E[A - cursor up | |
2260 ((eq char ?A) | |
2261 (term-down (- (max 1 term-terminal-parameter)) 0 t)) | |
2262 ;; \E[B - cursor down | |
2263 ((eq char ?B) | |
2264 (term-down (max 1 term-terminal-parameter) 0 t)) | |
2265 ;; \E[C - cursor right | |
2266 ((eq char ?C) | |
2267 (term-move-columns (max 1 term-terminal-parameter))) | |
2268 ;; \E[D - cursor left | |
2269 ((eq char ?D) | |
2270 (term-move-columns (- (max 1 term-terminal-parameter)))) | |
2271 ;; \E[J - clear to end of screen | |
2272 ((eq char ?J) | |
2273 (term-erase-in-display term-terminal-parameter)) | |
2274 ;; \E[K - clear to end of line | |
2275 ((eq char ?K) | |
2276 (term-erase-in-line term-terminal-parameter)) | |
2277 ;; \E[L - insert lines | |
2278 ((eq char ?L) | |
2279 (term-insert-lines (max 1 term-terminal-parameter))) | |
2280 ;; \E[M - delete lines | |
2281 ((eq char ?M) | |
2282 (term-delete-lines (max 1 term-terminal-parameter))) | |
2283 ;; \E[P - delete chars | |
2284 ((eq char ?P) | |
2285 (term-delete-chars (max 1 term-terminal-parameter))) | |
2286 ;; \E[@ - insert spaces | |
2287 ((eq char ?@) | |
2288 (term-insert-spaces (max 1 term-terminal-parameter))) | |
2289 ;; \E[?h - DEC Private Mode Set | |
2290 ((eq char ?h) | |
2291 (cond ((eq term-terminal-parameter 4) | |
2292 (setq term-insert-mode t)) | |
2293 ((eq term-terminal-parameter 47) | |
2294 (term-switch-to-alternate-sub-buffer t)))) | |
2295 ;; \E[?h - DEC Private Mode Reset | |
2296 ((eq char ?l) | |
2297 (cond ((eq term-terminal-parameter 4) | |
2298 (setq term-insert-mode nil)) | |
2299 ((eq term-terminal-parameter 47) | |
2300 (term-switch-to-alternate-sub-buffer nil)))) | |
2301 ;; \E[m - Set/reset standard mode | |
2302 ((eq char ?m) | |
2303 (cond ((eq term-terminal-parameter 7) | |
2304 (setq term-current-face 'highlight)) | |
2305 ((eq term-terminal-parameter 4) | |
2306 (setq term-current-face 'term-underline-face)) | |
2307 ((eq term-terminal-parameter 1) | |
2308 (setq term-current-face 'bold)) | |
2309 (t (setq term-current-face 'default)))) | |
2310 ;; \E[r - Set scrolling region | |
2311 ((eq char ?r) | |
2312 (term-scroll-region | |
2313 (1- term-terminal-previous-parameter) | |
2314 term-terminal-parameter)) | |
2315 (t))) | |
2316 | |
2317 (defun term-scroll-region (top bottom) | |
2318 "Set scrolling region. | |
2319 TOP is the top-most line (inclusive) of the new scrolling region, | |
2320 while BOTTOM is the line folling the new scrolling region (e.g. exclusive). | |
2321 The top-most line is line 0." | |
2322 (setq term-scroll-start | |
2323 (if (or (< top 0) (>= top term-height)) | |
2324 0 | |
2325 top)) | |
2326 (setq term-scroll-end | |
2327 (if (or (< bottom term-scroll-start) (> bottom term-height)) | |
2328 term-height | |
2329 bottom)) | |
2330 (setq term-scroll-with-delete | |
2331 (or (term-using-alternate-sub-buffer) | |
2332 (not (and (= term-scroll-start 0) | |
2333 (= term-scroll-end term-height)))))) | |
2334 | |
2335 (defun term-switch-to-alternate-sub-buffer (set) | |
2336 ;; If asked to switch to (from) the alternate sub-buffer, and already (not) | |
2337 ;; using it, do nothing. This test is needed for some programs (including | |
2338 ;; emacs) that emit the ti termcap string twice, for unknown reason. | |
2339 (if (eq set (not (term-using-alternate-sub-buffer))) | |
2340 (let ((row (term-current-row)) | |
2341 (col (term-horizontal-column))) | |
2342 (cond (set | |
2343 (goto-char (point-max)) | |
2344 (if (not (eq (preceding-char) ?\n)) | |
2345 (term-insert-char ?\n 1)) | |
2346 (setq term-scroll-with-delete t) | |
2347 (setq term-saved-home-marker (copy-marker term-home-marker)) | |
2348 (set-marker term-home-marker (point))) | |
2349 (t | |
2350 (setq term-scroll-with-delete | |
2351 (not (and (= term-scroll-start 0) | |
2352 (= term-scroll-end term-height)))) | |
2353 (set-marker term-home-marker term-saved-home-marker) | |
2354 (set-marker term-saved-home-marker nil) | |
2355 (setq term-saved-home-marker nil) | |
2356 (goto-char term-home-marker))) | |
2357 (setq term-current-column nil) | |
2358 (setq term-line-start-column nil) | |
2359 (setq term-current-row 0) | |
2360 (term-goto row col)))) | |
2361 | |
2362 ;; Default value for the symbol term-command-hook. | |
2363 | |
2364 (defun term-command-hook (string) | |
2365 (cond ((= (aref string 0) ?\032) | |
2366 ;; gdb (when invoked with -fullname) prints: | |
2367 ;; \032\032FULLFILENAME:LINENUMBER:CHARPOS:BEG_OR_MIDDLE:PC\n | |
2368 (let* ((first-colon (string-match ":" string 1)) | |
2369 (second-colon | |
2370 (string-match ":" string (1+ first-colon))) | |
2371 (filename (substring string 1 first-colon)) | |
2372 (fileline (string-to-int | |
2373 (substring string (1+ first-colon) second-colon)))) | |
2374 (setq term-pending-frame (cons filename fileline)))) | |
2375 ((= (aref string 0) ?/) | |
2376 (cd (substring string 1))) | |
2377 ((= (aref string 0) ?!) | |
2378 (eval (car (read-from-string string 1)))) | |
2379 (t)));; Otherwise ignore it | |
2380 | |
2381 ;; Make sure the file named TRUE-FILE is in a buffer that appears on the screen | |
2382 ;; and that its line LINE is visible. | |
2383 ;; Put the overlay-arrow on the line LINE in that buffer. | |
2384 ;; This is mainly used by gdb. | |
2385 | |
2386 (defun term-display-line (true-file line) | |
2387 (term-display-buffer-line (find-file-noselect true-file) line)) | |
2388 | |
2389 (defun term-display-buffer-line (buffer line) | |
2390 (let* ((window (display-buffer buffer t)) | |
2391 (pos)) | |
2392 (save-excursion | |
2393 (set-buffer buffer) | |
2394 (save-restriction | |
2395 (widen) | |
2396 (goto-line line) | |
2397 (setq pos (point)) | |
2398 (setq overlay-arrow-string "=>") | |
2399 (or overlay-arrow-position | |
2400 (setq overlay-arrow-position (make-marker))) | |
2401 (set-marker overlay-arrow-position (point) (current-buffer))) | |
2402 (cond ((or (< pos (point-min)) (> pos (point-max))) | |
2403 (widen) | |
2404 (goto-char pos)))) | |
2405 (set-window-point window overlay-arrow-position))) | |
2406 | |
2407 ;;; The buffer-local marker term-home-marker defines the "home position" | |
2408 ;;; (in terms of cursor motion). However, we move the term-home-marker | |
2409 ;;; "down" as needed so that is no more that a window-full above (point-max). | |
2410 | |
2411 (defun term-goto-home () | |
2412 (goto-char term-home-marker) | |
2413 (setq term-current-row 0) | |
2414 (setq term-current-column (current-column)) | |
2415 (setq term-start-line-column term-current-column)) | |
2416 | |
2417 ;;; FIXME: This can be optimized some. | |
2418 (defun term-goto (row col) | |
2419 (term-goto-home) | |
2420 (term-down row col)) | |
2421 | |
2422 ; The page is full, so enter "pager" mode, and wait for input. | |
2423 | |
2424 (defun term-process-pager () | |
2425 (if (not term-pager-break-map) | |
2426 (let* ((map (make-keymap)) | |
2427 (i 0)) | |
2428 ; (while (< i 128) | |
2429 ; (define-key map (make-string 1 i) 'term-send-raw) | |
2430 ; (setq i (1+ i))) | |
2431 (define-key map "\e" | |
2432 (lookup-key (current-global-map) "\e")) | |
2433 (define-key map "\C-x" | |
2434 (lookup-key (current-global-map) "\C-x")) | |
2435 (define-key map "\C-u" | |
2436 (lookup-key (current-global-map) "\C-u")) | |
2437 (define-key map " " 'term-pager-page) | |
2438 (define-key map "\r" 'term-pager-line) | |
2439 (define-key map "?" 'term-pager-help) | |
2440 (define-key map "h" 'term-pager-help) | |
2441 (define-key map "b" 'term-pager-back-page) | |
2442 (define-key map "\177" 'term-pager-back-line) | |
2443 (define-key map "q" 'term-pager-discard) | |
2444 (define-key map "D" 'term-pager-disable) | |
2445 (define-key map "<" 'term-pager-bob) | |
2446 (define-key map ">" 'term-pager-eob) | |
2447 (setq term-pager-break-map map))) | |
2448 ; (let ((process (get-buffer-process (current-buffer)))) | |
2449 ; (stop-process process)) | |
2450 (setq term-pager-old-local-map (current-local-map)) | |
2451 (use-local-map term-pager-break-map) | |
2452 (make-local-variable 'term-old-mode-line-format) | |
2453 (setq term-old-mode-line-format mode-line-format) | |
2454 (setq mode-line-format | |
2455 (list "-- **MORE** " | |
2456 mode-line-buffer-identification | |
2457 " [Type ? for help] " | |
2458 "%-"))) | |
2459 | |
2460 (defun term-pager-line (lines) | |
2461 (interactive "p") | |
2462 (let* ((moved (vertical-motion (1+ lines))) | |
2463 (deficit (- lines moved))) | |
2464 (if (> moved lines) | |
2465 (backward-char)) | |
2466 (cond ((<= deficit 0) ;; OK, had enough in the buffer for request. | |
2467 (recenter (1- term-height))) | |
2468 ((term-pager-continue deficit))))) | |
2469 | |
2470 (defun term-pager-page (arg) | |
2471 "Proceed past the **MORE** break, allowing the next page of output to appear" | |
2472 (interactive "p") | |
2473 (term-pager-line (* arg term-height))) | |
2474 | |
2475 ; Pager mode command to go to beginning of buffer | |
2476 (defun term-pager-bob () | |
2477 (interactive) | |
2478 (goto-char (point-min)) | |
2479 (if (= (vertical-motion term-height) term-height) | |
2480 (backward-char)) | |
2481 (recenter (1- term-height))) | |
2482 | |
2483 ; pager mode command to go to end of buffer | |
2484 (defun term-pager-eob () | |
2485 (interactive) | |
2486 (goto-char term-home-marker) | |
2487 (recenter 0) | |
2488 (goto-char (process-mark (get-buffer-process (current-buffer))))) | |
2489 | |
2490 (defun term-pager-back-line (lines) | |
2491 (interactive "p") | |
2492 (vertical-motion (- 1 lines)) | |
2493 (if (not (bobp)) | |
2494 (backward-char) | |
2495 (beep) | |
2496 ;; Move cursor to end of window. | |
2497 (vertical-motion term-height) | |
2498 (backward-char)) | |
2499 (recenter (1- term-height))) | |
2500 | |
2501 (defun term-pager-back-page (arg) | |
2502 (interactive "p") | |
2503 (term-pager-back-line (* arg term-height))) | |
2504 | |
2505 (defun term-pager-discard () | |
2506 (interactive) | |
2507 (setq term-terminal-parameter "") | |
2508 (interrupt-process nil t) | |
2509 (term-pager-continue term-height)) | |
2510 | |
2511 ; Disable pager processing. | |
2512 ; Only callable while in pager mode. (Contrast term-disable-pager.) | |
2513 (defun term-pager-disable () | |
2514 (interactive) | |
2515 (if (term-handling-pager) | |
2516 (term-pager-continue nil) | |
2517 (setq term-pager-count nil))) | |
2518 | |
2519 ; Enable pager processing. | |
2520 (defun term-pager-enable () | |
2521 (interactive) | |
2522 (or term-pager-count | |
2523 (setq term-pager-count 0))) ;; Or maybe set to (term-current-row) ?? | |
2524 | |
2525 (defun term-pager-help () | |
2526 "Provide help on commands available in a terminal-emulator **MORE** break" | |
2527 (interactive) | |
2528 (message "Terminal-emulator pager break help...") | |
2529 (sit-for 0) | |
2530 (with-electric-help | |
2531 (function (lambda () | |
2532 (princ (substitute-command-keys | |
2533 "\\<term-pager-break-map>\ | |
2534 Terminal-emulator MORE break.\n\ | |
2535 Type one of the following keys:\n\n\ | |
2536 \\[term-pager-page]\t\tMove forward one page.\n\ | |
2537 \\[term-pager-line]\t\tMove forward one line.\n\ | |
2538 \\[universal-argument] N \\[term-pager-page]\tMove N pages forward.\n\ | |
2539 \\[universal-argument] N \\[term-pager-line]\tMove N lines forward.\n\ | |
2540 \\[universal-argument] N \\[term-pager-back-line]\tMove N lines back.\n\ | |
2541 \\[universal-argument] N \\[term-pager-back-page]\t\tMove N pages back.\n\ | |
2542 \\[term-pager-bob]\t\tMove to the beginning of the buffer.\n\ | |
2543 \\[term-pager-eob]\t\tMove to the end of the buffer.\n\ | |
2544 \\[term-pager-discard]\t\tKill pending output and kill process.\n\ | |
2545 \\[term-pager-disable]\t\tDisable PAGER handling.\n\n\ | |
2546 \\{term-pager-break-map}\n\ | |
2547 Any other key is passed through to the program | |
2548 running under the terminal emulator and disables pager processing until | |
2549 all pending output has been dealt with.")) | |
2550 nil)))) | |
2551 | |
2552 (defun term-pager-continue (new-count) | |
2553 (let ((process (get-buffer-process (current-buffer)))) | |
2554 (use-local-map term-pager-old-local-map) | |
2555 (setq term-pager-old-local-map nil) | |
2556 (setq mode-line-format term-old-mode-line-format) | |
2557 (setq term-pager-count new-count) | |
2558 (set-process-filter process term-pager-old-filter) | |
2559 (funcall term-pager-old-filter process "") | |
2560 (continue-process process))) | |
2561 | |
2562 ;; Make sure there are DOWN blank lines below the current one. | |
2563 ;; Return 0 if we're unable (because of PAGER handling), else return DOWN. | |
2564 | |
2565 (defun term-handle-scroll (down) | |
2566 (let ((scroll-needed | |
2567 (- (+ (term-current-row) down 1) term-scroll-end))) | |
2568 (if (> scroll-needed 0) | |
2569 (let ((save-point (copy-marker (point))) (save-top)) | |
2570 (goto-char term-home-marker) | |
2571 (cond (term-scroll-with-delete | |
2572 ;; delete scroll-needed lines at term-scroll-start | |
2573 (term-vertical-motion term-scroll-start) | |
2574 (setq save-top (point)) | |
2575 (term-vertical-motion scroll-needed) | |
2576 (delete-region save-top (point)) | |
2577 (goto-char save-point) | |
2578 (term-vertical-motion down) | |
2579 (term-insert-char ?\n scroll-needed)) | |
2580 ((and (numberp term-pager-count) | |
2581 (< (setq term-pager-count (- term-pager-count down)) | |
2582 0)) | |
2583 (setq down 0) | |
2584 (term-process-pager)) | |
2585 (t | |
2586 (term-vertical-motion scroll-needed) | |
2587 (set-marker term-home-marker (point)))) | |
2588 (goto-char save-point) | |
2589 (set-marker save-point nil) | |
2590 (setq term-current-column nil) | |
2591 (setq term-line-start-column nil) | |
2592 (setq term-current-row nil)))) | |
2593 down) | |
2594 | |
2595 (defun term-down (down right &optional check-for-scroll) | |
2596 "Move down DOWN screen lines vertically, and RIGHT columns horizontally." | |
2597 (let ((start-column (term-horizontal-column))) | |
2598 (if check-for-scroll | |
2599 (setq down (term-handle-scroll down))) | |
2600 (term-adjust-current-row-cache down) | |
2601 (setq down (- down (term-vertical-motion down))) | |
2602 ; Extend buffer with extra blank lines if needed. | |
2603 (if (> down 0) (term-insert-char ?\n down)) | |
2604 (setq term-current-column nil) | |
2605 (setq term-start-line-column (current-column)) | |
2606 (move-to-column (+ term-start-line-column start-column right) t))) | |
2607 | |
2608 ;; Assuming point is at the beginning of a screen line, | |
2609 ;; if the line above point wraps around, add a ?\n to undo the wrapping. | |
2610 ;; FIXME: Probably should be called more than it is. | |
2611 (defun term-unwrap-line () | |
2612 (if (not (bolp)) (insert-before-markers ?\n))) | |
2613 | |
2614 (defun term-erase-in-line (kind) | |
2615 (if (> kind 1) ;; erase left of point | |
2616 (let ((cols (term-horizontal-column)) (saved-point (point)) | |
2617 (term-vertical-motion 0) | |
2618 (delete-region (point) saved-point) | |
2619 (term-insert-char ?\n cols)))) | |
2620 (if (not (eq kind 1)) ;; erase right of point | |
2621 (let ((saved-point (point)) | |
2622 (wrapped (and (zerop (term-horizontal-column)) | |
2623 (not (zerop (term-current-column)))))) | |
2624 (term-vertical-motion 1) | |
2625 (delete-region saved-point (point)) | |
2626 ;; wrapped is true if we're at the beginning of screen line, | |
2627 ;; but not a buffer line. If we delete the current screen line | |
2628 ;; that will make the previous line no longer wrap, and (because | |
2629 ;; of the way emacs display works) point will be at the end of | |
2630 ;; the previous screen line rather then the beginning of the | |
2631 ;; current one. To avoid that, we make sure that current line | |
2632 ;; contain a space, to force the previous line to continue to wrap. | |
2633 ;; We could do this always, but it seems preferable to not add the | |
2634 ;; extra space when wrapped is false. | |
2635 (if wrapped | |
2636 (insert ? )) | |
2637 (insert ?\n) | |
2638 (put-text-property saved-point (point) 'face 'default) | |
2639 (goto-char saved-point)))) | |
2640 | |
2641 (defun term-erase-in-display (kind) | |
2642 "Erases (that is blanks out) part of the window. | |
2643 If KIND is 0, erase from (point) to (point-max); | |
2644 if KIND is 1, erase from home to point; else erase from home to point-max. | |
2645 Should only be called when point is at the start of a screen line." | |
2646 (cond ((eq term-terminal-parameter 0) | |
2647 (delete-region (point) (point-max)) | |
2648 (term-unwrap-line)) | |
2649 ((let ((row (term-current-row)) | |
2650 (col (term-horizontal-column)) | |
2651 (start-region term-home-marker) | |
2652 (end-region (if (eq kind 1) (point) (point-max)))) | |
2653 (delete-region start-region end-region) | |
2654 (term-unwrap-line) | |
2655 (if (eq kind 1) | |
2656 (term-insert-char \?n row)) | |
2657 (setq term-current-column nil) | |
2658 (setq term-line-start-column nil) | |
2659 (setq term-current-row nil) | |
2660 (term-goto row col))))) | |
2661 | |
2662 (defun term-delete-chars (count) | |
2663 (let ((save-point (point))) | |
2664 (term-vertical-motion 1) | |
2665 (term-unwrap-line) | |
2666 (goto-char save-point) | |
2667 (move-to-column (+ (term-current-column) count) t) | |
2668 (delete-region save-point (point)))) | |
2669 | |
2670 (defun term-insert-spaces (count) | |
2671 (let ((save-point (point)) (save-eol)) | |
2672 (term-vertical-motion 1) | |
2673 (if (bolp) | |
2674 (backward-char)) | |
2675 (setq save-eol (point)) | |
2676 (move-to-column (+ (term-start-line-column) (- term-width count)) t) | |
2677 (if (> save-eol (point)) | |
2678 (delete-region (point) save-eol)) | |
2679 (goto-char save-point) | |
2680 (term-insert-char ? count) | |
2681 (goto-char save-point))) | |
2682 | |
2683 (defun term-delete-lines (lines) | |
2684 (let ((start (point)) | |
2685 (save-current-column term-current-column) | |
2686 (save-start-line-column term-start-line-column) | |
2687 (save-current-row (term-current-row))) | |
2688 (term-down lines 0) | |
2689 (delete-region start (point)) | |
2690 (term-down (- term-scroll-end save-current-row lines) 0) | |
2691 (term-insert-char ?\n lines) | |
2692 (setq term-current-column save-current-column) | |
2693 (setq term-start-line-column save-start-line-column) | |
2694 (setq term-current-row save-current-row) | |
2695 (goto-char start))) | |
2696 | |
2697 (defun term-insert-lines (lines) | |
2698 (let ((start (point)) | |
2699 (start-deleted) | |
2700 (save-current-column term-current-column) | |
2701 (save-start-line-column term-start-line-column) | |
2702 (save-current-row (term-current-row))) | |
2703 (term-down (- term-scroll-end save-current-row lines) 0) | |
2704 (setq start-deleted (point)) | |
2705 (term-down lines 0) | |
2706 (delete-region start-deleted (point)) | |
2707 (goto-char start) | |
2708 (setq term-current-column save-current-column) | |
2709 (setq term-start-line-column save-start-line-column) | |
2710 (setq term-current-row save-current-row) | |
2711 (term-insert-char ?\n lines) | |
2712 (goto-char start))) | |
2713 | |
2714 (defun term-set-output-log (name) | |
2715 "Record raw inferior process output in a buffer." | |
2716 (interactive (list (if term-log-buffer | |
2717 nil | |
2718 (read-buffer "Record output in buffer: " | |
2719 (format "%s output-log" | |
2720 (buffer-name (current-buffer))) | |
2721 nil)))) | |
2722 (if (or (null name) (equal name "")) | |
2723 (progn (setq term-log-buffer nil) | |
2724 (message "Output logging off.")) | |
2725 (if (get-buffer name) | |
2726 nil | |
2727 (save-excursion | |
2728 (set-buffer (get-buffer-create name)) | |
2729 (fundamental-mode) | |
2730 (buffer-disable-undo (current-buffer)) | |
2731 (erase-buffer))) | |
2732 (setq term-log-buffer (get-buffer name)) | |
2733 (message "Recording terminal emulator output into buffer \"%s\"" | |
2734 (buffer-name term-log-buffer)))) | |
2735 | |
2736 (defun term-stop-photo () | |
2737 "Discontinue raw inferior process logging." | |
2738 (interactive) | |
2739 (term-set-output-log nil)) | |
2740 | |
2741 (defun term-show-maximum-output () | |
2742 "Put the end of the buffer at the bottom of the window." | |
2743 (interactive) | |
2744 (goto-char (point-max)) | |
2745 (recenter -1)) | |
2746 | |
2747 ;;; Do the user's customisation... | |
2748 | |
2749 (defvar term-load-hook nil | |
2750 "This hook is run when term is loaded in. | |
2751 This is a good place to put keybindings.") | |
2752 | |
2753 (run-hooks 'term-load-hook) | |
2754 | |
2755 | |
2756 ;;; Filename/command/history completion in a buffer | |
2757 ;;; =========================================================================== | |
2758 ;;; Useful completion functions, courtesy of the Ergo group. | |
2759 | |
2760 ;;; Six commands: | |
2761 ;;; term-dynamic-complete Complete or expand command, filename, | |
2762 ;;; history at point. | |
2763 ;;; term-dynamic-complete-filename Complete filename at point. | |
2764 ;;; term-dynamic-list-filename-completions List completions in help buffer. | |
2765 ;;; term-replace-by-expanded-filename Expand and complete filename at point; | |
2766 ;;; replace with expanded/completed name. | |
2767 ;;; term-dynamic-simple-complete Complete stub given candidates. | |
2768 | |
2769 ;;; These are not installed in the term-mode keymap. But they are | |
2770 ;;; available for people who want them. Shell-mode installs them: | |
2771 ;;; (define-key shell-mode-map "\t" 'term-dynamic-complete) | |
2772 ;;; (define-key shell-mode-map "\M-?" | |
2773 ;;; 'term-dynamic-list-filename-completions))) | |
2774 ;;; | |
2775 ;;; Commands like this are fine things to put in load hooks if you | |
2776 ;;; want them present in specific modes. | |
2777 | |
2778 (defvar term-completion-autolist nil | |
2779 "*If non-nil, automatically list possiblities on partial completion. | |
2780 This mirrors the optional behavior of tcsh.") | |
2781 | |
2782 (defvar term-completion-addsuffix t | |
2783 "*If non-nil, add a `/' to completed directories, ` ' to file names. | |
2784 This mirrors the optional behavior of tcsh.") | |
2785 | |
2786 (defvar term-completion-recexact nil | |
2787 "*If non-nil, use shortest completion if characters cannot be added. | |
2788 This mirrors the optional behavior of tcsh. | |
2789 | |
2790 A non-nil value is useful if `term-completion-autolist' is non-nil too.") | |
2791 | |
2792 (defvar term-completion-fignore nil | |
2793 "*List of suffixes to be disregarded during file completion. | |
2794 This mirrors the optional behavior of bash and tcsh. | |
2795 | |
2796 Note that this applies to `term-dynamic-complete-filename' only.") | |
2797 | |
2798 (defvar term-file-name-prefix "" | |
2799 "Prefix prepended to absolute file names taken from process input. | |
2800 This is used by term's and shell's completion functions, and by shell's | |
2801 directory tracking functions.") | |
2802 | |
2803 | |
2804 (defun term-directory (directory) | |
2805 ;; Return expanded DIRECTORY, with `term-file-name-prefix' if absolute. | |
2806 (expand-file-name (if (file-name-absolute-p directory) | |
2807 (concat term-file-name-prefix directory) | |
2808 directory))) | |
2809 | |
2810 | |
2811 (defun term-word (word-chars) | |
2812 "Return the word of WORD-CHARS at point, or nil if non is found. | |
2813 Word constituents are considered to be those in WORD-CHARS, which is like the | |
2814 inside of a \"[...]\" (see `skip-chars-forward')." | |
2815 (save-excursion | |
2816 (let ((limit (point)) | |
2817 (word (concat "[" word-chars "]")) | |
2818 (non-word (concat "[^" word-chars "]"))) | |
2819 (if (re-search-backward non-word nil 'move) | |
2820 (forward-char 1)) | |
2821 ;; Anchor the search forwards. | |
2822 (if (or (eolp) (looking-at non-word)) | |
2823 nil | |
2824 (re-search-forward (concat word "+") limit) | |
2825 (buffer-substring (match-beginning 0) (match-end 0)))))) | |
2826 | |
2827 | |
2828 (defun term-match-partial-filename () | |
2829 "Return the filename at point, or nil if non is found. | |
2830 Environment variables are substituted. See `term-word'." | |
2831 (let ((filename (term-word "~/A-Za-z0-9+@:_.$#,={}-"))) | |
2832 (and filename (substitute-in-file-name filename)))) | |
2833 | |
2834 | |
2835 (defun term-dynamic-complete () | |
2836 "Dynamically perform completion at point. | |
2837 Calls the functions in `term-dynamic-complete-functions' to perform | |
2838 completion until a function returns non-nil, at which point completion is | |
2839 assumed to have occurred." | |
2840 (interactive) | |
2841 (let ((functions term-dynamic-complete-functions)) | |
2842 (while (and functions (null (funcall (car functions)))) | |
2843 (setq functions (cdr functions))))) | |
2844 | |
2845 | |
2846 (defun term-dynamic-complete-filename () | |
2847 "Dynamically complete the filename at point. | |
2848 Completes if after a filename. See `term-match-partial-filename' and | |
2849 `term-dynamic-complete-as-filename'. | |
2850 This function is similar to `term-replace-by-expanded-filename', except that | |
2851 it won't change parts of the filename already entered in the buffer; it just | |
2852 adds completion characters to the end of the filename. A completions listing | |
2853 may be shown in a help buffer if completion is ambiguous. | |
2854 | |
2855 Completion is dependent on the value of `term-completion-addsuffix', | |
2856 `term-completion-recexact' and `term-completion-fignore', and the timing of | |
2857 completions listing is dependent on the value of `term-completion-autolist'. | |
2858 | |
2859 Returns t if successful." | |
2860 (interactive) | |
2861 (if (term-match-partial-filename) | |
2862 (prog2 (or (eq (selected-window) (minibuffer-window)) | |
2863 (message "Completing file name...")) | |
2864 (term-dynamic-complete-as-filename)))) | |
2865 | |
2866 (defun term-dynamic-complete-as-filename () | |
2867 "Dynamically complete at point as a filename. | |
2868 See `term-dynamic-complete-filename'. Returns t if successful." | |
2869 (let* ((completion-ignore-case nil) | |
2870 (completion-ignored-extensions term-completion-fignore) | |
2871 (success t) | |
2872 (filename (or (term-match-partial-filename) "")) | |
2873 (pathdir (file-name-directory filename)) | |
2874 (pathnondir (file-name-nondirectory filename)) | |
2875 (directory (if pathdir (term-directory pathdir) default-directory)) | |
2876 (completion (file-name-completion pathnondir directory)) | |
2877 (mini-flag (eq (selected-window) (minibuffer-window)))) | |
2878 (cond ((null completion) | |
2879 (message "No completions of %s" filename) | |
2880 (setq success nil)) | |
2881 ((eq completion t) ; Means already completed "file". | |
2882 (if term-completion-addsuffix (insert " ")) | |
2883 (or mini-flag (message "Sole completion"))) | |
2884 ((string-equal completion "") ; Means completion on "directory/". | |
2885 (term-dynamic-list-filename-completions)) | |
2886 (t ; Completion string returned. | |
2887 (let ((file (concat (file-name-as-directory directory) completion))) | |
2888 (insert (substring (directory-file-name completion) | |
2889 (length pathnondir))) | |
2890 (cond ((symbolp (file-name-completion completion directory)) | |
2891 ;; We inserted a unique completion. | |
2892 (if term-completion-addsuffix | |
2893 (insert (if (file-directory-p file) "/" " "))) | |
2894 (or mini-flag (message "Completed"))) | |
2895 ((and term-completion-recexact term-completion-addsuffix | |
2896 (string-equal pathnondir completion) | |
2897 (file-exists-p file)) | |
2898 ;; It's not unique, but user wants shortest match. | |
2899 (insert (if (file-directory-p file) "/" " ")) | |
2900 (or mini-flag (message "Completed shortest"))) | |
2901 ((or term-completion-autolist | |
2902 (string-equal pathnondir completion)) | |
2903 ;; It's not unique, list possible completions. | |
2904 (term-dynamic-list-filename-completions)) | |
2905 (t | |
2906 (or mini-flag (message "Partially completed"))))))) | |
2907 success)) | |
2908 | |
2909 | |
2910 (defun term-replace-by-expanded-filename () | |
2911 "Dynamically expand and complete the filename at point. | |
2912 Replace the filename with an expanded, canonicalised and completed replacement. | |
2913 \"Expanded\" means environment variables (e.g., $HOME) and `~'s are replaced | |
2914 with the corresponding directories. \"Canonicalised\" means `..' and `.' are | |
2915 removed, and the filename is made absolute instead of relative. For expansion | |
2916 see `expand-file-name' and `substitute-in-file-name'. For completion see | |
2917 `term-dynamic-complete-filename'." | |
2918 (interactive) | |
2919 (replace-match (expand-file-name (term-match-partial-filename)) t t) | |
2920 (term-dynamic-complete-filename)) | |
2921 | |
2922 | |
2923 (defun term-dynamic-simple-complete (stub candidates) | |
2924 "Dynamically complete STUB from CANDIDATES list. | |
2925 This function inserts completion characters at point by completing STUB from | |
2926 the strings in CANDIDATES. A completions listing may be shown in a help buffer | |
2927 if completion is ambiguous. | |
2928 | |
2929 Returns nil if no completion was inserted. | |
2930 Returns `sole' if completed with the only completion match. | |
2931 Returns `shortest' if completed with the shortest of the completion matches. | |
2932 Returns `partial' if completed as far as possible with the completion matches. | |
2933 Returns `listed' if a completion listing was shown. | |
2934 | |
2935 See also `term-dynamic-complete-filename'." | |
2936 (let* ((completion-ignore-case nil) | |
2937 (candidates (mapcar (function (lambda (x) (list x))) candidates)) | |
2938 (completions (all-completions stub candidates))) | |
2939 (cond ((null completions) | |
2940 (message "No completions of %s" stub) | |
2941 nil) | |
2942 ((= 1 (length completions)) ; Gotcha! | |
2943 (let ((completion (car completions))) | |
2944 (if (string-equal completion stub) | |
2945 (message "Sole completion") | |
2946 (insert (substring completion (length stub))) | |
2947 (message "Completed")) | |
2948 (if term-completion-addsuffix (insert " ")) | |
2949 'sole)) | |
2950 (t ; There's no unique completion. | |
2951 (let ((completion (try-completion stub candidates))) | |
2952 ;; Insert the longest substring. | |
2953 (insert (substring completion (length stub))) | |
2954 (cond ((and term-completion-recexact term-completion-addsuffix | |
2955 (string-equal stub completion) | |
2956 (member completion completions)) | |
2957 ;; It's not unique, but user wants shortest match. | |
2958 (insert " ") | |
2959 (message "Completed shortest") | |
2960 'shortest) | |
2961 ((or term-completion-autolist | |
2962 (string-equal stub completion)) | |
2963 ;; It's not unique, list possible completions. | |
2964 (term-dynamic-list-completions completions) | |
2965 'listed) | |
2966 (t | |
2967 (message "Partially completed") | |
2968 'partial))))))) | |
2969 | |
2970 | |
2971 (defun term-dynamic-list-filename-completions () | |
2972 "List in help buffer possible completions of the filename at point." | |
2973 (interactive) | |
2974 (let* ((completion-ignore-case nil) | |
2975 (filename (or (term-match-partial-filename) "")) | |
2976 (pathdir (file-name-directory filename)) | |
2977 (pathnondir (file-name-nondirectory filename)) | |
2978 (directory (if pathdir (term-directory pathdir) default-directory)) | |
2979 (completions (file-name-all-completions pathnondir directory))) | |
2980 (if completions | |
2981 (term-dynamic-list-completions completions) | |
2982 (message "No completions of %s" filename)))) | |
2983 | |
2984 | |
2985 (defun term-dynamic-list-completions (completions) | |
2986 "List in help buffer sorted COMPLETIONS. | |
2987 Typing SPC flushes the help buffer." | |
2988 (let ((conf (current-window-configuration))) | |
2989 (with-output-to-temp-buffer "*Completions*" | |
2990 (display-completion-list (sort completions 'string-lessp))) | |
2991 (message "Hit space to flush") | |
2992 (let (key first) | |
2993 (if (save-excursion | |
2994 (set-buffer (get-buffer "*Completions*")) | |
2995 (setq key (read-key-sequence nil) | |
2996 first (aref key 0)) | |
2997 (and (consp first) | |
2998 (eq (window-buffer (posn-window (event-start first))) | |
2999 (get-buffer "*Completions*")) | |
3000 (eq (key-binding key) 'mouse-choose-completion))) | |
3001 ;; If the user does mouse-choose-completion with the mouse, | |
3002 ;; execute the command, then delete the completion window. | |
3003 (progn | |
3004 (mouse-choose-completion first) | |
3005 (set-window-configuration conf)) | |
3006 (if (eq first ?\ ) | |
3007 (set-window-configuration conf) | |
3008 (setq unread-command-events (listify-key-sequence key))))))) | |
3009 | |
3010 ;;; Converting process modes to use term mode | |
3011 ;;; =========================================================================== | |
3012 ;;; Renaming variables | |
3013 ;;; Most of the work is renaming variables and functions. These are the common | |
3014 ;;; ones: | |
3015 ;;; Local variables: | |
3016 ;;; last-input-start term-last-input-start | |
3017 ;;; last-input-end term-last-input-end | |
3018 ;;; shell-prompt-pattern term-prompt-regexp | |
3019 ;;; shell-set-directory-error-hook <no equivalent> | |
3020 ;;; Miscellaneous: | |
3021 ;;; shell-set-directory <unnecessary> | |
3022 ;;; shell-mode-map term-mode-map | |
3023 ;;; Commands: | |
3024 ;;; shell-send-input term-send-input | |
3025 ;;; shell-send-eof term-delchar-or-maybe-eof | |
3026 ;;; kill-shell-input term-kill-input | |
3027 ;;; interrupt-shell-subjob term-interrupt-subjob | |
3028 ;;; stop-shell-subjob term-stop-subjob | |
3029 ;;; quit-shell-subjob term-quit-subjob | |
3030 ;;; kill-shell-subjob term-kill-subjob | |
3031 ;;; kill-output-from-shell term-kill-output | |
3032 ;;; show-output-from-shell term-show-output | |
3033 ;;; copy-last-shell-input Use term-previous-input/term-next-input | |
3034 ;;; | |
3035 ;;; SHELL-SET-DIRECTORY is gone, its functionality taken over by | |
3036 ;;; SHELL-DIRECTORY-TRACKER, the shell mode's term-input-filter-functions. | |
3037 ;;; Term mode does not provide functionality equivalent to | |
3038 ;;; shell-set-directory-error-hook; it is gone. | |
3039 ;;; | |
3040 ;;; term-last-input-start is provided for modes which want to munge | |
3041 ;;; the buffer after input is sent, perhaps because the inferior | |
3042 ;;; insists on echoing the input. The LAST-INPUT-START variable in | |
3043 ;;; the old shell package was used to implement a history mechanism, | |
3044 ;;; but you should think twice before using term-last-input-start | |
3045 ;;; for this; the input history ring often does the job better. | |
3046 ;;; | |
3047 ;;; If you are implementing some process-in-a-buffer mode, called foo-mode, do | |
3048 ;;; *not* create the term-mode local variables in your foo-mode function. | |
3049 ;;; This is not modular. Instead, call term-mode, and let *it* create the | |
3050 ;;; necessary term-specific local variables. Then create the | |
3051 ;;; foo-mode-specific local variables in foo-mode. Set the buffer's keymap to | |
3052 ;;; be foo-mode-map, and its mode to be foo-mode. Set the term-mode hooks | |
3053 ;;; (term-{prompt-regexp, input-filter, input-filter-functions, | |
3054 ;;; get-old-input) that need to be different from the defaults. Call | |
3055 ;;; foo-mode-hook, and you're done. Don't run the term-mode hook yourself; | |
3056 ;;; term-mode will take care of it. The following example, from shell.el, | |
3057 ;;; is typical: | |
3058 ;;; | |
3059 ;;; (defvar shell-mode-map '()) | |
3060 ;;; (cond ((not shell-mode-map) | |
3061 ;;; (setq shell-mode-map (copy-keymap term-mode-map)) | |
3062 ;;; (define-key shell-mode-map "\C-c\C-f" 'shell-forward-command) | |
3063 ;;; (define-key shell-mode-map "\C-c\C-b" 'shell-backward-command) | |
3064 ;;; (define-key shell-mode-map "\t" 'term-dynamic-complete) | |
3065 ;;; (define-key shell-mode-map "\M-?" | |
3066 ;;; 'term-dynamic-list-filename-completions))) | |
3067 ;;; | |
3068 ;;; (defun shell-mode () | |
3069 ;;; (interactive) | |
3070 ;;; (term-mode) | |
3071 ;;; (setq term-prompt-regexp shell-prompt-pattern) | |
3072 ;;; (setq major-mode 'shell-mode) | |
3073 ;;; (setq mode-name "Shell") | |
3074 ;;; (use-local-map shell-mode-map) | |
3075 ;;; (make-local-variable 'shell-directory-stack) | |
3076 ;;; (setq shell-directory-stack nil) | |
3077 ;;; (add-hook 'term-input-filter-functions 'shell-directory-tracker) | |
3078 ;;; (run-hooks 'shell-mode-hook)) | |
3079 ;;; | |
3080 ;;; | |
3081 ;;; Note that make-term is different from make-shell in that it | |
3082 ;;; doesn't have a default program argument. If you give make-shell | |
3083 ;;; a program name of NIL, it cleverly chooses one of explicit-shell-name, | |
3084 ;;; $ESHELL, $SHELL, or /bin/sh. If you give make-term a program argument | |
3085 ;;; of NIL, it barfs. Adjust your code accordingly... | |
3086 ;;; | |
3087 ;;; Completion for term-mode users | |
3088 ;;; | |
3089 ;;; For modes that use term-mode, term-dynamic-complete-functions is the | |
3090 ;;; hook to add completion functions to. Functions on this list should return | |
3091 ;;; non-nil if completion occurs (i.e., further completion should not occur). | |
3092 ;;; You could use term-dynamic-simple-complete to do the bulk of the | |
3093 ;;; completion job. | |
3094 | |
3095 (provide 'term) | |
3096 | |
3097 ;;; term.el ends here | |
3098 |