Browse Source

Add tilde expansion for the open file dialog

master
cancel 5 years ago
parent
commit
cf7f260bba
  1. 27
      sysmisc.c
  2. 7
      sysmisc.h
  3. 3
      tui_main.c

27
sysmisc.c

@ -5,6 +5,30 @@
#include <errno.h> #include <errno.h>
#include <sys/stat.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 ORCA_NOINLINE
Cboard_error cboard_copy(Glyph const *gbuffer, Usz field_height, Cboard_error cboard_copy(Glyph const *gbuffer, Usz field_height,
Usz field_width, Usz rect_y, Usz rect_x, Usz rect_h, Usz field_width, Usz rect_y, Usz rect_x, Usz rect_h,
@ -220,9 +244,6 @@ typedef enum {
Conf_dir_no_home, Conf_dir_no_home,
} Conf_dir_error; } 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) { static Conf_dir_error try_get_conf_dir(oso **out) {
char const *xdgcfgdir = getenv(xdg_config_home_env); char const *xdgcfgdir = getenv(xdg_config_home_env);
if (xdgcfgdir) { if (xdgcfgdir) {

7
sysmisc.h

@ -1,6 +1,9 @@
#pragma once #pragma once
#include "base.h" #include "base.h"
#include <stdio.h> // FILE cannot be forward declared #include <stdio.h> // FILE cannot be forward declared
struct oso;
void expand_home_tilde(struct oso **path);
typedef enum { typedef enum {
Cboard_error_none = 0, Cboard_error_none = 0,
@ -127,7 +130,7 @@ typedef struct {
uint32_t stateflags; uint32_t stateflags;
} Ezconf_w; } Ezconf_w;
void ezconf_w_start(Ezconf_w *ezcw, Ezconf_opt *optsbuffer, void ezconf_w_start(Ezconf_w *ezcw, Ezconf_opt *optsbuffer, size_t buffercap,
size_t buffercap, char const *conf_file_name); char const *conf_file_name);
void ezconf_w_addopt(Ezconf_w *ezcw, char const *key, intptr_t id); void ezconf_w_addopt(Ezconf_w *ezcw, char const *key, intptr_t id);
bool ezconf_w_step(Ezconf_w *ezcw); bool ezconf_w_step(Ezconf_w *ezcw);

3
tui_main.c

@ -3139,6 +3139,9 @@ staticni Tui_menus_result tui_drive_menus(Tui *t, int key) {
switch (qform_id(qf)) { switch (qform_id(qf)) {
case Open_form_id: { case Open_form_id: {
oso *temp_name = get_nonempty_singular_form_text(qf); oso *temp_name = get_nonempty_singular_form_text(qf);
if (!temp_name)
break;
expand_home_tilde(&temp_name);
if (!temp_name) if (!temp_name)
break; break;
bool added_hist = undo_history_push(&t->ged.undo_hist, &t->ged.field, bool added_hist = undo_history_push(&t->ged.undo_hist, &t->ged.field,

Loading…
Cancel
Save