Index: filterelement.c
===================================================================
--- filterelement.c	(revision d43edd2156ec9694ba4cec1c13baba690ce46b19)
+++ filterelement.c	(revision 41c9a9675faf455c3c6adfbc629faf55ddbfe10f)
@@ -61,5 +61,5 @@
 {
   char * val = owl_filterelement_get_field(m, fe->field);
-  return !owl_regex_compare(&(fe->re), val);
+  return !owl_regex_compare(&(fe->re), val, NULL, NULL);
 }
 
Index: fmtext.c
===================================================================
--- fmtext.c	(revision 4d86e066c8f1fe00bd18a85523b809ba4c968947)
+++ fmtext.c	(revision 41c9a9675faf455c3c6adfbc629faf55ddbfe10f)
@@ -285,5 +285,4 @@
   char attr;
   short fg, bg, pair;
-  int search_results, search_len;
   
   if (w==NULL) {
@@ -292,10 +291,4 @@
   }
 
-  search_results = (do_search
-		    ? owl_fmtext_search(f, owl_global_get_search_string(&g))
-		    : 0);
-  search_len = (search_results
-		? strlen(owl_global_get_search_string(&g))
-		: 0);
   s = f->textbuff;
   /* Set default attributes. */
@@ -316,29 +309,28 @@
       tmp = p[0];
       p[0] = '\0';
