Changeset 95b52d1
- Timestamp:
- Jun 25, 2011, 3:24:36 AM (13 years ago)
- Branches:
- master, release-1.10, release-1.8, release-1.9
- Children:
- d8e2f07, 6829afc
- Parents:
- 8e515f9
- git-author:
- Anders Kaseorg <andersk@mit.edu> (06/22/11 02:36:29)
- git-committer:
- Anders Kaseorg <andersk@mit.edu> (06/25/11 03:24:36)
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
history.c
r8e515f9 r95b52d1 3 3 void owl_history_init(owl_history *h) 4 4 { 5 owl_list_create(&(h->hist));6 h->cur =0;/* current position in history */5 g_queue_init(&h->hist); 6 h->cur = h->hist.tail; /* current position in history */ 7 7 h->partial = false; /* is the 0th element is partially composed? */ 8 8 } … … 10 10 const char *owl_history_get_prev(owl_history *h) 11 11 { 12 13 12 if (!h) return NULL; 14 13 15 if ( owl_list_get_size(&(h->hist))==0) return(NULL);14 if (h->cur == NULL || g_list_previous(h->cur) == NULL) return NULL; 16 15 17 if (h->cur == owl_list_get_size(&(h->hist))-1) { 18 return(NULL); 19 } 20 21 h->cur++; 22 23 return(owl_list_get_element(&(h->hist), h->cur)); 16 h->cur = g_list_previous(h->cur); 17 return h->cur->data; 24 18 } 25 19 … … 27 21 { 28 22 if (!h) return NULL; 29 if (owl_list_get_size(&(h->hist))==0) return(NULL);30 if (h->cur==0) {31 return(NULL);32 }33 23 34 h->cur--; 35 return(owl_list_get_element(&(h->hist), h->cur)); 24 if (h->cur == NULL || g_list_next(h->cur) == NULL) return NULL; 25 26 h->cur = g_list_next(h->cur); 27 return h->cur->data; 36 28 } 37 29 38 30 void owl_history_store(owl_history *h, const char *line, bool partial) 39 31 { 40 int size;41 42 32 if (!h) return; 43 size=owl_list_get_size(&(h->hist));44 33 45 34 owl_history_reset(h); 46 35 47 36 /* check if the line is the same as the last */ 48 if (!partial && owl_list_get_size(&(h->hist)) > 0&&49 strcmp(line, owl_list_get_element(&(h->hist), 0)) == 0)37 if (!partial && !g_queue_is_empty(&h->hist) && 38 strcmp(line, g_queue_peek_tail(&h->hist)) == 0) 50 39 return; 51 40 52 41 /* if we've reached the max history size, pop off the last element */ 53 if (size>OWL_HISTORYSIZE) { 54 g_free(owl_list_get_element(&(h->hist), size-1)); 55 owl_list_remove_element(&(h->hist), size-1); 56 } 42 if (g_queue_get_length(&h->hist) > OWL_HISTORYSIZE) 43 g_free(g_queue_pop_head(&h->hist)); 57 44 58 45 /* add the new line */ 59 owl_list_prepend_element(&(h->hist), g_strdup(line));46 g_queue_push_tail(&h->hist, g_strdup(line)); 60 47 h->partial = partial; 48 h->cur = h->hist.tail; 61 49 } 62 50 … … 67 55 /* if partial is set, remove the first entry first */ 68 56 if (h->partial) { 69 g_free(owl_list_get_element(&(h->hist), 0)); 70 owl_list_remove_element(&(h->hist), 0); 57 g_free(g_queue_pop_tail(&h->hist)); 71 58 h->partial = false; 72 59 } 73 60 74 h->cur =0;61 h->cur = h->hist.tail; 75 62 } 76 63 … … 78 65 { 79 66 if (!h) return(0); 80 return h->cur > 0;67 return h->cur != NULL && g_list_next(h->cur) != NULL; 81 68 } -
owl.h
rb470451 r95b52d1 455 455 456 456 typedef struct _owl_history { 457 owl_listhist;458 intcur;457 GQueue hist; 458 GList *cur; 459 459 bool partial; 460 460 } owl_history;
Note: See TracChangeset
for help on using the changeset viewer.