2004-08-06 00:32:00 +04:00
|
|
|
/*
|
|
|
|
* This file is part of NetSurf, http://netsurf.sourceforge.net/
|
|
|
|
* Licensed under the GNU General Public License,
|
|
|
|
* http://www.opensource.org/licenses/gpl-license
|
|
|
|
* Copyright 2004 John M Bell <jmb202@ecs.soton.ac.uk>
|
2005-04-17 07:30:35 +04:00
|
|
|
* Copyright 2005 Adrian Lees <adrianl@users.sourceforge.net>
|
2004-08-06 00:32:00 +04:00
|
|
|
*/
|
|
|
|
|
2004-08-06 02:03:56 +04:00
|
|
|
/** \file
|
|
|
|
* Free text search (implementation)
|
|
|
|
*/
|
|
|
|
|
2005-03-25 17:25:25 +03:00
|
|
|
#include <ctype.h>
|
2004-08-06 00:32:00 +04:00
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "oslib/wimp.h"
|
|
|
|
|
|
|
|
#include "netsurf/utils/config.h"
|
2004-08-06 02:38:26 +04:00
|
|
|
#include "netsurf/content/content.h"
|
2004-08-06 00:32:00 +04:00
|
|
|
#include "netsurf/desktop/browser.h"
|
|
|
|
#include "netsurf/desktop/gui.h"
|
2005-04-17 07:30:35 +04:00
|
|
|
#include "netsurf/desktop/selection.h"
|
2004-08-06 00:32:00 +04:00
|
|
|
#include "netsurf/render/box.h"
|
|
|
|
#include "netsurf/render/html.h"
|
|
|
|
#include "netsurf/riscos/gui.h"
|
2005-04-08 00:46:22 +04:00
|
|
|
#include "netsurf/riscos/menus.h"
|
2004-08-06 00:32:00 +04:00
|
|
|
#include "netsurf/riscos/wimp.h"
|
|
|
|
#include "netsurf/utils/log.h"
|
|
|
|
#include "netsurf/utils/utils.h"
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef WITH_SEARCH
|
|
|
|
|
2005-03-25 17:25:25 +03:00
|
|
|
#ifndef NOF_ELEMENTS
|
|
|
|
#define NOF_ELEMENTS(array) (sizeof(array)/sizeof(*(array)))
|
|
|
|
#endif
|
|
|
|
|
2004-08-06 00:32:00 +04:00
|
|
|
struct list_entry {
|
2005-04-17 07:30:35 +04:00
|
|
|
/* start position of match */
|
|
|
|
struct box *start_box;
|
|
|
|
unsigned start_idx;
|
|
|
|
/* end of match */
|
|
|
|
struct box *end_box;
|
|
|
|
unsigned end_idx;
|
|
|
|
|
2004-08-06 00:32:00 +04:00
|
|
|
struct list_entry *prev;
|
|
|
|
struct list_entry *next;
|
|
|
|
};
|
|
|
|
|
2005-04-17 07:30:35 +04:00
|
|
|
struct gui_window *search_current_window = 0;
|
|
|
|
static struct selection *search_selection = 0;
|
2004-08-06 02:03:56 +04:00
|
|
|
|
2004-08-06 00:32:00 +04:00
|
|
|
static char *search_string = 0;
|
2005-04-17 07:30:35 +04:00
|
|
|
static struct list_entry search_head = { 0, 0, 0, 0, 0, 0 };
|
2004-08-06 02:03:56 +04:00
|
|
|
static struct list_entry *search_found = &search_head;
|
|
|
|
static struct list_entry *search_current = 0;
|
2004-08-06 02:38:26 +04:00
|
|
|
static struct content *search_content = 0;
|
2004-08-06 02:03:56 +04:00
|
|
|
static bool search_prev_from_top = false;
|
|
|
|
static bool search_prev_case_sens = false;
|
|
|
|
|
|
|
|
static void start_search(void);
|
|
|
|
static void end_search(void);
|
|
|
|
static void do_search(char *string, bool from_top, bool case_sens, bool forwards);
|
2005-04-17 07:30:35 +04:00
|
|
|
static const char *find_pattern(const char *string, int s_len,
|
|
|
|
const char *pattern, int p_len, bool case_sens, int *m_len);
|
|
|
|
static bool find_occurrences(const char *pattern, int p_len, struct box *cur,
|
|
|
|
bool case_sens);
|
2004-08-06 00:32:00 +04:00
|
|
|
|
|
|
|
|
2004-08-06 02:03:56 +04:00
|
|
|
/**
|
|
|
|
* Open the search dialog
|
|
|
|
*
|
|
|
|
* \param g the gui window to search
|
|
|
|
*/
|
2005-04-08 00:46:22 +04:00
|
|
|
void ro_gui_search_prepare(struct gui_window *g)
|
2004-08-06 02:03:56 +04:00
|
|
|
{
|
2005-04-17 07:30:35 +04:00
|
|
|
struct content *c;
|
|
|
|
|
2004-08-06 02:03:56 +04:00
|
|
|
assert(g != NULL);
|
|
|
|
|
|
|
|
search_current_window = g;
|
|
|
|
|
|
|
|
ro_gui_set_icon_string(dialog_search, ICON_SEARCH_TEXT, "");
|
|
|
|
ro_gui_set_icon_selected_state(dialog_search, ICON_SEARCH_FORWARDS,
|
|
|
|
true);
|
|
|
|
ro_gui_set_icon_selected_state(dialog_search, ICON_SEARCH_BACKWARDS,
|
|
|
|
false);
|
|
|
|
ro_gui_set_icon_selected_state(dialog_search, ICON_SEARCH_START,
|
|
|
|
false);
|
|
|
|
ro_gui_set_icon_selected_state(dialog_search,
|
|
|
|
ICON_SEARCH_CASE_SENSITIVE, false);
|
2005-04-17 07:30:35 +04:00
|
|
|
|
|
|
|
c = search_current_window->bw->current_content;
|
|
|
|
|
|
|
|
if (!c)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* only handle html contents */
|
|
|
|
if (c->type != CONTENT_HTML)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* \todo build me properly please! */
|
|
|
|
search_selection = selection_create(g->bw);
|
|
|
|
if (!search_selection)
|
|
|
|
warn_user("NoMemory", 0);
|
|
|
|
|
|
|
|
selection_init(search_selection, c->data.html.layout);
|
2004-08-06 02:03:56 +04:00
|
|
|
}
|
|
|
|
|
2004-08-06 00:32:00 +04:00
|
|
|
/**
|
|
|
|
* Handle clicks in the search dialog
|
|
|
|
*
|
|
|
|
* \param pointer wimp_pointer block
|
|
|
|
* \param parent The parent window of this persistent dialog
|
|
|
|
*/
|
2004-08-06 02:03:56 +04:00
|
|
|
void ro_gui_search_click(wimp_pointer *pointer)
|
2004-08-06 00:32:00 +04:00
|
|
|
{
|
|
|
|
if (pointer->buttons == wimp_CLICK_MENU)
|
|
|
|
return;
|
|
|
|
|
|
|
|
switch(pointer->i) {
|
|
|
|
case ICON_SEARCH_FORWARDS:
|
|
|
|
case ICON_SEARCH_BACKWARDS:
|
|
|
|
/* prevent deselection on adjust clicking */
|
|
|
|
if (pointer->buttons == wimp_CLICK_ADJUST)
|
|
|
|
ro_gui_set_icon_selected_state(dialog_search,
|
|
|
|
pointer->i, true);
|
|
|
|
break;
|
|
|
|
case ICON_SEARCH_FIND:
|
2004-08-06 02:03:56 +04:00
|
|
|
start_search();
|
2004-08-06 00:32:00 +04:00
|
|
|
break;
|
|
|
|
case ICON_SEARCH_CANCEL:
|
2004-08-06 02:03:56 +04:00
|
|
|
end_search();
|
2004-08-06 00:32:00 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2004-08-06 02:03:56 +04:00
|
|
|
* Handle keypresses in the search dialog
|
|
|
|
*
|
|
|
|
* \param key wimp_key block
|
|
|
|
* \return true if keypress handled, false otherwise
|
2004-08-06 00:32:00 +04:00
|
|
|
*/
|
2004-08-06 02:03:56 +04:00
|
|
|
bool ro_gui_search_keypress(wimp_key *key)
|
2004-08-06 00:32:00 +04:00
|
|
|
{
|
2004-08-06 02:03:56 +04:00
|
|
|
bool state;
|
|
|
|
|
|
|
|
switch (key->c) {
|
|
|
|
case 2: /* ctrl b */
|
|
|
|
ro_gui_set_icon_selected_state(dialog_search, ICON_SEARCH_FORWARDS, false);
|
|
|
|
ro_gui_set_icon_selected_state(dialog_search, ICON_SEARCH_BACKWARDS, true);
|
|
|
|
return true;
|
|
|
|
case 6: /* ctrl f */
|
|
|
|
ro_gui_set_icon_selected_state(dialog_search, ICON_SEARCH_FORWARDS, true);
|
|
|
|
ro_gui_set_icon_selected_state(dialog_search, ICON_SEARCH_BACKWARDS, false);
|
|
|
|
return true;
|
|
|
|
case 9: /* ctrl i */
|
|
|
|
state = ro_gui_get_icon_selected_state(dialog_search, ICON_SEARCH_CASE_SENSITIVE);
|
|
|
|
ro_gui_set_icon_selected_state(dialog_search, ICON_SEARCH_CASE_SENSITIVE, !state);
|
|
|
|
return true;
|
|
|
|
case 19: /* ctrl s */
|
|
|
|
state = ro_gui_get_icon_selected_state(dialog_search, ICON_SEARCH_START);
|
|
|
|
ro_gui_set_icon_selected_state(dialog_search, ICON_SEARCH_START, !state);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
case wimp_KEY_RETURN:
|
|
|
|
start_search();
|
|
|
|
return true;
|
|
|
|
case wimp_KEY_ESCAPE:
|
|
|
|
end_search();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Begins the search process
|
|
|
|
*/
|
|
|
|
void start_search(void)
|
|
|
|
{
|
|
|
|
char *string;
|
|
|
|
|
|
|
|
string = ro_gui_get_icon_string(dialog_search, ICON_SEARCH_TEXT);
|
|
|
|
if (strlen(string) == 0)
|
|
|
|
return;
|
|
|
|
do_search(string,
|
|
|
|
ro_gui_get_icon_selected_state(dialog_search,
|
|
|
|
ICON_SEARCH_START),
|
|
|
|
ro_gui_get_icon_selected_state(dialog_search,
|
|
|
|
ICON_SEARCH_CASE_SENSITIVE),
|
|
|
|
ro_gui_get_icon_selected_state(dialog_search,
|
|
|
|
ICON_SEARCH_FORWARDS));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Ends the search process, invalidating all global state and
|
|
|
|
* freeing the list of found boxes
|
|
|
|
*/
|
|
|
|
void end_search(void)
|
|
|
|
{
|
|
|
|
struct list_entry *a, *b;
|
|
|
|
|
2005-04-17 07:30:35 +04:00
|
|
|
selection_clear(search_selection, true);
|
|
|
|
selection_destroy(search_selection);
|
|
|
|
search_selection = 0;
|
|
|
|
|
2004-08-06 02:03:56 +04:00
|
|
|
search_current_window = 0;
|
|
|
|
|
|
|
|
if (search_string)
|
|
|
|
free(search_string);
|
|
|
|
search_string = 0;
|
|
|
|
|
|
|
|
for (a = search_found->next; a; a = b) {
|
|
|
|
b = a->next;
|
|
|
|
free(a);
|
|
|
|
}
|
|
|
|
search_found->prev = 0;
|
|
|
|
search_found->next = 0;
|
|
|
|
|
|
|
|
search_current = 0;
|
|
|
|
|
2004-08-06 02:38:26 +04:00
|
|
|
search_content = 0;
|
2004-08-06 02:03:56 +04:00
|
|
|
|
|
|
|
search_prev_from_top = false;
|
|
|
|
search_prev_case_sens = false;
|
|
|
|
|
|
|
|
/* and close the window */
|
2005-04-08 00:46:22 +04:00
|
|
|
ro_gui_menu_closed();
|
2004-08-06 02:03:56 +04:00
|
|
|
ro_gui_dialog_close(dialog_search);
|
2004-08-06 00:32:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Search for a string in the box tree
|
|
|
|
*
|
|
|
|
* \param string the string to search for
|
|
|
|
* \param from_top whether to display results from the top of the page, or
|
|
|
|
* the current scroll position
|
|
|
|
* \param case_sens whether to perform a case sensitive search
|
|
|
|
* \param forwards direction to search in
|
|
|
|
*/
|
2004-08-06 02:03:56 +04:00
|
|
|
void do_search(char *string, bool from_top, bool case_sens, bool forwards)
|
2004-08-06 00:32:00 +04:00
|
|
|
{
|
|
|
|
struct content *c;
|
|
|
|
struct box *box;
|
|
|
|
struct list_entry *a, *b;
|
|
|
|
int x,y;
|
|
|
|
bool new = false;
|
|
|
|
|
2004-08-06 02:03:56 +04:00
|
|
|
if (!search_current_window)
|
2004-08-06 00:32:00 +04:00
|
|
|
return;
|
|
|
|
|
2004-08-06 02:03:56 +04:00
|
|
|
c = search_current_window->bw->current_content;
|
2004-08-06 00:32:00 +04:00
|
|
|
|
2004-08-06 02:38:26 +04:00
|
|
|
if (!c)
|
|
|
|
return;
|
|
|
|
|
2004-08-06 00:32:00 +04:00
|
|
|
/* only handle html contents */
|
|
|
|
if (c->type != CONTENT_HTML)
|
|
|
|
return;
|
|
|
|
|
2004-10-18 01:16:00 +04:00
|
|
|
box = c->data.html.layout;
|
2004-08-06 00:32:00 +04:00
|
|
|
|
|
|
|
if (!box)
|
|
|
|
return;
|
|
|
|
|
2004-08-06 02:38:26 +04:00
|
|
|
// LOG(("'%s' - '%s' (%p, %p) %p (%d, %d) (%d, %d) %d", search_string, string, search_content, c, search_found->next, search_prev_from_top, from_top, search_prev_case_sens, case_sens, forwards));
|
2004-08-06 00:32:00 +04:00
|
|
|
|
|
|
|
/* check if we need to start a new search or continue an old one */
|
2004-08-06 02:38:26 +04:00
|
|
|
if (!search_string || c != search_content ||
|
|
|
|
!search_found->next || search_prev_from_top != from_top ||
|
2004-08-06 02:03:56 +04:00
|
|
|
search_prev_case_sens != case_sens ||
|
2004-08-06 00:32:00 +04:00
|
|
|
(case_sens && strcmp(string, search_string) != 0) ||
|
|
|
|
(!case_sens && strcasecmp(string, search_string) != 0)) {
|
|
|
|
if (search_string)
|
|
|
|
free(search_string);
|
|
|
|
search_string = strdup(string);
|
2004-08-06 02:03:56 +04:00
|
|
|
search_current = 0;
|
|
|
|
for (a = search_found->next; a; a = b) {
|
2004-08-06 00:32:00 +04:00
|
|
|
b = a->next;
|
|
|
|
free(a);
|
|
|
|
}
|
2004-08-06 02:03:56 +04:00
|
|
|
search_found->prev = 0;
|
|
|
|
search_found->next = 0;
|
2005-03-25 17:25:25 +03:00
|
|
|
if (!find_occurrences(string, strlen(string), box, case_sens)) {
|
2004-08-06 02:03:56 +04:00
|
|
|
for (a = search_found->next; a; a = b) {
|
2004-08-06 00:32:00 +04:00
|
|
|
b = a->next;
|
|
|
|
free(a);
|
|
|
|
}
|
2004-08-06 02:03:56 +04:00
|
|
|
search_found->prev = 0;
|
|
|
|
search_found->next = 0;
|
2004-08-06 00:32:00 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
new = true;
|
2004-08-06 02:38:26 +04:00
|
|
|
search_content = c;
|
2004-08-06 02:03:56 +04:00
|
|
|
search_prev_from_top = from_top;
|
|
|
|
search_prev_case_sens = case_sens;
|
2004-08-06 00:32:00 +04:00
|
|
|
}
|
|
|
|
|
2004-08-06 02:38:26 +04:00
|
|
|
// LOG(("%d %p %p (%p, %p)", new, search_found->next, search_current, search_current->prev, search_current->next));
|
2004-08-06 00:32:00 +04:00
|
|
|
|
2004-08-06 02:03:56 +04:00
|
|
|
if (!search_found->next)
|
2004-08-06 00:32:00 +04:00
|
|
|
return;
|
|
|
|
|
|
|
|
if (new && from_top) {
|
|
|
|
/* new search, beginning at the top of the page */
|
2004-08-06 02:03:56 +04:00
|
|
|
search_current = search_found->next;
|
2004-08-06 00:32:00 +04:00
|
|
|
}
|
|
|
|
else if (new) {
|
|
|
|
/* new search, beginning from user's current scroll
|
|
|
|
* position */
|
|
|
|
wimp_window_state state;
|
|
|
|
os_error *error;
|
|
|
|
|
2004-08-06 02:03:56 +04:00
|
|
|
state.w = search_current_window->window;
|
2004-08-06 00:32:00 +04:00
|
|
|
error = xwimp_get_window_state(&state);
|
|
|
|
if (error) {
|
|
|
|
LOG(("xwimp_get_window_state: 0x%x: %s",
|
|
|
|
error->errnum, error->errmess));
|
|
|
|
warn_user("WimpError", error->errmess);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2004-08-06 02:03:56 +04:00
|
|
|
for (a = search_found->next; a; a = a->next) {
|
2005-04-17 07:30:35 +04:00
|
|
|
box_coords(a->start_box, &x, &y);
|
2004-08-06 00:32:00 +04:00
|
|
|
LOG(("%d, %d", y, state.yscroll / 2));
|
|
|
|
if (forwards && -y <= state.yscroll / 2)
|
|
|
|
break;
|
|
|
|
if (!forwards && -y >= state.yscroll / 2)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (a)
|
2004-08-06 02:03:56 +04:00
|
|
|
search_current = a;
|
2004-08-06 00:32:00 +04:00
|
|
|
else
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* continued search in the direction specified */
|
2004-08-06 02:03:56 +04:00
|
|
|
if (forwards && search_current && search_current->next) {
|
|
|
|
search_current = search_current->next;
|
2004-08-06 00:32:00 +04:00
|
|
|
}
|
2004-08-06 02:03:56 +04:00
|
|
|
else if (!forwards && search_current && search_current->prev) {
|
|
|
|
search_current = search_current->prev;
|
2004-08-06 00:32:00 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-08-06 02:03:56 +04:00
|
|
|
if (!search_current)
|
2004-08-06 00:32:00 +04:00
|
|
|
return;
|
|
|
|
|
2005-04-17 07:30:35 +04:00
|
|
|
box = search_current->start_box;
|
|
|
|
|
|
|
|
selection_clear(search_selection, true);
|
|
|
|
selection_set_start(search_selection, search_current->start_box,
|
|
|
|
search_current->start_idx);
|
|
|
|
selection_set_end(search_selection, search_current->end_box,
|
|
|
|
search_current->end_idx);
|
|
|
|
|
2004-08-06 00:32:00 +04:00
|
|
|
/* get box position and jump to it */
|
2005-04-17 07:30:35 +04:00
|
|
|
box_coords(search_current->start_box, &x, &y);
|
2004-08-06 02:38:26 +04:00
|
|
|
// LOG(("%p (%d, %d)", search_current, x, y));
|
2004-08-06 02:03:56 +04:00
|
|
|
gui_window_set_scroll(search_current_window, x, y);
|
2004-08-06 00:32:00 +04:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2005-03-25 17:25:25 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Find the first occurrence of 'match' in 'string' and return its index
|
|
|
|
*
|
2005-04-17 07:30:35 +04:00
|
|
|
* /param string the string to be searched (unterminated)
|
|
|
|
* /param s_len length of the string to be searched
|
|
|
|
* /param pattern the pattern for which we are searching (unterminated)
|
|
|
|
* /param p_len length of pattern
|
|
|
|
* /param case_sens true iff case sensitive match required
|
|
|
|
* /param m_len accepts length of match in bytes
|
|
|
|
* /return pointer to first match, NULL if none
|
2005-03-25 17:25:25 +03:00
|
|
|
*/
|
|
|
|
|
2005-04-17 07:30:35 +04:00
|
|
|
const char *find_pattern(const char *string, int s_len, const char *pattern,
|
|
|
|
int p_len, bool case_sens, int *m_len)
|
2005-03-25 17:25:25 +03:00
|
|
|
{
|
2005-04-17 07:30:35 +04:00
|
|
|
struct { const char *ss, *s, *p; bool first; } context[16];
|
2005-03-25 17:25:25 +03:00
|
|
|
const char *ep = pattern + p_len;
|
|
|
|
const char *es = string + s_len;
|
|
|
|
const char *p = pattern - 1; /* a virtual '*' before the pattern */
|
|
|
|
const char *ss = string;
|
|
|
|
const char *s = string;
|
2005-04-17 07:30:35 +04:00
|
|
|
bool first = true;
|
2005-03-25 17:25:25 +03:00
|
|
|
int top = 0;
|
|
|
|
|
|
|
|
while (p < ep) {
|
|
|
|
bool matches;
|
|
|
|
if (p < pattern || *p == '*') {
|
|
|
|
char ch;
|
|
|
|
|
|
|
|
/* skip any further asterisks; one is the same as many */
|
|
|
|
do p++; while (p < ep && *p == '*');
|
|
|
|
|
|
|
|
/* if we're at the end of the pattern, yes, it matches */
|
2005-04-17 07:30:35 +04:00
|
|
|
if (p >= ep) break;
|
2005-03-25 17:25:25 +03:00
|
|
|
|
|
|
|
/* anything matches a # so continue matching from
|
|
|
|
here, and stack a context that will try to match
|
|
|
|
the wildcard against the next character */
|
|
|
|
|
|
|
|
ch = *p;
|
|
|
|
if (ch != '#') {
|
|
|
|
/* scan forwards until we find a match for this char */
|
|
|
|
if (!case_sens) ch = toupper(ch);
|
|
|
|
while (s < es) {
|
|
|
|
if (case_sens) {
|
|
|
|
if (*s == ch) break;
|
|
|
|
} else if (toupper(*s) == ch)
|
|
|
|
break;
|
|
|
|
s++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (s < es) {
|
2005-04-17 07:30:35 +04:00
|
|
|
/* remember where we are in case the match fails;
|
|
|
|
we can then resume */
|
2005-03-25 17:25:25 +03:00
|
|
|
if (top < (int)NOF_ELEMENTS(context)) {
|
|
|
|
context[top].ss = ss;
|
|
|
|
context[top].s = s + 1;
|
|
|
|
context[top].p = p - 1; /* ptr to last asterisk */
|
2005-04-17 07:30:35 +04:00
|
|
|
context[top].first = first;
|
2005-03-25 17:25:25 +03:00
|
|
|
top++;
|
|
|
|
}
|
2005-04-17 07:30:35 +04:00
|
|
|
|
|
|
|
if (first) {
|
|
|
|
ss = s; /* remember first non-'*' char */
|
|
|
|
first = false;
|
|
|
|
}
|
|
|
|
|
2005-03-25 17:25:25 +03:00
|
|
|
matches = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
matches = false;
|
|
|
|
}
|
|
|
|
else if (s < es) {
|
|
|
|
char ch = *p;
|
|
|
|
if (ch == '#')
|
|
|
|
matches = true;
|
|
|
|
else {
|
|
|
|
if (case_sens)
|
|
|
|
matches = (*s == ch);
|
|
|
|
else
|
|
|
|
matches = (toupper(*s) == toupper(ch));
|
|
|
|
}
|
2005-04-17 07:30:35 +04:00
|
|
|
if (matches && first) {
|
|
|
|
ss = s; /* remember first non-'*' char */
|
|
|
|
first = false;
|
|
|
|
}
|
2005-03-25 17:25:25 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
matches = false;
|
|
|
|
|
|
|
|
if (matches) {
|
|
|
|
p++; s++;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* doesn't match, resume with stacked context if we have one */
|
|
|
|
if (--top < 0) return NULL; /* no match, give up */
|
|
|
|
|
|
|
|
ss = context[top].ss;
|
|
|
|
s = context[top].s;
|
|
|
|
p = context[top].p;
|
2005-04-17 07:30:35 +04:00
|
|
|
first = context[top].first;
|
2005-03-25 17:25:25 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* end of pattern reached */
|
2005-04-17 07:30:35 +04:00
|
|
|
*m_len = s - ss;
|
2005-03-25 17:25:25 +03:00
|
|
|
return ss;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-08-06 00:32:00 +04:00
|
|
|
/**
|
|
|
|
* Finds all occurrences of a given string in the box tree
|
|
|
|
*
|
2005-03-25 17:25:25 +03:00
|
|
|
* \param pattern the string pattern to search for
|
|
|
|
* \param p_len pattern length
|
|
|
|
* \param cur pointer to the current box
|
2004-08-06 00:32:00 +04:00
|
|
|
* \param case_sens whether to perform a case sensitive search
|
|
|
|
* \return true on success, false on memory allocation failure
|
|
|
|
*/
|
2005-04-17 07:30:35 +04:00
|
|
|
bool find_occurrences(const char *pattern, int p_len, struct box *cur,
|
|
|
|
bool case_sens)
|
2004-08-06 00:32:00 +04:00
|
|
|
{
|
|
|
|
struct box *a;
|
|
|
|
|
|
|
|
/* ignore this box, if there's no visible text */
|
|
|
|
if (!cur->object && cur->text) {
|
2005-04-17 08:13:13 +04:00
|
|
|
const char *text = cur->text;
|
|
|
|
unsigned length = cur->length;
|
|
|
|
|
|
|
|
while (length > 0) {
|
|
|
|
unsigned match_length;
|
2005-04-17 07:30:35 +04:00
|
|
|
unsigned match_offset;
|
2005-04-17 08:13:13 +04:00
|
|
|
const char *new_text;
|
|
|
|
struct list_entry *entry;
|
|
|
|
const char *pos = find_pattern(text, length,
|
|
|
|
pattern, p_len, case_sens, &match_length);
|
|
|
|
if (!pos) break;
|
|
|
|
|
|
|
|
/* found string in box => add to list */
|
|
|
|
entry = calloc(1, sizeof(*entry));
|
2004-08-06 00:32:00 +04:00
|
|
|
if (!entry) {
|
|
|
|
warn_user("NoMemory", 0);
|
|
|
|
return false;
|
|
|
|
}
|
2005-04-17 08:13:13 +04:00
|
|
|
|
2005-04-17 07:30:35 +04:00
|
|
|
match_offset = pos - cur->text;
|
2005-04-17 08:13:13 +04:00
|
|
|
|
2005-04-17 07:30:35 +04:00
|
|
|
entry->start_box = cur;
|
|
|
|
entry->start_idx = match_offset;
|
|
|
|
entry->end_box = cur;
|
|
|
|
entry->end_idx = match_offset + match_length;
|
2004-08-06 00:32:00 +04:00
|
|
|
entry->next = 0;
|
2004-08-06 02:03:56 +04:00
|
|
|
entry->prev = search_found->prev;
|
|
|
|
if (!search_found->prev)
|
|
|
|
search_found->next = entry;
|
2004-08-06 00:32:00 +04:00
|
|
|
else
|
2004-08-06 02:03:56 +04:00
|
|
|
search_found->prev->next = entry;
|
|
|
|
search_found->prev = entry;
|
2005-04-17 08:13:13 +04:00
|
|
|
|
|
|
|
new_text = pos + match_length;
|
|
|
|
length -= (new_text - text);
|
|
|
|
text = new_text;
|
2004-08-06 00:32:00 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* and recurse */
|
|
|
|
for (a = cur->children; a; a = a->next) {
|
2005-03-25 17:25:25 +03:00
|
|
|
if (!find_occurrences(pattern, p_len, a, case_sens))
|
2004-08-06 02:03:56 +04:00
|
|
|
return false;
|
2004-08-06 00:32:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2005-04-17 07:30:35 +04:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determines whether any portion of the given text box should be
|
|
|
|
* selected because it matches the current search string.
|
|
|
|
*
|
|
|
|
* \param g gui window
|
|
|
|
* \param box box being tested
|
|
|
|
* \param start_idx byte offset within text box of highlight start
|
|
|
|
* \param end_idx byte offset of highlight end
|
|
|
|
* \return true iff part of the box should be highlighted
|
|
|
|
*/
|
|
|
|
|
|
|
|
bool gui_search_term_highlighted(struct gui_window *g, struct box *box,
|
|
|
|
unsigned *start_idx, unsigned *end_idx)
|
|
|
|
{
|
|
|
|
if (g == search_current_window && search_selection) {
|
|
|
|
if (selection_defined(search_selection))
|
|
|
|
return selection_highlighted(search_selection, box, start_idx, end_idx);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-08-06 00:32:00 +04:00
|
|
|
#endif
|