Andreas Krennmair:
[apps/madmutt.git] / history.c
1 /*
2  * Copyright (C) 1996-2000 Michael R. Elkins <me@mutt.org>
3  * 
4  *     This program is free software; you can redistribute it and/or modify
5  *     it under the terms of the GNU General Public License as published by
6  *     the Free Software Foundation; either version 2 of the License, or
7  *     (at your option) any later version.
8  * 
9  *     This program is distributed in the hope that it will be useful,
10  *     but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *     GNU General Public License for more details.
13  * 
14  *     You should have received a copy of the GNU General Public License
15  *     along with this program; if not, write to the Free Software
16  *     Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
17  */ 
18
19 #include "mutt.h"
20 #include "history.h"
21
22 /* global vars used for the string-history routines */
23
24 struct history
25 {
26   char **hist;
27   short cur;
28   short last;
29 }; 
30
31 static struct history History[HC_LAST];
32 static int OldSize = 0;
33
34 static void init_history (struct history *h)
35 {
36   int i;
37
38   if(OldSize)
39   {
40     if (h->hist)
41     {
42       for (i = 0 ; i < OldSize ; i ++)
43         FREE (&h->hist[i]);
44       FREE (&h->hist);
45     }
46   }
47   
48   if (HistSize)
49     h->hist = safe_calloc (HistSize, sizeof (char *));
50   
51   h->cur = 0;
52   h->last = 0;
53 }
54
55 void mutt_init_history(void)
56 {
57   history_class_t hclass;
58   
59   if (HistSize == OldSize)
60     return;
61   
62   for(hclass = HC_FIRST; hclass < HC_LAST; hclass++)
63     init_history(&History[hclass]);
64
65   OldSize = HistSize;
66 }
67   
68 void mutt_history_add (history_class_t hclass, const char *s)
69 {
70   int prev;
71   struct history *h = &History[hclass];
72   
73   if (!HistSize)
74     return; /* disabled */
75
76   if (*s)
77   {
78     prev = h->last - 1;
79     if (prev < 0) prev = HistSize - 1;
80     if (!h->hist[prev] || mutt_strcmp (h->hist[prev], s) != 0)
81     {
82       mutt_str_replace (&h->hist[h->last++], s);
83       if (h->last > HistSize - 1)
84         h->last = 0;
85     }
86   }
87   h->cur = h->last; /* reset to the last entry */
88 }
89
90 char *mutt_history_next (history_class_t hclass)
91 {
92   int next;
93   struct history *h = &History[hclass];
94   
95   if (!HistSize)
96     return (""); /* disabled */
97
98   next = h->cur + 1;
99   if (next > HistSize - 1)
100     next = 0;
101   h->cur = h->hist[next] ? next : 0;
102   return (h->hist[h->cur] ? h->hist[h->cur] : "");
103 }
104
105 char *mutt_history_prev (history_class_t hclass)
106 {
107   int prev;
108   struct history *h = &History[hclass];
109
110   if (!HistSize)
111     return (""); /* disabled */
112
113   prev = h->cur - 1;
114   if (prev < 0)
115   {
116     prev = HistSize - 1;
117     while (prev > 0 && h->hist[prev] == NULL)
118       prev--;
119   }
120   if (h->hist[prev])
121     h->cur = prev;
122   return (h->hist[h->cur] ? h->hist[h->cur] : "");
123 }