source: history.c @ 12294d2

release-1.10release-1.8release-1.9
Last change on this file since 12294d2 was 95b52d1, checked in by Anders Kaseorg <andersk@mit.edu>, 13 years ago
history: Store history in a GQueue Signed-off-by: Anders Kaseorg <andersk@mit.edu>
  • Property mode set to 100644
File size: 1.5 KB
RevLine 
[7d4fbcd]1#include "owl.h"
2
[70b53ec]3void owl_history_init(owl_history *h)
4{
[95b52d1]5  g_queue_init(&h->hist);
6  h->cur = h->hist.tail;        /* current position in history */
[b470451]7  h->partial = false;           /* is the 0th element is partially composed? */
[7d4fbcd]8}
9
[e19eb97]10const char *owl_history_get_prev(owl_history *h)
[70b53ec]11{
[10b866d]12  if (!h) return NULL;
[7d4fbcd]13
[95b52d1]14  if (h->cur == NULL || g_list_previous(h->cur) == NULL) return NULL;
[10b866d]15
[95b52d1]16  h->cur = g_list_previous(h->cur);
17  return h->cur->data;
[7d4fbcd]18}
19
[e19eb97]20const char *owl_history_get_next(owl_history *h)
[70b53ec]21{
[10b866d]22  if (!h) return NULL;
[7d4fbcd]23
[95b52d1]24  if (h->cur == NULL || g_list_next(h->cur) == NULL) return NULL;
25
26  h->cur = g_list_next(h->cur);
27  return h->cur->data;
[7d4fbcd]28}
29
[b470451]30void owl_history_store(owl_history *h, const char *line, bool partial)
[70b53ec]31{
[10b866d]32  if (!h) return;
[12c35df]33
[83a079a]34  owl_history_reset(h);
35
[43744ce]36  /* check if the line is the same as the last */
[95b52d1]37  if (!partial && !g_queue_is_empty(&h->hist) &&
38      strcmp(line, g_queue_peek_tail(&h->hist)) == 0)
[8e515f9]39    return;
[5a9f6fe]40
[7d4fbcd]41  /* if we've reached the max history size, pop off the last element */
[95b52d1]42  if (g_queue_get_length(&h->hist) > OWL_HISTORYSIZE)
43    g_free(g_queue_pop_head(&h->hist));
[7d4fbcd]44
45  /* add the new line */
[95b52d1]46  g_queue_push_tail(&h->hist, g_strdup(line));
[b470451]47  h->partial = partial;
[95b52d1]48  h->cur = h->hist.tail;
[7d4fbcd]49}
50
[70b53ec]51void owl_history_reset(owl_history *h)
52{
[10b866d]53  if (!h) return;
[ad37b39]54
55  /* if partial is set, remove the first entry first */
56  if (h->partial) {
[95b52d1]57    g_free(g_queue_pop_tail(&h->hist));
[b470451]58    h->partial = false;
[ad37b39]59  }
60
[95b52d1]61  h->cur = h->hist.tail;
[7d4fbcd]62}
63
[1e94c0b]64int owl_history_is_touched(const owl_history *h)
[70b53ec]65{
[10b866d]66  if (!h) return(0);
[95b52d1]67  return h->cur != NULL && g_list_next(h->cur) != NULL;
[7d4fbcd]68}
Note: See TracBrowser for help on using the repository browser.