-      if (search_results) {
+      if (owl_global_is_search_active(&g)) {
 	/* Search is active, so highlight search results. */
-	char tmp2, *ss;
-	ss = stristr(s, owl_global_get_search_string(&g));
-	while (ss) {
+	char tmp2;
+	int start, end;
+	while (owl_regex_compare(owl_global_get_search_re(&g), s, &start, &end) == 0) {
 	  /* Found search string, highlight it. */
 
-	  tmp2 = ss[0];
-	  ss[0] = '\0';
+	  tmp2 = s[start];
+	  s[start] = '\0';
 	  waddstr(w, s);
-	  ss[0] = tmp2;
+	  s[start] = tmp2;
 
 	  _owl_fmtext_wattrset(w, attr ^ OWL_FMTEXT_ATTR_REVERSE);
 	  _owl_fmtext_wcolor_set(w, pair);
 	  
-	  tmp2 = ss[search_len];
-	  ss[search_len] = '\0';
-	  waddstr(w, ss);
-	  ss[search_len] = tmp2;
+	  tmp2 = s[end];
+	  s[end] = '\0';
+	  waddstr(w, s + start);
+	  s[end] = tmp2;
 
 	  _owl_fmtext_wattrset(w, attr);
 	  _owl_fmtext_wcolor_set(w, pair);
 
-	  s = ss + search_len;
-	  ss = stristr(s, owl_global_get_search_string(&g));
+	  s += end;
 	}
       }
@@ -563,7 +555,7 @@
  *  insensitive search.
  */
-int owl_fmtext_search(owl_fmtext *f, char *string)
-{
-  if (stristr(f->textbuff, string)) return(1);
+int owl_fmtext_search(owl_fmtext *f, owl_regex *re)
+{
+  if (owl_regex_compare(re, f->textbuff, NULL, NULL) == 0) return(1);
   return(0);
 }
Index: functions.c
===================================================================
--- functions.c	(revision ab225e0ba13fd89d65bc7475778551aa3b5e26c5)
+++ functions.c	(revision 41c9a9675faf455c3c6adfbc629faf55ddbfe10f)
@@ -2967,5 +2967,12 @@
   /* direction is OWL_DIRECTION_DOWNWARDS or OWL_DIRECTION_UPWARDS or
    * OWL_DIRECTION_NONE */
-  owl_global_set_search_string(&g, string);
+  owl_regex re;
+
+  if (string && owl_regex_create_quoted(&re, string) == 0) {
+    owl_global_set_search_re(&g, &re);
+    owl_regex_free(&re);
+  } else {
+    owl_global_set_search_re(&g, NULL);
+  }
 
   if (direction == OWL_DIRECTION_NONE)
@@ -3020,5 +3027,5 @@
   for (i=start; i<viewsize && i>=0;) {
     m=owl_view_get_element(v, i);
-    if (owl_message_search(m, owl_global_get_search_string(&g))) {
+    if (owl_message_search(m, owl_global_get_search_re(&g))) {
       owl_global_set_curmsg(&g, i);
       owl_function_calculate_topmsg(direction);
Index: global.c
===================================================================
--- global.c	(revision 898eb153615509c2b7ed83da752450933132864b)
+++ global.c	(revision 41c9a9675faf455c3c6adfbc629faf55ddbfe10f)
@@ -65,5 +65,5 @@
   owl_fmtext_init_colorpair_mgr(&(g->cpmgr));
   g->debug=OWL_DEBUG;
-  g->searchstring=NULL;
+  owl_regex_init(&g->search_re);
   g->starttime=time(NULL); /* assumes we call init only a start time */
   g->lastinputtime=g->starttime;
@@ -708,16 +708,19 @@
 
 int owl_global_is_search_active(owl_global *g) {
-  if (g->searchstring != NULL) return(1);
-  return(0);
-}
-
-void owl_global_set_search_string(owl_global *g, char *string) {
-  if (g->searchstring != NULL) owl_free(g->searchstring);
-  g->searchstring = string ? owl_strdup(string) : NULL;
-}
-
-char *owl_global_get_search_string(owl_global *g) {
-  if (g->searchstring==NULL) return("");
-  return(g->searchstring);
+  if (owl_regex_is_set(&g->search_re)) return(1);
+  return(0);
+}
+
+void owl_global_set_search_re(owl_global *g, owl_regex *re) {
+  if (owl_regex_is_set(&g->search_re)) {
+    owl_regex_free(&g->search_re);
+    owl_regex_init(&g->search_re);
+  }
+  if (re != NULL)
+    owl_regex_copy(re, &g->search_re);
+}
+
+owl_regex *owl_global_get_search_re(owl_global *g) {
+  return &g->search_re;
 }
 
Index: message.c
===================================================================
--- message.c	(revision 4d86e066c8f1fe00bd18a85523b809ba4c968947)
+++ message.c	(revision 41c9a9675faf455c3c6adfbc629faf55ddbfe10f)
@@ -673,10 +673,10 @@
  * case insensitive because the functions it uses are
  */
-int owl_message_search(owl_message *m, char *string)
+int owl_message_search(owl_message *m, owl_regex *re)
 {
 
   owl_message_format(m); /* is this necessary? */
   
-  return (owl_fmtext_search(&(m->fmtext->fmtext), string));
+  return (owl_fmtext_search(&(m->fmtext->fmtext), re));
 }
 
Index: owl.h
===================================================================
--- owl.h	(revision 898eb153615509c2b7ed83da752450933132864b)
+++ owl.h	(revision 41c9a9675faf455c3c6adfbc629faf55ddbfe10f)
@@ -547,5 +547,5 @@
   int newmsgproc_pid;
   int malloced, freed;
-  char *searchstring;
+  owl_regex search_re;
   aim_session_t aimsess;
   aim_conn_t bosconn;
Index: regex.c
===================================================================
--- regex.c	(revision d43edd2156ec9694ba4cec1c13baba690ce46b19)
+++ regex.c	(revision 41c9a9675faf455c3c6adfbc629faf55ddbfe10f)
@@ -46,7 +46,8 @@
 }
 
-int owl_regex_compare(owl_regex *re, char *string)
+int owl_regex_compare(owl_regex *re, char *string, int *start, int *end)
 {
   int out, ret;
+  regmatch_t match;
 
   /* if the regex is not set we match */
@@ -55,9 +56,13 @@
   }
   
-  ret=regexec(&(re->re), string, 0, NULL, 0);
+  ret=regexec(&(re->re), string, 1, &match, 0);
   out=ret;
   if (re->negate) {
     out=!out;
+    match.rm_so = 0;
+    match.rm_eo = strlen(string);
   }
+  if (start != NULL) *start = match.rm_so;
+  if (end != NULL) *end = match.rm_eo;
   return(out);
 }
@@ -76,7 +81,5 @@
 void owl_regex_copy(owl_regex *a, owl_regex *b)
 {
-  b->negate=a->negate;
-  b->string=owl_strdup(a->string);
-  memcpy(&(b->re), &(a->re), sizeof(regex_t));
+  owl_regex_create(b, a->string);
 }
 
