From 09d7f30aec7775f9fc25afbb839cab311feb0310 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Enrico=20Tr=C3=B6ger?= Date: Wed, 6 Sep 2006 17:31:53 +0000 Subject: [PATCH] Convert tag names to UTF-8 before showing them in the sidebar. git-svn-id: https://geany.svn.sourceforge.net/svnroot/geany/trunk@794 ea778897-0a13-0410-b9d1-a72fbfd435f5 --- ChangeLog | 2 ++ src/document.c | 4 ++-- src/utils.c | 31 ++++++++++++++++++++++++------- src/utils.h | 5 ++++- 4 files changed, 32 insertions(+), 10 deletions(-) diff --git a/ChangeLog b/ChangeLog index 4b60800a..2e4ead83 100644 --- a/ChangeLog +++ b/ChangeLog @@ -2,6 +2,8 @@ * src/callbacks.c, src/document.c, src/ui_utils.c, src/sci_cb.c: Added own implementation of an Undo stack (not yet working). + * src/utils.c, src/document.c: + Convert tag names to UTF-8 before showing them in the sidebar. 2006-09-05 Enrico Tröger diff --git a/src/document.c b/src/document.c index abf44782..16a1d169 100644 --- a/src/document.c +++ b/src/document.c @@ -511,7 +511,7 @@ int document_open_file(gint idx, const gchar *filename, gint pos, gboolean reado } else { - gchar *converted_text = utils_convert_to_utf8_from_charset(data, size, forced_enc); + gchar *converted_text = utils_convert_to_utf8_from_charset(data, size, forced_enc, FALSE); if (converted_text == NULL) { msgwin_status_add(_("The file \"%s\" is not valid %s."), utf8_filename, forced_enc); @@ -542,7 +542,7 @@ int document_open_file(gint idx, const gchar *filename, gint pos, gboolean reado bom = TRUE; if (enc[4] != '8') // the BOM indicated something else than UTF-8 { - gchar *converted_text = utils_convert_to_utf8_from_charset(data, size, enc); + gchar *converted_text = utils_convert_to_utf8_from_charset(data, size, enc, FALSE); if (converted_text == NULL) { g_free(enc); diff --git a/src/utils.c b/src/utils.c index 6611a988..cc37f7d2 100644 --- a/src/utils.c +++ b/src/utils.c @@ -201,6 +201,8 @@ const GList *utils_get_tag_list(gint idx, guint tag_types) TMTag *tag; guint i; GeanySymbol *symbol; + gboolean doc_is_utf8 = FALSE; + gchar *utf8_name; if (tag_names) { @@ -214,18 +216,24 @@ const GList *utils_get_tag_list(gint idx, guint tag_types) tag_names = NULL; } + // do this comparison only once + if (utils_strcmp(doc_list[idx].encoding, "UTF-8")) doc_is_utf8 = TRUE; + for (i = 0; i < (doc_list[idx].tm_file)->tags_array->len; ++i) { tag = TM_TAG((doc_list[idx].tm_file)->tags_array->pdata[i]); if (tag == NULL) return NULL; - //geany_debug("%s: %d", doc_list[idx].file_name, tag->type); + if (tag->type & tag_types) { + if (! doc_is_utf8) utf8_name = utils_convert_to_utf8_from_charset(tag->name, -1, + doc_list[idx].encoding, TRUE); + else utf8_name = tag->name; if ((tag->atts.entry.scope != NULL) && isalpha(tag->atts.entry.scope[0])) { symbol = g_new0(GeanySymbol, 1); - symbol->str = g_strdup_printf("%s::%s [%ld]", tag->atts.entry.scope, tag->name, tag->atts.entry.line); + symbol->str = g_strdup_printf("%s::%s [%ld]", tag->atts.entry.scope, utf8_name, tag->atts.entry.line); symbol->type = tag->type; symbol->line = tag->atts.entry.line; tag_names = g_list_prepend(tag_names, symbol); @@ -233,11 +241,12 @@ const GList *utils_get_tag_list(gint idx, guint tag_types) else { symbol = g_new0(GeanySymbol, 1); - symbol->str = g_strdup_printf("%s [%ld]", tag->name, tag->atts.entry.line); + symbol->str = g_strdup_printf("%s [%ld]", utf8_name, tag->atts.entry.line); symbol->type = tag->type; symbol->line = tag->atts.entry.line; tag_names = g_list_prepend(tag_names, symbol); } + if (! doc_is_utf8) g_free(utf8_name); } } tag_names = g_list_sort(tag_names, (GCompareFunc) utils_compare_symbol); @@ -335,7 +344,10 @@ gint utils_write_file(const gchar *filename, const gchar *text) } -gchar *utils_convert_to_utf8_from_charset(const gchar *buffer, gsize size, const gchar *charset) +/* Converts a string from the given charset to UTF-8. + * If fast is set, no further checks are performed. */ +gchar *utils_convert_to_utf8_from_charset(const gchar *buffer, gsize size, const gchar *charset, + gboolean fast) { gchar *utf8_content = NULL; GError *conv_error = NULL; @@ -346,9 +358,14 @@ gchar *utils_convert_to_utf8_from_charset(const gchar *buffer, gsize size, const g_return_val_if_fail(charset != NULL, NULL); converted_contents = g_convert(buffer, size, "UTF-8", charset, NULL, - &bytes_written, &conv_error); + &bytes_written, &conv_error); - if (conv_error != NULL || ! g_utf8_validate(converted_contents, bytes_written, NULL)) + if (fast) + { + utf8_content = converted_contents; + if (conv_error != NULL) g_error_free(conv_error); + } + else if (conv_error != NULL || ! g_utf8_validate(converted_contents, bytes_written, NULL)) { if (conv_error != NULL) { @@ -395,7 +412,7 @@ gchar *utils_convert_to_utf8(const gchar *buffer, gsize size, gchar **used_encod charset = encodings[i].charset; geany_debug("Trying to convert %d bytes of data from %s into UTF-8.", size, charset); - utf8_content = utils_convert_to_utf8_from_charset(buffer, size, charset); + utf8_content = utils_convert_to_utf8_from_charset(buffer, size, charset, FALSE); if (utf8_content != NULL) { diff --git a/src/utils.h b/src/utils.h index a44d0224..4868b13e 100644 --- a/src/utils.h +++ b/src/utils.h @@ -46,7 +46,10 @@ gint utils_write_file(const gchar *filename, const gchar *text); gchar *utils_convert_to_utf8(const gchar *buffer, gsize size, gchar **used_encoding); -gchar *utils_convert_to_utf8_from_charset(const gchar *buffer, gsize size, const gchar *charset); +/* Converts a string from the given charset to UTF-8. + * If fast is set, no further checks are performed. */ +gchar *utils_convert_to_utf8_from_charset(const gchar *buffer, gsize size, const gchar *charset, + gboolean fast); /** * (stolen from anjuta and modified)