well this makes things fail for people ...
[apps/madmutt.git] / lib-ui / history.c
1 /*
2  * Copyright notice from original mutt:
3  * Copyright (C) 1996-2000 Michael R. Elkins <me@mutt.org>
4  *
5  * This file is part of mutt-ng, see http://www.muttng.org/.
6  * It's licensed under the GNU General Public License,
7  * please see the file GPL in the top level source directory.
8  */
9
10 #include <lib-ui/lib-ui.h>
11
12 #include "mutt.h"
13 #include "history.h"
14
15 /* global vars used for the string-history routines */
16
17 struct history {
18   char **hist;
19   short cur;
20   short last;
21 };
22
23 static struct history History[HC_LAST];
24 static int OldSize = 0;
25
26 static void init_history (struct history *h)
27 {
28   int i;
29
30   if (OldSize) {
31     if (h->hist) {
32       for (i = 0; i < OldSize; i++)
33         p_delete(&h->hist[i]);
34       p_delete(&h->hist);
35     }
36   }
37
38   if (HistSize)
39     h->hist = p_new(char *, HistSize);
40
41   h->cur = 0;
42   h->last = 0;
43 }
44
45 void mutt_init_history (void)
46 {
47   history_class_t hclass;
48
49   if (HistSize == OldSize)
50     return;
51
52   for (hclass = HC_FIRST; hclass < HC_LAST; hclass++)
53     init_history (&History[hclass]);
54
55   OldSize = HistSize;
56 }
57
58 void mutt_history_add (history_class_t hclass, const char *s)
59 {
60   int prev;
61   struct history *h = &History[hclass];
62
63   if (!HistSize)
64     return;                     /* disabled */
65
66   if (*s) {
67     prev = h->last - 1;
68     if (prev < 0)
69       prev = HistSize - 1;
70     if (!h->hist[prev] || m_strcmp(h->hist[prev], s) != 0) {
71       m_strreplace(&h->hist[h->last++], s);
72       if (h->last > HistSize - 1)
73         h->last = 0;
74     }
75   }
76   h->cur = h->last;             /* reset to the last entry */
77 }
78
79 const char *mutt_history_next (history_class_t hclass)
80 {
81   int next;
82   struct history *h = &History[hclass];
83
84   if (!HistSize)
85     return "";                /* disabled */
86
87   next = h->cur + 1;
88   if (next > HistSize - 1)
89     next = 0;
90   h->cur = h->hist[next] ? next : 0;
91   return h->hist[h->cur] ? h->hist[h->cur] : "";
92 }
93
94 const char *mutt_history_prev (history_class_t hclass)
95 {
96   int prev;
97   struct history *h = &History[hclass];
98
99   if (!HistSize)
100     return "";                /* disabled */
101
102   prev = h->cur - 1;
103   if (prev < 0) {
104     prev = HistSize - 1;
105     while (prev > 0 && h->hist[prev] == NULL)
106       prev--;
107   }
108   if (h->hist[prev])
109     h->cur = prev;
110   return h->hist[h->cur] ? h->hist[h->cur] : "";
111 }