1 ;;; term.el --- general command interpreter in a window stuff
3 ;; Copyright (C) 1988, 1990, 1992, 1994-1995, 2001-2014 Free Software
6 ;; Author: Per Bothner <per@bothner.com>
7 ;; Maintainer: Dan Nicolaescu <dann@ics.uci.edu>, Per Bothner <per@bothner.com>
8 ;; Based on comint mode written by: Olin Shivers <shivers@cs.cmu.edu>
11 ;; This file is part of GNU Emacs.
13 ;; GNU Emacs is free software: you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation, either version 3 of the License, or
16 ;; (at your option) any later version.
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
27 ;; Fixes for CJK support by Yong Lu <lyongu@yahoo.com>.
29 ;; Dir/Hostname tracking and ANSI colorization by
30 ;; Marco Melgazzi <marco@techie.com>.
32 ;; To see what I've modified and where it came from search for '-mm'
36 ;; Speed considerations and a few caveats
37 ;; --------------------------------------
39 ;; While the message passing and the colorization surely introduce some
40 ;; overhead this has became so small that IMHO it is surely outweighed by
41 ;; the benefits you get but, as usual, YMMV.
43 ;; Important caveat, when deciding the cursor/'gray keys' keycodes I had to
44 ;; make a choice: on my Linux box this choice allows me to run all the
45 ;; ncurses applications without problems but make these keys
46 ;; incomprehensible to all the cursesX programs. Your mileage may vary so
47 ;; you may consider changing the default 'emulation'. Just search for this
48 ;; piece of code and modify it as you like:
50 ;; ;; Which would be better: "\e[A" or "\eOA"? readline accepts either.
51 ;; ;; For my configuration it's definitely better \eOA but YMMV. -mm
52 ;; ;; For example: vi works with \eOA while elm wants \e[A ...
53 ;; (defun term-send-up () (interactive) (term-send-raw-string "\eOA"))
54 ;; (defun term-send-down () (interactive) (term-send-raw-string "\eOB"))
55 ;; (defun term-send-right () (interactive) (term-send-raw-string "\eOC"))
56 ;; (defun term-send-left () (interactive) (term-send-raw-string "\eOD"))
59 ;; IMPORTANT: additions & changes
60 ;; ------------------------------
62 ;; With this enhanced ansi-term.el you will get a reliable mechanism of
63 ;; directory/username/host tracking: the only drawback is that you will
64 ;; have to modify your shell start-up script. It's worth it, believe me :).
66 ;; When you rlogin/su/telnet and the account you access has a modified
67 ;; startup script, you will be able to access the remote files as usual
68 ;; with C-x C-f, if it's needed you will have to enter a password,
69 ;; otherwise the file should get loaded straight away.
71 ;; This is useful even if you work only on one host: it often happens that,
72 ;; for maintenance reasons, you have to edit files 'as root': before
73 ;; patching term.el, I su-ed in a term.el buffer and used vi :), now I
74 ;; simply do a C-x C-f and, via ange-ftp, the file is automatically loaded
75 ;; 'as-root'. ( If you don't want to enter the root password every time you
76 ;; can put it in your .netrc: note that this is -not- advisable if you're
77 ;; connected to the internet or if somebody else works on your workstation!)
79 ;; If you use wu-ftpd you can use some of its features to avoid root ftp
80 ;; access to the rest of the world: just put in /etc/ftphosts something like
83 ;; allow root 127.0.0.1
85 ;; # By default nobody can't do anything
89 ;; ----------------------------------------
91 ;; If, instead of 'term', you call 'ansi-term', you get multiple term
92 ;; buffers, after every new call ansi-term opens a new *ansi-term*<xx> window,
93 ;; where <xx> is, as usual, a number...
95 ;; ----------------------------------------
97 ;; With the term-buffer-maximum-size you can finally decide how many
98 ;; scrollback lines to keep: its default is 2048 but you can change it as
101 ;; ----------------------------------------
104 ;; ANSI colorization should work well, I've decided to limit the interpreter
105 ;; to five outstanding commands (like ESC [ 01;04;32;41;07m.
106 ;; You shouldn't need more, if you do, tell me and I'll increase it. It's
107 ;; so easy you could do it yourself...
109 ;; Blink, is not supported. Currently it's mapped as bold.
111 ;; ----------------------------------------
113 ;; If you'd like to check out my complete configuration, you can download
114 ;; it from http://www.polito.it/~s64912/things.html, it's ~500k in size and
115 ;; contains my .cshrc, .emacs and my whole site-lisp subdirectory. (notice
116 ;; that this term.el may be newer/older than the one in there, please
119 ;; This complete configuration contains, among other things, a complete
120 ;; rectangular marking solution (based on rect-mark.el and
121 ;; pc-bindings.el) and should be a good example of how extensively Emacs
122 ;; can be configured on a ppp-connected ws.
124 ;; ----------------------------------------
128 ;; - Add hooks to allow raw-mode keys to be configurable
129 ;; - Which keys are better ? \eOA or \e[A ?
136 ;; - Huge reworking of the faces code: now we only have roughly 20-30
137 ;; faces for everything so we're even faster than the old md-term.el !
138 ;; - Finished removing all the J-Shell code.
142 ;; - Now all the supportable ANSI commands work well.
143 ;; - Reworked a little the code: much less jsh-inspired stuff
147 ;; - Now all the faces are accessed through an array: much cleaner code.
149 ;; V2.2 November 4 1996
151 ;; - Implemented ANSI output colorization ( a bit rough but enough for
154 ;; - Implemented a maximum limit for the scroll buffer (stolen from
157 ;; v2.1 October 28 1996, first public release
159 ;; - Some new keybindings for term-char mode ( notably home/end/...)
160 ;; - Directory, hostname and username tracking via ange-ftp
161 ;; - Multi-term capability via the ansi-term call
163 ;; ----------------------------------------------------------------
164 ;; You should/could have something like this in your .emacs to take
165 ;; full advantage of this package
167 ;; (add-hook 'term-mode-hook
170 ;; (setq term-prompt-regexp "^[^#$%>\n]*[#$%>] *")
171 ;; (make-local-variable 'mouse-yank-at-point)
172 ;; (make-local-variable 'transient-mark-mode)
173 ;; (setq mouse-yank-at-point t)
174 ;; (setq transient-mark-mode nil)
175 ;; (auto-fill-mode -1)
176 ;; (setq tab-width 8 ))))
179 ;; ----------------------------------------
181 ;; If you want to use color ls the best setup is to have a different file
182 ;; when you use eterm ( see above, mine is named .emacs_dircolors ). This
183 ;; is necessary because some terminals, rxvt for example, need non-ansi
184 ;; hacks to work ( for example on my rxvt white is wired to fg, and to
185 ;; obtain normal white I have to do bold-white :)
187 ;; ----------------------------------------
190 ;; # Configuration file for the color ls utility
191 ;; # This file goes in the /etc directory, and must be world readable.
192 ;; # You can copy this file to .dir_colors in your $HOME directory to
193 ;; # override the system defaults.
195 ;; # COLOR needs one of these arguments: 'tty' colorizes output to ttys, but
196 ;; # not pipes. 'all' adds color characters to all output. 'none' shuts
197 ;; # colorization off.
201 ;; # Below, there should be one TERM entry for each termtype that is
205 ;; # EIGHTBIT, followed by '1' for on, '0' for off. (8-bit output)
208 ;; # Below are the color init strings for the basic file types. A color init
209 ;; # string consists of one or more of the following numeric codes:
210 ;; # Attribute codes:
211 ;; # 00=none 01=bold 04=underscore 05=blink 07=reverse 08=concealed
212 ;; # Text color codes:
213 ;; # 30=black 31=red 32=green 33=yellow 34=blue 35=magenta 36=cyan 37=white
214 ;; # Background color codes:
215 ;; # 40=black 41=red 42=green 43=yellow 44=blue 45=magenta 46=cyan 47=white
216 ;; NORMAL 00 # global default, although everything should be something.
217 ;; FILE 00 # normal file
218 ;; DIR 00;37 # directory
219 ;; LINK 00;36 # symbolic link
221 ;; SOCK 40;35 # socket
222 ;; BLK 33;01 # block device driver
223 ;; CHR 33;01 # character device driver
225 ;; # This is for files with execute permission:
228 ;; # List any file extensions like '.gz' or '.tar' that you would like ls
229 ;; # to colorize below. Put the extension, a space, and the color init
230 ;; # string. (and any comments you want to add after a '#')
231 ;; .tar 01;33 # archives or compressed
240 ;; .jpg 01;35 # image formats
247 ;; ----------------------------------------
249 ;; Notice: for directory/host/user tracking you need to have something
250 ;; like this in your shell startup script ( this is for tcsh but should
251 ;; be quite easy to port to other shells )
253 ;; ----------------------------------------
257 ;; set host = `hostname`
260 ;; # su does not change this but I'd like it to
262 ;; set user = `whoami`
266 ;; if ( eterm =~ $TERM ) then
268 ;; echo --------------------------------------------------------------
270 ;; echo Today is $date
271 ;; echo We are on $host running $os under Emacs term mode
272 ;; echo --------------------------------------------------------------
274 ;; setenv EDITOR emacsclient
276 ;; # Notice: $host and $user have been set before to 'hostname' and 'whoami'
277 ;; # this is necessary because, f.e., certain versions of 'su' do not change
278 ;; # $user, YMMV: if you don't want to fiddle with them define a couple
279 ;; # of new variables and use these instead.
280 ;; # NOTICE that there is a space between "AnSiT?" and $whatever NOTICE
282 ;; # These are because we want the real cwd in the messages, not the login
285 ;; set cwd_hack='$cwd'
286 ;; set host_hack='$host'
287 ;; set user_hack='$user'
289 ;; # Notice that the ^[ character is an ESC, not two chars. You can
290 ;; # get it in various ways, for example by typing
291 ;; # echo -e '\033' > escape.file
292 ;; # or by using your favorite editor
294 ;; foreach temp (cd pushd)
295 ;; alias $temp "$temp \!* ; echo '\eAnSiTc' $cwd_hack"
297 ;; alias popd 'popd ;echo "\eAnSiTc" $cwd'
299 ;; # Every command that can modify the user/host/directory should be aliased
300 ;; # as follows for the tracking mechanism to work.
302 ;; foreach temp ( rlogin telnet rsh sh ksh csh tcsh zsh bash tcl su )
303 ;; alias $temp "$temp \!* ; echo '\eAnSiTh' $host_hack ; \
304 ;; echo '\eAnSiTu' $user_hack ;echo '\eAnSiTc' $cwd_hack"
307 ;; # Start up & use color ls
309 ;; echo "\eAnSiTh" $host
310 ;; echo "\eAnSiTu" $user
311 ;; echo "\eAnSiTc" $cwd
313 ;; # some housekeeping
320 ;; eval `/bin/dircolors /home/marco/.emacs_dircolors`
325 ;; # Let's not clutter user space
332 ;;; Original Commentary:
333 ;; ---------------------
335 ;; The changelog is at the end of this file.
337 ;; Please send me bug reports, bug fixes, and extensions, so that I can
338 ;; merge them into the master source.
339 ;; - Per Bothner (bothner@cygnus.com)
341 ;; This file defines a general command-interpreter-in-a-buffer package
342 ;; (term mode). The idea is that you can build specific process-in-a-buffer
343 ;; modes on top of term mode -- e.g., lisp, shell, scheme, T, soar, ....
344 ;; This way, all these specific packages share a common base functionality,
345 ;; and a common set of bindings, which makes them easier to use (and
346 ;; saves code, implementation time, etc., etc.).
348 ;; For hints on converting existing process modes (e.g., tex-mode,
349 ;; background, dbx, gdb, kermit, prolog, telnet) to use term-mode
350 ;; instead of shell-mode, see the notes at the end of this file.
353 ;; Brief Command Documentation:
354 ;;============================================================================
355 ;; Term Mode Commands: (common to all derived modes, like cmushell & cmulisp
358 ;; m-p term-previous-input Cycle backwards in input history
359 ;; m-n term-next-input Cycle forwards
360 ;; m-r term-previous-matching-input Previous input matching a regexp
361 ;; m-s comint-next-matching-input Next input that matches
362 ;; return term-send-input
363 ;; c-c c-a term-bol Beginning of line; skip prompt.
364 ;; c-d term-delchar-or-maybe-eof Delete char unless at end of buff.
365 ;; c-c c-u term-kill-input ^u
366 ;; c-c c-w backward-kill-word ^w
367 ;; c-c c-c term-interrupt-subjob ^c
368 ;; c-c c-z term-stop-subjob ^z
369 ;; c-c c-\ term-quit-subjob ^\
370 ;; c-c c-o term-kill-output Delete last batch of process output
371 ;; c-c c-r term-show-output Show last batch of process output
372 ;; c-c c-h term-dynamic-list-input-ring List input history
374 ;; Not bound by default in term-mode
375 ;; term-send-invisible Read a line w/o echo, and send to proc
376 ;; (These are bound in shell-mode)
377 ;; term-dynamic-complete Complete filename at point.
378 ;; term-dynamic-list-completions List completions in help buffer.
379 ;; term-replace-by-expanded-filename Expand and complete filename at point;
380 ;; replace with expanded/completed name.
381 ;; term-kill-subjob No mercy.
382 ;; term-show-maximum-output Show as much output as possible.
383 ;; term-continue-subjob Send CONT signal to buffer's process
384 ;; group. Useful if you accidentally
385 ;; suspend your process (with C-c C-z).
387 ;; term-mode-hook is the term mode hook. Basically for your keybindings.
388 ;; term-load-hook is run after loading in this package.
392 ;; This is passed to the inferior in the EMACS environment variable,
393 ;; so it is important to increase it if there are protocol-relevant changes.
394 (defconst term-protocol-version
"0.96")
396 (eval-when-compile (require 'ange-ftp
))
400 (declare-function ring-empty-p
"ring" (ring))
401 (declare-function ring-ref
"ring" (ring index
))
402 (declare-function ring-insert-at-beginning
"ring" (ring item
))
403 (declare-function ring-length
"ring" (ring))
404 (declare-function ring-insert
"ring" (ring item
))
407 "General command interpreter in a window."
411 ;;; Buffer Local Variables:
412 ;;============================================================================
413 ;; Term mode buffer local variables:
414 ;; term-prompt-regexp - string term-bol uses to match prompt.
415 ;; term-delimiter-argument-list - list For delimiters and arguments
416 ;; term-last-input-start - marker Handy if inferior always echoes
417 ;; term-last-input-end - marker For term-kill-output command
418 ;; For the input history mechanism:
419 (defvar term-input-ring-size
32 "Size of input history ring.")
420 ;; term-input-ring-size - integer
421 ;; term-input-ring - ring
422 ;; term-input-ring-index - number ...
423 ;; term-input-autoexpand - symbol ...
424 ;; term-input-ignoredups - boolean ...
425 ;; term-last-input-match - string ...
426 ;; term-dynamic-complete-functions - hook For the completion mechanism
427 ;; term-completion-fignore - list ...
428 ;; term-get-old-input - function Hooks for specific
429 ;; term-input-filter-functions - hook process-in-a-buffer
430 ;; term-input-filter - function modes.
431 ;; term-input-send - function
432 ;; term-scroll-to-bottom-on-output - symbol ...
433 ;; term-scroll-show-maximum-output - boolean...
434 (defvar term-height
) ; Number of lines in window.
435 (defvar term-width
) ; Number of columns in window.
436 (defvar term-home-marker
) ; Marks the "home" position for cursor addressing.
437 (defvar term-saved-home-marker nil
438 "When using alternate sub-buffer,
439 contains saved term-home-marker from original sub-buffer.")
440 (defvar term-start-line-column
0
441 "(current-column) at start of screen line, or nil if unknown.")
442 (defvar term-current-column
0 "If non-nil, is cache for (current-column).")
443 (defvar term-current-row
0
444 "Current vertical row (relative to home-marker) or nil if unknown.")
445 (defvar term-insert-mode nil
)
446 (defvar term-vertical-motion
)
447 (defvar term-terminal-state
0
448 "State of the terminal emulator:
449 state 0: Normal state
450 state 1: Last character was a graphic in the last column.
451 If next char is graphic, first move one column right
452 \(and line warp) before displaying it.
453 This emulates (more or less) the behavior of xterm.
455 state 3: seen ESC [ (or ESC [ ?)
456 state 4: term-terminal-parameter contains pending output.")
457 (defvar term-kill-echo-list nil
458 "A queue of strings whose echo we want suppressed.")
459 (defvar term-terminal-parameter
)
460 (defvar term-terminal-previous-parameter
)
461 (defvar term-current-face
'term
)
462 (defvar term-scroll-start
0 "Top-most line (inclusive) of scrolling region.")
463 (defvar term-scroll-end
) ; Number of line (zero-based) after scrolling region.
464 (defvar term-pager-count nil
465 "Number of lines before we need to page; if nil, paging is disabled.")
466 (defvar term-saved-cursor nil
)
467 (defvar term-command-hook
)
468 (defvar term-log-buffer nil
)
469 (defvar term-scroll-with-delete nil
470 "If t, forward scrolling should be implemented by delete to
471 top-most line(s); and if nil, scrolling should be implemented
472 by moving term-home-marker. It is set to t if there is a
473 \(non-default) scroll-region OR the alternate buffer is used.")
474 (defvar term-pending-delete-marker
) ; New user input in line mode
475 ; needs to be deleted, because it gets echoed by the inferior.
476 ; To reduce flicker, we defer the delete until the next output.
477 (defvar term-old-mode-map nil
"Saves the old keymap when in char mode.")
478 (defvar term-old-mode-line-format
) ; Saves old mode-line-format while paging.
479 (defvar term-pager-old-local-map nil
"Saves old keymap while paging.")
480 (defvar term-pager-old-filter
) ; Saved process-filter while paging.
482 (defcustom explicit-shell-file-name nil
483 "If non-nil, is file name to use for explicitly requested inferior shell."
484 :type
'(choice (const nil
) file
)
487 (defvar term-prompt-regexp
"^"
488 "Regexp to recognize prompts in the inferior process.
489 Defaults to \"^\", the null string at BOL.
492 Canonical Lisp: \"^[^> \\n]*>+:? *\" (Lucid, franz, kcl, T, cscheme, oaklisp)
493 Lucid Common Lisp: \"^\\\\(>\\\\|\\\\(->\\\\)+\\\\) *\"
494 franz: \"^\\\\(->\\\\|<[0-9]*>:\\\\) *\"
496 shell: \"^[^#$%>\\n]*[#$%>] *\"
499 This is a good thing to set in mode hooks.")
501 (defvar term-delimiter-argument-list
()
502 "List of characters to recognize as separate arguments in input.
503 Strings comprising a character in this list will separate the arguments
504 surrounding them, and also be regarded as arguments in their own right
505 \(unlike whitespace). See `term-arguments'.
506 Defaults to the empty list.
508 For shells, a good value is (?\\| ?& ?< ?> ?\\( ?\\) ?\\;).
510 This is a good thing to set in mode hooks.")
512 (defcustom term-input-autoexpand nil
513 "If non-nil, expand input command history references on completion.
514 This mirrors the optional behavior of tcsh (its autoexpand and histlit).
516 If the value is `input', then the expansion is seen on input.
517 If the value is `history', then the expansion is only when inserting
518 into the buffer's input ring. See also `term-magic-space' and
519 `term-dynamic-complete'.
521 This variable is buffer-local."
522 :type
'(choice (const nil
) (const t
) (const input
) (const history
))
525 (defcustom term-input-ignoredups nil
526 "If non-nil, don't add input matching the last on the input ring.
527 This mirrors the optional behavior of bash.
529 This variable is buffer-local."
533 (defcustom term-input-ring-file-name nil
534 "If non-nil, name of the file to read/write input history.
535 See also `term-read-input-ring' and `term-write-input-ring'.
537 This variable is buffer-local, and is a good thing to set in mode hooks."
541 (defcustom term-scroll-to-bottom-on-output nil
542 "Controls whether interpreter output causes window to scroll.
543 If nil, then do not scroll. If t or `all', scroll all windows showing buffer.
544 If `this', scroll only the selected window.
545 If `others', scroll only those that are not the selected window.
549 See variable `term-scroll-show-maximum-output'.
550 This variable is buffer-local."
554 (defcustom term-scroll-show-maximum-output nil
555 "Controls how interpreter output causes window to scroll.
556 If non-nil, then show the maximum output when the window is scrolled.
558 See variable `term-scroll-to-bottom-on-output'.
559 This variable is buffer-local."
563 (defcustom term-suppress-hard-newline nil
564 "Non-nil means interpreter should not break long lines with newlines.
565 This means text can automatically reflow if the window is resized."
570 ;; Where gud-display-frame should put the debugging arrow. This is
571 ;; set by the marker-filter, which scans the debugger's output for
572 ;; indications of the current pc.
573 (defvar term-pending-frame nil
)
575 ;;; Here are the per-interpreter hooks.
576 (defvar term-get-old-input
(function term-get-old-input-default
)
577 "Function that submits old text in term mode.
578 This function is called when return is typed while the point is in old text.
579 It returns the text to be submitted as process input. The default is
580 `term-get-old-input-default', which grabs the current line, and strips off
581 leading text matching `term-prompt-regexp'.")
583 (defvar term-dynamic-complete-functions
584 '(term-replace-by-expanded-history term-dynamic-complete-filename
)
585 "List of functions called to perform completion.
586 Functions should return non-nil if completion was performed.
587 See also `term-dynamic-complete'.
589 This is a good thing to set in mode hooks.")
591 (defvar term-input-filter
592 (function (lambda (str) (not (string-match "\\`\\s *\\'" str
))))
593 "Predicate for filtering additions to input history.
594 Only inputs answering true to this function are saved on the input
595 history list. Default is to save anything that isn't all whitespace.")
597 (defvar term-input-filter-functions
'()
598 "Functions to call before input is sent to the process.
599 These functions get one argument, a string containing the text to send.
601 This variable is buffer-local.")
603 (defvar term-input-sender
(function term-simple-send
)
604 "Function to actually send to PROCESS the STRING submitted by user.
605 Usually this is just `term-simple-send', but if your mode needs to
606 massage the input string, this is your hook. This is called from
607 the user command `term-send-input'. `term-simple-send' just sends
608 the string plus a newline.")
610 (defcustom term-eol-on-send t
611 "Non-nil means go to the end of the line before sending input.
612 See `term-send-input'."
616 (defcustom term-mode-hook
'()
617 "Called upon entry into term mode.
618 This is run before the process is cranked up."
622 (defcustom term-exec-hook
'()
623 "Called each time a process is exec'd by `term-exec'.
624 This is called after the process is cranked up. It is useful for things that
625 must be done each time a process is executed in a term mode buffer (e.g.,
626 `process-kill-without-query'). In contrast, `term-mode-hook' is only
627 executed once when the buffer is created."
631 (defvar term-mode-map
632 (let ((map (make-sparse-keymap)))
633 (define-key map
"\ep" 'term-previous-input
)
634 (define-key map
"\en" 'term-next-input
)
635 (define-key map
"\er" 'term-previous-matching-input
)
636 (define-key map
"\es" 'term-next-matching-input
)
637 (unless (featurep 'xemacs
)
638 (define-key map
[?\A-\M-r
]
639 'term-previous-matching-input-from-input
)
640 (define-key map
[?\A-\M-s
] 'term-next-matching-input-from-input
))
641 (define-key map
"\e\C-l" 'term-show-output
)
642 (define-key map
"\C-m" 'term-send-input
)
643 (define-key map
"\C-d" 'term-delchar-or-maybe-eof
)
644 (define-key map
"\C-c\C-a" 'term-bol
)
645 (define-key map
"\C-c\C-u" 'term-kill-input
)
646 (define-key map
"\C-c\C-w" 'backward-kill-word
)
647 (define-key map
"\C-c\C-c" 'term-interrupt-subjob
)
648 (define-key map
"\C-c\C-z" 'term-stop-subjob
)
649 (define-key map
"\C-c\C-\\" 'term-quit-subjob
)
650 (define-key map
"\C-c\C-m" 'term-copy-old-input
)
651 (define-key map
"\C-c\C-o" 'term-kill-output
)
652 (define-key map
"\C-c\C-r" 'term-show-output
)
653 (define-key map
"\C-c\C-e" 'term-show-maximum-output
)
654 (define-key map
"\C-c\C-l" 'term-dynamic-list-input-ring
)
655 (define-key map
"\C-c\C-n" 'term-next-prompt
)
656 (define-key map
"\C-c\C-p" 'term-previous-prompt
)
657 (define-key map
"\C-c\C-d" 'term-send-eof
)
658 (define-key map
"\C-c\C-k" 'term-char-mode
)
659 (define-key map
"\C-c\C-j" 'term-line-mode
)
660 (define-key map
"\C-c\C-q" 'term-pager-toggle
)
661 ;; completion: (line mode only)
662 (easy-menu-define nil map
"Complete menu for Term mode."
664 ["Complete Before Point" term-dynamic-complete t
]
665 ["Complete File Name" term-dynamic-complete-filename t
]
666 ["File Completion Listing" term-dynamic-list-filename-completions t
]
667 ["Expand File Name" term-replace-by-expanded-filename t
]))
668 ;; Input history: (line mode only)
669 (easy-menu-define nil map
"In/Out menu for Term mode."
671 ["Expand History Before Point" term-replace-by-expanded-history
672 term-input-autoexpand
]
673 ["List Input History" term-dynamic-list-input-ring t
]
674 ["Previous Input" term-previous-input t
]
675 ["Next Input" term-next-input t
]
676 ["Previous Matching Current Input"
677 term-previous-matching-input-from-input t
]
678 ["Next Matching Current Input" term-next-matching-input-from-input t
]
679 ["Previous Matching Input..." term-previous-matching-input t
]
680 ["Next Matching Input..." term-next-matching-input t
]
681 ["Backward Matching Input..." term-backward-matching-input t
]
682 ["Forward Matching Input..." term-forward-matching-input t
]
683 ["Copy Old Input" term-copy-old-input t
]
684 ["Kill Current Input" term-kill-input t
]
685 ["Show Current Output Group" term-show-output t
]
686 ["Show Maximum Output" term-show-maximum-output t
]
687 ["Backward Output Group" term-previous-prompt t
]
688 ["Forward Output Group" term-next-prompt t
]
689 ["Kill Current Output Group" term-kill-output t
]))
691 "Keymap for Term mode.")
693 (defvar term-escape-char nil
694 "Escape character for char sub-mode of term mode.
695 Do not change it directly; use `term-set-escape-char' instead.")
697 (defvar term-pager-break-map
698 (let ((map (make-keymap)))
700 ;; (define-key map (make-string 1 i) 'term-send-raw))
701 (define-key map
"\e" (lookup-key (current-global-map) "\e"))
702 (define-key map
"\C-x" (lookup-key (current-global-map) "\C-x"))
703 (define-key map
"\C-u" (lookup-key (current-global-map) "\C-u"))
704 (define-key map
" " 'term-pager-page
)
705 (define-key map
"\r" 'term-pager-line
)
706 (define-key map
"?" 'term-pager-help
)
707 (define-key map
"h" 'term-pager-help
)
708 (define-key map
"b" 'term-pager-back-page
)
709 (define-key map
"\177" 'term-pager-back-line
)
710 (define-key map
"q" 'term-pager-discard
)
711 (define-key map
"D" 'term-pager-disable
)
712 (define-key map
"<" 'term-pager-bob
)
713 (define-key map
">" 'term-pager-eob
)
715 "Keymap used in Term pager mode.")
718 "True if communications via pty; false if by pipe. Buffer local.
719 This is to work around a bug in Emacs process signaling.")
721 (defvar term-last-input-match
""
722 "Last string searched for by term input history search, for defaulting.
723 Buffer local variable.")
725 (defvar term-input-ring nil
)
726 (defvar term-last-input-start
)
727 (defvar term-last-input-end
)
728 (defvar term-input-ring-index nil
729 "Index of last matched history element.")
730 (defvar term-matching-input-from-input-string
""
731 "Input previously used to match input history.")
732 ; This argument to set-process-filter disables reading from the process,
733 ; assuming this is Emacs 19.20 or newer.
734 (defvar term-pager-filter t
)
736 (put 'term-input-ring
'permanent-local t
)
737 (put 'term-input-ring-index
'permanent-local t
)
738 (put 'term-input-autoexpand
'permanent-local t
)
739 (put 'term-input-filter-functions
'permanent-local t
)
740 (put 'term-scroll-to-bottom-on-output
'permanent-local t
)
741 (put 'term-scroll-show-maximum-output
'permanent-local t
)
742 (put 'term-ptyp
'permanent-local t
)
744 (defmacro term-in-char-mode
() '(eq (current-local-map) term-raw-map
))
745 (defmacro term-in-line-mode
() '(not (term-in-char-mode)))
746 ;; True if currently doing PAGER handling.
747 (defmacro term-pager-enabled
() 'term-pager-count
)
748 (defmacro term-handling-pager
() 'term-pager-old-local-map
)
749 (defmacro term-using-alternate-sub-buffer
() 'term-saved-home-marker
)
751 ;; Let's silence the byte-compiler -mm
752 (defvar term-ansi-at-host nil
)
753 (defvar term-ansi-at-dir nil
)
754 (defvar term-ansi-at-user nil
)
755 (defvar term-ansi-at-message nil
)
756 (defvar term-ansi-at-save-user nil
)
757 (defvar term-ansi-at-save-pwd nil
)
758 (defvar term-ansi-at-save-anon nil
)
759 (defvar term-ansi-current-bold nil
)
760 (defvar term-ansi-current-color
0)
761 (defvar term-ansi-face-already-done nil
)
762 (defvar term-ansi-current-bg-color
0)
763 (defvar term-ansi-current-underline nil
)
764 (defvar term-ansi-current-reverse nil
)
765 (defvar term-ansi-current-invisible nil
)
767 ;; Four should be enough, if you want more, just add. -mm
768 (defvar term-terminal-more-parameters
0)
769 (defvar term-terminal-previous-parameter-2 -
1)
770 (defvar term-terminal-previous-parameter-3 -
1)
771 (defvar term-terminal-previous-parameter-4 -
1)
774 (defvar ansi-term-color-vector
785 (defcustom term-default-fg-color nil
786 "If non-nil, default color for foreground in Term mode."
788 :type
'(choice (const nil
) (string :tag
"color")))
789 (make-obsolete-variable 'term-default-fg-color
"use the face `term' instead."
792 (defcustom term-default-bg-color nil
793 "If non-nil, default color for foreground in Term mode."
795 :type
'(choice (const nil
) (string :tag
"color")))
796 (make-obsolete-variable 'term-default-bg-color
"use the face `term' instead."
801 :foreground
,term-default-fg-color
802 :background
,term-default-bg-color
804 "Default face to use in Term mode."
809 "Default face to use for bold text."
812 (defface term-underline
814 "Default face to use for underlined text."
817 (defface term-color-black
818 '((t :foreground
"black" :background
"black"))
819 "Face used to render black color code."
822 (defface term-color-red
823 '((t :foreground
"red3" :background
"red3"))
824 "Face used to render red color code."
827 (defface term-color-green
828 '((t :foreground
"green3" :background
"green3"))
829 "Face used to render green color code."
832 (defface term-color-yellow
833 '((t :foreground
"yellow3" :background
"yellow3"))
834 "Face used to render yellow color code."
837 (defface term-color-blue
838 '((t :foreground
"blue2" :background
"blue2"))
839 "Face used to render blue color code."
842 (defface term-color-magenta
843 '((t :foreground
"magenta3" :background
"magenta3"))
844 "Face used to render magenta color code."
847 (defface term-color-cyan
848 '((t :foreground
"cyan3" :background
"cyan3"))
849 "Face used to render cyan color code."
852 (defface term-color-white
853 '((t :foreground
"white" :background
"white"))
854 "Face used to render white color code."
857 ;; Inspiration came from comint.el -mm
858 (defcustom term-buffer-maximum-size
2048
859 "The maximum size in lines for term buffers.
860 Term buffers are truncated from the top to be no greater than this number.
861 Notice that a setting of 0 means \"don't truncate anything\". This variable
866 ;; Set up term-raw-map, etc.
869 (let* ((map (make-keymap))
870 (esc-map (make-keymap))
873 (define-key map
(make-string 1 i
) 'term-send-raw
)
874 ;; Avoid O and [. They are used in escape sequences for various keys.
875 (unless (or (eq i ?O
) (eq i
91))
876 (define-key esc-map
(make-string 1 i
) 'term-send-raw-meta
))
878 (define-key map
[remap self-insert-command
] 'term-send-raw
)
879 (define-key map
"\e" esc-map
)
881 ;; Added nearly all the 'gray keys' -mm
883 (if (featurep 'xemacs
)
884 (define-key map
[button2] 'term-mouse-paste)
885 (define-key map [mouse-2] 'term-mouse-paste))
886 (define-key map [up] 'term-send-up)
887 (define-key map [down] 'term-send-down)
888 (define-key map [right] 'term-send-right)
889 (define-key map [left] 'term-send-left)
890 (define-key map [delete] 'term-send-del)
891 (define-key map [deletechar] 'term-send-del)
892 (define-key map [backspace] 'term-send-backspace)
893 (define-key map [home] 'term-send-home)
894 (define-key map [end] 'term-send-end)
895 (define-key map [insert] 'term-send-insert)
896 (define-key map [S-prior] 'scroll-down)
897 (define-key map [S-next] 'scroll-up)
898 (define-key map [S-insert] 'term-paste)
899 (define-key map [prior] 'term-send-prior)
900 (define-key map [next] 'term-send-next)
902 "Keyboard map for sending characters directly to the inferior process.")
904 (easy-menu-define term-terminal-menu
905 (list term-mode-map term-raw-map term-pager-break-map)
906 "Terminal menu for Term mode."
908 ["Line mode" term-line-mode :active (term-in-char-mode)
909 :help "Switch to line (cooked) sub-mode of term mode"]
910 ["Character mode" term-char-mode :active (term-in-line-mode)
911 :help "Switch to char (raw) sub-mode of term mode"]
912 ["Paging" term-pager-toggle :style toggle :selected term-pager-count
913 :help "Toggle paging feature"]))
915 (easy-menu-define term-signals-menu
916 (list term-mode-map term-raw-map term-pager-break-map)
917 "Signals menu for Term mode."
919 ["BREAK" term-interrupt-subjob :active t
920 :help "Interrupt the current subjob"]
921 ["STOP" term-stop-subjob :active t :help "Stop the current subjob"]
922 ["CONT" term-continue-subjob :active t
923 :help "Send CONT signal to process buffer's process group"]
924 ["QUIT" term-quit-subjob :active t
925 :help "Send quit signal to the current subjob"]
926 ["KILL" term-kill-subjob :active t
927 :help "Send kill signal to the current subjob"]
928 ["EOF" term-send-eof :active t
929 :help "Send an EOF to the current buffer's process"]))
931 (easy-menu-define term-pager-menu term-pager-break-map
932 "Menu for Term pager mode."
934 ["1 page forwards" term-pager-page t]
935 ["1 page backwards" term-pager-back-page t]
936 ["1 line backwards" term-pager-back-line t]
937 ["1 line forwards" term-pager-line t]
938 ["Goto to beginning" term-pager-bob t]
939 ["Goto to end" term-pager-eob t]
940 ["Discard remaining output" term-pager-discard t]
941 ["Disable paging" term-pager-toggle t]
942 ["Help" term-pager-help t]))
944 (defvar term-raw-escape-map
945 (let ((map (make-sparse-keymap)))
946 (set-keymap-parent map 'Control-X-prefix)
947 ;; Define standard bindings in term-raw-escape-map.
948 (define-key map "\C-v" (lookup-key (current-global-map) "\C-v"))
949 (define-key map "\C-u" (lookup-key (current-global-map) "\C-u"))
950 (define-key map "\C-q" 'term-pager-toggle)
951 ;; The keybinding for term-char-mode is needed by the menubar code.
952 (define-key map "\C-k" 'term-char-mode)
953 (define-key map "\C-j" 'term-line-mode)
954 ;; It's convenient to have execute-extended-command here.
955 (define-key map [?\M-x] 'execute-extended-command)
958 (defun term-set-escape-char (key)
959 "Change `term-escape-char' and keymaps that depend on it."
960 (when term-escape-char
961 ;; Undo previous term-set-escape-char.
962 (define-key term-raw-map term-escape-char 'term-send-raw))
963 (setq term-escape-char (if (vectorp key) key (vector key)))
964 (define-key term-raw-map term-escape-char term-raw-escape-map)
965 ;; FIXME: If we later call term-set-escape-char again with another key,
966 ;; we should undo this binding.
967 (define-key term-raw-escape-map term-escape-char 'term-send-raw))
969 (term-set-escape-char (or term-escape-char ?\C-c))
971 (defvar overflow-newline-into-fringe)
973 (defun term-window-width ()
974 (if (and (not (featurep 'xemacs))
976 overflow-newline-into-fringe
977 (/= (frame-parameter nil 'right-fringe) 0))
979 (1- (window-body-width))))
982 (put 'term-mode 'mode-class 'special)
985 ;; Use this variable as a display table for `term-mode'.
986 (defvar term-display-table
987 (let ((dt (or (copy-sequence standard-display-table)
988 (make-display-table)))
990 ;; avoid changing the display table for ^J
993 (aset dt i (vector i))
997 (aset dt i (vector i))
1001 (aset dt i (vector i))
1005 (defun term-ansi-reset ()
1006 (setq term-current-face 'term)
1007 (setq term-ansi-current-underline nil)
1008 (setq term-ansi-current-bold nil)
1009 (setq term-ansi-current-reverse nil)
1010 (setq term-ansi-current-color 0)
1011 (setq term-ansi-current-invisible nil)
1012 ;; Stefan thought this should be t, but could not remember why.
1013 ;; Setting it to t seems to cause bug#11785. Setting it to nil
1014 ;; again to see if there are other consequences...
1015 (setq term-ansi-face-already-done nil)
1016 (setq term-ansi-current-bg-color 0))
1018 (define-derived-mode term-mode fundamental-mode "Term"
1019 "Major mode for interacting with an inferior interpreter.
1020 The interpreter name is same as buffer name, sans the asterisks.
1022 There are two submodes: line mode and char mode. By default, you are
1023 in char mode. In char sub-mode, each character (except
1024 `term-escape-char') is sent immediately to the subprocess.
1025 The escape character is equivalent to the usual meaning of C-x.
1027 In line mode, you send a line of input at a time; use
1028 \\[term-send-input] to send.
1030 In line mode, this maintains an input history of size
1031 `term-input-ring-size', and you can access it with the commands
1032 \\[term-next-input], \\[term-previous-input], and
1033 \\[term-dynamic-list-input-ring]. Input ring history expansion can be
1034 achieved with the commands \\[term-replace-by-expanded-history] or
1035 \\[term-magic-space]. Input ring expansion is controlled by the
1036 variable `term-input-autoexpand', and addition is controlled by the
1037 variable `term-input-ignoredups'.
1039 Input to, and output from, the subprocess can cause the window to scroll to
1040 the end of the buffer. See variables `term-scroll-to-bottom-on-input',
1041 and `term-scroll-to-bottom-on-output'.
1043 If you accidentally suspend your process, use \\[term-continue-subjob]
1046 This mode can be customized to create specific modes for running
1047 particular subprocesses. This can be done by setting the hooks
1048 `term-input-filter-functions', `term-input-filter',
1049 `term-input-sender' and `term-get-old-input' to appropriate functions,
1050 and the variable `term-prompt-regexp' to the appropriate regular
1053 Commands in raw mode:
1057 Commands in line mode:
1061 Entry to this mode runs the hooks on `term-mode-hook'."
1062 ;; we do not want indent to sneak in any tabs
1063 (setq indent-tabs-mode nil)
1064 (setq buffer-display-table term-display-table)
1065 (set (make-local-variable 'term-home-marker) (copy-marker 0))
1066 (set (make-local-variable 'term-height) (1- (window-height)))
1067 (set (make-local-variable 'term-width) (term-window-width))
1068 (set (make-local-variable 'term-last-input-start) (make-marker))
1069 (set (make-local-variable 'term-last-input-end) (make-marker))
1070 (set (make-local-variable 'term-last-input-match) "")
1071 (set (make-local-variable 'term-command-hook)
1072 (symbol-function 'term-command-hook))
1074 ;; These local variables are set to their local values:
1075 (make-local-variable 'term-saved-home-marker)
1076 (make-local-variable 'term-terminal-parameter)
1077 (make-local-variable 'term-saved-cursor)
1078 (make-local-variable 'term-prompt-regexp)
1079 (make-local-variable 'term-input-ring-size)
1080 (make-local-variable 'term-input-ring)
1081 (make-local-variable 'term-input-ring-file-name)
1082 (make-local-variable 'term-input-ring-index)
1083 (unless term-input-ring
1084 (setq term-input-ring (make-ring term-input-ring-size)))
1086 ;; I'm not sure these saves are necessary but, since I
1087 ;; haven't tested the whole thing on a net connected machine with
1088 ;; a properly configured ange-ftp, I've decided to be conservative
1089 ;; and put them in. -mm
1091 (set (make-local-variable 'term-ansi-at-host) (system-name))
1092 (set (make-local-variable 'term-ansi-at-dir) default-directory)
1093 (set (make-local-variable 'term-ansi-at-message) nil)
1095 ;; For user tracking purposes -mm
1096 (make-local-variable 'ange-ftp-default-user)
1097 (make-local-variable 'ange-ftp-default-password)
1098 (make-local-variable 'ange-ftp-generate-anonymous-password)
1100 ;; You may want to have different scroll-back sizes -mm
1101 (make-local-variable 'term-buffer-maximum-size)
1103 ;; Of course these have to be buffer-local -mm
1104 (make-local-variable 'term-ansi-current-bold)
1105 (make-local-variable 'term-ansi-current-color)
1106 (make-local-variable 'term-ansi-face-already-done)
1107 (make-local-variable 'term-ansi-current-bg-color)
1108 (make-local-variable 'term-ansi-current-underline)
1109 (make-local-variable 'term-ansi-current-reverse)
1110 (make-local-variable 'term-ansi-current-invisible)
1112 (make-local-variable 'term-terminal-parameter)
1113 (make-local-variable 'term-terminal-previous-parameter)
1114 (make-local-variable 'term-terminal-previous-parameter-2)
1115 (make-local-variable 'term-terminal-previous-parameter-3)
1116 (make-local-variable 'term-terminal-previous-parameter-4)
1117 (make-local-variable 'term-terminal-more-parameters)
1119 (make-local-variable 'term-terminal-state)
1120 (make-local-variable 'term-kill-echo-list)
1121 (make-local-variable 'term-start-line-column)
1122 (make-local-variable 'term-current-column)
1123 (make-local-variable 'term-current-row)
1124 (make-local-variable 'term-log-buffer)
1125 (make-local-variable 'term-scroll-start)
1126 (set (make-local-variable 'term-scroll-end) term-height)
1127 (make-local-variable 'term-scroll-with-delete)
1128 (make-local-variable 'term-pager-count)
1129 (make-local-variable 'term-pager-old-local-map)
1130 (make-local-variable 'term-old-mode-map)
1131 (make-local-variable 'term-insert-mode)
1132 (make-local-variable 'term-dynamic-complete-functions)
1133 (make-local-variable 'term-completion-fignore)
1134 (make-local-variable 'term-get-old-input)
1135 (make-local-variable 'term-matching-input-from-input-string)
1136 (make-local-variable 'term-input-autoexpand)
1137 (make-local-variable 'term-input-ignoredups)
1138 (make-local-variable 'term-delimiter-argument-list)
1139 (make-local-variable 'term-input-filter-functions)
1140 (make-local-variable 'term-input-filter)
1141 (make-local-variable 'term-input-sender)
1142 (make-local-variable 'term-eol-on-send)
1143 (make-local-variable 'term-scroll-to-bottom-on-output)
1144 (make-local-variable 'term-scroll-show-maximum-output)
1145 (make-local-variable 'term-ptyp)
1146 (make-local-variable 'term-exec-hook)
1147 (make-local-variable 'term-vertical-motion)
1148 (set (make-local-variable 'term-pending-delete-marker) (make-marker))
1149 (make-local-variable 'term-current-face)
1151 (set (make-local-variable 'term-pending-frame) nil)
1152 ;; Cua-mode's keybindings interfere with the term keybindings, disable it.
1153 (set (make-local-variable 'cua-mode) nil)
1155 (set (make-local-variable 'font-lock-defaults) '(nil t))
1157 (easy-menu-add term-terminal-menu)
1158 (easy-menu-add term-signals-menu)
1160 (setq term-input-ring (make-ring term-input-ring-size)))
1161 (term-update-mode-line))
1163 (defun term-reset-size (height width)
1164 (setq term-height height)
1165 (setq term-width width)
1166 (setq term-start-line-column nil)
1167 (setq term-current-row nil)
1168 (setq term-current-column nil)
1169 (term-set-scroll-region 0 height))
1171 ;; Recursive routine used to check if any string in term-kill-echo-list
1172 ;; matches part of the buffer before point.
1173 ;; If so, delete that matched part of the buffer - this suppresses echo.
1174 ;; Also, remove that string from the term-kill-echo-list.
1175 ;; We *also* remove any older string on the list, as a sanity measure,
1176 ;; in case something gets out of sync. (Except for type-ahead, there
1177 ;; should only be one element in the list.)
1179 (defun term-check-kill-echo-list ()
1180 (let ((cur term-kill-echo-list) (found nil) (save-point (point)))
1185 (let* ((str (car cur)) (len (length str)) (start (- (point) len)))
1186 (if (and (>= start (point-min))
1187 (string= str (buffer-substring start (point))))
1188 (progn (delete-char (- len))
1189 (setq term-kill-echo-list (cdr cur))
1190 (setq term-current-column nil)
1191 (setq term-current-row nil)
1192 (setq term-start-line-column nil)
1193 (setq cur nil found t))
1194 (setq cur (cdr cur))))))
1196 (goto-char save-point)))
1199 (defun term-check-size (process)
1200 (when (or (/= term-height (window-text-height))
1201 (/= term-width (term-window-width)))
1202 (term-reset-size (window-text-height) (term-window-width))
1203 (set-process-window-size process term-height term-width)))
1205 (defun term-send-raw-string (chars)
1207 (let ((proc (get-buffer-process (current-buffer))))
1209 (error "Current buffer has no process")
1210 ;; Note that (term-current-row) must be called *after*
1211 ;; (point) has been updated to (process-mark proc).
1212 (goto-char (process-mark proc))
1213 (when (term-pager-enabled)
1214 (setq term-pager-count (term-current-row)))
1215 (process-send-string proc chars))))
1217 (defun term-send-raw ()
1218 "Send the last character typed through the terminal-emulator
1219 without any interpretation."
1221 (let ((keys (this-command-keys)))
1222 (term-send-raw-string (string (aref keys (1- (length keys)))))))
1224 (defun term-send-raw-meta ()
1226 (let ((char last-input-event))
1227 (when (symbolp char)
1228 ;; Convert `return' to C-m, etc.
1229 (let ((tmp (get char 'event-symbol-elements)))
1230 (if tmp (setq char (car tmp)))
1232 (setq tmp (get char 'ascii-character))
1234 (when (numberp char)
1235 (let ((base (event-basic-type char))
1236 (mods (delq 'meta (event-modifiers char))))
1237 (if (memq 'control mods)
1238 (setq mods (delq 'shift mods)))
1239 (term-send-raw-string
1241 (event-convert-list (append mods (list base)))))))))
1243 (defun term-mouse-paste (click)
1244 "Insert the primary selection at the position clicked on."
1246 (if (featurep 'xemacs)
1247 (term-send-raw-string
1248 (or (condition-case () (x-get-selection) (error ()))
1249 (error "No selection available")))
1250 ;; Give temporary modes such as isearch a chance to turn off.
1251 (run-hooks 'mouse-leave-buffer-hook)
1252 (setq this-command 'yank)
1253 (mouse-set-point click)
1254 (term-send-raw-string
1255 ;; From `mouse-yank-primary':
1256 (or (if (fboundp 'x-get-selection-value)
1257 (if (eq system-type 'windows-nt)
1258 (or (x-get-selection 'PRIMARY)
1259 (x-get-selection-value))
1260 (or (x-get-selection-value)
1261 (x-get-selection 'PRIMARY)))
1262 (x-get-selection 'PRIMARY))
1263 (error "No selection is available")))))
1265 (defun term-paste ()
1266 "Insert the last stretch of killed text at point."
1268 (term-send-raw-string (current-kill 0)))
1270 ;; Which would be better: "\e[A" or "\eOA"? readline accepts either.
1271 ;; For my configuration it's definitely better \eOA but YMMV. -mm
1272 ;; For example: vi works with \eOA while elm wants \e[A ...
1273 ;; (terminfo: kcuu1, kcud1, kcuf1, kcub1, khome, kend, kpp, knp, kdch1, kbs)
1274 (defun term-send-up () (interactive) (term-send-raw-string "\eOA"))
1275 (defun term-send-down () (interactive) (term-send-raw-string "\eOB"))
1276 (defun term-send-right () (interactive) (term-send-raw-string "\eOC"))
1277 (defun term-send-left () (interactive) (term-send-raw-string "\eOD"))
1278 (defun term-send-home () (interactive) (term-send-raw-string "\e[1~"))
1279 (defun term-send-insert() (interactive) (term-send-raw-string "\e[2~"))
1280 (defun term-send-end () (interactive) (term-send-raw-string "\e[4~"))
1281 (defun term-send-prior () (interactive) (term-send-raw-string "\e[5~"))
1282 (defun term-send-next () (interactive) (term-send-raw-string "\e[6~"))
1283 (defun term-send-del () (interactive) (term-send-raw-string "\e[3~"))
1284 (defun term-send-backspace () (interactive) (term-send-raw-string "\C-?"))
1286 (defun term-char-mode ()
1287 "Switch to char (\"raw\") sub-mode of term mode.
1288 Each character you type is sent directly to the inferior without
1289 intervention from Emacs, except for the escape character (usually C-c)."
1291 ;; FIXME: Emit message? Cfr ilisp-raw-message
1292 (when (term-in-line-mode)
1293 (setq term-old-mode-map (current-local-map))
1294 (use-local-map term-raw-map)
1295 (easy-menu-add term-terminal-menu)
1296 (easy-menu-add term-signals-menu)
1298 ;; Send existing partial line to inferior (without newline).
1299 (let ((pmark (process-mark (get-buffer-process (current-buffer))))
1300 (save-input-sender term-input-sender))
1301 (when (> (point) pmark)
1304 (setq term-input-sender
1305 (symbol-function 'term-send-string))
1308 (setq term-input-sender save-input-sender))))
1309 (term-update-mode-line)))
1311 (defun term-line-mode ()
1312 "Switch to line (\"cooked\") sub-mode of term mode.
1313 This means that Emacs editing commands work as normally, until
1314 you type \\[term-send-input] which sends the current line to the inferior."
1316 (when (term-in-char-mode)
1317 (use-local-map term-old-mode-map)
1318 (term-update-mode-line)))
1320 (defun term-update-mode-line ()
1322 (if (term-in-char-mode)
1324 'help-echo "mouse-1: Switch to line mode"
1325 'mouse-face 'mode-line-highlight
1328 (mode-line keymap (down-mouse-1 . term-line-mode))))
1330 'help-echo "mouse-1: Switch to char mode"
1331 'mouse-face 'mode-line-highlight
1334 (mode-line keymap (down-mouse-1 . term-char-mode))))))
1336 (when (term-pager-enabled)
1340 'help-echo "mouse-1: Disable paging"
1341 'mouse-face 'mode-line-highlight
1344 (mode-line keymap (down-mouse-1 .
1345 term-pager-toggle)))))))
1347 (serial-item-config)
1348 (proc (get-buffer-process (current-buffer))))
1349 (when (and (term-check-proc (current-buffer))
1350 (equal (process-type nil) 'serial))
1351 (let ((temp (serial-speed)))
1352 (setq serial-item-speed
1354 ,(or (and temp (format " %d" temp)) "")
1355 help-echo "mouse-1: Change the speed of the serial port"
1356 mouse-face mode-line-highlight
1357 local-map (keymap (mode-line keymap
1358 (down-mouse-1 . serial-mode-line-speed-menu-1))))))
1359 (let ((temp (process-contact proc :summary)))
1360 (setq serial-item-config
1362 ,(or (and temp (format " %s" temp)) "")
1363 help-echo "mouse-1: Change the configuration of the serial port"
1364 mouse-face mode-line-highlight
1365 local-map (keymap (mode-line keymap
1366 (down-mouse-1 . serial-mode-line-config-menu-1)))))))
1367 (setq mode-line-process
1368 (list ": " term-mode term-page
1372 (force-mode-line-update))
1374 (defun term-check-proc (buffer)
1375 "True if there is a process associated w/buffer BUFFER, and it
1376 is alive. BUFFER can be either a buffer or the name of one."
1377 (let ((proc (get-buffer-process buffer)))
1378 (and proc (memq (process-status proc) '(run stop open listen connect)))))
1381 (defun make-term (name program &optional startfile &rest switches)
1382 "Make a term process NAME in a buffer, running PROGRAM.
1383 The name of the buffer is made by surrounding NAME with `*'s.
1384 If there is already a running process in that buffer, it is not restarted.
1385 Optional third arg STARTFILE is the name of a file to send the contents of to
1386 the process. Any more args are arguments to PROGRAM."
1387 (let ((buffer (get-buffer-create (concat "*" name "*"))))
1388 ;; If no process, or nuked process, crank up a new one and put buffer in
1389 ;; term mode. Otherwise, leave buffer and existing process alone.
1390 (cond ((not (term-check-proc buffer))
1391 (with-current-buffer buffer
1392 (term-mode)) ; Install local vars, mode, keymap, ...
1393 (term-exec buffer name program startfile switches)))
1397 (defun term (program)
1398 "Start a terminal-emulator in a new buffer.
1399 The buffer is in Term mode; see `term-mode' for the
1400 commands to use in that buffer.
1402 \\<term-raw-map>Type \\[switch-to-buffer] to switch to another buffer."
1403 (interactive (list (read-from-minibuffer "Run program: "
1404 (or explicit-shell-file-name
1408 (set-buffer (make-term "terminal" program))
1411 (switch-to-buffer "*terminal*"))
1413 (defun term-exec (buffer name command startfile switches)
1414 "Start up a process in buffer for term modes.
1415 Blasts any old process running in the buffer. Doesn't set the buffer mode.
1416 You can use this to cheaply run a series of processes in the same term
1417 buffer. The hook `term-exec-hook' is run after each exec."
1418 (with-current-buffer buffer
1419 (let ((proc (get-buffer-process buffer))) ; Blast any old process.
1420 (when proc (delete-process proc)))
1421 ;; Crank up a new process
1422 (let ((proc (term-exec-1 name buffer command switches)))
1423 (make-local-variable 'term-ptyp)
1424 (setq term-ptyp process-connection-type) ; t if pty, nil if pipe.
1425 ;; Jump to the end, and set the process mark.
1426 (goto-char (point-max))
1427 (set-marker (process-mark proc) (point))
1428 (set-process-filter proc 'term-emulate-terminal)
1429 (set-process-sentinel proc 'term-sentinel)
1430 ;; Feed it the startfile.
1432 ;;This is guaranteed to wait long enough
1433 ;;but has bad results if the term does not prompt at all
1434 ;; (while (= size (buffer-size))
1436 ;;I hope 1 second is enough!
1438 (goto-char (point-max))
1439 (insert-file-contents startfile)
1441 proc (delete-and-extract-region (point) (point-max)))))
1442 (run-hooks 'term-exec-hook)
1445 (defun term-sentinel (proc msg)
1446 "Sentinel for term buffers.
1447 The main purpose is to get rid of the local keymap."
1448 (let ((buffer (process-buffer proc)))
1449 (when (memq (process-status proc) '(signal exit))
1450 (if (null (buffer-name buffer))
1452 (set-process-buffer proc nil)
1453 (with-current-buffer buffer
1454 ;; Write something in the compilation buffer
1455 ;; and hack its mode line.
1456 ;; Get rid of local keymap.
1458 (term-handle-exit (process-name proc) msg)
1459 ;; Since the buffer and mode line will show that the
1460 ;; process is dead, we can delete it now. Otherwise it
1461 ;; will stay around until M-x list-processes.
1462 (delete-process proc))))))
1464 (defun term-handle-exit (process-name msg)
1465 "Write process exit (or other change) message MSG in the current buffer."
1466 (let ((buffer-read-only nil)
1469 ;; Record where we put the message, so we can ignore it
1472 (insert ?\n "Process " process-name " " msg)
1473 ;; Force mode line redisplay soon.
1474 (force-mode-line-update)
1475 (when (and opoint (< opoint omax))
1476 (goto-char opoint))))
1479 (defvar term-term-name "eterm-color"
1480 "Name to use for TERM.
1481 Using \"emacs\" loses, because bash disables editing if $TERM == emacs.")
1482 ;; Format string, usage:
1483 ;; (format term-termcap-string emacs-term-name "TERMCAP=" 24 80)
1484 (defvar term-termcap-format
1485 "%s%s:li#%d:co#%d:cl=\\E[H\\E[J:cd=\\E[J:bs:am:xn:cm=\\E[%%i%%d;%%dH\
1486 :nd=\\E[C:up=\\E[A:ce=\\E[K:ho=\\E[H:pt\
1487 :al=\\E[L:dl=\\E[M:DL=\\E[%%dM:AL=\\E[%%dL:cs=\\E[%%i%%d;%%dr:sf=^J\
1488 :dc=\\E[P:DC=\\E[%%dP:IC=\\E[%%d@:im=\\E[4h:ei=\\E[4l:mi:\
1489 :so=\\E[7m:se=\\E[m:us=\\E[4m:ue=\\E[m:md=\\E[1m:mr=\\E[7m:me=\\E[m\
1490 :UP=\\E[%%dA:DO=\\E[%%dB:LE=\\E[%%dD:RI=\\E[%%dC\
1491 :kl=\\EOD:kd=\\EOB:kr=\\EOC:ku=\\EOA:kN=\\E[6~:kP=\\E[5~:@7=\\E[4~:kh=\\E[1~\
1492 :mk=\\E[8m:cb=\\E[1K:op=\\E[39;49m:Co#8:pa#64:AB=\\E[4%%dm:AF=\\E[3%%dm:cr=^M\
1493 :bl=^G:do=^J:le=^H:ta=^I:se=\\E[27m:ue=\\E24m\
1494 :kb=^?:kD=^[[3~:sc=\\E7:rc=\\E8:r1=\\Ec:"
1496 ;; don't define :te=\\E[2J\\E[?47l\\E8:ti=\\E7\\E[?47h\
1497 "Termcap capabilities supported.")
1499 ;; This auxiliary function cranks up the process for term-exec in
1500 ;; the appropriate environment.
1502 (defun term-exec-1 (name buffer command switches)
1503 ;; We need to do an extra (fork-less) exec to run stty.
1504 ;; (This would not be needed if we had suitable Emacs primitives.)
1505 ;; The 'if ...; then shift; fi' hack is because Bourne shell
1506 ;; loses one arg when called with -c, and newer shells (bash, ksh) don't.
1507 ;; Thus we add an extra dummy argument "..", and then remove it.
1508 (let ((process-environment
1511 (format "TERM=%s" term-term-name)
1512 (format "TERMINFO=%s" data-directory)
1513 (format term-termcap-format "TERMCAP="
1514 term-term-name term-height term-width)
1515 ;; We are going to get rid of the binding for EMACS,
1516 ;; probably in Emacs 23, because it breaks
1517 ;; `./configure' of some packages that expect it to
1518 ;; say where to find EMACS.
1519 (format "EMACS=%s (term:%s)" emacs-version term-protocol-version)
1520 (format "INSIDE_EMACS=%s,term:%s" emacs-version term-protocol-version)
1521 (format "LINES=%d" term-height)
1522 (format "COLUMNS=%d" term-width))
1523 process-environment))
1524 (process-connection-type t)
1525 ;; We should suppress conversion of end-of-line format.
1526 (inhibit-eol-conversion t)
1527 ;; The process's output contains not just chars but also binary
1528 ;; escape codes, so we need to see the raw output. We will have to
1529 ;; do the decoding by hand on the parts that are made of chars.
1530 (coding-system-for-read 'binary))
1531 (apply 'start-process name buffer
1533 (format "stty -nl echo rows %d columns %d sane 2>/dev/null;\
1534 if [ $1 = .. ]; then shift; fi; exec \"$@\""
1535 term-height term-width)
1540 ;;; Input history processing in a buffer
1541 ;; ===========================================================================
1542 ;; Useful input history functions, courtesy of the Ergo group.
1545 ;; term-dynamic-list-input-ring List history in help buffer.
1546 ;; term-previous-input Previous input...
1547 ;; term-previous-matching-input ...matching a string.
1548 ;; term-previous-matching-input-from-input ... matching the current input.
1549 ;; term-next-input Next input...
1550 ;; term-next-matching-input ...matching a string.
1551 ;; term-next-matching-input-from-input ... matching the current input.
1552 ;; term-backward-matching-input Backwards input...
1553 ;; term-forward-matching-input ...matching a string.
1554 ;; term-replace-by-expanded-history Expand history at point;
1555 ;; replace with expanded history.
1556 ;; term-magic-space Expand history and insert space.
1559 ;; term-read-input-ring Read into term-input-ring...
1560 ;; term-write-input-ring Write to term-input-ring-file-name.
1561 ;; term-replace-by-expanded-history-before-point Workhorse function.
1563 (defun term-read-input-ring (&optional silent)
1564 "Set the buffer's `term-input-ring' from a history file.
1565 The name of the file is given by the variable `term-input-ring-file-name'.
1566 The history ring is of size `term-input-ring-size', regardless of file size.
1567 If `term-input-ring-file-name' is nil this function does nothing.
1569 If the optional argument SILENT is non-nil, we say nothing about a
1570 failure to read the history file.
1572 This function is useful for major mode commands and mode hooks.
1574 The structure of the history file should be one input command per line,
1575 with the most recent command last.
1576 See also `term-input-ignoredups' and `term-write-input-ring'."
1577 (cond ((or (null term-input-ring-file-name)
1578 (equal term-input-ring-file-name ""))
1580 ((not (file-readable-p term-input-ring-file-name))
1582 (message "Cannot read history file %s"
1583 term-input-ring-file-name)))
1585 (let ((file term-input-ring-file-name)
1587 (ring (make-ring term-input-ring-size)))
1589 (insert-file-contents file)
1590 ;; Save restriction in case file is already visited...
1591 ;; Watch for those date stamps in history files!
1592 (goto-char (point-max))
1593 (while (and (< count term-input-ring-size)
1594 (re-search-backward "^[ \t]*\\([^#\n].*\\)[ \t]*$"
1596 (let ((history (buffer-substring (match-beginning 1)
1598 (when (or (null term-input-ignoredups)
1600 (not (string-equal (ring-ref ring 0) history)))
1601 (ring-insert-at-beginning ring history)))
1602 (setq count (1+ count))))
1603 (setq term-input-ring ring
1604 term-input-ring-index nil)))))
1606 (defun term-write-input-ring ()
1607 "Write the buffer's `term-input-ring' to a history file.
1608 The name of the file is given by the variable `term-input-ring-file-name'.
1609 The original contents of the file are lost if `term-input-ring' is not empty.
1610 If `term-input-ring-file-name' is nil this function does nothing.
1612 Useful within process sentinels.
1614 See also `term-read-input-ring'."
1615 (cond ((or (null term-input-ring-file-name)
1616 (equal term-input-ring-file-name "")
1617 (null term-input-ring) (ring-empty-p term-input-ring))
1619 ((not (file-writable-p term-input-ring-file-name))
1620 (message "Cannot write history file %s" term-input-ring-file-name))
1622 (let* ((history-buf (get-buffer-create " *Temp Input History*"))
1623 (ring term-input-ring)
1624 (file term-input-ring-file-name)
1625 (index (ring-length ring)))
1626 ;; Write it all out into a buffer first. Much faster, but messier,
1627 ;; than writing it one line at a time.
1628 (with-current-buffer history-buf
1631 (setq index (1- index))
1632 (insert (ring-ref ring index) ?\n))
1633 (write-region (buffer-string) nil file nil 'no-message)
1634 (kill-buffer nil))))))
1637 (defun term-dynamic-list-input-ring ()
1638 "List in help buffer the buffer's input history."
1640 (if (or (not (ring-p term-input-ring))
1641 (ring-empty-p term-input-ring))
1642 (message "No history")
1644 (history-buffer " *Input History*")
1645 (index (1- (ring-length term-input-ring)))
1646 (conf (current-window-configuration)))
1647 ;; We have to build up a list ourselves from the ring vector.
1649 (setq history (cons (ring-ref term-input-ring index) history)
1651 ;; Change "completion" to "history reference"
1652 ;; to make the display accurate.
1653 (with-output-to-temp-buffer history-buffer
1654 (display-completion-list history)
1655 (set-buffer history-buffer)
1657 (while (search-backward "completion" nil 'move)
1658 (replace-match "history reference")))
1660 (message "Hit space to flush")
1661 (let ((ch (read-event)))
1663 (set-window-configuration conf)
1664 (setq unread-command-events (list ch)))))))
1667 (defun term-regexp-arg (prompt)
1668 ;; Return list of regexp and prefix arg using PROMPT.
1669 (let* (;; Don't clobber this.
1670 (last-command last-command)
1671 (regexp (read-from-minibuffer prompt nil nil nil
1672 'minibuffer-history-search-history)))
1673 (list (if (string-equal regexp "")
1674 (setcar minibuffer-history-search-history
1675 (nth 1 minibuffer-history-search-history))
1677 (prefix-numeric-value current-prefix-arg))))
1679 (defun term-search-arg (arg)
1680 ;; First make sure there is a ring and that we are after the process mark
1681 (cond ((not (term-after-pmark-p))
1682 (error "Not at command line"))
1683 ((or (null term-input-ring)
1684 (ring-empty-p term-input-ring))
1685 (error "Empty input ring"))
1687 ;; arg of zero resets search from beginning, and uses arg of 1
1688 (setq term-input-ring-index nil)
1693 (defun term-search-start (arg)
1694 ;; Index to start a directional search, starting at term-input-ring-index
1695 (if term-input-ring-index
1696 ;; If a search is running, offset by 1 in direction of arg
1697 (mod (+ term-input-ring-index (if (> arg 0) 1 -1))
1698 (ring-length term-input-ring))
1699 ;; For a new search, start from beginning or end, as appropriate
1701 0 ; First elt for forward search
1702 (1- (ring-length term-input-ring))))) ; Last elt for backward search
1704 (defun term-previous-input-string (arg)
1705 "Return the string ARG places along the input ring.
1706 Moves relative to `term-input-ring-index'."
1707 (ring-ref term-input-ring (if term-input-ring-index
1708 (mod (+ arg term-input-ring-index)
1709 (ring-length term-input-ring))
1712 (defun term-previous-input (arg)
1713 "Cycle backwards through input history."
1715 (term-previous-matching-input "." arg))
1717 (defun term-next-input (arg)
1718 "Cycle forwards through input history."
1720 (term-previous-input (- arg)))
1722 (defun term-previous-matching-input-string (regexp arg)
1723 "Return the string matching REGEXP ARG places along the input ring.
1724 Moves relative to `term-input-ring-index'."
1725 (let* ((pos (term-previous-matching-input-string-position regexp arg)))
1726 (when pos (ring-ref term-input-ring pos))))
1728 (defun term-previous-matching-input-string-position
1729 (regexp arg &optional start)
1730 "Return the index matching REGEXP ARG places along the input ring.
1731 Moves relative to START, or `term-input-ring-index'."
1732 (when (or (not (ring-p term-input-ring))
1733 (ring-empty-p term-input-ring))
1734 (error "No history"))
1735 (let* ((len (ring-length term-input-ring))
1736 (motion (if (> arg 0) 1 -1))
1737 (n (mod (- (or start (term-search-start arg)) motion) len))
1738 (tried-each-ring-item nil)
1740 ;; Do the whole search as many times as the argument says.
1741 (while (and (/= arg 0) (not tried-each-ring-item))
1744 n (mod (+ n motion) len))
1745 ;; If we haven't reached a match, step some more.
1746 (while (and (< n len) (not tried-each-ring-item)
1747 (not (string-match regexp (ring-ref term-input-ring n))))
1748 (setq n (mod (+ n motion) len)
1749 ;; If we have gone all the way around in this search.
1750 tried-each-ring-item (= n prev)))
1751 (setq arg (if (> arg 0) (1- arg) (1+ arg))))
1752 ;; Now that we know which ring element to use, if we found it, return that.
1753 (when (string-match regexp (ring-ref term-input-ring n))
1756 (defun term-previous-matching-input (regexp n)
1757 "Search backwards through input history for match for REGEXP.
1758 \(Previous history elements are earlier commands.)
1759 With prefix argument N, search for Nth previous match.
1760 If N is negative, find the next or Nth next match."
1761 (interactive (term-regexp-arg "Previous input matching (regexp): "))
1762 (setq n (term-search-arg n))
1763 (let ((pos (term-previous-matching-input-string-position regexp n)))
1764 ;; Has a match been found?
1767 (setq term-input-ring-index pos)
1768 (message "History item: %d" (1+ pos))
1770 ;; Can't use kill-region as it sets this-command
1771 (process-mark (get-buffer-process (current-buffer))) (point))
1772 (insert (ring-ref term-input-ring pos)))))
1774 (defun term-next-matching-input (regexp n)
1775 "Search forwards through input history for match for REGEXP.
1776 \(Later history elements are more recent commands.)
1777 With prefix argument N, search for Nth following match.
1778 If N is negative, find the previous or Nth previous match."
1779 (interactive (term-regexp-arg "Next input matching (regexp): "))
1780 (term-previous-matching-input regexp (- n)))
1782 (defun term-previous-matching-input-from-input (n)
1783 "Search backwards through input history for match for current input.
1784 \(Previous history elements are earlier commands.)
1785 With prefix argument N, search for Nth previous match.
1786 If N is negative, search forwards for the -Nth following match."
1788 (when (not (memq last-command '(term-previous-matching-input-from-input
1789 term-next-matching-input-from-input)))
1790 ;; Starting a new search
1791 (setq term-matching-input-from-input-string
1793 (process-mark (get-buffer-process (current-buffer)))
1795 term-input-ring-index nil))
1796 (term-previous-matching-input
1797 (concat "^" (regexp-quote term-matching-input-from-input-string))
1800 (defun term-next-matching-input-from-input (n)
1801 "Search forwards through input history for match for current input.
1802 \(Following history elements are more recent commands.)
1803 With prefix argument N, search for Nth following match.
1804 If N is negative, search backwards for the -Nth previous match."
1806 (term-previous-matching-input-from-input (- n)))
1809 (defun term-replace-by-expanded-history (&optional silent)
1810 "Expand input command history references before point.
1811 Expansion is dependent on the value of `term-input-autoexpand'.
1813 This function depends on the buffer's idea of the input history, which may not
1814 match the command interpreter's idea, assuming it has one.
1816 Assumes history syntax is like typical Un*x shells'. However, since Emacs
1817 cannot know the interpreter's idea of input line numbers, assuming it has one,
1818 it cannot expand absolute input line number references.
1820 If the optional argument SILENT is non-nil, never complain
1821 even if history reference seems erroneous.
1823 See `term-magic-space' and `term-replace-by-expanded-history-before-point'.
1825 Returns t if successful."
1827 (when (and term-input-autoexpand
1828 (string-match "[!^]" (funcall term-get-old-input))
1829 (save-excursion (beginning-of-line)
1830 (looking-at term-prompt-regexp)))
1831 ;; Looks like there might be history references in the command.
1832 (let ((previous-modified-tick (buffer-modified-tick)))
1833 (message "Expanding history references...")
1834 (term-replace-by-expanded-history-before-point silent)
1835 (/= previous-modified-tick (buffer-modified-tick)))))
1838 (defun term-replace-by-expanded-history-before-point (silent)
1839 "Expand directory stack reference before point.
1840 See `term-replace-by-expanded-history'. Returns t if successful."
1842 (let ((toend (- (line-end-position) (point)))
1843 (start (progn (term-bol nil) (point))))
1845 (skip-chars-forward "^!^" (- (line-end-position) toend))
1846 (< (point) (- (line-end-position) toend)))
1847 ;; This seems a bit complex. We look for references such as !!, !-num,
1848 ;; !foo, !?foo, !{bar}, !?{bar}, ^oh, ^my^, ^god^it, ^never^ends^.
1849 ;; If that wasn't enough, the plings can be suffixed with argument
1850 ;; range specifiers.
1851 ;; Argument ranges are complex too, so we hive off the input line,
1852 ;; referenced with plings, with the range string to `term-args'.
1853 (setq term-input-ring-index nil)
1854 (cond ((or (= (preceding-char) ?\\)
1855 (term-within-quotes start (point)))
1856 ;; The history is quoted, or we're in quotes.
1857 (goto-char (1+ (point))))
1858 ((looking-at "![0-9]+\\($\\|[^-]\\)")
1859 ;; We cannot know the interpreter's idea of input line numbers.
1860 (goto-char (match-end 0))
1861 (message "Absolute reference cannot be expanded"))
1862 ((looking-at "!-\\([0-9]+\\)\\(:?[0-9^$*-]+\\)?")
1863 ;; Just a number of args from `number' lines backward.
1864 (let ((number (1- (string-to-number
1865 (buffer-substring (match-beginning 1)
1867 (if (<= number (ring-length term-input-ring))
1870 (term-args (term-previous-input-string number)
1871 (match-beginning 2) (match-end 2))
1873 (setq term-input-ring-index number)
1874 (message "History item: %d" (1+ number)))
1875 (goto-char (match-end 0))
1876 (message "Relative reference exceeds input history size"))))
1877 ((or (looking-at "!!?:?\\([0-9^$*-]+\\)") (looking-at "!!"))
1878 ;; Just a number of args from the previous input line.
1880 (term-args (term-previous-input-string 0)
1881 (match-beginning 1) (match-end 1))
1883 (message "History item: previous"))
1885 "!\\??\\({\\(.+\\)}\\|\\(\\sw+\\)\\)\\(:?[0-9^$*-]+\\)?")
1886 ;; Most recent input starting with or containing (possibly
1887 ;; protected) string, maybe just a number of args. Phew.
1888 (let* ((mb1 (match-beginning 1)) (me1 (match-end 1))
1889 (mb2 (match-beginning 2)) (me2 (match-end 2))
1890 (exp (buffer-substring (or mb2 mb1) (or me2 me1)))
1891 (pref (if (save-match-data (looking-at "!\\?")) "" "^"))
1892 (pos (save-match-data
1893 (term-previous-matching-input-string-position
1894 (concat pref (regexp-quote exp)) 1))))
1897 (goto-char (match-end 0))
1899 (progn (message "Not found")
1901 (setq term-input-ring-index pos)
1903 (term-args (ring-ref term-input-ring pos)
1904 (match-beginning 4) (match-end 4))
1906 (message "History item: %d" (1+ pos)))))
1907 ((looking-at "\\^\\([^^]+\\)\\^?\\([^^]*\\)\\^?")
1908 ;; Quick substitution on the previous input line.
1909 (let ((old (buffer-substring (match-beginning 1) (match-end 1)))
1910 (new (buffer-substring (match-beginning 2) (match-end 2)))
1912 (replace-match (term-previous-input-string 0) t t)
1914 (goto-char (match-beginning 0))
1915 (if (not (search-forward old pos t))
1917 (error "Not found"))
1918 (replace-match new t t)
1919 (message "History item: substituted"))))
1921 (goto-char (match-end 0))))))))
1924 (defun term-magic-space (arg)
1925 "Expand input history references before point and insert ARG spaces.
1926 A useful command to bind to SPC. See `term-replace-by-expanded-history'."
1928 (term-replace-by-expanded-history)
1929 (self-insert-command arg))
1931 (defun term-within-quotes (beg end)
1932 "Return t if the number of quotes between BEG and END is odd.
1933 Quotes are single and double."
1934 (let ((countsq (term-how-many-region "\\(^\\|[^\\\\]\\)\'" beg end))
1935 (countdq (term-how-many-region "\\(^\\|[^\\\\]\\)\"" beg end)))
1936 (or (= (mod countsq 2) 1) (= (mod countdq 2) 1))))
1938 (defun term-how-many-region (regexp beg end)
1939 "Return number of matches for REGEXP from BEG to END."
1944 (while (re-search-forward regexp end t)
1945 (setq count (1+ count)))))
1948 (defun term-args (string begin end)
1949 ;; From STRING, return the args depending on the range specified in the text
1950 ;; from BEGIN to END. If BEGIN is nil, assume all args. Ignore leading `:'.
1951 ;; Range can be x-y, x-, -y, where x/y can be [0-9], *, ^, $.
1954 (term-arguments string 0 nil)
1955 (let* ((range (buffer-substring
1956 (if (eq (char-after begin) ?:) (1+ begin) begin) end))
1957 (nth (cond ((string-match "^[*^]" range) 1)
1958 ((string-match "^-" range) 0)
1959 ((string-equal range "$") nil)
1960 (t (string-to-number range))))
1961 (mth (cond ((string-match "[-*$]$" range) nil)
1962 ((string-match "-" range)
1963 (string-to-number (substring range (match-end 0))))
1965 (term-arguments string nth mth)))))
1967 ;; Return a list of arguments from ARG. Break it up at the
1968 ;; delimiters in term-delimiter-argument-list. Returned list is backwards.
1969 (defun term-delim-arg (arg)
1970 (if (null term-delimiter-argument-list)
1976 (let ((char (aref arg pos))
1978 (if (memq char term-delimiter-argument-list)
1979 (while (and (< pos len) (eq (aref arg pos) char))
1980 (setq pos (1+ pos)))
1981 (while (and (< pos len)
1982 (not (memq (aref arg pos)
1983 term-delimiter-argument-list)))
1984 (setq pos (1+ pos))))
1985 (setq args (cons (substring arg start pos) args))))
1988 (defun term-arguments (string nth mth)
1989 "Return from STRING the NTH to MTH arguments.
1990 NTH and/or MTH can be nil, which means the last argument.
1991 Returned arguments are separated by single spaces.
1992 We assume whitespace separates arguments, except within quotes.
1993 Also, a run of one or more of a single character
1994 in `term-delimiter-argument-list' is a separate argument.
1995 Argument 0 is the command name."
1996 (let ((argpart "[^ \n\t\"'`]+\\|\\(\"[^\"]*\"\\|'[^']*'\\|`[^`]*`\\)")
2000 ;; Build a list of all the args until we have as many as we want.
2001 (while (and (or (null mth) (<= count mth))
2002 (string-match argpart string pos))
2003 (if (and beg (= pos (match-beginning 0)))
2004 ;; It's contiguous, part of the same arg.
2005 (setq pos (match-end 0)
2006 quotes (or quotes (match-beginning 1)))
2007 ;; It's a new separate arg.
2009 ;; Put the previous arg, if there was one, onto ARGS.
2010 (setq str (substring string beg pos)
2011 args (if quotes (cons str args)
2012 (nconc (term-delim-arg str) args))
2014 (setq quotes (match-beginning 1))
2015 (setq beg (match-beginning 0))
2016 (setq pos (match-end 0))))
2018 (setq str (substring string beg pos)
2019 args (if quotes (cons str args)
2020 (nconc (term-delim-arg str) args))
2022 (let ((n (or nth (1- count)))
2023 (m (if mth (1- (- count mth)) 0)))
2025 (function (lambda (a) a)) (nthcdr n (nreverse (nthcdr m args))) " "))))
2028 ;;; Input processing stuff [line mode]
2031 (defun term-send-input ()
2032 "Send input to process.
2033 After the process output mark, sends all text from the process mark to
2034 point as input to the process. Before the process output mark, calls value
2035 of variable `term-get-old-input' to retrieve old input, copies it to the
2036 process mark, and sends it. A terminal newline is also inserted into the
2037 buffer and sent to the process. The list of function names contained in the
2038 value of `term-input-filter-functions' is called on the input before sending
2039 it. The input is entered into the input history ring, if the value of variable
2040 `term-input-filter' returns non-nil when called on the input.
2042 Any history reference may be expanded depending on the value of the variable
2043 `term-input-autoexpand'. The list of function names contained in the value
2044 of `term-input-filter-functions' is called on the input before sending it.
2045 The input is entered into the input history ring, if the value of variable
2046 `term-input-filter' returns non-nil when called on the input.
2048 If variable `term-eol-on-send' is non-nil, then point is moved to the
2049 end of line before sending the input.
2051 The values of `term-get-old-input', `term-input-filter-functions', and
2052 `term-input-filter' are chosen according to the command interpreter running
2053 in the buffer. E.g.,
2055 If the interpreter is the csh,
2056 term-get-old-input is the default: take the current line, discard any
2057 initial string matching regexp term-prompt-regexp.
2058 term-input-filter-functions monitors input for \"cd\", \"pushd\", and
2059 \"popd\" commands. When it sees one, it cd's the buffer.
2060 term-input-filter is the default: returns t if the input isn't all white
2063 If the term is Lucid Common Lisp,
2064 term-get-old-input snarfs the sexp ending at point.
2065 term-input-filter-functions does nothing.
2066 term-input-filter returns nil if the input matches input-filter-regexp,
2067 which matches (1) all whitespace (2) :a, :c, etc.
2069 Similarly for Soar, Scheme, etc."
2071 ;; Note that the input string does not include its terminal newline.
2072 (let ((proc (get-buffer-process (current-buffer))))
2073 (if (not proc) (error "Current buffer has no process")
2074 (let* ((pmark (process-mark proc))
2075 (pmark-val (marker-position pmark))
2076 (input-is-new (>= (point) pmark-val))
2077 (intxt (if input-is-new
2078 (progn (if term-eol-on-send (end-of-line))
2079 (buffer-substring pmark (point)))
2080 (funcall term-get-old-input)))
2081 (input (if (not (eq term-input-autoexpand 'input))
2082 ;; Just whatever's already there
2084 ;; Expand and leave it visible in buffer
2085 (term-replace-by-expanded-history t)
2086 (buffer-substring pmark (point))))
2087 (history (if (not (eq term-input-autoexpand 'history))
2089 ;; This is messy 'cos ultimately the original
2090 ;; functions used do insertion, rather than return
2091 ;; strings. We have to expand, then insert back.
2092 (term-replace-by-expanded-history t)
2093 (let ((copy (buffer-substring pmark (point))))
2094 (delete-region pmark (point))
2097 (when (term-pager-enabled)
2099 (goto-char (process-mark proc))
2100 (setq term-pager-count (term-current-row))))
2101 (when (and (funcall term-input-filter history)
2102 (or (null term-input-ignoredups)
2103 (not (ring-p term-input-ring))
2104 (ring-empty-p term-input-ring)
2105 (not (string-equal (ring-ref term-input-ring 0)
2107 (ring-insert term-input-ring history))
2108 (let ((functions term-input-filter-functions))
2110 (funcall (car functions) (concat input "\n"))
2111 (setq functions (cdr functions))))
2112 (setq term-input-ring-index nil)
2114 ;; Update the markers before we send the input
2115 ;; in case we get output amidst sending the input.
2116 (set-marker term-last-input-start pmark)
2117 (set-marker term-last-input-end (point))
2119 ;; Set up to delete, because inferior should echo.
2120 (when (marker-buffer term-pending-delete-marker)
2121 (delete-region term-pending-delete-marker pmark))
2122 (set-marker term-pending-delete-marker pmark-val)
2123 (set-marker (process-mark proc) (point)))
2125 (funcall term-input-sender proc input)))))
2127 (defun term-get-old-input-default ()
2128 "Default for `term-get-old-input'.
2129 Take the current line, and discard any initial text matching
2130 `term-prompt-regexp'."
2134 (let ((beg (point)))
2136 (buffer-substring beg (point)))))
2138 (defun term-copy-old-input ()
2139 "Insert after prompt old input at point as new input to be edited.
2140 Calls `term-get-old-input' to get old input."
2142 (let ((input (funcall term-get-old-input))
2143 (process (get-buffer-process (current-buffer))))
2145 (error "Current buffer has no process")
2146 (goto-char (process-mark process))
2149 (defun term-skip-prompt ()
2150 "Skip past the text matching regexp `term-prompt-regexp'.
2151 If this takes us past the end of the current line, don't skip at all."
2152 (let ((eol (line-end-position)))
2153 (when (and (looking-at term-prompt-regexp)
2154 (<= (match-end 0) eol))
2155 (goto-char (match-end 0)))))
2158 (defun term-after-pmark-p ()
2159 "Is point after the process output marker?"
2160 ;; Since output could come into the buffer after we looked at the point
2161 ;; but before we looked at the process marker's value, we explicitly
2162 ;; serialize. This is just because I don't know whether or not Emacs
2163 ;; services input during execution of lisp commands.
2164 (let ((proc-pos (marker-position
2165 (process-mark (get-buffer-process (current-buffer))))))
2166 (<= proc-pos (point))))
2168 (defun term-simple-send (proc string)
2169 "Default function for sending to PROC input STRING.
2170 This just sends STRING plus a newline. To override this,
2171 set the hook `term-input-sender'."
2172 (term-send-string proc string)
2173 (term-send-string proc "\n"))
2175 (defun term-bol (arg)
2176 "Go to the beginning of line, then skip past the prompt, if any.
2177 If a prefix argument is given (\\[universal-argument]), then no prompt skip
2178 -- go straight to column 0.
2180 The prompt skip is done by skipping text matching the regular expression
2181 `term-prompt-regexp', a buffer local variable."
2184 (when (null arg) (term-skip-prompt)))
2186 ;; These two functions are for entering text you don't want echoed or
2187 ;; saved -- typically passwords to ftp, telnet, or somesuch.
2188 ;; Just enter m-x term-send-invisible and type in your line.
2190 (defun term-read-noecho (prompt &optional stars)
2191 "Read a single line of text from user without echoing, and return it.
2192 Prompt with argument PROMPT, a string. Optional argument STARS causes
2193 input to be echoed with '*' characters on the prompt line. Input ends with
2194 RET, LFD, or ESC. DEL or C-h rubs out. C-u kills line. C-g aborts (if
2195 `inhibit-quit' is set because e.g. this function was called from a process
2196 filter and C-g is pressed, this function returns nil rather than a string).
2198 Note that the keystrokes comprising the text can still be recovered
2199 \(temporarily) with \\[view-lossage]. This may be a security bug for some
2204 (cursor-in-echo-area t)
2208 (message "%s%s" prompt (make-string (length ans) ?*))
2209 (message "%s" prompt))
2210 (setq c (read-char))
2212 ;; This function may get called from a process filter, where
2213 ;; inhibit-quit is set. In later versions of Emacs read-char
2214 ;; may clear quit-flag itself and return C-g. That would make
2215 ;; it impossible to quit this loop in a simple way, so
2216 ;; re-enable it here (for backward-compatibility the check for
2217 ;; quit-flag below would still be necessary, so this seems
2218 ;; like the simplest way to do things).
2221 ((or (= c ?\r) (= c ?\n) (= c ?\e))
2225 ((and (/= c ?\b) (/= c ?\177))
2226 (setq ans (concat ans (char-to-string c))))
2228 (setq ans (substring ans 0 -1)))))
2230 ;; Emulate a true quit, except that we have to return a value.
2232 (setq quit-flag nil)
2238 (defun term-send-invisible (str &optional proc)
2239 "Read a string without echoing.
2240 Then send it to the process running in the current buffer. A new-line
2241 is additionally sent. String is not saved on term input history list.
2242 Security bug: your string can still be temporarily recovered with
2244 (interactive "P") ; Defeat snooping via C-x esc
2245 (when (not (stringp str))
2246 (setq str (term-read-noecho "Non-echoed text: " t)))
2248 (setq proc (get-buffer-process (current-buffer))))
2249 (if (not proc) (error "Current buffer has no process")
2250 (setq term-kill-echo-list (nconc term-kill-echo-list
2252 (term-send-string proc str)
2253 (term-send-string proc "\n")))
2256 ;;; Low-level process communication
2258 (defcustom term-input-chunk-size 512
2259 "Long inputs send to term processes are broken up into chunks of this size.
2260 If your process is choking on big inputs, try lowering the value."
2264 (defun term-send-string (proc str)
2265 "Send to PROC the contents of STR as input.
2266 This is equivalent to `process-send-string', except that long input strings
2267 are broken up into chunks of size `term-input-chunk-size'. Processes
2268 are given a chance to output between chunks. This can help prevent processes
2269 from hanging when you send them long inputs on some OS's."
2270 (let* ((len (length str))
2271 (i (min len term-input-chunk-size)))
2272 (process-send-string proc (substring str 0 i))
2274 (let ((next-i (+ i term-input-chunk-size)))
2275 (accept-process-output)
2276 (process-send-string proc (substring str i (min len next-i)))
2279 (defun term-send-region (proc start end)
2280 "Send to PROC the region delimited by START and END.
2281 This is a replacement for `process-send-region' that tries to keep
2282 your process from hanging on long inputs. See `term-send-string'."
2283 (term-send-string proc (buffer-substring start end)))
2286 ;;; Random input hackage
2288 (defun term-kill-output ()
2289 "Kill all output from interpreter since last input."
2291 (let ((pmark (process-mark (get-buffer-process (current-buffer)))))
2292 (kill-region term-last-input-end pmark)
2294 (insert "*** output flushed ***\n")
2295 (set-marker pmark (point))))
2297 (defun term-show-output ()
2298 "Display start of this batch of interpreter output at top of window.
2299 Sets mark to the value of point when this command is run."
2301 (goto-char term-last-input-end)
2304 (set-window-start (selected-window) (point))
2307 (defun term-interrupt-subjob ()
2308 "Interrupt the current subjob."
2310 (interrupt-process nil term-ptyp))
2312 (defun term-kill-subjob ()
2313 "Send kill signal to the current subjob."
2315 (kill-process nil term-ptyp))
2317 (defun term-quit-subjob ()
2318 "Send quit signal to the current subjob."
2320 (quit-process nil term-ptyp))
2322 (defun term-stop-subjob ()
2323 "Stop the current subjob.
2324 WARNING: if there is no current subjob, you can end up suspending
2325 the top-level process running in the buffer. If you accidentally do
2326 this, use \\[term-continue-subjob] to resume the process. (This
2327 is not a problem with most shells, since they ignore this signal.)"
2329 (stop-process nil term-ptyp))
2331 (defun term-continue-subjob ()
2332 "Send CONT signal to process buffer's process group.
2333 Useful if you accidentally suspend the top-level process."
2335 (continue-process nil term-ptyp))
2337 (defun term-kill-input ()
2338 "Kill all text from last stuff output by interpreter to point."
2340 (let* ((pmark (process-mark (get-buffer-process (current-buffer))))
2341 (p-pos (marker-position pmark)))
2342 (when (> (point) p-pos)
2343 (kill-region pmark (point)))))
2345 (defun term-delchar-or-maybe-eof (arg)
2346 "Delete ARG characters forward, or send an EOF to process if at end of
2353 (defun term-send-eof ()
2354 "Send an EOF to the current buffer's process."
2358 (defun term-backward-matching-input (regexp n)
2359 "Search backward through buffer for match for REGEXP.
2360 Matches are searched for on lines that match `term-prompt-regexp'.
2361 With prefix argument N, search for Nth previous match.
2362 If N is negative, find the next or Nth next match."
2363 (interactive (term-regexp-arg "Backward input matching (regexp): "))
2364 (let* ((re (concat term-prompt-regexp ".*" regexp))
2365 (pos (save-excursion (end-of-line (if (> n 0) 0 1))
2366 (when (re-search-backward re nil t n)
2369 (progn (message "Not found")
2374 (defun term-forward-matching-input (regexp n)
2375 "Search forward through buffer for match for REGEXP.
2376 Matches are searched for on lines that match `term-prompt-regexp'.
2377 With prefix argument N, search for Nth following match.
2378 If N is negative, find the previous or Nth previous match."
2379 (interactive (term-regexp-arg "Forward input matching (regexp): "))
2380 (term-backward-matching-input regexp (- n)))
2383 (defun term-next-prompt (n)
2384 "Move to end of Nth next prompt in the buffer.
2385 See `term-prompt-regexp'."
2387 (let ((paragraph-start term-prompt-regexp))
2388 (end-of-line (if (> n 0) 1 0))
2389 (forward-paragraph n)
2390 (term-skip-prompt)))
2392 (defun term-previous-prompt (n)
2393 "Move to end of Nth previous prompt in the buffer.
2394 See `term-prompt-regexp'."
2396 (term-next-prompt (- n)))
2398 ;;; Support for source-file processing commands.
2399 ;;============================================================================
2400 ;; Many command-interpreters (e.g., Lisp, Scheme, Soar) have
2401 ;; commands that process files of source text (e.g. loading or compiling
2402 ;; files). So the corresponding process-in-a-buffer modes have commands
2403 ;; for doing this (e.g., lisp-load-file). The functions below are useful
2404 ;; for defining these commands.
2406 ;; Alas, these guys don't do exactly the right thing for Lisp, Scheme
2407 ;; and Soar, in that they don't know anything about file extensions.
2408 ;; So the compile/load interface gets the wrong default occasionally.
2409 ;; The load-file/compile-file default mechanism could be smarter -- it
2410 ;; doesn't know about the relationship between filename extensions and
2411 ;; whether the file is source or executable. If you compile foo.lisp
2412 ;; with compile-file, then the next load-file should use foo.bin for
2413 ;; the default, not foo.lisp. This is tricky to do right, particularly
2414 ;; because the extension for executable files varies so much (.o, .bin,
2415 ;; .lbin, .mo, .vo, .ao, ...).
2418 ;; TERM-SOURCE-DEFAULT -- determines defaults for source-file processing
2421 ;; TERM-CHECK-SOURCE -- if FNAME is in a modified buffer, asks you if you
2422 ;; want to save the buffer before issuing any process requests to the command
2425 ;; TERM-GET-SOURCE -- used by the source-file processing commands to prompt
2426 ;; for the file to process.
2428 ;; (TERM-SOURCE-DEFAULT previous-dir/file source-modes)
2429 ;;============================================================================
2430 ;; This function computes the defaults for the load-file and compile-file
2431 ;; commands for tea, soar, cmulisp, and cmuscheme modes.
2433 ;; - PREVIOUS-DIR/FILE is a pair (directory . filename) from the last
2434 ;; source-file processing command, or nil if there hasn't been one yet.
2435 ;; - SOURCE-MODES is a list used to determine what buffers contain source
2436 ;; files: if the major mode of the buffer is in SOURCE-MODES, it's source.
2437 ;; Typically, (lisp-mode) or (scheme-mode).
2439 ;; If the command is given while the cursor is inside a string, *and*
2440 ;; the string is an existing filename, *and* the filename is not a directory,
2441 ;; then the string is taken as default. This allows you to just position
2442 ;; your cursor over a string that's a filename and have it taken as default.
2444 ;; If the command is given in a file buffer whose major mode is in
2445 ;; SOURCE-MODES, then the filename is the default file, and the
2446 ;; file's directory is the default directory.
2448 ;; If the buffer isn't a source file buffer (e.g., it's the process buffer),
2449 ;; then the default directory & file are what was used in the last source-file
2450 ;; processing command (i.e., PREVIOUS-DIR/FILE). If this is the first time
2451 ;; the command has been run (PREVIOUS-DIR/FILE is nil), the default directory
2452 ;; is the cwd, with no default file. (\"no default file\" = nil)
2454 ;; SOURCE-REGEXP is typically going to be something like (tea-mode)
2455 ;; for T programs, (lisp-mode) for Lisp programs, (soar-mode lisp-mode)
2456 ;; for Soar programs, etc.
2458 ;; The function returns a pair: (default-directory . default-file).
2460 (defun term-source-default (previous-dir/file source-modes)
2461 (cond ((and buffer-file-name (memq major-mode source-modes))
2462 (cons (file-name-directory buffer-file-name)
2463 (file-name-nondirectory buffer-file-name)))
2466 (cons default-directory nil))))
2469 ;; (TERM-CHECK-SOURCE fname)
2470 ;;============================================================================
2471 ;; Prior to loading or compiling (or otherwise processing) a file (in the CMU
2472 ;; process-in-a-buffer modes), this function can be called on the filename.
2473 ;; If the file is loaded into a buffer, and the buffer is modified, the user
2474 ;; is queried to see if he wants to save the buffer before proceeding with
2475 ;; the load or compile.
2477 (defun term-check-source (fname)
2478 (let ((buff (get-file-buffer fname)))
2480 (buffer-modified-p buff)
2481 (y-or-n-p (format "Save buffer %s first? "
2482 (buffer-name buff))))
2484 (with-current-buffer buff
2488 ;; (TERM-GET-SOURCE prompt prev-dir/file source-modes mustmatch-p)
2489 ;;============================================================================
2490 ;; TERM-GET-SOURCE is used to prompt for filenames in command-interpreter
2491 ;; commands that process source files (like loading or compiling a file).
2492 ;; It prompts for the filename, provides a default, if there is one,
2493 ;; and returns the result filename.
2495 ;; See TERM-SOURCE-DEFAULT for more on determining defaults.
2497 ;; PROMPT is the prompt string. PREV-DIR/FILE is the (directory . file) pair
2498 ;; from the last source processing command. SOURCE-MODES is a list of major
2499 ;; modes used to determine what file buffers contain source files. (These
2500 ;; two arguments are used for determining defaults). If MUSTMATCH-P is true,
2501 ;; then the filename reader will only accept a file that exists.
2504 ;; (interactive (term-get-source "Compile file: " prev-lisp-dir/file
2507 ;; This is pretty stupid about strings. It decides we're in a string
2508 ;; if there's a quote on both sides of point on the current line.
2509 (defun term-extract-string ()
2510 "Return string around `point' that starts the current line or nil."
2512 (let* ((point (point))
2513 (bol (line-beginning-position))
2514 (eol (line-end-position))
2515 (start (and (search-backward "\"" bol t)
2517 (end (progn (goto-char point)
2518 (and (search-forward "\"" eol t)
2521 (buffer-substring start end)))))
2523 (defun term-get-source (prompt prev-dir/file source-modes mustmatch-p)
2524 (let* ((def (term-source-default prev-dir/file source-modes))
2525 (stringfile (term-extract-string))
2526 (sfile-p (and stringfile
2528 (file-exists-p stringfile)
2530 (not (file-directory-p stringfile))))
2531 (defdir (if sfile-p (file-name-directory stringfile)
2533 (deffile (if sfile-p (file-name-nondirectory stringfile)
2535 (ans (read-file-name (if deffile (format "%s(default %s) "
2539 (concat defdir deffile)
2541 (list (expand-file-name (substitute-in-file-name ans)))))
2543 ;; I am somewhat divided on this string-default feature. It seems
2544 ;; to violate the principle-of-least-astonishment, in that it makes
2545 ;; the default harder to predict, so you actually have to look and see
2546 ;; what the default really is before choosing it. This can trip you up.
2547 ;; On the other hand, it can be useful, I guess. I would appreciate feedback
2552 ;;; Simple process query facility.
2553 ;; ===========================================================================
2554 ;; This function is for commands that want to send a query to the process
2555 ;; and show the response to the user. For example, a command to get the
2556 ;; arglist for a Common Lisp function might send a "(arglist 'foo)" query
2557 ;; to an inferior Common Lisp process.
2559 ;; This simple facility just sends strings to the inferior process and pops
2560 ;; up a window for the process buffer so you can see what the process
2561 ;; responds with. We don't do anything fancy like try to intercept what the
2562 ;; process responds with and put it in a pop-up window or on the message
2563 ;; line. We just display the buffer. Low tech. Simple. Works good.
2565 ;; Send to the inferior process PROC the string STR. Pop-up but do not select
2566 ;; a window for the inferior process so that its response can be seen.
2567 (defun term-proc-query (proc str)
2568 (let* ((proc-buf (process-buffer proc))
2569 (proc-mark (process-mark proc)))
2570 (display-buffer proc-buf)
2571 (set-buffer proc-buf) ; but it's not the selected *window*
2572 (let ((proc-win (get-buffer-window proc-buf))
2573 (proc-pt (marker-position proc-mark)))
2574 (term-send-string proc str) ; send the query
2575 (accept-process-output proc) ; wait for some output
2576 ;; Try to position the proc window so you can see the answer.
2577 ;; This is bogus code. If you delete the (sit-for 0), it breaks.
2578 ;; I don't know why. Wizards invited to improve it.
2579 (when (not (pos-visible-in-window-p proc-pt proc-win))
2580 (let ((opoint (window-point proc-win)))
2581 (set-window-point proc-win proc-mark) (sit-for 0)
2582 (if (not (pos-visible-in-window-p opoint proc-win))
2584 (set-window-point proc-win opoint)))))))
2586 ;; Returns the current column in the current screen line.
2587 ;; Note: (current-column) yields column in buffer line.
2589 (defun term-horizontal-column ()
2590 (- (term-current-column) (term-start-line-column)))
2592 ;; Calls either vertical-motion or term-buffer-vertical-motion
2593 (defmacro term-vertical-motion (count)
2594 (list 'funcall 'term-vertical-motion count))
2596 ; An emulation of vertical-motion that is independent of having a window.
2597 ; Instead, it uses the term-width variable as the logical window width.
2599 (defun term-buffer-vertical-motion (count)
2601 (move-to-column (* term-width (/ (current-column) term-width)))
2605 (todo (+ count (/ (current-column) term-width))))
2607 ;; The loop iterates over buffer lines;
2608 ;; H is the number of screen lines in the current line, i.e.
2609 ;; the ceiling of dividing the buffer line width by term-width.
2610 (while (and (<= (setq H (max (/ (+ (current-column) term-width -1)
2615 (setq todo (- todo H))
2616 (forward-char) ;; Move past the ?\n
2617 (end-of-line)) ;; and on to the end of the next line.
2618 (if (and (>= todo H) (> todo 0))
2619 (+ (- count todo) H -1) ;; Hit end of buffer.
2620 (move-to-column (* todo term-width))
2622 (t ;; (< count 0) ;; Similar algorithm, but for upward motion.
2625 (while (and (<= (setq H (max (/ (+ (current-column) term-width -1)
2629 (progn (beginning-of-line)
2631 (setq todo (- todo H))
2632 (backward-char)) ;; Move to end of previous line.
2633 (if (and (>= todo H) (> todo 0))
2634 (+ count todo (- 1 H)) ;; Hit beginning of buffer.
2635 (move-to-column (* (- H todo 1) term-width))
2638 ;; The term-start-line-column variable is used as a cache.
2639 (defun term-start-line-column ()
2640 (cond (term-start-line-column)
2641 ((let ((save-pos (point)))
2642 (term-vertical-motion 0)
2643 (setq term-start-line-column (current-column))
2644 (goto-char save-pos)
2645 term-start-line-column))))
2647 ;; Same as (current-column), but uses term-current-column as a cache.
2648 (defun term-current-column ()
2649 (cond (term-current-column)
2650 ((setq term-current-column (current-column)))))
2652 ;; Move DELTA column right (or left if delta < 0 limiting at column 0).
2654 (defun term-move-columns (delta)
2655 (setq term-current-column (max 0 (+ (term-current-column) delta)))
2656 (let ((point-at-eol (line-end-position)))
2657 (move-to-column term-current-column t)
2658 ;; If move-to-column extends the current line it will use the face
2659 ;; from the last character on the line, set the face for the chars
2661 (when (> (point) point-at-eol)
2662 (put-text-property point-at-eol (point) 'font-lock-face 'default))))
2664 ;; Insert COUNT copies of CHAR in the default face.
2665 (defun term-insert-char (char count)
2666 (let ((old-point (point)))
2667 (insert-char char count)
2668 (put-text-property old-point (point) 'font-lock-face 'default)))
2670 (defun term-current-row ()
2671 (cond (term-current-row)
2672 ((setq term-current-row
2675 (narrow-to-region term-home-marker (point-max))
2676 (- (term-vertical-motion -9999))))))))
2678 (defun term-adjust-current-row-cache (delta)
2679 (when term-current-row
2680 (setq term-current-row
2681 (max 0 (+ term-current-row delta)))))
2683 (defun term-terminal-pos ()
2684 (save-excursion ; save-restriction
2685 (let ((save-col (term-current-column))
2687 (term-vertical-motion 0)
2688 (setq x (- save-col (current-column)))
2689 (setq y (term-vertical-motion term-height))
2692 ;;Function that handles term messages: code by rms (and you can see the
2693 ;;difference ;-) -mm
2695 (defun term-handle-ansi-terminal-messages (message)
2696 ;; Is there a command here?
2697 (while (string-match "\eAnSiT.+\n" message)
2698 ;; Extract the command code and the argument.
2699 (let* ((start (match-beginning 0))
2700 (command-code (aref message (+ start 6)))
2705 (string-match "\r?\n" message
2708 ;; Delete this command from MESSAGE.
2709 (setq message (replace-match "" t t message))
2711 ;; If we recognize the type of command, set the appropriate variable.
2712 (cond ((= command-code ?c)
2713 (setq term-ansi-at-dir argument))
2714 ((= command-code ?h)
2715 (setq term-ansi-at-host argument))
2716 ((= command-code ?u)
2717 (setq term-ansi-at-user argument))
2718 ;; Otherwise ignore this one.
2722 ;; Update default-directory based on the changes this command made.
2725 (setq default-directory
2726 (file-name-as-directory
2727 (if (and (string= term-ansi-at-host (system-name))
2728 (string= term-ansi-at-user (user-real-login-name)))
2729 (expand-file-name term-ansi-at-dir)
2730 (if (string= term-ansi-at-user (user-real-login-name))
2731 (concat "/" term-ansi-at-host ":" term-ansi-at-dir)
2732 (concat "/" term-ansi-at-user "@" term-ansi-at-host ":"
2733 term-ansi-at-dir)))))
2735 ;; I'm not sure this is necessary,
2736 ;; but it's best to be on the safe side.
2737 (if (string= term-ansi-at-host (system-name))
2739 (setq ange-ftp-default-user term-ansi-at-save-user)
2740 (setq ange-ftp-default-password term-ansi-at-save-pwd)
2741 (setq ange-ftp-generate-anonymous-password term-ansi-at-save-anon))
2742 (setq term-ansi-at-save-user ange-ftp-default-user)
2743 (setq term-ansi-at-save-pwd ange-ftp-default-password)
2744 (setq term-ansi-at-save-anon ange-ftp-generate-anonymous-password)
2745 (setq ange-ftp-default-user nil)
2746 (setq ange-ftp-default-password nil)
2747 (setq ange-ftp-generate-anonymous-password nil)))))
2751 ;; Terminal emulation
2752 ;; This is the standard process filter for term buffers.
2753 ;; It emulates (most of the features of) a VT100/ANSI-style terminal.
2755 (defun term-emulate-terminal (proc str)
2756 (with-current-buffer (process-buffer proc)
2757 (let* ((i 0) char funny
2758 count ; number of decoded chars in substring
2759 count-bytes ; number of bytes
2761 save-point save-marker old-point temp win
2762 (buffer-undo-list t)
2763 (selected (selected-window))
2765 handled-ansi-message
2766 (str-length (length str)))
2767 (save-selected-window
2769 (let ((newstr (term-handle-ansi-terminal-messages str)))
2770 (unless (eq str newstr)
2771 (setq handled-ansi-message t
2773 (setq str-length (length str))
2775 (when (marker-buffer term-pending-delete-marker)
2776 ;; Delete text following term-pending-delete-marker.
2777 (delete-region term-pending-delete-marker (process-mark proc))
2778 (set-marker term-pending-delete-marker nil))
2780 (when (/= (point) (process-mark proc))
2781 (setq save-point (point-marker)))
2783 ;; Note if the window size has changed. We used to reset
2784 ;; point too, but that gives incorrect results (Bug#4635).
2785 (if (eq (window-buffer) (current-buffer))
2787 (setq term-vertical-motion (symbol-function 'vertical-motion))
2788 (term-check-size proc))
2789 (setq term-vertical-motion
2790 (symbol-function 'term-buffer-vertical-motion)))
2791 (setq save-marker (copy-marker (process-mark proc)))
2792 (goto-char (process-mark proc))
2795 ;; If the buffer is in line mode, and there is a partial
2796 ;; input line, save the line (by narrowing to leave it
2797 ;; outside the restriction ) until we're done with output.
2798 (when (and (> (point-max) (process-mark proc))
2799 (term-in-line-mode))
2800 (narrow-to-region (point-min) (process-mark proc)))
2802 (when term-log-buffer
2803 (princ str term-log-buffer))
2804 (cond ((eq term-terminal-state 4) ;; Have saved pending output.
2805 (setq str (concat term-terminal-parameter str))
2806 (setq term-terminal-parameter nil)
2807 (setq str-length (length str))
2808 (setq term-terminal-state 0)))
2810 (while (< i str-length)
2811 (setq char (aref str i))
2812 (cond ((< term-terminal-state 2)
2813 ;; Look for prefix of regular chars
2815 (string-match "[\r\n\000\007\033\t\b\032\016\017]"
2817 (when (not funny) (setq funny str-length))
2819 ;; Decode the string before counting
2820 ;; characters, to avoid garbling of certain
2821 ;; multibyte characters (bug#1006).
2822 (setq decoded-substring
2823 (decode-coding-string
2824 (substring str i funny)
2825 locale-coding-system))
2826 (cond ((eq term-terminal-state 1)
2827 ;; We are in state 1, we need to wrap
2828 ;; around. Go to the beginning of
2829 ;; the next line and switch to state
2832 (term-move-columns (- (term-current-column)))
2833 (setq term-terminal-state 0)))
2834 (setq count (length decoded-substring))
2835 (setq temp (- (+ (term-horizontal-column) count)
2837 (cond ((or term-suppress-hard-newline (<= temp 0)))
2838 ;; All count chars fit in line.
2839 ((> count temp) ;; Some chars fit.
2840 ;; This iteration, handle only what fits.
2841 (setq count (- count temp))
2844 (encode-coding-string
2845 (substring decoded-substring 0 count)
2848 (setq funny (+ count-bytes i)))
2849 ((or (not (or term-pager-count
2850 term-scroll-with-delete))
2851 (> (term-handle-scroll 1) 0))
2852 (term-adjust-current-row-cache 1)
2853 (setq count (min count term-width))
2856 (encode-coding-string
2857 (substring decoded-substring 0 count)
2859 (setq funny (+ count-bytes i))
2860 (setq term-start-line-column
2861 term-current-column))
2862 (t ;; Doing PAGER processing.
2863 (setq count 0 funny i)
2864 (setq term-current-column nil)
2865 (setq term-start-line-column nil)))
2866 (setq old-point (point))
2868 ;; Insert a string, check how many columns
2869 ;; we moved, then delete that many columns
2870 ;; following point if not eob nor insert-mode.
2871 (let ((old-column (current-column))
2873 (insert (decode-coding-string (substring str i funny) locale-coding-system))
2874 (setq term-current-column (current-column)
2875 columns (- term-current-column old-column))
2876 (when (not (or (eobp) term-insert-mode))
2878 (term-move-columns columns)
2879 (delete-region pos (point)))
2880 ;; In insert mode if the current line
2881 ;; has become too long it needs to be
2883 (when term-insert-mode
2886 (when (> (current-column) term-width)
2887 (delete-region (- (point) (- (current-column) term-width))
2890 (setq term-current-column nil)
2892 (put-text-property old-point (point)
2893 'font-lock-face term-current-face)
2894 ;; If the last char was written in last column,
2895 ;; back up one column, but remember we did so.
2896 ;; Thus we emulate xterm/vt100-style line-wrapping.
2898 (term-move-columns -1)
2899 (setq term-terminal-state 1)))
2900 (setq i (1- funny)))
2901 ((and (setq term-terminal-state 0)
2902 (eq char ?\^I)) ; TAB (terminfo: ht)
2903 (setq count (term-current-column))
2904 ;; The line cannot exceed term-width. TAB at
2905 ;; the end of a line should not cause wrapping.
2906 (setq count (min term-width
2907 (+ count 8 (- (mod count 8)))))
2908 (if (> term-width count)
2911 (- count (term-current-column)))
2912 (setq term-current-column count))
2913 (when (> term-width (term-current-column))
2915 (1- (- term-width (term-current-column)))))
2916 (when (= term-width (term-current-column))
2917 (term-move-columns -1))))
2918 ((eq char ?\r) ;; (terminfo: cr)
2919 (term-vertical-motion 0)
2920 (setq term-current-column term-start-line-column))
2921 ((eq char ?\n) ;; (terminfo: cud1, ind)
2922 (unless (and term-kill-echo-list
2923 (term-check-kill-echo-list))
2925 ((eq char ?\b) ;; (terminfo: cub1)
2926 (term-move-columns -1))
2927 ((eq char ?\033) ; Escape
2928 (setq term-terminal-state 2))
2929 ((eq char 0)) ; NUL: Do nothing
2930 ((eq char ?\016)) ; Shift Out - ignored
2931 ((eq char ?\017)) ; Shift In - ignored
2932 ((eq char ?\^G) ;; (terminfo: bel)
2934 ((and (eq char ?\032)
2935 (not handled-ansi-message))
2936 (let ((end (string-match "\r?$" str i)))
2938 (funcall term-command-hook
2939 (decode-coding-string
2940 (prog1 (substring str (1+ i) end)
2941 (setq i (match-end 0)))
2942 locale-coding-system))
2943 (setq term-terminal-parameter (substring str i))
2944 (setq term-terminal-state 4)
2945 (setq i str-length))))
2946 (t ; insert char FIXME: Should never happen
2947 (term-move-columns 1)
2948 (backward-delete-char 1)
2950 ((eq term-terminal-state 2) ; Seen Esc
2951 (cond ((eq char ?\133) ;; ?\133 = ?[
2953 ;; Some modifications to cope with multiple
2954 ;; settings like ^[[01;32;43m -mm
2955 ;; Note that now the init value of
2956 ;; term-terminal-previous-parameter has been
2959 (setq term-terminal-parameter 0)
2960 (setq term-terminal-previous-parameter -1)
2961 (setq term-terminal-previous-parameter-2 -1)
2962 (setq term-terminal-previous-parameter-3 -1)
2963 (setq term-terminal-previous-parameter-4 -1)
2964 (setq term-terminal-more-parameters 0)
2965 (setq term-terminal-state 3))
2966 ((eq char ?D) ;; scroll forward
2967 (term-handle-deferred-scroll)
2969 (setq term-terminal-state 0))
2970 ;; ((eq char ?E) ;; (terminfo: nw), not used for
2971 ;; ;; now, but this is a working
2972 ;; ;; implementation
2974 ;; (term-goto term-current-row 0)
2975 ;; (setq term-terminal-state 0))
2976 ((eq char ?M) ;; scroll reversed (terminfo: ri)
2977 (if (or (< (term-current-row) term-scroll-start)
2978 (>= (1- (term-current-row))
2980 ;; Scrolling up will not move outside
2981 ;; the scroll region.
2983 ;; Scrolling the scroll region is needed.
2985 (setq term-terminal-state 0))
2986 ((eq char ?7) ;; Save cursor (terminfo: sc)
2987 (term-handle-deferred-scroll)
2988 (setq term-saved-cursor
2989 (list (term-current-row)
2990 (term-horizontal-column)
2991 term-ansi-current-bg-color
2992 term-ansi-current-bold
2993 term-ansi-current-color
2994 term-ansi-current-invisible
2995 term-ansi-current-reverse
2996 term-ansi-current-underline
2999 (setq term-terminal-state 0))
3000 ((eq char ?8) ;; Restore cursor (terminfo: rc)
3001 (when term-saved-cursor
3002 (term-goto (nth 0 term-saved-cursor)
3003 (nth 1 term-saved-cursor))
3004 (setq term-ansi-current-bg-color
3005 (nth 2 term-saved-cursor)
3006 term-ansi-current-bold
3007 (nth 3 term-saved-cursor)
3008 term-ansi-current-color
3009 (nth 4 term-saved-cursor)
3010 term-ansi-current-invisible
3011 (nth 5 term-saved-cursor)
3012 term-ansi-current-reverse
3013 (nth 6 term-saved-cursor)
3014 term-ansi-current-underline
3015 (nth 7 term-saved-cursor)
3017 (nth 8 term-saved-cursor)))
3018 (setq term-terminal-state 0))
3019 ((eq char ?c) ;; \Ec - Reset (terminfo: rs1)
3020 ;; This is used by the "clear" program.
3021 (setq term-terminal-state 0)
3022 (term-reset-terminal))
3023 ;; The \E#8 reset sequence for xterm. We
3024 ;; probably don't need to handle it, but this
3025 ;; is the code to parse it.
3027 ;; (when (eq (aref str (1+ i)) ?8)
3029 ;; (setq term-scroll-start 0)
3030 ;; (setq term-scroll-end term-height)
3031 ;; (setq term-terminal-state 0)))
3032 ((setq term-terminal-state 0))))
3033 ((eq term-terminal-state 3) ; Seen Esc [
3034 (cond ((and (>= char ?0) (<= char ?9))
3035 (setq term-terminal-parameter
3036 (+ (* 10 term-terminal-parameter) (- char ?0))))
3038 ;; Some modifications to cope with multiple
3039 ;; settings like ^[[01;32;43m -mm
3040 (setq term-terminal-more-parameters 1)
3041 (setq term-terminal-previous-parameter-4
3042 term-terminal-previous-parameter-3)
3043 (setq term-terminal-previous-parameter-3
3044 term-terminal-previous-parameter-2)
3045 (setq term-terminal-previous-parameter-2
3046 term-terminal-previous-parameter)
3047 (setq term-terminal-previous-parameter
3048 term-terminal-parameter)
3049 (setq term-terminal-parameter 0))
3050 ((eq char ??)) ; Ignore ?
3052 (term-handle-ansi-escape proc char)
3053 (setq term-terminal-more-parameters 0)
3054 (setq term-terminal-previous-parameter-4 -1)
3055 (setq term-terminal-previous-parameter-3 -1)
3056 (setq term-terminal-previous-parameter-2 -1)
3057 (setq term-terminal-previous-parameter -1)
3058 (setq term-terminal-state 0)))))
3059 (when (term-handling-pager)
3060 ;; Finish stuff to get ready to handle PAGER.
3061 (if (> (% (current-column) term-width) 0)
3062 (setq term-terminal-parameter
3064 ;; We're at column 0. Goto end of buffer; to compensate,
3065 ;; prepend a ?\r for later. This looks more consistent.
3067 (setq term-terminal-parameter
3068 (concat "\r" (substring str i)))
3069 (setq term-terminal-parameter (substring str (1- i)))
3070 (aset term-terminal-parameter 0 ?\r))
3071 (goto-char (point-max)))
3072 (setq term-terminal-state 4)
3073 (make-local-variable 'term-pager-old-filter)
3074 (setq term-pager-old-filter (process-filter proc))
3075 (set-process-filter proc term-pager-filter)
3076 (setq i str-length))
3079 (when (>= (term-current-row) term-height)
3080 (term-handle-deferred-scroll))
3082 (set-marker (process-mark proc) (point))
3084 (goto-char save-point)
3085 (set-marker save-point nil))
3087 ;; Check for a pending filename-and-line number to display.
3088 ;; We do this before scrolling, because we might create a new window.
3089 (when (and term-pending-frame
3090 (eq (window-buffer selected) (current-buffer)))
3091 (term-display-line (car term-pending-frame)
3092 (cdr term-pending-frame))
3093 (setq term-pending-frame nil)
3094 ;; We have created a new window, so check the window size.
3095 (term-check-size proc))
3097 ;; Scroll each window displaying the buffer but (by default)
3098 ;; only if the point matches the process-mark we started with.
3100 ;; Avoid infinite loop in strange case where minibuffer window
3101 ;; is selected but not active.
3102 (while (window-minibuffer-p win)
3103 (setq win (next-window win nil t)))
3106 (setq win (next-window win nil t))
3107 (when (eq (window-buffer win) (process-buffer proc))
3108 (let ((scroll term-scroll-to-bottom-on-output))
3110 (when (or (= (point) save-marker)
3111 (eq scroll t) (eq scroll 'all)
3112 ;; Maybe user wants point to jump to the end.
3113 (and (eq selected win)
3114 (or (eq scroll 'this) (not save-point)))
3115 (and (eq scroll 'others)
3116 (not (eq selected win))))
3117 (goto-char term-home-marker)
3119 (goto-char (process-mark proc))
3120 (if (not (pos-visible-in-window-p (point) win))
3122 ;; Optionally scroll so that the text
3123 ;; ends at the bottom of the window.
3124 (when (and term-scroll-show-maximum-output
3125 (>= (point) (process-mark proc)))
3127 (goto-char (point-max))
3129 (not (eq win last-win))))
3131 ;; Stolen from comint.el and adapted -mm
3132 (when (> term-buffer-maximum-size 0)
3134 (goto-char (process-mark (get-buffer-process (current-buffer))))
3135 (forward-line (- term-buffer-maximum-size))
3137 (delete-region (point-min) (point))))
3138 (set-marker save-marker nil)))
3139 ;; This might be expensive, but we need it to handle something
3140 ;; like `sleep 5 | less -c' in more-or-less real time.
3141 (when (get-buffer-window (current-buffer))
3144 (defun term-handle-deferred-scroll ()
3145 (let ((count (- (term-current-row) term-height)))
3148 (goto-char term-home-marker)
3149 (term-vertical-motion (1+ count))
3150 (set-marker term-home-marker (point))
3151 (setq term-current-row (1- term-height))))))
3153 (defun term-reset-terminal ()
3154 "Reset the terminal, delete all the content and set the face to the default one."
3157 (setq term-current-row 0)
3158 (setq term-current-column 1)
3159 (setq term-scroll-start 0)
3160 (setq term-scroll-end term-height)
3161 (setq term-insert-mode nil)
3162 ;; FIXME: No idea why this is here, it looks wrong. --Stef
3163 (setq term-ansi-face-already-done nil))
3165 ;; New function to deal with ansi colorized output, as you can see you can
3166 ;; have any bold/underline/fg/bg/reverse combination. -mm
3168 (defun term-handle-colors-array (parameter)
3171 ;; Bold (terminfo: bold)
3173 (setq term-ansi-current-bold t))
3177 (setq term-ansi-current-underline t))
3179 ;; Blink (unsupported by Emacs), will be translated to bold.
3180 ;; This may change in the future though.
3182 (setq term-ansi-current-bold t))
3184 ;; Reverse (terminfo: smso)
3186 (setq term-ansi-current-reverse t))
3190 (setq term-ansi-current-invisible t))
3192 ;; Reset underline (terminfo: rmul)
3194 (setq term-ansi-current-underline nil))
3196 ;; Reset reverse (terminfo: rmso)
3198 (setq term-ansi-current-reverse nil))
3201 ((and (>= parameter 30) (<= parameter 37))
3202 (setq term-ansi-current-color (- parameter 29)))
3206 (setq term-ansi-current-color 0))
3209 ((and (>= parameter 40) (<= parameter 47))
3210 (setq term-ansi-current-bg-color (- parameter 39)))
3214 (setq term-ansi-current-bg-color 0))
3216 ;; 0 (Reset) or unknown (reset anyway)
3220 ;; (message "Debug: U-%d R-%d B-%d I-%d D-%d F-%d B-%d"
3221 ;; term-ansi-current-underline
3222 ;; term-ansi-current-reverse
3223 ;; term-ansi-current-bold
3224 ;; term-ansi-current-invisible
3225 ;; term-ansi-face-already-done
3226 ;; term-ansi-current-color
3227 ;; term-ansi-current-bg-color)
3229 (unless term-ansi-face-already-done
3230 (if term-ansi-current-invisible
3232 (if term-ansi-current-reverse
3234 (elt ansi-term-color-vector term-ansi-current-color)
3237 (elt ansi-term-color-vector term-ansi-current-bg-color)
3239 (setq term-current-face
3240 (list :background color
3242 ) ;; No need to bother with anything else if it's invisible.
3243 (setq term-current-face
3246 (elt ansi-term-color-vector term-ansi-current-color)
3250 (elt ansi-term-color-vector term-ansi-current-bg-color)
3252 :inverse-video term-ansi-current-reverse))
3254 (when term-ansi-current-bold
3255 (setq term-current-face
3256 `(,term-current-face :inherit term-bold)))
3258 (when term-ansi-current-underline
3259 (setq term-current-face
3260 `(,term-current-face :inherit term-underline)))))
3262 ;; (message "Debug %S" term-current-face)
3263 ;; FIXME: shouldn't we set term-ansi-face-already-done to t here? --Stef
3264 (setq term-ansi-face-already-done nil))
3267 ;; Handle a character assuming (eq terminal-state 2) -
3268 ;; i.e. we have previously seen Escape followed by ?[.
3270 (defun term-handle-ansi-escape (proc char)
3272 ((or (eq char ?H) ;; cursor motion (terminfo: cup,home)
3273 ;; (eq char ?f) ;; xterm seems to handle this sequence too, not
3276 (when (<= term-terminal-parameter 0)
3277 (setq term-terminal-parameter 1))
3278 (when (<= term-terminal-previous-parameter 0)
3279 (setq term-terminal-previous-parameter 1))
3280 (when (> term-terminal-previous-parameter term-height)
3281 (setq term-terminal-previous-parameter term-height))
3282 (when (> term-terminal-parameter term-width)
3283 (setq term-terminal-parameter term-width))
3285 (1- term-terminal-previous-parameter)
3286 (1- term-terminal-parameter)))
3287 ;; \E[A - cursor up (terminfo: cuu, cuu1)
3289 (term-handle-deferred-scroll)
3290 (let ((tcr (term-current-row)))
3292 (if (< (- tcr term-terminal-parameter) term-scroll-start)
3293 ;; If the amount to move is before scroll start, move
3295 (- term-scroll-start tcr)
3296 (if (>= term-terminal-parameter tcr)
3298 (- (max 1 term-terminal-parameter)))) t)))
3299 ;; \E[B - cursor down (terminfo: cud)
3301 (let ((tcr (term-current-row)))
3302 (unless (= tcr (1- term-scroll-end))
3304 (if (> (+ tcr term-terminal-parameter) term-scroll-end)
3305 (- term-scroll-end 1 tcr)
3306 (max 1 term-terminal-parameter)) t))))
3307 ;; \E[C - cursor right (terminfo: cuf, cuf1)
3311 (if (>= (+ term-terminal-parameter (term-current-column)) term-width)
3312 (- term-width (term-current-column) 1)
3313 term-terminal-parameter))))
3314 ;; \E[D - cursor left (terminfo: cub)
3316 (term-move-columns (- (max 1 term-terminal-parameter))))
3317 ;; \E[J - clear to end of screen (terminfo: ed, clear)
3319 (term-erase-in-display term-terminal-parameter))
3320 ;; \E[K - clear to end of line (terminfo: el, el1)
3322 (term-erase-in-line term-terminal-parameter))
3323 ;; \E[L - insert lines (terminfo: il, il1)
3325 (term-insert-lines (max 1 term-terminal-parameter)))
3326 ;; \E[M - delete lines (terminfo: dl, dl1)
3328 (term-delete-lines (max 1 term-terminal-parameter)))
3329 ;; \E[P - delete chars (terminfo: dch, dch1)
3331 (term-delete-chars (max 1 term-terminal-parameter)))
3332 ;; \E[@ - insert spaces (terminfo: ich)
3334 (term-insert-spaces (max 1 term-terminal-parameter)))
3335 ;; \E[?h - DEC Private Mode Set
3337 (cond ((eq term-terminal-parameter 4) ;; (terminfo: smir)
3338 (setq term-insert-mode t))
3339 ;; ((eq term-terminal-parameter 47) ;; (terminfo: smcup)
3340 ;; (term-switch-to-alternate-sub-buffer t))
3342 ;; \E[?l - DEC Private Mode Reset
3344 (cond ((eq term-terminal-parameter 4) ;; (terminfo: rmir)
3345 (setq term-insert-mode nil))
3346 ;; ((eq term-terminal-parameter 47) ;; (terminfo: rmcup)
3347 ;; (term-switch-to-alternate-sub-buffer nil))
3350 ;; Modified to allow ansi coloring -mm
3351 ;; \E[m - Set/reset modes, set bg/fg
3352 ;;(terminfo: smso,rmso,smul,rmul,rev,bold,sgr0,invis,op,setab,setaf)
3354 (when (= term-terminal-more-parameters 1)
3355 (when (>= term-terminal-previous-parameter-4 0)
3356 (term-handle-colors-array term-terminal-previous-parameter-4))
3357 (when (>= term-terminal-previous-parameter-3 0)
3358 (term-handle-colors-array term-terminal-previous-parameter-3))
3359 (when (>= term-terminal-previous-parameter-2 0)
3360 (term-handle-colors-array term-terminal-previous-parameter-2))
3361 (term-handle-colors-array term-terminal-previous-parameter))
3362 (term-handle-colors-array term-terminal-parameter))
3364 ;; \E[6n - Report cursor position (terminfo: u7)
3366 (term-handle-deferred-scroll)
3367 (process-send-string proc
3370 (1+ (term-current-row))
3371 (1+ (term-horizontal-column)))))
3372 ;; \E[r - Set scrolling region (terminfo: csr)
3374 (term-set-scroll-region
3375 (1- term-terminal-previous-parameter)
3376 (1- term-terminal-parameter)))
3379 (defun term-set-scroll-region (top bottom)
3380 "Set scrolling region.
3381 TOP is the top-most line (inclusive) of the new scrolling region,
3382 while BOTTOM is the line following the new scrolling region (e.g. exclusive).
3383 The top-most line is line 0."
3384 (setq term-scroll-start
3385 (if (or (< top 0) (>= top term-height))
3388 (setq term-scroll-end
3389 (if (or (<= bottom term-scroll-start) (> bottom term-height))
3392 (setq term-scroll-with-delete
3393 (or (term-using-alternate-sub-buffer)
3394 (not (and (= term-scroll-start 0)
3395 (= term-scroll-end term-height)))))
3396 (term-move-columns (- (term-current-column)))
3399 ;; (defun term-switch-to-alternate-sub-buffer (set)
3400 ;; ;; If asked to switch to (from) the alternate sub-buffer, and already (not)
3401 ;; ;; using it, do nothing. This test is needed for some programs (including
3402 ;; ;; Emacs) that emit the ti termcap string twice, for unknown reason.
3403 ;; (term-handle-deferred-scroll)
3404 ;; (if (eq set (not (term-using-alternate-sub-buffer)))
3405 ;; (let ((row (term-current-row))
3406 ;; (col (term-horizontal-column)))
3408 ;; (goto-char (point-max))
3409 ;; (if (not (eq (preceding-char) ?\n))
3410 ;; (term-insert-char ?\n 1))
3411 ;; (setq term-scroll-with-delete t)
3412 ;; (setq term-saved-home-marker (copy-marker term-home-marker))
3413 ;; (set-marker term-home-marker (point)))
3415 ;; (setq term-scroll-with-delete
3416 ;; (not (and (= term-scroll-start 0)
3417 ;; (= term-scroll-end term-height))))
3418 ;; (set-marker term-home-marker term-saved-home-marker)
3419 ;; (set-marker term-saved-home-marker nil)
3420 ;; (setq term-saved-home-marker nil)
3421 ;; (goto-char term-home-marker)))
3422 ;; (setq term-current-column nil)
3423 ;; (setq term-current-row 0)
3424 ;; (term-goto row col))))
3426 ;; Default value for the symbol term-command-hook.
3428 (defun term-command-hook (string)
3429 (cond ((equal string "")
3431 ((= (aref string 0) ?\032)
3432 ;; gdb (when invoked with -fullname) prints:
3433 ;; \032\032FULLFILENAME:LINENUMBER:CHARPOS:BEG_OR_MIDDLE:PC\n
3434 (let* ((first-colon (string-match ":" string 1))
3436 (string-match ":" string (1+ first-colon)))
3437 (filename (substring string 1 first-colon))
3438 (fileline (string-to-number
3439 (substring string (1+ first-colon) second-colon))))
3440 (setq term-pending-frame (cons filename fileline))))
3441 ((= (aref string 0) ?/)
3442 (cd (substring string 1)))
3443 ;; Allowing the inferior to call functions in Emacs is
3444 ;; probably too big a security hole.
3445 ;; ((= (aref string 0) ?!)
3446 ;; (eval (car (read-from-string string 1))))
3447 (t)));; Otherwise ignore it
3449 ;; Make sure the file named TRUE-FILE is in a buffer that appears on the screen
3450 ;; and that its line LINE is visible.
3451 ;; Put the overlay-arrow on the line LINE in that buffer.
3452 ;; This is mainly used by gdb.
3454 (defun term-display-line (true-file line)
3455 (term-display-buffer-line (find-file-noselect true-file) line))
3457 (defun term-display-buffer-line (buffer line)
3458 (let* ((window (display-buffer buffer t))
3460 (with-current-buffer buffer
3463 (goto-char (point-min))
3464 (forward-line (1- line))
3466 (setq overlay-arrow-string "=>")
3467 (or overlay-arrow-position
3468 (setq overlay-arrow-position (make-marker)))
3469 (set-marker overlay-arrow-position (point) (current-buffer)))
3470 (cond ((or (< pos (point-min)) (> pos (point-max)))
3473 (set-window-point window overlay-arrow-position)))
3475 ;; The buffer-local marker term-home-marker defines the "home position"
3476 ;; (in terms of cursor motion). However, we move the term-home-marker
3477 ;; "down" as needed so that is no more that a window-full above (point-max).
3479 (defun term-goto-home ()
3480 (term-handle-deferred-scroll)
3481 (goto-char term-home-marker)
3482 (setq term-current-row 0)
3483 (setq term-current-column (current-column))
3484 (setq term-start-line-column term-current-column))
3486 (defun term-goto (row col)
3487 (term-handle-deferred-scroll)
3488 (cond ((and term-current-row (>= row term-current-row))
3489 ;; I assume this is a worthwhile optimization.
3490 (term-vertical-motion 0)
3491 (setq term-current-column term-start-line-column)
3492 (setq row (- row term-current-row)))
3496 (term-move-columns col))
3498 ;; The page is full, so enter "pager" mode, and wait for input.
3500 (defun term-process-pager ()
3501 ;; (let ((process (get-buffer-process (current-buffer))))
3502 ;; (stop-process process))
3503 (setq term-pager-old-local-map (current-local-map))
3504 (use-local-map term-pager-break-map)
3505 (easy-menu-add term-terminal-menu)
3506 (easy-menu-add term-signals-menu)
3507 (easy-menu-add term-pager-menu)
3508 (make-local-variable 'term-old-mode-line-format)
3509 (setq term-old-mode-line-format mode-line-format)
3510 (setq mode-line-format
3511 (list "-- **MORE** "
3512 mode-line-buffer-identification
3513 " [Type ? for help] "
3515 (force-mode-line-update))
3517 (defun term-pager-line (lines)
3519 (let* ((moved (vertical-motion (1+ lines)))
3520 (deficit (- lines moved)))
3521 (when (> moved lines)
3523 (cond ((<= deficit 0) ;; OK, had enough in the buffer for request.
3524 (recenter (1- term-height)))
3525 ((term-pager-continue deficit)))))
3527 (defun term-pager-page (arg)
3528 "Proceed past the **MORE** break, allowing the next page of output to appear."
3530 (term-pager-line (* arg term-height)))
3532 ;; Pager mode command to go to beginning of buffer.
3533 (defun term-pager-bob ()
3535 (goto-char (point-min))
3536 (when (= (vertical-motion term-height) term-height)
3538 (recenter (1- term-height)))
3540 ;; Pager mode command to go to end of buffer.
3541 (defun term-pager-eob ()
3543 (goto-char term-home-marker)
3545 (goto-char (process-mark (get-buffer-process (current-buffer)))))
3547 (defun term-pager-back-line (lines)
3549 (vertical-motion (- 1 lines))
3553 ;; Move cursor to end of window.
3554 (vertical-motion term-height)
3556 (recenter (1- term-height)))
3558 (defun term-pager-back-page (arg)
3560 (term-pager-back-line (* arg term-height)))
3562 (defun term-pager-discard ()
3564 (setq term-terminal-parameter "")
3565 (interrupt-process nil t)
3566 (term-pager-continue term-height))
3568 ;; Disable pager processing.
3569 ;; Only callable while in pager mode. (Contrast term-disable-pager.)
3570 (defun term-pager-disable ()
3572 (if (term-handling-pager)
3573 (term-pager-continue nil)
3574 (setq term-pager-count nil))
3575 (term-update-mode-line))
3577 ;; Enable pager processing.
3578 (defun term-pager-enable ()
3580 (or (term-pager-enabled)
3581 (setq term-pager-count 0)) ;; Or maybe set to (term-current-row) ??
3582 (term-update-mode-line))
3584 (defun term-pager-toggle ()
3586 (if (term-pager-enabled) (term-pager-disable) (term-pager-enable)))
3588 (defun term-pager-help ()
3589 "Provide help on commands available in a terminal-emulator **MORE** break."
3591 (message "Terminal-emulator pager break help...")
3594 (function (lambda ()
3595 (princ (substitute-command-keys
3596 "\\<term-pager-break-map>\
3597 Terminal-emulator MORE break.\n\
3598 Type one of the following keys:\n\n\
3599 \\[term-pager-page]\t\tMove forward one page.\n\
3600 \\[term-pager-line]\t\tMove forward one line.\n\
3601 \\[universal-argument] N \\[term-pager-page]\tMove N pages forward.\n\
3602 \\[universal-argument] N \\[term-pager-line]\tMove N lines forward.\n\
3603 \\[universal-argument] N \\[term-pager-back-line]\tMove N lines back.\n\
3604 \\[universal-argument] N \\[term-pager-back-page]\t\tMove N pages back.\n\
3605 \\[term-pager-bob]\t\tMove to the beginning of the buffer.\n\
3606 \\[term-pager-eob]\t\tMove to the end of the buffer.\n\
3607 \\[term-pager-discard]\t\tKill pending output and kill process.\n\
3608 \\[term-pager-disable]\t\tDisable PAGER handling.\n\n\
3609 \\{term-pager-break-map}\n\
3610 Any other key is passed through to the program
3611 running under the terminal emulator and disables pager processing until
3612 all pending output has been dealt with."))
3615 (defun term-pager-continue (new-count)
3616 (let ((process (get-buffer-process (current-buffer))))
3617 (use-local-map term-pager-old-local-map)
3618 (setq term-pager-old-local-map nil)
3619 (setq mode-line-format term-old-mode-line-format)
3620 (force-mode-line-update)
3621 (setq term-pager-count new-count)
3622 (set-process-filter process term-pager-old-filter)
3623 (funcall term-pager-old-filter process "")
3624 (continue-process process)))
3626 ;; Make sure there are DOWN blank lines below the current one.
3627 ;; Return 0 if we're unable (because of PAGER handling), else return DOWN.
3629 (defun term-handle-scroll (down)
3630 (let ((scroll-needed
3631 (- (+ (term-current-row) down)
3632 (if (< down 0) term-scroll-start term-scroll-end))))
3633 (when (or (and (< down 0) (< scroll-needed 0))
3634 (and (> down 0) (> scroll-needed 0)))
3635 (let ((save-point (copy-marker (point))) (save-top))
3636 (goto-char term-home-marker)
3637 (cond (term-scroll-with-delete
3640 ;; Delete scroll-needed lines at term-scroll-end,
3641 ;; then insert scroll-needed lines.
3642 (term-vertical-motion term-scroll-end)
3644 (setq save-top (point))
3645 (term-vertical-motion scroll-needed)
3647 (delete-region save-top (point))
3648 (goto-char save-point)
3649 (setq down (- scroll-needed down))
3650 (term-vertical-motion down))
3651 ;; Delete scroll-needed lines at term-scroll-start.
3652 (term-vertical-motion term-scroll-start)
3653 (setq save-top (point))
3654 (term-vertical-motion scroll-needed)
3655 (delete-region save-top (point))
3656 (goto-char save-point)
3657 (term-vertical-motion down)
3658 (term-adjust-current-row-cache (- scroll-needed)))
3659 (setq term-current-column nil)
3660 (term-insert-char ?\n (abs scroll-needed)))
3661 ((and (numberp term-pager-count)
3662 (< (setq term-pager-count (- term-pager-count down))
3665 (term-process-pager))
3667 (term-adjust-current-row-cache (- scroll-needed))
3668 (term-vertical-motion scroll-needed)
3669 (set-marker term-home-marker (point))))
3670 (goto-char save-point)
3671 (set-marker save-point nil))))
3674 (defun term-down (down &optional check-for-scroll)
3675 "Move down DOWN screen lines vertically."
3676 (let ((start-column (term-horizontal-column)))
3677 (when (and check-for-scroll (or term-scroll-with-delete term-pager-count))
3678 (setq down (term-handle-scroll down)))
3679 (unless (and (= term-current-row 0) (< down 0))
3680 (term-adjust-current-row-cache down)
3681 (when (or (/= (point) (point-max)) (< down 0))
3682 (setq down (- down (term-vertical-motion down)))))
3684 ;; Extend buffer with extra blank lines if needed.
3685 (term-insert-char ?\n down)
3686 (setq term-current-column 0)
3687 (setq term-start-line-column 0))
3689 (when (= term-current-row 0)
3690 ;; Insert lines if at the beginning.
3691 (save-excursion (term-insert-char ?\n (- down)))
3694 ;; Delete lines from the end.
3695 (forward-line term-height)
3697 (forward-line (- down))
3698 (delete-region p (point)))))
3699 (setq term-current-column 0)
3700 (setq term-start-line-column (current-column))))
3702 (term-move-columns start-column))))
3704 ;; Assuming point is at the beginning of a screen line,
3705 ;; if the line above point wraps around, add a ?\n to undo the wrapping.
3706 ;; FIXME: Probably should be called more than it is.
3707 (defun term-unwrap-line ()
3708 (when (not (bolp)) (insert-before-markers ?\n)))
3710 (defun term-erase-in-line (kind)
3711 (when (= kind 1) ;; erase left of point
3712 (let ((cols (term-horizontal-column)) (saved-point (point)))
3713 (term-vertical-motion 0)
3714 (delete-region (point) saved-point)
3715 (term-insert-char ? cols)))
3716 (when (not (eq kind 1)) ;; erase right of point
3717 (let ((saved-point (point))
3718 (wrapped (and (zerop (term-horizontal-column))
3719 (not (zerop (term-current-column))))))
3720 (term-vertical-motion 1)
3721 (delete-region saved-point (point))
3722 ;; wrapped is true if we're at the beginning of screen line,
3723 ;; but not a buffer line. If we delete the current screen line
3724 ;; that will make the previous line no longer wrap, and (because
3725 ;; of the way Emacs display works) point will be at the end of
3726 ;; the previous screen line rather then the beginning of the
3727 ;; current one. To avoid that, we make sure that current line
3728 ;; contain a space, to force the previous line to continue to wrap.
3729 ;; We could do this always, but it seems preferable to not add the
3730 ;; extra space when wrapped is false.
3734 (put-text-property saved-point (point) 'font-lock-face 'default)
3735 (goto-char saved-point))))
3737 (defun term-erase-in-display (kind)
3738 "Erase (that is blank out) part of the window.
3739 If KIND is 0, erase from (point) to (point-max);
3740 if KIND is 1, erase from home to point; else erase from home to point-max."
3741 (term-handle-deferred-scroll)
3742 (cond ((eq term-terminal-parameter 0)
3743 (let ((need-unwrap (bolp)))
3744 (delete-region (point) (point-max))
3745 (when need-unwrap (term-unwrap-line))))
3746 ((let ((row (term-current-row))
3747 (col (term-horizontal-column))
3748 (start-region term-home-marker)
3749 (end-region (if (eq kind 1) (point) (point-max))))
3750 (delete-region start-region end-region)
3753 (term-insert-char ?\n row))
3754 (setq term-current-column nil)
3755 (setq term-current-row nil)
3756 (term-goto row col)))))
3758 (defun term-delete-chars (count)
3759 (let ((save-point (point)))
3760 (term-vertical-motion 1)
3762 (goto-char save-point)
3763 (move-to-column (+ (term-current-column) count) t)
3764 (delete-region save-point (point))))
3766 ;; Insert COUNT spaces after point, but do not change any of
3767 ;; following screen lines. Hence we may have to delete characters
3768 ;; at the end of this screen line to make room.
3770 (defun term-insert-spaces (count)
3771 (let ((save-point (point)) (save-eol) (pnt-at-eol))
3772 (term-vertical-motion 1)
3775 (setq save-eol (point)
3776 pnt-at-eol (line-end-position))
3777 (move-to-column (+ (term-start-line-column) (- term-width count)) t)
3778 ;; If move-to-column extends the current line it will use the face
3779 ;; from the last character on the line, set the face for the chars
3781 (when (>= (point) pnt-at-eol)
3782 (put-text-property pnt-at-eol (point) 'font-lock-face 'default))
3783 (when (> save-eol (point))
3784 (delete-region (point) save-eol))
3785 (goto-char save-point)
3786 (term-insert-char ? count)
3787 (goto-char save-point)))
3789 (defun term-delete-lines (lines)
3790 (let ((start (point))
3791 (save-current-column term-current-column)
3792 (save-start-line-column term-start-line-column)
3793 (save-current-row (term-current-row)))
3794 ;; The number of inserted lines shouldn't exceed the scroll region end.
3795 ;; The `term-scroll-end' line is part of the scrolling region, so
3796 ;; we need to go one line past it in order to ensure correct
3798 (when (> (+ save-current-row lines) (1+ term-scroll-end))
3799 (setq lines (- lines (- (+ save-current-row lines) (1+ term-scroll-end)))))
3801 (delete-region start (point))
3802 (term-down (- (1+ term-scroll-end) save-current-row lines))
3803 (term-insert-char ?\n lines)
3804 (setq term-current-column save-current-column)
3805 (setq term-start-line-column save-start-line-column)
3806 (setq term-current-row save-current-row)
3809 (defun term-insert-lines (lines)
3810 (let ((start (point))
3812 (save-current-column term-current-column)
3813 (save-start-line-column term-start-line-column)
3814 (save-current-row (term-current-row)))
3815 ;; Inserting lines should take into account the scroll region.
3816 ;; The `term-scroll-end' line is part of the scrolling region, so
3817 ;; we need to go one line past it in order to ensure correct
3819 (if (< save-current-row term-scroll-start)
3820 ;; If point is before scroll start,
3822 (setq lines (- lines (- term-scroll-start save-current-row)))
3823 (term-down (- term-scroll-start save-current-row))
3824 (setq start (point)))
3825 ;; The number of inserted lines shouldn't exceed the scroll region end.
3826 (when (> (+ save-current-row lines) (1+ term-scroll-end))
3827 (setq lines (- lines (- (+ save-current-row lines)(1+ term-scroll-end)))))
3828 (term-down (- (1+ term-scroll-end) save-current-row lines)))
3829 (setq start-deleted (point))
3831 (delete-region start-deleted (point))
3833 (setq term-current-column save-current-column)
3834 (setq term-start-line-column save-start-line-column)
3835 (setq term-current-row save-current-row)
3836 (term-insert-char ?\n lines)
3839 (defun term-start-output-log (name)
3840 "Record raw inferior process output in a buffer."
3841 (interactive (list (if term-log-buffer
3843 (read-buffer "Record output in buffer: "
3844 (format "%s output-log"
3845 (buffer-name (current-buffer)))
3847 (if (or (null name) (equal name ""))
3848 (progn (setq term-log-buffer nil)
3849 (message "Output logging off."))
3850 (if (get-buffer name)
3852 (with-current-buffer (get-buffer-create name)
3854 (buffer-disable-undo (current-buffer))
3856 (setq term-log-buffer (get-buffer name))
3857 (message "Recording terminal emulator output into buffer \"%s\""
3858 (buffer-name term-log-buffer))))
3860 (defun term-stop-output-log ()
3861 "Discontinue raw inferior process logging."
3863 (term-start-output-log nil))
3865 (defun term-show-maximum-output ()
3866 "Put the end of the buffer at the bottom of the window."
3868 (goto-char (point-max))
3871 ;;; Do the user's customization...
3873 (defvar term-load-hook nil
3874 "This hook is run when term is loaded in.
3875 This is a good place to put keybindings.")
3877 (run-hooks 'term-load-hook)
3880 ;;; Filename/command/history completion in a buffer
3881 ;; ===========================================================================
3882 ;; Useful completion functions, courtesy of the Ergo group.
3885 ;; term-dynamic-complete Complete or expand command, filename,
3886 ;; history at point.
3887 ;; term-dynamic-complete-filename Complete filename at point.
3888 ;; term-dynamic-list-filename-completions List completions in help buffer.
3889 ;; term-replace-by-expanded-filename Expand and complete filename at point;
3890 ;; replace with expanded/completed name.
3892 ;; These are not installed in the term-mode keymap. But they are
3893 ;; available for people who want them. Shell-mode installs them:
3894 ;; (define-key shell-mode-map "\t" 'term-dynamic-complete)
3895 ;; (define-key shell-mode-map "\M-?"
3896 ;; 'term-dynamic-list-filename-completions)))
3898 ;; Commands like this are fine things to put in load hooks if you
3899 ;; want them present in specific modes.
3901 (defcustom term-completion-autolist nil
3902 "If non-nil, automatically list possibilities on partial completion.
3903 This mirrors the optional behavior of tcsh."
3907 (defcustom term-completion-addsuffix t
3908 "If non-nil, add a `/' to completed directories, ` ' to file names.
3909 If a cons pair, it should be of the form (DIRSUFFIX . FILESUFFIX) where
3910 DIRSUFFIX and FILESUFFIX are strings added on unambiguous or exact
3911 completion. This mirrors the optional behavior of tcsh."
3913 :type '(choice (const :tag "No suffix" nil)
3914 (cons (string :tag "dirsuffix") (string :tag "filesuffix"))
3915 (other :tag "Suffix" t)))
3917 (defcustom term-completion-recexact nil
3918 "If non-nil, use shortest completion if characters cannot be added.
3919 This mirrors the optional behavior of tcsh.
3921 A non-nil value is useful if `term-completion-autolist' is non-nil too."
3925 (defcustom term-completion-fignore nil
3926 "List of suffixes to be disregarded during file completion.
3927 This mirrors the optional behavior of bash and tcsh.
3929 Note that this applies to `term-dynamic-complete-filename' only."
3931 :type '(choice (const nil)
3932 (repeat :tag "List of suffixes" string)))
3934 (defvar term-file-name-prefix ""
3935 "Prefix prepended to absolute file names taken from process input.
3936 This is used by term's and shell's completion functions, and by shell's
3937 directory tracking functions.")
3940 (defun term-directory (directory)
3941 ;; Return expanded DIRECTORY, with `term-file-name-prefix' if absolute.
3942 (expand-file-name (if (file-name-absolute-p directory)
3943 (concat term-file-name-prefix directory)
3947 (defun term-word (word-chars)
3948 "Return the word of WORD-CHARS at point, or nil if none is found.
3949 Word constituents are considered to be those in WORD-CHARS, which is like the
3950 inside of a \"[...]\" (see `skip-chars-forward')."
3952 (let ((limit (point))
3953 (word (concat "[" word-chars "]"))
3954 (non-word (concat "[^" word-chars "]")))
3955 (when (re-search-backward non-word nil 'move)
3957 ;; Anchor the search forwards.
3958 (if (or (eolp) (looking-at non-word))
3960 (re-search-forward (concat word "+") limit)
3961 (buffer-substring (match-beginning 0) (match-end 0))))))
3964 (defun term-match-partial-filename ()
3965 "Return the filename at point, or nil if none is found.
3966 Environment variables are substituted. See `term-word'."
3967 (let ((filename (term-word "~/A-Za-z0-9+@:_.$#,={}-")))
3968 (and filename (substitute-in-file-name filename))))
3971 (defun term-dynamic-complete ()
3972 "Dynamically perform completion at point.
3973 Calls the functions in `term-dynamic-complete-functions' to perform
3974 completion until a function returns non-nil, at which point completion is
3975 assumed to have occurred."
3977 (let ((functions term-dynamic-complete-functions))
3978 (while (and functions (null (funcall (car functions))))
3979 (setq functions (cdr functions)))))
3982 (defun term-dynamic-complete-filename ()
3983 "Dynamically complete the filename at point.
3984 Completes if after a filename. See `term-match-partial-filename' and
3985 `term-dynamic-complete-as-filename'.
3986 This function is similar to `term-replace-by-expanded-filename', except that
3987 it won't change parts of the filename already entered in the buffer; it just
3988 adds completion characters to the end of the filename. A completions listing
3989 may be shown in a help buffer if completion is ambiguous.
3991 Completion is dependent on the value of `term-completion-addsuffix',
3992 `term-completion-recexact' and `term-completion-fignore', and the timing of
3993 completions listing is dependent on the value of `term-completion-autolist'.
3995 Returns t if successful."
3997 (when (term-match-partial-filename)
3998 (prog2 (or (eq (selected-window) (minibuffer-window))
3999 (message "Completing file name..."))
4000 (term-dynamic-complete-as-filename))))
4002 (defun term-dynamic-complete-as-filename ()
4003 "Dynamically complete at point as a filename.
4004 See `term-dynamic-complete-filename'. Returns t if successful."
4005 (let* ((completion-ignore-case nil)
4006 (completion-ignored-extensions term-completion-fignore)
4008 (dirsuffix (cond ((not term-completion-addsuffix) "")
4009 ((not (consp term-completion-addsuffix)) "/")
4010 (t (car term-completion-addsuffix))))
4011 (filesuffix (cond ((not term-completion-addsuffix) "")
4012 ((not (consp term-completion-addsuffix)) " ")
4013 (t (cdr term-completion-addsuffix))))
4014 (filename (or (term-match-partial-filename) ""))
4015 (pathdir (file-name-directory filename))
4016 (pathnondir (file-name-nondirectory filename))
4017 (directory (if pathdir (term-directory pathdir) default-directory))
4018 (completion (file-name-completion pathnondir directory))
4019 (mini-flag (eq (selected-window) (minibuffer-window))))
4020 (cond ((null completion)
4021 (message "No completions of %s" filename)
4023 ((eq completion t) ; Means already completed "file".
4024 (when term-completion-addsuffix (insert " "))
4025 (or mini-flag (message "Sole completion")))
4026 ((string-equal completion "") ; Means completion on "directory/".
4027 (term-dynamic-list-filename-completions))
4028 (t ; Completion string returned.
4029 (let ((file (concat (file-name-as-directory directory) completion)))
4030 (insert (substring (directory-file-name completion)
4031 (length pathnondir)))
4032 (cond ((symbolp (file-name-completion completion directory))
4033 ;; We inserted a unique completion.
4034 (insert (if (file-directory-p file) dirsuffix filesuffix))
4035 (or mini-flag (message "Completed")))
4036 ((and term-completion-recexact term-completion-addsuffix
4037 (string-equal pathnondir completion)
4038 (file-exists-p file))
4039 ;; It's not unique, but user wants shortest match.
4040 (insert (if (file-directory-p file) dirsuffix filesuffix))
4041 (or mini-flag (message "Completed shortest")))
4042 ((or term-completion-autolist
4043 (string-equal pathnondir completion))
4044 ;; It's not unique, list possible completions.
4045 (term-dynamic-list-filename-completions))
4047 (or mini-flag (message "Partially completed")))))))
4051 (defun term-replace-by-expanded-filename ()
4052 "Dynamically expand and complete the filename at point.
4053 Replace the filename with an expanded, canonicalized and completed replacement.
4054 \"Expanded\" means environment variables (e.g., $HOME) and `~'s are replaced
4055 with the corresponding directories. \"Canonicalized\" means `..' and `.' are
4056 removed, and the filename is made absolute instead of relative. For expansion
4057 see `expand-file-name' and `substitute-in-file-name'. For completion see
4058 `term-dynamic-complete-filename'."
4060 (replace-match (expand-file-name (term-match-partial-filename)) t t)
4061 (term-dynamic-complete-filename))
4064 (defun term-dynamic-simple-complete (stub candidates)
4065 "Dynamically complete STUB from CANDIDATES list.
4066 This function inserts completion characters at point by completing STUB from
4067 the strings in CANDIDATES. A completions listing may be shown in a help buffer
4068 if completion is ambiguous.
4070 Returns nil if no completion was inserted.
4071 Returns `sole' if completed with the only completion match.
4072 Returns `shortest' if completed with the shortest of the completion matches.
4073 Returns `partial' if completed as far as possible with the completion matches.
4074 Returns `listed' if a completion listing was shown.
4076 See also `term-dynamic-complete-filename'."
4077 (declare (obsolete completion-in-region "23.2"))
4078 (let* ((completion-ignore-case nil)
4079 (candidates (mapcar (function (lambda (x) (list x))) candidates))
4080 (completions (all-completions stub candidates)))
4081 (cond ((null completions)
4082 (message "No completions of %s" stub)
4084 ((= 1 (length completions)) ; Gotcha!
4085 (let ((completion (car completions)))
4086 (if (string-equal completion stub)
4087 (message "Sole completion")
4088 (insert (substring completion (length stub)))
4089 (message "Completed"))
4090 (when term-completion-addsuffix (insert " "))
4092 (t ; There's no unique completion.
4093 (let ((completion (try-completion stub candidates)))
4094 ;; Insert the longest substring.
4095 (insert (substring completion (length stub)))
4096 (cond ((and term-completion-recexact term-completion-addsuffix
4097 (string-equal stub completion)
4098 (member completion completions))
4099 ;; It's not unique, but user wants shortest match.
4101 (message "Completed shortest")
4103 ((or term-completion-autolist
4104 (string-equal stub completion))
4105 ;; It's not unique, list possible completions.
4106 (term-dynamic-list-completions completions)
4109 (message "Partially completed")
4112 (defun term-dynamic-list-filename-completions ()
4113 "List in help buffer possible completions of the filename at point."
4115 (let* ((completion-ignore-case nil)
4116 (filename (or (term-match-partial-filename) ""))
4117 (pathdir (file-name-directory filename))
4118 (pathnondir (file-name-nondirectory filename))
4119 (directory (if pathdir (term-directory pathdir) default-directory))
4120 (completions (file-name-all-completions pathnondir directory)))
4122 (term-dynamic-list-completions completions)
4123 (message "No completions of %s" filename))))
4126 (defun term-dynamic-list-completions (completions)
4127 "List in help buffer sorted COMPLETIONS.
4128 Typing SPC flushes the help buffer."
4129 (let ((conf (current-window-configuration)))
4130 (with-output-to-temp-buffer "*Completions*"
4131 (display-completion-list (sort completions 'string-lessp)))
4132 (message "Hit space to flush")
4134 (if (with-current-buffer (get-buffer "*Completions*")
4135 (setq key (read-key-sequence nil)
4138 (eq (window-buffer (posn-window (event-start first)))
4139 (get-buffer "*Completions*"))
4140 (memq (key-binding key)
4141 '(mouse-choose-completion choose-completion))))
4142 ;; If the user does choose-completion with the mouse,
4143 ;; execute the command, then delete the completion window.
4145 (choose-completion first)
4146 (set-window-configuration conf))
4148 (set-window-configuration conf)
4149 (setq unread-command-events (listify-key-sequence key)))))))
4151 ;; I need a make-term that doesn't surround with *s -mm
4152 (defun term-ansi-make-term (name program &optional startfile &rest switches)
4153 "Make a term process NAME in a buffer, running PROGRAM.
4154 The name of the buffer is NAME.
4155 If there is already a running process in that buffer, it is not restarted.
4156 Optional third arg STARTFILE is the name of a file to send the contents of to
4157 the process. Any more args are arguments to PROGRAM."
4158 (let ((buffer (get-buffer-create name )))
4159 ;; If no process, or nuked process, crank up a new one and put buffer in
4160 ;; term mode. Otherwise, leave buffer and existing process alone.
4161 (cond ((not (term-check-proc buffer))
4162 (with-current-buffer buffer
4163 (term-mode)) ; Install local vars, mode, keymap, ...
4164 (term-exec buffer name program startfile switches)))
4167 (defvar term-ansi-buffer-name nil)
4168 (defvar term-ansi-default-program nil)
4169 (defvar term-ansi-buffer-base-name nil)
4172 (defun ansi-term (program &optional new-buffer-name)
4173 "Start a terminal-emulator in a new buffer."
4174 (interactive (list (read-from-minibuffer "Run program: "
4175 (or explicit-shell-file-name
4180 ;; Pick the name of the new buffer.
4181 (setq term-ansi-buffer-name
4184 (if term-ansi-buffer-base-name
4185 (if (eq term-ansi-buffer-base-name t)
4186 (file-name-nondirectory program)
4187 term-ansi-buffer-base-name)
4190 (setq term-ansi-buffer-name (concat "*" term-ansi-buffer-name "*"))
4192 ;; In order to have more than one term active at a time
4193 ;; I'd like to have the term names have the *term-ansi-term<?>* form,
4194 ;; for now they have the *term-ansi-term*<?> form but we'll see...
4196 (setq term-ansi-buffer-name (generate-new-buffer-name term-ansi-buffer-name))
4197 (setq term-ansi-buffer-name (term-ansi-make-term term-ansi-buffer-name program))
4199 (set-buffer term-ansi-buffer-name)
4203 ;; Historical baggage. A call to term-set-escape-char used to not
4204 ;; undo any previous call to t-s-e-c. Because of this, ansi-term
4205 ;; ended up with both C-x and C-c as escape chars. Who knows what
4206 ;; the original intention was, but people could have become used to
4207 ;; either. (Bug#12842)
4208 (let (term-escape-char)
4209 ;; I wanna have find-file on C-x C-f -mm
4210 ;; your mileage may definitely vary, maybe it's better to put this in your
4212 (term-set-escape-char ?\C-x))
4214 (switch-to-buffer term-ansi-buffer-name))
4217 ;;; Serial terminals
4218 ;; ===========================================================================
4219 (defun serial-port-is-file-p ()
4220 "Guess whether serial ports are files on this system.
4221 Return t if this is a Unix-based system, where serial ports are
4222 files, such as /dev/ttyS0.
4223 Return nil if this is Windows or DOS, where serial ports have
4224 special identifiers such as COM1."
4225 (not (memq system-type '(windows-nt cygwin ms-dos))))
4227 (defvar serial-name-history
4228 (if (serial-port-is-file-p)
4229 (or (when (file-exists-p "/dev/ttys0") (list "/dev/ttys0"))
4230 (when (file-exists-p "/dev/ttyS0") (list "/dev/ttyS0")))
4232 "History of serial ports used by `serial-read-name'.")
4234 (defvar serial-speed-history
4235 ;; Initialized with reasonable values for newbies.
4236 (list "9600" ;; Given twice because 9600 b/s is the most common speed
4237 "1200" "2400" "4800" "9600" "14400" "19200"
4238 "28800" "38400" "57600" "115200")
4239 "History of serial port speeds used by `serial-read-speed'.")
4241 (defun serial-nice-speed-history ()
4242 "Return `serial-speed-history' cleaned up for a mouse-menu."
4246 (copy-sequence serial-speed-history)
4247 (lambda (a b) (when (and (stringp a) (stringp b))
4248 (> (string-to-number a) (string-to-number b))))))
4249 (dolist (i x) (when (not (equal i (car y))) (push i y)))
4252 (defconst serial-no-speed "nil"
4253 "String for `serial-read-speed' for special serial ports.
4254 If `serial-read-speed' reads this string from the user, it
4255 returns nil, which is recognized by `serial-process-configure'
4256 for special serial ports that cannot be configured.")
4258 (defun serial-supported-or-barf ()
4259 "Signal an error if serial processes are not supported."
4260 (unless (fboundp 'make-serial-process)
4261 (error "Serial processes are not supported on this system")))
4263 (defun serial-read-name ()
4264 "Read a serial port name from the user.
4265 Try to be nice by providing useful defaults and history.
4266 On Windows, prepend \\.\ to the port name unless it already
4267 contains a backslash. This handles the legacy ports COM1-COM9 as
4268 well as the newer ports COM10 and higher."
4269 (serial-supported-or-barf)
4270 (let* ((file-name-history serial-name-history)
4271 (h (car file-name-history))
4272 (x (if (serial-port-is-file-p)
4274 ;; `prompt': The most recently used port is provided as
4275 ;; the default value, which is used when the user
4276 ;; simply presses return.
4277 (if (stringp h) (format "Serial port (default %s): " h)
4279 ;; `directory': Most systems have their serial ports
4280 ;; in the same directory, so start in the directory
4281 ;; of the most recently used port, or in a reasonable
4282 ;; default directory.
4283 (or (and h (file-name-directory h))
4284 (and (file-exists-p "/dev/") "/dev/")
4285 (and (file-exists-p "/") "/"))
4286 ;; `default': This causes (read-file-name) to return
4287 ;; the empty string if he user simply presses return.
4288 ;; Using nil here may result in a default directory
4289 ;; of the current buffer, which is not useful for
4292 (read-from-minibuffer
4293 (if (stringp h) (format "Serial port (default %s): " h)
4295 nil nil nil '(file-name-history . 1) nil nil))))
4296 (if (or (null x) (and (stringp x) (zerop (length x))))
4298 (setq serial-name-history file-name-history))
4299 (when (or (null x) (and (stringp x) (zerop (length x))))
4300 (error "No serial port selected"))
4301 (when (and (not (serial-port-is-file-p))
4302 (not (string-match "\\\\" x)))
4303 (set 'x (concat "\\\\.\\" x)))
4306 (defun serial-read-speed ()
4307 "Read a serial port speed (in bits per second) from the user.
4308 Try to be nice by providing useful defaults and history."
4309 (serial-supported-or-barf)
4310 (let* ((history serial-speed-history)
4312 (x (read-from-minibuffer
4313 (cond ((string= h serial-no-speed)
4314 "Speed (default nil = set by port): ")
4316 (format "Speed (default %s b/s): " h))
4318 (format "Speed (b/s): ")))
4319 nil nil nil '(history . 1) nil nil)))
4320 (when (or (null x) (and (stringp x) (zerop (length x))))
4322 (when (or (null x) (not (stringp x)) (zerop (length x)))
4323 (error "Invalid speed"))
4324 (if (string= x serial-no-speed)
4326 (setq x (string-to-number x))
4327 (when (or (null x) (not (integerp x)) (<= x 0))
4328 (error "Invalid speed")))
4329 (setq serial-speed-history history)
4333 (defun serial-term (port speed)
4334 "Start a terminal-emulator for a serial port in a new buffer.
4335 PORT is the path or name of the serial port. For example, this
4336 could be \"/dev/ttyS0\" on Unix. On Windows, this could be
4337 \"COM1\" or \"\\\\.\\COM10\".
4338 SPEED is the speed of the serial port in bits per second. 9600
4339 is a common value. SPEED can be nil, see
4340 `serial-process-configure' for details.
4341 The buffer is in Term mode; see `term-mode' for the commands to
4343 \\<term-raw-map>Type \\[switch-to-buffer] to switch to another buffer."
4344 (interactive (list (serial-read-name) (serial-read-speed)))
4345 (serial-supported-or-barf)
4346 (let* ((process (make-serial-process
4349 :coding 'no-conversion
4351 (buffer (process-buffer process)))
4352 (with-current-buffer buffer
4355 (goto-char (point-max))
4356 (set-marker (process-mark process) (point))
4357 (set-process-filter process 'term-emulate-terminal)
4358 (set-process-sentinel process 'term-sentinel))
4359 (switch-to-buffer buffer)
4362 (defvar serial-mode-line-speed-menu nil)
4363 (defvar serial-mode-line-config-menu nil)
4365 (defun serial-speed ()
4366 "Return the speed of the serial port of the current buffer's process.
4367 The return value may be nil for a special serial port."
4368 (process-contact (get-buffer-process (current-buffer)) :speed))
4370 (defun serial-mode-line-speed-menu-1 (event)
4372 (save-selected-window
4373 (select-window (posn-window (event-start event)))
4374 (serial-update-speed-menu)
4375 (let* ((selection (serial-mode-line-speed-menu event))
4376 (binding (and selection (lookup-key serial-mode-line-speed-menu
4377 (vector (car selection))))))
4378 (when binding (call-interactively binding)))))
4380 (defun serial-mode-line-speed-menu (event)
4381 (x-popup-menu event serial-mode-line-speed-menu))
4383 (defun serial-update-speed-menu ()
4384 (setq serial-mode-line-speed-menu (make-sparse-keymap "Speed (b/s)"))
4385 (define-key serial-mode-line-speed-menu [serial-mode-line-speed-menu-other]
4386 '(menu-item "Other..."
4387 (lambda (event) (interactive "e")
4388 (let ((speed (serial-read-speed)))
4389 (serial-process-configure :speed speed)
4390 (term-update-mode-line)
4391 (message "Speed set to %d b/s" speed)))))
4392 (dolist (str (serial-nice-speed-history))
4393 (let ((num (or (and (stringp str) (string-to-number str)) 0)))
4395 serial-mode-line-speed-menu
4396 (vector (make-symbol (format "serial-mode-line-speed-menu-%s" str)))
4399 (lambda (event) (interactive "e")
4400 (serial-process-configure :speed ,num)
4401 (term-update-mode-line)
4402 (message "Speed set to %d b/s" ,num))
4403 :button (:toggle . (= (serial-speed) ,num)))))))
4405 (defun serial-mode-line-config-menu-1 (event)
4407 (save-selected-window
4408 (select-window (posn-window (event-start event)))
4409 (serial-update-config-menu)
4410 (let* ((selection (serial-mode-line-config-menu event))
4411 (binding (and selection (lookup-key serial-mode-line-config-menu
4412 (vector (car selection))))))
4413 (when binding (call-interactively binding)))))
4415 (defun serial-mode-line-config-menu (event)
4416 (x-popup-menu event serial-mode-line-config-menu))
4418 (defun serial-update-config-menu ()
4419 (setq serial-mode-line-config-menu (make-sparse-keymap "Configuration"))
4420 (let ((config (process-contact
4421 (get-buffer-process (current-buffer)) t)))
4422 (dolist (y '((:flowcontrol hw "Hardware flowcontrol (RTS/CTS)")
4423 (:flowcontrol sw "Software flowcontrol (XON/XOFF)")
4424 (:flowcontrol nil "No flowcontrol")
4425 (:stopbits 2 "2 stopbits")
4426 (:stopbits 1 "1 stopbit")
4427 (:parity odd "Odd parity")
4428 (:parity even "Even parity")
4429 (:parity nil "No parity")
4430 (:bytesize 7 "7 bits per byte")
4431 (:bytesize 8 "8 bits per byte")))
4432 (define-key serial-mode-line-config-menu
4433 (vector (make-symbol (format "%s-%s" (nth 0 y) (nth 1 y))))
4436 (lambda (event) (interactive "e")
4437 (serial-process-configure ,(nth 0 y) ',(nth 1 y))
4438 (term-update-mode-line)
4439 (message "%s" ,(nth 2 y)))
4440 ;; Use :toggle instead of :radio because a non-standard port
4441 ;; configuration may not match any menu items.
4442 :button (:toggle . ,(equal (plist-get config (nth 0 y))
4446 ;;; Converting process modes to use term mode
4447 ;; ===========================================================================
4448 ;; Renaming variables
4449 ;; Most of the work is renaming variables and functions. These are the common
4452 ;; last-input-start term-last-input-start
4453 ;; last-input-end term-last-input-end
4454 ;; shell-prompt-pattern term-prompt-regexp
4455 ;; shell-set-directory-error-hook <no equivalent>
4457 ;; shell-set-directory <unnecessary>
4458 ;; shell-mode-map term-mode-map
4460 ;; shell-send-input term-send-input
4461 ;; shell-send-eof term-delchar-or-maybe-eof
4462 ;; kill-shell-input term-kill-input
4463 ;; interrupt-shell-subjob term-interrupt-subjob
4464 ;; stop-shell-subjob term-stop-subjob
4465 ;; quit-shell-subjob term-quit-subjob
4466 ;; kill-shell-subjob term-kill-subjob
4467 ;; kill-output-from-shell term-kill-output
4468 ;; show-output-from-shell term-show-output
4469 ;; copy-last-shell-input Use term-previous-input/term-next-input
4471 ;; SHELL-SET-DIRECTORY is gone, its functionality taken over by
4472 ;; SHELL-DIRECTORY-TRACKER, the shell mode's term-input-filter-functions.
4473 ;; Term mode does not provide functionality equivalent to
4474 ;; shell-set-directory-error-hook; it is gone.
4476 ;; term-last-input-start is provided for modes which want to munge
4477 ;; the buffer after input is sent, perhaps because the inferior
4478 ;; insists on echoing the input. The LAST-INPUT-START variable in
4479 ;; the old shell package was used to implement a history mechanism,
4480 ;; but you should think twice before using term-last-input-start
4481 ;; for this; the input history ring often does the job better.
4483 ;; If you are implementing some process-in-a-buffer mode, called foo-mode, do
4484 ;; *not* create the term-mode local variables in your foo-mode function.
4485 ;; This is not modular. Instead, call term-mode, and let *it* create the
4486 ;; necessary term-specific local variables. Then create the
4487 ;; foo-mode-specific local variables in foo-mode. Set the buffer's keymap to
4488 ;; be foo-mode-map, and its mode to be foo-mode. Set the term-mode hooks
4489 ;; (term-{prompt-regexp, input-filter, input-filter-functions,
4490 ;; get-old-input) that need to be different from the defaults. Call
4491 ;; foo-mode-hook, and you're done. Don't run the term-mode hook yourself;
4492 ;; term-mode will take care of it. The following example, from shell.el,
4495 ;; (defvar shell-mode-map '())
4496 ;; (cond ((not shell-mode-map)
4497 ;; (setq shell-mode-map (copy-keymap term-mode-map))
4498 ;; (define-key shell-mode-map "\C-c\C-f" 'shell-forward-command)
4499 ;; (define-key shell-mode-map "\C-c\C-b" 'shell-backward-command)
4500 ;; (define-key shell-mode-map "\t" 'term-dynamic-complete)
4501 ;; (define-key shell-mode-map "\M-?"
4502 ;; 'term-dynamic-list-filename-completions)))
4504 ;; (defun shell-mode ()
4507 ;; (setq term-prompt-regexp shell-prompt-pattern)
4508 ;; (setq major-mode 'shell-mode)
4509 ;; (setq mode-name "Shell")
4510 ;; (use-local-map shell-mode-map)
4511 ;; (make-local-variable 'shell-directory-stack)
4512 ;; (setq shell-directory-stack nil)
4513 ;; (add-hook 'term-input-filter-functions 'shell-directory-tracker)
4514 ;; (run-mode-hooks 'shell-mode-hook))
4517 ;; Completion for term-mode users
4519 ;; For modes that use term-mode, term-dynamic-complete-functions is the
4520 ;; hook to add completion functions to. Functions on this list should return
4521 ;; non-nil if completion occurs (i.e., further completion should not occur).
4522 ;; You could use completion-in-region to do the bulk of the
4527 ;;; term.el ends here