Updated Makefile.am files after make -f git.mk
[anjuta.git] / plugins / document-manager / file_history.c
blob79bd3b51f4dd997bf56d2f9bf209c7f0997bae69
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 gboolean history_move;
33 } AnFileHistory;
35 static AnFileHistory *s_history = NULL;
37 static void an_file_history_init (void)
39 s_history = g_new(AnFileHistory, 1);
40 s_history->items = NULL;
41 s_history->current = NULL;
42 s_history->history_move = FALSE;
45 AnHistFile *an_hist_file_new (GFile *file, gint line)
47 AnHistFile *h_file;
49 g_return_val_if_fail(file, NULL);
51 h_file= g_new(AnHistFile, 1);
52 h_file->file = g_object_ref (file);
53 h_file->line = line;
54 return h_file;
57 void an_hist_file_free(AnHistFile *h_file)
59 g_return_if_fail(h_file);
60 g_object_unref (h_file->file);
61 g_free(h_file);
64 static void an_hist_items_free(GList *items)
66 GList *tmp;
68 g_return_if_fail(items);
69 for (tmp = items; tmp; tmp = g_list_next(tmp))
70 an_hist_file_free((AnHistFile *) tmp->data);
71 g_list_free(items);
74 void an_file_history_reset(void)
76 g_return_if_fail(s_history && s_history->items);
78 an_hist_items_free(s_history->items);
79 s_history->items = NULL;
80 s_history->current = NULL;
83 void an_file_history_push (GFile *file, gint line)
85 AnHistFile *h_file;
87 g_return_if_fail (file);
89 if (!s_history)
90 an_file_history_init();
92 if (s_history->current)
94 GList *next;
96 /* Only update line number when called by forward/backward */
97 if (s_history->history_move)
99 AnHistFile *h_file = (AnHistFile *) s_history->current->data;
101 if (g_file_equal (file,h_file->file))
103 h_file->line = line;
105 return;
108 next = s_history->current->next;
109 s_history->current->next = NULL;
110 an_hist_items_free(s_history->items);
112 s_history->items = next;
113 if (next) next->prev = NULL;
114 s_history->current = NULL;
115 if (g_list_length(s_history->items) > MAX_ENTRIES)
117 GList *tmp = g_list_nth(s_history->items, OPT_ENTRIES);
118 an_hist_items_free(tmp->next);
119 tmp->next = NULL;
122 h_file = an_hist_file_new(file, line);
123 s_history->items = g_list_prepend(s_history->items, h_file);
124 s_history->current = NULL;
127 void an_file_history_back(AnjutaDocman *docman)
129 AnHistFile *h_file;
130 GList *current;
132 if (!(s_history && (!s_history->current || s_history->current->next)))
133 return;
135 current = s_history->current ? s_history->current->next : s_history->items;
136 h_file = (AnHistFile *) current->data;
138 s_history->history_move = TRUE;
139 anjuta_docman_goto_file_line_mark (docman, h_file->file,
140 h_file->line, FALSE);
141 s_history->history_move = FALSE;
143 s_history->current = current;
146 void an_file_history_forward(AnjutaDocman *docman)
148 AnHistFile *h_file;
149 GList *current;
151 if (!(s_history && s_history->current && s_history->current->prev))
152 return;
154 current = s_history->current->prev;
155 h_file = (AnHistFile *) current->data;
157 s_history->history_move = TRUE;
158 anjuta_docman_goto_file_line_mark(docman, h_file->file,
159 h_file->line, FALSE);
160 s_history->history_move = FALSE;
162 s_history->current = current;
165 void an_file_history_dump(void)
167 GList *tmp;
168 AnHistFile *h_file;
170 g_return_if_fail(s_history && s_history->items);
171 fprintf(stderr, "--------------------------\n");
172 for (tmp = s_history->items; tmp; tmp = g_list_next(tmp))
174 gchar *uri;
175 h_file = (AnHistFile *) tmp->data;
176 uri = g_file_get_uri (h_file->file);
177 fprintf(stderr, "%s:%d", uri, h_file->line);
178 g_free (uri);
179 if (tmp == s_history->current)
180 fprintf(stderr, " (*)");
181 fprintf(stderr, "\n");
183 fprintf(stderr, "--------------------------\n");
186 void an_file_history_free(void)
188 an_file_history_reset();
189 g_free(s_history);
190 s_history = NULL;