aboutsummaryrefslogtreecommitdiffstats
path: root/lisp
diff options
context:
space:
mode:
authorJim Porter2024-02-13 12:27:38 -0800
committerJim Porter2024-02-13 12:27:38 -0800
commit160165e8a97cfa3f3ffd803be373a3b34ed87597 (patch)
tree714a72484f135fc651f709ff62efba70c42cd3cb /lisp
parent371ccf09fea26892a2fada028d27fb4b596636df (diff)
downloademacs-160165e8a97cfa3f3ffd803be373a3b34ed87597.tar.gz
emacs-160165e8a97cfa3f3ffd803be373a3b34ed87597.zip
; Compute the list of symbols for 'eshell-eval-using-options' once
* lisp/eshell/esh-opt.el (eshell--get-option-symbols): New function... (eshell-eval-using-options): ... use it. (eshell--do-opts, eshell--process-args): Take OPTION-SYMS. * test/lisp/eshell/esh-opt-tests.el (esh-opt-test/process-args): (esh-opt-test/process-args-parse-leading-options-only): (esh-opt-test/process-args-external): Pass OPTION-SYMS in.
Diffstat (limited to 'lisp')
-rw-r--r--lisp/eshell/esh-opt.el62
1 files changed, 34 insertions, 28 deletions
diff --git a/lisp/eshell/esh-opt.el b/lisp/eshell/esh-opt.el
index d01e3569d57..e6f5fc9629a 100644
--- a/lisp/eshell/esh-opt.el
+++ b/lisp/eshell/esh-opt.el
@@ -100,29 +100,37 @@ the new process for its value.
100Lastly, any remaining arguments will be available in the locally 100Lastly, any remaining arguments will be available in the locally
101let-bound variable `args'." 101let-bound variable `args'."
102 (declare (debug (form form sexp body))) 102 (declare (debug (form form sexp body)))
103 `(let* ((temp-args 103 (let ((option-syms (eshell--get-option-symbols
104 ,(if (memq ':preserve-args (cadr options)) 104 ;; `options' is of the form (quote OPTS).
105 (list 'copy-tree macro-args) 105 (cadr options))))
106 (list 'eshell-stringify-list 106 `(let* ((temp-args
107 (list 'flatten-tree macro-args)))) 107 ,(if (memq ':preserve-args (cadr options))
108 (processed-args (eshell--do-opts ,name ,options temp-args ,macro-args)) 108 (list 'copy-tree macro-args)
109 ,@(delete-dups 109 (list 'eshell-stringify-list
110 (delq nil (mapcar (lambda (opt) 110 (list 'flatten-tree macro-args))))
111 (and (listp opt) (nth 3 opt) 111 (args (eshell--do-opts ,name temp-args ,macro-args
112 `(,(nth 3 opt) (pop processed-args)))) 112 ,options ',option-syms))
113 ;; `options' is of the form (quote OPTS). 113 ;; Bind all the option variables. When done, `args' will
114 (cadr options)))) 114 ;; contain any remaining positional arguments.
115 (args processed-args)) 115 ,@(mapcar (lambda (sym) `(,sym (pop args))) option-syms))
116 ;; Silence unused lexical variable warning if body does not use `args'. 116 ;; Silence unused lexical variable warning if body does not use `args'.
117 (ignore args) 117 (ignore args)
118 ,@body-forms)) 118 ,@body-forms)))
119 119
120;;; Internal Functions: 120;;; Internal Functions:
121 121
122;; Documented part of the interface; see eshell-eval-using-options. 122;; Documented part of the interface; see eshell-eval-using-options.
123(defvar eshell--args) 123(defvar eshell--args)
124 124
125(defun eshell--do-opts (name options args orig-args) 125(defun eshell--get-option-symbols (options)
126 "Get a list of symbols for the specified OPTIONS.
127OPTIONS is a list of command-line options from
128`eshell-eval-using-options' (which see)."
129 (delete-dups
130 (delq nil (mapcar (lambda (opt) (and (listp opt) (nth 3 opt)))
131 options))))
132
133(defun eshell--do-opts (name args orig-args options option-syms)
126 "Helper function for `eshell-eval-using-options'. 134 "Helper function for `eshell-eval-using-options'.
127This code doesn't really need to be macro expanded everywhere." 135This code doesn't really need to be macro expanded everywhere."
128 (require 'esh-ext) 136 (require 'esh-ext)
@@ -134,7 +142,8 @@ This code doesn't really need to be macro expanded everywhere."
134 (if (and (= (length args) 0) 142 (if (and (= (length args) 0)
135 (memq ':show-usage options)) 143 (memq ':show-usage options))
136 (eshell-show-usage name options) 144 (eshell-show-usage name options)
137 (setq args (eshell--process-args name args options)) 145 (setq args (eshell--process-args name args options
146 option-syms))
138 nil)))) 147 nil))))
139 (when usage-msg 148 (when usage-msg
140 (user-error "%s" usage-msg)))))) 149 (user-error "%s" usage-msg))))))
@@ -269,16 +278,13 @@ triggered to say that the switch is unrecognized."
269 "%s: unrecognized option --%s") 278 "%s: unrecognized option --%s")
270 name (car switch))))))) 279 name (car switch)))))))
271 280
272(defun eshell--process-args (name args options) 281(defun eshell--process-args (name args options option-syms)
273 "Process the given ARGS using OPTIONS." 282 "Process the given ARGS for the command NAME using OPTIONS.
274 (let* ((seen ()) 283OPTION-SYMS is a list of symbols that will hold the processed arguments.
275 (opt-vals (delq nil (mapcar (lambda (opt) 284
276 (when (listp opt) 285Return a list of values corresponding to each element in OPTION-SYMS,
277 (let ((sym (nth 3 opt))) 286followed by any additional positional arguments."
278 (when (and sym (not (memq sym seen))) 287 (let* ((opt-vals (mapcar #'list option-syms))
279 (push sym seen)
280 (list sym)))))
281 options)))
282 (ai 0) arg 288 (ai 0) arg
283 (eshell--args args) 289 (eshell--args args)
284 (pos-argument-found nil)) 290 (pos-argument-found nil))