Пример #1
0
girara_completion_t*
girara_cc_set(girara_session_t* session, const char* input)
{
  if (input == NULL) {
    return NULL;
  }

  girara_completion_t* completion  = girara_completion_init();
  if (completion == NULL) {
    return NULL;
  }
  girara_completion_group_t* group = girara_completion_group_create(session, NULL);
  if (group == NULL) {
    girara_completion_free(completion);
    return NULL;
  }
  girara_completion_add_group(completion, group);

  unsigned int input_length = strlen(input);

  GIRARA_LIST_FOREACH(session->private_data->settings, girara_setting_t*, iter, setting)
    if ((setting->init_only == false) && (input_length <= strlen(setting->name)) &&
        !strncmp(input, setting->name, input_length)) {
      girara_completion_group_add_element(group, setting->name, setting->description);
    }
  GIRARA_LIST_FOREACH_END(session->private_data->settings, girara_setting_t*, iter, setting);

  return completion;
}
Пример #2
0
static girara_completion_t*
list_files_for_cc(zathura_t* zathura, const char* input, bool check_file_ext)
{
  girara_completion_t* completion  = girara_completion_init();
  girara_completion_group_t* group = girara_completion_group_create(zathura->ui.session, NULL);

  gchar* path         = NULL;
  gchar* current_path = NULL;

  if (completion == NULL || group == NULL) {
    goto error_free;
  }

  path = girara_fix_path(input);
  if (path == NULL) {
    goto error_free;
  }

  /* If the path does not begin with a slash we update the path with the current
   * working directory */
  if (strlen(path) == 0 || path[0] != '/') {
    long path_max;
#ifdef PATH_MAX
    path_max = PATH_MAX;
#else
    path_max = pathconf(path,_PC_PATH_MAX);
    if (path_max <= 0)
      path_max = 4096;
#endif

    char cwd[path_max];
    if (getcwd(cwd, path_max) == NULL) {
      goto error_free;
    }

    char* tmp_path = g_strdup_printf("%s/%s", cwd, path);

    g_free(path);
    path = tmp_path;
  }

  /* Append a slash if the given argument is a directory */
  bool is_dir = (path[strlen(path) - 1] == '/') ? true : false;
  if ((g_file_test(path, G_FILE_TEST_IS_DIR) == TRUE) && !is_dir) {
    char* tmp_path = g_strdup_printf("%s/", path);
    g_free(path);
    path = tmp_path;
    is_dir = true;
  }

  /* get current path */
  char* tmp    = g_strdup(path);
  current_path = is_dir ? g_strdup(tmp) : g_strdup(dirname(tmp));
  g_free(tmp);

  /* get current file */
  gchar* current_file     = is_dir ? "" : basename(path);
  int current_file_length = strlen(current_file);

  /* read directory */
  if (g_file_test(current_path, G_FILE_TEST_IS_DIR) == TRUE) {
    girara_list_t* names = list_files(zathura, current_path, current_file, current_file_length, is_dir, check_file_ext);
    if (!names) {
      goto error_free;
    }

    GIRARA_LIST_FOREACH(names, const char*, iter, file)
      girara_completion_group_add_element(group, file, NULL);
    GIRARA_LIST_FOREACH_END(names, const char*, iter, file);
    girara_list_free(names);
  }