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