Function: test-completion

test-completion is a function defined in minibuf.c.

Signature

(test-completion STRING COLLECTION &optional PREDICATE)

Documentation

Return non-nil if STRING is a valid completion.

For instance, if COLLECTION is a list of strings, STRING is a valid completion if it appears in the list and PREDICATE is satisfied.

Takes the same arguments as all-completions and try-completion.

If COLLECTION is a function, it is called with three arguments: the values STRING, PREDICATE and lambda.

View in manual

Probably introduced at or before Emacs version 22.1.

Aliases

mh-test-completion (obsolete since 29.1)

Source Code

// Defined in /usr/src/emacs/src/minibuf.c
{
  Lisp_Object tem = Qnil, arg = Qnil;

  CHECK_STRING (string);

  if (NILP (collection) || (CONSP (collection) && !FUNCTIONP (collection)))
    {
      tem = Fassoc_string (string, collection, completion_ignore_case ? Qt : Qnil);
      if (NILP (tem))
	return Qnil;
    }
  else if (OBARRAYP (collection) || VECTORP (collection))
    {
      collection = check_obarray (collection);
      /* Bypass intern-soft as that loses for nil.  */
      tem = oblookup (collection,
		      SSDATA (string),
		      SCHARS (string),
		      SBYTES (string));
      if (completion_ignore_case && !BARE_SYMBOL_P (tem))
	DOOBARRAY (XOBARRAY (collection), it)
	  {
	    Lisp_Object obj = obarray_iter_symbol (&it);
	    if (BASE_EQ (Fcompare_strings (string, make_fixnum (0),
					   Qnil,
					   Fsymbol_name (obj),
					   make_fixnum (0) , Qnil, Qt),
			 Qt))
	      {
		tem = obj;
		break;
	      }
	  }

      if (!BARE_SYMBOL_P (tem))
	return Qnil;
    }
  else if (HASH_TABLE_P (collection))
    {
      struct Lisp_Hash_Table *h = XHASH_TABLE (collection);
      ptrdiff_t i = hash_lookup (h, string);
      if (i >= 0)
        {
          tem = HASH_KEY (h, i);
          arg = HASH_VALUE (h, i);
          goto found_matching_key;
        }
      else
	DOHASH (h, k, v)
          {
            tem = k;
            Lisp_Object strkey = (SYMBOLP (tem) ? Fsymbol_name (tem) : tem);
            if (!STRINGP (strkey)) continue;
            if (BASE_EQ (Fcompare_strings (string, Qnil, Qnil,
					   strkey, Qnil, Qnil,
					   completion_ignore_case ? Qt : Qnil),
			 Qt))
	      {
                arg = v;
                goto found_matching_key;
              }
          }
      return Qnil;
    found_matching_key: ;
    }
  else
    return call3 (collection, string, predicate, Qlambda);

  /* Reject this element if it fails to match all the regexps.  */
  if (!match_regexps (string, Vcompletion_regexp_list,
		      completion_ignore_case))
    return Qnil;

  /* Finally, check the predicate.  */
  if (!NILP (predicate))
    {
      return HASH_TABLE_P (collection)
	? call2 (predicate, tem, arg)
	: call1 (predicate, tem);
    }
  else
    return Qt;
}