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
34 #include "geanyobject.h"
35 #include "sciwrappers.h"
39 #include "gtkcompat.h"
42 /* for the navigation history queue */
45 const gchar
*file
; /* This is the document's filename, in UTF-8 */
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();
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
);
87 if (nav_queue_pos
== 0)
89 gtk_action_set_sensitive(navigation_buttons
[0], TRUE
);
90 gtk_action_set_sensitive(navigation_buttons
[1], FALSE
);
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
);
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
);
116 static void add_new_position(const gchar
*utf8_filename
, gint pos
)
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
;
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
));
137 g_queue_push_head(navigation_queue
, npos
);
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
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
)
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
);
187 return editor_goto_pos(doc
->editor
, pos
, TRUE
);
191 void navqueue_go_back(void)
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)
201 fprev
= g_queue_peek_nth(navigation_queue
, nav_queue_pos
+ 1);
202 if (goto_file_pos(fprev
->file
, fprev
->pos
))
208 /** TODO: add option to re open the file */
209 g_free(g_queue_pop_nth(navigation_queue
, nav_queue_pos
+ 1));
215 void navqueue_go_forward(void)
219 if (nav_queue_pos
< 1 ||
220 nav_queue_pos
>= g_queue_get_length(navigation_queue
))
224 fnext
= g_queue_peek_nth(navigation_queue
, nav_queue_pos
- 1);
225 if (goto_file_pos(fnext
->file
, fnext
->pos
))
231 /** TODO: add option to re open the file */
232 g_free(g_queue_pop_nth(navigation_queue
, nav_queue_pos
- 1));
239 static gint
find_by_filename(gconstpointer a
, gconstpointer b
)
241 if (utils_str_equal(((const filepos
*)a
)->file
, (const gchar
*) b
))
248 /* Remove all elements with the given filename */
249 void navqueue_remove_file(const gchar
*filename
)
253 if (filename
== NULL
)
256 while ((match
= g_queue_find_custom(navigation_queue
, filename
, find_by_filename
)))
259 g_queue_delete_link(navigation_queue
, match
);