0
|
1 ;;; shell.el --- specialized comint.el for running the shell.
|
|
2
|
|
3 ;; Copyright (C) 1988, 1993, 1994, 1995 Free Software Foundation, Inc.
|
|
4
|
|
5 ;; Author: Olin Shivers <shivers@cs.cmu.edu>
|
|
6 ;; Maintainer: Simon Marshall <simon@gnu.ai.mit.edu>
|
|
7 ;; Keywords: processes
|
|
8
|
|
9 ;; This file is part of XEmacs.
|
|
10
|
|
11 ;; XEmacs is free software; you can redistribute it and/or modify it
|
|
12 ;; under the terms of the GNU General Public License as published by
|
|
13 ;; the Free Software Foundation; either version 2, or (at your option)
|
|
14 ;; any later version.
|
|
15
|
|
16 ;; XEmacs is distributed in the hope that it will be useful, but
|
|
17 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
19 ;; General Public License for more details.
|
|
20
|
|
21 ;; You should have received a copy of the GNU General Public License
|
16
|
22 ;; along with XEmacs; see the file COPYING. If not, write to the
|
|
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
24 ;; Boston, MA 02111-1307, USA.
|
0
|
25
|
|
26 ;;; Synched up with: FSF 19.30.
|
|
27
|
|
28 ;;; Commentary:
|
|
29
|
|
30 ;;; Please send me bug reports, bug fixes, and extensions, so that I can
|
|
31 ;;; merge them into the master source.
|
|
32 ;;; - Olin Shivers (shivers@cs.cmu.edu)
|
|
33 ;;; - Simon Marshall (simon@gnu.ai.mit.edu)
|
|
34
|
|
35 ;;; This file defines a a shell-in-a-buffer package (shell mode) built
|
|
36 ;;; on top of comint mode. This is actually cmushell with things
|
|
37 ;;; renamed to replace its counterpart in Emacs 18. cmushell is more
|
|
38 ;;; featureful, robust, and uniform than the Emacs 18 version.
|
|
39
|
|
40 ;;; Since this mode is built on top of the general command-interpreter-in-
|
|
41 ;;; a-buffer mode (comint mode), it shares a common base functionality,
|
|
42 ;;; and a common set of bindings, with all modes derived from comint mode.
|
|
43 ;;; This makes these modes easier to use.
|
|
44
|
|
45 ;;; For documentation on the functionality provided by comint mode, and
|
|
46 ;;; the hooks available for customising it, see the file comint.el.
|
|
47 ;;; For further information on shell mode, see the comments below.
|
|
48
|
|
49 ;;; Needs fixin:
|
|
50 ;;; When sending text from a source file to a subprocess, the process-mark can
|
|
51 ;;; move off the window, so you can lose sight of the process interactions.
|
|
52 ;;; Maybe I should ensure the process mark is in the window when I send
|
|
53 ;;; text to the process? Switch selectable?
|
|
54
|
|
55 ;; YOUR .EMACS FILE
|
|
56 ;;=============================================================================
|
|
57 ;; Some suggestions for your .emacs file.
|
|
58 ;;
|
|
59 ;; ;; Define M-# to run some strange command:
|
|
60 ;; (eval-after-load "shell"
|
|
61 ;; '(define-key shell-mode-map "\M-#" 'shells-dynamic-spell))
|
|
62
|
|
63 ;;; Brief Command Documentation:
|
|
64 ;;;============================================================================
|
|
65 ;;; Comint Mode Commands: (common to shell and all comint-derived modes)
|
|
66 ;;;
|
|
67 ;;; m-p comint-previous-input Cycle backwards in input history
|
|
68 ;;; m-n comint-next-input Cycle forwards
|
|
69 ;;; m-r comint-previous-matching-input Previous input matching a regexp
|
|
70 ;;; m-s comint-next-matching-input Next input that matches
|
|
71 ;;; m-c-l comint-show-output Show last batch of process output
|
|
72 ;;; return comint-send-input
|
|
73 ;;; c-d comint-delchar-or-maybe-eof Delete char unless at end of buff.
|
|
74 ;;; c-c c-a comint-bol Beginning of line; skip prompt
|
|
75 ;;; c-c c-u comint-kill-input ^u
|
|
76 ;;; c-c c-w backward-kill-word ^w
|
|
77 ;;; c-c c-c comint-interrupt-subjob ^c
|
|
78 ;;; c-c c-z comint-stop-subjob ^z
|
|
79 ;;; c-c c-\ comint-quit-subjob ^\
|
|
80 ;;; c-c c-o comint-kill-output Delete last batch of process output
|
|
81 ;;; c-c c-r comint-show-output Show last batch of process output
|
|
82 ;;; c-c c-h comint-dynamic-list-input-ring List input history
|
|
83 ;;; send-invisible Read line w/o echo & send to proc
|
|
84 ;;; comint-continue-subjob Useful if you accidentally suspend
|
|
85 ;;; top-level job
|
|
86 ;;; comint-mode-hook is the comint mode hook.
|
|
87
|
|
88 ;;; Shell Mode Commands:
|
|
89 ;;; shell Fires up the shell process
|
|
90 ;;; tab comint-dynamic-complete Complete filename/command/history
|
|
91 ;;; m-? comint-dynamic-list-filename-completions
|
|
92 ;;; List completions in help buffer
|
|
93 ;;; m-c-f shell-forward-command Forward a shell command
|
|
94 ;;; m-c-b shell-backward-command Backward a shell command
|
|
95 ;;; dirs Resync the buffer's dir stack
|
|
96 ;;; dirtrack-toggle Turn dir tracking on/off
|
|
97 ;;; comint-strip-ctrl-m Remove trailing ^Ms from output
|
|
98 ;;;
|
|
99 ;;; The shell mode hook is shell-mode-hook
|
|
100 ;;; comint-prompt-regexp is initialised to shell-prompt-pattern, for backwards
|
|
101 ;;; compatibility.
|
|
102
|
|
103 ;;; Read the rest of this file for more information.
|
|
104
|
|
105 ;;; Customization and Buffer Variables
|
|
106 ;;; ===========================================================================
|
|
107 ;;;
|
|
108
|
|
109 ;;; Code:
|
|
110
|
|
111 (require 'comint)
|
|
112
|
|
113 ;;;###autoload
|
|
114 (defvar shell-prompt-pattern (purecopy "^[^#$%>\n]*[#$%>] *")
|
|
115 "Regexp to match prompts in the inferior shell.
|
|
116 Defaults to \"^[^#$%>\\n]*[#$%>] *\", which works pretty well.
|
|
117 This variable is used to initialise `comint-prompt-regexp' in the
|
|
118 shell buffer.
|
|
119
|
|
120 The pattern should probably not match more than one line. If it does,
|
|
121 shell-mode may become confused trying to distinguish prompt from input
|
|
122 on lines which don't start with a prompt.
|
|
123
|
|
124 This is a fine thing to set in your `.emacs' file.")
|
|
125
|
|
126 (defvar shell-completion-fignore nil
|
|
127 "*List of suffixes to be disregarded during file/command completion.
|
|
128 This variable is used to initialize `comint-completion-fignore' in the shell
|
|
129 buffer. The default is nil, for compatibility with most shells.
|
|
130 Some people like (\"~\" \"#\" \"%\").
|
|
131
|
|
132 This is a fine thing to set in your `.emacs' file.")
|
|
133
|
|
134 ;jwz: turned this off; it's way too broken.
|
|
135 (defvar shell-delimiter-argument-list nil ;'(?\| ?& ?< ?> ?\( ?\) ?\;
|
|
136 "List of characters to recognise as separate arguments.
|
|
137 This variable is used to initialize `comint-delimiter-argument-list' in the
|
|
138 shell buffer. The default is (?\\| ?& ?< ?> ?\\( ?\\) ?\\;).
|
|
139
|
|
140 This is a fine thing to set in your `.emacs' file.")
|
|
141
|
|
142 (defvar shell-file-name-quote-list
|
|
143 (append shell-delimiter-argument-list '(?\ ?\* ?\! ?\" ?\' ?\`))
|
|
144 "List of characters to quote when in a file name.
|
|
145 This variable is used to initialize `comint-file-name-quote-list' in the
|
|
146 shell buffer. The default is (?\ ?\* ?\! ?\" ?\' ?\`) plus characters
|
|
147 in `shell-delimiter-argument-list'.
|
|
148
|
|
149 This is a fine thing to set in your `.emacs' file.")
|
|
150
|
|
151 (defvar shell-dynamic-complete-functions
|
|
152 '(comint-replace-by-expanded-history
|
|
153 shell-dynamic-complete-environment-variable
|
|
154 shell-dynamic-complete-command
|
|
155 shell-replace-by-expanded-directory
|
|
156 comint-dynamic-complete-filename)
|
|
157 "List of functions called to perform completion.
|
|
158 This variable is used to initialise `comint-dynamic-complete-functions' in the
|
|
159 shell buffer.
|
|
160
|
|
161 This is a fine thing to set in your `.emacs' file.")
|
|
162
|
|
163 (defvar shell-command-regexp "[^;&|\n]+"
|
|
164 "*Regexp to match a single command within a pipeline.
|
|
165 This is used for directory tracking and does not do a perfect job.")
|
|
166
|
|
167 (defvar shell-completion-execonly t
|
|
168 "*If non-nil, use executable files only for completion candidates.
|
|
169 This mirrors the optional behavior of tcsh.
|
|
170
|
|
171 Detecting executability of files may slow command completion considerably.")
|
|
172
|
|
173 (defvar shell-multiple-shells nil
|
|
174 "*If non-nil, each time shell mode is invoked, a new shell is made")
|
|
175
|
|
176 (defvar shell-popd-regexp "popd"
|
|
177 "*Regexp to match subshell commands equivalent to popd.")
|
|
178
|
|
179 (defvar shell-pushd-regexp "pushd"
|
|
180 "*Regexp to match subshell commands equivalent to pushd.")
|
|
181
|
|
182 (defvar shell-pushd-tohome nil
|
|
183 "*If non-nil, make pushd with no arg behave as \"pushd ~\" (like cd).
|
|
184 This mirrors the optional behavior of tcsh.")
|
|
185
|
|
186 (defvar shell-pushd-dextract nil
|
|
187 "*If non-nil, make \"pushd +n\" pop the nth dir to the stack top.
|
|
188 This mirrors the optional behavior of tcsh.")
|
|
189
|
|
190 (defvar shell-pushd-dunique nil
|
|
191 "*If non-nil, make pushd only add unique directories to the stack.
|
|
192 This mirrors the optional behavior of tcsh.")
|
|
193
|
|
194 (defvar shell-cd-regexp "cd"
|
|
195 "*Regexp to match subshell commands equivalent to cd.")
|
|
196
|
|
197 (defvar explicit-shell-file-name nil
|
|
198 "*If non-nil, is file name to use for explicitly requested inferior shell.")
|
|
199
|
|
200 (defvar explicit-csh-args
|
|
201 (if (eq system-type 'hpux)
|
|
202 ;; -T persuades HP's csh not to think it is smarter
|
|
203 ;; than us about what terminal modes to use.
|
|
204 '("-i" "-T")
|
|
205 '("-i"))
|
|
206 "*Args passed to inferior shell by M-x shell, if the shell is csh.
|
|
207 Value is a list of strings, which may be nil.")
|
|
208
|
|
209 (defvar shell-input-autoexpand 'history
|
|
210 "*If non-nil, expand input command history references on completion.
|
|
211 This mirrors the optional behavior of tcsh (its autoexpand and histlit).
|
|
212
|
|
213 If the value is `input', then the expansion is seen on input.
|
|
214 If the value is `history', then the expansion is only when inserting
|
|
215 into the buffer's input ring. See also `comint-magic-space' and
|
|
216 `comint-dynamic-complete'.
|
|
217
|
|
218 This variable supplies a default for `comint-input-autoexpand',
|
|
219 for Shell mode only.")
|
|
220
|
|
221 (defvar shell-dirstack nil
|
|
222 "List of directories saved by pushd in this buffer's shell.
|
|
223 Thus, this does not include the shell's current directory.")
|
|
224
|
|
225 (defvar shell-dirtrackp t
|
|
226 "Non-nil in a shell buffer means directory tracking is enabled.")
|
|
227
|
|
228 (defvar shell-last-dir nil
|
|
229 "Keep track of last directory for ksh `cd -' command.")
|
|
230
|
|
231 (defvar shell-dirstack-query nil
|
|
232 "Command used by `shell-resync-dirs' to query the shell.")
|
|
233
|
|
234 (defvar shell-mode-map nil)
|
|
235 (if (not shell-mode-map)
|
|
236 (let ((map (make-keymap)))
|
|
237 (set-keymap-parents map (list comint-mode-map))
|
|
238 (set-keymap-name map 'shell-mode-map)
|
|
239 (define-key map "\C-c\C-f" 'shell-forward-command)
|
|
240 (define-key map "\C-c\C-b" 'shell-backward-command)
|
|
241 (define-key map "\t" 'comint-dynamic-complete)
|
|
242 (define-key map "\M-?" 'comint-dynamic-list-filename-completions)
|
|
243 ;; XEmacs: this is a pretty common operation for those of us
|
|
244 ;; who use directory aliases ... someone shoot me if they
|
|
245 ;; don't like this binding. Another possibility is C-c C-s
|
|
246 ;; but that's way awkward.
|
|
247 (define-key map "\M-\C-m" 'shell-resync-dirs)
|
|
248 (setq shell-mode-map map)))
|
|
249
|
|
250 (defvar shell-mode-hook nil
|
|
251 "*Hook for customising Shell mode.")
|
|
252
|
|
253 (defvar shell-font-lock-keywords
|
70
|
254 (list (cons shell-prompt-pattern 'font-lock-keyword-face)
|
|
255 '("[ \t]\\([+-][^ \t\n]+\\)" 1 font-lock-comment-face)
|
|
256 '("^[^ \t\n]+:.*" . font-lock-string-face)
|
|
257 '("^\\[[1-9][0-9]*\\]" . font-lock-string-face))
|
0
|
258 "Additional expressions to highlight in Shell mode.")
|
|
259 (put 'shell-mode 'font-lock-defaults '(shell-font-lock-keywords t))
|
|
260
|
|
261 ;;; Basic Procedures
|
|
262 ;;; ===========================================================================
|
|
263 ;;;
|
|
264
|
|
265 (defun shell-mode ()
|
|
266 "Major mode for interacting with an inferior shell.
|
|
267 \\<shell-mode-map>\\[comint-send-input] after the end of the process' output sends the text from
|
|
268 the end of process to the end of the current line.
|
|
269 \\[comint-send-input] before end of process output copies the current line minus the
|
|
270 prompt to the end of the buffer and sends it (\\[comint-copy-old-input] just copies
|
|
271 the current line).
|
|
272 \\[send-invisible] reads a line of text without echoing it, and sends it to
|
|
273 the shell. This is useful for entering passwords. Or, add the function
|
|
274 `comint-watch-for-password-prompt' to `comint-output-filter-functions'.
|
|
275
|
|
276 If you want to make multiple shell buffers, rename the `*shell*' buffer
|
|
277 using \\[rename-buffer] or \\[rename-uniquely] and start a new shell.
|
|
278
|
|
279 If you want to make shell buffers limited in length, add the function
|
|
280 `comint-truncate-buffer' to `comint-output-filter-functions'.
|
|
281
|
|
282 If you accidentally suspend your process, use \\[comint-continue-subjob]
|
|
283 to continue it.
|
|
284
|
|
285 `cd', `pushd' and `popd' commands given to the shell are watched by Emacs to
|
|
286 keep this buffer's default directory the same as the shell's working directory.
|
|
287 While directory tracking is enabled, the shell's working directory is displayed
|
|
288 by \\[list-buffers] or \\[mouse-buffer-menu] in the `File' field.
|
|
289 \\[shell-resync-dirs] queries the shell and resyncs Emacs' idea of what the
|
|
290 current directory stack is.
|
|
291 \\[shell-dirtrack-toggle] turns directory tracking on and off.
|
|
292
|
|
293 \\{shell-mode-map}
|
|
294 Customization: Entry to this mode runs the hooks on `comint-mode-hook' and
|
|
295 `shell-mode-hook' (in that order). Before each input, the hooks on
|
|
296 `comint-input-filter-functions' are run. After each shell output, the hooks
|
|
297 on `comint-output-filter-functions' are run.
|
|
298
|
|
299 Variable `shell-multiple-shells' will automatically generate a new shell each
|
|
300 time it is invoked.
|
|
301
|
|
302 Variables `shell-cd-regexp', `shell-pushd-regexp' and `shell-popd-regexp'
|
|
303 are used to match their respective commands, while `shell-pushd-tohome',
|
|
304 `shell-pushd-dextract' and `shell-pushd-dunique' control the behavior of the
|
|
305 relevant command.
|
|
306
|
|
307 Variables `comint-completion-autolist', `comint-completion-addsuffix',
|
|
308 `comint-completion-recexact' and `comint-completion-fignore' control the
|
|
309 behavior of file name, command name and variable name completion. Variable
|
|
310 `shell-completion-execonly' controls the behavior of command name completion.
|
|
311 Variable `shell-completion-fignore' is used to initialise the value of
|
|
312 `comint-completion-fignore'.
|
|
313
|
|
314 Variables `comint-input-ring-file-name' and `comint-input-autoexpand' control
|
|
315 the initialisation of the input ring history, and history expansion.
|
|
316
|
|
317 Variables `comint-output-filter-functions', a hook, and
|
|
318 `comint-scroll-to-bottom-on-input' and `comint-scroll-to-bottom-on-output'
|
|
319 control whether input and output cause the window to scroll to the end of the
|
|
320 buffer."
|
|
321 (interactive)
|
|
322 (comint-mode)
|
|
323 (setq major-mode 'shell-mode)
|
|
324 (setq mode-name "Shell")
|
|
325 (use-local-map shell-mode-map)
|
|
326 (make-local-variable 'comint-prompt-regexp)
|
|
327 (setq comint-prompt-regexp shell-prompt-pattern)
|
|
328 (setq comint-completion-fignore shell-completion-fignore)
|
|
329 (make-local-variable 'comint-delimiter-argument-list)
|
|
330 (setq comint-delimiter-argument-list shell-delimiter-argument-list)
|
|
331 (make-local-variable 'comint-after-partial-filename-command)
|
|
332 (setq comint-after-partial-filename-command 'shell-after-partial-filename)
|
|
333 (make-local-variable 'comint-get-current-command)
|
|
334 (setq comint-get-current-command 'shell-get-current-command)
|
|
335 (make-local-variable 'comint-dynamic-complete-command-command)
|
|
336 (setq comint-dynamic-complete-command-command 'shell-dynamic-complete-command)
|
|
337 (setq comint-file-name-quote-list shell-file-name-quote-list)
|
|
338 (setq comint-dynamic-complete-functions shell-dynamic-complete-functions)
|
|
339 (make-local-variable 'paragraph-start)
|
|
340 (setq paragraph-start comint-prompt-regexp)
|
|
341 (make-local-variable 'shell-dirstack)
|
|
342 (setq shell-dirstack nil)
|
|
343 (make-local-variable 'shell-last-dir)
|
|
344 (setq shell-last-dir nil)
|
|
345 (make-local-variable 'shell-dirtrackp)
|
|
346 (setq shell-dirtrackp t)
|
|
347 (add-hook 'comint-input-filter-functions 'shell-directory-tracker nil t)
|
|
348 (setq comint-input-autoexpand shell-input-autoexpand)
|
|
349 (make-local-variable 'list-buffers-directory)
|
|
350 (setq list-buffers-directory (expand-file-name default-directory))
|
|
351 ;; shell-dependent assignments.
|
|
352 (let ((shell (file-name-nondirectory (car
|
|
353 (process-command (get-buffer-process (current-buffer)))))))
|
|
354 (setq comint-input-ring-file-name
|
|
355 (or (getenv "HISTFILE")
|
|
356 (cond ((string-equal shell "bash") "~/.bash_history")
|
|
357 ((string-equal shell "ksh") "~/.sh_history")
|
|
358 (t "~/.history"))))
|
|
359 (if (or (equal comint-input-ring-file-name "")
|
|
360 (equal (file-truename comint-input-ring-file-name) "/dev/null"))
|
|
361 (setq comint-input-ring-file-name nil))
|
|
362 (setq shell-dirstack-query
|
|
363 (if (string-match "^k?sh$" shell) "pwd" "dirs")))
|
|
364 (run-hooks 'shell-mode-hook)
|
|
365 (comint-read-input-ring t)
|
|
366 (shell-dirstack-message))
|
|
367
|
|
368
|
|
369 ;;;###autoload
|
|
370 (defun shell ()
|
|
371 "Run an inferior shell, with I/O through buffer *shell*.
|
|
372 If buffer exists but shell process is not running, make new shell.
|
|
373 If buffer exists and shell process is running,
|
|
374 just switch to buffer `*shell*'.
|
|
375 Program used comes from variable `explicit-shell-file-name',
|
|
376 or (if that is nil) from the ESHELL environment variable,
|
|
377 or else from SHELL if there is no ESHELL.
|
|
378 If a file `~/.emacs_SHELLNAME' exists, it is given as initial input
|
|
379 (Note that this may lose due to a timing error if the shell
|
|
380 discards input when it starts up.)
|
|
381 The buffer is put in Shell mode, giving commands for sending input
|
|
382 and controlling the subjobs of the shell. See `shell-mode'.
|
|
383 See also the variable `shell-prompt-pattern'.
|
|
384
|
|
385 The shell file name (sans directories) is used to make a symbol name
|
|
386 such as `explicit-csh-args'. If that symbol is a variable,
|
|
387 its value is used as a list of arguments when invoking the shell.
|
|
388 Otherwise, one argument `-i' is passed to the shell.
|
|
389
|
|
390 \(Type \\[describe-mode] in the shell buffer for a list of commands.)"
|
|
391 (interactive)
|
|
392 (let ((buffer "*shell*")
|
|
393 (buffer-name (if shell-multiple-shells
|
|
394 "*shell*"
|
|
395 "shell")))
|
|
396 (cond ((or shell-multiple-shells
|
|
397 (not (comint-check-proc buffer)))
|
|
398 (let* ((prog (or explicit-shell-file-name
|
|
399 (getenv "ESHELL")
|
|
400 (getenv "SHELL")
|
|
401 "/bin/sh"))
|
|
402 (name (file-name-nondirectory prog))
|
|
403 (startfile (concat "~/.emacs_" name))
|
|
404 (xargs-name (intern-soft (concat "explicit-" name "-args"))))
|
|
405 (setq buffer (set-buffer (apply 'make-comint buffer-name prog
|
|
406 (if (file-exists-p startfile)
|
|
407 startfile)
|
|
408 (if (and xargs-name
|
|
409 (boundp xargs-name))
|
|
410 (symbol-value xargs-name)
|
|
411 '("-i")))))
|
|
412 (shell-mode))))
|
|
413 (pop-to-buffer buffer)
|
|
414 (if shell-multiple-shells
|
|
415 (rename-buffer (generate-new-buffer-name "*shell*")))
|
|
416 ))
|
|
417
|
|
418 ;;; Don't do this when shell.el is loaded, only while dumping.
|
|
419 ;;;###autoload (add-hook 'same-window-buffer-names "*shell*")
|
|
420
|
|
421 ;;; Directory tracking
|
|
422 ;;; ===========================================================================
|
|
423 ;;; This code provides the shell mode input sentinel
|
|
424 ;;; SHELL-DIRECTORY-TRACKER
|
|
425 ;;; that tracks cd, pushd, and popd commands issued to the shell, and
|
|
426 ;;; changes the current directory of the shell buffer accordingly.
|
|
427 ;;;
|
|
428 ;;; This is basically a fragile hack, although it's more accurate than
|
|
429 ;;; the version in Emacs 18's shell.el. It has the following failings:
|
|
430 ;;; 1. It doesn't know about the cdpath shell variable.
|
|
431 ;;; 2. It cannot infallibly deal with command sequences, though it does well
|
|
432 ;;; with these and with ignoring commands forked in another shell with ()s.
|
|
433 ;;; 3. More generally, any complex command is going to throw it. Otherwise,
|
|
434 ;;; you'd have to build an entire shell interpreter in emacs lisp. Failing
|
|
435 ;;; that, there's no way to catch shell commands where cd's are buried
|
|
436 ;;; inside conditional expressions, aliases, and so forth.
|
|
437 ;;;
|
|
438 ;;; The whole approach is a crock. Shell aliases mess it up. File sourcing
|
|
439 ;;; messes it up. You run other processes under the shell; these each have
|
|
440 ;;; separate working directories, and some have commands for manipulating
|
|
441 ;;; their w.d.'s (e.g., the lcd command in ftp). Some of these programs have
|
|
442 ;;; commands that do *not* affect the current w.d. at all, but look like they
|
|
443 ;;; do (e.g., the cd command in ftp). In shells that allow you job
|
|
444 ;;; control, you can switch between jobs, all having different w.d.'s. So
|
|
445 ;;; simply saying %3 can shift your w.d..
|
|
446 ;;;
|
|
447 ;;; The solution is to relax, not stress out about it, and settle for
|
|
448 ;;; a hack that works pretty well in typical circumstances. Remember
|
|
449 ;;; that a half-assed solution is more in keeping with the spirit of Unix,
|
|
450 ;;; anyway. Blech.
|
|
451 ;;;
|
|
452 ;;; One good hack not implemented here for users of programmable shells
|
|
453 ;;; is to program up the shell w.d. manipulation commands to output
|
|
454 ;;; a coded command sequence to the tty. Something like
|
|
455 ;;; ESC | <cwd> |
|
|
456 ;;; where <cwd> is the new current working directory. Then trash the
|
|
457 ;;; directory tracking machinery currently used in this package, and
|
|
458 ;;; replace it with a process filter that watches for and strips out
|
|
459 ;;; these messages.
|
|
460
|
|
461 (defun shell-directory-tracker (str)
|
|
462 "Tracks cd, pushd and popd commands issued to the shell.
|
|
463 This function is called on each input passed to the shell.
|
|
464 It watches for cd, pushd and popd commands and sets the buffer's
|
|
465 default directory to track these commands.
|
|
466
|
|
467 You may toggle this tracking on and off with \\[shell-dirtrack-toggle].
|
|
468 If emacs gets confused, you can resync with the shell
|
|
469 with \\[shell-resync-dirs].
|
|
470
|
|
471 See variables `shell-cd-regexp', `shell-pushd-regexp', and `shell-popd-regexp',
|
|
472 while `shell-pushd-tohome', `shell-pushd-dextract' and `shell-pushd-dunique'
|
|
473 control the behavior of the relevant command.
|
|
474
|
|
475 Environment variables are expanded, see function `substitute-in-file-name'."
|
|
476 (if shell-dirtrackp
|
|
477 ;; We fail gracefully if we think the command will fail in the shell.
|
|
478 (condition-case err
|
|
479 (let ((start (progn (string-match "^[; \t]*" str) ; skip whitespace
|
|
480 (match-end 0)))
|
|
481 end cmd arg1)
|
|
482 (while (string-match shell-command-regexp str start)
|
|
483 (setq end (match-end 0)
|
|
484 cmd (comint-arguments (substring str start end) 0 0)
|
|
485 arg1 (comint-arguments (substring str start end) 1 1))
|
|
486 (cond ((string-match (concat "\\`\\(" shell-popd-regexp
|
|
487 "\\)\\($\\|[ \t]\\)")
|
|
488 cmd)
|
|
489 (shell-process-popd (substitute-in-file-name arg1)))
|
|
490 ((string-match (concat "\\`\\(" shell-pushd-regexp
|
|
491 "\\)\\($\\|[ \t]\\)")
|
|
492 cmd)
|
|
493 (shell-process-pushd (substitute-in-file-name arg1)))
|
|
494 ((string-match (concat "\\`\\(" shell-cd-regexp
|
|
495 "\\)\\($\\|[ \t]\\)")
|
|
496 cmd)
|
|
497 (shell-process-cd (substitute-in-file-name arg1))))
|
|
498 (setq start (progn (string-match "[; \t]*" str end) ; skip again
|
|
499 (match-end 0)))))
|
|
500 (error
|
|
501 ;; XEmacs change
|
|
502 (message nil)
|
|
503 (display-error err t)))))
|
|
504
|
|
505 ;; Like `cd', but prepends comint-file-name-prefix to absolute names.
|
|
506 (defun shell-cd-1 (dir dirstack)
|
|
507 (if shell-dirtrackp
|
|
508 (setq list-buffers-directory (file-name-as-directory
|
|
509 (expand-file-name dir))))
|
|
510 (condition-case nil
|
|
511 (progn (if (file-name-absolute-p dir)
|
|
512 (cd-absolute (concat comint-file-name-prefix dir))
|
|
513 (cd dir))
|
|
514 (setq shell-dirstack dirstack)
|
|
515 (shell-dirstack-message))
|
|
516 (file-error (message "Couldn't cd."))))
|
|
517
|
|
518 ;;; popd [+n]
|
|
519 (defun shell-process-popd (arg)
|
|
520 (let ((num (or (shell-extract-num arg) 0)))
|
|
521 (cond ((and num (= num 0) shell-dirstack)
|
|
522 (shell-cd-1 (car shell-dirstack) (cdr shell-dirstack)))
|
|
523 ((and num (> num 0) (<= num (length shell-dirstack)))
|
|
524 (let* ((ds (cons nil shell-dirstack))
|
|
525 (cell (nthcdr (1- num) ds)))
|
|
526 (rplacd cell (cdr (cdr cell)))
|
|
527 (setq shell-dirstack (cdr ds))
|
|
528 (shell-dirstack-message)))
|
|
529 (t
|
|
530 (error "Couldn't popd")))))
|
|
531
|
|
532 ;; Return DIR prefixed with comint-file-name-prefix as appropriate.
|
|
533 (defun shell-prefixed-directory-name (dir)
|
|
534 (if (= (length comint-file-name-prefix) 0)
|
|
535 dir
|
|
536 (if (file-name-absolute-p dir)
|
|
537 ;; The name is absolute, so prepend the prefix.
|
|
538 (concat comint-file-name-prefix dir)
|
|
539 ;; For relative name we assume default-directory already has the prefix.
|
|
540 (expand-file-name dir))))
|
|
541
|
|
542 ;;; cd [dir]
|
|
543 (defun shell-process-cd (arg)
|
|
544 (let ((new-dir (cond ((zerop (length arg)) (concat comint-file-name-prefix
|
|
545 "~"))
|
|
546 ((string-equal "-" arg) shell-last-dir)
|
|
547 (t (shell-prefixed-directory-name arg)))))
|
|
548 (setq shell-last-dir default-directory)
|
|
549 (shell-cd-1 new-dir shell-dirstack)))
|
|
550
|
|
551 ;;; pushd [+n | dir]
|
|
552 (defun shell-process-pushd (arg)
|
|
553 (let ((num (shell-extract-num arg)))
|
|
554 (cond ((zerop (length arg))
|
|
555 ;; no arg -- swap pwd and car of stack unless shell-pushd-tohome
|
|
556 (cond (shell-pushd-tohome
|
|
557 (shell-process-pushd (concat comint-file-name-prefix "~")))
|
|
558 (shell-dirstack
|
|
559 (let ((old default-directory))
|
|
560 (shell-cd-1 (car shell-dirstack)
|
|
561 (cons old (cdr shell-dirstack)))))
|
|
562 (t
|
|
563 (message "Directory stack empty."))))
|
|
564 ((numberp num)
|
|
565 ;; pushd +n
|
|
566 (cond ((> num (length shell-dirstack))
|
|
567 (message "Directory stack not that deep."))
|
|
568 ((= num 0)
|
|
569 (error (message "Couldn't cd.")))
|
|
570 (shell-pushd-dextract
|
|
571 (let ((dir (nth (1- num) shell-dirstack)))
|
|
572 (shell-process-popd arg)
|
|
573 (shell-process-pushd default-directory)
|
|
574 (shell-cd-1 dir shell-dirstack)))
|
|
575 (t
|
|
576 (let* ((ds (cons default-directory shell-dirstack))
|
|
577 (dslen (length ds))
|
|
578 (front (nthcdr num ds))
|
|
579 (back (reverse (nthcdr (- dslen num) (reverse ds))))
|
|
580 (new-ds (append front back)))
|
|
581 (shell-cd-1 (car new-ds) (cdr new-ds))))))
|
|
582 (t
|
|
583 ;; pushd <dir>
|
|
584 (let ((old-wd default-directory))
|
|
585 (shell-cd-1 (shell-prefixed-directory-name arg)
|
|
586 (if (or (null shell-pushd-dunique)
|
|
587 (not (member old-wd shell-dirstack)))
|
|
588 (cons old-wd shell-dirstack)
|
|
589 shell-dirstack)))))))
|
|
590
|
|
591 ;; If STR is of the form +n, for n>0, return n. Otherwise, nil.
|
|
592 (defun shell-extract-num (str)
|
|
593 (and (string-match "^\\+[1-9][0-9]*$" str)
|
|
594 (string-to-int str)))
|
|
595
|
|
596
|
|
597 (defun shell-dirtrack-toggle ()
|
|
598 "Turn directory tracking on and off in a shell buffer."
|
|
599 (interactive)
|
|
600 (if (setq shell-dirtrackp (not shell-dirtrackp))
|
|
601 (setq list-buffers-directory default-directory)
|
|
602 (setq list-buffers-directory nil))
|
|
603 (message "Directory tracking %s" (if shell-dirtrackp "ON" "OFF")))
|
|
604
|
|
605 ;;; For your typing convenience:
|
|
606 ;; XEmacs: removed this because then `M-x dir' doesn't complete to `dired'
|
|
607 ;;(define-function 'dirtrack-toggle 'shell-dirtrack-toggle)
|
|
608
|
|
609 (defun shell-cd (dir)
|
|
610 "Do normal `cd' to DIR, and set `list-buffers-directory'."
|
|
611 (if shell-dirtrackp
|
|
612 (setq list-buffers-directory (file-name-as-directory
|
|
613 (expand-file-name dir))))
|
|
614 (cd dir))
|
|
615
|
|
616 (defun shell-resync-dirs ()
|
|
617 "Resync the buffer's idea of the current directory stack.
|
|
618 This command queries the shell with the command bound to
|
|
619 `shell-dirstack-query' (default \"dirs\"), reads the next
|
|
620 line output and parses it to form the new directory stack.
|
|
621 DON'T issue this command unless the buffer is at a shell prompt.
|
|
622 Also, note that if some other subprocess decides to do output
|
|
623 immediately after the query, its output will be taken as the
|
|
624 new directory stack -- you lose. If this happens, just do the
|
|
625 command again."
|
|
626 (interactive)
|
|
627 (let* ((proc (get-buffer-process (current-buffer)))
|
|
628 (pmark (process-mark proc)))
|
|
629 (goto-char pmark)
|
|
630 (insert shell-dirstack-query) (insert "\n")
|
|
631 (sit-for 0) ; force redisplay
|
|
632 (comint-send-string proc shell-dirstack-query)
|
|
633 (comint-send-string proc "\n")
|
|
634 (set-marker pmark (point))
|
|
635 (let ((pt (point))) ; wait for 1 line
|
|
636 ;; This extra newline prevents the user's pending input from spoofing us.
|
|
637 (insert "\n") (backward-char 1)
|
|
638 (while (not (looking-at ".+\n"))
|
|
639 (accept-process-output proc)
|
|
640 (goto-char pt)
|
|
641 ;; kludge to cope with shells that have "stty echo" turned on.
|
|
642 ;; of course this will lose if there is only one dir on the stack
|
|
643 ;; and it is named "dirs"... -jwz
|
|
644 (if (looking-at "^dirs\r?\n") (delete-region (point) (match-end 0)))
|
|
645 ))
|
|
646 (goto-char pmark) (delete-char 1) ; remove the extra newline
|
|
647 ;; That's the dirlist. grab it & parse it.
|
|
648 (let* ((dl (buffer-substring (match-beginning 0) (1- (match-end 0))))
|
|
649 (dl-len (length dl))
|
|
650 (ds '()) ; new dir stack
|
|
651 (i 0))
|
|
652 (while (< i dl-len)
|
|
653 ;; regexp = optional whitespace, (non-whitespace), optional whitespace
|
|
654 (string-match "\\s *\\(\\S +\\)\\s *" dl i) ; pick off next dir
|
|
655 (setq ds (cons (concat comint-file-name-prefix
|
|
656 (substring dl (match-beginning 1)
|
|
657 (match-end 1)))
|
|
658 ds))
|
|
659 (setq i (match-end 0)))
|
|
660 (let ((ds (reverse ds)))
|
|
661 (shell-cd-1 (car ds) (cdr ds))))))
|
|
662
|
|
663 ;;; For your typing convenience:
|
|
664 ;; XEmacs: removed this because then `M-x dir' doesn't complete to `dired'
|
|
665 ;(define-function 'dirs 'shell-resync-dirs)
|
|
666
|
|
667 ;; XEmacs addition
|
|
668 (defvar shell-dirstack-message-hook nil
|
|
669 "Hook to run after a cd, pushd or popd event")
|
|
670
|
|
671 ;;; Show the current dirstack on the message line.
|
|
672 ;;; Pretty up dirs a bit by changing "/usr/jqr/foo" to "~/foo".
|
|
673 ;;; (This isn't necessary if the dirlisting is generated with a simple "dirs".)
|
|
674 ;;; All the commands that mung the buffer's dirstack finish by calling
|
|
675 ;;; this guy.
|
|
676 (defun shell-dirstack-message ()
|
|
677 (let* ((msg "")
|
|
678 (ds (cons default-directory shell-dirstack))
|
|
679 (home (format "^%s\\(/\\|$\\)" (regexp-quote (getenv "HOME"))))
|
|
680 (prefix (and comint-file-name-prefix
|
|
681 ;; XEmacs addition: don't turn "/foo" into "foo" !!
|
|
682 (not (= 0 (length comint-file-name-prefix)))
|
|
683 (format "^%s\\(/\\|$\\)"
|
|
684 (regexp-quote comint-file-name-prefix)))))
|
|
685 (while ds
|
|
686 (let ((dir (car ds)))
|
|
687 (if (string-match home dir)
|
|
688 (setq dir (concat "~/" (substring dir (match-end 0)))))
|
|
689 ;; Strip off comint-file-name-prefix if present.
|
|
690 (and prefix (string-match prefix dir)
|
|
691 (setq dir (substring dir (match-end 0)))
|
|
692 (setcar ds dir)
|
|
693 )
|
|
694 (setq msg (concat msg dir " "))
|
|
695 (setq ds (cdr ds))))
|
|
696 ;; XEmacs change
|
|
697 (run-hooks 'shell-dirstack-message-hook)
|
|
698 (message msg)))
|
|
699
|
|
700
|
|
701 (defun shell-forward-command (&optional arg)
|
|
702 "Move forward across ARG shell command(s). Does not cross lines.
|
|
703 See `shell-command-regexp'."
|
|
704 (interactive "p")
|
|
705 (let ((limit (save-excursion (end-of-line nil) (point))))
|
|
706 (if (re-search-forward (concat shell-command-regexp "\\([;&|][\t ]*\\)+")
|
|
707 limit 'move arg)
|
|
708 (skip-syntax-backward " "))))
|
|
709
|
|
710
|
|
711 (defun shell-backward-command (&optional arg)
|
|
712 "Move backward across ARG shell command(s). Does not cross lines.
|
|
713 See `shell-command-regexp'."
|
|
714 (interactive "p")
|
|
715 (let ((limit (save-excursion (comint-bol nil) (point))))
|
|
716 (if (> limit (point))
|
|
717 (save-excursion (beginning-of-line) (setq limit (point))))
|
|
718 (skip-syntax-backward " " limit)
|
|
719 (if (re-search-backward
|
|
720 (format "[;&|]+[\t ]*\\(%s\\)" shell-command-regexp) limit 'move arg)
|
|
721 (progn (goto-char (match-beginning 1))
|
|
722 (skip-chars-forward ";&|")))))
|
|
723
|
|
724
|
|
725 (defun shell-dynamic-complete-command ()
|
|
726 "Dynamically complete the command at point.
|
|
727 This function is similar to `comint-dynamic-complete-filename', except that it
|
|
728 searches `exec-path' (minus the trailing emacs library path) for completion
|
|
729 candidates. Note that this may not be the same as the shell's idea of the
|
|
730 path.
|
|
731
|
|
732 Completion is dependent on the value of `shell-completion-execonly', plus
|
|
733 those that effect file completion. See `shell-dynamic-complete-as-command'.
|
|
734
|
|
735 Returns t if successful."
|
|
736 (interactive)
|
|
737 (let ((filename (comint-match-partial-filename)))
|
|
738 (if (and filename
|
|
739 (save-match-data (not (string-match "[~/]" filename)))
|
|
740 (eq (match-beginning 0)
|
|
741 (save-excursion (shell-backward-command 1) (point))))
|
|
742 (prog2 (message "Completing command name...")
|
|
743 (shell-dynamic-complete-as-command)))))
|
|
744
|
|
745
|
|
746 (defun shell-dynamic-complete-as-command ()
|
|
747 "Dynamically complete at point as a command.
|
|
748 See `shell-dynamic-complete-filename'. Returns t if successful."
|
|
749 (let* ((filename (or (comint-match-partial-filename) ""))
|
|
750 (pathnondir (file-name-nondirectory filename))
|
|
751 (paths (cdr (reverse exec-path)))
|
|
752 (cwd (file-name-as-directory (expand-file-name default-directory)))
|
|
753 (ignored-extensions
|
|
754 (and comint-completion-fignore
|
|
755 (mapconcat (function (lambda (x) (concat (regexp-quote x) "$")))
|
|
756 comint-completion-fignore "\\|")))
|
|
757 (path "") (comps-in-path ()) (file "") (filepath "") (completions ()))
|
|
758 ;; Go thru each path in the search path, finding completions.
|
|
759 (while paths
|
|
760 (setq path (file-name-as-directory (comint-directory (or (car paths) ".")))
|
|
761 comps-in-path (and (file-accessible-directory-p path)
|
|
762 (file-name-all-completions pathnondir path)))
|
|
763 ;; Go thru each completion found, to see whether it should be used.
|
|
764 (while comps-in-path
|
|
765 (setq file (car comps-in-path)
|
|
766 filepath (concat path file))
|
|
767 (if (and (not (member file completions))
|
|
768 (not (and ignored-extensions
|
|
769 (string-match ignored-extensions file)))
|
|
770 (or (string-equal path cwd)
|
|
771 (not (file-directory-p filepath)))
|
|
772 (or (null shell-completion-execonly)
|
|
773 (file-executable-p filepath)))
|
|
774 (setq completions (cons file completions)))
|
|
775 (setq comps-in-path (cdr comps-in-path)))
|
|
776 (setq paths (cdr paths)))
|
|
777 ;; OK, we've got a list of completions.
|
|
778 (let ((success (let ((comint-completion-addsuffix nil))
|
|
779 (comint-dynamic-simple-complete pathnondir completions))))
|
|
780 (if (and (memq success '(sole shortest)) comint-completion-addsuffix
|
|
781 (not (file-directory-p (comint-match-partial-filename))))
|
|
782 (insert " "))
|
|
783 success)))
|
|
784
|
|
785
|
|
786 (defun shell-match-partial-variable ()
|
|
787 "Return the variable at point, or nil if non is found."
|
|
788 (save-excursion
|
|
789 (let ((limit (point)))
|
|
790 (if (re-search-backward "[^A-Za-z0-9_{}]" nil 'move)
|
|
791 (or (looking-at "\\$") (forward-char 1)))
|
|
792 ;; Anchor the search forwards.
|
|
793 (if (or (eolp) (looking-at "[^A-Za-z0-9_{}$]"))
|
|
794 nil
|
|
795 (re-search-forward "\\$?{?[A-Za-z0-9_]*}?" limit)
|
|
796 (buffer-substring (match-beginning 0) (match-end 0))))))
|
|
797
|
|
798
|
|
799 (defun shell-dynamic-complete-environment-variable ()
|
|
800 "Dynamically complete the environment variable at point.
|
|
801 Completes if after a variable, i.e., if it starts with a \"$\".
|
|
802 See `shell-dynamic-complete-as-environment-variable'.
|
|
803
|
|
804 This function is similar to `comint-dynamic-complete-filename', except that it
|
|
805 searches `process-environment' for completion candidates. Note that this may
|
|
806 not be the same as the interpreter's idea of variable names. The main problem
|
|
807 with this type of completion is that `process-environment' is the environment
|
|
808 which Emacs started with. Emacs does not track changes to the environment made
|
|
809 by the interpreter. Perhaps it would be more accurate if this function was
|
|
810 called `shell-dynamic-complete-process-environment-variable'.
|
|
811
|
|
812 Returns non-nil if successful."
|
|
813 (interactive)
|
|
814 (let ((variable (shell-match-partial-variable)))
|
|
815 (if (and variable (string-match "^\\$" variable))
|
|
816 (prog2 (message "Completing variable name...")
|
|
817 (shell-dynamic-complete-as-environment-variable)))))
|
|
818
|
|
819
|
|
820 (defun shell-dynamic-complete-as-environment-variable ()
|
|
821 "Dynamically complete at point as an environment variable.
|
|
822 Used by `shell-dynamic-complete-environment-variable'.
|
|
823 Uses `comint-dynamic-simple-complete'."
|
|
824 (let* ((var (or (shell-match-partial-variable) ""))
|
|
825 (variable (substring var (or (string-match "[^$({]\\|$" var) 0)))
|
|
826 (variables (mapcar (function (lambda (x)
|
|
827 (substring x 0 (string-match "=" x))))
|
|
828 process-environment))
|
|
829 (addsuffix comint-completion-addsuffix)
|
|
830 (comint-completion-addsuffix nil)
|
|
831 (success (comint-dynamic-simple-complete variable variables)))
|
|
832 (if (memq success '(sole shortest))
|
|
833 (let* ((var (shell-match-partial-variable))
|
|
834 (variable (substring var (string-match "[^$({]" var)))
|
|
835 (protection (cond ((string-match "{" var) "}")
|
|
836 ((string-match "(" var) ")")
|
|
837 (t "")))
|
|
838 (suffix (cond ((null addsuffix) "")
|
|
839 ((file-directory-p
|
|
840 (comint-directory (getenv variable))) "/")
|
|
841 (t " "))))
|
|
842 (insert protection suffix)))
|
|
843 success))
|
|
844
|
|
845
|
|
846 (defun shell-replace-by-expanded-directory ()
|
|
847 "Expand directory stack reference before point.
|
|
848 Directory stack references are of the form \"=digit\" or \"=-\".
|
|
849 See `default-directory' and `shell-dirstack'.
|
|
850
|
|
851 Returns t if successful."
|
|
852 (interactive)
|
|
853 (if (comint-match-partial-filename)
|
|
854 (save-excursion
|
|
855 (goto-char (match-beginning 0))
|
|
856 (let ((stack (cons default-directory shell-dirstack))
|
|
857 (index (cond ((looking-at "=-/?")
|
|
858 (length shell-dirstack))
|
|
859 ((looking-at "=\\([0-9]+\\)")
|
|
860 (string-to-number
|
|
861 (buffer-substring
|
|
862 (match-beginning 1) (match-end 1)))))))
|
|
863 (cond ((null index)
|
|
864 nil)
|
|
865 ((>= index (length stack))
|
|
866 (error "Directory stack not that deep."))
|
|
867 (t
|
|
868 (replace-match (file-name-as-directory (nth index stack)) t t)
|
|
869 (message "Directory item: %d" index)
|
|
870 t))))))
|
|
871
|
|
872 (provide 'shell)
|
|
873
|
|
874 ;;; shell.el ends here
|