Merge pull request #613 from SiegeLord/even_more_rust_updates
[geany-mirror.git] / src / highlighting.c
blob0a700b629ae70c85099f2b3956b82ec942005c68
1 /*
2 * highlighting.c - this file is part of Geany, a fast and lightweight IDE
4 * Copyright 2005-2012 Enrico Tröger <enrico(dot)troeger(at)uvena(dot)de>
5 * Copyright 2006-2012 Nick Treleaven <nick(dot)treleaven(at)btinternet(dot)com>
6 * Copyright 2011-2012 Colomban Wendling <ban(at)herbesfolles(dot)org>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 /**
24 * @file highlighting.h
25 * Syntax highlighting for the different filetypes, using the Scintilla lexers.
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
32 #include "highlighting.h"
33 #include "highlightingmappings.h"
35 #include "app.h"
36 #include "dialogs.h"
37 #include "document.h"
38 #include "editor.h"
39 #include "filetypesprivate.h"
40 #include "sciwrappers.h"
41 #include "support.h"
42 #include "symbols.h"
43 #include "ui_utils.h"
44 #include "utils.h"
46 #include "SciLexer.h"
48 #include <stdlib.h>
49 #include <ctype.h>
50 #include <string.h>
51 #include <glib.h>
52 #include <glib/gprintf.h>
55 #define GEANY_COLORSCHEMES_SUBDIR "colorschemes"
57 /* Whitespace has to be set after setting wordchars. */
58 #define GEANY_WHITESPACE_CHARS " \t" "!\"#$%&'()*+,-./:;<=>?@[\\]^`{|}~"
61 static gchar *whitespace_chars = NULL;
64 typedef struct
66 gsize count; /* number of styles */
67 GeanyLexerStyle *styling; /* array of styles, NULL if not used or uninitialised */
68 gchar **keywords;
69 gchar *wordchars; /* NULL used for style sets with no styles */
70 gchar **property_keys;
71 gchar **property_values;
72 } StyleSet;
74 /* each filetype has a styleset but GEANY_FILETYPES_NONE uses common_style_set for styling */
75 static StyleSet *style_sets = NULL;
78 enum /* Geany common styling */
80 GCS_DEFAULT,
81 GCS_SELECTION,
82 GCS_BRACE_GOOD,
83 GCS_BRACE_BAD,
84 GCS_MARGIN_LINENUMBER,
85 GCS_MARGIN_FOLDING,
86 GCS_FOLD_SYMBOL_HIGHLIGHT,
87 GCS_CURRENT_LINE,
88 GCS_CARET,
89 GCS_INDENT_GUIDE,
90 GCS_WHITE_SPACE,
91 GCS_LINE_WRAP_VISUALS,
92 GCS_LINE_WRAP_INDENT,
93 GCS_TRANSLUCENCY,
94 GCS_MARKER_LINE,
95 GCS_MARKER_SEARCH,
96 GCS_MARKER_MARK,
97 GCS_MARKER_TRANSLUCENCY,
98 GCS_LINE_HEIGHT,
99 GCS_CALLTIPS,
100 GCS_MAX
103 static struct
105 GeanyLexerStyle styling[GCS_MAX];
107 /* icon style, 1-4 */
108 gint fold_marker;
109 /* vertical line style, 0-2 */
110 gint fold_lines;
111 /* horizontal line when folded, 0-2 */
112 gint fold_draw_line;
114 gchar *wordchars;
115 } common_style_set = { { { 0 } }, 0, 0, 0, NULL };
118 /* For filetypes.common [named_styles] section.
119 * 0xBBGGRR format.
120 * e.g. "comment" => &GeanyLexerStyle{0x0000d0, 0xffffff, FALSE, FALSE} */
121 static GHashTable *named_style_hash = NULL;
123 /* 0xBBGGRR format, set by "default" named style. */
124 static GeanyLexerStyle gsd_default = {0x000000, 0xffffff, FALSE, FALSE};
127 /* Note: use sciwrappers.h instead where possible.
128 * Do not use SSM in files unrelated to scintilla. */
129 #define SSM(s, m, w, l) scintilla_send_message(s, m, w, l)
131 /* filetypes should use the filetypes.foo [lexer_properties] group instead of hardcoding */
132 static void sci_set_property(ScintillaObject *sci, const gchar *name, const gchar *value)
134 SSM(sci, SCI_SETPROPERTY, (uptr_t) name, (sptr_t) value);
138 static void new_styleset(guint file_type_id, gsize styling_count)
140 StyleSet *set = &style_sets[file_type_id];
142 set->count = styling_count;
143 set->styling = g_new0(GeanyLexerStyle, styling_count);
147 static void free_styleset(guint file_type_id)
149 StyleSet *style_ptr;
150 style_ptr = &style_sets[file_type_id];
152 style_ptr->count = 0;
153 g_free(style_ptr->styling);
154 style_ptr->styling = NULL;
155 g_strfreev(style_ptr->keywords);
156 style_ptr->keywords = NULL;
157 g_free(style_ptr->wordchars);
158 style_ptr->wordchars = NULL;
159 g_strfreev(style_ptr->property_keys);
160 style_ptr->property_keys = NULL;
161 g_strfreev(style_ptr->property_values);
162 style_ptr->property_values = NULL;
166 static void get_keyfile_keywords(GKeyFile *config, GKeyFile *configh,
167 const gchar *key, guint ft_id, guint pos)
169 style_sets[ft_id].keywords[pos] =
170 utils_get_setting(string, configh, config, "keywords", key, "");
174 static void get_keyfile_wordchars(GKeyFile *config, GKeyFile *configh, gchar **wordchars,
175 const gchar *default_wordchars)
177 *wordchars = utils_get_setting(string, configh, config,
178 "settings", "wordchars", default_wordchars);
182 static gboolean read_named_style(const gchar *named_style, GeanyLexerStyle *style)
184 GeanyLexerStyle *cs;
185 gchar *comma, *name = NULL;
186 const gchar *bold = NULL;
187 const gchar *italic = NULL;
189 g_return_val_if_fail(named_style, FALSE);
190 name = utils_strdupa(named_style); /* named_style must not be written to, may be a static string */
192 comma = strstr(name, ",");
193 if (comma)
195 bold = strstr(comma, ",bold");
196 italic = strstr(comma, ",italic");
197 *comma = '\0'; /* terminate name to make lookup work */
199 cs = g_hash_table_lookup(named_style_hash, name);
201 if (cs)
203 *style = *cs;
204 if (bold)
205 style->bold = !style->bold;
206 if (italic)
207 style->italic = !style->italic;
209 else
211 *style = gsd_default;
213 return (cs != NULL);
217 /* Parses a color in `str` which can be an HTML color (ex. #0099cc),
218 * an abbreviated HTML color (ex. #09c) or a hex string color
219 * (ex. 0x0099cc). The result of the conversion is stored into the
220 * location pointed to by `clr`. */
221 static void parse_color(GKeyFile *kf, const gchar *str, gint *clr)
223 gint c;
224 gchar *named_color = NULL;
226 g_return_if_fail(clr != NULL);
228 if (G_UNLIKELY(EMPTY(str)))
229 return;
231 named_color = g_key_file_get_string(kf, "named_colors", str, NULL);
232 if (named_color)
233 str = named_color;
235 c = utils_parse_color_to_bgr(str);
236 if (c == -1)
237 geany_debug("Bad color '%s'", str);
238 else
239 *clr = c;
241 g_free(named_color);
245 static void parse_keyfile_style(GKeyFile *kf, gchar **list,
246 const GeanyLexerStyle *default_style, GeanyLexerStyle *style)
248 gsize len;
250 g_return_if_fail(default_style);
251 g_return_if_fail(style);
253 *style = *default_style;
255 if (!list)
256 return;
258 len = g_strv_length(list);
259 if (len == 0)
260 return;
261 else if (len == 1)
263 gchar **items = g_strsplit(list[0], ",", 0);
264 if (items != NULL)
266 if (g_strv_length(items) > 0)
268 if (g_hash_table_lookup(named_style_hash, items[0]) != NULL)
270 if (!read_named_style(list[0], style))
271 geany_debug("Unable to read named style '%s'", items[0]);
272 g_strfreev(items);
273 return;
275 else if (strchr(list[0], ',') != NULL)
277 geany_debug("Unknown named style '%s'", items[0]);
278 g_strfreev(items);
279 return;
282 g_strfreev(items);
286 switch (len)
288 case 4:
289 style->italic = utils_atob(list[3]);
290 case 3:
291 style->bold = utils_atob(list[2]);
292 case 2:
293 parse_color(kf, list[1], &style->background);
294 case 1:
295 parse_color(kf, list[0], &style->foreground);
300 static void get_keyfile_style(GKeyFile *config, GKeyFile *configh,
301 const gchar *key_name, GeanyLexerStyle *style)
303 gchar **list;
304 gsize len;
306 g_return_if_fail(config);
307 g_return_if_fail(configh);
308 g_return_if_fail(key_name);
309 g_return_if_fail(style);
311 list = g_key_file_get_string_list(configh, "styling", key_name, &len, NULL);
312 if (list == NULL)
314 list = g_key_file_get_string_list(config, "styling", key_name, &len, NULL);
315 parse_keyfile_style(config, list, &gsd_default, style);
317 else
318 parse_keyfile_style(configh, list, &gsd_default, style);
320 g_strfreev(list);
324 static void convert_int(const gchar *int_str, gint *val)
326 gchar *end;
327 gint v = strtol(int_str, &end, 10);
329 if (int_str != end)
330 *val = v;
334 /* Get first and second integer numbers, store in foreground and background fields of @a style. */
335 static void get_keyfile_int(GKeyFile *config, GKeyFile *configh, const gchar *section,
336 const gchar *key, gint fdefault_val, gint sdefault_val,
337 GeanyLexerStyle *style)
339 gchar **list;
340 gsize len;
341 GeanyLexerStyle def = {fdefault_val, sdefault_val, FALSE, FALSE};
343 g_return_if_fail(config);
344 g_return_if_fail(configh);
345 g_return_if_fail(section);
346 g_return_if_fail(key);
348 list = g_key_file_get_string_list(configh, section, key, &len, NULL);
349 if (list == NULL)
350 list = g_key_file_get_string_list(config, section, key, &len, NULL);
352 *style = def;
353 if (!list)
354 return;
356 if (list[0])
358 convert_int(list[0], &style->foreground);
359 if (list[1])
361 convert_int(list[1], &style->background);
364 g_strfreev(list);
368 /* first or second can be NULL. */
369 static void get_keyfile_ints(GKeyFile *config, GKeyFile *configh, const gchar *section,
370 const gchar *key,
371 gint fdefault_val, gint sdefault_val,
372 gint *first, gint *second)
374 GeanyLexerStyle tmp_style;
376 get_keyfile_int(config, configh, section, key, fdefault_val, sdefault_val, &tmp_style);
377 if (first)
378 *first = tmp_style.foreground;
379 if (second)
380 *second = tmp_style.background;
384 static guint invert(guint icolour)
386 if (interface_prefs.highlighting_invert_all)
387 return 0xffffff - icolour;
389 return icolour;
393 static GeanyLexerStyle *get_style(guint ft_id, guint styling_index)
395 g_assert(ft_id < filetypes_array->len);
397 if (G_UNLIKELY(ft_id == GEANY_FILETYPES_NONE))
399 g_assert(styling_index < GCS_MAX);
400 return &common_style_set.styling[styling_index];
402 else
404 StyleSet *set = &style_sets[ft_id];
406 g_assert(styling_index < set->count);
407 return &set->styling[styling_index];
412 static void set_sci_style(ScintillaObject *sci, guint style, guint ft_id, guint styling_index)
414 GeanyLexerStyle *style_ptr = get_style(ft_id, styling_index);
416 SSM(sci, SCI_STYLESETFORE, style, invert(style_ptr->foreground));
417 SSM(sci, SCI_STYLESETBACK, style, invert(style_ptr->background));
418 SSM(sci, SCI_STYLESETBOLD, style, style_ptr->bold);
419 SSM(sci, SCI_STYLESETITALIC, style, style_ptr->italic);
423 void highlighting_free_styles(void)
425 guint i;
427 for (i = 0; i < filetypes_array->len; i++)
428 free_styleset(i);
430 if (named_style_hash)
431 g_hash_table_destroy(named_style_hash);
433 g_free(style_sets);
437 static gchar*
438 get_keyfile_whitespace_chars(GKeyFile *config, GKeyFile *configh)
440 return utils_get_setting(string, configh, config,
441 "settings", "whitespace_chars", GEANY_WHITESPACE_CHARS);
445 static void add_named_style(GKeyFile *config, const gchar *key)
447 const gchar group[] = "named_styles";
448 gchar **list;
449 gsize len;
451 list = g_key_file_get_string_list(config, group, key, &len, NULL);
452 /* we allow a named style to reference another style above it */
453 if (list && len >= 1)
455 GeanyLexerStyle *style = g_new0(GeanyLexerStyle, 1);
457 parse_keyfile_style(config, list, &gsd_default, style);
458 g_hash_table_insert(named_style_hash, g_strdup(key), style);
460 g_strfreev(list);
464 static void get_named_styles(GKeyFile *config)
466 const gchar group[] = "named_styles";
467 gchar **keys = g_key_file_get_keys(config, group, NULL, NULL);
468 gchar **ptr = keys;
470 if (!ptr)
471 return;
473 while (1)
475 const gchar *key = *ptr;
477 if (!key)
478 break;
480 /* don't replace already read default style with system one */
481 if (!g_str_equal(key, "default"))
482 add_named_style(config, key);
484 ptr++;
486 g_strfreev(keys);
490 static GKeyFile *utils_key_file_new(const gchar *filename)
492 GKeyFile *config = g_key_file_new();
494 g_key_file_load_from_file(config, filename, G_KEY_FILE_KEEP_COMMENTS, NULL);
495 return config;
499 static void load_named_styles(GKeyFile *config, GKeyFile *config_home)
501 const gchar *scheme = editor_prefs.color_scheme;
502 gboolean free_kf = FALSE;
504 if (named_style_hash)
505 g_hash_table_destroy(named_style_hash); /* reloading */
507 named_style_hash = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free);
509 if (!EMPTY(scheme))
511 gchar *path, *path_home;
513 path = g_build_path(G_DIR_SEPARATOR_S, app->datadir, GEANY_COLORSCHEMES_SUBDIR, scheme, NULL);
514 path_home = g_build_path(G_DIR_SEPARATOR_S, app->configdir, GEANY_COLORSCHEMES_SUBDIR, scheme, NULL);
516 if (g_file_test(path, G_FILE_TEST_EXISTS) || g_file_test(path_home, G_FILE_TEST_EXISTS))
518 config = utils_key_file_new(path);
519 config_home = utils_key_file_new(path_home);
520 free_kf = TRUE;
522 /* if color scheme is missing, use default */
523 g_free(path);
524 g_free(path_home);
526 /* first set default to the "default" named style */
527 add_named_style(config, "default");
528 read_named_style("default", &gsd_default); /* in case user overrides but not with both colors */
529 add_named_style(config_home, "default");
530 read_named_style("default", &gsd_default);
532 get_named_styles(config);
533 /* home overrides any system named style */
534 get_named_styles(config_home);
536 if (free_kf)
538 g_key_file_free(config);
539 g_key_file_free(config_home);
544 static void styleset_common_init(GKeyFile *config, GKeyFile *config_home)
546 load_named_styles(config, config_home);
548 get_keyfile_style(config, config_home, "default", &common_style_set.styling[GCS_DEFAULT]);
549 get_keyfile_style(config, config_home, "selection", &common_style_set.styling[GCS_SELECTION]);
550 get_keyfile_style(config, config_home, "brace_good", &common_style_set.styling[GCS_BRACE_GOOD]);
551 get_keyfile_style(config, config_home, "brace_bad", &common_style_set.styling[GCS_BRACE_BAD]);
552 get_keyfile_style(config, config_home, "margin_linenumber", &common_style_set.styling[GCS_MARGIN_LINENUMBER]);
553 get_keyfile_style(config, config_home, "margin_folding", &common_style_set.styling[GCS_MARGIN_FOLDING]);
554 get_keyfile_style(config, config_home, "fold_symbol_highlight", &common_style_set.styling[GCS_FOLD_SYMBOL_HIGHLIGHT]);
555 get_keyfile_style(config, config_home, "current_line", &common_style_set.styling[GCS_CURRENT_LINE]);
556 get_keyfile_style(config, config_home, "caret", &common_style_set.styling[GCS_CARET]);
557 get_keyfile_style(config, config_home, "indent_guide", &common_style_set.styling[GCS_INDENT_GUIDE]);
558 get_keyfile_style(config, config_home, "white_space", &common_style_set.styling[GCS_WHITE_SPACE]);
559 get_keyfile_style(config, config_home, "marker_line", &common_style_set.styling[GCS_MARKER_LINE]);
560 get_keyfile_style(config, config_home, "marker_search", &common_style_set.styling[GCS_MARKER_SEARCH]);
561 get_keyfile_style(config, config_home, "marker_mark", &common_style_set.styling[GCS_MARKER_MARK]);
562 get_keyfile_style(config, config_home, "calltips", &common_style_set.styling[GCS_CALLTIPS]);
564 get_keyfile_ints(config, config_home, "styling", "folding_style",
565 1, 1, &common_style_set.fold_marker, &common_style_set.fold_lines);
566 get_keyfile_ints(config, config_home, "styling", "folding_horiz_line",
567 2, 0, &common_style_set.fold_draw_line, NULL);
568 get_keyfile_ints(config, config_home, "styling", "caret_width",
569 1, 0, &common_style_set.styling[GCS_CARET].background, NULL); /* caret.foreground used earlier */
570 get_keyfile_int(config, config_home, "styling", "line_wrap_visuals",
571 3, 0, &common_style_set.styling[GCS_LINE_WRAP_VISUALS]);
572 get_keyfile_int(config, config_home, "styling", "line_wrap_indent",
573 0, 0, &common_style_set.styling[GCS_LINE_WRAP_INDENT]);
574 get_keyfile_int(config, config_home, "styling", "translucency",
575 256, 256, &common_style_set.styling[GCS_TRANSLUCENCY]);
576 get_keyfile_int(config, config_home, "styling", "marker_translucency",
577 256, 256, &common_style_set.styling[GCS_MARKER_TRANSLUCENCY]);
578 get_keyfile_int(config, config_home, "styling", "line_height",
579 0, 0, &common_style_set.styling[GCS_LINE_HEIGHT]);
581 g_free(common_style_set.wordchars);
582 get_keyfile_wordchars(config, config_home, &common_style_set.wordchars, GEANY_WORDCHARS);
583 g_free(whitespace_chars);
584 whitespace_chars = get_keyfile_whitespace_chars(config, config_home);
588 static void set_character_classes(ScintillaObject *sci, guint ft_id)
590 const gchar *word = (ft_id == GEANY_FILETYPES_NONE ?
591 common_style_set.wordchars : style_sets[ft_id].wordchars);
592 gchar *whitespace;
593 guint i, j;
595 SSM(sci, SCI_SETWORDCHARS, 0, (sptr_t) word);
597 /* setting wordchars resets character classes, so we have to set whitespaces after
598 * wordchars, but we want wordchars to have precenence over whitepace chars */
599 whitespace = g_malloc0(strlen(whitespace_chars) + 1);
600 for (i = 0, j = 0; whitespace_chars[i] != 0; i++)
602 if (! strchr(word, whitespace_chars[i]))
603 whitespace[j++] = whitespace_chars[i];
605 whitespace[j] = 0;
607 SSM(sci, SCI_SETWHITESPACECHARS, 0, (sptr_t) whitespace);
609 g_free(whitespace);
613 static void styleset_common(ScintillaObject *sci, guint ft_id)
615 GeanyLexerStyle *style;
617 SSM(sci, SCI_STYLECLEARALL, 0, 0);
619 set_character_classes(sci, ft_id);
621 /* caret colour, style and width */
622 SSM(sci, SCI_SETCARETFORE, invert(common_style_set.styling[GCS_CARET].foreground), 0);
623 SSM(sci, SCI_SETCARETWIDTH, common_style_set.styling[GCS_CARET].background, 0);
624 if (common_style_set.styling[GCS_CARET].bold)
625 SSM(sci, SCI_SETCARETSTYLE, CARETSTYLE_BLOCK, 0);
626 else
627 SSM(sci, SCI_SETCARETSTYLE, CARETSTYLE_LINE, 0);
629 /* line height */
630 SSM(sci, SCI_SETEXTRAASCENT, common_style_set.styling[GCS_LINE_HEIGHT].foreground, 0);
631 SSM(sci, SCI_SETEXTRADESCENT, common_style_set.styling[GCS_LINE_HEIGHT].background, 0);
633 /* colourise the current line */
634 SSM(sci, SCI_SETCARETLINEBACK, invert(common_style_set.styling[GCS_CURRENT_LINE].background), 0);
635 /* bold=enable current line */
636 SSM(sci, SCI_SETCARETLINEVISIBLE, common_style_set.styling[GCS_CURRENT_LINE].bold, 0);
638 /* Translucency for current line and selection */
639 SSM(sci, SCI_SETCARETLINEBACKALPHA, common_style_set.styling[GCS_TRANSLUCENCY].foreground, 0);
640 SSM(sci, SCI_SETSELALPHA, common_style_set.styling[GCS_TRANSLUCENCY].background, 0);
642 /* line wrapping visuals */
643 SSM(sci, SCI_SETWRAPVISUALFLAGS,
644 common_style_set.styling[GCS_LINE_WRAP_VISUALS].foreground, 0);
645 SSM(sci, SCI_SETWRAPVISUALFLAGSLOCATION,
646 common_style_set.styling[GCS_LINE_WRAP_VISUALS].background, 0);
647 SSM(sci, SCI_SETWRAPSTARTINDENT, common_style_set.styling[GCS_LINE_WRAP_INDENT].foreground, 0);
648 SSM(sci, SCI_SETWRAPINDENTMODE, common_style_set.styling[GCS_LINE_WRAP_INDENT].background, 0);
650 /* Error indicator */
651 SSM(sci, SCI_INDICSETSTYLE, GEANY_INDICATOR_ERROR, INDIC_SQUIGGLEPIXMAP);
652 SSM(sci, SCI_INDICSETFORE, GEANY_INDICATOR_ERROR, invert(0x0000FF /* red, in BGR */));
654 /* Search indicator, used for 'Mark' matches */
655 SSM(sci, SCI_INDICSETSTYLE, GEANY_INDICATOR_SEARCH, INDIC_ROUNDBOX);
656 SSM(sci, SCI_INDICSETFORE, GEANY_INDICATOR_SEARCH,
657 invert(common_style_set.styling[GCS_MARKER_SEARCH].background));
658 SSM(sci, SCI_INDICSETALPHA, GEANY_INDICATOR_SEARCH, 60);
660 /* define marker symbols
661 * 0 -> line marker */
662 SSM(sci, SCI_MARKERDEFINE, 0, SC_MARK_SHORTARROW);
663 SSM(sci, SCI_MARKERSETFORE, 0, invert(common_style_set.styling[GCS_MARKER_LINE].foreground));
664 SSM(sci, SCI_MARKERSETBACK, 0, invert(common_style_set.styling[GCS_MARKER_LINE].background));
665 SSM(sci, SCI_MARKERSETALPHA, 0, common_style_set.styling[GCS_MARKER_TRANSLUCENCY].foreground);
667 /* 1 -> user marker */
668 SSM(sci, SCI_MARKERDEFINE, 1, SC_MARK_PLUS);
669 SSM(sci, SCI_MARKERSETFORE, 1, invert(common_style_set.styling[GCS_MARKER_MARK].foreground));
670 SSM(sci, SCI_MARKERSETBACK, 1, invert(common_style_set.styling[GCS_MARKER_MARK].background));
671 SSM(sci, SCI_MARKERSETALPHA, 1, common_style_set.styling[GCS_MARKER_TRANSLUCENCY].background);
673 /* 2 -> folding marker, other folding settings */
674 SSM(sci, SCI_SETMARGINTYPEN, 2, SC_MARGIN_SYMBOL);
675 SSM(sci, SCI_SETMARGINMASKN, 2, SC_MASK_FOLDERS);
677 /* drawing a horizontal line when text if folded */
678 switch (common_style_set.fold_draw_line)
680 case 1:
682 SSM(sci, SCI_SETFOLDFLAGS, 4, 0);
683 break;
685 case 2:
687 SSM(sci, SCI_SETFOLDFLAGS, 16, 0);
688 break;
690 default:
692 SSM(sci, SCI_SETFOLDFLAGS, 0, 0);
693 break;
697 /* choose the folding style - boxes or circles, I prefer boxes, so it is default ;-) */
698 SSM(sci, SCI_MARKERDEFINE, SC_MARKNUM_FOLDEREND, SC_MARK_EMPTY);
699 SSM(sci, SCI_MARKERDEFINE, SC_MARKNUM_FOLDEROPENMID, SC_MARK_EMPTY);
700 switch (common_style_set.fold_marker)
702 case 2:
703 SSM(sci, SCI_MARKERDEFINE, SC_MARKNUM_FOLDEROPEN, SC_MARK_CIRCLEMINUS);
704 SSM(sci, SCI_MARKERDEFINE, SC_MARKNUM_FOLDER, SC_MARK_CIRCLEPLUS);
705 SSM(sci, SCI_MARKERDEFINE, SC_MARKNUM_FOLDEREND, SC_MARK_CIRCLEPLUSCONNECTED);
706 SSM(sci, SCI_MARKERDEFINE, SC_MARKNUM_FOLDEROPENMID, SC_MARK_CIRCLEMINUSCONNECTED);
707 break;
708 default:
709 SSM(sci, SCI_MARKERDEFINE, SC_MARKNUM_FOLDEROPEN, SC_MARK_BOXMINUS);
710 SSM(sci, SCI_MARKERDEFINE, SC_MARKNUM_FOLDER, SC_MARK_BOXPLUS);
711 SSM(sci, SCI_MARKERDEFINE, SC_MARKNUM_FOLDEREND, SC_MARK_BOXPLUSCONNECTED);
712 SSM(sci, SCI_MARKERDEFINE, SC_MARKNUM_FOLDEROPENMID, SC_MARK_BOXMINUSCONNECTED);
713 break;
714 case 3:
715 SSM(sci, SCI_MARKERDEFINE, SC_MARKNUM_FOLDEROPEN, SC_MARK_ARROWDOWN);
716 SSM(sci, SCI_MARKERDEFINE, SC_MARKNUM_FOLDER, SC_MARK_ARROW);
717 break;
718 case 4:
719 SSM(sci, SCI_MARKERDEFINE, SC_MARKNUM_FOLDEROPEN, SC_MARK_MINUS);
720 SSM(sci, SCI_MARKERDEFINE, SC_MARKNUM_FOLDER, SC_MARK_PLUS);
721 break;
724 /* choose the folding style - straight or curved, I prefer straight, so it is default ;-) */
725 switch (common_style_set.fold_lines)
727 case 2:
728 SSM(sci, SCI_MARKERDEFINE, SC_MARKNUM_FOLDERMIDTAIL, SC_MARK_TCORNERCURVE);
729 SSM(sci, SCI_MARKERDEFINE, SC_MARKNUM_FOLDERTAIL, SC_MARK_LCORNERCURVE);
730 SSM(sci, SCI_MARKERDEFINE, SC_MARKNUM_FOLDERSUB, SC_MARK_VLINE);
731 break;
732 default:
733 SSM(sci, SCI_MARKERDEFINE, SC_MARKNUM_FOLDERMIDTAIL, SC_MARK_TCORNER);
734 SSM(sci, SCI_MARKERDEFINE, SC_MARKNUM_FOLDERTAIL, SC_MARK_LCORNER);
735 SSM(sci, SCI_MARKERDEFINE, SC_MARKNUM_FOLDERSUB, SC_MARK_VLINE);
736 break;
737 case 0:
738 SSM(sci, SCI_MARKERDEFINE, SC_MARKNUM_FOLDERMIDTAIL, SC_MARK_EMPTY);
739 SSM(sci, SCI_MARKERDEFINE, SC_MARKNUM_FOLDERTAIL, SC_MARK_EMPTY);
740 SSM(sci, SCI_MARKERDEFINE, SC_MARKNUM_FOLDERSUB, SC_MARK_EMPTY);
741 break;
744 gint markers[] = {
745 SC_MARKNUM_FOLDEROPEN,
746 SC_MARKNUM_FOLDER,
747 SC_MARKNUM_FOLDERSUB,
748 SC_MARKNUM_FOLDERTAIL,
749 SC_MARKNUM_FOLDEREND,
750 SC_MARKNUM_FOLDEROPENMID,
751 SC_MARKNUM_FOLDERMIDTAIL
753 guint i;
755 foreach_range(i, G_N_ELEMENTS(markers))
757 SSM(sci, SCI_MARKERSETFORE, markers[i],
758 invert(common_style_set.styling[GCS_FOLD_SYMBOL_HIGHLIGHT].foreground));
759 SSM(sci, SCI_MARKERSETBACK, markers[i],
760 invert(common_style_set.styling[GCS_MARGIN_FOLDING].foreground));
764 /* set some common defaults */
765 sci_set_property(sci, "fold", "1");
766 sci_set_property(sci, "fold.compact", "0");
767 sci_set_property(sci, "fold.comment", "1");
768 sci_set_property(sci, "fold.preprocessor", "1");
769 sci_set_property(sci, "fold.at.else", "1");
771 style = &common_style_set.styling[GCS_SELECTION];
772 if (!style->bold && !style->italic)
774 geany_debug("selection style is set to invisible - ignoring!");
775 style->italic = TRUE;
776 style->background = 0xc0c0c0;
778 /* bold (3rd argument) is whether to override default foreground selection */
779 SSM(sci, SCI_SETSELFORE, style->bold, invert(style->foreground));
780 /* italic (4th argument) is whether to override default background selection */
781 SSM(sci, SCI_SETSELBACK, style->italic, invert(style->background));
783 SSM(sci, SCI_SETSTYLEBITS, SSM(sci, SCI_GETSTYLEBITSNEEDED, 0, 0), 0);
785 SSM(sci, SCI_SETFOLDMARGINCOLOUR, 1, invert(common_style_set.styling[GCS_MARGIN_FOLDING].background));
786 SSM(sci, SCI_SETFOLDMARGINHICOLOUR, 1, invert(common_style_set.styling[GCS_MARGIN_FOLDING].background));
787 set_sci_style(sci, STYLE_LINENUMBER, GEANY_FILETYPES_NONE, GCS_MARGIN_LINENUMBER);
788 set_sci_style(sci, STYLE_BRACELIGHT, GEANY_FILETYPES_NONE, GCS_BRACE_GOOD);
789 set_sci_style(sci, STYLE_BRACEBAD, GEANY_FILETYPES_NONE, GCS_BRACE_BAD);
790 set_sci_style(sci, STYLE_INDENTGUIDE, GEANY_FILETYPES_NONE, GCS_INDENT_GUIDE);
792 /* bold = common whitespace settings enabled */
793 SSM(sci, SCI_SETWHITESPACEFORE, common_style_set.styling[GCS_WHITE_SPACE].bold,
794 invert(common_style_set.styling[GCS_WHITE_SPACE].foreground));
795 SSM(sci, SCI_SETWHITESPACEBACK, common_style_set.styling[GCS_WHITE_SPACE].italic,
796 invert(common_style_set.styling[GCS_WHITE_SPACE].background));
798 if (common_style_set.styling[GCS_CALLTIPS].bold)
799 SSM(sci, SCI_CALLTIPSETFORE, invert(common_style_set.styling[GCS_CALLTIPS].foreground), 1);
800 if (common_style_set.styling[GCS_CALLTIPS].italic)
801 SSM(sci, SCI_CALLTIPSETBACK, invert(common_style_set.styling[GCS_CALLTIPS].background), 1);
805 /* Merge & assign global typedefs and user secondary keywords.
806 * keyword_idx is used for both style_sets[].keywords and scintilla keyword style number */
807 static void merge_type_keywords(ScintillaObject *sci, guint ft_id, guint keyword_idx)
809 const gchar *user_words = style_sets[ft_id].keywords[keyword_idx];
810 GString *s;
812 s = symbols_find_typenames_as_string(filetypes[ft_id]->lang, TRUE);
813 if (G_UNLIKELY(s == NULL))
814 s = g_string_sized_new(200);
815 else
816 g_string_append_c(s, ' '); /* append a space as delimiter to the existing list of words */
818 g_string_append(s, user_words);
820 sci_set_keywords(sci, keyword_idx, s->str);
821 g_string_free(s, TRUE);
825 static void styleset_init_from_mapping(guint ft_id, GKeyFile *config, GKeyFile *config_home,
826 const HLStyle *styles, gsize n_styles,
827 const HLKeyword *keywords, gsize n_keywords)
829 gsize i;
831 /* styles */
832 new_styleset(ft_id, n_styles);
833 foreach_range(i, n_styles)
835 GeanyLexerStyle *style = &style_sets[ft_id].styling[i];
837 get_keyfile_style(config, config_home, styles[i].name, style);
840 /* keywords */
841 if (n_keywords < 1)
842 style_sets[ft_id].keywords = NULL;
843 else
845 style_sets[ft_id].keywords = g_new(gchar*, n_keywords + 1);
846 foreach_range(i, n_keywords)
847 get_keyfile_keywords(config, config_home, keywords[i].key, ft_id, i);
848 style_sets[ft_id].keywords[i] = NULL;
853 /* STYLE_DEFAULT will be set to match the first style. */
854 static void styleset_from_mapping(ScintillaObject *sci, guint ft_id, guint lexer,
855 const HLStyle *styles, gsize n_styles,
856 const HLKeyword *keywords, gsize n_keywords,
857 const HLProperty *properties, gsize n_properties)
859 gsize i;
861 g_assert(ft_id != GEANY_FILETYPES_NONE);
863 /* lexer */
864 sci_set_lexer(sci, lexer);
866 /* styles */
867 styleset_common(sci, ft_id);
868 if (n_styles > 0)
870 /* first style is also default one */
871 set_sci_style(sci, STYLE_DEFAULT, ft_id, 0);
872 foreach_range(i, n_styles)
874 if (styles[i].fill_eol)
875 SSM(sci, SCI_STYLESETEOLFILLED, styles[i].style, TRUE);
876 set_sci_style(sci, styles[i].style, ft_id, i);
880 /* keywords */
881 foreach_range(i, n_keywords)
883 if (keywords[i].merge)
884 merge_type_keywords(sci, ft_id, i);
885 else
886 sci_set_keywords(sci, keywords[i].id, style_sets[ft_id].keywords[i]);
889 /* properties */
890 foreach_range(i, n_properties)
891 sci_set_property(sci, properties[i].property, properties[i].value);
896 static void styleset_default(ScintillaObject *sci, guint ft_id)
898 sci_set_lexer(sci, SCLEX_NULL);
900 /* we need to set STYLE_DEFAULT before we call SCI_STYLECLEARALL in styleset_common() */
901 set_sci_style(sci, STYLE_DEFAULT, GEANY_FILETYPES_NONE, GCS_DEFAULT);
903 styleset_common(sci, ft_id);
907 static void get_key_values(GKeyFile *config, const gchar *group, gchar **keys, gchar **values)
909 while (*keys)
911 gchar *str = g_key_file_get_string(config, group, *keys, NULL);
913 if (str)
914 SETPTR(*values, str);
916 keys++;
917 values++;
922 static void read_properties(GeanyFiletype *ft, GKeyFile *config, GKeyFile *configh)
924 gchar group[] = "lexer_properties";
925 gchar **keys;
926 gchar **keysh = g_key_file_get_keys(configh, group, NULL, NULL);
927 gchar **ptr;
929 /* remove overridden keys from system keyfile */
930 foreach_strv(ptr, keysh)
931 g_key_file_remove_key(config, group, *ptr, NULL);
933 /* merge sys and user keys */
934 keys = g_key_file_get_keys(config, group, NULL, NULL);
935 keys = utils_strv_join(keys, keysh);
937 if (keys)
939 gchar **values = g_new0(gchar*, g_strv_length(keys) + 1);
941 style_sets[ft->id].property_keys = keys;
942 style_sets[ft->id].property_values = values;
944 get_key_values(config, group, keys, values);
945 get_key_values(configh, group, keys, values);
950 static guint get_lexer_filetype(GeanyFiletype *ft)
952 ft = FALLBACK(ft->lexer_filetype, ft);
953 return ft->id;
957 #define init_styleset_case(LANG_NAME) \
958 case (GEANY_FILETYPES_##LANG_NAME): \
959 styleset_init_from_mapping(filetype_idx, config, configh, \
960 highlighting_styles_##LANG_NAME, \
961 HL_N_ENTRIES(highlighting_styles_##LANG_NAME), \
962 highlighting_keywords_##LANG_NAME, \
963 HL_N_ENTRIES(highlighting_keywords_##LANG_NAME)); \
964 break
966 /* Called by filetypes_load_config(). */
967 void highlighting_init_styles(guint filetype_idx, GKeyFile *config, GKeyFile *configh)
969 GeanyFiletype *ft = filetypes[filetype_idx];
970 guint lexer_id = get_lexer_filetype(ft);
971 gchar *default_str;
973 if (!style_sets)
974 style_sets = g_new0(StyleSet, filetypes_array->len);
976 /* Clear old information if necessary - e.g. when reloading config */
977 free_styleset(filetype_idx);
979 read_properties(ft, config, configh);
980 /* If a default style exists, check it uses a named style
981 * Note: almost all filetypes have a "default" style, except HTML ones */
982 default_str = utils_get_setting(string, configh, config,
983 "styling", "default", "default");
984 ft->priv->warn_color_scheme = !g_ascii_isalpha(*default_str);
985 g_free(default_str);
987 /* None filetype handled specially */
988 if (filetype_idx == GEANY_FILETYPES_NONE)
990 styleset_common_init(config, configh);
991 return;
993 /* All stylesets depend on filetypes.common */
994 filetypes_load_config(GEANY_FILETYPES_NONE, FALSE);
996 switch (lexer_id)
998 init_styleset_case(ABAQUS);
999 init_styleset_case(ADA);
1000 init_styleset_case(ASM);
1001 init_styleset_case(BASIC);
1002 init_styleset_case(BATCH);
1003 init_styleset_case(C);
1004 init_styleset_case(CAML);
1005 init_styleset_case(CMAKE);
1006 init_styleset_case(COBOL);
1007 init_styleset_case(COFFEESCRIPT);
1008 init_styleset_case(CONF);
1009 init_styleset_case(CSS);
1010 init_styleset_case(D);
1011 init_styleset_case(DIFF);
1012 init_styleset_case(LISP);
1013 init_styleset_case(ERLANG);
1014 init_styleset_case(DOCBOOK);
1015 init_styleset_case(FERITE);
1016 init_styleset_case(F77);
1017 init_styleset_case(FORTH);
1018 init_styleset_case(FORTRAN);
1019 init_styleset_case(GO);
1020 init_styleset_case(HASKELL);
1021 init_styleset_case(HAXE);
1022 init_styleset_case(AS);
1023 init_styleset_case(HTML);
1024 init_styleset_case(JAVA);
1025 init_styleset_case(JS);
1026 init_styleset_case(LATEX);
1027 init_styleset_case(LUA);
1028 init_styleset_case(MAKE);
1029 init_styleset_case(MATLAB);
1030 init_styleset_case(MARKDOWN);
1031 init_styleset_case(NSIS);
1032 init_styleset_case(OBJECTIVEC);
1033 init_styleset_case(PASCAL);
1034 init_styleset_case(PERL);
1035 init_styleset_case(PHP);
1036 init_styleset_case(PO);
1037 init_styleset_case(POWERSHELL);
1038 init_styleset_case(PYTHON);
1039 init_styleset_case(R);
1040 init_styleset_case(RUBY);
1041 init_styleset_case(RUST);
1042 init_styleset_case(SH);
1043 init_styleset_case(SQL);
1044 init_styleset_case(TCL);
1045 init_styleset_case(TXT2TAGS);
1046 init_styleset_case(VHDL);
1047 init_styleset_case(VERILOG);
1048 init_styleset_case(XML);
1049 init_styleset_case(YAML);
1050 init_styleset_case(ZEPHIR);
1051 default:
1052 if (ft->lexer_filetype)
1053 geany_debug("Filetype %s has a recursive lexer_filetype %s set!",
1054 ft->name, ft->lexer_filetype->name);
1057 /* should be done in filetypes.c really: */
1058 get_keyfile_wordchars(config, configh, &style_sets[filetype_idx].wordchars,
1059 common_style_set.wordchars);
1063 #define styleset_case(LANG_NAME) \
1064 case (GEANY_FILETYPES_##LANG_NAME): \
1065 styleset_from_mapping(sci, ft->id, highlighting_lexer_##LANG_NAME, \
1066 highlighting_styles_##LANG_NAME, \
1067 HL_N_ENTRIES(highlighting_styles_##LANG_NAME), \
1068 highlighting_keywords_##LANG_NAME, \
1069 HL_N_ENTRIES(highlighting_keywords_##LANG_NAME), \
1070 highlighting_properties_##LANG_NAME, \
1071 HL_N_ENTRIES(highlighting_properties_##LANG_NAME)); \
1072 break
1074 /** Sets up highlighting and other visual settings.
1075 * @param sci Scintilla widget.
1076 * @param ft Filetype settings to use. */
1077 GEANY_API_SYMBOL
1078 void highlighting_set_styles(ScintillaObject *sci, GeanyFiletype *ft)
1080 guint lexer_id = get_lexer_filetype(ft);
1082 filetypes_load_config(ft->id, FALSE); /* load filetypes.ext */
1084 switch (lexer_id)
1086 styleset_case(ABAQUS);
1087 styleset_case(ADA);
1088 styleset_case(ASM);
1089 styleset_case(BASIC);
1090 styleset_case(BATCH);
1091 styleset_case(C);
1092 styleset_case(CAML);
1093 styleset_case(CMAKE);
1094 styleset_case(COBOL);
1095 styleset_case(COFFEESCRIPT);
1096 styleset_case(CONF);
1097 styleset_case(CSS);
1098 styleset_case(D);
1099 styleset_case(DIFF);
1100 styleset_case(LISP);
1101 styleset_case(ERLANG);
1102 styleset_case(DOCBOOK);
1103 styleset_case(FERITE);
1104 styleset_case(F77);
1105 styleset_case(FORTH);
1106 styleset_case(FORTRAN);
1107 styleset_case(GO);
1108 styleset_case(HASKELL);
1109 styleset_case(HAXE);
1110 styleset_case(AS);
1111 styleset_case(HTML);
1112 styleset_case(JAVA);
1113 styleset_case(JS);
1114 styleset_case(LATEX);
1115 styleset_case(LUA);
1116 styleset_case(MAKE);
1117 styleset_case(MARKDOWN);
1118 styleset_case(MATLAB);
1119 styleset_case(NSIS);
1120 styleset_case(OBJECTIVEC);
1121 styleset_case(PASCAL);
1122 styleset_case(PERL);
1123 styleset_case(PHP);
1124 styleset_case(PO);
1125 styleset_case(POWERSHELL);
1126 styleset_case(PYTHON);
1127 styleset_case(R);
1128 styleset_case(RUBY);
1129 styleset_case(RUST);
1130 styleset_case(SH);
1131 styleset_case(SQL);
1132 styleset_case(TCL);
1133 styleset_case(TXT2TAGS);
1134 styleset_case(VHDL);
1135 styleset_case(VERILOG);
1136 styleset_case(XML);
1137 styleset_case(YAML);
1138 styleset_case(ZEPHIR);
1139 case GEANY_FILETYPES_NONE:
1140 default:
1141 styleset_default(sci, ft->id);
1143 /* [lexer_properties] settings */
1144 if (style_sets[ft->id].property_keys)
1146 gchar **prop = style_sets[ft->id].property_keys;
1147 gchar **val = style_sets[ft->id].property_values;
1149 while (*prop)
1151 sci_set_property(sci, *prop, *val);
1152 prop++;
1153 val++;
1159 /** Retrieves a style @a style_id for the filetype @a ft_id.
1160 * If the style was not already initialised
1161 * (e.g. by by opening a file of this type), it will be initialised. The returned pointer is
1162 * owned by Geany and must not be freed.
1163 * @param ft_id Filetype ID, e.g. @c GEANY_FILETYPES_DIFF.
1164 * @param style_id A Scintilla lexer style, e.g. @c SCE_DIFF_ADDED. See scintilla/include/SciLexer.h.
1165 * @return A pointer to the style struct.
1166 * @see Scintilla messages @c SCI_STYLEGETFORE, etc, for use with scintilla_send_message(). */
1167 GEANY_API_SYMBOL
1168 const GeanyLexerStyle *highlighting_get_style(gint ft_id, gint style_id)
1170 g_return_val_if_fail(ft_id >= 0 && (guint) ft_id < filetypes_array->len, NULL);
1171 g_return_val_if_fail(style_id >= 0, NULL);
1173 /* ensure filetype loaded */
1174 filetypes_load_config((guint) ft_id, FALSE);
1176 /* TODO: style_id might not be the real array index (Scintilla styles are not always synced
1177 * with array indices) */
1178 return get_style((guint) ft_id, (guint) style_id);
1182 enum
1184 SCHEME_MARKUP,
1185 SCHEME_FILE,
1186 SCHEME_COLUMNS
1189 static void on_color_scheme_changed(GtkTreeSelection *treesel, gpointer dummy)
1191 GtkTreeModel *model;
1192 GtkTreeIter iter;
1193 gchar *fname;
1194 gchar *path;
1196 if (!gtk_tree_selection_get_selected(treesel, &model, &iter))
1197 return;
1198 gtk_tree_model_get(model, &iter, SCHEME_FILE, &fname, -1);
1200 /* check if default item */
1201 if (!fname)
1203 SETPTR(editor_prefs.color_scheme, NULL);
1204 filetypes_reload();
1205 return;
1207 SETPTR(fname, utils_get_locale_from_utf8(fname));
1209 /* fname is just the basename from the menu item, so prepend the custom files path */
1210 path = g_build_path(G_DIR_SEPARATOR_S, app->configdir, GEANY_COLORSCHEMES_SUBDIR, fname, NULL);
1211 if (!g_file_test(path, G_FILE_TEST_EXISTS))
1213 /* try the system path */
1214 g_free(path);
1215 path = g_build_path(G_DIR_SEPARATOR_S, app->datadir, GEANY_COLORSCHEMES_SUBDIR, fname, NULL);
1217 if (g_file_test(path, G_FILE_TEST_EXISTS))
1219 SETPTR(editor_prefs.color_scheme, fname);
1220 fname = NULL;
1221 filetypes_reload();
1223 else
1225 SETPTR(fname, utils_get_utf8_from_locale(fname));
1226 ui_set_statusbar(TRUE, _("Could not find file '%s'."), fname);
1228 g_free(path);
1229 g_free(fname);
1233 static gchar *utils_get_setting_locale_string(GKeyFile *keyfile,
1234 const gchar *group, const gchar *key, const gchar *default_value)
1236 gchar *result = g_key_file_get_locale_string(keyfile, group, key, NULL, NULL);
1238 return FALLBACK(result, g_strdup(default_value));
1242 static void add_color_scheme_item(GtkListStore *store,
1243 gchar *name, gchar *desc, const gchar *fn, GtkTreeIter *current_iter)
1245 GtkTreeIter iter;
1246 gchar *markup;
1248 /* reuse parameters */
1249 name = g_markup_escape_text(name, -1);
1250 desc = g_markup_escape_text(desc, -1);
1251 markup = g_strdup_printf("<big><b>%s</b></big>\n%s", name, desc);
1252 g_free(name);
1253 g_free(desc);
1255 gtk_list_store_append(store, &iter);
1256 gtk_list_store_set(store, &iter, SCHEME_MARKUP, markup,
1257 SCHEME_FILE, fn, -1);
1258 g_free(markup);
1260 /* select the current iter if the the color scheme matches, or if it's the
1261 * default (fn == NULL), in case of bad config file. the default theme is
1262 * first anyway so if a later scheme matches it will override default */
1263 if ((! fn || utils_str_equal(fn, editor_prefs.color_scheme)) && current_iter)
1264 *current_iter = iter;
1268 static void add_color_scheme_file(GtkListStore *store, const gchar *fname, GtkTreeIter *current_iter)
1270 GKeyFile *hkeyfile, *skeyfile;
1271 gchar *path, *theme_name, *theme_desc;
1272 gchar *theme_fn = utils_get_utf8_from_locale(fname);
1274 path = g_build_filename(app->configdir, GEANY_COLORSCHEMES_SUBDIR, fname, NULL);
1275 hkeyfile = utils_key_file_new(path);
1276 SETPTR(path, g_build_filename(app->datadir, GEANY_COLORSCHEMES_SUBDIR, fname, NULL));
1277 skeyfile = utils_key_file_new(path);
1279 theme_name = utils_get_setting(locale_string, hkeyfile, skeyfile, "theme_info", "name", theme_fn);
1280 theme_desc = utils_get_setting(locale_string, hkeyfile, skeyfile, "theme_info", "description", NULL);
1281 add_color_scheme_item(store, theme_name, theme_desc, theme_fn, current_iter);
1283 g_free(path);
1284 g_free(theme_fn);
1285 g_free(theme_name);
1286 g_free(theme_desc);
1287 g_key_file_free(hkeyfile);
1288 g_key_file_free(skeyfile);
1292 static gboolean add_color_scheme_items(GtkListStore *store, GtkTreeIter *current_iter)
1294 GSList *list, *node;
1296 add_color_scheme_item(store, _("Default"), _("Default"), NULL, current_iter);
1297 list = utils_get_config_files(GEANY_COLORSCHEMES_SUBDIR);
1299 foreach_slist(node, list)
1301 gchar *fname = node->data;
1303 if (g_str_has_suffix(fname, ".conf"))
1304 add_color_scheme_file(store, fname, current_iter);
1306 g_free(fname);
1308 g_slist_free(list);
1309 return list != NULL;
1313 static void on_color_scheme_dialog_response(GtkWidget *dialog,
1314 gint response, gpointer *dialog_ptr)
1316 *dialog_ptr = NULL;
1317 gtk_widget_destroy(dialog);
1321 void highlighting_show_color_scheme_dialog(void)
1323 static GtkWidget *dialog = NULL;
1324 GtkListStore *store = gtk_list_store_new(SCHEME_COLUMNS,
1325 G_TYPE_STRING, G_TYPE_STRING);
1326 GtkCellRenderer *text_renderer;
1327 GtkTreeViewColumn *column;
1328 GtkTreeSelection *treesel;
1329 GtkTreeIter current_iter;
1330 GtkTreePath *path;
1331 GtkWidget *vbox, *swin, *tree;
1332 GeanyDocument *doc;
1334 doc = document_get_current();
1335 if (doc && doc->file_type->priv->warn_color_scheme)
1336 dialogs_show_msgbox_with_secondary(GTK_MESSAGE_WARNING,
1337 _("The current filetype overrides the default style."),
1338 _("This may cause color schemes to display incorrectly."));
1340 tree = gtk_tree_view_new_with_model(GTK_TREE_MODEL(store));
1341 g_object_unref(store);
1342 gtk_tree_view_set_rules_hint(GTK_TREE_VIEW(tree), TRUE);
1343 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(tree), FALSE);
1345 text_renderer = gtk_cell_renderer_text_new();
1346 g_object_set(text_renderer, "wrap-mode", PANGO_WRAP_WORD, NULL);
1347 column = gtk_tree_view_column_new_with_attributes(
1348 NULL, text_renderer, "markup", SCHEME_MARKUP, NULL);
1349 gtk_tree_view_append_column(GTK_TREE_VIEW(tree), column);
1351 add_color_scheme_items(store, &current_iter);
1353 treesel = gtk_tree_view_get_selection(GTK_TREE_VIEW(tree));
1354 gtk_tree_selection_select_iter(treesel, &current_iter);
1355 path = gtk_tree_model_get_path(GTK_TREE_MODEL(store), &current_iter);
1356 gtk_tree_view_scroll_to_cell(GTK_TREE_VIEW(tree), path, NULL, FALSE, 0, 0);
1357 gtk_tree_path_free(path);
1358 g_signal_connect(treesel, "changed", G_CALLBACK(on_color_scheme_changed), NULL);
1360 /* old dialog may still be showing */
1361 if (dialog)
1362 gtk_widget_destroy(dialog);
1363 dialog = gtk_dialog_new_with_buttons(_("Color Schemes"),
1364 GTK_WINDOW(main_widgets.window), GTK_DIALOG_DESTROY_WITH_PARENT,
1365 GTK_STOCK_CLOSE, GTK_RESPONSE_CLOSE, NULL);
1366 vbox = ui_dialog_vbox_new(GTK_DIALOG(dialog));
1367 gtk_box_set_spacing(GTK_BOX(vbox), 6);
1368 gtk_widget_set_name(dialog, "GeanyDialog");
1369 gtk_window_set_default_size(GTK_WINDOW(dialog),
1370 GEANY_DEFAULT_DIALOG_HEIGHT * 7/4, GEANY_DEFAULT_DIALOG_HEIGHT);
1372 swin = gtk_scrolled_window_new(NULL, NULL);
1373 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(swin), GTK_SHADOW_IN);
1374 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(swin),
1375 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
1376 gtk_container_add(GTK_CONTAINER(swin), tree);
1377 gtk_box_pack_start(GTK_BOX(vbox), swin, TRUE, TRUE, 0);
1378 g_signal_connect(dialog, "response", G_CALLBACK(on_color_scheme_dialog_response), &dialog);
1379 gtk_widget_show_all(dialog);
1383 /** Checks whether the given style is a string for the given lexer.
1385 * @param lexer Scintilla lexer type (@c SCLEX_*).
1386 * @param style Scintilla style (@c SCE_*).
1388 * @return @c TRUE if the style is a string, @c FALSE otherwise.
1390 GEANY_API_SYMBOL
1391 gboolean highlighting_is_string_style(gint lexer, gint style)
1393 /* Don't forget STRINGEOL, to prevent completion whilst typing a string with no closing char. */
1395 switch (lexer)
1397 case SCLEX_CPP:
1398 return (style == SCE_C_CHARACTER ||
1399 style == SCE_C_STRING ||
1400 style == SCE_C_STRINGEOL ||
1401 style == SCE_C_STRINGRAW ||
1402 style == SCE_C_VERBATIM ||
1403 style == SCE_C_TRIPLEVERBATIM ||
1404 style == SCE_C_HASHQUOTEDSTRING ||
1405 style == SCE_C_ESCAPESEQUENCE);
1407 case SCLEX_PASCAL:
1408 return (style == SCE_PAS_CHARACTER ||
1409 style == SCE_PAS_STRING ||
1410 style == SCE_PAS_STRINGEOL);
1412 case SCLEX_D:
1413 return (style == SCE_D_STRING ||
1414 style == SCE_D_STRINGEOL ||
1415 style == SCE_D_CHARACTER ||
1416 style == SCE_D_STRINGB ||
1417 style == SCE_D_STRINGR);
1419 case SCLEX_PYTHON:
1420 return (style == SCE_P_STRING ||
1421 style == SCE_P_TRIPLE ||
1422 style == SCE_P_TRIPLEDOUBLE ||
1423 style == SCE_P_CHARACTER ||
1424 style == SCE_P_STRINGEOL);
1426 case SCLEX_F77:
1427 case SCLEX_FORTRAN:
1428 return (style == SCE_F_STRING1 ||
1429 style == SCE_F_STRING2 ||
1430 style == SCE_F_STRINGEOL);
1432 case SCLEX_PERL:
1433 return (style == SCE_PL_STRING ||
1434 style == SCE_PL_CHARACTER ||
1435 style == SCE_PL_HERE_DELIM ||
1436 style == SCE_PL_HERE_Q ||
1437 style == SCE_PL_HERE_QQ ||
1438 style == SCE_PL_HERE_QX ||
1439 style == SCE_PL_POD ||
1440 style == SCE_PL_STRING_Q ||
1441 style == SCE_PL_STRING_QQ ||
1442 style == SCE_PL_STRING_QX ||
1443 style == SCE_PL_STRING_QR ||
1444 style == SCE_PL_STRING_QW ||
1445 style == SCE_PL_POD_VERB ||
1446 style == SCE_PL_XLAT
1447 /* we don't include any STRING_*_VAR for autocompletion */);
1449 case SCLEX_PO:
1450 return (style == SCE_PO_MSGCTXT_TEXT ||
1451 style == SCE_PO_MSGCTXT_TEXT_EOL ||
1452 style == SCE_PO_MSGID_TEXT ||
1453 style == SCE_PO_MSGID_TEXT_EOL ||
1454 style == SCE_PO_MSGSTR_TEXT ||
1455 style == SCE_PO_MSGSTR_TEXT_EOL);
1457 case SCLEX_R:
1458 return (style == SCE_R_STRING);
1460 case SCLEX_RUBY:
1461 return (style == SCE_RB_CHARACTER ||
1462 style == SCE_RB_STRING ||
1463 style == SCE_RB_HERE_DELIM ||
1464 style == SCE_RB_HERE_Q ||
1465 style == SCE_RB_HERE_QQ ||
1466 style == SCE_RB_HERE_QX ||
1467 style == SCE_RB_POD);
1469 case SCLEX_BASH:
1470 return (style == SCE_SH_STRING);
1472 case SCLEX_SQL:
1473 return (style == SCE_SQL_STRING);
1475 case SCLEX_TCL:
1476 return (style == SCE_TCL_IN_QUOTE);
1478 case SCLEX_LUA:
1479 return (style == SCE_LUA_LITERALSTRING ||
1480 style == SCE_LUA_CHARACTER ||
1481 style == SCE_LUA_STRINGEOL ||
1482 style == SCE_LUA_STRING);
1484 case SCLEX_HASKELL:
1485 case SCLEX_LITERATEHASKELL:
1486 return (style == SCE_HA_CHARACTER ||
1487 style == SCE_HA_STRINGEOL ||
1488 style == SCE_HA_STRING);
1490 case SCLEX_FREEBASIC:
1491 return (style == SCE_B_STRING ||
1492 style == SCE_B_STRINGEOL);
1494 case SCLEX_OCTAVE:
1495 return (style == SCE_MATLAB_STRING ||
1496 style == SCE_MATLAB_DOUBLEQUOTESTRING);
1498 case SCLEX_XML:
1499 case SCLEX_HTML:
1500 case SCLEX_PHPSCRIPT:
1501 return (
1502 style == SCE_HBA_STRING ||
1503 style == SCE_HBA_STRINGEOL ||
1504 style == SCE_HB_STRING ||
1505 style == SCE_HB_STRINGEOL ||
1506 style == SCE_H_CDATA ||
1507 style == SCE_H_DOUBLESTRING ||
1508 style == SCE_HJA_DOUBLESTRING ||
1509 style == SCE_HJA_SINGLESTRING ||
1510 style == SCE_HJA_STRINGEOL ||
1511 style == SCE_HJ_DOUBLESTRING ||
1512 style == SCE_HJ_SINGLESTRING ||
1513 style == SCE_HJ_STRINGEOL ||
1514 style == SCE_HPA_CHARACTER ||
1515 style == SCE_HPA_STRING ||
1516 style == SCE_HPA_TRIPLE ||
1517 style == SCE_HPA_TRIPLEDOUBLE ||
1518 style == SCE_HP_CHARACTER ||
1519 style == SCE_HPHP_HSTRING || /* HSTRING is a heredoc */
1520 style == SCE_HPHP_HSTRING_VARIABLE ||
1521 style == SCE_HPHP_SIMPLESTRING ||
1522 style == SCE_HP_STRING ||
1523 style == SCE_HP_TRIPLE ||
1524 style == SCE_HP_TRIPLEDOUBLE ||
1525 style == SCE_H_SGML_DOUBLESTRING ||
1526 style == SCE_H_SGML_SIMPLESTRING ||
1527 style == SCE_H_SINGLESTRING);
1529 case SCLEX_CMAKE:
1530 return (style == SCE_CMAKE_STRINGDQ ||
1531 style == SCE_CMAKE_STRINGLQ ||
1532 style == SCE_CMAKE_STRINGRQ ||
1533 style == SCE_CMAKE_STRINGVAR);
1535 case SCLEX_NSIS:
1536 return (style == SCE_NSIS_STRINGDQ ||
1537 style == SCE_NSIS_STRINGLQ ||
1538 style == SCE_NSIS_STRINGRQ ||
1539 style == SCE_NSIS_STRINGVAR);
1541 case SCLEX_ADA:
1542 return (style == SCE_ADA_CHARACTER ||
1543 style == SCE_ADA_STRING ||
1544 style == SCE_ADA_CHARACTEREOL ||
1545 style == SCE_ADA_STRINGEOL);
1547 case SCLEX_ABAQUS:
1548 return (style == SCE_ABAQUS_STRING);
1550 case SCLEX_RUST:
1551 return (style == SCE_RUST_CHARACTER ||
1552 style == SCE_RUST_BYTECHARACTER ||
1553 style == SCE_RUST_STRING ||
1554 style == SCE_RUST_STRINGR ||
1555 style == SCE_RUST_BYTESTRING ||
1556 style == SCE_RUST_BYTESTRINGR ||
1557 style == SCE_RUST_LEXERROR);
1559 case SCLEX_COFFEESCRIPT:
1560 return (style == SCE_COFFEESCRIPT_CHARACTER ||
1561 style == SCE_COFFEESCRIPT_STRING ||
1562 style == SCE_COFFEESCRIPT_STRINGEOL);
1564 case SCLEX_VERILOG:
1565 return (style == SCE_V_STRING);
1567 return FALSE;
1571 /** Checks whether the given style is a comment for the given lexer.
1573 * @param lexer Scintilla lexer type (@c SCLEX_*).
1574 * @param style Scintilla style (@c SCE_*).
1576 * @return @c TRUE if the style is a comment, @c FALSE otherwise.
1578 GEANY_API_SYMBOL
1579 gboolean highlighting_is_comment_style(gint lexer, gint style)
1581 switch (lexer)
1583 case SCLEX_COBOL:
1584 case SCLEX_CPP:
1585 return (style == SCE_C_COMMENT ||
1586 style == SCE_C_COMMENTLINE ||
1587 style == SCE_C_COMMENTDOC ||
1588 style == SCE_C_PREPROCESSORCOMMENT ||
1589 style == SCE_C_PREPROCESSORCOMMENTDOC ||
1590 style == SCE_C_COMMENTLINEDOC ||
1591 style == SCE_C_COMMENTDOCKEYWORD ||
1592 style == SCE_C_COMMENTDOCKEYWORDERROR ||
1593 style == SCE_C_TASKMARKER);
1595 case SCLEX_PASCAL:
1596 return (style == SCE_PAS_COMMENT ||
1597 style == SCE_PAS_COMMENT2 ||
1598 style == SCE_PAS_COMMENTLINE);
1600 case SCLEX_D:
1601 return (style == SCE_D_COMMENT ||
1602 style == SCE_D_COMMENTLINE ||
1603 style == SCE_D_COMMENTDOC ||
1604 style == SCE_D_COMMENTNESTED ||
1605 style == SCE_D_COMMENTLINEDOC ||
1606 style == SCE_D_COMMENTDOCKEYWORD ||
1607 style == SCE_D_COMMENTDOCKEYWORDERROR);
1609 case SCLEX_PYTHON:
1610 return (style == SCE_P_COMMENTLINE ||
1611 style == SCE_P_COMMENTBLOCK);
1613 case SCLEX_F77:
1614 case SCLEX_FORTRAN:
1615 return (style == SCE_F_COMMENT);
1617 case SCLEX_PERL:
1618 return (style == SCE_PL_COMMENTLINE);
1620 case SCLEX_PROPERTIES:
1621 return (style == SCE_PROPS_COMMENT);
1623 case SCLEX_PO:
1624 return (style == SCE_PO_COMMENT ||
1625 style == SCE_PO_PROGRAMMER_COMMENT);
1627 case SCLEX_LATEX:
1628 return (style == SCE_L_COMMENT ||
1629 style == SCE_L_COMMENT2);
1631 case SCLEX_MAKEFILE:
1632 return (style == SCE_MAKE_COMMENT);
1634 case SCLEX_RUBY:
1635 return (style == SCE_RB_COMMENTLINE);
1637 case SCLEX_BASH:
1638 return (style == SCE_SH_COMMENTLINE);
1640 case SCLEX_R:
1641 return (style == SCE_R_COMMENT);
1643 case SCLEX_SQL:
1644 return (style == SCE_SQL_COMMENT ||
1645 style == SCE_SQL_COMMENTLINE ||
1646 style == SCE_SQL_COMMENTDOC ||
1647 style == SCE_SQL_COMMENTLINEDOC ||
1648 style == SCE_SQL_COMMENTDOCKEYWORD ||
1649 style == SCE_SQL_COMMENTDOCKEYWORDERROR);
1651 case SCLEX_TCL:
1652 return (style == SCE_TCL_COMMENT ||
1653 style == SCE_TCL_COMMENTLINE ||
1654 style == SCE_TCL_COMMENT_BOX ||
1655 style == SCE_TCL_BLOCK_COMMENT);
1657 case SCLEX_OCTAVE:
1658 return (style == SCE_MATLAB_COMMENT);
1660 case SCLEX_LUA:
1661 return (style == SCE_LUA_COMMENT ||
1662 style == SCE_LUA_COMMENTLINE ||
1663 style == SCE_LUA_COMMENTDOC);
1665 case SCLEX_HASKELL:
1666 case SCLEX_LITERATEHASKELL:
1667 return (style == SCE_HA_COMMENTLINE ||
1668 style == SCE_HA_COMMENTBLOCK ||
1669 style == SCE_HA_COMMENTBLOCK2 ||
1670 style == SCE_HA_COMMENTBLOCK3 ||
1671 style == SCE_HA_LITERATE_COMMENT ||
1672 style == SCE_HA_LITERATE_CODEDELIM);
1674 case SCLEX_FREEBASIC:
1675 return (style == SCE_B_COMMENT ||
1676 style == SCE_B_COMMENTBLOCK ||
1677 style == SCE_B_DOCLINE ||
1678 style == SCE_B_DOCBLOCK ||
1679 style == SCE_B_DOCKEYWORD);
1681 case SCLEX_YAML:
1682 return (style == SCE_YAML_COMMENT);
1684 case SCLEX_XML:
1685 case SCLEX_HTML:
1686 case SCLEX_PHPSCRIPT:
1687 return (
1688 style == SCE_HBA_COMMENTLINE ||
1689 style == SCE_HB_COMMENTLINE ||
1690 style == SCE_H_COMMENT ||
1691 style == SCE_HJA_COMMENT ||
1692 style == SCE_HJA_COMMENTDOC ||
1693 style == SCE_HJA_COMMENTLINE ||
1694 style == SCE_HJ_COMMENT ||
1695 style == SCE_HJ_COMMENTDOC ||
1696 style == SCE_HJ_COMMENTLINE ||
1697 style == SCE_HPA_COMMENTLINE ||
1698 style == SCE_HP_COMMENTLINE ||
1699 style == SCE_HPHP_COMMENT ||
1700 style == SCE_HPHP_COMMENTLINE ||
1701 style == SCE_H_SGML_COMMENT);
1703 case SCLEX_CMAKE:
1704 return (style == SCE_CMAKE_COMMENT);
1706 case SCLEX_NSIS:
1707 return (style == SCE_NSIS_COMMENT ||
1708 style == SCE_NSIS_COMMENTBOX);
1710 case SCLEX_ADA:
1711 return (style == SCE_ADA_COMMENTLINE ||
1712 style == SCE_NSIS_COMMENTBOX);
1714 case SCLEX_ABAQUS:
1715 return (style == SCE_ABAQUS_COMMENT ||
1716 style == SCE_ABAQUS_COMMENTBLOCK);
1718 case SCLEX_ASM:
1719 return (style == SCE_ASM_COMMENT ||
1720 style == SCE_ASM_COMMENTBLOCK ||
1721 style == SCE_ASM_COMMENTDIRECTIVE);
1723 case SCLEX_RUST:
1724 return (style == SCE_RUST_COMMENTBLOCK ||
1725 style == SCE_RUST_COMMENTLINE ||
1726 style == SCE_RUST_COMMENTBLOCKDOC ||
1727 style == SCE_RUST_COMMENTLINEDOC);
1729 case SCLEX_COFFEESCRIPT:
1730 return (style == SCE_COFFEESCRIPT_COMMENTLINE ||
1731 style == SCE_COFFEESCRIPT_COMMENTBLOCK ||
1732 style == SCE_COFFEESCRIPT_VERBOSE_REGEX_COMMENT);
1734 case SCLEX_VERILOG:
1735 return (style == SCE_V_COMMENT ||
1736 style == SCE_V_COMMENTLINE ||
1737 style == SCE_V_COMMENTLINEBANG ||
1738 style == SCE_V_COMMENT_WORD);
1740 return FALSE;
1744 /** Checks whether the given style is normal code (not string, comment, preprocessor, etc).
1746 * @param lexer Scintilla lexer type (@c SCLEX_*).
1747 * @param style Scintilla style (@c SCE_*).
1749 * @return @c TRUE if the style is code, @c FALSE otherwise.
1751 GEANY_API_SYMBOL
1752 gboolean highlighting_is_code_style(gint lexer, gint style)
1754 switch (lexer)
1756 case SCLEX_CPP:
1758 if (style == SCE_C_PREPROCESSOR)
1759 return FALSE;
1760 break;
1762 case SCLEX_HASKELL:
1763 case SCLEX_LITERATEHASKELL:
1765 if (style == SCE_HA_PREPROCESSOR)
1766 return FALSE;
1767 break;
1769 case SCLEX_VERILOG:
1771 if (style == SCE_V_PREPROCESSOR)
1772 return FALSE;
1773 break;
1776 return !(highlighting_is_comment_style(lexer, style) ||
1777 highlighting_is_string_style(lexer, style));