Update of Dutch translation
[geany-mirror.git] / src / navqueue.c
blob57750c4f8c4a9d4be382c18370a93c0472a4a786
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 #include "geany.h"
29 #include "sciwrappers.h"
30 #include "document.h"
31 #include "utils.h"
32 #include "support.h"
33 #include "ui_utils.h"
34 #include "editor.h"
35 #include "navqueue.h"
36 #include "toolbar.h"
39 /* for the navigation history queue */
40 typedef struct
42 const gchar *file; /* This is the document's filename, in UTF-8 */
43 gint pos;
44 } filepos;
46 static GQueue *navigation_queue;
47 static guint nav_queue_pos;
49 static GtkAction *navigation_buttons[2];
53 void navqueue_init()
55 navigation_queue = g_queue_new();
56 nav_queue_pos = 0;
58 navigation_buttons[0] = toolbar_get_action_by_name("NavBack");
59 navigation_buttons[1] = toolbar_get_action_by_name("NavFor");
61 gtk_action_set_sensitive(navigation_buttons[0], FALSE);
62 gtk_action_set_sensitive(navigation_buttons[1], FALSE);
66 void navqueue_free()
68 while (! g_queue_is_empty(navigation_queue))
70 g_free(g_queue_pop_tail(navigation_queue));
72 g_queue_free(navigation_queue);
76 static void adjust_buttons(void)
78 if (g_queue_get_length(navigation_queue) < 2)
80 gtk_action_set_sensitive(navigation_buttons[0], FALSE);
81 gtk_action_set_sensitive(navigation_buttons[1], FALSE);
82 return;
84 if (nav_queue_pos == 0)
86 gtk_action_set_sensitive(navigation_buttons[0], TRUE);
87 gtk_action_set_sensitive(navigation_buttons[1], FALSE);
88 return;
90 /* forward should be sensitive since where not at the start */
91 gtk_action_set_sensitive(navigation_buttons[1], TRUE);
93 /* back should be sensitive if there's a place to go back to */
94 (nav_queue_pos < g_queue_get_length(navigation_queue) - 1) ?
95 gtk_action_set_sensitive(navigation_buttons[0], TRUE) :
96 gtk_action_set_sensitive(navigation_buttons[0], FALSE);
100 static gboolean
101 queue_pos_matches(guint queue_pos, const gchar *fname, gint pos)
103 if (queue_pos < g_queue_get_length(navigation_queue))
105 filepos *fpos = g_queue_peek_nth(navigation_queue, queue_pos);
107 return (utils_str_equal(fpos->file, fname) && fpos->pos == pos);
109 return FALSE;
113 static void add_new_position(const gchar *utf8_filename, gint pos)
115 filepos *npos;
116 guint i;
118 if (queue_pos_matches(nav_queue_pos, utf8_filename, pos))
119 return; /* prevent duplicates */
121 npos = g_new0(filepos, 1);
122 npos->file = utf8_filename;
123 npos->pos = pos;
125 /* if we've jumped to a new position from inside the queue rather than going forward */
126 if (nav_queue_pos > 0)
128 for (i = 0; i < nav_queue_pos; i++)
130 g_free(g_queue_pop_head(navigation_queue));
132 nav_queue_pos = 0;
134 g_queue_push_head(navigation_queue, npos);
135 adjust_buttons();
140 * Adds old file position and new file position to the navqueue, then goes to the new position.
142 * @param old_doc The document of the previous position, if set as invalid (@c NULL) then no old
143 * position is set
144 * @param new_doc The document of the new position, must be valid.
145 * @param line the line number of the new position. It is counted with 1 as the first line, not 0.
147 * @return @c TRUE if the cursor has changed the position to @a line or @c FALSE otherwise.
149 gboolean navqueue_goto_line(GeanyDocument *old_doc, GeanyDocument *new_doc, gint line)
151 gint pos;
153 g_return_val_if_fail(new_doc != NULL, FALSE);
154 g_return_val_if_fail(line >= 1, FALSE);
156 pos = sci_get_position_from_line(new_doc->editor->sci, line - 1);
158 /* first add old file position */
159 if (old_doc != NULL && old_doc->file_name)
161 gint cur_pos = sci_get_current_position(old_doc->editor->sci);
163 add_new_position(old_doc->file_name, cur_pos);
166 /* now add new file position */
167 if (new_doc->file_name)
169 add_new_position(new_doc->file_name, pos);
172 return editor_goto_pos(new_doc->editor, pos, TRUE);
176 static gboolean goto_file_pos(const gchar *file, gint pos)
178 GeanyDocument *doc = document_find_by_filename(file);
180 if (doc == NULL)
181 return FALSE;
183 return editor_goto_pos(doc->editor, pos, TRUE);
187 void navqueue_go_back()
189 filepos *fprev;
191 /* return if theres no place to go back to */
192 if (g_queue_is_empty(navigation_queue) ||
193 nav_queue_pos >= g_queue_get_length(navigation_queue) - 1)
194 return;
196 /* jump back */
197 fprev = g_queue_peek_nth(navigation_queue, nav_queue_pos + 1);
198 if (goto_file_pos(fprev->file, fprev->pos))
200 nav_queue_pos++;
202 else
204 /** TODO: add option to re open the file */
205 g_free(g_queue_pop_nth(navigation_queue, nav_queue_pos + 1));
207 adjust_buttons();
211 void navqueue_go_forward()
213 filepos *fnext;
215 if (nav_queue_pos < 1 ||
216 nav_queue_pos >= g_queue_get_length(navigation_queue))
217 return;
219 /* jump forward */
220 fnext = g_queue_peek_nth(navigation_queue, nav_queue_pos - 1);
221 if (goto_file_pos(fnext->file, fnext->pos))
223 nav_queue_pos--;
225 else
227 /** TODO: add option to re open the file */
228 g_free(g_queue_pop_nth(navigation_queue, nav_queue_pos - 1));
231 adjust_buttons();
235 static gint find_by_filename(gconstpointer a, gconstpointer b)
237 if (utils_str_equal(((const filepos*)a)->file, (const gchar*) b))
238 return 0;
239 else
240 return 1;
244 /* Remove all elements with the given filename */
245 void navqueue_remove_file(const gchar *filename)
247 GList *match;
249 if (filename == NULL)
250 return;
252 while ((match = g_queue_find_custom(navigation_queue, filename, find_by_filename)))
254 g_free(match->data);
255 g_queue_delete_link(navigation_queue, match);
258 adjust_buttons();