Ejemplo n.º 1
0
/**
   Matches the string against the wildcard, and if the wildcard is a
   possible completion of the string, the remainder of the string is
   inserted into the out vector.
*/
static bool wildcard_complete_internal(const wcstring &orig,
                                       const wchar_t *str,
                                       const wchar_t *wc,
                                       bool is_first,
                                       const wchar_t *desc,
                                       wcstring(*desc_func)(const wcstring &),
                                       std::vector<completion_t> *out,
                                       expand_flags_t expand_flags,
                                       complete_flags_t flags)
{
    if (!wc || ! str || orig.empty())
    {
        debug(2, L"Got null string on line %d of file %s", __LINE__, __FILE__);
        return 0;
    }

    bool has_match = false;
    string_fuzzy_match_t fuzzy_match(fuzzy_match_exact);
    const bool at_end_of_wildcard = (*wc == L'\0');
    const wchar_t *completion_string = NULL;

    // Hack hack hack
    // Implement EXPAND_FUZZY_MATCH by short-circuiting everything if there are no remaining wildcards
    if ((expand_flags & EXPAND_FUZZY_MATCH) && ! at_end_of_wildcard && ! wildcard_has(wc, true))
    {
        string_fuzzy_match_t local_fuzzy_match = string_fuzzy_match_string(wc, str);
        if (local_fuzzy_match.type != fuzzy_match_none)
        {
            has_match = true;
            fuzzy_match = local_fuzzy_match;

            /* If we're not a prefix or exact match, then we need to replace the token. Note that in this case we're not going to call ourselves recursively, so these modified flags won't "leak" except into the completion. */
            if (match_type_requires_full_replacement(local_fuzzy_match.type))
            {
                flags |= COMPLETE_REPLACES_TOKEN;
                completion_string = orig.c_str();
            }
            else
            {
                /* Since we are not replacing the token, be careful to only store the part of the string after the wildcard */
                size_t wc_len = wcslen(wc);
                assert(wcslen(str) >= wc_len);
                completion_string = str + wcslen(wc);
            }
        }
    }

    /* Maybe we satisfied the wildcard normally */
    if (! has_match)
    {
        bool file_has_leading_dot = (is_first && str[0] == L'.');
        if (at_end_of_wildcard && ! file_has_leading_dot)
        {
            has_match = true;
            if (flags & COMPLETE_REPLACES_TOKEN)
            {
                completion_string = orig.c_str();
            }
            else
            {
                completion_string = str;
            }
        }
    }

    if (has_match)
    {
        /* Wildcard complete */
        assert(completion_string != NULL);
        wcstring out_completion = completion_string;
        wcstring out_desc = (desc ? desc : L"");

        size_t complete_sep_loc = out_completion.find(PROG_COMPLETE_SEP);
        if (complete_sep_loc != wcstring::npos)
        {
            /* This completion has an embedded description, do not use the generic description */
            out_desc.assign(out_completion, complete_sep_loc + 1, out_completion.size() - complete_sep_loc - 1);
            out_completion.resize(complete_sep_loc);
        }
        else
        {
            if (desc_func && !(expand_flags & EXPAND_NO_DESCRIPTIONS))
            {
                /*
                  A description generating function is specified, call
                  it. If it returns something, use that as the
                  description.
                */
                wcstring func_desc = desc_func(orig);
                if (! func_desc.empty())
                    out_desc = func_desc;
            }

        }

        /* Note: out_completion may be empty if the completion really is empty, e.g. tab-completing 'foo' when a file 'foo' exists. */
        append_completion(out, out_completion, out_desc, flags, fuzzy_match);
        return true;
    }

    if (*wc == ANY_STRING)
    {
        bool res=false;

        /* Ignore hidden file */
        if (is_first && str[0] == L'.')
            return false;

        /* Try all submatches */
        for (size_t i=0; str[i] != L'\0'; i++)
        {
            const size_t before_count = out->size();
            if (wildcard_complete_internal(orig, str + i, wc+1, false, desc, desc_func, out, expand_flags, flags))
            {
                res = true;

                /* #929: if the recursive call gives us a prefix match, just stop. This is sloppy - what we really want to do is say, once we've seen a match of a particular type, ignore all matches of that type further down the string, such that the wildcard produces the "minimal match." */
                bool has_prefix_match = false;
                const size_t after_count = out->size();
                for (size_t j = before_count; j < after_count; j++)
                {
                    if (out->at(j).match.type <= fuzzy_match_prefix)
                    {
                        has_prefix_match = true;
                        break;
                    }
                }
                if (has_prefix_match)
                    break;
            }
        }
        return res;

    }
    else if (*wc == ANY_CHAR || *wc == *str)
    {
        return wildcard_complete_internal(orig, str+1, wc+1, false, desc, desc_func, out, expand_flags, flags);
    }
    else if (towlower(*wc) == towlower(*str))
    {
        return wildcard_complete_internal(orig, str+1, wc+1, false, desc, desc_func, out, expand_flags, flags | COMPLETE_REPLACES_TOKEN);
    }
    return false;
}
Ejemplo n.º 2
0
/**
   Matches the string against the wildcard, and if the wildcard is a
   possible completion of the string, the remainder of the string is
   inserted into the out vector.
*/
static bool wildcard_complete_internal(const wcstring &orig, 
									   const wchar_t *str, 
									   const wchar_t *wc, 
									   bool is_first,
									   const wchar_t *desc,
									   wcstring (*desc_func)(const wcstring &),
									   std::vector<completion_t> &out,
									   int flags )
{
	if( !wc || ! str || orig.empty())
	{
		debug( 2, L"Got null string on line %d of file %s", __LINE__, __FILE__ );
		return 0;		
	}

	if( *wc == 0 &&
		( (str[0] != L'.') || (!is_first)) )
	{
		wcstring out_completion;
        wcstring out_desc = (desc ? desc : L"");

		if( flags & COMPLETE_NO_CASE )
		{
			out_completion = orig;
		}
		else
		{
			out_completion = str;
		}

        size_t complete_sep_loc = out_completion.find(PROG_COMPLETE_SEP);
        if (complete_sep_loc != wcstring::npos)
		{
			/* This completion has an embedded description, do not use the generic description */
            out_desc.assign(out_completion, complete_sep_loc + 1, out_completion.size() - complete_sep_loc - 1);
            out_completion.resize(complete_sep_loc);			
		}
		else
		{
			if( desc_func )
			{
				/*
				  A description generating function is specified, call
				  it. If it returns something, use that as the
				  description.
				*/
				wcstring func_desc = desc_func( orig );
				if (! func_desc.empty())
					out_desc = func_desc;
			}
			
		}
		
        /* Note: out_completion may be empty if the completion really is empty, e.g. tab-completing 'foo' when a file 'foo' exists. */
        append_completion(out, out_completion, out_desc, flags);
		return true;
	}
	
	
	if( *wc == ANY_STRING )
	{		
		bool res=false;
		
		/* Ignore hidden file */
		if( is_first && str[0] == L'.' )
			return false;
		
		/* Try all submatches */
		do
		{
			res = wildcard_complete_internal( orig, str, wc+1, 0, desc, desc_func, out, flags );
			if (res)
				break;
		}
		while (*str++ != 0);
		return res;
		
	}
	else if( *wc == ANY_CHAR )
	{
		return wildcard_complete_internal( orig, str+1, wc+1, 0, desc, desc_func, out, flags );
	}	
	else if( *wc == *str )
	{
		return wildcard_complete_internal( orig, str+1, wc+1, 0, desc, desc_func, out, flags );
	}
	else if( towlower(*wc) == towlower(*str) )
	{
		return wildcard_complete_internal( orig, str+1, wc+1, 0, desc, desc_func, out, flags | COMPLETE_NO_CASE );
	}
	return false;	
}
Ejemplo n.º 3
0
/*
 * Scheme interfaces
 */
