1 ;;; esh-arg --- argument processing
3 ;; Copyright (C) 1999, 2000 Free Software Foundation
5 ;; This file is part of GNU Emacs.
7 ;; GNU Emacs is free software; you can redistribute it and/or modify
8 ;; it under the terms of the GNU General Public License as published by
9 ;; the Free Software Foundation; either version 2, or (at your option)
12 ;; GNU Emacs is distributed in the hope that it will be useful,
13 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ;; GNU General Public License for more details.
17 ;; You should have received a copy of the GNU General Public License
18 ;; along with GNU Emacs; see the file COPYING. If not, write to the
19 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 ;; Boston, MA 02111-1307, USA.
24 (eval-when-compile (require 'esh-maint
))
26 (defgroup eshell-arg nil
27 "Argument parsing involves transforming the arguments passed on the
28 command line into equivalent Lisp forms that, when evaluated, will
29 yield the values intended."
30 :tag
"Argument parsing"
35 ;; Parsing of arguments can be extended by adding functions to the
36 ;; hook `eshell-parse-argument-hook'. For a good example of this, see
37 ;; `eshell-parse-drive-letter', defined in eshell-dirs.el.
39 (defcustom eshell-parse-argument-hook
41 ;; a term such as #<buffer NAME>, or #<process NAME> is a buffer
42 ;; or process reference
43 'eshell-parse-special-reference
45 ;; numbers convert to numbers if they stand alone
48 (when (and (not eshell-current-argument
)
49 (not eshell-current-quoted
)
50 (looking-at eshell-number-regexp
)
51 (eshell-arg-delimiter (match-end 0)))
52 (goto-char (match-end 0))
53 (string-to-number (match-string 0)))))
55 ;; parse any non-special characters, based on the current context
58 (unless eshell-inside-quote-regexp
59 (setq eshell-inside-quote-regexp
61 (apply 'string eshell-special-chars-inside-quoting
))))
62 (unless eshell-outside-quote-regexp
63 (setq eshell-outside-quote-regexp
65 (apply 'string eshell-special-chars-outside-quoting
))))
66 (when (looking-at (if eshell-current-quoted
67 eshell-inside-quote-regexp
68 eshell-outside-quote-regexp
))
69 (goto-char (match-end 0))
70 (let ((str (match-string 0)))
72 (set-text-properties 0 (length str
) nil str
))
75 ;; whitespace or a comment is an argument delimiter
79 (when (or (looking-at "[ \t]+")
80 (and (not eshell-current-argument
)
81 (looking-at "#\\([^<'].*\\|$\\)")
84 (add-text-properties (match-beginning 0) (match-end 0)
86 (goto-char (match-end 0))
87 (eshell-finish-arg)))))
89 ;; backslash before a special character means escape it
90 'eshell-parse-backslash
92 ;; text beginning with ' is a literally quoted
93 'eshell-parse-literal-quote
95 ;; text beginning with " is interpolably quoted
96 'eshell-parse-double-quote
99 'eshell-parse-delimiter
)
100 "*Define how to process Eshell command line arguments.
101 When each function on this hook is called, point will be at the
102 current position within the argument list. The function should either
103 return nil, meaning that it did no argument parsing, or it should
104 return the result of the parse as a sexp. It is also responsible for
105 moving the point forward to reflect the amount of input text that was
108 If no function handles the current character at point, it will be
109 treated as a literal character."
117 (defcustom eshell-arg-load-hook
'(eshell-arg-initialize)
118 "*A hook that gets run when `eshell-arg' is loaded."
122 (defcustom eshell-delimiter-argument-list
'(?\
; ?& ?\| ?\> ? ?\t ?\n)
123 "List of characters to recognize as argument separators."
124 :type
'(repeat character
)
127 (defcustom eshell-special-chars-inside-quoting
'(?
\\ ?
\")
128 "*Characters which are still special inside double quotes."
129 :type
'(repeat character
)
132 (defcustom eshell-special-chars-outside-quoting
133 (append eshell-delimiter-argument-list
'(?
# ?
! ?
\\ ?
\" ?
\'))
134 "*Characters that require escaping outside of double quotes.
135 Without escaping them, they will introduce a change in the argument."
136 :type
'(repeat character
)
139 ;;; Internal Variables:
141 (defvar eshell-current-argument nil
)
142 (defvar eshell-current-modifiers nil
)
143 (defvar eshell-arg-listified nil
)
144 (defvar eshell-nested-argument nil
)
145 (defvar eshell-current-quoted nil
)
146 (defvar eshell-inside-quote-regexp nil
)
147 (defvar eshell-outside-quote-regexp nil
)
151 (defun eshell-arg-initialize ()
152 "Initialize the argument parsing code."
153 (define-key eshell-command-map
[(meta ?b
)] 'eshell-insert-buffer-name
)
154 (set (make-local-variable 'eshell-inside-quote-regexp
) nil
)
155 (set (make-local-variable 'eshell-outside-quote-regexp
) nil
))
157 (defun eshell-insert-buffer-name (buffer-name)
158 "Insert BUFFER-NAME into the current buffer at point."
159 (interactive "BName of buffer: ")
160 (insert-and-inherit "#<buffer " buffer-name
">"))
162 (defsubst eshell-escape-arg
(string)
163 "Return STRING with the `escaped' property on it."
165 (add-text-properties 0 (length string
) '(escaped t
) string
))
168 (defun eshell-resolve-current-argument ()
169 "If there are pending modifications to be made, make them now."
170 (when eshell-current-argument
171 (when eshell-arg-listified
172 (let ((parts eshell-current-argument
))
174 (unless (stringp (car parts
))
176 (list 'eshell-to-flat-string
(car parts
))))
177 (setq parts
(cdr parts
)))
178 (setq eshell-current-argument
179 (list 'eshell-convert
180 (append (list 'concat
) eshell-current-argument
))))
181 (setq eshell-arg-listified nil
))
182 (while eshell-current-modifiers
183 (setq eshell-current-argument
184 (list (car eshell-current-modifiers
) eshell-current-argument
)
185 eshell-current-modifiers
(cdr eshell-current-modifiers
))))
186 (setq eshell-current-modifiers nil
))
188 (defun eshell-finish-arg (&optional argument
)
189 "Finish the current argument being processed."
191 (setq eshell-current-argument argument
))
192 (throw 'eshell-arg-done t
))
194 (defsubst eshell-arg-delimiter
(&optional pos
)
195 "Return non-nil if POS is an argument delimiter.
196 If POS is nil, the location of point is checked."
197 (let ((pos (or pos
(point))))
198 (or (= pos
(point-max))
199 (memq (char-after pos
) eshell-delimiter-argument-list
))))
203 (defun eshell-parse-arguments (beg end
)
204 "Parse all of the arguments at point from BEG to END.
205 Returns the list of arguments in their raw form.
206 Point is left at the end of the arguments."
210 (narrow-to-region beg end
)
211 (let ((inhibit-point-motion-hooks t
)
213 after-change-functions
215 (remove-text-properties (point-min) (point-max)
216 '(arg-begin nil arg-end nil
))
219 (catch 'eshell-incomplete
221 (let* ((here (point))
222 (arg (eshell-parse-argument)))
224 (error "Failed to parse argument '%s'"
225 (buffer-substring here
(point-max))))
226 (and arg
(nconc args
(list arg
)))))))
228 (throw 'eshell-incomplete delim
)
229 (throw 'eshell-incomplete
230 (list delim
(point) (cdr args
)))))
233 (defun eshell-parse-argument ()
234 "Get the next argument. Leave point after it."
235 (let* ((outer (null eshell-nested-argument
))
236 (arg-begin (and outer
(point)))
237 (eshell-nested-argument t
)
238 eshell-current-argument
239 eshell-current-modifiers
240 eshell-arg-listified
)
241 (catch 'eshell-arg-done
244 (or (run-hook-with-args-until-success
245 'eshell-parse-argument-hook
)
247 (char-to-string (char-after))
249 (if (not eshell-current-argument
)
250 (setq eshell-current-argument result
)
251 (unless eshell-arg-listified
252 (setq eshell-current-argument
253 (list eshell-current-argument
)
254 eshell-arg-listified t
))
255 (nconc eshell-current-argument
(list result
))))))
256 (when (and outer eshell-current-argument
)
257 (add-text-properties arg-begin
(1+ arg-begin
)
258 '(arg-begin t rear-nonsticky
259 (arg-begin arg-end
)))
260 (add-text-properties (1- (point)) (point)
261 '(arg-end t rear-nonsticky
262 (arg-end arg-begin
))))
263 (eshell-resolve-current-argument)
264 eshell-current-argument
))
266 (defsubst eshell-operator
(&rest args
)
267 "A stub function that generates an error if a floating operator is found."
268 (error "Unhandled operator in input text"))
270 (defsubst eshell-looking-at-backslash-return
(pos)
271 "Test whether a backslash-return sequence occurs at POS."
272 (and (eq (char-after pos
) ?
\\)
273 (or (= (1+ pos
) (point-max))
274 (and (eq (char-after (1+ pos
)) ?
\n)
275 (= (+ pos
2) (point-max))))))
277 (defun eshell-quote-backslash (string &optional index
)
278 "Intelligently backslash the character occuring in STRING at INDEX.
279 If the character is itself a backslash, it needs no escaping."
280 (let ((char (aref string index
)))
282 (char-to-string char
)
283 (if (memq char eshell-special-chars-outside-quoting
)
284 (string ?
\\ char
)))))
286 (defun eshell-parse-backslash ()
287 "Parse a single backslash (\) character, which might mean escape.
288 It only means escape if the character immediately following is a
289 special character that is not itself a backslash."
290 (when (eq (char-after) ?
\\)
291 (if (eshell-looking-at-backslash-return (point))
292 (throw 'eshell-incomplete ?
\\)
293 (if (and (not (eq (char-after (1+ (point))) ?
\\))
294 (if eshell-current-quoted
295 (memq (char-after (1+ (point)))
296 eshell-special-chars-inside-quoting
)
297 (memq (char-after (1+ (point)))
298 eshell-special-chars-outside-quoting
)))
301 (list 'eshell-escape-arg
302 (char-to-string (char-before))))
303 ;; allow \\<RET> to mean a literal "\" character followed by a
304 ;; normal return, rather than a backslash followed by a line
305 ;; continuator (i.e., "\\ + \n" rather than "\ + \\n"). This
306 ;; is necessary because backslashes in Eshell are not special
307 ;; unless they either precede something special, or precede a
308 ;; backslash that precedes something special. (Mainly this is
309 ;; done to make using backslash on Windows systems more
311 (if (eshell-looking-at-backslash-return (1+ (point)))
316 (defun eshell-parse-literal-quote ()
317 "Parse a literally quoted string. Nothing has special meaning!"
318 (if (eq (char-after) ?
\')
319 (let ((end (eshell-find-delimiter ?
\' ?
\')))
321 (throw 'eshell-incomplete ?
\')
322 (let ((string (buffer-substring-no-properties (1+ (point)) end
)))
324 (while (string-match "''" string
)
325 (setq string
(replace-match "'" t t string
)))
326 (list 'eshell-escape-arg string
))))))
328 (defun eshell-parse-double-quote ()
329 "Parse a double quoted string, which allows for variable interpolation."
330 (when (eq (char-after) ?
\")
332 (let* ((end (eshell-find-delimiter ?
\" ?
\" nil nil t
))
333 (eshell-current-quoted t
))
335 (throw 'eshell-incomplete ?
\")
338 (narrow-to-region (point) end
)
339 (list 'eshell-escape-arg
340 (eshell-parse-argument)))
341 (goto-char (1+ end
)))))))
343 (defun eshell-parse-special-reference ()
344 "Parse a special syntax reference, of the form '#<type arg>'."
345 (if (and (not eshell-current-argument
)
346 (not eshell-current-quoted
)
347 (looking-at "#<\\(buffer\\|process\\)\\s-"))
348 (let ((here (point)))
349 (goto-char (match-end 0))
350 (let* ((buffer-p (string= (match-string 1) "buffer"))
351 (end (eshell-find-delimiter ?\
< ?\
>)))
353 (throw 'eshell-incomplete ?\
<)
354 (if (eshell-arg-delimiter (1+ end
))
356 (list (if buffer-p
'get-buffer-create
'get-process
)
357 (buffer-substring-no-properties (point) end
))
358 (goto-char (1+ end
)))
359 (ignore (goto-char here
))))))))
361 (defun eshell-parse-delimiter ()
362 "Parse an argument delimiter, which is essentially a command operator."
363 ;; this `eshell-operator' keyword gets parsed out by
364 ;; `eshell-separate-commands'. Right now the only possibility for
365 ;; error is an incorrect output redirection specifier.
366 (when (looking-at "[&|;\n]\\s-*")
367 (let ((end (match-end 0)))
368 (if eshell-current-argument
372 (list 'eshell-operator
374 ((eq (char-after end
) ?\
&)
375 (setq end
(1+ end
)) "&&")
376 ((eq (char-after end
) ?\|
)
377 (setq end
(1+ end
)) "||")
378 ((eq (char-after) ?
\n) ";")
380 (char-to-string (char-after)))))
381 (goto-char end
)))))))
383 ;;; esh-arg.el ends here