Enhance LASTLOG to use gtk_xtext_search_textentry()

This commit is contained in:
RichardHitt 2012-08-28 19:10:03 -07:00
parent b7f5f045c6
commit ed034bf80a
8 changed files with 124 additions and 72 deletions

View File

@ -98,7 +98,7 @@ void fe_set_nonchannel (struct session *sess, int state);
void fe_set_nick (struct server *serv, char *newnick); void fe_set_nick (struct server *serv, char *newnick);
void fe_ignore_update (int level); void fe_ignore_update (int level);
void fe_beep (void); void fe_beep (void);
void fe_lastlog (session *sess, session *lastlog_sess, char *sstr, gboolean regexp); void fe_lastlog (session *sess, session *lastlog_sess, char *sstr, gtk_xtext_search_flags flags);
void fe_set_lag (server *serv, int lag); void fe_set_lag (server *serv, int lag);
void fe_set_throttle (server *serv); void fe_set_throttle (server *serv);
void fe_set_away (server *serv); void fe_set_away (server *serv);

View File

@ -2362,7 +2362,7 @@ cmd_lagcheck (struct session *sess, char *tbuf, char *word[], char *word_eol[])
} }
static void static void
lastlog (session *sess, char *search, gboolean regexp) lastlog (session *sess, char *search, gtk_xtext_search_flags flags)
{ {
session *lastlog_sess; session *lastlog_sess;
@ -2374,29 +2374,50 @@ lastlog (session *sess, char *search, gboolean regexp)
lastlog_sess = new_ircwindow (sess->server, "(lastlog)", SESS_DIALOG, 0); lastlog_sess = new_ircwindow (sess->server, "(lastlog)", SESS_DIALOG, 0);
lastlog_sess->lastlog_sess = sess; lastlog_sess->lastlog_sess = sess;
lastlog_sess->lastlog_regexp = regexp; /* remember the search type */ lastlog_sess->lastlog_flags = flags;
fe_text_clear (lastlog_sess, 0); fe_text_clear (lastlog_sess, 0);
fe_lastlog (sess, lastlog_sess, search, regexp); fe_lastlog (sess, lastlog_sess, search, flags);
} }
static int static int
cmd_lastlog (struct session *sess, char *tbuf, char *word[], char *word_eol[]) cmd_lastlog (struct session *sess, char *tbuf, char *word[], char *word_eol[])
{ {
if (*word_eol[2]) int j = 2;
gtk_xtext_search_flags flags = 0;
gboolean doublehyphen = FALSE;
while (word_eol[j] != NULL && word_eol [j][0] == '-' && !doublehyphen)
{ {
if (!strcmp (word[2], "-r")) switch (word_eol [j][1])
{ {
lastlog (sess, word_eol[3], TRUE); case 'r':
} flags |= regexp;
else break;
{ case 'm':
lastlog (sess, word_eol[2], FALSE); flags |= case_match;
break;
case 'h':
flags |= highlight;
break;
case '-':
doublehyphen = TRUE;
break;
default:
break;
/* O dear whatever shall we do here? */
} }
j++;
}
if (*word_eol[j])
{
lastlog (sess, word_eol[j], flags);
return TRUE; return TRUE;
} }
else
return FALSE; {
return FALSE;
}
} }
static int static int
@ -3602,7 +3623,11 @@ const struct commands xc_cmds[] = {
{"LAGCHECK", cmd_lagcheck, 0, 0, 1, {"LAGCHECK", cmd_lagcheck, 0, 0, 1,
N_("LAGCHECK, forces a new lag check")}, N_("LAGCHECK, forces a new lag check")},
{"LASTLOG", cmd_lastlog, 0, 0, 1, {"LASTLOG", cmd_lastlog, 0, 0, 1,
N_("LASTLOG [-r] <string>, searches for a string in the buffer")}, N_("LASTLOG [-h] [-m] [-r] [--] <string>, searches for a string in the buffer\n"
" Use -h to highlight the found string(s)\n"
" Use -m to match case\n"
" Use -r when string is a Regular Expression\n"
" Use -- (double hyphen) to end options when searching for, say, the string '-r'")},
{"LIST", cmd_list, 1, 0, 1, 0}, {"LIST", cmd_list, 1, 0, 1, 0},
{"LOAD", cmd_load, 0, 0, 1, N_("LOAD [-e] <file>, loads a plugin or script")}, {"LOAD", cmd_load, 0, 0, 1, N_("LOAD [-e] <file>, loads a plugin or script")},
@ -4099,7 +4124,7 @@ handle_say (session *sess, char *text, int check_spch)
if (strcmp (sess->channel, "(lastlog)") == 0) if (strcmp (sess->channel, "(lastlog)") == 0)
{ {
lastlog (sess->lastlog_sess, text, sess->lastlog_regexp); lastlog (sess->lastlog_sess, text, sess->lastlog_flags);
return; return;
} }

View File

@ -591,7 +591,7 @@ static char defaultconf_commands[] =
"NAME DIALOG\n" "CMD query %2\n\n"\ "NAME DIALOG\n" "CMD query %2\n\n"\
"NAME DMSG\n" "CMD msg =%2 &3\n\n"\ "NAME DMSG\n" "CMD msg =%2 &3\n\n"\
"NAME EXIT\n" "CMD quit\n\n"\ "NAME EXIT\n" "CMD quit\n\n"\
"NAME GREP\n" "CMD lastlog -r &2\n\n"\ "NAME GREP\n" "CMD lastlog -r -- &2\n\n"\
"NAME IGNALL\n" "CMD ignore %2!*@* ALL\n\n"\ "NAME IGNALL\n" "CMD ignore %2!*@* ALL\n\n"\
"NAME J\n" "CMD join &2\n\n"\ "NAME J\n" "CMD join &2\n\n"\
"NAME KILL\n" "CMD quote KILL %2 :&3\n\n"\ "NAME KILL\n" "CMD quote KILL %2 :&3\n\n"\

View File

@ -346,6 +346,15 @@ struct xchatprefs
#define SET_ON 1 #define SET_ON 1
#define SET_DEFAULT 2 /* use global setting */ #define SET_DEFAULT 2 /* use global setting */
/* Moved from fe-gtk for use in outbound.c as well -- */
typedef enum gtk_xtext_search_flags_e {
case_match = 1,
backward = 2,
highlight = 4,
follow = 8,
regexp = 16
} gtk_xtext_search_flags;
typedef struct session typedef struct session
{ {
/* Per-Channel Alerts */ /* Per-Channel Alerts */
@ -405,7 +414,7 @@ typedef struct session
int end_of_names:1; int end_of_names:1;
int doing_who:1; /* /who sent on this channel */ int doing_who:1; /* /who sent on this channel */
int done_away_check:1; /* done checking for away status changes */ int done_away_check:1; /* done checking for away status changes */
unsigned int lastlog_regexp:1; /* this is a lastlog and using regexp */ gtk_xtext_search_flags lastlog_flags;
} session; } session;
struct msproxy_state_t struct msproxy_state_t

View File

@ -700,50 +700,48 @@ fe_beep (void)
gdk_beep (); gdk_beep ();
} }
static int
lastlog_regex_cmp (char *a, GRegex *reg)
{
GMatchInfo *gmi;
int ret;
g_regex_match (reg, a, 0, &gmi);
ret = (g_match_info_matches (gmi))? TRUE: FALSE;
g_match_info_free (gmi);
return ret;
}
void void
fe_lastlog (session *sess, session *lastlog_sess, char *sstr, gboolean regexp) fe_lastlog (session *sess, session *lastlog_sess, char *sstr, gtk_xtext_search_flags flags)
{ {
GRegex *search_re = NULL;
GError *err = NULL; GError *err = NULL;
xtext_buffer *buf, *lbuf;
if (gtk_xtext_is_empty (sess->res->buffer)) buf = sess->res->buffer;
if (gtk_xtext_is_empty (buf))
{ {
PrintText (lastlog_sess, _("Search buffer is empty.\n")); PrintText (lastlog_sess, _("Search buffer is empty.\n"));
return; return;
} }
if (!regexp) lbuf = lastlog_sess->res->buffer;
if (flags & regexp)
{ {
gtk_xtext_lastlog (lastlog_sess->res->buffer, sess->res->buffer, GRegexCompileFlags gcf = (flags & case_match)? 0: G_REGEX_CASELESS;
(void *) nocasestrstr, sstr);
return;
}
/* TODO: add arg 'match' and if it's TRUE don't use G_REGEX_CASELESS lbuf->search_re = g_regex_new (sstr, gcf, 0, &err);
* and for that matter don't use nocasesetrstr() above either */ if (err)
search_re = g_regex_new (sstr, G_REGEX_CASELESS, 0, &err); {
if (err) PrintText (lastlog_sess, _(err->message));
g_error_free (err);
return;
}
}
else
{ {
PrintText (lastlog_sess, _(err->message)); if (flags & case_match)
g_error_free (err); {
return; lbuf->search_nee = g_strdup (sstr);
}
else
{
lbuf->search_nee = g_utf8_casefold (sstr, strlen (sstr));
}
lbuf->search_lnee = strlen (lbuf->search_nee);
} }
lbuf->search_flags = flags;
gtk_xtext_lastlog (lastlog_sess->res->buffer, sess->res->buffer, lbuf->search_text = strdup (sstr);
(void *) lastlog_regex_cmp, search_re); gtk_xtext_lastlog (lbuf, buf);
g_regex_unref (search_re);
} }
void void

