Kaydet (Commit) a1baf148 authored tarafından Enrico Tröger's avatar Enrico Tröger

enabled autocompletion for all filetypes, removed mmap()-code, fixed memory leak

git-svn-id: https://geany.svn.sourceforge.net/svnroot/geany/trunk@336 ea778897-0a13-0410-b9d1-a72fbfd435f5
üst df1b9fd0
......@@ -106,6 +106,7 @@ class ScintillaGTK : public ScintillaBase {
static GdkAtom atomClipboard;
static GdkAtom atomUTF8;
static GdkAtom atomString;
static GdkAtom atomUriList;
GdkAtom atomSought;
#if PLAT_GTK_WIN32
......@@ -282,19 +283,19 @@ enum {
TARGET_STRING,
TARGET_TEXT,
TARGET_COMPOUND_TEXT,
TARGET_UTF8_STRING
TARGET_UTF8_STRING,
TARGET_URI
};
GdkAtom ScintillaGTK::atomClipboard = 0;
GdkAtom ScintillaGTK::atomUTF8 = 0;
GdkAtom ScintillaGTK::atomString = 0;
GdkAtom ScintillaGTK::atomUriList = 0;
static const GtkTargetEntry clipboardTargets[] = {
{ "text/uri-list", 0, TARGET_URI },
{ "UTF8_STRING", 0, TARGET_UTF8_STRING },
{ "STRING", 0, TARGET_STRING },
// { "TEXT", 0, TARGET_TEXT },
// { "COMPOUND_TEXT", 0, TARGET_COMPOUND_TEXT },
{ "text/uri-list", 0, 0 },
};
static const gint nClipboardTargets = sizeof(clipboardTargets) / sizeof(clipboardTargets[0]);
......@@ -1494,18 +1495,20 @@ void ScintillaGTK::ReceivedSelection(GtkSelectionData *selection_data) {
void ScintillaGTK::ReceivedDrop(GtkSelectionData *selection_data) {
dragWasDropped = true;
if ((selection_data->type == GDK_TARGET_STRING) || (selection_data->type == atomUTF8)) {
if (selection_data->type == atomUriList) {
char *ptr = new char[selection_data->length + 1];
ptr[selection_data->length] = '\0';
memcpy(ptr, selection_data->data, selection_data->length);
NotifyURIDropped(ptr);
delete []ptr;
} else if ((selection_data->type == GDK_TARGET_STRING) || (selection_data->type == atomUTF8)) {
if (selection_data->length > 0) {
SelectionText selText;
GetGtkSelectionText(selection_data, selText);
DropAt(posDrop, selText.s, false, selText.rectangular);
}
} else if (selection_data->length > 0) {
char *ptr = new char[selection_data->length + 1];
ptr[selection_data->length] = '\0';
memcpy(ptr, selection_data->data, selection_data->length);
NotifyURIDropped(ptr);
delete []ptr;
fprintf(stderr, "ReceivedDrop other %p\n", static_cast<void *>(selection_data->type));
}
Redraw();
}
......@@ -2466,9 +2469,10 @@ GtkType scintilla_get_type() {
}
void ScintillaGTK::ClassInit(GtkObjectClass* object_class, GtkWidgetClass *widget_class, GtkContainerClass *container_class) {
atomClipboard = gdk_atom_intern("CLIPBOARD", FALSE);
atomUTF8 = gdk_atom_intern("UTF8_STRING", FALSE);
atomString = GDK_SELECTION_TYPE_STRING;
atomClipboard = gdk_atom_intern("CLIPBOARD", FALSE);
atomUTF8 = gdk_atom_intern("UTF8_STRING", FALSE);
atomString = GDK_SELECTION_TYPE_STRING;
atomUriList = gdk_atom_intern("text/uri-list", FALSE);
// Define default signal handlers for the class: Could move more
// of the signal handlers here (those that currently attached to wDraw
......
......@@ -406,12 +406,8 @@ void document_open_file(gint idx, const gchar *filename, gint pos, gboolean read
gchar *utf8_filename = NULL;
gchar *locale_filename = NULL;
GError *err = NULL;
#if defined(HAVE_MMAP) && defined(HAVE_MUNMAP) && defined(HAVE_FCNTL_H)
gint fd;
void *map = NULL;
#else
gchar *map = NULL;
#endif
gchar *data = NULL;
//struct timeval tv, tv1;
//struct timezone tz;
//gettimeofday(&tv, &tz);
......@@ -456,70 +452,51 @@ void document_open_file(gint idx, const gchar *filename, gint pos, gboolean read
}
if (stat(locale_filename, &st) != 0)
{
msgwin_status_add(_("Could not stat file"));
g_free(utf8_filename);
g_free(locale_filename);
return;
}
#if defined(HAVE_MMAP) && defined(HAVE_MUNMAP) && defined(HAVE_FCNTL_H)
if ((fd = open(locale_filename, O_RDONLY)) < 0)
{
msgwin_status_add(_("Could not open file %s (%s)"), utf8_filename, g_strerror(errno));
g_free(utf8_filename);
g_free(locale_filename);
return;
}
/// EXPERIMENTAL map is NULL if size is 0, I hope this works in all cases
if ((st.st_size > 0) && ((map = mmap(0, st.st_size, PROT_READ, MAP_SHARED, fd, 0)) == MAP_FAILED))
{
msgwin_status_add(_("Could not open file %s (%s)"), utf8_filename, g_strerror(errno));
g_free(utf8_filename);
g_free(locale_filename);
return;
}
size = (gint)st.st_size;
#ifdef GEANY_WIN32
if (! g_file_get_contents(utf8_filename, &data, &size, &err))
#else
// use GLib function to load file on Win32 systems and those w/o mmap()
if (! g_file_get_contents(utf8_filename, &map, NULL, &err))
if (! g_file_get_contents(locale_filename, &data, &size, &err))
#endif
{
msgwin_status_add(_("Could not open file %s (%s)"), utf8_filename, err->message);
//msgwin_status_add(_("Could not open file %s (%s)"), utf8_filename, g_strerror(err->code));
msgwin_status_add(err->message);
g_error_free(err);
g_free(utf8_filename);
g_free(locale_filename);
return;
}
size = (gint)strlen(map);
#endif
/* Determine character encoding and convert to utf-8*/
if (size > 0)
{
if (g_utf8_validate(map, size, NULL))
if (g_utf8_validate(data, size, NULL))
{
enc = g_strdup("UTF-8");
}
else
{
gchar *converted_text = utils_convert_to_utf8(map, size, &enc);
gchar *converted_text = utils_convert_to_utf8(data, size, &enc);
if (converted_text == NULL)
{
msgwin_status_add(_("The file does not look like a text file or the file encoding is not supported."));
utils_beep();
#if defined(HAVE_MMAP) && defined(HAVE_MUNMAP) && defined(HAVE_FCNTL_H)
close(fd);
munmap(map, st.st_size);
#else
g_free(map);
#endif
g_free(data);
g_free(utf8_filename);
g_free(locale_filename);
return;
}
else
{
map = (void*)converted_text;
g_free(data);
data = (void*)converted_text;
size = strlen(converted_text);
}
}
......@@ -532,8 +509,8 @@ void document_open_file(gint idx, const gchar *filename, gint pos, gboolean read
if (! reload) idx = document_create_new_sci(utf8_filename);
// sets editor mode and add the text to the ScintillaObject
sci_add_text_buffer(doc_list[idx].sci, map, size);
editor_mode = utils_get_line_endings(map, size);
sci_add_text_buffer(doc_list[idx].sci, data, size);
editor_mode = utils_get_line_endings(data, size);
sci_set_eol_mode(doc_list[idx].sci, editor_mode);
sci_set_line_numbers(doc_list[idx].sci, app->show_linenumber_margin, 0);
......@@ -581,12 +558,8 @@ void document_open_file(gint idx, const gchar *filename, gint pos, gboolean read
}
document_set_text_changed(idx);
#if defined(HAVE_MMAP) && defined(HAVE_MUNMAP) && defined(HAVE_FCNTL_H)
close(fd);
munmap(map, st.st_size);
#else
g_free(map);
#endif
g_free(data);
// finally add current file to recent files menu, but not the files from the last session
if (! app->opening_session_files &&
......
......@@ -243,7 +243,7 @@ void on_editor_notification(GtkWidget *editor, gint scn, gpointer lscn, gpointer
case SC_EOL_CRLF: list = g_strsplit(nt->text, "\r\n", 0); break;
case SC_EOL_LF: list = g_strsplit(nt->text, "\n", 0); break;
default: list = g_strsplit(nt->text, "\n", 0);
}
}
for (i = 0; ; i++)
{
......@@ -409,7 +409,7 @@ gboolean sci_cb_start_auto_complete(ScintillaObject *sci, gint pos)
sci_get_line(sci, line, linebuf);
if (lexer != SCLEX_CPP && lexer != SCLEX_HTML && lexer != SCLEX_PASCAL) return FALSE;
//if (lexer != SCLEX_CPP && lexer != SCLEX_HTML && lexer != SCLEX_PASCAL) return FALSE;
if (lexer == SCLEX_HTML && style == SCE_H_DEFAULT) return FALSE;
if (lexer == SCLEX_CPP && (style == SCE_C_COMMENT ||
style == SCE_C_COMMENTLINE || style == SCE_C_COMMENTDOC)) return FALSE;
......@@ -592,13 +592,13 @@ void sci_cb_auto_forif(ScintillaObject *sci, gint pos, gint idx)
var = g_strdup("i");
contruct_len = 12;
}
construct = g_strdup_printf("(%s%s = 0; %s < ; %s++)%s{%s\t%s}%s",
construct = g_strdup_printf("(%s%s = 0; %s < ; %s++)%s{%s\t%s}%s",
(doc_list[idx].file_type->id == GEANY_FILETYPES_CPP) ? "int " : "",
var, var, var, eol, eol, eol, eol);
// add 4 characters because of "int " in C++ mode
contruct_len += (doc_list[idx].file_type->id == GEANY_FILETYPES_CPP) ? 4 : 0;
SSM(sci, SCI_INSERTTEXT, pos, (sptr_t) construct);
sci_goto_pos(sci, pos + contruct_len, TRUE);
g_free(var);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment