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