Updated Spanish translation
[anjuta-git-plugin.git] / plugins / document-manager / file_history.c
blob80762e035bc42d00c02e3256aebe8d379f646219
1 /*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU Library General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 #include <stdio.h>
19 #include <string.h>
21 #include <glib.h>
23 #include "file_history.h"
25 #define OPT_ENTRIES 5
26 #define MAX_ENTRIES 6
28 typedef struct _AnFileHistory
30 GList *items;
31 GList *current;
32 } AnFileHistory;
34 static AnFileHistory *s_history = NULL;
36 static void an_file_history_init()
38 s_history = g_new(AnFileHistory, 1);
39 s_history->items = NULL;
40 s_history->current = NULL;
43 AnHistFile *an_hist_file_new(const char *name, glong line)
45 AnHistFile *h_file;
47 g_return_val_if_fail(name, NULL);
48 h_file= g_new(AnHistFile, 1);
49 h_file->file = g_strdup(name);
50 h_file->line = line;
51 return h_file;
54 void an_hist_file_free(AnHistFile *h_file)
56 g_return_if_fail(h_file);
57 g_free(h_file->file);
58 g_free(h_file);
61 static void an_hist_items_free(GList *items)
63 GList *tmp;
65 g_return_if_fail(items);
66 for (tmp = items; tmp; tmp = g_list_next(tmp))
67 an_hist_file_free((AnHistFile *) tmp->data);
68 g_list_free(items);
71 void an_file_history_reset(void)
73 g_return_if_fail(s_history && s_history->items);
75 an_hist_items_free(s_history->items);
76 s_history->items = NULL;
77 s_history->current = NULL;
80 void an_file_history_push(const char *filename, glong line)
82 AnHistFile *h_file;
84 g_return_if_fail(filename);
85 if (!s_history)
86 an_file_history_init();
87 if (s_history->current)
89 AnHistFile *current = (AnHistFile *) s_history->current->data;
90 if ((0 == strcmp(filename, current->file)) &&
91 ((current->line < 1) || (line == current->line)))
93 current->line = line;
94 return;
96 if (s_history->current != s_history->items)
98 GList *tmp = s_history->current->prev;
99 if (tmp)
101 tmp->next = NULL;
102 an_hist_items_free(s_history->items);
104 s_history->items = s_history->current;
105 s_history->current->prev = NULL;
107 if (g_list_length(s_history->items) > MAX_ENTRIES)
109 GList *tmp = g_list_nth(s_history->items, OPT_ENTRIES);
110 an_hist_items_free(tmp->next);
111 tmp->next = NULL;
114 h_file = an_hist_file_new(filename, line);
115 s_history->items = g_list_prepend(s_history->items, h_file);
116 s_history->current = s_history->items;
119 void an_file_history_back(AnjutaDocman *docman)
121 AnHistFile *h_file;
123 if (!(s_history && s_history->current && s_history->current->next))
124 return;
126 s_history->current = s_history->current->next;
127 h_file = (AnHistFile *) s_history->current->data;
128 anjuta_docman_goto_file_line_mark (docman, h_file->file,
129 h_file->line, FALSE);
132 void an_file_history_forward(AnjutaDocman *docman)
134 AnHistFile *h_file;
136 if (!(s_history && s_history->current && s_history->current->prev))
137 return;
139 s_history->current = s_history->current->prev;
140 h_file = (AnHistFile *) s_history->current->data;
141 anjuta_docman_goto_file_line_mark(docman, h_file->file,
142 h_file->line, FALSE);
145 void an_file_history_dump(void)
147 GList *tmp;
148 AnHistFile *h_file;
150 g_return_if_fail(s_history && s_history->items);
151 fprintf(stderr, "--------------------------\n");
152 for (tmp = s_history->items; tmp; tmp = g_list_next(tmp))
154 h_file = (AnHistFile *) tmp->data;
155 fprintf(stderr, "%s:%ld", h_file->file, h_file->line);
156 if (tmp == s_history->current)
157 fprintf(stderr, " (*)");
158 fprintf(stderr, "\n");
160 fprintf(stderr, "--------------------------\n");
163 void an_file_history_free(void)
165 an_file_history_reset();
166 g_free(s_history);
167 s_history = NULL;