Update copyright notices for 2013.
[emacs.git] / lisp / eshell / esh-opt.el
blob6044759f8df98129e0877a63ae9f81de7e4d16c8
1 ;;; esh-opt.el --- command options processing
3 ;; Copyright (C) 1999-2013 Free Software Foundation, Inc.
5 ;; Author: John Wiegley <johnw@gnu.org>
7 ;; This file is part of GNU Emacs.
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 3 of the License, or
12 ;; (at your option) any later version.
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.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
22 ;;; Commentary:
24 ;;; Code:
26 (provide 'esh-opt)
28 (eval-when-compile (require 'esh-ext))
30 (defgroup eshell-opt nil
31 "The options processing code handles command argument parsing for
32 Eshell commands implemented in Lisp."
33 :tag "Command options processing"
34 :group 'eshell)
36 ;;; User Functions:
38 (defmacro eshell-eval-using-options (name macro-args options &rest body-forms)
39 "Process NAME's MACRO-ARGS using a set of command line OPTIONS.
40 After doing so, stores settings in local symbols as declared by OPTIONS;
41 then evaluates BODY-FORMS -- assuming all was OK.
43 OPTIONS is a list, beginning with one or more elements of the form:
44 \(SHORT LONG VALUE SYMBOL HELP-STRING)
45 Each of these elements represents a particular command-line switch.
47 SHORT is either nil, or a character that can be used as a switch -SHORT.
48 LONG is either nil, or a string that can be used as a switch --LONG.
49 At least one of SHORT and LONG must be non-nil.
50 VALUE is the value associated with the option. It can be either:
51 t - the option needs a value to be specified after the switch;
52 nil - the option is given the value t;
53 anything else - specifies the actual value for the option.
54 SYMBOL is either nil, or the name of the Lisp symbol that will be bound
55 to VALUE. A nil SYMBOL calls `eshell-show-usage', and so is appropriate
56 for a \"--help\" type option.
57 HELP-STRING is a documentation string for the option.
59 Any remaining elements of OPTIONS are :KEYWORD arguments. Some take
60 arguments, some do not. The recognized :KEYWORDS are:
62 :external STRING
63 STRING is an external command to run if there are unknown switches.
65 :usage STRING
66 STRING is the initial part of the command's documentation string.
67 It appears before the options are listed.
69 :post-usage STRING
70 STRING is an optional trailing part of the command's documentation string.
71 It appears after the options, but before the final part of the
72 documentation about the associated external command (if there is one).
74 :show-usage
75 If present, then show the usage message if the command is called with no
76 arguments.
78 :preserve-args
79 If present, do not pass MACRO-ARGS through `eshell-flatten-list'
80 and `eshell-stringify-list'.
82 For example, OPTIONS might look like:
84 '((?C nil nil multi-column \"multi-column display\")
85 (nil \"help\" nil nil \"show this usage display\")
86 (?r \"reverse\" nil reverse-list \"reverse order while sorting\")
87 :external \"ls\"
88 :usage \"[OPTION]... [FILE]...
89 List information about the FILEs (the current directory by default).
90 Sort entries alphabetically across.\")
92 `eshell-eval-using-options' returns the value of the last form in
93 BODY-FORMS. If instead an external command is run (because of
94 an unknown option), the tag `eshell-external' will be thrown with
95 the new process for its value.
97 Lastly, any remaining arguments will be available in a locally
98 interned variable `args' (created using a `let' form)."
99 (declare (debug (form form sexp body)))
100 `(let ((temp-args
101 ,(if (memq ':preserve-args (cadr options))
102 macro-args
103 (list 'eshell-stringify-list
104 (list 'eshell-flatten-list macro-args)))))
105 (let ,(append (delq nil (mapcar (lambda (opt)
106 (and (listp opt) (nth 3 opt)))
107 (cadr options)))
108 '(usage-msg last-value ext-command args))
109 ;; FIXME: `options' ends up hiding some variable names under `quote',
110 ;; which is incompatible with lexical scoping!!
111 (eshell-do-opt ,name ,options (lambda () ,@body-forms)))))
113 ;;; Internal Functions:
115 (defvar temp-args)
116 (defvar last-value)
117 (defvar usage-msg)
118 (defvar ext-command)
119 ;; Documented part of the interface; see eshell-eval-using-options.
120 (defvar args)
122 (defun eshell-do-opt (name options body-fun)
123 "Helper function for `eshell-eval-using-options'.
124 This code doesn't really need to be macro expanded everywhere."
125 (setq args temp-args)
126 (if (setq
127 ext-command
128 (catch 'eshell-ext-command
129 (when (setq
130 usage-msg
131 (catch 'eshell-usage
132 (setq last-value nil)
133 (if (and (= (length args) 0)
134 (memq ':show-usage options))
135 (throw 'eshell-usage
136 (eshell-show-usage name options)))
137 (setq args (eshell-process-args name args options)
138 last-value (funcall body-fun))
139 nil))
140 (error "%s" usage-msg))))
141 (throw 'eshell-external
142 (eshell-external-command ext-command args))
143 last-value))
145 (defun eshell-show-usage (name options)
146 "Display the usage message for NAME, using OPTIONS."
147 (let ((usage (format "usage: %s %s\n\n" name
148 (cadr (memq ':usage options))))
149 (extcmd (memq ':external options))
150 (post-usage (memq ':post-usage options))
151 had-option)
152 (while options
153 (when (listp (car options))
154 (let ((opt (car options)))
155 (setq had-option t)
156 (cond ((and (nth 0 opt)
157 (nth 1 opt))
158 (setq usage
159 (concat usage
160 (format " %-20s %s\n"
161 (format "-%c, --%s" (nth 0 opt)
162 (nth 1 opt))
163 (nth 4 opt)))))
164 ((nth 0 opt)
165 (setq usage
166 (concat usage
167 (format " %-20s %s\n"
168 (format "-%c" (nth 0 opt))
169 (nth 4 opt)))))
170 ((nth 1 opt)
171 (setq usage
172 (concat usage
173 (format " %-20s %s\n"
174 (format " --%s" (nth 1 opt))
175 (nth 4 opt)))))
176 (t (setq had-option nil)))))
177 (setq options (cdr options)))
178 (if post-usage
179 (setq usage (concat usage (and had-option "\n")
180 (cadr post-usage))))
181 (when extcmd
182 (setq extcmd (eshell-search-path (cadr extcmd)))
183 (if extcmd
184 (setq usage
185 (concat usage
186 (format "
187 This command is implemented in Lisp. If an unrecognized option is
188 passed to this command, the external version '%s'
189 will be called instead." extcmd)))))
190 (throw 'eshell-usage usage)))
192 (defun eshell-set-option (name ai opt options)
193 "Using NAME's remaining args (index AI), set the OPT within OPTIONS.
194 If the option consumes an argument for its value, the argument list
195 will be modified."
196 (if (not (nth 3 opt))
197 (eshell-show-usage name options)
198 (if (eq (nth 2 opt) t)
199 (if (> ai (length args))
200 (error "%s: missing option argument" name)
201 (set (nth 3 opt) (nth ai args))
202 (if (> ai 0)
203 (setcdr (nthcdr (1- ai) args) (nthcdr (1+ ai) args))
204 (setq args (cdr args))))
205 (set (nth 3 opt) (or (nth 2 opt) t)))))
207 (defun eshell-process-option (name switch kind ai options)
208 "For NAME, process SWITCH (of type KIND), from args at index AI.
209 The SWITCH will be looked up in the set of OPTIONS.
211 SWITCH should be either a string or character. KIND should be the
212 integer 0 if it's a character, or 1 if it's a string.
214 The SWITCH is then be matched against OPTIONS. If no matching handler
215 is found, and an :external command is defined (and available), it will
216 be called; otherwise, an error will be triggered to say that the
217 switch is unrecognized."
218 (let* ((opts options)
219 found)
220 (while opts
221 (if (and (listp (car opts))
222 (nth kind (car opts))
223 (equal switch (nth kind (car opts))))
224 (progn
225 (eshell-set-option name ai (car opts) options)
226 (setq found t opts nil))
227 (setq opts (cdr opts))))
228 (unless found
229 (let ((extcmd (memq ':external options)))
230 (when extcmd
231 (setq extcmd (eshell-search-path (cadr extcmd)))
232 (if extcmd
233 (throw 'eshell-ext-command extcmd)
234 (if (characterp switch)
235 (error "%s: unrecognized option -%c" name switch)
236 (error "%s: unrecognized option --%s" name switch))))))))
238 (defun eshell-process-args (name args options)
239 "Process the given ARGS using OPTIONS.
240 This assumes that symbols have been intern'd by `eshell-eval-using-options'."
241 (let ((ai 0) arg)
242 (while (< ai (length args))
243 (setq arg (nth ai args))
244 (if (not (and (stringp arg)
245 (string-match "^-\\(-\\)?\\(.*\\)" arg)))
246 (setq ai (1+ ai))
247 (let* ((dash (match-string 1 arg))
248 (switch (match-string 2 arg)))
249 (if (= ai 0)
250 (setq args (cdr args))
251 (setcdr (nthcdr (1- ai) args) (nthcdr (1+ ai) args)))
252 (if dash
253 (if (> (length switch) 0)
254 (eshell-process-option name switch 1 ai options)
255 (setq ai (length args)))
256 (let ((len (length switch))
257 (index 0))
258 (while (< index len)
259 (eshell-process-option name (aref switch index) 0 ai options)
260 (setq index (1+ index)))))))))
261 args)
263 ;;; esh-opt.el ends here