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