source: history.c @ 76f0e19

release-1.10release-1.4release-1.5release-1.6release-1.7release-1.8release-1.9
Last change on this file since 76f0e19 was 1e94c0b, checked in by Anders Kaseorg <andersk@mit.edu>, 15 years ago
Add const qualifiers for owl_history *. Signed-off-by: Anders Kaseorg <andersk@mit.edu>
  • Property mode set to 100644
File size: 1.9 KB
Line 
1#include "owl.h"
2
3void owl_history_init(owl_history *h)
4{
5  owl_list_create(&(h->hist));
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? */
9  h->repeats=1;                 /* by default we'll allow repeat entries */
10}
11
12void owl_history_set_norepeats(owl_history *h)
13{
14  h->repeats=0;
15}
16
17const char *owl_history_get_prev(owl_history *h)
18{
19
20  if (!h) return NULL;
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++;
30
31  return(owl_list_get_element(&(h->hist), h->cur));
32}
33
34const char *owl_history_get_next(owl_history *h)
35{
36  if (!h) return NULL;
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
46void owl_history_store(owl_history *h, const char *line)
47{
48  int size;
49
50  if (!h) return;
51  size=owl_list_get_size(&(h->hist));
52
53  /* if partial is set, remove the first entry first */
54  if (h->partial) {
55    owl_list_remove_element(&(h->hist), 0);
56  }
57
58  /* if repeats are disallowed, check if the line is the same as the last */
59  if (owl_list_get_size(&(h->hist))>0) {
60    if (!strcmp(line, owl_list_get_element(&(h->hist), 0))) return;
61  }
62
63  /* if we've reached the max history size, pop off the last element */
64  if (size>OWL_HISTORYSIZE) {
65    owl_free(owl_list_get_element(&(h->hist), size-1));
66    owl_list_remove_element(&(h->hist), size-1);
67  }
68
69  /* add the new line */
70  owl_list_prepend_element(&(h->hist), owl_strdup(line));
71}
72
73void owl_history_set_partial(owl_history *h)
74{
75  if (!h) return;
76  h->partial=1;
77}
78
79void owl_history_reset(owl_history *h)
80{
81  if (!h) return;
82  h->cur=0;
83  h->touched=0;
84  h->partial=0;
85}
86
87int owl_history_is_touched(const owl_history *h)
88{
89  if (!h) return(0);
90  if (h->touched) return(1);
91  return(0);
92}
Note: See TracBrowser for help on using the repository browser.