2005-11-22 12:26:26 +00:00
|
|
|
/*
|
|
|
|
* document.c - this file is part of Geany, a fast and lightweight IDE
|
|
|
|
*
|
2012-06-18 01:13:05 +02:00
|
|
|
* Copyright 2005-2012 Enrico Tröger <enrico(dot)troeger(at)uvena(dot)de>
|
|
|
|
* Copyright 2006-2012 Nick Treleaven <nick(dot)treleaven(at)btinternet(dot)com>
|
2005-11-22 12:26:26 +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.
|
|
|
|
*
|
2012-08-24 19:25:57 +02:00
|
|
|
* 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.
|
2005-11-22 12:26:26 +00:00
|
|
|
*/
|
|
|
|
|
2007-02-24 11:41:56 +00:00
|
|
|
/*
|
2008-02-17 18:00:42 +00:00
|
|
|
* Document related actions: new, save, open, etc.
|
|
|
|
* Also Scintilla search actions.
|
2007-02-24 11:41:56 +00:00
|
|
|
*/
|
|
|
|
|
2014-05-18 17:31:51 -07:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "document.h"
|
|
|
|
|
|
|
|
#include "app.h"
|
|
|
|
#include "callbacks.h" /* for ignore_callback */
|
|
|
|
#include "dialogs.h"
|
|
|
|
#include "documentprivate.h"
|
|
|
|
#include "encodings.h"
|
|
|
|
#include "filetypesprivate.h"
|
|
|
|
#include "geany.h" /* FIXME: why is this needed for DOC_FILENAME()? should come from documentprivate.h/document.h */
|
|
|
|
#include "geanyobject.h"
|
2014-05-28 18:01:23 +02:00
|
|
|
#include "geanywraplabel.h"
|
2014-05-18 17:31:51 -07:00
|
|
|
#include "highlighting.h"
|
|
|
|
#include "main.h"
|
|
|
|
#include "msgwindow.h"
|
|
|
|
#include "navqueue.h"
|
|
|
|
#include "notebook.h"
|
|
|
|
#include "project.h"
|
|
|
|
#include "sciwrappers.h"
|
|
|
|
#include "sidebar.h"
|
|
|
|
#include "support.h"
|
|
|
|
#include "symbols.h"
|
|
|
|
#include "ui_utils.h"
|
|
|
|
#include "utils.h"
|
|
|
|
#include "vte.h"
|
2014-08-01 12:00:20 +01:00
|
|
|
#include "win32.h"
|
2005-12-29 20:01:18 +00:00
|
|
|
|
2014-06-04 14:16:39 +02:00
|
|
|
#include "gtkcompat.h"
|
|
|
|
|
2008-06-04 15:43:52 +00:00
|
|
|
#ifdef HAVE_SYS_TIME_H
|
2005-12-29 20:01:18 +00:00
|
|
|
# include <sys/time.h>
|
|
|
|
#endif
|
2008-06-04 15:43:52 +00:00
|
|
|
#include <time.h>
|
|
|
|
|
2005-12-29 20:01:18 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#ifdef HAVE_SYS_TYPES_H
|
|
|
|
# include <sys/types.h>
|
|
|
|
#endif
|
2008-06-04 15:43:52 +00:00
|
|
|
|
2006-06-29 17:14:52 +00:00
|
|
|
#include <stdlib.h>
|
2005-11-22 12:26:26 +00:00
|
|
|
|
2008-06-04 15:43:52 +00:00
|
|
|
/* gstdio.h also includes sys/stat.h */
|
2007-04-16 15:58:34 +00:00
|
|
|
#include <glib/gstdio.h>
|
|
|
|
|
2009-01-24 19:38:53 +00:00
|
|
|
/* uncomment to use GIO based file monitoring, though it is not completely stable yet */
|
2009-07-08 22:20:41 +00:00
|
|
|
/*#define USE_GIO_FILEMON 1*/
|
2011-06-17 22:51:17 +00:00
|
|
|
#include <gio/gio.h>
|
2009-01-18 18:19:58 +00:00
|
|
|
|
2014-05-24 14:37:24 +02:00
|
|
|
#include <gdk/gdkkeysyms.h>
|
|
|
|
|
2008-05-16 12:08:39 +00:00
|
|
|
GeanyFilePrefs file_prefs;
|
|
|
|
|
2014-05-18 17:31:51 -07:00
|
|
|
|
2013-10-31 14:14:00 +00:00
|
|
|
/** Dynamic array of GeanyDocument pointers.
|
2014-05-02 15:33:44 +01:00
|
|
|
* Once a pointer is added to this, it is never freed. This means the same document pointer
|
|
|
|
* can represent a different document later on, or it may have been closed and become invalid.
|
|
|
|
* For this reason, you should use document_find_by_id() instead of storing
|
|
|
|
* document pointers over time if there is a chance the user can close the
|
|
|
|
* document.
|
2008-07-24 15:17:55 +00:00
|
|
|
*
|
2013-10-31 14:14:00 +00:00
|
|
|
* @warning You must check @c GeanyDocument::is_valid when iterating over this array.
|
|
|
|
* This is done automatically if you use the foreach_document() macro.
|
2008-07-24 15:17:55 +00:00
|
|
|
*
|
2013-10-31 14:14:00 +00:00
|
|
|
* @note
|
2008-07-24 15:17:55 +00:00
|
|
|
* Never assume that the order of document pointers is the same as the order of notebook tabs.
|
2013-10-31 14:14:00 +00:00
|
|
|
* One reason is that notebook tabs can be reordered.
|
|
|
|
* Use @c document_get_from_page() to lookup a document from a notebook tab number.
|
2014-04-27 15:53:18 +01:00
|
|
|
*
|
|
|
|
* @see documents. */
|
2010-05-11 12:10:31 +00:00
|
|
|
GPtrArray *documents_array = NULL;
|
2006-10-09 16:08:53 +00:00
|
|
|
|
2008-06-02 15:31:59 +00:00
|
|
|
|
|
|
|
/* an undo action, also used for redo actions */
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
GTrashStack *next; /* pointer to the next stack element(required for the GTrashStack) */
|
|
|
|
guint type; /* to identify the action */
|
|
|
|
gpointer *data; /* the old value (before the change), in case of a redo action
|
|
|
|
* it contains the new value */
|
|
|
|
} undo_action;
|
|
|
|
|
2014-06-09 12:31:35 +02:00
|
|
|
/* Custom document info bar response IDs */
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
RESPONSE_DOCUMENT_RELOAD = 1,
|
|
|
|
RESPONSE_DOCUMENT_SAVE,
|
|
|
|
};
|
|
|
|
|
2008-06-02 15:31:59 +00:00
|
|
|
|
2014-05-02 15:33:44 +01:00
|
|
|
static guint doc_id_counter = 0;
|
|
|
|
|
|
|
|
|
2008-06-15 13:35:48 +00:00
|
|
|
static void document_undo_clear(GeanyDocument *doc);
|
|
|
|
static void document_redo_add(GeanyDocument *doc, guint type, gpointer data);
|
2011-11-02 22:44:26 -07:00
|
|
|
static gboolean remove_page(guint page_num);
|
2014-06-24 18:30:54 +02:00
|
|
|
static GtkWidget* document_show_message(GeanyDocument *doc, GtkMessageType msgtype,
|
|
|
|
void (*response_cb)(GtkWidget *info_bar, gint response_id, GeanyDocument *doc),
|
|
|
|
const gchar *btn_1, GtkResponseType response_1,
|
|
|
|
const gchar *btn_2, GtkResponseType response_2,
|
|
|
|
const gchar *btn_3, GtkResponseType response_3,
|
|
|
|
const gchar *extra_text, const gchar *format, ...) G_GNUC_PRINTF(11, 12);
|
2006-10-10 16:02:41 +00:00
|
|
|
|
2005-11-22 12:26:26 +00:00
|
|
|
|
2008-06-03 17:22:04 +00:00
|
|
|
/**
|
2010-03-15 13:01:16 +00:00
|
|
|
* Finds a document whose @c real_path field matches the given filename.
|
2008-06-03 17:22:04 +00:00
|
|
|
*
|
|
|
|
* @param realname The filename to search, which should be identical to the
|
|
|
|
* string returned by @c tm_get_real_path().
|
|
|
|
*
|
2009-07-21 22:12:13 +00:00
|
|
|
* @return The matching document, or @c NULL.
|
2008-06-03 17:22:04 +00:00
|
|
|
* @note This is only really useful when passing a @c TMWorkObject::file_name.
|
2010-03-15 13:01:16 +00:00
|
|
|
* @see GeanyDocument::real_path.
|
2008-06-03 17:22:04 +00:00
|
|
|
* @see document_find_by_filename().
|
2009-01-30 18:53:23 +00:00
|
|
|
*
|
|
|
|
* @since 0.15
|
2008-06-03 17:22:04 +00:00
|
|
|
**/
|
2008-06-15 13:35:48 +00:00
|
|
|
GeanyDocument* document_find_by_real_path(const gchar *realname)
|
2007-09-05 16:22:50 +00:00
|
|
|
{
|
|
|
|
guint i;
|
2008-06-03 17:22:04 +00:00
|
|
|
|
2009-04-15 22:47:33 +00:00
|
|
|
if (! realname)
|
2008-06-12 16:50:01 +00:00
|
|
|
return NULL; /* file doesn't exist on disk */
|
2007-09-05 16:22:50 +00:00
|
|
|
|
2008-05-29 17:00:54 +00:00
|
|
|
for (i = 0; i < documents_array->len; i++)
|
2007-09-05 16:22:50 +00:00
|
|
|
{
|
2008-06-03 17:22:04 +00:00
|
|
|
GeanyDocument *doc = documents[i];
|
2007-09-05 16:22:50 +00:00
|
|
|
|
2011-03-24 22:00:18 +00:00
|
|
|
if (! doc->is_valid || ! doc->real_path)
|
2009-04-05 21:07:40 +00:00
|
|
|
continue;
|
2007-09-05 16:22:50 +00:00
|
|
|
|
2011-10-31 16:47:18 +00:00
|
|
|
if (utils_filenamecmp(realname, doc->real_path) == 0)
|
2008-06-03 17:22:04 +00:00
|
|
|
{
|
2008-06-12 16:50:01 +00:00
|
|
|
return doc;
|
2008-06-03 17:22:04 +00:00
|
|
|
}
|
2007-09-05 16:22:50 +00:00
|
|
|
}
|
2008-06-12 16:50:01 +00:00
|
|
|
return NULL;
|
2007-09-05 16:22:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-06-03 17:22:04 +00:00
|
|
|
/* dereference symlinks, /../ junk in path and return locale encoding */
|
2007-09-05 16:22:50 +00:00
|
|
|
static gchar *get_real_path_from_utf8(const gchar *utf8_filename)
|
|
|
|
{
|
|
|
|
gchar *locale_name = utils_get_locale_from_utf8(utf8_filename);
|
|
|
|
gchar *realname = tm_get_real_path(locale_name);
|
|
|
|
|
|
|
|
g_free(locale_name);
|
|
|
|
return realname;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-02-17 18:00:42 +00:00
|
|
|
/**
|
2010-03-15 13:01:16 +00:00
|
|
|
* Finds a document with the given filename.
|
2008-06-03 17:22:04 +00:00
|
|
|
* This matches either an exact GeanyDocument::file_name string, or variant
|
|
|
|
* filenames with relative elements in the path (e.g. @c "/dir/..//name" will
|
|
|
|
* match @c "/name").
|
2008-02-17 18:00:42 +00:00
|
|
|
*
|
2008-06-03 17:22:04 +00:00
|
|
|
* @param utf8_filename The filename to search (in UTF-8 encoding).
|
2008-02-17 18:00:42 +00:00
|
|
|
*
|
2009-07-21 22:12:13 +00:00
|
|
|
* @return The matching document, or @c NULL.
|
2008-06-12 16:50:01 +00:00
|
|
|
* @see document_find_by_real_path().
|
2008-02-17 18:00:42 +00:00
|
|
|
**/
|
2008-06-15 13:35:48 +00:00
|
|
|
GeanyDocument *document_find_by_filename(const gchar *utf8_filename)
|
2005-11-22 12:26:26 +00:00
|
|
|
{
|
|
|
|
guint i;
|
2008-06-12 16:50:01 +00:00
|
|
|
GeanyDocument *doc;
|
|
|
|
gchar *realname;
|
2005-11-22 12:26:26 +00:00
|
|
|
|
2009-04-15 22:47:33 +00:00
|
|
|
g_return_val_if_fail(utf8_filename != NULL, NULL);
|
2007-09-05 16:22:50 +00:00
|
|
|
|
2008-06-03 17:22:04 +00:00
|
|
|
/* First search GeanyDocument::file_name, so we can find documents with a
|
|
|
|
* filename set but not saved on disk, like vcdiff produces */
|
2008-05-29 17:00:54 +00:00
|
|
|
for (i = 0; i < documents_array->len; i++)
|
2005-11-22 12:26:26 +00:00
|
|
|
{
|
2008-06-12 16:50:01 +00:00
|
|
|
doc = documents[i];
|
2007-09-05 16:22:50 +00:00
|
|
|
|
2011-03-24 22:00:18 +00:00
|
|
|
if (! doc->is_valid || doc->file_name == NULL)
|
2009-04-05 21:07:40 +00:00
|
|
|
continue;
|
2007-09-05 16:22:50 +00:00
|
|
|
|
2011-10-31 16:47:18 +00:00
|
|
|
if (utils_filenamecmp(utf8_filename, doc->file_name) == 0)
|
2007-09-05 16:22:50 +00:00
|
|
|
{
|
2008-06-12 16:50:01 +00:00
|
|
|
return doc;
|
2007-09-05 16:22:50 +00:00
|
|
|
}
|
2005-11-22 12:26:26 +00:00
|
|
|
}
|
2008-06-12 16:50:01 +00:00
|
|
|
/* Now try matching based on the realpath(), which is unique per file on disk */
|
|
|
|
realname = get_real_path_from_utf8(utf8_filename);
|
2008-06-15 13:35:48 +00:00
|
|
|
doc = document_find_by_real_path(realname);
|
2008-06-12 16:50:01 +00:00
|
|
|
g_free(realname);
|
|
|
|
return doc;
|
2005-11-22 12:26:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-06-12 16:50:01 +00:00
|
|
|
/* returns the document which has sci, or NULL. */
|
2008-06-15 13:35:48 +00:00
|
|
|
GeanyDocument *document_find_by_sci(ScintillaObject *sci)
|
2005-11-22 12:26:26 +00:00
|
|
|
{
|
|
|
|
guint i;
|
|
|
|
|
2009-04-15 22:47:33 +00:00
|
|
|
g_return_val_if_fail(sci != NULL, NULL);
|
2005-11-22 12:26:26 +00:00
|
|
|
|
2008-06-12 16:50:01 +00:00
|
|
|
for (i = 0; i < documents_array->len; i++)
|
2005-11-22 12:26:26 +00:00
|
|
|
{
|
2008-07-14 11:13:54 +00:00
|
|
|
if (documents[i]->is_valid && documents[i]->editor->sci == sci)
|
2008-06-12 16:50:01 +00:00
|
|
|
return documents[i];
|
2005-11-22 12:26:26 +00:00
|
|
|
}
|
2008-06-12 16:50:01 +00:00
|
|
|
return NULL;
|
2005-11-22 12:26:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-05-02 15:33:44 +01:00
|
|
|
/** Lookup an old document by its ID.
|
|
|
|
* Useful when the corresponding document may have been closed since the
|
|
|
|
* ID was retrieved.
|
2014-08-29 15:30:24 +02:00
|
|
|
* @param id The ID of the document to find
|
2014-05-02 15:33:44 +01:00
|
|
|
* @return @c NULL if the document is no longer open.
|
|
|
|
*
|
|
|
|
* Example:
|
|
|
|
* @code
|
|
|
|
* static guint id;
|
|
|
|
* GeanyDocument *doc = ...;
|
|
|
|
* id = doc->id; // store ID
|
|
|
|
* ...
|
|
|
|
* // time passes - the document may have been closed by now
|
|
|
|
* GeanyDocument *doc = document_find_by_id(id);
|
|
|
|
* gboolean still_open = (doc != NULL);
|
|
|
|
* @endcode
|
|
|
|
* @since 1.25. */
|
|
|
|
GeanyDocument *document_find_by_id(guint id)
|
|
|
|
{
|
|
|
|
guint i;
|
|
|
|
|
|
|
|
if (!id)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
foreach_document(i)
|
|
|
|
{
|
|
|
|
if (documents[i]->id == id)
|
|
|
|
return documents[i];
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-03-12 18:15:48 +00:00
|
|
|
/** Gets the notebook page index for a document.
|
2009-10-14 12:21:18 +00:00
|
|
|
* @param doc The document.
|
|
|
|
* @return The index.
|
|
|
|
* @since 0.19 */
|
2008-06-15 13:35:48 +00:00
|
|
|
gint document_get_notebook_page(GeanyDocument *doc)
|
2007-02-23 15:36:21 +00:00
|
|
|
{
|
2011-11-06 12:29:37 -08:00
|
|
|
GtkWidget *parent;
|
2014-05-27 17:55:00 +02:00
|
|
|
GtkWidget *child;
|
2011-11-06 12:29:37 -08:00
|
|
|
|
2009-04-15 22:47:33 +00:00
|
|
|
g_return_val_if_fail(doc != NULL, -1);
|
2007-02-23 15:36:21 +00:00
|
|
|
|
2014-05-27 17:55:00 +02:00
|
|
|
child = GTK_WIDGET(doc->editor->sci);
|
|
|
|
parent = gtk_widget_get_parent(child);
|
|
|
|
/* search for the direct notebook child, mirroring document_get_from_page() */
|
|
|
|
while (parent && ! GTK_IS_NOTEBOOK(parent))
|
|
|
|
{
|
|
|
|
child = parent;
|
|
|
|
parent = gtk_widget_get_parent(child);
|
|
|
|
}
|
2011-11-06 12:29:37 -08:00
|
|
|
|
2014-05-27 17:55:00 +02:00
|
|
|
return gtk_notebook_page_num(GTK_NOTEBOOK(main_widgets.notebook), child);
|
2011-11-06 12:29:37 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-11-13 06:05:53 -08:00
|
|
|
/*
|
|
|
|
* Recursively searches a containers children until it finds a
|
|
|
|
* Scintilla widget, or NULL if one was not found.
|
|
|
|
*/
|
2011-11-06 12:29:37 -08:00
|
|
|
static ScintillaObject *locate_sci_in_container(GtkWidget *container)
|
|
|
|
{
|
|
|
|
ScintillaObject *sci = NULL;
|
|
|
|
GList *children, *iter;
|
|
|
|
|
|
|
|
g_return_val_if_fail(GTK_IS_CONTAINER(container), NULL);
|
|
|
|
|
|
|
|
children = gtk_container_get_children(GTK_CONTAINER(container));
|
|
|
|
for (iter = children; iter != NULL; iter = g_list_next(iter))
|
|
|
|
{
|
|
|
|
if (IS_SCINTILLA(iter->data))
|
|
|
|
{
|
|
|
|
sci = SCINTILLA(iter->data);
|
|
|
|
break;
|
|
|
|
}
|
2011-11-13 06:05:53 -08:00
|
|
|
else if (GTK_IS_CONTAINER(iter->data))
|
|
|
|
{
|
|
|
|
sci = locate_sci_in_container(iter->data);
|
|
|
|
if (IS_SCINTILLA(sci))
|
|
|
|
break;
|
|
|
|
sci = NULL;
|
|
|
|
}
|
2011-11-06 12:29:37 -08:00
|
|
|
}
|
|
|
|
g_list_free(children);
|
|
|
|
|
|
|
|
return sci;
|
2007-02-23 15:36:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-02-17 18:00:42 +00:00
|
|
|
/**
|
2010-03-15 13:01:16 +00:00
|
|
|
* Finds the document for the given notebook page @a page_num.
|
2008-02-17 18:00:42 +00:00
|
|
|
*
|
|
|
|
* @param page_num The notebook page number to search.
|
|
|
|
*
|
2009-07-21 22:12:13 +00:00
|
|
|
* @return The corresponding document for the given notebook page, or @c NULL.
|
2008-02-17 18:00:42 +00:00
|
|
|
**/
|
2008-06-12 16:50:01 +00:00
|
|
|
GeanyDocument *document_get_from_page(guint page_num)
|
2005-11-22 12:26:26 +00:00
|
|
|
{
|
2011-11-06 12:29:37 -08:00
|
|
|
GtkWidget *parent;
|
2006-05-08 01:57:25 +00:00
|
|
|
ScintillaObject *sci;
|
2008-06-12 16:50:01 +00:00
|
|
|
|
2009-04-15 22:47:33 +00:00
|
|
|
if (page_num >= documents_array->len)
|
2008-06-12 16:50:01 +00:00
|
|
|
return NULL;
|
2006-01-11 18:38:17 +00:00
|
|
|
|
2011-11-06 12:29:37 -08:00
|
|
|
parent = gtk_notebook_get_nth_page(GTK_NOTEBOOK(main_widgets.notebook), page_num);
|
|
|
|
g_return_val_if_fail(GTK_IS_BOX(parent), NULL);
|
|
|
|
|
|
|
|
sci = locate_sci_in_container(parent);
|
|
|
|
g_return_val_if_fail(IS_SCINTILLA(sci), NULL);
|
2005-11-22 12:26:26 +00:00
|
|
|
|
2008-06-15 13:35:48 +00:00
|
|
|
return document_find_by_sci(sci);
|
2005-11-22 12:26:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-02-17 18:00:42 +00:00
|
|
|
/**
|
2010-03-15 13:01:16 +00:00
|
|
|
* Finds the current document.
|
2008-02-17 18:00:42 +00:00
|
|
|
*
|
2009-08-09 20:31:03 +00:00
|
|
|
* @return A pointer to the current document or @c NULL if there are no opened documents.
|
2008-02-17 18:00:42 +00:00
|
|
|
**/
|
2008-06-12 16:50:01 +00:00
|
|
|
GeanyDocument *document_get_current(void)
|
2005-11-22 12:26:26 +00:00
|
|
|
{
|
2014-05-27 17:59:48 +02:00
|
|
|
gint cur_page = gtk_notebook_get_current_page(GTK_NOTEBOOK(main_widgets.notebook));
|
2005-11-22 12:26:26 +00:00
|
|
|
|
2009-04-15 22:47:33 +00:00
|
|
|
if (cur_page == -1)
|
2008-06-12 16:50:01 +00:00
|
|
|
return NULL;
|
2005-11-22 12:26:26 +00:00
|
|
|
else
|
2014-05-27 17:59:48 +02:00
|
|
|
return document_get_from_page((guint) cur_page);
|
2005-11-22 12:26:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-11-10 01:41:01 +01:00
|
|
|
void document_init_doclist(void)
|
2005-11-22 12:26:26 +00:00
|
|
|
{
|
2008-05-29 17:00:54 +00:00
|
|
|
documents_array = g_ptr_array_new();
|
2006-10-09 16:08:53 +00:00
|
|
|
}
|
2005-11-22 12:26:26 +00:00
|
|
|
|
2006-10-09 16:08:53 +00:00
|
|
|
|
2013-11-10 01:41:01 +01:00
|
|
|
void document_finalize(void)
|
2006-10-09 16:08:53 +00:00
|
|
|
{
|
2011-10-10 21:16:53 +02:00
|
|
|
guint i;
|
|
|
|
|
|
|
|
for (i = 0; i < documents_array->len; i++)
|
|
|
|
g_free(documents[i]);
|
2008-05-29 17:00:54 +00:00
|
|
|
g_ptr_array_free(documents_array, TRUE);
|
2005-11-22 12:26:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-04-21 20:52:51 +00:00
|
|
|
/**
|
|
|
|
* Returns the last part of the filename of the given GeanyDocument. The result is also
|
2009-07-21 22:12:13 +00:00
|
|
|
* truncated to a maximum of @a length characters in case the filename is very long.
|
2009-04-21 20:52:51 +00:00
|
|
|
*
|
|
|
|
* @param doc The document to use.
|
|
|
|
* @param length The length of the resulting string or -1 to use a default value.
|
|
|
|
*
|
2009-07-21 22:12:13 +00:00
|
|
|
* @return The ellipsized last part of the filename of @a doc, should be freed when no
|
2009-04-21 20:52:51 +00:00
|
|
|
* longer needed.
|
|
|
|
*
|
|
|
|
* @since 0.17
|
|
|
|
*/
|
|
|
|
/* TODO make more use of this */
|
|
|
|
gchar *document_get_basename_for_display(GeanyDocument *doc, gint length)
|
|
|
|
{
|
|
|
|
gchar *base_name, *short_name;
|
|
|
|
|
|
|
|
g_return_val_if_fail(doc != NULL, NULL);
|
|
|
|
|
|
|
|
if (length < 0)
|
|
|
|
length = 30;
|
|
|
|
|
|
|
|
base_name = g_path_get_basename(DOC_FILENAME(doc));
|
2011-06-17 22:52:17 +00:00
|
|
|
short_name = utils_str_middle_truncate(base_name, (guint)length);
|
2009-04-21 20:52:51 +00:00
|
|
|
|
|
|
|
g_free(base_name);
|
|
|
|
|
|
|
|
return short_name;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-11-18 20:13:29 +00:00
|
|
|
void document_update_tab_label(GeanyDocument *doc)
|
|
|
|
{
|
2009-04-21 20:52:51 +00:00
|
|
|
gchar *short_name;
|
2008-11-18 20:13:50 +00:00
|
|
|
GtkWidget *parent;
|
2008-11-18 20:13:29 +00:00
|
|
|
|
2009-04-15 22:47:33 +00:00
|
|
|
g_return_if_fail(doc != NULL);
|
2008-11-18 20:13:50 +00:00
|
|
|
|
2009-04-21 20:52:51 +00:00
|
|
|
short_name = document_get_basename_for_display(doc, -1);
|
|
|
|
|
2008-12-08 12:59:13 +00:00
|
|
|
/* we need to use the event box for the tooltip, labels don't get the necessary events */
|
2008-11-18 20:13:50 +00:00
|
|
|
parent = gtk_widget_get_parent(doc->priv->tab_label);
|
2008-12-08 12:59:13 +00:00
|
|
|
parent = gtk_widget_get_parent(parent);
|
2008-11-18 20:13:50 +00:00
|
|
|
|
2009-04-21 20:52:51 +00:00
|
|
|
gtk_label_set_text(GTK_LABEL(doc->priv->tab_label), short_name);
|
2008-11-18 20:13:29 +00:00
|
|
|
|
2011-06-13 18:41:50 +00:00
|
|
|
gtk_widget_set_tooltip_text(parent, DOC_FILENAME(doc));
|
2008-11-18 20:13:29 +00:00
|
|
|
|
2009-04-21 20:52:51 +00:00
|
|
|
g_free(short_name);
|
2008-11-18 20:13:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-02-17 18:00:42 +00:00
|
|
|
/**
|
2010-03-15 13:01:16 +00:00
|
|
|
* Updates the tab labels, the status bar, the window title and some save-sensitive buttons
|
2008-02-17 18:00:42 +00:00
|
|
|
* according to the document's save state.
|
|
|
|
* This is called by Geany mostly when opening or saving files.
|
|
|
|
*
|
2008-06-12 16:50:01 +00:00
|
|
|
* @param doc The document to use.
|
|
|
|
* @param changed Whether the document state should indicate changes have been made.
|
2008-02-17 18:00:42 +00:00
|
|
|
**/
|
2008-06-15 13:35:48 +00:00
|
|
|
void document_set_text_changed(GeanyDocument *doc, gboolean changed)
|
2005-11-22 12:26:26 +00:00
|
|
|
{
|
2009-04-15 22:47:33 +00:00
|
|
|
g_return_if_fail(doc != NULL);
|
2008-06-12 16:50:01 +00:00
|
|
|
|
2008-06-15 13:35:48 +00:00
|
|
|
doc->changed = changed;
|
2008-06-12 16:50:01 +00:00
|
|
|
|
2009-04-15 22:47:33 +00:00
|
|
|
if (! main_status.quitting)
|
2005-11-22 12:26:26 +00:00
|
|
|
{
|
2008-06-15 13:35:48 +00:00
|
|
|
ui_update_tab_status(doc);
|
|
|
|
ui_save_buttons_toggle(changed);
|
|
|
|
ui_set_window_title(doc);
|
|
|
|
ui_update_statusbar(doc, -1);
|
2006-09-06 16:09:08 +00:00
|
|
|
}
|
2005-11-22 12:26:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-06-15 13:35:48 +00:00
|
|
|
/* returns the next free place in the document list,
|
2008-05-29 17:00:54 +00:00
|
|
|
* or -1 if the documents_array is full */
|
2008-02-20 11:24:23 +00:00
|
|
|
static gint document_get_new_idx(void)
|
2006-10-09 16:08:53 +00:00
|
|
|
{
|
|
|
|
guint i;
|
|
|
|
|
2008-05-29 17:00:54 +00:00
|
|
|
for (i = 0; i < documents_array->len; i++)
|
2006-10-09 16:08:53 +00:00
|
|
|
{
|
2008-07-11 14:11:53 +00:00
|
|
|
if (documents[i]->editor == NULL)
|
2006-10-09 16:08:53 +00:00
|
|
|
{
|
|
|
|
return (gint) i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-06-20 12:34:13 +00:00
|
|
|
static void queue_colourise(GeanyDocument *doc)
|
|
|
|
{
|
|
|
|
/* Colourise the editor before it is next drawn */
|
2008-09-26 17:28:50 +00:00
|
|
|
doc->priv->colourise_needed = TRUE;
|
2008-06-20 12:34:13 +00:00
|
|
|
|
|
|
|
/* If the editor doesn't need drawing (e.g. after saving the current
|
|
|
|
* document), we need to force a redraw, so the expose event is triggered.
|
|
|
|
* This ensures we don't start colourising before all documents are opened/saved,
|
|
|
|
* only once the editor is drawn. */
|
2008-07-14 11:13:54 +00:00
|
|
|
gtk_widget_queue_draw(GTK_WIDGET(doc->editor->sci));
|
2008-06-20 12:34:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-06-17 22:51:51 +00:00
|
|
|
#ifdef USE_GIO_FILEMON
|
2009-01-21 15:50:48 +00:00
|
|
|
static void monitor_file_changed_cb(G_GNUC_UNUSED GFileMonitor *monitor, G_GNUC_UNUSED GFile *file,
|
2009-07-08 22:20:17 +00:00
|
|
|
G_GNUC_UNUSED GFile *other_file, GFileMonitorEvent event,
|
|
|
|
GeanyDocument *doc)
|
2009-01-18 18:19:58 +00:00
|
|
|
{
|
2009-04-15 22:47:33 +00:00
|
|
|
g_return_if_fail(doc != NULL);
|
2009-01-24 19:38:31 +00:00
|
|
|
|
2009-01-18 18:19:58 +00:00
|
|
|
if (file_prefs.disk_check_timeout == 0)
|
|
|
|
return;
|
|
|
|
|
2009-07-08 22:20:17 +00:00
|
|
|
geany_debug("%s: event: %d previous file status: %d",
|
|
|
|
G_STRFUNC, event, doc->priv->file_disk_status);
|
2009-01-18 18:19:58 +00:00
|
|
|
switch (event)
|
|
|
|
{
|
2009-01-21 15:50:48 +00:00
|
|
|
case G_FILE_MONITOR_EVENT_CHANGES_DONE_HINT:
|
2009-01-18 18:19:58 +00:00
|
|
|
{
|
|
|
|
if (doc->priv->file_disk_status == FILE_IGNORE)
|
|
|
|
doc->priv->file_disk_status = FILE_OK;
|
2009-07-08 22:20:17 +00:00
|
|
|
else
|
|
|
|
doc->priv->file_disk_status = FILE_CHANGED;
|
|
|
|
g_message("%s: FILE_CHANGED", G_STRFUNC);
|
2009-01-18 18:19:58 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case G_FILE_MONITOR_EVENT_DELETED:
|
|
|
|
{
|
2009-07-08 22:20:17 +00:00
|
|
|
doc->priv->file_disk_status = FILE_CHANGED;
|
|
|
|
g_message("%s: FILE_MISSING", G_STRFUNC);
|
2009-01-21 15:50:48 +00:00
|
|
|
break;
|
|
|
|
}
|
2009-07-08 22:20:17 +00:00
|
|
|
default:
|
2009-01-18 18:19:58 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (doc->priv->file_disk_status != FILE_OK)
|
|
|
|
{
|
|
|
|
ui_update_tab_status(doc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2010-04-25 17:42:33 +00:00
|
|
|
static void document_stop_file_monitoring(GeanyDocument *doc)
|
2009-01-18 18:19:58 +00:00
|
|
|
{
|
2009-04-15 22:47:33 +00:00
|
|
|
g_return_if_fail(doc != NULL);
|
2009-01-18 18:19:58 +00:00
|
|
|
|
|
|
|
if (doc->priv->monitor != NULL)
|
|
|
|
{
|
|
|
|
g_object_unref(doc->priv->monitor);
|
|
|
|
doc->priv->monitor = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-01-21 15:50:48 +00:00
|
|
|
static void monitor_file_setup(GeanyDocument *doc)
|
2009-01-18 18:19:58 +00:00
|
|
|
{
|
2009-04-15 22:47:33 +00:00
|
|
|
g_return_if_fail(doc != NULL);
|
2009-01-18 18:19:58 +00:00
|
|
|
/* Disable file monitoring completely for remote files (i.e. remote GIO files) as GFileMonitor
|
|
|
|
* doesn't work at all for remote files and legacy polling is too slow. */
|
|
|
|
if (! doc->priv->is_remote)
|
|
|
|
{
|
2011-06-17 22:51:51 +00:00
|
|
|
#ifdef USE_GIO_FILEMON
|
2009-01-18 18:19:58 +00:00
|
|
|
gchar *locale_filename;
|
|
|
|
|
|
|
|
/* stop any previous monitoring */
|
|
|
|
document_stop_file_monitoring(doc);
|
|
|
|
|
|
|
|
locale_filename = utils_get_locale_from_utf8(doc->file_name);
|
|
|
|
if (locale_filename != NULL && g_file_test(locale_filename, G_FILE_TEST_EXISTS))
|
|
|
|
{
|
|
|
|
/* get a file monitor and connect to the 'changed' signal */
|
2009-07-08 22:20:17 +00:00
|
|
|
GFile *file = g_file_new_for_path(locale_filename);
|
2009-01-18 18:19:58 +00:00
|
|
|
doc->priv->monitor = g_file_monitor_file(file, G_FILE_MONITOR_NONE, NULL, NULL);
|
2009-01-21 15:50:48 +00:00
|
|
|
g_signal_connect(doc->priv->monitor, "changed",
|
|
|
|
G_CALLBACK(monitor_file_changed_cb), doc);
|
2009-01-18 18:19:58 +00:00
|
|
|
|
|
|
|
/* we set the rate limit according to the GUI pref but it's most probably not used */
|
|
|
|
g_file_monitor_set_rate_limit(doc->priv->monitor, file_prefs.disk_check_timeout * 1000);
|
|
|
|
|
|
|
|
g_object_unref(file);
|
|
|
|
}
|
|
|
|
g_free(locale_filename);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
doc->priv->file_disk_status = FILE_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-02-28 14:32:38 +00:00
|
|
|
void document_try_focus(GeanyDocument *doc, GtkWidget *source_widget)
|
2009-09-03 12:04:27 +00:00
|
|
|
{
|
|
|
|
/* doc might not be valid e.g. if user closed a tab whilst Geany is opening files */
|
|
|
|
if (DOC_VALID(doc))
|
|
|
|
{
|
|
|
|
GtkWidget *sci = GTK_WIDGET(doc->editor->sci);
|
|
|
|
GtkWidget *focusw = gtk_window_get_focus(GTK_WINDOW(main_widgets.window));
|
|
|
|
|
2010-02-28 14:32:38 +00:00
|
|
|
if (source_widget == NULL)
|
|
|
|
source_widget = doc->priv->tag_tree;
|
|
|
|
|
|
|
|
if (focusw == source_widget)
|
2009-09-03 12:04:27 +00:00
|
|
|
gtk_widget_grab_focus(sci);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static gboolean on_idle_focus(gpointer doc)
|
|
|
|
{
|
2010-02-28 14:32:38 +00:00
|
|
|
document_try_focus(doc, NULL);
|
2009-09-03 12:04:27 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-03-27 13:59:51 +00:00
|
|
|
/* Creates a new document and editor, adding a tab in the notebook.
|
2008-07-24 15:17:55 +00:00
|
|
|
* @return The created document */
|
2008-06-15 13:35:48 +00:00
|
|
|
static GeanyDocument *document_create(const gchar *utf8_filename)
|
2008-03-27 13:59:51 +00:00
|
|
|
{
|
2009-09-03 12:04:27 +00:00
|
|
|
GeanyDocument *doc;
|
2008-06-15 13:35:48 +00:00
|
|
|
gint new_idx;
|
2008-05-22 14:41:28 +00:00
|
|
|
gint cur_pages = gtk_notebook_get_n_pages(GTK_NOTEBOOK(main_widgets.notebook));
|
2008-03-27 13:59:51 +00:00
|
|
|
|
|
|
|
if (cur_pages == 1)
|
|
|
|
{
|
2011-11-02 22:44:26 -07:00
|
|
|
doc = document_get_current();
|
2010-09-30 14:49:54 +00:00
|
|
|
/* remove the empty document first */
|
2011-11-02 22:44:26 -07:00
|
|
|
if (doc != NULL && doc->file_name == NULL && ! doc->changed)
|
2010-09-30 14:49:54 +00:00
|
|
|
/* prevent immediately opening another new doc with
|
|
|
|
* new_document_after_close pref */
|
|
|
|
remove_page(0);
|
2008-03-27 13:59:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
new_idx = document_get_new_idx();
|
|
|
|
if (new_idx == -1) /* expand the array, no free places */
|
|
|
|
{
|
2011-11-02 22:44:26 -07:00
|
|
|
doc = g_new0(GeanyDocument, 1);
|
2008-05-29 17:00:54 +00:00
|
|
|
|
|
|
|
new_idx = documents_array->len;
|
2011-11-02 22:44:26 -07:00
|
|
|
g_ptr_array_add(documents_array, doc);
|
2008-03-27 13:59:51 +00:00
|
|
|
}
|
2012-01-03 10:21:09 -08:00
|
|
|
|
2009-09-03 12:04:27 +00:00
|
|
|
doc = documents[new_idx];
|
2012-01-03 10:21:09 -08:00
|
|
|
|
2011-11-02 22:44:26 -07:00
|
|
|
/* initialize default document settings */
|
|
|
|
doc->priv = g_new0(GeanyDocumentPrivate, 1);
|
2014-05-02 15:33:44 +01:00
|
|
|
doc->id = ++doc_id_counter;
|
2009-09-03 12:04:27 +00:00
|
|
|
doc->index = new_idx;
|
|
|
|
doc->file_name = g_strdup(utf8_filename);
|
|
|
|
doc->editor = editor_create(doc);
|
2011-11-02 22:44:26 -07:00
|
|
|
#ifndef USE_GIO_FILEMON
|
|
|
|
doc->priv->last_check = time(NULL);
|
|
|
|
#endif
|
2008-03-27 13:59:51 +00:00
|
|
|
|
2009-09-28 11:16:59 +00:00
|
|
|
sidebar_openfiles_add(doc); /* sets doc->iter */
|
2006-12-05 10:37:36 +00:00
|
|
|
|
2009-09-03 12:04:27 +00:00
|
|
|
notebook_new_tab(doc);
|
2006-12-05 10:37:36 +00:00
|
|
|
|
2008-02-27 13:17:29 +00:00
|
|
|
/* select document in sidebar */
|
2007-01-12 13:04:39 +00:00
|
|
|
{
|
|
|
|
GtkTreeSelection *sel;
|
|
|
|
|
|
|
|
sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(tv.tree_openfiles));
|
2009-09-03 12:04:27 +00:00
|
|
|
gtk_tree_selection_select_iter(sel, &doc->priv->iter);
|
2007-01-12 13:04:39 +00:00
|
|
|
}
|
|
|
|
|
2007-07-20 12:06:51 +00:00
|
|
|
ui_document_buttons_update();
|
2006-12-05 10:37:36 +00:00
|
|
|
|
2009-09-03 12:04:27 +00:00
|
|
|
doc->is_valid = TRUE; /* do this last to prevent UI updating with NULL items. */
|
|
|
|
return doc;
|
2005-11-22 12:26:26 +00:00
|
|
|
}
|
|
|
|
|
2009-09-03 12:04:27 +00:00
|
|
|
|
2008-07-01 14:20:16 +00:00
|
|
|
/**
|
2010-03-15 13:01:16 +00:00
|
|
|
* Closes the given document.
|
2008-07-01 14:20:16 +00:00
|
|
|
*
|
|
|
|
* @param doc The document to remove.
|
|
|
|
*
|
2009-07-21 22:12:13 +00:00
|
|
|
* @return @c TRUE if the document was actually removed or @c FALSE otherwise.
|
2009-01-30 18:53:23 +00:00
|
|
|
*
|
|
|
|
* @since 0.15
|
2008-07-01 14:20:16 +00:00
|
|
|
**/
|
|
|
|
gboolean document_close(GeanyDocument *doc)
|
|
|
|
{
|
2009-04-15 22:47:33 +00:00
|
|
|
g_return_val_if_fail(doc, FALSE);
|
2008-07-01 14:20:16 +00:00
|
|
|
|
|
|
|
return document_remove_page(document_get_notebook_page(doc));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-11-02 22:44:26 -07:00
|
|
|
/* Call document_remove_page() instead, this is only needed for document_create()
|
|
|
|
* to prevent re-opening a new document when the last document is closed (if enabled). */
|
2010-09-30 14:49:54 +00:00
|
|
|
static gboolean remove_page(guint page_num)
|
2005-11-22 12:26:26 +00:00
|
|
|
{
|
2008-06-15 13:35:48 +00:00
|
|
|
GeanyDocument *doc = document_get_from_page(page_num);
|
2005-11-22 12:26:26 +00:00
|
|
|
|
2011-11-02 22:44:26 -07:00
|
|
|
g_return_val_if_fail(doc != NULL, FALSE);
|
2008-09-18 12:43:41 +00:00
|
|
|
|
2008-09-16 15:47:26 +00:00
|
|
|
if (doc->changed && ! dialogs_show_unsaved_file(doc))
|
|
|
|
return FALSE;
|
2008-09-18 12:43:41 +00:00
|
|
|
|
2008-09-16 15:47:26 +00:00
|
|
|
/* tell any plugins that the document is about to be closed */
|
2008-11-21 17:33:35 +00:00
|
|
|
g_signal_emit_by_name(geany_object, "document-close", doc);
|
2008-06-02 15:31:59 +00:00
|
|
|
|
2008-09-16 15:47:26 +00:00
|
|
|
/* Checking real_path makes it likely the file exists on disk */
|
|
|
|
if (! main_status.closing_all && doc->real_path != NULL)
|
2011-11-03 20:45:52 +01:00
|
|
|
ui_add_recent_document(doc);
|
2008-09-16 15:47:26 +00:00
|
|
|
|
2009-06-05 15:57:01 +00:00
|
|
|
doc->is_valid = FALSE;
|
2014-05-02 15:33:44 +01:00
|
|
|
doc->id = 0;
|
2009-06-05 15:57:01 +00:00
|
|
|
|
2014-04-12 15:59:07 +02:00
|
|
|
if (main_status.quitting)
|
|
|
|
{
|
|
|
|
/* we need to destroy the ScintillaWidget so our handlers on it are
|
|
|
|
* disconnected before we free any data they may use (like the editor).
|
|
|
|
* when not quitting, this is handled by removing the notebook page. */
|
2014-07-26 14:55:58 +02:00
|
|
|
gtk_notebook_remove_page(GTK_NOTEBOOK(main_widgets.notebook), page_num);
|
2014-04-12 15:59:07 +02:00
|
|
|
}
|
|
|
|
else
|
2010-05-16 17:43:24 +00:00
|
|
|
{
|
|
|
|
notebook_remove_page(page_num);
|
|
|
|
sidebar_remove_document(doc);
|
|
|
|
navqueue_remove_file(doc->file_name);
|
|
|
|
msgwin_status_add(_("File %s closed."), DOC_FILENAME(doc));
|
|
|
|
}
|
2008-09-16 15:47:26 +00:00
|
|
|
g_free(doc->encoding);
|
2008-09-26 17:28:50 +00:00
|
|
|
g_free(doc->priv->saved_encoding.encoding);
|
2008-09-16 15:47:26 +00:00
|
|
|
g_free(doc->file_name);
|
|
|
|
g_free(doc->real_path);
|
|
|
|
tm_workspace_remove_object(doc->tm_file, TRUE, TRUE);
|
|
|
|
|
2013-03-17 17:28:46 +01:00
|
|
|
if (doc->priv->tag_tree)
|
|
|
|
gtk_widget_destroy(doc->priv->tag_tree);
|
|
|
|
|
2008-09-16 15:47:26 +00:00
|
|
|
editor_destroy(doc->editor);
|
2011-11-02 22:44:26 -07:00
|
|
|
doc->editor = NULL; /* needs to be NULL for document_undo_clear() call below */
|
2008-09-16 15:47:26 +00:00
|
|
|
|
2009-01-18 18:19:58 +00:00
|
|
|
document_stop_file_monitoring(doc);
|
|
|
|
|
2008-09-16 15:47:26 +00:00
|
|
|
document_undo_clear(doc);
|
2011-11-02 22:44:26 -07:00
|
|
|
|
2008-09-26 17:28:50 +00:00
|
|
|
g_free(doc->priv);
|
|
|
|
|
2011-11-02 22:44:26 -07:00
|
|
|
/* reset document settings to defaults for re-use */
|
|
|
|
memset(doc, 0, sizeof(GeanyDocument));
|
|
|
|
|
2008-09-16 15:47:26 +00:00
|
|
|
if (gtk_notebook_get_n_pages(GTK_NOTEBOOK(main_widgets.notebook)) == 0)
|
|
|
|
{
|
2009-09-28 11:16:59 +00:00
|
|
|
sidebar_update_tag_list(NULL, FALSE);
|
2008-09-16 15:47:26 +00:00
|
|
|
ui_set_window_title(NULL);
|
|
|
|
ui_save_buttons_toggle(FALSE);
|
2010-05-22 19:38:49 +00:00
|
|
|
ui_update_popup_reundo_items(NULL);
|
2008-09-16 15:47:26 +00:00
|
|
|
ui_document_buttons_update();
|
|
|
|
build_menu_update(NULL);
|
2005-11-22 12:26:26 +00:00
|
|
|
}
|
2008-09-16 15:47:26 +00:00
|
|
|
return TRUE;
|
2005-11-22 12:26:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-09-30 14:49:54 +00:00
|
|
|
/**
|
|
|
|
* Removes the given notebook tab at @a page_num and clears all related information
|
|
|
|
* in the document list.
|
|
|
|
*
|
|
|
|
* @param page_num The notebook page number to remove.
|
|
|
|
*
|
|
|
|
* @return @c TRUE if the document was actually removed or @c FALSE otherwise.
|
|
|
|
**/
|
|
|
|
gboolean document_remove_page(guint page_num)
|
|
|
|
{
|
|
|
|
gboolean done = remove_page(page_num);
|
|
|
|
|
|
|
|
if (done && ui_prefs.new_document_after_close)
|
|
|
|
document_new_file_if_non_open();
|
|
|
|
|
|
|
|
return done;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-02-27 13:17:29 +00:00
|
|
|
/* used to keep a record of the unchanged document state encoding */
|
2008-06-15 13:35:48 +00:00
|
|
|
static void store_saved_encoding(GeanyDocument *doc)
|
2006-10-24 13:41:34 +00:00
|
|
|
{
|
2008-09-26 17:28:50 +00:00
|
|
|
g_free(doc->priv->saved_encoding.encoding);
|
|
|
|
doc->priv->saved_encoding.encoding = g_strdup(doc->encoding);
|
|
|
|
doc->priv->saved_encoding.has_bom = doc->has_bom;
|
2006-10-24 13:41:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-04-08 14:07:17 +00:00
|
|
|
/* Opens a new empty document only if there are no other documents open */
|
2009-01-11 18:29:39 +00:00
|
|
|
GeanyDocument *document_new_file_if_non_open(void)
|
2008-04-08 14:07:17 +00:00
|
|
|
{
|
2008-05-22 14:41:28 +00:00
|
|
|
if (gtk_notebook_get_n_pages(GTK_NOTEBOOK(main_widgets.notebook)) == 0)
|
2008-04-08 14:07:17 +00:00
|
|
|
return document_new_file(NULL, NULL, NULL);
|
|
|
|
|
2008-06-15 13:35:48 +00:00
|
|
|
return NULL;
|
2008-04-08 14:07:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-02-17 18:00:42 +00:00
|
|
|
/**
|
2008-07-24 12:12:47 +00:00
|
|
|
* Creates a new document.
|
2010-11-10 13:13:28 +00:00
|
|
|
* Line endings in @a text will be converted to the default setting.
|
2010-03-15 13:01:16 +00:00
|
|
|
* Afterwards, the @c "document-new" signal is emitted for plugins.
|
2008-02-17 18:00:42 +00:00
|
|
|
*
|
2008-07-24 12:12:47 +00:00
|
|
|
* @param utf8_filename The file name in UTF-8 encoding, or @c NULL to open a file as "untitled".
|
2008-02-17 18:00:42 +00:00
|
|
|
* @param ft The filetype to set or @c NULL to detect it from @a filename if not @c NULL.
|
|
|
|
* @param text The initial content of the file (in UTF-8 encoding), or @c NULL.
|
|
|
|
*
|
2008-06-12 16:50:01 +00:00
|
|
|
* @return The new document.
|
2008-02-17 18:00:42 +00:00
|
|
|
**/
|
2010-08-01 17:20:50 +00:00
|
|
|
GeanyDocument *document_new_file(const gchar *utf8_filename, GeanyFiletype *ft, const gchar *text)
|
2005-11-22 12:26:26 +00:00
|
|
|
{
|
2009-07-21 16:23:05 +00:00
|
|
|
GeanyDocument *doc;
|
|
|
|
|
|
|
|
if (utf8_filename && g_path_is_absolute(utf8_filename))
|
|
|
|
{
|
|
|
|
gchar *tmp;
|
|
|
|
tmp = utils_strdupa(utf8_filename); /* work around const */
|
|
|
|
utils_tidy_path(tmp);
|
|
|
|
utf8_filename = tmp;
|
|
|
|
}
|
|
|
|
doc = document_create(utf8_filename);
|
2005-11-22 12:26:26 +00:00
|
|
|
|
2009-04-15 22:47:33 +00:00
|
|
|
g_assert(doc != NULL);
|
2006-07-22 01:29:10 +00:00
|
|
|
|
2008-07-14 11:13:54 +00:00
|
|
|
sci_set_undo_collection(doc->editor->sci, FALSE); /* avoid creation of an undo action */
|
2007-09-03 16:09:53 +00:00
|
|
|
if (text)
|
2010-08-01 17:20:50 +00:00
|
|
|
{
|
|
|
|
GString *template = g_string_new(text);
|
|
|
|
utils_ensure_same_eol_characters(template, file_prefs.default_eol_character);
|
|
|
|
|
|
|
|
sci_set_text(doc->editor->sci, template->str);
|
|
|
|
g_string_free(template, TRUE);
|
|
|
|
}
|
2007-09-03 16:09:53 +00:00
|
|
|
else
|
2008-07-14 11:13:54 +00:00
|
|
|
sci_clear_all(doc->editor->sci);
|
2005-11-22 12:26:26 +00:00
|
|
|
|
2008-07-14 11:13:54 +00:00
|
|
|
sci_set_eol_mode(doc->editor->sci, file_prefs.default_eol_character);
|
2008-05-14 15:36:27 +00:00
|
|
|
|
2008-07-14 11:13:54 +00:00
|
|
|
sci_set_undo_collection(doc->editor->sci, TRUE);
|
|
|
|
sci_empty_undo_buffer(doc->editor->sci);
|
2006-10-10 16:02:41 +00:00
|
|
|
|
2008-06-15 13:35:48 +00:00
|
|
|
doc->encoding = g_strdup(encodings[file_prefs.default_new_encoding].charset);
|
2008-02-27 13:17:29 +00:00
|
|
|
/* store the opened encoding for undo/redo */
|
2008-06-15 13:35:48 +00:00
|
|
|
store_saved_encoding(doc);
|
2006-10-24 13:41:34 +00:00
|
|
|
|
2008-07-24 12:12:47 +00:00
|
|
|
if (ft == NULL && utf8_filename != NULL) /* guess the filetype from the filename if one is given */
|
2008-07-23 14:44:27 +00:00
|
|
|
ft = filetypes_detect_from_document(doc);
|
2007-02-15 23:56:15 +00:00
|
|
|
|
2011-11-10 19:18:02 -08:00
|
|
|
document_set_filetype(doc, ft); /* also re-parses tags */
|
2009-06-17 10:46:43 +00:00
|
|
|
|
2008-06-15 13:35:48 +00:00
|
|
|
ui_set_window_title(doc);
|
|
|
|
build_menu_update(doc);
|
|
|
|
document_set_text_changed(doc, FALSE);
|
|
|
|
ui_document_show_hide(doc); /* update the document menu */
|
2006-10-10 16:02:41 +00:00
|
|
|
|
2014-06-27 22:05:46 +02:00
|
|
|
sci_set_line_numbers(doc->editor->sci, editor_prefs.show_linenumber_margin);
|
2009-09-03 12:04:27 +00:00
|
|
|
/* bring it in front, jump to the start and grab the focus */
|
|
|
|
editor_goto_pos(doc->editor, 0, FALSE);
|
2010-02-28 14:32:38 +00:00
|
|
|
document_try_focus(doc, NULL);
|
2005-11-22 12:26:26 +00:00
|
|
|
|
2011-06-17 22:51:51 +00:00
|
|
|
#ifdef USE_GIO_FILEMON
|
2009-01-21 15:50:48 +00:00
|
|
|
monitor_file_setup(doc);
|
2009-01-28 16:18:22 +00:00
|
|
|
#else
|
|
|
|
doc->priv->mtime = time(NULL);
|
|
|
|
#endif
|
2009-01-18 18:19:58 +00:00
|
|
|
|
2008-02-27 13:17:29 +00:00
|
|
|
/* "the" SCI signal (connect after initial setup(i.e. adding text)) */
|
2008-12-18 19:23:32 +00:00
|
|
|
g_signal_connect(doc->editor->sci, "sci-notify", G_CALLBACK(editor_sci_notify_cb), doc->editor);
|
2006-09-06 16:09:08 +00:00
|
|
|
|
2008-11-21 17:33:35 +00:00
|
|
|
g_signal_emit_by_name(geany_object, "document-new", doc);
|
2007-08-10 16:11:17 +00:00
|
|
|
|
2007-10-24 10:52:48 +00:00
|
|
|
msgwin_status_add(_("New file \"%s\" opened."),
|
2008-06-18 13:36:33 +00:00
|
|
|
DOC_FILENAME(doc));
|
2007-02-15 23:56:15 +00:00
|
|
|
|
2008-06-15 13:35:48 +00:00
|
|
|
return doc;
|
2005-11-22 12:26:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-02-17 18:00:42 +00:00
|
|
|
/**
|
2010-03-15 13:01:16 +00:00
|
|
|
* Opens a document specified by @a locale_filename.
|
|
|
|
* Afterwards, the @c "document-open" signal is emitted for plugins.
|
2008-02-17 18:00:42 +00:00
|
|
|
*
|
2009-08-09 20:31:03 +00:00
|
|
|
* @param locale_filename The filename of the document to load, in locale encoding.
|
|
|
|
* @param readonly Whether to open the document in read-only mode.
|
|
|
|
* @param ft The filetype for the document or @c NULL to auto-detect the filetype.
|
2008-02-17 18:00:42 +00:00
|
|
|
* @param forced_enc The file encoding to use or @c NULL to auto-detect the file encoding.
|
|
|
|
*
|
2009-07-21 22:12:13 +00:00
|
|
|
* @return The document opened or @c NULL.
|
2008-02-17 18:00:42 +00:00
|
|
|
**/
|
2008-06-15 13:35:48 +00:00
|
|
|
GeanyDocument *document_open_file(const gchar *locale_filename, gboolean readonly,
|
2008-05-16 14:17:54 +00:00
|
|
|
GeanyFiletype *ft, const gchar *forced_enc)
|
2007-09-11 15:21:11 +00:00
|
|
|
{
|
2008-06-15 13:35:48 +00:00
|
|
|
return document_open_file_full(NULL, locale_filename, 0, readonly, ft, forced_enc);
|
2007-09-11 15:21:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-01-06 12:35:43 +00:00
|
|
|
typedef struct
|
|
|
|
{
|
2008-02-27 13:17:29 +00:00
|
|
|
gchar *data; /* null-terminated file data */
|
|
|
|
gsize len; /* string length of data */
|
2007-01-06 12:35:43 +00:00
|
|
|
gchar *enc;
|
|
|
|
gboolean bom;
|
2008-02-27 13:17:29 +00:00
|
|
|
time_t mtime; /* modification time, read by stat::st_mtime */
|
2007-01-06 12:35:43 +00:00
|
|
|
gboolean readonly;
|
|
|
|
} FileData;
|
|
|
|
|
|
|
|
|
|
|
|
/* loads textfile data, verifies and converts to forced_enc or UTF-8. Also handles BOM. */
|
2009-04-21 20:52:51 +00:00
|
|
|
static gboolean load_text_file(const gchar *locale_filename, const gchar *display_filename,
|
|
|
|
FileData *filedata, const gchar *forced_enc)
|
2007-01-06 12:35:43 +00:00
|
|
|
{
|
|
|
|
GError *err = NULL;
|
|
|
|
struct stat st;
|
|
|
|
|
|
|
|
filedata->data = NULL;
|
|
|
|
filedata->len = 0;
|
|
|
|
filedata->enc = NULL;
|
|
|
|
filedata->bom = FALSE;
|
|
|
|
filedata->readonly = FALSE;
|
|
|
|
|
2007-04-16 15:58:34 +00:00
|
|
|
if (g_stat(locale_filename, &st) != 0)
|
2007-01-06 12:35:43 +00:00
|
|
|
{
|
2009-04-21 20:52:51 +00:00
|
|
|
ui_set_statusbar(TRUE, _("Could not open file %s (%s)"),
|
|
|
|
display_filename, g_strerror(errno));
|
2007-01-06 12:35:43 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
filedata->mtime = st.st_mtime;
|
|
|
|
|
|
|
|
if (! g_file_get_contents(locale_filename, &filedata->data, NULL, &err))
|
|
|
|
{
|
2008-02-13 18:54:05 +00:00
|
|
|
ui_set_statusbar(TRUE, "%s", err->message);
|
2007-01-06 12:35:43 +00:00
|
|
|
g_error_free(err);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2011-03-18 15:57:02 +00:00
|
|
|
filedata->len = (gsize) st.st_size;
|
|
|
|
if (! encodings_convert_to_utf8_auto(&filedata->data, &filedata->len, forced_enc,
|
|
|
|
&filedata->enc, &filedata->bom, &filedata->readonly))
|
|
|
|
{
|
|
|
|
if (forced_enc)
|
|
|
|
{
|
|
|
|
ui_set_statusbar(TRUE, _("The file \"%s\" is not valid %s."),
|
|
|
|
display_filename, forced_enc);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ui_set_statusbar(TRUE,
|
|
|
|
_("The file \"%s\" does not look like a text file or the file encoding is not supported."),
|
|
|
|
display_filename);
|
|
|
|
}
|
|
|
|
g_free(filedata->data);
|
|
|
|
return FALSE;
|
|
|
|
}
|
2007-05-24 10:24:26 +00:00
|
|
|
|
2011-03-18 15:57:02 +00:00
|
|
|
if (filedata->readonly)
|
2007-01-06 12:35:43 +00:00
|
|
|
{
|
2008-06-05 12:43:10 +00:00
|
|
|
const gchar *warn_msg = _(
|
|
|
|
"The file \"%s\" could not be opened properly and has been truncated. " \
|
|
|
|
"This can occur if the file contains a NULL byte. " \
|
|
|
|
"Be aware that saving it can cause data loss.\nThe file was set to read-only.");
|
2007-01-06 12:35:43 +00:00
|
|
|
|
2007-08-23 11:34:06 +00:00
|
|
|
if (main_status.main_window_realized)
|
2009-04-21 20:52:51 +00:00
|
|
|
dialogs_show_msgbox(GTK_MESSAGE_WARNING, warn_msg, display_filename);
|
2007-01-06 12:35:43 +00:00
|
|
|
|
2009-04-21 20:52:51 +00:00
|
|
|
ui_set_statusbar(TRUE, warn_msg, display_filename);
|
2007-01-06 12:35:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
2006-09-08 15:05:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-02-01 15:00:59 +00:00
|
|
|
/* Sets the cursor position on opening a file. First it sets the line when cl_options.goto_line
|
|
|
|
* is set, otherwise it sets the line when pos is greater than zero and finally it sets the column
|
2009-09-03 12:04:27 +00:00
|
|
|
* if cl_options.goto_column is set.
|
|
|
|
*
|
|
|
|
* returns the new position which may have changed */
|
2009-09-10 16:59:20 +00:00
|
|
|
static gint set_cursor_position(GeanyEditor *editor, gint pos)
|
2007-02-01 15:00:59 +00:00
|
|
|
{
|
|
|
|
if (cl_options.goto_line >= 0)
|
2008-02-27 13:17:29 +00:00
|
|
|
{ /* goto line which was specified on command line and then undefine the line */
|
2008-07-14 11:13:54 +00:00
|
|
|
sci_goto_line(editor->sci, cl_options.goto_line - 1, TRUE);
|
2008-07-08 16:30:37 +00:00
|
|
|
editor->scroll_percent = 0.5F;
|
2007-02-01 15:00:59 +00:00
|
|
|
cl_options.goto_line = -1;
|
|
|
|
}
|
|
|
|
else if (pos > 0)
|
|
|
|
{
|
2008-07-14 11:13:54 +00:00
|
|
|
sci_set_current_position(editor->sci, pos, FALSE);
|
2008-07-08 16:30:37 +00:00
|
|
|
editor->scroll_percent = 0.5F;
|
2007-02-01 15:00:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (cl_options.goto_column >= 0)
|
2008-02-27 13:17:29 +00:00
|
|
|
{ /* goto column which was specified on command line and then undefine the column */
|
2009-09-03 12:04:27 +00:00
|
|
|
|
|
|
|
gint new_pos = sci_get_current_position(editor->sci) + cl_options.goto_column;
|
|
|
|
sci_set_current_position(editor->sci, new_pos, FALSE);
|
2008-07-08 16:30:37 +00:00
|
|
|
editor->scroll_percent = 0.5F;
|
2007-02-01 15:00:59 +00:00
|
|
|
cl_options.goto_column = -1;
|
2009-09-03 12:04:27 +00:00
|
|
|
return new_pos;
|
2007-02-01 15:00:59 +00:00
|
|
|
}
|
2009-09-03 12:04:27 +00:00
|
|
|
return sci_get_current_position(editor->sci);
|
2007-02-01 15:00:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-08-19 14:55:56 +00:00
|
|
|
/* Count lines that start with some hard tabs then a soft tab. */
|
2008-08-18 15:58:36 +00:00
|
|
|
static gboolean detect_tabs_and_spaces(GeanyEditor *editor)
|
|
|
|
{
|
|
|
|
const GeanyIndentPrefs *iprefs = editor_get_indent_prefs(editor);
|
|
|
|
ScintillaObject *sci = editor->sci;
|
|
|
|
gsize count = 0;
|
2009-07-04 09:20:28 +00:00
|
|
|
struct Sci_TextToFind ttf;
|
2011-06-17 22:52:17 +00:00
|
|
|
gchar *soft_tab = g_strnfill((gsize)iprefs->width, ' ');
|
2008-08-19 14:55:56 +00:00
|
|
|
gchar *regex = g_strconcat("^\t+", soft_tab, "[^ ]", NULL);
|
2008-08-18 15:58:36 +00:00
|
|
|
|
|
|
|
g_free(soft_tab);
|
|
|
|
|
|
|
|
ttf.chrg.cpMin = 0;
|
|
|
|
ttf.chrg.cpMax = sci_get_length(sci);
|
|
|
|
ttf.lpstrText = regex;
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
gint pos;
|
|
|
|
|
|
|
|
pos = sci_find_text(sci, SCFIND_REGEXP, &ttf);
|
|
|
|
if (pos == -1)
|
|
|
|
break; /* no more matches */
|
|
|
|
count++;
|
|
|
|
ttf.chrg.cpMin = ttf.chrgText.cpMax + 1; /* search after this match */
|
|
|
|
}
|
|
|
|
g_free(regex);
|
2008-08-19 14:55:56 +00:00
|
|
|
/* The 0.02 is a low weighting to ignore a few possibly accidental occurrences */
|
2008-08-18 15:58:36 +00:00
|
|
|
return count > sci_get_line_count(sci) * 0.02;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-08-25 20:14:17 +00:00
|
|
|
/* Detect the indent type based on counting the leading indent characters for each line.
|
|
|
|
* Returns whether detection succeeded, and the detected type in *type_ upon success */
|
2011-08-25 20:15:02 +00:00
|
|
|
gboolean document_detect_indent_type(GeanyDocument *doc, GeanyIndentType *type_)
|
2007-10-17 12:27:07 +00:00
|
|
|
{
|
2011-08-25 20:15:02 +00:00
|
|
|
GeanyEditor *editor = doc->editor;
|
2008-08-04 14:07:49 +00:00
|
|
|
ScintillaObject *sci = editor->sci;
|
2011-06-17 22:52:17 +00:00
|
|
|
gint line, line_count;
|
2007-10-17 12:27:07 +00:00
|
|
|
gsize tabs = 0, spaces = 0;
|
|
|
|
|
2008-08-18 15:58:36 +00:00
|
|
|
if (detect_tabs_and_spaces(editor))
|
2011-08-25 20:14:17 +00:00
|
|
|
{
|
|
|
|
*type_ = GEANY_INDENT_TYPE_BOTH;
|
|
|
|
return TRUE;
|
|
|
|
}
|
2008-08-04 14:07:49 +00:00
|
|
|
|
2009-02-10 21:11:25 +00:00
|
|
|
line_count = sci_get_line_count(sci);
|
|
|
|
for (line = 0; line < line_count; line++)
|
2007-10-17 12:27:07 +00:00
|
|
|
{
|
|
|
|
gint pos = sci_get_position_from_line(sci, line);
|
|
|
|
gchar c;
|
|
|
|
|
2009-10-26 18:13:50 +00:00
|
|
|
/* most code will have indent total <= 24, otherwise it's more likely to be
|
|
|
|
* alignment than indentation */
|
|
|
|
if (sci_get_line_indentation(sci, line) > 24)
|
|
|
|
continue;
|
|
|
|
|
2007-10-17 12:27:07 +00:00
|
|
|
c = sci_get_char_at(sci, pos);
|
|
|
|
if (c == '\t')
|
|
|
|
tabs++;
|
2011-03-26 03:12:38 +00:00
|
|
|
/* check for at least 2 spaces */
|
|
|
|
else if (c == ' ' && sci_get_char_at(sci, pos + 1) == ' ')
|
|
|
|
spaces++;
|
2007-10-17 12:27:07 +00:00
|
|
|
}
|
|
|
|
if (spaces == 0 && tabs == 0)
|
2011-08-25 20:14:17 +00:00
|
|
|
return FALSE;
|
2007-10-17 12:27:07 +00:00
|
|
|
|
2008-08-19 14:55:56 +00:00
|
|
|
/* the factors may need to be tweaked */
|
|
|
|
if (spaces > tabs * 4)
|
2011-08-25 20:14:17 +00:00
|
|
|
*type_ = GEANY_INDENT_TYPE_SPACES;
|
2008-08-19 14:55:56 +00:00
|
|
|
else if (tabs > spaces * 4)
|
2011-08-25 20:14:17 +00:00
|
|
|
*type_ = GEANY_INDENT_TYPE_TABS;
|
2007-10-17 12:27:07 +00:00
|
|
|
else
|
2011-08-25 20:14:17 +00:00
|
|
|
*type_ = GEANY_INDENT_TYPE_BOTH;
|
|
|
|
|
|
|
|
return TRUE;
|
2007-10-17 12:27:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-08-25 20:14:17 +00:00
|
|
|
/* Detect the indent width based on counting the leading indent characters for each line.
|
|
|
|
* Returns whether detection succeeded, and the detected width in *width_ upon success */
|
|
|
|
static gboolean detect_indent_width(GeanyEditor *editor, GeanyIndentType type, gint *width_)
|
2008-05-18 16:03:20 +00:00
|
|
|
{
|
2011-03-26 17:25:24 +00:00
|
|
|
const GeanyIndentPrefs *iprefs = editor_get_indent_prefs(editor);
|
|
|
|
ScintillaObject *sci = editor->sci;
|
2011-06-17 22:52:17 +00:00
|
|
|
gint line, line_count;
|
2011-03-26 17:25:24 +00:00
|
|
|
gint widths[7] = { 0 }; /* width can be from 2 to 8 */
|
|
|
|
gint count, width, i;
|
|
|
|
|
|
|
|
/* can't easily detect the supposed width of a tab, guess the default is OK */
|
|
|
|
if (type == GEANY_INDENT_TYPE_TABS)
|
2011-08-25 20:14:17 +00:00
|
|
|
return FALSE;
|
2011-03-26 17:25:24 +00:00
|
|
|
|
|
|
|
/* force 8 at detection time for tab & spaces -- anyway we don't use tabs at this point */
|
|
|
|
sci_set_tab_width(sci, 8);
|
|
|
|
|
|
|
|
line_count = sci_get_line_count(sci);
|
|
|
|
for (line = 0; line < line_count; line++)
|
|
|
|
{
|
2013-06-02 22:42:47 +02:00
|
|
|
gint pos = sci_get_line_indent_position(sci, line);
|
|
|
|
|
|
|
|
/* We probably don't have style info yet, because we're generally called just after
|
|
|
|
* the document got created, so we can't use highlighting_is_code_style().
|
|
|
|
* That's not good, but the assumption below that concerning lines start with an
|
|
|
|
* asterisk (common continuation character for C/C++/Java/...) should do the trick
|
|
|
|
* without removing too much legitimate lines. */
|
|
|
|
if (sci_get_char_at(sci, pos) == '*')
|
|
|
|
continue;
|
|
|
|
|
2011-03-26 17:25:24 +00:00
|
|
|
width = sci_get_line_indentation(sci, line);
|
|
|
|
/* most code will have indent total <= 24, otherwise it's more likely to be
|
|
|
|
* alignment than indentation */
|
|
|
|
if (width > 24)
|
|
|
|
continue;
|
|
|
|
/* < 2 is no indentation */
|
|
|
|
if (width < 2)
|
|
|
|
continue;
|
2008-05-18 16:03:20 +00:00
|
|
|
|
2011-03-26 17:25:24 +00:00
|
|
|
for (i = G_N_ELEMENTS(widths) - 1; i >= 0; i--)
|
|
|
|
{
|
|
|
|
if ((width % (i + 2)) == 0)
|
|
|
|
widths[i]++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
count = 0;
|
|
|
|
width = iprefs->width;
|
2011-07-28 17:59:58 +00:00
|
|
|
for (i = G_N_ELEMENTS(widths) - 1; i >= 0; i--)
|
2011-03-26 17:25:24 +00:00
|
|
|
{
|
2011-07-28 17:59:58 +00:00
|
|
|
/* give large indents higher weight not to be fooled by spurious indents */
|
|
|
|
if (widths[i] >= count * 1.5)
|
2011-03-26 17:25:24 +00:00
|
|
|
{
|
|
|
|
width = i + 2;
|
|
|
|
count = widths[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-25 20:14:17 +00:00
|
|
|
if (count == 0)
|
|
|
|
return FALSE;
|
2011-03-26 17:25:24 +00:00
|
|
|
|
2011-08-25 20:14:17 +00:00
|
|
|
*width_ = width;
|
|
|
|
return TRUE;
|
2011-03-20 17:50:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-08-25 20:15:02 +00:00
|
|
|
/* same as detect_indent_width() but uses editor's indent type */
|
|
|
|
gboolean document_detect_indent_width(GeanyDocument *doc, gint *width_)
|
|
|
|
{
|
|
|
|
return detect_indent_width(doc->editor, doc->editor->indent_type, width_);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-03-20 17:50:59 +00:00
|
|
|
void document_apply_indent_settings(GeanyDocument *doc)
|
|
|
|
{
|
|
|
|
const GeanyIndentPrefs *iprefs = editor_get_indent_prefs(NULL);
|
2011-08-25 20:14:17 +00:00
|
|
|
GeanyIndentType type = iprefs->type;
|
2011-03-26 17:25:24 +00:00
|
|
|
gint width = iprefs->width;
|
2011-03-20 17:50:59 +00:00
|
|
|
|
2011-08-25 20:15:02 +00:00
|
|
|
if (iprefs->detect_type && document_detect_indent_type(doc, &type))
|
2008-08-18 15:58:36 +00:00
|
|
|
{
|
2011-08-25 20:14:17 +00:00
|
|
|
if (type != iprefs->type)
|
2008-08-18 15:58:36 +00:00
|
|
|
{
|
|
|
|
const gchar *name = NULL;
|
|
|
|
|
|
|
|
switch (type)
|
|
|
|
{
|
|
|
|
case GEANY_INDENT_TYPE_SPACES:
|
|
|
|
name = _("Spaces");
|
|
|
|
break;
|
|
|
|
case GEANY_INDENT_TYPE_TABS:
|
|
|
|
name = _("Tabs");
|
|
|
|
break;
|
|
|
|
case GEANY_INDENT_TYPE_BOTH:
|
|
|
|
name = _("Tabs and Spaces");
|
|
|
|
break;
|
2008-08-04 14:07:49 +00:00
|
|
|
}
|
2008-09-01 14:50:26 +00:00
|
|
|
/* For translators: first wildcard is the indentation mode (Spaces, Tabs, Tabs
|
|
|
|
* and Spaces), the second one is the filename */
|
2008-08-18 15:58:36 +00:00
|
|
|
ui_set_statusbar(TRUE, _("Setting %s indentation mode for %s."), name,
|
2010-05-05 12:07:45 +00:00
|
|
|
DOC_FILENAME(doc));
|
2008-08-18 15:58:36 +00:00
|
|
|
}
|
2008-05-18 16:03:20 +00:00
|
|
|
}
|
2011-08-25 20:14:17 +00:00
|
|
|
else if (doc->file_type->indent_type > -1)
|
|
|
|
type = doc->file_type->indent_type;
|
2011-03-26 17:25:24 +00:00
|
|
|
|
2011-08-25 20:14:17 +00:00
|
|
|
if (iprefs->detect_width && detect_indent_width(doc->editor, type, &width))
|
|
|
|
{
|
2011-03-26 17:25:24 +00:00
|
|
|
if (width != iprefs->width)
|
|
|
|
{
|
|
|
|
ui_set_statusbar(TRUE, _("Setting indentation width to %d for %s."), width,
|
|
|
|
DOC_FILENAME(doc));
|
|
|
|
}
|
|
|
|
}
|
2011-08-25 20:14:17 +00:00
|
|
|
else if (doc->file_type->indent_width > -1)
|
|
|
|
width = doc->file_type->indent_width;
|
|
|
|
|
2011-03-26 17:25:24 +00:00
|
|
|
editor_set_indent(doc->editor, type, width);
|
2008-05-18 16:03:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-10-07 16:47:31 +00:00
|
|
|
void document_show_tab(GeanyDocument *doc)
|
|
|
|
{
|
|
|
|
gtk_notebook_set_current_page(GTK_NOTEBOOK(main_widgets.notebook),
|
|
|
|
document_get_notebook_page(doc));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-06-15 13:35:48 +00:00
|
|
|
/* To open a new file, set doc to NULL; filename should be locale encoded.
|
|
|
|
* To reload a file, set the doc for the document to be reloaded; filename should be NULL.
|
2007-08-24 12:06:59 +00:00
|
|
|
* pos is the cursor position, which can be overridden by --line and --column.
|
2007-09-11 16:05:03 +00:00
|
|
|
* forced_enc can be NULL to detect the file encoding.
|
2008-06-20 14:19:57 +00:00
|
|
|
* Returns: doc of the opened file or NULL if an error occurred. */
|
2008-06-15 13:35:48 +00:00
|
|
|
GeanyDocument *document_open_file_full(GeanyDocument *doc, const gchar *filename, gint pos,
|
|
|
|
gboolean readonly, GeanyFiletype *ft, const gchar *forced_enc)
|
2005-11-22 12:26:26 +00:00
|
|
|
{
|
2006-05-17 18:29:29 +00:00
|
|
|
gint editor_mode;
|
2008-06-15 13:35:48 +00:00
|
|
|
gboolean reload = (doc == NULL) ? FALSE : TRUE;
|
2006-01-23 17:05:29 +00:00
|
|
|
gchar *utf8_filename = NULL;
|
2009-04-21 20:52:51 +00:00
|
|
|
gchar *display_filename = NULL;
|
2006-02-06 06:26:53 +00:00
|
|
|
gchar *locale_filename = NULL;
|
2008-05-16 14:17:54 +00:00
|
|
|
GeanyFiletype *use_ft;
|
2007-01-06 12:35:43 +00:00
|
|
|
FileData filedata;
|
2006-05-16 19:06:08 +00:00
|
|
|
|
2014-01-18 15:12:22 +01:00
|
|
|
g_return_val_if_fail(doc == NULL || doc->is_valid, NULL);
|
|
|
|
|
2005-11-22 12:26:26 +00:00
|
|
|
if (reload)
|
|
|
|
{
|
2008-06-15 13:35:48 +00:00
|
|
|
utf8_filename = g_strdup(doc->file_name);
|
2006-08-13 08:36:52 +00:00
|
|
|
locale_filename = utils_get_locale_from_utf8(utf8_filename);
|
2005-11-22 12:26:26 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-02-27 13:17:29 +00:00
|
|
|
/* filename must not be NULL when opening a file */
|
2011-10-31 17:22:39 +00:00
|
|
|
g_return_val_if_fail(filename, NULL);
|
2006-05-08 01:57:25 +00:00
|
|
|
|
2008-03-26 18:53:59 +00:00
|
|
|
#ifdef G_OS_WIN32
|
|
|
|
/* if filename is a shortcut, try to resolve it */
|
2008-03-27 13:59:51 +00:00
|
|
|
locale_filename = win32_get_shortcut_target(filename);
|
2008-03-26 18:53:59 +00:00
|
|
|
#else
|
2006-02-06 06:26:53 +00:00
|
|
|
locale_filename = g_strdup(filename);
|
2008-03-26 18:53:59 +00:00
|
|
|
#endif
|
2009-07-20 15:37:02 +00:00
|
|
|
/* remove relative junk */
|
|
|
|
utils_tidy_path(locale_filename);
|
|
|
|
|
2008-02-27 13:17:29 +00:00
|
|
|
/* try to get the UTF-8 equivalent for the filename, fallback to filename if error */
|
2006-08-13 08:36:52 +00:00
|
|
|
utf8_filename = utils_get_utf8_from_locale(locale_filename);
|
2006-01-23 17:05:29 +00:00
|
|
|
|
2008-02-27 13:17:29 +00:00
|
|
|
/* if file is already open, switch to it and go */
|
2008-06-15 13:35:48 +00:00
|
|
|
doc = document_find_by_filename(utf8_filename);
|
|
|
|
if (doc != NULL)
|
2005-11-22 12:26:26 +00:00
|
|
|
{
|
2011-11-03 20:45:52 +01:00
|
|
|
ui_add_recent_document(doc); /* either add or reorder recent item */
|
2009-09-24 16:19:27 +00:00
|
|
|
/* show the doc before reload dialog */
|
2011-10-07 16:47:31 +00:00
|
|
|
document_show_tab(doc);
|
2008-06-15 13:35:48 +00:00
|
|
|
document_check_disk_status(doc, TRUE); /* force a file changed check */
|
2005-11-22 12:26:26 +00:00
|
|
|
}
|
|
|
|
}
|
2009-09-16 13:56:58 +00:00
|
|
|
if (reload || doc == NULL)
|
2009-09-04 11:34:12 +00:00
|
|
|
{ /* doc possibly changed */
|
|
|
|
display_filename = utils_str_middle_truncate(utf8_filename, 100);
|
2005-11-22 12:26:26 +00:00
|
|
|
|
2009-09-04 11:34:12 +00:00
|
|
|
if (! load_text_file(locale_filename, display_filename, &filedata, forced_enc))
|
|
|
|
{
|
|
|
|
g_free(display_filename);
|
|
|
|
g_free(utf8_filename);
|
|
|
|
g_free(locale_filename);
|
|
|
|
return NULL;
|
|
|
|
}
|
2006-07-22 01:29:10 +00:00
|
|
|
|
2009-09-04 11:34:12 +00:00
|
|
|
if (! reload)
|
|
|
|
{
|
|
|
|
doc = document_create(utf8_filename);
|
|
|
|
g_return_val_if_fail(doc != NULL, NULL); /* really should not happen */
|
2009-01-09 18:22:07 +00:00
|
|
|
|
2009-09-04 11:34:12 +00:00
|
|
|
/* file exists on disk, set real_path */
|
2012-01-25 16:26:16 +00:00
|
|
|
SETPTR(doc->real_path, tm_get_real_path(locale_filename));
|
2009-01-21 15:50:48 +00:00
|
|
|
|
2009-09-04 11:34:12 +00:00
|
|
|
doc->priv->is_remote = utils_is_remote_path(locale_filename);
|
|
|
|
monitor_file_setup(doc);
|
|
|
|
}
|
2005-11-22 12:26:26 +00:00
|
|
|
|
2009-09-04 11:34:12 +00:00
|
|
|
sci_set_undo_collection(doc->editor->sci, FALSE); /* avoid creation of an undo action */
|
|
|
|
sci_empty_undo_buffer(doc->editor->sci);
|
2006-10-10 16:02:41 +00:00
|
|
|
|
2009-09-04 11:34:12 +00:00
|
|
|
/* add the text to the ScintillaObject */
|
|
|
|
sci_set_readonly(doc->editor->sci, FALSE); /* to allow replacing text */
|
|
|
|
sci_set_text(doc->editor->sci, filedata.data); /* NULL terminated data */
|
|
|
|
queue_colourise(doc); /* Ensure the document gets colourised. */
|
2007-01-06 12:35:43 +00:00
|
|
|
|
2009-09-04 11:34:12 +00:00
|
|
|
/* detect & set line endings */
|
|
|
|
editor_mode = utils_get_line_endings(filedata.data, filedata.len);
|
|
|
|
sci_set_eol_mode(doc->editor->sci, editor_mode);
|
|
|
|
g_free(filedata.data);
|
2006-10-10 16:02:41 +00:00
|
|
|
|
2009-09-04 11:34:12 +00:00
|
|
|
sci_set_undo_collection(doc->editor->sci, TRUE);
|
2006-10-10 16:02:41 +00:00
|
|
|
|
2009-09-04 11:34:12 +00:00
|
|
|
doc->priv->mtime = filedata.mtime; /* get the modification time from file and keep it */
|
|
|
|
g_free(doc->encoding); /* if reloading, free old encoding */
|
|
|
|
doc->encoding = filedata.enc;
|
|
|
|
doc->has_bom = filedata.bom;
|
|
|
|
store_saved_encoding(doc); /* store the opened encoding for undo/redo */
|
2006-06-20 11:55:34 +00:00
|
|
|
|
2011-11-21 20:33:37 +02:00
|
|
|
doc->readonly = readonly || filedata.readonly;
|
2009-09-04 11:34:12 +00:00
|
|
|
sci_set_readonly(doc->editor->sci, doc->readonly);
|
2014-05-20 07:14:10 +02:00
|
|
|
doc->priv->protected = 0;
|
2007-01-06 12:35:43 +00:00
|
|
|
|
2009-09-04 11:34:12 +00:00
|
|
|
/* update line number margin width */
|
|
|
|
doc->priv->line_count = sci_get_line_count(doc->editor->sci);
|
2014-06-27 22:05:46 +02:00
|
|
|
sci_set_line_numbers(doc->editor->sci, editor_prefs.show_linenumber_margin);
|
2007-01-06 12:35:43 +00:00
|
|
|
|
2009-09-04 11:34:12 +00:00
|
|
|
if (! reload)
|
|
|
|
{
|
2008-06-05 12:14:51 +00:00
|
|
|
|
2009-09-04 11:34:12 +00:00
|
|
|
/* "the" SCI signal (connect after initial setup(i.e. adding text)) */
|
|
|
|
g_signal_connect(doc->editor->sci, "sci-notify", G_CALLBACK(editor_sci_notify_cb),
|
|
|
|
doc->editor);
|
2006-12-14 12:54:36 +00:00
|
|
|
|
2009-09-04 11:34:12 +00:00
|
|
|
use_ft = (ft != NULL) ? ft : filetypes_detect_from_document(doc);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{ /* reloading */
|
|
|
|
document_undo_clear(doc);
|
2008-06-02 15:52:00 +00:00
|
|
|
|
2009-09-04 11:34:12 +00:00
|
|
|
use_ft = ft;
|
|
|
|
}
|
|
|
|
/* update taglist, typedef keywords and build menu if necessary */
|
|
|
|
document_set_filetype(doc, use_ft);
|
2006-06-29 17:14:52 +00:00
|
|
|
|
2009-09-04 11:34:12 +00:00
|
|
|
/* set indentation settings after setting the filetype */
|
|
|
|
if (reload)
|
2010-09-14 15:01:29 +00:00
|
|
|
editor_set_indent(doc->editor, doc->editor->indent_type, doc->editor->indent_width); /* resetup sci */
|
2009-09-04 11:34:12 +00:00
|
|
|
else
|
2010-05-05 12:07:45 +00:00
|
|
|
document_apply_indent_settings(doc);
|
2008-05-18 16:03:20 +00:00
|
|
|
|
2009-09-04 11:34:12 +00:00
|
|
|
document_set_text_changed(doc, FALSE); /* also updates tab state */
|
|
|
|
ui_document_show_hide(doc); /* update the document menu */
|
2005-11-22 12:26:26 +00:00
|
|
|
|
2009-09-04 11:34:12 +00:00
|
|
|
/* finally add current file to recent files menu, but not the files from the last session */
|
|
|
|
if (! main_status.opening_session_files)
|
2011-11-03 20:45:52 +01:00
|
|
|
ui_add_recent_document(doc);
|
2007-08-10 16:11:17 +00:00
|
|
|
|
2009-09-04 11:34:12 +00:00
|
|
|
if (reload)
|
2011-04-10 13:53:05 +00:00
|
|
|
{
|
|
|
|
g_signal_emit_by_name(geany_object, "document-reload", doc);
|
2009-09-04 11:34:12 +00:00
|
|
|
ui_set_statusbar(TRUE, _("File %s reloaded."), display_filename);
|
2011-04-10 13:53:05 +00:00
|
|
|
}
|
2009-09-04 11:34:12 +00:00
|
|
|
else
|
2011-04-10 13:53:05 +00:00
|
|
|
{
|
|
|
|
g_signal_emit_by_name(geany_object, "document-open", doc);
|
2009-09-04 11:34:12 +00:00
|
|
|
/* For translators: this is the status window message for opening a file. %d is the number
|
|
|
|
* of the newly opened file, %s indicates whether the file is opened read-only
|
|
|
|
* (it is replaced with the string ", read-only"). */
|
|
|
|
msgwin_status_add(_("File %s opened(%d%s)."),
|
|
|
|
display_filename, gtk_notebook_get_n_pages(GTK_NOTEBOOK(main_widgets.notebook)),
|
|
|
|
(readonly) ? _(", read-only") : "");
|
2011-04-10 13:53:05 +00:00
|
|
|
}
|
2009-09-04 11:34:12 +00:00
|
|
|
}
|
2006-06-26 15:49:07 +00:00
|
|
|
|
2009-04-21 20:52:51 +00:00
|
|
|
g_free(display_filename);
|
2006-01-23 17:05:29 +00:00
|
|
|
g_free(utf8_filename);
|
2006-02-06 06:26:53 +00:00
|
|
|
g_free(locale_filename);
|
2008-11-07 14:37:17 +00:00
|
|
|
|
2009-09-16 13:56:58 +00:00
|
|
|
/* set the cursor position according to pos, cl_options.goto_line and cl_options.goto_column */
|
|
|
|
pos = set_cursor_position(doc->editor, pos);
|
2009-09-03 12:04:27 +00:00
|
|
|
/* now bring the file in front */
|
|
|
|
editor_goto_pos(doc->editor, pos, FALSE);
|
|
|
|
|
|
|
|
/* finally, let the editor widget grab the focus so you can start coding
|
|
|
|
* right away */
|
|
|
|
g_idle_add(on_idle_focus, doc);
|
2008-06-15 13:35:48 +00:00
|
|
|
return doc;
|
2005-11-22 12:26:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-11-15 23:12:13 +00:00
|
|
|
/* Takes a new line separated list of filename URIs and opens each file.
|
2011-06-17 22:52:17 +00:00
|
|
|
* length is the length of the string */
|
|
|
|
void document_open_file_list(const gchar *data, gsize length)
|
2006-11-15 23:12:13 +00:00
|
|
|
{
|
2011-06-17 22:52:17 +00:00
|
|
|
guint i;
|
2006-11-15 23:12:13 +00:00
|
|
|
gchar *filename;
|
|
|
|
gchar **list;
|
|
|
|
|
2009-04-15 22:47:33 +00:00
|
|
|
g_return_if_fail(data != NULL);
|
2006-11-15 23:12:13 +00:00
|
|
|
|
2011-03-26 03:12:38 +00:00
|
|
|
list = g_strsplit(data, utils_get_eol_char(utils_get_line_endings(data, length)), 0);
|
2006-11-15 23:12:13 +00:00
|
|
|
|
2012-01-31 15:04:23 +01:00
|
|
|
/* stop at the end or first empty item, because last item is empty but not null */
|
|
|
|
for (i = 0; list[i] != NULL && list[i][0] != '\0'; i++)
|
2006-11-15 23:12:13 +00:00
|
|
|
{
|
2012-01-31 15:04:23 +01:00
|
|
|
filename = utils_get_path_from_uri(list[i]);
|
|
|
|
if (filename == NULL)
|
2009-04-05 21:07:40 +00:00
|
|
|
continue;
|
2007-09-11 15:21:11 +00:00
|
|
|
document_open_file(filename, FALSE, NULL, NULL);
|
2006-11-15 23:12:13 +00:00
|
|
|
g_free(filename);
|
|
|
|
}
|
|
|
|
|
|
|
|
g_strfreev(list);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-02-17 18:00:42 +00:00
|
|
|
/**
|
2008-06-20 14:19:57 +00:00
|
|
|
* Opens each file in the list @a filenames.
|
2008-02-17 18:00:42 +00:00
|
|
|
* Internally, document_open_file() is called for every list item.
|
|
|
|
*
|
|
|
|
* @param filenames A list of filenames to load, in locale encoding.
|
2009-08-09 20:31:03 +00:00
|
|
|
* @param readonly Whether to open the document in read-only mode.
|
|
|
|
* @param ft The filetype for the document or @c NULL to auto-detect the filetype.
|
2008-02-17 18:00:42 +00:00
|
|
|
* @param forced_enc The file encoding to use or @c NULL to auto-detect the file encoding.
|
|
|
|
**/
|
2008-05-16 14:17:54 +00:00
|
|
|
void document_open_files(const GSList *filenames, gboolean readonly, GeanyFiletype *ft,
|
2007-03-06 16:57:09 +00:00
|
|
|
const gchar *forced_enc)
|
|
|
|
{
|
|
|
|
const GSList *item;
|
|
|
|
|
|
|
|
for (item = filenames; item != NULL; item = g_slist_next(item))
|
|
|
|
{
|
2007-09-11 15:21:11 +00:00
|
|
|
document_open_file(item->data, readonly, ft, forced_enc);
|
2007-03-06 16:57:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-16 14:13:38 +00:00
|
|
|
|
2008-02-17 18:00:42 +00:00
|
|
|
/**
|
2009-07-21 22:12:13 +00:00
|
|
|
* Reloads the document with the specified file encoding
|
2008-02-17 18:00:42 +00:00
|
|
|
* @a forced_enc or @c NULL to auto-detect the file encoding.
|
|
|
|
*
|
2008-06-12 16:50:01 +00:00
|
|
|
* @param doc The document to reload.
|
2008-02-17 18:00:42 +00:00
|
|
|
* @param forced_enc The file encoding to use or @c NULL to auto-detect the file encoding.
|
|
|
|
*
|
2009-08-09 20:31:03 +00:00
|
|
|
* @return @c TRUE if the document was actually reloaded or @c FALSE otherwise.
|
2008-02-17 18:00:42 +00:00
|
|
|
**/
|
2008-06-15 13:35:48 +00:00
|
|
|
gboolean document_reload_file(GeanyDocument *doc, const gchar *forced_enc)
|
2006-06-20 11:55:34 +00:00
|
|
|
{
|
|
|
|
gint pos = 0;
|
2008-06-15 13:35:48 +00:00
|
|
|
GeanyDocument *new_doc;
|
2006-07-22 01:29:10 +00:00
|
|
|
|
2009-04-15 22:47:33 +00:00
|
|
|
g_return_val_if_fail(doc != NULL, FALSE);
|
2006-06-20 11:55:34 +00:00
|
|
|
|
2014-05-24 16:53:59 +02:00
|
|
|
/* Use cancel because the response handler would call this recursively */
|
|
|
|
if (doc->priv->info_bars[MSG_TYPE_RELOAD] != NULL)
|
|
|
|
gtk_info_bar_response(GTK_INFO_BAR(doc->priv->info_bars[MSG_TYPE_RELOAD]), GTK_RESPONSE_CANCEL);
|
|
|
|
|
2008-02-27 13:17:29 +00:00
|
|
|
/* try to set the cursor to the position before reloading */
|
2008-07-14 11:13:54 +00:00
|
|
|
pos = sci_get_current_position(doc->editor->sci);
|
2009-07-08 22:20:17 +00:00
|
|
|
new_doc = document_open_file_full(doc, NULL, pos, doc->readonly, doc->file_type, forced_enc);
|
|
|
|
|
2008-06-15 13:35:48 +00:00
|
|
|
return (new_doc != NULL);
|
2006-06-20 11:55:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-06-24 09:13:44 +02:00
|
|
|
/* also used for reloading when forced_enc is NULL */
|
|
|
|
gboolean document_reload_prompt(GeanyDocument *doc, const gchar *forced_enc)
|
|
|
|
{
|
|
|
|
gchar *base_name;
|
|
|
|
gboolean result = FALSE;
|
|
|
|
|
2014-06-24 23:07:08 +02:00
|
|
|
g_return_val_if_fail(doc != NULL, FALSE);
|
2014-06-24 09:13:44 +02:00
|
|
|
|
|
|
|
/* No need to reload "untitled" (non-file-backed) documents */
|
|
|
|
if (doc->file_name == NULL)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
if (forced_enc == NULL)
|
|
|
|
forced_enc = doc->encoding;
|
|
|
|
|
|
|
|
base_name = g_path_get_basename(doc->file_name);
|
|
|
|
/* don't prompt if file hasn't been edited at all */
|
|
|
|
if ((!doc->changed && !document_can_undo(doc) && !document_can_redo(doc)) ||
|
|
|
|
dialogs_show_question_full(NULL, _("_Reload"), GTK_STOCK_CANCEL,
|
|
|
|
_("Any unsaved changes will be lost."),
|
|
|
|
_("Are you sure you want to reload '%s'?"), base_name))
|
|
|
|
{
|
|
|
|
result = document_reload_file(doc, forced_enc);
|
|
|
|
if (forced_enc != NULL)
|
|
|
|
ui_update_statusbar(doc, -1);
|
|
|
|
}
|
|
|
|
g_free(base_name);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-01-21 15:50:12 +00:00
|
|
|
static gboolean document_update_timestamp(GeanyDocument *doc, const gchar *locale_filename)
|
2006-12-09 13:07:23 +00:00
|
|
|
{
|
2011-06-17 22:51:51 +00:00
|
|
|
#ifndef USE_GIO_FILEMON
|
2006-12-09 13:07:23 +00:00
|
|
|
struct stat st;
|
2006-12-10 21:29:04 +00:00
|
|
|
|
2009-04-15 22:47:33 +00:00
|
|
|
g_return_val_if_fail(doc != NULL, FALSE);
|
2006-12-09 13:07:23 +00:00
|
|
|
|
2009-01-21 15:50:12 +00:00
|
|
|
/* stat the file to get the timestamp, otherwise on Windows the actual
|
|
|
|
* timestamp can be ahead of time(NULL) */
|
2007-04-16 15:58:34 +00:00
|
|
|
if (g_stat(locale_filename, &st) != 0)
|
2006-12-09 13:07:23 +00:00
|
|
|
{
|
2008-06-15 13:35:48 +00:00
|
|
|
ui_set_statusbar(TRUE, _("Could not open file %s (%s)"), doc->file_name,
|
2006-12-09 13:07:23 +00:00
|
|
|
g_strerror(errno));
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2009-01-18 18:19:58 +00:00
|
|
|
doc->priv->mtime = st.st_mtime; /* get the modification time from file and keep it */
|
|
|
|
#endif
|
2006-12-09 13:07:23 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-11-29 18:08:20 +00:00
|
|
|
/* Sets line and column to the given position byte_pos in the document.
|
|
|
|
* byte_pos is the position counted in bytes, not characters */
|
2008-06-15 13:35:48 +00:00
|
|
|
static void get_line_column_from_pos(GeanyDocument *doc, guint byte_pos, gint *line, gint *column)
|
2007-11-29 18:08:20 +00:00
|
|
|
{
|
|
|
|
gint i;
|
|
|
|
gint line_start;
|
|
|
|
|
2008-02-27 13:17:29 +00:00
|
|
|
/* for some reason we can use byte count instead of character count here */
|
2008-07-14 11:13:54 +00:00
|
|
|
*line = sci_get_line_from_position(doc->editor->sci, byte_pos);
|
|
|
|
line_start = sci_get_position_from_line(doc->editor->sci, *line);
|
2008-02-27 13:17:29 +00:00
|
|
|
/* get the column in the line */
|
2007-11-29 18:08:20 +00:00
|
|
|
*column = byte_pos - line_start;
|
|
|
|
|
2008-02-27 13:17:29 +00:00
|
|
|
/* any non-ASCII characters are encoded with two bytes(UTF-8, always in Scintilla), so
|
|
|
|
* skip one byte(i++) and decrease the column number which is based on byte count */
|
2007-11-29 18:08:20 +00:00
|
|
|
for (i = line_start; i < (line_start + *column); i++)
|
|
|
|
{
|
2008-07-14 11:13:54 +00:00
|
|
|
if (sci_get_char_at(doc->editor->sci, i) < 0)
|
2007-11-29 18:08:20 +00:00
|
|
|
{
|
|
|
|
(*column)--;
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-07-08 16:30:37 +00:00
|
|
|
static void replace_header_filename(GeanyDocument *doc)
|
|
|
|
{
|
|
|
|
gchar *filebase;
|
|
|
|
gchar *filename;
|
2009-07-04 09:20:28 +00:00
|
|
|
struct Sci_TextToFind ttf;
|
2008-07-08 16:30:37 +00:00
|
|
|
|
2009-04-15 22:47:33 +00:00
|
|
|
g_return_if_fail(doc != NULL);
|
|
|
|
g_return_if_fail(doc->file_type != NULL);
|
2008-07-08 16:30:37 +00:00
|
|
|
|
2013-03-05 23:32:17 +01:00
|
|
|
filebase = g_regex_escape_string(GEANY_STRING_UNTITLED, -1);
|
2009-08-12 11:19:54 +00:00
|
|
|
if (doc->file_type->extension)
|
2013-03-05 23:32:17 +01:00
|
|
|
SETPTR(filebase, g_strconcat("\\b", filebase, "\\.\\w+", NULL));
|
2009-08-12 11:19:54 +00:00
|
|
|
else
|
2013-03-05 23:32:17 +01:00
|
|
|
SETPTR(filebase, g_strconcat("\\b", filebase, "\\b", NULL));
|
2009-08-12 11:19:54 +00:00
|
|
|
|
2008-07-08 16:30:37 +00:00
|
|
|
filename = g_path_get_basename(doc->file_name);
|
|
|
|
|
|
|
|
/* only search the first 3 lines */
|
|
|
|
ttf.chrg.cpMin = 0;
|
2013-03-05 23:52:45 +01:00
|
|
|
ttf.chrg.cpMax = sci_get_position_from_line(doc->editor->sci, 4);
|
2010-05-05 12:50:59 +00:00
|
|
|
ttf.lpstrText = filebase;
|
2008-07-08 16:30:37 +00:00
|
|
|
|
2014-08-10 01:46:16 +02:00
|
|
|
if (search_find_text(doc->editor->sci, GEANY_FIND_MATCHCASE | GEANY_FIND_REGEXP, &ttf, NULL) != -1)
|
2008-07-08 16:30:37 +00:00
|
|
|
{
|
2009-07-29 17:54:48 +00:00
|
|
|
sci_set_target_start(doc->editor->sci, ttf.chrgText.cpMin);
|
|
|
|
sci_set_target_end(doc->editor->sci, ttf.chrgText.cpMax);
|
|
|
|
sci_replace_target(doc->editor->sci, filename, FALSE);
|
2008-07-08 16:30:37 +00:00
|
|
|
}
|
|
|
|
g_free(filebase);
|
|
|
|
g_free(filename);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-01-18 18:19:58 +00:00
|
|
|
/**
|
|
|
|
* Renames the file in @a doc to @a new_filename. Only the file on disk is actually renamed,
|
|
|
|
* you still have to call @ref document_save_file_as() to change the @a doc object.
|
|
|
|
* It also stops monitoring for file changes to prevent receiving too many file change events
|
|
|
|
* while renaming. File monitoring is setup again in @ref document_save_file_as().
|
|
|
|
*
|
|
|
|
* @param doc The current document which should be renamed.
|
|
|
|
* @param new_filename The new filename in UTF-8 encoding.
|
2009-01-30 18:53:23 +00:00
|
|
|
*
|
|
|
|
* @since 0.16
|
|
|
|
**/
|
2009-01-18 18:19:58 +00:00
|
|
|
void document_rename_file(GeanyDocument *doc, const gchar *new_filename)
|
|
|
|
{
|
|
|
|
gchar *old_locale_filename = utils_get_locale_from_utf8(doc->file_name);
|
|
|
|
gchar *new_locale_filename = utils_get_locale_from_utf8(new_filename);
|
2009-04-05 21:41:38 +00:00
|
|
|
gint result;
|
2009-01-18 18:19:58 +00:00
|
|
|
|
|
|
|
/* stop file monitoring to avoid getting events for deleting/creating files,
|
|
|
|
* it's re-setup in document_save_file_as() */
|
|
|
|
document_stop_file_monitoring(doc);
|
|
|
|
|
2009-04-05 21:41:38 +00:00
|
|
|
result = g_rename(old_locale_filename, new_locale_filename);
|
2009-04-15 22:47:33 +00:00
|
|
|
if (result != 0)
|
2009-04-05 21:41:38 +00:00
|
|
|
{
|
|
|
|
dialogs_show_msgbox_with_secondary(GTK_MESSAGE_ERROR,
|
|
|
|
_("Error renaming file."), g_strerror(errno));
|
|
|
|
}
|
2009-01-18 18:19:58 +00:00
|
|
|
g_free(old_locale_filename);
|
|
|
|
g_free(new_locale_filename);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-05-20 07:14:10 +02:00
|
|
|
static void protect_document(GeanyDocument *doc)
|
|
|
|
{
|
|
|
|
/* do not call queue_colourise because to we want to keep the text-changed indication! */
|
|
|
|
if (!doc->priv->protected++)
|
|
|
|
sci_set_readonly(doc->editor->sci, TRUE);
|
2014-09-03 15:22:10 +01:00
|
|
|
|
|
|
|
ui_update_tab_status(doc);
|
2014-05-20 07:14:10 +02:00
|
|
|
}
|
|
|
|
|
2014-09-03 17:43:17 +01:00
|
|
|
|
2014-05-20 07:14:10 +02:00
|
|
|
static void unprotect_document(GeanyDocument *doc)
|
|
|
|
{
|
|
|
|
g_return_if_fail(doc->priv->protected > 0);
|
|
|
|
|
|
|
|
if (!--doc->priv->protected && doc->readonly == FALSE)
|
|
|
|
sci_set_readonly(doc->editor->sci, FALSE);
|
2014-09-03 15:22:10 +01:00
|
|
|
|
|
|
|
ui_update_tab_status(doc);
|
2014-05-20 07:14:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-09-09 17:31:27 +00:00
|
|
|
/* Return TRUE if the document doesn't have a full filename set.
|
|
|
|
* This makes filenames without a path show the save as dialog, e.g. for file templates.
|
|
|
|
* Otherwise just use the set filename instead of asking the user - e.g. for command-line
|
|
|
|
* new files. */
|
2009-12-20 15:36:52 +00:00
|
|
|
gboolean document_need_save_as(GeanyDocument *doc)
|
|
|
|
{
|
|
|
|
g_return_val_if_fail(doc != NULL, FALSE);
|
|
|
|
|
2010-09-09 17:31:27 +00:00
|
|
|
return (doc->file_name == NULL || !g_path_is_absolute(doc->file_name));
|
2009-12-20 15:36:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-01-18 18:19:58 +00:00
|
|
|
/**
|
|
|
|
* Saves the document, detecting the filetype.
|
2008-03-21 14:02:59 +00:00
|
|
|
*
|
2008-06-15 13:35:48 +00:00
|
|
|
* @param doc The document for the file to save.
|
2008-06-03 17:22:04 +00:00
|
|
|
* @param utf8_fname The new name for the document, in UTF-8, or NULL.
|
2008-03-21 14:02:59 +00:00
|
|
|
* @return @c TRUE if the file was saved or @c FALSE if the file could not be saved.
|
2009-01-18 18:19:58 +00:00
|
|
|
*
|
2008-03-21 14:02:59 +00:00
|
|
|
* @see document_save_file().
|
2009-01-30 18:53:23 +00:00
|
|
|
*
|
|
|
|
* @since 0.16
|
|
|
|
**/
|
2008-06-15 13:35:48 +00:00
|
|
|
gboolean document_save_file_as(GeanyDocument *doc, const gchar *utf8_fname)
|
2008-03-21 14:02:59 +00:00
|
|
|
{
|
2008-03-21 16:59:30 +00:00
|
|
|
gboolean ret;
|
2014-05-20 07:14:10 +02:00
|
|
|
gboolean new_file;
|
2008-03-21 16:59:30 +00:00
|
|
|
|
2009-04-15 22:47:33 +00:00
|
|
|
g_return_val_if_fail(doc != NULL, FALSE);
|
2008-03-21 14:02:59 +00:00
|
|
|
|
2014-05-27 16:02:59 +02:00
|
|
|
new_file = document_need_save_as(doc) || (utf8_fname != NULL && strcmp(doc->file_name, utf8_fname) != 0);
|
2009-01-18 18:19:58 +00:00
|
|
|
if (utf8_fname != NULL)
|
2012-01-25 16:26:16 +00:00
|
|
|
SETPTR(doc->file_name, g_strdup(utf8_fname));
|
2009-04-05 21:07:40 +00:00
|
|
|
|
2008-12-21 20:09:08 +00:00
|
|
|
/* reset real path, it's retrieved again in document_save() */
|
2012-01-25 16:26:16 +00:00
|
|
|
SETPTR(doc->real_path, NULL);
|
2008-06-03 17:22:04 +00:00
|
|
|
|
2008-03-21 14:02:59 +00:00
|
|
|
/* detect filetype */
|
2010-10-25 17:24:47 +00:00
|
|
|
if (doc->file_type->id == GEANY_FILETYPES_NONE)
|
2008-03-21 14:02:59 +00:00
|
|
|
{
|
2008-07-23 14:44:27 +00:00
|
|
|
GeanyFiletype *ft = filetypes_detect_from_document(doc);
|
2008-03-21 14:02:59 +00:00
|
|
|
|
2008-06-15 13:35:48 +00:00
|
|
|
document_set_filetype(doc, ft);
|
|
|
|
if (document_get_current() == doc)
|
2008-03-21 14:02:59 +00:00
|
|
|
{
|
2008-05-22 14:41:28 +00:00
|
|
|
ignore_callback = TRUE;
|
2008-06-15 13:35:48 +00:00
|
|
|
filetypes_select_radio_item(doc->file_type);
|
2008-05-22 14:41:28 +00:00
|
|
|
ignore_callback = FALSE;
|
2008-03-21 14:02:59 +00:00
|
|
|
}
|
|
|
|
}
|
2014-05-20 07:14:10 +02:00
|
|
|
|
|
|
|
if (new_file)
|
|
|
|
{
|
|
|
|
sci_set_readonly(doc->editor->sci, FALSE);
|
|
|
|
doc->readonly = FALSE;
|
|
|
|
if (doc->priv->protected > 0)
|
|
|
|
unprotect_document(doc);
|
|
|
|
}
|
|
|
|
|
2008-07-08 16:30:37 +00:00
|
|
|
replace_header_filename(doc);
|
2008-03-21 16:59:30 +00:00
|
|
|
|
2008-06-15 13:35:48 +00:00
|
|
|
ret = document_save_file(doc, TRUE);
|
2009-01-18 18:19:58 +00:00
|
|
|
|
|
|
|
/* file monitoring support, add file monitoring after the file has been saved
|
|
|
|
* to ignore any earlier events */
|
2009-01-21 15:50:48 +00:00
|
|
|
monitor_file_setup(doc);
|
2009-01-18 18:19:58 +00:00
|
|
|
doc->priv->file_disk_status = FILE_IGNORE;
|
|
|
|
|
2009-04-15 22:47:33 +00:00
|
|
|
if (ret)
|
2011-11-03 20:45:52 +01:00
|
|
|
ui_add_recent_document(doc);
|
2008-03-21 16:59:30 +00:00
|
|
|
return ret;
|
2008-03-21 14:02:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-06-15 13:35:48 +00:00
|
|
|
static gsize save_convert_to_encoding(GeanyDocument *doc, gchar **data, gsize *len)
|
2008-04-25 12:49:38 +00:00
|
|
|
{
|
|
|
|
GError *conv_error = NULL;
|
|
|
|
gchar* conv_file_contents = NULL;
|
|
|
|
gsize bytes_read;
|
|
|
|
gsize conv_len;
|
|
|
|
|
2011-11-15 20:32:36 +01:00
|
|
|
g_return_val_if_fail(data != NULL && *data != NULL, FALSE);
|
2009-04-15 22:47:33 +00:00
|
|
|
g_return_val_if_fail(len != NULL, FALSE);
|
2008-04-25 12:49:38 +00:00
|
|
|
|
|
|
|
/* try to convert it from UTF-8 to original encoding */
|
2008-06-15 13:35:48 +00:00
|
|
|
conv_file_contents = g_convert(*data, *len - 1, doc->encoding, "UTF-8",
|
2008-04-25 12:49:38 +00:00
|
|
|
&bytes_read, &conv_len, &conv_error);
|
|
|
|
|
2009-04-15 22:47:33 +00:00
|
|
|
if (conv_error != NULL)
|
2008-04-25 12:49:38 +00:00
|
|
|
{
|
|
|
|
gchar *text = g_strdup_printf(
|
|
|
|
_("An error occurred while converting the file from UTF-8 in \"%s\". The file remains unsaved."),
|
2008-06-15 13:35:48 +00:00
|
|
|
doc->encoding);
|
2008-04-25 12:49:38 +00:00
|
|
|
gchar *error_text;
|
|
|
|
|
|
|
|
if (conv_error->code == G_CONVERT_ERROR_ILLEGAL_SEQUENCE)
|
|
|
|
{
|
|
|
|
gint line, column;
|
|
|
|
gint context_len;
|
|
|
|
gunichar unic;
|
|
|
|
/* don't read over the doc length */
|
|
|
|
gint max_len = MIN((gint)bytes_read + 6, (gint)*len - 1);
|
2013-05-22 04:22:53 +02:00
|
|
|
gchar context[7]; /* read 6 bytes from Sci + '\0' */
|
2008-07-14 11:13:54 +00:00
|
|
|
sci_get_text_range(doc->editor->sci, bytes_read, max_len, context);
|
2008-04-25 12:49:38 +00:00
|
|
|
|
|
|
|
/* take only one valid Unicode character from the context and discard the leftover */
|
|
|
|
unic = g_utf8_get_char_validated(context, -1);
|
|
|
|
context_len = g_unichar_to_utf8(unic, context);
|
|
|
|
context[context_len] = '\0';
|
2008-06-15 13:35:48 +00:00
|
|
|
get_line_column_from_pos(doc, bytes_read, &line, &column);
|
2008-04-25 12:49:38 +00:00
|
|
|
|
|
|
|
error_text = g_strdup_printf(
|
|
|
|
_("Error message: %s\nThe error occurred at \"%s\" (line: %d, column: %d)."),
|
|
|
|
conv_error->message, context, line + 1, column);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
error_text = g_strdup_printf(_("Error message: %s."), conv_error->message);
|
|
|
|
|
|
|
|
geany_debug("encoding error: %s", conv_error->message);
|
|
|
|
dialogs_show_msgbox_with_secondary(GTK_MESSAGE_ERROR, text, error_text);
|
|
|
|
g_error_free(conv_error);
|
|
|
|
g_free(text);
|
|
|
|
g_free(error_text);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-04-27 15:05:40 +00:00
|
|
|
g_free(*data);
|
|
|
|
*data = conv_file_contents;
|
2008-04-25 12:49:38 +00:00
|
|
|
*len = conv_len;
|
|
|
|
}
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-11-02 17:42:54 +00:00
|
|
|
static gchar *write_data_to_disk(const gchar *locale_filename,
|
2011-12-17 23:10:03 +01:00
|
|
|
const gchar *data, gsize len)
|
2008-04-25 12:49:38 +00:00
|
|
|
{
|
2009-04-21 20:54:04 +00:00
|
|
|
GError *error = NULL;
|
2010-11-07 13:08:13 +00:00
|
|
|
|
2011-06-20 16:11:18 +00:00
|
|
|
if (file_prefs.use_safe_file_saving)
|
2010-11-07 13:08:13 +00:00
|
|
|
{
|
2011-06-20 16:11:18 +00:00
|
|
|
/* Use old GLib API for safe saving (GVFS-safe, but alters ownership and permissons).
|
|
|
|
* This is the only option that handles disk space exhaustion. */
|
|
|
|
if (g_file_set_contents(locale_filename, data, len, &error))
|
|
|
|
geany_debug("Wrote %s with g_file_set_contents().", locale_filename);
|
|
|
|
}
|
|
|
|
else if (file_prefs.use_gio_unsafe_file_saving)
|
|
|
|
{
|
2010-11-07 13:08:13 +00:00
|
|
|
GFile *fp;
|
2008-04-25 12:49:38 +00:00
|
|
|
|
2011-06-20 16:11:18 +00:00
|
|
|
/* Use GIO API to save file (GVFS-safe)
|
|
|
|
* It is best in most GVFS setups but don't seem to work correctly on some more complex
|
|
|
|
* setups (saving from some VM to their host, over some SMB shares, etc.) */
|
2010-11-07 13:08:13 +00:00
|
|
|
fp = g_file_new_for_path(locale_filename);
|
2010-11-23 16:23:22 +00:00
|
|
|
g_file_replace_contents(fp, data, len, NULL, file_prefs.gio_unsafe_save_backup,
|
2010-11-07 13:08:13 +00:00
|
|
|
G_FILE_CREATE_NONE, NULL, NULL, &error);
|
|
|
|
g_object_unref(fp);
|
2011-06-20 16:11:18 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2010-11-07 13:08:13 +00:00
|
|
|
FILE *fp;
|
2010-11-11 13:13:37 +00:00
|
|
|
int save_errno;
|
|
|
|
gchar *display_name = g_filename_display_name(locale_filename);
|
2008-04-25 12:49:38 +00:00
|
|
|
|
2010-11-02 17:42:54 +00:00
|
|
|
/* Use POSIX API for unsafe saving (GVFS-unsafe) */
|
2010-11-11 13:13:37 +00:00
|
|
|
/* The error handling is taken from glib-2.26.0 gfileutils.c */
|
2010-11-10 18:19:20 +00:00
|
|
|
errno = 0;
|
2009-04-21 20:54:04 +00:00
|
|
|
fp = g_fopen(locale_filename, "wb");
|
2010-11-10 13:44:10 +00:00
|
|
|
if (fp == NULL)
|
2010-11-11 13:13:37 +00:00
|
|
|
{
|
|
|
|
save_errno = errno;
|
|
|
|
|
|
|
|
g_set_error(&error,
|
|
|
|
G_FILE_ERROR,
|
|
|
|
g_file_error_from_errno(save_errno),
|
|
|
|
_("Failed to open file '%s' for writing: fopen() failed: %s"),
|
|
|
|
display_name,
|
|
|
|
g_strerror(save_errno));
|
|
|
|
}
|
2010-11-10 13:44:10 +00:00
|
|
|
else
|
|
|
|
{
|
2011-12-17 23:10:03 +01:00
|
|
|
gsize bytes_written;
|
2010-11-11 13:13:37 +00:00
|
|
|
|
2010-11-10 18:19:20 +00:00
|
|
|
errno = 0;
|
2010-11-10 13:44:10 +00:00
|
|
|
bytes_written = fwrite(data, sizeof(gchar), len, fp);
|
2008-04-25 12:49:38 +00:00
|
|
|
|
2010-11-10 13:44:10 +00:00
|
|
|
if (len != bytes_written)
|
2010-11-11 13:13:37 +00:00
|
|
|
{
|
|
|
|
save_errno = errno;
|
|
|
|
|
|
|
|
g_set_error(&error,
|
|
|
|
G_FILE_ERROR,
|
|
|
|
g_file_error_from_errno(save_errno),
|
|
|
|
_("Failed to write file '%s': fwrite() failed: %s"),
|
|
|
|
display_name,
|
|
|
|
g_strerror(save_errno));
|
|
|
|
}
|
2008-04-25 12:49:38 +00:00
|
|
|
|
2010-11-11 13:13:37 +00:00
|
|
|
errno = 0;
|
|
|
|
/* preserve the fwrite() error if any */
|
|
|
|
if (fclose(fp) != 0 && error == NULL)
|
|
|
|
{
|
|
|
|
save_errno = errno;
|
|
|
|
|
|
|
|
g_set_error(&error,
|
|
|
|
G_FILE_ERROR,
|
|
|
|
g_file_error_from_errno(save_errno),
|
|
|
|
_("Failed to close file '%s': fclose() failed: %s"),
|
|
|
|
display_name,
|
|
|
|
g_strerror(save_errno));
|
|
|
|
}
|
2010-11-10 13:44:10 +00:00
|
|
|
}
|
2010-11-11 13:13:37 +00:00
|
|
|
|
|
|
|
g_free(display_name);
|
2009-04-21 20:54:04 +00:00
|
|
|
}
|
2010-11-02 17:42:54 +00:00
|
|
|
if (error != NULL)
|
|
|
|
{
|
|
|
|
gchar *msg = g_strdup(error->message);
|
|
|
|
g_error_free(error);
|
2010-12-01 13:06:01 +00:00
|
|
|
/* geany will warn about file truncation for unsafe saving below */
|
2010-11-02 17:42:54 +00:00
|
|
|
return msg;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static gchar *save_doc(GeanyDocument *doc, const gchar *locale_filename,
|
2011-12-17 23:10:03 +01:00
|
|
|
const gchar *data, gsize len)
|
2010-11-02 17:42:54 +00:00
|
|
|
{
|
|
|
|
gchar *err;
|
|
|
|
|
|
|
|
g_return_val_if_fail(doc != NULL, g_strdup(g_strerror(EINVAL)));
|
|
|
|
g_return_val_if_fail(data != NULL, g_strdup(g_strerror(EINVAL)));
|
|
|
|
|
|
|
|
err = write_data_to_disk(locale_filename, data, len);
|
|
|
|
if (err)
|
|
|
|
return err;
|
2009-01-09 18:22:07 +00:00
|
|
|
|
|
|
|
/* now the file is on disk, set real_path */
|
2009-04-21 20:54:04 +00:00
|
|
|
if (doc->real_path == NULL)
|
2009-01-09 18:22:07 +00:00
|
|
|
{
|
|
|
|
doc->real_path = tm_get_real_path(locale_filename);
|
|
|
|
doc->priv->is_remote = utils_is_remote_path(locale_filename);
|
2009-07-16 15:36:27 +00:00
|
|
|
monitor_file_setup(doc);
|
2009-01-09 18:22:07 +00:00
|
|
|
}
|
2009-04-21 20:54:04 +00:00
|
|
|
return NULL;
|
2008-04-25 12:49:38 +00:00
|
|
|
}
|
|
|
|
|
2014-09-03 17:43:17 +01:00
|
|
|
|
|
|
|
static gboolean save_file_handle_infobars(GeanyDocument *doc, gboolean force)
|
|
|
|
{
|
|
|
|
GtkWidget *bar = NULL;
|
|
|
|
|
|
|
|
document_show_tab(doc);
|
|
|
|
|
|
|
|
if (doc->priv->info_bars[MSG_TYPE_RELOAD])
|
|
|
|
{
|
|
|
|
if (!dialogs_show_question_full(NULL, _("_Overwrite"), GTK_STOCK_CANCEL,
|
|
|
|
_("Overwrite?"),
|
|
|
|
_("The file '%s' on the disk is more recent than the current buffer."),
|
|
|
|
doc->file_name))
|
|
|
|
return FALSE;
|
|
|
|
bar = doc->priv->info_bars[MSG_TYPE_RELOAD];
|
|
|
|
}
|
|
|
|
else if (doc->priv->info_bars[MSG_TYPE_RESAVE])
|
|
|
|
{
|
|
|
|
if (!dialogs_show_question_full(NULL, GTK_STOCK_SAVE, GTK_STOCK_CANCEL,
|
|
|
|
_("Try to resave the file?"),
|
|
|
|
_("File \"%s\" was not found on disk!"),
|
|
|
|
doc->file_name))
|
|
|
|
return FALSE;
|
|
|
|
bar = doc->priv->info_bars[MSG_TYPE_RESAVE];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
g_assert_not_reached();
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
gtk_info_bar_response(GTK_INFO_BAR(bar), RESPONSE_DOCUMENT_SAVE);
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-02-17 18:00:42 +00:00
|
|
|
/**
|
2011-11-02 15:17:17 +00:00
|
|
|
* Saves the document.
|
|
|
|
* Also shows the Save As dialog if necessary.
|
|
|
|
* If the file is not modified, this function may do nothing unless @a force is set to @c TRUE.
|
|
|
|
*
|
2013-10-28 16:03:02 +00:00
|
|
|
* Saving may include replacing tabs with spaces,
|
2010-12-06 18:04:42 +00:00
|
|
|
* stripping trailing spaces and adding a final new line at the end of the file, depending
|
|
|
|
* on user preferences. Then the @c "document-before-save" signal is emitted,
|
2009-09-06 16:51:11 +00:00
|
|
|
* allowing plugins to modify the document before it is saved, and data is
|
2011-11-02 15:17:17 +00:00
|
|
|
* actually written to disk.
|
2008-02-17 18:00:42 +00:00
|
|
|
*
|
2011-11-02 15:17:17 +00:00
|
|
|
* On successful saving:
|
|
|
|
* - GeanyDocument::real_path is set.
|
|
|
|
* - The filetype is set again or auto-detected if it wasn't set yet.
|
|
|
|
* - The @c "document-save" signal is emitted for plugins.
|
2008-02-17 18:00:42 +00:00
|
|
|
*
|
2011-11-02 15:17:17 +00:00
|
|
|
* @warning You should ensure @c doc->file_name has an absolute path unless you want the
|
|
|
|
* Save As dialog to be shown. A @c NULL value also shows the dialog. This behaviour was
|
|
|
|
* added in Geany 1.22.
|
2010-12-06 18:04:42 +00:00
|
|
|
*
|
2009-08-09 20:31:03 +00:00
|
|
|
* @param doc The document to save.
|
2011-11-02 15:17:17 +00:00
|
|
|
* @param force Whether to save the file even if it is not modified.
|
2008-02-17 18:00:42 +00:00
|
|
|
*
|
|
|
|
* @return @c TRUE if the file was saved or @c FALSE if the file could not or should not be saved.
|
|
|
|
**/
|
2008-06-15 13:35:48 +00:00
|
|
|
gboolean document_save_file(GeanyDocument *doc, gboolean force)
|
2005-11-22 12:26:26 +00:00
|
|
|
{
|
2009-04-21 20:54:04 +00:00
|
|
|
gchar *errmsg;
|
2005-11-22 12:26:26 +00:00
|
|
|
gchar *data;
|
2008-04-25 12:49:38 +00:00
|
|
|
gsize len;
|
2009-01-21 15:50:12 +00:00
|
|
|
gchar *locale_filename;
|
2012-01-19 18:09:29 +00:00
|
|
|
const GeanyFilePrefs *fp;
|
2008-04-25 12:49:38 +00:00
|
|
|
|
2009-04-15 22:47:33 +00:00
|
|
|
g_return_val_if_fail(doc != NULL, FALSE);
|
2005-11-22 12:26:26 +00:00
|
|
|
|
2011-11-02 15:17:17 +00:00
|
|
|
if (document_need_save_as(doc))
|
|
|
|
{
|
|
|
|
/* ensure doc is the current tab before showing the dialog */
|
|
|
|
document_show_tab(doc);
|
|
|
|
return dialogs_show_save_as();
|
|
|
|
}
|
|
|
|
|
2008-02-27 13:17:29 +00:00
|
|
|
/* the "changed" flag should exclude the "readonly" flag, but check it anyway for safety */
|
2014-09-03 17:43:17 +01:00
|
|
|
if (doc->readonly)
|
2014-05-20 07:14:10 +02:00
|
|
|
return FALSE;
|
|
|
|
if (!force && !doc->changed)
|
2008-04-25 12:49:38 +00:00
|
|
|
return FALSE;
|
2014-09-03 17:43:17 +01:00
|
|
|
if (doc->priv->protected)
|
|
|
|
{
|
|
|
|
return save_file_handle_infobars(doc, force);
|
|
|
|
}
|
2005-11-22 12:26:26 +00:00
|
|
|
|
2012-01-19 18:09:29 +00:00
|
|
|
fp = project_get_file_prefs();
|
2013-10-28 16:03:02 +00:00
|
|
|
/* replaces tabs with spaces but only if the current file is not a Makefile */
|
2012-01-19 18:09:29 +00:00
|
|
|
if (fp->replace_tabs && doc->file_type->id != GEANY_FILETYPES_MAKE)
|
2008-09-25 18:28:37 +00:00
|
|
|
editor_replace_tabs(doc->editor);
|
2008-02-27 13:17:29 +00:00
|
|
|
/* strip trailing spaces */
|
2012-01-19 18:09:29 +00:00
|
|
|
if (fp->strip_trailing_spaces)
|
2008-09-25 18:28:37 +00:00
|
|
|
editor_strip_trailing_spaces(doc->editor);
|
2008-02-27 13:17:29 +00:00
|
|
|
/* ensure the file has a newline at the end */
|
2012-01-19 18:09:29 +00:00
|
|
|
if (fp->final_new_line)
|
2008-09-25 18:28:37 +00:00
|
|
|
editor_ensure_final_newline(doc->editor);
|
2010-10-25 16:24:55 +00:00
|
|
|
/* ensure newlines are consistent */
|
2012-01-19 18:09:29 +00:00
|
|
|
if (fp->ensure_convert_new_lines)
|
2010-10-25 16:24:55 +00:00
|
|
|
sci_convert_eols(doc->editor->sci, sci_get_eol_mode(doc->editor->sci));
|
2005-11-22 12:26:26 +00:00
|
|
|
|
2009-09-06 16:51:11 +00:00
|
|
|
/* notify plugins which may wish to modify the document before it's saved */
|
|
|
|
g_signal_emit_by_name(geany_object, "document-before-save", doc);
|
|
|
|
|
2008-07-14 11:13:54 +00:00
|
|
|
len = sci_get_length(doc->editor->sci) + 1;
|
2008-06-15 13:35:48 +00:00
|
|
|
if (doc->has_bom && encodings_is_unicode_charset(doc->encoding))
|
2008-02-27 13:17:29 +00:00
|
|
|
{ /* always write a UTF-8 BOM because in this moment the text itself is still in UTF-8
|
2008-06-15 13:35:48 +00:00
|
|
|
* encoding, it will be converted to doc->encoding below and this conversion
|
2008-02-27 13:17:29 +00:00
|
|
|
* also changes the BOM */
|
|
|
|
data = (gchar*) g_malloc(len + 3); /* 3 chars for BOM */
|
2008-02-20 11:24:23 +00:00
|
|
|
data[0] = (gchar) 0xef;
|
|
|
|
data[1] = (gchar) 0xbb;
|
|
|
|
data[2] = (gchar) 0xbf;
|
2008-07-14 11:13:54 +00:00
|
|
|
sci_get_text(doc->editor->sci, len, data + 3);
|
2006-07-22 01:29:10 +00:00
|
|
|
len += 3;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2006-07-23 13:14:23 +00:00
|
|
|
data = (gchar*) g_malloc(len);
|
2008-07-14 11:13:54 +00:00
|
|
|
sci_get_text(doc->editor->sci, len, data);
|
2006-07-22 01:29:10 +00:00
|
|
|
}
|
2005-11-22 12:26:26 +00:00
|
|
|
|
2008-02-27 13:17:29 +00:00
|
|
|
/* save in original encoding, skip when it is already UTF-8 or has the encoding "None" */
|
2008-06-15 13:35:48 +00:00
|
|
|
if (doc->encoding != NULL && ! utils_str_equal(doc->encoding, "UTF-8") &&
|
|
|
|
! utils_str_equal(doc->encoding, encodings[GEANY_ENCODING_NONE].charset))
|
2006-06-19 18:31:17 +00:00
|
|
|
{
|
2009-09-16 14:13:38 +00:00
|
|
|
if (! save_convert_to_encoding(doc, &data, &len))
|
2006-06-19 18:31:17 +00:00
|
|
|
{
|
2006-06-28 19:27:19 +00:00
|
|
|
g_free(data);
|
2006-09-05 18:33:48 +00:00
|
|
|
return FALSE;
|
2006-06-19 18:31:17 +00:00
|
|
|
}
|
|
|
|
}
|
2006-07-23 13:14:23 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
len = strlen(data);
|
|
|
|
}
|
2007-11-05 15:09:13 +00:00
|
|
|
|
2009-01-21 15:50:12 +00:00
|
|
|
locale_filename = utils_get_locale_from_utf8(doc->file_name);
|
|
|
|
|
2009-01-24 19:38:31 +00:00
|
|
|
/* ignore file changed notification when the file is written */
|
|
|
|
doc->priv->file_disk_status = FILE_IGNORE;
|
|
|
|
|
2008-04-25 12:49:38 +00:00
|
|
|
/* actually write the content of data to the file on disk */
|
2010-11-02 17:42:54 +00:00
|
|
|
errmsg = save_doc(doc, locale_filename, data, len);
|
2005-11-22 12:26:26 +00:00
|
|
|
g_free(data);
|
|
|
|
|
2009-04-21 20:54:04 +00:00
|
|
|
if (errmsg != NULL)
|
2005-11-22 12:26:26 +00:00
|
|
|
{
|
2009-04-21 20:54:04 +00:00
|
|
|
ui_set_statusbar(TRUE, _("Error saving file (%s)."), errmsg);
|
2010-12-01 13:06:01 +00:00
|
|
|
|
2012-02-06 16:08:55 +00:00
|
|
|
if (!file_prefs.use_safe_file_saving)
|
2010-12-01 13:06:01 +00:00
|
|
|
{
|
2012-01-25 16:26:16 +00:00
|
|
|
SETPTR(errmsg,
|
2010-12-01 13:06:01 +00:00
|
|
|
g_strdup_printf(_("%s\n\nThe file on disk may now be truncated!"), errmsg));
|
|
|
|
}
|
2009-04-21 20:54:04 +00:00
|
|
|
dialogs_show_msgbox_with_secondary(GTK_MESSAGE_ERROR, _("Error saving file."), errmsg);
|
2009-01-24 19:38:31 +00:00
|
|
|
doc->priv->file_disk_status = FILE_OK;
|
2006-02-10 20:56:17 +00:00
|
|
|
utils_beep();
|
2009-01-21 15:50:12 +00:00
|
|
|
g_free(locale_filename);
|
2009-04-21 20:54:04 +00:00
|
|
|
g_free(errmsg);
|
2006-09-05 18:33:48 +00:00
|
|
|
return FALSE;
|
2005-11-22 12:26:26 +00:00
|
|
|
}
|
|
|
|
|
2008-02-27 13:17:29 +00:00
|
|
|
/* store the opened encoding for undo/redo */
|
2008-06-15 13:35:48 +00:00
|
|
|
store_saved_encoding(doc);
|
2006-10-24 13:41:34 +00:00
|
|
|
|
2008-02-27 13:17:29 +00:00
|
|
|
/* ignore the following things if we are quitting */
|
2009-04-15 22:47:33 +00:00
|
|
|
if (! main_status.quitting)
|
2006-02-10 20:56:17 +00:00
|
|
|
{
|
2008-07-14 11:13:54 +00:00
|
|
|
sci_set_savepoint(doc->editor->sci);
|
2006-12-09 13:07:23 +00:00
|
|
|
|
2008-12-21 20:09:08 +00:00
|
|
|
if (file_prefs.disk_check_timeout > 0)
|
2009-01-21 15:50:12 +00:00
|
|
|
document_update_timestamp(doc, locale_filename);
|
2006-12-09 13:07:23 +00:00
|
|
|
|
2008-03-21 14:02:59 +00:00
|
|
|
/* update filetype-related things */
|
2008-06-15 13:35:48 +00:00
|
|
|
document_set_filetype(doc, doc->file_type);
|
2007-09-05 18:38:42 +00:00
|
|
|
|
2008-11-18 20:13:29 +00:00
|
|
|
document_update_tab_label(doc);
|
2008-11-07 14:37:17 +00:00
|
|
|
|
2008-06-15 13:35:48 +00:00
|
|
|
msgwin_status_add(_("File %s saved."), doc->file_name);
|
|
|
|
ui_update_statusbar(doc, -1);
|
2006-08-30 18:53:45 +00:00
|
|
|
#ifdef HAVE_VTE
|
2009-02-01 18:03:19 +00:00
|
|
|
vte_cwd((doc->real_path != NULL) ? doc->real_path : doc->file_name, FALSE);
|
2006-08-30 18:53:45 +00:00
|
|
|
#endif
|
2007-08-10 16:11:17 +00:00
|
|
|
}
|
2009-01-21 15:50:12 +00:00
|
|
|
g_free(locale_filename);
|
|
|
|
|
2008-11-21 17:33:35 +00:00
|
|
|
g_signal_emit_by_name(geany_object, "document-save", doc);
|
|
|
|
|
2006-09-05 18:33:48 +00:00
|
|
|
return TRUE;
|
2005-11-22 12:26:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-07-09 17:33:31 +00:00
|
|
|
/* special search function, used from the find entry in the toolbar
|
|
|
|
* return TRUE if text was found otherwise FALSE
|
|
|
|
* return also TRUE if text is empty */
|
2014-08-19 03:40:52 +02:00
|
|
|
gboolean document_search_bar_find(GeanyDocument *doc, const gchar *text, gboolean inc,
|
2011-03-24 16:51:02 +00:00
|
|
|
gboolean backwards)
|
2005-11-22 12:26:26 +00:00
|
|
|
{
|
2006-10-21 10:50:45 +00:00
|
|
|
gint start_pos, search_pos;
|
2009-07-04 09:20:28 +00:00
|
|
|
struct Sci_TextToFind ttf;
|
2005-11-22 12:26:26 +00:00
|
|
|
|
2009-04-15 22:47:33 +00:00
|
|
|
g_return_val_if_fail(text != NULL, FALSE);
|
|
|
|
g_return_val_if_fail(doc != NULL, FALSE);
|
|
|
|
if (! *text)
|
2007-07-09 17:33:31 +00:00
|
|
|
return TRUE;
|
2005-11-22 12:26:26 +00:00
|
|
|
|
2011-03-24 16:51:02 +00:00
|
|
|
start_pos = (inc || backwards) ? sci_get_selection_start(doc->editor->sci) :
|
2008-07-14 11:13:54 +00:00
|
|
|
sci_get_selection_end(doc->editor->sci); /* equal if no selection */
|
2006-10-21 10:50:45 +00:00
|
|
|
|
2011-03-24 16:51:02 +00:00
|
|
|
/* search cursor to end or start */
|
2006-10-21 10:50:45 +00:00
|
|
|
ttf.chrg.cpMin = start_pos;
|
2011-03-24 16:51:02 +00:00
|
|
|
ttf.chrg.cpMax = backwards ? 0 : sci_get_length(doc->editor->sci);
|
2006-10-21 10:50:45 +00:00
|
|
|
ttf.lpstrText = (gchar *)text;
|
2014-08-19 03:40:52 +02:00
|
|
|
search_pos = sci_find_text(doc->editor->sci, 0, &ttf);
|
2006-10-21 10:50:45 +00:00
|
|
|
|
2011-03-24 16:51:02 +00:00
|
|
|
/* if no match, search start (or end) to cursor */
|
2006-10-21 10:50:45 +00:00
|
|
|
if (search_pos == -1)
|
|
|
|
{
|
2011-03-24 16:51:02 +00:00
|
|
|
if (backwards)
|
|
|
|
{
|
|
|
|
ttf.chrg.cpMin = sci_get_length(doc->editor->sci);
|
|
|
|
ttf.chrg.cpMax = start_pos;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ttf.chrg.cpMin = 0;
|
|
|
|
ttf.chrg.cpMax = start_pos + strlen(text);
|
|
|
|
}
|
2014-08-19 03:40:52 +02:00
|
|
|
search_pos = sci_find_text(doc->editor->sci, 0, &ttf);
|
2005-11-22 12:26:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (search_pos != -1)
|
|
|
|
{
|
2008-07-14 11:13:54 +00:00
|
|
|
gint line = sci_get_line_from_position(doc->editor->sci, ttf.chrgText.cpMin);
|
2008-02-22 13:30:16 +00:00
|
|
|
|
2008-02-27 13:17:29 +00:00
|
|
|
/* unfold maybe folded results */
|
2008-07-14 11:13:54 +00:00
|
|
|
sci_ensure_line_is_visible(doc->editor->sci, line);
|
2007-10-09 12:04:15 +00:00
|
|
|
|
2008-07-14 11:13:54 +00:00
|
|
|
sci_set_selection_start(doc->editor->sci, ttf.chrgText.cpMin);
|
|
|
|
sci_set_selection_end(doc->editor->sci, ttf.chrgText.cpMax);
|
2007-10-09 12:04:15 +00:00
|
|
|
|
2008-09-25 18:28:37 +00:00
|
|
|
if (! editor_line_in_view(doc->editor, line))
|
2008-02-27 13:17:29 +00:00
|
|
|
{ /* we need to force scrolling in case the cursor is outside of the current visible area
|
2008-05-29 17:00:54 +00:00
|
|
|
* GeanyDocument::scroll_percent doesn't work because sci isn't always updated
|
2008-02-27 13:17:29 +00:00
|
|
|
* while searching */
|
2008-09-25 18:28:37 +00:00
|
|
|
editor_scroll_to_line(doc->editor, -1, 0.3F);
|
2008-02-22 13:30:16 +00:00
|
|
|
}
|
2009-07-23 16:54:32 +00:00
|
|
|
else
|
|
|
|
sci_scroll_caret(doc->editor->sci); /* may need horizontal scrolling */
|
2007-07-09 17:33:31 +00:00
|
|
|
return TRUE;
|
2005-11-22 12:26:26 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2006-11-03 15:09:13 +00:00
|
|
|
if (! inc)
|
2005-11-22 12:26:26 +00:00
|
|
|
{
|
2007-10-24 10:52:48 +00:00
|
|
|
ui_set_statusbar(FALSE, _("\"%s\" was not found."), text);
|
2005-11-22 12:26:26 +00:00
|
|
|
}
|
2006-10-21 10:50:45 +00:00
|
|
|
utils_beep();
|
2008-07-14 11:13:54 +00:00
|
|
|
sci_goto_pos(doc->editor->sci, start_pos, FALSE); /* clear selection */
|
2007-07-09 17:33:31 +00:00
|
|
|
return FALSE;
|
2005-11-22 12:26:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-07-20 21:19:18 +00:00
|
|
|
/* General search function, used from the find dialog.
|
|
|
|
* Returns -1 on failure or the start position of the matching text.
|
2011-06-03 13:40:42 +00:00
|
|
|
* Will skip past any selection, ignoring it.
|
|
|
|
*
|
|
|
|
* @param text Text to find.
|
|
|
|
* @param original_text Text as it was entered by user, or @c NULL to use @c text
|
|
|
|
*/
|
|
|
|
gint document_find_text(GeanyDocument *doc, const gchar *text, const gchar *original_text,
|
2014-08-19 03:42:58 +02:00
|
|
|
GeanyFindFlags flags, gboolean search_backwards, GeanyMatchInfo **match_,
|
2013-04-12 18:07:30 +02:00
|
|
|
gboolean scroll, GtkWidget *parent)
|
2005-11-22 12:26:26 +00:00
|
|
|
{
|
2007-10-01 12:22:44 +00:00
|
|
|
gint selection_end, selection_start, search_pos;
|
2005-11-22 12:26:26 +00:00
|
|
|
|
2009-04-15 22:47:33 +00:00
|
|
|
g_return_val_if_fail(doc != NULL && text != NULL, -1);
|
|
|
|
if (! *text)
|
2009-04-05 21:07:40 +00:00
|
|
|
return -1;
|
|
|
|
|
2008-02-27 13:17:29 +00:00
|
|
|
/* Sci doesn't support searching backwards with a regex */
|
2014-08-10 01:46:16 +02:00
|
|
|
if (flags & GEANY_FIND_REGEXP)
|
2008-06-15 13:35:48 +00:00
|
|
|
search_backwards = FALSE;
|
2005-11-22 12:26:26 +00:00
|
|
|
|
2011-06-03 13:40:42 +00:00
|
|
|
if (!original_text)
|
|
|
|
original_text = text;
|
|
|
|
|
2008-07-14 11:13:54 +00:00
|
|
|
selection_start = sci_get_selection_start(doc->editor->sci);
|
|
|
|
selection_end = sci_get_selection_end(doc->editor->sci);
|
2005-11-22 12:26:26 +00:00
|
|
|
if ((selection_end - selection_start) > 0)
|
2008-02-27 13:17:29 +00:00
|
|
|
{ /* there's a selection so go to the end */
|
2005-11-22 12:26:26 +00:00
|
|
|
if (search_backwards)
|
2008-07-14 11:13:54 +00:00
|
|
|
sci_goto_pos(doc->editor->sci, selection_start, TRUE);
|
2005-11-22 12:26:26 +00:00
|
|
|
else
|
2008-07-14 11:13:54 +00:00
|
|
|
sci_goto_pos(doc->editor->sci, selection_end, TRUE);
|
2005-11-22 12:26:26 +00:00
|
|
|
}
|
|
|
|
|
2008-07-14 11:13:54 +00:00
|
|
|
sci_set_search_anchor(doc->editor->sci);
|
2005-11-22 12:26:26 +00:00
|
|
|
if (search_backwards)
|
2013-04-12 18:07:30 +02:00
|
|
|
search_pos = search_find_prev(doc->editor->sci, text, flags, match_);
|
2005-11-22 12:26:26 +00:00
|
|
|
else
|
2013-04-12 18:07:30 +02:00
|
|
|
search_pos = search_find_next(doc->editor->sci, text, flags, match_);
|
2005-11-22 12:26:26 +00:00
|
|
|
|
|
|
|
if (search_pos != -1)
|
|
|
|
{
|
2008-02-27 13:17:29 +00:00
|
|
|
/* unfold maybe folded results */
|
2008-07-14 11:13:54 +00:00
|
|
|
sci_ensure_line_is_visible(doc->editor->sci,
|
|
|
|
sci_get_line_from_position(doc->editor->sci, search_pos));
|
2006-11-03 15:09:13 +00:00
|
|
|
if (scroll)
|
2008-07-08 16:30:37 +00:00
|
|
|
doc->editor->scroll_percent = 0.3F;
|
2005-11-22 12:26:26 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-07-14 11:13:54 +00:00
|
|
|
gint sci_len = sci_get_length(doc->editor->sci);
|
2006-10-21 10:50:45 +00:00
|
|
|
|
2008-02-27 13:17:29 +00:00
|
|
|
/* if we just searched the whole text, give up searching. */
|
2006-10-21 10:50:45 +00:00
|
|
|
if ((selection_end == 0 && ! search_backwards) ||
|
|
|
|
(selection_end == sci_len && search_backwards))
|
|
|
|
{
|
2011-06-03 13:40:42 +00:00
|
|
|
ui_set_statusbar(FALSE, _("\"%s\" was not found."), original_text);
|
2006-10-21 10:50:45 +00:00
|
|
|
utils_beep();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2008-02-27 13:17:29 +00:00
|
|
|
/* we searched only part of the document, so ask whether to wraparound. */
|
2011-12-05 21:24:33 +02:00
|
|
|
if (search_prefs.always_wrap ||
|
2007-07-04 17:08:53 +00:00
|
|
|
dialogs_show_question_full(parent, GTK_STOCK_FIND, GTK_STOCK_CANCEL,
|
2011-06-03 13:40:42 +00:00
|
|
|
_("Wrap search and find again?"), _("\"%s\" was not found."), original_text))
|
2005-11-22 12:26:26 +00:00
|
|
|
{
|
2006-10-21 10:50:45 +00:00
|
|
|
gint ret;
|
2006-11-18 15:47:28 +00:00
|
|
|
|
2008-07-14 11:13:54 +00:00
|
|
|
sci_set_current_position(doc->editor->sci, (search_backwards) ? sci_len : 0, FALSE);
|
2013-04-12 18:07:30 +02:00
|
|
|
ret = document_find_text(doc, text, original_text, flags, search_backwards, match_, scroll, parent);
|
2006-11-18 15:47:28 +00:00
|
|
|
if (ret == -1)
|
2008-02-27 13:17:29 +00:00
|
|
|
{ /* return to original cursor position if not found */
|
2008-07-14 11:13:54 +00:00
|
|
|
sci_set_current_position(doc->editor->sci, selection_start, FALSE);
|
2006-11-18 15:47:28 +00:00
|
|
|
}
|
2006-10-21 10:50:45 +00:00
|
|
|
return ret;
|
2005-11-22 12:26:26 +00:00
|
|
|
}
|
|
|
|
}
|
2006-07-20 21:19:18 +00:00
|
|
|
return search_pos;
|
2005-11-22 12:26:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-10-22 14:56:05 +00:00
|
|
|
/* Replaces the selection if it matches, otherwise just finds the next match.
|
2011-06-03 13:40:42 +00:00
|
|
|
* Returns: start of replaced text, or -1 if no replacement was made
|
|
|
|
*
|
|
|
|
* @param find_text Text to find.
|
|
|
|
* @param original_find_text Text to find as it was entered by user, or @c NULL to use @c find_text
|
|
|
|
*/
|
|
|
|
gint document_replace_text(GeanyDocument *doc, const gchar *find_text, const gchar *original_find_text,
|
2014-08-19 03:42:58 +02:00
|
|
|
const gchar *replace_text, GeanyFindFlags flags, gboolean search_backwards)
|
2005-11-22 12:26:26 +00:00
|
|
|
{
|
|
|
|
gint selection_end, selection_start, search_pos;
|
2013-04-12 18:07:30 +02:00
|
|
|
GeanyMatchInfo *match = NULL;
|
2005-11-22 12:26:26 +00:00
|
|
|
|
2009-04-15 22:47:33 +00:00
|
|
|
g_return_val_if_fail(doc != NULL && find_text != NULL && replace_text != NULL, -1);
|
2009-04-05 21:07:40 +00:00
|
|
|
|
2009-04-15 22:47:33 +00:00
|
|
|
if (! *find_text)
|
2009-04-05 21:07:40 +00:00
|
|
|
return -1;
|
2006-10-22 14:56:05 +00:00
|
|
|
|
2008-02-27 13:17:29 +00:00
|
|
|
/* Sci doesn't support searching backwards with a regex */
|
2014-08-10 01:46:16 +02:00
|
|
|
if (flags & GEANY_FIND_REGEXP)
|
2008-06-15 13:35:48 +00:00
|
|
|
search_backwards = FALSE;
|
2005-11-22 12:26:26 +00:00
|
|
|
|
2011-06-03 13:40:42 +00:00
|
|
|
if (!original_find_text)
|
|
|
|
original_find_text = find_text;
|
|
|
|
|
2008-07-14 11:13:54 +00:00
|
|
|
selection_start = sci_get_selection_start(doc->editor->sci);
|
|
|
|
selection_end = sci_get_selection_end(doc->editor->sci);
|
2006-07-20 21:19:18 +00:00
|
|
|
if (selection_end == selection_start)
|
|
|
|
{
|
2008-02-27 13:17:29 +00:00
|
|
|
/* no selection so just find the next match */
|
2013-04-12 18:07:30 +02:00
|
|
|
document_find_text(doc, find_text, original_find_text, flags, search_backwards, NULL, TRUE, NULL);
|
2006-10-22 14:56:05 +00:00
|
|
|
return -1;
|
2005-11-22 12:26:26 +00:00
|
|
|
}
|
2008-02-27 13:17:29 +00:00
|
|
|
/* there's a selection so go to the start before finding to search through it
|
|
|
|
* this ensures there is a match */
|
2005-11-22 12:26:26 +00:00
|
|
|
if (search_backwards)
|
2008-07-14 11:13:54 +00:00
|
|
|
sci_goto_pos(doc->editor->sci, selection_end, TRUE);
|
2005-11-22 12:26:26 +00:00
|
|
|
else
|
2008-07-14 11:13:54 +00:00
|
|
|
sci_goto_pos(doc->editor->sci, selection_start, TRUE);
|
2006-07-20 21:19:18 +00:00
|
|
|
|
2013-04-12 18:07:30 +02:00
|
|
|
search_pos = document_find_text(doc, find_text, original_find_text, flags, search_backwards, &match, TRUE, NULL);
|
2008-02-27 13:17:29 +00:00
|
|
|
/* return if the original selected text did not match (at the start of the selection) */
|
2008-06-15 13:35:48 +00:00
|
|
|
if (search_pos != selection_start)
|
2013-04-12 18:07:30 +02:00
|
|
|
{
|
|
|
|
if (search_pos != -1)
|
|
|
|
geany_match_info_free(match);
|
2008-06-15 13:35:48 +00:00
|
|
|
return -1;
|
2013-04-12 18:07:30 +02:00
|
|
|
}
|
2005-11-22 12:26:26 +00:00
|
|
|
|
|
|
|
if (search_pos != -1)
|
|
|
|
{
|
2013-04-12 18:07:30 +02:00
|
|
|
gint replace_len = search_replace_match(doc->editor->sci, match, replace_text);
|
2008-02-27 13:17:29 +00:00
|
|
|
/* select the replacement - find text will skip past the selected text */
|
2008-07-14 11:13:54 +00:00
|
|
|
sci_set_selection_start(doc->editor->sci, search_pos);
|
|
|
|
sci_set_selection_end(doc->editor->sci, search_pos + replace_len);
|
2013-04-12 18:07:30 +02:00
|
|
|
geany_match_info_free(match);
|
2005-11-22 12:26:26 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-02-27 13:17:29 +00:00
|
|
|
/* no match in the selection */
|
2006-02-10 20:56:17 +00:00
|
|
|
utils_beep();
|
2005-11-22 12:26:26 +00:00
|
|
|
}
|
2006-10-22 14:56:05 +00:00
|
|
|
return search_pos;
|
2005-11-22 12:26:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-06-03 13:40:42 +00:00
|
|
|
static void show_replace_summary(GeanyDocument *doc, gint count, const gchar *original_find_text,
|
|
|
|
const gchar *original_replace_text)
|
2006-12-17 17:13:46 +00:00
|
|
|
{
|
2011-06-03 13:40:42 +00:00
|
|
|
gchar *filename;
|
2006-12-17 17:13:46 +00:00
|
|
|
|
|
|
|
if (count == 0)
|
|
|
|
{
|
2011-06-03 13:40:42 +00:00
|
|
|
ui_set_statusbar(FALSE, _("No matches found for \"%s\"."), original_find_text);
|
2006-12-17 17:13:46 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-06-15 13:35:48 +00:00
|
|
|
filename = g_path_get_basename(DOC_FILENAME(doc));
|
2011-06-03 13:40:42 +00:00
|
|
|
ui_set_statusbar(TRUE, ngettext(
|
|
|
|
"%s: replaced %d occurrence of \"%s\" with \"%s\".",
|
|
|
|
"%s: replaced %d occurrences of \"%s\" with \"%s\".",
|
|
|
|
count), filename, count, original_find_text, original_replace_text);
|
2006-12-17 17:13:46 +00:00
|
|
|
g_free(filename);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-06-15 13:35:48 +00:00
|
|
|
/* Replace all text matches in a certain range within document.
|
2007-03-13 15:40:59 +00:00
|
|
|
* If not NULL, *new_range_end is set to the new range endpoint after replacing,
|
|
|
|
* or -1 if no text was found.
|
|
|
|
* scroll_to_match is whether to scroll the last replacement in view (which also
|
|
|
|
* clears the selection).
|
|
|
|
* Returns: the number of replacements made. */
|
|
|
|
static guint
|
2008-06-15 13:35:48 +00:00
|
|
|
document_replace_range(GeanyDocument *doc, const gchar *find_text, const gchar *replace_text,
|
2014-08-19 03:42:58 +02:00
|
|
|
GeanyFindFlags flags, gint start, gint end, gboolean scroll_to_match, gint *new_range_end)
|
2005-11-22 12:26:26 +00:00
|
|
|
{
|
2006-08-23 18:08:48 +00:00
|
|
|
gint count = 0;
|
2009-07-04 09:20:28 +00:00
|
|
|
struct Sci_TextToFind ttf;
|
2007-07-24 15:34:35 +00:00
|
|
|
ScintillaObject *sci;
|
2005-11-22 12:26:26 +00:00
|
|
|
|
2007-03-13 15:40:59 +00:00
|
|
|
if (new_range_end != NULL)
|
|
|
|
*new_range_end = -1;
|
2009-04-05 21:07:40 +00:00
|
|
|
|
2009-04-15 22:47:33 +00:00
|
|
|
g_return_val_if_fail(doc != NULL && find_text != NULL && replace_text != NULL, 0);
|
2009-04-05 21:07:40 +00:00
|
|
|
|
2009-04-15 22:47:33 +00:00
|
|
|
if (! *find_text || doc->readonly)
|
2009-04-05 21:07:40 +00:00
|
|
|
return 0;
|
2005-11-22 12:26:26 +00:00
|
|
|
|
2008-07-14 11:13:54 +00:00
|
|
|
sci = doc->editor->sci;
|
2007-07-24 15:34:35 +00:00
|
|
|
|
2006-07-18 14:09:01 +00:00
|
|
|
ttf.chrg.cpMin = start;
|
|
|
|
ttf.chrg.cpMax = end;
|
|
|
|
ttf.lpstrText = (gchar*)find_text;
|
2007-03-06 16:57:09 +00:00
|
|
|
|
2010-03-08 13:23:32 +00:00
|
|
|
sci_start_undo_action(sci);
|
|
|
|
count = search_replace_range(sci, &ttf, flags, replace_text);
|
2007-07-24 15:34:35 +00:00
|
|
|
sci_end_undo_action(sci);
|
2006-09-02 23:28:34 +00:00
|
|
|
|
2007-03-13 15:40:59 +00:00
|
|
|
if (count > 0)
|
2008-02-27 13:17:29 +00:00
|
|
|
{ /* scroll last match in view, will destroy the existing selection */
|
2007-03-01 17:36:51 +00:00
|
|
|
if (scroll_to_match)
|
2007-07-24 15:34:35 +00:00
|
|
|
sci_goto_pos(sci, ttf.chrg.cpMin, TRUE);
|
2007-03-06 16:57:09 +00:00
|
|
|
|
2007-03-13 15:40:59 +00:00
|
|
|
if (new_range_end != NULL)
|
2010-03-25 13:43:37 +00:00
|
|
|
*new_range_end = ttf.chrg.cpMax;
|
2006-07-18 17:43:22 +00:00
|
|
|
}
|
2007-03-13 15:40:59 +00:00
|
|
|
return count;
|
2005-11-22 12:26:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-06-15 13:35:48 +00:00
|
|
|
void document_replace_sel(GeanyDocument *doc, const gchar *find_text, const gchar *replace_text,
|
2014-08-19 03:42:58 +02:00
|
|
|
const gchar *original_find_text, const gchar *original_replace_text, GeanyFindFlags flags)
|
2005-11-22 12:26:26 +00:00
|
|
|
{
|
2007-04-15 19:25:22 +00:00
|
|
|
gint selection_end, selection_start, selection_mode, selected_lines, last_line = 0;
|
2007-03-13 15:40:59 +00:00
|
|
|
gint max_column = 0, count = 0;
|
2007-03-01 17:36:51 +00:00
|
|
|
gboolean replaced = FALSE;
|
2005-11-22 12:26:26 +00:00
|
|
|
|
2009-04-15 22:47:33 +00:00
|
|
|
g_return_if_fail(doc != NULL && find_text != NULL && replace_text != NULL);
|
2009-04-05 21:07:40 +00:00
|
|
|
|
2009-04-15 22:47:33 +00:00
|
|
|
if (! *find_text)
|
2009-04-05 21:07:40 +00:00
|
|
|
return;
|
2005-11-22 12:26:26 +00:00
|
|
|
|
2008-07-14 11:13:54 +00:00
|
|
|
selection_start = sci_get_selection_start(doc->editor->sci);
|
|
|
|
selection_end = sci_get_selection_end(doc->editor->sci);
|
2008-02-27 13:17:29 +00:00
|
|
|
/* do we have a selection? */
|
2006-07-18 14:09:01 +00:00
|
|
|
if ((selection_end - selection_start) == 0)
|
|
|
|
{
|
|
|
|
utils_beep();
|
|
|
|
return;
|
|
|
|
}
|
2005-11-22 12:26:26 +00:00
|
|
|
|
2008-07-14 11:13:54 +00:00
|
|
|
selection_mode = sci_get_selection_mode(doc->editor->sci);
|
|
|
|
selected_lines = sci_get_lines_selected(doc->editor->sci);
|
2008-02-27 13:17:29 +00:00
|
|
|
/* handle rectangle, multi line selections (it doesn't matter on a single line) */
|
2007-03-01 17:36:51 +00:00
|
|
|
if (selection_mode == SC_SEL_RECTANGLE && selected_lines > 1)
|
2006-12-17 17:13:46 +00:00
|
|
|
{
|
2007-03-13 15:40:59 +00:00
|
|
|
gint first_line, line;
|
2007-03-06 16:57:09 +00:00
|
|
|
|
2008-07-14 11:13:54 +00:00
|
|
|
sci_start_undo_action(doc->editor->sci);
|
2007-03-06 16:57:09 +00:00
|
|
|
|
2008-07-14 11:13:54 +00:00
|
|
|
first_line = sci_get_line_from_position(doc->editor->sci, selection_start);
|
2008-02-27 13:17:29 +00:00
|
|
|
/* Find the last line with chars selected (not EOL char) */
|
2008-07-14 11:13:54 +00:00
|
|
|
last_line = sci_get_line_from_position(doc->editor->sci,
|
2008-09-25 18:28:37 +00:00
|
|
|
selection_end - editor_get_eol_char_len(doc->editor));
|
2007-03-01 17:36:51 +00:00
|
|
|
last_line = MAX(first_line, last_line);
|
|
|
|
for (line = first_line; line < (first_line + selected_lines); line++)
|
|
|
|
{
|
2008-07-14 11:13:54 +00:00
|
|
|
gint line_start = sci_get_pos_at_line_sel_start(doc->editor->sci, line);
|
|
|
|
gint line_end = sci_get_pos_at_line_sel_end(doc->editor->sci, line);
|
2007-03-06 16:57:09 +00:00
|
|
|
|
2008-02-27 13:17:29 +00:00
|
|
|
/* skip line if there is no selection */
|
2007-03-01 17:36:51 +00:00
|
|
|
if (line_start != INVALID_POSITION)
|
|
|
|
{
|
2008-02-27 13:17:29 +00:00
|
|
|
/* don't let document_replace_range() scroll to match to keep our selection */
|
2007-03-13 15:40:59 +00:00
|
|
|
gint new_sel_end;
|
|
|
|
|
2008-06-15 13:35:48 +00:00
|
|
|
count += document_replace_range(doc, find_text, replace_text, flags,
|
2007-03-13 15:40:59 +00:00
|
|
|
line_start, line_end, FALSE, &new_sel_end);
|
|
|
|
if (new_sel_end != -1)
|
2007-03-01 17:36:51 +00:00
|
|
|
{
|
2007-03-06 16:57:09 +00:00
|
|
|
replaced = TRUE;
|
2008-02-27 13:17:29 +00:00
|
|
|
/* this gets the greatest column within the selection after replacing */
|
2007-03-01 17:36:51 +00:00
|
|
|
max_column = MAX(max_column,
|
2008-07-14 11:13:54 +00:00
|
|
|
new_sel_end - sci_get_position_from_line(doc->editor->sci, line));
|
2007-03-01 17:36:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-07-14 11:13:54 +00:00
|
|
|
sci_end_undo_action(doc->editor->sci);
|
2006-12-17 17:13:46 +00:00
|
|
|
}
|
2008-02-27 13:17:29 +00:00
|
|
|
else /* handle normal line selection */
|
2005-11-22 12:26:26 +00:00
|
|
|
{
|
2008-06-15 13:35:48 +00:00
|
|
|
count += document_replace_range(doc, find_text, replace_text, flags,
|
2007-03-13 15:40:59 +00:00
|
|
|
selection_start, selection_end, TRUE, &selection_end);
|
2007-03-01 17:36:51 +00:00
|
|
|
if (selection_end != -1)
|
|
|
|
replaced = TRUE;
|
2005-11-22 12:26:26 +00:00
|
|
|
}
|
2007-03-06 16:57:09 +00:00
|
|
|
|
2007-03-01 17:36:51 +00:00
|
|
|
if (replaced)
|
2008-02-27 13:17:29 +00:00
|
|
|
{ /* update the selection for the new endpoint */
|
2007-03-06 16:57:09 +00:00
|
|
|
|
2007-03-01 17:36:51 +00:00
|
|
|
if (selection_mode == SC_SEL_RECTANGLE && selected_lines > 1)
|
|
|
|
{
|
2008-02-27 13:17:29 +00:00
|
|
|
/* now we can scroll to the selection and destroy it because we rebuild it later */
|
2008-07-14 11:13:54 +00:00
|
|
|
/*sci_goto_pos(doc->editor->sci, selection_start, FALSE);*/
|
2007-03-06 16:57:09 +00:00
|
|
|
|
2008-02-27 13:17:29 +00:00
|
|
|
/* Note: the selection will be wrapped to last_line + 1 if max_column is greater than
|
|
|
|
* the highest column on the last line. The wrapped selection is completely different
|
|
|
|
* from the original one, so skip the selection at all */
|
2008-03-13 17:53:03 +00:00
|
|
|
/* TODO is there a better way to handle the wrapped selection? */
|
2008-07-14 11:13:54 +00:00
|
|
|
if ((sci_get_line_length(doc->editor->sci, last_line) - 1) >= max_column)
|
2008-02-27 13:17:29 +00:00
|
|
|
{ /* for keeping and adjusting the selection in multi line rectangle selection we
|
|
|
|
* need the last line of the original selection and the greatest column number after
|
|
|
|
* replacing and set the selection end to the last line at the greatest column */
|
2008-07-14 11:13:54 +00:00
|
|
|
sci_set_selection_start(doc->editor->sci, selection_start);
|
|
|
|
sci_set_selection_end(doc->editor->sci,
|
|
|
|
sci_get_position_from_line(doc->editor->sci, last_line) + max_column);
|
|
|
|
sci_set_selection_mode(doc->editor->sci, selection_mode);
|
2007-03-01 17:36:51 +00:00
|
|
|
}
|
|
|
|
}
|
2007-03-06 16:57:09 +00:00
|
|
|
else
|
2007-03-01 17:36:51 +00:00
|
|
|
{
|
2008-07-14 11:13:54 +00:00
|
|
|
sci_set_selection_start(doc->editor->sci, selection_start);
|
|
|
|
sci_set_selection_end(doc->editor->sci, selection_end);
|
2007-03-01 17:36:51 +00:00
|
|
|
}
|
2007-03-06 16:57:09 +00:00
|
|
|
}
|
2008-02-27 13:17:29 +00:00
|
|
|
else /* no replacements */
|
2007-03-01 17:36:51 +00:00
|
|
|
utils_beep();
|
2007-03-13 15:40:59 +00:00
|
|
|
|
2011-06-03 13:40:42 +00:00
|
|
|
show_replace_summary(doc, count, original_find_text, original_replace_text);
|
2006-07-18 14:09:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-01-18 17:05:13 +00:00
|
|
|
/* returns number of replacements made. */
|
|
|
|
gint document_replace_all(GeanyDocument *doc, const gchar *find_text, const gchar *replace_text,
|
2014-08-19 03:42:58 +02:00
|
|
|
const gchar *original_find_text, const gchar *original_replace_text, GeanyFindFlags flags)
|
2006-07-18 14:09:01 +00:00
|
|
|
{
|
2007-03-13 15:40:59 +00:00
|
|
|
gint len, count;
|
2009-04-15 22:47:33 +00:00
|
|
|
g_return_val_if_fail(doc != NULL && find_text != NULL && replace_text != NULL, FALSE);
|
2009-04-05 21:07:40 +00:00
|
|
|
|
2009-04-15 22:47:33 +00:00
|
|
|
if (! *find_text)
|
2009-04-05 21:07:40 +00:00
|
|
|
return FALSE;
|
2006-07-18 14:09:01 +00:00
|
|
|
|
2008-07-14 11:13:54 +00:00
|
|
|
len = sci_get_length(doc->editor->sci);
|
2007-03-13 15:40:59 +00:00
|
|
|
count = document_replace_range(
|
2008-06-15 13:35:48 +00:00
|
|
|
doc, find_text, replace_text, flags, 0, len, TRUE, NULL);
|
2007-03-13 15:40:59 +00:00
|
|
|
|
2011-06-03 13:40:42 +00:00
|
|
|
show_replace_summary(doc, count, original_find_text, original_replace_text);
|
2010-01-18 17:05:13 +00:00
|
|
|
return count;
|
2005-11-22 12:26:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-11-10 19:18:02 -08:00
|
|
|
/*
|
|
|
|
* Parses or re-parses the document's buffer and updates the type
|
|
|
|
* keywords and symbol list.
|
|
|
|
*
|
|
|
|
* @param doc The document.
|
|
|
|
*/
|
|
|
|
void document_update_tags(GeanyDocument *doc)
|
2008-11-07 14:37:17 +00:00
|
|
|
{
|
2011-10-22 23:48:53 -07:00
|
|
|
guchar *buffer_ptr;
|
|
|
|
gsize len;
|
2008-11-07 14:37:17 +00:00
|
|
|
|
2011-11-10 19:18:02 -08:00
|
|
|
g_return_if_fail(DOC_VALID(doc));
|
|
|
|
g_return_if_fail(app->tm_workspace != NULL);
|
2007-11-01 11:49:29 +00:00
|
|
|
|
2011-11-10 19:18:02 -08:00
|
|
|
/* early out if it's a new file or doesn't support tags */
|
|
|
|
if (! doc->file_name || ! doc->file_type || !filetype_has_tags(doc->file_type))
|
2006-09-30 16:15:45 +00:00
|
|
|
{
|
2011-11-10 19:18:02 -08:00
|
|
|
/* We must call sidebar_update_tag_list() before returning,
|
|
|
|
* to ensure that the symbol list is always updated properly (e.g.
|
|
|
|
* when creating a new document with a partial filename set. */
|
2009-09-28 11:16:59 +00:00
|
|
|
sidebar_update_tag_list(doc, FALSE);
|
2006-09-30 16:15:45 +00:00
|
|
|
return;
|
|
|
|
}
|
2005-11-22 12:26:26 +00:00
|
|
|
|
2011-11-10 19:18:02 -08:00
|
|
|
/* create a new TM file if there isn't one yet */
|
|
|
|
if (! doc->tm_file)
|
2005-11-22 12:26:26 +00:00
|
|
|
{
|
2008-06-15 13:35:48 +00:00
|
|
|
gchar *locale_filename = utils_get_locale_from_utf8(doc->file_name);
|
2009-10-19 14:24:40 +00:00
|
|
|
const gchar *name;
|
2007-11-01 13:00:32 +00:00
|
|
|
|
2009-10-19 14:24:40 +00:00
|
|
|
/* lookup the name rather than using filetype name to support custom filetypes */
|
|
|
|
name = tm_source_file_get_lang_name(doc->file_type->lang);
|
|
|
|
doc->tm_file = tm_source_file_new(locale_filename, FALSE, name);
|
2006-01-23 17:05:29 +00:00
|
|
|
g_free(locale_filename);
|
2007-11-01 13:00:32 +00:00
|
|
|
|
2011-11-10 19:18:02 -08:00
|
|
|
if (doc->tm_file && !tm_workspace_add_object(doc->tm_file))
|
2007-10-31 16:22:01 +00:00
|
|
|
{
|
2011-11-10 19:18:02 -08:00
|
|
|
tm_work_object_free(doc->tm_file);
|
|
|
|
doc->tm_file = NULL;
|
2007-10-31 16:22:01 +00:00
|
|
|
}
|
2005-11-22 12:26:26 +00:00
|
|
|
}
|
2011-11-10 19:18:02 -08:00
|
|
|
|
|
|
|
/* early out if there's no work object and we couldn't create one */
|
|
|
|
if (doc->tm_file == NULL)
|
|
|
|
{
|
|
|
|
/* We must call sidebar_update_tag_list() before returning,
|
|
|
|
* to ensure that the symbol list is always updated properly (e.g.
|
|
|
|
* when creating a new document with a partial filename set. */
|
|
|
|
sidebar_update_tag_list(doc, FALSE);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-11-22 16:58:44 +01:00
|
|
|
len = sci_get_length(doc->editor->sci);
|
|
|
|
/* tm_source_file_buffer_update() below don't support 0-length data,
|
|
|
|
* so just empty the tags array and leave */
|
|
|
|
if (len < 1)
|
|
|
|
{
|
|
|
|
tm_tags_array_free(doc->tm_file->tags_array, FALSE);
|
|
|
|
sidebar_update_tag_list(doc, FALSE);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-11-10 19:18:02 -08:00
|
|
|
/* Parse Scintilla's buffer directly using TagManager
|
|
|
|
* Note: this buffer *MUST NOT* be modified */
|
|
|
|
buffer_ptr = (guchar *) scintilla_send_message(doc->editor->sci, SCI_GETCHARACTERPOINTER, 0, 0);
|
|
|
|
tm_source_file_buffer_update(doc->tm_file, buffer_ptr, len, TRUE);
|
|
|
|
|
|
|
|
sidebar_update_tag_list(doc, TRUE);
|
2012-01-03 10:21:09 -08:00
|
|
|
document_highlight_tags(doc);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Re-highlights type keywords without re-parsing the whole document. */
|
|
|
|
void document_highlight_tags(GeanyDocument *doc)
|
|
|
|
{
|
|
|
|
GString *keywords_str;
|
|
|
|
gchar *keywords;
|
|
|
|
gint keyword_idx;
|
2011-11-10 19:18:02 -08:00
|
|
|
|
|
|
|
/* some filetypes support type keywords (such as struct names), but not
|
|
|
|
* necessarily all filetypes for a particular scintilla lexer. this
|
|
|
|
* tells us whether the filetype supports keywords, and if so
|
|
|
|
* which index to use for the scintilla keywords set. */
|
|
|
|
switch (doc->file_type->id)
|
|
|
|
{
|
|
|
|
case GEANY_FILETYPES_C:
|
|
|
|
case GEANY_FILETYPES_CPP:
|
|
|
|
case GEANY_FILETYPES_CS:
|
|
|
|
case GEANY_FILETYPES_D:
|
|
|
|
case GEANY_FILETYPES_JAVA:
|
2011-11-13 00:45:12 +01:00
|
|
|
case GEANY_FILETYPES_OBJECTIVEC:
|
2011-11-10 19:18:02 -08:00
|
|
|
case GEANY_FILETYPES_VALA:
|
2013-10-16 17:19:29 -04:00
|
|
|
case GEANY_FILETYPES_RUST:
|
2011-11-10 19:18:02 -08:00
|
|
|
{
|
|
|
|
|
|
|
|
/* index of the keyword set in the Scintilla lexer, for
|
|
|
|
* example in LexCPP.cxx, see "cppWordLists" global array.
|
|
|
|
* TODO: this magic number should be a member of the filetype */
|
|
|
|
keyword_idx = 3;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return; /* early out if type keywords are not supported */
|
|
|
|
}
|
2012-04-23 16:17:40 +01:00
|
|
|
if (!app->tm_workspace->work_object.tags_array)
|
|
|
|
return;
|
2011-11-10 19:18:02 -08:00
|
|
|
|
|
|
|
/* get any type keywords and tell scintilla about them
|
|
|
|
* this will cause the type keywords to be colourized in scintilla */
|
|
|
|
keywords_str = symbols_find_tags_as_string(app->tm_workspace->work_object.tags_array,
|
|
|
|
TM_GLOBAL_TYPE_MASK, doc->file_type->lang);
|
|
|
|
if (keywords_str)
|
2005-11-22 12:26:26 +00:00
|
|
|
{
|
2011-11-10 19:18:02 -08:00
|
|
|
keywords = g_string_free(keywords_str, FALSE);
|
|
|
|
sci_set_keywords(doc->editor->sci, keyword_idx, keywords);
|
|
|
|
g_free(keywords);
|
2011-11-12 18:15:28 -08:00
|
|
|
queue_colourise(doc); /* force re-highlighting the entire document */
|
2005-11-22 12:26:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-04-26 13:52:31 +00:00
|
|
|
static gboolean on_document_update_tag_list_idle(gpointer data)
|
|
|
|
{
|
|
|
|
GeanyDocument *doc = data;
|
|
|
|
|
|
|
|
if (! DOC_VALID(doc))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
if (! main_status.quitting)
|
2011-11-10 19:18:02 -08:00
|
|
|
document_update_tags(doc);
|
2011-04-26 13:52:31 +00:00
|
|
|
|
|
|
|
doc->priv->tag_list_update_source = 0;
|
2011-11-10 19:18:02 -08:00
|
|
|
|
|
|
|
/* don't update the tags until another modification of the buffer */
|
2011-04-26 13:52:31 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void document_update_tag_list_in_idle(GeanyDocument *doc)
|
|
|
|
{
|
|
|
|
if (editor_prefs.autocompletion_update_freq <= 0 || ! filetype_has_tags(doc->file_type))
|
|
|
|
return;
|
|
|
|
|
2011-11-10 19:18:02 -08:00
|
|
|
/* prevent "stacking up" callback handlers, we only need one to run soon */
|
2011-04-26 13:52:31 +00:00
|
|
|
if (doc->priv->tag_list_update_source != 0)
|
|
|
|
g_source_remove(doc->priv->tag_list_update_source);
|
2011-11-10 19:18:02 -08:00
|
|
|
|
2011-04-26 13:52:31 +00:00
|
|
|
doc->priv->tag_list_update_source = g_timeout_add_full(G_PRIORITY_LOW,
|
|
|
|
editor_prefs.autocompletion_update_freq, on_document_update_tag_list_idle, doc, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-07-13 11:58:21 +00:00
|
|
|
static void document_load_config(GeanyDocument *doc, GeanyFiletype *type,
|
|
|
|
gboolean filetype_changed)
|
2005-11-22 12:26:26 +00:00
|
|
|
{
|
2009-06-17 10:46:43 +00:00
|
|
|
g_return_if_fail(doc);
|
|
|
|
if (type == NULL)
|
|
|
|
type = filetypes[GEANY_FILETYPES_NONE];
|
2005-11-22 12:26:26 +00:00
|
|
|
|
2009-07-13 11:58:21 +00:00
|
|
|
if (filetype_changed)
|
2006-12-07 15:13:49 +00:00
|
|
|
{
|
2008-06-15 13:35:48 +00:00
|
|
|
doc->file_type = type;
|
2006-12-14 12:54:36 +00:00
|
|
|
|
2008-02-27 13:17:29 +00:00
|
|
|
/* delete tm file object to force creation of a new one */
|
2008-06-15 13:35:48 +00:00
|
|
|
if (doc->tm_file != NULL)
|
2006-12-14 12:54:36 +00:00
|
|
|
{
|
2008-06-15 13:35:48 +00:00
|
|
|
tm_workspace_remove_object(doc->tm_file, TRUE, TRUE);
|
|
|
|
doc->tm_file = NULL;
|
2006-12-14 12:54:36 +00:00
|
|
|
}
|
2009-10-20 12:07:12 +00:00
|
|
|
/* load tags files before highlighting (some lexers highlight global typenames) */
|
|
|
|
if (type->id != GEANY_FILETYPES_NONE)
|
|
|
|
symbols_global_tags_loaded(type->id);
|
|
|
|
|
2009-10-15 16:54:15 +00:00
|
|
|
highlighting_set_styles(doc->editor->sci, type);
|
2008-09-21 16:43:45 +00:00
|
|
|
editor_set_indentation_guides(doc->editor);
|
2008-06-15 13:35:48 +00:00
|
|
|
build_menu_update(doc);
|
2008-06-20 12:34:13 +00:00
|
|
|
queue_colourise(doc);
|
2010-09-30 13:50:33 +00:00
|
|
|
doc->priv->symbol_list_sort_mode = type->priv->symbol_list_sort_mode;
|
2006-12-07 15:13:49 +00:00
|
|
|
}
|
|
|
|
|
2011-11-10 19:18:02 -08:00
|
|
|
document_update_tags(doc);
|
2005-11-22 12:26:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-07-13 11:58:21 +00:00
|
|
|
/** Sets the filetype of the document (which controls syntax highlighting and tags)
|
|
|
|
* @param doc The document to use.
|
|
|
|
* @param type The filetype. */
|
|
|
|
void document_set_filetype(GeanyDocument *doc, GeanyFiletype *type)
|
|
|
|
{
|
|
|
|
gboolean ft_changed;
|
2009-09-06 16:51:11 +00:00
|
|
|
GeanyFiletype *old_ft;
|
2009-07-13 11:58:21 +00:00
|
|
|
|
|
|
|
g_return_if_fail(doc);
|
|
|
|
if (type == NULL)
|
|
|
|
type = filetypes[GEANY_FILETYPES_NONE];
|
|
|
|
|
2009-09-06 16:51:11 +00:00
|
|
|
old_ft = doc->file_type;
|
2009-07-13 11:58:21 +00:00
|
|
|
geany_debug("%s : %s (%s)",
|
|
|
|
(doc->file_name != NULL) ? doc->file_name : "unknown",
|
2010-11-04 18:06:09 +00:00
|
|
|
type->name,
|
2009-07-13 11:58:21 +00:00
|
|
|
(doc->encoding != NULL) ? doc->encoding : "unknown");
|
|
|
|
|
|
|
|
ft_changed = (doc->file_type != type); /* filetype has changed */
|
|
|
|
document_load_config(doc, type, ft_changed);
|
2009-09-06 16:51:11 +00:00
|
|
|
|
|
|
|
if (ft_changed)
|
2010-10-26 17:20:48 +00:00
|
|
|
{
|
2011-08-25 20:14:17 +00:00
|
|
|
const GeanyIndentPrefs *iprefs = editor_get_indent_prefs(NULL);
|
2011-03-26 17:25:24 +00:00
|
|
|
|
2011-08-25 20:14:17 +00:00
|
|
|
/* assume that if previous filetype was none and the settings are the default ones, this
|
|
|
|
* is the first time the filetype is carefully set, so we should apply indent settings */
|
2014-04-14 02:38:50 +02:00
|
|
|
if ((! old_ft || old_ft->id == GEANY_FILETYPES_NONE) &&
|
2011-08-25 20:14:17 +00:00
|
|
|
doc->editor->indent_type == iprefs->type &&
|
|
|
|
doc->editor->indent_width == iprefs->width)
|
|
|
|
{
|
|
|
|
document_apply_indent_settings(doc);
|
2011-03-20 17:50:59 +00:00
|
|
|
ui_document_show_hide(doc);
|
2011-03-26 17:25:24 +00:00
|
|
|
}
|
2011-08-25 20:14:17 +00:00
|
|
|
|
2010-10-26 17:20:48 +00:00
|
|
|
sidebar_openfiles_update(doc); /* to update the icon */
|
2009-09-06 16:51:11 +00:00
|
|
|
g_signal_emit_by_name(geany_object, "document-filetype-set", doc, old_ft);
|
2010-10-26 17:20:48 +00:00
|
|
|
}
|
2009-07-13 11:58:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void document_reload_config(GeanyDocument *doc)
|
|
|
|
{
|
|
|
|
document_load_config(doc, doc->file_type, TRUE);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-02-17 18:00:42 +00:00
|
|
|
/**
|
2009-08-09 20:31:03 +00:00
|
|
|
* Sets the encoding of a document.
|
2008-02-17 18:00:42 +00:00
|
|
|
* This function only set the encoding of the %document, it does not any conversions. The new
|
|
|
|
* encoding is used when e.g. saving the file.
|
|
|
|
*
|
2009-08-09 20:31:03 +00:00
|
|
|
* @param doc The document to use.
|
|
|
|
* @param new_encoding The encoding to be set for the document.
|
2008-02-17 18:00:42 +00:00
|
|
|
**/
|
2008-06-15 13:35:48 +00:00
|
|
|
void document_set_encoding(GeanyDocument *doc, const gchar *new_encoding)
|
2006-10-10 16:02:41 +00:00
|
|
|
{
|
2009-04-15 22:47:33 +00:00
|
|
|
if (doc == NULL || new_encoding == NULL ||
|
2009-04-05 21:07:40 +00:00
|
|
|
utils_str_equal(new_encoding, doc->encoding))
|
|
|
|
return;
|
2006-10-10 16:02:41 +00:00
|
|
|
|
2008-06-15 13:35:48 +00:00
|
|
|
g_free(doc->encoding);
|
|
|
|
doc->encoding = g_strdup(new_encoding);
|
2006-10-10 16:02:41 +00:00
|
|
|
|
2008-06-15 13:35:48 +00:00
|
|
|
ui_update_statusbar(doc, -1);
|
2008-12-18 21:21:53 +00:00
|
|
|
gtk_widget_set_sensitive(ui_lookup_widget(main_widgets.window, "menu_write_unicode_bom1"),
|
2008-06-15 13:35:48 +00:00
|
|
|
encodings_is_unicode_charset(doc->encoding));
|
2006-10-10 16:02:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-09-06 16:09:08 +00:00
|
|
|
/* own Undo / Redo implementation to be able to undo / redo changes
|
|
|
|
* to the encoding or the Unicode BOM (which are Scintilla independet).
|
|
|
|
* All Scintilla events are stored in the undo / redo buffer and are passed through. */
|
|
|
|
|
2006-10-10 16:02:41 +00:00
|
|
|
/* Clears the Undo and Redo buffer (to be called when reloading or closing the document) */
|
2008-06-15 13:35:48 +00:00
|
|
|
void document_undo_clear(GeanyDocument *doc)
|
2006-09-06 16:09:08 +00:00
|
|
|
{
|
|
|
|
undo_action *a;
|
|
|
|
|
2008-09-26 17:28:50 +00:00
|
|
|
while (g_trash_stack_height(&doc->priv->undo_actions) > 0)
|
2006-09-06 16:09:08 +00:00
|
|
|
{
|
2008-09-26 17:28:50 +00:00
|
|
|
a = g_trash_stack_pop(&doc->priv->undo_actions);
|
2009-04-05 21:07:40 +00:00
|
|
|
if (G_LIKELY(a != NULL))
|
2006-09-06 16:09:08 +00:00
|
|
|
{
|
|
|
|
switch (a->type)
|
|
|
|
{
|
|
|
|
case UNDO_ENCODING: g_free(a->data); break;
|
|
|
|
default: break;
|
|
|
|
}
|
|
|
|
g_free(a);
|
|
|
|
}
|
|
|
|
}
|
2008-09-26 17:28:50 +00:00
|
|
|
doc->priv->undo_actions = NULL;
|
2006-09-06 16:09:08 +00:00
|
|
|
|
2008-09-26 17:28:50 +00:00
|
|
|
while (g_trash_stack_height(&doc->priv->redo_actions) > 0)
|
2006-09-06 16:09:08 +00:00
|
|
|
{
|
2008-09-26 17:28:50 +00:00
|
|
|
a = g_trash_stack_pop(&doc->priv->redo_actions);
|
2009-04-05 21:07:40 +00:00
|
|
|
if (G_LIKELY(a != NULL))
|
2006-09-06 16:09:08 +00:00
|
|
|
{
|
|
|
|
switch (a->type)
|
|
|
|
{
|
|
|
|
case UNDO_ENCODING: g_free(a->data); break;
|
|
|
|
default: break;
|
|
|
|
}
|
|
|
|
g_free(a);
|
|
|
|
}
|
|
|
|
}
|
2008-09-26 17:28:50 +00:00
|
|
|
doc->priv->redo_actions = NULL;
|
2006-09-06 16:09:08 +00:00
|
|
|
|
2008-07-11 14:11:53 +00:00
|
|
|
if (! main_status.quitting && doc->editor != NULL)
|
2008-06-15 13:35:48 +00:00
|
|
|
document_set_text_changed(doc, FALSE);
|
2006-09-06 16:09:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-11-27 17:05:51 +00:00
|
|
|
/* note: this is called on SCN_MODIFIED notifications */
|
2008-06-15 13:35:48 +00:00
|
|
|
void document_undo_add(GeanyDocument *doc, guint type, gpointer data)
|
2006-09-06 16:09:08 +00:00
|
|
|
{
|
|
|
|
undo_action *action;
|
|
|
|
|
2009-04-15 22:47:33 +00:00
|
|
|
g_return_if_fail(doc != NULL);
|
2006-09-06 16:09:08 +00:00
|
|
|
|
|
|
|
action = g_new0(undo_action, 1);
|
|
|
|
action->type = type;
|
|
|
|
action->data = data;
|
|
|
|
|
2008-09-26 17:28:50 +00:00
|
|
|
g_trash_stack_push(&doc->priv->undo_actions, action);
|
2006-09-06 16:09:08 +00:00
|
|
|
|
2012-11-27 17:05:51 +00:00
|
|
|
/* avoid unnecessary redraws */
|
|
|
|
if (type != UNDO_SCINTILLA || !doc->changed)
|
|
|
|
document_set_text_changed(doc, TRUE);
|
|
|
|
|
2008-06-15 13:35:48 +00:00
|
|
|
ui_update_popup_reundo_items(doc);
|
2006-09-06 16:09:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-06-15 13:35:48 +00:00
|
|
|
gboolean document_can_undo(GeanyDocument *doc)
|
2006-09-06 16:09:08 +00:00
|
|
|
{
|
2009-04-15 22:47:33 +00:00
|
|
|
g_return_val_if_fail(doc != NULL, FALSE);
|
2006-09-06 16:09:08 +00:00
|
|
|
|
2008-09-26 17:28:50 +00:00
|
|
|
if (g_trash_stack_height(&doc->priv->undo_actions) > 0 || sci_can_undo(doc->editor->sci))
|
2006-09-06 16:09:08 +00:00
|
|
|
return TRUE;
|
|
|
|
else
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-06-15 13:35:48 +00:00
|
|
|
static void update_changed_state(GeanyDocument *doc)
|
2006-10-24 13:41:34 +00:00
|
|
|
{
|
2008-06-15 13:35:48 +00:00
|
|
|
doc->changed =
|
2008-07-14 11:13:54 +00:00
|
|
|
(sci_is_modified(doc->editor->sci) ||
|
2008-09-26 17:28:50 +00:00
|
|
|
doc->has_bom != doc->priv->saved_encoding.has_bom ||
|
|
|
|
! utils_str_equal(doc->encoding, doc->priv->saved_encoding.encoding));
|
2008-06-15 13:35:48 +00:00
|
|
|
document_set_text_changed(doc, doc->changed);
|
2006-10-24 13:41:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-06-15 13:35:48 +00:00
|
|
|
void document_undo(GeanyDocument *doc)
|
2006-10-10 16:02:41 +00:00
|
|
|
{
|
|
|
|
undo_action *action;
|
|
|
|
|
2009-04-15 22:47:33 +00:00
|
|
|
g_return_if_fail(doc != NULL);
|
2006-10-10 16:02:41 +00:00
|
|
|
|
2008-09-26 17:28:50 +00:00
|
|
|
action = g_trash_stack_pop(&doc->priv->undo_actions);
|
2006-10-10 16:02:41 +00:00
|
|
|
|
2009-04-05 21:07:40 +00:00
|
|
|
if (G_UNLIKELY(action == NULL))
|
2006-10-10 16:02:41 +00:00
|
|
|
{
|
2008-02-27 13:17:29 +00:00
|
|
|
/* fallback, should not be necessary */
|
2009-01-21 22:49:21 +00:00
|
|
|
geany_debug("%s: fallback used", G_STRFUNC);
|
2008-07-14 11:13:54 +00:00
|
|
|
sci_undo(doc->editor->sci);
|
2006-10-10 16:02:41 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
switch (action->type)
|
|
|
|
{
|
|
|
|
case UNDO_SCINTILLA:
|
|
|
|
{
|
2008-06-15 13:35:48 +00:00
|
|
|
document_redo_add(doc, UNDO_SCINTILLA, NULL);
|
2006-10-10 16:02:41 +00:00
|
|
|
|
2008-07-14 11:13:54 +00:00
|
|
|
sci_undo(doc->editor->sci);
|
2006-10-10 16:02:41 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case UNDO_BOM:
|
|
|
|
{
|
2008-06-15 13:35:48 +00:00
|
|
|
document_redo_add(doc, UNDO_BOM, GINT_TO_POINTER(doc->has_bom));
|
2006-10-10 16:02:41 +00:00
|
|
|
|
2008-06-15 13:35:48 +00:00
|
|
|
doc->has_bom = GPOINTER_TO_INT(action->data);
|
|
|
|
ui_update_statusbar(doc, -1);
|
|
|
|
ui_document_show_hide(doc);
|
2006-10-10 16:02:41 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case UNDO_ENCODING:
|
|
|
|
{
|
2008-02-27 13:17:29 +00:00
|
|
|
/* use the "old" encoding */
|
2008-06-15 13:35:48 +00:00
|
|
|
document_redo_add(doc, UNDO_ENCODING, g_strdup(doc->encoding));
|
2006-10-10 16:02:41 +00:00
|
|
|
|
2008-06-15 13:35:48 +00:00
|
|
|
document_set_encoding(doc, (const gchar*)action->data);
|
2006-10-10 16:02:41 +00:00
|
|
|
|
2008-05-22 14:41:28 +00:00
|
|
|
ignore_callback = TRUE;
|
2006-10-10 16:02:41 +00:00
|
|
|
encodings_select_radio_item((const gchar*)action->data);
|
2008-05-22 14:41:28 +00:00
|
|
|
ignore_callback = FALSE;
|
2006-10-10 16:02:41 +00:00
|
|
|
|
|
|
|
g_free(action->data);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default: break;
|
|
|
|
}
|
|
|
|
}
|
2008-02-27 13:17:29 +00:00
|
|
|
g_free(action); /* free the action which was taken from the stack */
|
2006-10-10 16:02:41 +00:00
|
|
|
|
2008-06-15 13:35:48 +00:00
|
|
|
update_changed_state(doc);
|
|
|
|
ui_update_popup_reundo_items(doc);
|
2006-10-10 16:02:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-06-15 13:35:48 +00:00
|
|
|
gboolean document_can_redo(GeanyDocument *doc)
|
2006-09-06 16:09:08 +00:00
|
|
|
{
|
2009-04-15 22:47:33 +00:00
|
|
|
g_return_val_if_fail(doc != NULL, FALSE);
|
2006-09-06 16:09:08 +00:00
|
|
|
|
2008-09-26 17:28:50 +00:00
|
|
|
if (g_trash_stack_height(&doc->priv->redo_actions) > 0 || sci_can_redo(doc->editor->sci))
|
2006-10-10 16:02:41 +00:00
|
|
|
return TRUE;
|
|
|
|
else
|
|
|
|
return FALSE;
|
2006-09-06 16:09:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-06-15 13:35:48 +00:00
|
|
|
void document_redo(GeanyDocument *doc)
|
2006-09-06 16:09:08 +00:00
|
|
|
{
|
2006-09-08 10:20:15 +00:00
|
|
|
undo_action *action;
|
|
|
|
|
2009-04-15 22:47:33 +00:00
|
|
|
g_return_if_fail(doc != NULL);
|
2006-09-06 16:09:08 +00:00
|
|
|
|
2008-09-26 17:28:50 +00:00
|
|
|
action = g_trash_stack_pop(&doc->priv->redo_actions);
|
2006-09-06 16:09:08 +00:00
|
|
|
|
2009-04-05 21:07:40 +00:00
|
|
|
if (G_UNLIKELY(action == NULL))
|
2006-09-06 16:09:08 +00:00
|
|
|
{
|
2008-02-27 13:17:29 +00:00
|
|
|
/* fallback, should not be necessary */
|
2009-01-21 22:49:21 +00:00
|
|
|
geany_debug("%s: fallback used", G_STRFUNC);
|
2008-07-14 11:13:54 +00:00
|
|
|
sci_redo(doc->editor->sci);
|
2006-09-06 16:09:08 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
switch (action->type)
|
|
|
|
{
|
|
|
|
case UNDO_SCINTILLA:
|
|
|
|
{
|
2008-06-15 13:35:48 +00:00
|
|
|
document_undo_add(doc, UNDO_SCINTILLA, NULL);
|
2006-10-10 16:02:41 +00:00
|
|
|
|
2008-07-14 11:13:54 +00:00
|
|
|
sci_redo(doc->editor->sci);
|
2006-09-06 16:09:08 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case UNDO_BOM:
|
|
|
|
{
|
2008-06-15 13:35:48 +00:00
|
|
|
document_undo_add(doc, UNDO_BOM, GINT_TO_POINTER(doc->has_bom));
|
2006-10-10 16:02:41 +00:00
|
|
|
|
2008-06-15 13:35:48 +00:00
|
|
|
doc->has_bom = GPOINTER_TO_INT(action->data);
|
|
|
|
ui_update_statusbar(doc, -1);
|
|
|
|
ui_document_show_hide(doc);
|
2006-09-06 16:09:08 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case UNDO_ENCODING:
|
|
|
|
{
|
2008-06-15 13:35:48 +00:00
|
|
|
document_undo_add(doc, UNDO_ENCODING, g_strdup(doc->encoding));
|
2006-10-10 16:02:41 +00:00
|
|
|
|
2008-06-15 13:35:48 +00:00
|
|
|
document_set_encoding(doc, (const gchar*)action->data);
|
2006-10-10 16:02:41 +00:00
|
|
|
|
2008-05-22 14:41:28 +00:00
|
|
|
ignore_callback = TRUE;
|
2006-10-10 16:02:41 +00:00
|
|
|
encodings_select_radio_item((const gchar*)action->data);
|
2008-05-22 14:41:28 +00:00
|
|
|
ignore_callback = FALSE;
|
2006-10-10 16:02:41 +00:00
|
|
|
|
|
|
|
g_free(action->data);
|
2006-09-06 16:09:08 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default: break;
|
|
|
|
}
|
|
|
|
}
|
2008-02-27 13:17:29 +00:00
|
|
|
g_free(action); /* free the action which was taken from the stack */
|
2006-09-06 16:09:08 +00:00
|
|
|
|
2008-06-15 13:35:48 +00:00
|
|
|
update_changed_state(doc);
|
|
|
|
ui_update_popup_reundo_items(doc);
|
2006-09-06 16:09:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-06-15 13:35:48 +00:00
|
|
|
static void document_redo_add(GeanyDocument *doc, guint type, gpointer data)
|
2006-09-06 16:09:08 +00:00
|
|
|
{
|
2006-10-10 16:02:41 +00:00
|
|
|
undo_action *action;
|
|
|
|
|
2009-04-15 22:47:33 +00:00
|
|
|
g_return_if_fail(doc != NULL);
|
2006-10-10 16:02:41 +00:00
|
|
|
|
|
|
|
action = g_new0(undo_action, 1);
|
|
|
|
action->type = type;
|
|
|
|
action->data = data;
|
2006-09-06 16:09:08 +00:00
|
|
|
|
2008-09-26 17:28:50 +00:00
|
|
|
g_trash_stack_push(&doc->priv->redo_actions, action);
|
2006-10-10 16:02:41 +00:00
|
|
|
|
2012-11-27 17:05:51 +00:00
|
|
|
if (type != UNDO_SCINTILLA || !doc->changed)
|
|
|
|
document_set_text_changed(doc, TRUE);
|
|
|
|
|
2008-06-15 13:35:48 +00:00
|
|
|
ui_update_popup_reundo_items(doc);
|
2006-09-06 16:09:08 +00:00
|
|
|
}
|
2006-07-17 10:42:26 +00:00
|
|
|
|
2006-10-10 16:02:41 +00:00
|
|
|
|
2013-02-11 03:31:05 +01:00
|
|
|
enum
|
|
|
|
{
|
|
|
|
STATUS_CHANGED,
|
|
|
|
STATUS_DISK_CHANGED,
|
|
|
|
STATUS_READONLY
|
|
|
|
};
|
2014-09-03 15:22:10 +01:00
|
|
|
|
2013-02-11 03:31:05 +01:00
|
|
|
static struct
|
|
|
|
{
|
|
|
|
const gchar *name;
|
|
|
|
GdkColor color;
|
|
|
|
gboolean loaded;
|
|
|
|
} document_status_styles[] = {
|
|
|
|
{ "geany-document-status-changed", {0}, FALSE },
|
|
|
|
{ "geany-document-status-disk-changed", {0}, FALSE },
|
|
|
|
{ "geany-document-status-readonly", {0}, FALSE }
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
static gint document_get_status_id(GeanyDocument *doc)
|
|
|
|
{
|
|
|
|
if (doc->changed)
|
|
|
|
return STATUS_CHANGED;
|
|
|
|
#ifdef USE_GIO_FILEMON
|
|
|
|
else if (doc->priv->file_disk_status == FILE_CHANGED)
|
2014-09-03 15:22:10 +01:00
|
|
|
#else
|
|
|
|
else if (doc->priv->protected)
|
2013-02-11 03:31:05 +01:00
|
|
|
#endif
|
2014-09-03 15:22:10 +01:00
|
|
|
return STATUS_DISK_CHANGED;
|
2013-02-11 03:31:05 +01:00
|
|
|
else if (doc->readonly)
|
|
|
|
return STATUS_READONLY;
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* returns an identifier that is to be set as a widget name or class to get it styled
|
|
|
|
* depending on the document status (changed, readonly, etc.)
|
|
|
|
* a NULL return value means default (unchanged) style */
|
|
|
|
const gchar *document_get_status_widget_class(GeanyDocument *doc)
|
|
|
|
{
|
|
|
|
gint status;
|
|
|
|
|
|
|
|
g_return_val_if_fail(doc != NULL, NULL);
|
|
|
|
|
|
|
|
status = document_get_status_id(doc);
|
|
|
|
if (status < 0)
|
|
|
|
return NULL;
|
|
|
|
else
|
|
|
|
return document_status_styles[status].name;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-02-08 19:52:21 +00:00
|
|
|
/**
|
2009-10-14 12:21:18 +00:00
|
|
|
* Gets the status color of the document, or @c NULL if default widget coloring should be used.
|
|
|
|
* Returned colors are red if the document has changes, green if the document is read-only
|
2009-07-21 22:12:13 +00:00
|
|
|
* or simply @c NULL if the document is unmodified but writable.
|
2009-02-08 19:52:21 +00:00
|
|
|
*
|
|
|
|
* @param doc The document to use.
|
|
|
|
*
|
2009-10-14 12:21:18 +00:00
|
|
|
* @return The color for the document or @c NULL if the default color should be used. The color
|
2009-02-08 19:52:21 +00:00
|
|
|
* object is owned by Geany and should not be modified or freed.
|
|
|
|
*
|
|
|
|
* @since 0.16
|
|
|
|
*/
|
|
|
|
const GdkColor *document_get_status_color(GeanyDocument *doc)
|
2006-12-05 10:37:36 +00:00
|
|
|
{
|
2013-02-11 03:31:05 +01:00
|
|
|
gint status;
|
2006-12-05 10:37:36 +00:00
|
|
|
|
2009-04-15 22:47:33 +00:00
|
|
|
g_return_val_if_fail(doc != NULL, NULL);
|
2008-06-15 13:35:48 +00:00
|
|
|
|
2013-02-11 03:31:05 +01:00
|
|
|
status = document_get_status_id(doc);
|
|
|
|
if (status < 0)
|
|
|
|
return NULL;
|
|
|
|
if (! document_status_styles[status].loaded)
|
|
|
|
{
|
2013-02-11 04:47:21 +01:00
|
|
|
#if GTK_CHECK_VERSION(3, 0, 0)
|
|
|
|
GdkRGBA color;
|
|
|
|
GtkWidgetPath *path = gtk_widget_path_new();
|
|
|
|
GtkStyleContext *ctx = gtk_style_context_new();
|
|
|
|
gtk_widget_path_append_type(path, GTK_TYPE_WINDOW);
|
|
|
|
gtk_widget_path_append_type(path, GTK_TYPE_BOX);
|
|
|
|
gtk_widget_path_append_type(path, GTK_TYPE_NOTEBOOK);
|
|
|
|
gtk_widget_path_append_type(path, GTK_TYPE_LABEL);
|
|
|
|
gtk_widget_path_iter_set_name(path, -1, document_status_styles[status].name);
|
|
|
|
gtk_style_context_set_screen(ctx, gtk_widget_get_screen(GTK_WIDGET(doc->editor->sci)));
|
|
|
|
gtk_style_context_set_path(ctx, path);
|
|
|
|
gtk_style_context_get_color(ctx, GTK_STATE_NORMAL, &color);
|
|
|
|
document_status_styles[status].color.red = 0xffff * color.red;
|
|
|
|
document_status_styles[status].color.green = 0xffff * color.green;
|
|
|
|
document_status_styles[status].color.blue = 0xffff * color.blue;
|
|
|
|
document_status_styles[status].loaded = TRUE;
|
|
|
|
gtk_widget_path_unref(path);
|
|
|
|
g_object_unref(ctx);
|
|
|
|
#else
|
2013-02-11 03:31:05 +01:00
|
|
|
GtkSettings *settings = gtk_widget_get_settings(GTK_WIDGET(doc->editor->sci));
|
|
|
|
gchar *path = g_strconcat("GeanyMainWindow.GtkHBox.GtkNotebook.",
|
|
|
|
document_status_styles[status].name, NULL);
|
|
|
|
GtkStyle *style = gtk_rc_get_style_by_paths(settings, path, NULL, GTK_TYPE_LABEL);
|
2006-12-05 10:37:36 +00:00
|
|
|
|
2013-02-11 03:31:05 +01:00
|
|
|
document_status_styles[status].color = style->fg[GTK_STATE_NORMAL];
|
|
|
|
document_status_styles[status].loaded = TRUE;
|
|
|
|
g_free(path);
|
2013-02-11 04:47:21 +01:00
|
|
|
#endif
|
2013-02-11 03:31:05 +01:00
|
|
|
}
|
|
|
|
return &document_status_styles[status].color;
|
2006-12-05 10:37:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-04-27 15:53:18 +01:00
|
|
|
/** Accessor function for @ref documents_array items.
|
2008-12-03 18:03:54 +00:00
|
|
|
* @warning Always check the returned document is valid (@c doc->is_valid).
|
|
|
|
* @param idx @c documents_array index.
|
2009-01-30 18:53:23 +00:00
|
|
|
* @return The document, or @c NULL if @a idx is out of range.
|
|
|
|
*
|
|
|
|
* @since 0.16
|
|
|
|
*/
|
2008-12-03 18:03:54 +00:00
|
|
|
GeanyDocument *document_index(gint idx)
|
2006-10-24 13:41:34 +00:00
|
|
|
{
|
2009-04-15 22:47:33 +00:00
|
|
|
return (idx >= 0 && idx < (gint) documents_array->len) ? documents[idx] : NULL;
|
2006-10-24 13:41:34 +00:00
|
|
|
}
|
2007-02-23 13:26:06 +00:00
|
|
|
|
|
|
|
|
2012-12-12 13:59:29 +00:00
|
|
|
GeanyDocument *document_clone(GeanyDocument *old_doc)
|
2007-07-17 14:52:57 +00:00
|
|
|
{
|
2007-09-03 16:09:53 +00:00
|
|
|
gchar *text;
|
2008-06-15 13:35:48 +00:00
|
|
|
GeanyDocument *doc;
|
2012-11-17 16:22:23 +00:00
|
|
|
ScintillaObject *old_sci;
|
2007-07-17 14:52:57 +00:00
|
|
|
|
2012-12-12 13:59:29 +00:00
|
|
|
g_return_val_if_fail(old_doc, NULL);
|
2012-11-17 16:22:23 +00:00
|
|
|
old_sci = old_doc->editor->sci;
|
|
|
|
if (sci_has_selection(old_sci))
|
|
|
|
text = sci_get_selection_contents(old_sci);
|
|
|
|
else
|
|
|
|
text = sci_get_contents(old_sci, -1);
|
|
|
|
|
2012-11-15 16:08:56 +00:00
|
|
|
doc = document_new_file(NULL, old_doc->file_type, text);
|
2007-09-03 16:09:53 +00:00
|
|
|
g_free(text);
|
2012-11-16 14:04:19 +00:00
|
|
|
document_set_text_changed(doc, TRUE);
|
2007-07-17 14:52:57 +00:00
|
|
|
|
2008-02-27 13:17:29 +00:00
|
|
|
/* copy file properties */
|
2008-07-08 16:30:37 +00:00
|
|
|
doc->editor->line_wrapping = old_doc->editor->line_wrapping;
|
2012-11-16 14:14:18 +00:00
|
|
|
doc->editor->line_breaking = old_doc->editor->line_breaking;
|
|
|
|
doc->editor->auto_indent = old_doc->editor->auto_indent;
|
|
|
|
editor_set_indent(doc->editor, old_doc->editor->indent_type,
|
|
|
|
old_doc->editor->indent_width);
|
2008-06-15 13:35:48 +00:00
|
|
|
doc->readonly = old_doc->readonly;
|
|
|
|
doc->has_bom = old_doc->has_bom;
|
2014-05-20 07:14:10 +02:00
|
|
|
doc->priv->protected = 0;
|
2008-06-15 13:35:48 +00:00
|
|
|
document_set_encoding(doc, old_doc->encoding);
|
2008-07-14 11:13:54 +00:00
|
|
|
sci_set_lines_wrapped(doc->editor->sci, doc->editor->line_wrapping);
|
|
|
|
sci_set_readonly(doc->editor->sci, doc->readonly);
|
2008-06-15 13:35:48 +00:00
|
|
|
|
2012-11-15 16:08:56 +00:00
|
|
|
/* update ui */
|
2008-06-15 13:35:48 +00:00
|
|
|
ui_document_show_hide(doc);
|
2012-12-12 13:59:29 +00:00
|
|
|
return doc;
|
2007-07-17 14:52:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-04-24 14:33:12 +00:00
|
|
|
/* @note If successful, this should always be followed up with a call to
|
|
|
|
* document_close_all().
|
|
|
|
* @return TRUE if all files were saved or had their changes discarded. */
|
|
|
|
gboolean document_account_for_unsaved(void)
|
|
|
|
{
|
2011-11-02 00:04:35 +01:00
|
|
|
guint i, p, page_count;
|
2008-04-24 14:33:12 +00:00
|
|
|
|
2009-02-10 21:11:25 +00:00
|
|
|
page_count = gtk_notebook_get_n_pages(GTK_NOTEBOOK(main_widgets.notebook));
|
2011-11-02 00:04:35 +01:00
|
|
|
/* iterate over documents in tabs order */
|
2009-02-10 21:11:25 +00:00
|
|
|
for (p = 0; p < page_count; p++)
|
2008-04-24 14:33:12 +00:00
|
|
|
{
|
2011-11-02 00:04:35 +01:00
|
|
|
GeanyDocument *doc = document_get_from_page(p);
|
|
|
|
|
2010-05-16 17:43:24 +00:00
|
|
|
if (DOC_VALID(doc) && doc->changed)
|
2008-04-24 14:33:12 +00:00
|
|
|
{
|
2008-06-15 13:35:48 +00:00
|
|
|
if (! dialogs_show_unsaved_file(doc))
|
2008-04-24 14:33:12 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* all documents should now be accounted for, so ignore any changes */
|
2011-11-02 00:04:35 +01:00
|
|
|
foreach_document (i)
|
2008-04-24 14:33:12 +00:00
|
|
|
{
|
2011-11-02 00:04:35 +01:00
|
|
|
documents[i]->changed = FALSE;
|
2008-04-24 14:33:12 +00:00
|
|
|
}
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void force_close_all(void)
|
|
|
|
{
|
2008-05-29 17:00:54 +00:00
|
|
|
guint i, len = documents_array->len;
|
2008-04-24 14:33:12 +00:00
|
|
|
|
|
|
|
/* check all documents have been accounted for */
|
|
|
|
for (i = 0; i < len; i++)
|
|
|
|
{
|
2008-06-18 14:18:26 +00:00
|
|
|
if (documents[i]->is_valid)
|
2008-04-24 14:33:12 +00:00
|
|
|
{
|
2008-05-29 17:00:54 +00:00
|
|
|
g_return_if_fail(!documents[i]->changed);
|
2008-04-24 14:33:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
main_status.closing_all = TRUE;
|
|
|
|
|
2010-05-16 17:43:24 +00:00
|
|
|
foreach_document(i)
|
2008-04-24 14:33:12 +00:00
|
|
|
{
|
2010-05-16 17:43:24 +00:00
|
|
|
document_close(documents[i]);
|
2008-04-24 14:33:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
main_status.closing_all = FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
gboolean document_close_all(void)
|
|
|
|
{
|
|
|
|
if (! document_account_for_unsaved())
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
force_close_all();
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-05-20 06:45:23 +02:00
|
|
|
/* *
|
|
|
|
* Shows a message related to a document.
|
|
|
|
*
|
|
|
|
* Use this whenever the user needs to see a document-related message,
|
|
|
|
* for example when the file was externally modified or deleted.
|
|
|
|
*
|
|
|
|
* Any of the buttons can be @c NULL. If not @c NULL, @a btn_1's
|
|
|
|
* @a response_1 response will be the default for the @c GtkInfoBar or
|
|
|
|
* @c GtkDialog.
|
|
|
|
*
|
|
|
|
* @param doc @c GeanyDocument.
|
|
|
|
* @param msgtype The type of message.
|
|
|
|
* @param response_cb A callback function called when there's a response.
|
|
|
|
* @param btn_1 The first action area button.
|
|
|
|
* @param response_1 The response for @a btn_1.
|
|
|
|
* @param btn_2 The second action area button.
|
|
|
|
* @param response_2 The response for @a btn_2.
|
|
|
|
* @param btn_3 The third action area button.
|
|
|
|
* @param response_3 The response for @a btn_3.
|
|
|
|
* @param extra_text Text to show below the main message.
|
|
|
|
* @param format The text format for the main message.
|
|
|
|
* @param ... Used with @a format as in @c printf.
|
|
|
|
*
|
|
|
|
* @since 1.25
|
|
|
|
* */
|
|
|
|
static GtkWidget* document_show_message(GeanyDocument *doc, GtkMessageType msgtype,
|
|
|
|
void (*response_cb)(GtkWidget *info_bar, gint response_id, GeanyDocument *doc),
|
|
|
|
const gchar *btn_1, GtkResponseType response_1,
|
|
|
|
const gchar *btn_2, GtkResponseType response_2,
|
|
|
|
const gchar *btn_3, GtkResponseType response_3,
|
|
|
|
const gchar *extra_text, const gchar *format, ...)
|
|
|
|
{
|
|
|
|
va_list args;
|
|
|
|
gchar *text, *markup;
|
|
|
|
GtkWidget *hbox, *vbox, *icon, *label, *extra_label, *content_area;
|
2014-05-26 16:24:24 +02:00
|
|
|
GtkWidget *info_widget, *parent;
|
2014-05-20 06:45:23 +02:00
|
|
|
parent = gtk_notebook_get_nth_page(GTK_NOTEBOOK(main_widgets.notebook),
|
|
|
|
document_get_notebook_page(doc));
|
|
|
|
|
|
|
|
va_start(args, format);
|
|
|
|
text = g_strdup_vprintf(format, args);
|
|
|
|
va_end(args);
|
|
|
|
|
|
|
|
markup = g_strdup_printf("<span size=\"larger\">%s</span>", text);
|
|
|
|
g_free(text);
|
|
|
|
|
|
|
|
info_widget = gtk_info_bar_new();
|
|
|
|
/* must be done now else Gtk-WARNING: widget not within a GtkWindow */
|
|
|
|
gtk_box_pack_start(GTK_BOX(parent), info_widget, FALSE, TRUE, 0);
|
|
|
|
|
|
|
|
gtk_info_bar_set_message_type(GTK_INFO_BAR(info_widget), msgtype);
|
|
|
|
|
|
|
|
if (btn_1)
|
|
|
|
gtk_info_bar_add_button(GTK_INFO_BAR(info_widget), btn_1, response_1);
|
|
|
|
if (btn_2)
|
|
|
|
gtk_info_bar_add_button(GTK_INFO_BAR(info_widget), btn_2, response_2);
|
|
|
|
if (btn_3)
|
|
|
|
gtk_info_bar_add_button(GTK_INFO_BAR(info_widget), btn_3, response_3);
|
|
|
|
|
|
|
|
content_area = gtk_info_bar_get_content_area(GTK_INFO_BAR(info_widget));
|
|
|
|
|
2014-05-28 18:01:23 +02:00
|
|
|
label = geany_wrap_label_new(NULL);
|
2014-05-20 06:45:23 +02:00
|
|
|
gtk_label_set_markup(GTK_LABEL(label), markup);
|
|
|
|
g_free(markup);
|
|
|
|
|
|
|
|
g_signal_connect(info_widget, "response", G_CALLBACK(response_cb), doc);
|
|
|
|
g_signal_connect_after(info_widget, "response", G_CALLBACK(gtk_widget_destroy), NULL);
|
|
|
|
|
|
|
|
hbox = gtk_hbox_new(FALSE, 12);
|
2014-05-28 18:01:23 +02:00
|
|
|
gtk_box_pack_start(GTK_BOX(content_area), hbox, TRUE, TRUE, 0);
|
2014-05-20 06:45:23 +02:00
|
|
|
|
|
|
|
switch (msgtype)
|
|
|
|
{
|
|
|
|
case GTK_MESSAGE_INFO:
|
|
|
|
icon = gtk_image_new_from_stock(GTK_STOCK_DIALOG_INFO, GTK_ICON_SIZE_DIALOG);
|
|
|
|
break;
|
|
|
|
case GTK_MESSAGE_WARNING:
|
|
|
|
icon = gtk_image_new_from_stock(GTK_STOCK_DIALOG_WARNING, GTK_ICON_SIZE_DIALOG);
|
|
|
|
break;
|
|
|
|
case GTK_MESSAGE_QUESTION:
|
|
|
|
icon = gtk_image_new_from_stock(GTK_STOCK_DIALOG_QUESTION, GTK_ICON_SIZE_DIALOG);
|
|
|
|
break;
|
|
|
|
case GTK_MESSAGE_ERROR:
|
|
|
|
icon = gtk_image_new_from_stock(GTK_STOCK_DIALOG_ERROR, GTK_ICON_SIZE_DIALOG);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
icon = NULL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (icon)
|
|
|
|
gtk_box_pack_start(GTK_BOX(hbox), icon, FALSE, TRUE, 0);
|
|
|
|
|
|
|
|
if (extra_text)
|
|
|
|
{
|
|
|
|
vbox = gtk_vbox_new(FALSE, 6);
|
2014-05-28 18:01:23 +02:00
|
|
|
extra_label = geany_wrap_label_new(extra_text);
|
2014-05-20 06:45:23 +02:00
|
|
|
gtk_box_pack_start(GTK_BOX(vbox), label, TRUE, TRUE, 0);
|
|
|
|
gtk_box_pack_start(GTK_BOX(vbox), extra_label, TRUE, TRUE, 0);
|
2014-05-28 18:01:23 +02:00
|
|
|
gtk_box_pack_start(GTK_BOX(hbox), vbox, TRUE, TRUE, 0);
|
2014-05-20 06:45:23 +02:00
|
|
|
}
|
|
|
|
else
|
2014-05-28 18:01:23 +02:00
|
|
|
gtk_box_pack_start(GTK_BOX(hbox), label, TRUE, TRUE, 0);
|
2014-05-20 06:45:23 +02:00
|
|
|
|
|
|
|
gtk_box_reorder_child(GTK_BOX(parent), info_widget, 0);
|
|
|
|
|
|
|
|
gtk_widget_show_all(info_widget);
|
|
|
|
|
|
|
|
return info_widget;
|
|
|
|
}
|
|
|
|
|
2014-09-03 17:43:17 +01:00
|
|
|
|
2011-11-06 15:59:32 -08:00
|
|
|
static void on_monitor_reload_file_response(GtkWidget *bar, gint response_id, GeanyDocument *doc)
|
|
|
|
{
|
2014-05-20 07:14:10 +02:00
|
|
|
unprotect_document(doc);
|
2014-05-24 16:53:59 +02:00
|
|
|
doc->priv->info_bars[MSG_TYPE_RELOAD] = NULL;
|
2014-05-20 07:14:10 +02:00
|
|
|
|
2014-06-09 12:31:35 +02:00
|
|
|
if (response_id == RESPONSE_DOCUMENT_RELOAD)
|
2011-11-06 15:59:32 -08:00
|
|
|
document_reload_file(doc, doc->encoding);
|
2014-06-09 12:31:35 +02:00
|
|
|
else if (response_id == RESPONSE_DOCUMENT_SAVE)
|
2014-08-27 16:32:25 +01:00
|
|
|
document_save_file(doc, TRUE);
|
|
|
|
else if (response_id == GTK_RESPONSE_CANCEL)
|
|
|
|
document_set_text_changed(doc, TRUE);
|
2011-11-06 15:59:32 -08:00
|
|
|
}
|
|
|
|
|
2014-09-03 17:43:17 +01:00
|
|
|
|
2014-05-26 16:24:24 +02:00
|
|
|
static gboolean on_sci_key(GtkWidget *widget, GdkEventKey *event, gpointer data)
|
2014-05-24 14:37:24 +02:00
|
|
|
{
|
|
|
|
GtkInfoBar *bar = GTK_INFO_BAR(data);
|
|
|
|
|
|
|
|
g_return_val_if_fail(event->type == GDK_KEY_PRESS, FALSE);
|
|
|
|
|
|
|
|
switch (event->keyval)
|
|
|
|
{
|
2014-06-04 14:16:39 +02:00
|
|
|
case GDK_Tab:
|
|
|
|
case GDK_ISO_Left_Tab:
|
2014-05-24 14:37:24 +02:00
|
|
|
{
|
2014-05-26 16:24:24 +02:00
|
|
|
GtkWidget *action_area = gtk_info_bar_get_action_area(bar);
|
2014-06-04 14:16:39 +02:00
|
|
|
GtkDirectionType dir = event->keyval == GDK_Tab ? GTK_DIR_TAB_FORWARD : GTK_DIR_TAB_BACKWARD;
|
2014-05-26 16:24:24 +02:00
|
|
|
gtk_widget_child_focus(action_area, dir);
|
2014-05-24 14:37:24 +02:00
|
|
|
return TRUE;
|
|
|
|
}
|
2014-06-04 14:16:39 +02:00
|
|
|
case GDK_Escape:
|
2014-05-24 14:37:24 +02:00
|
|
|
{
|
|
|
|
gtk_info_bar_response(bar, GTK_RESPONSE_CANCEL);
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-06-06 22:11:11 +02:00
|
|
|
/* Sets up a signal handler to intercept some keys during the lifetime of the GtkInfoBar */
|
2014-05-24 14:37:24 +02:00
|
|
|
static void enable_key_intercept(GeanyDocument *doc, GtkWidget *bar)
|
|
|
|
{
|
2014-06-09 12:35:22 +02:00
|
|
|
/* automatically focus editor again on bar close */
|
2014-08-07 15:14:22 +02:00
|
|
|
g_signal_connect_object(bar, "destroy", G_CALLBACK(gtk_widget_grab_focus), doc->editor->sci,
|
2014-07-26 13:36:19 +02:00
|
|
|
G_CONNECT_SWAPPED);
|
2014-06-06 22:11:11 +02:00
|
|
|
g_signal_connect_object(doc->editor->sci, "key-press-event", G_CALLBACK(on_sci_key), bar, 0);
|
2014-05-24 14:37:24 +02:00
|
|
|
}
|
2011-11-06 15:59:32 -08:00
|
|
|
|
2014-06-06 22:11:11 +02:00
|
|
|
|
2010-09-08 14:56:27 +00:00
|
|
|
static void monitor_reload_file(GeanyDocument *doc)
|
2008-06-12 16:50:01 +00:00
|
|
|
{
|
2008-06-15 13:35:48 +00:00
|
|
|
gchar *base_name = g_path_get_basename(doc->file_name);
|
2008-06-12 16:50:01 +00:00
|
|
|
|
2014-05-21 07:11:56 +02:00
|
|
|
/* show this message only once */
|
|
|
|
if (doc->priv->info_bars[MSG_TYPE_RELOAD] == NULL)
|
|
|
|
{
|
|
|
|
GtkWidget *bar;
|
2009-07-08 22:20:17 +00:00
|
|
|
|
2014-05-21 07:11:56 +02:00
|
|
|
bar = document_show_message(doc, GTK_MESSAGE_QUESTION, on_monitor_reload_file_response,
|
2014-06-09 12:31:35 +02:00
|
|
|
_("_Reload"), RESPONSE_DOCUMENT_RELOAD,
|
2014-09-04 11:53:32 +01:00
|
|
|
_("_Overwrite"), RESPONSE_DOCUMENT_SAVE,
|
2014-05-21 07:11:56 +02:00
|
|
|
GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
|
|
|
|
_("Do you want to reload it?"),
|
|
|
|
_("The file '%s' on the disk is more recent than the current buffer."),
|
|
|
|
base_name);
|
2011-11-06 15:59:32 -08:00
|
|
|
|
2014-05-21 07:11:56 +02:00
|
|
|
protect_document(doc);
|
|
|
|
doc->priv->info_bars[MSG_TYPE_RELOAD] = bar;
|
2014-05-24 14:37:24 +02:00
|
|
|
enable_key_intercept(doc, bar);
|
2014-05-21 07:11:56 +02:00
|
|
|
}
|
2011-11-06 15:59:32 -08:00
|
|
|
g_free(base_name);
|
2008-06-12 16:50:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-05-21 07:52:57 +02:00
|
|
|
static void on_monitor_resave_missing_file_response(GtkWidget *bar,
|
|
|
|
gint response_id,
|
|
|
|
GeanyDocument *doc)
|
2009-01-18 18:19:58 +00:00
|
|
|
{
|
2014-05-20 07:14:10 +02:00
|
|
|
unprotect_document(doc);
|
|
|
|
|
2014-06-09 12:31:35 +02:00
|
|
|
if (response_id == RESPONSE_DOCUMENT_SAVE)
|
2014-08-01 11:54:05 +01:00
|
|
|
dialogs_show_save_as();
|
2014-05-21 07:52:57 +02:00
|
|
|
|
2014-05-21 07:11:56 +02:00
|
|
|
doc->priv->info_bars[MSG_TYPE_RESAVE] = NULL;
|
2014-05-21 07:52:57 +02:00
|
|
|
}
|
|
|
|
|
2009-12-20 15:37:40 +00:00
|
|
|
|
2014-05-21 07:52:57 +02:00
|
|
|
static void monitor_resave_missing_file(GeanyDocument *doc)
|
|
|
|
{
|
2014-05-21 07:11:56 +02:00
|
|
|
if (doc->priv->info_bars[MSG_TYPE_RESAVE] == NULL)
|
|
|
|
{
|
2014-05-26 16:24:24 +02:00
|
|
|
GtkWidget *bar = doc->priv->info_bars[MSG_TYPE_RELOAD];
|
|
|
|
|
2014-05-21 08:11:37 +02:00
|
|
|
if (bar != NULL) /* the "file on disk is newer" warning is now moot */
|
|
|
|
gtk_info_bar_response(GTK_INFO_BAR(bar), GTK_RESPONSE_CANCEL);
|
2014-05-21 07:11:56 +02:00
|
|
|
|
|
|
|
bar = document_show_message(doc, GTK_MESSAGE_WARNING,
|
|
|
|
on_monitor_resave_missing_file_response,
|
2014-06-09 12:31:35 +02:00
|
|
|
GTK_STOCK_SAVE, RESPONSE_DOCUMENT_SAVE,
|
2014-05-21 07:11:56 +02:00
|
|
|
GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
|
|
|
|
NULL, GTK_RESPONSE_NONE,
|
|
|
|
_("Try to resave the file?"),
|
|
|
|
_("File \"%s\" was not found on disk!"),
|
|
|
|
doc->file_name);
|
|
|
|
|
|
|
|
protect_document(doc);
|
2014-06-08 14:22:56 +02:00
|
|
|
document_set_text_changed(doc, TRUE);
|
|
|
|
/* don't prompt more than once */
|
|
|
|
SETPTR(doc->real_path, NULL);
|
2014-05-21 07:11:56 +02:00
|
|
|
doc->priv->info_bars[MSG_TYPE_RESAVE] = bar;
|
2014-05-24 14:37:24 +02:00
|
|
|
enable_key_intercept(doc, bar);
|
2014-05-21 07:11:56 +02:00
|
|
|
}
|
2009-01-18 18:19:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-07-08 22:20:17 +00:00
|
|
|
/* Set force to force a disk check, otherwise it is ignored if there was a check
|
|
|
|
* in the last file_prefs.disk_check_timeout seconds.
|
|
|
|
* @return @c TRUE if the file has changed. */
|
|
|
|
gboolean document_check_disk_status(GeanyDocument *doc, gboolean force)
|
2009-01-18 18:19:58 +00:00
|
|
|
{
|
2009-07-08 22:20:17 +00:00
|
|
|
gboolean ret = FALSE;
|
|
|
|
gboolean use_gio_filemon;
|
|
|
|
time_t cur_time = 0;
|
2009-01-18 18:19:58 +00:00
|
|
|
struct stat st;
|
|
|
|
gchar *locale_filename;
|
2009-07-08 22:20:17 +00:00
|
|
|
FileDiskStatus old_status;
|
2009-01-18 18:19:58 +00:00
|
|
|
|
2009-07-08 22:20:17 +00:00
|
|
|
g_return_val_if_fail(doc != NULL, FALSE);
|
2009-01-18 18:19:58 +00:00
|
|
|
|
2009-07-08 22:20:17 +00:00
|
|
|
/* ignore remote files and documents that have never been saved to disk */
|
2012-04-23 16:17:40 +01:00
|
|
|
if (notebook_switch_in_progress() || file_prefs.disk_check_timeout == 0
|
2012-01-12 00:33:50 +01:00
|
|
|
|| doc->real_path == NULL || doc->priv->is_remote)
|
2009-07-08 22:20:17 +00:00
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
use_gio_filemon = (doc->priv->monitor != NULL);
|
|
|
|
|
|
|
|
if (use_gio_filemon)
|
|
|
|
{
|
|
|
|
if (doc->priv->file_disk_status != FILE_CHANGED && ! force)
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
cur_time = time(NULL);
|
|
|
|
if (! force && doc->priv->last_check > (cur_time - file_prefs.disk_check_timeout))
|
|
|
|
return FALSE;
|
2009-01-18 18:19:58 +00:00
|
|
|
|
2009-07-08 22:20:17 +00:00
|
|
|
doc->priv->last_check = cur_time;
|
|
|
|
}
|
2009-01-18 18:19:58 +00:00
|
|
|
|
|
|
|
locale_filename = utils_get_locale_from_utf8(doc->file_name);
|
|
|
|
if (g_stat(locale_filename, &st) != 0)
|
|
|
|
{
|
2009-07-08 22:20:17 +00:00
|
|
|
monitor_resave_missing_file(doc);
|
2010-09-08 14:56:27 +00:00
|
|
|
/* doc may be closed now */
|
2009-07-08 22:20:17 +00:00
|
|
|
ret = TRUE;
|
2009-01-18 18:19:58 +00:00
|
|
|
}
|
2011-10-13 16:56:40 +01:00
|
|
|
else if (! use_gio_filemon && /* ignore check when using GIO */
|
|
|
|
doc->priv->mtime > cur_time)
|
2009-01-18 18:19:58 +00:00
|
|
|
{
|
2009-01-21 22:49:21 +00:00
|
|
|
g_warning("%s: Something is wrong with the time stamps.", G_STRFUNC);
|
2011-10-13 16:56:40 +01:00
|
|
|
/* Note: on Windows st.st_mtime can be newer than cur_time */
|
2009-01-18 18:19:58 +00:00
|
|
|
}
|
|
|
|
else if (doc->priv->mtime < st.st_mtime)
|
|
|
|
{
|
2014-05-21 07:11:56 +02:00
|
|
|
/* make sure the user is not prompted again after he cancelled the "reload file?" message */
|
2009-07-08 22:20:17 +00:00
|
|
|
doc->priv->mtime = st.st_mtime;
|
2010-09-08 14:56:27 +00:00
|
|
|
monitor_reload_file(doc);
|
|
|
|
/* doc may be closed now */
|
2009-07-08 22:20:17 +00:00
|
|
|
ret = TRUE;
|
2009-01-18 18:19:58 +00:00
|
|
|
}
|
|
|
|
g_free(locale_filename);
|
|
|
|
|
2009-12-20 20:29:45 +00:00
|
|
|
if (DOC_VALID(doc))
|
2010-09-08 14:56:27 +00:00
|
|
|
{ /* doc can get invalid when a document was closed */
|
2009-12-20 20:29:45 +00:00
|
|
|
old_status = doc->priv->file_disk_status;
|
|
|
|
doc->priv->file_disk_status = FILE_OK;
|
|
|
|
if (old_status != doc->priv->file_disk_status)
|
|
|
|
ui_update_tab_status(doc);
|
|
|
|
}
|
2008-06-15 13:35:48 +00:00
|
|
|
return ret;
|
2008-06-12 16:50:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-04-13 12:16:02 +00:00
|
|
|
/** Compares documents by their display names.
|
|
|
|
* This matches @c GCompareFunc for use with e.g. @c g_ptr_array_sort().
|
|
|
|
* @note 'Display name' means the base name of the document's filename.
|
2011-04-10 17:03:41 +00:00
|
|
|
*
|
2011-04-13 12:16:02 +00:00
|
|
|
* @param a @c GeanyDocument**.
|
|
|
|
* @param b @c GeanyDocument**.
|
|
|
|
* @warning The arguments take the address of each document pointer.
|
|
|
|
* @return Negative value if a < b; zero if a = b; positive value if a > b.
|
2011-04-10 17:03:41 +00:00
|
|
|
*
|
2011-04-13 12:16:02 +00:00
|
|
|
* @since 0.21
|
2011-04-10 17:03:41 +00:00
|
|
|
*/
|
2011-04-12 17:20:59 +00:00
|
|
|
gint document_compare_by_display_name(gconstpointer a, gconstpointer b)
|
2011-04-10 17:03:41 +00:00
|
|
|
{
|
2011-04-12 17:17:54 +00:00
|
|
|
GeanyDocument *doc_a = *((GeanyDocument**) a);
|
|
|
|
GeanyDocument *doc_b = *((GeanyDocument**) b);
|
2011-04-10 17:03:41 +00:00
|
|
|
gchar *base_name_a, *base_name_b;
|
|
|
|
gint result;
|
|
|
|
|
|
|
|
base_name_a = g_path_get_basename(DOC_FILENAME(doc_a));
|
|
|
|
base_name_b = g_path_get_basename(DOC_FILENAME(doc_b));
|
|
|
|
|
|
|
|
result = strcmp(base_name_a, base_name_b);
|
|
|
|
|
|
|
|
g_free(base_name_a);
|
|
|
|
g_free(base_name_b);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-04-17 13:40:04 +00:00
|
|
|
/** Compares documents by their tab order.
|
|
|
|
* This matches @c GCompareFunc for use with e.g. @c g_ptr_array_sort().
|
|
|
|
*
|
|
|
|
* @param a @c GeanyDocument**.
|
|
|
|
* @param b @c GeanyDocument**.
|
|
|
|
* @warning The arguments take the address of each document pointer.
|
|
|
|
* @return Negative value if a < b; zero if a = b; positive value if a > b.
|
|
|
|
*
|
|
|
|
* @since 0.21 (GEANY_API_VERSION 209)
|
|
|
|
*/
|
|
|
|
gint document_compare_by_tab_order(gconstpointer a, gconstpointer b)
|
|
|
|
{
|
|
|
|
GeanyDocument *doc_a = *((GeanyDocument**) a);
|
|
|
|
GeanyDocument *doc_b = *((GeanyDocument**) b);
|
|
|
|
gint notebook_position_doc_a;
|
|
|
|
gint notebook_position_doc_b;
|
|
|
|
|
|
|
|
notebook_position_doc_a = document_get_notebook_page(doc_a);
|
|
|
|
notebook_position_doc_b = document_get_notebook_page(doc_b);
|
|
|
|
|
|
|
|
if (notebook_position_doc_a < notebook_position_doc_b)
|
|
|
|
return -1;
|
|
|
|
if (notebook_position_doc_a > notebook_position_doc_b)
|
|
|
|
return 1;
|
|
|
|
/* equality */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** Compares documents by their tab order, in reverse order.
|
|
|
|
* This matches @c GCompareFunc for use with e.g. @c g_ptr_array_sort().
|
|
|
|
*
|
|
|
|
* @param a @c GeanyDocument**.
|
|
|
|
* @param b @c GeanyDocument**.
|
|
|
|
* @warning The arguments take the address of each document pointer.
|
|
|
|
* @return Negative value if a < b; zero if a = b; positive value if a > b.
|
|
|
|
*
|
|
|
|
* @since 0.21 (GEANY_API_VERSION 209)
|
|
|
|
*/
|
|
|
|
gint document_compare_by_tab_order_reverse(gconstpointer a, gconstpointer b)
|
|
|
|
{
|
|
|
|
GeanyDocument *doc_a = *((GeanyDocument**) a);
|
|
|
|
GeanyDocument *doc_b = *((GeanyDocument**) b);
|
|
|
|
gint notebook_position_doc_a;
|
|
|
|
gint notebook_position_doc_b;
|
|
|
|
|
|
|
|
notebook_position_doc_a = document_get_notebook_page(doc_a);
|
|
|
|
notebook_position_doc_b = document_get_notebook_page(doc_b);
|
|
|
|
|
|
|
|
if (notebook_position_doc_a < notebook_position_doc_b)
|
|
|
|
return 1;
|
|
|
|
if (notebook_position_doc_a > notebook_position_doc_b)
|
|
|
|
return -1;
|
|
|
|
/* equality */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-06-02 21:15:37 +00:00
|
|
|
void document_grab_focus(GeanyDocument *doc)
|
|
|
|
{
|
|
|
|
g_return_if_fail(doc != NULL);
|
|
|
|
|
|
|
|
gtk_widget_grab_focus(GTK_WIDGET(doc->editor->sci));
|
|
|
|
}
|