2005-11-22 12:26:26 +00:00
|
|
|
/*
|
|
|
|
* document.c - this file is part of Geany, a fast and lightweight IDE
|
|
|
|
*
|
2009-01-04 18:30:42 +00:00
|
|
|
* Copyright 2005-2009 Enrico Tröger <enrico(dot)troeger(at)uvena(dot)de>
|
|
|
|
* Copyright 2006-2009 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.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
2006-05-10 19:54:44 +00:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
2005-11-22 12:26:26 +00:00
|
|
|
*
|
2005-12-01 22:59:02 +00:00
|
|
|
* $Id$
|
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
|
|
|
*/
|
|
|
|
|
2005-12-29 20:01:18 +00:00
|
|
|
#include "geany.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-13 19:37:21 +00:00
|
|
|
#include <ctype.h>
|
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 */
|
|
|
|
/* #define USE_GIO_FILEMON 1 */
|
|
|
|
|
|
|
|
#if defined(HAVE_GIO) && defined(USE_GIO_FILEMON)
|
2009-01-18 18:19:58 +00:00
|
|
|
# include <gio/gio.h>
|
|
|
|
#endif
|
|
|
|
|
2005-11-22 12:26:26 +00:00
|
|
|
#include "document.h"
|
2008-06-02 15:31:59 +00:00
|
|
|
#include "documentprivate.h"
|
2007-08-15 15:37:21 +00:00
|
|
|
#include "filetypes.h"
|
2005-12-29 20:01:18 +00:00
|
|
|
#include "support.h"
|
|
|
|
#include "sciwrappers.h"
|
2007-05-28 16:07:30 +00:00
|
|
|
#include "editor.h"
|
2005-12-29 20:01:18 +00:00
|
|
|
#include "dialogs.h"
|
|
|
|
#include "msgwindow.h"
|
2005-11-22 12:26:26 +00:00
|
|
|
#include "templates.h"
|
|
|
|
#include "treeviews.h"
|
2006-09-05 14:24:47 +00:00
|
|
|
#include "ui_utils.h"
|
2005-12-29 20:01:18 +00:00
|
|
|
#include "utils.h"
|
2006-06-21 18:54:07 +00:00
|
|
|
#include "encodings.h"
|
2006-06-25 12:13:35 +00:00
|
|
|
#include "notebook.h"
|
2006-08-20 15:47:18 +00:00
|
|
|
#include "main.h"
|
2006-08-31 18:50:10 +00:00
|
|
|
#include "vte.h"
|
2006-10-18 19:35:42 +00:00
|
|
|
#include "build.h"
|
2006-11-08 11:42:05 +00:00
|
|
|
#include "symbols.h"
|
2007-11-05 15:09:13 +00:00
|
|
|
#include "highlighting.h"
|
2008-04-19 08:14:00 +00:00
|
|
|
#include "navqueue.h"
|
2008-03-26 18:53:59 +00:00
|
|
|
#include "win32.h"
|
2008-05-16 12:08:39 +00:00
|
|
|
#include "search.h"
|
2005-11-22 12:26:26 +00:00
|
|
|
|
|
|
|
|
2008-05-16 12:08:39 +00:00
|
|
|
GeanyFilePrefs file_prefs;
|
|
|
|
|
2008-07-24 15:17:55 +00:00
|
|
|
/** Dynamic array of GeanyDocument pointers holding information about the notebook tabs.
|
|
|
|
* Once a pointer is added to this, it is never freed. This means you can keep a pointer
|
|
|
|
* to a document over time, but it might no longer represent a notebook tab. To check this,
|
|
|
|
* check @c doc_ptr->is_valid. Of course, the pointer may represent a different
|
|
|
|
* file by then.
|
|
|
|
*
|
|
|
|
* You also need to check @c GeanyDocument::is_valid when iterating over this array.
|
|
|
|
*
|
|
|
|
* Never assume that the order of document pointers is the same as the order of notebook tabs.
|
|
|
|
* Notebook tabs can be reordered. Use @c document_get_from_page(). */
|
2008-05-29 17:00:54 +00:00
|
|
|
GPtrArray *documents_array;
|
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;
|
|
|
|
|
|
|
|
|
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);
|
2008-11-07 14:37:17 +00:00
|
|
|
static gboolean update_tags_from_buffer(GeanyDocument *doc);
|
2006-10-10 16:02:41 +00:00
|
|
|
|
2005-11-22 12:26:26 +00:00
|
|
|
|
2008-02-27 13:17:29 +00:00
|
|
|
/* ignore the case of filenames and paths under WIN32, causes errors if not */
|
2007-09-05 16:22:50 +00:00
|
|
|
#ifdef G_OS_WIN32
|
2008-11-11 19:18:51 +00:00
|
|
|
#define filenamecmp(a,b) utils_str_casecmp((a), (b))
|
2007-09-05 16:22:50 +00:00
|
|
|
#else
|
|
|
|
#define filenamecmp(a,b) strcmp((a), (b))
|
|
|
|
#endif
|
|
|
|
|
2008-06-03 17:22:04 +00:00
|
|
|
/**
|
2008-07-24 15:17:55 +00:00
|
|
|
* Find and retrieve a document with the given filename from the document list.
|
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().
|
|
|
|
*
|
2008-06-12 16:50:01 +00:00
|
|
|
* @return The matching document, or NULL.
|
2008-06-03 17:22:04 +00:00
|
|
|
* @note This is only really useful when passing a @c TMWorkObject::file_name.
|
|
|
|
* @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
|
|
|
|
2009-04-05 21:07:40 +00:00
|
|
|
if (! doc->is_valid || G_UNLIKELY(! doc->real_path))
|
|
|
|
continue;
|
2007-09-05 16:22:50 +00:00
|
|
|
|
2008-06-03 17:22:04 +00:00
|
|
|
if (filenamecmp(realname, doc->real_path) == 0)
|
|
|
|
{
|
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
|
|
|
/**
|
2008-07-24 15:17:55 +00:00
|
|
|
* Find and retrieve a document with the given filename from the document list.
|
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
|
|
|
*
|
2008-06-12 16:50:01 +00:00
|
|
|
* @return The matching document, or NULL.
|
|
|
|
* @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
|
|
|
|
2009-04-05 21:07:40 +00:00
|
|
|
if (! doc->is_valid || G_UNLIKELY(doc->file_name == NULL))
|
|
|
|
continue;
|
2007-09-05 16:22:50 +00:00
|
|
|
|
2008-06-03 17:22:04 +00:00
|
|
|
if (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
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-07-24 15:17:55 +00:00
|
|
|
/* returns the index of the notebook page for the document. */
|
2008-06-15 13:35:48 +00:00
|
|
|
gint document_get_notebook_page(GeanyDocument *doc)
|
2007-02-23 15:36:21 +00: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
|
|
|
|
2008-05-22 14:41:28 +00:00
|
|
|
return gtk_notebook_page_num(GTK_NOTEBOOK(main_widgets.notebook),
|
2008-07-14 11:13:54 +00:00
|
|
|
GTK_WIDGET(doc->editor->sci));
|
2007-02-23 15:36:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-02-17 18:00:42 +00:00
|
|
|
/**
|
2008-07-24 15:17:55 +00:00
|
|
|
* Find and retrieve 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.
|
|
|
|
*
|
2008-06-12 16:50:01 +00:00
|
|
|
* @return The corresponding document for the given notebook page, or 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
|
|
|
{
|
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
|
|
|
|
2006-05-08 01:57:25 +00:00
|
|
|
sci = (ScintillaObject*)gtk_notebook_get_nth_page(
|
2008-05-22 14:41:28 +00:00
|
|
|
GTK_NOTEBOOK(main_widgets.notebook), page_num);
|
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
|
|
|
/**
|
2008-06-12 16:50:01 +00:00
|
|
|
* Find and retrieve the current %document.
|
2008-02-17 18:00:42 +00:00
|
|
|
*
|
2008-06-12 16:50:01 +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
|
|
|
{
|
2008-05-22 14:41:28 +00: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
|
2007-05-28 15:24:21 +00:00
|
|
|
{
|
|
|
|
ScintillaObject *sci = (ScintillaObject*)
|
2008-05-22 14:41:28 +00:00
|
|
|
gtk_notebook_get_nth_page(GTK_NOTEBOOK(main_widgets.notebook), cur_page);
|
2007-05-28 15:24:21 +00:00
|
|
|
|
2008-06-15 13:35:48 +00:00
|
|
|
return document_find_by_sci(sci);
|
2007-05-28 15:24:21 +00:00
|
|
|
}
|
2005-11-22 12:26:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-10-09 16:08:53 +00:00
|
|
|
void document_init_doclist()
|
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
|
|
|
|
|
|
|
void document_finalize()
|
|
|
|
{
|
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
|
|
|
|
* truncated to a maximum of @c length characters in case the filename is very long.
|
|
|
|
*
|
|
|
|
* @param doc The document to use.
|
|
|
|
* @param length The length of the resulting string or -1 to use a default value.
|
|
|
|
*
|
|
|
|
* @return The ellipsized last part of the filename of @c doc, should be freed when no
|
|
|
|
* 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));
|
|
|
|
short_name = utils_str_middle_truncate(base_name, length);
|
|
|
|
|
|
|
|
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
|
|
|
|
2008-11-18 20:14:42 +00:00
|
|
|
ui_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
|
|
|
/**
|
|
|
|
* Update the tab labels, the status bar, the window title and some save-sensitive buttons
|
|
|
|
* 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-18 14:18:26 +00:00
|
|
|
/* Sets is_valid to FALSE and initializes some members to NULL, to mark it uninitialized.
|
|
|
|
* The flag is_valid is set to TRUE in document_create(). */
|
2008-05-16 14:17:54 +00:00
|
|
|
static void init_doc_struct(GeanyDocument *new_doc)
|
2006-10-09 16:08:53 +00:00
|
|
|
{
|
2008-09-26 17:28:50 +00:00
|
|
|
GeanyDocumentPrivate *priv;
|
2008-06-02 15:31:59 +00:00
|
|
|
|
2008-09-26 17:28:50 +00:00
|
|
|
memset(new_doc, 0, sizeof(GeanyDocument));
|
2008-05-29 17:00:54 +00:00
|
|
|
|
2008-06-18 14:18:26 +00:00
|
|
|
new_doc->is_valid = FALSE;
|
2006-10-09 16:08:53 +00:00
|
|
|
new_doc->has_tags = FALSE;
|
|
|
|
new_doc->readonly = FALSE;
|
|
|
|
new_doc->file_name = NULL;
|
|
|
|
new_doc->file_type = NULL;
|
|
|
|
new_doc->tm_file = NULL;
|
|
|
|
new_doc->encoding = NULL;
|
|
|
|
new_doc->has_bom = FALSE;
|
2008-07-11 14:11:53 +00:00
|
|
|
new_doc->editor = NULL;
|
2008-05-14 16:37:09 +00:00
|
|
|
new_doc->changed = FALSE;
|
2008-06-03 17:22:04 +00:00
|
|
|
new_doc->real_path = NULL;
|
2008-06-02 15:31:59 +00:00
|
|
|
|
2008-09-26 17:28:50 +00:00
|
|
|
new_doc->priv = g_new0(GeanyDocumentPrivate, 1);
|
|
|
|
priv = new_doc->priv;
|
|
|
|
priv->tag_store = NULL;
|
|
|
|
priv->tag_tree = NULL;
|
|
|
|
priv->saved_encoding.encoding = NULL;
|
|
|
|
priv->saved_encoding.has_bom = FALSE;
|
|
|
|
priv->undo_actions = NULL;
|
|
|
|
priv->redo_actions = NULL;
|
2008-09-29 12:44:31 +00:00
|
|
|
priv->line_count = 0;
|
2009-01-28 16:18:22 +00:00
|
|
|
priv->last_check = time(NULL);
|
2006-10-09 16:08:53 +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
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-01-24 19:38:53 +00:00
|
|
|
#if defined(HAVE_GIO) && defined(USE_GIO_FILEMON)
|
2009-01-21 15:50:48 +00:00
|
|
|
/* Set the file status to 'changed' after a single 'created' event. */
|
|
|
|
static gboolean monitor_finish_pending_create(gpointer doc)
|
|
|
|
{
|
2009-04-15 22:47:33 +00:00
|
|
|
g_return_val_if_fail(doc != NULL, FALSE);
|
2009-01-21 15:50:48 +00:00
|
|
|
|
|
|
|
((GeanyDocument *)doc)->priv->file_disk_status = FILE_CHANGED;
|
|
|
|
ui_update_tab_status(doc);
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Resets the 'ignore' file status after a reload action. */
|
2009-04-24 19:29:20 +00:00
|
|
|
static gboolean monitor_reset_ignore(gpointer data)
|
2009-01-21 15:50:48 +00:00
|
|
|
{
|
2009-04-24 19:29:20 +00:00
|
|
|
GeanyDocument *doc = data;
|
|
|
|
/* FIXME even if the doc is valid, it might be the wrong document if the original document was
|
|
|
|
* closed a new one opened which got this doc pointer while the timeout was active. Either
|
|
|
|
* remove the timeout when closing or document or remove the timeout completely. */
|
|
|
|
if (DOC_VALID(doc))
|
|
|
|
{
|
|
|
|
doc->priv->file_disk_status = FILE_OK;
|
|
|
|
ui_update_tab_status(doc);
|
|
|
|
}
|
2009-01-21 15:50:48 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void monitor_file_changed_cb(G_GNUC_UNUSED GFileMonitor *monitor, G_GNUC_UNUSED GFile *file,
|
2009-01-18 18:19:58 +00:00
|
|
|
G_GNUC_UNUSED GFile *other_file, GFileMonitorEvent event, GeanyDocument *doc)
|
|
|
|
{
|
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;
|
|
|
|
|
|
|
|
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
|
|
|
{
|
2009-01-21 15:50:48 +00:00
|
|
|
if (doc->priv->file_disk_status == FILE_CREATED_PENDING)
|
|
|
|
{
|
|
|
|
g_source_remove(doc->priv->last_check);
|
|
|
|
doc->priv->file_disk_status = FILE_IGNORE;
|
|
|
|
}
|
2009-01-18 18:19:58 +00:00
|
|
|
if (doc->priv->file_disk_status == FILE_IGNORE)
|
|
|
|
{ /* ignore this change completely, used after saving a file */
|
2009-01-21 15:50:48 +00:00
|
|
|
FileDiskStatus old_status = doc->priv->file_disk_status;
|
2009-01-18 18:19:58 +00:00
|
|
|
doc->priv->file_disk_status = FILE_OK;
|
2009-01-21 15:50:48 +00:00
|
|
|
if (old_status != FILE_OK)
|
|
|
|
ui_update_tab_status(doc);
|
2009-01-18 18:19:58 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
doc->priv->file_disk_status = FILE_CHANGED;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case G_FILE_MONITOR_EVENT_DELETED:
|
|
|
|
{
|
2009-01-21 15:50:48 +00:00
|
|
|
if (doc->priv->file_disk_status != FILE_IGNORE)
|
|
|
|
doc->priv->file_disk_status = FILE_MISSING;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case G_FILE_MONITOR_EVENT_CREATED:
|
|
|
|
{
|
|
|
|
if (doc->priv->file_disk_status == FILE_MISSING)
|
|
|
|
{ /* When the last state was 'missing', we are most probably in a
|
|
|
|
* delete-create-change set, so set the new state to 'ok' and do the real handling
|
|
|
|
* in the upcoming G_FILE_MONITOR_EVENT_CHANGES_DONE_HINT event. */
|
|
|
|
doc->priv->file_disk_status = FILE_OK;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{ /* In case we have a single 'created' event and not within a set of other events,
|
|
|
|
* we defer the processing into a timeout function which is removed when a
|
|
|
|
* G_FILE_MONITOR_EVENT_CHANGES_DONE_HINT event arrives before. */
|
|
|
|
doc->priv->file_disk_status = FILE_CREATED_PENDING;
|
|
|
|
/* re-use otherwise unused 'last_check' field to store the timeout ID, we need it
|
|
|
|
* to remove the timeout and we need to store it per-document */
|
2009-01-24 19:38:31 +00:00
|
|
|
doc->priv->last_check = g_timeout_add_seconds(1, monitor_finish_pending_create, doc);
|
2009-01-21 15:50:48 +00:00
|
|
|
}
|
|
|
|
if (doc->real_path == NULL)
|
|
|
|
{
|
|
|
|
gchar *locale_filename = g_file_get_path(file);
|
|
|
|
doc->real_path = tm_get_real_path(locale_filename);
|
|
|
|
g_free(locale_filename);
|
|
|
|
}
|
2009-01-18 18:19:58 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* ignore */
|
2009-01-21 15:50:48 +00:00
|
|
|
case G_FILE_MONITOR_EVENT_CHANGED:
|
2009-01-18 18:19:58 +00:00
|
|
|
case G_FILE_MONITOR_EVENT_ATTRIBUTE_CHANGED:
|
|
|
|
case G_FILE_MONITOR_EVENT_PRE_UNMOUNT:
|
|
|
|
case G_FILE_MONITOR_EVENT_UNMOUNTED:
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (doc->priv->file_disk_status != FILE_OK)
|
|
|
|
{
|
|
|
|
ui_update_tab_status(doc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
void document_stop_file_monitoring(GeanyDocument *doc)
|
|
|
|
{
|
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)
|
|
|
|
{
|
2009-01-24 19:38:53 +00:00
|
|
|
#if defined(HAVE_GIO) && defined(USE_GIO_FILEMON)
|
2009-01-18 18:19:58 +00:00
|
|
|
gchar *locale_filename;
|
|
|
|
GFile *file;
|
|
|
|
|
|
|
|
/* 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 */
|
|
|
|
file = g_file_new_for_path(locale_filename);
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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
|
|
|
{
|
2008-05-16 14:17:54 +00:00
|
|
|
GeanyDocument *this;
|
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)
|
|
|
|
{
|
2008-06-15 13:35:48 +00:00
|
|
|
GeanyDocument *doc = document_get_current();
|
2008-03-27 13:59:51 +00:00
|
|
|
/* remove the empty document and open a new one */
|
2008-06-16 18:31:59 +00:00
|
|
|
if (doc != NULL && doc->file_name == NULL && ! doc->changed)
|
2008-06-15 13:35:48 +00:00
|
|
|
document_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 */
|
|
|
|
{
|
2008-09-26 17:28:50 +00:00
|
|
|
GeanyDocument *new_doc = g_new0(GeanyDocument, 1);
|
2008-05-29 17:00:54 +00:00
|
|
|
|
|
|
|
new_idx = documents_array->len;
|
|
|
|
g_ptr_array_add(documents_array, new_doc);
|
2008-03-27 13:59:51 +00:00
|
|
|
}
|
2008-05-29 17:00:54 +00:00
|
|
|
this = documents[new_idx];
|
2008-05-14 16:37:09 +00:00
|
|
|
init_doc_struct(this); /* initialize default document settings */
|
2008-06-18 14:18:26 +00:00
|
|
|
this->index = new_idx;
|
2008-05-14 16:37:09 +00:00
|
|
|
|
2008-06-05 12:14:51 +00:00
|
|
|
this->file_name = g_strdup(utf8_filename);
|
2008-03-27 13:59:51 +00:00
|
|
|
|
2008-07-11 14:11:53 +00:00
|
|
|
this->editor = editor_create(this);
|
2008-03-27 13:59:51 +00:00
|
|
|
|
2008-09-21 16:43:45 +00:00
|
|
|
editor_apply_update_prefs(this->editor);
|
2008-03-27 13:59:51 +00:00
|
|
|
|
2008-06-15 13:35:48 +00:00
|
|
|
treeviews_openfiles_add(this); /* sets this->iter */
|
2006-12-05 10:37:36 +00:00
|
|
|
|
2008-06-16 18:31:59 +00:00
|
|
|
notebook_new_tab(this);
|
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));
|
2008-09-26 17:28:50 +00:00
|
|
|
gtk_tree_selection_select_iter(sel, &this->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
|
|
|
|
2008-07-15 18:51:19 +00:00
|
|
|
gtk_widget_grab_focus(GTK_WIDGET(this->editor->sci));
|
2008-07-15 18:46:17 +00:00
|
|
|
|
2008-06-18 14:18:26 +00:00
|
|
|
this->is_valid = TRUE; /* do this last to prevent UI updating with NULL items. */
|
2008-06-15 13:35:48 +00:00
|
|
|
return this;
|
2005-11-22 12:26:26 +00:00
|
|
|
}
|
|
|
|
|
2008-07-01 14:20:16 +00:00
|
|
|
/**
|
|
|
|
* Close the given document.
|
|
|
|
*
|
|
|
|
* @param doc The document to remove.
|
|
|
|
*
|
|
|
|
* @return @a TRUE if the document was actually removed or @a 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));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-02-17 18:00:42 +00:00
|
|
|
/**
|
|
|
|
* Remove the given notebook tab at @a page_num and clear all related information
|
|
|
|
* in the document list.
|
|
|
|
*
|
|
|
|
* @param page_num The notebook page number to remove.
|
|
|
|
*
|
|
|
|
* @return @a TRUE if the document was actually removed or @a FALSE otherwise.
|
|
|
|
**/
|
2008-06-12 16:50:01 +00:00
|
|
|
gboolean document_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
|
|
|
|
2009-04-05 21:07:40 +00:00
|
|
|
if (G_UNLIKELY(doc == NULL))
|
2008-09-16 15:47:26 +00:00
|
|
|
{
|
2009-01-21 22:49:21 +00:00
|
|
|
g_warning("%s: page_num: %d", G_STRFUNC, page_num);
|
2008-09-16 15:47:26 +00:00
|
|
|
return 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))
|
2005-11-22 12:26:26 +00:00
|
|
|
{
|
2008-09-16 15:47:26 +00:00
|
|
|
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)
|
|
|
|
ui_add_recent_file(doc->file_name);
|
|
|
|
|
2009-06-05 15:57:01 +00:00
|
|
|
doc->is_valid = FALSE;
|
|
|
|
|
2008-09-16 15:47:26 +00:00
|
|
|
notebook_remove_page(page_num);
|
|
|
|
treeviews_remove_document(doc);
|
|
|
|
navqueue_remove_file(doc->file_name);
|
|
|
|
msgwin_status_add(_("File %s closed."), DOC_FILENAME(doc));
|
|
|
|
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);
|
|
|
|
|
|
|
|
editor_destroy(doc->editor);
|
|
|
|
doc->editor = NULL;
|
|
|
|
|
2009-01-18 18:19:58 +00:00
|
|
|
document_stop_file_monitoring(doc);
|
|
|
|
|
2008-09-16 15:47:26 +00:00
|
|
|
doc->file_name = NULL;
|
|
|
|
doc->real_path = NULL;
|
|
|
|
doc->file_type = NULL;
|
|
|
|
doc->encoding = NULL;
|
|
|
|
doc->has_bom = FALSE;
|
|
|
|
doc->tm_file = NULL;
|
|
|
|
document_undo_clear(doc);
|
2008-09-26 17:28:50 +00:00
|
|
|
g_free(doc->priv);
|
|
|
|
|
2008-09-16 15:47:26 +00:00
|
|
|
if (gtk_notebook_get_n_pages(GTK_NOTEBOOK(main_widgets.notebook)) == 0)
|
|
|
|
{
|
|
|
|
treeviews_update_tag_list(NULL, FALSE);
|
|
|
|
/*on_notebook1_switch_page(GTK_NOTEBOOK(main_widgets.notebook), NULL, 0, NULL);*/
|
|
|
|
ui_set_window_title(NULL);
|
|
|
|
ui_save_buttons_toggle(FALSE);
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
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.
|
2008-02-17 18:00:42 +00:00
|
|
|
* After all, the "document-new" signal is emitted for plugins.
|
|
|
|
*
|
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
|
|
|
**/
|
2008-07-24 12:12:47 +00:00
|
|
|
GeanyDocument *document_new_file(const gchar *utf8_filename, GeanyFiletype *ft,
|
|
|
|
const gchar *text)
|
2005-11-22 12:26:26 +00:00
|
|
|
{
|
2008-07-24 12:12:47 +00:00
|
|
|
GeanyDocument *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)
|
2008-07-14 11:13:54 +00:00
|
|
|
sci_set_text(doc->editor->sci, text);
|
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
|
|
|
/* convert the eol chars in the template text in case they are different from
|
2008-05-16 12:08:39 +00:00
|
|
|
* from file_prefs.default_eol */
|
2008-05-14 15:36:27 +00:00
|
|
|
if (text != NULL)
|
2008-07-14 11:13:54 +00:00
|
|
|
sci_convert_eols(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
|
|
|
|
2008-06-15 13:35:48 +00:00
|
|
|
document_set_filetype(doc, ft); /* also clears taglist */
|
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_update_tag_list(doc, FALSE);
|
|
|
|
document_set_text_changed(doc, FALSE);
|
|
|
|
ui_document_show_hide(doc); /* update the document menu */
|
2006-10-10 16:02:41 +00:00
|
|
|
|
2008-07-14 11:13:54 +00:00
|
|
|
sci_set_line_numbers(doc->editor->sci, editor_prefs.show_linenumber_margin, 0);
|
|
|
|
sci_goto_pos(doc->editor->sci, 0, TRUE);
|
2005-11-22 12:26:26 +00:00
|
|
|
|
2009-01-28 16:18:22 +00:00
|
|
|
#if defined(HAVE_GIO) && defined(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
|
|
|
/**
|
|
|
|
* Open a %document specified by @a locale_filename.
|
|
|
|
* After all, the "document-open" signal is emitted for plugins.
|
|
|
|
*
|
|
|
|
* @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.
|
|
|
|
* @param forced_enc The file encoding to use or @c NULL to auto-detect the file encoding.
|
|
|
|
*
|
2008-06-12 16:50:01 +00:00
|
|
|
* @return The document opened or 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 size; /* actual file size on disk */
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
2008-02-27 13:17:29 +00:00
|
|
|
/* reload file with specified encoding */
|
2006-09-08 15:05:32 +00:00
|
|
|
static gboolean
|
2007-01-06 12:35:43 +00:00
|
|
|
handle_forced_encoding(FileData *filedata, const gchar *forced_enc)
|
2006-09-08 15:05:32 +00:00
|
|
|
{
|
2007-01-07 16:22:41 +00:00
|
|
|
GeanyEncodingIndex enc_idx;
|
|
|
|
|
2006-12-07 16:09:45 +00:00
|
|
|
if (utils_str_equal(forced_enc, "UTF-8"))
|
2006-09-08 15:05:32 +00:00
|
|
|
{
|
2007-01-06 12:35:43 +00:00
|
|
|
if (! g_utf8_validate(filedata->data, filedata->len, NULL))
|
2006-09-08 15:05:32 +00:00
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2006-10-25 14:48:07 +00:00
|
|
|
gchar *converted_text = encodings_convert_to_utf8_from_charset(
|
2008-10-26 14:16:09 +00:00
|
|
|
filedata->data, filedata->size, forced_enc, FALSE);
|
2009-04-15 22:47:33 +00:00
|
|
|
if (converted_text == NULL)
|
2006-09-08 15:05:32 +00:00
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2007-01-06 12:35:43 +00:00
|
|
|
g_free(filedata->data);
|
|
|
|
filedata->data = converted_text;
|
|
|
|
filedata->len = strlen(converted_text);
|
2006-09-08 15:05:32 +00:00
|
|
|
}
|
|
|
|
}
|
2007-05-24 10:24:26 +00:00
|
|
|
enc_idx = encodings_scan_unicode_bom(filedata->data, filedata->size, NULL);
|
2007-01-07 16:22:41 +00:00
|
|
|
filedata->bom = (enc_idx == GEANY_ENCODING_UTF_8);
|
|
|
|
filedata->enc = g_strdup(forced_enc);
|
2006-09-08 15:05:32 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-02-27 13:17:29 +00:00
|
|
|
/* detect encoding and convert to UTF-8 if necessary */
|
2006-09-08 15:05:32 +00:00
|
|
|
static gboolean
|
2008-10-26 14:16:09 +00:00
|
|
|
handle_encoding(FileData *filedata, GeanyEncodingIndex enc_idx)
|
2006-09-08 15:05:32 +00:00
|
|
|
{
|
2009-04-15 22:47:33 +00:00
|
|
|
g_return_val_if_fail(filedata->enc == NULL, FALSE);
|
|
|
|
g_return_val_if_fail(filedata->bom == FALSE, FALSE);
|
2007-01-07 16:22:41 +00:00
|
|
|
|
2007-05-24 10:24:26 +00:00
|
|
|
if (filedata->size == 0)
|
2007-01-07 16:22:41 +00:00
|
|
|
{
|
2008-02-27 13:17:29 +00:00
|
|
|
/* we have no data so assume UTF-8, filedata->len can be 0 even we have an empty
|
|
|
|
* e.g. UTF32 file with a BOM(so size is 4, len is 0) */
|
2007-01-07 16:22:41 +00:00
|
|
|
filedata->enc = g_strdup("UTF-8");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-02-27 13:17:29 +00:00
|
|
|
/* first check for a BOM */
|
2007-01-07 16:22:41 +00:00
|
|
|
if (enc_idx != GEANY_ENCODING_NONE)
|
2006-09-08 15:05:32 +00:00
|
|
|
{
|
2007-01-07 16:22:41 +00:00
|
|
|
filedata->enc = g_strdup(encodings[enc_idx].charset);
|
2007-01-06 12:35:43 +00:00
|
|
|
filedata->bom = TRUE;
|
2007-01-07 16:22:41 +00:00
|
|
|
|
2008-02-27 13:17:29 +00:00
|
|
|
if (enc_idx != GEANY_ENCODING_UTF_8) /* the BOM indicated something else than UTF-8 */
|
2006-09-08 15:05:32 +00:00
|
|
|
{
|
2006-10-25 14:48:07 +00:00
|
|
|
gchar *converted_text = encodings_convert_to_utf8_from_charset(
|
2007-05-24 10:24:26 +00:00
|
|
|
filedata->data, filedata->size, filedata->enc, FALSE);
|
2009-04-15 22:47:33 +00:00
|
|
|
if (converted_text != NULL)
|
2006-09-08 15:05:32 +00:00
|
|
|
{
|
2007-01-06 12:35:43 +00:00
|
|
|
g_free(filedata->data);
|
|
|
|
filedata->data = converted_text;
|
|
|
|
filedata->len = strlen(converted_text);
|
2006-09-08 15:05:32 +00:00
|
|
|
}
|
2007-01-07 16:22:41 +00:00
|
|
|
else
|
|
|
|
{
|
2008-02-27 13:17:29 +00:00
|
|
|
/* there was a problem converting data from BOM encoding type */
|
2007-01-07 16:22:41 +00:00
|
|
|
g_free(filedata->enc);
|
|
|
|
filedata->enc = NULL;
|
|
|
|
filedata->bom = FALSE;
|
|
|
|
}
|
2006-09-08 15:05:32 +00:00
|
|
|
}
|
|
|
|
}
|
2007-01-07 16:22:41 +00:00
|
|
|
|
2008-02-27 13:17:29 +00:00
|
|
|
if (filedata->enc == NULL) /* either there was no BOM or the BOM encoding failed */
|
2006-09-08 15:05:32 +00:00
|
|
|
{
|
2008-02-27 13:17:29 +00:00
|
|
|
/* try UTF-8 first */
|
2008-10-26 14:16:09 +00:00
|
|
|
if ((filedata->size == filedata->len) &&
|
|
|
|
g_utf8_validate(filedata->data, filedata->len, NULL))
|
2006-09-08 15:05:32 +00:00
|
|
|
{
|
2007-01-06 12:35:43 +00:00
|
|
|
filedata->enc = g_strdup("UTF-8");
|
2006-09-08 15:05:32 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-02-27 13:17:29 +00:00
|
|
|
/* detect the encoding */
|
2007-01-06 12:35:43 +00:00
|
|
|
gchar *converted_text = encodings_convert_to_utf8(filedata->data,
|
2007-05-24 10:24:26 +00:00
|
|
|
filedata->size, &filedata->enc);
|
2006-09-08 15:05:32 +00:00
|
|
|
|
2009-04-15 22:47:33 +00:00
|
|
|
if (converted_text == NULL)
|
2006-09-08 15:05:32 +00:00
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
2007-01-07 16:22:41 +00:00
|
|
|
g_free(filedata->data);
|
|
|
|
filedata->data = converted_text;
|
|
|
|
filedata->len = strlen(converted_text);
|
2006-09-08 15:05:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
2007-01-06 12:35:43 +00:00
|
|
|
handle_bom(FileData *filedata)
|
2006-09-08 15:05:32 +00:00
|
|
|
{
|
2007-01-07 16:22:41 +00:00
|
|
|
guint bom_len;
|
2007-01-06 12:35:43 +00:00
|
|
|
|
2007-05-24 10:24:26 +00:00
|
|
|
encodings_scan_unicode_bom(filedata->data, filedata->size, &bom_len);
|
2007-01-07 16:22:41 +00:00
|
|
|
g_return_if_fail(bom_len != 0);
|
2007-01-06 12:35:43 +00:00
|
|
|
|
2008-02-27 13:17:29 +00:00
|
|
|
/* use filedata->len here because the contents are already converted into UTF-8 */
|
2007-01-07 16:22:41 +00:00
|
|
|
filedata->len -= bom_len;
|
2008-02-27 13:17:29 +00:00
|
|
|
/* overwrite the BOM with the remainder of the file contents, plus the NULL terminator. */
|
2007-01-07 16:22:41 +00:00
|
|
|
g_memmove(filedata->data, filedata->data + bom_len, filedata->len + 1);
|
2007-01-07 18:48:56 +00:00
|
|
|
filedata->data = g_realloc(filedata->data, filedata->len + 1);
|
2007-01-06 12:35:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* 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;
|
2007-05-24 10:24:26 +00:00
|
|
|
GeanyEncodingIndex tmp_enc_idx;
|
2007-01-06 12:35:43 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2008-02-27 13:17:29 +00:00
|
|
|
/* use strlen to check for null chars */
|
2007-05-24 10:24:26 +00:00
|
|
|
filedata->size = (gsize) st.st_size;
|
2007-01-06 12:35:43 +00:00
|
|
|
filedata->len = strlen(filedata->data);
|
|
|
|
|
2008-02-27 13:17:29 +00:00
|
|
|
/* temporarily retrieve the encoding idx based on the BOM to suppress the following warning
|
|
|
|
* if we have a BOM */
|
2007-05-24 10:24:26 +00:00
|
|
|
tmp_enc_idx = encodings_scan_unicode_bom(filedata->data, filedata->size, NULL);
|
|
|
|
|
2008-10-26 14:16:09 +00:00
|
|
|
/* check whether the size of the loaded data is equal to the size of the file in the
|
|
|
|
* filesystem file size may be 0 to allow opening files in /proc/ which have typically a
|
|
|
|
* file size of 0 bytes */
|
2008-03-05 17:34:00 +00:00
|
|
|
if (filedata->len != filedata->size && filedata->size != 0 && (
|
2008-02-27 13:17:29 +00:00
|
|
|
tmp_enc_idx == GEANY_ENCODING_UTF_8 || /* tmp_enc_idx can be UTF-7/8/16/32, UCS and None */
|
2008-10-26 14:16:09 +00:00
|
|
|
tmp_enc_idx == GEANY_ENCODING_UTF_7)) /* filter UTF-7/8 where no NULL bytes are allowed */
|
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
|
|
|
|
2008-02-27 13:17:29 +00:00
|
|
|
/* set the file to read-only mode because saving it is probably dangerous */
|
2007-01-06 12:35:43 +00:00
|
|
|
filedata->readonly = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Determine character encoding and convert to UTF-8 */
|
|
|
|
if (forced_enc != NULL)
|
|
|
|
{
|
2008-02-27 13:17:29 +00:00
|
|
|
/* the encoding should be ignored(requested by user), so open the file "as it is" */
|
2007-01-06 12:35:43 +00:00
|
|
|
if (utils_str_equal(forced_enc, encodings[GEANY_ENCODING_NONE].charset))
|
|
|
|
{
|
|
|
|
filedata->bom = FALSE;
|
|
|
|
filedata->enc = g_strdup(encodings[GEANY_ENCODING_NONE].charset);
|
|
|
|
}
|
|
|
|
else if (! handle_forced_encoding(filedata, forced_enc))
|
|
|
|
{
|
2008-12-04 15:56:54 +00:00
|
|
|
/* For translators: the second wildcard is an encoding name, e.g.
|
|
|
|
* The file \"test.txt\" is not valid UTF-8. */
|
2008-10-26 14:16:09 +00:00
|
|
|
ui_set_statusbar(TRUE, _("The file \"%s\" is not valid %s."),
|
2009-04-21 20:52:51 +00:00
|
|
|
display_filename, forced_enc);
|
2007-01-06 12:35:43 +00:00
|
|
|
utils_beep();
|
|
|
|
g_free(filedata->data);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
2008-10-26 14:16:09 +00:00
|
|
|
else if (! handle_encoding(filedata, tmp_enc_idx))
|
2007-01-06 12:35:43 +00:00
|
|
|
{
|
2007-10-24 10:52:48 +00:00
|
|
|
ui_set_statusbar(TRUE,
|
2008-10-26 14:16:09 +00:00
|
|
|
_("The file \"%s\" does not look like a text file or the file encoding is not supported."),
|
2009-04-21 20:52:51 +00:00
|
|
|
display_filename);
|
2007-01-06 12:35:43 +00:00
|
|
|
utils_beep();
|
|
|
|
g_free(filedata->data);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (filedata->bom)
|
|
|
|
handle_bom(filedata);
|
|
|
|
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
|
|
|
|
* if cl_options.goto_column is set. */
|
2008-07-08 16:30:37 +00:00
|
|
|
static void 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 */
|
2008-07-14 11:13:54 +00:00
|
|
|
gint cur_pos = sci_get_current_position(editor->sci);
|
|
|
|
sci_set_current_position(editor->sci, cur_pos + cl_options.goto_column, 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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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;
|
2008-08-18 15:58:36 +00:00
|
|
|
gchar *soft_tab = g_strnfill(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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-08-19 14:55:56 +00:00
|
|
|
/* Detect the indent type based on counting the leading indent characters for each line. */
|
2008-08-04 14:07:49 +00:00
|
|
|
static GeanyIndentType detect_indent_type(GeanyEditor *editor)
|
2007-10-17 12:27:07 +00:00
|
|
|
{
|
2008-08-04 14:07:49 +00:00
|
|
|
const GeanyIndentPrefs *iprefs = editor_get_indent_prefs(editor);
|
|
|
|
ScintillaObject *sci = editor->sci;
|
2009-02-10 21:11:25 +00:00
|
|
|
guint 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))
|
2008-08-04 14:07:49 +00:00
|
|
|
return GEANY_INDENT_TYPE_BOTH;
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
c = sci_get_char_at(sci, pos);
|
|
|
|
if (c == '\t')
|
|
|
|
tabs++;
|
|
|
|
else
|
|
|
|
if (c == ' ')
|
|
|
|
{
|
2008-08-19 14:55:56 +00:00
|
|
|
/* check for at least 2 spaces */
|
2007-10-17 12:27:07 +00:00
|
|
|
if (sci_get_char_at(sci, pos + 1) == ' ')
|
|
|
|
spaces++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (spaces == 0 && tabs == 0)
|
2008-08-04 14:07:49 +00:00
|
|
|
return iprefs->type;
|
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)
|
|
|
|
return GEANY_INDENT_TYPE_SPACES;
|
|
|
|
else if (tabs > spaces * 4)
|
|
|
|
return GEANY_INDENT_TYPE_TABS;
|
2007-10-17 12:27:07 +00:00
|
|
|
else
|
2008-08-19 14:55:56 +00:00
|
|
|
return GEANY_INDENT_TYPE_BOTH;
|
2007-10-17 12:27:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-08-04 14:07:49 +00:00
|
|
|
static void set_indentation(GeanyEditor *editor)
|
2008-05-18 16:03:20 +00:00
|
|
|
{
|
2008-08-04 14:07:49 +00:00
|
|
|
const GeanyIndentPrefs *iprefs = editor_get_indent_prefs(editor);
|
2008-05-18 16:03:20 +00:00
|
|
|
|
2008-08-04 14:07:49 +00:00
|
|
|
switch (FILETYPE_ID(editor->document->file_type))
|
|
|
|
{
|
|
|
|
case GEANY_FILETYPES_MAKE:
|
|
|
|
/* force using tabs for indentation for Makefiles */
|
|
|
|
editor_set_indent_type(editor, GEANY_INDENT_TYPE_TABS);
|
2008-08-18 15:58:36 +00:00
|
|
|
return;
|
2008-08-04 14:07:49 +00:00
|
|
|
case GEANY_FILETYPES_F77:
|
|
|
|
/* force using spaces for indentation for Fortran 77 */
|
|
|
|
editor_set_indent_type(editor, GEANY_INDENT_TYPE_SPACES);
|
2008-08-18 15:58:36 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (iprefs->detect_type)
|
|
|
|
{
|
|
|
|
GeanyIndentType type = detect_indent_type(editor);
|
|
|
|
|
|
|
|
if (type != iprefs->type)
|
|
|
|
{
|
|
|
|
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,
|
|
|
|
DOC_FILENAME(editor->document));
|
|
|
|
}
|
|
|
|
editor_set_indent_type(editor, type);
|
2008-05-18 16:03:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-11-07 14:37:17 +00:00
|
|
|
#if 0
|
|
|
|
static gboolean auto_update_tag_list(gpointer data)
|
|
|
|
{
|
|
|
|
GeanyDocument *doc = data;
|
|
|
|
|
2009-04-15 22:47:33 +00:00
|
|
|
if (! doc || ! doc->is_valid || doc->tm_file == NULL)
|
2008-11-07 14:37:17 +00:00
|
|
|
return FALSE;
|
2008-11-11 19:18:51 +00:00
|
|
|
|
2008-11-07 14:37:17 +00:00
|
|
|
if (gtk_window_get_focus(GTK_WINDOW(main_widgets.window)) != GTK_WIDGET(doc->editor->sci))
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
if (update_tags_from_buffer(doc))
|
|
|
|
treeviews_update_tag_list(doc, TRUE);
|
2008-11-11 19:18:51 +00:00
|
|
|
|
2008-11-07 14:37:17 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
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
|
|
|
|
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 */
|
2009-04-15 22:47:33 +00:00
|
|
|
if (filename == NULL)
|
2006-04-27 18:06:35 +00:00
|
|
|
{
|
2007-10-24 10:52:48 +00:00
|
|
|
ui_set_statusbar(FALSE, _("Invalid filename"));
|
2008-06-15 13:35:48 +00:00
|
|
|
return NULL;
|
2006-04-27 18:06:35 +00:00
|
|
|
}
|
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
|
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
|
|
|
{
|
2008-02-27 13:17:29 +00:00
|
|
|
ui_add_recent_file(utf8_filename); /* either add or reorder recent item */
|
2008-05-22 14:41:28 +00:00
|
|
|
gtk_notebook_set_current_page(GTK_NOTEBOOK(main_widgets.notebook),
|
|
|
|
gtk_notebook_page_num(GTK_NOTEBOOK(main_widgets.notebook),
|
2008-07-14 11:13:54 +00:00
|
|
|
(GtkWidget*) doc->editor->sci));
|
2006-02-06 06:26:53 +00:00
|
|
|
g_free(utf8_filename);
|
|
|
|
g_free(locale_filename);
|
2008-06-15 13:35:48 +00:00
|
|
|
document_check_disk_status(doc, TRUE); /* force a file changed check */
|
2008-07-08 16:30:37 +00:00
|
|
|
set_cursor_position(doc->editor, pos);
|
2008-06-15 13:35:48 +00:00
|
|
|
return doc;
|
2005-11-22 12:26:26 +00:00
|
|
|
}
|
|
|
|
}
|
2009-04-21 20:52:51 +00:00
|
|
|
display_filename = utils_str_middle_truncate(utf8_filename, 100);
|
2005-11-22 12:26:26 +00:00
|
|
|
|
2008-02-27 13:17:29 +00:00
|
|
|
/* if default encoding for opening files is set, use it if no forced encoding is set */
|
2008-05-16 12:08:39 +00:00
|
|
|
if (file_prefs.default_open_encoding >= 0 && forced_enc == NULL)
|
|
|
|
forced_enc = encodings[file_prefs.default_open_encoding].charset;
|
2007-06-26 15:41:27 +00:00
|
|
|
|
2009-04-21 20:52:51 +00:00
|
|
|
if (! load_text_file(locale_filename, display_filename, &filedata, forced_enc))
|
2006-07-22 01:29:10 +00:00
|
|
|
{
|
2009-04-21 20:52:51 +00:00
|
|
|
g_free(display_filename);
|
2006-09-08 15:05:32 +00:00
|
|
|
g_free(utf8_filename);
|
|
|
|
g_free(locale_filename);
|
2008-06-15 13:35:48 +00:00
|
|
|
return NULL;
|
2006-07-22 01:29:10 +00:00
|
|
|
}
|
|
|
|
|
2009-01-09 18:22:07 +00:00
|
|
|
if (! reload)
|
|
|
|
{
|
|
|
|
doc = document_create(utf8_filename);
|
2009-04-15 22:47:33 +00:00
|
|
|
g_return_val_if_fail(doc != NULL, NULL); /* really should not happen */
|
2009-01-09 18:22:07 +00:00
|
|
|
|
2009-01-21 15:50:48 +00:00
|
|
|
/* file exists on disk, set real_path */
|
2009-04-05 21:07:40 +00:00
|
|
|
setptr(doc->real_path, tm_get_real_path(locale_filename));
|
2009-01-21 15:50:48 +00:00
|
|
|
|
2009-01-09 18:22:07 +00:00
|
|
|
doc->priv->is_remote = utils_is_remote_path(locale_filename);
|
2009-01-21 15:50:48 +00:00
|
|
|
monitor_file_setup(doc);
|
2009-01-09 18:22:07 +00:00
|
|
|
}
|
2005-11-22 12:26:26 +00:00
|
|
|
|
2008-07-14 11:13:54 +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
|
|
|
|
2008-02-27 13:17:29 +00:00
|
|
|
/* add the text to the ScintillaObject */
|
2008-07-14 11:13:54 +00:00
|
|
|
sci_set_readonly(doc->editor->sci, FALSE); /* to allow replacing text */
|
|
|
|
sci_set_text(doc->editor->sci, filedata.data); /* NULL terminated data */
|
2008-07-03 17:21:29 +00:00
|
|
|
queue_colourise(doc); /* Ensure the document gets colourised. */
|
2007-01-06 12:35:43 +00:00
|
|
|
|
2008-02-27 13:17:29 +00:00
|
|
|
/* detect & set line endings */
|
2007-01-06 12:35:43 +00:00
|
|
|
editor_mode = utils_get_line_endings(filedata.data, filedata.len);
|
2008-07-14 11:13:54 +00:00
|
|
|
sci_set_eol_mode(doc->editor->sci, editor_mode);
|
2007-01-06 12:35:43 +00:00
|
|
|
g_free(filedata.data);
|
2006-10-10 16:02:41 +00:00
|
|
|
|
2008-07-14 11:13:54 +00:00
|
|
|
sci_set_undo_collection(doc->editor->sci, TRUE);
|
2006-10-10 16:02:41 +00:00
|
|
|
|
2009-01-18 18:19:58 +00:00
|
|
|
doc->priv->mtime = filedata.mtime; /* get the modification time from file and keep it */
|
2008-06-15 13:35:48 +00:00
|
|
|
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
|
|
|
|
2008-06-15 13:35:48 +00:00
|
|
|
doc->readonly = readonly || filedata.readonly;
|
2008-07-14 11:13:54 +00:00
|
|
|
sci_set_readonly(doc->editor->sci, doc->readonly);
|
2007-01-06 12:35:43 +00:00
|
|
|
|
2008-02-27 13:17:29 +00:00
|
|
|
/* update line number margin width */
|
2008-09-29 12:44:31 +00:00
|
|
|
doc->priv->line_count = sci_get_line_count(doc->editor->sci);
|
2008-07-14 11:13:54 +00:00
|
|
|
sci_set_line_numbers(doc->editor->sci, editor_prefs.show_linenumber_margin, 0);
|
2007-01-06 12:35:43 +00:00
|
|
|
|
2008-02-27 13:17:29 +00:00
|
|
|
/* set the cursor position according to pos, cl_options.goto_line and cl_options.goto_column */
|
2008-07-08 16:30:37 +00:00
|
|
|
set_cursor_position(doc->editor, pos);
|
2006-06-20 11:55:34 +00:00
|
|
|
|
2006-06-20 14:54:09 +00:00
|
|
|
if (! reload)
|
2005-11-22 12:26:26 +00:00
|
|
|
{
|
2008-06-05 12:14:51 +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-12-14 12:54:36 +00:00
|
|
|
|
2008-07-23 14:44:27 +00:00
|
|
|
use_ft = (ft != NULL) ? ft : filetypes_detect_from_document(doc);
|
2006-06-20 14:54:09 +00:00
|
|
|
}
|
2006-06-29 17:14:52 +00:00
|
|
|
else
|
2008-02-27 13:17:29 +00:00
|
|
|
{ /* reloading */
|
2008-06-15 13:35:48 +00:00
|
|
|
document_undo_clear(doc);
|
2008-06-02 15:52:00 +00:00
|
|
|
|
2006-12-14 12:54:36 +00:00
|
|
|
use_ft = ft;
|
2006-06-26 16:19:28 +00:00
|
|
|
}
|
2008-02-27 13:17:29 +00:00
|
|
|
/* update taglist, typedef keywords and build menu if necessary */
|
2008-06-15 13:35:48 +00:00
|
|
|
document_set_filetype(doc, use_ft);
|
2006-06-29 17:14:52 +00:00
|
|
|
|
2008-05-18 16:03:20 +00:00
|
|
|
/* set indentation settings after setting the filetype */
|
|
|
|
if (reload)
|
2008-08-04 14:07:49 +00:00
|
|
|
editor_set_indent_type(doc->editor, doc->editor->indent_type); /* resetup sci */
|
2008-05-18 16:03:20 +00:00
|
|
|
else
|
2008-08-04 14:07:49 +00:00
|
|
|
set_indentation(doc->editor);
|
2008-05-18 16:03:20 +00:00
|
|
|
|
2008-06-15 13:35:48 +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
|
|
|
|
2008-02-27 13:17:29 +00:00
|
|
|
/* finally add current file to recent files menu, but not the files from the last session */
|
2007-08-23 11:34:06 +00:00
|
|
|
if (! main_status.opening_session_files)
|
2007-08-10 16:11:17 +00:00
|
|
|
ui_add_recent_file(utf8_filename);
|
|
|
|
|
2009-04-05 21:07:40 +00:00
|
|
|
if (! reload)
|
2008-06-15 13:35:48 +00:00
|
|
|
g_signal_emit_by_name(geany_object, "document-open", doc);
|
2006-01-23 17:05:29 +00:00
|
|
|
|
2006-06-26 15:49:07 +00:00
|
|
|
if (reload)
|
2009-04-21 20:52:51 +00:00
|
|
|
ui_set_statusbar(TRUE, _("File %s reloaded."), display_filename);
|
2006-06-26 15:49:07 +00:00
|
|
|
else
|
2007-10-24 10:52:48 +00:00
|
|
|
msgwin_status_add(_("File %s opened(%d%s)."),
|
2009-04-21 20:52:51 +00:00
|
|
|
display_filename, gtk_notebook_get_n_pages(GTK_NOTEBOOK(main_widgets.notebook)),
|
|
|
|
(readonly) ? _(", read-only") : "");
|
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
|
|
|
|
|
|
|
/* TODO This could be used to automatically update the symbol list,
|
|
|
|
* based on a configurable interval */
|
|
|
|
/*g_timeout_add(10000, auto_update_tag_list, doc);*/
|
2006-06-11 22:39:11 +00:00
|
|
|
|
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.
|
|
|
|
* length is the length of the string or -1 if it should be detected */
|
2006-11-17 12:19:31 +00:00
|
|
|
void document_open_file_list(const gchar *data, gssize length)
|
2006-11-15 23:12:13 +00:00
|
|
|
{
|
|
|
|
gint i;
|
|
|
|
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
|
|
|
|
2006-11-17 12:19:31 +00:00
|
|
|
if (length < 0)
|
2006-11-15 23:12:13 +00:00
|
|
|
length = strlen(data);
|
|
|
|
|
2008-02-27 14:23:13 +00:00
|
|
|
switch (utils_get_line_endings(data, length))
|
2006-11-15 23:12:13 +00:00
|
|
|
{
|
|
|
|
case SC_EOL_CR: list = g_strsplit(data, "\r", 0); break;
|
|
|
|
case SC_EOL_CRLF: list = g_strsplit(data, "\r\n", 0); break;
|
|
|
|
case SC_EOL_LF: list = g_strsplit(data, "\n", 0); break;
|
|
|
|
default: list = g_strsplit(data, "\n", 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; ; i++)
|
|
|
|
{
|
2009-04-05 21:07:40 +00:00
|
|
|
if (list[i] == NULL)
|
|
|
|
break;
|
2006-11-15 23:12:13 +00:00
|
|
|
filename = g_filename_from_uri(list[i], NULL, NULL);
|
2009-04-05 21:07:40 +00:00
|
|
|
if (G_UNLIKELY(filename == NULL))
|
|
|
|
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.
|
|
|
|
* @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.
|
|
|
|
* @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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-02-17 18:00:42 +00:00
|
|
|
/**
|
2008-06-15 13:35:48 +00:00
|
|
|
* Reloads the @a 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.
|
|
|
|
*
|
|
|
|
* @return @a TRUE if the %document was actually reloaded or @a FALSE otherwise.
|
|
|
|
**/
|
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
|
|
|
|
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);
|
2008-06-15 13:35:48 +00:00
|
|
|
new_doc = document_open_file_full(doc, NULL, pos, doc->readonly,
|
|
|
|
doc->file_type, forced_enc);
|
2009-01-24 19:38:53 +00:00
|
|
|
#if defined(HAVE_GIO) && defined(USE_GIO_FILEMON)
|
2009-01-21 15:50:48 +00:00
|
|
|
if (new_doc != NULL)
|
|
|
|
{
|
|
|
|
doc->priv->file_disk_status = FILE_IGNORE;
|
|
|
|
/* In case the reload operation didn't produce any events to ignore,
|
|
|
|
* remove the ignore status. */
|
2009-01-24 19:38:31 +00:00
|
|
|
g_timeout_add_seconds(3, monitor_reset_ignore, doc);
|
2009-01-21 15:50:48 +00:00
|
|
|
}
|
|
|
|
#endif
|
2008-06-15 13:35:48 +00:00
|
|
|
return (new_doc != NULL);
|
2006-06-20 11:55:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
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
|
|
|
{
|
2009-01-24 19:38:53 +00:00
|
|
|
#if ! defined(HAVE_GIO) || ! defined(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
|
|
|
|
|
|
|
filebase = g_strconcat(GEANY_STRING_UNTITLED, ".", (doc->file_type)->extension, NULL);
|
|
|
|
filename = g_path_get_basename(doc->file_name);
|
|
|
|
|
|
|
|
/* only search the first 3 lines */
|
|
|
|
ttf.chrg.cpMin = 0;
|
2008-07-14 11:13:54 +00:00
|
|
|
ttf.chrg.cpMax = sci_get_position_from_line(doc->editor->sci, 3);
|
2008-07-08 16:30:37 +00:00
|
|
|
ttf.lpstrText = (gchar*)filebase;
|
|
|
|
|
2008-07-14 11:13:54 +00:00
|
|
|
if (sci_find_text(doc->editor->sci, SCFIND_MATCHCASE, &ttf) != -1)
|
2008-07-08 16:30:37 +00:00
|
|
|
{
|
2008-07-14 11:13:54 +00:00
|
|
|
sci_target_start(doc->editor->sci, ttf.chrgText.cpMin);
|
|
|
|
sci_target_end(doc->editor->sci, ttf.chrgText.cpMax);
|
|
|
|
sci_target_replace(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);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
|
2009-04-15 22:47:33 +00:00
|
|
|
g_return_val_if_fail(doc != NULL, FALSE);
|
2008-03-21 14:02:59 +00:00
|
|
|
|
2009-01-18 18:19:58 +00:00
|
|
|
if (utf8_fname != NULL)
|
2009-04-05 21:07:40 +00:00
|
|
|
setptr(doc->file_name, g_strdup(utf8_fname));
|
|
|
|
|
2008-12-21 20:09:08 +00:00
|
|
|
/* reset real path, it's retrieved again in document_save() */
|
|
|
|
setptr(doc->real_path, NULL);
|
2008-06-03 17:22:04 +00:00
|
|
|
|
2008-03-21 14:02:59 +00:00
|
|
|
/* detect filetype */
|
2008-06-15 13:35:48 +00:00
|
|
|
if (FILETYPE_ID(doc->file_type) == 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
|
|
|
}
|
|
|
|
}
|
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)
|
2008-06-15 13:35:48 +00:00
|
|
|
ui_add_recent_file(doc->file_name);
|
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;
|
|
|
|
|
2009-04-15 22:47:33 +00:00
|
|
|
g_return_val_if_fail(data != NULL || *data == NULL, FALSE);
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
gchar *context = NULL;
|
|
|
|
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);
|
|
|
|
context = g_malloc(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);
|
|
|
|
g_free(context);
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-04-21 20:54:04 +00:00
|
|
|
static gchar *write_data_to_disk(GeanyDocument *doc, const gchar *locale_filename,
|
|
|
|
const gchar *data, gint len)
|
2008-04-25 12:49:38 +00:00
|
|
|
{
|
|
|
|
FILE *fp;
|
|
|
|
gint bytes_written;
|
|
|
|
gint err = 0;
|
2009-04-21 20:54:04 +00:00
|
|
|
GError *error = NULL;
|
2008-04-25 12:49:38 +00:00
|
|
|
|
2009-04-21 20:54:04 +00:00
|
|
|
g_return_val_if_fail(doc != NULL, g_strdup(g_strerror(EINVAL)));
|
|
|
|
g_return_val_if_fail(data != NULL, g_strdup(g_strerror(EINVAL)));
|
2008-04-25 12:49:38 +00:00
|
|
|
|
2009-04-21 20:54:04 +00:00
|
|
|
/* we never use g_file_set_contents() for remote files as Geany only writes such files
|
|
|
|
* 'into' the Fuse mountpoint, the file itself is then written by GVfs on the target
|
|
|
|
* remote filesystem. */
|
|
|
|
if (! file_prefs.use_safe_file_saving || doc->priv->is_remote)
|
|
|
|
{
|
|
|
|
fp = g_fopen(locale_filename, "wb");
|
|
|
|
if (G_UNLIKELY(fp == NULL))
|
|
|
|
return g_strdup(g_strerror(errno));
|
|
|
|
|
|
|
|
bytes_written = fwrite(data, sizeof(gchar), len, fp);
|
2008-04-25 12:49:38 +00:00
|
|
|
|
2009-04-21 20:54:04 +00:00
|
|
|
if (G_UNLIKELY(len != bytes_written))
|
|
|
|
err = errno;
|
2008-04-25 12:49:38 +00:00
|
|
|
|
2009-04-21 20:54:04 +00:00
|
|
|
fclose(fp);
|
2008-04-25 12:49:38 +00:00
|
|
|
|
2009-04-21 20:54:04 +00:00
|
|
|
if (err != 0)
|
|
|
|
return g_strdup(g_strerror(err));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
g_file_set_contents(locale_filename, data, len, &error);
|
|
|
|
if (error != NULL)
|
|
|
|
{
|
|
|
|
gchar *msg = g_strdup(error->message);
|
|
|
|
g_error_free(error);
|
|
|
|
return msg;
|
|
|
|
}
|
|
|
|
}
|
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);
|
|
|
|
}
|
2008-04-25 12:49:38 +00:00
|
|
|
|
2009-04-21 20:54:04 +00:00
|
|
|
return NULL;
|
2008-04-25 12:49:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-02-17 18:00:42 +00:00
|
|
|
/**
|
2008-06-15 13:35:48 +00:00
|
|
|
* Save the @a document. Saving includes replacing tabs by spaces,
|
2008-02-17 18:00:42 +00:00
|
|
|
* stripping trailing spaces and adding a final new line at the end of the file (all only if
|
|
|
|
* user enabled these features). The filetype is set again or auto-detected if it wasn't
|
|
|
|
* set yet. After all, the "document-save" signal is emitted for plugins.
|
|
|
|
*
|
|
|
|
* If the file is not modified, this functions does nothing unless force is set to @c TRUE.
|
|
|
|
*
|
2008-06-12 16:50:01 +00:00
|
|
|
* @param doc The %document to save.
|
2008-02-17 18:00:42 +00:00
|
|
|
* @param force Whether to save the file even if it is not modified (e.g. for Save As).
|
|
|
|
*
|
|
|
|
* @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;
|
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
|
|
|
|
2008-02-27 13:17:29 +00:00
|
|
|
/* the "changed" flag should exclude the "readonly" flag, but check it anyway for safety */
|
2009-01-28 17:01:32 +00:00
|
|
|
if (! force && ! ui_prefs.allow_always_save && (! doc->changed || doc->readonly))
|
2008-04-25 12:49:38 +00:00
|
|
|
return FALSE;
|
2005-11-22 12:26:26 +00:00
|
|
|
|
2009-04-05 21:07:40 +00:00
|
|
|
if (G_UNLIKELY(doc->file_name == NULL))
|
2005-11-22 12:26:26 +00:00
|
|
|
{
|
2007-10-24 10:52:48 +00:00
|
|
|
ui_set_statusbar(TRUE, _("Error saving file."));
|
2006-02-10 20:56:17 +00:00
|
|
|
utils_beep();
|
2006-09-05 18:33:48 +00:00
|
|
|
return FALSE;
|
2005-11-22 12:26:26 +00:00
|
|
|
}
|
|
|
|
|
2008-05-18 16:03:20 +00:00
|
|
|
/* replaces tabs by spaces but only if the current file is not a Makefile */
|
2008-06-15 13:35:48 +00:00
|
|
|
if (file_prefs.replace_tabs && FILETYPE_ID(doc->file_type) != 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 */
|
2008-06-15 13:35:48 +00:00
|
|
|
if (file_prefs.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 */
|
2008-06-15 13:35:48 +00:00
|
|
|
if (file_prefs.final_new_line)
|
2008-09-25 18:28:37 +00:00
|
|
|
editor_ensure_final_newline(doc->editor);
|
2005-11-22 12:26:26 +00:00
|
|
|
|
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
|
|
|
{
|
2008-06-15 13:35:48 +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 */
|
2009-04-21 20:54:04 +00:00
|
|
|
errmsg = write_data_to_disk(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);
|
|
|
|
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 */
|
2008-06-15 13:35:48 +00:00
|
|
|
gboolean document_search_bar_find(GeanyDocument *doc, const gchar *text, gint flags, gboolean inc)
|
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
|
|
|
|
2008-07-14 11:13:54 +00:00
|
|
|
start_pos = (inc) ? sci_get_selection_start(doc->editor->sci) :
|
|
|
|
sci_get_selection_end(doc->editor->sci); /* equal if no selection */
|
2006-10-21 10:50:45 +00:00
|
|
|
|
2008-02-27 13:17:29 +00:00
|
|
|
/* search cursor to end */
|
2006-10-21 10:50:45 +00:00
|
|
|
ttf.chrg.cpMin = start_pos;
|
2008-07-14 11:13:54 +00:00
|
|
|
ttf.chrg.cpMax = sci_get_length(doc->editor->sci);
|
2006-10-21 10:50:45 +00:00
|
|
|
ttf.lpstrText = (gchar *)text;
|
2008-07-14 11:13:54 +00:00
|
|
|
search_pos = sci_find_text(doc->editor->sci, flags, &ttf);
|
2006-10-21 10:50:45 +00:00
|
|
|
|
2008-02-27 13:17:29 +00:00
|
|
|
/* if no match, search start to cursor */
|
2006-10-21 10:50:45 +00:00
|
|
|
if (search_pos == -1)
|
|
|
|
{
|
|
|
|
ttf.chrg.cpMin = 0;
|
|
|
|
ttf.chrg.cpMax = start_pos + strlen(text);
|
2008-07-14 11:13:54 +00:00
|
|
|
search_pos = sci_find_text(doc->editor->sci, flags, &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
|
|
|
}
|
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.
|
|
|
|
* Will skip past any selection, ignoring it. */
|
2008-06-15 13:35:48 +00:00
|
|
|
gint document_find_text(GeanyDocument *doc, const gchar *text, gint flags, gboolean search_backwards,
|
2007-07-04 17:08:53 +00: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 */
|
2008-06-15 13:35:48 +00:00
|
|
|
if (flags & SCFIND_REGEXP)
|
|
|
|
search_backwards = FALSE;
|
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);
|
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)
|
2008-07-14 11:13:54 +00:00
|
|
|
search_pos = sci_search_prev(doc->editor->sci, flags, text);
|
2005-11-22 12:26:26 +00:00
|
|
|
else
|
2008-07-14 11:13:54 +00:00
|
|
|
search_pos = sci_search_next(doc->editor->sci, flags, text);
|
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))
|
|
|
|
{
|
2007-10-24 10:52:48 +00:00
|
|
|
ui_set_statusbar(FALSE, _("\"%s\" was not found."), 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. */
|
2008-05-16 12:08:39 +00:00
|
|
|
if (search_prefs.suppress_dialogs ||
|
2007-07-04 17:08:53 +00:00
|
|
|
dialogs_show_question_full(parent, GTK_STOCK_FIND, GTK_STOCK_CANCEL,
|
2006-11-18 15:47:28 +00:00
|
|
|
_("Wrap search and find again?"), _("\"%s\" was not found."), 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);
|
2008-06-15 13:35:48 +00:00
|
|
|
ret = document_find_text(doc, text, flags, search_backwards, 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.
|
|
|
|
* Returns: start of replaced text, or -1 if no replacement was made */
|
2008-06-15 13:35:48 +00:00
|
|
|
gint document_replace_text(GeanyDocument *doc, const gchar *find_text, const gchar *replace_text,
|
2006-10-22 14:56:05 +00:00
|
|
|
gint flags, gboolean search_backwards)
|
2005-11-22 12:26:26 +00:00
|
|
|
{
|
|
|
|
gint selection_end, selection_start, search_pos;
|
|
|
|
|
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 */
|
2008-06-15 13:35:48 +00:00
|
|
|
if (flags & SCFIND_REGEXP)
|
|
|
|
search_backwards = FALSE;
|
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);
|
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 */
|
2008-06-15 13:35:48 +00:00
|
|
|
document_find_text(doc, find_text, flags, search_backwards, 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
|
|
|
|
2008-06-15 13:35:48 +00:00
|
|
|
search_pos = document_find_text(doc, find_text, flags, search_backwards, 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)
|
|
|
|
return -1;
|
2005-11-22 12:26:26 +00:00
|
|
|
|
|
|
|
if (search_pos != -1)
|
|
|
|
{
|
2006-07-18 14:09:01 +00:00
|
|
|
gint replace_len;
|
2008-02-27 13:17:29 +00:00
|
|
|
/* search next/prev will select matching text, which we use to set the replace target */
|
2008-07-14 11:13:54 +00:00
|
|
|
sci_target_from_selection(doc->editor->sci);
|
|
|
|
replace_len = sci_target_replace(doc->editor->sci, replace_text, flags & SCFIND_REGEXP);
|
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);
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-06-15 13:35:48 +00:00
|
|
|
static void show_replace_summary(GeanyDocument *doc, gint count, const gchar *find_text,
|
2006-12-17 17:13:46 +00:00
|
|
|
const gchar *replace_text, gboolean escaped_chars)
|
|
|
|
{
|
|
|
|
gchar *escaped_find_text, *escaped_replace_text, *filename;
|
|
|
|
|
|
|
|
if (count == 0)
|
|
|
|
{
|
2007-10-24 10:52:48 +00:00
|
|
|
ui_set_statusbar(FALSE, _("No matches found for \"%s\"."), 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));
|
2006-12-17 17:13:46 +00:00
|
|
|
|
|
|
|
if (escaped_chars)
|
2008-02-27 13:17:29 +00:00
|
|
|
{ /* escape special characters for showing */
|
2006-12-17 17:13:46 +00:00
|
|
|
escaped_find_text = g_strescape(find_text, NULL);
|
|
|
|
escaped_replace_text = g_strescape(replace_text, NULL);
|
2008-04-22 14:44:45 +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, escaped_find_text, escaped_replace_text);
|
2006-12-17 17:13:46 +00:00
|
|
|
g_free(escaped_find_text);
|
|
|
|
g_free(escaped_replace_text);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-04-22 14:44:45 +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, find_text, 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,
|
2007-03-13 15:40:59 +00:00
|
|
|
gint 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
|
|
|
|
|
|
|
sci_start_undo_action(sci);
|
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
|
|
|
|
2006-05-25 15:32:03 +00:00
|
|
|
while (TRUE)
|
|
|
|
{
|
2007-07-24 15:34:35 +00:00
|
|
|
gint search_pos;
|
|
|
|
gint find_len = 0, replace_len = 0;
|
|
|
|
|
|
|
|
search_pos = sci_find_text(sci, flags, &ttf);
|
2006-07-18 14:09:01 +00:00
|
|
|
find_len = ttf.chrgText.cpMax - ttf.chrgText.cpMin;
|
2007-06-28 16:00:29 +00:00
|
|
|
if (search_pos == -1)
|
2008-02-27 13:17:29 +00:00
|
|
|
break; /* no more matches */
|
2007-06-28 16:00:29 +00:00
|
|
|
if (find_len == 0 && ! NZV(replace_text))
|
2008-02-27 13:17:29 +00:00
|
|
|
break; /* nothing to do */
|
2005-11-22 12:26:26 +00:00
|
|
|
|
2006-07-18 14:09:01 +00:00
|
|
|
if (search_pos + find_len > end)
|
2008-02-27 13:17:29 +00:00
|
|
|
break; /* found text is partly out of range */
|
2006-03-10 00:52:09 +00:00
|
|
|
else
|
|
|
|
{
|
2007-07-24 15:34:35 +00:00
|
|
|
gint movepastEOL = 0;
|
|
|
|
|
|
|
|
sci_target_start(sci, search_pos);
|
|
|
|
sci_target_end(sci, search_pos + find_len);
|
|
|
|
|
|
|
|
if (find_len <= 0)
|
|
|
|
{
|
2008-09-25 18:29:53 +00:00
|
|
|
gchar chNext = sci_get_char_at(sci, sci_get_target_end(sci));
|
2007-08-24 11:31:27 +00:00
|
|
|
|
|
|
|
if (chNext == '\r' || chNext == '\n')
|
2007-07-24 15:34:35 +00:00
|
|
|
movepastEOL = 1;
|
|
|
|
}
|
|
|
|
replace_len = sci_target_replace(sci, replace_text,
|
2006-07-18 14:09:01 +00:00
|
|
|
flags & SCFIND_REGEXP);
|
2006-08-23 18:08:48 +00:00
|
|
|
count++;
|
2007-06-28 16:00:29 +00:00
|
|
|
if (search_pos == end)
|
2008-02-27 13:17:29 +00:00
|
|
|
break; /* Prevent hang when replacing regex $ */
|
2007-06-28 16:00:29 +00:00
|
|
|
|
2008-02-27 13:17:29 +00:00
|
|
|
/* make the next search start after the replaced text */
|
2007-07-24 15:34:35 +00:00
|
|
|
start = search_pos + replace_len + movepastEOL;
|
|
|
|
if (find_len == 0)
|
2008-09-25 18:29:53 +00:00
|
|
|
start = sci_get_position_after(sci, start); /* prevent '[ ]*' regex rematching part of replaced text */
|
2007-06-28 16:00:29 +00:00
|
|
|
ttf.chrg.cpMin = start;
|
2008-02-27 13:17:29 +00:00
|
|
|
end += replace_len - find_len; /* update end of range now text has changed */
|
2007-06-28 16:00:29 +00:00
|
|
|
ttf.chrg.cpMax = end;
|
2006-03-10 00:52:09 +00:00
|
|
|
}
|
2005-11-22 12:26:26 +00:00
|
|
|
}
|
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)
|
|
|
|
*new_range_end = end;
|
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,
|
|
|
|
gint flags, gboolean escaped_chars)
|
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
|
|
|
|
2008-06-15 13:35:48 +00:00
|
|
|
show_replace_summary(doc, count, find_text, replace_text, escaped_chars);
|
2006-07-18 14:09:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-02-27 13:17:29 +00:00
|
|
|
/* returns TRUE if at least one replacement was made. */
|
2008-06-15 13:35:48 +00:00
|
|
|
gboolean document_replace_all(GeanyDocument *doc, const gchar *find_text, const gchar *replace_text,
|
2006-11-25 12:32:22 +00:00
|
|
|
gint flags, gboolean escaped_chars)
|
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
|
|
|
|
2008-06-15 13:35:48 +00:00
|
|
|
show_replace_summary(doc, count, find_text, replace_text, escaped_chars);
|
2007-03-13 15:40:59 +00:00
|
|
|
return (count > 0);
|
2005-11-22 12:26:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-11-07 14:37:17 +00:00
|
|
|
static gboolean update_tags_from_buffer(GeanyDocument *doc)
|
|
|
|
{
|
|
|
|
gboolean result;
|
|
|
|
#if 1
|
|
|
|
/* old code */
|
|
|
|
result = tm_source_file_update(doc->tm_file, TRUE, FALSE, TRUE);
|
|
|
|
#else
|
|
|
|
gsize len = sci_get_length(doc->editor->sci) + 1;
|
|
|
|
gchar *text = g_malloc(len);
|
|
|
|
|
|
|
|
/* we copy the whole text into memory instead using a direct char pointer from
|
|
|
|
* Scintilla because tm_source_file_buffer_update() does modify the string slightly */
|
|
|
|
sci_get_text(doc->editor->sci, len, text);
|
|
|
|
result = tm_source_file_buffer_update(doc->tm_file, (guchar*) text, len, TRUE);
|
|
|
|
g_free(text);
|
|
|
|
#endif
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-06-15 13:35:48 +00:00
|
|
|
void document_update_tag_list(GeanyDocument *doc, gboolean update)
|
2005-11-22 12:26:26 +00:00
|
|
|
{
|
2007-11-01 13:00:32 +00:00
|
|
|
/* We must call treeviews_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. */
|
2007-10-31 16:22:01 +00:00
|
|
|
gboolean success = FALSE;
|
2007-11-01 11:49:29 +00:00
|
|
|
|
2008-02-27 13:17:29 +00:00
|
|
|
/* if the filetype doesn't have a tag parser or it is a new file */
|
2009-04-15 22:47:33 +00:00
|
|
|
if (doc == NULL || doc->file_type == NULL || app->tm_workspace == NULL ||
|
|
|
|
! filetype_has_tags(doc->file_type) || ! doc->file_name)
|
2006-09-30 16:15:45 +00:00
|
|
|
{
|
2008-02-27 13:17:29 +00:00
|
|
|
/* set the default (empty) tag list */
|
2008-06-15 13:35:48 +00:00
|
|
|
treeviews_update_tag_list(doc, FALSE);
|
2006-09-30 16:15:45 +00:00
|
|
|
return;
|
|
|
|
}
|
2005-11-22 12:26:26 +00:00
|
|
|
|
2009-04-15 22:47:33 +00:00
|
|
|
if (doc->tm_file == NULL)
|
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);
|
2007-11-01 13:00:32 +00:00
|
|
|
|
2008-06-15 13:35:48 +00:00
|
|
|
doc->tm_file = tm_source_file_new(locale_filename, FALSE, doc->file_type->name);
|
2006-01-23 17:05:29 +00:00
|
|
|
g_free(locale_filename);
|
2007-11-01 13:00:32 +00:00
|
|
|
|
2008-06-15 13:35:48 +00:00
|
|
|
if (doc->tm_file)
|
2007-10-31 16:22:01 +00:00
|
|
|
{
|
2008-06-15 13:35:48 +00:00
|
|
|
if (!tm_workspace_add_object(doc->tm_file))
|
2007-11-01 11:49:29 +00:00
|
|
|
{
|
2008-06-15 13:35:48 +00:00
|
|
|
tm_work_object_free(doc->tm_file);
|
|
|
|
doc->tm_file = NULL;
|
2007-11-01 11:49:29 +00:00
|
|
|
}
|
2007-11-01 13:00:32 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
if (update)
|
2008-11-07 14:37:17 +00:00
|
|
|
update_tags_from_buffer(doc);
|
2007-11-01 13:00:32 +00:00
|
|
|
success = TRUE;
|
|
|
|
}
|
2007-10-31 16:22:01 +00:00
|
|
|
}
|
2005-11-22 12:26:26 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-11-07 14:37:17 +00:00
|
|
|
success = update_tags_from_buffer(doc);
|
2009-04-05 21:07:40 +00:00
|
|
|
if (G_UNLIKELY(! success))
|
2006-06-26 17:44:18 +00:00
|
|
|
geany_debug("tag list updating failed");
|
2005-11-22 12:26:26 +00:00
|
|
|
}
|
2008-06-15 13:35:48 +00:00
|
|
|
treeviews_update_tag_list(doc, success);
|
2005-11-22 12:26:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-03-08 13:32:03 +00:00
|
|
|
/* Caches the list of project typenames, as a space separated GString.
|
|
|
|
* Returns: TRUE if typenames have changed.
|
|
|
|
* (*types) is set to the list of typenames, or NULL if there are none. */
|
2007-10-09 12:04:15 +00:00
|
|
|
static gboolean get_project_typenames(const GString **types, gint lang)
|
2006-11-08 11:42:05 +00:00
|
|
|
{
|
2007-03-08 13:32:03 +00:00
|
|
|
static GString *last_typenames = NULL;
|
2006-11-08 11:42:05 +00:00
|
|
|
GString *s = NULL;
|
|
|
|
|
2009-04-15 22:47:33 +00:00
|
|
|
if (app->tm_workspace)
|
2006-11-08 11:42:05 +00:00
|
|
|
{
|
|
|
|
GPtrArray *tags_array = app->tm_workspace->work_object.tags_array;
|
|
|
|
|
|
|
|
if (tags_array)
|
|
|
|
{
|
2007-10-09 12:04:15 +00:00
|
|
|
s = symbols_find_tags_as_string(tags_array, TM_GLOBAL_TYPE_MASK, lang);
|
2006-11-08 11:42:05 +00:00
|
|
|
}
|
|
|
|
}
|
2007-03-08 13:32:03 +00:00
|
|
|
|
|
|
|
if (s && last_typenames && g_string_equal(s, last_typenames))
|
|
|
|
{
|
|
|
|
g_string_free(s, TRUE);
|
|
|
|
*types = last_typenames;
|
2008-02-27 13:17:29 +00:00
|
|
|
return FALSE; /* project typenames haven't changed */
|
2007-03-08 13:32:03 +00:00
|
|
|
}
|
2008-02-27 13:17:29 +00:00
|
|
|
/* cache typename list for next time */
|
2007-03-08 13:32:03 +00:00
|
|
|
if (last_typenames)
|
|
|
|
g_string_free(last_typenames, TRUE);
|
|
|
|
last_typenames = s;
|
|
|
|
|
|
|
|
*types = s;
|
2009-04-05 21:07:40 +00:00
|
|
|
if (s == NULL)
|
|
|
|
return FALSE;
|
2007-03-08 13:32:03 +00:00
|
|
|
return TRUE;
|
2006-11-08 11:42:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-03-08 13:32:03 +00:00
|
|
|
/* If sci is NULL, update project typenames for all documents that support typenames,
|
|
|
|
* if typenames have changed.
|
|
|
|
* If sci is not NULL, then if sci supports typenames, project typenames are updated
|
|
|
|
* if necessary, and typename keywords are set for sci.
|
|
|
|
* Returns: TRUE if any scintilla type keywords were updated. */
|
2008-06-20 12:34:13 +00:00
|
|
|
static gboolean update_type_keywords(GeanyDocument *doc, gint lang)
|
2006-12-15 12:52:52 +00:00
|
|
|
{
|
|
|
|
gboolean ret = FALSE;
|
2007-03-08 13:32:03 +00:00
|
|
|
guint n;
|
|
|
|
const GString *s;
|
2009-03-16 14:03:20 +00:00
|
|
|
ScintillaObject *sci;
|
2006-12-15 12:52:52 +00:00
|
|
|
|
2009-04-15 22:47:33 +00:00
|
|
|
g_return_val_if_fail(doc != NULL, FALSE);
|
2009-03-16 14:03:20 +00:00
|
|
|
sci = doc->editor->sci;
|
2009-03-13 09:58:27 +00:00
|
|
|
|
|
|
|
switch (FILETYPE_ID(doc->file_type))
|
|
|
|
{ /* continue working with the following languages, skip on all others */
|
|
|
|
case GEANY_FILETYPES_C:
|
|
|
|
case GEANY_FILETYPES_CPP:
|
|
|
|
case GEANY_FILETYPES_CS:
|
|
|
|
case GEANY_FILETYPES_D:
|
|
|
|
case GEANY_FILETYPES_JAVA:
|
|
|
|
case GEANY_FILETYPES_VALA:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
sci = doc->editor->sci;
|
2009-04-15 22:47:33 +00:00
|
|
|
if (sci != NULL && editor_lexer_get_type_keyword_idx(sci_get_lexer(sci)) == -1)
|
2007-03-08 13:32:03 +00:00
|
|
|
return FALSE;
|
2006-12-15 12:52:52 +00:00
|
|
|
|
2007-10-09 12:04:15 +00:00
|
|
|
if (! get_project_typenames(&s, lang))
|
2008-02-27 13:17:29 +00:00
|
|
|
{ /* typenames have not changed */
|
2007-03-08 13:32:03 +00:00
|
|
|
if (s != NULL && sci != NULL)
|
2007-02-23 13:26:06 +00:00
|
|
|
{
|
2007-05-28 16:07:30 +00:00
|
|
|
gint keyword_idx = editor_lexer_get_type_keyword_idx(sci_get_lexer(sci));
|
2007-03-08 13:32:03 +00:00
|
|
|
|
|
|
|
sci_set_keywords(sci, keyword_idx, s->str);
|
2008-06-20 12:34:13 +00:00
|
|
|
queue_colourise(doc);
|
2007-02-23 13:26:06 +00:00
|
|
|
}
|
2007-03-08 13:32:03 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
2009-04-15 22:47:33 +00:00
|
|
|
g_return_val_if_fail(s != NULL, FALSE);
|
2006-12-16 17:18:53 +00:00
|
|
|
|
2008-05-29 17:00:54 +00:00
|
|
|
for (n = 0; n < documents_array->len; n++)
|
2007-03-08 13:32:03 +00:00
|
|
|
{
|
2008-07-11 14:25:26 +00:00
|
|
|
if (documents[n]->is_valid)
|
2006-12-15 12:52:52 +00:00
|
|
|
{
|
2008-07-14 11:13:54 +00:00
|
|
|
ScintillaObject *wid = documents[n]->editor->sci;
|
2007-05-28 16:07:30 +00:00
|
|
|
gint keyword_idx = editor_lexer_get_type_keyword_idx(sci_get_lexer(wid));
|
2006-12-15 12:52:52 +00:00
|
|
|
|
2007-03-08 13:32:03 +00:00
|
|
|
if (keyword_idx > 0)
|
2006-12-15 12:52:52 +00:00
|
|
|
{
|
2007-03-08 13:32:03 +00:00
|
|
|
sci_set_keywords(wid, keyword_idx, s->str);
|
2008-06-20 12:34:13 +00:00
|
|
|
queue_colourise(documents[n]);
|
2007-03-08 13:32:03 +00:00
|
|
|
ret = TRUE;
|
2006-12-15 12:52:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-06-12 16:50:01 +00:00
|
|
|
/** Sets the filetype of the document (which controls syntax highlighting and tags)
|
|
|
|
* @param doc The document to use.
|
|
|
|
* @param type The filetype. */
|
2008-06-15 13:35:48 +00:00
|
|
|
void document_set_filetype(GeanyDocument *doc, GeanyFiletype *type)
|
2005-11-22 12:26:26 +00:00
|
|
|
{
|
2007-03-12 16:24:52 +00:00
|
|
|
gboolean ft_changed;
|
2006-12-15 12:52:52 +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
|
|
|
|
2007-12-30 15:31:45 +00:00
|
|
|
geany_debug("%s : %s (%s)",
|
2008-06-15 13:35:48 +00:00
|
|
|
(doc->file_name != NULL) ? doc->file_name : "unknown",
|
2007-12-30 15:31:45 +00:00
|
|
|
(type->name != NULL) ? type->name : "unknown",
|
2008-06-15 13:35:48 +00:00
|
|
|
(doc->encoding != NULL) ? doc->encoding : "unknown");
|
2006-12-07 15:13:49 +00:00
|
|
|
|
2008-06-15 13:35:48 +00:00
|
|
|
ft_changed = (doc->file_type != type);
|
2008-02-27 13:17:29 +00:00
|
|
|
if (ft_changed) /* filetype has 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
|
|
|
}
|
2008-07-14 11:13:54 +00:00
|
|
|
highlighting_set_styles(doc->editor->sci, type->id);
|
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);
|
2006-12-07 15:13:49 +00:00
|
|
|
}
|
|
|
|
|
2008-06-15 13:35:48 +00:00
|
|
|
document_update_tag_list(doc, TRUE);
|
2008-06-20 12:34:13 +00:00
|
|
|
|
|
|
|
/* Update session typename keywords. */
|
|
|
|
update_type_keywords(doc, type->lang);
|
2005-11-22 12:26:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-02-17 18:00:42 +00:00
|
|
|
/**
|
|
|
|
* Sets the encoding of a %document.
|
|
|
|
* 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.
|
|
|
|
*
|
2008-06-12 16:50:01 +00:00
|
|
|
* @param doc The %document to use.
|
2008-02-17 18:00:42 +00:00
|
|
|
* @param new_encoding The encoding to be set for the %document.
|
|
|
|
**/
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
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
|
|
|
|
2008-06-15 13:35:48 +00:00
|
|
|
document_set_text_changed(doc, TRUE);
|
|
|
|
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
|
|
|
|
2008-06-15 13:35:48 +00:00
|
|
|
document_set_text_changed(doc, TRUE);
|
|
|
|
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
|
|
|
|
2009-02-08 19:52:21 +00:00
|
|
|
/**
|
|
|
|
* Gets the status colour of the document, or NULL if default widget colouring should be used.
|
|
|
|
* Returned colours are red if the document has changes, green is the document is read-only
|
|
|
|
* or simply NULL if the document is unmodified but writable.
|
|
|
|
*
|
|
|
|
* @param doc The document to use.
|
|
|
|
*
|
|
|
|
* @return The colour for the document or NULL if the default colour should be used. The colour
|
|
|
|
* 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
|
|
|
{
|
|
|
|
static GdkColor red = {0, 0xFFFF, 0, 0};
|
|
|
|
static GdkColor green = {0, 0, 0x7FFF, 0};
|
2009-04-21 20:52:32 +00:00
|
|
|
#if defined(HAVE_GIO) && defined(USE_GIO_FILEMON)
|
|
|
|
static GdkColor orange = {0, 0xFFFF, 0x7FFF, 0};
|
|
|
|
#endif
|
2006-12-05 10:37:36 +00:00
|
|
|
GdkColor *color = NULL;
|
|
|
|
|
2009-04-15 22:47:33 +00:00
|
|
|
g_return_val_if_fail(doc != NULL, NULL);
|
2008-06-15 13:35:48 +00:00
|
|
|
|
|
|
|
if (doc->changed)
|
2006-12-05 10:37:36 +00:00
|
|
|
color = &red;
|
2009-01-27 21:12:46 +00:00
|
|
|
#if defined(HAVE_GIO) && defined(USE_GIO_FILEMON)
|
2009-01-18 18:19:58 +00:00
|
|
|
else if (doc->priv->file_disk_status == FILE_MISSING ||
|
|
|
|
doc->priv->file_disk_status == FILE_CHANGED)
|
|
|
|
{
|
|
|
|
color = &orange;
|
|
|
|
}
|
2009-01-27 21:12:46 +00:00
|
|
|
#endif
|
2008-06-15 13:35:48 +00:00
|
|
|
else if (doc->readonly)
|
2006-12-05 10:37:36 +00:00
|
|
|
color = &green;
|
|
|
|
|
2008-02-27 13:17:29 +00:00
|
|
|
return color; /* return pointer to static GdkColor. */
|
2006-12-05 10:37:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-12-03 18:03:54 +00:00
|
|
|
/** Accessor function for @ref GeanyData::documents_array items.
|
|
|
|
* @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
|
|
|
|
|
|
|
|
2008-06-20 14:19:57 +00:00
|
|
|
/* create a new file and copy file content and properties */
|
2008-06-15 13:35:48 +00:00
|
|
|
GeanyDocument *document_clone(GeanyDocument *old_doc, const gchar *utf8_filename)
|
2007-07-17 14:52:57 +00:00
|
|
|
{
|
2008-06-15 13:35:48 +00:00
|
|
|
gint len;
|
2007-09-03 16:09:53 +00:00
|
|
|
gchar *text;
|
2008-06-15 13:35:48 +00:00
|
|
|
GeanyDocument *doc;
|
2007-07-17 14:52:57 +00:00
|
|
|
|
2009-04-15 22:47:33 +00:00
|
|
|
g_return_val_if_fail(old_doc != NULL, NULL);
|
2009-04-05 21:07:40 +00:00
|
|
|
|
2008-07-14 11:13:54 +00:00
|
|
|
len = sci_get_length(old_doc->editor->sci) + 1;
|
2007-09-03 16:09:53 +00:00
|
|
|
text = (gchar*) g_malloc(len);
|
2008-07-14 11:13:54 +00:00
|
|
|
sci_get_text(old_doc->editor->sci, len, text);
|
2008-02-27 13:17:29 +00:00
|
|
|
/* use old file type (or maybe NULL for auto detect would be better?) */
|
2008-06-15 13:35:48 +00:00
|
|
|
doc = document_new_file(utf8_filename, old_doc->file_type, text);
|
2007-09-03 16:09:53 +00:00
|
|
|
g_free(text);
|
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;
|
2008-06-15 13:35:48 +00:00
|
|
|
doc->readonly = old_doc->readonly;
|
|
|
|
doc->has_bom = old_doc->has_bom;
|
|
|
|
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
|
|
|
|
|
|
|
ui_document_show_hide(doc);
|
|
|
|
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)
|
|
|
|
{
|
2009-02-10 21:11:25 +00:00
|
|
|
guint i, p, page_count, len = documents_array->len;
|
2009-04-05 21:07:40 +00:00
|
|
|
GeanyDocument *doc;
|
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));
|
|
|
|
for (p = 0; p < page_count; p++)
|
2008-04-24 14:33:12 +00:00
|
|
|
{
|
2009-04-05 21:07:40 +00:00
|
|
|
doc = document_get_from_page(p);
|
2008-06-15 13:35:48 +00:00
|
|
|
if (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 */
|
|
|
|
for (i = 0; i < len; i++)
|
|
|
|
{
|
2009-04-05 21:07:40 +00:00
|
|
|
doc = documents[i];
|
|
|
|
if (doc->is_valid && doc->changed)
|
2008-04-24 14:33:12 +00:00
|
|
|
{
|
2009-04-05 21:07:40 +00:00
|
|
|
doc->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;
|
|
|
|
|
2008-05-22 14:41:28 +00:00
|
|
|
while (gtk_notebook_get_n_pages(GTK_NOTEBOOK(main_widgets.notebook)) > 0)
|
2008-04-24 14:33:12 +00:00
|
|
|
{
|
2008-06-15 13:35:48 +00:00
|
|
|
document_remove_page(0);
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-01-21 15:50:48 +00:00
|
|
|
static gboolean 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);
|
|
|
|
gboolean want_reload;
|
2008-06-12 16:50:01 +00:00
|
|
|
|
2008-06-15 13:35:48 +00:00
|
|
|
want_reload = dialogs_show_question_full(NULL, _("_Reload"), GTK_STOCK_CANCEL,
|
|
|
|
_("Do you want to reload it?"),
|
|
|
|
_("The file '%s' on the disk is more recent than\n"
|
|
|
|
"the current buffer."), base_name);
|
|
|
|
if (want_reload)
|
|
|
|
{
|
|
|
|
document_reload_file(doc, NULL);
|
|
|
|
}
|
|
|
|
g_free(base_name);
|
|
|
|
return want_reload;
|
2008-06-12 16:50:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-01-21 15:50:48 +00:00
|
|
|
static gboolean monitor_resave_missing_file(GeanyDocument *doc)
|
2009-01-18 18:19:58 +00:00
|
|
|
{
|
|
|
|
gboolean want_reload = FALSE;
|
|
|
|
|
|
|
|
/* file is missing - set unsaved state */
|
|
|
|
document_set_text_changed(doc, TRUE);
|
|
|
|
/* don't prompt more than once */
|
|
|
|
setptr(doc->real_path, NULL);
|
|
|
|
|
|
|
|
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))
|
|
|
|
{
|
|
|
|
dialogs_show_save_as();
|
|
|
|
want_reload = TRUE;
|
|
|
|
}
|
|
|
|
return want_reload;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-01-21 15:50:48 +00:00
|
|
|
static time_t monitor_check_status_real(GeanyDocument *doc, gboolean force)
|
2009-01-18 18:19:58 +00:00
|
|
|
{
|
|
|
|
time_t t = 0;
|
|
|
|
struct stat st;
|
|
|
|
gchar *locale_filename;
|
|
|
|
|
|
|
|
t = time(NULL);
|
|
|
|
|
|
|
|
if (! force && doc->priv->last_check > (t - file_prefs.disk_check_timeout))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
doc->priv->last_check = t;
|
|
|
|
|
|
|
|
locale_filename = utils_get_locale_from_utf8(doc->file_name);
|
|
|
|
if (g_stat(locale_filename, &st) != 0)
|
|
|
|
{
|
|
|
|
doc->priv->file_disk_status = FILE_MISSING;
|
|
|
|
return 0;
|
|
|
|
}
|
2009-04-05 21:07:40 +00:00
|
|
|
else if (G_UNLIKELY(doc->priv->mtime > t) || G_UNLIKELY(st.st_mtime > t))
|
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);
|
2009-01-18 18:19:58 +00:00
|
|
|
}
|
|
|
|
else if (doc->priv->mtime < st.st_mtime)
|
|
|
|
{
|
|
|
|
doc->priv->file_disk_status = FILE_CHANGED;
|
|
|
|
/* return st.st_mtime to set it after the file has been possibly reloaded */
|
|
|
|
t = st.st_mtime;
|
|
|
|
}
|
|
|
|
g_free(locale_filename);
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-06-15 13:35:48 +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)
|
2008-06-12 16:50:01 +00:00
|
|
|
{
|
2008-06-15 13:35:48 +00:00
|
|
|
gboolean ret = FALSE;
|
2009-01-18 18:19:58 +00:00
|
|
|
time_t t;
|
2008-06-12 16:50:01 +00:00
|
|
|
|
2009-04-05 21:07:40 +00:00
|
|
|
if (file_prefs.disk_check_timeout == 0 || G_UNLIKELY(doc == NULL))
|
2008-06-16 18:31:59 +00:00
|
|
|
return FALSE;
|
2009-01-19 19:30:18 +00:00
|
|
|
|
|
|
|
/* ignore documents that have never been saved to disk and remote files */
|
|
|
|
if (doc->real_path == NULL || doc->priv->is_remote)
|
2008-06-15 13:35:48 +00:00
|
|
|
return FALSE;
|
2008-06-12 16:50:01 +00:00
|
|
|
|
2009-01-27 19:10:12 +00:00
|
|
|
#if defined(HAVE_GIO) && defined(USE_GIO_FILEMON)
|
2009-01-21 15:50:48 +00:00
|
|
|
/* when we saved a file recently, we want to ignore any changes */
|
|
|
|
if (doc->priv->file_disk_status == FILE_IGNORE)
|
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
2009-01-27 19:10:12 +00:00
|
|
|
#else
|
|
|
|
/* check the file's mtime */
|
2009-01-21 15:50:48 +00:00
|
|
|
t = monitor_check_status_real(doc, force);
|
2009-01-27 19:10:12 +00:00
|
|
|
#endif
|
2008-06-12 16:50:01 +00:00
|
|
|
|
2009-01-18 18:19:58 +00:00
|
|
|
switch (doc->priv->file_disk_status)
|
2008-06-15 13:35:48 +00:00
|
|
|
{
|
2009-01-18 18:19:58 +00:00
|
|
|
case FILE_CHANGED:
|
2008-06-15 13:35:48 +00:00
|
|
|
{
|
2009-01-21 15:50:48 +00:00
|
|
|
monitor_reload_file(doc);
|
2009-01-18 18:19:58 +00:00
|
|
|
doc->priv->mtime = t;
|
|
|
|
ret = TRUE;
|
|
|
|
break;
|
2008-06-15 13:35:48 +00:00
|
|
|
}
|
2009-01-18 18:19:58 +00:00
|
|
|
case FILE_MISSING:
|
2008-06-15 13:35:48 +00:00
|
|
|
{
|
2009-01-21 15:50:48 +00:00
|
|
|
monitor_resave_missing_file(doc);
|
2009-01-18 18:19:58 +00:00
|
|
|
ret = TRUE;
|
|
|
|
break;
|
2008-06-15 13:35:48 +00:00
|
|
|
}
|
2009-01-18 18:19:58 +00:00
|
|
|
default:
|
|
|
|
break;
|
2008-06-15 13:35:48 +00:00
|
|
|
}
|
2009-01-18 18:19:58 +00:00
|
|
|
|
2009-01-27 21:12:46 +00:00
|
|
|
doc->priv->file_disk_status = FILE_OK;
|
|
|
|
/*ui_update_tab_status(doc);*/
|
2009-01-18 18:19:58 +00:00
|
|
|
|
2008-06-15 13:35:48 +00:00
|
|
|
return ret;
|
2008-06-12 16:50:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|