git-svn-id: svn://svn.berlios.de/mutt-ng/trunk@59 e385b8ad-14ed-0310-8656-cc95a2468c6d
[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 #if HAVE_CONFIG_H
20 # include "config.h"
21 #endif
22
23 #include "mutt.h"
24 #include "history.h"
25
26 /* global vars used for the string-history routines */
27
28 struct history
29 {
30   char **hist;
31   short cur;
32   short last;
33 }; 
34
35 static struct history History[HC_LAST];
36 static int OldSize = 0;
37
38 static void init_history (struct history *h)
39 {
40   int i;
41
42   if(OldSize)
43   {
44     if (h->hist)
45     {
46       for (i = 0 ; i < OldSize ; i ++)
47         FREE (&h->hist[i]);
48       FREE (&h->hist);
49     }
50   }
51   
52   if (HistSize)
53     h->hist = safe_calloc (HistSize, sizeof (char *));
54   
55   h->cur = 0;
56   h->last = 0;
57 }
58
59 void mutt_init_history(void)
60 {
61   history_class_t hclass;
62   
63   if (HistSize == OldSize)
64     return;
65   
66   for(hclass = HC_FIRST; hclass < HC_LAST; hclass++)
67     init_history(&History[hclass]);
68
69   OldSize = HistSize;
70 }
71   
72 void mutt_history_add (history_class_t hclass, const char *s)
73 {
74   int prev;
75   struct history *h = &History[hclass];
76   
77   if (!HistSize)
78     return; /* disabled */
79
80   if (*s)
81   {
82     prev = h->last - 1;
83     if (prev < 0) prev = HistSize - 1;
84     if (!h->hist[prev] || mutt_strcmp (h->hist[prev], s) != 0)
85     {
86       mutt_str_replace (&h->hist[h->last++], s);
87       if (h->last > HistSize - 1)
88         h->last = 0;
89     }
90   }
91   h->cur = h->last; /* reset to the last entry */
92 }
93
94 char *mutt_history_next (history_class_t hclass)
95 {
96   int next;
97   struct history *h = &History[hclass];
98   
99   if (!HistSize)
100     return (""); /* disabled */
101
102   next = h->cur + 1;
103   if (next > HistSize - 1)
104     next = 0;
105   h->cur = h->hist[next] ? next : 0;
106   return (h->hist[h->cur] ? h->hist[h->cur] : "");
107 }
108
109 char *mutt_history_prev (history_class_t hclass)
110 {
111   int prev;
112   struct history *h = &History[hclass];
113
114   if (!HistSize)
115     return (""); /* disabled */
116
117   prev = h->cur - 1;
118   if (prev < 0)
119   {
120     prev = HistSize - 1;
121     while (prev > 0 && h->hist[prev] == NULL)
122       prev--;
123   }
124   if (h->hist[prev])
125     h->cur = prev;
126   return (h->hist[h->cur] ? h->hist[h->cur] : "");
127 }