static uim_lisp
notify_get_plugins_internal(void)
{
  uim_lisp ret_;
  DIR *dirp;
  struct dirent *dp;
  size_t plen, slen;
  const uim_notify_desc *desc;
  void *handle;
  uim_notify_desc *(*desc_func)(void);
  const char *str;

  plen = sizeof(NOTIFY_PLUGIN_PREFIX);
  slen = sizeof(NOTIFY_PLUGIN_SUFFIX);

  desc = uim_notify_stderr_get_desc();
  ret_ = CONS(LIST3(MAKE_SYM(desc->name),
		    MAKE_STR(desc->name),
		    MAKE_STR(desc->desc)),
	      uim_scm_null());

  if (getenv("UIM_DISABLE_NOTIFY") != NULL)
    return uim_scm_callf("reverse", "o", ret_);

  dirp = opendir(NOTIFY_PLUGIN_PATH);
  if (dirp) {
    while ((dp = readdir(dirp)) != NULL) {
      size_t len = strlen(dp->d_name);
      char path[PATH_MAX];
      if ((len < plen + slen - 1) ||
	  (PATH_MAX < (sizeof(NOTIFY_PLUGIN_PATH "/") + len)) ||
	  (strcmp(dp->d_name, NOTIFY_PLUGIN_PREFIX) <= 0) ||
	  (strcmp(dp->d_name + len + 1 - slen, NOTIFY_PLUGIN_SUFFIX) != 0))
	continue;

      snprintf(path, sizeof(path), "%s/%s", NOTIFY_PLUGIN_PATH, dp->d_name);
      handle = dlopen(path, RTLD_NOW);
      if ((str = dlerror()) != NULL) {
	fprintf(stderr, "load failed %s(%s)\n", path, str);
	continue;
      }
      desc_func = (uim_notify_desc *(*)(void))dlfunc(handle, "uim_notify_plugin_get_desc");
      if (!desc_func) {
	fprintf(stderr, "cannot found 'uim_notify_get_desc()' in %s\n", path);
	dlclose(handle);
	continue;
      }

      desc = desc_func();

      ret_ = CONS(LIST3(MAKE_SYM(desc->name),
			MAKE_STR(desc->name),
			MAKE_STR(desc->desc)),
		  ret_);

      dlclose(handle);
    }
    (void)closedir(dirp);
  }
  return uim_scm_callf("reverse", "o", ret_);
}