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