Mercurial > emacs
annotate lisp/eshell/esh-arg.el @ 71615:a6099d890443
*** empty log message ***
author | Stefan Monnier <monnier@iro.umontreal.ca> |
---|---|
date | Tue, 04 Jul 2006 20:28:11 +0000 |
parents | 067115a6e738 |
children | f7702c5f335d c5406394f567 |
rev | line source |
---|---|
38414
67b464da13ec
Some fixes to follow coding conventions.
Pavel Janík <Pavel@Janik.cz>
parents:
37656
diff
changeset
|
1 ;;; esh-arg.el --- argument processing |
29876 | 2 |
64701
34bd8e434dd7
Update years in copyright notice; nfc.
Thien-Thi Nguyen <ttn@gnuvola.org>
parents:
64085
diff
changeset
|
3 ;; Copyright (C) 1999, 2000, 2002, 2003, 2004, |
68648
067115a6e738
Update years in copyright notice; nfc.
Thien-Thi Nguyen <ttn@gnuvola.org>
parents:
65163
diff
changeset
|
4 ;; 2005, 2006 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 | |
10 ;; GNU Emacs is free software; you can redistribute it and/or modify | |
11 ;; it under the terms of the GNU General Public License as published by | |
12 ;; the Free Software Foundation; either version 2, or (at your option) | |
13 ;; any later version. | |
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 | |
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the | |
64085 | 22 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
23 ;; Boston, MA 02110-1301, USA. | |
29876 | 24 |
25 (provide 'esh-arg) | |
26 | |
27 (eval-when-compile (require 'esh-maint)) | |
28 | |
29 (defgroup eshell-arg nil | |
30 "Argument parsing involves transforming the arguments passed on the | |
31 command line into equivalent Lisp forms that, when evaluated, will | |
32 yield the values intended." | |
33 :tag "Argument parsing" | |
34 :group 'eshell) | |
35 | |
36 ;;; Commentary: | |
37 | |
38 ;; Parsing of arguments can be extended by adding functions to the | |
39 ;; hook `eshell-parse-argument-hook'. For a good example of this, see | |
40 ;; `eshell-parse-drive-letter', defined in eshell-dirs.el. | |
41 | |
42 (defcustom eshell-parse-argument-hook | |
43 (list | |
44 ;; a term such as #<buffer NAME>, or #<process NAME> is a buffer | |
45 ;; or process reference | |
46 'eshell-parse-special-reference | |
47 | |
48 ;; numbers convert to numbers if they stand alone | |
49 (function | |
50 (lambda () | |
51 (when (and (not eshell-current-argument) | |
52 (not eshell-current-quoted) | |
53 (looking-at eshell-number-regexp) | |
54 (eshell-arg-delimiter (match-end 0))) | |
55 (goto-char (match-end 0)) | |
37656
e108dfe2ea99
(eshell-parse-argument-hook): If a number is encountered as an
John Wiegley <johnw@newartisans.com>
parents:
32526
diff
changeset
|
56 (let ((str (match-string 0))) |
e108dfe2ea99
(eshell-parse-argument-hook): If a number is encountered as an
John Wiegley <johnw@newartisans.com>
parents:
32526
diff
changeset
|
57 (if (> (length str) 0) |
e108dfe2ea99
(eshell-parse-argument-hook): If a number is encountered as an
John Wiegley <johnw@newartisans.com>
parents:
32526
diff
changeset
|
58 (add-text-properties 0 1 '(number t) str)) |
e108dfe2ea99
(eshell-parse-argument-hook): If a number is encountered as an
John Wiegley <johnw@newartisans.com>
parents:
32526
diff
changeset
|
59 str)))) |
29876 | 60 |
61 ;; parse any non-special characters, based on the current context | |
62 (function | |
63 (lambda () | |
64 (unless eshell-inside-quote-regexp | |
65 (setq eshell-inside-quote-regexp | |
66 (format "[^%s]+" | |
67 (apply 'string eshell-special-chars-inside-quoting)))) | |
68 (unless eshell-outside-quote-regexp | |
69 (setq eshell-outside-quote-regexp | |
70 (format "[^%s]+" | |
71 (apply 'string eshell-special-chars-outside-quoting)))) | |
72 (when (looking-at (if eshell-current-quoted | |
73 eshell-inside-quote-regexp | |
74 eshell-outside-quote-regexp)) | |
75 (goto-char (match-end 0)) | |
76 (let ((str (match-string 0))) | |
77 (if str | |
78 (set-text-properties 0 (length str) nil str)) | |
79 str)))) | |
80 | |
81 ;; whitespace or a comment is an argument delimiter | |
82 (function | |
83 (lambda () | |
84 (let (comment-p) | |
85 (when (or (looking-at "[ \t]+") | |
86 (and (not eshell-current-argument) | |
87 (looking-at "#\\([^<'].*\\|$\\)") | |
88 (setq comment-p t))) | |
89 (if comment-p | |
90 (add-text-properties (match-beginning 0) (match-end 0) | |
91 '(comment t))) | |
92 (goto-char (match-end 0)) | |
93 (eshell-finish-arg))))) | |
94 | |
95 ;; backslash before a special character means escape it | |
96 'eshell-parse-backslash | |
97 | |
98 ;; text beginning with ' is a literally quoted | |
99 'eshell-parse-literal-quote | |
100 | |
101 ;; text beginning with " is interpolably quoted | |
102 'eshell-parse-double-quote | |
103 | |
104 ;; argument delimiter | |
105 'eshell-parse-delimiter) | |
106 "*Define how to process Eshell command line arguments. | |
107 When each function on this hook is called, point will be at the | |
108 current position within the argument list. The function should either | |
109 return nil, meaning that it did no argument parsing, or it should | |
110 return the result of the parse as a sexp. It is also responsible for | |
111 moving the point forward to reflect the amount of input text that was | |
112 parsed. | |
113 | |
114 If no function handles the current character at point, it will be | |
115 treated as a literal character." | |
116 :type 'hook | |
117 :group 'eshell-arg) | |
118 | |
119 ;;; Code: | |
120 | |
121 ;;; User Variables: | |
122 | |
123 (defcustom eshell-arg-load-hook '(eshell-arg-initialize) | |
124 "*A hook that gets run when `eshell-arg' is loaded." | |
125 :type 'hook | |
126 :group 'eshell-arg) | |
127 | |
128 (defcustom eshell-delimiter-argument-list '(?\; ?& ?\| ?\> ? ?\t ?\n) | |
129 "List of characters to recognize as argument separators." | |
130 :type '(repeat character) | |
131 :group 'eshell-arg) | |
132 | |
133 (defcustom eshell-special-chars-inside-quoting '(?\\ ?\") | |
134 "*Characters which are still special inside double quotes." | |
135 :type '(repeat character) | |
136 :group 'eshell-arg) | |
137 | |
138 (defcustom eshell-special-chars-outside-quoting | |
139 (append eshell-delimiter-argument-list '(?# ?! ?\\ ?\" ?\')) | |
140 "*Characters that require escaping outside of double quotes. | |
141 Without escaping them, they will introduce a change in the argument." | |
142 :type '(repeat character) | |
143 :group 'eshell-arg) | |
144 | |
145 ;;; Internal Variables: | |
146 | |
147 (defvar eshell-current-argument nil) | |
148 (defvar eshell-current-modifiers nil) | |
149 (defvar eshell-arg-listified nil) | |
150 (defvar eshell-nested-argument nil) | |
151 (defvar eshell-current-quoted nil) | |
152 (defvar eshell-inside-quote-regexp nil) | |
153 (defvar eshell-outside-quote-regexp nil) | |
154 | |
155 ;;; Functions: | |
156 | |
157 (defun eshell-arg-initialize () | |
158 "Initialize the argument parsing code." | |
159 (define-key eshell-command-map [(meta ?b)] 'eshell-insert-buffer-name) | |
160 (set (make-local-variable 'eshell-inside-quote-regexp) nil) | |
161 (set (make-local-variable 'eshell-outside-quote-regexp) nil)) | |
162 | |
163 (defun eshell-insert-buffer-name (buffer-name) | |
164 "Insert BUFFER-NAME into the current buffer at point." | |
165 (interactive "BName of buffer: ") | |
166 (insert-and-inherit "#<buffer " buffer-name ">")) | |
167 | |
168 (defsubst eshell-escape-arg (string) | |
169 "Return STRING with the `escaped' property on it." | |
170 (if (stringp string) | |
171 (add-text-properties 0 (length string) '(escaped t) string)) | |
172 string) | |
173 | |
174 (defun eshell-resolve-current-argument () | |
175 "If there are pending modifications to be made, make them now." | |
176 (when eshell-current-argument | |
177 (when eshell-arg-listified | |
178 (let ((parts eshell-current-argument)) | |
179 (while parts | |
180 (unless (stringp (car parts)) | |
181 (setcar parts | |
182 (list 'eshell-to-flat-string (car parts)))) | |
183 (setq parts (cdr parts))) | |
184 (setq eshell-current-argument | |
185 (list 'eshell-convert | |
186 (append (list 'concat) eshell-current-argument)))) | |
187 (setq eshell-arg-listified nil)) | |
188 (while eshell-current-modifiers | |
189 (setq eshell-current-argument | |
190 (list (car eshell-current-modifiers) eshell-current-argument) | |
191 eshell-current-modifiers (cdr eshell-current-modifiers)))) | |
192 (setq eshell-current-modifiers nil)) | |
193 | |
194 (defun eshell-finish-arg (&optional argument) | |
195 "Finish the current argument being processed." | |
196 (if argument | |
197 (setq eshell-current-argument argument)) | |
198 (throw 'eshell-arg-done t)) | |
199 | |
200 (defsubst eshell-arg-delimiter (&optional pos) | |
201 "Return non-nil if POS is an argument delimiter. | |
202 If POS is nil, the location of point is checked." | |
203 (let ((pos (or pos (point)))) | |
204 (or (= pos (point-max)) | |
205 (memq (char-after pos) eshell-delimiter-argument-list)))) | |
206 | |
207 ;; Argument parsing | |
208 | |
209 (defun eshell-parse-arguments (beg end) | |
210 "Parse all of the arguments at point from BEG to END. | |
211 Returns the list of arguments in their raw form. | |
212 Point is left at the end of the arguments." | |
213 (save-excursion | |
214 (save-restriction | |
215 (goto-char beg) | |
216 (narrow-to-region beg end) | |
217 (let ((inhibit-point-motion-hooks t) | |
218 (args (list t)) | |
219 after-change-functions | |
220 delim) | |
221 (remove-text-properties (point-min) (point-max) | |
222 '(arg-begin nil arg-end nil)) | |
223 (if (setq | |
224 delim | |
225 (catch 'eshell-incomplete | |
226 (while (not (eobp)) | |
227 (let* ((here (point)) | |
228 (arg (eshell-parse-argument))) | |
229 (if (= (point) here) | |
230 (error "Failed to parse argument '%s'" | |
231 (buffer-substring here (point-max)))) | |
232 (and arg (nconc args (list arg))))))) | |
233 (if (listp delim) | |
234 (throw 'eshell-incomplete delim) | |
235 (throw 'eshell-incomplete | |
236 (list delim (point) (cdr args))))) | |
237 (cdr args))))) | |
238 | |
239 (defun eshell-parse-argument () | |
240 "Get the next argument. Leave point after it." | |
241 (let* ((outer (null eshell-nested-argument)) | |
242 (arg-begin (and outer (point))) | |
243 (eshell-nested-argument t) | |
244 eshell-current-argument | |
245 eshell-current-modifiers | |
246 eshell-arg-listified) | |
247 (catch 'eshell-arg-done | |
248 (while (not (eobp)) | |
249 (let ((result | |
250 (or (run-hook-with-args-until-success | |
251 'eshell-parse-argument-hook) | |
252 (prog1 | |
253 (char-to-string (char-after)) | |
254 (forward-char))))) | |
255 (if (not eshell-current-argument) | |
256 (setq eshell-current-argument result) | |
257 (unless eshell-arg-listified | |
258 (setq eshell-current-argument | |
259 (list eshell-current-argument) | |
260 eshell-arg-listified t)) | |
261 (nconc eshell-current-argument (list result)))))) | |
262 (when (and outer eshell-current-argument) | |
263 (add-text-properties arg-begin (1+ arg-begin) | |
264 '(arg-begin t rear-nonsticky | |
265 (arg-begin arg-end))) | |
266 (add-text-properties (1- (point)) (point) | |
267 '(arg-end t rear-nonsticky | |
268 (arg-end arg-begin)))) | |
269 (eshell-resolve-current-argument) | |
270 eshell-current-argument)) | |
271 | |
272 (defsubst eshell-operator (&rest args) | |
273 "A stub function that generates an error if a floating operator is found." | |
274 (error "Unhandled operator in input text")) | |
275 | |
276 (defsubst eshell-looking-at-backslash-return (pos) | |
277 "Test whether a backslash-return sequence occurs at POS." | |
278 (and (eq (char-after pos) ?\\) | |
279 (or (= (1+ pos) (point-max)) | |
280 (and (eq (char-after (1+ pos)) ?\n) | |
281 (= (+ pos 2) (point-max)))))) | |
282 | |
283 (defun eshell-quote-backslash (string &optional index) | |
284 "Intelligently backslash the character occuring in STRING at INDEX. | |
285 If the character is itself a backslash, it needs no escaping." | |
286 (let ((char (aref string index))) | |
287 (if (eq char ?\\) | |
288 (char-to-string char) | |
289 (if (memq char eshell-special-chars-outside-quoting) | |
290 (string ?\\ char))))) | |
291 | |
292 (defun eshell-parse-backslash () | |
293 "Parse a single backslash (\) character, which might mean escape. | |
294 It only means escape if the character immediately following is a | |
295 special character that is not itself a backslash." | |
296 (when (eq (char-after) ?\\) | |
297 (if (eshell-looking-at-backslash-return (point)) | |
298 (throw 'eshell-incomplete ?\\) | |
299 (if (and (not (eq (char-after (1+ (point))) ?\\)) | |
300 (if eshell-current-quoted | |
301 (memq (char-after (1+ (point))) | |
302 eshell-special-chars-inside-quoting) | |
303 (memq (char-after (1+ (point))) | |
304 eshell-special-chars-outside-quoting))) | |
305 (progn | |
306 (forward-char 2) | |
307 (list 'eshell-escape-arg | |
308 (char-to-string (char-before)))) | |
309 ;; allow \\<RET> to mean a literal "\" character followed by a | |
310 ;; normal return, rather than a backslash followed by a line | |
311 ;; continuator (i.e., "\\ + \n" rather than "\ + \\n"). This | |
312 ;; is necessary because backslashes in Eshell are not special | |
313 ;; unless they either precede something special, or precede a | |
314 ;; backslash that precedes something special. (Mainly this is | |
315 ;; done to make using backslash on Windows systems more | |
316 ;; natural-feeling). | |
317 (if (eshell-looking-at-backslash-return (1+ (point))) | |
318 (forward-char)) | |
319 (forward-char) | |
320 "\\")))) | |
321 | |
322 (defun eshell-parse-literal-quote () | |
323 "Parse a literally quoted string. Nothing has special meaning!" | |
324 (if (eq (char-after) ?\') | |
325 (let ((end (eshell-find-delimiter ?\' ?\'))) | |
326 (if (not end) | |
327 (throw 'eshell-incomplete ?\') | |
328 (let ((string (buffer-substring-no-properties (1+ (point)) end))) | |
329 (goto-char (1+ end)) | |
330 (while (string-match "''" string) | |
331 (setq string (replace-match "'" t t string))) | |
332 (list 'eshell-escape-arg string)))))) | |
333 | |
334 (defun eshell-parse-double-quote () | |
335 "Parse a double quoted string, which allows for variable interpolation." | |
336 (when (eq (char-after) ?\") | |
337 (let* ((end (eshell-find-delimiter ?\" ?\" nil nil t)) | |
338 (eshell-current-quoted t)) | |
339 (if (not end) | |
340 (throw 'eshell-incomplete ?\") | |
341 (prog1 | |
342 (save-restriction | |
32446
aab90b31807c
Added better remote directory support to Eshell, as well as a few bug
John Wiegley <johnw@newartisans.com>
parents:
29934
diff
changeset
|
343 (forward-char) |
29876 | 344 (narrow-to-region (point) end) |
65163
ef138cf4be94
(eshell-parse-double-quote): If a double-quoted argument resolves to
John Wiegley <johnw@newartisans.com>
parents:
64701
diff
changeset
|
345 (let ((arg (eshell-parse-argument))) |
ef138cf4be94
(eshell-parse-double-quote): If a double-quoted argument resolves to
John Wiegley <johnw@newartisans.com>
parents:
64701
diff
changeset
|
346 (if (eq arg nil) |
ef138cf4be94
(eshell-parse-double-quote): If a double-quoted argument resolves to
John Wiegley <johnw@newartisans.com>
parents:
64701
diff
changeset
|
347 "" |
ef138cf4be94
(eshell-parse-double-quote): If a double-quoted argument resolves to
John Wiegley <johnw@newartisans.com>
parents:
64701
diff
changeset
|
348 (list 'eshell-escape-arg arg)))) |
29876 | 349 (goto-char (1+ end))))))) |
350 | |
351 (defun eshell-parse-special-reference () | |
352 "Parse a special syntax reference, of the form '#<type arg>'." | |
353 (if (and (not eshell-current-argument) | |
354 (not eshell-current-quoted) | |
355 (looking-at "#<\\(buffer\\|process\\)\\s-")) | |
356 (let ((here (point))) | |
357 (goto-char (match-end 0)) | |
358 (let* ((buffer-p (string= (match-string 1) "buffer")) | |
359 (end (eshell-find-delimiter ?\< ?\>))) | |
360 (if (not end) | |
361 (throw 'eshell-incomplete ?\<) | |
362 (if (eshell-arg-delimiter (1+ end)) | |
363 (prog1 | |
364 (list (if buffer-p 'get-buffer-create 'get-process) | |
365 (buffer-substring-no-properties (point) end)) | |
366 (goto-char (1+ end))) | |
367 (ignore (goto-char here)))))))) | |
368 | |
369 (defun eshell-parse-delimiter () | |
370 "Parse an argument delimiter, which is essentially a command operator." | |
371 ;; this `eshell-operator' keyword gets parsed out by | |
372 ;; `eshell-separate-commands'. Right now the only possibility for | |
373 ;; error is an incorrect output redirection specifier. | |
374 (when (looking-at "[&|;\n]\\s-*") | |
375 (let ((end (match-end 0))) | |
376 (if eshell-current-argument | |
377 (eshell-finish-arg) | |
378 (eshell-finish-arg | |
379 (prog1 | |
380 (list 'eshell-operator | |
381 (cond | |
382 ((eq (char-after end) ?\&) | |
383 (setq end (1+ end)) "&&") | |
384 ((eq (char-after end) ?\|) | |
385 (setq end (1+ end)) "||") | |
386 ((eq (char-after) ?\n) ";") | |
387 (t | |
388 (char-to-string (char-after))))) | |
389 (goto-char end))))))) | |
390 | |
52401 | 391 ;;; arch-tag: 7f593a2b-8fc1-4def-8f84-8f51ed0198d6 |
29876 | 392 ;;; esh-arg.el ends here |