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 used;
31 int pp, p, tp;
32 int prfx_bg, bg, trail_bg;
33 int prfx_fg, fg, trail_fg;
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 .background = A_REVERSE,
75 .tab = A_REVERSE,
76 .current_tab = A_NORMAL,
77 };
79 struct body_face body_face = {
80 .lbg = -1, .lfg = -1,
81 .bg = -1, .fg = -1,
82 .rbg = -1, .rfg = -1,
83 };
85 struct modeline_face modeline_face = {
86 .background = A_REVERSE,
87 };
89 struct minibuffer_face minibuffer_face = {
90 .background = A_NORMAL,
91 };
93 struct mapping {
94 const char *label;
95 int linetype;
96 } mappings[] = {
97 {"text", LINE_TEXT},
98 {"link", LINE_LINK},
99 {"title1", LINE_TITLE_1},
100 {"title2", LINE_TITLE_2},
101 {"title3", LINE_TITLE_3},
102 {"item", LINE_ITEM},
103 {"quote", LINE_QUOTE},
104 {"pre.start", LINE_PRE_START},
105 {"pre", LINE_PRE_CONTENT},
106 {"pre.end", LINE_PRE_END},
107 };
109 static struct mapping *
110 mapping_by_name(const char *name)
112 size_t i;
114 for (i = 0; i < sizeof(mappings)/sizeof(mappings[0]); ++i) {
115 if (!strcmp(name, mappings[i].label))
116 return &mappings[i];
119 return NULL;
122 int
123 config_setprfx(const char *name, const char *prfx, const char *cont)
125 struct lineprefix *p;
126 struct mapping *m;
128 if (!has_prefix(name, "line."))
129 return 0;
130 name += 5;
132 if ((m = mapping_by_name(name)) == NULL)
133 return 0;
135 p = &line_prefixes[m->linetype];
136 p->prfx1 = prfx;
137 p->prfx2 = cont;
139 return 1;
142 int
143 config_setvari(const char *var, int val)
145 if (!strcmp(var, "fill-column")) {
146 if ((fill_column = val) <= 0)
147 fill_column = INT_MAX;
148 } else if (!strcmp(var, "olivetti-mode")) {
149 olivetti_mode = !!val;
150 } else if (!strcmp(var, "enable-colors")) {
151 enable_colors = !!val;
152 } else {
153 return 0;
156 return 1;
159 int
160 config_setvars(const char *var, char *val)
162 if (!strcmp(var, "new-tab-url")) {
163 if (new_tab_url != NULL)
164 free(new_tab_url);
165 new_tab_url = val;
166 } else
167 return 0;
168 return 1;
171 int
172 config_setcolor(int bg, const char *name, int prfx, int line, int trail)
174 struct mapping *m;
175 struct lineface_descr *d;
177 if (has_prefix(name, "line.")) {
178 name += 5;
180 if ((m = mapping_by_name(name)) == NULL)
181 return 0;
183 d = &linefaces_descr[m->linetype];
185 d->used = 1;
186 if (bg) {
187 d->prfx_bg = prfx;
188 d->bg = line;
189 d->trail_bg = trail;
190 } else {
191 d->prfx_fg = prfx;
192 d->fg = line;
193 d->trail_fg = trail;
195 } else if (!strcmp(name, "line")) {
196 if (bg) {
197 body_face.lbg = prfx;
198 body_face.bg = line;
199 body_face.rbg = trail;
200 } else {
201 body_face.fg = prfx;
202 body_face.fg = line;
203 body_face.fg = trail;
205 } else {
206 return 0;
209 return 1;
212 void
213 config_apply_colors(void)
215 size_t i, len;
216 struct lineface_descr *d;
217 struct line_face *f;
219 len = sizeof(linefaces_descr)/sizeof(linefaces_descr[0]);
220 for (i = 0; i < len; ++i) {
221 d = &linefaces_descr[i];
222 f = &line_faces[i];
224 if (d->used) {
225 init_pair(d->pp, d->prfx_fg, d->prfx_bg);
226 f->prefix_prop = COLOR_PAIR(d->pp);
228 init_pair(d->p, d->fg, d->bg);
229 f->text_prop = COLOR_PAIR(d->p);
231 init_pair(d->tp, d->trail_fg, d->trail_bg);
232 f->trail_prop = COLOR_PAIR(d->tp);
236 init_pair(PBODY, body_face.fg, body_face.bg);
237 body_face.body = COLOR_PAIR(PBODY);
239 init_pair(PBLEFT, body_face.lfg, body_face.lbg);
240 body_face.left = COLOR_PAIR(PBLEFT);
242 init_pair(PBRIGHT, body_face.rfg, body_face.rbg);
243 body_face.right = COLOR_PAIR(PBRIGHT);