1 | #include "owl.h" |
---|
2 | |
---|
3 | static const char fileIdent[] = "$Id$"; |
---|
4 | |
---|
5 | void owl_history_init(owl_history *h) { |
---|
6 | owl_list_create(&(h->hist)); |
---|
7 | h->cur=0; /* current position in history */ |
---|
8 | h->touched=0; /* whether we've gone into history */ |
---|
9 | h->partial=0; /* is the 0th element is partially composed? */ |
---|
10 | } |
---|
11 | |
---|
12 | char *owl_history_get_prev(owl_history *h) { |
---|
13 | |
---|
14 | if (!h) return NULL; |
---|
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++; |
---|
24 | |
---|
25 | return(owl_list_get_element(&(h->hist), h->cur)); |
---|
26 | } |
---|
27 | |
---|
28 | char *owl_history_get_next(owl_history *h) { |
---|
29 | if (!h) return NULL; |
---|
30 | if (owl_list_get_size(&(h->hist))==0) return(NULL); |
---|
31 | if (h->cur==0) { |
---|
32 | return(NULL); |
---|
33 | } |
---|
34 | |
---|
35 | h->cur--; |
---|
36 | return(owl_list_get_element(&(h->hist), h->cur)); |
---|
37 | } |
---|
38 | |
---|
39 | void owl_history_store(owl_history *h, char *line) { |
---|
40 | int size; |
---|
41 | |
---|
42 | if (!h) return; |
---|
43 | |
---|
44 | /* if partial is set, remove the first entry first */ |
---|
45 | if (h->partial) { |
---|
46 | owl_list_remove_element(&(h->hist), 0); |
---|
47 | } |
---|
48 | |
---|
49 | /* if we've reached the max history size, pop off the last element */ |
---|
50 | size=owl_list_get_size(&(h->hist)); |
---|
51 | if (size>OWL_HISTORYSIZE) { |
---|
52 | owl_free(owl_list_get_element(&(h->hist), size-1)); |
---|
53 | owl_list_remove_element(&(h->hist), size-1); |
---|
54 | } |
---|
55 | |
---|
56 | /* add the new line */ |
---|
57 | owl_list_prepend_element(&(h->hist), owl_strdup(line)); |
---|
58 | } |
---|
59 | |
---|
60 | void owl_history_set_partial(owl_history *h) { |
---|
61 | if (!h) return; |
---|
62 | h->partial=1; |
---|
63 | } |
---|
64 | |
---|
65 | void owl_history_reset(owl_history *h) { |
---|
66 | if (!h) return; |
---|
67 | h->cur=0; |
---|
68 | h->touched=0; |
---|
69 | h->partial=0; |
---|
70 | } |
---|
71 | |
---|
72 | int owl_history_is_touched(owl_history *h) { |
---|
73 | if (!h) return(0); |
---|
74 | if (h->touched) return(1); |
---|
75 | return(0); |
---|
76 | } |
---|