Auto-commit of generated files.
[bpt/emacs.git] / lisp / eshell / esh-ext.el
CommitLineData
60370d40 1;;; esh-ext.el --- commands external to Eshell
affbf647 2
acaf905b 3;; Copyright (C) 1999-2012 Free Software Foundation, Inc.
affbf647 4
7de5b421
GM
5;; Author: John Wiegley <johnw@gnu.org>
6
affbf647
GM
7;; This file is part of GNU Emacs.
8
4ee57b2a 9;; GNU Emacs is free software: you can redistribute it and/or modify
affbf647 10;; it under the terms of the GNU General Public License as published by
4ee57b2a
GM
11;; the Free Software Foundation, either version 3 of the License, or
12;; (at your option) any later version.
affbf647
GM
13
14;; GNU Emacs is distributed in the hope that it will be useful,
15;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17;; GNU General Public License for more details.
18
19;; You should have received a copy of the GNU General Public License
4ee57b2a 20;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
affbf647 21
affbf647
GM
22;;; Commentary:
23
24;; To force a command to invoked external, either provide an explicit
25;; pathname for the command argument, or prefix the command name with
26;; an asterix character. Example:
27;;
28;; grep ; make invoke `grep' Lisp function, or `eshell/grep'
29;; /bin/grep ; will definitely invoke /bin/grep
30;; *grep ; will also invoke /bin/grep
31
8c7309fe
GM
32;;; Code:
33
4e6cc05c
GM
34(provide 'esh-ext)
35
36(eval-when-compile
a464a6c7 37 (require 'cl-lib)
4e6cc05c
GM
38 (require 'esh-cmd))
39(require 'esh-util)
a464a6c7 40(require 'esh-opt)
4e6cc05c
GM
41
42(defgroup eshell-ext nil
43 "External commands are invoked when operating system executables are
44loaded into memory, thus beginning a new process."
45 :tag "External commands"
46 :group 'eshell)
47
affbf647
GM
48;;; User Variables:
49
d783d303 50(defcustom eshell-ext-load-hook nil
ec60da52 51 "A hook that gets run when `eshell-ext' is loaded."
d783d303 52 :version "24.1" ; removed eshell-ext-initialize
affbf647
GM
53 :type 'hook
54 :group 'eshell-ext)
55
194c8d98 56(defcustom eshell-binary-suffixes exec-suffixes
ec60da52 57 "A list of suffixes used when searching for executable files."
affbf647
GM
58 :type '(repeat string)
59 :group 'eshell-ext)
60
61(defcustom eshell-force-execution nil
ec60da52 62 "If non-nil, try to execute binary files regardless of permissions.
affbf647
GM
63This can be useful on systems like Windows, where the operating system
64doesn't happen to honor the permission bits in certain cases; or in
65cases where you want to associate an interpreter with a particular
66kind of script file, but the language won't let you but a '#!'
67interpreter line in the file, and you don't want to make it executable
68since nothing else but Eshell will be able to understand
69`eshell-interpreter-alist'."
70 :type 'boolean
71 :group 'eshell-ext)
72
73(defun eshell-search-path (name)
74 "Search the environment path for NAME."
75 (if (file-name-absolute-p name)
76 name
605a20a9 77 (let ((list (eshell-parse-colon-path eshell-path-env))
affbf647
GM
78 suffixes n1 n2 file)
79 (while list
80 (setq n1 (concat (car list) name))
81 (setq suffixes eshell-binary-suffixes)
82 (while suffixes
83 (setq n2 (concat n1 (car suffixes)))
84 (if (and (or (file-executable-p n2)
85 (and eshell-force-execution
86 (file-readable-p n2)))
87 (not (file-directory-p n2)))
88 (setq file n2 suffixes nil list nil))
89 (setq suffixes (cdr suffixes)))
90 (setq list (cdr list)))
91 file)))
92
93(defcustom eshell-windows-shell-file
94 (if (eshell-under-windows-p)
6283a7d3 95 (if (string-match "\\(cmdproxy\\|sh\\)\\.\\(com\\|exe\\)"
affbf647
GM
96 shell-file-name)
97 (or (eshell-search-path "cmd.exe")
b0c9a334 98 (eshell-search-path "command.com"))
affbf647 99 shell-file-name))
ec60da52 100 "The name of the shell command to use for DOS/Windows batch files.
affbf647
GM
101This defaults to nil on non-Windows systems, where this variable is
102wholly ignored."
f569d4c4 103 :type '(choice file (const nil))
affbf647
GM
104 :group 'eshell-ext)
105
106(defsubst eshell-invoke-batch-file (&rest args)
107 "Invoke a .BAT or .CMD file on DOS/Windows systems."
108 ;; since CMD.EXE can't handle forward slashes in the initial
109 ;; argument...
6b0e3e4d 110 (setcar args (subst-char-in-string ?/ ?\\ (car args)))
affbf647 111 (throw 'eshell-replace-command
93376c5b
CY
112 (eshell-parse-command
113 (eshell-quote-argument eshell-windows-shell-file)
114 (cons "/c" args))))
affbf647
GM
115
116(defcustom eshell-interpreter-alist
117 (if (eshell-under-windows-p)
118 '(("\\.\\(bat\\|cmd\\)\\'" . eshell-invoke-batch-file)))
ec60da52 119 "An alist defining interpreter substitutions.
affbf647
GM
120Each member is a cons cell of the form:
121
122 (MATCH . INTERPRETER)
123
124MATCH should be a regexp, which is matched against the command name,
125or a function. If either returns a non-nil value, then INTERPRETER
126will be used for that command.
127
128If INTERPRETER is a string, it will be called as the command name,
129with the original command name passed as the first argument, with all
130subsequent arguments following. If INTERPRETER is a function, it will
131be called with all of those arguments. Note that interpreter
132functions should throw `eshell-replace-command' with the alternate
133command form, or they should return a value compatible with the
134possible return values of `eshell-external-command', which see."
135 :type '(repeat (cons (choice regexp (function :tag "Predicate"))
136 (choice string (function :tag "Interpreter"))))
137 :group 'eshell-ext)
138
139(defcustom eshell-alternate-command-hook nil
ec60da52 140 "A hook run whenever external command lookup fails.
affbf647
GM
141If a functions wishes to provide an alternate command, they must throw
142it using the tag `eshell-replace-command'. This is done because the
143substituted command need not be external at all, and therefore must be
144passed up to a higher level for re-evaluation.
145
146Or, if the function returns a filename, that filename will be invoked
147with the current command arguments rather than the command specified
148by the user on the command line."
149 :type 'hook
150 :group 'eshell-ext)
151
152(defcustom eshell-command-interpreter-max-length 256
ec60da52 153 "The maximum length of any command interpreter string, plus args."
affbf647
GM
154 :type 'integer
155 :group 'eshell-ext)
156
1228240f 157(defcustom eshell-explicit-command-char ?*
ec60da52 158 "If this char occurs before a command name, call it externally.
451eaf8d
RS
159That is, although `vi' may be an alias, `\vi' will always call the
160external version."
1228240f
JW
161 :type 'character
162 :group 'eshell-ext)
163
affbf647
GM
164;;; Functions:
165
166(defun eshell-ext-initialize ()
167 "Initialize the external command handling code."
affbf647
GM
168 (add-hook 'eshell-named-command-hook 'eshell-explicit-command nil t))
169
170(defun eshell-explicit-command (command args)
171 "If a command name begins with `*', call it externally always.
172This bypasses all Lisp functions and aliases."
173 (when (and (> (length command) 1)
1228240f 174 (eq (aref command 0) eshell-explicit-command-char))
affbf647
GM
175 (let ((cmd (eshell-search-path (substring command 1))))
176 (if cmd
177 (or (eshell-external-command cmd args)
178 (error "%s: external command failed" cmd))
179 (error "%s: external command not found"
180 (substring command 1))))))
181
605a20a9 182(defun eshell-remote-command (command args)
affbf647
GM
183 "Insert output from a remote COMMAND, using ARGS.
184A remote command is something that executes on a different machine.
185An external command simply means external to Emacs.
186
187Note that this function is very crude at the moment. It gathers up
188all the output from the remote command, and sends it all at once,
189causing the user to wonder if anything's really going on..."
190 (let ((outbuf (generate-new-buffer " *eshell remote output*"))
191 (errbuf (generate-new-buffer " *eshell remote error*"))
8477cc7a 192 (command (or (file-remote-p command 'localname) command))
affbf647
GM
193 (exitcode 1))
194 (unwind-protect
195 (progn
196 (setq exitcode
605a20a9
MA
197 (shell-command
198 (mapconcat 'shell-quote-argument
199 (append (list command) args) " ")
200 outbuf errbuf))
937e6a56
SM
201 (eshell-print (with-current-buffer outbuf (buffer-string)))
202 (eshell-error (with-current-buffer errbuf (buffer-string))))
affbf647
GM
203 (eshell-close-handles exitcode 'nil)
204 (kill-buffer outbuf)
205 (kill-buffer errbuf))))
206
207(defun eshell-external-command (command args)
208 "Insert output from an external COMMAND, using ARGS."
209 (setq args (eshell-stringify-list (eshell-flatten-list args)))
c2c43c23
MA
210 (let ((interp (eshell-find-interpreter
211 command
7b2fbe3b
MA
212 ;; `eshell-find-interpreter' does not work correctly
213 ;; for Tramp file name syntax. But we don't need to
214 ;; know the interpreter in that case, therefore the
215 ;; check is suppressed.
c2c43c23
MA
216 (or (and (stringp command) (file-remote-p command))
217 (file-remote-p default-directory)))))
a464a6c7 218 (cl-assert interp)
605a20a9
MA
219 (if (functionp (car interp))
220 (apply (car interp) (append (cdr interp) args))
221 (eshell-gather-process-output
222 (car interp) (append (cdr interp) args)))))
affbf647
GM
223
224(defun eshell/addpath (&rest args)
225 "Add a set of paths to PATH."
226 (eshell-eval-using-options
227 "addpath" args
228 '((?b "begin" nil prepend "add path element at beginning")
229 (?h "help" nil nil "display this usage message")
230 :usage "[-b] PATH
231Adds the given PATH to $PATH.")
232 (if args
233 (progn
e296d94b
GM
234 (setq eshell-path-env (getenv "PATH")
235 args (mapconcat 'identity args path-separator)
236 eshell-path-env
237 (if prepend
238 (concat args path-separator eshell-path-env)
239 (concat eshell-path-env path-separator args)))
5eaeacb5
GM
240 (setenv "PATH" eshell-path-env))
241 (dolist (dir (parse-colon-path (getenv "PATH")))
242 (eshell-printn dir)))))
affbf647 243
127fd3c2
JW
244(put 'eshell/addpath 'eshell-no-numeric-conversions t)
245
affbf647
GM
246(defun eshell-script-interpreter (file)
247 "Extract the script to run from FILE, if it has #!<interp> in it.
248Return nil, or a list of the form:
249
250 (INTERPRETER [ARGS] FILE)"
251 (let ((maxlen eshell-command-interpreter-max-length))
252 (if (and (file-readable-p file)
253 (file-regular-p file))
254 (with-temp-buffer
255 (insert-file-contents-literally file nil 0 maxlen)
30104690 256 (if (looking-at "#![ \t]*\\([^ \r\t\n]+\\)\\([ \t]+\\(.+\\)\\)?")
affbf647
GM
257 (if (match-string 3)
258 (list (match-string 1)
259 (match-string 3)
260 file)
261 (list (match-string 1)
262 file)))))))
263
264(defun eshell-find-interpreter (file &optional no-examine-p)
265 "Find the command interpreter with which to execute FILE.
266If NO-EXAMINE-P is non-nil, FILE will not be inspected for a script
267line of the form #!<interp>."
268 (let ((finterp
269 (catch 'found
270 (ignore
a9eeff78 271 (dolist (possible eshell-interpreter-alist)
affbf647
GM
272 (cond
273 ((functionp (car possible))
274 (and (funcall (car possible) file)
275 (throw 'found (cdr possible))))
276 ((stringp (car possible))
277 (and (string-match (car possible) file)
278 (throw 'found (cdr possible))))
279 (t
280 (error "Invalid interpreter-alist test"))))))))
281 (if finterp ; first check
282 (list finterp file)
283 (let ((fullname (if (file-name-directory file) file
284 (eshell-search-path file)))
285 (suffixes eshell-binary-suffixes))
286 (if (and fullname (not (or eshell-force-execution
287 (file-executable-p fullname))))
288 (while suffixes
289 (let ((try (concat fullname (car suffixes))))
290 (if (or (file-executable-p try)
291 (and eshell-force-execution
292 (file-readable-p try)))
293 (setq fullname try suffixes nil)
294 (setq suffixes (cdr suffixes))))))
295 (cond ((not (and fullname (file-exists-p fullname)))
296 (let ((name (or fullname file)))
297 (unless (setq fullname
298 (run-hook-with-args-until-success
299 'eshell-alternate-command-hook file))
300 (error "%s: command not found" name))))
301 ((not (or eshell-force-execution
302 (file-executable-p fullname)))
303 (error "%s: Permission denied" fullname)))
304 (let (interp)
305 (unless no-examine-p
306 (setq interp (eshell-script-interpreter fullname))
307 (if interp
308 (setq interp
309 (cons (car (eshell-find-interpreter (car interp) t))
310 (cdr interp)))))
311 (or interp (list fullname)))))))
312
affbf647 313;;; esh-ext.el ends here