From 781c03933eff38ab4db8ad08a54e8a235d33d861 Mon Sep 17 00:00:00 2001 From: Jim Porter Date: Sat, 2 Sep 2023 16:17:27 -0700 Subject: [PATCH] Apply Eshell tilde expansion before glob expansion By treating 'eshell-current-modifiers' as a hook, we can simplify much of the code working with it and ensure that we call modifiers in a more-correct order. * lisp/eshell/em-dirs.el (eshell-expand-user-reference-1) (eshell-expand-user-reference): Simplify. We now only get a single argument. (eshell-parse-user-reference): * lisp/eshell/em-glob.el (eshell-add-glob-modifier): * lisp/eshell/em-pred.el (eshell-parse-arg-modifier): Use 'add-hook'. --- lisp/eshell/em-dirs.el | 15 ++++----------- lisp/eshell/em-glob.el | 4 ++-- lisp/eshell/em-pred.el | 24 +++++++----------------- 3 files changed, 13 insertions(+), 30 deletions(-) diff --git a/lisp/eshell/em-dirs.el b/lisp/eshell/em-dirs.el index 640d3676750..d62f36e56c2 100644 --- a/lisp/eshell/em-dirs.el +++ b/lisp/eshell/em-dirs.el @@ -253,26 +253,19 @@ Thus, this does not include the current directory.") (throw 'eshell-replace-command (eshell-parse-command "cd" (flatten-tree args))))) -(defun eshell-expand-user-reference-1 (file) +(defun eshell-expand-user-reference (file) "Expand a user reference in FILE to its real directory name." (replace-regexp-in-string (rx bos (group "~" (*? anychar)) (or "/" eos)) #'expand-file-name file)) -(defun eshell-expand-user-reference (file) - "Expand a user reference in FILE to its real directory name. -FILE can be either a string or a list of strings to expand." - ;; If the argument was a glob pattern, then FILE is a list, so - ;; expand each element of the glob's resulting list. - (if (listp file) - (mapcar #'eshell-expand-user-reference-1 file) - (eshell-expand-user-reference-1 file))) - (defun eshell-parse-user-reference () "An argument beginning with ~ is a filename to be expanded." (when (and (not eshell-current-argument) (eq (char-after) ?~)) - (add-to-list 'eshell-current-modifiers #'eshell-expand-user-reference) + ;; Apply this modifier fairly early so it happens before things + ;; like glob expansion. + (add-hook 'eshell-current-modifiers #'eshell-expand-user-reference -50) (forward-char) (char-to-string (char-before)))) diff --git a/lisp/eshell/em-glob.el b/lisp/eshell/em-glob.el index 1141b673e97..0d0ff6188b6 100644 --- a/lisp/eshell/em-glob.el +++ b/lisp/eshell/em-glob.el @@ -156,8 +156,8 @@ This mimics the behavior of zsh if non-nil, but bash if nil." (defun eshell-add-glob-modifier () "Add `eshell-extended-glob' to the argument modifier list." (when eshell-glob-splice-results - (add-to-list 'eshell-current-modifiers 'eshell-splice-args t)) - (add-to-list 'eshell-current-modifiers 'eshell-extended-glob)) + (add-hook 'eshell-current-modifiers #'eshell-splice-args 99)) + (add-hook 'eshell-current-modifiers #'eshell-extended-glob)) (defun eshell-parse-glob-chars () "Parse a globbing delimiter. diff --git a/lisp/eshell/em-pred.el b/lisp/eshell/em-pred.el index 1d67f1af990..ae7d0c43bc4 100644 --- a/lisp/eshell/em-pred.el +++ b/lisp/eshell/em-pred.el @@ -302,24 +302,14 @@ This function is specially for adding onto `eshell-parse-argument-hook'." (preds (car modifiers)) (mods (cdr modifiers))) (when (or preds mods) - ;; Has to go at the end, which is only natural since + ;; Has to go near the end (but before + ;; `eshell-splice-args'), which is only natural since ;; syntactically it can only occur at the end. - (setq eshell-current-modifiers - (append - eshell-current-modifiers - (list - (lambda (lst) - (eshell-apply-modifiers - lst preds mods modifier-string))))) - (when (memq 'eshell-splice-args eshell-current-modifiers) - ;; If splicing results, ensure that - ;; `eshell-splice-args' is the last modifier. - ;; Eshell's command parsing can't handle it anywhere - ;; else. - (setq eshell-current-modifiers - (append (delq 'eshell-splice-args - eshell-current-modifiers) - (list 'eshell-splice-args))))))) + (add-hook 'eshell-current-modifiers + (lambda (lst) + (eshell-apply-modifiers + lst preds mods modifier-string)) + 90)))) (goto-char (1+ end)) (eshell-finish-arg)))))) -- 2.39.2