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