EX_SCR,
EX_SET,
EX_SHELLCMD,
+ EX_SOURCE,
EX_TABOPEN,
} ExCode;
static VbCmdResult ex_set(const ExArg *arg);
static VbCmdResult ex_shellcmd(const ExArg *arg);
static VbCmdResult ex_shortcut(const ExArg *arg);
+static VbCmdResult ex_source(const ExArg *arg);
static VbCmdResult ex_handlers(const ExArg *arg);
static gboolean complete(short direction);
{"shortcut-add", EX_SCA, ex_shortcut, EX_FLAG_RHS},
{"shortcut-default", EX_SCD, ex_shortcut, EX_FLAG_RHS},
{"shortcut-remove", EX_SCR, ex_shortcut, EX_FLAG_RHS},
+ {"source", EX_SOURCE, ex_source, EX_FLAG_RHS|EX_FLAG_EXP},
{"tabopen", EX_TABOPEN, ex_open, EX_FLAG_CMD},
};
return res;
}
+/**
+ * Run all ex commands in a file.
+ */
+gboolean ex_run_file(const char *filename)
+{
+ char *line, **lines;
+
+ lines = util_get_lines(filename);
+
+ if (!lines) {
+ return false;
+ }
+
+ int length = g_strv_length(lines) - 1;
+ for (int i = 0; i < length; i++) {
+ line = lines[i];
+ if (*line == '#') {
+ continue;
+ }
+ if (ex_run_string(line, false) & VB_CMD_ERROR) {
+ g_warning("Invalid command in %s: '%s'", filename, line);
+ }
+ }
+ g_strfreev(lines);
+
+ return true;
+}
+
/**
* Parses given input string into given ExArg pointer.
*/
return res;
}
+static VbCmdResult ex_source(const ExArg *arg)
+{
+ return ex_run_file(arg->rhs->str) ? VB_CMD_SUCCESS | VB_CMD_KEEPINPUT : VB_CMD_ERROR;
+}
+
static VbCmdResult ex_handlers(const ExArg *arg)
{
char *p;
break;
#endif
+ case EX_SOURCE:
+ sort = true;
+ found = util_filename_fill_completion(store, token);
+ break;
+
default:
break;
}
void ex_input_changed(const char *text);
gboolean ex_fill_completion(GtkListStore *store, const char *input);
VbCmdResult ex_run_string(const char *input, gboolean enable_history);
+gboolean ex_run_file(const char *filename);
#endif /* end of include guard: _EX_H */
static void read_config(void)
{
- char *line, **lines;
-
- /* read config from config files */
- lines = util_get_lines(vb.files[FILES_CONFIG]);
-
- if (lines) {
- int length = g_strv_length(lines) - 1;
- for (int i = 0; i < length; i++) {
- line = lines[i];
- if (*line == '#') {
- continue;
- }
- if (ex_run_string(line, false) & VB_CMD_ERROR ) {
- g_warning("Invalid user config: '%s'", line);
- }
- }
- }
- g_strfreev(lines);
+ ex_run_file(vb.files[FILES_CONFIG]);
}
static void setup_signals()
return found;
}
+
+gboolean util_filename_fill_completion(GtkListStore *store, const char *input)
+{
+ gboolean found = false;
+
+ const char *last_slash = strrchr(input, '/');
+ const char *input_basename = last_slash ? last_slash + 1 : input;
+ char *input_dirname = g_strndup(input, input_basename - input);
+ char *real_dirname = util_expand(
+ *input_dirname ? input_dirname : ".",
+ UTIL_EXP_TILDE|UTIL_EXP_DOLLAR|UTIL_EXP_SPECIAL
+ );
+
+ GError *error = NULL;
+ GDir *dir = g_dir_open(real_dirname, 0, &error);
+ if (error) {
+ /* Can't open directory, likely bad user input */
+ g_error_free(error);
+ } else {
+ const char *filename;
+ GtkTreeIter iter;
+ while ((filename = g_dir_read_name(dir))) {
+ if (g_str_has_prefix(filename, input_basename)) {
+ char *fullpath = g_build_filename(real_dirname, filename, NULL);
+ char *result;
+ if (g_file_test(fullpath, G_FILE_TEST_IS_DIR)) {
+ result = g_strconcat(input_dirname, filename, "/", NULL);
+ } else {
+ result = g_strconcat(input_dirname, filename, NULL);
+ }
+ g_free(fullpath);
+ gtk_list_store_append(store, &iter);
+ gtk_list_store_set(store, &iter, COMPLETION_STORE_FIRST, result, -1);
+ g_free(result);
+ found = true;
+ }
+ }
+ g_dir_close(dir);
+ }
+
+ g_free(input_dirname);
+ g_free(real_dirname);
+
+ return found;
+}
const char *quoteable);
gboolean util_wildmatch(const char *pattern, const char *subject);
gboolean util_fill_completion(GtkListStore *store, const char *input, GList *src);
+gboolean util_filename_fill_completion(GtkListStore *store, const char *input);
#endif /* end of include guard: _UTIL_H */