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'.
This commit is contained in:
Jim Porter 2023-09-02 16:17:27 -07:00
parent b0427f5ffe
commit 781c03933e
3 changed files with 13 additions and 30 deletions

View file

@ -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))))

View file

@ -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.

View file

@ -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))))))