annotate lisp/comint.el @ 445:2a2230dd1b1c

*** empty log message ***
author Jim Blandy <jimb@redhat.com>
date Thu, 05 Dec 1991 23:21:45 +0000
parents 35ea4993d08c
children 4cd7543be581
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
114
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
1 ;;; -*-Emacs-Lisp-*- General command interpreter in a window stuff
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
2 ;;; Copyright (C) 1989 Free Software Foundation, Inc.
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
3 ;;; Original author: Olin Shivers <olin.shivers@cs.cmu.edu> Aug 1988
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
4
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
5 ;;; This file is part of GNU Emacs.
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
6
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
7 ;;; GNU Emacs is free software; you can redistribute it and/or modify
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
8 ;;; it under the terms of the GNU General Public License as published by
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
9 ;;; the Free Software Foundation; either version 1, or (at your option)
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
10 ;;; any later version.
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
11
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
12 ;;; GNU Emacs is distributed in the hope that it will be useful,
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
13 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
14 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
15 ;;; GNU General Public License for more details.
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
16
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
17 ;;; You should have received a copy of the GNU General Public License
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
18 ;;; along with GNU Emacs; see the file COPYING. If not, write to
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
19 ;;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
20
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
21 ;;; This file defines a general command-interpreter-in-a-buffer package
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
22 ;;; (comint mode). The idea is that you can build specific process-in-a-buffer
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
23 ;;; modes on top of comint mode -- e.g., lisp, shell, scheme, T, soar, ....
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
24 ;;; This way, all these specific packages share a common base functionality,
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
25 ;;; and a common set of bindings, which makes them easier to use (and
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
26 ;;; saves code, implementation time, etc., etc.).
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
27
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
28 ;;; For documentation on the functionality provided by comint mode, and
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
29 ;;; the hooks available for customising it, see the comments below.
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
30 ;;; For further information on the standard derived modes (shell,
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
31 ;;; inferior-lisp, inferior-scheme, ...), see the relevant source files.
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
32
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
33 ;;; For hints on converting existing process modes to use comint-mode
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
34 ;;; instead of shell-mode, see the notes at the end of this file.
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
35
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
36 (require 'history)
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
37 (provide 'comint)
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
38 (defconst comint-version "2.01")
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
39
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
40
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
41 ;;; Not bound by default in comint-mode
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
42 ;;; send-invisible Read a line w/o echo, and send to proc
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
43 ;;; (These are bound in shell-mode)
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
44 ;;; comint-dynamic-complete Complete filename at point.
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
45 ;;; comint-dynamic-list-completions List completions in help buffer.
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
46 ;;; comint-replace-by-expanded-filename Expand and complete filename at point;
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
47 ;;; replace with expanded/completed name.
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
48 (defvar comint-mode-map nil)
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
49
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
50 (if comint-mode-map
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
51 nil
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
52 (setq comint-mode-map (make-sparse-keymap))
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
53 (define-key comint-mode-map "\C-a" 'comint-bol)
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
54 (define-key comint-mode-map "\C-d" 'comint-delchar-or-maybe-eof)
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
55 (define-key comint-mode-map "\C-m" 'comint-send-input)
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
56 (define-key comint-mode-map "\M-p" 'comint-previous-input)
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
57 (define-key comint-mode-map "\M-n" 'comint-next-input)
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
58 (define-key comint-mode-map "\M-s" 'comint-previous-similar-input)
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
59 (define-key comint-mode-map "\C-c\C-c" 'comint-interrupt-subjob) ; tty ^C
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
60 (define-key comint-mode-map "\C-c\C-f" 'comint-continue-subjob) ; shell "fg"
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
61 (define-key comint-mode-map "\C-c\C-l" 'comint-show-output)
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
62 (define-key comint-mode-map "\C-c\C-o" 'comint-flush-output) ; tty ^O
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
63 (define-key comint-mode-map "\C-c\C-r" 'comint-history-search-backward)
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
64 (define-key comint-mode-map "\C-c\C-s" 'comint-history-search-forward)
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
65 (define-key comint-mode-map "\C-c\C-u" 'comint-kill-input) ; tty ^U
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
66 (define-key comint-mode-map "\C-c\C-w" 'backward-kill-word) ; tty ^W
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
67 (define-key comint-mode-map "\C-c\C-z" 'comint-stop-subjob) ; tty ^Z
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
68 (define-key comint-mode-map "\C-c\C-\\" 'comint-quit-subjob)) ; tty ^\
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
69
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
70 ;;; Buffer Local Variables:
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
71 ;;;============================================================================
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
72 ;;; Comint mode buffer local variables:
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
73 ;;; comint-prompt-regexp - string comint-bol uses to match prompt.
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
74 ;;; comint-last-input-end - marker For comint-flush-output command
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
75 ;;; input-ring-size - integer For the input history
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
76 ;;; input-ring - ring mechanism
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
77 ;;; input-ring-index - marker ...
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
78 ;;; comint-last-input-match - string ...
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
79 ;;; comint-get-old-input - function Hooks for specific
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
80 ;;; comint-input-sentinel - function process-in-a-buffer
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
81 ;;; comint-input-filter - function modes.
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
82 ;;; comint-input-send - function
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
83 ;;; comint-eol-on-send - boolean
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
84
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
85 (make-variable-buffer-local
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
86 (defvar comint-prompt-regexp "^"
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
87 "*Regexp to recognise prompts in the inferior process. Defaults to \"^\".
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
88
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
89 Good choices:
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
90 Canonical Lisp: \"^[^> \n]*>+:? *\" (Lucid, Franz, KCL, T, cscheme, oaklisp)
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
91 Lucid Common Lisp: \"^\\(>\\|\\(->\\)+\\) *\"
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
92 Franz: \"^\\(->\\|<[0-9]*>:\\) *\"
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
93 KCL and T: \"^>+ *\"
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
94 shell: \"^[^#$%>\n]*[#$%>] *\"
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
95
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
96 This is a good thing to set in mode hooks."))
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
97
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
98 (make-variable-buffer-local
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
99 (defvar input-ring-size 30 "Size of input history ring."))
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
100
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
101 ;;; Here are the per-interpreter hooks.
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
102 (make-variable-buffer-local
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
103 (defvar comint-get-old-input (function comint-get-old-input-default)
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
104 "Function that submits old text in comint mode.
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
105 This function is called when return is typed while the point is in old text.
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
106 It returns the text to be submitted as process input. The default is
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
107 comint-get-old-input-default, which grabs the current line, and strips off
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
108 leading text matching comint-prompt-regexp."))
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
109
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
110 (make-variable-buffer-local
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
111 (defvar comint-input-sentinel (function ignore)
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
112 "Called on each input submitted to comint mode process by comint-send-input.
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
113 Thus it can, for instance, track cd/pushd/popd commands issued to the csh."))
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
114
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
115 (make-variable-buffer-local
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
116 (defvar comint-input-filter
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
117 (function (lambda (str) (not (string-match "\\`\\s *\\'" str))))
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
118 "Predicate for filtering additions to input history.
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
119 Only inputs answering true to this function are saved on the input
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
120 history list. Default is to save anything that isn't all whitespace"))
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
121
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
122 (defvar comint-mode-hook '()
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
123 "Called upon entry into comint-mode")
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
124
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
125 (defun comint-mode ()
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
126 "Major mode for interacting with an inferior interpreter.
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
127 Interpreter name is same as buffer name, sans the asterisks.
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
128 Return at end of buffer sends line as input.
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
129 Return not at end copies rest of line to end and sends it.
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
130
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
131 This mode is typically customised to create inferior-lisp-mode,
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
132 shell-mode, et cetera. This can be done by setting the hooks
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
133 comint-input-sentinel, comint-input-filter, and comint-get-old-input
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
134 to appropriate functions, and the variable comint-prompt-regexp
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
135 to the appropriate regular expression.
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
136
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
137 An input history is maintained of size input-ring-size, and
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
138 can be accessed with the commands comint-next-input [\\[comint-next-input]] and
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
139 comint-previous-input [\\[comint-previous-input]]. Commands not keybound by
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
140 default are send-invisible, comint-dynamic-complete, and
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
141 comint-list-dynamic-completions.
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
142
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
143 If you accidentally suspend your process, use \\[comint-continue-subjob]
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
144 to continue it.
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
145
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
146 \\{comint-mode-map}
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
147
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
148 Entry to this mode runs the hooks on comint-mode-hook."
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
149 (interactive)
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
150 (kill-all-local-variables)
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
151 (setq major-mode 'comint-mode
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
152 mode-name "Comint"
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
153 mode-line-process '(": %s"))
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
154 (use-local-map comint-mode-map)
182
35ea4993d08c *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 114
diff changeset
155 (set (make-local-variable 'input-ring) (make-ring input-ring-size))
35ea4993d08c *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 114
diff changeset
156 (put 'input-ring 'preserved t)
114
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
157 (set (make-local-variable 'comint-last-input-match) "")
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
158 (set (make-local-variable 'comint-last-similar--string) "")
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
159 (set (make-local-variable 'input-ring-index) 0)
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
160 (set (make-local-variable 'comint-last-input-end) (make-marker))
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
161 (set-marker comint-last-input-end (point-max))
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
162 (run-hooks 'comint-mode-hook))
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
163
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
164 (defun comint-check-proc (buffer-name)
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
165 "True if there is a running or stopped process associated with BUFFER."
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
166 (let ((proc (get-buffer-process buffer-name)))
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
167 (and proc (memq (process-status proc) '(run stop)))))
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
168
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
169 (defun comint-mark ()
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
170 ;; Returns the process-mark of the current-buffer
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
171 (process-mark (get-buffer-process (current-buffer))))
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
172
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
173 ;;; Note that this guy, unlike shell.el's make-shell, barfs if you pass it ()
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
174 ;;; for the second argument (program).
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
175 (defun make-comint (name program &optional startfile &rest switches)
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
176 (let* ((buffer (get-buffer-create (concat "*" name "*")))
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
177 (proc (get-buffer-process buffer)))
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
178 ;; If no process, or nuked process, crank up a new one and put buffer in
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
179 ;; comint mode. Otherwise, leave buffer and existing process alone.
182
35ea4993d08c *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 114
diff changeset
180 (cond ((not (comint-check-proc buffer))
114
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
181 (save-excursion
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
182 (set-buffer buffer)
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
183 (comint-mode)) ; Install local vars, mode, keymap, ...
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
184 (comint-exec buffer name program startfile switches)))
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
185 buffer))
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
186
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
187 (defun comint-exec (buffer name command startfile switches)
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
188 "Fires up a process in buffer for comint modes.
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
189 Blasts any old process running in the buffer. Doesn't set the buffer mode.
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
190 You can use this to cheaply run a series of processes in the same buffer."
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
191 (or command (error "No program for comint process"))
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
192 (save-excursion
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
193 (set-buffer buffer)
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
194 (let ((proc (get-buffer-process buffer))) ; Blast any old process.
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
195 (if proc (delete-process proc)))
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
196 ;; Crank up a new process
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
197 (let ((proc (comint-exec-1 name buffer command switches)))
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
198 ;; Jump to the end, and set the process mark.
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
199 (set-marker (comint-mark) (goto-char (point-max)))
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
200 ;; Feed it the startfile.
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
201 (cond (startfile
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
202 ;;This is guaranteed to wait long enough
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
203 ;;but has bad results if the comint does not prompt at all
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
204 ;; (while (= size (buffer-size))
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
205 ;; (sleep-for 1))
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
206 ;;I hope 1 second is enough!
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
207 (sleep-for 1)
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
208 (goto-char (point-max))
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
209 (insert-file-contents startfile)
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
210 (setq startfile (buffer-substring (point) (point-max)))
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
211 (delete-region (point) (point-max))
182
35ea4993d08c *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 114
diff changeset
212 (comint-send-string proc startfile))))
114
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
213 buffer))
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
214
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
215 ;;; This auxiliary function cranks up the process for comint-exec in
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
216 ;;; the appropriate environment. It is twice as long as it should be
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
217 ;;; because emacs has two distinct mechanisms for manipulating the
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
218 ;;; process environment, selected at compile time with the
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
219 ;;; MAINTAIN-ENVIRONMENT #define. In one case, process-environment
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
220 ;;; is bound; in the other it isn't.
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
221
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
222 (defun comint-exec-1 (name buffer command switches)
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
223 (if (boundp 'process-environment) ; Not a completely reliable test.
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
224 (let ((process-environment
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
225 (comint-update-env process-environment
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
226 (list (format "TERMCAP=emacs:co#%d:tc=unknown"
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
227 (screen-width))
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
228 "TERM=emacs"
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
229 "EMACS=t"))))
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
230 (apply 'start-process name buffer command switches))
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
231 (let ((tcapv (getenv "TERMCAP"))
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
232 (termv (getenv "TERM"))
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
233 (emv (getenv "EMACS")))
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
234 (unwind-protect
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
235 (progn (setenv "TERMCAP" (format "emacs:co#%d:tc=unknown"
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
236 (screen-width)))
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
237 (setenv "TERM" "emacs")
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
238 (setenv "EMACS" "t")
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
239 (apply 'start-process name buffer command switches))
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
240 (setenv "TERMCAP" tcapv)
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
241 (setenv "TERM" termv)
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
242 (setenv "EMACS" emv)))))
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
243
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
244 ;; This is just (append new old-env) that compresses out shadowed entries.
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
245 ;; It's also pretty ugly, mostly due to elisp's horrible iteration structures.
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
246 (defun comint-update-env (old-env new)
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
247 (let ((ans (reverse new))
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
248 (vars (mapcar (function (lambda (vv)
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
249 (and (string-match "^[^=]*=" vv)
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
250 (substring vv 0 (match-end 0)))))
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
251 new)))
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
252 (while old-env
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
253 (let* ((vv (car old-env)) ; vv is var=value
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
254 (var (and (string-match "^[^=]*=" vv)
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
255 (substring vv 0 (match-end 0)))))
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
256 (setq old-env (cdr old-env))
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
257 (cond ((not (and var (member var vars)))
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
258 (if var (setq var (cons var vars)))
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
259 (setq ans (cons vv ans))))))
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
260 (nreverse ans)))
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
261
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
262 ;;; Input history retrieval commands
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
263 ;;; M-p -- previous input M-n -- next input
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
264 ;;; C-c r -- previous input matching
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
265 ;;; ===========================================================================
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
266
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
267 (defun comint-previous-input (arg)
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
268 "Cycle backwards through input history."
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
269 (interactive "*p")
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
270 (let ((len (ring-length input-ring)))
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
271 (if (<= len 0) (error "Empty input ring"))
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
272 (if (< (point) (comint-mark))
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
273 (delete-region (comint-mark) (goto-char (point-max))))
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
274 (cond ((eq last-command 'comint-previous-input)
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
275 (delete-region (mark) (point)))
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
276 ((eq last-command 'comint-previous-similar-input)
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
277 (delete-region (comint-mark) (point)))
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
278 (t
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
279 (setq input-ring-index
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
280 (if (> arg 0) -1
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
281 (if (< arg 0) 1 0)))
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
282 (push-mark (point))))
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
283 (setq input-ring-index (comint-mod (+ input-ring-index arg) len))
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
284 (message "%d" (1+ input-ring-index))
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
285 (insert (ring-ref input-ring input-ring-index))
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
286 (setq this-command 'comint-previous-input)))
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
287
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
288 (defun comint-next-input (arg)
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
289 "Cycle forwards through input history."
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
290 (interactive "*p")
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
291 (comint-previous-input (- arg)))
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
292
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
293 (defun comint-previous-input-matching (str)
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
294 "Searches backwards through input history for substring match."
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
295 (interactive (let* ((last-command last-command) ; preserve around r-f-m
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
296 (s (read-from-minibuffer
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
297 (format "Command substring (default %s): "
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
298 comint-last-input-match))))
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
299 (list (if (string= s "") comint-last-input-match s))))
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
300 ; (interactive "sCommand substring: ")
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
301 (setq comint-last-input-match str) ; update default
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
302 (if (not (eq last-command 'comint-previous-input))
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
303 (setq input-ring-index -1))
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
304 (let ((str (regexp-quote str))
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
305 (len (ring-length input-ring))
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
306 (n (+ input-ring-index 1)))
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
307 (while (and (< n len) (not (string-match str (ring-ref input-ring n))))
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
308 (setq n (+ n 1)))
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
309 (cond ((< n len)
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
310 (comint-previous-input (- n input-ring-index)))
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
311 (t (if (eq last-command 'comint-previous-input)
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
312 (setq this-command 'comint-previous-input))
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
313 (error "Not found")))))
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
314
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
315 ;;;
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
316 ;;; Similar input -- contributed by ccm and highly winning.
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
317 ;;;
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
318 ;;; Reenter input, removing back to the last insert point if it exists.
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
319 ;;;
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
320 (defun comint-previous-similar-input (arg)
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
321 "Reenters the last input that matches the string typed so far. If repeated
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
322 successively older inputs are reentered. If arg is 1, it will go back
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
323 in the history, if -1 it will go forward."
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
324 (interactive "p")
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
325 (if (< (point) (comint-mark))
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
326 (error "Not after process mark"))
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
327 (if (not (eq last-command 'comint-previous-similar-input))
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
328 (setq input-ring-index -1
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
329 comint-last-similar-string
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
330 (buffer-substring (comint-mark) (point))))
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
331 (let* ((size (length comint-last-similar-string))
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
332 (len (ring-length input-ring))
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
333 (n (+ input-ring-index arg))
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
334 entry)
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
335 (while (and (< n len)
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
336 (or (< (length (setq entry (ring-ref input-ring n))) size)
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
337 (not (equal comint-last-similar-string
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
338 (substring entry 0 size)))))
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
339 (setq n (+ n arg)))
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
340 (cond ((< n len)
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
341 (setq input-ring-index n)
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
342 (if (eq last-command 'comint-previous-similar-input)
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
343 (delete-region (comint-mark) (point)))
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
344 (insert (substring entry size)))
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
345 (t (error "Not found")))))
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
346
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
347 (defun comint-send-input (&optional terminator delete)
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
348 "Send input to process, followed by a linefeed or optional TERMINATOR.
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
349 After the process output mark, sends all text from the process mark to
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
350 end of buffer as input to the process. Before the process output mark, calls
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
351 value of variable comint-get-old-input to retrieve old input, replaces it in
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
352 the input region (from the end of process output to the end of the buffer) and
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
353 then sends it. In either case, the value of variable comint-input-sentinel is
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
354 called on the input before sending it. The input is entered into the input
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
355 history ring, if value of variable comint-input-filter returns non-nil when
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
356 called on the input.
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
357
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
358 If optional second argument DELETE is non-nil, then the input is deleted from
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
359 the end of the buffer. This is useful if the process unconditionally echoes
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
360 input. Processes which use TERMINATOR or DELETE should have a command wrapper
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
361 which provides them bound to RET; see telnet.el for an example.
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
362
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
363 comint-get-old-input, comint-input-sentinel, and comint-input-filter are chosen
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
364 according to the command interpreter running in the buffer. For example,
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
365
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
366 If the interpreter is the csh,
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
367 comint-get-old-input defaults: takes the current line, discard any
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
368 initial string matching regexp comint-prompt-regexp.
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
369 comint-input-sentinel: monitors input for \"cd\", \"pushd\", and \"popd\"
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
370 commands. When it sees one, it changes the default directory of the buffer.
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
371 comint-input-filter defaults: returns t if the input isn't all whitespace.
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
372
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
373 If the comint is Lucid Common Lisp,
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
374 comint-get-old-input: snarfs the sexp ending at point.
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
375 comint-input-sentinel: does nothing.
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
376 comint-input-filter: returns nil if the input matches input-filter-regexp,
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
377 which matches (1) all whitespace (2) :a, :c, etc.
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
378
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
379 Similar functions are used for other process modes."
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
380 (interactive)
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
381 ;; Note that the input string does not include its terminal newline.
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
382 (if (not (get-buffer-process (current-buffer)))
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
383 (error "Current buffer has no process")
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
384 (let* ((pmark (comint-mark))
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
385 (input (if (>= (point) pmark)
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
386 (buffer-substring pmark (goto-char (point-max)))
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
387 (let ((copy (funcall comint-get-old-input)))
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
388 (delete-region pmark (goto-char (point-max)))
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
389 (insert copy)
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
390 copy))))
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
391 (set-marker comint-last-input-end (point))
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
392 (setq input-ring-index 0)
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
393 (if (funcall comint-input-filter input) (ring-insert input-ring input))
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
394 (funcall comint-input-sentinel input)
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
395 (comint-send-string nil (concat input (or terminator "\n")))
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
396 (if delete (delete-region mark (point))
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
397 (insert "\n"))
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
398 (set-marker (comint-mark) (point)))))
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
399
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
400 (defun comint-get-old-input-default ()
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
401 "Default for comint-get-old-input: use the current line sans prompt."
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
402 (save-excursion
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
403 (comint-bol)
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
404 (buffer-substring (point) (progn (end-of-line) (point)))))
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
405
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
406 (defun comint-bol (arg)
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
407 "Goes to the beginning of line, then skips past the prompt, if any.
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
408 With a prefix argument, (\\[universal-argument]), then doesn't skip prompt.
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
409
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
410 The prompt skip is done by passing over text matching the regular expression
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
411 comint-prompt-regexp, a buffer local variable."
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
412 (interactive "P")
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
413 (beginning-of-line)
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
414 (or arg (if (looking-at comint-prompt-regexp) (goto-char (match-end 0)))))
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
415
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
416 ;;; These two functions are for entering text you don't want echoed or
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
417 ;;; saved -- typically passwords to ftp, telnet, or somesuch.
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
418 ;;; Just enter M-x send-invisible and type in your line.
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
419 (defun comint-read-noecho (prompt)
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
420 "Prompting with PROMPT, read a single line of text without echoing.
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
421 The text can still be recovered (temporarily) with \\[view-lossage]. This
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
422 may be a security bug for some applications."
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
423 (let ((echo-keystrokes 0)
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
424 (answ "")
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
425 tem)
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
426 (if (and (stringp prompt) (not (string= (message prompt) "")))
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
427 (message prompt))
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
428 (while (not (or (= (setq tem (read-char)) ?\^m)
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
429 (= tem ?\n)))
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
430 (setq answ (concat answ (char-to-string tem))))
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
431 (message "")
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
432 answ))
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
433
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
434 (defun send-invisible (str)
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
435 "Read a string without echoing, and send it to the current buffer's process.
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
436 A newline is also sent. String is not saved on comint input history list.
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
437 Security bug: your string can still be temporarily recovered with \\[view-lossage]."
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
438 ; (interactive (list (comint-read-noecho "Enter non-echoed text")))
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
439 (interactive "P") ; Defeat snooping via C-x esc
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
440 (let ((proc (get-buffer-process (current-buffer))))
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
441 (if (not proc) (error "Current buffer has no process")
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
442 (comint-send-string proc
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
443 (if (stringp str) str
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
444 (comint-read-noecho "Enter non-echoed text")))
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
445 (comint-send-string proc "\n"))))
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
446
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
447
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
448 ;;; Low-level process communication
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
449
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
450 (defvar comint-input-chunk-size 512
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
451 "*Long inputs send to comint processes are broken up into chunks of this size.
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
452 If your process is choking on big inputs, try lowering the value.")
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
453
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
454 (defun comint-send-string (proc str)
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
455 "Send PROCESS the contents of STRING as input.
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
456 This is equivalent to process-send-string, except that long input strings
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
457 are broken up into chunks of size comint-input-chunk-size. Processes
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
458 are given a chance to output between chunks. This can help prevent processes
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
459 from hanging when you send them long inputs on some OS's."
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
460 (let* ((len (length str))
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
461 (i (min len comint-input-chunk-size)))
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
462 (process-send-string proc (substring str 0 i))
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
463 (while (< i len)
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
464 (let ((next-i (+ i comint-input-chunk-size)))
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
465 (accept-process-output)
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
466 (process-send-string proc (substring str i (min len next-i)))
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
467 (setq i next-i)))))
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
468
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
469 (defun comint-send-region (proc start end)
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
470 "Sends to PROC the region delimited by START and END.
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
471 This is a replacement for process-send-region that tries to keep
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
472 your process from hanging on long inputs. See comint-send-string."
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
473 (comint-send-string proc (buffer-substring start end)))
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
474
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
475
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
476 ;;; Random input hackage
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
477
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
478 (defun comint-flush-output ()
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
479 "Kill all output from interpreter since last input."
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
480 (interactive)
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
481 (save-excursion
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
482 (goto-char (comint-mark))
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
483 (beginning-of-line)
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
484 (delete-region (1+ comint-last-input-end) (point))
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
485 (insert "*** output flushed ***\n")))
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
486
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
487 (defun comint-show-output ()
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
488 "Start display of the current window at line preceding start of last output.
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
489 \"Last output\" is considered to start at the line following the last command
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
490 entered to the process."
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
491 (interactive)
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
492 (goto-char comint-last-input-end)
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
493 (beginning-of-line)
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
494 (set-window-start (selected-window) (point))
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
495 (comint-bol))
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
496
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
497 (defun comint-interrupt-subjob ()
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
498 "Sent an interrupt signal to the current subprocess.
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
499 If the process-connection-type is via ptys, the signal is sent to the current
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
500 process group of the pseudoterminal which Emacs is using to communicate with
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
501 the subprocess. If the process is a job-control shell, this means the
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
502 shell's current subjob. If the process connection is via pipes, the signal is
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
503 sent to the immediate subprocess."
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
504 (interactive)
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
505 (interrupt-process nil t))
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
506
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
507 (defun comint-kill-subjob ()
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
508 "Send a kill signal to the current subprocess.
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
509 See comint-interrupt-subjob for a description of \"current subprocess\"."
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
510 (interactive)
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
511 (kill-process nil t))
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
512
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
513 (defun comint-quit-subjob ()
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
514 "Send a quit signal to the current subprocess.
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
515 See comint-interrupt-subjob for a description of \"current subprocess\"."
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
516 (interactive)
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
517 (quit-process nil t))
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
518
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
519 (defun comint-stop-subjob ()
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
520 "Stop the current subprocess.
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
521 See comint-interrupt-subjob for a description of \"current subprocess\".
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
522
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
523 WARNING: if there is no current subjob, you can end up suspending
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
524 the top-level process running in the buffer. If you accidentally do
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
525 this, use \\[comint-continue-subjob] to resume the process. (This is not a
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
526 problem with most shells, since they ignore this signal.)"
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
527 (interactive)
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
528 (stop-process nil t))
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
529
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
530 (defun comint-continue-subjob ()
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
531 "Send a continue signal to current subprocess.
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
532 See comint-interrupt-subjob for a description of \"current subprocess\".
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
533 Useful if you accidentally suspend the top-level process."
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
534 (interactive)
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
535 (continue-process nil t))
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
536
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
537 (defun comint-kill-input ()
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
538 "Kill from current command through point."
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
539 (interactive)
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
540 (let ((pmark (comint-mark)))
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
541 (if (> (point) pmark)
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
542 (kill-region pmark (point))
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
543 (error "Nothing to kill"))))
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
544
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
545 (defun comint-delchar-or-maybe-eof (arg)
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
546 "Delete ARG characters forward, or send an EOF to process if at end of buffer."
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
547 (interactive "p")
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
548 (if (eobp)
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
549 (process-send-eof)
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
550 (delete-char arg)))
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
551
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
552 ;;; Support for source-file processing commands.
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
553 ;;;============================================================================
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
554 ;;; Many command-interpreters (e.g., Lisp, Scheme, Soar) have
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
555 ;;; commands that process files of source text (e.g. loading or compiling
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
556 ;;; files). So the corresponding process-in-a-buffer modes have commands
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
557 ;;; for doing this (e.g., lisp-load-file). The functions below are useful
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
558 ;;; for defining these commands.
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
559 ;;;
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
560 ;;; Alas, these guys don't do exactly the right thing for Lisp, Scheme
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
561 ;;; and Soar, in that they don't know anything about file extensions.
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
562 ;;; So the compile/load interface gets the wrong default occasionally.
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
563 ;;; The load-file/compile-file default mechanism could be smarter -- it
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
564 ;;; doesn't know about the relationship between filename extensions and
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
565 ;;; whether the file is source or executable. If you compile foo.lisp
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
566 ;;; with compile-file, then the next load-file should use foo.bin for
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
567 ;;; the default, not foo.lisp. This is tricky to do right, particularly
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
568 ;;; because the extension for executable files varies so much (.o, .bin,
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
569 ;;; .lbin, .mo, .vo, .ao, ...).
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
570
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
571
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
572 ;;; COMINT-SOURCE-DEFAULT -- determines defaults for source-file processing
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
573 ;;; commands.
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
574 ;;;
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
575 ;;; COMINT-CHECK-SOURCE -- if FNAME is in a modified buffer, asks you if you
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
576 ;;; want to save the buffer before issuing any process requests to the command
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
577 ;;; interpreter.
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
578 ;;;
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
579 ;;; COMINT-GET-SOURCE -- used by the source-file processing commands to prompt
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
580 ;;; for the file to process.
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
581
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
582 ;;; (COMINT-SOURCE-DEFAULT previous-dir/file source-modes)
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
583 ;;;============================================================================
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
584 ;;; This function computes the defaults for the load-file and compile-file
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
585 ;;; commands for tea, soar, lisp, and scheme modes.
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
586 ;;;
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
587 ;;; - PREVIOUS-DIR/FILE is a pair (directory . filename) from the last
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
588 ;;; source-file processing command. NIL if there hasn't been one yet.
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
589 ;;; - SOURCE-MODES is a list used to determine what buffers contain source
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
590 ;;; files: if the major mode of the buffer is in SOURCE-MODES, it's source.
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
591 ;;; Typically, (lisp-mode) or (scheme-mode).
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
592 ;;;
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
593 ;;; If the command is given while the cursor is inside a string, *and*
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
594 ;;; the string is an existing filename, *and* the filename is not a directory,
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
595 ;;; then the string is taken as default. This allows you to just position
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
596 ;;; your cursor over a string that's a filename and have it taken as default.
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
597 ;;;
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
598 ;;; If the command is given in a file buffer whose major mode is in
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
599 ;;; SOURCE-MODES, then the the filename is the default file, and the
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
600 ;;; file's directory is the default directory.
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
601 ;;;
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
602 ;;; If the buffer isn't a source file buffer (e.g., it's the process buffer),
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
603 ;;; then the default directory & file are what was used in the last source-file
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
604 ;;; processing command (i.e., PREVIOUS-DIR/FILE). If this is the first time
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
605 ;;; the command has been run (PREVIOUS-DIR/FILE is nil), the default directory
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
606 ;;; is the cwd, with no default file. (\"no default file\" = nil)
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
607 ;;;
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
608 ;;; SOURCE-REGEXP is typically going to be something like (tea-mode)
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
609 ;;; for T programs, (lisp-mode) for Lisp programs, (soar-mode lisp-mode)
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
610 ;;; for Soar programs, etc.
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
611 ;;;
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
612 ;;; The function returns a pair: (default-directory . default-file).
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
613
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
614 (defun comint-source-default (previous-dir/file source-modes)
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
615 (cond ((and buffer-file-name (memq major-mode source-modes))
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
616 (cons (file-name-directory buffer-file-name)
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
617 (file-name-nondirectory buffer-file-name)))
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
618 (previous-dir/file)
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
619 (t
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
620 (cons default-directory nil))))
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
621
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
622 ;;; (COMINT-CHECK-SOURCE fname)
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
623 ;;;============================================================================
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
624 ;;; Prior to loading or compiling (or otherwise processing) a file (in the
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
625 ;;; process-in-a-buffer modes), this function can be called on the filename.
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
626 ;;; If the file is loaded into a buffer, and the buffer is modified, the user
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
627 ;;; is queried to see if he wants to save the buffer before proceeding with
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
628 ;;; the load or compile.
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
629
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
630 (defun comint-check-source (fname)
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
631 (let ((buff (get-file-buffer fname)))
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
632 (if (and buff
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
633 (buffer-modified-p buff)
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
634 (y-or-n-p (format "Save buffer %s first? "
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
635 (buffer-name buff))))
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
636 ;; save BUFF.
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
637 (let ((old-buffer (current-buffer)))
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
638 (set-buffer buff)
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
639 (save-buffer)
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
640 (set-buffer old-buffer)))))
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
641
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
642 ;;; (COMINT-GET-SOURCE prompt prev-dir/file source-modes mustmatch-p)
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
643 ;;;============================================================================
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
644 ;;; COMINT-GET-SOURCE is used to prompt for filenames in command-interpreter
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
645 ;;; commands that process source files (like loading or compiling a file).
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
646 ;;; It prompts for the filename, provides a default, if there is one,
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
647 ;;; and returns the result filename.
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
648 ;;;
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
649 ;;; See COMINT-SOURCE-DEFAULT for more on determining defaults.
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
650 ;;;
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
651 ;;; PROMPT is the prompt string. PREV-DIR/FILE is the (directory . file) pair
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
652 ;;; from the last source processing command. SOURCE-MODES is a list of major
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
653 ;;; modes used to determine what file buffers contain source files. (These
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
654 ;;; two arguments are used for determining defaults). If MUSTMATCH-P is true,
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
655 ;;; then the filename reader will only accept a file that exists.
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
656 ;;;
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
657 ;;; A typical use:
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
658 ;;; (interactive (comint-get-source "Compile file: " prev-lisp-dir/file
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
659 ;;; '(lisp-mode) t))
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
660
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
661 ;;; This is pretty stupid about strings. It decides we're in a string
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
662 ;;; if there's a quote on both sides of point on the current line.
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
663 (defun comint-extract-string ()
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
664 "Returns string around point that starts the current line or nil."
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
665 (save-excursion
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
666 (let* ((point (point))
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
667 (bol (progn (beginning-of-line) (point)))
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
668 (eol (progn (end-of-line) (point)))
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
669 (start (progn (goto-char point)
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
670 (and (search-backward "\"" bol t)
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
671 (1+ (point)))))
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
672 (end (progn (goto-char point)
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
673 (and (search-forward "\"" eol t)
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
674 (1- (point))))))
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
675 (and start end
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
676 (buffer-substring start end)))))
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
677
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
678 (defun comint-get-source (prompt prev-dir/file source-modes mustmatch-p)
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
679 (let* ((def (comint-source-default prev-dir/file source-modes))
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
680 (stringfile (comint-extract-string))
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
681 (sfile-p (and stringfile
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
682 (file-exists-p stringfile)
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
683 (not (file-directory-p stringfile))))
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
684 (defdir (if sfile-p (file-name-directory stringfile)
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
685 (car def)))
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
686 (deffile (if sfile-p (file-name-nondirectory stringfile)
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
687 (cdr def)))
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
688 (ans (read-file-name (if deffile (format "%s(default %s) "
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
689 prompt deffile)
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
690 prompt)
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
691 defdir
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
692 (concat defdir deffile)
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
693 mustmatch-p)))
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
694 (list (expand-file-name (substitute-in-file-name ans)))))
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
695
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
696
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
697 ;;; Simple process query facility.
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
698 ;;; ===========================================================================
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
699 ;;; This function is for commands that want to send a query to the process
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
700 ;;; and show the response to the user. For example, a command to get the
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
701 ;;; arglist for a Common Lisp function might send a "(arglist 'foo)" query
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
702 ;;; to an inferior Common Lisp process.
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
703 ;;;
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
704 ;;; This simple facility just sends strings to the inferior process and pops
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
705 ;;; up a window for the process buffer so you can see what the process
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
706 ;;; responds with. We don't do anything fancy like try to intercept what the
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
707 ;;; process responds with and put it in a pop-up window or on the message
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
708 ;;; line. We just display the buffer. Low tech. Simple. Works good.
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
709
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
710 ;;; Send to the inferior process PROC the string STR. Pop-up but do not select
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
711 ;;; a window for the inferior process so that its response can be seen.
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
712 (defun comint-proc-query (proc str)
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
713 (let* ((proc-buf (process-buffer proc))
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
714 (proc-mark (process-mark proc)))
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
715 (display-buffer proc-buf)
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
716 (set-buffer proc-buf) ; but it's not the selected *window*
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
717 (let ((proc-win (get-buffer-window proc-buf))
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
718 (proc-pt (marker-position proc-mark)))
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
719 (comint-send-string proc str) ; send the query
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
720 (accept-process-output proc) ; wait for some output
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
721 ;; Try to position the proc window so you can see the answer.
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
722 ;; This is bogus code. If you delete the (sit-for 0), it breaks.
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
723 ;; I don't know why. Wizards invited to improve it.
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
724 (if (not (pos-visible-in-window-p proc-pt proc-win))
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
725 (let ((opoint (window-point proc-win)))
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
726 (set-window-point proc-win proc-mark) (sit-for 0)
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
727 (if (not (pos-visible-in-window-p opoint proc-win))
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
728 (push-mark opoint)
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
729 (set-window-point proc-win opoint)))))))
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
730
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
731
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
732 ;;; Filename completion in a buffer
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
733 ;;; ===========================================================================
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
734 ;;; Useful completion functions, courtesy of the Ergo group.
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
735 ;;; M-<Tab> will complete the filename at the cursor as much as possible
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
736 ;;; M-? will display a list of completions in the help buffer.
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
737
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
738 ;;; Three commands:
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
739 ;;; comint-dynamic-complete Complete filename at point.
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
740 ;;; comint-dynamic-list-completions List completions in help buffer.
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
741 ;;; comint-replace-by-expanded-filename Expand and complete filename at point;
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
742 ;;; replace with expanded/completed name.
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
743
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
744 ;;; These are not installed in the comint-mode keymap. But they are
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
745 ;;; available for people who want them. Shell-mode-map uses them, though.
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
746
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
747 (defun comint-match-partial-pathname ()
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
748 "Returns the string of an existing filename or causes an error."
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
749 (if (save-excursion (backward-char 1) (looking-at "\\s ")) ""
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
750 (save-excursion
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
751 (re-search-backward "[^~/A-Za-z0-9---_.$#,]+")
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
752 (re-search-forward "[~/A-Za-z0-9---_.$#,]+")
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
753 (substitute-in-file-name
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
754 (buffer-substring (match-beginning 0) (match-end 0))))))
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
755
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
756 (defun comint-replace-by-expanded-filename ()
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
757 "Replace the filename at point with its expanded, canonicalised completion.
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
758 \"Expanded\" means environment variables (e.g., $HOME) and ~'s are
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
759 replaced with the corresponding directories. \"Canonicalised\" means ..
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
760 and . are removed, and the filename is made absolute instead of relative.
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
761 See functions expand-file-name and substitute-in-file-name. See also
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
762 comint-dynamic-complete."
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
763 (interactive)
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
764 (let* ((pathname (comint-match-partial-pathname))
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
765 (pathdir (file-name-directory pathname))
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
766 (pathnondir (file-name-nondirectory pathname))
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
767 (completion (file-name-completion pathnondir
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
768 (or pathdir default-directory))))
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
769 (cond ((null completion)
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
770 (error "No completions"))
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
771 ((eql completion t)
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
772 (message "Sole completion"))
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
773 (t ; this means a string was returned.
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
774 (delete-region (match-beginning 0) (match-end 0))
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
775 (insert (expand-file-name (concat pathdir completion)))))))
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
776
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
777 (defun comint-dynamic-complete ()
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
778 "Complete the filename at point.
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
779 This function is similar to comint-replace-by-expanded-filename, except
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
780 that it won't change parts of the filename already entered in the buffer;
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
781 it just adds completion characters to the end of the filename."
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
782 (interactive)
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
783 (let* ((pathname (comint-match-partial-pathname))
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
784 (pathdir (file-name-directory pathname))
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
785 (pathnondir (file-name-nondirectory pathname))
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
786 (completion (file-name-completion pathnondir
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
787 (or pathdir default-directory))))
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
788 (cond ((null completion)
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
789 (error "No completions"))
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
790 ((eql completion t)
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
791 (error "Sole completion"))
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
792 (t ; this means a string was returned.
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
793 (goto-char (match-end 0))
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
794 (insert (substring completion (length pathnondir)))))))
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
795
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
796 (defun comint-dynamic-list-completions ()
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
797 "List all possible completions of the filename at point."
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
798 (interactive)
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
799 (let* ((pathname (comint-match-partial-pathname))
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
800 (pathdir (file-name-directory pathname))
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
801 (pathnondir (file-name-nondirectory pathname))
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
802 (completions
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
803 (file-name-all-completions pathnondir
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
804 (or pathdir default-directory))))
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
805 (cond ((null completions)
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
806 (error "No completions"))
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
807 (t
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
808 (let ((conf (current-window-configuration)))
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
809 (with-output-to-temp-buffer " *Completions*"
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
810 (display-completion-list completions))
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
811 (sit-for 0)
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
812 (message "Hit space to flush.")
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
813 (let ((ch (read-char)))
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
814 (if (= ch ?\ )
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
815 (set-window-configuration conf)
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
816 (setq unread-command-char ch))))))))
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
817
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
818
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
819 ;;; Converting process modes to use comint mode
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
820 ;;; ===========================================================================
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
821 ;;; Renaming variables
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
822 ;;; Most of the work is renaming variables and functions.
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
823 ;;; These are the common ones.
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
824
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
825 ;;; Local variables --
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
826 ;;; last-input-end comint-last-input-end
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
827 ;;; last-input-start <unnecessary>
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
828 ;;; shell-prompt-pattern comint-prompt-regexp
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
829 ;;; shell-set-directory-error-hook <no equivalent>
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
830 ;;; Miscellaneous --
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
831 ;;; shell-set-directory <unnecessary>
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
832 ;;; shell-mode-map comint-mode-map
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
833 ;;; Commands --
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
834 ;;; shell-send-input comint-send-input
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
835 ;;; shell-send-eof comint-delchar-or-maybe-eof
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
836 ;;; kill-shell-input comint-kill-input
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
837 ;;; interrupt-shell-subjob comint-interrupt-subjob
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
838 ;;; stop-shell-subjob comint-stop-subjob
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
839 ;;; quit-shell-subjob comint-quit-subjob
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
840 ;;; kill-shell-subjob comint-kill-subjob
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
841 ;;; kill-output-from-shell comint-kill-output
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
842 ;;; show-output-from-shell comint-show-output
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
843 ;;; copy-last-shell-input Use comint-previous-input/comint-next-input
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
844 ;;;
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
845 ;;; LAST-INPUT-START is no longer necessary because inputs are stored on the
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
846 ;;; input history ring. SHELL-SET-DIRECTORY is gone, its functionality taken
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
847 ;;; over by SHELL-DIRECTORY-TRACKER, the shell mode's comint-input-sentinel.
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
848 ;;; Comint mode does not provide functionality equivalent to
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
849 ;;; shell-set-directory-error-hook; it is gone.
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
850 ;;;
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
851 ;;; If you are implementing some process-in-a-buffer mode, called foo-mode, do
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
852 ;;; *not* create the comint-mode local variables in your foo-mode function.
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
853 ;;; This is not modular. Instead, call comint-mode, and let *it* create the
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
854 ;;; necessary comint-specific local variables. Then create the
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
855 ;;; foo-mode-specific local variables in foo-mode. Set the buffer's keymap to
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
856 ;;; be foo-mode-map, and its mode to be foo-mode. Set the comint-mode hooks
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
857 ;;; (comint-prompt-regexp, comint-input-filter, comint-input-sentinel,
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
858 ;;; comint-get-old-input) that need to be different from the defaults. Call
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
859 ;;; foo-mode-hook, and you're done. Don't run the comint-mode hook yourself;
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
860 ;;; comint-mode will take care of it.
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
861 ;;;
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
862 ;;; Note that make-comint is different from make-shell in that it
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
863 ;;; doesn't have a default program argument. If you give make-shell
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
864 ;;; a program name of NIL, it cleverly chooses one of explicit-shell-name,
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
865 ;;; $ESHELL, $SHELL, or /bin/sh. If you give make-comint a program argument
899728e6052a Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
866 ;;; of NIL, it barfs. Adjust your code accordingly...