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 = INT_MAX;
26 int olivetti_mode = 0;
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 } linefaces_descr[] = {
34 [LINE_TEXT] = {.pp=PT_PRFX, .p=PT, .tp=PT_TRAIL},
35 [LINE_LINK] = {.pp=PL_PRFX, .p=PL, .tp=PL_TRAIL},
36 [LINE_TITLE_1] = {.pp=PT1_PRFX, .p=PT1, .tp=PT1_TRAIL},
37 [LINE_TITLE_2] = {.pp=PT2_PRFX, .p=PT1, .tp=PT1_TRAIL},
38 [LINE_TITLE_3] = {.pp=PT3_PRFX, .p=PT3, .tp=PT3_TRAIL},
39 [LINE_ITEM] = {.pp=PI_PRFX, .p=PI, .tp=PI_TRAIL},
40 [LINE_QUOTE] = {.pp=PQ_PRFX, .p=PQ, .tp=PQ_TRAIL},
41 [LINE_PRE_START] = {.pp=PPSTART_PRFX, .p=PT, .tp=PT_TRAIL},
42 [LINE_PRE_CONTENT] = {.pp=PP_PRFX, .p=PP, .tp=PP_TRAIL},
43 [LINE_PRE_END] = {.pp=PPEND_PRFX, .p=PPEND, .tp=PPEND_TRAIL},
44 };
46 struct lineprefix line_prefixes[] = {
47 [LINE_TEXT] = { "", "" },
48 [LINE_LINK] = { "=> ", " " },
49 [LINE_TITLE_1] = { "# ", " " },
50 [LINE_TITLE_2] = { "## ", " " },
51 [LINE_TITLE_3] = { "### ", " " },
52 [LINE_ITEM] = { "* ", " " },
53 [LINE_QUOTE] = { "> ", " " },
54 [LINE_PRE_START] = { "```", " " },
55 [LINE_PRE_CONTENT] = { "", "" },
56 [LINE_PRE_END] = { "```", "```" },
57 };
59 struct line_face line_faces[] = {
60 [LINE_TEXT] = { 0, 0, 0 },
61 [LINE_LINK] = { 0, A_UNDERLINE, 0 },
62 [LINE_TITLE_1] = { A_BOLD, A_BOLD, 0 },
63 [LINE_TITLE_2] = { A_BOLD, A_BOLD, 0 },
64 [LINE_TITLE_3] = { A_BOLD, A_BOLD, 0 },
65 [LINE_ITEM] = { 0, 0, 0 },
66 [LINE_QUOTE] = { 0, A_DIM, 0 },
67 [LINE_PRE_START] = { 0, 0, 0 },
68 [LINE_PRE_CONTENT] = { 0, 0, 0 },
69 [LINE_PRE_END] = { 0, 0, 0 },
70 };
72 struct tab_face tab_face = {
73 .background = A_REVERSE,
74 .tab = A_REVERSE,
75 .current_tab = A_NORMAL,
76 };
78 struct body_face body_face = {
79 .lbg = -1, .lfg = -1,
80 .bg = -1, .fg = -1,
81 .rbg = -1, .rfg = -1,
82 };
84 struct modeline_face modeline_face = {
85 .background = A_REVERSE,
86 };
88 struct minibuffer_face minibuffer_face = {
89 .background = A_NORMAL,
90 };
92 struct mapping {
93 const char *label;
94 int linetype;
95 } mappings[] = {
96 {"text", LINE_TEXT},
97 {"link", LINE_LINK},
98 {"title1", LINE_TITLE_1},
99 {"title2", LINE_TITLE_2},
100 {"title3", LINE_TITLE_3},
101 {"item", LINE_ITEM},
102 {"quote", LINE_QUOTE},
103 {"pre.start", LINE_PRE_START},
104 {"pre", LINE_PRE_CONTENT},
105 {"pre.end", LINE_PRE_END},
106 };
108 static struct mapping *
109 mapping_by_name(const char *name)
111 size_t i;
113 for (i = 0; i < sizeof(mappings)/sizeof(mappings[0]); ++i) {
114 if (!strcmp(name, mappings[i].label))
115 return &mappings[i];
118 return NULL;
121 void
122 config_init(void)
124 struct lineface_descr *d;
125 size_t i, len;
127 len = sizeof(linefaces_descr)/sizeof(linefaces_descr[0]);
128 for (i = 0; i < len; ++i) {
129 d = &linefaces_descr[i];
131 d->prfx_bg = d->bg = d->trail_bg = -1;
132 d->prfx_fg = d->fg = d->trail_fg = -1;
136 int
137 config_setprfx(const char *name, const char *prfx, const char *cont)
139 struct lineprefix *p;
140 struct mapping *m;
142 if (!has_prefix(name, "line."))
143 return 0;
144 name += 5;
146 if ((m = mapping_by_name(name)) == NULL)
147 return 0;
149 p = &line_prefixes[m->linetype];
150 p->prfx1 = prfx;
151 p->prfx2 = cont;
153 return 1;
156 int
157 config_setvari(const char *var, int val)
159 if (!strcmp(var, "fill-column")) {
160 if ((fill_column = val) <= 0)
161 fill_column = INT_MAX;
162 } else if (!strcmp(var, "olivetti-mode")) {
163 olivetti_mode = !!val;
164 } else if (!strcmp(var, "enable-colors")) {
165 enable_colors = !!val;
166 } else {
167 return 0;
170 return 1;
173 int
174 config_setvars(const char *var, char *val)
176 if (!strcmp(var, "new-tab-url")) {
177 if (new_tab_url != NULL)
178 free(new_tab_url);
179 new_tab_url = val;
180 } else
181 return 0;
182 return 1;
185 int
186 config_setcolor(int bg, const char *name, int prfx, int line, int trail)
188 struct mapping *m;
189 struct lineface_descr *d;
191 if (has_prefix(name, "line.")) {
192 name += 5;
194 if ((m = mapping_by_name(name)) == NULL)
195 return 0;
197 d = &linefaces_descr[m->linetype];
199 if (bg) {
200 d->prfx_bg = prfx;
201 d->bg = line;
202 d->trail_bg = trail;
203 } else {
204 d->prfx_fg = prfx;
205 d->fg = line;
206 d->trail_fg = trail;
208 } else if (!strcmp(name, "line")) {
209 if (bg) {
210 body_face.lbg = prfx;
211 body_face.bg = line;
212 body_face.rbg = trail;
213 } else {
214 body_face.fg = prfx;
215 body_face.fg = line;
216 body_face.fg = trail;
218 } else {
219 return 0;
222 return 1;
225 void
226 config_apply_colors(void)
228 size_t i, len;
229 struct lineface_descr *d;
230 struct line_face *f;
232 len = sizeof(linefaces_descr)/sizeof(linefaces_descr[0]);
233 for (i = 0; i < len; ++i) {
234 d = &linefaces_descr[i];
235 f = &line_faces[i];
237 init_pair(d->pp, d->prfx_fg, d->prfx_bg);
238 f->prefix_prop = COLOR_PAIR(d->pp);
240 init_pair(d->p, d->fg, d->bg);
241 f->text_prop = COLOR_PAIR(d->p);
243 init_pair(d->tp, d->trail_fg, d->trail_bg);
244 f->trail_prop = COLOR_PAIR(d->tp);
247 init_pair(PBODY, body_face.fg, body_face.bg);
248 body_face.body = COLOR_PAIR(PBODY);
250 init_pair(PBLEFT, body_face.lfg, body_face.lbg);
251 body_face.left = COLOR_PAIR(PBLEFT);
253 init_pair(PBRIGHT, body_face.rfg, body_face.rbg);
254 body_face.right = COLOR_PAIR(PBRIGHT);