2005-11-22 12:26:26 +00:00
|
|
|
/*
|
|
|
|
* document.c - this file is part of Geany, a fast and lightweight IDE
|
|
|
|
*
|
2006-01-11 18:38:17 +00:00
|
|
|
* Copyright 2006 Enrico Troeger <enrico.troeger@uvena.de>
|
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
|
|
|
*/
|
|
|
|
|
2006-07-04 21:31:08 +00:00
|
|
|
#include "SciLexer.h"
|
2005-12-29 20:01:18 +00:00
|
|
|
#include "geany.h"
|
|
|
|
|
|
|
|
#ifdef TIME_WITH_SYS_TIME
|
|
|
|
# include <time.h>
|
|
|
|
# include <sys/time.h>
|
|
|
|
#endif
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#ifdef HAVE_SYS_STAT_H
|
|
|
|
# include <sys/stat.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_SYS_TYPES_H
|
|
|
|
# include <sys/types.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_FCNTL_H
|
|
|
|
# include <fcntl.h>
|
|
|
|
#endif
|
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
|
|
|
|
|
|
|
#include "document.h"
|
2005-12-29 20:01:18 +00:00
|
|
|
#include "support.h"
|
|
|
|
#include "sciwrappers.h"
|
|
|
|
#include "sci_cb.h"
|
|
|
|
#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"
|
2005-11-22 12:26:26 +00:00
|
|
|
|
|
|
|
|
2006-10-09 16:08:53 +00:00
|
|
|
/* dynamic array of document elements to hold all information of the notebook tabs */
|
|
|
|
GArray *doc_array;
|
|
|
|
|
|
|
|
|
2006-07-18 17:43:22 +00:00
|
|
|
/* Returns -1 if no text found or the new range endpoint after replacing. */
|
2006-07-18 14:09:01 +00:00
|
|
|
static gint
|
2006-07-18 17:43:22 +00:00
|
|
|
document_replace_range(gint idx, const gchar *find_text, const gchar *replace_text,
|
2006-09-02 23:28:34 +00:00
|
|
|
gint flags, gint start, gint end, gboolean escaped_chars);
|
2006-07-18 14:09:01 +00:00
|
|
|
|
2006-10-10 16:02:41 +00:00
|
|
|
static void document_undo_clear(gint idx);
|
|
|
|
static void document_redo_add(gint idx, guint type, gpointer data);
|
|
|
|
|
|
|
|
|
2005-11-22 12:26:26 +00:00
|
|
|
|
2006-06-07 21:24:15 +00:00
|
|
|
/* returns the index of the notebook page which has the given filename
|
|
|
|
* is_tm_filename is needed when passing TagManager filenames because they are
|
|
|
|
* dereferenced, and would not match the link filename. */
|
|
|
|
gint document_find_by_filename(const gchar *filename, gboolean is_tm_filename)
|
2005-11-22 12:26:26 +00:00
|
|
|
{
|
|
|
|
guint i;
|
|
|
|
|
|
|
|
if (! filename) return -1;
|
|
|
|
|
2006-10-09 16:08:53 +00:00
|
|
|
for(i = 0; i < doc_array->len; i++)
|
2005-11-22 12:26:26 +00:00
|
|
|
{
|
2006-06-11 14:08:10 +00:00
|
|
|
gchar *dl_fname = (is_tm_filename && doc_list[i].tm_file) ?
|
|
|
|
doc_list[i].tm_file->file_name : doc_list[i].file_name;
|
2006-07-26 17:02:16 +00:00
|
|
|
#ifdef G_OS_WIN32
|
2005-11-22 12:26:26 +00:00
|
|
|
// ignore the case of filenames and paths under WIN32, causes errors if not
|
2006-06-07 21:24:15 +00:00
|
|
|
if (dl_fname && ! strcasecmp(dl_fname, filename)) return i;
|
2005-11-22 12:26:26 +00:00
|
|
|
#else
|
2006-12-07 16:09:45 +00:00
|
|
|
if (dl_fname && utils_str_equal(dl_fname, filename)) return i;
|
2005-11-22 12:26:26 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* returns the index of the notebook page which has sci */
|
|
|
|
gint document_find_by_sci(ScintillaObject *sci)
|
|
|
|
{
|
|
|
|
guint i;
|
|
|
|
|
|
|
|
if (! sci) return -1;
|
|
|
|
|
2006-10-09 16:08:53 +00:00
|
|
|
for(i = 0; i < doc_array->len; i++)
|
2005-11-22 12:26:26 +00:00
|
|
|
{
|
2006-01-14 22:40:05 +00:00
|
|
|
if (doc_list[i].is_valid && doc_list[i].sci == sci) return i;
|
2005-11-22 12:26:26 +00:00
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* returns the index of the given notebook page in the document list */
|
2006-01-11 18:38:17 +00:00
|
|
|
gint document_get_n_idx(guint page_num)
|
2005-11-22 12:26:26 +00:00
|
|
|
{
|
2006-05-08 01:57:25 +00:00
|
|
|
ScintillaObject *sci;
|
2006-10-09 16:08:53 +00:00
|
|
|
if (page_num >= doc_array->len) return -1;
|
2006-01-11 18:38:17 +00:00
|
|
|
|
2006-05-08 01:57:25 +00:00
|
|
|
sci = (ScintillaObject*)gtk_notebook_get_nth_page(
|
2006-01-11 18:38:17 +00:00
|
|
|
GTK_NOTEBOOK(app->notebook), page_num);
|
2005-11-22 12:26:26 +00:00
|
|
|
|
|
|
|
return document_find_by_sci(sci);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* returns the index of the current notebook page in the document list */
|
2006-09-02 20:49:11 +00:00
|
|
|
gint document_get_cur_idx()
|
2005-11-22 12:26:26 +00:00
|
|
|
{
|
|
|
|
gint cur_page = gtk_notebook_get_current_page(GTK_NOTEBOOK(app->notebook));
|
|
|
|
ScintillaObject *sci = (ScintillaObject*)gtk_notebook_get_nth_page(GTK_NOTEBOOK(app->notebook), cur_page);
|
|
|
|
|
|
|
|
if (cur_page == -1)
|
|
|
|
return -1;
|
|
|
|
else
|
|
|
|
return document_find_by_sci(sci);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-09-02 20:49:11 +00:00
|
|
|
/* returns NULL if no documents are open */
|
|
|
|
document *document_get_current()
|
|
|
|
{
|
|
|
|
gint idx = document_get_cur_idx();
|
|
|
|
|
|
|
|
return DOC_IDX_VALID(idx) ? &doc_list[idx] : NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-10-09 16:08:53 +00:00
|
|
|
void document_init_doclist()
|
2005-11-22 12:26:26 +00:00
|
|
|
{
|
2006-10-09 16:08:53 +00:00
|
|
|
doc_array = g_array_new(FALSE, FALSE, sizeof(document));
|
|
|
|
}
|
2005-11-22 12:26:26 +00:00
|
|
|
|
2006-10-09 16:08:53 +00:00
|
|
|
|
|
|
|
void document_finalize()
|
|
|
|
{
|
|
|
|
g_array_free(doc_array, TRUE);
|
2005-11-22 12:26:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-09-06 16:09:08 +00:00
|
|
|
void document_set_text_changed(gint idx)
|
2005-11-22 12:26:26 +00:00
|
|
|
{
|
2006-12-04 14:32:50 +00:00
|
|
|
if (DOC_IDX_VALID(idx) && ! app->quitting)
|
2005-11-22 12:26:26 +00:00
|
|
|
{
|
2006-12-05 10:37:36 +00:00
|
|
|
ui_update_tab_status(idx);
|
2006-09-06 16:09:08 +00:00
|
|
|
ui_save_buttons_toggle(doc_list[idx].changed);
|
|
|
|
ui_set_window_title(idx);
|
2006-10-24 13:41:34 +00:00
|
|
|
ui_update_statusbar(idx, -1);
|
2006-09-06 16:09:08 +00:00
|
|
|
}
|
2005-11-22 12:26:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-08-01 13:24:58 +00:00
|
|
|
// Apply just the prefs that can change in the Preferences dialog
|
|
|
|
void document_apply_update_prefs(ScintillaObject *sci)
|
|
|
|
{
|
|
|
|
sci_set_mark_long_lines(sci, app->long_line_type, app->long_line_column, app->long_line_color);
|
|
|
|
|
|
|
|
sci_set_tab_width(sci, app->pref_editor_tab_width);
|
2006-08-13 15:04:30 +00:00
|
|
|
sci_set_autoc_max_height(sci, app->autocompletion_max_height);
|
2006-08-01 13:24:58 +00:00
|
|
|
|
|
|
|
sci_set_indentionguides(sci, app->pref_editor_show_indent_guide);
|
|
|
|
sci_set_visible_white_spaces(sci, app->pref_editor_show_white_space);
|
|
|
|
sci_set_visible_eols(sci, app->pref_editor_show_line_endings);
|
|
|
|
|
|
|
|
sci_set_folding_margin_visible(sci, app->pref_editor_folding);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-10-09 16:08:53 +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_new_sci(). */
|
|
|
|
static void init_doc_struct(document *new_doc)
|
|
|
|
{
|
|
|
|
new_doc->is_valid = FALSE;
|
|
|
|
new_doc->has_tags = FALSE;
|
|
|
|
new_doc->use_auto_indention = app->pref_editor_use_auto_indention;
|
|
|
|
new_doc->line_breaking = app->pref_editor_line_breaking;
|
|
|
|
new_doc->readonly = FALSE;
|
|
|
|
new_doc->tag_store = NULL;
|
|
|
|
new_doc->tag_tree = NULL;
|
|
|
|
new_doc->file_name = NULL;
|
|
|
|
new_doc->file_type = NULL;
|
|
|
|
new_doc->tm_file = NULL;
|
|
|
|
new_doc->encoding = NULL;
|
|
|
|
new_doc->has_bom = FALSE;
|
|
|
|
new_doc->sci = NULL;
|
|
|
|
new_doc->undo_actions = NULL;
|
|
|
|
new_doc->redo_actions = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* returns the next free place(i.e. index) in the document list,
|
|
|
|
* or -1 if the current doc_array is full */
|
|
|
|
static gint document_get_new_idx()
|
|
|
|
{
|
|
|
|
guint i;
|
|
|
|
|
|
|
|
for(i = 0; i < doc_array->len; i++)
|
|
|
|
{
|
|
|
|
if (doc_list[i].sci == NULL)
|
|
|
|
{
|
|
|
|
return (gint) i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-11-22 12:26:26 +00:00
|
|
|
/* creates a new tab in the notebook and does all related stuff
|
|
|
|
* finally it returns the index of the created document */
|
2006-10-23 20:32:37 +00:00
|
|
|
static gint document_create_new_sci(const gchar *filename)
|
2005-11-22 12:26:26 +00:00
|
|
|
{
|
|
|
|
ScintillaObject *sci;
|
|
|
|
PangoFontDescription *pfd;
|
2006-12-05 10:37:36 +00:00
|
|
|
gchar *fname;
|
2006-09-07 15:51:24 +00:00
|
|
|
gint new_idx;
|
2006-07-22 01:29:10 +00:00
|
|
|
document *this;
|
2006-06-25 12:13:35 +00:00
|
|
|
gint tabnum;
|
2006-11-21 18:39:23 +00:00
|
|
|
gint cur_pages = gtk_notebook_get_n_pages(GTK_NOTEBOOK(app->notebook));
|
2006-05-09 22:02:22 +00:00
|
|
|
|
2006-11-22 16:07:18 +00:00
|
|
|
if (cur_pages == 1)
|
2006-09-07 15:51:24 +00:00
|
|
|
{
|
|
|
|
gint idx = document_get_cur_idx();
|
|
|
|
// remove the empty document and open a new one
|
|
|
|
if (doc_list[idx].file_name == NULL && ! doc_list[idx].changed) document_remove(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
new_idx = document_get_new_idx();
|
2006-10-09 16:08:53 +00:00
|
|
|
if (new_idx == -1) // expand the array, no free places
|
|
|
|
{
|
|
|
|
document new_doc;
|
|
|
|
init_doc_struct(&new_doc);
|
|
|
|
new_idx = doc_array->len;
|
|
|
|
g_array_append_val(doc_array, new_doc);
|
|
|
|
}
|
|
|
|
this = &doc_list[new_idx];
|
2006-07-22 01:29:10 +00:00
|
|
|
|
2005-11-22 12:26:26 +00:00
|
|
|
/* SCI - Code */
|
|
|
|
sci = SCINTILLA(scintilla_new());
|
2006-01-14 22:40:05 +00:00
|
|
|
scintilla_set_id(sci, new_idx);
|
2006-06-13 19:37:21 +00:00
|
|
|
this->sci = sci;
|
2006-05-09 22:02:22 +00:00
|
|
|
|
|
|
|
gtk_widget_show(GTK_WIDGET(sci));
|
|
|
|
|
2006-04-27 18:06:35 +00:00
|
|
|
sci_set_codepage(sci, SC_CP_UTF8);
|
2005-12-01 22:59:02 +00:00
|
|
|
//SSM(sci, SCI_SETWRAPSTARTINDENT, 4, 0);
|
2006-02-26 18:19:28 +00:00
|
|
|
// disable scintilla provided popup menu
|
2005-11-22 12:26:26 +00:00
|
|
|
sci_use_popup(sci, FALSE);
|
2006-02-10 20:56:17 +00:00
|
|
|
sci_assign_cmdkey(sci, SCK_HOME, SCI_VCHOMEWRAP);
|
2006-02-26 18:19:28 +00:00
|
|
|
sci_assign_cmdkey(sci, SCK_END, SCI_LINEENDWRAP);
|
2006-07-01 14:12:05 +00:00
|
|
|
// disable select all to be able to redefine it
|
|
|
|
sci_clear_cmdkey(sci, 'A' | (SCMOD_CTRL << 16));
|
2006-08-01 13:24:58 +00:00
|
|
|
|
|
|
|
document_apply_update_prefs(sci);
|
|
|
|
|
2005-11-22 12:26:26 +00:00
|
|
|
sci_set_symbol_margin(sci, app->show_markers_margin);
|
2006-05-09 22:02:22 +00:00
|
|
|
sci_set_line_numbers(sci, app->show_linenumber_margin, 0);
|
2006-02-14 22:07:55 +00:00
|
|
|
sci_set_lines_wrapped(sci, app->pref_editor_line_breaking);
|
2006-08-02 10:50:53 +00:00
|
|
|
|
2006-12-05 10:37:36 +00:00
|
|
|
// signal for insert-key(works without too, but to update the right status bar)
|
|
|
|
/* g_signal_connect((GtkWidget*) sci, "key-press-event",
|
|
|
|
G_CALLBACK(keybindings_got_event), GINT_TO_POINTER(new_idx));
|
|
|
|
*/ // signal for the popup menu
|
|
|
|
g_signal_connect((GtkWidget*) sci, "button-press-event",
|
|
|
|
G_CALLBACK(on_editor_button_press_event), GINT_TO_POINTER(new_idx));
|
|
|
|
|
2005-11-22 12:26:26 +00:00
|
|
|
pfd = pango_font_description_from_string(app->editor_font);
|
|
|
|
fname = g_strdup_printf("!%s", pango_font_description_get_family(pfd));
|
2006-06-13 19:37:21 +00:00
|
|
|
document_set_font(new_idx, fname, pango_font_description_get_size(pfd) / PANGO_SCALE);
|
2005-11-22 12:26:26 +00:00
|
|
|
pango_font_description_free(pfd);
|
|
|
|
g_free(fname);
|
|
|
|
|
2006-06-13 19:37:21 +00:00
|
|
|
this->tag_store = NULL;
|
|
|
|
this->tag_tree = NULL;
|
2005-12-18 22:08:42 +00:00
|
|
|
|
2005-11-22 12:26:26 +00:00
|
|
|
// store important pointers in the tab list
|
2006-06-13 19:37:21 +00:00
|
|
|
this->file_name = (filename) ? g_strdup(filename) : NULL;
|
|
|
|
this->encoding = NULL;
|
2006-10-24 13:41:34 +00:00
|
|
|
this->saved_encoding.encoding = NULL;
|
|
|
|
this->saved_encoding.has_bom = FALSE;
|
2006-06-13 19:37:21 +00:00
|
|
|
this->tm_file = NULL;
|
|
|
|
this->file_type = NULL;
|
|
|
|
this->mtime = 0;
|
|
|
|
this->changed = FALSE;
|
|
|
|
this->last_check = time(NULL);
|
|
|
|
this->readonly = FALSE;
|
2006-08-01 10:06:28 +00:00
|
|
|
this->line_breaking = app->pref_editor_line_breaking;
|
|
|
|
this->use_auto_indention = app->pref_editor_use_auto_indention;
|
2006-06-13 19:37:21 +00:00
|
|
|
this->has_tags = FALSE;
|
2005-12-18 22:08:42 +00:00
|
|
|
|
2006-12-05 10:37:36 +00:00
|
|
|
treeviews_openfiles_add(new_idx); // sets this->iter
|
|
|
|
|
|
|
|
tabnum = notebook_new_tab(new_idx);
|
|
|
|
gtk_notebook_set_current_page(GTK_NOTEBOOK(app->notebook), tabnum);
|
|
|
|
|
|
|
|
ui_close_buttons_toggle();
|
|
|
|
|
|
|
|
this->is_valid = TRUE; // do this last to prevent UI updating with NULL items.
|
2006-10-09 16:08:53 +00:00
|
|
|
g_assert(doc_list[new_idx].sci == sci);
|
2005-11-22 12:26:26 +00:00
|
|
|
return new_idx;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-01-14 22:40:05 +00:00
|
|
|
/* removes the given notebook tab and clears the related entry in the document list */
|
2006-01-11 18:38:17 +00:00
|
|
|
gboolean document_remove(guint page_num)
|
2005-11-22 12:26:26 +00:00
|
|
|
{
|
|
|
|
gint idx = document_get_n_idx(page_num);
|
|
|
|
|
2006-10-09 16:08:53 +00:00
|
|
|
if (DOC_IDX_VALID(idx))
|
2005-11-22 12:26:26 +00:00
|
|
|
{
|
|
|
|
if (doc_list[idx].changed && ! dialogs_show_unsaved_file(idx))
|
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
2006-11-22 16:07:18 +00:00
|
|
|
notebook_remove_page(page_num);
|
2006-12-05 10:37:36 +00:00
|
|
|
treeviews_remove_document(idx);
|
2006-11-25 12:32:22 +00:00
|
|
|
msgwin_status_add(_("File %s closed."), DOC_FILENAME(idx));
|
2005-11-22 12:26:26 +00:00
|
|
|
g_free(doc_list[idx].encoding);
|
2006-10-24 13:41:34 +00:00
|
|
|
g_free(doc_list[idx].saved_encoding.encoding);
|
2005-11-22 12:26:26 +00:00
|
|
|
g_free(doc_list[idx].file_name);
|
|
|
|
tm_workspace_remove_object(doc_list[idx].tm_file, TRUE);
|
2006-01-11 18:38:17 +00:00
|
|
|
doc_list[idx].is_valid = FALSE;
|
2005-11-22 12:26:26 +00:00
|
|
|
doc_list[idx].sci = NULL;
|
|
|
|
doc_list[idx].file_name = NULL;
|
|
|
|
doc_list[idx].file_type = NULL;
|
2006-04-27 18:06:35 +00:00
|
|
|
doc_list[idx].encoding = NULL;
|
2006-07-23 15:45:05 +00:00
|
|
|
doc_list[idx].has_bom = FALSE;
|
2005-11-22 12:26:26 +00:00
|
|
|
doc_list[idx].tm_file = NULL;
|
2006-09-06 18:26:08 +00:00
|
|
|
document_undo_clear(idx);
|
2005-11-22 12:26:26 +00:00
|
|
|
if (gtk_notebook_get_n_pages(GTK_NOTEBOOK(app->notebook)) == 0)
|
|
|
|
{
|
2006-09-05 14:24:47 +00:00
|
|
|
ui_update_tag_list(-1, FALSE);
|
2005-11-22 12:26:26 +00:00
|
|
|
//on_notebook1_switch_page(GTK_NOTEBOOK(app->notebook), NULL, 0, NULL);
|
2006-09-05 14:24:47 +00:00
|
|
|
ui_set_window_title(-1);
|
|
|
|
ui_save_buttons_toggle(FALSE);
|
|
|
|
ui_close_buttons_toggle();
|
2006-10-18 19:35:42 +00:00
|
|
|
build_menu_update(-1);
|
2005-11-22 12:26:26 +00:00
|
|
|
}
|
|
|
|
}
|
2006-01-14 22:40:05 +00:00
|
|
|
else geany_debug("Error: idx: %d page_num: %d", idx, page_num);
|
|
|
|
|
2005-11-22 12:26:26 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-10-24 13:41:34 +00:00
|
|
|
// used to keep a record of the unchanged document state encoding
|
|
|
|
static void store_saved_encoding(gint idx)
|
|
|
|
{
|
|
|
|
g_free(doc_list[idx].saved_encoding.encoding);
|
|
|
|
doc_list[idx].saved_encoding.encoding = g_strdup(doc_list[idx].encoding);
|
|
|
|
doc_list[idx].saved_encoding.has_bom = doc_list[idx].has_bom;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-11-22 12:26:26 +00:00
|
|
|
/* This creates a new document, by clearing the text widget and setting the
|
|
|
|
current filename to NULL. */
|
|
|
|
void document_new_file(filetype *ft)
|
|
|
|
{
|
2006-10-09 16:08:53 +00:00
|
|
|
gint idx = document_create_new_sci(NULL);
|
2006-11-14 16:03:25 +00:00
|
|
|
gchar *template = templates_get_template_new_file(ft);
|
2005-11-22 12:26:26 +00:00
|
|
|
|
2006-10-09 16:08:53 +00:00
|
|
|
g_assert(idx != -1);
|
2006-07-22 01:29:10 +00:00
|
|
|
|
2006-10-10 16:02:41 +00:00
|
|
|
sci_set_undo_collection(doc_list[idx].sci, FALSE); // avoid creation of an undo action
|
2006-10-09 16:08:53 +00:00
|
|
|
sci_clear_all(doc_list[idx].sci);
|
|
|
|
sci_set_text(doc_list[idx].sci, template);
|
|
|
|
g_free(template);
|
2005-11-22 12:26:26 +00:00
|
|
|
|
2006-10-10 16:02:41 +00:00
|
|
|
#ifdef G_OS_WIN32
|
|
|
|
sci_set_eol_mode(doc_list[idx].sci, SC_EOL_CRLF);
|
|
|
|
#else
|
|
|
|
sci_set_eol_mode(doc_list[idx].sci, SC_EOL_LF);
|
|
|
|
#endif
|
|
|
|
sci_set_undo_collection(doc_list[idx].sci, TRUE);
|
|
|
|
sci_empty_undo_buffer(doc_list[idx].sci);
|
|
|
|
|
2006-10-09 16:08:53 +00:00
|
|
|
doc_list[idx].encoding = g_strdup(encodings[app->pref_editor_default_encoding].charset);
|
2006-10-24 13:41:34 +00:00
|
|
|
// store the opened encoding for undo/redo
|
|
|
|
store_saved_encoding(idx);
|
|
|
|
|
2006-10-09 16:08:53 +00:00
|
|
|
//document_set_filetype(idx, (ft == NULL) ? filetypes[GEANY_FILETYPES_ALL] : ft);
|
|
|
|
document_set_filetype(idx, ft); // also clears taglist
|
|
|
|
if (ft == NULL) filetypes[GEANY_FILETYPES_ALL]->style_func_ptr(doc_list[idx].sci);
|
|
|
|
ui_set_window_title(idx);
|
2006-10-18 19:35:42 +00:00
|
|
|
build_menu_update(idx);
|
2006-10-09 16:08:53 +00:00
|
|
|
doc_list[idx].mtime = time(NULL);
|
|
|
|
doc_list[idx].changed = FALSE;
|
|
|
|
document_set_text_changed(idx);
|
|
|
|
ui_document_show_hide(idx); //update the document menu
|
2006-10-10 16:02:41 +00:00
|
|
|
|
2006-10-09 16:08:53 +00:00
|
|
|
sci_set_line_numbers(doc_list[idx].sci, app->show_linenumber_margin, 0);
|
|
|
|
sci_goto_pos(doc_list[idx].sci, 0, TRUE);
|
2005-11-22 12:26:26 +00:00
|
|
|
|
2006-10-09 16:08:53 +00:00
|
|
|
// "the" SCI signal (connect after initial setup(i.e. adding text))
|
|
|
|
g_signal_connect((GtkWidget*) doc_list[idx].sci, "sci-notify",
|
|
|
|
G_CALLBACK(on_editor_notification), GINT_TO_POINTER(idx));
|
2006-09-06 16:09:08 +00:00
|
|
|
|
2006-10-09 16:08:53 +00:00
|
|
|
msgwin_status_add(_("New file opened."));
|
2005-11-22 12:26:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-09-08 15:05:32 +00:00
|
|
|
// reload file with specified encoding
|
|
|
|
static gboolean
|
|
|
|
handle_forced_encoding(gchar **data, gsize *size, const gchar *forced_enc, gchar **enc,
|
|
|
|
gboolean *bom)
|
|
|
|
{
|
2006-12-07 16:09:45 +00:00
|
|
|
if (utils_str_equal(forced_enc, "UTF-8"))
|
2006-09-08 15:05:32 +00:00
|
|
|
{
|
|
|
|
if (! g_utf8_validate(*data, *size, NULL))
|
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2006-12-07 16:09:45 +00:00
|
|
|
*bom = utils_str_equal(utils_scan_unicode_bom(*data), "UTF-8");
|
2006-09-08 15:05:32 +00:00
|
|
|
*enc = g_strdup(forced_enc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2006-10-25 14:48:07 +00:00
|
|
|
gchar *converted_text = encodings_convert_to_utf8_from_charset(
|
|
|
|
*data, *size, forced_enc, FALSE);
|
2006-09-08 15:05:32 +00:00
|
|
|
if (converted_text == NULL)
|
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
g_free(*data);
|
|
|
|
*data = (void*)converted_text;
|
|
|
|
*size = strlen(converted_text);
|
2006-12-07 16:09:45 +00:00
|
|
|
*bom = utils_str_equal(utils_scan_unicode_bom(*data), "UTF-8");
|
2006-09-08 15:05:32 +00:00
|
|
|
*enc = g_strdup(forced_enc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
handle_encoding(gchar **data, gsize *size, gchar **enc, gboolean *bom)
|
|
|
|
{
|
|
|
|
if (*size > 0)
|
|
|
|
{ // the usual way to detect encoding and convert to UTF-8
|
|
|
|
if (*size >= 4)
|
|
|
|
{
|
|
|
|
*enc = utils_scan_unicode_bom(*data);
|
|
|
|
}
|
|
|
|
if (*enc != NULL)
|
|
|
|
{
|
|
|
|
*bom = TRUE;
|
|
|
|
if ((*enc)[4] != '8') // the BOM indicated something else than UTF-8
|
|
|
|
{
|
2006-10-25 14:48:07 +00:00
|
|
|
gchar *converted_text = encodings_convert_to_utf8_from_charset(
|
|
|
|
*data, *size, *enc, FALSE);
|
2006-09-08 15:05:32 +00:00
|
|
|
if (converted_text == NULL)
|
|
|
|
{
|
|
|
|
g_free(*enc);
|
|
|
|
*enc = NULL;
|
|
|
|
*bom = FALSE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
g_free(*data);
|
|
|
|
*data = (void*)converted_text;
|
|
|
|
*size = strlen(converted_text);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// this if is important, else doesn't work because enc can be altered in the above block
|
|
|
|
if (*enc == NULL)
|
|
|
|
{
|
|
|
|
if (g_utf8_validate(*data, *size, NULL))
|
|
|
|
{
|
|
|
|
*enc = g_strdup("UTF-8");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2006-10-25 14:48:07 +00:00
|
|
|
gchar *converted_text = encodings_convert_to_utf8(*data, *size, enc);
|
2006-09-08 15:05:32 +00:00
|
|
|
|
|
|
|
if (converted_text == NULL)
|
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
g_free(*data);
|
|
|
|
*data = (void*)converted_text;
|
|
|
|
*size = strlen(converted_text);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
*enc = g_strdup("UTF-8");
|
|
|
|
}
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
handle_bom(gchar **data)
|
|
|
|
{
|
|
|
|
gchar *data_without_bom;
|
|
|
|
data_without_bom = g_strdup(*data + 3);
|
|
|
|
g_free(*data);
|
|
|
|
*data = data_without_bom;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-11-22 12:26:26 +00:00
|
|
|
/* If idx is set to -1, it creates a new tab, opens the file from filename and
|
|
|
|
* set the cursor to pos.
|
|
|
|
* If idx is greater than -1, it reloads the file in the tab corresponding to
|
|
|
|
* idx and set the cursor to position 0. In this case, filename should be NULL
|
2006-06-11 22:39:11 +00:00
|
|
|
* It returns the idx of the opened file or -1 if an error occurred.
|
2005-11-22 12:26:26 +00:00
|
|
|
*/
|
2006-07-22 01:29:10 +00:00
|
|
|
int document_open_file(gint idx, const gchar *filename, gint pos, gboolean readonly, filetype *ft,
|
|
|
|
const gchar *forced_enc)
|
2005-11-22 12:26:26 +00:00
|
|
|
{
|
2006-05-17 18:29:29 +00:00
|
|
|
gint editor_mode;
|
|
|
|
gsize size;
|
2005-11-22 12:26:26 +00:00
|
|
|
gboolean reload = (idx == -1) ? FALSE : TRUE;
|
|
|
|
struct stat st;
|
2006-07-22 01:29:10 +00:00
|
|
|
gboolean bom = FALSE;
|
2005-11-22 12:26:26 +00:00
|
|
|
gchar *enc = NULL;
|
2006-01-23 17:05:29 +00:00
|
|
|
gchar *utf8_filename = NULL;
|
2006-02-06 06:26:53 +00:00
|
|
|
gchar *locale_filename = NULL;
|
2006-01-23 17:05:29 +00:00
|
|
|
GError *err = NULL;
|
2006-05-16 19:06:08 +00:00
|
|
|
gchar *data = NULL;
|
|
|
|
|
2005-11-22 12:26:26 +00:00
|
|
|
//struct timeval tv, tv1;
|
|
|
|
//struct timezone tz;
|
|
|
|
//gettimeofday(&tv, &tz);
|
|
|
|
|
|
|
|
if (reload)
|
|
|
|
{
|
2006-02-06 06:26:53 +00:00
|
|
|
utf8_filename = g_strdup(doc_list[idx].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
|
|
|
|
{
|
2006-04-27 18:06:35 +00:00
|
|
|
// filename must not be NULL when it is a new file
|
|
|
|
if (filename == NULL)
|
|
|
|
{
|
|
|
|
msgwin_status_add(_("Invalid filename"));
|
2006-06-11 22:39:11 +00:00
|
|
|
return -1;
|
2006-04-27 18:06:35 +00:00
|
|
|
}
|
2006-05-08 01:57:25 +00:00
|
|
|
|
2006-01-23 17:05:29 +00:00
|
|
|
// try to get the UTF-8 equivalent for the filename, fallback to filename if error
|
2006-02-06 06:26:53 +00:00
|
|
|
locale_filename = g_strdup(filename);
|
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
|
|
|
|
2005-11-22 12:26:26 +00:00
|
|
|
// if file is already open, switch to it and go
|
2006-06-07 21:24:15 +00:00
|
|
|
idx = document_find_by_filename(utf8_filename, FALSE);
|
2005-11-22 12:26:26 +00:00
|
|
|
if (idx >= 0)
|
|
|
|
{
|
2006-09-09 12:03:11 +00:00
|
|
|
ui_add_recent_file(utf8_filename); // either add or reorder recent item
|
2005-11-22 12:26:26 +00:00
|
|
|
gtk_notebook_set_current_page(GTK_NOTEBOOK(app->notebook),
|
|
|
|
gtk_notebook_page_num(GTK_NOTEBOOK(app->notebook),
|
|
|
|
(GtkWidget*) doc_list[idx].sci));
|
2006-06-11 22:39:11 +00:00
|
|
|
sci_goto_pos(doc_list[idx].sci, pos, TRUE);
|
2006-02-06 06:26:53 +00:00
|
|
|
g_free(utf8_filename);
|
|
|
|
g_free(locale_filename);
|
2006-06-11 22:39:11 +00:00
|
|
|
return idx;
|
2005-11-22 12:26:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-02-06 06:26:53 +00:00
|
|
|
if (stat(locale_filename, &st) != 0)
|
2005-11-22 12:26:26 +00:00
|
|
|
{
|
2006-01-23 17:05:29 +00:00
|
|
|
msgwin_status_add(_("Could not open file %s (%s)"), utf8_filename, g_strerror(errno));
|
2006-02-06 06:26:53 +00:00
|
|
|
g_free(utf8_filename);
|
|
|
|
g_free(locale_filename);
|
2006-06-11 22:39:11 +00:00
|
|
|
return -1;
|
2005-11-22 12:26:26 +00:00
|
|
|
}
|
2006-05-16 19:06:08 +00:00
|
|
|
|
2006-07-26 17:02:16 +00:00
|
|
|
#ifdef G_OS_WIN32
|
2006-10-25 14:38:48 +00:00
|
|
|
if (! g_file_get_contents(utf8_filename, &data, NULL, &err))
|
2005-12-29 20:01:18 +00:00
|
|
|
#else
|
2006-10-25 14:38:48 +00:00
|
|
|
if (! g_file_get_contents(locale_filename, &data, NULL, &err))
|
2006-05-16 19:06:08 +00:00
|
|
|
#endif
|
2006-02-20 22:57:59 +00:00
|
|
|
{
|
2006-05-16 19:06:08 +00:00
|
|
|
msgwin_status_add(err->message);
|
2006-04-27 18:06:35 +00:00
|
|
|
g_error_free(err);
|
2006-02-06 06:26:53 +00:00
|
|
|
g_free(utf8_filename);
|
|
|
|
g_free(locale_filename);
|
2006-06-11 22:39:11 +00:00
|
|
|
return -1;
|
2005-12-29 20:01:18 +00:00
|
|
|
}
|
2005-11-22 12:26:26 +00:00
|
|
|
|
2006-10-25 14:38:48 +00:00
|
|
|
/* check whether the size of the loaded data is equal to the size of the file in the filesystem */
|
|
|
|
//size = strlen(data);
|
|
|
|
size = strlen(data);
|
2006-10-30 11:49:35 +00:00
|
|
|
if (size != (gsize) st.st_size)
|
2006-10-25 14:38:48 +00:00
|
|
|
{
|
|
|
|
gchar *warn_msg = _("The file \"%s\" could not opened properly and probably was truncated. "
|
2006-10-25 19:25:35 +00:00
|
|
|
"Be aware that saving it can cause data loss.\nThe file was set to read-only.");
|
2006-10-25 14:38:48 +00:00
|
|
|
|
|
|
|
if (app->main_window_realized)
|
|
|
|
dialogs_show_msgbox(GTK_MESSAGE_WARNING, warn_msg, utf8_filename);
|
2006-11-15 23:12:13 +00:00
|
|
|
|
2006-10-25 14:38:48 +00:00
|
|
|
msgwin_status_add(warn_msg, utf8_filename);
|
2006-11-15 23:12:13 +00:00
|
|
|
|
2006-10-25 14:38:48 +00:00
|
|
|
// set the file to read-only mode because saving it is probably dangerous
|
|
|
|
readonly = TRUE;
|
|
|
|
}
|
|
|
|
|
2006-10-23 00:05:42 +00:00
|
|
|
/* Determine character encoding and convert to UTF-8 */
|
|
|
|
if (forced_enc != NULL)
|
2006-09-08 15:05:32 +00:00
|
|
|
{
|
2006-10-23 00:05:42 +00:00
|
|
|
// the encoding should be ignored(requested by user), so open the file "as it is"
|
2006-12-07 16:09:45 +00:00
|
|
|
if (utils_str_equal(forced_enc, encodings[GEANY_ENCODING_NONE].charset))
|
2006-10-23 00:05:42 +00:00
|
|
|
{
|
|
|
|
bom = FALSE;
|
2006-10-23 20:32:37 +00:00
|
|
|
enc = g_strdup(encodings[GEANY_ENCODING_NONE].charset);
|
2006-10-23 00:05:42 +00:00
|
|
|
}
|
|
|
|
else if (! handle_forced_encoding(&data, &size, forced_enc, &enc, &bom))
|
2006-07-22 01:29:10 +00:00
|
|
|
{
|
2006-09-08 15:05:32 +00:00
|
|
|
msgwin_status_add(_("The file \"%s\" is not valid %s."), utf8_filename, forced_enc);
|
|
|
|
utils_beep();
|
|
|
|
g_free(data);
|
|
|
|
g_free(utf8_filename);
|
|
|
|
g_free(locale_filename);
|
|
|
|
return -1;
|
2005-11-22 12:26:26 +00:00
|
|
|
}
|
|
|
|
}
|
2006-09-08 15:05:32 +00:00
|
|
|
else if (! handle_encoding(&data, &size, &enc, &bom))
|
2006-07-22 01:29:10 +00:00
|
|
|
{
|
2006-09-08 15:05:32 +00:00
|
|
|
msgwin_status_add(
|
|
|
|
_("The file \"%s\" does not look like a text file or the file encoding is not supported."),
|
|
|
|
utf8_filename);
|
|
|
|
utils_beep();
|
2006-07-22 01:29:10 +00:00
|
|
|
g_free(data);
|
2006-09-08 15:05:32 +00:00
|
|
|
g_free(utf8_filename);
|
|
|
|
g_free(locale_filename);
|
|
|
|
return -1;
|
2006-07-22 01:29:10 +00:00
|
|
|
}
|
|
|
|
|
2006-09-08 15:05:32 +00:00
|
|
|
if (bom) handle_bom(&data);
|
|
|
|
|
2006-01-23 17:05:29 +00:00
|
|
|
if (! reload) idx = document_create_new_sci(utf8_filename);
|
2006-07-22 01:29:10 +00:00
|
|
|
if (idx == -1) return -1; // really should not happen
|
2005-11-22 12:26:26 +00:00
|
|
|
|
2006-05-25 12:57:12 +00:00
|
|
|
// set editor mode and add the text to the ScintillaObject
|
2006-10-10 16:02:41 +00:00
|
|
|
sci_set_undo_collection(doc_list[idx].sci, FALSE); // avoid creation of an undo action
|
|
|
|
sci_empty_undo_buffer(doc_list[idx].sci);
|
|
|
|
sci_set_text(doc_list[idx].sci, data); // NULL terminated data
|
|
|
|
|
2006-05-16 19:06:08 +00:00
|
|
|
editor_mode = utils_get_line_endings(data, size);
|
2005-11-22 12:26:26 +00:00
|
|
|
sci_set_eol_mode(doc_list[idx].sci, editor_mode);
|
2006-02-14 22:07:55 +00:00
|
|
|
sci_set_line_numbers(doc_list[idx].sci, app->show_linenumber_margin, 0);
|
2006-10-10 16:02:41 +00:00
|
|
|
|
|
|
|
sci_set_undo_collection(doc_list[idx].sci, TRUE);
|
|
|
|
|
|
|
|
doc_list[idx].mtime = st.st_mtime; // get the modification time from file and keep it
|
2005-11-22 12:26:26 +00:00
|
|
|
doc_list[idx].changed = FALSE;
|
2006-01-23 17:05:29 +00:00
|
|
|
doc_list[idx].file_name = g_strdup(utf8_filename);
|
2006-07-23 13:14:23 +00:00
|
|
|
doc_list[idx].encoding = enc;
|
2006-07-23 15:45:05 +00:00
|
|
|
doc_list[idx].has_bom = bom;
|
2006-10-24 13:41:34 +00:00
|
|
|
store_saved_encoding(idx); // store the opened encoding for undo/redo
|
2006-06-20 11:55:34 +00:00
|
|
|
|
2006-10-03 13:16:42 +00:00
|
|
|
if (cl_options.goto_line >= 0)
|
|
|
|
{ // goto line which was specified on command line and then undefine the line
|
2006-11-03 15:09:13 +00:00
|
|
|
sci_goto_line(doc_list[idx].sci, cl_options.goto_line - 1, TRUE);
|
|
|
|
sci_scroll_to_line(doc_list[idx].sci, -1, 0.5);
|
2006-10-03 13:16:42 +00:00
|
|
|
cl_options.goto_line = -1;
|
|
|
|
}
|
|
|
|
else if (pos >= 0)
|
|
|
|
sci_goto_pos(doc_list[idx].sci, pos, TRUE);
|
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
|
|
|
{
|
2006-10-06 23:23:05 +00:00
|
|
|
filetype *use_ft = (ft != NULL) ? ft : filetypes_get_from_filename(idx);
|
2006-05-08 01:57:25 +00:00
|
|
|
|
2006-07-24 20:23:56 +00:00
|
|
|
doc_list[idx].readonly = readonly;
|
|
|
|
sci_set_readonly(doc_list[idx].sci, readonly);
|
2006-05-08 01:57:25 +00:00
|
|
|
|
2006-09-30 16:15:45 +00:00
|
|
|
document_set_filetype(idx, use_ft); // also sets taglist
|
2006-10-18 19:35:42 +00:00
|
|
|
build_menu_update(idx);
|
2006-10-10 13:52:22 +00:00
|
|
|
|
|
|
|
// "the" SCI signal (connect after initial setup(i.e. adding text))
|
|
|
|
g_signal_connect((GtkWidget*) doc_list[idx].sci, "sci-notify",
|
|
|
|
G_CALLBACK(on_editor_notification), GINT_TO_POINTER(idx));
|
2006-06-20 14:54:09 +00:00
|
|
|
}
|
2006-06-29 17:14:52 +00:00
|
|
|
else
|
2006-09-30 16:15:45 +00:00
|
|
|
{ // reloading
|
2006-06-26 16:19:28 +00:00
|
|
|
document_update_tag_list(idx, TRUE);
|
2006-10-10 16:02:41 +00:00
|
|
|
document_undo_clear(idx);
|
2006-06-26 16:19:28 +00:00
|
|
|
}
|
2006-06-29 17:14:52 +00:00
|
|
|
|
2006-12-05 10:37:36 +00:00
|
|
|
document_set_text_changed(idx); // also updates tab state
|
|
|
|
ui_document_show_hide(idx); // update the document menu
|
2005-11-22 12:26:26 +00:00
|
|
|
|
2006-05-16 19:06:08 +00:00
|
|
|
g_free(data);
|
|
|
|
|
2005-11-22 12:26:26 +00:00
|
|
|
|
2005-12-21 19:43:48 +00:00
|
|
|
// finally add current file to recent files menu, but not the files from the last session
|
2006-09-05 14:24:47 +00:00
|
|
|
if (! app->opening_session_files) ui_add_recent_file(utf8_filename);
|
2006-01-23 17:05:29 +00:00
|
|
|
|
2006-06-26 15:49:07 +00:00
|
|
|
if (reload)
|
|
|
|
msgwin_status_add(_("File %s reloaded."), utf8_filename);
|
|
|
|
else
|
|
|
|
msgwin_status_add(_("File %s opened(%d%s)."),
|
|
|
|
utf8_filename, gtk_notebook_get_n_pages(GTK_NOTEBOOK(app->notebook)),
|
|
|
|
(readonly) ? _(", read-only") : "");
|
|
|
|
|
2006-01-23 17:05:29 +00:00
|
|
|
g_free(utf8_filename);
|
2006-02-06 06:26:53 +00:00
|
|
|
g_free(locale_filename);
|
2005-11-22 12:26:26 +00:00
|
|
|
//gettimeofday(&tv1, &tz);
|
|
|
|
//geany_debug("%s: %d", filename, (gint)(tv1.tv_usec - tv.tv_usec));
|
2006-06-11 22:39:11 +00:00
|
|
|
|
|
|
|
return idx;
|
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;
|
|
|
|
|
|
|
|
if (data == NULL) return;
|
|
|
|
|
2006-11-17 12:19:31 +00:00
|
|
|
if (length < 0)
|
2006-11-15 23:12:13 +00:00
|
|
|
length = strlen(data);
|
|
|
|
|
|
|
|
switch (utils_get_line_endings((gchar*) data, length))
|
|
|
|
{
|
|
|
|
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++)
|
|
|
|
{
|
|
|
|
if (list[i] == NULL) break;
|
|
|
|
filename = g_filename_from_uri(list[i], NULL, NULL);
|
|
|
|
if (filename == NULL) continue;
|
|
|
|
document_open_file(-1, filename, 0, FALSE, NULL, NULL);
|
|
|
|
g_free(filename);
|
|
|
|
}
|
|
|
|
|
|
|
|
g_strfreev(list);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-07-22 01:29:10 +00:00
|
|
|
gint document_reload_file(gint idx, const gchar *forced_enc)
|
2006-06-20 11:55:34 +00:00
|
|
|
{
|
|
|
|
gint pos = 0;
|
2006-07-22 01:29:10 +00:00
|
|
|
|
2006-06-20 11:55:34 +00:00
|
|
|
if (idx < 0 || ! doc_list[idx].is_valid)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
// try to set the cursor to the position before reloading
|
|
|
|
pos = sci_get_current_position(doc_list[idx].sci);
|
|
|
|
return document_open_file(idx, NULL, pos, doc_list[idx].readonly,
|
2006-07-22 01:29:10 +00:00
|
|
|
doc_list[idx].file_type, forced_enc);
|
2006-06-20 11:55:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-12-09 13:07:23 +00:00
|
|
|
static gboolean document_update_timestamp(gint idx)
|
|
|
|
{
|
|
|
|
struct stat st;
|
|
|
|
gchar *locale_filename;
|
|
|
|
|
|
|
|
g_return_val_if_fail(DOC_IDX_VALID(idx), FALSE);
|
|
|
|
|
|
|
|
locale_filename = utils_get_locale_from_utf8(doc_list[idx].file_name);
|
|
|
|
|
|
|
|
if (stat(locale_filename, &st) != 0)
|
|
|
|
{
|
|
|
|
msgwin_status_add(_("Could not open file %s (%s)"), doc_list[idx].file_name,
|
|
|
|
g_strerror(errno));
|
|
|
|
g_free(locale_filename);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
doc_list[idx].mtime = st.st_mtime; // get the modification time from file and keep it
|
|
|
|
g_free(locale_filename);
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-08-02 10:50:53 +00:00
|
|
|
/* This saves the file.
|
2006-09-05 18:33:48 +00:00
|
|
|
* When force is set then it is always saved, even if it is unchanged(useful when using Save As)
|
|
|
|
* It returns whether the file could be saved or not. */
|
|
|
|
gboolean document_save_file(gint idx, gboolean force)
|
2005-11-22 12:26:26 +00:00
|
|
|
{
|
|
|
|
gchar *data;
|
|
|
|
FILE *fp;
|
|
|
|
gint bytes_written, len;
|
2006-02-10 20:56:17 +00:00
|
|
|
gchar *locale_filename = NULL;
|
2005-11-22 12:26:26 +00:00
|
|
|
|
2006-10-23 20:32:37 +00:00
|
|
|
if (! DOC_IDX_VALID(idx)) return FALSE;
|
|
|
|
// the changed flag should exclude the readonly flag, but check it anyway for safety
|
|
|
|
if (! force && (! doc_list[idx].changed || doc_list[idx].readonly)) return FALSE;
|
2005-11-22 12:26:26 +00:00
|
|
|
|
|
|
|
if (doc_list[idx].file_name == NULL)
|
|
|
|
{
|
|
|
|
msgwin_status_add(_("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
|
|
|
}
|
|
|
|
|
2006-06-21 18:54:07 +00:00
|
|
|
// replaces tabs by spaces
|
2006-07-17 10:42:26 +00:00
|
|
|
if (app->pref_editor_replace_tabs) document_replace_tabs(idx);
|
2005-11-22 12:26:26 +00:00
|
|
|
// strip trailing spaces
|
2006-07-17 10:42:26 +00:00
|
|
|
if (app->pref_editor_trail_space) document_strip_trailing_spaces(idx);
|
2005-11-22 12:26:26 +00:00
|
|
|
// ensure the file has a newline at the end
|
2006-07-17 10:42:26 +00:00
|
|
|
if (app->pref_editor_new_line) document_ensure_final_newline(idx);
|
2005-11-22 12:26:26 +00:00
|
|
|
// ensure there a really the same EOL chars
|
|
|
|
sci_convert_eols(doc_list[idx].sci, sci_get_eol_mode(doc_list[idx].sci));
|
|
|
|
|
|
|
|
len = sci_get_length(doc_list[idx].sci) + 1;
|
2006-07-23 15:45:05 +00:00
|
|
|
if (doc_list[idx].has_bom && utils_is_unicode_charset(doc_list[idx].encoding))
|
2006-07-22 01:29:10 +00:00
|
|
|
{
|
2006-07-23 13:14:23 +00:00
|
|
|
data = (gchar*) g_malloc(len + 3); // 3 chars for BOM
|
2006-07-22 01:29:10 +00:00
|
|
|
data[0] = 0xef;
|
|
|
|
data[1] = 0xbb;
|
|
|
|
data[2] = 0xbf;
|
|
|
|
sci_get_text(doc_list[idx].sci, len, data + 3);
|
|
|
|
len += 3;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2006-07-23 13:14:23 +00:00
|
|
|
data = (gchar*) g_malloc(len);
|
2006-07-22 01:29:10 +00:00
|
|
|
sci_get_text(doc_list[idx].sci, len, data);
|
|
|
|
}
|
2005-11-22 12:26:26 +00:00
|
|
|
|
2006-10-23 20:32:37 +00:00
|
|
|
// save in original encoding, skip when it is already UTF-8 or has the encoding "None"
|
2006-12-07 16:09:45 +00:00
|
|
|
if (doc_list[idx].encoding != NULL && ! utils_str_equal(doc_list[idx].encoding, "UTF-8") &&
|
|
|
|
! utils_str_equal(doc_list[idx].encoding, encodings[GEANY_ENCODING_NONE].charset))
|
2006-06-19 18:31:17 +00:00
|
|
|
{
|
|
|
|
GError *conv_error = NULL;
|
|
|
|
gchar* conv_file_contents = NULL;
|
2006-07-22 01:29:10 +00:00
|
|
|
gsize conv_len;
|
2006-06-28 19:27:19 +00:00
|
|
|
|
2006-06-19 18:31:17 +00:00
|
|
|
// try to convert it from UTF-8 to original encoding
|
2006-07-23 13:14:23 +00:00
|
|
|
conv_file_contents = g_convert(data, len-1, doc_list[idx].encoding, "UTF-8",
|
2006-07-22 01:29:10 +00:00
|
|
|
NULL, &conv_len, &conv_error);
|
2006-06-29 17:14:52 +00:00
|
|
|
|
2006-06-19 18:31:17 +00:00
|
|
|
if (conv_error != NULL)
|
|
|
|
{
|
2006-10-25 14:38:48 +00:00
|
|
|
dialogs_show_msgbox(GTK_MESSAGE_ERROR,
|
2006-07-22 01:29:10 +00:00
|
|
|
_("An error occurred while converting the file from UTF-8 in \"%s\". The file remains unsaved."
|
|
|
|
"\nError message: %s\n"),
|
2006-06-28 19:27:19 +00:00
|
|
|
doc_list[idx].encoding, conv_error->message);
|
|
|
|
geany_debug("encoding error: %s)", conv_error->message);
|
2006-06-21 18:54:07 +00:00
|
|
|
g_error_free(conv_error);
|
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
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
g_free(data);
|
|
|
|
data = conv_file_contents;
|
2006-07-22 01:29:10 +00:00
|
|
|
len = conv_len;
|
2006-06-19 18:31:17 +00:00
|
|
|
}
|
|
|
|
}
|
2006-07-23 13:14:23 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
len = strlen(data);
|
|
|
|
}
|
2006-08-13 08:36:52 +00:00
|
|
|
locale_filename = utils_get_locale_from_utf8(doc_list[idx].file_name);
|
2006-07-23 13:14:23 +00:00
|
|
|
fp = fopen(locale_filename, "wb"); // this should fix the windows \n problem
|
2006-08-13 08:36:52 +00:00
|
|
|
g_free(locale_filename);
|
2005-11-22 12:26:26 +00:00
|
|
|
if (fp == NULL)
|
|
|
|
{
|
|
|
|
msgwin_status_add(_("Error saving file (%s)."), strerror(errno));
|
2006-02-10 20:56:17 +00:00
|
|
|
utils_beep();
|
2005-11-22 12:26:26 +00:00
|
|
|
g_free(data);
|
2006-09-05 18:33:48 +00:00
|
|
|
return FALSE;
|
2005-11-22 12:26:26 +00:00
|
|
|
}
|
|
|
|
bytes_written = fwrite(data, sizeof (gchar), len, fp);
|
|
|
|
fclose (fp);
|
|
|
|
|
|
|
|
g_free(data);
|
|
|
|
|
|
|
|
if (len != bytes_written)
|
|
|
|
{
|
|
|
|
msgwin_status_add(_("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
|
|
|
}
|
|
|
|
|
2006-10-24 13:41:34 +00:00
|
|
|
// store the opened encoding for undo/redo
|
|
|
|
store_saved_encoding(idx);
|
|
|
|
|
2006-01-11 18:38:17 +00:00
|
|
|
// ignore the following things if we are quitting
|
2006-01-14 22:40:05 +00:00
|
|
|
if (! app->quitting)
|
2006-02-10 20:56:17 +00:00
|
|
|
{
|
|
|
|
gchar *basename = g_path_get_basename(doc_list[idx].file_name);
|
2006-01-14 22:40:05 +00:00
|
|
|
|
2006-02-10 20:56:17 +00:00
|
|
|
// set line numbers again, to reset the margin width, if
|
|
|
|
// there are more lines than before
|
2006-02-14 22:07:55 +00:00
|
|
|
sci_set_line_numbers(doc_list[idx].sci, app->show_linenumber_margin, 0);
|
2006-01-11 18:38:17 +00:00
|
|
|
sci_set_savepoint(doc_list[idx].sci);
|
2006-12-09 13:07:23 +00:00
|
|
|
|
|
|
|
/* stat the file to get the timestamp, otherwise on Windows the actual
|
|
|
|
* timestamp can be ahead of time(NULL) */
|
|
|
|
document_update_timestamp(idx);
|
|
|
|
|
2006-01-11 18:38:17 +00:00
|
|
|
if (doc_list[idx].file_type == NULL || doc_list[idx].file_type->id == GEANY_FILETYPES_ALL)
|
2006-08-26 17:12:55 +00:00
|
|
|
{
|
2006-10-06 23:23:05 +00:00
|
|
|
doc_list[idx].file_type = filetypes_get_from_filename(idx);
|
2006-08-26 17:12:55 +00:00
|
|
|
filetypes_select_radio_item(doc_list[idx].file_type);
|
|
|
|
}
|
2006-01-11 18:38:17 +00:00
|
|
|
document_set_filetype(idx, doc_list[idx].file_type);
|
|
|
|
tm_workspace_update(TM_WORK_OBJECT(app->tm_workspace), TRUE, TRUE, FALSE);
|
2006-02-10 20:56:17 +00:00
|
|
|
gtk_label_set_text(GTK_LABEL(doc_list[idx].tab_label), basename);
|
|
|
|
gtk_label_set_text(GTK_LABEL(doc_list[idx].tabmenu_label), basename);
|
2006-01-11 18:38:17 +00:00
|
|
|
msgwin_status_add(_("File %s saved."), doc_list[idx].file_name);
|
2006-09-05 14:24:47 +00:00
|
|
|
ui_update_statusbar(idx, -1);
|
2006-02-10 20:56:17 +00:00
|
|
|
g_free(basename);
|
2006-08-30 18:53:45 +00:00
|
|
|
#ifdef HAVE_VTE
|
|
|
|
vte_cwd(doc_list[idx].file_name);
|
|
|
|
#endif
|
|
|
|
|
2006-01-11 18:38:17 +00:00
|
|
|
}
|
2006-09-05 18:33:48 +00:00
|
|
|
return TRUE;
|
2005-11-22 12:26:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* special search function, used from the find entry in the toolbar */
|
2006-11-03 15:09:13 +00:00
|
|
|
void document_search_bar_find(gint idx, 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;
|
|
|
|
struct TextToFind ttf;
|
2005-11-22 12:26:26 +00:00
|
|
|
|
2006-07-18 17:43:22 +00:00
|
|
|
g_return_if_fail(text != NULL);
|
2006-10-21 10:50:45 +00:00
|
|
|
if (! DOC_IDX_VALID(idx) || ! *text) return;
|
2005-11-22 12:26:26 +00:00
|
|
|
|
2006-10-21 10:50:45 +00:00
|
|
|
start_pos = (inc) ? sci_get_selection_start(doc_list[idx].sci) :
|
|
|
|
sci_get_selection_end(doc_list[idx].sci); // equal if no selection
|
|
|
|
|
|
|
|
// search cursor to end
|
|
|
|
ttf.chrg.cpMin = start_pos;
|
|
|
|
ttf.chrg.cpMax = sci_get_length(doc_list[idx].sci);
|
|
|
|
ttf.lpstrText = (gchar *)text;
|
|
|
|
search_pos = sci_find_text(doc_list[idx].sci, flags, &ttf);
|
|
|
|
|
|
|
|
// if no match, search start to cursor
|
|
|
|
if (search_pos == -1)
|
|
|
|
{
|
|
|
|
ttf.chrg.cpMin = 0;
|
|
|
|
ttf.chrg.cpMax = start_pos + strlen(text);
|
|
|
|
search_pos = sci_find_text(doc_list[idx].sci, flags, &ttf);
|
2005-11-22 12:26:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (search_pos != -1)
|
|
|
|
{
|
2006-10-21 10:50:45 +00:00
|
|
|
sci_set_selection_start(doc_list[idx].sci, ttf.chrgText.cpMin);
|
|
|
|
sci_set_selection_end(doc_list[idx].sci, ttf.chrgText.cpMax);
|
2006-11-03 15:09:13 +00:00
|
|
|
sci_scroll_to_line(doc_list[idx].sci, -1, 0.3);
|
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
|
|
|
{
|
2006-11-25 16:33:38 +00:00
|
|
|
ui_set_statusbar(_("\"%s\" was not found."), text);
|
2005-11-22 12:26:26 +00:00
|
|
|
}
|
2006-10-21 10:50:45 +00:00
|
|
|
utils_beep();
|
|
|
|
sci_goto_pos(doc_list[idx].sci, start_pos, FALSE); // clear selection
|
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. */
|
2006-11-03 15:09:13 +00:00
|
|
|
gint document_find_text(gint idx, const gchar *text, gint flags, gboolean search_backwards,
|
|
|
|
gboolean scroll)
|
2005-11-22 12:26:26 +00:00
|
|
|
{
|
2006-11-18 15:47:28 +00:00
|
|
|
gint selection_end, selection_start, search_pos, first_visible_line;
|
2005-11-22 12:26:26 +00:00
|
|
|
|
2006-07-20 21:19:18 +00:00
|
|
|
g_return_val_if_fail(text != NULL, -1);
|
|
|
|
if (idx == -1 || ! *text) return -1;
|
2006-07-18 17:43:22 +00:00
|
|
|
// Sci doesn't support searching backwards with a regex
|
|
|
|
if (flags & SCFIND_REGEXP) search_backwards = FALSE;
|
2005-11-22 12:26:26 +00:00
|
|
|
|
2006-11-18 15:47:28 +00:00
|
|
|
first_visible_line = sci_get_first_visible_line(doc_list[idx].sci);
|
2006-11-21 18:39:23 +00:00
|
|
|
|
2006-07-20 21:19:18 +00:00
|
|
|
selection_start = sci_get_selection_start(doc_list[idx].sci);
|
|
|
|
selection_end = sci_get_selection_end(doc_list[idx].sci);
|
2005-11-22 12:26:26 +00:00
|
|
|
if ((selection_end - selection_start) > 0)
|
|
|
|
{ // there's a selection so go to the end
|
|
|
|
if (search_backwards)
|
2006-05-26 17:11:41 +00:00
|
|
|
sci_goto_pos(doc_list[idx].sci, selection_start, TRUE);
|
2005-11-22 12:26:26 +00:00
|
|
|
else
|
2006-05-26 17:11:41 +00:00
|
|
|
sci_goto_pos(doc_list[idx].sci, selection_end, TRUE);
|
2005-11-22 12:26:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sci_set_search_anchor(doc_list[idx].sci);
|
|
|
|
if (search_backwards)
|
|
|
|
search_pos = sci_search_prev(doc_list[idx].sci, flags, text);
|
|
|
|
else
|
|
|
|
search_pos = sci_search_next(doc_list[idx].sci, flags, text);
|
|
|
|
|
|
|
|
if (search_pos != -1)
|
|
|
|
{
|
2006-11-03 15:09:13 +00:00
|
|
|
if (scroll)
|
|
|
|
sci_scroll_to_line(doc_list[idx].sci, -1, 0.3);
|
2005-11-22 12:26:26 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2006-10-21 10:50:45 +00:00
|
|
|
gint sci_len = sci_get_length(doc_list[idx].sci);
|
|
|
|
|
|
|
|
// if we just searched the whole text, give up searching.
|
|
|
|
if ((selection_end == 0 && ! search_backwards) ||
|
|
|
|
(selection_end == sci_len && search_backwards))
|
|
|
|
{
|
2006-11-25 16:33:38 +00:00
|
|
|
ui_set_statusbar(_("\"%s\" was not found."), text);
|
2006-10-21 10:50:45 +00:00
|
|
|
utils_beep();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// we searched only part of the document, so ask whether to wraparound.
|
2006-11-18 15:47:28 +00:00
|
|
|
if (app->pref_main_suppress_search_dialogs ||
|
|
|
|
dialogs_show_question_full(GTK_STOCK_FIND, GTK_STOCK_CANCEL,
|
|
|
|
_("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
|
|
|
|
2006-10-21 10:50:45 +00:00
|
|
|
sci_goto_pos(doc_list[idx].sci, (search_backwards) ? sci_len : 0, TRUE);
|
2006-11-03 15:09:13 +00:00
|
|
|
ret = document_find_text(idx, text, flags, search_backwards, scroll);
|
2006-11-18 15:47:28 +00:00
|
|
|
if (ret == -1)
|
|
|
|
{ // return to original cursor position if not found
|
|
|
|
gint new_visible_line;
|
|
|
|
|
|
|
|
sci_goto_pos(doc_list[idx].sci, selection_start, FALSE);
|
|
|
|
new_visible_line = sci_get_first_visible_line(doc_list[idx].sci);
|
|
|
|
sci_scroll_lines(doc_list[idx].sci, first_visible_line - new_visible_line);
|
|
|
|
}
|
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 */
|
|
|
|
gint document_replace_text(gint idx, const gchar *find_text, const gchar *replace_text,
|
|
|
|
gint flags, gboolean search_backwards)
|
2005-11-22 12:26:26 +00:00
|
|
|
{
|
|
|
|
gint selection_end, selection_start, search_pos;
|
|
|
|
|
2006-10-22 14:56:05 +00:00
|
|
|
g_return_val_if_fail(find_text != NULL && replace_text != NULL, -1);
|
|
|
|
if (idx == -1 || ! *find_text) return -1;
|
|
|
|
|
2006-07-18 17:43:22 +00:00
|
|
|
// Sci doesn't support searching backwards with a regex
|
|
|
|
if (flags & SCFIND_REGEXP) search_backwards = FALSE;
|
2005-11-22 12:26:26 +00:00
|
|
|
|
2006-10-22 14:56:05 +00:00
|
|
|
selection_start = sci_get_selection_start(doc_list[idx].sci);
|
|
|
|
selection_end = sci_get_selection_end(doc_list[idx].sci);
|
2006-07-20 21:19:18 +00:00
|
|
|
if (selection_end == selection_start)
|
|
|
|
{
|
|
|
|
// no selection so just find the next match
|
2006-11-03 15:09:13 +00:00
|
|
|
document_find_text(idx, find_text, flags, search_backwards, TRUE);
|
2006-10-22 14:56:05 +00:00
|
|
|
return -1;
|
2005-11-22 12:26:26 +00:00
|
|
|
}
|
2006-07-20 21:19:18 +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)
|
2006-07-20 21:19:18 +00:00
|
|
|
sci_goto_pos(doc_list[idx].sci, selection_end, TRUE);
|
2005-11-22 12:26:26 +00:00
|
|
|
else
|
2006-07-20 21:19:18 +00:00
|
|
|
sci_goto_pos(doc_list[idx].sci, selection_start, TRUE);
|
|
|
|
|
2006-11-03 15:09:13 +00:00
|
|
|
search_pos = document_find_text(idx, find_text, flags, search_backwards, TRUE);
|
2006-07-20 21:19:18 +00:00
|
|
|
// return if the original selected text did not match (at the start of the selection)
|
2006-10-22 14:56:05 +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;
|
|
|
|
// search next/prev will select matching text, which we use to set the replace target
|
|
|
|
sci_target_from_selection(doc_list[idx].sci);
|
|
|
|
replace_len = sci_target_replace(doc_list[idx].sci, replace_text, flags & SCFIND_REGEXP);
|
2006-07-20 21:19:18 +00:00
|
|
|
// select the replacement - find text will skip past the selected text
|
2005-11-22 12:26:26 +00:00
|
|
|
sci_set_selection_start(doc_list[idx].sci, search_pos);
|
2006-07-18 14:09:01 +00:00
|
|
|
sci_set_selection_end(doc_list[idx].sci, search_pos + replace_len);
|
2005-11-22 12:26:26 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2006-07-20 21:19:18 +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
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-07-18 17:43:22 +00:00
|
|
|
/* Returns -1 if no text found or the new range endpoint after replacing. */
|
2006-07-18 14:09:01 +00:00
|
|
|
static gint
|
2006-07-18 17:43:22 +00:00
|
|
|
document_replace_range(gint idx, const gchar *find_text, const gchar *replace_text,
|
2006-09-02 23:28:34 +00:00
|
|
|
gint flags, gint start, gint end, gboolean escaped_chars)
|
2005-11-22 12:26:26 +00:00
|
|
|
{
|
2006-07-18 14:09:01 +00:00
|
|
|
gint search_pos;
|
2006-08-23 18:08:48 +00:00
|
|
|
gint count = 0;
|
2006-07-18 14:09:01 +00:00
|
|
|
gint find_len = 0, replace_len = 0;
|
|
|
|
gboolean match_found = FALSE;
|
2006-11-25 12:32:22 +00:00
|
|
|
gchar *escaped_find_text, *escaped_replace_text, *filename;
|
2006-07-18 14:09:01 +00:00
|
|
|
struct TextToFind ttf;
|
2005-11-22 12:26:26 +00:00
|
|
|
|
2006-07-18 14:09:01 +00:00
|
|
|
g_return_val_if_fail(find_text != NULL && replace_text != NULL, FALSE);
|
|
|
|
if (idx == -1 || ! *find_text) return FALSE;
|
2005-11-22 12:26:26 +00:00
|
|
|
|
2006-05-25 15:32:03 +00:00
|
|
|
sci_start_undo_action(doc_list[idx].sci);
|
2006-07-18 14:09:01 +00:00
|
|
|
ttf.chrg.cpMin = start;
|
|
|
|
ttf.chrg.cpMax = end;
|
|
|
|
ttf.lpstrText = (gchar*)find_text;
|
|
|
|
|
2006-05-25 15:32:03 +00:00
|
|
|
while (TRUE)
|
|
|
|
{
|
2006-07-18 14:09:01 +00:00
|
|
|
search_pos = sci_find_text(doc_list[idx].sci, flags, &ttf);
|
|
|
|
if (search_pos == -1) break;
|
|
|
|
find_len = ttf.chrgText.cpMax - ttf.chrgText.cpMin;
|
2005-11-22 12:26:26 +00:00
|
|
|
|
2006-07-18 14:09:01 +00:00
|
|
|
if (search_pos + find_len > end)
|
|
|
|
break; //found text is partly out of range
|
2006-03-10 00:52:09 +00:00
|
|
|
else
|
|
|
|
{
|
2006-07-18 14:09:01 +00:00
|
|
|
match_found = TRUE;
|
2006-05-25 15:32:03 +00:00
|
|
|
sci_target_start(doc_list[idx].sci, search_pos);
|
2006-07-18 14:09:01 +00:00
|
|
|
sci_target_end(doc_list[idx].sci, search_pos + find_len);
|
|
|
|
replace_len = sci_target_replace(doc_list[idx].sci, replace_text,
|
|
|
|
flags & SCFIND_REGEXP);
|
|
|
|
ttf.chrg.cpMin = search_pos + replace_len; //next search starts after replacement
|
|
|
|
end += replace_len - find_len; //update end of range now text has changed
|
|
|
|
ttf.chrg.cpMax = end;
|
2006-08-23 18:08:48 +00:00
|
|
|
count++;
|
2006-03-10 00:52:09 +00:00
|
|
|
}
|
2005-11-22 12:26:26 +00:00
|
|
|
}
|
2006-05-25 15:32:03 +00:00
|
|
|
sci_end_undo_action(doc_list[idx].sci);
|
2006-09-02 23:28:34 +00:00
|
|
|
|
2006-11-25 12:32:22 +00:00
|
|
|
filename = g_path_get_basename(DOC_FILENAME(idx));
|
|
|
|
|
2006-09-02 23:28:34 +00:00
|
|
|
if (escaped_chars)
|
|
|
|
{ // escape special characters for showing
|
|
|
|
escaped_find_text = g_strescape(find_text, NULL);
|
|
|
|
escaped_replace_text = g_strescape(replace_text, NULL);
|
2006-11-25 12:32:22 +00:00
|
|
|
msgwin_status_add(_("%s: replaced %d occurrences of \"%s\" with \"%s\"."),
|
|
|
|
filename, count, escaped_find_text, escaped_replace_text);
|
2006-09-02 23:28:34 +00:00
|
|
|
g_free(escaped_find_text);
|
|
|
|
g_free(escaped_replace_text);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2006-11-25 12:32:22 +00:00
|
|
|
msgwin_status_add(_("%s: replaced %d occurrences of \"%s\" with \"%s\"."),
|
|
|
|
filename, count, find_text, replace_text);
|
2006-09-02 23:28:34 +00:00
|
|
|
}
|
2006-11-25 12:32:22 +00:00
|
|
|
g_free(filename);
|
2006-05-25 15:32:03 +00:00
|
|
|
|
2006-07-18 14:09:01 +00:00
|
|
|
if (match_found)
|
2006-07-18 17:43:22 +00:00
|
|
|
{
|
|
|
|
// scroll last match in view.
|
|
|
|
sci_goto_pos(doc_list[idx].sci, ttf.chrg.cpMin, TRUE);
|
2006-07-18 14:09:01 +00:00
|
|
|
return end;
|
2006-07-18 17:43:22 +00:00
|
|
|
}
|
2006-07-18 14:09:01 +00:00
|
|
|
else
|
|
|
|
return -1; //no text was found
|
2005-11-22 12:26:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-09-02 23:28:34 +00:00
|
|
|
void document_replace_sel(gint idx, const gchar *find_text, const gchar *replace_text, gint flags,
|
|
|
|
gboolean escaped_chars)
|
2005-11-22 12:26:26 +00:00
|
|
|
{
|
2006-07-18 14:09:01 +00:00
|
|
|
gint selection_end, selection_start;
|
2005-11-22 12:26:26 +00:00
|
|
|
|
2006-07-18 14:09:01 +00:00
|
|
|
g_return_if_fail(find_text != NULL && replace_text != NULL);
|
|
|
|
if (idx == -1 || ! *find_text) return;
|
2005-11-22 12:26:26 +00:00
|
|
|
|
2006-07-18 14:09:01 +00:00
|
|
|
selection_start = sci_get_selection_start(doc_list[idx].sci);
|
|
|
|
selection_end = sci_get_selection_end(doc_list[idx].sci);
|
|
|
|
if ((selection_end - selection_start) == 0)
|
|
|
|
{
|
|
|
|
utils_beep();
|
|
|
|
return;
|
|
|
|
}
|
2005-11-22 12:26:26 +00:00
|
|
|
|
2006-07-18 14:09:01 +00:00
|
|
|
selection_end = document_replace_range(idx, find_text, replace_text, flags,
|
2006-09-02 23:28:34 +00:00
|
|
|
selection_start, selection_end, escaped_chars);
|
2006-07-18 14:09:01 +00:00
|
|
|
if (selection_end == -1)
|
|
|
|
utils_beep();
|
|
|
|
else
|
2005-11-22 12:26:26 +00:00
|
|
|
{
|
2006-07-18 14:09:01 +00:00
|
|
|
//update the selection for the new endpoint
|
|
|
|
sci_set_selection_start(doc_list[idx].sci, selection_start);
|
|
|
|
sci_set_selection_end(doc_list[idx].sci, selection_end);
|
2005-11-22 12:26:26 +00:00
|
|
|
}
|
2006-07-18 14:09:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-11-25 12:32:22 +00:00
|
|
|
// returns TRUE if at least one replacement was made.
|
|
|
|
gboolean document_replace_all(gint idx, const gchar *find_text, const gchar *replace_text,
|
|
|
|
gint flags, gboolean escaped_chars)
|
2006-07-18 14:09:01 +00:00
|
|
|
{
|
|
|
|
gint len;
|
2006-11-25 12:32:22 +00:00
|
|
|
g_return_val_if_fail(find_text != NULL && replace_text != NULL, FALSE);
|
|
|
|
if (idx == -1 || ! *find_text) return FALSE;
|
2006-07-18 14:09:01 +00:00
|
|
|
|
|
|
|
len = sci_get_length(doc_list[idx].sci);
|
2006-09-02 23:28:34 +00:00
|
|
|
if (document_replace_range(idx, find_text, replace_text, flags, 0, len, escaped_chars) == -1)
|
2006-11-25 12:32:22 +00:00
|
|
|
{
|
2006-07-18 14:09:01 +00:00
|
|
|
utils_beep();
|
2006-11-25 12:32:22 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
return TRUE;
|
2005-11-22 12:26:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-06-13 19:37:21 +00:00
|
|
|
void document_set_font(gint idx, const gchar *font_name, gint size)
|
2005-11-22 12:26:26 +00:00
|
|
|
{
|
|
|
|
gint style;
|
|
|
|
|
|
|
|
for (style = 0; style <= 127; style++)
|
2006-06-13 19:37:21 +00:00
|
|
|
sci_set_font(doc_list[idx].sci, style, font_name, size);
|
2005-11-22 12:26:26 +00:00
|
|
|
// line number and braces
|
2006-06-13 19:37:21 +00:00
|
|
|
sci_set_font(doc_list[idx].sci, STYLE_LINENUMBER, font_name, size);
|
|
|
|
sci_set_font(doc_list[idx].sci, STYLE_BRACELIGHT, font_name, size);
|
|
|
|
sci_set_font(doc_list[idx].sci, STYLE_BRACEBAD, font_name, size);
|
2005-11-22 12:26:26 +00:00
|
|
|
// zoom to 100% to prevent confusion
|
2006-06-13 19:37:21 +00:00
|
|
|
sci_zoom_off(doc_list[idx].sci);
|
2005-11-22 12:26:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-05-10 23:06:47 +00:00
|
|
|
void document_update_tag_list(gint idx, gboolean update)
|
2005-11-22 12:26:26 +00:00
|
|
|
{
|
2006-09-30 16:15:45 +00:00
|
|
|
// if the filetype doesn't have a tag parser or it is a new file
|
2006-06-26 16:19:28 +00:00
|
|
|
if (idx == -1 || doc_list[idx].file_type == NULL ||
|
2006-09-30 16:15:45 +00:00
|
|
|
! doc_list[idx].file_type->has_tags || ! doc_list[idx].file_name)
|
|
|
|
{
|
|
|
|
// set the default (empty) tag list
|
|
|
|
ui_update_tag_list(idx, FALSE);
|
|
|
|
return;
|
|
|
|
}
|
2005-11-22 12:26:26 +00:00
|
|
|
|
|
|
|
if (doc_list[idx].tm_file == NULL)
|
|
|
|
{
|
2006-08-13 08:36:52 +00:00
|
|
|
gchar *locale_filename = utils_get_locale_from_utf8(doc_list[idx].file_name);
|
2006-06-24 14:36:35 +00:00
|
|
|
doc_list[idx].tm_file = tm_source_file_new(locale_filename, FALSE,
|
|
|
|
doc_list[idx].file_type->name);
|
2006-01-23 17:05:29 +00:00
|
|
|
g_free(locale_filename);
|
|
|
|
if (! doc_list[idx].tm_file) return;
|
2005-11-22 12:26:26 +00:00
|
|
|
tm_workspace_add_object(doc_list[idx].tm_file);
|
2006-05-10 23:06:47 +00:00
|
|
|
if (update)
|
|
|
|
tm_source_file_update(doc_list[idx].tm_file, TRUE, FALSE, TRUE);
|
2006-09-05 14:24:47 +00:00
|
|
|
ui_update_tag_list(idx, TRUE);
|
2005-11-22 12:26:26 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (tm_source_file_update(doc_list[idx].tm_file, TRUE, FALSE, TRUE))
|
|
|
|
{
|
2006-09-05 14:24:47 +00:00
|
|
|
ui_update_tag_list(idx, TRUE);
|
2005-11-22 12:26:26 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2006-06-26 17:44:18 +00:00
|
|
|
geany_debug("tag list updating failed");
|
2005-11-22 12:26:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-11-08 11:42:05 +00:00
|
|
|
static GString *get_project_typenames()
|
|
|
|
{
|
|
|
|
GString *s = NULL;
|
|
|
|
|
|
|
|
if (app->tm_workspace)
|
|
|
|
{
|
|
|
|
GPtrArray *tags_array = app->tm_workspace->work_object.tags_array;
|
|
|
|
|
|
|
|
if (tags_array)
|
|
|
|
{
|
|
|
|
s = symbols_find_tags_as_string(tags_array,
|
|
|
|
tm_tag_typedef_t | tm_tag_struct_t | tm_tag_class_t);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-12-07 15:13:49 +00:00
|
|
|
/* sets the filetype of the document (sets syntax highlighting and tagging) */
|
2005-11-22 12:26:26 +00:00
|
|
|
void document_set_filetype(gint idx, filetype *type)
|
|
|
|
{
|
2006-12-07 15:13:49 +00:00
|
|
|
if (type == NULL ||
|
|
|
|
! DOC_IDX_VALID(idx) ||
|
|
|
|
doc_list[idx].file_type == type)
|
|
|
|
return;
|
2005-11-22 12:26:26 +00:00
|
|
|
|
2006-08-15 12:51:36 +00:00
|
|
|
geany_debug("%s : %s (%s)", doc_list[idx].file_name, type->name, doc_list[idx].encoding);
|
2006-04-27 18:06:35 +00:00
|
|
|
doc_list[idx].file_type = type;
|
2006-12-07 15:13:49 +00:00
|
|
|
|
|
|
|
// delete tm file object to force creation of a new one
|
|
|
|
if (doc_list[idx].tm_file != NULL)
|
|
|
|
{
|
|
|
|
tm_workspace_remove_object(doc_list[idx].tm_file, TRUE);
|
|
|
|
doc_list[idx].tm_file = NULL;
|
|
|
|
}
|
|
|
|
|
2006-05-10 23:06:47 +00:00
|
|
|
document_update_tag_list(idx, TRUE);
|
2006-04-27 18:06:35 +00:00
|
|
|
type->style_func_ptr(doc_list[idx].sci);
|
|
|
|
|
|
|
|
// For C/C++/Java files, get list of typedefs for colourising
|
|
|
|
if (sci_get_lexer(doc_list[idx].sci) == SCLEX_CPP)
|
2005-11-22 12:26:26 +00:00
|
|
|
{
|
2006-11-08 11:42:05 +00:00
|
|
|
GString *s = get_project_typenames();
|
2005-11-22 12:26:26 +00:00
|
|
|
|
2006-11-08 11:42:05 +00:00
|
|
|
if (s != NULL)
|
2006-04-27 18:06:35 +00:00
|
|
|
{
|
2006-11-08 11:42:05 +00:00
|
|
|
guint n;
|
|
|
|
|
|
|
|
for (n = 0; n < doc_array->len; n++)
|
2005-11-22 12:26:26 +00:00
|
|
|
{
|
2006-11-08 11:42:05 +00:00
|
|
|
if (doc_list[n].sci)
|
2006-04-27 18:06:35 +00:00
|
|
|
{
|
2006-11-08 11:42:05 +00:00
|
|
|
sci_set_keywords(doc_list[n].sci, 3, s->str);
|
|
|
|
sci_colourise(doc_list[n].sci, 0, -1);
|
2006-04-27 18:06:35 +00:00
|
|
|
}
|
2005-11-22 12:26:26 +00:00
|
|
|
}
|
2006-11-08 11:42:05 +00:00
|
|
|
//SSM(doc_list[idx].sci, SCI_SETKEYWORDS, 3, (sptr_t) s->str);
|
|
|
|
g_string_free(s, TRUE);
|
2005-11-22 12:26:26 +00:00
|
|
|
}
|
|
|
|
}
|
2006-04-27 18:06:35 +00:00
|
|
|
sci_colourise(doc_list[idx].sci, 0, -1);
|
2006-10-18 19:35:42 +00:00
|
|
|
build_menu_update(idx);
|
2005-11-22 12:26:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
gchar *document_get_eol_mode(gint idx)
|
|
|
|
{
|
|
|
|
if (idx == -1) return '\0';
|
|
|
|
|
|
|
|
switch (sci_get_eol_mode(doc_list[idx].sci))
|
|
|
|
{
|
|
|
|
case SC_EOL_CRLF: return _("Win (CRLF)"); break;
|
2006-06-28 19:27:19 +00:00
|
|
|
case SC_EOL_CR: return _("Mac (CR)"); break;
|
2005-11-22 12:26:26 +00:00
|
|
|
case SC_EOL_LF:
|
|
|
|
default: return _("Unix (LF)"); break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-02-26 18:19:28 +00:00
|
|
|
void document_unfold_all(gint idx)
|
|
|
|
{
|
|
|
|
gint lines, pos, i;
|
|
|
|
|
|
|
|
if (idx == -1 || ! doc_list[idx].is_valid) return;
|
|
|
|
|
|
|
|
lines = sci_get_line_count(doc_list[idx].sci);
|
|
|
|
pos = sci_get_current_position(doc_list[idx].sci);
|
|
|
|
|
|
|
|
for (i = 0; i < lines; i++)
|
|
|
|
{
|
|
|
|
sci_ensure_line_is_visible(doc_list[idx].sci, i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void document_fold_all(gint idx)
|
|
|
|
{
|
|
|
|
gint lines, pos, i;
|
|
|
|
|
|
|
|
if (idx == -1 || ! doc_list[idx].is_valid) return;
|
|
|
|
|
|
|
|
lines = sci_get_line_count(doc_list[idx].sci);
|
|
|
|
pos = sci_get_current_position(doc_list[idx].sci);
|
|
|
|
|
|
|
|
for (i = 0; i < lines; i++)
|
|
|
|
{
|
|
|
|
gint level = sci_get_fold_level(doc_list[idx].sci, i);
|
|
|
|
if (level & SC_FOLDLEVELHEADERFLAG)
|
|
|
|
{
|
|
|
|
if (sci_get_fold_expanded(doc_list[idx].sci, i))
|
|
|
|
sci_toggle_fold(doc_list[idx].sci, i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2006-06-13 19:37:21 +00:00
|
|
|
|
|
|
|
|
|
|
|
void document_clear_indicators(gint idx)
|
|
|
|
{
|
|
|
|
glong last_pos = sci_get_length(doc_list[idx].sci);
|
|
|
|
if (last_pos > 0)
|
|
|
|
{
|
|
|
|
sci_start_styling(doc_list[idx].sci, 0, INDIC2_MASK);
|
|
|
|
sci_set_styling(doc_list[idx].sci, last_pos, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void document_set_indicator(gint idx, gint line)
|
|
|
|
{
|
|
|
|
gint start, end, current_mask;
|
|
|
|
guint i = 0, len;
|
|
|
|
gchar *linebuf;
|
|
|
|
|
|
|
|
if (idx == -1 || ! doc_list[idx].is_valid) return;
|
2006-06-29 17:14:52 +00:00
|
|
|
|
2006-06-13 19:37:21 +00:00
|
|
|
start = sci_get_position_from_line(doc_list[idx].sci, line);
|
|
|
|
end = sci_get_position_from_line(doc_list[idx].sci, line + 1);
|
2006-06-29 17:14:52 +00:00
|
|
|
|
2006-06-13 19:37:21 +00:00
|
|
|
// skip blank lines
|
2006-09-06 17:53:58 +00:00
|
|
|
if ((start + 1) == end ||
|
|
|
|
sci_get_line_length(doc_list[idx].sci, line) == utils_get_eol_char_len(idx))
|
|
|
|
return;
|
2006-06-13 19:37:21 +00:00
|
|
|
|
|
|
|
// don't set the indicator on whitespace
|
2006-10-02 15:22:29 +00:00
|
|
|
len = end - start;
|
|
|
|
linebuf = sci_get_line(doc_list[idx].sci, line);
|
2006-06-13 19:37:21 +00:00
|
|
|
|
|
|
|
while (isspace(linebuf[i])) i++;
|
2006-11-01 10:58:47 +00:00
|
|
|
while (len > 1 && len > i && isspace(linebuf[len-1])) len--;
|
2006-06-13 19:37:21 +00:00
|
|
|
g_free(linebuf);
|
2006-06-29 17:14:52 +00:00
|
|
|
|
2006-06-13 19:37:21 +00:00
|
|
|
current_mask = sci_get_style_at(doc_list[idx].sci, start);
|
|
|
|
current_mask &= INDICS_MASK;
|
|
|
|
current_mask |= INDIC2_MASK;
|
|
|
|
sci_start_styling(doc_list[idx].sci, start + i, INDIC2_MASK);
|
2006-09-06 17:53:58 +00:00
|
|
|
//geany_debug("%p\tline: %d\tstart-end: %d - %d\t%d - %i", doc_list[idx].sci, line, start, end, len, i);
|
2006-06-13 19:37:21 +00:00
|
|
|
sci_set_styling(doc_list[idx].sci, len - i, current_mask);
|
|
|
|
}
|
2006-06-29 17:14:52 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* simple file print */
|
|
|
|
void document_print(gint idx)
|
|
|
|
{
|
|
|
|
gchar *cmdline;
|
|
|
|
|
|
|
|
if (idx == -1 || ! doc_list[idx].is_valid || doc_list[idx].file_name == NULL) return;
|
|
|
|
|
|
|
|
cmdline = g_strdup(app->tools_print_cmd);
|
|
|
|
cmdline = utils_str_replace(cmdline, "%f", doc_list[idx].file_name);
|
|
|
|
|
|
|
|
if (dialogs_show_question(_("The file \"%s\" will be printed with the following command:\n\n%s"),
|
|
|
|
doc_list[idx].file_name, cmdline))
|
|
|
|
{
|
|
|
|
gint rc;
|
|
|
|
// system() is not the best way, but the only one I found to get the following working:
|
|
|
|
// a2ps -1 --medium=A4 -o - %f | xfprint4
|
|
|
|
rc = system(cmdline);
|
|
|
|
if (rc != 0)
|
|
|
|
{
|
2006-10-25 14:38:48 +00:00
|
|
|
dialogs_show_msgbox(GTK_MESSAGE_ERROR, _("Printing of \"%s\" failed (return code: %d)."),
|
2006-06-29 17:14:52 +00:00
|
|
|
doc_list[idx].file_name, rc);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
msgwin_status_add(_("File %s printed."), doc_list[idx].file_name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
g_free(cmdline);
|
|
|
|
}
|
2006-07-17 10:42:26 +00:00
|
|
|
|
|
|
|
|
|
|
|
void document_replace_tabs(gint idx)
|
|
|
|
{
|
2006-10-25 17:03:24 +00:00
|
|
|
gint search_pos;
|
2006-10-25 19:23:19 +00:00
|
|
|
gint tab_len;
|
2006-10-25 17:03:24 +00:00
|
|
|
gchar *tab_str;
|
|
|
|
struct TextToFind ttf;
|
2006-07-17 10:42:26 +00:00
|
|
|
|
2006-10-25 17:03:24 +00:00
|
|
|
if (! DOC_IDX_VALID(idx)) return;
|
2006-07-17 10:42:26 +00:00
|
|
|
|
2006-10-25 17:03:24 +00:00
|
|
|
sci_start_undo_action(doc_list[idx].sci);
|
2006-10-25 19:23:19 +00:00
|
|
|
tab_len = sci_get_tab_width(doc_list[idx].sci);
|
2006-10-25 17:03:24 +00:00
|
|
|
ttf.chrg.cpMin = 0;
|
|
|
|
ttf.chrg.cpMax = sci_get_length(doc_list[idx].sci);
|
|
|
|
ttf.lpstrText = (gchar*)"\t";
|
|
|
|
tab_str = g_strnfill(tab_len, ' ');
|
2006-07-17 10:42:26 +00:00
|
|
|
|
2006-10-25 17:03:24 +00:00
|
|
|
while (TRUE)
|
2006-07-17 10:42:26 +00:00
|
|
|
{
|
2006-10-25 17:03:24 +00:00
|
|
|
search_pos = sci_find_text(doc_list[idx].sci, SCFIND_MATCHCASE, &ttf);
|
|
|
|
if (search_pos == -1) break;
|
2006-07-17 10:42:26 +00:00
|
|
|
|
2006-10-25 17:03:24 +00:00
|
|
|
sci_target_start(doc_list[idx].sci, search_pos);
|
|
|
|
sci_target_end(doc_list[idx].sci, search_pos + 1);
|
|
|
|
sci_target_replace(doc_list[idx].sci, tab_str, FALSE);
|
|
|
|
ttf.chrg.cpMin = search_pos + tab_len - 1; // next search starts after replacement
|
|
|
|
ttf.chrg.cpMax += tab_len - 1; // update end of range now text has changed
|
2006-07-17 10:42:26 +00:00
|
|
|
}
|
2006-10-25 17:03:24 +00:00
|
|
|
sci_end_undo_action(doc_list[idx].sci);
|
|
|
|
g_free(tab_str);
|
2006-07-17 10:42:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void document_strip_trailing_spaces(gint idx)
|
|
|
|
{
|
|
|
|
gint max_lines = sci_get_line_count(doc_list[idx].sci);
|
|
|
|
gint line;
|
|
|
|
|
|
|
|
for (line = 0; line < max_lines; line++)
|
|
|
|
{
|
|
|
|
gint line_start = sci_get_position_from_line(doc_list[idx].sci, line);
|
|
|
|
gint line_end = sci_get_line_end_from_position(doc_list[idx].sci, line);
|
|
|
|
gint i = line_end - 1;
|
|
|
|
gchar ch = sci_get_char_at(doc_list[idx].sci, i);
|
|
|
|
|
|
|
|
while ((i >= line_start) && ((ch == ' ') || (ch == '\t')))
|
|
|
|
{
|
|
|
|
i--;
|
|
|
|
ch = sci_get_char_at(doc_list[idx].sci, i);
|
|
|
|
}
|
|
|
|
if (i < (line_end-1))
|
|
|
|
{
|
|
|
|
sci_target_start(doc_list[idx].sci, i + 1);
|
|
|
|
sci_target_end(doc_list[idx].sci, line_end);
|
2006-07-18 14:09:01 +00:00
|
|
|
sci_target_replace(doc_list[idx].sci, "", FALSE);
|
2006-07-17 10:42:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void document_ensure_final_newline(gint idx)
|
|
|
|
{
|
|
|
|
gint max_lines = sci_get_line_count(doc_list[idx].sci);
|
|
|
|
gboolean append_newline = (max_lines == 1);
|
|
|
|
gint end_document = sci_get_position_from_line(doc_list[idx].sci, max_lines);
|
|
|
|
|
|
|
|
if (max_lines > 1)
|
|
|
|
{
|
|
|
|
append_newline = end_document > sci_get_position_from_line(doc_list[idx].sci, max_lines - 1);
|
|
|
|
}
|
|
|
|
if (append_newline)
|
|
|
|
{
|
|
|
|
const gchar *eol = "\n";
|
|
|
|
switch (sci_get_eol_mode(doc_list[idx].sci))
|
|
|
|
{
|
|
|
|
case SC_EOL_CRLF:
|
|
|
|
eol = "\r\n";
|
|
|
|
break;
|
|
|
|
case SC_EOL_CR:
|
|
|
|
eol = "\r";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
sci_insert_text(doc_list[idx].sci, end_document, eol);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-10-10 16:02:41 +00:00
|
|
|
|
|
|
|
void document_set_encoding(gint idx, const gchar *new_encoding)
|
|
|
|
{
|
|
|
|
if (! DOC_IDX_VALID(idx) || new_encoding == NULL ||
|
2006-12-07 16:09:45 +00:00
|
|
|
utils_str_equal(new_encoding, doc_list[idx].encoding)) return;
|
2006-10-10 16:02:41 +00:00
|
|
|
|
|
|
|
g_free(doc_list[idx].encoding);
|
|
|
|
doc_list[idx].encoding = g_strdup(new_encoding);
|
|
|
|
|
|
|
|
ui_update_statusbar(idx, -1);
|
|
|
|
gtk_widget_set_sensitive(lookup_widget(app->window, "menu_write_unicode_bom1"),
|
|
|
|
utils_is_unicode_charset(doc_list[idx].encoding));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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) */
|
2006-09-06 16:09:08 +00:00
|
|
|
void document_undo_clear(gint idx)
|
|
|
|
{
|
|
|
|
undo_action *a;
|
|
|
|
|
|
|
|
while (g_trash_stack_height(&doc_list[idx].undo_actions) > 0)
|
|
|
|
{
|
|
|
|
a = g_trash_stack_pop(&doc_list[idx].undo_actions);
|
|
|
|
if (a != NULL)
|
|
|
|
{
|
|
|
|
switch (a->type)
|
|
|
|
{
|
|
|
|
case UNDO_ENCODING: g_free(a->data); break;
|
|
|
|
default: break;
|
|
|
|
}
|
|
|
|
|
|
|
|
g_free(a);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
doc_list[idx].undo_actions = NULL;
|
|
|
|
|
|
|
|
while (g_trash_stack_height(&doc_list[idx].redo_actions) > 0)
|
|
|
|
{
|
|
|
|
a = g_trash_stack_pop(&doc_list[idx].redo_actions);
|
|
|
|
if (a != NULL)
|
|
|
|
{
|
|
|
|
switch (a->type)
|
|
|
|
{
|
|
|
|
case UNDO_ENCODING: g_free(a->data); break;
|
|
|
|
default: break;
|
|
|
|
}
|
|
|
|
|
|
|
|
g_free(a);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
doc_list[idx].redo_actions = NULL;
|
|
|
|
|
|
|
|
doc_list[idx].changed = FALSE;
|
|
|
|
if (! app->quitting) document_set_text_changed(idx);
|
2006-10-10 16:02:41 +00:00
|
|
|
|
|
|
|
//geany_debug("%s: new undo stack height: %d, new redo stack height: %d", __func__,
|
|
|
|
//g_trash_stack_height(&doc_list[idx].undo_actions), g_trash_stack_height(&doc_list[idx].redo_actions));
|
2006-09-06 16:09:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void document_undo_add(gint idx, guint type, gpointer data)
|
|
|
|
{
|
|
|
|
undo_action *action;
|
|
|
|
|
2006-10-10 16:02:41 +00:00
|
|
|
if (! DOC_IDX_VALID(idx)) return;
|
2006-09-06 16:09:08 +00:00
|
|
|
|
|
|
|
action = g_new0(undo_action, 1);
|
|
|
|
action->type = type;
|
|
|
|
action->data = data;
|
|
|
|
|
|
|
|
g_trash_stack_push(&doc_list[idx].undo_actions, action);
|
|
|
|
|
|
|
|
doc_list[idx].changed = TRUE;
|
|
|
|
document_set_text_changed(idx);
|
|
|
|
ui_update_popup_reundo_items(idx);
|
|
|
|
|
2006-10-10 16:02:41 +00:00
|
|
|
//geany_debug("%s: new stack height: %d, added type: %d", __func__,
|
|
|
|
//g_trash_stack_height(&doc_list[idx].undo_actions), action->type);
|
2006-09-06 16:09:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
gboolean document_can_undo(gint idx)
|
|
|
|
{
|
2006-10-10 16:02:41 +00:00
|
|
|
if (! DOC_IDX_VALID(idx)) return FALSE;
|
2006-09-06 16:09:08 +00:00
|
|
|
|
|
|
|
if (g_trash_stack_height(&doc_list[idx].undo_actions) > 0 || sci_can_undo(doc_list[idx].sci))
|
|
|
|
return TRUE;
|
|
|
|
else
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-10-24 13:41:34 +00:00
|
|
|
static void update_changed_state(gint idx)
|
|
|
|
{
|
|
|
|
doc_list[idx].changed =
|
|
|
|
(sci_is_modified(doc_list[idx].sci) ||
|
|
|
|
doc_list[idx].has_bom != doc_list[idx].saved_encoding.has_bom ||
|
2006-12-07 16:09:45 +00:00
|
|
|
! utils_str_equal(doc_list[idx].encoding, doc_list[idx].saved_encoding.encoding));
|
2006-10-24 13:41:34 +00:00
|
|
|
document_set_text_changed(idx);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-10-10 16:02:41 +00:00
|
|
|
void document_undo(gint idx)
|
|
|
|
{
|
|
|
|
undo_action *action;
|
|
|
|
|
|
|
|
if (! DOC_IDX_VALID(idx)) return;
|
|
|
|
|
|
|
|
action = g_trash_stack_pop(&doc_list[idx].undo_actions);
|
|
|
|
|
|
|
|
if (action == NULL)
|
|
|
|
{
|
|
|
|
// fallback, should not be necessary
|
|
|
|
geany_debug("%s: fallback used", __func__);
|
|
|
|
sci_undo(doc_list[idx].sci);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
switch (action->type)
|
|
|
|
{
|
|
|
|
case UNDO_SCINTILLA:
|
|
|
|
{
|
|
|
|
document_redo_add(idx, UNDO_SCINTILLA, NULL);
|
|
|
|
|
|
|
|
sci_undo(doc_list[idx].sci);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case UNDO_BOM:
|
|
|
|
{
|
|
|
|
document_redo_add(idx, UNDO_BOM, GINT_TO_POINTER(doc_list[idx].has_bom));
|
|
|
|
|
|
|
|
doc_list[idx].has_bom = GPOINTER_TO_INT(action->data);
|
|
|
|
ui_update_statusbar(idx, -1);
|
|
|
|
ui_document_show_hide(idx);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case UNDO_ENCODING:
|
|
|
|
{
|
|
|
|
// use the "old" encoding
|
|
|
|
document_redo_add(idx, UNDO_ENCODING, g_strdup(doc_list[idx].encoding));
|
|
|
|
|
|
|
|
document_set_encoding(idx, (const gchar*)action->data);
|
|
|
|
|
|
|
|
app->ignore_callback = TRUE;
|
|
|
|
encodings_select_radio_item((const gchar*)action->data);
|
|
|
|
app->ignore_callback = FALSE;
|
|
|
|
|
|
|
|
g_free(action->data);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default: break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
g_free(action); // free the action which was taken from the stack
|
|
|
|
|
2006-10-24 13:41:34 +00:00
|
|
|
update_changed_state(idx);
|
2006-10-10 16:02:41 +00:00
|
|
|
ui_update_popup_reundo_items(idx);
|
|
|
|
//geany_debug("%s: new stack height: %d", __func__, g_trash_stack_height(&doc_list[idx].undo_actions));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-09-06 16:09:08 +00:00
|
|
|
gboolean document_can_redo(gint idx)
|
|
|
|
{
|
2006-10-10 16:02:41 +00:00
|
|
|
if (! DOC_IDX_VALID(idx)) return FALSE;
|
2006-09-06 16:09:08 +00:00
|
|
|
|
2006-10-10 16:02:41 +00:00
|
|
|
if (g_trash_stack_height(&doc_list[idx].redo_actions) > 0 || sci_can_redo(doc_list[idx].sci))
|
|
|
|
return TRUE;
|
|
|
|
else
|
|
|
|
return FALSE;
|
2006-09-06 16:09:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-10-10 16:02:41 +00:00
|
|
|
void document_redo(gint idx)
|
2006-09-06 16:09:08 +00:00
|
|
|
{
|
2006-09-08 10:20:15 +00:00
|
|
|
undo_action *action;
|
|
|
|
|
2006-10-10 16:02:41 +00:00
|
|
|
if (! DOC_IDX_VALID(idx)) return;
|
2006-09-06 16:09:08 +00:00
|
|
|
|
2006-10-10 16:02:41 +00:00
|
|
|
action = g_trash_stack_pop(&doc_list[idx].redo_actions);
|
2006-09-06 16:09:08 +00:00
|
|
|
|
|
|
|
if (action == NULL)
|
|
|
|
{
|
|
|
|
// fallback, should not be necessary
|
2006-10-10 16:02:41 +00:00
|
|
|
geany_debug("%s: fallback used", __func__);
|
|
|
|
sci_redo(doc_list[idx].sci);
|
2006-09-06 16:09:08 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
switch (action->type)
|
|
|
|
{
|
|
|
|
case UNDO_SCINTILLA:
|
|
|
|
{
|
2006-10-10 16:02:41 +00:00
|
|
|
document_undo_add(idx, UNDO_SCINTILLA, NULL);
|
|
|
|
|
|
|
|
sci_redo(doc_list[idx].sci);
|
2006-09-06 16:09:08 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case UNDO_BOM:
|
|
|
|
{
|
2006-10-10 16:02:41 +00:00
|
|
|
document_undo_add(idx, UNDO_BOM, GINT_TO_POINTER(doc_list[idx].has_bom));
|
|
|
|
|
2006-09-06 16:09:08 +00:00
|
|
|
doc_list[idx].has_bom = GPOINTER_TO_INT(action->data);
|
|
|
|
ui_update_statusbar(idx, -1);
|
|
|
|
ui_document_show_hide(idx);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case UNDO_ENCODING:
|
|
|
|
{
|
2006-10-10 16:02:41 +00:00
|
|
|
document_undo_add(idx, UNDO_ENCODING, g_strdup(doc_list[idx].encoding));
|
|
|
|
|
|
|
|
document_set_encoding(idx, (const gchar*)action->data);
|
|
|
|
|
|
|
|
app->ignore_callback = TRUE;
|
|
|
|
encodings_select_radio_item((const gchar*)action->data);
|
|
|
|
app->ignore_callback = FALSE;
|
|
|
|
|
|
|
|
g_free(action->data);
|
2006-09-06 16:09:08 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default: break;
|
|
|
|
}
|
|
|
|
}
|
2006-10-10 16:02:41 +00:00
|
|
|
g_free(action); // free the action which was taken from the stack
|
2006-09-06 16:09:08 +00:00
|
|
|
|
2006-10-24 13:41:34 +00:00
|
|
|
update_changed_state(idx);
|
2006-09-06 16:09:08 +00:00
|
|
|
ui_update_popup_reundo_items(idx);
|
2006-10-10 16:02:41 +00:00
|
|
|
//geany_debug("%s: new stack height: %d", __func__, g_trash_stack_height(&doc_list[idx].redo_actions));
|
2006-09-06 16:09:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-10-10 16:02:41 +00:00
|
|
|
static void document_redo_add(gint idx, guint type, gpointer data)
|
2006-09-06 16:09:08 +00:00
|
|
|
{
|
2006-10-10 16:02:41 +00:00
|
|
|
undo_action *action;
|
|
|
|
|
|
|
|
if (! DOC_IDX_VALID(idx)) return;
|
|
|
|
|
|
|
|
action = g_new0(undo_action, 1);
|
|
|
|
action->type = type;
|
|
|
|
action->data = data;
|
2006-09-06 16:09:08 +00:00
|
|
|
|
2006-10-10 16:02:41 +00:00
|
|
|
g_trash_stack_push(&doc_list[idx].redo_actions, action);
|
|
|
|
|
|
|
|
doc_list[idx].changed = TRUE;
|
|
|
|
document_set_text_changed(idx);
|
|
|
|
ui_update_popup_reundo_items(idx);
|
|
|
|
|
|
|
|
//geany_debug("%s: new stack height: %d, added type: %d", __func__,
|
|
|
|
//g_trash_stack_height(&doc_list[idx].redo_actions), action->type);
|
2006-09-06 16:09:08 +00:00
|
|
|
}
|
2006-07-17 10:42:26 +00:00
|
|
|
|
2006-10-10 16:02:41 +00:00
|
|
|
|
2006-12-05 10:37:36 +00:00
|
|
|
/* Gets the status colour of the document, or NULL if default widget
|
|
|
|
* colouring should be used. */
|
|
|
|
GdkColor *document_get_status(gint idx)
|
|
|
|
{
|
|
|
|
static GdkColor red = {0, 0xFFFF, 0, 0};
|
|
|
|
static GdkColor green = {0, 0, 0x7FFF, 0};
|
|
|
|
GdkColor *color = NULL;
|
|
|
|
|
|
|
|
if (doc_list[idx].changed)
|
|
|
|
color = &red;
|
|
|
|
else if (doc_list[idx].readonly)
|
|
|
|
color = &green;
|
|
|
|
|
|
|
|
return color; // return pointer to static GdkColor.
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-10-24 13:41:34 +00:00
|
|
|
// useful debugging function (usually debug macros aren't enabled)
|
2006-10-24 16:29:17 +00:00
|
|
|
#ifdef GEANY_DEBUG
|
2006-10-24 13:41:34 +00:00
|
|
|
document *doc(gint idx)
|
|
|
|
{
|
|
|
|
return DOC_IDX_VALID(idx) ? &doc_list[idx] : NULL;
|
|
|
|
}
|
2006-10-24 16:29:17 +00:00
|
|
|
#endif
|