API: Rename document_reload_file -> document_reload_force
[geany-mirror.git] / src / navqueue.c
blob26d3a2a7f842a579a993e6b86517885dcec5ad54
1 /*
2 * navqueue.c - this file is part of Geany, a fast and lightweight IDE
4 * Copyright 2007 Dave Moore <wrex006(at)gmail(dot)com>
5 * Copyright 2007-2012 Enrico Tröger <enrico(dot)troeger(at)uvena(dot)de>
6 * Copyright 2007-2012 Nick Treleaven <nick(dot)treleaven(at)btinternet(dot)com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 * Simple code navigation
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
31 #include "navqueue.h"
33 #include "document.h"
34 #include "geanyobject.h"
35 #include "sciwrappers.h"
36 #include "toolbar.h"
37 #include "utils.h"
39 #include "gtkcompat.h"
42 /* for the navigation history queue */
43 typedef struct
45 const gchar *file; /* This is the document's filename, in UTF-8 */
46 gint pos;
47 } filepos;
49 static GQueue *navigation_queue;
50 static guint nav_queue_pos;
52 static GtkAction *navigation_buttons[2];
56 void navqueue_init(void)
58 navigation_queue = g_queue_new();
59 nav_queue_pos = 0;
61 navigation_buttons[0] = toolbar_get_action_by_name("NavBack");
62 navigation_buttons[1] = toolbar_get_action_by_name("NavFor");
64 gtk_action_set_sensitive(navigation_buttons[0], FALSE);
65 gtk_action_set_sensitive(navigation_buttons[1], FALSE);
69 void navqueue_free(void)
71 while (! g_queue_is_empty(navigation_queue))
73 g_free(g_queue_pop_tail(navigation_queue));
75 g_queue_free(navigation_queue);
79 static void adjust_buttons(void)
81 if (g_queue_get_length(navigation_queue) < 2)
83 gtk_action_set_sensitive(navigation_buttons[0], FALSE);
84 gtk_action_set_sensitive(navigation_buttons[1], FALSE);
85 return;
87 if (nav_queue_pos == 0)
89 gtk_action_set_sensitive(navigation_buttons[0], TRUE);
90 gtk_action_set_sensitive(navigation_buttons[1], FALSE);
91 return;
93 /* forward should be sensitive since where not at the start */
94 gtk_action_set_sensitive(navigation_buttons[1], TRUE);
96 /* back should be sensitive if there's a place to go back to */
97 (nav_queue_pos < g_queue_get_length(navigation_queue) - 1) ?
98 gtk_action_set_sensitive(navigation_buttons[0], TRUE) :
99 gtk_action_set_sensitive(navigation_buttons[0], FALSE);
103 static gboolean
104 queue_pos_matches(guint queue_pos, const gchar *fname, gint pos)
106 if (queue_pos < g_queue_get_length(navigation_queue))
108 filepos *fpos = g_queue_peek_nth(navigation_queue, queue_pos);
110 return (utils_str_equal(fpos->file, fname) && fpos->pos == pos);
112 return FALSE;
116 static void add_new_position(const gchar *utf8_filename, gint pos)
118 filepos *npos;
119 guint i;
121 if (queue_pos_matches(nav_queue_pos, utf8_filename, pos))
122 return; /* prevent duplicates */
124 npos = g_new0(filepos, 1);
125 npos->file = utf8_filename;
126 npos->pos = pos;
128 /* if we've jumped to a new position from inside the queue rather than going forward */
129 if (nav_queue_pos > 0)
131 for (i = 0; i < nav_queue_pos; i++)
133 g_free(g_queue_pop_head(navigation_queue));
135 nav_queue_pos = 0;
137 g_queue_push_head(navigation_queue, npos);
138 adjust_buttons();
143 * Adds old file position and new file position to the navqueue, then goes to the new position.
145 * @param old_doc The document of the previous position, if set as invalid (@c NULL) then no old
146 * position is set
147 * @param new_doc The document of the new position, must be valid.
148 * @param line the line number of the new position. It is counted with 1 as the first line, not 0.
150 * @return @c TRUE if the cursor has changed the position to @a line or @c FALSE otherwise.
152 gboolean navqueue_goto_line(GeanyDocument *old_doc, GeanyDocument *new_doc, gint line)
154 gint pos;
156 g_return_val_if_fail(old_doc == NULL || old_doc->is_valid, FALSE);
157 g_return_val_if_fail(DOC_VALID(new_doc), FALSE);
158 g_return_val_if_fail(line >= 1, FALSE);
160 pos = sci_get_position_from_line(new_doc->editor->sci, line - 1);
162 /* first add old file position */
163 if (old_doc != NULL && old_doc->file_name)
165 gint cur_pos = sci_get_current_position(old_doc->editor->sci);
167 add_new_position(old_doc->file_name, cur_pos);
170 /* now add new file position */
171 if (new_doc->file_name)
173 add_new_position(new_doc->file_name, pos);
176 return editor_goto_pos(new_doc->editor, pos, TRUE);
180 static gboolean goto_file_pos(const gchar *file, gint pos)
182 GeanyDocument *doc = document_find_by_filename(file);
184 if (doc == NULL)
185 return FALSE;
187 return editor_goto_pos(doc->editor, pos, TRUE);
191 void navqueue_go_back(void)
193 filepos *fprev;
195 /* return if theres no place to go back to */
196 if (g_queue_is_empty(navigation_queue) ||
197 nav_queue_pos >= g_queue_get_length(navigation_queue) - 1)
198 return;
200 /* jump back */
201 fprev = g_queue_peek_nth(navigation_queue, nav_queue_pos + 1);
202 if (goto_file_pos(fprev->file, fprev->pos))
204 nav_queue_pos++;
206 else
208 /** TODO: add option to re open the file */
209 g_free(g_queue_pop_nth(navigation_queue, nav_queue_pos + 1));
211 adjust_buttons();
215 void navqueue_go_forward(void)
217 filepos *fnext;
219 if (nav_queue_pos < 1 ||
220 nav_queue_pos >= g_queue_get_length(navigation_queue))
221 return;
223 /* jump forward */
224 fnext = g_queue_peek_nth(navigation_queue, nav_queue_pos - 1);
225 if (goto_file_pos(fnext->file, fnext->pos))
227 nav_queue_pos--;
229 else
231 /** TODO: add option to re open the file */
232 g_free(g_queue_pop_nth(navigation_queue, nav_queue_pos - 1));
235 adjust_buttons();
239 static gint find_by_filename(gconstpointer a, gconstpointer b)
241 if (utils_str_equal(((const filepos*)a)->file, (const gchar*) b))
242 return 0;
243 else
244 return 1;
248 /* Remove all elements with the given filename */
249 void navqueue_remove_file(const gchar *filename)
251 GList *match;
253 if (filename == NULL)
254 return;
256 while ((match = g_queue_find_custom(navigation_queue, filename, find_by_filename)))
258 g_free(match->data);
259 g_queue_delete_link(navigation_queue, match);
262 adjust_buttons();