Subject: bug#10085: 24.0.91; completion-pcm--find-all-completions returns wrong remote file names

From: Stefan Monnier <[email-masked]>
Date: Tue, 22 Nov 2011 14:05:42 -0500

> Start "emacs -Q". Apply "C-x C-f /sudo:: TAB". This results in
> "/sudo:sudo:root@". A correct expansion would be "/sudo:root@".

> As far as I can see, Tramp's completion functions work properly.
> The problem seems to be `completion-pcm--find-all-completions'.

I think the patch below fixes it.  The problem is that PCM assumes that
a field separator cannot appear within a field.  In "/sudo::" the first
/ is a field separator, and the last ":" is also a field separator, but
the first ":" isn't.

Sadly the fix could have some detrimental impact on performance, and
I think that the overall problem is linked to an oddity of Tramp's
completion:

If you do "C-x C-f /sudo ?" you get "/sudo:" but if you
do "C-x C-f /sudo: ?" you get "sudo:root@".  So fundamentally, the : of
"/sudo:" acts a field separator, so (completion-boundaries "/sudo:") should
probably return (6 . 0) rather than (1 . 0), and then "C-x C-f /sudo: ?"
should list '("root@" ":") rather than '("sudo:root@").

Related inconsistency from a trace of C-x C-f /sud: TAB:
   1 -> completion-file-name-table: string="/sud:" pred=file-exists-p action=(boundaries . "")
   1 <- completion-file-name-table: (boundaries 5)
   [...]
   ======================================================================
   1 -> completion-file-name-table: string="/sudo:" pred=file-exists-p action=(boundaries . "")
   1 <- completion-file-name-table: (boundaries 1)

        Stefan

=== modified file 'lisp/minibuffer.el'
*** lisp/minibuffer.el	2011-11-19 09:18:31 +0000
--- lisp/minibuffer.el	2011-11-22 18:45:38 +0000
***************
*** 2458,2464 ****
                    (between nil))
                ;; Eliminate submatches that don't end with the separator.
                (dolist (submatch (prog1 suball (setq suball ())))
!                 (when (eq sep (aref submatch (1- (length submatch))))
                    (push submatch suball)))
                (when suball
                  ;; Update the boundaries and corresponding pattern.
--- 2458,2474 ----
                    (between nil))
                ;; Eliminate submatches that don't end with the separator.
                (dolist (submatch (prog1 suball (setq suball ())))
!                 (when (and (eq sep (aref submatch (1- (length submatch))))
! 			   ;; The `sep' check is an optimization, but we need
! 			   ;; to check that submatch really introduces
! 			   ;; a new field.  E.g. When completing "/sudo::",
! 			   ;; prefix="/sudo:" and submatch is "sudo:" which
! 			   ;; matches `sep' but is not sufficient (we'd need
! 			   ;; "sudo::" or "sudo:foo@bar:" to get back to the
! 			   ;; field that we're trying to complete).
! 			   (let ((match (concat subprefix submatch)))
! 			     (eq (length match)
! 				 (completion-boundaries match table pred ""))))
                    (push submatch suball)))
                (when suball
                  ;; Update the boundaries and corresponding pattern.