source: history.c @ 9923144

release-1.8release-1.9
Last change on this file since 9923144 was 9923144, checked in by Anders Kaseorg <andersk@mit.edu>, 10 years ago
history: Get rid of touched and associated bug This fixes the following bug: type a partial line, up, down, type the rest of the line, up, down; oops, we’re back to the first partial line. Signed-off-by: Anders Kaseorg <andersk@mit.edu>
  • Property mode set to 100644
File size: 1.7 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->partial=0;                 /* is the 0th element is partially composed? */
8}
9
10const char *owl_history_get_prev(owl_history *h)
11{
12
13  if (!h) return NULL;
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++;
22
23  return(owl_list_get_element(&(h->hist), h->cur));
24}
25
26const char *owl_history_get_next(owl_history *h)
27{
28  if (!h) return NULL;
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
38void owl_history_store(owl_history *h, const char *line)
39{
40  int size;
41
42  if (!h) return;
43  size=owl_list_get_size(&(h->hist));
44
45  /* if partial is set, remove the first entry first */
46  if (h->partial) {
47    g_free(owl_list_get_element(&(h->hist), 0));
48    owl_list_remove_element(&(h->hist), 0);
49  }
50
51  /* check if the line is the same as the last */
52  if (owl_list_get_size(&(h->hist))>0) {
53    if (!strcmp(line, owl_list_get_element(&(h->hist), 0))) return;
54  }
55
56  /* if we've reached the max history size, pop off the last element */
57  if (size>OWL_HISTORYSIZE) {
58    g_free(owl_list_get_element(&(h->hist), size-1));
59    owl_list_remove_element(&(h->hist), size-1);
60  }
61
62  /* add the new line */
63  owl_list_prepend_element(&(h->hist), g_strdup(line));
64}
65
66void owl_history_set_partial(owl_history *h)
67{
68  if (!h) return;
69  h->partial=1;
70}
71
72void owl_history_reset(owl_history *h)
73{
74  if (!h) return;
75  h->cur=0;
76  h->partial=0;
77}
78
79int owl_history_is_touched(const owl_history *h)
80{
81  if (!h) return(0);
82  return h->cur > 0;
83}
Note: See TracBrowser for help on using the repository browser.