2007-06-02 16:14:07 +00:00
|
|
|
/*
|
|
|
|
* navqueue.c - this file is part of Geany, a fast and lightweight IDE
|
|
|
|
*
|
2008-01-06 18:11:57 +00:00
|
|
|
* Copyright 2007 Dave Moore <wrex006(at)gmail(dot)com>
|
2009-01-04 18:30:42 +00:00
|
|
|
* Copyright 2007-2009 Enrico Tröger <enrico(dot)troeger(at)uvena(dot)de>
|
|
|
|
* Copyright 2007-2009 Nick Treleaven <nick(dot)treleaven(at)btinternet(dot)com>
|
2007-06-02 16:14:07 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*
|
2007-06-03 16:15:53 +00:00
|
|
|
* $Id$
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Simple code navigation
|
2007-06-02 16:14:07 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "geany.h"
|
|
|
|
|
|
|
|
#include "sciwrappers.h"
|
|
|
|
#include "document.h"
|
|
|
|
#include "utils.h"
|
2007-08-23 11:34:06 +00:00
|
|
|
#include "support.h"
|
2008-05-22 14:41:28 +00:00
|
|
|
#include "ui_utils.h"
|
|
|
|
#include "editor.h"
|
2008-06-12 20:09:57 +00:00
|
|
|
#include "navqueue.h"
|
2008-12-06 11:10:06 +00:00
|
|
|
#include "toolbar.h"
|
2007-06-02 16:14:07 +00:00
|
|
|
|
|
|
|
|
2008-02-27 13:17:29 +00:00
|
|
|
/* for the navigation history queue */
|
2007-06-02 16:14:07 +00:00
|
|
|
typedef struct
|
|
|
|
{
|
2008-04-22 12:59:08 +00:00
|
|
|
const gchar *file; /* This is the document's filename, in UTF-8 */
|
2008-04-10 18:18:34 +00:00
|
|
|
gint pos;
|
2007-06-02 16:14:07 +00:00
|
|
|
} filepos;
|
|
|
|
|
2007-08-23 11:34:06 +00:00
|
|
|
static GQueue *navigation_queue;
|
|
|
|
static guint nav_queue_pos;
|
|
|
|
|
2008-12-06 11:10:06 +00:00
|
|
|
static GtkAction *navigation_buttons[2];
|
2007-08-23 11:34:06 +00:00
|
|
|
|
2007-06-02 16:14:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
void navqueue_init()
|
|
|
|
{
|
|
|
|
navigation_queue = g_queue_new();
|
|
|
|
nav_queue_pos = 0;
|
2007-08-23 11:34:06 +00:00
|
|
|
|
2008-12-06 11:10:06 +00:00
|
|
|
navigation_buttons[0] = toolbar_get_action_by_name("NavBack");
|
|
|
|
navigation_buttons[1] = toolbar_get_action_by_name("NavFor");
|
2007-06-02 16:14:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void navqueue_free()
|
|
|
|
{
|
|
|
|
while (! g_queue_is_empty(navigation_queue))
|
|
|
|
{
|
|
|
|
g_free(g_queue_pop_tail(navigation_queue));
|
|
|
|
}
|
|
|
|
g_queue_free(navigation_queue);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-02-20 11:24:23 +00:00
|
|
|
static void adjust_buttons(void)
|
2007-06-02 16:14:07 +00:00
|
|
|
{
|
|
|
|
if (g_queue_get_length(navigation_queue) < 2)
|
|
|
|
{
|
2008-12-06 11:10:06 +00:00
|
|
|
gtk_action_set_sensitive(navigation_buttons[0], FALSE);
|
|
|
|
gtk_action_set_sensitive(navigation_buttons[1], FALSE);
|
2007-06-02 16:14:07 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (nav_queue_pos == 0)
|
|
|
|
{
|
2008-12-06 11:10:06 +00:00
|
|
|
gtk_action_set_sensitive(navigation_buttons[0], TRUE);
|
|
|
|
gtk_action_set_sensitive(navigation_buttons[1], FALSE);
|
2007-06-02 16:14:07 +00:00
|
|
|
return;
|
|
|
|
}
|
2008-02-27 13:17:29 +00:00
|
|
|
/* forward should be sensitive since where not at the start */
|
2008-12-06 11:10:06 +00:00
|
|
|
gtk_action_set_sensitive(navigation_buttons[1], TRUE);
|
2007-06-02 16:14:07 +00:00
|
|
|
|
2008-02-27 13:17:29 +00:00
|
|
|
/* back should be sensitive if there's a place to go back to */
|
2007-06-02 16:14:07 +00:00
|
|
|
(nav_queue_pos < g_queue_get_length(navigation_queue) - 1) ?
|
2008-12-06 11:10:06 +00:00
|
|
|
gtk_action_set_sensitive(navigation_buttons[0], TRUE) :
|
|
|
|
gtk_action_set_sensitive(navigation_buttons[0], FALSE);
|
2007-06-02 16:14:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-07-17 16:11:38 +00:00
|
|
|
static gboolean
|
2008-04-10 18:18:34 +00:00
|
|
|
queue_pos_matches(guint queue_pos, const gchar *fname, gint pos)
|
2007-07-17 16:11:38 +00:00
|
|
|
{
|
|
|
|
if (queue_pos < g_queue_get_length(navigation_queue))
|
|
|
|
{
|
|
|
|
filepos *fpos = g_queue_peek_nth(navigation_queue, queue_pos);
|
|
|
|
|
2008-04-10 18:18:34 +00:00
|
|
|
return (utils_str_equal(fpos->file, fname) && fpos->pos == pos);
|
2007-07-17 16:11:38 +00:00
|
|
|
}
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-04-22 12:59:08 +00:00
|
|
|
static void add_new_position(const gchar *utf8_filename, gint pos)
|
2007-06-02 16:14:07 +00:00
|
|
|
{
|
|
|
|
filepos *npos;
|
|
|
|
guint i;
|
|
|
|
|
2008-04-19 08:14:00 +00:00
|
|
|
if (queue_pos_matches(nav_queue_pos, utf8_filename, pos))
|
2008-02-27 13:17:29 +00:00
|
|
|
return; /* prevent duplicates */
|
2007-07-17 16:11:38 +00:00
|
|
|
|
2007-06-02 16:14:07 +00:00
|
|
|
npos = g_new0(filepos, 1);
|
2008-04-19 08:14:00 +00:00
|
|
|
npos->file = utf8_filename;
|
2008-04-10 18:18:34 +00:00
|
|
|
npos->pos = pos;
|
2007-06-02 16:14:07 +00:00
|
|
|
|
2008-02-27 13:17:29 +00:00
|
|
|
/* if we've jumped to a new position from inside the queue rather than going forward */
|
2007-06-02 16:14:07 +00:00
|
|
|
if (nav_queue_pos > 0)
|
|
|
|
{
|
|
|
|
for (i = 0; i < nav_queue_pos; i++)
|
|
|
|
{
|
|
|
|
g_free(g_queue_pop_head(navigation_queue));
|
|
|
|
}
|
|
|
|
nav_queue_pos = 0;
|
|
|
|
}
|
|
|
|
g_queue_push_head(navigation_queue, npos);
|
|
|
|
adjust_buttons();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-04-17 17:48:12 +00:00
|
|
|
/**
|
|
|
|
* Add old file position and new file position to the navqueue, then goes to the new position.
|
|
|
|
*
|
2008-06-12 20:09:57 +00:00
|
|
|
* @param old_doc The document of the previous position, if set as invalid (@c NULL) then no old
|
2008-04-17 17:48:12 +00:00
|
|
|
* position is set
|
2008-06-12 20:09:57 +00:00
|
|
|
* @param new_doc The document of the new position, must be valid.
|
2008-04-17 17:48:12 +00:00
|
|
|
* @param line the line number of the new position. It is counted with 1 as the first line, not 0.
|
|
|
|
*
|
|
|
|
* @return @a TRUE if the cursor has changed the position to @a line or @a FALSE otherwise.
|
|
|
|
**/
|
2008-06-12 20:45:18 +00:00
|
|
|
gboolean navqueue_goto_line(GeanyDocument *old_doc, GeanyDocument *new_doc, gint line)
|
2007-07-17 16:11:38 +00:00
|
|
|
{
|
2008-04-10 18:18:34 +00:00
|
|
|
gint pos;
|
|
|
|
|
2009-04-05 21:07:40 +00:00
|
|
|
g_return_val_if_fail(G_LIKELY(new_doc != NULL), FALSE);
|
|
|
|
g_return_val_if_fail(G_LIKELY(line >= 1), FALSE);
|
2007-07-17 16:11:38 +00:00
|
|
|
|
2008-07-14 11:13:54 +00:00
|
|
|
pos = sci_get_position_from_line(new_doc->editor->sci, line - 1);
|
2008-04-10 18:18:34 +00:00
|
|
|
|
2008-04-01 13:12:29 +00:00
|
|
|
/* first add old file position */
|
2008-06-12 20:09:57 +00:00
|
|
|
if (old_doc != NULL && old_doc->file_name)
|
2007-07-17 16:11:38 +00:00
|
|
|
{
|
2008-07-14 11:13:54 +00:00
|
|
|
gint cur_pos = sci_get_current_position(old_doc->editor->sci);
|
2007-07-17 16:11:38 +00:00
|
|
|
|
2008-06-12 20:09:57 +00:00
|
|
|
add_new_position(old_doc->file_name, cur_pos);
|
2007-07-17 16:11:38 +00:00
|
|
|
}
|
|
|
|
|
2008-04-01 13:12:29 +00:00
|
|
|
/* now add new file position */
|
2009-04-05 21:07:40 +00:00
|
|
|
if (G_LIKELY(new_doc->file_name))
|
2008-04-10 18:18:34 +00:00
|
|
|
{
|
2008-06-12 20:09:57 +00:00
|
|
|
add_new_position(new_doc->file_name, pos);
|
2008-04-10 18:18:34 +00:00
|
|
|
}
|
2008-04-01 13:12:29 +00:00
|
|
|
|
2008-07-08 13:53:08 +00:00
|
|
|
return editor_goto_pos(new_doc->editor, pos, TRUE);
|
2008-06-12 20:09:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-06-03 17:22:04 +00:00
|
|
|
static gboolean goto_file_pos(const gchar *file, gint pos)
|
2008-05-22 14:41:28 +00:00
|
|
|
{
|
2008-06-15 13:35:48 +00:00
|
|
|
GeanyDocument *doc = document_find_by_filename(file);
|
2008-05-22 14:41:28 +00:00
|
|
|
|
2009-04-05 21:07:40 +00:00
|
|
|
if (G_UNLIKELY(doc == NULL))
|
2008-06-15 13:35:48 +00:00
|
|
|
return FALSE;
|
2008-05-22 14:41:28 +00:00
|
|
|
|
2008-07-08 13:53:08 +00:00
|
|
|
return editor_goto_pos(doc->editor, pos, TRUE);
|
2007-07-17 16:11:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-06-02 16:14:07 +00:00
|
|
|
void navqueue_go_back()
|
|
|
|
{
|
|
|
|
filepos *fprev;
|
|
|
|
|
2008-02-27 13:17:29 +00:00
|
|
|
/* return if theres no place to go back to */
|
2007-06-02 16:14:07 +00:00
|
|
|
if (g_queue_is_empty(navigation_queue) ||
|
|
|
|
nav_queue_pos >= g_queue_get_length(navigation_queue) - 1)
|
|
|
|
return;
|
|
|
|
|
2008-02-27 13:17:29 +00:00
|
|
|
/* jump back */
|
2007-06-02 16:14:07 +00:00
|
|
|
fprev = g_queue_peek_nth(navigation_queue, nav_queue_pos + 1);
|
2008-06-03 17:22:04 +00:00
|
|
|
if (goto_file_pos(fprev->file, fprev->pos))
|
2007-06-02 16:14:07 +00:00
|
|
|
{
|
|
|
|
nav_queue_pos++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-02-27 13:17:29 +00:00
|
|
|
/** TODO: add option to re open the file */
|
2007-07-18 12:20:13 +00:00
|
|
|
g_free(g_queue_pop_nth(navigation_queue, nav_queue_pos + 1));
|
2007-06-02 16:14:07 +00:00
|
|
|
}
|
|
|
|
adjust_buttons();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void navqueue_go_forward()
|
|
|
|
{
|
|
|
|
filepos *fnext;
|
|
|
|
|
2007-07-18 12:20:13 +00:00
|
|
|
if (nav_queue_pos < 1 ||
|
2007-07-18 12:25:18 +00:00
|
|
|
nav_queue_pos >= g_queue_get_length(navigation_queue))
|
2007-06-02 16:14:07 +00:00
|
|
|
return;
|
|
|
|
|
2008-02-27 13:17:29 +00:00
|
|
|
/* jump forward */
|
2007-06-02 16:14:07 +00:00
|
|
|
fnext = g_queue_peek_nth(navigation_queue, nav_queue_pos - 1);
|
2008-06-03 17:22:04 +00:00
|
|
|
if (goto_file_pos(fnext->file, fnext->pos))
|
2007-06-02 16:14:07 +00:00
|
|
|
{
|
|
|
|
nav_queue_pos--;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-02-27 13:17:29 +00:00
|
|
|
/** TODO: add option to re open the file */
|
2007-07-18 12:20:13 +00:00
|
|
|
g_free(g_queue_pop_nth(navigation_queue, nav_queue_pos - 1));
|
2007-06-02 16:14:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
adjust_buttons();
|
|
|
|
}
|
|
|
|
|
2008-04-19 08:14:00 +00:00
|
|
|
|
|
|
|
static gint find_by_filename(gconstpointer a, gconstpointer b)
|
|
|
|
{
|
|
|
|
if (utils_str_equal(((const filepos*)a)->file, (const gchar*) b))
|
|
|
|
return 0;
|
|
|
|
else
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Remove all elements with the given filename */
|
|
|
|
void navqueue_remove_file(const gchar *filename)
|
|
|
|
{
|
|
|
|
GList *match;
|
|
|
|
|
2009-04-05 21:07:40 +00:00
|
|
|
if (G_UNLIKELY(filename == NULL))
|
2008-04-19 08:14:00 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
while ((match = g_queue_find_custom(navigation_queue, filename, find_by_filename)))
|
|
|
|
{
|
|
|
|
g_free(match->data);
|
|
|
|
g_queue_delete_link(navigation_queue, match);
|
|
|
|
}
|
|
|
|
|
|
|
|
adjust_buttons();
|
|
|
|
}
|