Blame


1 c92e529c 2021-06-15 op /*
2 c92e529c 2021-06-15 op * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
3 c92e529c 2021-06-15 op *
4 c92e529c 2021-06-15 op * Permission to use, copy, modify, and distribute this software for any
5 c92e529c 2021-06-15 op * purpose with or without fee is hereby granted, provided that the above
6 c92e529c 2021-06-15 op * copyright notice and this permission notice appear in all copies.
7 c92e529c 2021-06-15 op *
8 c92e529c 2021-06-15 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 c92e529c 2021-06-15 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 c92e529c 2021-06-15 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 c92e529c 2021-06-15 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 c92e529c 2021-06-15 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 c92e529c 2021-06-15 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 c92e529c 2021-06-15 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 c92e529c 2021-06-15 op */
16 c92e529c 2021-06-15 op
17 c92e529c 2021-06-15 op #include "telescope.h"
18 c92e529c 2021-06-15 op
19 c92e529c 2021-06-15 op #include <curses.h>
20 1a99965e 2021-06-19 op #include <limits.h>
21 db7cc27a 2021-06-19 op #include <stdlib.h>
22 69e1d1b6 2021-06-15 op #include <string.h>
23 c92e529c 2021-06-15 op
24 db7cc27a 2021-06-19 op char *new_tab_url = NULL;
25 2c362f95 2021-06-24 op int fill_column = 80;
26 2c362f95 2021-06-24 op int olivetti_mode = 1;
27 2c748a1f 2021-06-21 op int enable_colors = 1;
28 1a99965e 2021-06-19 op
29 74a2587f 2021-06-21 op static struct lineface_descr {
30 d89b86d6 2021-06-21 op int pp, p, tp;
31 d89b86d6 2021-06-21 op int prfx_bg, bg, trail_bg;
32 d89b86d6 2021-06-21 op int prfx_fg, fg, trail_fg;
33 d0149485 2021-06-22 op int prfx_attr, attr, trail_attr;
34 74a2587f 2021-06-21 op } linefaces_descr[] = {
35 d89b86d6 2021-06-21 op [LINE_TEXT] = {.pp=PT_PRFX, .p=PT, .tp=PT_TRAIL},
36 d89b86d6 2021-06-21 op [LINE_LINK] = {.pp=PL_PRFX, .p=PL, .tp=PL_TRAIL},
37 d89b86d6 2021-06-21 op [LINE_TITLE_1] = {.pp=PT1_PRFX, .p=PT1, .tp=PT1_TRAIL},
38 d89b86d6 2021-06-21 op [LINE_TITLE_2] = {.pp=PT2_PRFX, .p=PT1, .tp=PT1_TRAIL},
39 d89b86d6 2021-06-21 op [LINE_TITLE_3] = {.pp=PT3_PRFX, .p=PT3, .tp=PT3_TRAIL},
40 d89b86d6 2021-06-21 op [LINE_ITEM] = {.pp=PI_PRFX, .p=PI, .tp=PI_TRAIL},
41 d89b86d6 2021-06-21 op [LINE_QUOTE] = {.pp=PQ_PRFX, .p=PQ, .tp=PQ_TRAIL},
42 d89b86d6 2021-06-21 op [LINE_PRE_START] = {.pp=PPSTART_PRFX, .p=PT, .tp=PT_TRAIL},
43 d89b86d6 2021-06-21 op [LINE_PRE_CONTENT] = {.pp=PP_PRFX, .p=PP, .tp=PP_TRAIL},
44 d89b86d6 2021-06-21 op [LINE_PRE_END] = {.pp=PPEND_PRFX, .p=PPEND, .tp=PPEND_TRAIL},
45 74a2587f 2021-06-21 op };
46 74a2587f 2021-06-21 op
47 c92e529c 2021-06-15 op struct lineprefix line_prefixes[] = {
48 c92e529c 2021-06-15 op [LINE_TEXT] = { "", "" },
49 c92e529c 2021-06-15 op [LINE_LINK] = { "=> ", " " },
50 c92e529c 2021-06-15 op [LINE_TITLE_1] = { "# ", " " },
51 c92e529c 2021-06-15 op [LINE_TITLE_2] = { "## ", " " },
52 c92e529c 2021-06-15 op [LINE_TITLE_3] = { "### ", " " },
53 c92e529c 2021-06-15 op [LINE_ITEM] = { "* ", " " },
54 c92e529c 2021-06-15 op [LINE_QUOTE] = { "> ", " " },
55 c92e529c 2021-06-15 op [LINE_PRE_START] = { "```", " " },
56 c92e529c 2021-06-15 op [LINE_PRE_CONTENT] = { "", "" },
57 c92e529c 2021-06-15 op [LINE_PRE_END] = { "```", "```" },
58 c92e529c 2021-06-15 op };
59 c92e529c 2021-06-15 op
60 c92e529c 2021-06-15 op struct line_face line_faces[] = {
61 d89b86d6 2021-06-21 op [LINE_TEXT] = { 0, 0, 0 },
62 d89b86d6 2021-06-21 op [LINE_LINK] = { 0, A_UNDERLINE, 0 },
63 d89b86d6 2021-06-21 op [LINE_TITLE_1] = { A_BOLD, A_BOLD, 0 },
64 d89b86d6 2021-06-21 op [LINE_TITLE_2] = { A_BOLD, A_BOLD, 0 },
65 d89b86d6 2021-06-21 op [LINE_TITLE_3] = { A_BOLD, A_BOLD, 0 },
66 d89b86d6 2021-06-21 op [LINE_ITEM] = { 0, 0, 0 },
67 d89b86d6 2021-06-21 op [LINE_QUOTE] = { 0, A_DIM, 0 },
68 d89b86d6 2021-06-21 op [LINE_PRE_START] = { 0, 0, 0 },
69 d89b86d6 2021-06-21 op [LINE_PRE_CONTENT] = { 0, 0, 0 },
70 d89b86d6 2021-06-21 op [LINE_PRE_END] = { 0, 0, 0 },
71 c92e529c 2021-06-15 op };
72 c92e529c 2021-06-15 op
73 c92e529c 2021-06-15 op struct tab_face tab_face = {
74 33d904b6 2021-06-22 op .bg_attr = A_REVERSE, .bg_bg = -1, .bg_fg = -1,
75 33d904b6 2021-06-22 op .t_attr = A_REVERSE, .t_bg = -1, .t_fg = -1,
76 33d904b6 2021-06-22 op .c_attr = A_NORMAL, .c_bg = -1, .c_fg = -1,
77 33d904b6 2021-06-22 op
78 33d904b6 2021-06-22 op /*
79 33d904b6 2021-06-22 op * set these so that even when enable-color=0 the bar has some
80 33d904b6 2021-06-22 op * sane defaults.
81 33d904b6 2021-06-22 op */
82 33d904b6 2021-06-22 op .background = A_REVERSE,
83 33d904b6 2021-06-22 op .tab = A_REVERSE,
84 33d904b6 2021-06-22 op .current = A_NORMAL,
85 c92e529c 2021-06-15 op };
86 d5493194 2021-06-15 op
87 b598590d 2021-06-21 op struct body_face body_face = {
88 160abe04 2021-06-21 op .lbg = -1, .lfg = -1,
89 160abe04 2021-06-21 op .bg = -1, .fg = -1,
90 160abe04 2021-06-21 op .rbg = -1, .rfg = -1,
91 b598590d 2021-06-21 op };
92 b598590d 2021-06-21 op
93 d5493194 2021-06-15 op struct modeline_face modeline_face = {
94 d5493194 2021-06-15 op .background = A_REVERSE,
95 d5493194 2021-06-15 op };
96 d5493194 2021-06-15 op
97 d5493194 2021-06-15 op struct minibuffer_face minibuffer_face = {
98 d5493194 2021-06-15 op .background = A_NORMAL,
99 d5493194 2021-06-15 op };
100 69e1d1b6 2021-06-15 op
101 74a2587f 2021-06-21 op struct mapping {
102 74a2587f 2021-06-21 op const char *label;
103 74a2587f 2021-06-21 op int linetype;
104 74a2587f 2021-06-21 op } mappings[] = {
105 d89b86d6 2021-06-21 op {"text", LINE_TEXT},
106 d89b86d6 2021-06-21 op {"link", LINE_LINK},
107 d89b86d6 2021-06-21 op {"title1", LINE_TITLE_1},
108 d89b86d6 2021-06-21 op {"title2", LINE_TITLE_2},
109 d89b86d6 2021-06-21 op {"title3", LINE_TITLE_3},
110 d89b86d6 2021-06-21 op {"item", LINE_ITEM},
111 d89b86d6 2021-06-21 op {"quote", LINE_QUOTE},
112 d89b86d6 2021-06-21 op {"pre.start", LINE_PRE_START},
113 d89b86d6 2021-06-21 op {"pre", LINE_PRE_CONTENT},
114 d89b86d6 2021-06-21 op {"pre.end", LINE_PRE_END},
115 74a2587f 2021-06-21 op };
116 74a2587f 2021-06-21 op
117 74a2587f 2021-06-21 op static struct mapping *
118 74a2587f 2021-06-21 op mapping_by_name(const char *name)
119 74a2587f 2021-06-21 op {
120 74a2587f 2021-06-21 op size_t i;
121 74a2587f 2021-06-21 op
122 74a2587f 2021-06-21 op for (i = 0; i < sizeof(mappings)/sizeof(mappings[0]); ++i) {
123 74a2587f 2021-06-21 op if (!strcmp(name, mappings[i].label))
124 74a2587f 2021-06-21 op return &mappings[i];
125 74a2587f 2021-06-21 op }
126 74a2587f 2021-06-21 op
127 74a2587f 2021-06-21 op return NULL;
128 74a2587f 2021-06-21 op }
129 74a2587f 2021-06-21 op
130 33fc3a8f 2021-06-21 op void
131 33fc3a8f 2021-06-21 op config_init(void)
132 33fc3a8f 2021-06-21 op {
133 33fc3a8f 2021-06-21 op struct lineface_descr *d;
134 33fc3a8f 2021-06-21 op size_t i, len;
135 33fc3a8f 2021-06-21 op
136 33fc3a8f 2021-06-21 op len = sizeof(linefaces_descr)/sizeof(linefaces_descr[0]);
137 33fc3a8f 2021-06-21 op for (i = 0; i < len; ++i) {
138 33fc3a8f 2021-06-21 op d = &linefaces_descr[i];
139 33fc3a8f 2021-06-21 op
140 33fc3a8f 2021-06-21 op d->prfx_bg = d->bg = d->trail_bg = -1;
141 33fc3a8f 2021-06-21 op d->prfx_fg = d->fg = d->trail_fg = -1;
142 33fc3a8f 2021-06-21 op }
143 33fc3a8f 2021-06-21 op }
144 33fc3a8f 2021-06-21 op
145 69e1d1b6 2021-06-15 op int
146 d89b86d6 2021-06-21 op config_setprfx(const char *name, const char *prfx, const char *cont)
147 69e1d1b6 2021-06-15 op {
148 69e1d1b6 2021-06-15 op struct lineprefix *p;
149 74a2587f 2021-06-21 op struct mapping *m;
150 69e1d1b6 2021-06-15 op
151 69e1d1b6 2021-06-15 op if (!has_prefix(name, "line."))
152 69e1d1b6 2021-06-15 op return 0;
153 69e1d1b6 2021-06-15 op name += 5;
154 69e1d1b6 2021-06-15 op
155 74a2587f 2021-06-21 op if ((m = mapping_by_name(name)) == NULL)
156 74a2587f 2021-06-21 op return 0;
157 69e1d1b6 2021-06-15 op
158 74a2587f 2021-06-21 op p = &line_prefixes[m->linetype];
159 d89b86d6 2021-06-21 op p->prfx1 = prfx;
160 d89b86d6 2021-06-21 op p->prfx2 = cont;
161 69e1d1b6 2021-06-15 op
162 74a2587f 2021-06-21 op return 1;
163 69e1d1b6 2021-06-15 op }
164 86e7d7b4 2021-06-19 op
165 86e7d7b4 2021-06-19 op int
166 86e7d7b4 2021-06-19 op config_setvari(const char *var, int val)
167 86e7d7b4 2021-06-19 op {
168 1a99965e 2021-06-19 op if (!strcmp(var, "fill-column")) {
169 cc073cd2 2021-06-19 op if ((fill_column = val) <= 0)
170 2106687b 2021-06-19 op fill_column = INT_MAX;
171 72b18268 2021-06-19 op } else if (!strcmp(var, "olivetti-mode")) {
172 72b18268 2021-06-19 op olivetti_mode = !!val;
173 2c748a1f 2021-06-21 op } else if (!strcmp(var, "enable-colors")) {
174 2c748a1f 2021-06-21 op enable_colors = !!val;
175 2c748a1f 2021-06-21 op } else {
176 1a99965e 2021-06-19 op return 0;
177 2c748a1f 2021-06-21 op }
178 2c748a1f 2021-06-21 op
179 1a99965e 2021-06-19 op return 1;
180 86e7d7b4 2021-06-19 op }
181 86e7d7b4 2021-06-19 op
182 86e7d7b4 2021-06-19 op int
183 86e7d7b4 2021-06-19 op config_setvars(const char *var, char *val)
184 86e7d7b4 2021-06-19 op {
185 db7cc27a 2021-06-19 op if (!strcmp(var, "new-tab-url")) {
186 db7cc27a 2021-06-19 op if (new_tab_url != NULL)
187 db7cc27a 2021-06-19 op free(new_tab_url);
188 db7cc27a 2021-06-19 op new_tab_url = val;
189 db7cc27a 2021-06-19 op } else
190 db7cc27a 2021-06-19 op return 0;
191 db7cc27a 2021-06-19 op return 1;
192 86e7d7b4 2021-06-19 op }
193 74a2587f 2021-06-21 op
194 74a2587f 2021-06-21 op int
195 d89b86d6 2021-06-21 op config_setcolor(int bg, const char *name, int prfx, int line, int trail)
196 74a2587f 2021-06-21 op {
197 74a2587f 2021-06-21 op struct mapping *m;
198 74a2587f 2021-06-21 op struct lineface_descr *d;
199 74a2587f 2021-06-21 op
200 33d904b6 2021-06-22 op if (!strcmp(name, "tabline")) {
201 33d904b6 2021-06-22 op if (bg)
202 33d904b6 2021-06-22 op tab_face.bg_bg = prfx;
203 33d904b6 2021-06-22 op else
204 33d904b6 2021-06-22 op tab_face.bg_fg = prfx;
205 33d904b6 2021-06-22 op } else if (has_prefix(name, "tabline.")) {
206 33d904b6 2021-06-22 op name += 8;
207 33d904b6 2021-06-22 op
208 33d904b6 2021-06-22 op if (!strcmp(name, "tab")) {
209 33d904b6 2021-06-22 op if (bg)
210 33d904b6 2021-06-22 op tab_face.t_bg = prfx;
211 33d904b6 2021-06-22 op else
212 33d904b6 2021-06-22 op tab_face.t_fg = prfx;
213 33d904b6 2021-06-22 op } else if (!strcmp(name, "current")) {
214 33d904b6 2021-06-22 op if (bg)
215 33d904b6 2021-06-22 op tab_face.c_bg = prfx;
216 33d904b6 2021-06-22 op else
217 33d904b6 2021-06-22 op tab_face.c_fg = prfx;
218 33d904b6 2021-06-22 op } else
219 33d904b6 2021-06-22 op return 0;
220 33d904b6 2021-06-22 op } else if (has_prefix(name, "line.")) {
221 50f7147c 2021-06-21 op name += 5;
222 74a2587f 2021-06-21 op
223 50f7147c 2021-06-21 op if ((m = mapping_by_name(name)) == NULL)
224 50f7147c 2021-06-21 op return 0;
225 74a2587f 2021-06-21 op
226 50f7147c 2021-06-21 op d = &linefaces_descr[m->linetype];
227 74a2587f 2021-06-21 op
228 50f7147c 2021-06-21 op if (bg) {
229 50f7147c 2021-06-21 op d->prfx_bg = prfx;
230 50f7147c 2021-06-21 op d->bg = line;
231 50f7147c 2021-06-21 op d->trail_bg = trail;
232 50f7147c 2021-06-21 op } else {
233 50f7147c 2021-06-21 op d->prfx_fg = prfx;
234 50f7147c 2021-06-21 op d->fg = line;
235 50f7147c 2021-06-21 op d->trail_fg = trail;
236 50f7147c 2021-06-21 op }
237 b598590d 2021-06-21 op } else if (!strcmp(name, "line")) {
238 160abe04 2021-06-21 op if (bg) {
239 160abe04 2021-06-21 op body_face.lbg = prfx;
240 160abe04 2021-06-21 op body_face.bg = line;
241 160abe04 2021-06-21 op body_face.rbg = trail;
242 160abe04 2021-06-21 op } else {
243 b598590d 2021-06-21 op body_face.fg = prfx;
244 160abe04 2021-06-21 op body_face.fg = line;
245 160abe04 2021-06-21 op body_face.fg = trail;
246 160abe04 2021-06-21 op }
247 74a2587f 2021-06-21 op } else {
248 50f7147c 2021-06-21 op return 0;
249 74a2587f 2021-06-21 op }
250 74a2587f 2021-06-21 op
251 74a2587f 2021-06-21 op return 1;
252 74a2587f 2021-06-21 op }
253 74a2587f 2021-06-21 op
254 d0149485 2021-06-22 op int
255 d0149485 2021-06-22 op config_setattr(const char *name, int prfx, int line, int trail)
256 d0149485 2021-06-22 op {
257 d0149485 2021-06-22 op struct mapping *m;
258 d0149485 2021-06-22 op struct lineface_descr *d;
259 d0149485 2021-06-22 op
260 33d904b6 2021-06-22 op if (!strcmp(name, "tabline")) {
261 33d904b6 2021-06-22 op tab_face.bg_attr = prfx;
262 33d904b6 2021-06-22 op } else if (has_prefix(name, "tabline.")) {
263 33d904b6 2021-06-22 op name += 8;
264 33d904b6 2021-06-22 op
265 33d904b6 2021-06-22 op if (!strcmp(name, "tab"))
266 33d904b6 2021-06-22 op tab_face.t_attr = prfx;
267 33d904b6 2021-06-22 op else if (!strcmp(name, "current"))
268 33d904b6 2021-06-22 op tab_face.c_attr = prfx;
269 33d904b6 2021-06-22 op else
270 33d904b6 2021-06-22 op return 0;
271 33d904b6 2021-06-22 op } else if (has_prefix(name, "line.")) {
272 d0149485 2021-06-22 op name += 5;
273 d0149485 2021-06-22 op
274 d0149485 2021-06-22 op if ((m = mapping_by_name(name)) == NULL)
275 d0149485 2021-06-22 op return 0;
276 d0149485 2021-06-22 op
277 d0149485 2021-06-22 op d = &linefaces_descr[m->linetype];
278 d0149485 2021-06-22 op
279 d0149485 2021-06-22 op d->prfx_attr = prfx;
280 d0149485 2021-06-22 op d->attr = line;
281 d0149485 2021-06-22 op d->trail_attr = trail;
282 d0149485 2021-06-22 op } else {
283 d0149485 2021-06-22 op return 0;
284 d0149485 2021-06-22 op }
285 d0149485 2021-06-22 op
286 d0149485 2021-06-22 op return 1;
287 986af347 2021-06-24 op }
288 986af347 2021-06-24 op
289 8d7c8aab 2021-06-24 op static inline void
290 986af347 2021-06-24 op tl_init_pair(int colors, int pair, int f, int b)
291 986af347 2021-06-24 op {
292 986af347 2021-06-24 op if (f >= colors || !enable_colors)
293 986af347 2021-06-24 op f = -1;
294 986af347 2021-06-24 op if (b >= colors || !enable_colors)
295 986af347 2021-06-24 op b = -1;
296 986af347 2021-06-24 op init_pair(pair, f, b);
297 d0149485 2021-06-22 op }
298 d0149485 2021-06-22 op
299 74a2587f 2021-06-21 op void
300 42d61f50 2021-06-24 op config_apply_style(void)
301 74a2587f 2021-06-21 op {
302 986af347 2021-06-24 op size_t i, colors, len;
303 74a2587f 2021-06-21 op struct lineface_descr *d;
304 74a2587f 2021-06-21 op struct line_face *f;
305 74a2587f 2021-06-21 op
306 986af347 2021-06-24 op colors = COLORS;
307 986af347 2021-06-24 op
308 74a2587f 2021-06-21 op len = sizeof(linefaces_descr)/sizeof(linefaces_descr[0]);
309 74a2587f 2021-06-21 op for (i = 0; i < len; ++i) {
310 74a2587f 2021-06-21 op d = &linefaces_descr[i];
311 74a2587f 2021-06-21 op f = &line_faces[i];
312 74a2587f 2021-06-21 op
313 986af347 2021-06-24 op tl_init_pair(colors, d->pp, d->prfx_fg, d->prfx_bg);
314 d0149485 2021-06-22 op f->prefix_prop = COLOR_PAIR(d->pp) | d->prfx_attr;
315 d89b86d6 2021-06-21 op
316 986af347 2021-06-24 op tl_init_pair(colors, d->p, d->fg, d->bg);
317 d0149485 2021-06-22 op f->text_prop = COLOR_PAIR(d->p) | d->attr;
318 d89b86d6 2021-06-21 op
319 986af347 2021-06-24 op tl_init_pair(colors, d->tp, d->trail_fg, d->trail_bg);
320 d0149485 2021-06-22 op f->trail_prop = COLOR_PAIR(d->tp) | d->trail_attr;
321 74a2587f 2021-06-21 op }
322 b598590d 2021-06-21 op
323 33d904b6 2021-06-22 op /* tab line */
324 986af347 2021-06-24 op tl_init_pair(colors, PTL_BG, tab_face.bg_fg, tab_face.bg_bg);
325 33d904b6 2021-06-22 op tab_face.background = COLOR_PAIR(PTL_BG) | tab_face.bg_attr;
326 33d904b6 2021-06-22 op
327 986af347 2021-06-24 op tl_init_pair(colors, PTL_TAB, tab_face.t_fg, tab_face.t_bg);
328 33d904b6 2021-06-22 op tab_face.tab = COLOR_PAIR(PTL_TAB) | tab_face.t_attr;
329 33d904b6 2021-06-22 op
330 986af347 2021-06-24 op tl_init_pair(colors, PTL_CURR, tab_face.c_fg, tab_face.c_bg);
331 33d904b6 2021-06-22 op tab_face.current = COLOR_PAIR(PTL_CURR) | tab_face.c_attr;
332 33d904b6 2021-06-22 op
333 33d904b6 2021-06-22 op /* body */
334 986af347 2021-06-24 op tl_init_pair(colors, PBODY, body_face.fg, body_face.bg);
335 b598590d 2021-06-21 op body_face.body = COLOR_PAIR(PBODY);
336 160abe04 2021-06-21 op
337 986af347 2021-06-24 op tl_init_pair(colors, PBLEFT, body_face.lfg, body_face.lbg);
338 160abe04 2021-06-21 op body_face.left = COLOR_PAIR(PBLEFT);
339 160abe04 2021-06-21 op
340 986af347 2021-06-24 op tl_init_pair(colors, PBRIGHT, body_face.rfg, body_face.rbg);
341 160abe04 2021-06-21 op body_face.right = COLOR_PAIR(PBRIGHT);
342 74a2587f 2021-06-21 op }