[7d4fbcd] | 1 | #include "owl.h" |
---|
| 2 | |
---|
[1aee7d9] | 3 | static const char fileIdent[] = "$Id$"; |
---|
| 4 | |
---|
[70b53ec] | 5 | void owl_history_init(owl_history *h) |
---|
| 6 | { |
---|
[7d4fbcd] | 7 | owl_list_create(&(h->hist)); |
---|
[10b866d] | 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? */ |
---|
[12c35df] | 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; |
---|
[7d4fbcd] | 17 | } |
---|
| 18 | |
---|
[70b53ec] | 19 | char *owl_history_get_prev(owl_history *h) |
---|
| 20 | { |
---|
[10b866d] | 21 | |
---|
| 22 | if (!h) return NULL; |
---|
[7d4fbcd] | 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++; |
---|
[10b866d] | 32 | |
---|
[7d4fbcd] | 33 | return(owl_list_get_element(&(h->hist), h->cur)); |
---|
| 34 | } |
---|
| 35 | |
---|
[70b53ec] | 36 | char *owl_history_get_next(owl_history *h) |
---|
| 37 | { |
---|
[10b866d] | 38 | if (!h) return NULL; |
---|
[7d4fbcd] | 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 | |
---|
[70b53ec] | 48 | void owl_history_store(owl_history *h, char *line) |
---|
| 49 | { |
---|
[12c35df] | 50 | int i, size; |
---|
[7d4fbcd] | 51 | |
---|
[10b866d] | 52 | if (!h) return; |
---|
[12c35df] | 53 | size=owl_list_get_size(&(h->hist)); |
---|
| 54 | |
---|
| 55 | /* if repeats are disallowed, check if the line is present already */ |
---|
[3c7d007a] | 56 | if (!h->repeats) { |
---|
| 57 | for (i=0; i<size; i++) { |
---|
| 58 | if (!strcmp(line, owl_list_get_element(&(h->hist), i))) return; |
---|
| 59 | } |
---|
[12c35df] | 60 | } |
---|
[10b866d] | 61 | |
---|
[7d4fbcd] | 62 | /* if partial is set, remove the first entry first */ |
---|
| 63 | if (h->partial) { |
---|
| 64 | owl_list_remove_element(&(h->hist), 0); |
---|
| 65 | } |
---|
[10b866d] | 66 | |
---|
[7d4fbcd] | 67 | /* if we've reached the max history size, pop off the last element */ |
---|
| 68 | if (size>OWL_HISTORYSIZE) { |
---|
| 69 | owl_free(owl_list_get_element(&(h->hist), size-1)); |
---|
| 70 | owl_list_remove_element(&(h->hist), size-1); |
---|
| 71 | } |
---|
| 72 | |
---|
| 73 | /* add the new line */ |
---|
| 74 | owl_list_prepend_element(&(h->hist), owl_strdup(line)); |
---|
| 75 | } |
---|
| 76 | |
---|
[70b53ec] | 77 | void owl_history_set_partial(owl_history *h) |
---|
| 78 | { |
---|
[10b866d] | 79 | if (!h) return; |
---|
[7d4fbcd] | 80 | h->partial=1; |
---|
| 81 | } |
---|
| 82 | |
---|
[70b53ec] | 83 | void owl_history_reset(owl_history *h) |
---|
| 84 | { |
---|
[10b866d] | 85 | if (!h) return; |
---|
[7d4fbcd] | 86 | h->cur=0; |
---|
| 87 | h->touched=0; |
---|
| 88 | h->partial=0; |
---|
| 89 | } |
---|
| 90 | |
---|
[70b53ec] | 91 | int owl_history_is_touched(owl_history *h) |
---|
| 92 | { |
---|
[10b866d] | 93 | if (!h) return(0); |
---|
[7d4fbcd] | 94 | if (h->touched) return(1); |
---|
| 95 | return(0); |
---|
| 96 | } |
---|