Subject: bug#60464: 29.0.60; Regression - pcomplete-arg fails with argument 'last

From: Gregory Heytings <[email-masked]>
Date: Sun, 01 Jan 2023 18:19:20 +0000

>> After working a bit more on this bug, I concluded that what Stefan 
>> initially suggested, to use the string representation of the value, is 
>> safer than trying to extract the string corresponding to the argument 
>> that the user typed in from the command line.
>
> Could you expand on when/where it's "unsafe" or what it breaks?
>

I simply spent too much time trying to get the "extract the correct part 
of the command line from the buffer" right, and wasn't able to convince 
myself that the result was correct in all circumstances.  Given that 
returning (format "%S" arg) is what you initially suggested, and that it 
cannot be wrong, I concluded that it was the best/safest thing to do.

The semantics of the "index" argument of the pcomplete-arg function are 
tricky: it can be 0, "the current argument being examined", < 0, "closer 
to the last argument", and > 0, "closer to the first argument".  And then 
you also have the special values 'first and 'last.  And it can also be 
nil, which is equivalent to 0.

There is a pcomplete-actual-arg function, which returns "the actual text 
representation of the last argument" (in fact, "the actual text 
representation of the INDEXth argument and the following ones"), but no 
function which returns the actual text representation of a given argument.

Perhaps we could just use it and assume that all arguments are separated 
by spaces, though, in which case the patch would become:

diff --git a/lisp/pcomplete.el b/lisp/pcomplete.el
index 5bee515246..c829b6c3b7 100644
--- a/lisp/pcomplete.el
+++ b/lisp/pcomplete.el
@@ -648,10 +648,11 @@ pcomplete-arg
  accessing absolute argument positions.

  When the argument has been transformed into something that is not
-a string by `pcomplete-parse-arguments-function', the text
-representation of the argument, namely what the user actually
-typed in, is returned, and the value of the argument is stored in
-the pcomplete-arg-value text property of that string."
+a string by `pcomplete-parse-arguments-function' and INDEX is not
+`last', the text representation of the argument, namely what the
+user actually typed in, is returned, and the value of the
+argument is stored in the pcomplete-arg-value text property of
+that string."
    (let ((arg
           (nth (+ (pcase index
                    ('first 0)
@@ -659,11 +660,11 @@ pcomplete-arg
                    (_      (- pcomplete-index (or index 0))))
                  (or offset 0))
                pcomplete-args)))
-    (if (stringp arg)
+    (if (or (stringp arg)
+            (eq index 'last))
          arg
        (propertize
-       (buffer-substring (pcomplete-begin index offset)
-                         (pcomplete-begin (1- (or index 0)) offset))
+       (car (split-string (pcomplete-actual-arg index offset)))
         'pcomplete-arg-value arg))))

  (defun pcomplete-begin (&optional index offset)