From 73a62e10fdc5f205fdcbc5bb825281ef79a3f68a Mon Sep 17 00:00:00 2001
From: Sebastian Gniazdowski <sgniazdowski@gmail.com>
Date: Wed, 17 Feb 2021 10:38:06 -0600
Subject: Support for glob patterns in open file dialog
---
src/editor/edit-impl.h | 2 +-
src/editor/editcmd.c | 43 +++++++++++++++++++++++++++++++++--------
src/editor/editwidget.c | 2 +-
3 files changed, 37 insertions(+), 10 deletions(-)
diff --git a/src/editor/edit-impl.h b/src/editor/edit-impl.h
index 3ad04dbea..efae19de9 100644
a
|
b
|
WEdit *edit_init (WEdit * edit, int y, int x, int lines, int cols, |
185 | 185 | const vfs_path_t * filename_vpath, long line); |
186 | 186 | gboolean edit_clean (WEdit * edit); |
187 | 187 | gboolean edit_ok_to_exit (WEdit * edit); |
188 | | gboolean edit_load_cmd (WDialog * h); |
| 188 | gboolean edit_load_cmd (WDialog * h, const void *data); |
189 | 189 | gboolean edit_load_file_from_history (WDialog * h); |
190 | 190 | gboolean edit_load_syntax_file (WDialog * h); |
191 | 191 | gboolean edit_load_menu_file (WDialog * h); |
diff --git a/src/editor/editcmd.c b/src/editor/editcmd.c
index 0d2caa923..a55810f30 100644
a
|
b
|
|
44 | 44 | #include <errno.h> |
45 | 45 | #include <sys/stat.h> |
46 | 46 | #include <stdlib.h> |
| 47 | #include <glob.h> |
47 | 48 | |
48 | 49 | #include "lib/global.h" |
49 | 50 | #include "lib/tty/tty.h" |
… |
… |
edit_save_confirm_cmd (WEdit * edit) |
2063 | 2064 | */ |
2064 | 2065 | |
2065 | 2066 | gboolean |
2066 | | edit_load_cmd (WDialog * h) |
| 2067 | edit_load_cmd (WDialog * h, const void *data) |
2067 | 2068 | { |
2068 | 2069 | char *exp; |
2069 | 2070 | gboolean ret = TRUE; /* possible cancel */ |
2070 | 2071 | |
2071 | | exp = input_expand_dialog (_("Load"), _("Enter file name:"), |
2072 | | MC_HISTORY_EDIT_LOAD, INPUT_LAST_TEXT, |
2073 | | INPUT_COMPLETE_FILENAMES | INPUT_COMPLETE_CD); |
| 2072 | if (data != NULL) |
| 2073 | exp = g_strdup ((char *) data); |
| 2074 | else |
| 2075 | exp = input_expand_dialog (_("Load"), _("Enter file name:"), |
| 2076 | MC_HISTORY_EDIT_LOAD, INPUT_LAST_TEXT, |
| 2077 | INPUT_COMPLETE_FILENAMES | INPUT_COMPLETE_CD); |
2074 | 2078 | |
2075 | 2079 | if (exp != NULL && *exp != '\0') |
2076 | 2080 | { |
2077 | | vfs_path_t *exp_vpath; |
| 2081 | if (g_strstr_len (exp, -1, "*") || g_strstr_len (exp, -1, "?") |
| 2082 | || g_strstr_len (exp, -1, "[")) |
| 2083 | { |
| 2084 | int gret; |
| 2085 | gsize idx; |
| 2086 | glob_t globbuf = { 0 }; |
| 2087 | gret = glob (exp, GLOB_MARK | GLOB_BRACE | GLOB_NOMAGIC | GLOB_TILDE | GLOB_TILDE_CHECK, |
| 2088 | NULL, &globbuf); |
| 2089 | if (gret != 0) |
| 2090 | message (D_ERROR, _("Problem opening files"), |
| 2091 | _("Pattern `%s` did not match any files"), exp); |
2078 | 2092 | |
2079 | | exp_vpath = vfs_path_from_str (exp); |
2080 | | ret = edit_load_file_from_filename (h, exp_vpath); |
2081 | | vfs_path_free (exp_vpath); |
| 2093 | /* Are any results returned? */ |
| 2094 | for (idx = 0; idx < globbuf.gl_pathc; idx++) |
| 2095 | if (globbuf.gl_pathv != NULL && globbuf.gl_pathv[idx] != NULL) |
| 2096 | edit_load_cmd (h, globbuf.gl_pathv[idx]); |
| 2097 | |
| 2098 | if (globbuf.gl_pathc > 0) |
| 2099 | globfree (&globbuf); |
| 2100 | } |
| 2101 | else |
| 2102 | { |
| 2103 | vfs_path_t *exp_vpath; |
| 2104 | |
| 2105 | exp_vpath = vfs_path_from_str (exp); |
| 2106 | ret = edit_load_file_from_filename (h, exp_vpath); |
| 2107 | vfs_path_free (exp_vpath); |
| 2108 | } |
2082 | 2109 | } |
2083 | 2110 | |
2084 | 2111 | g_free (exp); |
diff --git a/src/editor/editwidget.c b/src/editor/editwidget.c
index 18ac00e66..10ceaff83 100644
a
|
b
|
edit_dialog_command_execute (WDialog * h, long command) |
391 | 391 | edit_add_window (h, wh->y + 1, wh->x, wh->lines - 2, wh->cols, NULL, 0); |
392 | 392 | break; |
393 | 393 | case CK_EditFile: |
394 | | edit_load_cmd (h); |
| 394 | edit_load_cmd (h, NULL); |
395 | 395 | break; |
396 | 396 | case CK_History: |
397 | 397 | edit_load_file_from_history (h); |