Revision: 3825 http://geany.svn.sourceforge.net/geany/?rev=3825&view=rev Author: ntrel Date: 2009-05-30 11:57:56 +0000 (Sat, 30 May 2009)
Log Message: ----------- Implement named styles support for filetypes.* using a filetypes.common [named_styles] section e.g.: foo=0xc00000;0xffffff;false;true bar=foo These can be used in e.g. filetypes.c as: comment=foo
Modified Paths: -------------- trunk/ChangeLog trunk/TODO trunk/src/highlighting.c trunk/src/utils.c
Modified: trunk/ChangeLog =================================================================== --- trunk/ChangeLog 2009-05-29 13:35:55 UTC (rev 3824) +++ trunk/ChangeLog 2009-05-30 11:57:56 UTC (rev 3825) @@ -1,3 +1,14 @@ +2009-05-30 Nick Treleaven <nick(dot)treleaven(at)btinternet(dot)com> + + * src/utils.c, src/highlighting.c, TODO: + Implement named styles support for filetypes.* using a + filetypes.common [named_styles] section e.g.: + foo=0xc00000;0xffffff;false;true + bar=foo + These can be used in e.g. filetypes.c as: + comment=foo + + 2009-05-28 Enrico Tröger <enrico(dot)troeger(at)uvena(dot)de>
* src/ui_utils.c:
Modified: trunk/TODO =================================================================== --- trunk/TODO 2009-05-29 13:35:55 UTC (rev 3824) +++ trunk/TODO 2009-05-30 11:57:56 UTC (rev 3825) @@ -9,7 +9,7 @@ Next version or later: o documentation: list and explain filetype modes o common default highlighting styles configurable for all - programming languages + programming languages (filetypes.common named styles now done) o configurable filetype and project make commands (e.g. using bud for D) o (support for adding plugin filetypes - SCI_LOADLEXERLIBRARY?)
Modified: trunk/src/highlighting.c =================================================================== --- trunk/src/highlighting.c 2009-05-29 13:35:55 UTC (rev 3824) +++ trunk/src/highlighting.c 2009-05-30 11:57:56 UTC (rev 3825) @@ -26,10 +26,12 @@ * Syntax highlighting for the different filetypes, using the Scintilla lexers. */
+#include "geany.h" + #include <stdlib.h> +#include <ctype.h>
#include "SciLexer.h" -#include "geany.h" #include "highlighting.h" #include "editor.h" #include "utils.h" @@ -51,10 +53,10 @@
typedef struct { - gsize count; /* number of styles */ + gsize count; /* number of styles */ GeanyLexerStyle *styling; /* array of styles, NULL if not used or uninitialised */ - gchar **keywords; - gchar *wordchars; /* NULL used for style sets with no styles */ + gchar **keywords; + gchar *wordchars; /* NULL used for style sets with no styles */ } StyleSet;
/* each filetype has a styleset except GEANY_FILETYPES_NONE, which uses common_style_set */ @@ -93,20 +95,42 @@ static struct { GeanyLexerStyle styling[GCS_MAX]; - FoldingStyle folding_style; - gboolean invert_all; - gchar *wordchars; + FoldingStyle folding_style; + gboolean invert_all; + gchar *wordchars; } common_style_set;
/* used for default styles */ typedef struct { - gchar *name; + gchar *name; GeanyLexerStyle *style; } StyleEntry;
+/* For filetypes.common [named_styles] section. + * e.g. "comment" => &GeanyLexerStyle{0xd00000, 0xffffff, FALSE, FALSE} */ +static GHashTable *named_style_hash = NULL; + + +/* Geany generic styles, initialized to defaults. + * Currently only used as default styling for C-like languages. + * Note: Ideally named styles would be used as common styling for all programming + * languages (and perhaps all filetypes). + * These could be replaced by default named styles in filetypes.common. */ +static GeanyLexerStyle gsd_default = {0x000000, 0xffffff, FALSE, FALSE}; +static GeanyLexerStyle gsd_comment = {0xd00000, 0xffffff, FALSE, FALSE}; +static GeanyLexerStyle gsd_comment_doc = {0x3f5fbf, 0xffffff, TRUE, FALSE}; +static GeanyLexerStyle gsd_number = {0x007f00, 0xffffff, FALSE, FALSE}; +static GeanyLexerStyle gsd_reserved_word = {0x00007f, 0xffffff, TRUE, FALSE}; +static GeanyLexerStyle gsd_system_word = {0x991111, 0xffffff, TRUE, FALSE}; +static GeanyLexerStyle gsd_user_word = {0x0000d0, 0xffffff, TRUE, FALSE}; +static GeanyLexerStyle gsd_string = {0xff901e, 0xffffff, FALSE, FALSE}; +static GeanyLexerStyle gsd_pragma = {0x007f7f, 0xffffff, FALSE, FALSE}; +static GeanyLexerStyle gsd_string_eol = {0x000000, 0xe0c0e0, FALSE, FALSE}; + + static void new_style_array(gint file_type_id, gint styling_count) { StyleSet *set = &style_sets[file_type_id]; @@ -165,6 +189,20 @@ }
+static void read_named_style(const gchar *name, GeanyLexerStyle *style) +{ + GeanyLexerStyle *cs = g_hash_table_lookup(named_style_hash, name); + + if (cs) + *style = *cs; + else + { + *style = gsd_default; + geany_debug("No named style '%s'! Check filetypes.common.", name); + } +} + + /* convert 0x..RRGGBB to 0x..BBGGRR */ static gint rotate_rgb(gint color) { @@ -174,24 +212,25 @@ }
-static void get_keyfile_style(GKeyFile *config, GKeyFile *configh, - const gchar *key_name, const GeanyLexerStyle *default_style, GeanyLexerStyle *style) +/* FIXME: is not safe for badly formed key e.g. "key='" */ +static void parse_keyfile_style(gchar **list, + const GeanyLexerStyle *default_style, GeanyLexerStyle *style) { - gchar **list; - gsize len; - - g_return_if_fail(config); - g_return_if_fail(configh); - g_return_if_fail(key_name); g_return_if_fail(default_style); g_return_if_fail(style);
- list = g_key_file_get_string_list(configh, "styling", key_name, &len, NULL); - if (list == NULL) - list = g_key_file_get_string_list(config, "styling", key_name, &len, NULL); + if (G_LIKELY(list != NULL) && G_UNLIKELY(list[0] != NULL)) + { + const gchar *str = list[0];
- if (G_LIKELY(list != NULL) && G_UNLIKELY(list[0] != NULL)) - style->foreground = (gint) utils_strtod(list[0], NULL, FALSE); + if (list[1] == NULL && isalpha(str[0])) + { + read_named_style(str, style); + return; + } + else + style->foreground = (gint) utils_strtod(str, NULL, FALSE); + } else style->foreground = rotate_rgb(default_style->foreground);
@@ -209,7 +248,26 @@ style->italic = utils_atob(list[3]); else style->italic = default_style->italic; +}
+ +static void get_keyfile_style(GKeyFile *config, GKeyFile *configh, + const gchar *key_name, const GeanyLexerStyle *default_style, GeanyLexerStyle *style) +{ + gchar **list; + gsize len; + + g_return_if_fail(config); + g_return_if_fail(configh); + g_return_if_fail(key_name); + g_return_if_fail(default_style); + g_return_if_fail(style); + + list = g_key_file_get_string_list(configh, "styling", key_name, &len, NULL); + if (list == NULL) + list = g_key_file_get_string_list(config, "styling", key_name, &len, NULL); + + parse_keyfile_style(list, default_style, style); g_strfreev(list); }
@@ -314,6 +372,9 @@ g_strfreev(style_ptr->keywords); g_free(style_ptr->wordchars); } + + if (named_style_hash) + g_hash_table_destroy(named_style_hash); }
@@ -355,6 +416,40 @@ }
+static void get_named_styles(GKeyFile *config) +{ + const gchar group[] = "named_styles"; + gchar **keys = g_key_file_get_keys(config, group, NULL, NULL); + gchar **ptr = keys; + + if (!ptr) + return; + + while (1) + { + gchar **list; + gsize len; + const gchar *key = *ptr; + + if (!key) + break; + + list = g_key_file_get_string_list(config, group, key, &len, NULL); + /* we allow a named style to reference another style above it */ + if (list && len >= 1) + { + GeanyLexerStyle *style = g_new0(GeanyLexerStyle, 1); + + parse_keyfile_style(list, &gsd_default, style); + g_hash_table_insert(named_style_hash, g_strdup(key), style); + } + g_strfreev(list); + ptr++; + } + g_strfreev(keys); +} + + static void styleset_common_init(gint ft_id, GKeyFile *config, GKeyFile *config_home) { static gboolean common_style_set_valid = FALSE; @@ -413,6 +508,11 @@ get_keyfile_wordchars(config, config_home, &common_style_set.wordchars); whitespace_chars = get_keyfile_whitespace_chars(config, config_home);
+ /* named styles */ + named_style_hash = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free); + get_named_styles(config); + /* home overwrites any system named style */ + get_named_styles(config_home); }
@@ -615,22 +715,6 @@ }
-/* Geany generic styles, initialized to defaults. - * Ideally these would be used as common styling for all compilable programming - * languages (and perhaps partially used for scripting languages too). - * Currently only used as default styling for C-like languages. */ -GeanyLexerStyle gsd_default = {0x000000, 0xffffff, FALSE, FALSE}; -GeanyLexerStyle gsd_comment = {0xd00000, 0xffffff, FALSE, FALSE}; -GeanyLexerStyle gsd_comment_doc = {0x3f5fbf, 0xffffff, TRUE, FALSE}; -GeanyLexerStyle gsd_number = {0x007f00, 0xffffff, FALSE, FALSE}; -GeanyLexerStyle gsd_reserved_word = {0x00007f, 0xffffff, TRUE, FALSE}; -GeanyLexerStyle gsd_system_word = {0x991111, 0xffffff, TRUE, FALSE}; -GeanyLexerStyle gsd_user_word = {0x0000d0, 0xffffff, TRUE, FALSE}; -GeanyLexerStyle gsd_string = {0xff901e, 0xffffff, FALSE, FALSE}; -GeanyLexerStyle gsd_pragma = {0x007f7f, 0xffffff, FALSE, FALSE}; -GeanyLexerStyle gsd_string_eol = {0x000000, 0xe0c0e0, FALSE, FALSE}; - - /* call new_style_array(filetype_idx, >= 20) before using this. */ static void styleset_c_like_init(GKeyFile *config, GKeyFile *config_home, gint filetype_idx)
Modified: trunk/src/utils.c =================================================================== --- trunk/src/utils.c 2009-05-29 13:35:55 UTC (rev 3824) +++ trunk/src/utils.c 2009-05-30 11:57:56 UTC (rev 3825) @@ -928,7 +928,7 @@ }
- guint utils_get_value_of_hex(const gchar ch) + static guint utils_get_value_of_hex(const gchar ch) { if (ch >= '0' && ch <= '9') return ch - '0';
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.