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