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