Implement hlsearch/nohlsearch

This commit is contained in:
Moritz Lipp 2012-04-22 19:12:45 +02:00
parent a86940b3f4
commit 036dc0cd57
6 changed files with 125 additions and 20 deletions

View file

@ -16,8 +16,10 @@
#include "page-widget.h"
#include "page.h"
#include "internal.h"
#include "render.h"
#include <girara/session.h>
#include <girara/settings.h>
#include <girara/datastructures.h>
#include <girara/utils.h>
@ -191,6 +193,20 @@ cmd_help(girara_session_t* UNUSED(session), girara_list_t*
return true;
}
bool
cmd_hlsearch(girara_session_t* session, girara_list_t* UNUSED(argument_list))
{
g_return_val_if_fail(session != NULL, false);
g_return_val_if_fail(session->global.data != NULL, false);
zathura_t* zathura = session->global.data;
document_draw_search_results(zathura, true);
render_all(zathura);
return true;
}
bool
cmd_open(girara_session_t* session, girara_list_t* argument_list)
{
@ -241,6 +257,19 @@ cmd_print(girara_session_t* session, girara_list_t* UNUSED(argument_list))
return true;
}
bool
cmd_nohlsearch(girara_session_t* session, girara_list_t* UNUSED(argument_list))
{
g_return_val_if_fail(session != NULL, false);
g_return_val_if_fail(session->global.data != NULL, false);
zathura_t* zathura = session->global.data;
document_draw_search_results(zathura, false);
render_all(zathura);
return true;
}
bool
cmd_save(girara_session_t* session, girara_list_t* argument_list)
{
@ -313,6 +342,15 @@ cmd_search(girara_session_t* session, const char* input, girara_argument_t* argu
unsigned int number_of_pages = zathura_document_get_number_of_pages(zathura->document);
unsigned int current_page_number = zathura_document_get_current_page_number(zathura->document);
/* reset search highlighting */
bool nohlsearch = false;
girara_setting_get(session, "nohlsearch", &nohlsearch);
if (nohlsearch == false) {
document_draw_search_results(zathura, true);
}
/* search pages */
for (unsigned int page_id = 0; page_id < number_of_pages; ++page_id) {
unsigned int index = (page_id + current_page_number) % number_of_pages;
zathura_page_t* page = zathura_document_get_page(zathura->document, index);

View file

@ -60,6 +60,15 @@ bool cmd_info(girara_session_t* session, girara_list_t* argument_list);
*/
bool cmd_help(girara_session_t* session, girara_list_t* argument_list);
/**
* Shows current search results
*
* @param session The used girara session
* @param argument_list List of passed arguments
* @return true if no error occured
*/
bool cmd_hlsearch(girara_session_t* session, girara_list_t* argument_list);
/**
* Opens a document file
*
@ -78,6 +87,15 @@ bool cmd_open(girara_session_t* session, girara_list_t* argument_list);
*/
bool cmd_print(girara_session_t* session, girara_list_t* argument_list);
/**
* Hides current search results
*
* @param session The used girara session
* @param argument_list List of passed arguments
* @return true if no error occured
*/
bool cmd_nohlsearch(girara_session_t* session, girara_list_t* argument_list);
/**
* Close zathura
*

View file

@ -8,6 +8,7 @@
#include "zathura.h"
#include "render.h"
#include "marks.h"
#include "utils.h"
#include <girara/settings.h>
#include <girara/session.h>
@ -55,6 +56,19 @@ cb_page_padding_changed(girara_session_t* session, const char* UNUSED(name),
gtk_table_set_col_spacings(GTK_TABLE(zathura->ui.page_widget), val);
}
static void
cb_nohlsearch_changed(girara_session_t* session, const char* UNUSED(name),
girara_setting_type_t UNUSED(type), void* value, void* UNUSED(data))
{
g_return_if_fail(value != NULL);
g_return_if_fail(session != NULL);
g_return_if_fail(session->global.data != NULL);
zathura_t* zathura = session->global.data;
document_draw_search_results(zathura, !(*(bool*) value));
render_all(zathura);
}
void
config_load_default(zathura_t* zathura)
{
@ -124,6 +138,8 @@ config_load_default(zathura_t* zathura)
girara_setting_add(gsession, "show-directories", &bool_value, BOOLEAN, false, _("Show directories"), NULL, NULL);
bool_value = false;
girara_setting_add(gsession, "open-first-page", &bool_value, BOOLEAN, false, _("Always open on first page"), NULL, NULL);
bool_value = false;
girara_setting_add(gsession, "nohlsearch", &bool_value, BOOLEAN, false, _("Highlight search results"), cb_nohlsearch_changed, NULL);
/* define default shortcuts */
girara_shortcut_add(gsession, GDK_CONTROL_MASK, GDK_KEY_c, NULL, sc_abort, 0, 0, NULL);
@ -256,8 +272,10 @@ config_load_default(zathura_t* zathura)
girara_inputbar_command_add(gsession, "write!", NULL, cmd_savef, cc_write, _("Save document (and force overwriting)"));
girara_inputbar_command_add(gsession, "export", NULL, cmd_export, cc_export, _("Save attachments"));
girara_inputbar_command_add(gsession, "offset", NULL, cmd_offset, NULL, _("Set page offset"));
girara_inputbar_command_add(gsession, "delmarks", "delm", cmd_marks_delete, NULL, _("Delete the specified marks"));
girara_inputbar_command_add(gsession, "mark", NULL, cmd_marks_add, NULL, _("Mark current location within the document"));
girara_inputbar_command_add(gsession, "delmarks", "delm", cmd_marks_delete, NULL, _("Delete the specified marks"));
girara_inputbar_command_add(gsession, "nohlsearch", "nohl", cmd_nohlsearch, NULL, _("Don't highlight current search results"));
girara_inputbar_command_add(gsession, "hlsearch", NULL, cmd_hlsearch, NULL, _("Highlight current search results"));
girara_special_command_add(gsession, '/', cmd_search, true, FORWARD, NULL);
girara_special_command_add(gsession, '?', cmd_search, true, BACKWARD, NULL);

View file

@ -27,6 +27,7 @@ typedef struct zathura_page_widget_private_s {
unsigned int number_of_links; /**< Offset to the links */
girara_list_t* search_results; /**< A list if there are search results that should be drawn */
int search_current; /**< The index of the current search result */
bool draw_search_results; /**< Draw search results */
zathura_rectangle_t selection; /**< Region selected with the mouse */
struct {
int x;
@ -69,7 +70,8 @@ enum properties_e
PROP_SEARCH_RESULT,
PROP_SEARCH_RESULTS,
PROP_SEARCH_RESULTS_LENGTH,
PROP_SEARCH_RESULTS_CURRENT
PROP_SEARCH_RESULTS_CURRENT,
PROP_DRAW_SEARCH_RESULTS
};
static void
@ -113,6 +115,8 @@ zathura_page_widget_class_init(ZathuraPageClass* class)
g_param_spec_int("search-current", "search-current", "The current search result", -1, INT_MAX, 0, G_PARAM_WRITABLE | G_PARAM_READABLE));
g_object_class_install_property(object_class, PROP_SEARCH_RESULTS_LENGTH,
g_param_spec_int("search-length", "search-length", "The number of search results", -1, INT_MAX, 0, G_PARAM_READABLE));
g_object_class_install_property(object_class, PROP_DRAW_SEARCH_RESULTS,
g_param_spec_boolean("draw-search-results", "draw-search-results", "Set to true if search results should be drawn", FALSE, G_PARAM_WRITABLE));
}
static void
@ -133,6 +137,7 @@ zathura_page_widget_init(ZathuraPage* widget)
priv->images_got = false;
priv->current_image = NULL;
priv->last_view = g_get_real_time();
priv->draw_search_results = true;
g_static_mutex_init(&(priv->lock));
/* we want mouse events */
@ -206,12 +211,12 @@ zathura_page_widget_set_property(GObject* object, guint prop_id, const GValue* v
priv->link_offset = g_value_get_int(value);
break;
case PROP_SEARCH_RESULTS:
if (priv->search_results != NULL) {
if (priv->search_results != NULL && priv->draw_search_results) {
redraw_all_rects(pageview, priv->search_results);
girara_list_free(priv->search_results);
}
priv->search_results = g_value_get_pointer(value);
if (priv->search_results != NULL) {
if (priv->search_results != NULL && priv->draw_search_results) {
priv->draw_links = false;
redraw_all_rects(pageview, priv->search_results);
}
@ -231,10 +236,15 @@ zathura_page_widget_set_property(GObject* object, guint prop_id, const GValue* v
priv->search_current = val;
zathura_rectangle_t* rect = girara_list_nth(priv->search_results, priv->search_current);
zathura_rectangle_t rectangle = recalc_rectangle(priv->page, *rect);
if (priv->draw_search_results) {
redraw_rect(pageview, &rectangle);
}
}
break;
}
case PROP_DRAW_SEARCH_RESULTS:
priv->draw_search_results = g_value_get_boolean(value);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
}
@ -364,7 +374,7 @@ zathura_page_widget_draw(GtkWidget* widget, cairo_t* cairo)
}
/* draw search results */
if (priv->search_results != NULL) {
if (priv->search_results != NULL && priv->draw_search_results == true) {
int idx = 0;
GIRARA_LIST_FOREACH(priv->search_results, zathura_rectangle_t*, iter, rect)
zathura_rectangle_t rectangle = recalc_rectangle(priv->page, *rect);

13
utils.c
View file

@ -373,3 +373,16 @@ open_remote(zathura_t* zathura, const char* file)
g_free(uri);
g_free(dir);
}
void
document_draw_search_results(zathura_t* zathura, bool value)
{
if (zathura == NULL || zathura->document == NULL || zathura->pages == NULL) {
return;
}
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++) {
g_object_set(zathura->pages[page_id], "draw-search-results", (value == true) ? TRUE : FALSE, NULL);
}
}

View file

@ -141,4 +141,12 @@ void zathura_link_evaluate(zathura_t* zathura, zathura_link_t* link);
*/
void open_remote(zathura_t* zathura, const char* file);
/**
* Set if the search results should be drawn or not
*
* @param zathura Zathura instance
* @param value true if they should be drawn, otherwise false
*/
void document_draw_search_results(zathura_t* zathura, bool value);
#endif // UTILS_H