From 83a334820be5fc57fbe3a9f9167deb665d7ece89 Mon Sep 17 00:00:00 2001 From: Jonathan Teran Date: Tue, 27 Jul 2021 17:43:17 -0300 Subject: [PATCH] Add '$f' and '$p' expansions to 'exec' shortcut function There are other expansions available, but I don't really think they make sense, or why there are two of them for the same thing (`$FILE` and `%` expand to the file name). I added `$f` and `$p`, which expand to the document's path and current page respectively, while keeping the other 2 just in case it breaks someone's setup. I also documented them in both `zathura(1)` and `zathurarc(5)`. --- doc/man/zathura.1.rst | 3 ++- doc/man/zathurarc.5.rst | 3 ++- zathura/commands.c | 26 +++++++++++++++++++++----- zathura/shortcuts.c | 17 +++++++++++++++++ zathura/zathura.h | 5 ++++- 5 files changed, 46 insertions(+), 8 deletions(-) diff --git a/doc/man/zathura.1.rst b/doc/man/zathura.1.rst index a5766f4..b63b429 100644 --- a/doc/man/zathura.1.rst +++ b/doc/man/zathura.1.rst @@ -225,7 +225,8 @@ close Close document exec - Execute an external command + Execute an external command. ``$f`` expands to the current document path, and ``$p`` to the + current page number info Show document information diff --git a/doc/man/zathurarc.5.rst b/doc/man/zathurarc.5.rst index e1b7cab..9d6692a 100644 --- a/doc/man/zathurarc.5.rst +++ b/doc/man/zathurarc.5.rst @@ -231,7 +231,8 @@ They can also be combined with modifiers: * ``exec``: - Execute an external command. + Execute an external command. ``$f`` expands to the current document path, + and ``$p`` to the current page number. * ``focus_inputbar`` diff --git a/zathura/commands.c b/zathura/commands.c index d14241b..7684a60 100644 --- a/zathura/commands.c +++ b/zathura/commands.c @@ -519,6 +519,7 @@ error_ret: return true; } + bool cmd_exec(girara_session_t* session, girara_list_t* argument_list) { @@ -533,18 +534,33 @@ cmd_exec(girara_session_t* session, girara_list_t* argument_list) if (zathura->document != NULL) { const char* path = zathura_document_get_path(zathura->document); + unsigned int page = zathura_document_get_current_page_number(zathura->document); + char page_buf[ZATHURA_PAGE_NUMBER_MAX_DIGITS]; + snprintf(page_buf, ZATHURA_PAGE_NUMBER_MAX_DIGITS, "%d", page); GIRARA_LIST_FOREACH_BODY_WITH_ITER(argument_list, char*, iter, value, char* r = girara_replace_substring(value, "$FILE", path); + char* s = NULL; if (r != NULL) { - char* s = girara_replace_substring(r, "%", path); + s = girara_replace_substring(r, "%", path); g_free(r); - - if (s != NULL) { - girara_list_iterator_set(iter, s); - } + r = NULL; } + + if (s != NULL) { + r = girara_replace_substring(s, "$f", path); + g_free(s); + s = NULL; + } + + if (r != NULL) { + s = girara_replace_substring(r, "$p", page_buf); + g_free(r); + r = NULL; + } + + girara_list_iterator_set(iter, s ? s : r); ); } diff --git a/zathura/shortcuts.c b/zathura/shortcuts.c index 61fa695..77731cc 100644 --- a/zathura/shortcuts.c +++ b/zathura/shortcuts.c @@ -1469,6 +1469,9 @@ sc_exec(girara_session_t* session, girara_argument_t* argument, girara_event_t* if (zathura->document != NULL) { const char* path = zathura_document_get_path(zathura->document); + unsigned int page = zathura_document_get_current_page_number(zathura->document); + char page_buf[ZATHURA_PAGE_NUMBER_MAX_DIGITS]; + snprintf(page_buf, ZATHURA_PAGE_NUMBER_MAX_DIGITS, "%d", page + 1); girara_argument_t new_argument = *argument; @@ -1484,6 +1487,20 @@ sc_exec(girara_session_t* session, girara_argument_t* argument, girara_event_t* return false; } + r = girara_replace_substring(s, "$f", path); + g_free(s); + + if (r == NULL) { + return false; + } + + s = girara_replace_substring(r, "$p", page_buf); + g_free(r); + + if (s == NULL) { + return false; + } + new_argument.data = s; const bool ret = girara_sc_exec(session, &new_argument, event, t); g_free(s); diff --git a/zathura/zathura.h b/zathura/zathura.h index ea0f038..3891ce7 100644 --- a/zathura/zathura.h +++ b/zathura/zathura.h @@ -70,8 +70,11 @@ enum { ZOOM_SMOOTH }; -/* unspecified page number */ +/* on page numbers */ enum { + /* We should never open a pdf with more that 99.999 pages... right? */ + ZATHURA_PAGE_NUMBER_MAX_DIGITS = 5, + /* unspecified page numbers */ ZATHURA_PAGE_NUMBER_UNSPECIFIED = INT_MIN };