From bf2e0e293cff3fcc64441533c2650b280c954212 Mon Sep 17 00:00:00 2001 From: Jim Porter Date: Sun, 20 Oct 2024 15:30:19 -0700 Subject: [PATCH] Add QUOTED argument to 'eshell-stringify' This will make it easier to reconstitute numbers that we converted to strings. * lisp/eshell/esh-util.el (eshell--numeric-string-p): New function. (eshell-stringify, eshell-stringify-list): Add QUOTED argument. (eshell-convert, eshell-list-to-string): Stringify as quoted. * lisp/eshell/esh-arg.el (eshell--numberlike-p): Remove. (eshell-concat-1): Pass along QUOTED. * lisp/eshell/esh-cmd.el (eshell-lisp-command): Use 'eshell--numeric-string-p'. * lisp/eshell/esh-var.el (eshell-parse-variable): * lisp/eshell/em-cmpl.el (eshell-complete-parse-arguments): Stringify as quoted. (cherry picked from commit 4d69d3778a51427654965cbcc5bb03611034b656) --- lisp/eshell/em-cmpl.el | 2 +- lisp/eshell/esh-arg.el | 11 ++++------- lisp/eshell/esh-cmd.el | 4 +--- lisp/eshell/esh-util.el | 20 ++++++++++++++------ lisp/eshell/esh-var.el | 2 +- 5 files changed, 21 insertions(+), 18 deletions(-) diff --git a/lisp/eshell/em-cmpl.el b/lisp/eshell/em-cmpl.el index 4c79f7b187a..ef931db62b2 100644 --- a/lisp/eshell/em-cmpl.el +++ b/lisp/eshell/em-cmpl.el @@ -444,7 +444,7 @@ to writing a completion function." ('nil (propertize "" 'pcomplete-arg-value arg)) (_ - (propertize (eshell-stringify arg) + (propertize (eshell-stringify arg t) 'pcomplete-arg-value arg)))) args) posns))) diff --git a/lisp/eshell/esh-arg.el b/lisp/eshell/esh-arg.el index 4ea25f7f202..4f8119670d2 100644 --- a/lisp/eshell/esh-arg.el +++ b/lisp/eshell/esh-arg.el @@ -239,19 +239,16 @@ would produce (\"abc\" \"d\")." (t (setq result (eshell-concat-1 quoted result i)))))))) -(defsubst eshell--numberlike-p (object) - (or (numberp object) - (and (stringp object) (get-text-property 0 'number object)))) - (defun eshell-concat-1 (quoted first second) "Concatenate FIRST and SECOND. If QUOTED is nil and either FIRST or SECOND are numberlike, try to mark the result as a number as well." - (let ((result (concat (eshell-stringify first) (eshell-stringify second)))) + (let ((result (concat (eshell-stringify first quoted) + (eshell-stringify second quoted)))) (remove-text-properties 0 (length result) '(number) result) (when (and (not quoted) - (or (eshell--numberlike-p first) - (eshell--numberlike-p second))) + (or (numberp first) (eshell--numeric-string-p first) + (numberp second) (eshell--numeric-string-p second))) (eshell-mark-numeric-string result)) result)) diff --git a/lisp/eshell/esh-cmd.el b/lisp/eshell/esh-cmd.el index 88d9c0affef..441a39ac86a 100644 --- a/lisp/eshell/esh-cmd.el +++ b/lisp/eshell/esh-cmd.el @@ -1577,9 +1577,7 @@ a string naming a Lisp function." (while args (let ((arg (car args))) (cond - ((and numeric (stringp arg) (> (length arg) 0) - (text-property-any 0 (length arg) - 'number t arg)) + ((and numeric (eshell--numeric-string-p arg)) ;; If any of the arguments are flagged as ;; numbers waiting for conversion, convert ;; them now. diff --git a/lisp/eshell/esh-util.el b/lisp/eshell/esh-util.el index 65e19228e0e..de3f86ccae4 100644 --- a/lisp/eshell/esh-util.el +++ b/lisp/eshell/esh-util.el @@ -353,6 +353,12 @@ See `eshell-convertible-to-number-p'." (eshell--do-mark-numeric-string string)) string) +(defsubst eshell--numeric-string-p (string) + "Return non-nil if STRING has been marked as numeric." + (and (stringp string) + (length> string 0) + (not (text-property-not-all 0 (length string) 'number t string)))) + (defun eshell-convert-to-number (string) "Try to convert STRING to a number. If STRING doesn't look like a number (or @@ -377,7 +383,7 @@ trailing newlines removed. Otherwise, this behaves as follows: (cond ((not (stringp string)) (if to-string - (eshell-stringify string) + (eshell-stringify string t) string)) (to-string (string-trim-right string "\n+")) (t (let ((len (length string))) @@ -499,25 +505,27 @@ Prepend remote identification of `default-directory', if any." (define-obsolete-function-alias 'eshell-flatten-list #'flatten-tree "27.1") -(defun eshell-stringify (object) +(defun eshell-stringify (object &optional quoted) "Convert OBJECT into a string value." (cond ((stringp object) object) ((numberp object) - (number-to-string object)) + (if quoted + (number-to-string object) + (propertize (number-to-string object) 'number t))) ((and (eq object t) (not eshell-stringify-t)) nil) (t (string-trim-right (pp-to-string object))))) -(defsubst eshell-stringify-list (args) +(defsubst eshell-stringify-list (args &optional quoted) "Convert each element of ARGS into a string value." - (mapcar #'eshell-stringify args)) + (mapcar (lambda (i) (eshell-stringify i quoted)) args)) (defsubst eshell-list-to-string (list) "Convert LIST into a single string separated by spaces." - (mapconcat #'eshell-stringify list " ")) + (mapconcat (lambda (i) (eshell-stringify i t)) list " ")) (defsubst eshell-flatten-and-stringify (&rest args) "Flatten and stringify all of the ARGS into a single string." diff --git a/lisp/eshell/esh-var.el b/lisp/eshell/esh-var.el index e3ff76abc26..bf474c5e279 100644 --- a/lisp/eshell/esh-var.el +++ b/lisp/eshell/esh-var.el @@ -495,7 +495,7 @@ process any indices that come after the variable reference." (if splice (setq value `(eshell-list-to-string ,value) splice nil) - (setq value `(eshell-stringify ,value)))) + (setq value `(eshell-stringify ,value t)))) (setq value `(eshell-escape-arg ,value)) (when splice (setq value `(eshell-splice-args ,value))) -- 2.39.5