View File

@ -1235,7 +1235,7 @@ menu_search_prev ()
xtext_buffer *buf = xtext->buffer; xtext_buffer *buf = xtext->buffer;
if (!gtk_xtext_search(xtext, buf->search_text, if (!gtk_xtext_search(xtext, buf->search_text,
(buf->search_flags & (case_match | follow | regexp) | backward), NULL)) (buf->search_flags & (case_match | follow | regexp)) | backward, NULL))
{ {
fe_message (_("Search hit end, not found."), FE_MSG_ERROR); fe_message (_("Search hit end, not found."), FE_MSG_ERROR);
} }

View File

@ -183,7 +183,8 @@ gtk_xtext_strip_color (unsigned char *text, int len, unsigned char *outbuf,
static gboolean gtk_xtext_check_ent_visibility (GtkXText * xtext, textentry *find_ent, int add); static gboolean gtk_xtext_check_ent_visibility (GtkXText * xtext, textentry *find_ent, int add);
static int gtk_xtext_render_page_timeout (GtkXText * xtext); static int gtk_xtext_render_page_timeout (GtkXText * xtext);
static int gtk_xtext_search_offset (xtext_buffer *buf, textentry *ent, unsigned int off); static int gtk_xtext_search_offset (xtext_buffer *buf, textentry *ent, unsigned int off);
static void gtk_xtext_search_textentry (xtext_buffer *, textentry *, int); static GList * gtk_xtext_search_textentry (xtext_buffer *, textentry *);
static void gtk_xtext_search_textentry_add (xtext_buffer *, textentry *, GList *, gboolean);
static void gtk_xtext_search_textentry_del (xtext_buffer *, textentry *); static void gtk_xtext_search_textentry_del (xtext_buffer *, textentry *);
static void gtk_xtext_search_textentry_fini (gpointer, gpointer); static void gtk_xtext_search_textentry_fini (gpointer, gpointer);
static void gtk_xtext_search_fini (xtext_buffer *); static void gtk_xtext_search_fini (xtext_buffer *);
@ -5226,8 +5227,8 @@ gtk_xtext_unstrip_color (gint start, gint end, GSList *slp, GList **gl, gint max
} }
/* Search a single textentry for occurrence(s) of search arg string */ /* Search a single textentry for occurrence(s) of search arg string */
static void static GList *
gtk_xtext_search_textentry (xtext_buffer *buf, textentry *ent, int pre) gtk_xtext_search_textentry (xtext_buffer *buf, textentry *ent)
{ {
gchar *str; /* text string to be searched */ gchar *str; /* text string to be searched */
GList *gl = NULL; GList *gl = NULL;
@ -5236,7 +5237,7 @@ gtk_xtext_search_textentry (xtext_buffer *buf, textentry *ent, int pre)
if (buf->search_text == NULL) if (buf->search_text == NULL)
{ {
return; return gl;
} }
str = gtk_xtext_strip_color (ent->str, ent->str_len, buf->xtext->scratch_buffer, str = gtk_xtext_strip_color (ent->str, ent->str_len, buf->xtext->scratch_buffer,
@ -5250,7 +5251,7 @@ gtk_xtext_search_textentry (xtext_buffer *buf, textentry *ent, int pre)
if (buf->search_re == NULL) if (buf->search_re == NULL)
{ {
return; return gl;
} }
g_regex_match (buf->search_re, str, 0, &gmi); g_regex_match (buf->search_re, str, 0, &gmi);
while (g_match_info_matches (gmi)) while (g_match_info_matches (gmi))
@ -5289,6 +5290,13 @@ gtk_xtext_search_textentry (xtext_buffer *buf, textentry *ent, int pre)
/* Common processing --- */ /* Common processing --- */
g_slist_free (slp); g_slist_free (slp);
return gl;
}
/* Add a list of found search results to an entry, maybe NULL */
static void
gtk_xtext_search_textentry_add (xtext_buffer *buf, textentry *ent, GList *gl, gboolean pre)
{
ent->marks = gl; ent->marks = gl;
if (gl) if (gl)
{ {
@ -5298,7 +5306,6 @@ gtk_xtext_search_textentry (xtext_buffer *buf, textentry *ent, int pre)
buf->hintsearch = ent; buf->hintsearch = ent;
} }
} }
return;
} }
/* Free all search information for a textentry */ /* Free all search information for a textentry */
@ -5424,7 +5431,10 @@ gtk_xtext_search (GtkXText * xtext, const gchar *text, gtk_xtext_search_flags fl
ent = gl? gl->data: buf->text_first; ent = gl? gl->data: buf->text_first;
for (; ent; ent = ent->next) for (; ent; ent = ent->next)
{ {
gtk_xtext_search_textentry (buf, ent, FALSE); GList *gl;
gl = gtk_xtext_search_textentry (buf, ent);
gtk_xtext_search_textentry_add (buf, ent, gl, FALSE);
} }
} }
buf->search_flags = flags; buf->search_flags = flags;
@ -5448,7 +5458,10 @@ gtk_xtext_search (GtkXText * xtext, const gchar *text, gtk_xtext_search_flags fl
} }
for (ent = buf->text_first; ent; ent = ent->next) for (ent = buf->text_first; ent; ent = ent->next)
{ {
gtk_xtext_search_textentry (buf, ent, TRUE); GList *gl;
gl = gtk_xtext_search_textentry (buf, ent);
gtk_xtext_search_textentry_add (buf, ent, gl, TRUE);
} }
buf->search_found = g_list_reverse (buf->search_found); buf->search_found = g_list_reverse (buf->search_found);
} }
@ -5661,7 +5674,10 @@ gtk_xtext_append_entry (xtext_buffer *buf, textentry * ent, time_t stamp)
} }
if (buf->search_flags & follow) if (buf->search_flags & follow)
{ {
gtk_xtext_search_textentry (buf, ent, FALSE); GList *gl;
gl = gtk_xtext_search_textentry (buf, ent);
gtk_xtext_search_textentry_add (buf, ent, gl, FALSE);
} }
} }
@ -5764,30 +5780,41 @@ gtk_xtext_is_empty (xtext_buffer *buf)
return buf->text_first == NULL; return buf->text_first == NULL;
} }
int int
gtk_xtext_lastlog (xtext_buffer *out, xtext_buffer *search_area, gtk_xtext_lastlog (xtext_buffer *out, xtext_buffer *search_area)
int (*cmp_func) (char *, void *), void *userdata)
{ {
textentry *ent = search_area->text_first; textentry *ent;
int matches = 0; int matches;
GList *gl;
ent = search_area->text_first;
matches = 0;
while (ent) while (ent)
{ {
if (cmp_func (ent->str, userdata)) gl = gtk_xtext_search_textentry (out, ent);
if (gl)
{ {
matches++; matches++;
/* copy the text over */ /* copy the text over */
if (search_area->xtext->auto_indent) if (search_area->xtext->auto_indent)
{
gtk_xtext_append_indent (out, ent->str, ent->left_len, gtk_xtext_append_indent (out, ent->str, ent->left_len,
ent->str + ent->left_len + 1, ent->str + ent->left_len + 1,
ent->str_len - ent->left_len - 1, 0); ent->str_len - ent->left_len - 1, 0);
}
else else
{
gtk_xtext_append (out, ent->str, ent->str_len); gtk_xtext_append (out, ent->str, ent->str_len);
/* copy the timestamp over */ }
out->text_last->stamp = ent->stamp; out->text_last->stamp = ent->stamp;
gtk_xtext_search_textentry_add (out, out->text_last, gl, TRUE);
} }
ent = ent->next; ent = ent->next;
} }
out->search_found = g_list_reverse (out->search_found);
return matches; return matches;
} }

