- else
- {
- compare = min (bestmatchsize, len);
- p1 = SDATA (bestmatch);
- p2 = (unsigned char *) dp->d_name;
- matchsize = scmp (p1, p2, compare);
- if (matchsize < 0)
- matchsize = compare;
- if (completion_ignore_case)
- {
- /* If this is an exact match except for case,
- use it as the best match rather than one that is not
- an exact match. This way, we get the case pattern
- of the actual match. */
- /* This tests that the current file is an exact match
- but BESTMATCH is not (it is too long). */
- if ((matchsize == len
- && matchsize + !!directoryp
- < SCHARS (bestmatch))
- ||
- /* If there is no exact match ignoring case,
- prefer a match that does not change the case
- of the input. */
- /* If there is more than one exact match aside from
- case, and one of them is exact including case,
- prefer that one. */
- /* This == checks that, of current file and BESTMATCH,
- either both or neither are exact. */
- (((matchsize == len)
- ==
- (matchsize + !!directoryp
- == SCHARS (bestmatch)))
- && !bcmp (p2, SDATA (encoded_file), SCHARS (encoded_file))
- && bcmp (p1, SDATA (encoded_file), SCHARS (encoded_file))))
- bestmatch = name;
- }
+ bestmatchsize = matchsize;
+
+ /* If the best completion so far is reduced to the string
+ we're trying to complete, then we already know there's no
+ other completion, so there's no point looking any further. */
+ if (matchsize <= SCHARS (file)
+ && !includeall /* A future match may allow includeall to 0. */
+ /* If completion-ignore-case is non-nil, don't
+ short-circuit because we want to find the best
+ possible match *including* case differences. */
+ && (!completion_ignore_case || matchsize == 0)
+ /* The return value depends on whether it's the sole match. */
+ && matchcount > 1)
+ break;