2 * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 #include "telescope.h"
24 char *new_tab_url = NULL;
26 int olivetti_mode = 1;
27 int enable_colors = 1;
29 static struct lineface_descr {
31 int prfx_bg, bg, trail_bg;
32 int prfx_fg, fg, trail_fg;
33 int prfx_attr, attr, trail_attr;
34 } linefaces_descr[] = {
35 [LINE_TEXT] = {.pp=PT_PRFX, .p=PT, .tp=PT_TRAIL},
36 [LINE_LINK] = {.pp=PL_PRFX, .p=PL, .tp=PL_TRAIL},
37 [LINE_TITLE_1] = {.pp=PT1_PRFX, .p=PT1, .tp=PT1_TRAIL},
38 [LINE_TITLE_2] = {.pp=PT2_PRFX, .p=PT1, .tp=PT1_TRAIL},
39 [LINE_TITLE_3] = {.pp=PT3_PRFX, .p=PT3, .tp=PT3_TRAIL},
40 [LINE_ITEM] = {.pp=PI_PRFX, .p=PI, .tp=PI_TRAIL},
41 [LINE_QUOTE] = {.pp=PQ_PRFX, .p=PQ, .tp=PQ_TRAIL},
42 [LINE_PRE_START] = {.pp=PPSTART_PRFX, .p=PT, .tp=PT_TRAIL},
43 [LINE_PRE_CONTENT] = {.pp=PP_PRFX, .p=PP, .tp=PP_TRAIL},
44 [LINE_PRE_END] = {.pp=PPEND_PRFX, .p=PPEND, .tp=PPEND_TRAIL},
47 struct lineprefix line_prefixes[] = {
48 [LINE_TEXT] = { "", "" },
49 [LINE_LINK] = { "=> ", " " },
50 [LINE_TITLE_1] = { "# ", " " },
51 [LINE_TITLE_2] = { "## ", " " },
52 [LINE_TITLE_3] = { "### ", " " },
53 [LINE_ITEM] = { "* ", " " },
54 [LINE_QUOTE] = { "> ", " " },
55 [LINE_PRE_START] = { "```", " " },
56 [LINE_PRE_CONTENT] = { "", "" },
57 [LINE_PRE_END] = { "```", "```" },
60 struct line_face line_faces[] = {
61 [LINE_TEXT] = { 0, 0, 0 },
62 [LINE_LINK] = { 0, A_UNDERLINE, 0 },
63 [LINE_TITLE_1] = { A_BOLD, A_BOLD, 0 },
64 [LINE_TITLE_2] = { A_BOLD, A_BOLD, 0 },
65 [LINE_TITLE_3] = { A_BOLD, A_BOLD, 0 },
66 [LINE_ITEM] = { 0, 0, 0 },
67 [LINE_QUOTE] = { 0, A_DIM, 0 },
68 [LINE_PRE_START] = { 0, 0, 0 },
69 [LINE_PRE_CONTENT] = { 0, 0, 0 },
70 [LINE_PRE_END] = { 0, 0, 0 },
73 struct tab_face tab_face = {
74 .bg_attr = A_REVERSE, .bg_bg = -1, .bg_fg = -1,
75 .t_attr = A_REVERSE, .t_bg = -1, .t_fg = -1,
76 .c_attr = A_NORMAL, .c_bg = -1, .c_fg = -1,
79 * set these so that even when enable-color=0 the bar has some
82 .background = A_REVERSE,
87 struct body_face body_face = {
93 struct modeline_face modeline_face = {
94 .background = A_REVERSE,
97 struct minibuffer_face minibuffer_face = {
98 .background = A_NORMAL,
107 {"title1", LINE_TITLE_1},
108 {"title2", LINE_TITLE_2},
109 {"title3", LINE_TITLE_3},
111 {"quote", LINE_QUOTE},
112 {"pre.start", LINE_PRE_START},
113 {"pre", LINE_PRE_CONTENT},
114 {"pre.end", LINE_PRE_END},
117 static struct mapping *
118 mapping_by_name(const char *name)
122 for (i = 0; i < sizeof(mappings)/sizeof(mappings[0]); ++i) {
123 if (!strcmp(name, mappings[i].label))
133 struct lineface_descr *d;
136 len = sizeof(linefaces_descr)/sizeof(linefaces_descr[0]);
137 for (i = 0; i < len; ++i) {
138 d = &linefaces_descr[i];
140 d->prfx_bg = d->bg = d->trail_bg = -1;
141 d->prfx_fg = d->fg = d->trail_fg = -1;
146 config_setprfx(const char *name, const char *prfx, const char *cont)
148 struct lineprefix *p;
151 if (!has_prefix(name, "line."))
155 if ((m = mapping_by_name(name)) == NULL)
158 p = &line_prefixes[m->linetype];
166 config_setvari(const char *var, int val)
168 if (!strcmp(var, "fill-column")) {
169 if ((fill_column = val) <= 0)
170 fill_column = INT_MAX;
171 } else if (!strcmp(var, "olivetti-mode")) {
172 olivetti_mode = !!val;
173 } else if (!strcmp(var, "enable-colors")) {
174 enable_colors = !!val;
183 config_setvars(const char *var, char *val)
185 if (!strcmp(var, "new-tab-url")) {
186 if (new_tab_url != NULL)
195 config_setcolor(int bg, const char *name, int prfx, int line, int trail)
198 struct lineface_descr *d;
200 if (!strcmp(name, "tabline")) {
202 tab_face.bg_bg = prfx;
204 tab_face.bg_fg = prfx;
205 } else if (has_prefix(name, "tabline.")) {
208 if (!strcmp(name, "tab")) {
210 tab_face.t_bg = prfx;
212 tab_face.t_fg = prfx;
213 } else if (!strcmp(name, "current")) {
215 tab_face.c_bg = prfx;
217 tab_face.c_fg = prfx;
220 } else if (has_prefix(name, "line.")) {
223 if ((m = mapping_by_name(name)) == NULL)
226 d = &linefaces_descr[m->linetype];
237 } else if (!strcmp(name, "line")) {
239 body_face.lbg = prfx;
241 body_face.rbg = trail;
245 body_face.fg = trail;
255 config_setattr(const char *name, int prfx, int line, int trail)
258 struct lineface_descr *d;
260 if (!strcmp(name, "tabline")) {
261 tab_face.bg_attr = prfx;
262 } else if (has_prefix(name, "tabline.")) {
265 if (!strcmp(name, "tab"))
266 tab_face.t_attr = prfx;
267 else if (!strcmp(name, "current"))
268 tab_face.c_attr = prfx;
271 } else if (has_prefix(name, "line.")) {
274 if ((m = mapping_by_name(name)) == NULL)
277 d = &linefaces_descr[m->linetype];
281 d->trail_attr = trail;
290 tl_init_pair(int colors, int pair, int f, int b)
292 if (f >= colors || !enable_colors)
294 if (b >= colors || !enable_colors)
296 init_pair(pair, f, b);
300 config_apply_style(void)
302 size_t i, colors, len;
303 struct lineface_descr *d;
308 len = sizeof(linefaces_descr)/sizeof(linefaces_descr[0]);
309 for (i = 0; i < len; ++i) {
310 d = &linefaces_descr[i];
313 tl_init_pair(colors, d->pp, d->prfx_fg, d->prfx_bg);
314 f->prefix_prop = COLOR_PAIR(d->pp) | d->prfx_attr;
316 tl_init_pair(colors, d->p, d->fg, d->bg);
317 f->text_prop = COLOR_PAIR(d->p) | d->attr;
319 tl_init_pair(colors, d->tp, d->trail_fg, d->trail_bg);
320 f->trail_prop = COLOR_PAIR(d->tp) | d->trail_attr;
324 tl_init_pair(colors, PTL_BG, tab_face.bg_fg, tab_face.bg_bg);
325 tab_face.background = COLOR_PAIR(PTL_BG) | tab_face.bg_attr;
327 tl_init_pair(colors, PTL_TAB, tab_face.t_fg, tab_face.t_bg);
328 tab_face.tab = COLOR_PAIR(PTL_TAB) | tab_face.t_attr;
330 tl_init_pair(colors, PTL_CURR, tab_face.c_fg, tab_face.c_bg);
331 tab_face.current = COLOR_PAIR(PTL_CURR) | tab_face.c_attr;
334 tl_init_pair(colors, PBODY, body_face.fg, body_face.bg);
335 body_face.body = COLOR_PAIR(PBODY);
337 tl_init_pair(colors, PBLEFT, body_face.lfg, body_face.lbg);
338 body_face.left = COLOR_PAIR(PBLEFT);
340 tl_init_pair(colors, PBRIGHT, body_face.rfg, body_face.rbg);
341 body_face.right = COLOR_PAIR(PBRIGHT);