ref: cf7f260bba5e1df5c884ab2ec28b7f2af856da71
parent: 1f2987cdb40823987ecf4341ab3a34e0d8e0be5c
author: cancel <cancel@cancel.fm>
date: Tue Jan 28 02:34:47 EST 2020
Add tilde expansion for the open file dialog
--- a/sysmisc.c
+++ b/sysmisc.c
@@ -5,6 +5,30 @@
#include <errno.h>
#include <sys/stat.h>
+static char const *const xdg_config_home_env = "XDG_CONFIG_HOME";
+static char const *const home_env = "HOME";
+
+void expand_home_tilde(oso **path) {
+ oso *s = *path;
+ size_t n = osolen(s);
+ if (n < 2)
+ return;
+ if (osoc(s)[0] != '~' || osoc(s)[1] != '/')
+ return;
+ char const *homedir = getenv(home_env);
+ if (!homedir)
+ return;
+ size_t hlen = strlen(homedir);
+ osoensurecap(&s, n + hlen - 1);
+ if (!s)
+ goto done;
+ memmove((char *)s + hlen, (char *)s + 1, n); // includes '\0'
+ memcpy((char *)s, homedir, hlen);
+ osopokelen(s, n + hlen - 1);
+done:
+ *path = s;
+}
+
ORCA_NOINLINE
Cboard_error cboard_copy(Glyph const *gbuffer, Usz field_height,
Usz field_width, Usz rect_y, Usz rect_x, Usz rect_h,
@@ -219,9 +243,6 @@
Conf_dir_ok = 0,
Conf_dir_no_home,
} Conf_dir_error;
-
-static char const *const xdg_config_home_env = "XDG_CONFIG_HOME";
-static char const *const home_env = "HOME";
static Conf_dir_error try_get_conf_dir(oso **out) {
char const *xdgcfgdir = getenv(xdg_config_home_env);
--- a/sysmisc.h
+++ b/sysmisc.h
@@ -1,7 +1,10 @@
#pragma once
#include "base.h"
#include <stdio.h> // FILE cannot be forward declared
+struct oso;
+void expand_home_tilde(struct oso **path);
+
typedef enum {
Cboard_error_none = 0,
Cboard_error_unavailable,
@@ -127,7 +130,7 @@
uint32_t stateflags;
} Ezconf_w;
-void ezconf_w_start(Ezconf_w *ezcw, Ezconf_opt *optsbuffer,
- size_t buffercap, char const *conf_file_name);
+void ezconf_w_start(Ezconf_w *ezcw, Ezconf_opt *optsbuffer, size_t buffercap,
+ char const *conf_file_name);
void ezconf_w_addopt(Ezconf_w *ezcw, char const *key, intptr_t id);
bool ezconf_w_step(Ezconf_w *ezcw);
--- a/tui_main.c
+++ b/tui_main.c
@@ -3141,6 +3141,9 @@
oso *temp_name = get_nonempty_singular_form_text(qf);
if (!temp_name)
break;
+ expand_home_tilde(&temp_name);
+ if (!temp_name)
+ break;
bool added_hist = undo_history_push(&t->ged.undo_hist, &t->ged.field,
t->ged.tick_num);
Field_load_error fle =