Blob


1 /*
2 * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
3 *
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.
7 *
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.
15 */
17 #include "telescope.h"
19 #include <curses.h>
20 #include <limits.h>
21 #include <stdlib.h>
22 #include <string.h>
24 char *new_tab_url = NULL;
25 int fill_column = 80;
26 int olivetti_mode = 1;
27 int enable_colors = 1;
29 static struct lineface_descr {
30 int pp, p, tp;
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},
45 };
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] = { "```", "```" },
58 };
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 },
71 };
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,
78 /*
79 * set these so that even when enable-color=0 the bar has some
80 * sane defaults.
81 */
82 .background = A_REVERSE,
83 .tab = A_REVERSE,
84 .current = A_NORMAL,
85 };
87 struct body_face body_face = {
88 .lbg = -1, .lfg = -1,
89 .bg = -1, .fg = -1,
90 .rbg = -1, .rfg = -1,
91 };
93 struct modeline_face modeline_face = {
94 .background = A_REVERSE,
95 };
97 struct minibuffer_face minibuffer_face = {
98 .background = A_NORMAL,
99 };
101 struct mapping {
102 const char *label;
103 int linetype;
104 } mappings[] = {
105 {"text", LINE_TEXT},
106 {"link", LINE_LINK},
107 {"title1", LINE_TITLE_1},
108 {"title2", LINE_TITLE_2},
109 {"title3", LINE_TITLE_3},
110 {"item", LINE_ITEM},
111 {"quote", LINE_QUOTE},
112 {"pre.start", LINE_PRE_START},
113 {"pre", LINE_PRE_CONTENT},
114 {"pre.end", LINE_PRE_END},
115 };
117 static struct mapping *
118 mapping_by_name(const char *name)
120 size_t i;
122 for (i = 0; i < sizeof(mappings)/sizeof(mappings[0]); ++i) {
123 if (!strcmp(name, mappings[i].label))
124 return &mappings[i];
127 return NULL;
130 void
131 config_init(void)
133 struct lineface_descr *d;
134 size_t i, len;
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;
145 int
146 config_setprfx(const char *name, const char *prfx, const char *cont)
148 struct lineprefix *p;
149 struct mapping *m;
151 if (!has_prefix(name, "line."))
152 return 0;
153 name += 5;
155 if ((m = mapping_by_name(name)) == NULL)
156 return 0;
158 p = &line_prefixes[m->linetype];
159 p->prfx1 = prfx;
160 p->prfx2 = cont;
162 return 1;
165 int
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;
175 } else {
176 return 0;
179 return 1;
182 int
183 config_setvars(const char *var, char *val)
185 if (!strcmp(var, "new-tab-url")) {
186 if (new_tab_url != NULL)
187 free(new_tab_url);
188 new_tab_url = val;
189 } else
190 return 0;
191 return 1;
194 int
195 config_setcolor(int bg, const char *name, int prfx, int line, int trail)
197 struct mapping *m;
198 struct lineface_descr *d;
200 if (!strcmp(name, "tabline")) {
201 if (bg)
202 tab_face.bg_bg = prfx;
203 else
204 tab_face.bg_fg = prfx;
205 } else if (has_prefix(name, "tabline.")) {
206 name += 8;
208 if (!strcmp(name, "tab")) {
209 if (bg)
210 tab_face.t_bg = prfx;
211 else
212 tab_face.t_fg = prfx;
213 } else if (!strcmp(name, "current")) {
214 if (bg)
215 tab_face.c_bg = prfx;
216 else
217 tab_face.c_fg = prfx;
218 } else
219 return 0;
220 } else if (has_prefix(name, "line.")) {
221 name += 5;
223 if ((m = mapping_by_name(name)) == NULL)
224 return 0;
226 d = &linefaces_descr[m->linetype];
228 if (bg) {
229 d->prfx_bg = prfx;
230 d->bg = line;
231 d->trail_bg = trail;
232 } else {
233 d->prfx_fg = prfx;
234 d->fg = line;
235 d->trail_fg = trail;
237 } else if (!strcmp(name, "line")) {
238 if (bg) {
239 body_face.lbg = prfx;
240 body_face.bg = line;
241 body_face.rbg = trail;
242 } else {
243 body_face.fg = prfx;
244 body_face.fg = line;
245 body_face.fg = trail;
247 } else {
248 return 0;
251 return 1;
254 int
255 config_setattr(const char *name, int prfx, int line, int trail)
257 struct mapping *m;
258 struct lineface_descr *d;
260 if (!strcmp(name, "tabline")) {
261 tab_face.bg_attr = prfx;
262 } else if (has_prefix(name, "tabline.")) {
263 name += 8;
265 if (!strcmp(name, "tab"))
266 tab_face.t_attr = prfx;
267 else if (!strcmp(name, "current"))
268 tab_face.c_attr = prfx;
269 else
270 return 0;
271 } else if (has_prefix(name, "line.")) {
272 name += 5;
274 if ((m = mapping_by_name(name)) == NULL)
275 return 0;
277 d = &linefaces_descr[m->linetype];
279 d->prfx_attr = prfx;
280 d->attr = line;
281 d->trail_attr = trail;
282 } else {
283 return 0;
286 return 1;
289 static inline void
290 tl_init_pair(int colors, int pair, int f, int b)
292 if (f >= colors || !enable_colors)
293 f = -1;
294 if (b >= colors || !enable_colors)
295 b = -1;
296 init_pair(pair, f, b);
299 void
300 config_apply_style(void)
302 size_t i, colors, len;
303 struct lineface_descr *d;
304 struct line_face *f;
306 colors = COLORS;
308 len = sizeof(linefaces_descr)/sizeof(linefaces_descr[0]);
309 for (i = 0; i < len; ++i) {
310 d = &linefaces_descr[i];
311 f = &line_faces[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;
323 /* tab line */
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;
333 /* body */
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);