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 c92e529c 2021-06-15 op struct lineprefix line_prefixes[] = {
30 c92e529c 2021-06-15 op [LINE_TEXT] = { "", "" },
31 c92e529c 2021-06-15 op [LINE_LINK] = { "=> ", " " },
32 c92e529c 2021-06-15 op [LINE_TITLE_1] = { "# ", " " },
33 c92e529c 2021-06-15 op [LINE_TITLE_2] = { "## ", " " },
34 c92e529c 2021-06-15 op [LINE_TITLE_3] = { "### ", " " },
35 c92e529c 2021-06-15 op [LINE_ITEM] = { "* ", " " },
36 c92e529c 2021-06-15 op [LINE_QUOTE] = { "> ", " " },
37 c92e529c 2021-06-15 op [LINE_PRE_START] = { "```", " " },
38 c92e529c 2021-06-15 op [LINE_PRE_CONTENT] = { "", "" },
39 c92e529c 2021-06-15 op [LINE_PRE_END] = { "```", "```" },
40 c92e529c 2021-06-15 op };
41 c92e529c 2021-06-15 op
42 c92e529c 2021-06-15 op struct line_face line_faces[] = {
43 2af29222 2021-06-24 op [LINE_TEXT] = {
44 2af29222 2021-06-24 op .prfx_pair = PT_PRFX,
45 2af29222 2021-06-24 op .pair = PT,
46 2af29222 2021-06-24 op .trail_pair = PT_TRAIL,
47 2af29222 2021-06-24 op },
48 2af29222 2021-06-24 op [LINE_LINK] = {
49 2af29222 2021-06-24 op .prfx_pair = PL_PRFX,
50 2af29222 2021-06-24 op .pair = PL,
51 2af29222 2021-06-24 op .trail_pair = PL_TRAIL,
52 2af29222 2021-06-24 op .attr = A_UNDERLINE,
53 2af29222 2021-06-24 op },
54 2af29222 2021-06-24 op [LINE_TITLE_1] = {
55 2af29222 2021-06-24 op .prfx_pair = PT1_PRFX,
56 2af29222 2021-06-24 op .pair = PT1,
57 2af29222 2021-06-24 op .trail_pair = PT1_TRAIL,
58 2af29222 2021-06-24 op .attr = A_BOLD,
59 2af29222 2021-06-24 op },
60 2af29222 2021-06-24 op [LINE_TITLE_2] = {
61 2af29222 2021-06-24 op .prfx_pair = PT2_PRFX,
62 2af29222 2021-06-24 op .pair = PT2,
63 2af29222 2021-06-24 op .trail_pair = PT2_TRAIL,
64 2af29222 2021-06-24 op .attr = A_BOLD,
65 2af29222 2021-06-24 op },
66 2af29222 2021-06-24 op [LINE_TITLE_3] = {
67 2af29222 2021-06-24 op .prfx_pair = PT3_PRFX,
68 2af29222 2021-06-24 op .pair = PT3,
69 2af29222 2021-06-24 op .trail_pair = PT3_TRAIL,
70 2af29222 2021-06-24 op .attr = A_BOLD,
71 2af29222 2021-06-24 op },
72 2af29222 2021-06-24 op [LINE_ITEM] = {
73 2af29222 2021-06-24 op .prfx_pair = PI_PRFX,
74 2af29222 2021-06-24 op .pair = PI,
75 2af29222 2021-06-24 op .trail_pair = PI_TRAIL,
76 2af29222 2021-06-24 op },
77 2af29222 2021-06-24 op [LINE_QUOTE] = {
78 2af29222 2021-06-24 op .prfx_pair = PQ_PRFX,
79 2af29222 2021-06-24 op .pair = PQ,
80 2af29222 2021-06-24 op .trail_pair = PQ_TRAIL,
81 2af29222 2021-06-24 op .attr = A_DIM,
82 2af29222 2021-06-24 op },
83 2af29222 2021-06-24 op [LINE_PRE_START] = {
84 2af29222 2021-06-24 op .prfx_pair = PPSTART_PRFX,
85 2af29222 2021-06-24 op .pair = PPSTART,
86 2af29222 2021-06-24 op .trail_pair = PPSTART_TRAIL,
87 2af29222 2021-06-24 op },
88 2af29222 2021-06-24 op [LINE_PRE_CONTENT] = {
89 2af29222 2021-06-24 op .prfx_pair = PP_PRFX,
90 2af29222 2021-06-24 op .pair = PP,
91 2af29222 2021-06-24 op .trail_pair = PP_TRAIL,
92 2af29222 2021-06-24 op },
93 2af29222 2021-06-24 op [LINE_PRE_END] = {
94 2af29222 2021-06-24 op .prfx_pair = PPEND_PRFX,
95 2af29222 2021-06-24 op .pair = PPEND,
96 2af29222 2021-06-24 op .trail_pair = PPEND_TRAIL,
97 2af29222 2021-06-24 op },
98 c92e529c 2021-06-15 op };
99 c92e529c 2021-06-15 op
100 c92e529c 2021-06-15 op struct tab_face tab_face = {
101 33d904b6 2021-06-22 op .bg_attr = A_REVERSE, .bg_bg = -1, .bg_fg = -1,
102 33d904b6 2021-06-22 op .t_attr = A_REVERSE, .t_bg = -1, .t_fg = -1,
103 33d904b6 2021-06-22 op .c_attr = A_NORMAL, .c_bg = -1, .c_fg = -1,
104 33d904b6 2021-06-22 op
105 33d904b6 2021-06-22 op /*
106 33d904b6 2021-06-22 op * set these so that even when enable-color=0 the bar has some
107 33d904b6 2021-06-22 op * sane defaults.
108 33d904b6 2021-06-22 op */
109 33d904b6 2021-06-22 op .background = A_REVERSE,
110 33d904b6 2021-06-22 op .tab = A_REVERSE,
111 33d904b6 2021-06-22 op .current = A_NORMAL,
112 c92e529c 2021-06-15 op };
113 d5493194 2021-06-15 op
114 b598590d 2021-06-21 op struct body_face body_face = {
115 160abe04 2021-06-21 op .lbg = -1, .lfg = -1,
116 160abe04 2021-06-21 op .bg = -1, .fg = -1,
117 160abe04 2021-06-21 op .rbg = -1, .rfg = -1,
118 b598590d 2021-06-21 op };
119 b598590d 2021-06-21 op
120 d5493194 2021-06-15 op struct modeline_face modeline_face = {
121 d5493194 2021-06-15 op .background = A_REVERSE,
122 d5493194 2021-06-15 op };
123 d5493194 2021-06-15 op
124 d5493194 2021-06-15 op struct minibuffer_face minibuffer_face = {
125 d5493194 2021-06-15 op .background = A_NORMAL,
126 d5493194 2021-06-15 op };
127 69e1d1b6 2021-06-15 op
128 74a2587f 2021-06-21 op struct mapping {
129 74a2587f 2021-06-21 op const char *label;
130 74a2587f 2021-06-21 op int linetype;
131 74a2587f 2021-06-21 op } mappings[] = {
132 d89b86d6 2021-06-21 op {"text", LINE_TEXT},
133 d89b86d6 2021-06-21 op {"link", LINE_LINK},
134 d89b86d6 2021-06-21 op {"title1", LINE_TITLE_1},
135 d89b86d6 2021-06-21 op {"title2", LINE_TITLE_2},
136 d89b86d6 2021-06-21 op {"title3", LINE_TITLE_3},
137 d89b86d6 2021-06-21 op {"item", LINE_ITEM},
138 d89b86d6 2021-06-21 op {"quote", LINE_QUOTE},
139 d89b86d6 2021-06-21 op {"pre.start", LINE_PRE_START},
140 d89b86d6 2021-06-21 op {"pre", LINE_PRE_CONTENT},
141 d89b86d6 2021-06-21 op {"pre.end", LINE_PRE_END},
142 74a2587f 2021-06-21 op };
143 74a2587f 2021-06-21 op
144 74a2587f 2021-06-21 op static struct mapping *
145 74a2587f 2021-06-21 op mapping_by_name(const char *name)
146 74a2587f 2021-06-21 op {
147 74a2587f 2021-06-21 op size_t i;
148 74a2587f 2021-06-21 op
149 74a2587f 2021-06-21 op for (i = 0; i < sizeof(mappings)/sizeof(mappings[0]); ++i) {
150 74a2587f 2021-06-21 op if (!strcmp(name, mappings[i].label))
151 74a2587f 2021-06-21 op return &mappings[i];
152 74a2587f 2021-06-21 op }
153 74a2587f 2021-06-21 op
154 74a2587f 2021-06-21 op return NULL;
155 74a2587f 2021-06-21 op }
156 74a2587f 2021-06-21 op
157 33fc3a8f 2021-06-21 op void
158 33fc3a8f 2021-06-21 op config_init(void)
159 33fc3a8f 2021-06-21 op {
160 2af29222 2021-06-24 op struct line_face *f;
161 33fc3a8f 2021-06-21 op size_t i, len;
162 33fc3a8f 2021-06-21 op
163 2af29222 2021-06-24 op len = sizeof(line_faces)/sizeof(line_faces[0]);
164 33fc3a8f 2021-06-21 op for (i = 0; i < len; ++i) {
165 2af29222 2021-06-24 op f = &line_faces[i];
166 33fc3a8f 2021-06-21 op
167 2af29222 2021-06-24 op f->prfx_bg = f->bg = f->trail_bg = -1;
168 2af29222 2021-06-24 op f->prfx_fg = f->fg = f->trail_fg = -1;
169 33fc3a8f 2021-06-21 op }
170 752e45c8 2021-06-25 op
171 752e45c8 2021-06-25 op line_faces[LINE_LINK].fg = COLOR_BLUE;
172 33fc3a8f 2021-06-21 op }
173 33fc3a8f 2021-06-21 op
174 69e1d1b6 2021-06-15 op int
175 d89b86d6 2021-06-21 op config_setprfx(const char *name, const char *prfx, const char *cont)
176 69e1d1b6 2021-06-15 op {
177 69e1d1b6 2021-06-15 op struct lineprefix *p;
178 74a2587f 2021-06-21 op struct mapping *m;
179 69e1d1b6 2021-06-15 op
180 69e1d1b6 2021-06-15 op if (!has_prefix(name, "line."))
181 69e1d1b6 2021-06-15 op return 0;
182 69e1d1b6 2021-06-15 op name += 5;
183 69e1d1b6 2021-06-15 op
184 74a2587f 2021-06-21 op if ((m = mapping_by_name(name)) == NULL)
185 74a2587f 2021-06-21 op return 0;
186 69e1d1b6 2021-06-15 op
187 74a2587f 2021-06-21 op p = &line_prefixes[m->linetype];
188 d89b86d6 2021-06-21 op p->prfx1 = prfx;
189 d89b86d6 2021-06-21 op p->prfx2 = cont;
190 69e1d1b6 2021-06-15 op
191 74a2587f 2021-06-21 op return 1;
192 69e1d1b6 2021-06-15 op }
193 86e7d7b4 2021-06-19 op
194 86e7d7b4 2021-06-19 op int
195 86e7d7b4 2021-06-19 op config_setvari(const char *var, int val)
196 86e7d7b4 2021-06-19 op {
197 1a99965e 2021-06-19 op if (!strcmp(var, "fill-column")) {
198 cc073cd2 2021-06-19 op if ((fill_column = val) <= 0)
199 2106687b 2021-06-19 op fill_column = INT_MAX;
200 72b18268 2021-06-19 op } else if (!strcmp(var, "olivetti-mode")) {
201 72b18268 2021-06-19 op olivetti_mode = !!val;
202 2c748a1f 2021-06-21 op } else if (!strcmp(var, "enable-colors")) {
203 2c748a1f 2021-06-21 op enable_colors = !!val;
204 2c748a1f 2021-06-21 op } else {
205 1a99965e 2021-06-19 op return 0;
206 2c748a1f 2021-06-21 op }
207 2c748a1f 2021-06-21 op
208 1a99965e 2021-06-19 op return 1;
209 86e7d7b4 2021-06-19 op }
210 86e7d7b4 2021-06-19 op
211 86e7d7b4 2021-06-19 op int
212 86e7d7b4 2021-06-19 op config_setvars(const char *var, char *val)
213 86e7d7b4 2021-06-19 op {
214 db7cc27a 2021-06-19 op if (!strcmp(var, "new-tab-url")) {
215 db7cc27a 2021-06-19 op if (new_tab_url != NULL)
216 db7cc27a 2021-06-19 op free(new_tab_url);
217 db7cc27a 2021-06-19 op new_tab_url = val;
218 db7cc27a 2021-06-19 op } else
219 db7cc27a 2021-06-19 op return 0;
220 db7cc27a 2021-06-19 op return 1;
221 86e7d7b4 2021-06-19 op }
222 74a2587f 2021-06-21 op
223 74a2587f 2021-06-21 op int
224 d89b86d6 2021-06-21 op config_setcolor(int bg, const char *name, int prfx, int line, int trail)
225 74a2587f 2021-06-21 op {
226 74a2587f 2021-06-21 op struct mapping *m;
227 2af29222 2021-06-24 op struct line_face *f;
228 74a2587f 2021-06-21 op
229 33d904b6 2021-06-22 op if (!strcmp(name, "tabline")) {
230 33d904b6 2021-06-22 op if (bg)
231 33d904b6 2021-06-22 op tab_face.bg_bg = prfx;
232 33d904b6 2021-06-22 op else
233 33d904b6 2021-06-22 op tab_face.bg_fg = prfx;
234 33d904b6 2021-06-22 op } else if (has_prefix(name, "tabline.")) {
235 33d904b6 2021-06-22 op name += 8;
236 33d904b6 2021-06-22 op
237 33d904b6 2021-06-22 op if (!strcmp(name, "tab")) {
238 33d904b6 2021-06-22 op if (bg)
239 33d904b6 2021-06-22 op tab_face.t_bg = prfx;
240 33d904b6 2021-06-22 op else
241 33d904b6 2021-06-22 op tab_face.t_fg = prfx;
242 33d904b6 2021-06-22 op } else if (!strcmp(name, "current")) {
243 33d904b6 2021-06-22 op if (bg)
244 33d904b6 2021-06-22 op tab_face.c_bg = prfx;
245 33d904b6 2021-06-22 op else
246 33d904b6 2021-06-22 op tab_face.c_fg = prfx;
247 33d904b6 2021-06-22 op } else
248 33d904b6 2021-06-22 op return 0;
249 33d904b6 2021-06-22 op } else if (has_prefix(name, "line.")) {
250 50f7147c 2021-06-21 op name += 5;
251 74a2587f 2021-06-21 op
252 50f7147c 2021-06-21 op if ((m = mapping_by_name(name)) == NULL)
253 50f7147c 2021-06-21 op return 0;
254 74a2587f 2021-06-21 op
255 2af29222 2021-06-24 op f = &line_faces[m->linetype];
256 74a2587f 2021-06-21 op
257 50f7147c 2021-06-21 op if (bg) {
258 2af29222 2021-06-24 op f->prfx_bg = prfx;
259 2af29222 2021-06-24 op f->bg = line;
260 2af29222 2021-06-24 op f->trail_bg = trail;
261 50f7147c 2021-06-21 op } else {
262 2af29222 2021-06-24 op f->prfx_fg = prfx;
263 2af29222 2021-06-24 op f->fg = line;
264 2af29222 2021-06-24 op f->trail_fg = trail;
265 50f7147c 2021-06-21 op }
266 b598590d 2021-06-21 op } else if (!strcmp(name, "line")) {
267 160abe04 2021-06-21 op if (bg) {
268 160abe04 2021-06-21 op body_face.lbg = prfx;
269 160abe04 2021-06-21 op body_face.bg = line;
270 160abe04 2021-06-21 op body_face.rbg = trail;
271 160abe04 2021-06-21 op } else {
272 b598590d 2021-06-21 op body_face.fg = prfx;
273 160abe04 2021-06-21 op body_face.fg = line;
274 160abe04 2021-06-21 op body_face.fg = trail;
275 160abe04 2021-06-21 op }
276 74a2587f 2021-06-21 op } else {
277 50f7147c 2021-06-21 op return 0;
278 74a2587f 2021-06-21 op }
279 74a2587f 2021-06-21 op
280 74a2587f 2021-06-21 op return 1;
281 74a2587f 2021-06-21 op }
282 74a2587f 2021-06-21 op
283 d0149485 2021-06-22 op int
284 d0149485 2021-06-22 op config_setattr(const char *name, int prfx, int line, int trail)
285 d0149485 2021-06-22 op {
286 d0149485 2021-06-22 op struct mapping *m;
287 2af29222 2021-06-24 op struct line_face *f;
288 d0149485 2021-06-22 op
289 33d904b6 2021-06-22 op if (!strcmp(name, "tabline")) {
290 33d904b6 2021-06-22 op tab_face.bg_attr = prfx;
291 33d904b6 2021-06-22 op } else if (has_prefix(name, "tabline.")) {
292 33d904b6 2021-06-22 op name += 8;
293 33d904b6 2021-06-22 op
294 33d904b6 2021-06-22 op if (!strcmp(name, "tab"))
295 33d904b6 2021-06-22 op tab_face.t_attr = prfx;
296 33d904b6 2021-06-22 op else if (!strcmp(name, "current"))
297 33d904b6 2021-06-22 op tab_face.c_attr = prfx;
298 33d904b6 2021-06-22 op else
299 33d904b6 2021-06-22 op return 0;
300 33d904b6 2021-06-22 op } else if (has_prefix(name, "line.")) {
301 d0149485 2021-06-22 op name += 5;
302 d0149485 2021-06-22 op
303 d0149485 2021-06-22 op if ((m = mapping_by_name(name)) == NULL)
304 d0149485 2021-06-22 op return 0;
305 d0149485 2021-06-22 op
306 2af29222 2021-06-24 op f = &line_faces[m->linetype];
307 d0149485 2021-06-22 op
308 2af29222 2021-06-24 op f->prfx_attr = prfx;
309 2af29222 2021-06-24 op f->attr = line;
310 2af29222 2021-06-24 op f->trail_attr = trail;
311 d0149485 2021-06-22 op } else {
312 d0149485 2021-06-22 op return 0;
313 d0149485 2021-06-22 op }
314 d0149485 2021-06-22 op
315 d0149485 2021-06-22 op return 1;
316 986af347 2021-06-24 op }
317 986af347 2021-06-24 op
318 8d7c8aab 2021-06-24 op static inline void
319 986af347 2021-06-24 op tl_init_pair(int colors, int pair, int f, int b)
320 986af347 2021-06-24 op {
321 986af347 2021-06-24 op if (f >= colors || !enable_colors)
322 986af347 2021-06-24 op f = -1;
323 986af347 2021-06-24 op if (b >= colors || !enable_colors)
324 986af347 2021-06-24 op b = -1;
325 986af347 2021-06-24 op init_pair(pair, f, b);
326 d0149485 2021-06-22 op }
327 d0149485 2021-06-22 op
328 74a2587f 2021-06-21 op void
329 42d61f50 2021-06-24 op config_apply_style(void)
330 74a2587f 2021-06-21 op {
331 986af347 2021-06-24 op size_t i, colors, len;
332 74a2587f 2021-06-21 op struct line_face *f;
333 74a2587f 2021-06-21 op
334 986af347 2021-06-24 op colors = COLORS;
335 986af347 2021-06-24 op
336 2af29222 2021-06-24 op len = sizeof(line_faces)/sizeof(line_faces[0]);
337 74a2587f 2021-06-21 op for (i = 0; i < len; ++i) {
338 74a2587f 2021-06-21 op f = &line_faces[i];
339 74a2587f 2021-06-21 op
340 2af29222 2021-06-24 op tl_init_pair(colors, f->prfx_pair, f->prfx_fg, f->prfx_bg);
341 2af29222 2021-06-24 op f->prefix = COLOR_PAIR(f->prfx_pair) | f->prfx_attr;
342 d89b86d6 2021-06-21 op
343 2af29222 2021-06-24 op tl_init_pair(colors, f->pair, f->fg, f->bg);
344 2af29222 2021-06-24 op f->text = COLOR_PAIR(f->pair) | f->attr;
345 d89b86d6 2021-06-21 op
346 2af29222 2021-06-24 op tl_init_pair(colors, f->trail_pair, f->trail_fg, f->trail_bg);
347 2af29222 2021-06-24 op f->trail = COLOR_PAIR(f->trail_pair) | f->trail_attr;
348 74a2587f 2021-06-21 op }
349 b598590d 2021-06-21 op
350 33d904b6 2021-06-22 op /* tab line */
351 986af347 2021-06-24 op tl_init_pair(colors, PTL_BG, tab_face.bg_fg, tab_face.bg_bg);
352 33d904b6 2021-06-22 op tab_face.background = COLOR_PAIR(PTL_BG) | tab_face.bg_attr;
353 33d904b6 2021-06-22 op
354 986af347 2021-06-24 op tl_init_pair(colors, PTL_TAB, tab_face.t_fg, tab_face.t_bg);
355 33d904b6 2021-06-22 op tab_face.tab = COLOR_PAIR(PTL_TAB) | tab_face.t_attr;
356 33d904b6 2021-06-22 op
357 986af347 2021-06-24 op tl_init_pair(colors, PTL_CURR, tab_face.c_fg, tab_face.c_bg);
358 33d904b6 2021-06-22 op tab_face.current = COLOR_PAIR(PTL_CURR) | tab_face.c_attr;
359 33d904b6 2021-06-22 op
360 33d904b6 2021-06-22 op /* body */
361 986af347 2021-06-24 op tl_init_pair(colors, PBODY, body_face.fg, body_face.bg);
362 b598590d 2021-06-21 op body_face.body = COLOR_PAIR(PBODY);
363 160abe04 2021-06-21 op
364 986af347 2021-06-24 op tl_init_pair(colors, PBLEFT, body_face.lfg, body_face.lbg);
365 160abe04 2021-06-21 op body_face.left = COLOR_PAIR(PBLEFT);
366 160abe04 2021-06-21 op
367 986af347 2021-06-24 op tl_init_pair(colors, PBRIGHT, body_face.rfg, body_face.rbg);
368 160abe04 2021-06-21 op body_face.right = COLOR_PAIR(PBRIGHT);
369 74a2587f 2021-06-21 op }