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