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