| 1 | #include "owl.h" |
|---|
| 2 | |
|---|
| 3 | static const char fileIdent[] = "$Id$"; |
|---|
| 4 | |
|---|
| 5 | void owl_history_init(owl_history *h) |
|---|
| 6 | { |
|---|
| 7 | owl_list_create(&(h->hist)); |
|---|
| 8 | h->cur=0; /* current position in history */ |
|---|
| 9 | h->touched=0; /* whether we've gone into history */ |
|---|
| 10 | h->partial=0; /* is the 0th element is partially composed? */ |
|---|
| 11 | h->repeats=1; /* by default we'll allow repeat entries */ |
|---|
| 12 | } |
|---|
| 13 | |
|---|
| 14 | void owl_history_set_norepeats(owl_history *h) |
|---|
| 15 | { |
|---|
| 16 | h->repeats=0; |
|---|
| 17 | } |
|---|
| 18 | |
|---|
| 19 | char *owl_history_get_prev(owl_history *h) |
|---|
| 20 | { |
|---|
| 21 | |
|---|
| 22 | if (!h) return NULL; |
|---|
| 23 | h->touched=1; |
|---|
| 24 | |
|---|
| 25 | if (owl_list_get_size(&(h->hist))==0) return(NULL); |
|---|
| 26 | |
|---|
| 27 | if (h->cur == owl_list_get_size(&(h->hist))-1) { |
|---|
| 28 | return(NULL); |
|---|
| 29 | } |
|---|
| 30 | |
|---|
| 31 | h->cur++; |
|---|
| 32 | |
|---|
| 33 | return(owl_list_get_element(&(h->hist), h->cur)); |
|---|
| 34 | } |
|---|
| 35 | |
|---|
| 36 | char *owl_history_get_next(owl_history *h) |
|---|
| 37 | { |
|---|
| 38 | if (!h) return NULL; |
|---|
| 39 | if (owl_list_get_size(&(h->hist))==0) return(NULL); |
|---|
| 40 | if (h->cur==0) { |
|---|
| 41 | return(NULL); |
|---|
| 42 | } |
|---|
| 43 | |
|---|
| 44 | h->cur--; |
|---|
| 45 | return(owl_list_get_element(&(h->hist), h->cur)); |
|---|
| 46 | } |
|---|
| 47 | |
|---|
| 48 | void owl_history_store(owl_history *h, char *line) |
|---|
| 49 | { |
|---|
| 50 | int size; |
|---|
| 51 | |
|---|
| 52 | if (!h) return; |
|---|
| 53 | size=owl_list_get_size(&(h->hist)); |
|---|
| 54 | |
|---|
| 55 | /* if partial is set, remove the first entry first */ |
|---|
| 56 | if (h->partial) { |
|---|
| 57 | owl_list_remove_element(&(h->hist), 0); |
|---|
| 58 | } |
|---|
| 59 | |
|---|
| 60 | /* if repeats are disallowed, check if the line is the same as the last */ |
|---|
| 61 | if (owl_list_get_size(&(h->hist))>0) { |
|---|
| 62 | if (!strcmp(line, owl_list_get_element(&(h->hist), 0))) return; |
|---|
| 63 | } |
|---|
| 64 | |
|---|
| 65 | /* if we've reached the max history size, pop off the last element */ |
|---|
| 66 | if (size>OWL_HISTORYSIZE) { |
|---|
| 67 | owl_free(owl_list_get_element(&(h->hist), size-1)); |
|---|
| 68 | owl_list_remove_element(&(h->hist), size-1); |
|---|
| 69 | } |
|---|
| 70 | |
|---|
| 71 | /* add the new line */ |
|---|
| 72 | owl_list_prepend_element(&(h->hist), owl_strdup(line)); |
|---|
| 73 | } |
|---|
| 74 | |
|---|
| 75 | void owl_history_set_partial(owl_history *h) |
|---|
| 76 | { |
|---|
| 77 | if (!h) return; |
|---|
| 78 | h->partial=1; |
|---|
| 79 | } |
|---|
| 80 | |
|---|
| 81 | void owl_history_reset(owl_history *h) |
|---|
| 82 | { |
|---|
| 83 | if (!h) return; |
|---|
| 84 | h->cur=0; |
|---|
| 85 | h->touched=0; |
|---|
| 86 | h->partial=0; |
|---|
| 87 | } |
|---|
| 88 | |
|---|
| 89 | int owl_history_is_touched(owl_history *h) |
|---|
| 90 | { |
|---|
| 91 | if (!h) return(0); |
|---|
| 92 | if (h->touched) return(1); |
|---|
| 93 | return(0); |
|---|
| 94 | } |
|---|