[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? */ |
---|
[7d4fbcd] | 9 | } |
---|
| 10 | |
---|
[e19eb97] | 11 | const char *owl_history_get_prev(owl_history *h) |
---|
[70b53ec] | 12 | { |
---|
[10b866d] | 13 | |
---|
| 14 | if (!h) return NULL; |
---|
[7d4fbcd] | 15 | h->touched=1; |
---|
| 16 | |
---|
| 17 | if (owl_list_get_size(&(h->hist))==0) return(NULL); |
---|
| 18 | |
---|
| 19 | if (h->cur == owl_list_get_size(&(h->hist))-1) { |
---|
| 20 | return(NULL); |
---|
| 21 | } |
---|
| 22 | |
---|
| 23 | h->cur++; |
---|
[10b866d] | 24 | |
---|
[7d4fbcd] | 25 | return(owl_list_get_element(&(h->hist), h->cur)); |
---|
| 26 | } |
---|
| 27 | |
---|
[e19eb97] | 28 | const char *owl_history_get_next(owl_history *h) |
---|
[70b53ec] | 29 | { |
---|
[10b866d] | 30 | if (!h) return NULL; |
---|
[7d4fbcd] | 31 | if (owl_list_get_size(&(h->hist))==0) return(NULL); |
---|
| 32 | if (h->cur==0) { |
---|
| 33 | return(NULL); |
---|
| 34 | } |
---|
| 35 | |
---|
| 36 | h->cur--; |
---|
| 37 | return(owl_list_get_element(&(h->hist), h->cur)); |
---|
| 38 | } |
---|
| 39 | |
---|
[e19eb97] | 40 | void owl_history_store(owl_history *h, const char *line) |
---|
[70b53ec] | 41 | { |
---|
[52f3507] | 42 | int size; |
---|
[7d4fbcd] | 43 | |
---|
[10b866d] | 44 | if (!h) return; |
---|
[12c35df] | 45 | size=owl_list_get_size(&(h->hist)); |
---|
| 46 | |
---|
[7d4fbcd] | 47 | /* if partial is set, remove the first entry first */ |
---|
| 48 | if (h->partial) { |
---|
[ddbbcffa] | 49 | g_free(owl_list_get_element(&(h->hist), 0)); |
---|
[7d4fbcd] | 50 | owl_list_remove_element(&(h->hist), 0); |
---|
| 51 | } |
---|
[10b866d] | 52 | |
---|
[43744ce] | 53 | /* check if the line is the same as the last */ |
---|
[5a9f6fe] | 54 | if (owl_list_get_size(&(h->hist))>0) { |
---|
| 55 | if (!strcmp(line, owl_list_get_element(&(h->hist), 0))) return; |
---|
| 56 | } |
---|
| 57 | |
---|
[7d4fbcd] | 58 | /* if we've reached the max history size, pop off the last element */ |
---|
| 59 | if (size>OWL_HISTORYSIZE) { |
---|
[ddbbcffa] | 60 | g_free(owl_list_get_element(&(h->hist), size-1)); |
---|
[7d4fbcd] | 61 | owl_list_remove_element(&(h->hist), size-1); |
---|
| 62 | } |
---|
| 63 | |
---|
| 64 | /* add the new line */ |
---|
[d4927a7] | 65 | owl_list_prepend_element(&(h->hist), g_strdup(line)); |
---|
[7d4fbcd] | 66 | } |
---|
| 67 | |
---|
[70b53ec] | 68 | void owl_history_set_partial(owl_history *h) |
---|
| 69 | { |
---|
[10b866d] | 70 | if (!h) return; |
---|
[7d4fbcd] | 71 | h->partial=1; |
---|
| 72 | } |
---|
| 73 | |
---|
[70b53ec] | 74 | void owl_history_reset(owl_history *h) |
---|
| 75 | { |
---|
[10b866d] | 76 | if (!h) return; |
---|
[7d4fbcd] | 77 | h->cur=0; |
---|
| 78 | h->touched=0; |
---|
| 79 | h->partial=0; |
---|
| 80 | } |
---|
| 81 | |
---|
[1e94c0b] | 82 | int owl_history_is_touched(const owl_history *h) |
---|
[70b53ec] | 83 | { |
---|
[10b866d] | 84 | if (!h) return(0); |
---|
[7d4fbcd] | 85 | if (h->touched) return(1); |
---|
| 86 | return(0); |
---|
| 87 | } |
---|