Commit | Line | Data |
---|---|---|
c0a193ea | 1 | ;;; shell.el --- specialized comint.el for running the shell -*- lexical-binding: t -*- |
5109bfb0 | 2 | |
ba318903 PE |
3 | ;; Copyright (C) 1988, 1993-1997, 2000-2014 Free Software Foundation, |
4 | ;; 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 | 38 | ;; For documentation on the functionality provided by comint mode, and |
333f9019 | 39 | ;; the hooks available for customizing it, see the file comint.el. |
b578f267 | 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 | ;;============================================================================= | |
865fe16f | 50 | ;; Some suggestions for your init file. |
a9ec2adb | 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 ^\ | |
3226d6ca | 73 | ;; c-c c-o comint-delete-output Delete last batch of process output |
a9e73449 | 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 | |
c0a193ea | 83 | ;; tab completion-at-point Complete filename/command/history |
b578f267 EN |
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 | |
e1dbe924 | 93 | ;; comint-prompt-regexp is initialized to shell-prompt-pattern, for backwards |
b578f267 | 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) | |
e17d9003 | 101 | (require 'pcomplete) |
630cc463 | 102 | |
b578f267 EN |
103 | ;;; Customization and Buffer Variables |
104 | ||
ed366e87 | 105 | (defgroup shell nil |
edf959f9 | 106 | "Running shell from within Emacs buffers." |
ed366e87 RS |
107 | :group 'processes |
108 | :group 'unix) | |
109 | ||
110 | (defgroup shell-directories nil | |
edf959f9 | 111 | "Directory support in shell mode." |
ed366e87 RS |
112 | :group 'shell) |
113 | ||
ed8be7ff GM |
114 | ;; Unused. |
115 | ;;; (defgroup shell-faces nil | |
116 | ;;; "Faces in shell buffers." | |
117 | ;;; :group 'shell) | |
ed366e87 | 118 | |
c88ab9ce | 119 | ;;;###autoload |
1e8780b1 | 120 | (defcustom shell-dumb-shell-regexp (purecopy "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 |
d079ee5f | 141 | on lines which don't start with a prompt." |
fad22e2e RS |
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. | |
d079ee5f | 149 | Some people like (\"~\" \"#\" \"%\")." |
ed366e87 RS |
150 | :type '(repeat (string :tag "Suffix")) |
151 | :group 'shell) | |
e11284d5 | 152 | |
13932042 | 153 | (defcustom shell-delimiter-argument-list '(?\| ?& ?< ?> ?\( ?\) ?\;) |
2df2f980 | 154 | "List of characters to recognize as separate arguments. |
56eb1060 | 155 | This variable is used to initialize `comint-delimiter-argument-list' in the |
64a14c74 GM |
156 | shell buffer. The value may depend on the operating system or shell." |
157 | :type '(choice (const nil) | |
158 | (repeat :tag "List of characters" character)) | |
64a14c74 | 159 | :group 'shell) |
39d1a2ce | 160 | |
d079ee5f | 161 | (defcustom shell-file-name-chars |
c60ee5e7 | 162 | (if (memq system-type '(ms-dos windows-nt cygwin)) |
4aed0379 | 163 | "~/A-Za-z0-9_^$!#%&{}@`'.,:()-" |
f0d5dc24 | 164 | "[]~/A-Za-z0-9+@:_.$#%,={}-") |
76f63ea0 RS |
165 | "String of characters valid in a file name. |
166 | This variable is used to initialize `comint-file-name-chars' in the | |
d079ee5f CY |
167 | shell buffer. The value may depend on the operating system or shell." |
168 | :type 'string | |
169 | :group 'shell) | |
d76b71af | 170 | |
d079ee5f | 171 | (defcustom shell-file-name-quote-list |
39d1a2ce SM |
172 | (if (memq system-type '(ms-dos windows-nt)) |
173 | nil | |
3ca5dc5b | 174 | (append shell-delimiter-argument-list '(?\s ?$ ?\* ?\! ?\" ?\' ?\` ?\# ?\\))) |
7b4c6503 SM |
175 | "List of characters to quote when in a file name. |
176 | This variable is used to initialize `comint-file-name-quote-list' in the | |
d079ee5f CY |
177 | shell buffer. The value may depend on the operating system or shell." |
178 | :type '(repeat character) | |
179 | :group 'shell) | |
7b4c6503 | 180 | |
d079ee5f | 181 | (defcustom shell-dynamic-complete-functions |
c0a193ea SM |
182 | '(comint-c-a-p-replace-by-expanded-history |
183 | shell-environment-variable-completion | |
184 | shell-command-completion | |
185 | shell-c-a-p-replace-by-expanded-directory | |
7baca3bc | 186 | pcomplete-completions-at-point |
c0a193ea | 187 | shell-filename-completion |
7baca3bc | 188 | comint-filename-completion) |
988a4d60 | 189 | "List of functions called to perform completion. |
2df2f980 | 190 | This variable is used to initialize `comint-dynamic-complete-functions' in the |
d079ee5f CY |
191 | shell buffer." |
192 | :type '(repeat function) | |
193 | :group 'shell) | |
988a4d60 | 194 | |
ed366e87 | 195 | (defcustom shell-command-regexp "[^;&|\n]+" |
05327ca9 | 196 | "Regexp to match a single command within a pipeline. |
ed366e87 RS |
197 | This is used for directory tracking and does not do a perfect job." |
198 | :type 'regexp | |
199 | :group 'shell) | |
d76b71af | 200 | |
6ed020c8 | 201 | (defcustom shell-command-separator-regexp "[;&|\n \t]*" |
05327ca9 | 202 | "Regexp to match a single command within a pipeline. |
6ed020c8 JB |
203 | This is used for directory tracking and does not do a perfect job." |
204 | :type 'regexp | |
205 | :group 'shell) | |
206 | ||
ed366e87 | 207 | (defcustom shell-completion-execonly t |
05327ca9 | 208 | "If non-nil, use executable files only for completion candidates. |
5394cb44 | 209 | This mirrors the optional behavior of tcsh. |
d76b71af | 210 | |
ed366e87 RS |
211 | Detecting executability of files may slow command completion considerably." |
212 | :type 'boolean | |
213 | :group 'shell) | |
d76b71af | 214 | |
ed366e87 | 215 | (defcustom shell-popd-regexp "popd" |
05327ca9 | 216 | "Regexp to match subshell commands equivalent to popd." |
ed366e87 RS |
217 | :type 'regexp |
218 | :group 'shell-directories) | |
be9b65ac | 219 | |
ed366e87 | 220 | (defcustom shell-pushd-regexp "pushd" |
05327ca9 | 221 | "Regexp to match subshell commands equivalent to pushd." |
ed366e87 RS |
222 | :type 'regexp |
223 | :group 'shell-directories) | |
be9b65ac | 224 | |
ed366e87 | 225 | (defcustom shell-pushd-tohome nil |
05327ca9 | 226 | "If non-nil, make pushd with no arg behave as \"pushd ~\" (like cd). |
ed366e87 RS |
227 | This mirrors the optional behavior of tcsh." |
228 | :type 'boolean | |
229 | :group 'shell-directories) | |
d76b71af | 230 | |
ed366e87 | 231 | (defcustom shell-pushd-dextract nil |
05327ca9 | 232 | "If non-nil, make \"pushd +n\" pop the nth dir to the stack top. |
ed366e87 RS |
233 | This mirrors the optional behavior of tcsh." |
234 | :type 'boolean | |
235 | :group 'shell-directories) | |
d76b71af | 236 | |
ed366e87 | 237 | (defcustom shell-pushd-dunique nil |
05327ca9 | 238 | "If non-nil, make pushd only add unique directories to the stack. |
ed366e87 RS |
239 | This mirrors the optional behavior of tcsh." |
240 | :type 'boolean | |
241 | :group 'shell-directories) | |
d76b71af | 242 | |
ed366e87 | 243 | (defcustom shell-cd-regexp "cd" |
05327ca9 | 244 | "Regexp to match subshell commands equivalent to cd." |
ed366e87 RS |
245 | :type 'regexp |
246 | :group 'shell-directories) | |
be9b65ac | 247 | |
ed366e87 | 248 | (defcustom shell-chdrive-regexp |
84728570 | 249 | (if (memq system-type '(ms-dos windows-nt)) |
32d01212 RS |
250 | ; NetWare allows the five chars between upper and lower alphabetics. |
251 | "[]a-zA-Z^_`\\[\\\\]:" | |
252 | nil) | |
05327ca9 | 253 | "If non-nil, is regexp used to track drive changes." |
233f3fb8 RS |
254 | :type '(choice regexp |
255 | (const nil)) | |
ed366e87 | 256 | :group 'shell-directories) |
32d01212 | 257 | |
8a6387ed | 258 | (defcustom shell-dirtrack-verbose t |
05327ca9 | 259 | "If non-nil, show the directory stack following directory change. |
a034393c | 260 | This is effective only if directory tracking is enabled. |
c004e539 GM |
261 | The `dirtrack' package provides an alternative implementation of this feature - |
262 | see the function `dirtrack-mode'." | |
8a6387ed SM |
263 | :type 'boolean |
264 | :group 'shell-directories) | |
265 | ||
ed366e87 | 266 | (defcustom explicit-shell-file-name nil |
05327ca9 | 267 | "If non-nil, is file name to use for explicitly requested inferior shell." |
ed366e87 RS |
268 | :type '(choice (const :tag "None" nil) file) |
269 | :group 'shell) | |
be9b65ac | 270 | |
400b0022 KS |
271 | ;; Note: There are no explicit references to the variable `explicit-csh-args'. |
272 | ;; It is used implicitly by M-x shell when the shell is `csh'. | |
ed366e87 | 273 | (defcustom explicit-csh-args |
be9b65ac DL |
274 | (if (eq system-type 'hpux) |
275 | ;; -T persuades HP's csh not to think it is smarter | |
276 | ;; than us about what terminal modes to use. | |
277 | '("-i" "-T") | |
278 | '("-i")) | |
05327ca9 | 279 | "Args passed to inferior shell by \\[shell], if the shell is csh. |
ed366e87 RS |
280 | Value is a list of strings, which may be nil." |
281 | :type '(repeat (string :tag "Argument")) | |
282 | :group 'shell) | |
be9b65ac | 283 | |
400b0022 KS |
284 | ;; Note: There are no explicit references to the variable `explicit-bash-args'. |
285 | ;; It is used implicitly by M-x shell when the interactive shell is `bash'. | |
6674676b | 286 | (defcustom explicit-bash-args |
0463c1ef PE |
287 | ;; Tell bash not to use readline. It's safe to assume --noediting now, |
288 | ;; as it was introduced in 1996 in Bash version 2. | |
289 | '("--noediting" "-i") | |
05327ca9 | 290 | "Args passed to inferior shell by \\[shell], if the shell is bash. |
6674676b AS |
291 | Value is a list of strings, which may be nil." |
292 | :type '(repeat (string :tag "Argument")) | |
293 | :group 'shell) | |
294 | ||
ed366e87 | 295 | (defcustom shell-input-autoexpand 'history |
05327ca9 | 296 | "If non-nil, expand input command history references on completion. |
4f99443b RS |
297 | This mirrors the optional behavior of tcsh (its autoexpand and histlit). |
298 | ||
299 | If the value is `input', then the expansion is seen on input. | |
300 | If the value is `history', then the expansion is only when inserting | |
301 | into the buffer's input ring. See also `comint-magic-space' and | |
c0a193ea | 302 | `comint-dynamic-complete-functions'. |
4f99443b RS |
303 | |
304 | This variable supplies a default for `comint-input-autoexpand', | |
ed366e87 | 305 | for Shell mode only." |
6f0984e5 AS |
306 | :type '(choice (const :tag "off" nil) |
307 | (const input) | |
308 | (const history) | |
309 | (const :tag "on" t)) | |
362ae0b3 | 310 | :group 'shell) |
4f99443b | 311 | |
be9b65ac | 312 | (defvar shell-dirstack nil |
c88ab9ce ER |
313 | "List of directories saved by pushd in this buffer's shell. |
314 | Thus, this does not include the shell's current directory.") | |
be9b65ac | 315 | |
b493a9b2 RS |
316 | (defvar shell-dirtrackp t |
317 | "Non-nil in a shell buffer means directory tracking is enabled.") | |
318 | ||
fa8f1b25 ER |
319 | (defvar shell-last-dir nil |
320 | "Keep track of last directory for ksh `cd -' command.") | |
321 | ||
c63e83a4 | 322 | (defvar shell-dirstack-query nil |
4dea1735 | 323 | "Command used by `shell-resync-dirs' to query the shell.") |
be9b65ac | 324 | |
a0310a6c DN |
325 | (defvar shell-mode-map |
326 | (let ((map (nconc (make-sparse-keymap) comint-mode-map))) | |
327 | (define-key map "\C-c\C-f" 'shell-forward-command) | |
328 | (define-key map "\C-c\C-b" 'shell-backward-command) | |
c0a193ea | 329 | (define-key map "\t" 'completion-at-point) |
a0310a6c DN |
330 | (define-key map (kbd "M-RET") 'shell-resync-dirs) |
331 | (define-key map "\M-?" 'comint-dynamic-list-filename-completions) | |
332 | (define-key map [menu-bar completion] | |
333 | (cons "Complete" | |
334 | (copy-keymap (lookup-key comint-mode-map [menu-bar completion])))) | |
335 | (define-key-after (lookup-key map [menu-bar completion]) | |
336 | [complete-env-variable] '("Complete Env. Variable Name" . | |
337 | shell-dynamic-complete-environment-variable) | |
338 | 'complete-file) | |
339 | (define-key-after (lookup-key map [menu-bar completion]) | |
340 | [expand-directory] '("Expand Directory Reference" . | |
341 | shell-replace-by-expanded-directory) | |
342 | 'complete-expand) | |
343 | map)) | |
be9b65ac | 344 | |
ed366e87 | 345 | (defcustom shell-mode-hook '() |
5a0c3f56 | 346 | "Hook for customizing Shell mode." |
ed366e87 RS |
347 | :type 'hook |
348 | :group 'shell) | |
be9b65ac | 349 | |
5945a1f7 | 350 | (defvar shell-font-lock-keywords |
cb6e07b1 | 351 | '(("[ \t]\\([+-][^ \t\n]+\\)" 1 font-lock-comment-face) |
473fbd89 SM |
352 | ("^[^ \t\n]+:.*" . font-lock-string-face) |
353 | ("^\\[[1-9][0-9]*\\]" . font-lock-string-face)) | |
5945a1f7 | 354 | "Additional expressions to highlight in Shell mode.") |
84728570 | 355 | |
be9b65ac | 356 | ;;; Basic Procedures |
be9b65ac | 357 | |
2473256d SM |
358 | (defun shell--unquote&requote-argument (qstr &optional upos) |
359 | (unless upos (setq upos 0)) | |
360 | (let* ((qpos 0) | |
361 | (dquotes nil) | |
362 | (ustrs '()) | |
363 | (re (concat | |
364 | "[\"']" | |
365 | "\\|\\$\\(?:\\([[:alpha:]][[:alnum:]]*\\)" | |
366 | "\\|{\\(?1:[^{}]+\\)}\\)" | |
367 | (when (memq system-type '(ms-dos windows-nt)) | |
368 | "\\|%\\(?1:[^\\\\/]*\\)%") | |
369 | (when comint-file-name-quote-list | |
370 | "\\|\\\\\\(.\\)"))) | |
371 | (qupos nil) | |
372 | (push (lambda (str end) | |
373 | (push str ustrs) | |
374 | (setq upos (- upos (length str))) | |
375 | (unless (or qupos (> upos 0)) | |
376 | (setq qupos (if (< end 0) (- end) (+ upos end)))))) | |
377 | match) | |
378 | (while (setq match (string-match re qstr qpos)) | |
379 | (funcall push (substring qstr qpos match) match) | |
380 | (cond | |
381 | ((match-beginning 2) (funcall push (match-string 2 qstr) (match-end 0))) | |
382 | ((match-beginning 1) (funcall push (getenv (match-string 1 qstr)) | |
383 | (- (match-end 0)))) | |
384 | ((eq (aref qstr match) ?\") (setq dquotes (not dquotes))) | |
385 | ((eq (aref qstr match) ?\') | |
386 | (cond | |
387 | (dquotes (funcall push "'" (match-end 0))) | |
388 | ((< match (1+ (length qstr))) | |
389 | (let ((end (string-match "'" qstr (1+ match)))) | |
390 | (funcall push (substring qstr (1+ match) end) | |
391 | (or end (length qstr))))) | |
392 | (t nil))) | |
393 | (t (error "Unexpected case in shell--unquote&requote-argument!"))) | |
394 | (setq qpos (match-end 0))) | |
395 | (funcall push (substring qstr qpos) (length qstr)) | |
396 | (list (mapconcat #'identity (nreverse ustrs) "") | |
397 | qupos #'comint-quote-filename))) | |
398 | ||
399 | (defun shell--unquote-argument (str) | |
400 | (car (shell--unquote&requote-argument str))) | |
401 | (defun shell--requote-argument (upos qstr) | |
402 | ;; See `completion-table-with-quoting'. | |
403 | (let ((res (shell--unquote&requote-argument qstr upos))) | |
404 | (cons (nth 1 res) (nth 2 res)))) | |
405 | ||
406 | (defun shell--parse-pcomplete-arguments () | |
b3fd59bd | 407 | "Parse whitespace separated arguments in the current region." |
2473256d | 408 | ;; FIXME: share code with shell--unquote&requote-argument. |
b3fd59bd SM |
409 | (let ((begin (save-excursion (shell-backward-command 1) (point))) |
410 | (end (point)) | |
411 | begins args) | |
412 | (save-excursion | |
413 | (goto-char begin) | |
414 | (while (< (point) end) | |
415 | (skip-chars-forward " \t\n") | |
416 | (push (point) begins) | |
356a3681 SM |
417 | (let ((arg ())) |
418 | (while (looking-at | |
419 | (eval-when-compile | |
420 | (concat | |
421 | "\\(?:[^\s\t\n\\\"']+" | |
422 | "\\|'\\([^']*\\)'?" | |
423 | "\\|\"\\(\\(?:[^\"\\]\\|\\\\.\\)*\\)\"?" | |
424 | "\\|\\\\\\(\\(?:.\\|\n\\)?\\)\\)"))) | |
425 | (goto-char (match-end 0)) | |
426 | (cond | |
427 | ((match-beginning 3) ;Backslash escape. | |
3d53ee1b | 428 | (push (cond |
2473256d | 429 | ((null comint-file-name-quote-list) |
3d53ee1b SM |
430 | (goto-char (match-beginning 3)) "\\") |
431 | ((= (match-beginning 3) (match-end 3)) "\\") | |
432 | (t (match-string 3))) | |
356a3681 SM |
433 | arg)) |
434 | ((match-beginning 2) ;Double quote. | |
2473256d | 435 | (push (if (null comint-file-name-quote-list) (match-string 2) |
fe263b8f SM |
436 | (replace-regexp-in-string |
437 | "\\\\\\(.\\)" "\\1" (match-string 2))) | |
356a3681 SM |
438 | arg)) |
439 | ((match-beginning 1) ;Single quote. | |
440 | (push (match-string 1) arg)) | |
441 | (t (push (match-string 0) arg)))) | |
442 | (push (mapconcat #'identity (nreverse arg) "") args))) | |
b3fd59bd | 443 | (cons (nreverse args) (nreverse begins))))) |
8e249bbd | 444 | |
7baca3bc CY |
445 | (defun shell-command-completion-function () |
446 | "Completion function for shell command names. | |
447 | This is the value of `pcomplete-command-completion-function' for | |
448 | Shell buffers. It implements `shell-completion-execonly' for | |
449 | `pcomplete' completion." | |
450 | (pcomplete-here (pcomplete-entries nil | |
451 | (if shell-completion-execonly | |
452 | 'file-executable-p)))) | |
453 | ||
914a0ae1 SM |
454 | (defun shell-completion-vars () |
455 | "Setup completion vars for `shell-mode' and `read-shell-command'." | |
456 | (set (make-local-variable 'comint-completion-fignore) | |
457 | shell-completion-fignore) | |
458 | (set (make-local-variable 'comint-delimiter-argument-list) | |
459 | shell-delimiter-argument-list) | |
460 | (set (make-local-variable 'comint-file-name-chars) shell-file-name-chars) | |
461 | (set (make-local-variable 'comint-file-name-quote-list) | |
462 | shell-file-name-quote-list) | |
463 | (set (make-local-variable 'comint-dynamic-complete-functions) | |
464 | shell-dynamic-complete-functions) | |
2473256d SM |
465 | (setq-local comint-unquote-function #'shell--unquote-argument) |
466 | (setq-local comint-requote-function #'shell--requote-argument) | |
914a0ae1 | 467 | (set (make-local-variable 'pcomplete-parse-arguments-function) |
2473256d | 468 | #'shell--parse-pcomplete-arguments) |
1c6d8c76 SM |
469 | (set (make-local-variable 'pcomplete-termination-string) |
470 | (cond ((not comint-completion-addsuffix) "") | |
471 | ((stringp comint-completion-addsuffix) | |
472 | comint-completion-addsuffix) | |
473 | ((not (consp comint-completion-addsuffix)) " ") | |
474 | (t (cdr comint-completion-addsuffix)))) | |
7baca3bc CY |
475 | (set (make-local-variable 'pcomplete-command-completion-function) |
476 | #'shell-command-completion-function) | |
e17d9003 SM |
477 | ;; Don't use pcomplete's defaulting mechanism, rely on |
478 | ;; shell-dynamic-complete-functions instead. | |
479 | (set (make-local-variable 'pcomplete-default-completion-function) #'ignore) | |
914a0ae1 SM |
480 | (setq comint-input-autoexpand shell-input-autoexpand) |
481 | ;; Not needed in shell-mode because it's inherited from comint-mode, but | |
482 | ;; placed here for read-shell-command. | |
483 | (add-hook 'completion-at-point-functions 'comint-completion-at-point nil t)) | |
484 | ||
e9904b59 RS |
485 | (put 'shell-mode 'mode-class 'special) |
486 | ||
9f299ecb | 487 | (define-derived-mode shell-mode comint-mode "Shell" |
933907e9 | 488 | "Major mode for interacting with an inferior shell.\\<shell-mode-map> |
8079b590 SM |
489 | \\[comint-send-input] after the end of the process' output sends the text from |
490 | the end of process to the end of the current line. | |
491 | \\[comint-send-input] before end of process output copies the current line minus the prompt to | |
492 | the end of the buffer and sends it (\\[comint-copy-old-input] just copies the current line). | |
e9933cb1 | 493 | \\[send-invisible] reads a line of text without echoing it, and sends it to |
56783585 RS |
494 | the shell. This is useful for entering passwords. Or, add the function |
495 | `comint-watch-for-password-prompt' to `comint-output-filter-functions'. | |
be9b65ac | 496 | |
e9933cb1 SM |
497 | If you want to make multiple shell buffers, rename the `*shell*' buffer |
498 | using \\[rename-buffer] or \\[rename-uniquely] and start a new shell. | |
499 | ||
8079b590 | 500 | If you want to make shell buffers limited in length, add the function |
4841acc4 | 501 | `comint-truncate-buffer' to `comint-output-filter-functions'. |
8079b590 | 502 | |
be9b65ac DL |
503 | If you accidentally suspend your process, use \\[comint-continue-subjob] |
504 | to continue it. | |
505 | ||
e9933cb1 SM |
506 | `cd', `pushd' and `popd' commands given to the shell are watched by Emacs to |
507 | keep this buffer's default directory the same as the shell's working directory. | |
0b3c9cf1 SM |
508 | While directory tracking is enabled, the shell's working directory is displayed |
509 | by \\[list-buffers] or \\[mouse-buffer-menu] in the `File' field. | |
44e97401 | 510 | \\[dirs] queries the shell and resyncs Emacs's idea of what the current |
be9b65ac | 511 | directory stack is. |
a034393c GM |
512 | \\[shell-dirtrack-mode] turns directory tracking on and off. |
513 | \(The `dirtrack' package provides an alternative implementation of this | |
c004e539 | 514 | feature - see the function `dirtrack-mode'.) |
be9b65ac DL |
515 | |
516 | \\{shell-mode-map} | |
24fdffaa | 517 | Customization: Entry to this mode runs the hooks on `comint-mode-hook' and |
988a4d60 | 518 | `shell-mode-hook' (in that order). Before each input, the hooks on |
ba02c167 RS |
519 | `comint-input-filter-functions' are run. After each shell output, the hooks |
520 | on `comint-output-filter-functions' are run. | |
be9b65ac | 521 | |
84728570 SS |
522 | Variables `shell-cd-regexp', `shell-chdrive-regexp', `shell-pushd-regexp' |
523 | and `shell-popd-regexp' are used to match their respective commands, | |
524 | while `shell-pushd-tohome', `shell-pushd-dextract' and `shell-pushd-dunique' | |
32d01212 | 525 | control the behavior of the relevant command. |
d76b71af | 526 | |
e11284d5 RS |
527 | Variables `comint-completion-autolist', `comint-completion-addsuffix', |
528 | `comint-completion-recexact' and `comint-completion-fignore' control the | |
529 | behavior of file name, command name and variable name completion. Variable | |
530 | `shell-completion-execonly' controls the behavior of command name completion. | |
2df2f980 | 531 | Variable `shell-completion-fignore' is used to initialize the value of |
e11284d5 | 532 | `comint-completion-fignore'. |
d76b71af RS |
533 | |
534 | Variables `comint-input-ring-file-name' and `comint-input-autoexpand' control | |
ab646e49 | 535 | the initialization of the input ring history, and history expansion. |
d76b71af | 536 | |
ba02c167 | 537 | Variables `comint-output-filter-functions', a hook, and |
e11284d5 | 538 | `comint-scroll-to-bottom-on-input' and `comint-scroll-to-bottom-on-output' |
988a4d60 RS |
539 | control whether input and output cause the window to scroll to the end of the |
540 | buffer." | |
d76b71af | 541 | (setq comint-prompt-regexp shell-prompt-pattern) |
914a0ae1 | 542 | (shell-completion-vars) |
e03035e3 | 543 | (set (make-local-variable 'paragraph-separate) "\\'") |
914a0ae1 SM |
544 | (set (make-local-variable 'paragraph-start) comint-prompt-regexp) |
545 | (set (make-local-variable 'font-lock-defaults) '(shell-font-lock-keywords t)) | |
546 | (set (make-local-variable 'shell-dirstack) nil) | |
547 | (set (make-local-variable 'shell-last-dir) nil) | |
11ee8d90 | 548 | (shell-dirtrack-mode 1) |
0fd40f89 CY |
549 | |
550 | ;; By default, ansi-color applies faces using overlays. This is | |
551 | ;; very inefficient in Shell buffers (e.g. Bug#10835). We use a | |
552 | ;; custom `ansi-color-apply-face-function' to convert color escape | |
553 | ;; sequences into `font-lock-face' properties. | |
276479fd CY |
554 | (setq-local ansi-color-apply-face-function #'shell-apply-ansi-color) |
555 | (shell-reapply-ansi-color) | |
0fd40f89 | 556 | |
5d9719e6 RS |
557 | ;; This is not really correct, since the shell buffer does not really |
558 | ;; edit this directory. But it is useful in the buffer list and menus. | |
5d9719e6 | 559 | (setq list-buffers-directory (expand-file-name default-directory)) |
d76b71af | 560 | ;; shell-dependent assignments. |
6ba0fd3e | 561 | (when (ring-empty-p comint-input-ring) |
ee8107c0 | 562 | (let ((shell (file-name-nondirectory (car |
0be6f4f1 GM |
563 | (process-command (get-buffer-process (current-buffer)))))) |
564 | (hsize (getenv "HISTSIZE"))) | |
565 | (and (stringp hsize) | |
566 | (integerp (setq hsize (string-to-number hsize))) | |
567 | (> hsize 0) | |
568 | (set (make-local-variable 'comint-input-ring-size) hsize)) | |
ee8107c0 RS |
569 | (setq comint-input-ring-file-name |
570 | (or (getenv "HISTFILE") | |
571 | (cond ((string-equal shell "bash") "~/.bash_history") | |
572 | ((string-equal shell "ksh") "~/.sh_history") | |
573 | (t "~/.history")))) | |
574 | (if (or (equal comint-input-ring-file-name "") | |
575 | (equal (file-truename comint-input-ring-file-name) | |
576 | (file-truename "/dev/null"))) | |
577 | (setq comint-input-ring-file-name nil)) | |
578 | ;; Arrange to write out the input ring on exit, if the shell doesn't | |
579 | ;; do this itself. | |
580 | (if (and comint-input-ring-file-name | |
581 | (string-match shell-dumb-shell-regexp shell)) | |
582 | (set-process-sentinel (get-buffer-process (current-buffer)) | |
583 | #'shell-write-history-on-exit)) | |
584 | (setq shell-dirstack-query | |
585 | (cond ((string-equal shell "sh") "pwd") | |
586 | ((string-equal shell "ksh") "echo $PWD ~-") | |
3811bec8 LK |
587 | (t "dirs"))) |
588 | ;; Bypass a bug in certain versions of bash. | |
589 | (when (string-equal shell "bash") | |
c0a193ea | 590 | (add-hook 'comint-preoutput-filter-functions |
3811bec8 | 591 | 'shell-filter-ctrl-a-ctrl-b nil t))) |
ee8107c0 | 592 | (comint-read-input-ring t))) |
4dced927 | 593 | |
276479fd CY |
594 | (defun shell-apply-ansi-color (beg end face) |
595 | "Apply FACE as the ansi-color face for the text between BEG and END." | |
596 | (when face | |
597 | (put-text-property beg end 'ansi-color-face face) | |
598 | (put-text-property beg end 'font-lock-face face))) | |
599 | ||
600 | (defun shell-reapply-ansi-color () | |
601 | "Reapply ansi-color faces to the existing contents of the buffer." | |
602 | (save-restriction | |
603 | (widen) | |
604 | (let* ((pos (point-min)) | |
605 | (end (or (next-single-property-change pos 'ansi-color-face) | |
606 | (point-max))) | |
607 | face) | |
608 | (while end | |
609 | (if (setq face (get-text-property pos 'ansi-color-face)) | |
610 | (put-text-property pos (or end (point-max)) | |
611 | 'font-lock-face face)) | |
612 | (setq pos end | |
613 | end (next-single-property-change pos 'ansi-color-face)))))) | |
614 | ||
c0a193ea | 615 | (defun shell-filter-ctrl-a-ctrl-b (string) |
3811bec8 LK |
616 | "Remove `^A' and `^B' characters from comint output. |
617 | ||
618 | Bash uses these characters as internal quoting characters in its | |
619 | prompt. Due to a bug in some bash versions (including 2.03, | |
620 | 2.04, and 2.05b), they may erroneously show up when bash is | |
621 | started with the `--noediting' option and Select Graphic | |
622 | Rendition (SGR) control sequences (formerly known as ANSI escape | |
623 | sequences) are used to color the prompt. | |
624 | ||
c0a193ea SM |
625 | This function can be put on `comint-preoutput-filter-functions'." |
626 | (if (string-match "[\C-a\C-b]" string) | |
627 | (replace-regexp-in-string "[\C-a\C-b]" "" string t t) | |
628 | string)) | |
3811bec8 | 629 | |
4dced927 AI |
630 | (defun shell-write-history-on-exit (process event) |
631 | "Called when the shell process is stopped. | |
632 | ||
633 | Writes the input history to a history file | |
84728570 | 634 | `comint-input-ring-file-name' using `comint-write-input-ring' |
4dced927 AI |
635 | and inserts a short message in the shell buffer. |
636 | ||
637 | This function is a sentinel watching the shell interpreter process. | |
638 | Sentinels will always get the two parameters PROCESS and EVENT." | |
639 | ;; Write history. | |
640 | (comint-write-input-ring) | |
84728570 SS |
641 | (let ((buf (process-buffer process))) |
642 | (when (buffer-live-p buf) | |
643 | (with-current-buffer buf | |
644 | (insert (format "\nProcess %s %s\n" process event)))))) | |
645 | ||
c88ab9ce | 646 | ;;;###autoload |
fd1035aa MB |
647 | (defun shell (&optional buffer) |
648 | "Run an inferior shell, with I/O through BUFFER (which defaults to `*shell*'). | |
649 | Interactively, a prefix arg means to prompt for BUFFER. | |
66a100be MA |
650 | If `default-directory' is a remote file name, it is also prompted |
651 | to change if called with a prefix arg. | |
652 | ||
fd1035aa MB |
653 | If BUFFER exists but shell process is not running, make new shell. |
654 | If BUFFER exists and shell process is running, just switch to BUFFER. | |
24fdffaa | 655 | Program used comes from variable `explicit-shell-file-name', |
a9ec2adb | 656 | or (if that is nil) from the ESHELL environment variable, |
c3342e8e | 657 | or (if that is nil) from `shell-file-name'. |
ec1d7c6e | 658 | If a file `~/.emacs_SHELLNAME' exists, or `~/.emacs.d/init_SHELLNAME.sh', |
71af6fc4 RS |
659 | it is given as initial input (but this may be lost, due to a timing |
660 | error, if the shell discards input when it starts up). | |
fbc270e7 RS |
661 | The buffer is put in Shell mode, giving commands for sending input |
662 | and controlling the subjobs of the shell. See `shell-mode'. | |
663 | See also the variable `shell-prompt-pattern'. | |
be9b65ac | 664 | |
ebe4d555 RS |
665 | To specify a coding system for converting non-ASCII characters |
666 | in the input and output to the shell, use \\[universal-coding-system-argument] | |
667 | before \\[shell]. You can also specify this with \\[set-buffer-process-coding-system] | |
668 | in the shell buffer, after you start the shell. | |
669 | The default comes from `process-coding-system-alist' and | |
670 | `default-process-coding-system'. | |
671 | ||
a9ec2adb | 672 | The shell file name (sans directories) is used to make a symbol name |
62c9fad7 | 673 | such as `explicit-csh-args'. If that symbol is a variable, |
be9b65ac DL |
674 | its value is used as a list of arguments when invoking the shell. |
675 | Otherwise, one argument `-i' is passed to the shell. | |
676 | ||
677 | \(Type \\[describe-mode] in the shell buffer for a list of commands.)" | |
fd1035aa MB |
678 | (interactive |
679 | (list | |
680 | (and current-prefix-arg | |
d9af6951 AS |
681 | (prog1 |
682 | (read-buffer "Shell buffer: " | |
a813fe3f CY |
683 | ;; If the current buffer is an inactive |
684 | ;; shell buffer, use it as the default. | |
685 | (if (and (eq major-mode 'shell-mode) | |
686 | (null (get-buffer-process (current-buffer)))) | |
687 | (buffer-name) | |
688 | (generate-new-buffer-name "*shell*"))) | |
d9af6951 AS |
689 | (if (file-remote-p default-directory) |
690 | ;; It must be possible to declare a local default-directory. | |
8f5f1e68 SM |
691 | ;; FIXME: This can't be right: it changes the default-directory |
692 | ;; of the current-buffer rather than of the *shell* buffer. | |
d9af6951 AS |
693 | (setq default-directory |
694 | (expand-file-name | |
7e27ce9c | 695 | (read-directory-name |
d9af6951 | 696 | "Default directory: " default-directory default-directory |
7e27ce9c | 697 | t nil)))))))) |
8f5f1e68 SM |
698 | (setq buffer (if (or buffer (not (derived-mode-p 'shell-mode)) |
699 | (comint-check-proc (current-buffer))) | |
700 | (get-buffer-create (or buffer "*shell*")) | |
701 | ;; If the current buffer is a dead shell buffer, use it. | |
702 | (current-buffer))) | |
77c992bc MA |
703 | |
704 | ;; On remote hosts, the local `shell-file-name' might be useless. | |
06b60517 | 705 | (if (and (called-interactively-p 'any) |
77c992bc MA |
706 | (file-remote-p default-directory) |
707 | (null explicit-shell-file-name) | |
708 | (null (getenv "ESHELL"))) | |
709 | (with-current-buffer buffer | |
710 | (set (make-local-variable 'explicit-shell-file-name) | |
711 | (file-remote-p | |
712 | (expand-file-name | |
713 | (read-file-name | |
714 | "Remote shell path: " default-directory shell-file-name | |
715 | t shell-file-name)) | |
716 | 'localname)))) | |
717 | ||
37ac18a3 CY |
718 | ;; The buffer's window must be correctly set when we call comint (so |
719 | ;; that comint sets the COLUMNS env var properly). | |
72258fe5 | 720 | (pop-to-buffer-same-window buffer) |
5a45735a SM |
721 | (unless (comint-check-proc buffer) |
722 | (let* ((prog (or explicit-shell-file-name | |
723 | (getenv "ESHELL") shell-file-name)) | |
724 | (name (file-name-nondirectory prog)) | |
725 | (startfile (concat "~/.emacs_" name)) | |
726 | (xargs-name (intern-soft (concat "explicit-" name "-args")))) | |
ec1d7c6e | 727 | (unless (file-exists-p startfile) |
f7c94f43 | 728 | (setq startfile (concat user-emacs-directory "init_" name ".sh"))) |
5a45735a SM |
729 | (apply 'make-comint-in-buffer "shell" buffer prog |
730 | (if (file-exists-p startfile) startfile) | |
731 | (if (and xargs-name (boundp xargs-name)) | |
732 | (symbol-value xargs-name) | |
733 | '("-i"))) | |
734 | (shell-mode))) | |
735 | buffer) | |
519bfec4 | 736 | |
be9b65ac | 737 | ;;; Directory tracking |
05327ca9 SM |
738 | ;; |
739 | ;; This code provides the shell mode input sentinel | |
740 | ;; SHELL-DIRECTORY-TRACKER | |
741 | ;; that tracks cd, pushd, and popd commands issued to the shell, and | |
742 | ;; changes the current directory of the shell buffer accordingly. | |
743 | ;; | |
744 | ;; This is basically a fragile hack, although it's more accurate than | |
745 | ;; the version in Emacs 18's shell.el. It has the following failings: | |
746 | ;; 1. It doesn't know about the cdpath shell variable. | |
747 | ;; 2. It cannot infallibly deal with command sequences, though it does well | |
748 | ;; with these and with ignoring commands forked in another shell with ()s. | |
749 | ;; 3. More generally, any complex command is going to throw it. Otherwise, | |
750 | ;; you'd have to build an entire shell interpreter in Emacs Lisp. Failing | |
751 | ;; that, there's no way to catch shell commands where cd's are buried | |
752 | ;; inside conditional expressions, aliases, and so forth. | |
753 | ;; | |
754 | ;; The whole approach is a crock. Shell aliases mess it up. File sourcing | |
755 | ;; messes it up. You run other processes under the shell; these each have | |
756 | ;; separate working directories, and some have commands for manipulating | |
757 | ;; their w.d.'s (e.g., the lcd command in ftp). Some of these programs have | |
758 | ;; commands that do *not* affect the current w.d. at all, but look like they | |
759 | ;; do (e.g., the cd command in ftp). In shells that allow you job | |
760 | ;; control, you can switch between jobs, all having different w.d.'s. So | |
761 | ;; simply saying %3 can shift your w.d.. | |
762 | ;; | |
763 | ;; The solution is to relax, not stress out about it, and settle for | |
764 | ;; a hack that works pretty well in typical circumstances. Remember | |
765 | ;; that a half-assed solution is more in keeping with the spirit of Unix, | |
766 | ;; anyway. Blech. | |
767 | ;; | |
768 | ;; One good hack not implemented here for users of programmable shells | |
769 | ;; is to program up the shell w.d. manipulation commands to output | |
770 | ;; a coded command sequence to the tty. Something like | |
771 | ;; ESC | <cwd> | | |
772 | ;; where <cwd> is the new current working directory. Then trash the | |
773 | ;; directory tracking machinery currently used in this package, and | |
774 | ;; replace it with a process filter that watches for and strips out | |
775 | ;; these messages. | |
be9b65ac | 776 | |
be9b65ac DL |
777 | (defun shell-directory-tracker (str) |
778 | "Tracks cd, pushd and popd commands issued to the shell. | |
779 | This function is called on each input passed to the shell. | |
780 | It watches for cd, pushd and popd commands and sets the buffer's | |
781 | default directory to track these commands. | |
782 | ||
a034393c | 783 | You may toggle this tracking on and off with \\[shell-dirtrack-mode]. |
05327ca9 | 784 | If Emacs gets confused, you can resync with the shell with \\[dirs]. |
c004e539 GM |
785 | \(The `dirtrack' package provides an alternative implementation of this |
786 | feature - see the function `dirtrack-mode'.) | |
be9b65ac | 787 | |
32d01212 | 788 | See variables `shell-cd-regexp', `shell-chdrive-regexp', `shell-pushd-regexp', |
84728570 | 789 | and `shell-popd-regexp', while `shell-pushd-tohome', `shell-pushd-dextract', |
32d01212 | 790 | and `shell-pushd-dunique' control the behavior of the relevant command. |
d76b71af | 791 | |
24fdffaa | 792 | Environment variables are expanded, see function `substitute-in-file-name'." |
d76b71af RS |
793 | (if shell-dirtrackp |
794 | ;; We fail gracefully if we think the command will fail in the shell. | |
8c05cb10 | 795 | (with-demoted-errors "Couldn't cd: %s" |
6ed020c8 JB |
796 | (let ((start (progn (string-match |
797 | (concat "^" shell-command-separator-regexp) | |
798 | str) ; skip whitespace | |
d76b71af | 799 | (match-end 0))) |
4b29d9fb | 800 | (case-fold-search) |
d76b71af RS |
801 | end cmd arg1) |
802 | (while (string-match shell-command-regexp str start) | |
803 | (setq end (match-end 0) | |
804 | cmd (comint-arguments (substring str start end) 0 0) | |
805 | arg1 (comint-arguments (substring str start end) 1 1)) | |
5f49d5e8 KH |
806 | (if arg1 |
807 | (setq arg1 (shell-unquote-argument arg1))) | |
54021695 KH |
808 | (cond ((string-match (concat "\\`\\(" shell-popd-regexp |
809 | "\\)\\($\\|[ \t]\\)") | |
810 | cmd) | |
32d01212 | 811 | (shell-process-popd (comint-substitute-in-file-name arg1))) |
54021695 KH |
812 | ((string-match (concat "\\`\\(" shell-pushd-regexp |
813 | "\\)\\($\\|[ \t]\\)") | |
814 | cmd) | |
32d01212 | 815 | (shell-process-pushd (comint-substitute-in-file-name arg1))) |
54021695 KH |
816 | ((string-match (concat "\\`\\(" shell-cd-regexp |
817 | "\\)\\($\\|[ \t]\\)") | |
818 | cmd) | |
32d01212 RS |
819 | (shell-process-cd (comint-substitute-in-file-name arg1))) |
820 | ((and shell-chdrive-regexp | |
821 | (string-match (concat "\\`\\(" shell-chdrive-regexp | |
822 | "\\)\\($\\|[ \t]\\)") | |
823 | cmd)) | |
824 | (shell-process-cd (comint-substitute-in-file-name cmd)))) | |
6ed020c8 JB |
825 | (setq start (progn (string-match shell-command-separator-regexp |
826 | str end) | |
827 | ;; skip again | |
8c05cb10 | 828 | (match-end 0)))))))) |
d76b71af | 829 | |
5f49d5e8 KH |
830 | (defun shell-unquote-argument (string) |
831 | "Remove all kinds of shell quoting from STRING." | |
832 | (save-match-data | |
f5058b96 EZ |
833 | (let ((idx 0) next inside |
834 | (quote-chars | |
835 | (if (string-match shell-dumb-shell-regexp | |
836 | (file-name-nondirectory | |
837 | (car (process-command (get-buffer-process (current-buffer)))))) | |
838 | "['`\"]" | |
839 | "[\\'`\"]"))) | |
5f49d5e8 | 840 | (while (and (< idx (length string)) |
f5058b96 | 841 | (setq next (string-match quote-chars string next))) |
5f49d5e8 KH |
842 | (cond ((= (aref string next) ?\\) |
843 | (setq string (replace-match "" nil nil string)) | |
844 | (setq next (1+ next))) | |
845 | ((and inside (= (aref string next) inside)) | |
846 | (setq string (replace-match "" nil nil string)) | |
847 | (setq inside nil)) | |
848 | (inside | |
849 | (setq next (1+ next))) | |
850 | (t | |
851 | (setq inside (aref string next)) | |
852 | (setq string (replace-match "" nil nil string))))) | |
853 | string))) | |
854 | ||
05327ca9 | 855 | ;; popd [+n] |
be9b65ac | 856 | (defun shell-process-popd (arg) |
d76b71af RS |
857 | (let ((num (or (shell-extract-num arg) 0))) |
858 | (cond ((and num (= num 0) shell-dirstack) | |
f8aefe82 | 859 | (shell-cd (shell-prefixed-directory-name (car shell-dirstack))) |
d76b71af RS |
860 | (setq shell-dirstack (cdr shell-dirstack)) |
861 | (shell-dirstack-message)) | |
862 | ((and num (> num 0) (<= num (length shell-dirstack))) | |
863 | (let* ((ds (cons nil shell-dirstack)) | |
864 | (cell (nthcdr (1- num) ds))) | |
865 | (rplacd cell (cdr (cdr cell))) | |
866 | (setq shell-dirstack (cdr ds)) | |
867 | (shell-dirstack-message))) | |
868 | (t | |
ba02c167 | 869 | (error "Couldn't popd"))))) |
be9b65ac | 870 | |
3db9a4eb | 871 | ;; Return DIR prefixed with comint-file-name-prefix as appropriate. |
4a02b552 RS |
872 | (defun shell-prefixed-directory-name (dir) |
873 | (if (= (length comint-file-name-prefix) 0) | |
874 | dir | |
875 | (if (file-name-absolute-p dir) | |
876 | ;; The name is absolute, so prepend the prefix. | |
877 | (concat comint-file-name-prefix dir) | |
0b3c9cf1 | 878 | ;; For relative name we assume default-directory already has the prefix. |
4a02b552 | 879 | (expand-file-name dir)))) |
3db9a4eb | 880 | |
05327ca9 | 881 | ;; cd [dir] |
be9b65ac | 882 | (defun shell-process-cd (arg) |
3db9a4eb RM |
883 | (let ((new-dir (cond ((zerop (length arg)) (concat comint-file-name-prefix |
884 | "~")) | |
d76b71af | 885 | ((string-equal "-" arg) shell-last-dir) |
3db9a4eb | 886 | (t (shell-prefixed-directory-name arg))))) |
d76b71af | 887 | (setq shell-last-dir default-directory) |
0b3c9cf1 | 888 | (shell-cd new-dir) |
d76b71af | 889 | (shell-dirstack-message))) |
be9b65ac | 890 | |
05327ca9 | 891 | ;; pushd [+n | dir] |
be9b65ac | 892 | (defun shell-process-pushd (arg) |
d76b71af RS |
893 | (let ((num (shell-extract-num arg))) |
894 | (cond ((zerop (length arg)) | |
895 | ;; no arg -- swap pwd and car of stack unless shell-pushd-tohome | |
896 | (cond (shell-pushd-tohome | |
3db9a4eb | 897 | (shell-process-pushd (concat comint-file-name-prefix "~"))) |
d76b71af RS |
898 | (shell-dirstack |
899 | (let ((old default-directory)) | |
0b3c9cf1 | 900 | (shell-cd (car shell-dirstack)) |
221ca406 | 901 | (setq shell-dirstack (cons old (cdr shell-dirstack))) |
d76b71af RS |
902 | (shell-dirstack-message))) |
903 | (t | |
904 | (message "Directory stack empty.")))) | |
905 | ((numberp num) | |
906 | ;; pushd +n | |
907 | (cond ((> num (length shell-dirstack)) | |
908 | (message "Directory stack not that deep.")) | |
909 | ((= num 0) | |
8c05cb10 | 910 | (error "Couldn't cd")) |
d76b71af RS |
911 | (shell-pushd-dextract |
912 | (let ((dir (nth (1- num) shell-dirstack))) | |
913 | (shell-process-popd arg) | |
914 | (shell-process-pushd default-directory) | |
0b3c9cf1 | 915 | (shell-cd dir) |
d76b71af RS |
916 | (shell-dirstack-message))) |
917 | (t | |
918 | (let* ((ds (cons default-directory shell-dirstack)) | |
919 | (dslen (length ds)) | |
920 | (front (nthcdr num ds)) | |
921 | (back (reverse (nthcdr (- dslen num) (reverse ds)))) | |
922 | (new-ds (append front back))) | |
0b3c9cf1 | 923 | (shell-cd (car new-ds)) |
d76b71af RS |
924 | (setq shell-dirstack (cdr new-ds)) |
925 | (shell-dirstack-message))))) | |
926 | (t | |
927 | ;; pushd <dir> | |
928 | (let ((old-wd default-directory)) | |
0b3c9cf1 | 929 | (shell-cd (shell-prefixed-directory-name arg)) |
d76b71af RS |
930 | (if (or (null shell-pushd-dunique) |
931 | (not (member old-wd shell-dirstack))) | |
932 | (setq shell-dirstack (cons old-wd shell-dirstack))) | |
933 | (shell-dirstack-message)))))) | |
be9b65ac DL |
934 | |
935 | ;; If STR is of the form +n, for n>0, return n. Otherwise, nil. | |
936 | (defun shell-extract-num (str) | |
937 | (and (string-match "^\\+[1-9][0-9]*$" str) | |
027a4b6b | 938 | (string-to-number str))) |
be9b65ac | 939 | |
05327ca9 SM |
940 | (defvaralias 'shell-dirtrack-mode 'shell-dirtrackp) |
941 | (define-minor-mode shell-dirtrack-mode | |
06e21633 CY |
942 | "Toggle directory tracking in this shell buffer (Shell Dirtrack mode). |
943 | With a prefix argument ARG, enable Shell Dirtrack mode if ARG is | |
944 | positive, and disable it otherwise. If called from Lisp, enable | |
945 | the mode if ARG is omitted or nil. | |
946 | ||
947 | The `dirtrack' package provides an alternative implementation of | |
948 | this feature; see the function `dirtrack-mode'." | |
05327ca9 SM |
949 | nil nil nil |
950 | (setq list-buffers-directory (if shell-dirtrack-mode default-directory)) | |
951 | (if shell-dirtrack-mode | |
952 | (add-hook 'comint-input-filter-functions 'shell-directory-tracker nil t) | |
953 | (remove-hook 'comint-input-filter-functions 'shell-directory-tracker t))) | |
954 | ||
a034393c GM |
955 | (define-obsolete-function-alias 'shell-dirtrack-toggle 'shell-dirtrack-mode |
956 | "23.1") | |
be9b65ac | 957 | |
0b3c9cf1 SM |
958 | (defun shell-cd (dir) |
959 | "Do normal `cd' to DIR, and set `list-buffers-directory'." | |
05327ca9 | 960 | (cd dir) |
b7e97ed5 | 961 | (if shell-dirtrackp |
05327ca9 | 962 | (setq list-buffers-directory default-directory))) |
be9b65ac DL |
963 | |
964 | (defun shell-resync-dirs () | |
965 | "Resync the buffer's idea of the current directory stack. | |
84728570 | 966 | This command queries the shell with the command bound to |
24fdffaa | 967 | `shell-dirstack-query' (default \"dirs\"), reads the next |
be9b65ac DL |
968 | line output and parses it to form the new directory stack. |
969 | DON'T issue this command unless the buffer is at a shell prompt. | |
970 | Also, note that if some other subprocess decides to do output | |
971 | immediately after the query, its output will be taken as the | |
ab646e49 | 972 | new directory stack -- you lose. If this happens, just do the |
be9b65ac DL |
973 | command again." |
974 | (interactive) | |
975 | (let* ((proc (get-buffer-process (current-buffer))) | |
45a4af63 GM |
976 | (pmark (process-mark proc)) |
977 | (started-at-pmark (= (point) (marker-position pmark)))) | |
978 | (save-excursion | |
979 | (goto-char pmark) | |
980 | ;; If the process echoes commands, don't insert a fake command in | |
981 | ;; the buffer or it will appear twice. | |
982 | (unless comint-process-echoes | |
983 | (insert shell-dirstack-query) (insert "\n")) | |
984 | (sit-for 0) ; force redisplay | |
985 | (comint-send-string proc shell-dirstack-query) | |
986 | (comint-send-string proc "\n") | |
987 | (set-marker pmark (point)) | |
988 | (let ((pt (point)) | |
989 | (regexp | |
990 | (concat | |
991 | (if comint-process-echoes | |
992 | ;; Skip command echo if the process echoes | |
993 | (concat "\\(" (regexp-quote shell-dirstack-query) "\n\\)") | |
994 | "\\(\\)") | |
995 | "\\(.+\n\\)"))) | |
996 | ;; This extra newline prevents the user's pending input from spoofing us. | |
997 | (insert "\n") (backward-char 1) | |
998 | ;; Wait for one line. | |
999 | (while (not (looking-at regexp)) | |
1000 | (accept-process-output proc) | |
1001 | (goto-char pt))) | |
1002 | (goto-char pmark) (delete-char 1) ; remove the extra newline | |
1003 | ;; That's the dirlist. grab it & parse it. | |
1004 | (let* ((dl (buffer-substring (match-beginning 2) (1- (match-end 2)))) | |
1005 | (dl-len (length dl)) | |
1006 | (ds '()) ; new dir stack | |
1007 | (i 0)) | |
1008 | (while (< i dl-len) | |
1009 | ;; regexp = optional whitespace, (non-whitespace), optional whitespace | |
1010 | (string-match "\\s *\\(\\S +\\)\\s *" dl i) ; pick off next dir | |
1011 | (setq ds (cons (concat comint-file-name-prefix | |
1012 | (substring dl (match-beginning 1) | |
1013 | (match-end 1))) | |
1014 | ds)) | |
1015 | (setq i (match-end 0))) | |
1016 | (let ((ds (nreverse ds))) | |
30213927 GM |
1017 | (with-demoted-errors "Couldn't cd: %s" |
1018 | (shell-cd (car ds)) | |
1019 | (setq shell-dirstack (cdr ds) | |
1020 | shell-last-dir (car shell-dirstack)) | |
1021 | (shell-dirstack-message))))) | |
45a4af63 | 1022 | (if started-at-pmark (goto-char (marker-position pmark))))) |
be9b65ac | 1023 | |
05327ca9 | 1024 | ;; For your typing convenience: |
31e1d920 | 1025 | (defalias 'dirs 'shell-resync-dirs) |
be9b65ac DL |
1026 | |
1027 | ||
05327ca9 SM |
1028 | ;; Show the current dirstack on the message line. |
1029 | ;; Pretty up dirs a bit by changing "/usr/jqr/foo" to "~/foo". | |
1030 | ;; (This isn't necessary if the dirlisting is generated with a simple "dirs".) | |
1031 | ;; All the commands that mung the buffer's dirstack finish by calling | |
1032 | ;; this guy. | |
be9b65ac | 1033 | (defun shell-dirstack-message () |
8a6387ed SM |
1034 | (when shell-dirtrack-verbose |
1035 | (let* ((msg "") | |
1036 | (ds (cons default-directory shell-dirstack)) | |
1037 | (home (expand-file-name (concat comint-file-name-prefix "~/"))) | |
1038 | (homelen (length home))) | |
1039 | (while ds | |
1040 | (let ((dir (car ds))) | |
1041 | (and (>= (length dir) homelen) | |
1042 | (string= home (substring dir 0 homelen)) | |
1043 | (setq dir (concat "~/" (substring dir homelen)))) | |
1044 | ;; Strip off comint-file-name-prefix if present. | |
1045 | (and comint-file-name-prefix | |
1046 | (>= (length dir) (length comint-file-name-prefix)) | |
1047 | (string= comint-file-name-prefix | |
1048 | (substring dir 0 (length comint-file-name-prefix))) | |
1049 | (setq dir (substring dir (length comint-file-name-prefix))) | |
1050 | (setcar ds dir)) | |
1051 | (setq msg (concat msg (directory-file-name dir) " ")) | |
1052 | (setq ds (cdr ds)))) | |
1053 | (message "%s" msg)))) | |
84728570 | 1054 | |
c64d8c55 RS |
1055 | ;; This was mostly copied from shell-resync-dirs. |
1056 | (defun shell-snarf-envar (var) | |
1057 | "Return as a string the shell's value of environment variable VAR." | |
1058 | (let* ((cmd (format "printenv '%s'\n" var)) | |
1059 | (proc (get-buffer-process (current-buffer))) | |
1060 | (pmark (process-mark proc))) | |
1061 | (goto-char pmark) | |
1062 | (insert cmd) | |
1063 | (sit-for 0) ; force redisplay | |
1064 | (comint-send-string proc cmd) | |
1065 | (set-marker pmark (point)) | |
1066 | (let ((pt (point))) ; wait for 1 line | |
1067 | ;; This extra newline prevents the user's pending input from spoofing us. | |
1068 | (insert "\n") (backward-char 1) | |
1069 | (while (not (looking-at ".+\n")) | |
1070 | (accept-process-output proc) | |
1071 | (goto-char pt))) | |
1072 | (goto-char pmark) (delete-char 1) ; remove the extra newline | |
1073 | (buffer-substring (match-beginning 0) (1- (match-end 0))))) | |
1074 | ||
1075 | (defun shell-copy-environment-variable (variable) | |
1076 | "Copy the environment variable VARIABLE from the subshell to Emacs. | |
1077 | This command reads the value of the specified environment variable | |
1078 | in the shell, and sets the same environment variable in Emacs | |
a9e73449 | 1079 | \(what `getenv' in Emacs would return) to that value. |
c64d8c55 RS |
1080 | That value will affect any new subprocesses that you subsequently start |
1081 | from Emacs." | |
1082 | (interactive (list (read-envvar-name "\ | |
1083 | Copy Shell environment variable to Emacs: "))) | |
1084 | (setenv variable (shell-snarf-envar variable))) | |
84728570 | 1085 | |
d76b71af RS |
1086 | (defun shell-forward-command (&optional arg) |
1087 | "Move forward across ARG shell command(s). Does not cross lines. | |
1088 | See `shell-command-regexp'." | |
1089 | (interactive "p") | |
5ed619e0 | 1090 | (let ((limit (line-end-position))) |
19134c18 | 1091 | (if (re-search-forward (concat shell-command-regexp "\\([;&|][\t ]*\\)+") |
d76b71af | 1092 | limit 'move arg) |
b0f86743 | 1093 | (skip-syntax-backward " ")))) |
d76b71af RS |
1094 | |
1095 | ||
1096 | (defun shell-backward-command (&optional arg) | |
1097 | "Move backward across ARG shell command(s). Does not cross lines. | |
1098 | See `shell-command-regexp'." | |
1099 | (interactive "p") | |
1100 | (let ((limit (save-excursion (comint-bol nil) (point)))) | |
28bc09d6 MB |
1101 | (when (> limit (point)) |
1102 | (setq limit (line-beginning-position))) | |
b0f86743 | 1103 | (skip-syntax-backward " " limit) |
d76b71af | 1104 | (if (re-search-backward |
19134c18 | 1105 | (format "[;&|]+[\t ]*\\(%s\\)" shell-command-regexp) limit 'move arg) |
d76b71af | 1106 | (progn (goto-char (match-beginning 1)) |
b0f86743 | 1107 | (skip-chars-forward ";&|"))))) |
d76b71af | 1108 | |
d76b71af RS |
1109 | (defun shell-dynamic-complete-command () |
1110 | "Dynamically complete the command at point. | |
1111 | This function is similar to `comint-dynamic-complete-filename', except that it | |
fdadeb49 | 1112 | searches `exec-path' (minus trailing `exec-directory') for completion |
d76b71af RS |
1113 | candidates. Note that this may not be the same as the shell's idea of the |
1114 | path. | |
1115 | ||
fdadeb49 JC |
1116 | Completion is dependent on the value of `shell-completion-execonly', |
1117 | `shell-completion-fignore', plus those that affect file completion. See Info | |
1118 | node `Shell Options'. | |
988a4d60 RS |
1119 | |
1120 | Returns t if successful." | |
d76b71af | 1121 | (interactive) |
c0a193ea SM |
1122 | (let ((data (shell-command-completion))) |
1123 | (if data | |
290d5b58 | 1124 | (prog2 (unless (window-minibuffer-p) |
c0a193ea SM |
1125 | (message "Completing command name...")) |
1126 | (apply #'completion-in-region data))))) | |
1127 | ||
1128 | (defun shell-command-completion () | |
1129 | "Return the completion data for the command at point, if any." | |
988a4d60 RS |
1130 | (let ((filename (comint-match-partial-filename))) |
1131 | (if (and filename | |
1132 | (save-match-data (not (string-match "[~/]" filename))) | |
1133 | (eq (match-beginning 0) | |
1134 | (save-excursion (shell-backward-command 1) (point)))) | |
c0a193ea | 1135 | (shell--command-completion-data)))) |
988a4d60 | 1136 | |
c0a193ea SM |
1137 | (defun shell--command-completion-data () |
1138 | "Return the completion data for the command at point." | |
56783585 | 1139 | (let* ((filename (or (comint-match-partial-filename) "")) |
c0a193ea SM |
1140 | (start (if (zerop (length filename)) (point) (match-beginning 0))) |
1141 | (end (if (zerop (length filename)) (point) (match-end 0))) | |
f0d5dc24 | 1142 | (filenondir (file-name-nondirectory filename)) |
fdadeb49 JC |
1143 | ; why cdr? see `shell-dynamic-complete-command' |
1144 | (path-dirs (append (cdr (reverse exec-path)) | |
1145 | (if (memq system-type '(windows-nt ms-dos)) '(".")))) | |
d76b71af RS |
1146 | (cwd (file-name-as-directory (expand-file-name default-directory))) |
1147 | (ignored-extensions | |
a867a90a | 1148 | (and comint-completion-fignore |
c0a193ea | 1149 | (mapconcat (function (lambda (x) (concat (regexp-quote x) "\\'"))) |
a867a90a | 1150 | comint-completion-fignore "\\|"))) |
2df2f980 | 1151 | (dir "") (comps-in-dir ()) |
f0d5dc24 RS |
1152 | (file "") (abs-file-name "") (completions ())) |
1153 | ;; Go thru each dir in the search path, finding completions. | |
1154 | (while path-dirs | |
1155 | (setq dir (file-name-as-directory (comint-directory (or (car path-dirs) "."))) | |
1156 | comps-in-dir (and (file-accessible-directory-p dir) | |
1157 | (file-name-all-completions filenondir dir))) | |
d76b71af | 1158 | ;; Go thru each completion found, to see whether it should be used. |
f0d5dc24 RS |
1159 | (while comps-in-dir |
1160 | (setq file (car comps-in-dir) | |
1161 | abs-file-name (concat dir file)) | |
d76b71af | 1162 | (if (and (not (member file completions)) |
7b2db0a0 KH |
1163 | (not (and ignored-extensions |
1164 | (string-match ignored-extensions file))) | |
f0d5dc24 RS |
1165 | (or (string-equal dir cwd) |
1166 | (not (file-directory-p abs-file-name))) | |
d76b71af | 1167 | (or (null shell-completion-execonly) |
f0d5dc24 | 1168 | (file-executable-p abs-file-name))) |
d76b71af | 1169 | (setq completions (cons file completions))) |
f0d5dc24 RS |
1170 | (setq comps-in-dir (cdr comps-in-dir))) |
1171 | (setq path-dirs (cdr path-dirs))) | |
d76b71af | 1172 | ;; OK, we've got a list of completions. |
c0a193ea SM |
1173 | (list |
1174 | start end | |
1175 | (lambda (string pred action) | |
a2a25d24 SM |
1176 | (if (string-match "/" string) |
1177 | (completion-file-name-table string pred action) | |
1178 | (complete-with-action action completions string pred))) | |
1179 | :exit-function | |
1180 | (lambda (_string finished) | |
1181 | (when (memq finished '(sole finished)) | |
1182 | (if (looking-at " ") | |
1183 | (goto-char (match-end 0)) | |
1184 | (insert " "))))))) | |
c0a193ea SM |
1185 | |
1186 | ;; (defun shell-dynamic-complete-as-command () | |
1187 | ;; "Dynamically complete at point as a command. | |
1188 | ;; See `shell-dynamic-complete-filename'. Returns t if successful." | |
1189 | ;; (apply #'completion-in-region shell--command-completion-data)) | |
988a4d60 | 1190 | |
a086d081 CY |
1191 | (defun shell-dynamic-complete-filename () |
1192 | "Dynamically complete the filename at point. | |
1193 | This completes only if point is at a suitable position for a | |
1194 | filename argument." | |
1195 | (interactive) | |
c0a193ea SM |
1196 | (let ((data (shell-filename-completion))) |
1197 | (if data (apply #'completion-in-region data)))) | |
1198 | ||
1199 | (defun shell-filename-completion () | |
1200 | "Return the completion data for file name at point, if any." | |
a086d081 CY |
1201 | (let ((opoint (point)) |
1202 | (beg (comint-line-beginning-position))) | |
1203 | (when (save-excursion | |
1204 | (goto-char (if (re-search-backward "[;|&]" beg t) | |
1205 | (match-end 0) | |
1206 | beg)) | |
1207 | (re-search-forward "[^ \t][ \t]" opoint t)) | |
c0a193ea | 1208 | (comint-filename-completion)))) |
988a4d60 RS |
1209 | |
1210 | (defun shell-match-partial-variable () | |
da6a48c9 | 1211 | "Return the shell variable at point, or nil if none is found." |
988a4d60 | 1212 | (save-excursion |
c0a193ea SM |
1213 | (if (re-search-backward "[^A-Za-z0-9_{(]" nil 'move) |
1214 | (or (looking-at "\\$") (forward-char 1))) | |
1215 | (if (or (eolp) (looking-at "[^A-Za-z0-9_{($]")) | |
1216 | nil | |
1217 | (looking-at "\\$?[{(]?[A-Za-z0-9_]*[})]?") | |
1218 | (buffer-substring (match-beginning 0) (match-end 0))))) | |
a9ec2adb | 1219 | |
988a4d60 RS |
1220 | (defun shell-dynamic-complete-environment-variable () |
1221 | "Dynamically complete the environment variable at point. | |
1222 | Completes if after a variable, i.e., if it starts with a \"$\". | |
988a4d60 RS |
1223 | |
1224 | This function is similar to `comint-dynamic-complete-filename', except that it | |
1225 | searches `process-environment' for completion candidates. Note that this may | |
1226 | not be the same as the interpreter's idea of variable names. The main problem | |
1227 | with this type of completion is that `process-environment' is the environment | |
1228 | which Emacs started with. Emacs does not track changes to the environment made | |
1229 | by the interpreter. Perhaps it would be more accurate if this function was | |
1230 | called `shell-dynamic-complete-process-environment-variable'. | |
1231 | ||
1232 | Returns non-nil if successful." | |
1233 | (interactive) | |
c0a193ea SM |
1234 | (let ((data (shell-environment-variable-completion))) |
1235 | (if data | |
290d5b58 | 1236 | (prog2 (unless (window-minibuffer-p) |
c35be7ec | 1237 | (message "Completing variable name...")) |
c0a193ea SM |
1238 | (apply #'completion-in-region data))))) |
1239 | ||
1240 | ||
1241 | (defun shell-environment-variable-completion () | |
1242 | "Completion data for an environment variable at point, if any." | |
1243 | (let* ((var (shell-match-partial-variable)) | |
1244 | (end (match-end 0))) | |
1245 | (when (and (not (zerop (length var))) (eq (aref var 0) ?$)) | |
1246 | (let* ((start | |
1247 | (save-excursion | |
1248 | (goto-char (match-beginning 0)) | |
1249 | (looking-at "\\$?[({]*") | |
1250 | (match-end 0))) | |
1251 | (variables (mapcar (lambda (x) | |
1252 | (substring x 0 (string-match "=" x))) | |
1253 | process-environment)) | |
a464a6c7 | 1254 | (suffix (pcase (char-before start) (?\{ "}") (?\( ")") (_ "")))) |
a2a25d24 SM |
1255 | (list start end variables |
1256 | :exit-function | |
1257 | (lambda (s finished) | |
1258 | (when (memq finished '(sole finished)) | |
1259 | (let ((suf (concat suffix | |
1260 | (if (file-directory-p | |
1261 | (comint-directory (getenv s))) | |
1262 | "/")))) | |
1263 | (if (looking-at (regexp-quote suf)) | |
1264 | (goto-char (match-end 0)) | |
1265 | (insert suf)))))))))) | |
c0a193ea SM |
1266 | |
1267 | ||
1268 | (defun shell-c-a-p-replace-by-expanded-directory () | |
1269 | "Expand directory stack reference before point. | |
1270 | For use on `completion-at-point-functions'." | |
1271 | (when (comint-match-partial-filename) | |
1272 | (save-excursion | |
1273 | (goto-char (match-beginning 0)) | |
1274 | (let ((stack (cons default-directory shell-dirstack)) | |
1275 | (index (cond ((looking-at "=-/?") | |
1276 | (length shell-dirstack)) | |
1277 | ((looking-at "=\\([0-9]+\\)/?") | |
1278 | (string-to-number | |
1279 | (buffer-substring | |
1280 | (match-beginning 1) (match-end 1))))))) | |
1281 | (when index | |
1282 | (let ((start (match-beginning 0)) | |
1283 | (end (match-end 0)) | |
1284 | (replacement (file-name-as-directory (nth index stack)))) | |
1285 | (lambda () | |
1286 | (cond | |
1287 | ((>= index (length stack)) | |
1288 | (error "Directory stack not that deep")) | |
1289 | (t | |
1290 | (save-excursion | |
1291 | (goto-char start) | |
1292 | (insert replacement) | |
1293 | (delete-char (- end start))) | |
1294 | (message "Directory item: %d" index) | |
1295 | t))))))))) | |
988a4d60 RS |
1296 | |
1297 | (defun shell-replace-by-expanded-directory () | |
1298 | "Expand directory stack reference before point. | |
1299 | Directory stack references are of the form \"=digit\" or \"=-\". | |
1300 | See `default-directory' and `shell-dirstack'. | |
1301 | ||
1302 | Returns t if successful." | |
1303 | (interactive) | |
c0a193ea SM |
1304 | (let ((f (shell-c-a-p-replace-by-expanded-directory))) |
1305 | (if f (funcall f)))) | |
84728570 | 1306 | |
c88ab9ce ER |
1307 | (provide 'shell) |
1308 | ||
1309 | ;;; shell.el ends here |