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