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