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

From: Daniel Mendler <[email-masked]>
Date: Sun, 1 Jan 2023 19:26:05 +0100

Hi Gregory!

On 1/1/23 18:45, Gregory Heytings wrote:
> 
>>> Feel free to suggest something else.
>>
>> Yes, I did. The command line string should be returned.
>>
> 
> I mean, to propose some other code.
> 
>>
>> I believe you that doing this correctly is non trivial. But this doesn't 
>> justify going with your hack.
>>
> 
> It's extracting the correct part of the command line string that is hacky, 
> too hacky to my taste.

I only gave this a quick try and I don't make use of pcomplete-* helper
functions which may exist. This is just a best effort solution, which
assumes that we can split at spaces. If that's not the case we will get
wrong results, also in the presence of quotation.

But I believe that this is better than nothing and it should give a
sufficiently good result in most cases. We should acknowledge that
completion is not always perfect. It is okay to return a heuristical
result sometimes. If we observe bugs due to invalid return values we
could improve afterwards. I still consider this better than returning an
"arbitrary" string value as in your most recent proposal.

(defun pcomplete-arg (&optional index offset)
  (let* ((idx (+ (pcase index
                   ('first 0)
                   ('last  pcomplete-last)
                   (_      (- pcomplete-index (or index 0))))
                 (or offset 0)))
         (arg (nth idx pcomplete-args)))
    (if (stringp arg)
        arg
      (propertize
       (buffer-substring-no-properties
        (nth idx pcomplete-begins)
        (save-excursion
          (if (< idx pcomplete-last)
              (goto-char (nth (1+ idx) pcomplete-begins))
            (end-of-line))
          (if (re-search-backward "\\S-" nil t)
              (1+ (point))
            (pos-eol))))
       'pcomplete-arg-value arg))))

Daniel