Some fixes to follow coding conventions.
[bpt/emacs.git] / lisp / eshell / esh-opt.el
CommitLineData
60370d40 1;;; esh-opt.el --- command options processing
affbf647 2
faadfb0a 3;; Copyright (C) 1999, 2000 Free Software Foundation
affbf647 4
7de5b421
GM
5;; Author: John Wiegley <johnw@gnu.org>
6
affbf647
GM
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-opt)
25
26(eval-when-compile (require 'esh-maint))
27
28(defgroup eshell-opt nil
29 "The options processing code handles command argument parsing for
30Eshell commands implemented in Lisp."
31 :tag "Command options processing"
32 :group 'eshell)
33
34;;; Commentary:
35
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)."
60 `(let ((temp-args
61 ,(if (memq ':preserve-args (cadr options))
62 macro-args
63 (list 'eshell-stringify-list
64 (list 'eshell-flatten-list macro-args)))))
65 (let ,(append (mapcar (function
66 (lambda (opt)
67 (or (and (listp opt) (nth 3 opt))
68 'eshell-option-stub)))
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
75(eval-when-compile
76 (defvar temp-args)
77 (defvar last-value)
78 (defvar usage-msg)
79 (defvar ext-command)
80 (defvar args))
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))
101 (error usage-msg))))
102 (throw 'eshell-external
103 (eshell-external-command ext-command args))
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)
197 (if (char-valid-p switch)
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
226;;; Code:
227
228;;; esh-opt.el ends here