Skip to content

Commit dc62bfe

Browse files
committed
codenav/goto_file: Add support for '~' in paths (= home dir)
1 parent f1b8b9e commit dc62bfe

File tree

1 file changed

+38
-6
lines changed

1 file changed

+38
-6
lines changed

codenav/src/goto_file.c

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,27 @@ goto_file_cleanup(void)
104104
gtk_widget_destroy(menu_item);
105105
}
106106

107+
/**
108+
* @brief Replace any special characters present in path_name
109+
* @param gchar** path_name
110+
* @return void
111+
*
112+
*/
113+
static void
114+
path_name_replace_special(gchar **path_name)
115+
{
116+
GString *str;
117+
118+
if(*path_name[0] == '~')
119+
{
120+
str = g_string_new(*path_name);
121+
utils_string_replace_first(str, "~", g_get_home_dir());
122+
123+
free(*path_name);
124+
*path_name = g_string_free(str, FALSE);
125+
}
126+
}
127+
107128
/**
108129
* @brief Populate the file list with file list of directory
109130
* @param const char* dirname the directory where to find files
@@ -157,7 +178,7 @@ static void
157178
directory_check(GtkEntry* entry, GtkEntryCompletion* completion)
158179
{
159180
GtkTreeModel* completion_list;
160-
gchar *text, *new_dir, *new_dir_path;
181+
gchar *text, *new_dir, *new_dir_exp, *new_dir_path;
161182

162183
/* We need to discern between text written by the user and text filled by the
163184
* autocomplete, and to only use what the user wrote for the completion model.
@@ -188,15 +209,20 @@ directory_check(GtkEntry* entry, GtkEntryCompletion* completion)
188209
g_free(curr_dir);
189210
curr_dir = new_dir;
190211

212+
/* Replace special chars here (e.g. '~') */
213+
new_dir_exp = g_strdup(new_dir);
214+
path_name_replace_special(&new_dir_exp);
215+
191216
/* Assemble full path to the entered directory */
192-
new_dir_path = (g_path_is_absolute(new_dir) ? g_strdup(new_dir)
193-
: g_build_filename(ref_dir, new_dir, NULL));
217+
new_dir_path = (g_path_is_absolute(new_dir_exp) ? g_strdup(new_dir_exp)
218+
: g_build_filename(ref_dir, new_dir_exp, NULL));
194219

195220
/* Build the new file list for completion */
196221
completion_list = build_file_list(new_dir_path, new_dir);
197222
gtk_entry_completion_set_model (completion, completion_list);
198223
g_object_unref(completion_list);
199224

225+
g_free(new_dir_exp);
200226
g_free(new_dir_path);
201227
}
202228

@@ -368,8 +394,8 @@ menu_item_activate(guint key_id)
368394
GtkWidget* dialog_new = NULL;
369395
GtkWidget* dialog_entry;
370396
GeanyDocument* current_doc = document_get_current();
371-
gchar *chosen_path;
372397
const gchar *chosen_file;
398+
gchar *chosen_file_exp, *chosen_path;
373399
gint response;
374400

375401
log_func();
@@ -386,8 +412,12 @@ menu_item_activate(guint key_id)
386412
response = gtk_dialog_run(GTK_DIALOG(dialog));
387413

388414
chosen_file = gtk_entry_get_text(GTK_ENTRY(dialog_entry));
389-
chosen_path = (g_path_is_absolute(chosen_file) ? g_strdup(chosen_file)
390-
: g_build_filename(ref_dir, chosen_file, NULL));
415+
416+
chosen_file_exp = g_strdup(chosen_file);
417+
path_name_replace_special(&chosen_file_exp);
418+
419+
chosen_path = (g_path_is_absolute(chosen_file_exp) ? g_strdup(chosen_file_exp)
420+
: g_build_filename(ref_dir, chosen_file_exp, NULL));
391421

392422
if ( response == GTK_RESPONSE_ACCEPT )
393423
{
@@ -444,6 +474,8 @@ menu_item_activate(guint key_id)
444474
/* Freeing memory */
445475

446476
gtk_widget_destroy(dialog);
477+
478+
g_free(chosen_file_exp);
447479
g_free(chosen_path);
448480

449481
g_free(ref_dir);

0 commit comments

Comments
 (0)