From f1f65664dab973ebe85f0169bf6f5eb210c0d15f Mon Sep 17 00:00:00 2001 From: Sidharth Kapur Date: Thu, 1 Oct 2015 16:55:07 -0500 Subject: [PATCH] Add feature to show ~ instead of $HOME in title --- zathura/config.c | 2 ++ zathura/zathura.c | 40 +++++++++++++++++++++++++++++++++------- zathura/zathura.h | 2 ++ 3 files changed, 37 insertions(+), 7 deletions(-) diff --git a/zathura/config.c b/zathura/config.c index 25275af..22078ca 100644 --- a/zathura/config.c +++ b/zathura/config.c @@ -224,6 +224,8 @@ config_load_default(zathura_t* zathura) bool_value = false; girara_setting_add(gsession, "window-title-basename", &bool_value, BOOLEAN, false, _("Use basename of the file in the window title"), NULL, NULL); bool_value = false; + girara_setting_add(gsession, "window-title-home-tilde", &bool_value, BOOLEAN, false, _("Use ~ instead of $HOME in the filename in the window title"), NULL, NULL); + bool_value = false; girara_setting_add(gsession, "window-title-page", &bool_value, BOOLEAN, false, _("Display the page number in the window title"), NULL, NULL); bool_value = false; girara_setting_add(gsession, "statusbar-basename", &bool_value, BOOLEAN, false, _("Use basename of the file in the statusbar"), NULL, NULL); diff --git a/zathura/zathura.c b/zathura/zathura.c index 391f756..6e92bca 100644 --- a/zathura/zathura.c +++ b/zathura/zathura.c @@ -546,6 +546,38 @@ document_info_open(gpointer data) return FALSE; } +const char* +get_window_title_filename(zathura_t* zathura, const char* file_path) +{ + bool basename_only = false; + girara_setting_get(zathura->ui.session, "window-title-basename", &basename_only); + if (basename_only == false) { + bool home_tilde = false; + girara_setting_get(zathura->ui.session, "window-title-home-tilde", &home_tilde); + if (home_tilde) { + char *home = getenv("HOME"); + int home_len = home ? strlen(home) : 0; + int file_path_len = file_path ? strlen(file_path) : 0; + + if (home_len > 1 && strncmp(home, file_path, home_len) == 0 && (!file_path[home_len] || file_path[home_len] == '/')) { + // Length should be total length of path - length of $HOME + 1 for '~' + 1 for '\0' + int tlen = file_path_len - home_len + 2; + char *tdir = malloc(sizeof(char) * tlen); + strncpy(tdir + 1, file_path + home_len, tlen); + tdir[0] = '~'; + tdir[tlen - 1] = '\0'; + return tdir; + } else { + return file_path; + } + } else { + return file_path; + } + } else { + return zathura_document_get_basename(zathura->document); + } +} + bool document_open(zathura_t* zathura, const char* path, const char* password, int page_number) @@ -838,13 +870,7 @@ document_open(zathura_t* zathura, const char* path, const char* password, } /* update title */ - basename_only = false; - girara_setting_get(zathura->ui.session, "window-title-basename", &basename_only); - if (basename_only == false) { - girara_set_window_title(zathura->ui.session, file_path); - } else { - girara_set_window_title(zathura->ui.session, zathura_document_get_basename(document)); - } + girara_set_window_title(zathura->ui.session, get_window_title_filename(zathura, file_path)); g_free(file_uri); diff --git a/zathura/zathura.h b/zathura/zathura.h index d4b71af..a829415 100644 --- a/zathura/zathura.h +++ b/zathura/zathura.h @@ -278,6 +278,8 @@ void zathura_set_plugin_dir(zathura_t* zathura, const char* dir); */ void zathura_set_argv(zathura_t* zathura, char** argv); +const char* get_window_title_filename(zathura_t* zathura, const char* file_path); + /** * Opens a file *