1 ;; term.el --- general command interpreter in a window stuff
2 ;; Copyright (C) 1988, 1990, 1992, 1994, 1995 Free Software Foundation, Inc.
4 ;; Author: Per Bothner <bothner@cygnus.com>
5 ;; Based on comint mode written by: Olin Shivers <shivers@cs.cmu.edu>
8 ;; This file is part of GNU Emacs.
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)
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.
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.
26 ;;; The changelog is at the end of this file.
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)
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.).
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.
44 ;;; Brief Command Documentation:
45 ;;;============================================================================
46 ;;; Term Mode Commands: (common to all derived modes, like cmushell & cmulisp
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
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).
78 ;;; term-mode-hook is the term mode hook. Basically for your keybindings.
79 ;;; term-load-hook is run after loading in this package.
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.
85 (defconst term-version
"0.94")
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...
112 (defvar explicit-shell-file-name nil
113 "*If non-nil, is file name to use for explicitly requested inferior shell.")
115 (defvar term-prompt-regexp
"^"
116 "Regexp to recognise prompts in the inferior process.
117 Defaults to \"^\", the null string at BOL.
120 Canonical Lisp: \"^[^> \\n]*>+:? *\" (Lucid, franz, kcl, T, cscheme, oaklisp)
121 Lucid Common Lisp: \"^\\\\(>\\\\|\\\\(->\\\\)+\\\\) *\"
122 franz: \"^\\\\(->\\\\|<[0-9]*>:\\\\) *\"
124 shell: \"^[^#$%>\\n]*[#$%>] *\"
127 This is a good thing to set in mode hooks.")
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.
136 For shells, a good value is (?\\| ?& ?< ?> ?\\( ?\\) ?;).
138 This is a good thing to set in mode hooks.")
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).
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'.
149 This variable is buffer-local.")
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.
155 This variable is buffer-local.")
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'.
161 This variable is buffer-local, and is a good thing to set in mode hooks.")
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.
171 See variable `term-scroll-show-maximum-output'.
172 This variable is buffer-local.")
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.
178 See variable `term-scroll-to-bottom-on-output'.
179 This variable is buffer-local.")
181 (defvar term-input-ring-size
32
182 "Size of input history ring.")
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
)
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")
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'.
203 This is a good thing to set in mode hooks.")
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")
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.
215 This variable is buffer-local.")
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.")
224 (defvar term-mode-hook
'()
225 "Called upon entry into term-mode
226 This is run before the process is cranked up.")
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.")
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
)
241 (defvar term-pager-break-map nil
)
244 "True if communications via pty; false if by pipe. Buffer local.
245 This is to work around a bug in emacs process signalling.")
247 (defvar term-last-input-match
""
248 "Last string searched for by term input history search, for defaulting.
249 Buffer local variable.")
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
)
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
)
271 ;; Do FORMS if running under Emacs-19.
272 (defmacro term-if-emacs19
(&rest forms
)
273 (if (string-match "^19" emacs-version
) (cons 'progn forms
)))
274 ;; True if running under XEmacs (previously Lucid emacs).
275 (defmacro term-is-xemacs
() '(string-match "Lucid" emacs-version
))
276 ;; Do FORM if running under XEmacs (previously Lucid emacs).
277 (defmacro term-if-xemacs
(&rest forms
)
278 (if (term-is-xemacs) (cons 'progn forms
)))
279 ;; Do FORM if NOT running under XEmacs (previously Lucid emacs).
280 (defmacro term-ifnot-xemacs
(&rest forms
)
281 (if (not (term-is-xemacs)) (cons 'progn forms
)))
283 (defmacro term-in-char-mode
() '(eq (current-local-map) term-raw-map
))
284 (defmacro term-in-line-mode
() '(not (term-in-char-mode)))
287 (defvar term-terminal-menu
289 [ "Character mode" term-char-mode
(term-in-line-mode)]
290 [ "Line mode" term-line-mode
(term-in-char-mode)]
291 [ "Enable paging" term-pager-toggle
(not term-pager-count
)]
292 [ "Disable paging" term-pager-toggle term-pager-count
])))
295 "Major mode for interacting with an inferior interpreter.
296 Interpreter name is same as buffer name, sans the asterisks.
297 In line sub-mode, return at end of buffer sends line as input,
298 while return not at end copies rest of line to end and sends it.
299 In char sub-mode, each character (except `term-escape-char`) is
302 This mode is typically customised to create inferior-lisp-mode,
303 shell-mode, etc.. This can be done by setting the hooks
304 term-input-filter-functions, term-input-filter, term-input-sender and
305 term-get-old-input to appropriate functions, and the variable
306 term-prompt-regexp to the appropriate regular expression.
308 An input history is maintained of size `term-input-ring-size', and
309 can be accessed with the commands \\[term-next-input], \\[term-previous-input], and \\[term-dynamic-list-input-ring].
310 Input ring history expansion can be achieved with the commands
311 \\[term-replace-by-expanded-history] or \\[term-magic-space].
312 Input ring expansion is controlled by the variable `term-input-autoexpand',
313 and addition is controlled by the variable `term-input-ignoredups'.
315 Input to, and output from, the subprocess can cause the window to scroll to
316 the end of the buffer. See variables `term-scroll-to-bottom-on-input',
317 and `term-scroll-to-bottom-on-output'.
319 If you accidentally suspend your process, use \\[term-continue-subjob]
324 Entry to this mode runs the hooks on term-mode-hook"
326 ;; Do not remove this. All major modes must do this.
327 (kill-all-local-variables)
328 (setq major-mode
'term-mode
)
329 (setq mode-name
"Term")
330 (setq mode-line-process
'(": line %s"))
331 (use-local-map term-mode-map
)
332 (make-local-variable 'term-home-marker
)
333 (setq term-home-marker
(copy-marker 0))
334 (make-local-variable 'term-saved-home-marker
)
335 (setq term-saved-home-marker nil
)
336 (make-local-variable 'term-height
)
337 (make-local-variable 'term-width
)
338 (setq term-width
(1- (window-width)))
339 (setq term-height
(1- (window-height)))
340 (make-local-variable 'term-terminal-parameter
)
341 (make-local-variable 'term-saved-cursor
)
342 (setq term-saved-cursor nil
)
343 (make-local-variable 'term-last-input-start
)
344 (setq term-last-input-start
(make-marker))
345 (make-local-variable 'term-last-input-end
)
346 (setq term-last-input-end
(make-marker))
347 (make-local-variable 'term-last-input-match
)
348 (setq term-last-input-match
"")
349 (make-local-variable 'term-prompt-regexp
) ; Don't set; default
350 (make-local-variable 'term-input-ring-size
) ; ...to global val.
351 (make-local-variable 'term-input-ring
)
352 (make-local-variable 'term-input-ring-file-name
)
353 (or (and (boundp 'term-input-ring
) term-input-ring
)
354 (setq term-input-ring
(make-ring term-input-ring-size
)))
355 (make-local-variable 'term-input-ring-index
)
356 (or (and (boundp 'term-input-ring-index
) term-input-ring-index
)
357 (setq term-input-ring-index nil
))
359 (make-local-variable 'term-command-hook
)
360 (setq term-command-hook
(symbol-function 'term-command-hook
))
362 ;; state 0: Normal state
363 ;; state 1: Last character was a graphic in the last column.
364 ;; If next char is graphic, first move one column right
365 ;; (and line warp) before displaying it.
366 ;; This emulates (more or less) the behavior of xterm.
368 ;; state 3: seen ESC [ (or ESC [ ?)
369 ;; state 4: term-terminal-parameter contains pending output.
370 (make-local-variable 'term-terminal-state
)
371 (setq term-terminal-state
0)
373 ;; A queue of strings whose echo we want suppressed.
374 (make-local-variable 'term-kill-echo-list
)
375 (setq term-kill-echo-list nil
)
377 ;; (current-column) at start of screen line, or nil if unknown.
378 (make-local-variable 'term-start-line-column
)
379 (setq term-start-line-column
0)
380 ;; Cache for (current-column), or nil if unknown.
381 (make-local-variable 'term-current-column
)
382 (setq term-current-column
0)
383 ;; Current vertical row (from home-marker) or nil if unknown.
384 (make-local-variable 'term-current-row
)
385 (setq term-current-row
0)
386 (make-local-variable 'term-log-buffer
)
387 (setq term-log-buffer nil
)
388 (make-local-variable 'term-scroll-start
)
389 (setq term-scroll-start
0)
390 (make-local-variable 'term-scroll-end
)
391 (setq term-scroll-end term-height
)
392 ;; term-scroll-with-delete is t if forward scrolling should
393 ;; be implemented by delete to top-most line(s); and nil if
394 ;; scrolling should be implemented by moving term-home-marker.
395 ;; It is set to t iff there is a (non-default) scroll-region
396 ;; OR the alternate buffer is used.
397 (make-local-variable 'term-scroll-with-delete
)
398 (setq term-scroll-with-delete nil
)
399 (make-local-variable 'term-pager-count
)
400 ;;(setq term-pager-count 0)
401 (setq term-pager-count nil
)
402 ;; Used to save the old keymap when doing PAGER processing.
403 (make-local-variable 'term-pager-old-local-map
)
404 (setq term-pager-old-local-map nil
)
405 ;; Used to save the old keymap when in char mode.
406 (make-local-variable 'term-old-mode-map
)
407 (setq term-old-mode-map nil
)
408 (make-local-variable 'term-insert-mode
)
409 (setq term-insert-mode nil
)
410 (make-local-variable 'term-dynamic-complete-functions
)
411 (make-local-variable 'term-completion-fignore
)
412 (make-local-variable 'term-get-old-input
)
413 (make-local-variable 'term-matching-input-from-input-string
)
414 (make-local-variable 'term-input-autoexpand
)
415 (make-local-variable 'term-input-ignoredups
)
416 (make-local-variable 'term-delimiter-argument-list
)
417 (make-local-variable 'term-input-filter-functions
)
418 (make-local-variable 'term-input-filter
)
419 (make-local-variable 'term-input-sender
)
420 (make-local-variable 'term-scroll-to-bottom-on-output
)
421 (make-local-variable 'term-scroll-show-maximum-output
)
422 (make-local-variable 'term-ptyp
)
423 (make-local-variable 'term-exec-hook
)
424 (make-local-variable 'term-vertical-motion
)
425 (make-local-variable 'term-pending-delete-marker
)
426 (setq term-pending-delete-marker
(make-marker))
427 (make-local-variable 'term-current-face
)
428 (setq term-current-face
'default
)
429 (make-local-variable 'term-pending-frame
)
430 (setq term-pending-frame nil
)
431 (make-local-variable 'term-chars-mode
)
432 (setq term-chars-mode nil
)
433 (run-hooks 'term-mode-hook
)
436 (append current-menubar
(list term-terminal-menu
))))
438 (setq term-input-ring
(make-ring term-input-ring-size
))))
442 (setq term-mode-map
(make-sparse-keymap))
443 (define-key term-mode-map
"\ep" 'term-previous-input
)
444 (define-key term-mode-map
"\en" 'term-next-input
)
445 (define-key term-mode-map
"\er" 'term-previous-matching-input
)
446 (define-key term-mode-map
"\es" 'term-next-matching-input
)
448 (define-key term-mode-map
[?\A-\M-r
] 'term-previous-matching-input-from-input
)
449 (define-key term-mode-map
[?\A-\M-s
] 'term-next-matching-input-from-input
))
450 (define-key term-mode-map
"\e\C-l" 'term-show-output
)
451 (define-key term-mode-map
"\C-m" 'term-send-input
)
452 (define-key term-mode-map
"\C-d" 'term-delchar-or-maybe-eof
)
453 (define-key term-mode-map
"\C-c\C-a" 'term-bol
)
454 (define-key term-mode-map
"\C-c\C-u" 'term-kill-input
)
455 (define-key term-mode-map
"\C-c\C-w" 'backward-kill-word
)
456 (define-key term-mode-map
"\C-c\C-c" 'term-interrupt-subjob
)
457 (define-key term-mode-map
"\C-c\C-z" 'term-stop-subjob
)
458 (define-key term-mode-map
"\C-c\C-\\" 'term-quit-subjob
)
459 (define-key term-mode-map
"\C-c\C-m" 'term-copy-old-input
)
460 (define-key term-mode-map
"\C-c\C-o" 'term-kill-output
)
461 (define-key term-mode-map
"\C-c\C-r" 'term-show-output
)
462 (define-key term-mode-map
"\C-c\C-e" 'term-show-maximum-output
)
463 (define-key term-mode-map
"\C-c\C-l" 'term-dynamic-list-input-ring
)
464 (define-key term-mode-map
"\C-c\C-n" 'term-next-prompt
)
465 (define-key term-mode-map
"\C-c\C-p" 'term-previous-prompt
)
466 (define-key term-mode-map
"\C-c\C-d" 'term-send-eof
)
467 (define-key term-mode-map
"\C-c\C-k" 'term-char-mode
)
468 (define-key term-mode-map
"\C-c\C-j" 'term-line-mode
)
469 (define-key term-mode-map
"\C-c\C-q" 'term-pager-toggle
)
471 (copy-face 'default
'term-underline-face
)
472 (set-face-underline-p 'term-underline-face t
)
475 ; (define-key term-mode-map [menu-bar completion]
476 ; (cons "Complete" (make-sparse-keymap "Complete")))
477 ; (define-key term-mode-map [menu-bar completion complete-expand]
478 ; '("Expand File Name" . term-replace-by-expanded-filename))
479 ; (define-key term-mode-map [menu-bar completion complete-listing]
480 ; '("File Completion Listing" . term-dynamic-list-filename-completions))
481 ; (define-key term-mode-map [menu-bar completion complete-file]
482 ; '("Complete File Name" . term-dynamic-complete-filename))
483 ; (define-key term-mode-map [menu-bar completion complete]
484 ; '("Complete Before Point" . term-dynamic-complete))
485 ; ;; Put them in the menu bar:
486 ; (setq menu-bar-final-items (append '(terminal completion inout signals)
487 ; menu-bar-final-items))
494 (defvar term-terminal-menu
(make-sparse-keymap "Terminal"))
495 (define-key term-terminal-menu
[terminal-pager-enable
]
496 '("Enable paging" . term-fake-pager-enable
))
497 (define-key term-terminal-menu
[terminal-pager-disable
]
498 '("Disable paging" . term-fake-pager-disable
))
499 (define-key term-terminal-menu
[terminal-char-mode
]
500 '("Character mode" . term-char-mode
))
501 (define-key term-terminal-menu
[terminal-line-mode
]
502 '("Line mode" . term-line-mode
))
503 (define-key term-mode-map
[menu-bar terminal
]
504 (setq term-terminal-menu
(cons "Terminal" term-terminal-menu
)))
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
))
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
))
559 (defvar term-signals-menu
(make-sparse-keymap "Signals"))
560 (define-key term-signals-menu
[eof] '("EOF" . term-send-eof))
561 (define-key term-signals-menu [kill] '("KILL" . term-kill-subjob))
562 (define-key term-signals-menu [quit] '("QUIT" . term-quit-subjob))
563 (define-key term-signals-menu [cont] '("CONT" . term-continue-subjob))
564 (define-key term-signals-menu [stop] '("STOP" . term-stop-subjob))
565 (define-key term-signals-menu [] '("BREAK" . term-interrupt-subjob))
566 (define-key term-mode-map [menu-bar signals]
567 (setq term-signals-menu (cons "Signals" term-signals-menu)))
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))
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.)
586 (defun term-check-kill-echo-list ()
587 (let ((cur term-kill-echo-list) (found nil) (save-point (point)))
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))))))
603 (goto-char save-point)))
606 (defun term-check-size (process)
607 (if (or (/= term-height (1- (window-height)))
608 (/= term-width (1- (window-width))))
610 (term-reset-size (1- (window-height)) (1- (window-width)))
611 (set-process-window-size process term-height term-width))))
613 (defun term-send-raw-string (chars)
614 (let ((proc (get-buffer-process (current-buffer))))
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))
621 (setq term-pager-count (term-current-row)))
622 (send-string proc chars))))
624 (defun term-send-raw ()
625 "Send the last character typed through the terminal-emulator
626 without any interpretation."
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)))
634 (defun term-send-raw-meta ()
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))))
644 (defun term-mouse-paste (click arg)
645 "Insert the last stretch of killed text at the position clicked on."
647 (mouse-set-point click)
648 (setq this-command 'yank)
649 (term-send-raw-string (current-kill (cond
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"))
660 (defun term-set-escape-char (c)
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 "\C-q" 'term-pager-toggle)
674 ;; The keybinding for term-char-mode is needed by the menubar code.
675 (define-key term-raw-escape-map "\C-k" 'term-char-mode)
676 (define-key term-raw-escape-map "\C-j" 'term-line-mode))
678 (defun term-char-mode ()
679 "Switch to char (\"raw\") sub-mode of term mode.
680 Each character you type is sent directly to the inferior without
681 intervention from emacs, except for the escape character (usually C-c)."
683 (if (not term-raw-map)
684 (let* ((map (make-keymap))
685 (esc-map (make-keymap))
688 (define-key map (make-string 1 i) 'term-send-raw)
689 (define-key esc-map (make-string 1 i) 'term-send-raw-meta)
691 (define-key map "\e" esc-map)
692 (setq term-raw-map map)
693 (setq term-raw-escape-map
694 (copy-keymap (lookup-key (current-global-map) "\C-x")))
697 (define-key term-raw-map [(button2)] 'term-mouse-paste))
699 (define-key term-raw-map [mouse-2] 'term-mouse-paste)
700 (define-key term-raw-map [menu-bar terminal] term-terminal-menu)
701 (define-key term-raw-map [menu-bar signals] term-signals-menu)
702 (define-key term-raw-map [up] 'term-send-up)
703 (define-key term-raw-map [down] 'term-send-down)
704 (define-key term-raw-map [right] 'term-send-right)
705 (define-key term-raw-map [left] 'term-send-left))
706 (term-set-escape-char ?\C-c))))
707 ;; FIXME: Emit message? Cfr ilisp-raw-message
708 (if (term-in-line-mode)
710 (setq term-old-mode-map (current-local-map))
711 (use-local-map term-raw-map)
713 ;; Send existing partial line to inferior (without newline).
714 (let ((pmark (process-mark (get-buffer-process (current-buffer))))
715 (save-input-sender term-input-sender))
716 (if (> (point) pmark)
719 (setq term-input-sender
720 (symbol-function 'term-send-string))
723 (setq term-input-sender save-input-sender))))
725 (setq mode-line-process '(": char %s"))
726 (set-buffer-modified-p (buffer-modified-p))))) ;; Updates mode line.
728 (defun term-line-mode ()
729 "Switch to line (\"cooked\") sub-mode of term mode.
730 This means that emacs editing commands work as normally, until
731 you type \\[term-send-input] which sends the current line to the inferior."
733 (if (term-in-char-mode)
735 (use-local-map term-old-mode-map)
736 (setq mode-line-process '(": line %s"))
737 (set-buffer-modified-p (buffer-modified-p))))) ;; Updates mode line.
739 (defun term-check-proc (buffer)
740 "True if there is a process associated w/buffer BUFFER, and
741 it is alive (status RUN or STOP). BUFFER can be either a buffer or the
743 (let ((proc (get-buffer-process buffer)))
744 (and proc (memq (process-status proc) '(run stop)))))
746 ;;; Note that this guy, unlike shell.el's make-shell, barfs if you pass it ()
747 ;;; for the second argument (program).
749 (defun make-term (name program &optional startfile &rest switches)
750 "Make a term process NAME in a buffer, running PROGRAM.
751 The name of the buffer is made by surrounding NAME with `*'s.
752 If there is already a running process in that buffer, it is not restarted.
753 Optional third arg STARTFILE is the name of a file to send the contents of to
754 the process. Any more args are arguments to PROGRAM."
755 (let ((buffer (get-buffer-create (concat "*" name "*"))))
756 ;; If no process, or nuked process, crank up a new one and put buffer in
757 ;; term mode. Otherwise, leave buffer and existing process alone.
758 (cond ((not (term-check-proc buffer))
761 (term-mode)) ; Install local vars, mode, keymap, ...
762 (term-exec buffer name program startfile switches)))
766 (defun term (program)
767 "Start a terminal-emulator in a new buffer."
768 (interactive (list (read-from-minibuffer "Run program: "
769 (or explicit-shell-file-name
773 (set-buffer (make-term "terminal" program))
776 (switch-to-buffer "*terminal*"))
778 (defun term-exec (buffer name command startfile switches)
779 "Start up a process in buffer for term modes.
780 Blasts any old process running in the buffer. Doesn't set the buffer mode.
781 You can use this to cheaply run a series of processes in the same term
782 buffer. The hook term-exec-hook is run after each exec."
785 (let ((proc (get-buffer-process buffer))) ; Blast any old process.
786 (if proc (delete-process proc)))
787 ;; Crank up a new process
788 (let ((proc (term-exec-1 name buffer command switches)))
789 (make-local-variable 'term-ptyp)
790 (setq term-ptyp process-connection-type) ; T if pty, NIL if pipe.
791 ;; Jump to the end, and set the process mark.
792 (goto-char (point-max))
793 (set-marker (process-mark proc) (point))
794 (set-process-filter proc 'term-emulate-terminal)
795 ;; Feed it the startfile.
797 ;;This is guaranteed to wait long enough
798 ;;but has bad results if the term does not prompt at all
799 ;; (while (= size (buffer-size))
801 ;;I hope 1 second is enough!
803 (goto-char (point-max))
804 (insert-file-contents startfile)
805 (setq startfile (buffer-substring (point) (point-max)))
806 (delete-region (point) (point-max))
807 (term-send-string proc startfile)))
808 (run-hooks 'term-exec-hook)
811 ;;; Name to use for TERM.
812 ;;; Using "emacs" loses, because bash disables editing if TERM == emacs.
813 (defvar term-term-name "eterm")
814 ; Format string, usage: (format term-termcap-string emacs-term-name "TERMCAP=" 24 80)
815 (defvar term-termcap-format
816 "%s%s:li#%d:co#%d:cl=\\E[H\\E[J:cd=\\E[J:bs:am:xn:cm=\\E[%%i%%d;%%dH\
817 :nd=\\E[C:up=\\E[A:ce=\\E[K:ho=\\E[H:pt\
818 :al=\\E[L:dl=\\E[M:DL=\\E[%%dM:AL=\\E[%%dL:cs=\\E[%%i%%d;%%dr:sf=\\n\
819 :te=\\E[2J\\E[?47l\\E8:ti=\\E7\\E[?47h\
820 :dc=\\E[P:DC=\\E[%%dP:IC=\\E[%%d@:im=\\E[4h:ei=\\E[4l:mi:\
821 :so=\\E[7m:se=\\E[m:us=\\E[4m:ue=\\E[m:md=\\E[1m:mr=\\E[7m:me=\\E[m\
822 :UP=\\E[%%dA:DO=\\E[%%dB:LE=\\E[%%dD:RI=\\E[%%dC"
824 "termcap capabilties supported")
826 ;;; This auxiliary function cranks up the process for term-exec in
827 ;;; the appropriate environment.
829 (defun term-exec-1 (name buffer command switches)
830 ;; We need to do an extra (fork-less) exec to run stty.
831 ;; (This would not be needed if we had suitable emacs primitives.)
832 ;; The 'if ...; then shift; fi' hack is because Bourne shell
833 ;; loses one arg when called with -c, and newer shells (bash, ksh) don't.
834 ;; Thus we add an extra dummy argument "..", and then remove it.
835 (let ((process-environment
838 (format "TERM=%s" term-term-name)
839 (if (and (boundp 'system-uses-terminfo) system-uses-terminfo)
840 (format "TERMINFO=%s" data-directory)
841 (format term-termcap-format "TERMCAP="
842 term-term-name term-height term-width))
843 (format "EMACS=%s (term:%s)" emacs-version term-version)
844 (format "LINES=%d" term-height)
845 (format "COLUMNS=%d" term-width))
846 process-environment)))
847 (apply 'start-process name buffer
849 (format "stty -nl echo rows %d columns %d sane 2>/dev/null;\
850 if [ $1 = .. ]; then shift; fi; exec \"$@\""
851 term-height term-width)
855 ;;; This should be in emacs, but it isn't.
856 (defun term-mem (item list &optional elt=)
857 "Test to see if ITEM is equal to an item in LIST.
858 Option comparison function ELT= defaults to equal."
859 (let ((elt= (or elt= (function equal)))
861 (while (and list (not done))
862 (if (funcall elt= item (car list))
864 (setq list (cdr list))))
868 ;;; Input history processing in a buffer
869 ;;; ===========================================================================
870 ;;; Useful input history functions, courtesy of the Ergo group.
873 ;;; term-dynamic-list-input-ring List history in help buffer.
874 ;;; term-previous-input Previous input...
875 ;;; term-previous-matching-input ...matching a string.
876 ;;; term-previous-matching-input-from-input ... matching the current input.
877 ;;; term-next-input Next input...
878 ;;; term-next-matching-input ...matching a string.
879 ;;; term-next-matching-input-from-input ... matching the current input.
880 ;;; term-backward-matching-input Backwards input...
881 ;;; term-forward-matching-input ...matching a string.
882 ;;; term-replace-by-expanded-history Expand history at point;
883 ;;; replace with expanded history.
884 ;;; term-magic-space Expand history and insert space.
887 ;;; term-read-input-ring Read into term-input-ring...
888 ;;; term-write-input-ring Write to term-input-ring-file-name.
889 ;;; term-replace-by-expanded-history-before-point Workhorse function.
891 (defun term-read-input-ring (&optional silent)
892 "Sets the buffer's `term-input-ring' from a history file.
893 The name of the file is given by the variable `term-input-ring-file-name'.
894 The history ring is of size `term-input-ring-size', regardless of file size.
895 If `term-input-ring-file-name' is nil this function does nothing.
897 If the optional argument SILENT is non-nil, we say nothing about a
898 failure to read the history file.
900 This function is useful for major mode commands and mode hooks.
902 The structure of the history file should be one input command per line,
903 with the most recent command last.
904 See also `term-input-ignoredups' and `term-write-input-ring'."
905 (cond ((or (null term-input-ring-file-name)
906 (equal term-input-ring-file-name ""))
908 ((not (file-readable-p term-input-ring-file-name))
910 (message "Cannot read history file %s"
911 term-input-ring-file-name)))
913 (let ((history-buf (get-buffer-create " *temp*"))
914 (file term-input-ring-file-name)
916 (ring (make-ring term-input-ring-size)))
919 (set-buffer history-buf)
922 (insert-file-contents file)
923 ;; Save restriction in case file is already visited...
924 ;; Watch for those date stamps in history files!
925 (goto-char (point-max))
926 (while (and (< count term-input-ring-size)
927 (re-search-backward "^[ \t]*\\([^#\n].*\\)[ \t]*$"
929 (let ((history (buffer-substring (match-beginning 1)
931 (if (or (null term-input-ignoredups)
933 (not (string-equal (ring-ref ring 0) history)))
934 (ring-insert-at-beginning ring history)))
935 (setq count (1+ count))))
936 (kill-buffer history-buf))
937 (setq term-input-ring ring
938 term-input-ring-index nil)))))
940 (defun term-write-input-ring ()
941 "Writes the buffer's `term-input-ring' to a history file.
942 The name of the file is given by the variable `term-input-ring-file-name'.
943 The original contents of the file are lost if `term-input-ring' is not empty.
944 If `term-input-ring-file-name' is nil this function does nothing.
946 Useful within process sentinels.
948 See also `term-read-input-ring'."
949 (cond ((or (null term-input-ring-file-name)
950 (equal term-input-ring-file-name "")
951 (null term-input-ring) (ring-empty-p term-input-ring))
953 ((not (file-writable-p term-input-ring-file-name))
954 (message "Cannot write history file %s" term-input-ring-file-name))
956 (let* ((history-buf (get-buffer-create " *Temp Input History*"))
957 (ring term-input-ring)
958 (file term-input-ring-file-name)
959 (index (ring-length ring)))
960 ;; Write it all out into a buffer first. Much faster, but messier,
961 ;; than writing it one line at a time.
963 (set-buffer history-buf)
966 (setq index (1- index))
967 (insert (ring-ref ring index) ?\n))
968 (write-region (buffer-string) nil file nil 'no-message)
969 (kill-buffer nil))))))
972 (defun term-dynamic-list-input-ring ()
973 "List in help buffer the buffer's input history."
975 (if (or (not (ring-p term-input-ring))
976 (ring-empty-p term-input-ring))
977 (message "No history")
979 (history-buffer " *Input History*")
980 (index (1- (ring-length term-input-ring)))
981 (conf (current-window-configuration)))
982 ;; We have to build up a list ourselves from the ring vector.
984 (setq history (cons (ring-ref term-input-ring index) history)
986 ;; Change "completion" to "history reference"
987 ;; to make the display accurate.
988 (with-output-to-temp-buffer history-buffer
989 (display-completion-list history)
990 (set-buffer history-buffer)
992 (while (search-backward "completion" nil 'move)
993 (replace-match "history reference")))
995 (message "Hit space to flush")
996 (let ((ch (read-event)))
998 (set-window-configuration conf)
999 (setq unread-command-events (list ch)))))))
1002 (defun term-regexp-arg (prompt)
1003 ;; Return list of regexp and prefix arg using PROMPT.
1004 (let* ((minibuffer-history-sexp-flag nil)
1005 ;; Don't clobber this.
1006 (last-command last-command)
1007 (regexp (read-from-minibuffer prompt nil nil nil
1008 'minibuffer-history-search-history)))
1009 (list (if (string-equal regexp "")
1010 (setcar minibuffer-history-search-history
1011 (nth 1 minibuffer-history-search-history))
1013 (prefix-numeric-value current-prefix-arg))))
1015 (defun term-search-arg (arg)
1016 ;; First make sure there is a ring and that we are after the process mark
1017 (cond ((not (term-after-pmark-p))
1018 (error "Not at command line"))
1019 ((or (null term-input-ring)
1020 (ring-empty-p term-input-ring))
1021 (error "Empty input ring"))
1023 ;; arg of zero resets search from beginning, and uses arg of 1
1024 (setq term-input-ring-index nil)
1029 (defun term-search-start (arg)
1030 ;; Index to start a directional search, starting at term-input-ring-index
1031 (if term-input-ring-index
1032 ;; If a search is running, offset by 1 in direction of arg
1033 (mod (+ term-input-ring-index (if (> arg 0) 1 -1))
1034 (ring-length term-input-ring))
1035 ;; For a new search, start from beginning or end, as appropriate
1037 0 ; First elt for forward search
1038 (1- (ring-length term-input-ring))))) ; Last elt for backward search
1040 (defun term-previous-input-string (arg)
1041 "Return the string ARG places along the input ring.
1042 Moves relative to `term-input-ring-index'."
1043 (ring-ref term-input-ring (if term-input-ring-index
1044 (mod (+ arg term-input-ring-index)
1045 (ring-length term-input-ring))
1048 (defun term-previous-input (arg)
1049 "Cycle backwards through input history."
1051 (term-previous-matching-input "." arg))
1053 (defun term-next-input (arg)
1054 "Cycle forwards through input history."
1056 (term-previous-input (- arg)))
1058 (defun term-previous-matching-input-string (regexp arg)
1059 "Return the string matching REGEXP ARG places along the input ring.
1060 Moves relative to `term-input-ring-index'."
1061 (let* ((pos (term-previous-matching-input-string-position regexp arg)))
1062 (if pos (ring-ref term-input-ring pos))))
1064 (defun term-previous-matching-input-string-position (regexp arg &optional start)
1065 "Return the index matching REGEXP ARG places along the input ring.
1066 Moves relative to START, or `term-input-ring-index'."
1067 (if (or (not (ring-p term-input-ring))
1068 (ring-empty-p term-input-ring))
1069 (error "No history"))
1070 (let* ((len (ring-length term-input-ring))
1071 (motion (if (> arg 0) 1 -1))
1072 (n (mod (- (or start (term-search-start arg)) motion) len))
1073 (tried-each-ring-item nil)
1075 ;; Do the whole search as many times as the argument says.
1076 (while (and (/= arg 0) (not tried-each-ring-item))
1079 n (mod (+ n motion) len))
1080 ;; If we haven't reached a match, step some more.
1081 (while (and (< n len) (not tried-each-ring-item)
1082 (not (string-match regexp (ring-ref term-input-ring n))))
1083 (setq n (mod (+ n motion) len)
1084 ;; If we have gone all the way around in this search.
1085 tried-each-ring-item (= n prev)))
1086 (setq arg (if (> arg 0) (1- arg) (1+ arg))))
1087 ;; Now that we know which ring element to use, if we found it, return that.
1088 (if (string-match regexp (ring-ref term-input-ring n))
1091 (defun term-previous-matching-input (regexp arg)
1092 "Search backwards through input history for match for REGEXP.
1093 \(Previous history elements are earlier commands.)
1094 With prefix argument N, search for Nth previous match.
1095 If N is negative, find the next or Nth next match."
1096 (interactive (term-regexp-arg "Previous input matching (regexp): "))
1097 (setq arg (term-search-arg arg))
1098 (let ((pos (term-previous-matching-input-string-position regexp arg)))
1099 ;; Has a match been found?
1102 (setq term-input-ring-index pos)
1103 (message "History item: %d" (1+ pos))
1105 ;; Can't use kill-region as it sets this-command
1106 (process-mark (get-buffer-process (current-buffer))) (point))
1107 (insert (ring-ref term-input-ring pos)))))
1109 (defun term-next-matching-input (regexp arg)
1110 "Search forwards through input history for match for REGEXP.
1111 \(Later history elements are more recent commands.)
1112 With prefix argument N, search for Nth following match.
1113 If N is negative, find the previous or Nth previous match."
1114 (interactive (term-regexp-arg "Next input matching (regexp): "))
1115 (term-previous-matching-input regexp (- arg)))
1117 (defun term-previous-matching-input-from-input (arg)
1118 "Search backwards through input history for match for current input.
1119 \(Previous history elements are earlier commands.)
1120 With prefix argument N, search for Nth previous match.
1121 If N is negative, search forwards for the -Nth following match."
1123 (if (not (memq last-command '(term-previous-matching-input-from-input
1124 term-next-matching-input-from-input)))
1125 ;; Starting a new search
1126 (setq term-matching-input-from-input-string
1128 (process-mark (get-buffer-process (current-buffer)))
1130 term-input-ring-index nil))
1131 (term-previous-matching-input
1132 (concat "^" (regexp-quote term-matching-input-from-input-string))
1135 (defun term-next-matching-input-from-input (arg)
1136 "Search forwards through input history for match for current input.
1137 \(Following history elements are more recent commands.)
1138 With prefix argument N, search for Nth following match.
1139 If N is negative, search backwards for the -Nth previous match."
1141 (term-previous-matching-input-from-input (- arg)))
1144 (defun term-replace-by-expanded-history (&optional silent)
1145 "Expand input command history references before point.
1146 Expansion is dependent on the value of `term-input-autoexpand'.
1148 This function depends on the buffer's idea of the input history, which may not
1149 match the command interpreter's idea, assuming it has one.
1151 Assumes history syntax is like typical Un*x shells'. However, since emacs
1152 cannot know the interpreter's idea of input line numbers, assuming it has one,
1153 it cannot expand absolute input line number references.
1155 If the optional argument SILENT is non-nil, never complain
1156 even if history reference seems erroneous.
1158 See `term-magic-space' and `term-replace-by-expanded-history-before-point'.
1160 Returns t if successful."
1162 (if (and term-input-autoexpand
1163 (string-match "[!^]" (funcall term-get-old-input))
1164 (save-excursion (beginning-of-line)
1165 (looking-at term-prompt-regexp)))
1166 ;; Looks like there might be history references in the command.
1167 (let ((previous-modified-tick (buffer-modified-tick)))
1168 (message "Expanding history references...")
1169 (term-replace-by-expanded-history-before-point silent)
1170 (/= previous-modified-tick (buffer-modified-tick)))))
1173 (defun term-replace-by-expanded-history-before-point (silent)
1174 "Expand directory stack reference before point.
1175 See `term-replace-by-expanded-history'. Returns t if successful."
1177 (let ((toend (- (save-excursion (end-of-line nil) (point)) (point)))
1178 (start (progn (term-bol nil) (point))))
1180 (skip-chars-forward "^!^"
1182 (end-of-line nil) (- (point) toend)))
1185 (end-of-line nil) (- (point) toend))))
1186 ;; This seems a bit complex. We look for references such as !!, !-num,
1187 ;; !foo, !?foo, !{bar}, !?{bar}, ^oh, ^my^, ^god^it, ^never^ends^.
1188 ;; If that wasn't enough, the plings can be suffixed with argument
1189 ;; range specifiers.
1190 ;; Argument ranges are complex too, so we hive off the input line,
1191 ;; referenced with plings, with the range string to `term-args'.
1192 (setq term-input-ring-index nil)
1193 (cond ((or (= (preceding-char) ?\\)
1194 (term-within-quotes start (point)))
1195 ;; The history is quoted, or we're in quotes.
1196 (goto-char (1+ (point))))
1197 ((looking-at "![0-9]+\\($\\|[^-]\\)")
1198 ;; We cannot know the interpreter's idea of input line numbers.
1199 (goto-char (match-end 0))
1200 (message "Absolute reference cannot be expanded"))
1201 ((looking-at "!-\\([0-9]+\\)\\(:?[0-9^$*-]+\\)?")
1202 ;; Just a number of args from `number' lines backward.
1203 (let ((number (1- (string-to-number
1204 (buffer-substring (match-beginning 1)
1206 (if (<= number (ring-length term-input-ring))
1209 (term-args (term-previous-input-string number)
1210 (match-beginning 2) (match-end 2))
1212 (setq term-input-ring-index number)
1213 (message "History item: %d" (1+ number)))
1214 (goto-char (match-end 0))
1215 (message "Relative reference exceeds input history size"))))
1216 ((or (looking-at "!!?:?\\([0-9^$*-]+\\)") (looking-at "!!"))
1217 ;; Just a number of args from the previous input line.
1219 (term-args (term-previous-input-string 0)
1220 (match-beginning 1) (match-end 1))
1222 (message "History item: previous"))
1224 "!\\??\\({\\(.+\\)}\\|\\(\\sw+\\)\\)\\(:?[0-9^$*-]+\\)?")
1225 ;; Most recent input starting with or containing (possibly
1226 ;; protected) string, maybe just a number of args. Phew.
1227 (let* ((mb1 (match-beginning 1)) (me1 (match-end 1))
1228 (mb2 (match-beginning 2)) (me2 (match-end 2))
1229 (exp (buffer-substring (or mb2 mb1) (or me2 me1)))
1230 (pref (if (save-match-data (looking-at "!\\?")) "" "^"))
1231 (pos (save-match-data
1232 (term-previous-matching-input-string-position
1233 (concat pref (regexp-quote exp)) 1))))
1236 (goto-char (match-end 0))
1238 (progn (message "Not found")
1240 (setq term-input-ring-index pos)
1242 (term-args (ring-ref term-input-ring pos)
1243 (match-beginning 4) (match-end 4))
1245 (message "History item: %d" (1+ pos)))))
1246 ((looking-at "\\^\\([^^]+\\)\\^?\\([^^]*\\)\\^?")
1247 ;; Quick substitution on the previous input line.
1248 (let ((old (buffer-substring (match-beginning 1) (match-end 1)))
1249 (new (buffer-substring (match-beginning 2) (match-end 2)))
1251 (replace-match (term-previous-input-string 0) t t)
1253 (goto-char (match-beginning 0))
1254 (if (not (search-forward old pos t))
1256 (error "Not found"))
1257 (replace-match new t t)
1258 (message "History item: substituted"))))
1260 (goto-char (match-end 0))))))))
1263 (defun term-magic-space (arg)
1264 "Expand input history references before point and insert ARG spaces.
1265 A useful command to bind to SPC. See `term-replace-by-expanded-history'."
1267 (term-replace-by-expanded-history)
1268 (self-insert-command arg))
1270 (defun term-within-quotes (beg end)
1271 "Return t if the number of quotes between BEG and END is odd.
1272 Quotes are single and double."
1273 (let ((countsq (term-how-many-region "\\(^\\|[^\\\\]\\)\'" beg end))
1274 (countdq (term-how-many-region "\\(^\\|[^\\\\]\\)\"" beg end)))
1275 (or (= (mod countsq 2) 1) (= (mod countdq 2) 1))))
1277 (defun term-how-many-region (regexp beg end)
1278 "Return number of matches for REGEXP from BEG to END."
1283 (while (re-search-forward regexp end t)
1284 (setq count (1+ count)))))
1287 (defun term-args (string begin end)
1288 ;; From STRING, return the args depending on the range specified in the text
1289 ;; from BEGIN to END. If BEGIN is nil, assume all args. Ignore leading `:'.
1290 ;; Range can be x-y, x-, -y, where x/y can be [0-9], *, ^, $.
1293 (term-arguments string 0 nil)
1294 (let* ((range (buffer-substring
1295 (if (eq (char-after begin) ?:) (1+ begin) begin) end))
1296 (nth (cond ((string-match "^[*^]" range) 1)
1297 ((string-match "^-" range) 0)
1298 ((string-equal range "$") nil)
1299 (t (string-to-number range))))
1300 (mth (cond ((string-match "[-*$]$" range) nil)
1301 ((string-match "-" range)
1302 (string-to-number (substring range (match-end 0))))
1304 (term-arguments string nth mth)))))
1306 ;; Return a list of arguments from ARG. Break it up at the
1307 ;; delimiters in term-delimiter-argument-list. Returned list is backwards.
1308 (defun term-delim-arg (arg)
1309 (if (null term-delimiter-argument-list)
1315 (let ((char (aref arg pos))
1317 (if (memq char term-delimiter-argument-list)
1318 (while (and (< pos len) (eq (aref arg pos) char))
1319 (setq pos (1+ pos)))
1320 (while (and (< pos len)
1321 (not (memq (aref arg pos)
1322 term-delimiter-argument-list)))
1323 (setq pos (1+ pos))))
1324 (setq args (cons (substring arg start pos) args))))
1327 (defun term-arguments (string nth mth)
1328 "Return from STRING the NTH to MTH arguments.
1329 NTH and/or MTH can be nil, which means the last argument.
1330 Returned arguments are separated by single spaces.
1331 We assume whitespace separates arguments, except within quotes.
1332 Also, a run of one or more of a single character
1333 in `term-delimiter-argument-list' is a separate argument.
1334 Argument 0 is the command name."
1335 (let ((argpart "[^ \n\t\"'`]+\\|\\(\"[^\"]*\"\\|'[^']*'\\|`[^`]*`\\)")
1338 beg str value quotes)
1339 ;; Build a list of all the args until we have as many as we want.
1340 (while (and (or (null mth) (<= count mth))
1341 (string-match argpart string pos))
1342 (if (and beg (= pos (match-beginning 0)))
1343 ;; It's contiguous, part of the same arg.
1344 (setq pos (match-end 0)
1345 quotes (or quotes (match-beginning 1)))
1346 ;; It's a new separate arg.
1348 ;; Put the previous arg, if there was one, onto ARGS.
1349 (setq str (substring string beg pos)
1350 args (if quotes (cons str args)
1351 (nconc (term-delim-arg str) args))
1353 (setq quotes (match-beginning 1))
1354 (setq beg (match-beginning 0))
1355 (setq pos (match-end 0))))
1357 (setq str (substring string beg pos)
1358 args (if quotes (cons str args)
1359 (nconc (term-delim-arg str) args))
1361 (let ((n (or nth (1- count)))
1362 (m (if mth (1- (- count mth)) 0)))
1364 (function (lambda (a) a)) (nthcdr n (nreverse (nthcdr m args))) " "))))
1367 ;;; Input processing stuff [line mode]
1370 (defun term-send-input ()
1371 "Send input to process.
1372 After the process output mark, sends all text from the process mark to
1373 point as input to the process. Before the process output mark, calls value
1374 of variable term-get-old-input to retrieve old input, copies it to the
1375 process mark, and sends it. A terminal newline is also inserted into the
1376 buffer and sent to the process. The list of function names contained in the
1377 value of `term-input-filter-functions' is called on the input before sending
1378 it. The input is entered into the input history ring, if the value of variable
1379 term-input-filter returns non-nil when called on the input.
1381 Any history reference may be expanded depending on the value of the variable
1382 `term-input-autoexpand'. The list of function names contained in the value
1383 of `term-input-filter-functions' is called on the input before sending it.
1384 The input is entered into the input history ring, if the value of variable
1385 `term-input-filter' returns non-nil when called on the input.
1387 The values of `term-get-old-input', `term-input-filter-functions', and
1388 `term-input-filter' are chosen according to the command interpreter running
1389 in the buffer. E.g.,
1391 If the interpreter is the csh,
1392 term-get-old-input is the default: take the current line, discard any
1393 initial string matching regexp term-prompt-regexp.
1394 term-input-filter-functions monitors input for \"cd\", \"pushd\", and
1395 \"popd\" commands. When it sees one, it cd's the buffer.
1396 term-input-filter is the default: returns T if the input isn't all white
1399 If the term is Lucid Common Lisp,
1400 term-get-old-input snarfs the sexp ending at point.
1401 term-input-filter-functions does nothing.
1402 term-input-filter returns NIL if the input matches input-filter-regexp,
1403 which matches (1) all whitespace (2) :a, :c, etc.
1405 Similarly for Soar, Scheme, etc."
1407 ;; Note that the input string does not include its terminal newline.
1408 (let ((proc (get-buffer-process (current-buffer))))
1409 (if (not proc) (error "Current buffer has no process")
1410 (let* ((pmark (process-mark proc))
1411 (pmark-val (marker-position pmark))
1412 (intxt (if (>= (point) pmark-val)
1413 (progn (end-of-line)
1414 (let ((copy (buffer-substring pmark (point))))
1415 ;; Delete, because inferior should echo.
1416 (set-marker term-pending-delete-marker
1418 (set-marker (process-mark proc) (point))
1420 (funcall term-get-old-input)))
1421 (input (if (not (eq term-input-autoexpand 'input))
1422 ;; Just whatever's already there
1424 ;; Expand and leave it visible in buffer
1425 (term-replace-by-expanded-history t)
1426 (buffer-substring pmark (point))))
1427 (history (if (not (eq term-input-autoexpand 'history))
1429 ;; This is messy 'cos ultimately the original
1430 ;; functions used do insertion, rather than return
1431 ;; strings. We have to expand, then insert back.
1432 (term-replace-by-expanded-history t)
1433 (let ((copy (buffer-substring pmark (point))))
1434 (delete-region pmark (point))
1437 (if term-pager-count
1439 (goto-char (process-mark proc))
1440 (setq term-pager-count (term-current-row))))
1441 (if (and (funcall term-input-filter history)
1442 (or (null term-input-ignoredups)
1443 (not (ring-p term-input-ring))
1444 (ring-empty-p term-input-ring)
1445 (not (string-equal (ring-ref term-input-ring 0)
1447 (ring-insert term-input-ring history))
1448 (let ((functions term-input-filter-functions))
1450 (funcall (car functions) (concat input "\n"))
1451 (setq functions (cdr functions))))
1452 (setq term-input-ring-index nil)
1454 ;; Update the markers before we send the input
1455 ;; in case we get output amidst sending the input.
1456 (set-marker term-last-input-start pmark)
1457 (set-marker term-last-input-end (point))
1458 (funcall term-input-sender proc input)))))
1460 (defun term-get-old-input-default ()
1461 "Default for term-get-old-input.
1462 Take the current line, and discard any initial text matching
1463 term-prompt-regexp."
1467 (let ((beg (point)))
1469 (buffer-substring beg (point)))))
1471 (defun term-copy-old-input ()
1472 "Insert after prompt old input at point as new input to be edited.
1473 Calls `term-get-old-input' to get old input."
1475 (let ((input (funcall term-get-old-input))
1476 (process (get-buffer-process (current-buffer))))
1478 (error "Current buffer has no process")
1479 (goto-char (process-mark process))
1482 (defun term-skip-prompt ()
1483 "Skip past the text matching regexp term-prompt-regexp.
1484 If this takes us past the end of the current line, don't skip at all."
1485 (let ((eol (save-excursion (end-of-line) (point))))
1486 (if (and (looking-at term-prompt-regexp)
1487 (<= (match-end 0) eol))
1488 (goto-char (match-end 0)))))
1491 (defun term-after-pmark-p ()
1492 "Is point after the process output marker?"
1493 ;; Since output could come into the buffer after we looked at the point
1494 ;; but before we looked at the process marker's value, we explicitly
1495 ;; serialise. This is just because I don't know whether or not emacs
1496 ;; services input during execution of lisp commands.
1497 (let ((proc-pos (marker-position
1498 (process-mark (get-buffer-process (current-buffer))))))
1499 (<= proc-pos (point))))
1501 (defun term-simple-send (proc string)
1502 "Default function for sending to PROC input STRING.
1503 This just sends STRING plus a newline. To override this,
1504 set the hook TERM-INPUT-SENDER."
1505 (term-send-string proc string)
1506 (term-send-string proc "\n"))
1508 (defun term-bol (arg)
1509 "Goes to the beginning of line, then skips past the prompt, if any.
1510 If a prefix argument is given (\\[universal-argument]), then no prompt skip
1511 -- go straight to column 0.
1513 The prompt skip is done by skipping text matching the regular expression
1514 term-prompt-regexp, a buffer local variable."
1517 (if (null arg) (term-skip-prompt)))
1519 ;;; These two functions are for entering text you don't want echoed or
1520 ;;; saved -- typically passwords to ftp, telnet, or somesuch.
1521 ;;; Just enter m-x term-send-invisible and type in your line.
1523 (defun term-read-noecho (prompt &optional stars)
1524 "Read a single line of text from user without echoing, and return it.
1525 Prompt with argument PROMPT, a string. Optional argument STARS causes
1526 input to be echoed with '*' characters on the prompt line. Input ends with
1527 RET, LFD, or ESC. DEL or C-h rubs out. C-u kills line. C-g aborts (if
1528 `inhibit-quit' is set because e.g. this function was called from a process
1529 filter and C-g is pressed, this function returns nil rather than a string).
1531 Note that the keystrokes comprising the text can still be recovered
1532 \(temporarily) with \\[view-lossage]. This may be a security bug for some
1537 (cursor-in-echo-area t)
1541 (message "%s%s" prompt (make-string (length ans) ?*))
1543 (setq c (read-char))
1545 ;; This function may get called from a process filter, where
1546 ;; inhibit-quit is set. In later versions of emacs read-char
1547 ;; may clear quit-flag itself and return C-g. That would make
1548 ;; it impossible to quit this loop in a simple way, so
1549 ;; re-enable it here (for backward-compatibility the check for
1550 ;; quit-flag below would still be necessary, so this seems
1551 ;; like the simplest way to do things).
1554 ((or (= c ?\r) (= c ?\n) (= c ?\e))
1558 ((and (/= c ?\b) (/= c ?\177))
1559 (setq ans (concat ans (char-to-string c))))
1561 (setq ans (substring ans 0 -1)))))
1563 ;; Emulate a true quit, except that we have to return a value.
1565 (setq quit-flag nil)
1571 (defun term-send-invisible (str &optional proc)
1572 "Read a string without echoing.
1573 Then send it to the process running in the current buffer. A new-line
1574 is additionally sent. String is not saved on term input history list.
1575 Security bug: your string can still be temporarily recovered with
1577 (interactive (list (term-read-noecho "Enter non-echoed text")))
1578 (interactive "P") ; Defeat snooping via C-x esc
1579 (if (not (stringp str))
1580 (setq str (term-read-noecho "Non-echoed text: " t)))
1582 (setq proc (get-buffer-process (current-buffer))))
1583 (if (not proc) (error "Current buffer has no process")
1584 (setq term-kill-echo-list (nconc term-kill-echo-list
1586 (term-send-string proc str)
1587 (term-send-string proc "\n")))
1590 ;;; Low-level process communication
1592 (defvar term-input-chunk-size 512
1593 "*Long inputs send to term processes are broken up into chunks of this size.
1594 If your process is choking on big inputs, try lowering the value.")
1596 (defun term-send-string (proc str)
1597 "Send PROCESS the contents of STRING as input.
1598 This is equivalent to process-send-string, except that long input strings
1599 are broken up into chunks of size term-input-chunk-size. Processes
1600 are given a chance to output between chunks. This can help prevent processes
1601 from hanging when you send them long inputs on some OS's."
1602 (let* ((len (length str))
1603 (i (min len term-input-chunk-size)))
1604 (process-send-string proc (substring str 0 i))
1606 (let ((next-i (+ i term-input-chunk-size)))
1607 (accept-process-output)
1608 (process-send-string proc (substring str i (min len next-i)))
1611 (defun term-send-region (proc start end)
1612 "Sends to PROC the region delimited by START and END.
1613 This is a replacement for process-send-region that tries to keep
1614 your process from hanging on long inputs. See term-send-string."
1615 (term-send-string proc (buffer-substring start end)))
1618 ;;; Random input hackage
1620 (defun term-kill-output ()
1621 "Kill all output from interpreter since last input."
1623 (let ((pmark (process-mark (get-buffer-process (current-buffer)))))
1624 (kill-region term-last-input-end pmark)
1626 (insert "*** output flushed ***\n")
1627 (set-marker pmark (point))))
1629 (defun term-show-output ()
1630 "Display start of this batch of interpreter output at top of window.
1631 Sets mark to the value of point when this command is run."
1633 (goto-char term-last-input-end)
1636 (set-window-start (selected-window) (point))
1639 (defun term-interrupt-subjob ()
1640 "Interrupt the current subjob."
1642 (interrupt-process nil term-ptyp))
1644 (defun term-kill-subjob ()
1645 "Send kill signal to the current subjob."
1647 (kill-process nil term-ptyp))
1649 (defun term-quit-subjob ()
1650 "Send quit signal to the current subjob."
1652 (quit-process nil term-ptyp))
1654 (defun term-stop-subjob ()
1655 "Stop the current subjob.
1656 WARNING: if there is no current subjob, you can end up suspending
1657 the top-level process running in the buffer. If you accidentally do
1658 this, use \\[term-continue-subjob] to resume the process. (This
1659 is not a problem with most shells, since they ignore this signal.)"
1661 (stop-process nil term-ptyp))
1663 (defun term-continue-subjob ()
1664 "Send CONT signal to process buffer's process group.
1665 Useful if you accidentally suspend the top-level process."
1667 (continue-process nil term-ptyp))
1669 (defun term-kill-input ()
1670 "Kill all text from last stuff output by interpreter to point."
1672 (let* ((pmark (process-mark (get-buffer-process (current-buffer))))
1673 (p-pos (marker-position pmark)))
1674 (if (> (point) p-pos)
1675 (kill-region pmark (point)))))
1677 (defun term-delchar-or-maybe-eof (arg)
1678 "Delete ARG characters forward, or send an EOF to process if at end of buffer."
1684 (defun term-send-eof ()
1685 "Send an EOF to the current buffer's process."
1689 (defun term-backward-matching-input (regexp arg)
1690 "Search backward through buffer for match for REGEXP.
1691 Matches are searched for on lines that match `term-prompt-regexp'.
1692 With prefix argument N, search for Nth previous match.
1693 If N is negative, find the next or Nth next match."
1694 (interactive (term-regexp-arg "Backward input matching (regexp): "))
1695 (let* ((re (concat term-prompt-regexp ".*" regexp))
1696 (pos (save-excursion (end-of-line (if (> arg 0) 0 1))
1697 (if (re-search-backward re nil t arg)
1700 (progn (message "Not found")
1705 (defun term-forward-matching-input (regexp arg)
1706 "Search forward through buffer for match for REGEXP.
1707 Matches are searched for on lines that match `term-prompt-regexp'.
1708 With prefix argument N, search for Nth following match.
1709 If N is negative, find the previous or Nth previous match."
1710 (interactive (term-regexp-arg "Forward input matching (regexp): "))
1711 (term-backward-matching-input regexp (- arg)))
1714 (defun term-next-prompt (n)
1715 "Move to end of Nth next prompt in the buffer.
1716 See `term-prompt-regexp'."
1718 (let ((paragraph-start term-prompt-regexp))
1719 (end-of-line (if (> n 0) 1 0))
1720 (forward-paragraph n)
1721 (term-skip-prompt)))
1723 (defun term-previous-prompt (n)
1724 "Move to end of Nth previous prompt in the buffer.
1725 See `term-prompt-regexp'."
1727 (term-next-prompt (- n)))
1729 ;;; Support for source-file processing commands.
1730 ;;;============================================================================
1731 ;;; Many command-interpreters (e.g., Lisp, Scheme, Soar) have
1732 ;;; commands that process files of source text (e.g. loading or compiling
1733 ;;; files). So the corresponding process-in-a-buffer modes have commands
1734 ;;; for doing this (e.g., lisp-load-file). The functions below are useful
1735 ;;; for defining these commands.
1737 ;;; Alas, these guys don't do exactly the right thing for Lisp, Scheme
1738 ;;; and Soar, in that they don't know anything about file extensions.
1739 ;;; So the compile/load interface gets the wrong default occasionally.
1740 ;;; The load-file/compile-file default mechanism could be smarter -- it
1741 ;;; doesn't know about the relationship between filename extensions and
1742 ;;; whether the file is source or executable. If you compile foo.lisp
1743 ;;; with compile-file, then the next load-file should use foo.bin for
1744 ;;; the default, not foo.lisp. This is tricky to do right, particularly
1745 ;;; because the extension for executable files varies so much (.o, .bin,
1746 ;;; .lbin, .mo, .vo, .ao, ...).
1749 ;;; TERM-SOURCE-DEFAULT -- determines defaults for source-file processing
1752 ;;; TERM-CHECK-SOURCE -- if FNAME is in a modified buffer, asks you if you
1753 ;;; want to save the buffer before issuing any process requests to the command
1756 ;;; TERM-GET-SOURCE -- used by the source-file processing commands to prompt
1757 ;;; for the file to process.
1759 ;;; (TERM-SOURCE-DEFAULT previous-dir/file source-modes)
1760 ;;;============================================================================
1761 ;;; This function computes the defaults for the load-file and compile-file
1762 ;;; commands for tea, soar, cmulisp, and cmuscheme modes.
1764 ;;; - PREVIOUS-DIR/FILE is a pair (directory . filename) from the last
1765 ;;; source-file processing command. NIL if there hasn't been one yet.
1766 ;;; - SOURCE-MODES is a list used to determine what buffers contain source
1767 ;;; files: if the major mode of the buffer is in SOURCE-MODES, it's source.
1768 ;;; Typically, (lisp-mode) or (scheme-mode).
1770 ;;; If the command is given while the cursor is inside a string, *and*
1771 ;;; the string is an existing filename, *and* the filename is not a directory,
1772 ;;; then the string is taken as default. This allows you to just position
1773 ;;; your cursor over a string that's a filename and have it taken as default.
1775 ;;; If the command is given in a file buffer whose major mode is in
1776 ;;; SOURCE-MODES, then the the filename is the default file, and the
1777 ;;; file's directory is the default directory.
1779 ;;; If the buffer isn't a source file buffer (e.g., it's the process buffer),
1780 ;;; then the default directory & file are what was used in the last source-file
1781 ;;; processing command (i.e., PREVIOUS-DIR/FILE). If this is the first time
1782 ;;; the command has been run (PREVIOUS-DIR/FILE is nil), the default directory
1783 ;;; is the cwd, with no default file. (\"no default file\" = nil)
1785 ;;; SOURCE-REGEXP is typically going to be something like (tea-mode)
1786 ;;; for T programs, (lisp-mode) for Lisp programs, (soar-mode lisp-mode)
1787 ;;; for Soar programs, etc.
1789 ;;; The function returns a pair: (default-directory . default-file).
1791 (defun term-source-default (previous-dir/file source-modes)
1792 (cond ((and buffer-file-name (memq major-mode source-modes))
1793 (cons (file-name-directory buffer-file-name)
1794 (file-name-nondirectory buffer-file-name)))
1797 (cons default-directory nil))))
1800 ;;; (TERM-CHECK-SOURCE fname)
1801 ;;;============================================================================
1802 ;;; Prior to loading or compiling (or otherwise processing) a file (in the CMU
1803 ;;; process-in-a-buffer modes), this function can be called on the filename.
1804 ;;; If the file is loaded into a buffer, and the buffer is modified, the user
1805 ;;; is queried to see if he wants to save the buffer before proceeding with
1806 ;;; the load or compile.
1808 (defun term-check-source (fname)
1809 (let ((buff (get-file-buffer fname)))
1811 (buffer-modified-p buff)
1812 (y-or-n-p (format "Save buffer %s first? "
1813 (buffer-name buff))))
1815 (let ((old-buffer (current-buffer)))
1818 (set-buffer old-buffer)))))
1821 ;;; (TERM-GET-SOURCE prompt prev-dir/file source-modes mustmatch-p)
1822 ;;;============================================================================
1823 ;;; TERM-GET-SOURCE is used to prompt for filenames in command-interpreter
1824 ;;; commands that process source files (like loading or compiling a file).
1825 ;;; It prompts for the filename, provides a default, if there is one,
1826 ;;; and returns the result filename.
1828 ;;; See TERM-SOURCE-DEFAULT for more on determining defaults.
1830 ;;; PROMPT is the prompt string. PREV-DIR/FILE is the (directory . file) pair
1831 ;;; from the last source processing command. SOURCE-MODES is a list of major
1832 ;;; modes used to determine what file buffers contain source files. (These
1833 ;;; two arguments are used for determining defaults). If MUSTMATCH-P is true,
1834 ;;; then the filename reader will only accept a file that exists.
1837 ;;; (interactive (term-get-source "Compile file: " prev-lisp-dir/file
1838 ;;; '(lisp-mode) t))
1840 ;;; This is pretty stupid about strings. It decides we're in a string
1841 ;;; if there's a quote on both sides of point on the current line.
1842 (defun term-extract-string ()
1843 "Returns string around POINT that starts the current line or nil."
1845 (let* ((point (point))
1846 (bol (progn (beginning-of-line) (point)))
1847 (eol (progn (end-of-line) (point)))
1848 (start (progn (goto-char point)
1849 (and (search-backward "\"" bol t)
1851 (end (progn (goto-char point)
1852 (and (search-forward "\"" eol t)
1855 (buffer-substring start end)))))
1857 (defun term-get-source (prompt prev-dir/file source-modes mustmatch-p)
1858 (let* ((def (term-source-default prev-dir/file source-modes))
1859 (stringfile (term-extract-string))
1860 (sfile-p (and stringfile
1862 (file-exists-p stringfile)
1864 (not (file-directory-p stringfile))))
1865 (defdir (if sfile-p (file-name-directory stringfile)
1867 (deffile (if sfile-p (file-name-nondirectory stringfile)
1869 (ans (read-file-name (if deffile (format "%s(default %s) "
1873 (concat defdir deffile)
1875 (list (expand-file-name (substitute-in-file-name ans)))))
1877 ;;; I am somewhat divided on this string-default feature. It seems
1878 ;;; to violate the principle-of-least-astonishment, in that it makes
1879 ;;; the default harder to predict, so you actually have to look and see
1880 ;;; what the default really is before choosing it. This can trip you up.
1881 ;;; On the other hand, it can be useful, I guess. I would appreciate feedback
1886 ;;; Simple process query facility.
1887 ;;; ===========================================================================
1888 ;;; This function is for commands that want to send a query to the process
1889 ;;; and show the response to the user. For example, a command to get the
1890 ;;; arglist for a Common Lisp function might send a "(arglist 'foo)" query
1891 ;;; to an inferior Common Lisp process.
1893 ;;; This simple facility just sends strings to the inferior process and pops
1894 ;;; up a window for the process buffer so you can see what the process
1895 ;;; responds with. We don't do anything fancy like try to intercept what the
1896 ;;; process responds with and put it in a pop-up window or on the message
1897 ;;; line. We just display the buffer. Low tech. Simple. Works good.
1899 ;;; Send to the inferior process PROC the string STR. Pop-up but do not select
1900 ;;; a window for the inferior process so that its response can be seen.
1901 (defun term-proc-query (proc str)
1902 (let* ((proc-buf (process-buffer proc))
1903 (proc-mark (process-mark proc)))
1904 (display-buffer proc-buf)
1905 (set-buffer proc-buf) ; but it's not the selected *window*
1906 (let ((proc-win (get-buffer-window proc-buf))
1907 (proc-pt (marker-position proc-mark)))
1908 (term-send-string proc str) ; send the query
1909 (accept-process-output proc) ; wait for some output
1910 ;; Try to position the proc window so you can see the answer.
1911 ;; This is bogus code. If you delete the (sit-for 0), it breaks.
1912 ;; I don't know why. Wizards invited to improve it.
1913 (if (not (pos-visible-in-window-p proc-pt proc-win))
1914 (let ((opoint (window-point proc-win)))
1915 (set-window-point proc-win proc-mark) (sit-for 0)
1916 (if (not (pos-visible-in-window-p opoint proc-win))
1918 (set-window-point proc-win opoint)))))))
1920 ;;; Returns the current column in the current screen line.
1921 ;;; Note: (current-column) yields column in buffer line.
1923 (defun term-horizontal-column ()
1924 (- (term-current-column) (term-start-line-column)))
1926 ;; Calls either vertical-motion or buffer-vertical-motion
1927 (defmacro term-vertical-motion (count)
1928 (list 'funcall 'term-vertical-motion count))
1930 ;; An emulation of vertical-motion that is independent of having a window.
1931 ;; Instead, it uses the term-width variable as the logical window width.
1933 (defun buffer-vertical-motion (count)
1935 (move-to-column (* term-width (/ (current-column) term-width)))
1939 (todo (+ count (/ (current-column) term-width))))
1941 ;; The loop interates over buffer lines;
1942 ;; H is the number of screen lines in the current line, i.e.
1943 ;; the ceiling of dividing the buffer line width by term-width.
1944 (while (and (<= (setq H (max (/ (+ (current-column) term-width -1)
1949 (setq todo (- todo H))
1950 (forward-char) ;; Move past the ?\n
1951 (end-of-line)) ;; and on to the end of the next line.
1952 (if (and (>= todo H) (> todo 0))
1953 (+ (- count todo) H -1) ;; Hit end of buffer.
1954 (move-to-column (* todo term-width))
1956 (t ;; (< count 0) ;; Similar algorithm, but for upward motion.
1959 (while (and (<= (setq H (max (/ (+ (current-column) term-width -1)
1963 (progn (beginning-of-line)
1965 (setq todo (- todo H))
1966 (backward-char)) ;; Move to end of previos line
1967 (if (and (>= todo H) (> todo 0))
1968 (+ count todo (- 1 H)) ;; Hit beginning of buffer.
1969 (move-to-column (* (- H todo 1) term-width))
1972 ;;; The term-start-line-column variable is used as a cache.
1973 (defun term-start-line-column ()
1974 (cond (term-start-line-column)
1975 ((let ((save-pos (point)))
1976 (term-vertical-motion 0)
1977 (setq term-start-line-column (current-column))
1978 (goto-char save-pos)
1979 term-start-line-column))))
1981 ;;; Same as (current-column), but uses term-current-column as a cache.
1982 (defun term-current-column ()
1983 (cond (term-current-column)
1984 ((setq term-current-column (current-column)))))
1986 ;;; Move DELTA column right (or left if delta < 0).
1988 (defun term-move-columns (delta)
1989 (setq term-current-column (+ (term-current-column) delta))
1990 (move-to-column term-current-column t))
1992 ;; Insert COUNT copies of CHAR in the default face.
1993 (defun term-insert-char (char count)
1994 (let ((old-point (point)))
1995 (insert-char char count)
1996 (put-text-property old-point (point) 'face 'default)))
1998 (defun term-current-row ()
1999 (cond (term-current-row)
2000 ((setq term-current-row
2003 (narrow-to-region term-home-marker (point-max))
2004 (- (term-vertical-motion -9999))))))))
2006 (defun term-adjust-current-row-cache (delta)
2007 (if term-current-row
2008 (setq term-current-row (+ term-current-row delta))))
2010 ;; True if currently doing PAGER handling.
2011 (defmacro term-handling-pager () 'term-pager-old-local-map)
2013 (defmacro term-using-alternate-sub-buffer () 'term-saved-home-marker)
2015 (defun term-terminal-pos ()
2016 (save-excursion ; save-restriction
2017 (let ((save-col (term-current-column))
2019 (term-vertical-motion 0)
2020 (setq x (- save-col (current-column)))
2021 (setq y (term-vertical-motion term-height))
2024 ;;; Terminal emulation
2025 ;;; This is the standard process filter for term buffers.
2026 ;;; It emulates (most of the features of) a VT100/ANSI-style terminal.
2028 (defun term-emulate-terminal (proc str)
2029 (let* ((previous-buffer (current-buffer))
2030 (i 0) char funny count save-point save-marker old-point temp win
2031 (selected (selected-window))
2032 (str-length (length str)))
2035 (set-buffer (process-buffer proc))
2037 (if (marker-buffer term-pending-delete-marker)
2039 ;; Delete text following term-pending-delete-marker.
2040 (delete-region term-pending-delete-marker (process-mark proc))
2041 (set-marker term-pending-delete-marker nil)))
2043 (if (eq (window-buffer) (current-buffer))
2045 (setq term-vertical-motion (symbol-function 'vertical-motion))
2046 (term-check-size proc))
2047 (setq term-vertical-motion
2048 (symbol-function 'buffer-vertical-motion)))
2050 (setq save-marker (copy-marker (process-mark proc)))
2052 (if (/= (point) (process-mark proc))
2053 (progn (setq save-point (point-marker))
2054 (goto-char (process-mark proc))))
2057 ;; If the buffer is in line mode, and there is a partial
2058 ;; input line, save the line (by narrowing to leave it
2059 ;; outside the restriction ) until we're done with output.
2060 (if (and (> (point-max) (process-mark proc))
2061 (term-in-line-mode))
2062 (narrow-to-region (point-min) (process-mark proc)))
2065 (princ str term-log-buffer))
2066 (cond ((eq term-terminal-state 4) ;; Have saved pending output.
2067 (setq str (concat term-terminal-parameter str))
2068 (setq term-terminal-parameter nil)
2069 (setq str-length (length str))
2070 (setq term-terminal-state 0)))
2072 (while (< i str-length)
2073 (setq char (aref str i))
2074 (cond ((< term-terminal-state 2)
2075 ;; Look for prefix of regular chars
2077 (string-match "[\r\n\000\007\033\t\b\032\016\017]"
2079 (if (not funny) (setq funny str-length))
2081 (cond ((eq term-terminal-state 1)
2082 (term-move-columns 1)
2083 (setq term-terminal-state 0)))
2084 (setq count (- funny i))
2085 (setq temp (- (+ (term-horizontal-column) count)
2087 (cond ((<= temp 0)) ;; All count chars fit in line.
2088 ((> count temp) ;; Some chars fit.
2089 ;; This iteration, handle only what fits.
2090 (setq count (- count temp))
2091 (setq funny (+ count i)))
2092 ((> (term-handle-scroll 1) 0)
2093 (setq count (min count term-width))
2094 (setq funny (+ count i))
2095 (term-adjust-current-row-cache 1)
2096 (setq term-start-line-column
2097 term-current-column))
2098 (t ;; Doing PAGER processing.
2099 (setq count 0 funny i)
2100 (setq term-current-column nil)
2101 (setq term-start-line-column nil)))
2102 (if term-insert-mode
2103 ;; Inserting spaces, then deleting them, then
2104 ;; inserting the actual text seems clumsy, but
2105 ;; it is simple, and the overhead is miniscule.
2106 (term-insert-spaces count))
2107 (setq old-point (point))
2108 (term-move-columns count)
2109 (delete-region old-point (point))
2110 (insert (substring str i funny))
2111 (put-text-property old-point (point)
2112 'face term-current-face)
2113 ;; If the last char was written in last column,
2114 ;; back up one column, but remember we did so.
2115 ;; Thus we emulate xterm/vt100-style line-wrapping.
2117 (term-move-columns -1)
2118 (setq term-terminal-state 1)))
2119 (setq i (1- funny)))
2120 ((and (setq term-terminal-state 0)
2121 (eq char ?\^I)) ; TAB
2122 ;; FIXME: Does not handle line wrap!
2123 (setq count (term-current-column))
2124 (setq count (+ count 8 (- (mod count 8))))
2125 (if (< (move-to-column count nil) count)
2126 (term-insert-char char 1))
2127 (setq term-current-column count)
2128 (setq term-start-line-column nil))
2130 (term-move-columns -1))
2132 (term-vertical-motion 0)
2133 (setq term-current-column nil))
2135 (if (not (and term-kill-echo-list
2136 (term-check-kill-echo-list)))
2138 ((eq char ?\033) ; Escape
2139 (setq term-terminal-state 2))
2140 ((eq char 0)) ; NUL: Do nothing
2141 ((eq char ?\016)) ; Shift Out - ignored
2142 ((eq char ?\017)) ; Shift In - ignored
2146 (let ((end (string-match "\n" str i)))
2148 (progn (funcall term-command-hook
2149 (substring str (1+ i) (1- end)))
2151 (setq term-terminal-parameter
2153 (setq term-terminal-state 4)
2154 (setq i str-length))))
2155 (t ; insert char FIXME: Should never happen
2156 (term-move-columns 1)
2157 (backward-delete-char 1)
2159 ((eq term-terminal-state 2) ; Seen Esc
2160 (cond ((eq char ?\133) ;; ?\133 = ?[
2161 (make-local-variable 'term-terminal-parameter)
2162 (make-local-variable 'term-terminal-previous-parameter)
2163 (setq term-terminal-parameter 0)
2164 (setq term-terminal-previous-parameter 0)
2165 (setq term-terminal-state 3))
2166 ((eq char ?D) ;; scroll forward
2168 (setq term-terminal-state 0))
2169 ((eq char ?M) ;; scroll reversed
2170 (term-insert-lines 1)
2171 (setq term-terminal-state 0))
2172 ((eq char ?7) ;; Save cursor
2173 (setq term-saved-cursor
2174 (cons (term-current-row)
2175 (term-horizontal-column)))
2176 (setq term-terminal-state 0))
2177 ((eq char ?8) ;; Restore cursor
2178 (if term-saved-cursor
2179 (term-goto (car term-saved-cursor)
2180 (cdr term-saved-cursor)))
2181 (setq term-terminal-state 0))
2182 ((setq term-terminal-state 0))))
2183 ((eq term-terminal-state 3) ; Seen Esc [
2184 (cond ((and (>= char ?0) (<= char ?9))
2185 (setq term-terminal-parameter
2186 (+ (* 10 term-terminal-parameter) (- char ?0))))
2187 ((eq char ?\073 ) ; ?;
2188 (setq term-terminal-previous-parameter
2189 term-terminal-parameter)
2190 (setq term-terminal-parameter 0))
2191 ((eq char ??)) ; Ignore ?
2193 (term-handle-ansi-escape char)
2194 (setq term-terminal-state 0)))))
2195 (if (term-handling-pager)
2196 ;; Finish stuff to get ready to handle PAGER.
2198 (if (> (% (current-column) term-width) 0)
2199 (setq term-terminal-parameter
2201 ;; We're at column 0. Goto end of buffer; to compensate,
2202 ;; prepend a ?\r for later. This looks more consistent.
2204 (setq term-terminal-parameter
2205 (concat "\r" (substring str i)))
2206 (setq term-terminal-parameter (substring str (1- i)))
2207 (aset term-terminal-parameter 0 ?\r))
2208 (goto-char (point-max)))
2209 (setq term-terminal-state 4)
2210 (make-local-variable 'term-pager-old-filter)
2211 (setq term-pager-old-filter (process-filter proc))
2212 (set-process-filter proc term-pager-filter)
2213 (setq i str-length)))
2216 (set-marker (process-mark proc) (point))
2218 (progn (goto-char save-point)
2219 (set-marker save-point nil)))
2221 ;; Check for a pending filename-and-line number to display.
2222 ;; We do this before scrolling, because we might create a new window.
2223 (if (and term-pending-frame
2224 (eq (window-buffer selected) (current-buffer)))
2225 (progn (term-display-line (car term-pending-frame)
2226 (cdr term-pending-frame))
2227 (setq term-pending-frame nil)
2228 ;; We have created a new window, so check the window size.
2229 (term-check-size proc)))
2231 ;; Scroll each window displaying the buffer but (by default)
2232 ;; only if the point matches the process-mark we started with.
2235 (setq win (next-window win nil t))
2236 (if (eq (window-buffer win) (process-buffer proc))
2237 (let ((scroll term-scroll-to-bottom-on-output))
2239 (if (or (= (point) save-marker)
2240 (eq scroll t) (eq scroll 'all)
2241 ;; Maybe user wants point to jump to the end.
2242 (and (eq selected win)
2243 (or (eq scroll 'this) (not save-point)))
2244 (and (eq scroll 'others)
2245 (not (eq selected win))))
2247 (goto-char term-home-marker)
2249 (goto-char (process-mark proc))
2250 (if (not (pos-visible-in-window-p (point) win))
2252 ;; Optionally scroll so that the text
2253 ;; ends at the bottom of the window.
2254 (if (and term-scroll-show-maximum-output
2255 (>= (point) (process-mark proc)))
2257 (goto-char (point-max))
2259 (not (eq win selected))))
2261 (set-marker save-marker nil))
2262 ;; unwind-protect cleanup-forms follow:
2263 (set-buffer previous-buffer)
2264 (select-window selected))))
2266 ;;; Handle a character assuming (eq terminal-state 2) -
2267 ;;; i.e. we have previousely seen Escape followed by ?[.
2269 (defun term-handle-ansi-escape (char)
2271 ((eq char ?H) ; cursor motion
2272 (if (<= term-terminal-parameter 0)
2273 (setq term-terminal-parameter 1))
2274 (if (<= term-terminal-previous-parameter 0)
2275 (setq term-terminal-previous-parameter 1))
2277 (1- term-terminal-previous-parameter)
2278 (1- term-terminal-parameter)))
2281 (term-down (- (max 1 term-terminal-parameter)) 0 t))
2282 ;; \E[B - cursor down
2284 (term-down (max 1 term-terminal-parameter) 0 t))
2285 ;; \E[C - cursor right
2287 (term-move-columns (max 1 term-terminal-parameter)))
2288 ;; \E[D - cursor left
2290 (term-move-columns (- (max 1 term-terminal-parameter))))
2291 ;; \E[J - clear to end of screen
2293 (term-erase-in-display term-terminal-parameter))
2294 ;; \E[K - clear to end of line
2296 (term-erase-in-line term-terminal-parameter))
2297 ;; \E[L - insert lines
2299 (term-insert-lines (max 1 term-terminal-parameter)))
2300 ;; \E[M - delete lines
2302 (term-delete-lines (max 1 term-terminal-parameter)))
2303 ;; \E[P - delete chars
2305 (term-delete-chars (max 1 term-terminal-parameter)))
2306 ;; \E[@ - insert spaces
2308 (term-insert-spaces (max 1 term-terminal-parameter)))
2309 ;; \E[?h - DEC Private Mode Set
2311 (cond ((eq term-terminal-parameter 4)
2312 (setq term-insert-mode t))
2313 ((eq term-terminal-parameter 47)
2314 (term-switch-to-alternate-sub-buffer t))))
2315 ;; \E[?l - DEC Private Mode Reset
2317 (cond ((eq term-terminal-parameter 4)
2318 (setq term-insert-mode nil))
2319 ((eq term-terminal-parameter 47)
2320 (term-switch-to-alternate-sub-buffer nil))))
2321 ;; \E[m - Set/reset standard mode
2323 (cond ((eq term-terminal-parameter 7)
2324 (setq term-current-face 'highlight))
2325 ((eq term-terminal-parameter 4)
2326 (setq term-current-face 'term-underline-face))
2327 ((eq term-terminal-parameter 1)
2328 (setq term-current-face 'bold))
2329 (t (setq term-current-face 'default))))
2330 ;; \E[r - Set scrolling region
2333 (1- term-terminal-previous-parameter)
2334 term-terminal-parameter))
2337 (defun term-scroll-region (top bottom)
2338 "Set scrolling region.
2339 TOP is the top-most line (inclusive) of the new scrolling region,
2340 while BOTTOM is the line folling the new scrolling region (e.g. exclusive).
2341 The top-most line is line 0."
2342 (setq term-scroll-start
2343 (if (or (< top 0) (>= top term-height))
2346 (setq term-scroll-end
2347 (if (or (< bottom term-scroll-start) (> bottom term-height))
2350 (setq term-scroll-with-delete
2351 (or (term-using-alternate-sub-buffer)
2352 (not (and (= term-scroll-start 0)
2353 (= term-scroll-end term-height))))))
2355 (defun term-switch-to-alternate-sub-buffer (set)
2356 ;; If asked to switch to (from) the alternate sub-buffer, and already (not)
2357 ;; using it, do nothing. This test is needed for some programs (including
2358 ;; emacs) that emit the ti termcap string twice, for unknown reason.
2359 (if (eq set (not (term-using-alternate-sub-buffer)))
2360 (let ((row (term-current-row))
2361 (col (term-horizontal-column)))
2363 (goto-char (point-max))
2364 (if (not (eq (preceding-char) ?\n))
2365 (term-insert-char ?\n 1))
2366 (setq term-scroll-with-delete t)
2367 (setq term-saved-home-marker (copy-marker term-home-marker))
2368 (set-marker term-home-marker (point)))
2370 (setq term-scroll-with-delete
2371 (not (and (= term-scroll-start 0)
2372 (= term-scroll-end term-height))))
2373 (set-marker term-home-marker term-saved-home-marker)
2374 (set-marker term-saved-home-marker nil)
2375 (setq term-saved-home-marker nil)
2376 (goto-char term-home-marker)))
2377 (setq term-current-column nil)
2378 (setq term-line-start-column nil)
2379 (setq term-current-row 0)
2380 (term-goto row col))))
2382 ;; Default value for the symbol term-command-hook.
2384 (defun term-command-hook (string)
2385 (cond ((= (aref string 0) ?\032)
2386 ;; gdb (when invoked with -fullname) prints:
2387 ;; \032\032FULLFILENAME:LINENUMBER:CHARPOS:BEG_OR_MIDDLE:PC\n
2388 (let* ((first-colon (string-match ":" string 1))
2390 (string-match ":" string (1+ first-colon)))
2391 (filename (substring string 1 first-colon))
2392 (fileline (string-to-int
2393 (substring string (1+ first-colon) second-colon))))
2394 (setq term-pending-frame (cons filename fileline))))
2395 ((= (aref string 0) ?/)
2396 (cd (substring string 1)))
2397 ;; Allowing the inferior to call functions in emacs is
2398 ;; probably too big a security hole.
2399 ;; ((= (aref string 0) ?!)
2400 ;; (eval (car (read-from-string string 1))))
2401 (t)));; Otherwise ignore it
2403 ;; Make sure the file named TRUE-FILE is in a buffer that appears on the screen
2404 ;; and that its line LINE is visible.
2405 ;; Put the overlay-arrow on the line LINE in that buffer.
2406 ;; This is mainly used by gdb.
2408 (defun term-display-line (true-file line)
2409 (term-display-buffer-line (find-file-noselect true-file) line))
2411 (defun term-display-buffer-line (buffer line)
2412 (let* ((window (display-buffer buffer t))
2420 (setq overlay-arrow-string "=>")
2421 (or overlay-arrow-position
2422 (setq overlay-arrow-position (make-marker)))
2423 (set-marker overlay-arrow-position (point) (current-buffer)))
2424 (cond ((or (< pos (point-min)) (> pos (point-max)))
2427 (set-window-point window overlay-arrow-position)))
2429 ;;; The buffer-local marker term-home-marker defines the "home position"
2430 ;;; (in terms of cursor motion). However, we move the term-home-marker
2431 ;;; "down" as needed so that is no more that a window-full above (point-max).
2433 (defun term-goto-home ()
2434 (goto-char term-home-marker)
2435 (setq term-current-row 0)
2436 (setq term-current-column (current-column))
2437 (setq term-start-line-column term-current-column))
2439 ;;; FIXME: This can be optimized some.
2440 (defun term-goto (row col)
2442 (term-down row col))
2444 ; The page is full, so enter "pager" mode, and wait for input.
2446 (defun term-process-pager ()
2447 (if (not term-pager-break-map)
2448 (let* ((map (make-keymap))
2451 ; (define-key map (make-string 1 i) 'term-send-raw)
2453 (define-key map "\e"
2454 (lookup-key (current-global-map) "\e"))
2455 (define-key map "\C-x"
2456 (lookup-key (current-global-map) "\C-x"))
2457 (define-key map "\C-u"
2458 (lookup-key (current-global-map) "\C-u"))
2459 (define-key map " " 'term-pager-page)
2460 (define-key map "\r" 'term-pager-line)
2461 (define-key map "?" 'term-pager-help)
2462 (define-key map "h" 'term-pager-help)
2463 (define-key map "b" 'term-pager-back-page)
2464 (define-key map "\177" 'term-pager-back-line)
2465 (define-key map "q" 'term-pager-discard)
2466 (define-key map "D" 'term-pager-disable)
2467 (define-key map "<" 'term-pager-bob)
2468 (define-key map ">" 'term-pager-eob)
2473 (define-key map [menu-bar terminal] term-terminal-menu)
2474 (define-key map [menu-bar signals] term-signals-menu)
2475 (setq tmp (make-sparse-keymap "More pages?"))
2476 (define-key tmp [help] '("Help" . term-pager-help))
2477 (define-key tmp [disable]
2478 '("Diable paging" . term-fake-pager-disable))
2479 (define-key tmp [discard]
2480 '("Discard remaining output" . term-pager-discard))
2481 (define-key tmp [eob] '("Goto to end" . term-pager-eob))
2482 (define-key tmp [bob] '("Goto to beginning" . term-pager-bob))
2483 (define-key tmp [line] '("1 line forwards" . term-pager-line))
2484 (define-key tmp [bline] '("1 line backwards" . term-pager-back-line))
2485 (define-key tmp [back] '("1 page backwards" . term-pager-back-page))
2486 (define-key tmp [page] '("1 page forwards" . term-pager-page))
2487 (define-key map [menu-bar page] (cons "More pages?" tmp))
2490 (setq term-pager-break-map map)))
2491 ; (let ((process (get-buffer-process (current-buffer))))
2492 ; (stop-process process))
2493 (setq term-pager-old-local-map (current-local-map))
2494 (use-local-map term-pager-break-map)
2495 (make-local-variable 'term-old-mode-line-format)
2496 (setq term-old-mode-line-format mode-line-format)
2497 (setq mode-line-format
2498 (list "-- **MORE** "
2499 mode-line-buffer-identification
2500 " [Type ? for help] "
2502 (set-buffer-modified-p (buffer-modified-p))) ;;No-op, but updates mode line.
2504 (defun term-pager-line (lines)
2506 (let* ((moved (vertical-motion (1+ lines)))
2507 (deficit (- lines moved)))
2510 (cond ((<= deficit 0) ;; OK, had enough in the buffer for request.
2511 (recenter (1- term-height)))
2512 ((term-pager-continue deficit)))))
2514 (defun term-pager-page (arg)
2515 "Proceed past the **MORE** break, allowing the next page of output to appear"
2517 (term-pager-line (* arg term-height)))
2519 ; Pager mode command to go to beginning of buffer
2520 (defun term-pager-bob ()
2522 (goto-char (point-min))
2523 (if (= (vertical-motion term-height) term-height)
2525 (recenter (1- term-height)))
2527 ; pager mode command to go to end of buffer
2528 (defun term-pager-eob ()
2530 (goto-char term-home-marker)
2532 (goto-char (process-mark (get-buffer-process (current-buffer)))))
2534 (defun term-pager-back-line (lines)
2536 (vertical-motion (- 1 lines))
2540 ;; Move cursor to end of window.
2541 (vertical-motion term-height)
2543 (recenter (1- term-height)))
2545 (defun term-pager-back-page (arg)
2547 (term-pager-back-line (* arg term-height)))
2549 (defun term-pager-discard ()
2551 (setq term-terminal-parameter "")
2552 (interrupt-process nil t)
2553 (term-pager-continue term-height))
2555 ; Disable pager processing.
2556 ; Only callable while in pager mode. (Contrast term-disable-pager.)
2557 (defun term-pager-disable ()
2559 (if (term-handling-pager)
2560 (term-pager-continue nil)
2561 (setq term-pager-count nil)))
2563 ; Enable pager processing.
2564 (defun term-pager-enable ()
2566 (or term-pager-count
2567 (setq term-pager-count 0))) ;; Or maybe set to (term-current-row) ??
2569 (defun term-pager-toggle ()
2571 (if term-pager-count (term-pager-disable) (term-pager-enable)))
2574 (defalias 'term-fake-pager-enable 'term-pager-toggle)
2575 (defalias 'term-fake-pager-disable 'term-pager-toggle)
2576 (put 'term-char-mode 'menu-enable '(term-in-line-mode))
2577 (put 'term-line-mode 'menu-enable '(term-in-char-mode))
2578 (put 'term-fake-pager-enable 'menu-enable '(not term-pager-count))
2579 (put 'term-fake-pager-disable 'menu-enable 'term-pager-count))
2581 (defun term-pager-help ()
2582 "Provide help on commands available in a terminal-emulator **MORE** break"
2584 (message "Terminal-emulator pager break help...")
2587 (function (lambda ()
2588 (princ (substitute-command-keys
2589 "\\<term-pager-break-map>\
2590 Terminal-emulator MORE break.\n\
2591 Type one of the following keys:\n\n\
2592 \\[term-pager-page]\t\tMove forward one page.\n\
2593 \\[term-pager-line]\t\tMove forward one line.\n\
2594 \\[universal-argument] N \\[term-pager-page]\tMove N pages forward.\n\
2595 \\[universal-argument] N \\[term-pager-line]\tMove N lines forward.\n\
2596 \\[universal-argument] N \\[term-pager-back-line]\tMove N lines back.\n\
2597 \\[universal-argument] N \\[term-pager-back-page]\t\tMove N pages back.\n\
2598 \\[term-pager-bob]\t\tMove to the beginning of the buffer.\n\
2599 \\[term-pager-eob]\t\tMove to the end of the buffer.\n\
2600 \\[term-pager-discard]\t\tKill pending output and kill process.\n\
2601 \\[term-pager-disable]\t\tDisable PAGER handling.\n\n\
2602 \\{term-pager-break-map}\n\
2603 Any other key is passed through to the program
2604 running under the terminal emulator and disables pager processing until
2605 all pending output has been dealt with."))
2608 (defun term-pager-continue (new-count)
2609 (let ((process (get-buffer-process (current-buffer))))
2610 (use-local-map term-pager-old-local-map)
2611 (setq term-pager-old-local-map nil)
2612 (setq mode-line-format term-old-mode-line-format)
2613 (set-buffer-modified-p (buffer-modified-p)) ;; Updates mode line.
2614 (setq term-pager-count new-count)
2615 (set-process-filter process term-pager-old-filter)
2616 (funcall term-pager-old-filter process "")
2617 (continue-process process)))
2619 ;; Make sure there are DOWN blank lines below the current one.
2620 ;; Return 0 if we're unable (because of PAGER handling), else return DOWN.
2622 (defun term-handle-scroll (down)
2623 (let ((scroll-needed
2624 (- (+ (term-current-row) down 1) term-scroll-end)))
2625 (if (> scroll-needed 0)
2626 (let ((save-point (copy-marker (point))) (save-top))
2627 (goto-char term-home-marker)
2628 (cond (term-scroll-with-delete
2629 ;; delete scroll-needed lines at term-scroll-start
2630 (term-vertical-motion term-scroll-start)
2631 (setq save-top (point))
2632 (term-vertical-motion scroll-needed)
2633 (delete-region save-top (point))
2634 (goto-char save-point)
2635 (term-vertical-motion down)
2636 (term-insert-char ?\n scroll-needed))
2637 ((and (numberp term-pager-count)
2638 (< (setq term-pager-count (- term-pager-count down))
2641 (term-process-pager))
2643 (term-vertical-motion scroll-needed)
2644 (set-marker term-home-marker (point))))
2645 (goto-char save-point)
2646 (set-marker save-point nil)
2647 (setq term-current-column nil)
2648 (setq term-line-start-column nil)
2649 (setq term-current-row nil))))
2652 (defun term-down (down right &optional check-for-scroll)
2653 "Move down DOWN screen lines vertically, and RIGHT columns horizontally."
2654 (let ((start-column (term-horizontal-column)))
2655 (if check-for-scroll
2656 (setq down (term-handle-scroll down)))
2657 (term-adjust-current-row-cache down)
2658 (setq down (- down (term-vertical-motion down)))
2659 ; Extend buffer with extra blank lines if needed.
2660 (if (> down 0) (term-insert-char ?\n down))
2661 (setq term-current-column nil)
2662 (setq term-start-line-column (current-column))
2663 (move-to-column (+ term-start-line-column start-column right) t)))
2665 ;; Assuming point is at the beginning of a screen line,
2666 ;; if the line above point wraps around, add a ?\n to undo the wrapping.
2667 ;; FIXME: Probably should be called more than it is.
2668 (defun term-unwrap-line ()
2669 (if (not (bolp)) (insert-before-markers ?\n)))
2671 (defun term-erase-in-line (kind)
2672 (if (> kind 1) ;; erase left of point
2673 (let ((cols (term-horizontal-column)) (saved-point (point))
2674 (term-vertical-motion 0)
2675 (delete-region (point) saved-point)
2676 (term-insert-char ?\n cols))))
2677 (if (not (eq kind 1)) ;; erase right of point
2678 (let ((saved-point (point))
2679 (wrapped (and (zerop (term-horizontal-column))
2680 (not (zerop (term-current-column))))))
2681 (term-vertical-motion 1)
2682 (delete-region saved-point (point))
2683 ;; wrapped is true if we're at the beginning of screen line,
2684 ;; but not a buffer line. If we delete the current screen line
2685 ;; that will make the previous line no longer wrap, and (because
2686 ;; of the way emacs display works) point will be at the end of
2687 ;; the previous screen line rather then the beginning of the
2688 ;; current one. To avoid that, we make sure that current line
2689 ;; contain a space, to force the previous line to continue to wrap.
2690 ;; We could do this always, but it seems preferable to not add the
2691 ;; extra space when wrapped is false.
2695 (put-text-property saved-point (point) 'face 'default)
2696 (goto-char saved-point))))
2698 (defun term-erase-in-display (kind)
2699 "Erases (that is blanks out) part of the window.
2700 If KIND is 0, erase from (point) to (point-max);
2701 if KIND is 1, erase from home to point; else erase from home to point-max.
2702 Should only be called when point is at the start of a screen line."
2703 (cond ((eq term-terminal-parameter 0)
2704 (delete-region (point) (point-max))
2706 ((let ((row (term-current-row))
2707 (col (term-horizontal-column))
2708 (start-region term-home-marker)
2709 (end-region (if (eq kind 1) (point) (point-max))))
2710 (delete-region start-region end-region)
2713 (term-insert-char \?n row))
2714 (setq term-current-column nil)
2715 (setq term-line-start-column nil)
2716 (setq term-current-row nil)
2717 (term-goto row col)))))
2719 (defun term-delete-chars (count)
2720 (let ((save-point (point)))
2721 (term-vertical-motion 1)
2723 (goto-char save-point)
2724 (move-to-column (+ (term-current-column) count) t)
2725 (delete-region save-point (point))))
2727 (defun term-insert-spaces (count)
2728 (let ((save-point (point)) (save-eol))
2729 (term-vertical-motion 1)
2732 (setq save-eol (point))
2733 (move-to-column (+ (term-start-line-column) (- term-width count)) t)
2734 (if (> save-eol (point))
2735 (delete-region (point) save-eol))
2736 (goto-char save-point)
2737 (term-insert-char ? count)
2738 (goto-char save-point)))
2740 (defun term-delete-lines (lines)
2741 (let ((start (point))
2742 (save-current-column term-current-column)
2743 (save-start-line-column term-start-line-column)
2744 (save-current-row (term-current-row)))
2746 (delete-region start (point))
2747 (term-down (- term-scroll-end save-current-row lines) 0)
2748 (term-insert-char ?\n lines)
2749 (setq term-current-column save-current-column)
2750 (setq term-start-line-column save-start-line-column)
2751 (setq term-current-row save-current-row)
2754 (defun term-insert-lines (lines)
2755 (let ((start (point))
2757 (save-current-column term-current-column)
2758 (save-start-line-column term-start-line-column)
2759 (save-current-row (term-current-row)))
2760 (term-down (- term-scroll-end save-current-row lines) 0)
2761 (setq start-deleted (point))
2763 (delete-region start-deleted (point))
2765 (setq term-current-column save-current-column)
2766 (setq term-start-line-column save-start-line-column)
2767 (setq term-current-row save-current-row)
2768 (term-insert-char ?\n lines)
2771 (defun term-set-output-log (name)
2772 "Record raw inferior process output in a buffer."
2773 (interactive (list (if term-log-buffer
2775 (read-buffer "Record output in buffer: "
2776 (format "%s output-log"
2777 (buffer-name (current-buffer)))
2779 (if (or (null name) (equal name ""))
2780 (progn (setq term-log-buffer nil)
2781 (message "Output logging off."))
2782 (if (get-buffer name)
2785 (set-buffer (get-buffer-create name))
2787 (buffer-disable-undo (current-buffer))
2789 (setq term-log-buffer (get-buffer name))
2790 (message "Recording terminal emulator output into buffer \"%s\""
2791 (buffer-name term-log-buffer))))
2793 (defun term-stop-photo ()
2794 "Discontinue raw inferior process logging."
2796 (term-set-output-log nil))
2798 (defun term-show-maximum-output ()
2799 "Put the end of the buffer at the bottom of the window."
2801 (goto-char (point-max))
2804 ;;; Do the user's customisation...
2806 (defvar term-load-hook nil
2807 "This hook is run when term is loaded in.
2808 This is a good place to put keybindings.")
2810 (run-hooks 'term-load-hook)
2813 ;;; Filename/command/history completion in a buffer
2814 ;;; ===========================================================================
2815 ;;; Useful completion functions, courtesy of the Ergo group.
2818 ;;; term-dynamic-complete Complete or expand command, filename,
2819 ;;; history at point.
2820 ;;; term-dynamic-complete-filename Complete filename at point.
2821 ;;; term-dynamic-list-filename-completions List completions in help buffer.
2822 ;;; term-replace-by-expanded-filename Expand and complete filename at point;
2823 ;;; replace with expanded/completed name.
2824 ;;; term-dynamic-simple-complete Complete stub given candidates.
2826 ;;; These are not installed in the term-mode keymap. But they are
2827 ;;; available for people who want them. Shell-mode installs them:
2828 ;;; (define-key shell-mode-map "\t" 'term-dynamic-complete)
2829 ;;; (define-key shell-mode-map "\M-?"
2830 ;;; 'term-dynamic-list-filename-completions)))
2832 ;;; Commands like this are fine things to put in load hooks if you
2833 ;;; want them present in specific modes.
2835 (defvar term-completion-autolist nil
2836 "*If non-nil, automatically list possiblities on partial completion.
2837 This mirrors the optional behavior of tcsh.")
2839 (defvar term-completion-addsuffix t
2840 "*If non-nil, add a `/' to completed directories, ` ' to file names.
2841 This mirrors the optional behavior of tcsh.")
2843 (defvar term-completion-recexact nil
2844 "*If non-nil, use shortest completion if characters cannot be added.
2845 This mirrors the optional behavior of tcsh.
2847 A non-nil value is useful if `term-completion-autolist' is non-nil too.")
2849 (defvar term-completion-fignore nil
2850 "*List of suffixes to be disregarded during file completion.
2851 This mirrors the optional behavior of bash and tcsh.
2853 Note that this applies to `term-dynamic-complete-filename' only.")
2855 (defvar term-file-name-prefix ""
2856 "Prefix prepended to absolute file names taken from process input.
2857 This is used by term's and shell's completion functions, and by shell's
2858 directory tracking functions.")
2861 (defun term-directory (directory)
2862 ;; Return expanded DIRECTORY, with `term-file-name-prefix' if absolute.
2863 (expand-file-name (if (file-name-absolute-p directory)
2864 (concat term-file-name-prefix directory)
2868 (defun term-word (word-chars)
2869 "Return the word of WORD-CHARS at point, or nil if non is found.
2870 Word constituents are considered to be those in WORD-CHARS, which is like the
2871 inside of a \"[...]\" (see `skip-chars-forward')."
2873 (let ((limit (point))
2874 (word (concat "[" word-chars "]"))
2875 (non-word (concat "[^" word-chars "]")))
2876 (if (re-search-backward non-word nil 'move)
2878 ;; Anchor the search forwards.
2879 (if (or (eolp) (looking-at non-word))
2881 (re-search-forward (concat word "+") limit)
2882 (buffer-substring (match-beginning 0) (match-end 0))))))
2885 (defun term-match-partial-filename ()
2886 "Return the filename at point, or nil if non is found.
2887 Environment variables are substituted. See `term-word'."
2888 (let ((filename (term-word "~/A-Za-z0-9+@:_.$#,={}-")))
2889 (and filename (substitute-in-file-name filename))))
2892 (defun term-dynamic-complete ()
2893 "Dynamically perform completion at point.
2894 Calls the functions in `term-dynamic-complete-functions' to perform
2895 completion until a function returns non-nil, at which point completion is
2896 assumed to have occurred."
2898 (let ((functions term-dynamic-complete-functions))
2899 (while (and functions (null (funcall (car functions))))
2900 (setq functions (cdr functions)))))
2903 (defun term-dynamic-complete-filename ()
2904 "Dynamically complete the filename at point.
2905 Completes if after a filename. See `term-match-partial-filename' and
2906 `term-dynamic-complete-as-filename'.
2907 This function is similar to `term-replace-by-expanded-filename', except that
2908 it won't change parts of the filename already entered in the buffer; it just
2909 adds completion characters to the end of the filename. A completions listing
2910 may be shown in a help buffer if completion is ambiguous.
2912 Completion is dependent on the value of `term-completion-addsuffix',
2913 `term-completion-recexact' and `term-completion-fignore', and the timing of
2914 completions listing is dependent on the value of `term-completion-autolist'.
2916 Returns t if successful."
2918 (if (term-match-partial-filename)
2919 (prog2 (or (eq (selected-window) (minibuffer-window))
2920 (message "Completing file name..."))
2921 (term-dynamic-complete-as-filename))))
2923 (defun term-dynamic-complete-as-filename ()
2924 "Dynamically complete at point as a filename.
2925 See `term-dynamic-complete-filename'. Returns t if successful."
2926 (let* ((completion-ignore-case nil)
2927 (completion-ignored-extensions term-completion-fignore)
2929 (filename (or (term-match-partial-filename) ""))
2930 (pathdir (file-name-directory filename))
2931 (pathnondir (file-name-nondirectory filename))
2932 (directory (if pathdir (term-directory pathdir) default-directory))
2933 (completion (file-name-completion pathnondir directory))
2934 (mini-flag (eq (selected-window) (minibuffer-window))))
2935 (cond ((null completion)
2936 (message "No completions of %s" filename)
2938 ((eq completion t) ; Means already completed "file".
2939 (if term-completion-addsuffix (insert " "))
2940 (or mini-flag (message "Sole completion")))
2941 ((string-equal completion "") ; Means completion on "directory/".
2942 (term-dynamic-list-filename-completions))
2943 (t ; Completion string returned.
2944 (let ((file (concat (file-name-as-directory directory) completion)))
2945 (insert (substring (directory-file-name completion)
2946 (length pathnondir)))
2947 (cond ((symbolp (file-name-completion completion directory))
2948 ;; We inserted a unique completion.
2949 (if term-completion-addsuffix
2950 (insert (if (file-directory-p file) "/" " ")))
2951 (or mini-flag (message "Completed")))
2952 ((and term-completion-recexact term-completion-addsuffix
2953 (string-equal pathnondir completion)
2954 (file-exists-p file))
2955 ;; It's not unique, but user wants shortest match.
2956 (insert (if (file-directory-p file) "/" " "))
2957 (or mini-flag (message "Completed shortest")))
2958 ((or term-completion-autolist
2959 (string-equal pathnondir completion))
2960 ;; It's not unique, list possible completions.
2961 (term-dynamic-list-filename-completions))
2963 (or mini-flag (message "Partially completed")))))))
2967 (defun term-replace-by-expanded-filename ()
2968 "Dynamically expand and complete the filename at point.
2969 Replace the filename with an expanded, canonicalised and completed replacement.
2970 \"Expanded\" means environment variables (e.g., $HOME) and `~'s are replaced
2971 with the corresponding directories. \"Canonicalised\" means `..' and `.' are
2972 removed, and the filename is made absolute instead of relative. For expansion
2973 see `expand-file-name' and `substitute-in-file-name'. For completion see
2974 `term-dynamic-complete-filename'."
2976 (replace-match (expand-file-name (term-match-partial-filename)) t t)
2977 (term-dynamic-complete-filename))
2980 (defun term-dynamic-simple-complete (stub candidates)
2981 "Dynamically complete STUB from CANDIDATES list.
2982 This function inserts completion characters at point by completing STUB from
2983 the strings in CANDIDATES. A completions listing may be shown in a help buffer
2984 if completion is ambiguous.
2986 Returns nil if no completion was inserted.
2987 Returns `sole' if completed with the only completion match.
2988 Returns `shortest' if completed with the shortest of the completion matches.
2989 Returns `partial' if completed as far as possible with the completion matches.
2990 Returns `listed' if a completion listing was shown.
2992 See also `term-dynamic-complete-filename'."
2993 (let* ((completion-ignore-case nil)
2994 (candidates (mapcar (function (lambda (x) (list x))) candidates))
2995 (completions (all-completions stub candidates)))
2996 (cond ((null completions)
2997 (message "No completions of %s" stub)
2999 ((= 1 (length completions)) ; Gotcha!
3000 (let ((completion (car completions)))
3001 (if (string-equal completion stub)
3002 (message "Sole completion")
3003 (insert (substring completion (length stub)))
3004 (message "Completed"))
3005 (if term-completion-addsuffix (insert " "))
3007 (t ; There's no unique completion.
3008 (let ((completion (try-completion stub candidates)))
3009 ;; Insert the longest substring.
3010 (insert (substring completion (length stub)))
3011 (cond ((and term-completion-recexact term-completion-addsuffix
3012 (string-equal stub completion)
3013 (member completion completions))
3014 ;; It's not unique, but user wants shortest match.
3016 (message "Completed shortest")
3018 ((or term-completion-autolist
3019 (string-equal stub completion))
3020 ;; It's not unique, list possible completions.
3021 (term-dynamic-list-completions completions)
3024 (message "Partially completed")
3028 (defun term-dynamic-list-filename-completions ()
3029 "List in help buffer possible completions of the filename at point."
3031 (let* ((completion-ignore-case nil)
3032 (filename (or (term-match-partial-filename) ""))
3033 (pathdir (file-name-directory filename))
3034 (pathnondir (file-name-nondirectory filename))
3035 (directory (if pathdir (term-directory pathdir) default-directory))
3036 (completions (file-name-all-completions pathnondir directory)))
3038 (term-dynamic-list-completions completions)
3039 (message "No completions of %s" filename))))
3042 (defun term-dynamic-list-completions (completions)
3043 "List in help buffer sorted COMPLETIONS.
3044 Typing SPC flushes the help buffer."
3045 (let ((conf (current-window-configuration)))
3046 (with-output-to-temp-buffer "*Completions*"
3047 (display-completion-list (sort completions 'string-lessp)))
3048 (message "Hit space to flush")
3051 (set-buffer (get-buffer "*Completions*"))
3052 (setq key (read-key-sequence nil)
3055 (eq (window-buffer (posn-window (event-start first)))
3056 (get-buffer "*Completions*"))
3057 (eq (key-binding key) 'mouse-choose-completion)))
3058 ;; If the user does mouse-choose-completion with the mouse,
3059 ;; execute the command, then delete the completion window.
3061 (mouse-choose-completion first)
3062 (set-window-configuration conf))
3064 (set-window-configuration conf)
3065 (setq unread-command-events (listify-key-sequence key)))))))
3067 ;;; Converting process modes to use term mode
3068 ;;; ===========================================================================
3069 ;;; Renaming variables
3070 ;;; Most of the work is renaming variables and functions. These are the common
3072 ;;; Local variables:
3073 ;;; last-input-start term-last-input-start
3074 ;;; last-input-end term-last-input-end
3075 ;;; shell-prompt-pattern term-prompt-regexp
3076 ;;; shell-set-directory-error-hook <no equivalent>
3078 ;;; shell-set-directory <unnecessary>
3079 ;;; shell-mode-map term-mode-map
3081 ;;; shell-send-input term-send-input
3082 ;;; shell-send-eof term-delchar-or-maybe-eof
3083 ;;; kill-shell-input term-kill-input
3084 ;;; interrupt-shell-subjob term-interrupt-subjob
3085 ;;; stop-shell-subjob term-stop-subjob
3086 ;;; quit-shell-subjob term-quit-subjob
3087 ;;; kill-shell-subjob term-kill-subjob
3088 ;;; kill-output-from-shell term-kill-output
3089 ;;; show-output-from-shell term-show-output
3090 ;;; copy-last-shell-input Use term-previous-input/term-next-input
3092 ;;; SHELL-SET-DIRECTORY is gone, its functionality taken over by
3093 ;;; SHELL-DIRECTORY-TRACKER, the shell mode's term-input-filter-functions.
3094 ;;; Term mode does not provide functionality equivalent to
3095 ;;; shell-set-directory-error-hook; it is gone.
3097 ;;; term-last-input-start is provided for modes which want to munge
3098 ;;; the buffer after input is sent, perhaps because the inferior
3099 ;;; insists on echoing the input. The LAST-INPUT-START variable in
3100 ;;; the old shell package was used to implement a history mechanism,
3101 ;;; but you should think twice before using term-last-input-start
3102 ;;; for this; the input history ring often does the job better.
3104 ;;; If you are implementing some process-in-a-buffer mode, called foo-mode, do
3105 ;;; *not* create the term-mode local variables in your foo-mode function.
3106 ;;; This is not modular. Instead, call term-mode, and let *it* create the
3107 ;;; necessary term-specific local variables. Then create the
3108 ;;; foo-mode-specific local variables in foo-mode. Set the buffer's keymap to
3109 ;;; be foo-mode-map, and its mode to be foo-mode. Set the term-mode hooks
3110 ;;; (term-{prompt-regexp, input-filter, input-filter-functions,
3111 ;;; get-old-input) that need to be different from the defaults. Call
3112 ;;; foo-mode-hook, and you're done. Don't run the term-mode hook yourself;
3113 ;;; term-mode will take care of it. The following example, from shell.el,
3116 ;;; (defvar shell-mode-map '())
3117 ;;; (cond ((not shell-mode-map)
3118 ;;; (setq shell-mode-map (copy-keymap term-mode-map))
3119 ;;; (define-key shell-mode-map "\C-c\C-f" 'shell-forward-command)
3120 ;;; (define-key shell-mode-map "\C-c\C-b" 'shell-backward-command)
3121 ;;; (define-key shell-mode-map "\t" 'term-dynamic-complete)
3122 ;;; (define-key shell-mode-map "\M-?"
3123 ;;; 'term-dynamic-list-filename-completions)))
3125 ;;; (defun shell-mode ()
3128 ;;; (setq term-prompt-regexp shell-prompt-pattern)
3129 ;;; (setq major-mode 'shell-mode)
3130 ;;; (setq mode-name "Shell")
3131 ;;; (use-local-map shell-mode-map)
3132 ;;; (make-local-variable 'shell-directory-stack)
3133 ;;; (setq shell-directory-stack nil)
3134 ;;; (add-hook 'term-input-filter-functions 'shell-directory-tracker)
3135 ;;; (run-hooks 'shell-mode-hook))
3138 ;;; Note that make-term is different from make-shell in that it
3139 ;;; doesn't have a default program argument. If you give make-shell
3140 ;;; a program name of NIL, it cleverly chooses one of explicit-shell-name,
3141 ;;; $ESHELL, $SHELL, or /bin/sh. If you give make-term a program argument
3142 ;;; of NIL, it barfs. Adjust your code accordingly...
3144 ;;; Completion for term-mode users
3146 ;;; For modes that use term-mode, term-dynamic-complete-functions is the
3147 ;;; hook to add completion functions to. Functions on this list should return
3148 ;;; non-nil if completion occurs (i.e., further completion should not occur).
3149 ;;; You could use term-dynamic-simple-complete to do the bulk of the
3154 ;;; term.el ends here