[957] in BarnOwl Developers

home help back first fref pref prev next nref lref last post

[D-O-H] r942 - branches/barnowl_sqlite/owl

daemon@ATHENA.MIT.EDU (nelhage@MIT.EDU)
Thu Oct 29 18:11:32 2009

Resent-From: nelhage@mit.edu
Resent-To: barnowl-dev-mtg@charon.mit.edu
X-Original-To: nelhage@nelhage.com
To: dirty-owl-hackers@mit.edu
From: nelhage@MIT.EDU
Reply-to: dirty-owl-hackers@MIT.EDU
Date: Sun, 17 Feb 2008 22:59:50 -0500 (EST)

Author: nelhage
Date: 2008-02-17 22:59:47 -0500 (Sun, 17 Feb 2008)
New Revision: 942

Modified:
   branches/barnowl_sqlite/owl/functions.c
   branches/barnowl_sqlite/owl/global.c
   branches/barnowl_sqlite/owl/mainwin.c
   branches/barnowl_sqlite/owl/owl.c
   branches/barnowl_sqlite/owl/owl.h
   branches/barnowl_sqlite/owl/util.c
   branches/barnowl_sqlite/owl/view.c
Log:
First pass at a iterator-based view. This is probably horribly
unstable.


Modified: branches/barnowl_sqlite/owl/functions.c
===================================================================
--- branches/barnowl_sqlite/owl/functions.c	2008-02-15 18:43:30 UTC (rev 941)
+++ branches/barnowl_sqlite/owl/functions.c	2008-02-18 03:59:47 UTC (rev 942)
@@ -547,8 +547,9 @@
  * if no matching messages are found.  */
 void owl_function_nextmsg_full(char *filter, int skip_deleted, int last_if_none)
 {
-  int curmsg, i, viewsize, found;
+  int found;
   owl_view *v;
+  owl_view_iterator it;
   owl_filter *f = NULL;
   owl_message *m;
 
@@ -562,24 +563,26 @@
     }
   }
 
-  curmsg=owl_global_get_curmsg(&g);
-  viewsize=owl_view_get_size(v);
+  owl_view_iterator_clone(&it, owl_global_get_curmsg(&g));
   found=0;
 
   /* just check to make sure we're in bounds... */
-  if (curmsg>viewsize-1) curmsg=viewsize-1;
-  if (curmsg<0) curmsg=0;
+  /* if (curmsg>viewsize-1) curmsg=viewsize-1;
+     if (curmsg<0) curmsg=0; */
 
