Theme improvements (#1382)
[geany-mirror.git] / src / navqueue.c
blob442c8fc3ea1701b9db42ae0510d67c15018e04c9
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;
195 GeanyDocument *doc = document_get_current();
197 /* If the navqueue is currently at some position A, but the actual cursor is at some other
198 * place B, we should add B to the navqueue, so that (1) we go back to A, not to the next
199 * item in the queue; and (2) we can later restore B by going forward.
200 * (If A = B, add_new_position will ignore it.) */
201 if (doc)
203 if (doc->file_name)
204 add_new_position(doc->file_name, sci_get_current_position(doc->editor->sci));
206 else
207 /* see also https://github.com/geany/geany/pull/1537 */
208 g_warning("Attempted navigation when nothing is open");
210 /* return if theres no place to go back to */
211 if (g_queue_is_empty(navigation_queue) ||
212 nav_queue_pos >= g_queue_get_length(navigation_queue) - 1)
213 return;
215 /* jump back */
216 fprev = g_queue_peek_nth(navigation_queue, nav_queue_pos + 1);
217 if (goto_file_pos(fprev->file, fprev->pos))
219 nav_queue_pos++;
221 else
223 /** TODO: add option to re open the file */
224 g_free(g_queue_pop_nth(navigation_queue, nav_queue_pos + 1));
226 adjust_buttons();
230 void navqueue_go_forward(void)
232 filepos *fnext;
234 if (nav_queue_pos < 1 ||
235 nav_queue_pos >= g_queue_get_length(navigation_queue))
236 return;
238 /* jump forward */
239 fnext = g_queue_peek_nth(navigation_queue, nav_queue_pos - 1);
240 if (goto_file_pos(fnext->file, fnext->pos))
242 nav_queue_pos--;
244 else
246 /** TODO: add option to re open the file */
247 g_free(g_queue_pop_nth(navigation_queue, nav_queue_pos - 1));
250 adjust_buttons();
254 static gint find_by_filename(gconstpointer a, gconstpointer b)
256 if (utils_str_equal(((const filepos*)a)->file, (const gchar*) b))
257 return 0;
258 else
259 return 1;
263 /* Remove all elements with the given filename */
264 void navqueue_remove_file(const gchar *filename)
266 GList *match;
268 if (filename == NULL)
269 return;
271 while ((match = g_queue_find_custom(navigation_queue, filename, find_by_filename)))
273 g_free(match->data);
274 g_queue_delete_link(navigation_queue, match);
277 adjust_buttons();