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)`.
This commit is contained in:
Jonathan Teran 2021-07-27 17:43:17 -03:00 committed by Sebastian Ramacher
parent b3d5d820f5
commit 83a334820b
5 changed files with 46 additions and 8 deletions

View File

@ -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

View File

@ -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``

View File

@ -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);
);
}

View File

@ -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);

View File

@ -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
};