-  for (i=curmsg+1; i<viewsize; i++) {
-    m=owl_view_get_element(v, i);
+  if(owl_view_iterator_has_next(&it))
+    owl_view_iterator_next(&it);
+  for (;owl_view_iterator_has_next(&it);
+       owl_view_iterator_next(&it)) {
+    m = owl_view_iterator_get_message(&it);
     if (skip_deleted && owl_message_is_delete(m)) continue;
     if (f && !owl_filter_message_match(f, m)) continue;
     found = 1;
     break;
   }
 
-  if (i>owl_view_get_size(v)-1) i=owl_view_get_size(v)-1;
-  if (i<0) i=0;
+  /* if (i>owl_view_get_size(v)-1) i=owl_view_get_size(v)-1;
+     if (i<0) i=0; */
 
   if (!found) {
     owl_function_makemsg("already at last%s message%s%s",
@@ -589,7 +592,7 @@
   }
 
   if (last_if_none || found) {
-    owl_global_set_curmsg(&g, i);
+    owl_global_set_curmsg(&g, &it);
     owl_function_calculate_topmsg(OWL_DIRECTION_DOWNWARDS);
     owl_mainwin_redisplay(owl_global_get_mainwin(&g));
     owl_global_set_direction_downwards(&g);
@@ -598,8 +601,9 @@
 
 void owl_function_prevmsg_full(char *filter, int skip_deleted, int first_if_none)
 {
-  int curmsg, i, viewsize, found;
+  int found;
   owl_view *v;
+  owl_view_iterator it;
   owl_filter *f = NULL;
   owl_message *m;
 
@@ -613,23 +617,23 @@
     }
   }
 
-  curmsg=owl_global_get_curmsg(&g);
-  viewsize=owl_view_get_size(v);
+  owl_view_iterator_clone(&it, owl_global_get_curmsg(&g));
   found=0;
 
   /* just check to make sure we're in bounds... */
-  if (curmsg<0) curmsg=0;
+  /* if (curmsg<0) curmsg=0; */
 
-  for (i=curmsg-1; i>=0; i--) {
-    m=owl_view_get_element(v, i);
+  if(owl_view_iterator_has_prev(&it))
+    owl_view_iterator_prev(&it);
+  for (;owl_view_iterator_has_prev(&it);
+       owl_view_iterator_prev(&it)) {
+    m = owl_view_iterator_get_message(&it);
     if (skip_deleted && owl_message_is_delete(m)) continue;
     if (f && !owl_filter_message_match(f, m)) continue;
     found = 1;
     break;
   }
 
-  if (i<0) i=0;
-
   if (!found) {
     owl_function_makemsg("already at first%s message%s%s",
 			 skip_deleted?" non-deleted":"",
@@ -638,7 +642,7 @@
   }
 
   if (first_if_none || found) {
-    owl_global_set_curmsg(&g, i);
+    owl_global_set_curmsg(&g, &it);
     owl_function_calculate_topmsg(OWL_DIRECTION_UPWARDS);
     owl_mainwin_redisplay(owl_global_get_mainwin(&g));
     owl_global_set_direction_upwards(&g);
@@ -679,20 +683,17 @@
 /* if move_after is 1, moves after the delete */
 void owl_function_deletecur(int move_after)
 {
-  int curmsg;
-  owl_view *v;
+  owl_message *m;
 
-  v=owl_global_get_current_view(&g);
+  m = owl_global_get_current_message(&g);
 
-  /* bail if there's no current message */
-  if (owl_view_get_size(v) < 1) {
+  if (m == NULL) {
     owl_function_error("No current message to delete");
     return;
   }
 
   /* mark the message for deletion */
-  curmsg=owl_global_get_curmsg(&g);
-  owl_view_delete_element(v, curmsg);
+  owl_message_mark_delete(m);
 
   if (move_after) {
     /* move the poiner in the appropriate direction 
@@ -707,26 +708,20 @@
 
 void owl_function_undeletecur(int move_after)
 {
-  int curmsg;
-  owl_view *v;
-
-  v=owl_global_get_current_view(&g);
+  owl_message *m;
   
-  if (owl_view_get_size(v) < 1) {
+  m = owl_global_get_current_message(&g);
+
+  if (m == NULL) {
     owl_function_error("No current message to undelete");
     return;
   }
-  curmsg=owl_global_get_curmsg(&g);
 
-  owl_view_undelete_element(v, curmsg);
+  owl_message_unmark_delete(m);
 
   if (move_after) {
     if (owl_global_get_direction(&g)==OWL_DIRECTION_UPWARDS) {
-      if (curmsg>0) {
-	owl_function_prevmsg();
-      } else {
-	owl_function_nextmsg();
-      }
+      owl_function_prevmsg();
     } else {
       owl_function_nextmsg();
     }
@@ -737,17 +732,16 @@
 
 void owl_function_expunge()
 {
-  int curmsg;
   owl_message *m;
   owl_messagelist *ml;
+  owl_view_iterator it;
   owl_view *v;
   int lastmsgid=0;
 
-  curmsg=owl_global_get_curmsg(&g);
   v=owl_global_get_current_view(&g);
   ml=owl_global_get_msglist(&g);
 
-  m=owl_view_get_element(v, curmsg);
+  m = owl_global_get_current_message(&g);
   if (m) lastmsgid = owl_message_get_id(m);
 
   /* expunge the message list */
@@ -758,10 +752,8 @@
 
   /* find where the new position should be
      (as close as possible to where we last where) */
-  curmsg = owl_view_get_nearest_to_msgid(v, lastmsgid);
-  if (curmsg>owl_view_get_size(v)-1) curmsg = owl_view_get_size(v)-1;
-  if (curmsg<0) curmsg = 0;
-  owl_global_set_curmsg(&g, curmsg);
+  owl_view_iterator_init_id(&it, v, lastmsgid);
+  owl_global_set_curmsg(&g, &it);
   owl_function_calculate_topmsg(OWL_DIRECTION_NONE);
   /* if there are no messages set the direction to down in case we
      delete everything upwards */
@@ -773,30 +765,23 @@
 
 void owl_function_firstmsg()
 {
-  owl_global_set_curmsg(&g, 0);
-  owl_global_set_topmsg(&g, 0);
+  owl_view_iterator it;
+  owl_view_iterator_init_start(&it, owl_global_get_current_view(&g));
+  owl_global_set_curmsg(&g, &it);
+  owl_global_set_topmsg(&g, &it);
   owl_mainwin_redisplay(owl_global_get_mainwin(&g));
   owl_global_set_direction_downwards(&g);
 }
 
 void owl_function_lastmsg_noredisplay()
 {
-  int oldcurmsg, curmsg;
   owl_view *v;
+  owl_view_iterator it;
 
   v=owl_global_get_current_view(&g);
-  oldcurmsg=owl_global_get_curmsg(&g);
-  curmsg=owl_view_get_size(v)-1;  
-  if (curmsg<0) curmsg=0;
-  owl_global_set_curmsg(&g, curmsg);
-  if (oldcurmsg < curmsg) {
-    owl_function_calculate_topmsg(OWL_DIRECTION_DOWNWARDS);
-  } else if (curmsg<owl_view_get_size(v)) {
-    /* If already at the end, blank the screen and move curmsg
-     * past the end of the messages. */
-    owl_global_set_topmsg(&g, curmsg+1);
-    owl_global_set_curmsg(&g, curmsg+1);
-  } 
+  owl_view_iterator_init_end(&it, v);
+  owl_global_set_curmsg(&g, &it);
+  owl_function_calculate_topmsg(OWL_DIRECTION_DOWNWARDS);
   owl_mainwin_redisplay(owl_global_get_mainwin(&g));
   owl_global_set_direction_downwards(&g);
 }
@@ -1100,12 +1085,13 @@
 
 void owl_function_calculate_topmsg(int direction)
 {
-  int recwinlines, topmsg, curmsg;
+  int recwinlines;
+  owl_view_iterator *curmsg, *topmsg;
   owl_view *v;
 
   v=owl_global_get_current_view(&g);
-  curmsg=owl_global_get_curmsg(&g);
-  topmsg=owl_global_get_topmsg(&g);
+  curmsg = owl_global_get_curmsg(&g);
+  topmsg = owl_global_get_topmsg(&g);
   recwinlines=owl_global_get_recwin_lines(&g);
 
   /*
@@ -1116,174 +1102,174 @@
 
   switch (owl_global_get_scrollmode(&g)) {
   case OWL_SCROLLMODE_TOP:
-    topmsg = owl_function_calculate_topmsg_top(direction, v, curmsg, topmsg, recwinlines);
+    owl_function_calculate_topmsg_top(direction, v, curmsg, topmsg, recwinlines);
     break;
   case OWL_SCROLLMODE_NEARTOP:
-    topmsg = owl_function_calculate_topmsg_neartop(direction, v, curmsg, topmsg, recwinlines);
+    owl_function_calculate_topmsg_neartop(direction, v, curmsg, topmsg, recwinlines);
     break;
   case OWL_SCROLLMODE_CENTER:
-    topmsg = owl_function_calculate_topmsg_center(direction, v, curmsg, topmsg, recwinlines);
+    owl_function_calculate_topmsg_center(direction, v, curmsg, topmsg, recwinlines);
     break;
   case OWL_SCROLLMODE_PAGED:
-    topmsg = owl_function_calculate_topmsg_paged(direction, v, curmsg, topmsg, recwinlines, 0);
+    owl_function_calculate_topmsg_paged(direction, v, curmsg, topmsg, recwinlines, 0);
     break;
   case OWL_SCROLLMODE_PAGEDCENTER:
-    topmsg = owl_function_calculate_topmsg_paged(direction, v, curmsg, topmsg, recwinlines, 1);
+    owl_function_calculate_topmsg_paged(direction, v, curmsg, topmsg, recwinlines, 1);
     break;
   case OWL_SCROLLMODE_NORMAL:
   default:
-    topmsg = owl_function_calculate_topmsg_normal(direction, v, curmsg, topmsg, recwinlines);
+    owl_function_calculate_topmsg_normal(direction, v, curmsg, topmsg, recwinlines);
   }
-  owl_function_debugmsg("Calculated a topmsg of %i", topmsg);
-  owl_global_set_topmsg(&g, topmsg);
 }
 
-/* Returns what the new topmsg should be.  
+/* Updates `topmsg' to indicate the new topmsg
  * Passed the last direction of movement, 
  * the current view,
  * the current message number in the view,
  * the top message currently being displayed,
  * and the number of lines in the recwin.
  */
-int owl_function_calculate_topmsg_top(int direction, owl_view *v, int curmsg, int topmsg, int recwinlines)
+void owl_function_calculate_topmsg_top(int direction, owl_view *v, owl_view_iterator *curmsg, owl_view_iterator *topmsg, int recwinlines)
 {
-  return(curmsg);
+  /* nop */
 }
 
-int owl_function_calculate_topmsg_neartop(int direction, owl_view *v, int curmsg, int topmsg, int recwinlines)
+void owl_function_calculate_topmsg_neartop(int direction, owl_view *v, owl_view_iterator *curmsg, owl_view_iterator *topmsg, int recwinlines)
 {
-  if (curmsg>0 
-      && (owl_message_get_numlines(owl_view_get_element(v, curmsg-1))
-	  <  recwinlines/2)) {
-    return(curmsg-1);
-  } else {
-    return(curmsg);
+  owl_view_iterator it;
+  owl_view_iterator_clone(&it, curmsg);
+  owl_view_iterator_prev(&it);
+  if (owl_message_get_numlines(owl_view_iterator_get_message(&it))
+      <  recwinlines/2) {
+    owl_view_iterator_clone(curmsg, &it);
   }
 }
   
-int owl_function_calculate_topmsg_center(int direction, owl_view *v, int curmsg, int topmsg, int recwinlines)
+void owl_function_calculate_topmsg_center(int direction, owl_view *v, owl_view_iterator *curmsg, owl_view_iterator *topmsg, int recwinlines)
 {
-  int i, last, lines;
+  int lines;
+  owl_view_iterator *it = curmsg;
 
-  last = curmsg;
   lines = 0;
-  for (i=curmsg-1; i>=0; i--) {
-    lines += owl_message_get_numlines(owl_view_get_element(v, i));
+  for (owl_view_iterator_prev(it);
+       !owl_view_iterator_is_at_start(it);
+       owl_view_iterator_prev(it)) {
+    lines += owl_message_get_numlines(owl_view_iterator_get_message(it));
     if (lines > recwinlines/2) break;
-    last = i;
   }
-  return(last);
 }
   
-int owl_function_calculate_topmsg_paged(int direction, owl_view *v, int curmsg, int topmsg, int recwinlines, int center_on_page)
+void owl_function_calculate_topmsg_paged(int direction, owl_view *v, owl_view_iterator *curmsg, owl_view_iterator *topmsg, int recwinlines, int center_on_page)
 {
-  int i, last, lines, savey;
+  owl_function_error("topmsg_paged not supported yet");
+  /* int i, last, lines, savey; */
   
-  /* If we're off the top of the screen, scroll up such that the 
-   * curmsg is near the botton of the screen. */
-  if (curmsg < topmsg) {
-    last = curmsg;
-    lines = 0;
-    for (i=curmsg; i>=0; i--) {
-      lines += owl_message_get_numlines(owl_view_get_element(v, i));
-      if (lines > recwinlines) break;
-    last = i;
-    }
-    if (center_on_page) {
-      return(owl_function_calculate_topmsg_center(direction, v, curmsg, 0, recwinlines));
-    } else {
-      return(last);
-    }
-  }
+/*   /\* If we're off the top of the screen, scroll up such that the  */
+/*    * curmsg is near the botton of the screen. *\/ */
+/*   if (curmsg < topmsg) { */
+/*     last = curmsg; */
+/*     lines = 0; */
+/*     for (i=curmsg; i>=0; i--) { */
+/*       lines += owl_message_get_numlines(owl_view_get_element(v, i)); */
+/*       if (lines > recwinlines) break; */
+/*     last = i; */
+/*     } */
+/*     if (center_on_page) { */
+/*       return(owl_function_calculate_topmsg_center(direction, v, curmsg, 0, recwinlines)); */
+/*     } else { */
+/*       return(last); */
+/*     } */
+/*   } */
 
-  /* Find number of lines from top to bottom of curmsg (store in savey) */
-  savey=0;
-  for (i=topmsg; i<=curmsg; i++) {
-    savey+=owl_message_get_numlines(owl_view_get_element(v, i));
-  }
+/*   /\* Find number of lines from top to bottom of curmsg (store in savey) *\/ */
+/*   savey=0; */
+/*   for (i=topmsg; i<=curmsg; i++) { */
+/*     savey+=owl_message_get_numlines(owl_view_get_element(v, i)); */
+/*   } */
 
-  /* if we're off the bottom of the screen, scroll down */
-  if (savey > recwinlines) {
-    if (center_on_page) {
-      return(owl_function_calculate_topmsg_center(direction, v, curmsg, 0, recwinlines));
-    } else {
-      return(curmsg);
-    }
-  }
+/*   /\* if we're off the bottom of the screen, scroll down *\/ */
+/*   if (savey > recwinlines) { */
+/*     if (center_on_page) { */
+/*       return(owl_function_calculate_topmsg_center(direction, v, curmsg, 0, recwinlines)); */
+/*     } else { */
+/*       return(curmsg); */
+/*     } */
+/*   } */
 
-  /* else just stay as we are... */
-  return(topmsg);
+/*   /\* else just stay as we are... *\/ */
+/*   return(topmsg); */
 }
 
-int owl_function_calculate_topmsg_normal(int direction, owl_view *v, int curmsg, int topmsg, int recwinlines)
+void owl_function_calculate_topmsg_normal(int direction, owl_view *v, owl_view_iterator *curmsg, owl_view_iterator *topmsg, int recwinlines)
 {
-  int savey, i, foo, y;
+  owl_function_error("topmsg_normal not supported yet");
+/*   int savey, i, foo, y; */
 
-  if (curmsg<0) return(topmsg);
+/*   if (curmsg<0) return(topmsg); */
     
-  /* If we're off the top of the screen then center */
-  if (curmsg<topmsg) {
-    topmsg=owl_function_calculate_topmsg_center(direction, v, curmsg, 0, recwinlines);
-  }
+/*   /\* If we're off the top of the screen then center *\/ */
+/*   if (curmsg<topmsg) { */
+/*     topmsg=owl_function_calculate_topmsg_center(direction, v, curmsg, 0, recwinlines); */
+/*   } */
 
-  /* If curmsg is so far past topmsg that there are more messages than
-     lines, skip the line counting that follows because we're
-     certainly off screen.  */
-  savey=curmsg-topmsg;
-  if (savey <= recwinlines) {
-    /* Find number of lines from top to bottom of curmsg (store in savey) */
-    savey = 0;
-    for (i=topmsg; i<=curmsg; i++) {
-      savey+=owl_message_get_numlines(owl_view_get_element(v, i));
-    }
-  }
+/*   /\* If curmsg is so far past topmsg that there are more messages than */
+/*      lines, skip the line counting that follows because we're */
+/*      certainly off screen.  *\/ */
+/*   savey=curmsg-topmsg; */
+/*   if (savey <= recwinlines) { */
+/*     /\* Find number of lines from top to bottom of curmsg (store in savey) *\/ */
+/*     savey = 0; */
+/*     for (i=topmsg; i<=curmsg; i++) { */
+/*       savey+=owl_message_get_numlines(owl_view_get_element(v, i)); */
+/*     } */
+/*   } */
 
-  /* If we're off the bottom of the screen, set the topmsg to curmsg
-   * and scroll upwards */
-  if (savey > recwinlines) {
-    topmsg=curmsg;
-    savey=owl_message_get_numlines(owl_view_get_element(v, curmsg));
-    direction=OWL_DIRECTION_UPWARDS;
-  }
+/*   /\* If we're off the bottom of the screen, set the topmsg to curmsg */
+/*    * and scroll upwards *\/ */
+/*   if (savey > recwinlines) { */
+/*     topmsg=curmsg; */
+/*     savey=owl_message_get_numlines(owl_view_get_element(v, curmsg)); */
+/*     direction=OWL_DIRECTION_UPWARDS; */
+/*   } */
   
-  /* If our bottom line is less than 1/4 down the screen then scroll up */
-  if (direction == OWL_DIRECTION_UPWARDS || direction == OWL_DIRECTION_NONE) {
-    if (savey < (recwinlines / 4)) {
-      y=0;
-      for (i=curmsg; i>=0; i--) {
-	foo=owl_message_get_numlines(owl_view_get_element(v, i));
-	/* will we run the curmsg off the screen? */
-	if ((foo+y) >= recwinlines) {
-	  i++;
-	  if (i>curmsg) i=curmsg;
-	  break;
-	}
-	/* have saved 1/2 the screen space? */
-	y+=foo;
-	if (y > (recwinlines / 2)) break;
-      }
-      if (i<0) i=0;
-      return(i);
-    }
-  }
+/*   /\* If our bottom line is less than 1/4 down the screen then scroll up *\/ */
+/*   if (direction == OWL_DIRECTION_UPWARDS || direction == OWL_DIRECTION_NONE) { */
+/*     if (savey < (recwinlines / 4)) { */
+/*       y=0; */
+/*       for (i=curmsg; i>=0; i--) { */
+/* 	foo=owl_message_get_numlines(owl_view_get_element(v, i)); */
+/* 	/\* will we run the curmsg off the screen? *\/ */
+/* 	if ((foo+y) >= recwinlines) { */
+/* 	  i++; */
+/* 	  if (i>curmsg) i=curmsg; */
+/* 	  break; */
+/* 	} */
+/* 	/\* have saved 1/2 the screen space? *\/ */
+/* 	y+=foo; */
+/* 	if (y > (recwinlines / 2)) break; */
+/*       } */
+/*       if (i<0) i=0; */
+/*       return(i); */
+/*     } */
+/*   } */
 
-  if (direction == OWL_DIRECTION_DOWNWARDS || direction == OWL_DIRECTION_NONE) {
-    /* If curmsg bottom line is more than 3/4 down the screen then scroll down */
-    if (savey > ((recwinlines * 3)/4)) {
-      y=0;
-      /* count lines from the top until we can save 1/2 the screen size */
-      for (i=topmsg; i<curmsg; i++) {
-	y+=owl_message_get_numlines(owl_view_get_element(v, i));
-	if (y > (recwinlines / 2)) break;
-      }
-      if (i==curmsg) {
-	i--;
-      }
-      return(i+1);
-    }
-  }
+/*   if (direction == OWL_DIRECTION_DOWNWARDS || direction == OWL_DIRECTION_NONE) { */
+/*     /\* If curmsg bottom line is more than 3/4 down the screen then scroll down *\/ */
+/*     if (savey > ((recwinlines * 3)/4)) { */
+/*       y=0; */
+/*       /\* count lines from the top until we can save 1/2 the screen size *\/ */
+/*       for (i=topmsg; i<curmsg; i++) { */
+/* 	y+=owl_message_get_numlines(owl_view_get_element(v, i)); */
+/* 	if (y > (recwinlines / 2)) break; */
+/*       } */
+/*       if (i==curmsg) { */
+/* 	i--; */
+/*       } */
+/*       return(i+1); */
+/*     } */
+/*   } */
 
-  return(topmsg);
+/*   return(topmsg); */
 }
 
 void owl_function_resize()
@@ -1675,20 +1661,20 @@
 {
   /* scroll down or up within the current message IF the message is truncated */
 
-  int offset, curmsg, lines;
+  int offset, lines;
   owl_view *v;
   owl_message *m;
 
   offset=owl_global_get_curmsg_vert_offset(&g);
   v=owl_global_get_current_view(&g);
-  curmsg=owl_global_get_curmsg(&g);
-  m=owl_view_get_element(v, curmsg);
-  if (!m || owl_view_get_size(v)==0) return;
+  m = owl_global_get_current_message(&g);
+  if (!m) return;
   lines=owl_message_get_numlines(m);
 
   if (offset==0) {
     /* Bail if the curmsg isn't the last one displayed */
-    if (curmsg != owl_mainwin_get_last_msg(owl_global_get_mainwin(&g))) {
+    if (owl_message_get_id(m) !=
+        owl_message_get_id(owl_mainwin_get_last_msg(owl_global_get_mainwin(&g)))) {
       owl_function_makemsg("The entire message is already displayed");
       return;
     }
@@ -1750,16 +1736,15 @@
 
 void owl_function_mainwin_pagedown()
 {
-  int i;
+  owl_view_iterator iter;
 
-  i=owl_mainwin_get_last_msg(owl_global_get_mainwin(&g));
-  if (i<0) return;
+  owl_view_iterator_clone(&iter, owl_mainwin_get_last_msg(owl_global_get_mainwin(&g)));
+  if (!owl_view_iterator_is_valid(&iter)) return;
   if (owl_mainwin_is_last_msg_truncated(owl_global_get_mainwin(&g))
-      && (owl_global_get_curmsg(&g) < i)
-      && (i>0)) {
-    i--;
+      && (owl_view_iterator_cmp(owl_global_get_curmsg(&g), &iter) < 0)) {
+    owl_view_iterator_prev(&iter);
   }
-  owl_global_set_curmsg(&g, i);
+  owl_global_set_curmsg(&g, &iter);
   owl_function_nextmsg();
 }
 
@@ -1873,8 +1858,9 @@
   /* mark for deletion all messages in the current view that match the
    * 'trash' filter */
 
-  int i, j, count;
+  int count;
   owl_message *m;
+  owl_view_iterator it;
   owl_view *v;
   owl_filter *f;
 
@@ -1888,9 +1874,10 @@
   v=owl_global_get_current_view(&g);
 
   count=0;
-  j=owl_view_get_size(v);
-  for (i=0; i<j; i++) {
-    m=owl_view_get_element(v, i);
+  for(owl_view_iterator_init_start(&it, v);
+      !owl_view_iterator_is_at_end(&it);
+      owl_view_iterator_next(&it)) {
+    m = owl_view_iterator_get_message(&it);
     if (owl_filter_message_match(f, m)) {
       count++;
       owl_message_mark_delete(m);
@@ -2024,139 +2011,135 @@
   owl_message *m;
   owl_filter *f;
   
-  if (owl_view_get_size(owl_global_get_current_view(&g))==0) {
+  char *class, *inst, *to, *cc=NULL;
+  
+  m = owl_global_get_current_message(&g);
+  if (!m) {
     owl_function_error("No message selected");
-  } else {
-    char *class, *inst, *to, *cc=NULL;
-    
-    m = owl_global_get_current_message(&g);
-    if (!m) {
-      owl_function_error("No message selected");
+    return;
+  }
+
+  /* first check if we catch the reply-lockout filter */
+  f=owl_global_get_filter(&g, "reply-lockout");
+  if (f) {
+    if (owl_filter_message_match(f, m)) {
+      owl_function_error("Sorry, replies to this message have been disabled by the reply-lockout filter");
       return;
     }
+  }
 
-    /* first check if we catch the reply-lockout filter */
-    f=owl_global_get_filter(&g, "reply-lockout");
-    if (f) {
-      if (owl_filter_message_match(f, m)) {
-	owl_function_error("Sorry, replies to this message have been disabled by the reply-lockout filter");
-	return;
-      }
-    }
+  /* loopback */
+  if (owl_message_is_type_loopback(m)) {
+    owl_function_loopwrite_setup();
+    return;
+  }
 
-    /* loopback */
-    if (owl_message_is_type_loopback(m)) {
-      owl_function_loopwrite_setup();
-      return;
-    }
+  /* zephyr */
+  if (owl_message_is_type_zephyr(m)) {
+    /* if it's a zephyr we sent, send it out the same way again */
+    if (owl_message_is_direction_out(m)) {
+        buff = owl_strdup(owl_message_get_zwriteline(m));
+    } else {
 
-    /* zephyr */
-    if (owl_message_is_type_zephyr(m)) {
-      /* if it's a zephyr we sent, send it out the same way again */
-      if (owl_message_is_direction_out(m)) {
-          buff = owl_strdup(owl_message_get_zwriteline(m));
+      /* Special case a personal reply to a webzephyr user on a class */
+      if ((type==1) && !strcasecmp(owl_message_get_opcode(m), OWL_WEBZEPHYR_OPCODE)) {
+        class=OWL_WEBZEPHYR_CLASS;
+        inst=owl_message_get_sender(m);
+        to=OWL_WEBZEPHYR_PRINCIPAL;
+      } else if (!strcasecmp(owl_message_get_class(m), OWL_WEBZEPHYR_CLASS) && owl_message_is_loginout(m)) {
+        /* Special case LOGIN/LOGOUT notifications on class "webzephyr" */
+        class=OWL_WEBZEPHYR_CLASS;
+        inst=owl_message_get_instance(m);
+        to=OWL_WEBZEPHYR_PRINCIPAL;
+      } else if (owl_message_is_loginout(m)) {
+        /* Normal LOGIN/LOGOUT messages */
+        class="MESSAGE";
+        inst="PERSONAL";
+        to=owl_message_get_sender(m);
+      } else if (type==1) {
+        /* Personal reply */
+        class="MESSAGE";
+        inst="PERSONAL";
+        to=owl_message_get_sender(m);
       } else {
-
-        /* Special case a personal reply to a webzephyr user on a class */
-        if ((type==1) && !strcasecmp(owl_message_get_opcode(m), OWL_WEBZEPHYR_OPCODE)) {
-          class=OWL_WEBZEPHYR_CLASS;
-          inst=owl_message_get_sender(m);
-          to=OWL_WEBZEPHYR_PRINCIPAL;
-        } else if (!strcasecmp(owl_message_get_class(m), OWL_WEBZEPHYR_CLASS) && owl_message_is_loginout(m)) {
-          /* Special case LOGIN/LOGOUT notifications on class "webzephyr" */
-          class=OWL_WEBZEPHYR_CLASS;
-          inst=owl_message_get_instance(m);
-          to=OWL_WEBZEPHYR_PRINCIPAL;
-        } else if (owl_message_is_loginout(m)) {
-          /* Normal LOGIN/LOGOUT messages */
-          class="MESSAGE";
-          inst="PERSONAL";
-          to=owl_message_get_sender(m);
-        } else if (type==1) {
-          /* Personal reply */
-          class="MESSAGE";
-          inst="PERSONAL";
-          to=owl_message_get_sender(m);
+        /* General reply */
+        class=owl_message_get_class(m);
+        inst=owl_message_get_instance(m);
+        to=owl_message_get_recipient(m);
+        cc=owl_message_get_cc_without_recipient(m);
+        if (!strcmp(to, "") || !strcmp(to, "*")) {
+          to="";
+        } else if (to[0]=='@') {
+          /* leave it, to get the realm */
         } else {
-          /* General reply */
-          class=owl_message_get_class(m);
-          inst=owl_message_get_instance(m);
-          to=owl_message_get_recipient(m);
-          cc=owl_message_get_cc_without_recipient(m);
-          if (!strcmp(to, "") || !strcmp(to, "*")) {
-            to="";
-          } else if (to[0]=='@') {
-            /* leave it, to get the realm */
-          } else {
-            to=owl_message_get_sender(m);
-          }
+          to=owl_message_get_sender(m);
         }
+      }
 
-        /* create the command line */
-        if (!strcasecmp(owl_message_get_opcode(m), "CRYPT")) {
-          buff=owl_strdup("zcrypt");
+      /* create the command line */
+      if (!strcasecmp(owl_message_get_opcode(m), "CRYPT")) {
+        buff=owl_strdup("zcrypt");
+      } else {
+        buff = owl_strdup("zwrite");
+      }
+      if (strcasecmp(class, "message")) {
+        buff = owl_sprintf("%s -c %s%s%s", oldbuff=buff, owl_getquoting(class), class, owl_getquoting(class));
+        owl_free(oldbuff);
+      }
+      if (strcasecmp(inst, "personal")) {
+        buff = owl_sprintf("%s -i %s%s%s", oldbuff=buff, owl_getquoting(inst), inst, owl_getquoting(inst));
+        owl_free(oldbuff);
+      }
+      if (*to != '\0') {
+        char *tmp, *oldtmp, *tmp2;
+        tmp=short_zuser(to);
+        if (cc) {
+          tmp = owl_util_uniq(oldtmp=tmp, cc, "-");
+          owl_free(oldtmp);
+          buff = owl_sprintf("%s -C %s", oldbuff=buff, tmp);
+          owl_free(oldbuff);
         } else {
-          buff = owl_strdup("zwrite");
-        }
-        if (strcasecmp(class, "message")) {
-          buff = owl_sprintf("%s -c %s%s%s", oldbuff=buff, owl_getquoting(class), class, owl_getquoting(class));
+          if (owl_global_is_smartstrip(&g)) {
+            tmp2=tmp;
+            tmp=owl_zephyr_smartstripped_user(tmp2);
+            owl_free(tmp2);
+          }
+          buff = owl_sprintf("%s %s", oldbuff=buff, tmp);
           owl_free(oldbuff);
         }
-        if (strcasecmp(inst, "personal")) {
-          buff = owl_sprintf("%s -i %s%s%s", oldbuff=buff, owl_getquoting(inst), inst, owl_getquoting(inst));
-          owl_free(oldbuff);
-        }
-        if (*to != '\0') {
-          char *tmp, *oldtmp, *tmp2;
-          tmp=short_zuser(to);
-          if (cc) {
-            tmp = owl_util_uniq(oldtmp=tmp, cc, "-");
-            owl_free(oldtmp);
-            buff = owl_sprintf("%s -C %s", oldbuff=buff, tmp);
-            owl_free(oldbuff);
-          } else {
-            if (owl_global_is_smartstrip(&g)) {
-              tmp2=tmp;
-              tmp=owl_zephyr_smartstripped_user(tmp2);
-              owl_free(tmp2);
-            }
-            buff = owl_sprintf("%s %s", oldbuff=buff, tmp);
-            owl_free(oldbuff);
-          }
-          owl_free(tmp);
-        }
-        if (cc) owl_free(cc);
+        owl_free(tmp);
       }
-    } else if (owl_message_is_type_aim(m)) {
-      /* aim */
-      if (owl_message_is_direction_out(m)) {
-	buff=owl_sprintf("aimwrite %s", owl_message_get_recipient(m));
-      } else {
-	buff=owl_sprintf("aimwrite %s", owl_message_get_sender(m));
-      }
-    } else {
-      char *cmd;
-      if((type==0 && (cmd=owl_message_get_attribute_value(m, "replycmd")))
-         || (type==1 && (cmd=owl_message_get_attribute_value(m, "replysendercmd")))) {
-        buff = owl_strdup(cmd);
-      }
+      if (cc) owl_free(cc);
     }
-
-    if(!buff) {
-        owl_function_error("I don't know how to reply to that message.");
-        return;
-    }
-    
-    if (enter) {
-      owl_history *hist = owl_global_get_cmd_history(&g);
-      owl_history_store(hist, buff);
-      owl_history_reset(hist);
-      owl_function_command_norv(buff);
+  } else if (owl_message_is_type_aim(m)) {
+    /* aim */
+    if (owl_message_is_direction_out(m)) {
+      buff=owl_sprintf("aimwrite %s", owl_message_get_recipient(m));
     } else {
-      owl_function_start_command(buff);
+      buff=owl_sprintf("aimwrite %s", owl_message_get_sender(m));
     }
-    owl_free(buff);
+  } else {
+    char *cmd;
+    if((type==0 && (cmd=owl_message_get_attribute_value(m, "replycmd")))
+       || (type==1 && (cmd=owl_message_get_attribute_value(m, "replysendercmd")))) {
+      buff = owl_strdup(cmd);
+    }
   }
+
+  if(!buff) {
+      owl_function_error("I don't know how to reply to that message.");
+      return;
+  }
+  
+  if (enter) {
+    owl_history *hist = owl_global_get_cmd_history(&g);
+    owl_history_store(hist, buff);
+    owl_history_reset(hist);
+    owl_function_command_norv(buff);
+  } else {
+    owl_function_start_command(buff);
+  }
+  owl_free(buff);
 }
 
 void owl_function_zlocate(int argc, char **argv, int auth)
@@ -2331,20 +2314,18 @@
 {
   owl_view *v;
   owl_filter *f;
-  int curid=-1, newpos, curmsg;
+  int curid=-1;
+  owl_view_iterator it;
   owl_message *curm=NULL;
 
   v=owl_global_get_current_view(&g);
 
-  curmsg=owl_global_get_curmsg(&g);
-  if (curmsg==-1) {
-    owl_function_debugmsg("Hit the curmsg==-1 case in change_view");
+  curm=owl_global_get_current_message(&g);
+  if (curm == NULL) {
+    owl_function_debugmsg("Hit the NULL curm case in change_view");
   } else {
-    curm=owl_view_get_element(v, curmsg);
-    if (curm) {
       curid=owl_message_get_id(curm);
       owl_view_save_curmsgid(v, curid);
-    }
   }
 
   f=owl_global_get_filter(&g, filtname);
@@ -2360,13 +2341,12 @@
    *   to the last message pointed to in that view. 
    * - If the view we're leaving is empty, try to restore the position
    *   from the last time we were in the new view.  */
-  if (curm) {
-    newpos = owl_view_get_nearest_to_msgid(v, curid);
-  } else {
-    newpos = owl_view_get_nearest_to_saved(v);
+  if(curid < 0) {
+    curid = owl_filter_get_cachedmsgid(v->filter);
   }
+  owl_view_iterator_init_id(&it, v, curid);
 
-  owl_global_set_curmsg(&g, newpos);
+  owl_global_set_curmsg(&g, &it);
   owl_function_calculate_topmsg(OWL_DIRECTION_DOWNWARDS);
   owl_mainwin_redisplay(owl_global_get_mainwin(&g));
   owl_global_set_direction_downwards(&g);
@@ -2762,21 +2742,28 @@
 void owl_function_delete_curview_msgs(int flag)
 {
   owl_view *v;
-  int i, j;
+  owl_view_iterator it;
+  owl_message *m;
+  int count = 0;
 
   v=owl_global_get_current_view(&g);
-  j=owl_view_get_size(v);
-  for (i=0; i<j; i++) {
+
+
+  for(owl_view_iterator_init_start(&it, v);
+      !owl_view_iterator_is_at_end(&it);
+      owl_view_iterator_next(&it)) {
+    m = owl_view_iterator_get_message(&it);
+    count++;
     if (flag == 1) {
-      owl_message_mark_delete(owl_view_get_element(v, i));
+      owl_message_mark_delete(m);
     } else if (flag == 0) {
-      owl_message_unmark_delete(owl_view_get_element(v, i));
+      owl_message_unmark_delete(m);
     }
   }
 
-  owl_function_makemsg("%i messages marked for %sdeletion", j, flag?"":"un");
+  owl_function_makemsg("%i messages marked for %sdeletion", count, flag?"":"un");
 
-  owl_mainwin_redisplay(owl_global_get_mainwin(&g));  
+  owl_mainwin_redisplay(owl_global_get_mainwin(&g));
 }
 
 /* Create a filter based on the current message.  Returns the name of
@@ -3191,49 +3178,52 @@
    */
 
   owl_view *v;
-  int viewsize, i, curmsg, start;
+  owl_view_iterator start, *it;
   owl_message *m;
 
   v=owl_global_get_current_view(&g);
-  viewsize=owl_view_get_size(v);
-  curmsg=owl_global_get_curmsg(&g);
   
-  if (viewsize==0) {
+  owl_view_iterator_clone(&start, owl_global_get_curmsg(&g));
+  
+  if (owl_view_is_empty(v)) {
     owl_function_error("No messages present");
     return;
   }
 
   if (mode==0) {
-    start=curmsg;
   } else if (direction==OWL_DIRECTION_DOWNWARDS) {
-    start=curmsg+1;
+    owl_view_iterator_next(&start);
   } else {
-    start=curmsg-1;
+    owl_view_iterator_prev(&start);
   }
 
   /* bounds check */
-  if (start>=viewsize || start<0) {
+  if (owl_view_iterator_is_at_start(&start)
+      || owl_view_iterator_is_at_end(&start)) {
     owl_function_error("No further matches found");
     return;
   }
 
-  for (i=start; i<viewsize && i>=0;) {
-    m=owl_view_get_element(v, i);
+  it = &start;
+
+  while(owl_view_iterator_is_at_start(it)
+        || owl_view_iterator_is_at_end(it)) {
+    m=owl_view_iterator_get_message(it);
     if (owl_message_search(m, owl_global_get_search_string(&g))) {
-      owl_global_set_curmsg(&g, i);
+      owl_global_set_curmsg(&g, it);
       owl_function_calculate_topmsg(direction);
       owl_mainwin_redisplay(owl_global_get_mainwin(&g));
       if (direction==OWL_DIRECTION_DOWNWARDS) {
-	owl_global_set_direction_downwards(&g);
+        owl_global_set_direction_downwards(&g);
       } else {
-	owl_global_set_direction_upwards(&g);
+        owl_global_set_direction_upwards(&g);
       }
       return;
     }
     if (direction==OWL_DIRECTION_DOWNWARDS) {
-      i++;
+      owl_view_iterator_next(it);
     } else {
-      i--;
+      owl_view_iterator_prev(it);
     }
   }
   owl_mainwin_redisplay(owl_global_get_mainwin(&g));
@@ -3356,9 +3346,9 @@
 /* Dump messages in the current view to the file 'filename'. */
 void owl_function_dump(char *filename) 
 {
-  int i, j, count;
   owl_message *m;
   owl_view *v;
+  owl_view_iterator it;
   FILE *file;
 
   v=owl_global_get_current_view(&g);
@@ -3378,10 +3368,10 @@
     return;
   }
 
-  count=0;
-  j=owl_view_get_size(v);
-  for (i=0; i<j; i++) {
-    m=owl_view_get_element(v, i);
+  for(owl_view_iterator_init_start(&it, v);
+      !owl_view_iterator_is_at_end(&it);
+      owl_view_iterator_next(&it)) {
+    m = owl_view_iterator_get_message(&it);
     fputs(owl_message_get_text(m), file);
   }
   fclose(file);

Modified: branches/barnowl_sqlite/owl/global.c
===================================================================
--- branches/barnowl_sqlite/owl/global.c	2008-02-15 18:43:30 UTC (rev 941)
+++ branches/barnowl_sqlite/owl/global.c	2008-02-18 03:59:47 UTC (rev 942)
@@ -32,8 +32,7 @@
 
   owl_context_init(&g->ctx);
   owl_context_set_startup(&g->ctx);
-  g->curmsg=0;
-  g->topmsg=0;
+
   g->needrefresh=1;
   g->startupargs=NULL;
 
@@ -176,12 +175,12 @@
 
 /* curmsg */
 
-int owl_global_get_curmsg(owl_global *g) {
-  return(g->curmsg);
+owl_view_iterator* owl_global_get_curmsg(owl_global *g) {
+  return &(g->curmsg);
 }
 
-void owl_global_set_curmsg(owl_global *g, int i) {
-  g->curmsg=i;
+void owl_global_set_curmsg(owl_global *g, owl_view_iterator *it) {
+  owl_view_iterator_clone(&(g->curmsg), it);
   /* we will reset the vertical offset from here */
   /* we might want to move this out to the functions later */
   owl_global_set_curmsg_vert_offset(g, 0);
@@ -189,12 +188,16 @@
 
 /* topmsg */
 
-int owl_global_get_topmsg(owl_global *g) {
-  return(g->topmsg);
+owl_view_iterator* owl_global_get_topmsg(owl_global *g) {
+  return &(g->topmsg);
 }
 
-void owl_global_set_topmsg(owl_global *g, int i) {
-  g->topmsg=i;
+void owl_global_set_topmsg(owl_global *g, owl_view_iterator *it) {
+  if(!it) {
+    owl_view_iterator_invalidate(&(g->topmsg));
+  } else {
+    owl_view_iterator_clone(&(g->topmsg), it);
+  }
 }
 
 /* windows */
@@ -630,8 +633,8 @@
 
 owl_message *owl_global_get_current_message(owl_global *g) {
   owl_view *cur = owl_global_get_current_view(g);
-  if(owl_view_get_size(cur) < 1) return NULL;
-  return owl_view_get_element(cur, owl_global_get_curmsg(g));
+  if(owl_view_is_empty(cur)) return NULL;
+  return owl_view_iterator_get_message(owl_global_get_curmsg(g));
 }
 
 /* has colors */
@@ -676,7 +679,7 @@
   
   v=owl_global_get_current_view(g);
   
-  if (owl_global_get_curmsg(g)==owl_view_get_size(v)-1) return(1);
+  if (owl_view_iterator_is_at_end(owl_global_get_curmsg(g))) return(1);
   return(0);
 }
 

Modified: branches/barnowl_sqlite/owl/mainwin.c
===================================================================
--- branches/barnowl_sqlite/owl/mainwin.c	2008-02-15 18:43:30 UTC (rev 941)
+++ branches/barnowl_sqlite/owl/mainwin.c	2008-02-18 03:59:47 UTC (rev 942)
@@ -5,15 +5,16 @@
 void owl_mainwin_init(owl_mainwin *mw)
 {
   mw->curtruncated=0;
-  mw->lastdisplayed=-1;
+  owl_view_iterator_invalidate(&(mw->lastdisplayed));
 }
 
 void owl_mainwin_redisplay(owl_mainwin *mw)
 {
   owl_message *m;
-  int i, p, q, lines, isfull, viewsize;
+  int p, q, lines, isfull;
   int x, y, savey, recwinlines, start;
-  int topmsg, curmsg, fgcolor, bgcolor;
+  int fgcolor, bgcolor;
+  owl_view_iterator *topmsg, *curmsg, it, *iter = &it;
   WINDOW *recwin;
   owl_view *v;
   owl_list *filtlist;
@@ -34,16 +35,15 @@
 
   recwinlines=owl_global_get_recwin_lines(&g);
   topmsg=owl_global_get_topmsg(&g);
-  viewsize=owl_view_get_size(v);
 
   /* if there are no messages or if topmsg is past the end of the messages,
    * just draw a blank screen */
-  if (viewsize==0 || topmsg>=viewsize) {
-    if (viewsize==0) {
-      owl_global_set_topmsg(&g, 0);
+  if (owl_view_is_empty(v) || !owl_view_iterator_is_valid(topmsg)) {
+    if (owl_view_is_empty(v)) {
+      owl_global_set_topmsg(&g, NULL);
     }
     mw->curtruncated=0;
-    mw->lastdisplayed=-1;
+    owl_view_iterator_invalidate(&(mw->lastdisplayed));
     wnoutrefresh(recwin);
     owl_global_set_needrefresh(&g);
     return;
@@ -54,16 +54,20 @@
   mw->curtruncated=0;
   mw->lasttruncated=0;
 
-  for (i=topmsg; i<viewsize; i++) {
+  for(owl_view_iterator_clone(iter, topmsg);
+      !owl_view_iterator_is_at_end(iter);
+      owl_view_iterator_next(iter)) {
     if (isfull) break;
-    m=owl_view_get_element(v, i);
+    m = owl_view_iterator_get_message(iter);
+    int iscurrent = owl_message_get_id(m) ==
+      owl_message_get_id(owl_view_iterator_get_message(curmsg));
 
     /* hold on to y in case this is the current message or deleted */
     getyx(recwin, y, x);
     savey=y;
 
     /* if it's the current message, account for a vert_offset */
-    if (i==owl_global_get_curmsg(&g)) {
+    if (iscurrent) {
       start=owl_global_get_curmsg_vert_offset(&g);
       lines=owl_message_get_numlines(m)-start;
     } else {
@@ -88,7 +92,7 @@
     }
 
     /* if we'll fill the screen print a partial message */
-    if ((y+lines > recwinlines) && (i==owl_global_get_curmsg(&g))) mw->curtruncated=1;
+    if ((y+lines > recwinlines) && iscurrent) mw->curtruncated=1;
     if (y+lines > recwinlines) mw->lasttruncated=1;
     if (y+lines > recwinlines-1) {
       isfull=1;
@@ -113,7 +117,7 @@
     getyx(recwin, y, x);
     wattrset(recwin, A_NORMAL);
     if (owl_global_get_rightshift(&g)==0) {   /* this lame and should be fixed */
-      if (owl_message_get_id(m)==owl_message_get_id(owl_view_get_element(v, curmsg))) {
+      if (iscurrent) {
 	wmove(recwin, savey, 0);
 	wattron(recwin, A_BOLD);	
 	if (owl_global_get_curmsg_vert_offset(&g)>0) {
@@ -136,7 +140,8 @@
     }
     wattroff(recwin, A_BOLD);
   }
-  mw->lastdisplayed=i-1;
+  // owl_view_iterator_prev(iter);
+  owl_view_iterator_clone(&(mw->lastdisplayed), iter);
 
   wnoutrefresh(recwin);
   owl_global_set_needrefresh(&g);
@@ -155,8 +160,7 @@
   return(0);
 }
 
-int owl_mainwin_get_last_msg(owl_mainwin *mw)
+owl_view_iterator *owl_mainwin_get_last_msg(owl_mainwin *mw)
 {
-  /* return the number of the last message displayed. -1 if none */
-  return(mw->lastdisplayed);
+  return &(mw->lastdisplayed);
 }

Modified: branches/barnowl_sqlite/owl/owl.c
===================================================================
--- branches/barnowl_sqlite/owl/owl.c	2008-02-15 18:43:30 UTC (rev 941)
+++ branches/barnowl_sqlite/owl/owl.c	2008-02-18 03:59:47 UTC (rev 942)
@@ -314,6 +314,7 @@
   /* set the current view */
   owl_function_debugmsg("startup: setting the current view");
   owl_view_create(owl_global_get_current_view(&g), "main", f, owl_global_get_style_by_name(&g, "default"));
+  owl_function_firstmsg();
 
   /* AIM init */
   owl_function_debugmsg("startup: doing AIM initialization");

Modified: branches/barnowl_sqlite/owl/owl.h
===================================================================
--- branches/barnowl_sqlite/owl/owl.h	2008-02-15 18:43:30 UTC (rev 941)
+++ branches/barnowl_sqlite/owl/owl.h	2008-02-18 03:59:47 UTC (rev 942)
@@ -212,6 +212,9 @@
 #define OWL_ENABLE_ZCRYPT 1
 #endif
 
+#define OWL_ITERATE_FORWARD     0
+#define OWL_ITERATE_REVERSE     1
+
 #define OWL_META(key) ((key)|0200)
 /* OWL_CTRL is definied in kepress.c */
 
@@ -265,6 +268,7 @@
   int size;
   int avail;
   void **list;
+  void **alloc;
 } owl_list;
 
 typedef struct _owl_dict_el {
@@ -355,12 +359,6 @@
   void (*formatfunc) (owl_fmtext *fm, owl_message *m);
 } owl_style;
 
-typedef struct _owl_mainwin {
-  int curtruncated;
-  int lasttruncated;
-  int lastdisplayed;
-} owl_mainwin;
-
 typedef struct _owl_viewwin {
   owl_fmtext fmtext;
   int textlines;
@@ -426,6 +424,17 @@
   owl_style *style;
 } owl_view;
 
+typedef struct _owl_view_iterator {
+  owl_view *view;
+  int index;
+} owl_view_iterator;
+
+typedef struct _owl_mainwin {
+  int curtruncated;
+  int lasttruncated;
+  owl_view_iterator lastdisplayed;
+} owl_mainwin;
+
 typedef struct _owl_history {
   owl_list hist;
   int cur;
@@ -537,7 +546,7 @@
   owl_context ctx;
   owl_errqueue errqueue;
   int lines, cols;
-  int curmsg, topmsg;
+  owl_view_iterator curmsg, topmsg;
   int curmsg_vert_offset;
   owl_view current_view;
   owl_messagelist *msglist;

Modified: branches/barnowl_sqlite/owl/util.c
===================================================================
--- branches/barnowl_sqlite/owl/util.c	2008-02-15 18:43:30 UTC (rev 941)
+++ branches/barnowl_sqlite/owl/util.c	2008-02-18 03:59:47 UTC (rev 942)
@@ -13,7 +13,8 @@
   WINDOW *sepwin;
   owl_messagelist *ml;
   owl_view *v;
-  int x, y, i;
+  owl_view_iterator iter;
+  int x, y;
   char *foo, *appendtosepbar;
 
   sepwin=owl_global_get_curs_sepwin(&g);
@@ -39,9 +40,7 @@
   if (owl_messagelist_get_size(ml)==0) {
     strcpy(buff, " (-/-) ");
   } else {
-    snprintf(buff, 1024, " (%i/%i/%i) ", owl_global_get_curmsg(&g)+1,
-	    owl_view_get_size(v),
-	    owl_messagelist_get_size(ml));
+    snprintf(buff, 1024, " (??? / ??? / %i) ", owl_messagelist_get_size(ml));
   }
   waddstr(sepwin, buff);
 
@@ -60,9 +59,8 @@
     wattroff(sepwin, A_BOLD);
   }
 
-  i=owl_mainwin_get_last_msg(owl_global_get_mainwin(&g));
-  if ((i != -1) &&
-      (i < owl_view_get_size(v)-1)) {
+  owl_view_iterator_clone(&iter, owl_mainwin_get_last_msg(owl_global_get_mainwin(&g)));
+  if (owl_view_iterator_is_valid(&iter)) {
     getyx(sepwin, y, x);
     wmove(sepwin, y, x+2);
     wattron(sepwin, A_BOLD);

Modified: branches/barnowl_sqlite/owl/view.c
===================================================================
--- branches/barnowl_sqlite/owl/view.c	2008-02-15 18:43:30 UTC (rev 941)
+++ branches/barnowl_sqlite/owl/view.c	2008-02-18 03:59:47 UTC (rev 942)
@@ -71,37 +71,32 @@
   return(owl_style_get_name(v->style));
 }
 
-owl_message *owl_view_get_element(owl_view *v, int index)
+owl_message *_owl_view_get_element(owl_view *v, int index)
 {
-  if(index < 0 || index >= owl_view_get_size(v))
+  if(index < 0 || index >= _owl_view_get_size(v))
     return NULL;
   int id = (int)owl_list_get_element(&(v->messages), index);
   return owl_messagelist_get_by_id(owl_global_get_msglist(&g), id);
 }
 
-void owl_view_delete_element(owl_view *v, int index)
+int owl_view_is_empty(owl_view *v)
 {
-  owl_message_mark_delete(owl_view_get_element(v, index));
+  return owl_list_get_size(&(v->messages)) == 0;
 }
 
-void owl_view_undelete_element(owl_view *v, int index)
+int _owl_view_get_size(owl_view *v)
 {
-  owl_message_unmark_delete(owl_view_get_element(v, index));
-}
-
-int owl_view_get_size(owl_view *v)
-{
   return(owl_list_get_size(&(v->messages)));
 }
 
 /* Returns the position in the view with a message closest 
  * to the passed msgid. */
-int owl_view_get_nearest_to_msgid(owl_view *v, int targetid)
+int _owl_view_get_nearest_to_msgid(owl_view *v, int targetid)
 {
   int first, last, mid = 0, max, bestdist, curid = 0;
 
   first = 0;
-  last = max = owl_view_get_size(v) - 1;
+  last = max = _owl_view_get_size(v) - 1;
   while (first <= last) {
     mid = (first + last) / 2;
     curid = (int)owl_list_get_element(&(v->messages), mid);
@@ -125,15 +120,6 @@
   return mid;
 }
 
-int owl_view_get_nearest_to_saved(owl_view *v)
-{
-  int cachedid;
-
-  cachedid=owl_filter_get_cachedmsgid(v->filter);
-  if (cachedid<0) return(0);
-  return (owl_view_get_nearest_to_msgid(v, cachedid));
-}
-
 /* saves the current message position in the filter so it can 
  * be restored later if we switch back to this filter. */
 void owl_view_save_curmsgid(owl_view *v, int curid)
@@ -167,3 +153,82 @@
   owl_list_free_simple(&(v->messages));
   if (v->name) owl_free(v->name);
 }
+
+/* View Iterators */
+
+void owl_view_iterator_invalidate(owl_view_iterator *it)
+{
+  it->index = -1;
+  it->view = NULL;
+}
+
+void owl_view_iterator_init_id(owl_view_iterator *it, owl_view *v, int message_id)
+{
+  it->view = v;
+  it->index = _owl_view_get_nearest_to_msgid(v, message_id);
+}
+
+void owl_view_iterator_init_start(owl_view_iterator *it, owl_view *v)
+{
+  it->view = v;
+  it->index = 0;
+}
+
+void owl_view_iterator_init_end(owl_view_iterator *it, owl_view *v)
+{
+  it->view = v;
+  it->index = _owl_view_get_size(v) - 1;
+}
+
+void owl_view_iterator_clone(owl_view_iterator *dst, owl_view_iterator *src)
+{
+  dst->view  = src->view;
+  dst->index = src->index;
+}
+
+int owl_view_iterator_has_prev(owl_view_iterator *it)
+{
+  return it->index > 0;
+}
+
+int owl_view_iterator_has_next(owl_view_iterator *it)
+{
+  return it->index < _owl_view_get_size(it->view) - 1;
+}
+
+int owl_view_iterator_is_at_end(owl_view_iterator *it)
+{
+  return it->index >= _owl_view_get_size(it->view);
+}
+
+int owl_view_iterator_is_at_start(owl_view_iterator *it)
+{
+  return it->index < 0;
+}
+
+int owl_view_iterator_is_valid(owl_view_iterator *it)
+{
+  return it->view != NULL && it->index >= 0 && it->index < _owl_view_get_size(it->view);
+}
+
+void owl_view_iterator_prev(owl_view_iterator *it)
+{
+  if(!owl_view_iterator_is_at_start(it))
+    it->index--;
+}
+
+void owl_view_iterator_next(owl_view_iterator *it)
+{
+  if(!owl_view_iterator_is_at_end(it))
+    it->index++;
+}
+
+owl_message * owl_view_iterator_get_message(owl_view_iterator *it)
+{
+  return _owl_view_get_element(it->view, it->index);
+}
+
+int owl_view_iterator_cmp(owl_view_iterator *it1, owl_view_iterator *it2)
+{
+  return it2->index - it1->index;
+}


home help back first fref pref prev next nref lref last post