Subject: bug#22169: 25.0.50; File name compiletion doesn't work with non-ASCII characters on OS X

From: Anders Lindgren <[email-masked]>
Date: Sun, 20 Dec 2015 23:00:40 +0100

Hi!

I managed to get the attached patch to work (when used in conjunction with
my previous patch).

I've tested:

* C-x C-f a TAB
* (find-file-all-competions "a" ".")

    -- Anders

On Sun, Dec 20, 2015 at 8:39 PM, Eli Zaretskii <[email-masked]> wrote:

> > Date: Sun, 20 Dec 2015 20:16:29 +0100
> > From: Anders Lindgren <[email-masked]>
> > Cc: [email-masked], [email-masked]
> >
> > Unfortunately, it still doesn't work, the "a" is still deleted. You can
> see
> > what happens here:
> >
> > (file-name-all-completions "" ".")
> > ("åäö.txt" "aao.txt" "../" "./")
> >
> > (file-name-all-completions "a" ".")
> > ("åäö.txt" "aao.txt") <= Incorrect result
>
> So something's wrong with the patch I wrote, because it was supposed
> to reject "åäö.txt" in the last case.  Can you see why it didn't?
>
> > I gave this a bit of thinking, would the following work:
> >
> > - For each match of the current system (using encoded comparison), after
> the
> > decoding of the entry, perform a second comparison with the decoded
> (original)
> > version of "file" (when not empty).
> >
> > There is no extra decoding included, as the number of entries decoded is
> the
> > same as before (even if some entries will be rejected now). The extra
> > comparison is only performed if "file" is not empty, so it will not
> affect
> > normal directory retrieval, only when performing a completion operation.
> >
> > Concretely, in the example above, completing "a" will find both entries
> which
> > are decoded. However, the second comparison will reject "åäö.txt".
>
> That's exactly what my patch was supposed to do -- it makes a second
> comparison right before adding a candidate to the result.  If you can
> see why it isn't working, we can take it from there.
>
> Thanks.
>
diff --git a/src/dired.c b/src/dired.c
index 84bf247..a2f388c 100644
--- a/src/dired.c
+++ b/src/dired.c
@@ -637,6 +637,20 @@ file_name_completion (Lisp_Object file, Lisp_Object dirname, bool all_flag,
       if (!NILP (predicate) && NILP (call1 (predicate, name)))
 	continue;
 
+      /* Reject entries where the encoded strings matched but the
+         decoded doesn't.  Concretely, "a" should not match "a-ring"
+         on file system encoded using UTF-8 decomposed characters. */
+      Lisp_Object zero = make_number (0);
+      if (SCHARS (file) <= SCHARS (name))
+        {
+          Lisp_Object cmp
+            = Fcompare_strings (name, zero, make_number (SCHARS (file)),
+                                file, zero, make_number (SCHARS (file)),
+                                completion_ignore_case ? Qt : Qnil);
+          if (!EQ (cmp, Qt))
+            continue;
+        }
+
       /* Suitably record this match.  */
 
       matchcount += matchcount <= 1;
@@ -650,7 +664,6 @@ file_name_completion (Lisp_Object file, Lisp_Object dirname, bool all_flag,
 	}
       else
 	{
-	  Lisp_Object zero = make_number (0);
 	  /* FIXME: This is a copy of the code in Ftry_completion.  */
 	  ptrdiff_t compare = min (bestmatchsize, SCHARS (name));
 	  Lisp_Object cmp