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 .bg = 0,
81 .fg = 0,
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 int
122 config_setprfx(const char *name, const char *prfx, const char *cont)
124 struct lineprefix *p;
125 struct mapping *m;
127 if (!has_prefix(name, "line."))
128 return 0;
129 name += 5;
131 if ((m = mapping_by_name(name)) == NULL)
132 return 0;
134 p = &line_prefixes[m->linetype];
135 p->prfx1 = prfx;
136 p->prfx2 = cont;
138 return 1;
141 int
142 config_setvari(const char *var, int val)
144 if (!strcmp(var, "fill-column")) {
145 if ((fill_column = val) <= 0)
146 fill_column = INT_MAX;
147 } else if (!strcmp(var, "olivetti-mode")) {
148 olivetti_mode = !!val;
149 } else if (!strcmp(var, "enable-colors")) {
150 enable_colors = !!val;
151 } else {
152 return 0;
155 return 1;
158 int
159 config_setvars(const char *var, char *val)
161 if (!strcmp(var, "new-tab-url")) {
162 if (new_tab_url != NULL)
163 free(new_tab_url);
164 new_tab_url = val;
165 } else
166 return 0;
167 return 1;
170 int
171 config_setcolor(int bg, const char *name, int prfx, int line, int trail)
173 struct mapping *m;
174 struct lineface_descr *d;
176 if (has_prefix(name, "line.")) {
177 name += 5;
179 if ((m = mapping_by_name(name)) == NULL)
180 return 0;
182 d = &linefaces_descr[m->linetype];
184 d->used = 1;
185 if (bg) {
186 d->prfx_bg = prfx;
187 d->bg = line;
188 d->trail_bg = trail;
189 } else {
190 d->prfx_fg = prfx;
191 d->fg = line;
192 d->trail_fg = trail;
194 } else if (!strcmp(name, "line")) {
195 if (bg)
196 body_face.bg = prfx;
197 else
198 body_face.fg = prfx;
199 } else {
200 return 0;
203 return 1;
206 void
207 config_apply_colors(void)
209 size_t i, len;
210 struct lineface_descr *d;
211 struct line_face *f;
213 len = sizeof(linefaces_descr)/sizeof(linefaces_descr[0]);
214 for (i = 0; i < len; ++i) {
215 d = &linefaces_descr[i];
216 f = &line_faces[i];
218 if (d->used) {
219 init_pair(d->pp, d->prfx_fg, d->prfx_bg);
220 f->prefix_prop = COLOR_PAIR(d->pp);
222 init_pair(d->p, d->fg, d->bg);
223 f->text_prop = COLOR_PAIR(d->p);
225 init_pair(d->tp, d->trail_fg, d->trail_bg);
226 f->trail_prop = COLOR_PAIR(d->tp);
230 init_pair(PBODY, body_face.fg, body_face.bg);
231 body_face.body = COLOR_PAIR(PBODY);