Commit 2192b717 authored by Benoît Knecht's avatar Benoît Knecht Committed by Sebastian Ramacher

Add new shortcut function to display links

This is useful when the text of the link doesn't match its target. The
default key is set to 'F'.

See issue 266 <http://bugs.pwmt.org/issue266>.
Reported-by: default avatarIron <o380770@rtrtr.com>
Signed-off-by: Sebastian Ramacher's avatarSebastian Ramacher <sebastian+dev@ramacher.at>
parent 526e76b6
......@@ -186,8 +186,15 @@ cb_index_row_activated(GtkTreeView* tree_view, GtkTreePath* path,
g_object_unref(model);
}
bool
cb_sc_follow(GtkEntry* entry, girara_session_t* session)
typedef enum zathura_link_action_e
{
ZATHURA_LINK_ACTION_FOLLOW,
ZATHURA_LINK_ACTION_DISPLAY
} zathura_link_action_t;
static bool
handle_link(GtkEntry* entry, girara_session_t* session,
zathura_link_action_t action)
{
g_return_val_if_fail(session != NULL, FALSE);
g_return_val_if_fail(session->global.data != NULL, FALSE);
......@@ -226,10 +233,17 @@ cb_sc_follow(GtkEntry* entry, girara_session_t* session)
zathura_link_t* link = zathura_page_widget_link_get(ZATHURA_PAGE(page_widget), index);
if (link != NULL) {
zathura_jumplist_save(zathura);
zathura_link_evaluate(zathura, link);
invalid_index = false;
zathura_jumplist_add(zathura);
switch (action) {
case ZATHURA_LINK_ACTION_FOLLOW:
zathura_jumplist_save(zathura);
zathura_link_evaluate(zathura, link);
zathura_jumplist_add(zathura);
break;
case ZATHURA_LINK_ACTION_DISPLAY:
zathura_link_display(zathura, link);
break;
}
}
}
}
......@@ -243,6 +257,18 @@ cb_sc_follow(GtkEntry* entry, girara_session_t* session)
return (eval == TRUE) ? TRUE : FALSE;
}
bool
cb_sc_follow(GtkEntry* entry, girara_session_t* session)
{
return handle_link(entry, session, ZATHURA_LINK_ACTION_FOLLOW);
}
bool
cb_sc_display_link(GtkEntry* entry, girara_session_t* session)
{
return handle_link(entry, session, ZATHURA_LINK_ACTION_DISPLAY);
}
void
cb_file_monitor(GFileMonitor* monitor, GFile* file, GFile* UNUSED(other_file), GFileMonitorEvent event, girara_session_t* session)
{
......
......@@ -80,6 +80,15 @@ void cb_index_row_activated(GtkTreeView* tree_view, GtkTreePath* path,
*/
bool cb_sc_follow(GtkEntry* entry, girara_session_t* session);
/**
* Called when input has been passed to the sc_display_link dialog
*
* @param entry The dialog inputbar
* @param session The girara session
* @return true if no error occured and the event has been handled
*/
bool cb_sc_display_link(GtkEntry* entry, girara_session_t* session);
/**
* Emitted when file has been changed
*
......
......@@ -209,6 +209,8 @@ config_load_default(zathura_t* zathura)
girara_shortcut_add(gsession, 0, GDK_KEY_a, NULL, sc_adjust_window, NORMAL, ZATHURA_ADJUST_BESTFIT, NULL);
girara_shortcut_add(gsession, 0, GDK_KEY_s, NULL, sc_adjust_window, NORMAL, ZATHURA_ADJUST_WIDTH, NULL);
girara_shortcut_add(gsession, 0, GDK_KEY_F, NULL, sc_display_link, NORMAL, 0, NULL);
girara_shortcut_add(gsession, 0, GDK_KEY_slash, NULL, sc_focus_inputbar, NORMAL, 0, &("/"));
girara_shortcut_add(gsession, GDK_SHIFT_MASK, GDK_KEY_slash, NULL, sc_focus_inputbar, NORMAL, 0, &("/"));
girara_shortcut_add(gsession, 0, GDK_KEY_question, NULL, sc_focus_inputbar, NORMAL, 0, &("?"));
......@@ -381,6 +383,7 @@ config_load_default(zathura_t* zathura)
girara_shortcut_mapping_add(gsession, "abort", sc_abort);
girara_shortcut_mapping_add(gsession, "adjust_window", sc_adjust_window);
girara_shortcut_mapping_add(gsession, "change_mode", sc_change_mode);
girara_shortcut_mapping_add(gsession, "display_link", sc_display_link);
girara_shortcut_mapping_add(gsession, "jumplist", sc_jumplist);
girara_shortcut_mapping_add(gsession, "follow", sc_follow);
girara_shortcut_mapping_add(gsession, "goto", sc_goto);
......
......@@ -169,6 +169,28 @@ zathura_link_evaluate(zathura_t* zathura, zathura_link_t* link)
}
}
void
zathura_link_display(zathura_t* zathura, zathura_link_t* link)
{
zathura_link_type_t type = zathura_link_get_type(link);
zathura_link_target_t target = zathura_link_get_target(link);
switch (type) {
case ZATHURA_LINK_GOTO_DEST:
girara_notify(zathura->ui.session, GIRARA_INFO, _("Link: page %d"),
target.page_number);
break;
case ZATHURA_LINK_GOTO_REMOTE:
case ZATHURA_LINK_URI:
case ZATHURA_LINK_LAUNCH:
case ZATHURA_LINK_NAMED:
girara_notify(zathura->ui.session, GIRARA_INFO, _("Link: %s"),
target.value);
break;
default:
girara_notify(zathura->ui.session, GIRARA_ERROR, _("Link: Invalid"));
}
}
static void
link_remote(zathura_t* zathura, const char* file)
{
......
......@@ -56,4 +56,12 @@ zathura_link_target_t zathura_link_get_target(zathura_link_t* link);
*/
void zathura_link_evaluate(zathura_t* zathura, zathura_link_t* link);
/**
* Display a link using girara_notify
*
* @param zathura Zathura instance
* @param link The link
*/
void zathura_link_display(zathura_t* zathura, zathura_link_t* link);
#endif // LINK_H
......@@ -18,6 +18,39 @@
#include "print.h"
#include "page-widget.h"
/* Helper function; see sc_display_link and sc_follow. */
static bool
draw_links(zathura_t* zathura)
{
/* set pages to draw links */
bool show_links = false;
unsigned int page_offset = 0;
unsigned int number_of_pages = zathura_document_get_number_of_pages(zathura->document);
for (unsigned int page_id = 0; page_id < number_of_pages; page_id++) {
zathura_page_t* page = zathura_document_get_page(zathura->document, page_id);
if (page == NULL) {
continue;
}
GtkWidget* page_widget = zathura_page_get_widget(zathura, page);
g_object_set(page_widget, "search-results", NULL, NULL);
if (zathura_page_get_visibility(page) == true) {
g_object_set(page_widget, "draw-links", TRUE, NULL);
int number_of_links = 0;
g_object_get(page_widget, "number-of-links", &number_of_links, NULL);
if (number_of_links != 0) {
show_links = true;
}
g_object_set(page_widget, "offset-links", page_offset, NULL);
page_offset += number_of_links;
} else {
g_object_set(page_widget, "draw-links", FALSE, NULL);
}
}
return show_links;
}
bool
sc_abort(girara_session_t* session, girara_argument_t* UNUSED(argument),
girara_event_t* UNUSED(event), unsigned int UNUSED(t))
......@@ -182,6 +215,30 @@ sc_change_mode(girara_session_t* session, girara_argument_t* argument,
return false;
}
bool
sc_display_link(girara_session_t* session, girara_argument_t* UNUSED(argument),
girara_event_t* UNUSED(event), unsigned int UNUSED(t))
{
g_return_val_if_fail(session != NULL, false);
g_return_val_if_fail(session->global.data != NULL, false);
zathura_t* zathura = session->global.data;
if (zathura->document == NULL || zathura->ui.session == NULL) {
return false;
}
bool show_links = draw_links(zathura);
/* ask for input */
if (show_links) {
girara_dialog(zathura->ui.session, "Display link:", FALSE, NULL,
(girara_callback_inputbar_activate_t) cb_sc_display_link,
zathura->ui.session);
}
return false;
}
bool
sc_focus_inputbar(girara_session_t* session, girara_argument_t* argument, girara_event_t* UNUSED(event), unsigned int UNUSED(t))
{
......@@ -248,32 +305,7 @@ sc_follow(girara_session_t* session, girara_argument_t* UNUSED(argument),
return false;
}
/* set pages to draw links */
bool show_links = false;
unsigned int page_offset = 0;
unsigned int number_of_pages = zathura_document_get_number_of_pages(zathura->document);
for (unsigned int page_id = 0; page_id < number_of_pages; page_id++) {
zathura_page_t* page = zathura_document_get_page(zathura->document, page_id);
if (page == NULL) {
continue;
}
GtkWidget* page_widget = zathura_page_get_widget(zathura, page);
g_object_set(page_widget, "search-results", NULL, NULL);
if (zathura_page_get_visibility(page) == true) {
g_object_set(page_widget, "draw-links", TRUE, NULL);
int number_of_links = 0;
g_object_get(page_widget, "number-of-links", &number_of_links, NULL);
if (number_of_links != 0) {
show_links = true;
}
g_object_set(page_widget, "offset-links", page_offset, NULL);
page_offset += number_of_links;
} else {
g_object_set(page_widget, "draw-links", FALSE, NULL);
}
}
bool show_links = draw_links(zathura);
/* ask for input */
if (show_links == true) {
......
......@@ -38,6 +38,17 @@ bool sc_adjust_window(girara_session_t* session, girara_argument_t* argument, gi
*/
bool sc_change_mode(girara_session_t* session, girara_argument_t* argument, girara_event_t* event, unsigned int t);
/**
* Display a link
*
* @param session The used girara session
* @param argument The used argument
* @param event Girara event
* @param t Number of executions
* @return true if no error occured otherwise false
*/
bool sc_display_link(girara_session_t* session, girara_argument_t* argument, girara_event_t* event, unsigned int t);
/**
* Shortcut function to focus the inputbar
*
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment