| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include <stdlib.h> | ||
| 2 | #include <string.h> | ||
| 3 | #include "color.h" | ||
| 4 | #include "command/serialize.h" | ||
| 5 | #include "util/array.h" | ||
| 6 | #include "util/bsearch.h" | ||
| 7 | #include "util/debug.h" | ||
| 8 | #include "util/xmalloc.h" | ||
| 9 | |||
| 10 | static const char builtin_style_names[NR_BSE][16] = { | ||
| 11 | [BSE_ACTIVETAB] = "activetab", | ||
| 12 | [BSE_COMMANDLINE] = "commandline", | ||
| 13 | [BSE_CURRENTLINE] = "currentline", | ||
| 14 | [BSE_DEFAULT] = "default", | ||
| 15 | [BSE_DIALOG] = "dialog", | ||
| 16 | [BSE_ERRORMSG] = "errormsg", | ||
| 17 | [BSE_INACTIVETAB] = "inactivetab", | ||
| 18 | [BSE_INFOMSG] = "infomsg", | ||
| 19 | [BSE_LINENUMBER] = "linenumber", | ||
| 20 | [BSE_NOLINE] = "noline", | ||
| 21 | [BSE_NONTEXT] = "nontext", | ||
| 22 | [BSE_SELECTION] = "selection", | ||
| 23 | [BSE_STATUSLINE] = "statusline", | ||
| 24 | [BSE_TABBAR] = "tabbar", | ||
| 25 | [BSE_WSERROR] = "wserror", | ||
| 26 | }; | ||
| 27 | |||
| 28 | 24 | UNITTEST { | |
| 29 | 24 | CHECK_BSEARCH_STR_ARRAY(builtin_style_names); | |
| 30 | 24 | } | |
| 31 | |||
| 32 | 9673 | static TermStyle *find_real_style(StyleMap *styles, const char *name) | |
| 33 | { | ||
| 34 | 9673 | ssize_t idx = BSEARCH_IDX(name, builtin_style_names, vstrcmp); | |
| 35 |
2/2✓ Branch 0 (3→4) taken 302 times.
✓ Branch 1 (3→7) taken 9371 times.
|
9673 | if (idx >= 0) { |
| 36 | 302 | BUG_ON(idx >= ARRAYLEN(builtin_style_names)); | |
| 37 | 302 | return &styles->builtin[idx]; | |
| 38 | } | ||
| 39 | 9371 | return hashmap_get(&styles->other, name); | |
| 40 | } | ||
| 41 | |||
| 42 | 9048 | static const TermStyle *find_real_style_const(const StyleMap *styles, const char *name) | |
| 43 | { | ||
| 44 | 9048 | return find_real_style((StyleMap*)styles, name); | |
| 45 | } | ||
| 46 | |||
| 47 | 625 | bool set_highlight_style(StyleMap *styles, const char *name, const TermStyle *style) | |
| 48 | { | ||
| 49 | 625 | TermStyle *existing = find_real_style(styles, name); | |
| 50 |
2/2✓ Branch 0 (3→4) taken 301 times.
✓ Branch 1 (3→5) taken 324 times.
|
625 | if (existing) { |
| 51 | 301 | bool different = !same_style(style, existing); | |
| 52 | 301 | *existing = *style; | |
| 53 | 301 | return different; | |
| 54 | } | ||
| 55 | |||
| 56 | 324 | hashmap_insert(&styles->other, xstrdup(name), XMEMDUP(style)); | |
| 57 | 324 | return true; | |
| 58 | } | ||
| 59 | |||
| 60 | 4570 | const TermStyle *find_style(const StyleMap *styles, const char *name) | |
| 61 | { | ||
| 62 | 4570 | const TermStyle *style = find_real_style_const(styles, name); | |
| 63 |
2/2✓ Branch 0 (3→4) taken 4478 times.
✓ Branch 1 (3→6) taken 92 times.
|
4570 | if (style) { |
| 64 | return style; | ||
| 65 | } | ||
| 66 | |||
| 67 | 4478 | const char *dot = strchr(name, '.'); | |
| 68 |
1/2✓ Branch 0 (4→5) taken 4478 times.
✗ Branch 1 (4→6) not taken.
|
4478 | return dot ? find_real_style_const(styles, dot + 1) : NULL; |
| 69 | } | ||
| 70 | |||
| 71 | 18 | void clear_hl_styles(StyleMap *styles) | |
| 72 | { | ||
| 73 | 18 | hashmap_clear(&styles->other, free); | |
| 74 | 18 | } | |
| 75 | |||
| 76 | 1 | void collect_builtin_styles(PointerArray *a, const char *prefix) | |
| 77 | { | ||
| 78 | 1 | COLLECT_STRINGS(builtin_style_names, a, prefix); | |
| 79 | 1 | } | |
| 80 | |||
| 81 | typedef struct { | ||
| 82 | const char *name; | ||
| 83 | TermStyle style; | ||
| 84 | } HlStyle; | ||
| 85 | |||
| 86 | 292 | static int hlstyle_cmp(const void *ap, const void *bp) | |
| 87 | { | ||
| 88 | 292 | const HlStyle *a = ap; | |
| 89 | 292 | const HlStyle *b = bp; | |
| 90 | 292 | return strcmp(a->name, b->name); | |
| 91 | } | ||
| 92 | |||
| 93 | 102 | void string_append_hl_style(String *s, const char *name, const TermStyle *style) | |
| 94 | { | ||
| 95 | 102 | string_append_literal(s, "hi "); | |
| 96 | 102 | string_append_escaped_arg(s, name, true); | |
| 97 | 102 | string_append_byte(s, ' '); | |
| 98 |
1/2✗ Branch 0 (5→6) not taken.
✓ Branch 1 (5→7) taken 102 times.
|
102 | if (unlikely(name[0] == '-')) { |
| 99 | ✗ | string_append_literal(s, "-- "); | |
| 100 | } | ||
| 101 | 102 | char buf[TERM_STYLE_BUFSIZE]; | |
| 102 | 102 | string_append_cstring(s, term_style_to_string(buf, style)); | |
| 103 | 102 | } | |
| 104 | |||
| 105 | 2 | String dump_hl_styles(const StyleMap *styles) | |
| 106 | { | ||
| 107 | 2 | String buf = string_new(4096); | |
| 108 | 2 | string_append_literal(&buf, "# UI colors:\n"); | |
| 109 |
2/2✓ Branch 0 (8→5) taken 30 times.
✓ Branch 1 (8→9) taken 2 times.
|
32 | for (size_t i = 0; i < NR_BSE; i++) { |
| 110 | 30 | string_append_hl_style(&buf, builtin_style_names[i], &styles->builtin[i]); | |
| 111 | 30 | string_append_byte(&buf, '\n'); | |
| 112 | } | ||
| 113 | |||
| 114 | 2 | const HashMap *hl_styles = &styles->other; | |
| 115 | 2 | const size_t count = hl_styles->count; | |
| 116 |
1/2✗ Branch 0 (9→10) not taken.
✓ Branch 1 (9→11) taken 2 times.
|
2 | if (unlikely(count == 0)) { |
| 117 | ✗ | return buf; | |
| 118 | } | ||
| 119 | |||
| 120 | // Copy the HashMap entries into an array | ||
| 121 | 2 | HlStyle *array = xmallocarray(count, sizeof(*array)); | |
| 122 | 2 | size_t n = 0; | |
| 123 |
2/2✓ Branch 0 (15→13) taken 72 times.
✓ Branch 1 (15→16) taken 2 times.
|
74 | for (HashMapIter it = hashmap_iter(hl_styles); hashmap_next(&it); ) { |
| 124 | 72 | const TermStyle *style = it.entry->value; | |
| 125 | 72 | array[n++] = (HlStyle) { | |
| 126 | 72 | .name = it.entry->key, | |
| 127 | 72 | .style = *style, | |
| 128 | }; | ||
| 129 | } | ||
| 130 | |||
| 131 | // Sort the array | ||
| 132 | 2 | BUG_ON(n != count); | |
| 133 | 2 | qsort(array, count, sizeof(array[0]), hlstyle_cmp); | |
| 134 | |||
| 135 | 2 | string_append_literal(&buf, "\n# Syntax colors:\n"); | |
| 136 |
2/2✓ Branch 0 (24→21) taken 72 times.
✓ Branch 1 (24→25) taken 2 times.
|
74 | for (size_t i = 0; i < count; i++) { |
| 137 | 72 | string_append_hl_style(&buf, array[i].name, &array[i].style); | |
| 138 | 72 | string_append_byte(&buf, '\n'); | |
| 139 | } | ||
| 140 | |||
| 141 | 2 | free(array); | |
| 142 | 2 | return buf; | |
| 143 | } | ||
| 144 |