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