Index: fmtext.c
===================================================================
--- fmtext.c	(revision bd3f2322e69e72e9760505782651c29325f28b4b)
+++ fmtext.c	(revision 12c35dfd9f7df5c95af719f99063f7e2abe07b6e)
@@ -122,4 +122,315 @@
   }
 }
+
+/* Internal function.  Append text from 'in' between index 'start' and
+ * 'stop' to the end of 'f'
+ */
+void _owl_fmtext_append_fmtext(owl_fmtext *f, owl_fmtext *in, int start, int stop)
+{
+  int newlen, i;
+
+  newlen=strlen(f->textbuff)+(stop-start+1);
+  f->textbuff=owl_realloc(f->textbuff, newlen+1);
+  f->fmbuff=owl_realloc(f->fmbuff, newlen+1);
+  f->colorbuff=owl_realloc(f->colorbuff, newlen+1);
+
+  strncat(f->textbuff, in->textbuff+start, stop-start+1);
+  f->textbuff[newlen]='\0';
+  for (i=start; i<=stop; i++) {
+    f->fmbuff[f->textlen+(i-start)]=in->fmbuff[i];
+    f->colorbuff[f->textlen+(i-start)]=in->colorbuff[i];
+  }
+  f->textlen=newlen;
+}
+
+/* append fmtext 'in' to 'f' */
+void owl_fmtext_append_fmtext(owl_fmtext *f, owl_fmtext *in)
+{
+  _owl_fmtext_append_fmtext(f, in, 0, in->textlen);
+
+}
+
+/* Append 'nspaces' number of spaces to the end of 'f' */
+void owl_fmtext_append_spaces(owl_fmtext *f, int nspaces)
+{
+  int i;
+  for (i=0; i<nspaces; i++) {
+    owl_fmtext_append_normal(f, " ");
+  }
+}
+
+/* requires that the list values are strings or NULL.
+ * joins the elements together with join_with. 
+ * If format_fn is specified, passes it the list element value
+ * and it will return a string which this needs to free. */
+void owl_fmtext_append_list(owl_fmtext *f, owl_list *l, char *join_with, char *(format_fn)(void*))
+{
+  int i, size;
+  void *elem;
+  char *text;
+
+  size = owl_list_get_size(l);
+  for (i=0; i<size; i++) {
+    elem = (char*)owl_list_get_element(l,i);
+    if (elem && format_fn) {
+      text = format_fn(elem);
+      if (text) {
+	owl_fmtext_append_normal(f, text);
+	owl_free(text);
+      }
+    } else if (elem) {
+      owl_fmtext_append_normal(f, elem);
+    }
+    if ((i < size-1) && join_with) {
+      owl_fmtext_append_normal(f, join_with);
+    }
+  }
+}
+
+/* Return a plain version of the fmtext.  Caller is responsible for
+ * freeing the return
+ */
+char *owl_fmtext_print_plain(owl_fmtext *f)
+{
+  return(owl_strdup(f->textbuff));
+}
+
+/* add the formatted text to the curses window 'w'.  The window 'w'
+ * must already be initiatlized with curses
+ */
+void owl_fmtext_curs_waddstr(owl_fmtext *f, WINDOW *w)
+{
+  char *tmpbuff;
+  int position, trans1, trans2, len, lastsame;
+
+  if (w==NULL) {
+    owl_function_debugmsg("Hit a null window in owl_fmtext_curs_waddstr.");
+    return;
+  }
+
+  tmpbuff=owl_malloc(f->textlen+10);
+
+  position=0;
+  len=f->textlen;
+  while (position<=len) {
+    /* find the last char with the current format and color */
+    trans1=owl_util_find_trans(f->fmbuff+position, len-position);
+    trans2=owl_util_find_trans(f->colorbuff+position, len-position);
+
+    if (trans1<trans2) {
+      lastsame=position+trans1;
+    } else {
+      lastsame=position+trans2;
+    }
+
+    /* set the format */
+    wattrset(w, A_NORMAL);
+    if (f->fmbuff[position] & OWL_FMTEXT_ATTR_BOLD) {
+      wattron(w, A_BOLD);
+    }
+    if (f->fmbuff[position] & OWL_FMTEXT_ATTR_REVERSE) {
+      wattron(w, A_REVERSE);
+    }
+    if (f->fmbuff[position] & OWL_FMTEXT_ATTR_UNDERLINE) {
+      wattron(w, A_UNDERLINE);
+    }
+
+    /* set the color */
+    /* warning, this is sort of a hack */
+    if (owl_global_get_hascolors(&g)) {
+      if (f->colorbuff[position]!=OWL_COLOR_DEFAULT) {
+	wattron(w, COLOR_PAIR(f->colorbuff[position]));
+      }
+    }
+
+    /* add the text */
+    strncpy(tmpbuff, f->textbuff + position, lastsame-position+1);
+    tmpbuff[lastsame-position+1]='\0';
+    waddstr(w, tmpbuff);
+
+    position=lastsame+1;
+  }
+  owl_free(tmpbuff);
+}
+
+
+/* start with line 'aline' (where the first line is 0) and print
+ * 'lines' number of lines into 'out'
+ */
+int owl_fmtext_truncate_lines(owl_fmtext *in, int aline, int lines, owl_fmtext *out)
+{
+  char *ptr1, *ptr2;
+  int i, offset;
+  
+  /* find the starting line */
+  ptr1=in->textbuff;
+  if (aline!=0) {
+    for (i=0; i<aline; i++) {
+      ptr1=strchr(ptr1, '\n');
+      if (!ptr1) return(-1);
+      ptr1++;
+    }
+  }
+  /* ptr1 now holds the starting point */
+
+  /* copy in the next 'lines' lines */
+  if (lines<1) return(-1);
+
+  for (i=0; i<lines; i++) {
+    offset=ptr1-in->textbuff;
+    ptr2=strchr(ptr1, '\n');
+    if (!ptr2) {
+      _owl_fmtext_append_fmtext(out, in, offset, (in->textlen)-1);
+      return(-1);
+    }
+    _owl_fmtext_append_fmtext(out, in, offset, (ptr2-ptr1)+offset);
+    ptr1=ptr2+1;
+  }
+  return(0);
+}
+
+/* Truncate the message so that each line begins at column 'acol' and
+ * ends at 'bcol' or sooner.  The first column is number 0.  The new
+ * message is placed in 'out'.  The message is * expected to end in a
+ * new line for now
+ */
+void owl_fmtext_truncate_cols(owl_fmtext *in, int acol, int bcol, owl_fmtext *out)
+{
+  char *ptr1, *ptr2, *last;
+  int len, offset;
+
+  last=in->textbuff+in->textlen-1;
+  ptr1=in->textbuff;
+  while (ptr1<=last) {
+    ptr2=strchr(ptr1, '\n');
+    if (!ptr2) {
+      /* but this shouldn't happen if we end in a \n */
+      break;
+    }
+    
+    if (ptr2==ptr1) {
+      owl_fmtext_append_normal(out, "\n");
+      ptr1++;
+      continue;
+    }
+
+    /* we need to check that we won't run over here */
+    len=bcol-acol;
+    if (len > (ptr2-(ptr1+acol))) {
+      /* the whole line fits with room to spare, don't take a full 'len' */
+      len=ptr2-(ptr1+acol);
+    }
+    if (len>last-ptr1) {
+      /* the whole rest of the text fits with room to spare, adjust for it */
+      len-=(last-ptr1);
+    }
+    if (len<=0) {
+      /* saftey check */
+      owl_fmtext_append_normal(out, "\n");
+      ptr1=ptr2+1;
+      continue;
+    }
+
+    offset=ptr1-in->textbuff;
+    _owl_fmtext_append_fmtext(out, in, offset+acol, offset+acol+len);
+
+    ptr1=ptr2+1;
+  }
+}
+
+/* Return the number of lines in 'f' */
+int owl_fmtext_num_lines(owl_fmtext *f)
+{
+  int lines, i;
+
+  if (f->textlen==0) return(0);
+  
+  lines=0;
+  for (i=0; i<f->textlen; i++) {
+    if (f->textbuff[i]=='\n') lines++;
+  }
+
+  /* if the last char wasn't a \n there's one more line */
+  if (f->textbuff[i-1]!='\n') lines++;
+
+  return(lines);
+}
+
+char *owl_fmtext_get_text(owl_fmtext *f)
+{
+  return(f->textbuff);
+}
+
+void owl_fmtext_set_char(owl_fmtext *f, int index, int ch)
+{
+  /* set the charater at 'index' to be 'char'.  If index is out of
+   * bounds don't do anything */
+  if ((index < 0) || (index > f->textlen-1)) return;
+  f->textbuff[index]=ch;
+}
+
+/* Free all memory allocated by the object */
+void owl_fmtext_free(owl_fmtext *f)
+{
+  if (f->textbuff) owl_free(f->textbuff);
+  if (f->fmbuff) owl_free(f->fmbuff);
+  if (f->colorbuff) owl_free(f->colorbuff);
+}
+
+/* Make a copy of the fmtext 'src' into 'dst' */
+void owl_fmtext_copy(owl_fmtext *dst, owl_fmtext *src)
+{
+  int mallocsize;
+
+  if (src->textlen==0) {
+    mallocsize=5;
+  } else {
+    mallocsize=src->textlen+2;
+  }
+  dst->textlen=src->textlen;
+  dst->textbuff=owl_malloc(mallocsize);
+  dst->fmbuff=owl_malloc(mallocsize);
+  dst->colorbuff=owl_malloc(mallocsize);
+  memcpy(dst->textbuff, src->textbuff, src->textlen+1);
+  memcpy(dst->fmbuff, src->fmbuff, src->textlen);
+  memcpy(dst->colorbuff, src->colorbuff, src->textlen);
+}
+
+/* highlight all instances of "string".  Return the number of
+ * instances found.  This is a case insensitive search.
+ */
+int owl_fmtext_search_and_highlight(owl_fmtext *f, char *string)
+{
+
+  int found, len;
+  char *ptr1, *ptr2;
+
+  len=strlen(string);
+  found=0;
+  ptr1=f->textbuff;
+  while (ptr1-f->textbuff <= f->textlen) {
+    ptr2=stristr(ptr1, string);
+    if (!ptr2) return(found);
+
+    found++;
+    _owl_fmtext_add_attr(f, OWL_FMTEXT_ATTR_REVERSE,
+			 ptr2 - f->textbuff,
+			 ptr2 - f->textbuff + len - 1);
+
+    ptr1=ptr2+len;
+  }
+  return(found);
+}
+
+/* return 1 if the string is found, 0 if not.  This is a case
+ *  insensitive search.
+ */
+int owl_fmtext_search(owl_fmtext *f, char *string)
+{
+
+  if (stristr(f->textbuff, string)) return(1);
+  return(0);
+}
+
 
 /* Append the text 'text' to 'f' and interpret the zephyr style
@@ -311,312 +622,2 @@
   }
 }
-
-/* Internal function.  Append text from 'in' between index 'start' and
- * 'stop' to the end of 'f'
- */
-void _owl_fmtext_append_fmtext(owl_fmtext *f, owl_fmtext *in, int start, int stop)
-{
-  int newlen, i;
-
-  newlen=strlen(f->textbuff)+(stop-start+1);
-  f->textbuff=owl_realloc(f->textbuff, newlen+1);
-  f->fmbuff=owl_realloc(f->fmbuff, newlen+1);
-  f->colorbuff=owl_realloc(f->colorbuff, newlen+1);
-
-  strncat(f->textbuff, in->textbuff+start, stop-start+1);
-  f->textbuff[newlen]='\0';
-  for (i=start; i<=stop; i++) {
-    f->fmbuff[f->textlen+(i-start)]=in->fmbuff[i];
-    f->colorbuff[f->textlen+(i-start)]=in->colorbuff[i];
-  }
-  f->textlen=newlen;
-}
-
-/* append fmtext 'in' to 'f' */
-void owl_fmtext_append_fmtext(owl_fmtext *f, owl_fmtext *in)
-{
-  _owl_fmtext_append_fmtext(f, in, 0, in->textlen);
-
-}
-
-/* Append 'nspaces' number of spaces to the end of 'f' */
-void owl_fmtext_append_spaces(owl_fmtext *f, int nspaces)
-{
-  int i;
-  for (i=0; i<nspaces; i++) {
-    owl_fmtext_append_normal(f, " ");
-  }
-}
-
-/* requires that the list values are strings or NULL.
- * joins the elements together with join_with. 
- * If format_fn is specified, passes it the list element value
- * and it will return a string which this needs to free. */
-void owl_fmtext_append_list(owl_fmtext *f, owl_list *l, char *join_with, char *(format_fn)(void*))
-{
-  int i, size;
-  void *elem;
-  char *text;
-
-  size = owl_list_get_size(l);
-  for (i=0; i<size; i++) {
-    elem = (char*)owl_list_get_element(l,i);
-    if (elem && format_fn) {
-      text = format_fn(elem);
-      if (text) {
-	owl_fmtext_append_normal(f, text);
-	owl_free(text);
-      }
-    } else if (elem) {
-      owl_fmtext_append_normal(f, elem);
-    }
-    if ((i < size-1) && join_with) {
-      owl_fmtext_append_normal(f, join_with);
-    }
-  }
-}
-
-/* Return a plain version of the fmtext.  Caller is responsible for
- * freeing the return
- */
-char *owl_fmtext_print_plain(owl_fmtext *f)
-{
-  return(owl_strdup(f->textbuff));
-}
-
-/* add the formatted text to the curses window 'w'.  The window 'w'
- * must already be initiatlized with curses
- */
-void owl_fmtext_curs_waddstr(owl_fmtext *f, WINDOW *w)
-{
-  char *tmpbuff;
-  int position, trans1, trans2, len, lastsame;
-
-  if (w==NULL) {
-    owl_function_debugmsg("Hit a null window in owl_fmtext_curs_waddstr.");
-    return;
-  }
-
-  tmpbuff=owl_malloc(f->textlen+10);
-
-  position=0;
-  len=f->textlen;
-  while (position<=len) {
-    /* find the last char with the current format and color */
-    trans1=owl_util_find_trans(f->fmbuff+position, len-position);
-    trans2=owl_util_find_trans(f->colorbuff+position, len-position);
-
-    if (trans1<trans2) {
-      lastsame=position+trans1;
-    } else {
-      lastsame=position+trans2;
-    }
-
-    /* set the format */
-    wattrset(w, A_NORMAL);
-    if (f->fmbuff[position] & OWL_FMTEXT_ATTR_BOLD) {
-      wattron(w, A_BOLD);
-    }
-    if (f->fmbuff[position] & OWL_FMTEXT_ATTR_REVERSE) {
-      wattron(w, A_REVERSE);
-    }
-    if (f->fmbuff[position] & OWL_FMTEXT_ATTR_UNDERLINE) {
-      wattron(w, A_UNDERLINE);
-    }
-
-    /* set the color */
-    /* warning, this is sort of a hack */
-    if (owl_global_get_hascolors(&g)) {
-      if (f->colorbuff[position]!=OWL_COLOR_DEFAULT) {
-	wattron(w, COLOR_PAIR(f->colorbuff[position]));
-      }
-    }
-
-    /* add the text */
-    strncpy(tmpbuff, f->textbuff + position, lastsame-position+1);
-    tmpbuff[lastsame-position+1]='\0';
-    waddstr(w, tmpbuff);
-
-    position=lastsame+1;
-  }
-  owl_free(tmpbuff);
-}
-
-
-/* start with line 'aline' (where the first line is 0) and print
- * 'lines' number of lines into 'out'
- */
-int owl_fmtext_truncate_lines(owl_fmtext *in, int aline, int lines, owl_fmtext *out)
-{
-  char *ptr1, *ptr2;
-  int i, offset;
-  
-  /* find the starting line */
-  ptr1=in->textbuff;
-  if (aline!=0) {
-    for (i=0; i<aline; i++) {
-      ptr1=strchr(ptr1, '\n');
-      if (!ptr1) return(-1);
-      ptr1++;
-    }
-  }
-  /* ptr1 now holds the starting point */
-
-  /* copy in the next 'lines' lines */
-  if (lines<1) return(-1);
-
-  for (i=0; i<lines; i++) {
-    offset=ptr1-in->textbuff;
-    ptr2=strchr(ptr1, '\n');
-    if (!ptr2) {
-      _owl_fmtext_append_fmtext(out, in, offset, (in->textlen)-1);
-      return(-1);
-    }
-    _owl_fmtext_append_fmtext(out, in, offset, (ptr2-ptr1)+offset);
-    ptr1=ptr2+1;
-  }
-  return(0);
-}
-
-/* Truncate the message so that each line begins at column 'acol' and
- * ends at 'bcol' or sooner.  The first column is number 0.  The new
- * message is placed in 'out'.  The message is * expected to end in a
- * new line for now
- */
-void owl_fmtext_truncate_cols(owl_fmtext *in, int acol, int bcol, owl_fmtext *out)
-{
-  char *ptr1, *ptr2, *last;
-  int len, offset;
-
-  last=in->textbuff+in->textlen-1;
-  ptr1=in->textbuff;
-  while (ptr1<=last) {
-    ptr2=strchr(ptr1, '\n');
-    if (!ptr2) {
-      /* but this shouldn't happen if we end in a \n */
-      break;
-    }
-    
-    if (ptr2==ptr1) {
-      owl_fmtext_append_normal(out, "\n");
-      ptr1++;
-      continue;
-    }
-
-    /* we need to check that we won't run over here */
-    len=bcol-acol;
-    if (len > (ptr2-(ptr1+acol))) {
-      /* the whole line fits with room to spare, don't take a full 'len' */
-      len=ptr2-(ptr1+acol);
-    }
-    if (len>last-ptr1) {
-      /* the whole rest of the text fits with room to spare, adjust for it */
-      len-=(last-ptr1);
-    }
-    if (len<=0) {
-      /* saftey check */
-      owl_fmtext_append_normal(out, "\n");
-      ptr1=ptr2+1;
-      continue;
-    }
-
-    offset=ptr1-in->textbuff;
-    _owl_fmtext_append_fmtext(out, in, offset+acol, offset+acol+len);
-
-    ptr1=ptr2+1;
-  }
-}
-
-/* Return the number of lines in 'f' */
-int owl_fmtext_num_lines(owl_fmtext *f)
-{
-  int lines, i;
-
-  if (f->textlen==0) return(0);
-  
-  lines=0;
-  for (i=0; i<f->textlen; i++) {
-    if (f->textbuff[i]=='\n') lines++;
-  }
-
-  /* if the last char wasn't a \n there's one more line */
-  if (f->textbuff[i-1]!='\n') lines++;
-
-  return(lines);
-}
-
-char *owl_fmtext_get_text(owl_fmtext *f)
-{
-  return(f->textbuff);
-}
-
-void owl_fmtext_set_char(owl_fmtext *f, int index, int ch)
-{
-  /* set the charater at 'index' to be 'char'.  If index is out of
-   * bounds don't do anything */
-  if ((index < 0) || (index > f->textlen-1)) return;
-  f->textbuff[index]=ch;
-}
-
-/* Free all memory allocated by the object */
-void owl_fmtext_free(owl_fmtext *f)
-{
-  if (f->textbuff) owl_free(f->textbuff);
-  if (f->fmbuff) owl_free(f->fmbuff);
-  if (f->colorbuff) owl_free(f->colorbuff);
-}
-
-/* Make a copy of the fmtext 'src' into 'dst' */
-void owl_fmtext_copy(owl_fmtext *dst, owl_fmtext *src)
-{
-  int mallocsize;
-
-  if (src->textlen==0) {
-    mallocsize=5;
-  } else {
-    mallocsize=src->textlen+2;
-  }
-  dst->textlen=src->textlen;
-  dst->textbuff=owl_malloc(mallocsize);
-  dst->fmbuff=owl_malloc(mallocsize);
-  dst->colorbuff=owl_malloc(mallocsize);
-  memcpy(dst->textbuff, src->textbuff, src->textlen+1);
-  memcpy(dst->fmbuff, src->fmbuff, src->textlen);
-  memcpy(dst->colorbuff, src->colorbuff, src->textlen);
-}
-
-/* highlight all instance of "string".  Return the number of
- * instances found.  This is a case insensitive search.
- */
-int owl_fmtext_search_and_highlight(owl_fmtext *f, char *string)
-{
-
-  int found, len;
-  char *ptr1, *ptr2;
-
-  len=strlen(string);
-  found=0;
-  ptr1=f->textbuff;
-  while (ptr1-f->textbuff <= f->textlen) {
-    ptr2=stristr(ptr1, string);
-    if (!ptr2) return(found);
-
-    found++;
-    _owl_fmtext_add_attr(f, OWL_FMTEXT_ATTR_REVERSE,
-			 ptr2 - f->textbuff,
-			 ptr2 - f->textbuff + len - 1);
-
-    ptr1=ptr2+len;
-  }
-  return(found);
-}
-
-/* return 1 if the string is found, 0 if not.  This is a case
- *  insensitive search.
- */
-int owl_fmtext_search(owl_fmtext *f, char *string)
-{
-
-  if (stristr(f->textbuff, string)) return(1);
-  return(0);
-}
