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