Mercurial > emacs
annotate lisp/eshell/esh-ext.el @ 102476:9191f269c2d9
Fix date in entry.
author | Juanma Barranquero <lekktu@gmail.com> |
---|---|
date | Wed, 11 Mar 2009 01:04:23 +0000 |
parents | d1ba3386e97a |
children | 328150f0cf76 |
rev | line source |
---|---|
38414
67b464da13ec
Some fixes to follow coding conventions.
Pavel Janík <Pavel@Janik.cz>
parents:
37818
diff
changeset
|
1 ;;; esh-ext.el --- commands external to Eshell |
29876 | 2 |
74509 | 3 ;; Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, |
100908 | 4 ;; 2005, 2006, 2007, 2008, 2009 Free Software Foundation, Inc. |
29876 | 5 |
32526 | 6 ;; Author: John Wiegley <johnw@gnu.org> |
7 | |
29876 | 8 ;; This file is part of GNU Emacs. |
9 | |
94661
b5b0801a7637
Switch to recommended form of GPLv3 permissions notice.
Glenn Morris <rgm@gnu.org>
parents:
93975
diff
changeset
|
10 ;; GNU Emacs is free software: you can redistribute it and/or modify |
29876 | 11 ;; it under the terms of the GNU General Public License as published by |
94661
b5b0801a7637
Switch to recommended form of GPLv3 permissions notice.
Glenn Morris <rgm@gnu.org>
parents:
93975
diff
changeset
|
12 ;; the Free Software Foundation, either version 3 of the License, or |
b5b0801a7637
Switch to recommended form of GPLv3 permissions notice.
Glenn Morris <rgm@gnu.org>
parents:
93975
diff
changeset
|
13 ;; (at your option) any later version. |
29876 | 14 |
15 ;; GNU Emacs is distributed in the hope that it will be useful, | |
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
18 ;; GNU General Public License for more details. | |
19 | |
20 ;; You should have received a copy of the GNU General Public License | |
94661
b5b0801a7637
Switch to recommended form of GPLv3 permissions notice.
Glenn Morris <rgm@gnu.org>
parents:
93975
diff
changeset
|
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
29876 | 22 |
23 ;;; Commentary: | |
24 | |
25 ;; To force a command to invoked external, either provide an explicit | |
26 ;; pathname for the command argument, or prefix the command name with | |
27 ;; an asterix character. Example: | |
28 ;; | |
29 ;; grep ; make invoke `grep' Lisp function, or `eshell/grep' | |
30 ;; /bin/grep ; will definitely invoke /bin/grep | |
31 ;; *grep ; will also invoke /bin/grep | |
32 | |
101347 | 33 ;;; Code: |
34 | |
87084
c7c065bc43a2
Require individual files if needed when compiling, rather than
Glenn Morris <rgm@gnu.org>
parents:
86202
diff
changeset
|
35 (provide 'esh-ext) |
c7c065bc43a2
Require individual files if needed when compiling, rather than
Glenn Morris <rgm@gnu.org>
parents:
86202
diff
changeset
|
36 |
c7c065bc43a2
Require individual files if needed when compiling, rather than
Glenn Morris <rgm@gnu.org>
parents:
86202
diff
changeset
|
37 (eval-when-compile |
98564
f79ec7c34dc5
Sven Joachim <svenjoac at gmx.de>
Glenn Morris <rgm@gnu.org>
parents:
94661
diff
changeset
|
38 (require 'cl) |
87084
c7c065bc43a2
Require individual files if needed when compiling, rather than
Glenn Morris <rgm@gnu.org>
parents:
86202
diff
changeset
|
39 (require 'esh-cmd)) |
c7c065bc43a2
Require individual files if needed when compiling, rather than
Glenn Morris <rgm@gnu.org>
parents:
86202
diff
changeset
|
40 (require 'esh-util) |
c7c065bc43a2
Require individual files if needed when compiling, rather than
Glenn Morris <rgm@gnu.org>
parents:
86202
diff
changeset
|
41 |
c7c065bc43a2
Require individual files if needed when compiling, rather than
Glenn Morris <rgm@gnu.org>
parents:
86202
diff
changeset
|
42 (defgroup eshell-ext nil |
c7c065bc43a2
Require individual files if needed when compiling, rather than
Glenn Morris <rgm@gnu.org>
parents:
86202
diff
changeset
|
43 "External commands are invoked when operating system executables are |
c7c065bc43a2
Require individual files if needed when compiling, rather than
Glenn Morris <rgm@gnu.org>
parents:
86202
diff
changeset
|
44 loaded into memory, thus beginning a new process." |
c7c065bc43a2
Require individual files if needed when compiling, rather than
Glenn Morris <rgm@gnu.org>
parents:
86202
diff
changeset
|
45 :tag "External commands" |
c7c065bc43a2
Require individual files if needed when compiling, rather than
Glenn Morris <rgm@gnu.org>
parents:
86202
diff
changeset
|
46 :group 'eshell) |
c7c065bc43a2
Require individual files if needed when compiling, rather than
Glenn Morris <rgm@gnu.org>
parents:
86202
diff
changeset
|
47 |
29876 | 48 ;;; User Variables: |
49 | |
50 (defcustom eshell-ext-load-hook '(eshell-ext-initialize) | |
51 "*A hook that gets run when `eshell-ext' is loaded." | |
52 :type 'hook | |
53 :group 'eshell-ext) | |
54 | |
44002
a27b6cb6448d
(eshell-binary-suffixes): Use exec-suffixes.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
43336
diff
changeset
|
55 (defcustom eshell-binary-suffixes exec-suffixes |
29876 | 56 "*A list of suffixes used when searching for executable files." |
57 :type '(repeat string) | |
58 :group 'eshell-ext) | |
59 | |
60 (defcustom eshell-force-execution nil | |
61 "*If non-nil, try to execute binary files regardless of permissions. | |
62 This can be useful on systems like Windows, where the operating system | |
63 doesn't happen to honor the permission bits in certain cases; or in | |
64 cases where you want to associate an interpreter with a particular | |
65 kind of script file, but the language won't let you but a '#!' | |
66 interpreter line in the file, and you don't want to make it executable | |
67 since nothing else but Eshell will be able to understand | |
68 `eshell-interpreter-alist'." | |
69 :type 'boolean | |
70 :group 'eshell-ext) | |
71 | |
72 (defun eshell-search-path (name) | |
73 "Search the environment path for NAME." | |
74 (if (file-name-absolute-p name) | |
75 name | |
76 (let ((list (parse-colon-path (getenv "PATH"))) | |
77 suffixes n1 n2 file) | |
78 (while list | |
79 (setq n1 (concat (car list) name)) | |
80 (setq suffixes eshell-binary-suffixes) | |
81 (while suffixes | |
82 (setq n2 (concat n1 (car suffixes))) | |
83 (if (and (or (file-executable-p n2) | |
84 (and eshell-force-execution | |
85 (file-readable-p n2))) | |
86 (not (file-directory-p n2))) | |
87 (setq file n2 suffixes nil list nil)) | |
88 (setq suffixes (cdr suffixes))) | |
89 (setq list (cdr list))) | |
90 file))) | |
91 | |
92 (defcustom eshell-windows-shell-file | |
93 (if (eshell-under-windows-p) | |
94 (if (string-match "\\(\\`cmdproxy\\|sh\\)\\.\\(com\\|exe\\)" | |
95 shell-file-name) | |
96 (or (eshell-search-path "cmd.exe") | |
65169
4614e3dc5d9f
(eshell-windows-shell-file): Look for command.com, not command.exe.
Eli Zaretskii <eliz@gnu.org>
parents:
64701
diff
changeset
|
97 (eshell-search-path "command.com")) |
29876 | 98 shell-file-name)) |
99 "*The name of the shell command to use for DOS/Windows batch files. | |
100 This defaults to nil on non-Windows systems, where this variable is | |
101 wholly ignored." | |
35717
a16b7aced16a
(eshell-windows-shell-file): Fix :type.
Dave Love <fx@gnu.org>
parents:
32526
diff
changeset
|
102 :type '(choice file (const nil)) |
29876 | 103 :group 'eshell-ext) |
104 | |
105 (defsubst eshell-invoke-batch-file (&rest args) | |
106 "Invoke a .BAT or .CMD file on DOS/Windows systems." | |
107 ;; since CMD.EXE can't handle forward slashes in the initial | |
108 ;; argument... | |
62915
b89e30bcd2bb
Changed all uses of `directory-sep-char' to ?/, and all uses of
John Wiegley <johnw@newartisans.com>
parents:
57162
diff
changeset
|
109 (setcar args (subst-char-in-string ?/ ?\\ (car args))) |
29876 | 110 (throw 'eshell-replace-command |
31241 | 111 (eshell-parse-command eshell-windows-shell-file (cons "/c" args)))) |
29876 | 112 |
113 (defcustom eshell-interpreter-alist | |
114 (if (eshell-under-windows-p) | |
115 '(("\\.\\(bat\\|cmd\\)\\'" . eshell-invoke-batch-file))) | |
116 "*An alist defining interpreter substitutions. | |
117 Each member is a cons cell of the form: | |
118 | |
119 (MATCH . INTERPRETER) | |
120 | |
121 MATCH should be a regexp, which is matched against the command name, | |
122 or a function. If either returns a non-nil value, then INTERPRETER | |
123 will be used for that command. | |
124 | |
125 If INTERPRETER is a string, it will be called as the command name, | |
126 with the original command name passed as the first argument, with all | |
127 subsequent arguments following. If INTERPRETER is a function, it will | |
128 be called with all of those arguments. Note that interpreter | |
129 functions should throw `eshell-replace-command' with the alternate | |
130 command form, or they should return a value compatible with the | |
131 possible return values of `eshell-external-command', which see." | |
132 :type '(repeat (cons (choice regexp (function :tag "Predicate")) | |
133 (choice string (function :tag "Interpreter")))) | |
134 :group 'eshell-ext) | |
135 | |
136 (defcustom eshell-alternate-command-hook nil | |
137 "*A hook run whenever external command lookup fails. | |
138 If a functions wishes to provide an alternate command, they must throw | |
139 it using the tag `eshell-replace-command'. This is done because the | |
140 substituted command need not be external at all, and therefore must be | |
141 passed up to a higher level for re-evaluation. | |
142 | |
143 Or, if the function returns a filename, that filename will be invoked | |
144 with the current command arguments rather than the command specified | |
145 by the user on the command line." | |
146 :type 'hook | |
147 :group 'eshell-ext) | |
148 | |
149 (defcustom eshell-command-interpreter-max-length 256 | |
150 "*The maximum length of any command interpreter string, plus args." | |
151 :type 'integer | |
152 :group 'eshell-ext) | |
153 | |
37818
225c25d9631c
(eshell-explicit-command-char): A new configuration variable, which
John Wiegley <johnw@newartisans.com>
parents:
37661
diff
changeset
|
154 (defcustom eshell-explicit-command-char ?* |
225c25d9631c
(eshell-explicit-command-char): A new configuration variable, which
John Wiegley <johnw@newartisans.com>
parents:
37661
diff
changeset
|
155 "*If this char occurs before a command name, call it externally. |
57162
a9bbdf07a7d6
(eshell-explicit-command-char): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents:
52401
diff
changeset
|
156 That is, although `vi' may be an alias, `\vi' will always call the |
a9bbdf07a7d6
(eshell-explicit-command-char): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents:
52401
diff
changeset
|
157 external version." |
37818
225c25d9631c
(eshell-explicit-command-char): A new configuration variable, which
John Wiegley <johnw@newartisans.com>
parents:
37661
diff
changeset
|
158 :type 'character |
225c25d9631c
(eshell-explicit-command-char): A new configuration variable, which
John Wiegley <johnw@newartisans.com>
parents:
37661
diff
changeset
|
159 :group 'eshell-ext) |
225c25d9631c
(eshell-explicit-command-char): A new configuration variable, which
John Wiegley <johnw@newartisans.com>
parents:
37661
diff
changeset
|
160 |
29876 | 161 ;;; Functions: |
162 | |
163 (defun eshell-ext-initialize () | |
164 "Initialize the external command handling code." | |
165 (add-hook 'eshell-named-command-hook 'eshell-explicit-command nil t)) | |
166 | |
167 (defun eshell-explicit-command (command args) | |
168 "If a command name begins with `*', call it externally always. | |
169 This bypasses all Lisp functions and aliases." | |
170 (when (and (> (length command) 1) | |
37818
225c25d9631c
(eshell-explicit-command-char): A new configuration variable, which
John Wiegley <johnw@newartisans.com>
parents:
37661
diff
changeset
|
171 (eq (aref command 0) eshell-explicit-command-char)) |
29876 | 172 (let ((cmd (eshell-search-path (substring command 1)))) |
173 (if cmd | |
174 (or (eshell-external-command cmd args) | |
175 (error "%s: external command failed" cmd)) | |
176 (error "%s: external command not found" | |
177 (substring command 1)))))) | |
178 | |
179 (defun eshell-remote-command (handler command args) | |
180 "Insert output from a remote COMMAND, using ARGS. | |
181 A remote command is something that executes on a different machine. | |
182 An external command simply means external to Emacs. | |
183 | |
184 Note that this function is very crude at the moment. It gathers up | |
185 all the output from the remote command, and sends it all at once, | |
186 causing the user to wonder if anything's really going on..." | |
187 (let ((outbuf (generate-new-buffer " *eshell remote output*")) | |
188 (errbuf (generate-new-buffer " *eshell remote error*")) | |
189 (exitcode 1)) | |
190 (unwind-protect | |
191 (progn | |
192 (setq exitcode | |
193 (funcall handler 'shell-command | |
194 (mapconcat 'shell-quote-argument | |
195 (append (list command) args) " ") | |
196 outbuf errbuf)) | |
197 (eshell-print (save-excursion (set-buffer outbuf) | |
198 (buffer-string))) | |
199 (eshell-error (save-excursion (set-buffer errbuf) | |
200 (buffer-string)))) | |
201 (eshell-close-handles exitcode 'nil) | |
202 (kill-buffer outbuf) | |
203 (kill-buffer errbuf)))) | |
204 | |
205 (defun eshell-external-command (command args) | |
206 "Insert output from an external COMMAND, using ARGS." | |
207 (setq args (eshell-stringify-list (eshell-flatten-list args))) | |
208 (let ((handler | |
209 (unless (or (equal default-directory "/") | |
210 (and (eshell-under-windows-p) | |
211 (string-match "\\`[A-Za-z]:[/\\\\]\\'" | |
212 default-directory))) | |
213 (find-file-name-handler default-directory | |
214 'shell-command)))) | |
43319
539d3657c52c
(eshell-external-command): Added a fix for XEmacs' new dired.el, which
John Wiegley <johnw@newartisans.com>
parents:
38414
diff
changeset
|
215 (if (and handler |
86202
794e428cd497
* eshell/esh-util.el (eshell-under-xemacs-p): Remove.
Dan Nicolaescu <dann@ics.uci.edu>
parents:
78220
diff
changeset
|
216 (not (and (featurep 'xemacs) |
43319
539d3657c52c
(eshell-external-command): Added a fix for XEmacs' new dired.el, which
John Wiegley <johnw@newartisans.com>
parents:
38414
diff
changeset
|
217 (eq handler 'dired-handler-fn)))) |
29876 | 218 (eshell-remote-command handler command args)) |
219 (let ((interp (eshell-find-interpreter command))) | |
220 (assert interp) | |
221 (if (functionp (car interp)) | |
222 (apply (car interp) (append (cdr interp) args)) | |
223 (eshell-gather-process-output | |
224 (car interp) (append (cdr interp) args)))))) | |
225 | |
226 (defun eshell/addpath (&rest args) | |
227 "Add a set of paths to PATH." | |
228 (eshell-eval-using-options | |
229 "addpath" args | |
230 '((?b "begin" nil prepend "add path element at beginning") | |
231 (?h "help" nil nil "display this usage message") | |
232 :usage "[-b] PATH | |
233 Adds the given PATH to $PATH.") | |
234 (if args | |
235 (progn | |
236 (if prepend | |
237 (setq args (nreverse args))) | |
238 (while args | |
239 (setenv "PATH" | |
240 (if prepend | |
241 (concat (car args) path-separator | |
242 (getenv "PATH")) | |
243 (concat (getenv "PATH") path-separator | |
244 (car args)))) | |
245 (setq args (cdr args)))) | |
246 (let ((paths (parse-colon-path (getenv "PATH")))) | |
247 (while paths | |
248 (eshell-printn (car paths)) | |
249 (setq paths (cdr paths))))))) | |
250 | |
37661
6d7c89c79996
Set the property `eshell-no-numeric-conversions' on the following
John Wiegley <johnw@newartisans.com>
parents:
35717
diff
changeset
|
251 (put 'eshell/addpath 'eshell-no-numeric-conversions t) |
6d7c89c79996
Set the property `eshell-no-numeric-conversions' on the following
John Wiegley <johnw@newartisans.com>
parents:
35717
diff
changeset
|
252 |
29876 | 253 (defun eshell-script-interpreter (file) |
254 "Extract the script to run from FILE, if it has #!<interp> in it. | |
255 Return nil, or a list of the form: | |
256 | |
257 (INTERPRETER [ARGS] FILE)" | |
258 (let ((maxlen eshell-command-interpreter-max-length)) | |
259 (if (and (file-readable-p file) | |
260 (file-regular-p file)) | |
261 (with-temp-buffer | |
262 (insert-file-contents-literally file nil 0 maxlen) | |
44553
536e5590a586
(eshell-script-interpreter): Fix for CRLF operating systems to the
John Wiegley <johnw@newartisans.com>
parents:
44002
diff
changeset
|
263 (if (looking-at "#![ \t]*\\([^ \r\t\n]+\\)\\([ \t]+\\(.+\\)\\)?") |
29876 | 264 (if (match-string 3) |
265 (list (match-string 1) | |
266 (match-string 3) | |
267 file) | |
268 (list (match-string 1) | |
269 file))))))) | |
270 | |
271 (defun eshell-find-interpreter (file &optional no-examine-p) | |
272 "Find the command interpreter with which to execute FILE. | |
273 If NO-EXAMINE-P is non-nil, FILE will not be inspected for a script | |
274 line of the form #!<interp>." | |
275 (let ((finterp | |
276 (catch 'found | |
277 (ignore | |
278 (eshell-for possible eshell-interpreter-alist | |
279 (cond | |
280 ((functionp (car possible)) | |
281 (and (funcall (car possible) file) | |
282 (throw 'found (cdr possible)))) | |
283 ((stringp (car possible)) | |
284 (and (string-match (car possible) file) | |
285 (throw 'found (cdr possible)))) | |
286 (t | |
287 (error "Invalid interpreter-alist test")))))))) | |
288 (if finterp ; first check | |
289 (list finterp file) | |
290 (let ((fullname (if (file-name-directory file) file | |
291 (eshell-search-path file))) | |
292 (suffixes eshell-binary-suffixes)) | |
293 (if (and fullname (not (or eshell-force-execution | |
294 (file-executable-p fullname)))) | |
295 (while suffixes | |
296 (let ((try (concat fullname (car suffixes)))) | |
297 (if (or (file-executable-p try) | |
298 (and eshell-force-execution | |
299 (file-readable-p try))) | |
300 (setq fullname try suffixes nil) | |
301 (setq suffixes (cdr suffixes)))))) | |
302 (cond ((not (and fullname (file-exists-p fullname))) | |
303 (let ((name (or fullname file))) | |
304 (unless (setq fullname | |
305 (run-hook-with-args-until-success | |
306 'eshell-alternate-command-hook file)) | |
307 (error "%s: command not found" name)))) | |
308 ((not (or eshell-force-execution | |
309 (file-executable-p fullname))) | |
310 (error "%s: Permission denied" fullname))) | |
311 (let (interp) | |
312 (unless no-examine-p | |
313 (setq interp (eshell-script-interpreter fullname)) | |
314 (if interp | |
315 (setq interp | |
316 (cons (car (eshell-find-interpreter (car interp) t)) | |
317 (cdr interp))))) | |
318 (or interp (list fullname))))))) | |
319 | |
93975
1e3a407766b9
Fix up comment convention on the arch-tag lines.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
87649
diff
changeset
|
320 ;; arch-tag: 178d4064-7e60-4745-b81f-bab5d8d7c40f |
29876 | 321 ;;; esh-ext.el ends here |