Update displayed copyright years
[geany-mirror.git] / src / navqueue.c
blob01d74d99907e8960691d0abadb17b2215f671cdd
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 GEANY_API_SYMBOL
153 gboolean navqueue_goto_line(GeanyDocument *old_doc, GeanyDocument *new_doc, gint line)
155 gint pos;
157 g_return_val_if_fail(old_doc == NULL || old_doc->is_valid, FALSE);
158 g_return_val_if_fail(DOC_VALID(new_doc), FALSE);
159 g_return_val_if_fail(line >= 1, FALSE);
161 pos = sci_get_position_from_line(new_doc->editor->sci, line - 1);
163 /* first add old file position */
164 if (old_doc != NULL && old_doc->file_name)
166 gint cur_pos = sci_get_current_position(old_doc->editor->sci);
168 add_new_position(old_doc->file_name, cur_pos);
171 /* now add new file position */
172 if (new_doc->file_name)
174 add_new_position(new_doc->file_name, pos);
177 return editor_goto_pos(new_doc->editor, pos, TRUE);
181 static gboolean goto_file_pos(const gchar *file, gint pos)
183 GeanyDocument *doc = document_find_by_filename(file);
185 if (doc == NULL)
186 return FALSE;
188 return editor_goto_pos(doc->editor, pos, TRUE);
192 void navqueue_go_back(void)
194 filepos *fprev;
196 /* return if theres no place to go back to */
197 if (g_queue_is_empty(navigation_queue) ||
198 nav_queue_pos >= g_queue_get_length(navigation_queue) - 1)
199 return;
201 /* jump back */
202 fprev = g_queue_peek_nth(navigation_queue, nav_queue_pos + 1);
203 if (goto_file_pos(fprev->file, fprev->pos))
205 nav_queue_pos++;
207 else
209 /** TODO: add option to re open the file */
210 g_free(g_queue_pop_nth(navigation_queue, nav_queue_pos + 1));
212 adjust_buttons();
216 void navqueue_go_forward(void)
218 filepos *fnext;
220 if (nav_queue_pos < 1 ||
221 nav_queue_pos >= g_queue_get_length(navigation_queue))
222 return;
224 /* jump forward */
225 fnext = g_queue_peek_nth(navigation_queue, nav_queue_pos - 1);
226 if (goto_file_pos(fnext->file, fnext->pos))
228 nav_queue_pos--;
230 else
232 /** TODO: add option to re open the file */
233 g_free(g_queue_pop_nth(navigation_queue, nav_queue_pos - 1));
236 adjust_buttons();
240 static gint find_by_filename(gconstpointer a, gconstpointer b)
242 if (utils_str_equal(((const filepos*)a)->file, (const gchar*) b))
243 return 0;
244 else
245 return 1;
249 /* Remove all elements with the given filename */
250 void navqueue_remove_file(const gchar *filename)
252 GList *match;
254 if (filename == NULL)
255 return;
257 while ((match = g_queue_find_custom(navigation_queue, filename, find_by_filename)))
259 g_free(match->data);
260 g_queue_delete_link(navigation_queue, match);
263 adjust_buttons();