View File

@ -43,13 +43,6 @@
typedef struct _GtkXText GtkXText; typedef struct _GtkXText GtkXText;
typedef struct _GtkXTextClass GtkXTextClass; typedef struct _GtkXTextClass GtkXTextClass;
typedef struct textentry textentry; typedef struct textentry textentry;
typedef enum gtk_xtext_search_flags_e {
case_match = 1,
backward = 2,
highlight = 4,
follow = 8,
regexp = 16
} gtk_xtext_search_flags;
typedef struct { typedef struct {
GtkXText *xtext; /* attached to this widget */ GtkXText *xtext; /* attached to this widget */
@ -263,7 +256,7 @@ void gtk_xtext_set_palette (GtkXText * xtext, GdkColor palette[]);
void gtk_xtext_clear (xtext_buffer *buf, int lines); void gtk_xtext_clear (xtext_buffer *buf, int lines);
void gtk_xtext_save (GtkXText * xtext, int fh); void gtk_xtext_save (GtkXText * xtext, int fh);
void gtk_xtext_refresh (GtkXText * xtext, int do_trans); void gtk_xtext_refresh (GtkXText * xtext, int do_trans);
int gtk_xtext_lastlog (xtext_buffer *out, xtext_buffer *search_area, int (*cmp_func) (char *, void *userdata), void *userdata); int gtk_xtext_lastlog (xtext_buffer *out, xtext_buffer *search_area);
textentry *gtk_xtext_search (GtkXText * xtext, const gchar *text, gtk_xtext_search_flags flags, GError **err); textentry *gtk_xtext_search (GtkXText * xtext, const gchar *text, gtk_xtext_search_flags flags, GError **err);
void gtk_xtext_reset_marker_pos (GtkXText *xtext); void gtk_xtext_reset_marker_pos (GtkXText *xtext);
void gtk_xtext_check_marker_visibility(GtkXText *xtext); void gtk_xtext_check_marker_visibility(GtkXText *xtext);