Several fixes to cus-theme.el.
[bpt/emacs.git] / lisp / eshell / esh-opt.el
CommitLineData
60370d40 1;;; esh-opt.el --- command options processing
affbf647 2
73b0cd50 3;; Copyright (C) 1999-2011 Free Software Foundation, Inc.
affbf647 4
7de5b421
GM
5;; Author: John Wiegley <johnw@gnu.org>
6
affbf647
GM
7;; This file is part of GNU Emacs.
8
4ee57b2a 9;; GNU Emacs is free software: you can redistribute it and/or modify
affbf647 10;; it under the terms of the GNU General Public License as published by
4ee57b2a
GM
11;; the Free Software Foundation, either version 3 of the License, or
12;; (at your option) any later version.
affbf647
GM
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
4ee57b2a 20;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
affbf647 21
4e6cc05c
GM
22;;; Commentary:
23
8c7309fe
GM
24;;; Code:
25
affbf647
GM
26(provide 'esh-opt)
27
4e6cc05c 28(eval-when-compile (require 'esh-ext))
affbf647
GM
29
30(defgroup eshell-opt nil
31 "The options processing code handles command argument parsing for
32Eshell commands implemented in Lisp."
33 :tag "Command options processing"
34 :group 'eshell)
35
affbf647
GM
36;;; User Functions:
37
38(defmacro eshell-eval-using-options (name macro-args
39 options &rest body-forms)
40 "Process NAME's MACRO-ARGS using a set of command line OPTIONS.
41After doing so, settings will be stored in local symbols as declared
42by OPTIONS; FORMS will then be evaluated -- assuming all was OK.
43
44The syntax of OPTIONS is:
45
46 '((?C nil nil multi-column \"multi-column display\")
47 (nil \"help\" nil nil \"show this usage display\")
48 (?r \"reverse\" nil reverse-list \"reverse order while sorting\")
49 :external \"ls\"
50 :usage \"[OPTION]... [FILE]...
51 List information about the FILEs (the current directory by default).
52 Sort entries alphabetically across.\")
53
54`eshell-eval-using-options' returns the value of the last form in
55BODY-FORMS. If instead an external command is run, the tag
56`eshell-external' will be thrown with the new process for its value.
57
58Lastly, any remaining arguments will be available in a locally
59interned variable `args' (created using a `let' form)."
0d182a34 60 (declare (debug (form form sexp body)))
affbf647
GM
61 `(let ((temp-args
62 ,(if (memq ':preserve-args (cadr options))
63 macro-args
64 (list 'eshell-stringify-list
65 (list 'eshell-flatten-list macro-args)))))
5ca67338
GM
66 (let ,(append (mapcar (lambda (opt)
67 (or (and (listp opt) (nth 3 opt))
68 'eshell-option-stub))
affbf647
GM
69 (cadr options))
70 '(usage-msg last-value ext-command args))
71 (eshell-do-opt ,name ,options (quote ,body-forms)))))
72
73;;; Internal Functions:
74
1a32899d
GM
75(defvar temp-args)
76(defvar last-value)
77(defvar usage-msg)
78(defvar ext-command)
5ca67338 79;; Documented part of the interface; see eshell-eval-using-options.
1a32899d 80(defvar args)
affbf647
GM
81
82(defun eshell-do-opt (name options body-forms)
83 "Helper function for `eshell-eval-using-options'.
84This code doesn't really need to be macro expanded everywhere."
85 (setq args temp-args)
86 (if (setq
87 ext-command
88 (catch 'eshell-ext-command
89 (when (setq
90 usage-msg
91 (catch 'eshell-usage
92 (setq last-value nil)
93 (if (and (= (length args) 0)
94 (memq ':show-usage options))
95 (throw 'eshell-usage
96 (eshell-show-usage name options)))
97 (setq args (eshell-process-args name args options)
98 last-value (eval (append (list 'progn)
99 body-forms)))
100 nil))
c009ca06 101 (error "%s" usage-msg))))
affbf647 102 (throw 'eshell-external
0d182a34 103 (eshell-external-command ext-command args))
affbf647
GM
104 last-value))
105
106(defun eshell-show-usage (name options)
107 "Display the usage message for NAME, using OPTIONS."
108 (let ((usage (format "usage: %s %s\n\n" name
109 (cadr (memq ':usage options))))
110 (extcmd (memq ':external options))
111 (post-usage (memq ':post-usage options))
112 had-option)
113 (while options
114 (when (listp (car options))
115 (let ((opt (car options)))
116 (setq had-option t)
117 (cond ((and (nth 0 opt)
118 (nth 1 opt))
119 (setq usage
120 (concat usage
121 (format " %-20s %s\n"
122 (format "-%c, --%s" (nth 0 opt)
123 (nth 1 opt))
124 (nth 4 opt)))))
125 ((nth 0 opt)
126 (setq usage
127 (concat usage
128 (format " %-20s %s\n"
129 (format "-%c" (nth 0 opt))
130 (nth 4 opt)))))
131 ((nth 1 opt)
132 (setq usage
133 (concat usage
134 (format " %-20s %s\n"
135 (format " --%s" (nth 1 opt))
136 (nth 4 opt)))))
137 (t (setq had-option nil)))))
138 (setq options (cdr options)))
139 (if post-usage
140 (setq usage (concat usage (and had-option "\n")
141 (cadr post-usage))))
142 (when extcmd
143 (setq extcmd (eshell-search-path (cadr extcmd)))
144 (if extcmd
145 (setq usage
146 (concat usage
147 (format "
148This command is implemented in Lisp. If an unrecognized option is
149passed to this command, the external version '%s'
150will be called instead." extcmd)))))
151 (throw 'eshell-usage usage)))
152
153(defun eshell-set-option (name ai opt options)
154 "Using NAME's remaining args (index AI), set the OPT within OPTIONS.
155If the option consumes an argument for its value, the argument list
156will be modified."
157 (if (not (nth 3 opt))
158 (eshell-show-usage name options)
159 (if (eq (nth 2 opt) t)
160 (if (> ai (length args))
161 (error "%s: missing option argument" name)
162 (set (nth 3 opt) (nth ai args))
163 (if (> ai 0)
164 (setcdr (nthcdr (1- ai) args) (nthcdr (1+ ai) args))
165 (setq args (cdr args))))
166 (set (nth 3 opt) (or (nth 2 opt) t)))))
167
168(defun eshell-process-option (name switch kind ai options)
169 "For NAME, process SWITCH (of type KIND), from args at index AI.
170The SWITCH will be looked up in the set of OPTIONS.
171
172SWITCH should be either a string or character. KIND should be the
173integer 0 if it's a character, or 1 if it's a string.
174
175The SWITCH is then be matched against OPTIONS. If no matching handler
176is found, and an :external command is defined (and available), it will
177be called; otherwise, an error will be triggered to say that the
178switch is unrecognized."
179 (let* ((opts options)
180 found)
181 (while opts
182 (if (and (listp (car opts))
183 (nth kind (car opts))
184 (if (= kind 0)
185 (eq switch (nth kind (car opts)))
186 (string= switch (nth kind (car opts)))))
187 (progn
188 (eshell-set-option name ai (car opts) options)
189 (setq found t opts nil))
190 (setq opts (cdr opts))))
191 (unless found
192 (let ((extcmd (memq ':external options)))
193 (when extcmd
194 (setq extcmd (eshell-search-path (cadr extcmd)))
195 (if extcmd
196 (throw 'eshell-ext-command extcmd)
8bd3a3ad 197 (if (characterp switch)
affbf647
GM
198 (error "%s: unrecognized option -%c" name switch)
199 (error "%s: unrecognized option --%s" name switch))))))))
200
201(defun eshell-process-args (name args options)
202 "Process the given ARGS using OPTIONS.
203This assumes that symbols have been intern'd by `eshell-with-options'."
204 (let ((ai 0) arg)
205 (while (< ai (length args))
206 (setq arg (nth ai args))
207 (if (not (and (stringp arg)
208 (string-match "^-\\(-\\)?\\(.*\\)" arg)))
209 (setq ai (1+ ai))
210 (let* ((dash (match-string 1 arg))
211 (switch (match-string 2 arg)))
212 (if (= ai 0)
213 (setq args (cdr args))
214 (setcdr (nthcdr (1- ai) args) (nthcdr (1+ ai) args)))
215 (if dash
216 (if (> (length switch) 0)
217 (eshell-process-option name switch 1 ai options)
218 (setq ai (length args)))
219 (let ((len (length switch))
220 (index 0))
221 (while (< index len)
222 (eshell-process-option name (aref switch index) 0 ai options)
223 (setq index (1+ index)))))))))
224 args)
225
affbf647 226;;; esh-opt.el ends here