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 1a99965e 2021-06-19 op int fill_column = INT_MAX;
26 72b18268 2021-06-19 op int olivetti_mode = 0;
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 used;
31 d89b86d6 2021-06-21 op int pp, p, tp;
32 d89b86d6 2021-06-21 op int prfx_bg, bg, trail_bg;
33 d89b86d6 2021-06-21 op int prfx_fg, fg, trail_fg;
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 c92e529c 2021-06-15 op .background = A_REVERSE,
75 c92e529c 2021-06-15 op .tab = A_REVERSE,
76 c92e529c 2021-06-15 op .current_tab = A_NORMAL,
77 c92e529c 2021-06-15 op };
78 d5493194 2021-06-15 op
79 b598590d 2021-06-21 op struct body_face body_face = {
80 160abe04 2021-06-21 op .lbg = -1, .lfg = -1,
81 160abe04 2021-06-21 op .bg = -1, .fg = -1,
82 160abe04 2021-06-21 op .rbg = -1, .rfg = -1,
83 b598590d 2021-06-21 op };
84 b598590d 2021-06-21 op
85 d5493194 2021-06-15 op struct modeline_face modeline_face = {
86 d5493194 2021-06-15 op .background = A_REVERSE,
87 d5493194 2021-06-15 op };
88 d5493194 2021-06-15 op
89 d5493194 2021-06-15 op struct minibuffer_face minibuffer_face = {
90 d5493194 2021-06-15 op .background = A_NORMAL,
91 d5493194 2021-06-15 op };
92 69e1d1b6 2021-06-15 op
93 74a2587f 2021-06-21 op struct mapping {
94 74a2587f 2021-06-21 op const char *label;
95 74a2587f 2021-06-21 op int linetype;
96 74a2587f 2021-06-21 op } mappings[] = {
97 d89b86d6 2021-06-21 op {"text", LINE_TEXT},
98 d89b86d6 2021-06-21 op {"link", LINE_LINK},
99 d89b86d6 2021-06-21 op {"title1", LINE_TITLE_1},
100 d89b86d6 2021-06-21 op {"title2", LINE_TITLE_2},
101 d89b86d6 2021-06-21 op {"title3", LINE_TITLE_3},
102 d89b86d6 2021-06-21 op {"item", LINE_ITEM},
103 d89b86d6 2021-06-21 op {"quote", LINE_QUOTE},
104 d89b86d6 2021-06-21 op {"pre.start", LINE_PRE_START},
105 d89b86d6 2021-06-21 op {"pre", LINE_PRE_CONTENT},
106 d89b86d6 2021-06-21 op {"pre.end", LINE_PRE_END},
107 74a2587f 2021-06-21 op };
108 74a2587f 2021-06-21 op
109 74a2587f 2021-06-21 op static struct mapping *
110 74a2587f 2021-06-21 op mapping_by_name(const char *name)
111 74a2587f 2021-06-21 op {
112 74a2587f 2021-06-21 op size_t i;
113 74a2587f 2021-06-21 op
114 74a2587f 2021-06-21 op for (i = 0; i < sizeof(mappings)/sizeof(mappings[0]); ++i) {
115 74a2587f 2021-06-21 op if (!strcmp(name, mappings[i].label))
116 74a2587f 2021-06-21 op return &mappings[i];
117 74a2587f 2021-06-21 op }
118 74a2587f 2021-06-21 op
119 74a2587f 2021-06-21 op return NULL;
120 74a2587f 2021-06-21 op }
121 74a2587f 2021-06-21 op
122 69e1d1b6 2021-06-15 op int
123 d89b86d6 2021-06-21 op config_setprfx(const char *name, const char *prfx, const char *cont)
124 69e1d1b6 2021-06-15 op {
125 69e1d1b6 2021-06-15 op struct lineprefix *p;
126 74a2587f 2021-06-21 op struct mapping *m;
127 69e1d1b6 2021-06-15 op
128 69e1d1b6 2021-06-15 op if (!has_prefix(name, "line."))
129 69e1d1b6 2021-06-15 op return 0;
130 69e1d1b6 2021-06-15 op name += 5;
131 69e1d1b6 2021-06-15 op
132 74a2587f 2021-06-21 op if ((m = mapping_by_name(name)) == NULL)
133 74a2587f 2021-06-21 op return 0;
134 69e1d1b6 2021-06-15 op
135 74a2587f 2021-06-21 op p = &line_prefixes[m->linetype];
136 d89b86d6 2021-06-21 op p->prfx1 = prfx;
137 d89b86d6 2021-06-21 op p->prfx2 = cont;
138 69e1d1b6 2021-06-15 op
139 74a2587f 2021-06-21 op return 1;
140 69e1d1b6 2021-06-15 op }
141 86e7d7b4 2021-06-19 op
142 86e7d7b4 2021-06-19 op int
143 86e7d7b4 2021-06-19 op config_setvari(const char *var, int val)
144 86e7d7b4 2021-06-19 op {
145 1a99965e 2021-06-19 op if (!strcmp(var, "fill-column")) {
146 cc073cd2 2021-06-19 op if ((fill_column = val) <= 0)
147 2106687b 2021-06-19 op fill_column = INT_MAX;
148 72b18268 2021-06-19 op } else if (!strcmp(var, "olivetti-mode")) {
149 72b18268 2021-06-19 op olivetti_mode = !!val;
150 2c748a1f 2021-06-21 op } else if (!strcmp(var, "enable-colors")) {
151 2c748a1f 2021-06-21 op enable_colors = !!val;
152 2c748a1f 2021-06-21 op } else {
153 1a99965e 2021-06-19 op return 0;
154 2c748a1f 2021-06-21 op }
155 2c748a1f 2021-06-21 op
156 1a99965e 2021-06-19 op return 1;
157 86e7d7b4 2021-06-19 op }
158 86e7d7b4 2021-06-19 op
159 86e7d7b4 2021-06-19 op int
160 86e7d7b4 2021-06-19 op config_setvars(const char *var, char *val)
161 86e7d7b4 2021-06-19 op {
162 db7cc27a 2021-06-19 op if (!strcmp(var, "new-tab-url")) {
163 db7cc27a 2021-06-19 op if (new_tab_url != NULL)
164 db7cc27a 2021-06-19 op free(new_tab_url);
165 db7cc27a 2021-06-19 op new_tab_url = val;
166 db7cc27a 2021-06-19 op } else
167 db7cc27a 2021-06-19 op return 0;
168 db7cc27a 2021-06-19 op return 1;
169 86e7d7b4 2021-06-19 op }
170 74a2587f 2021-06-21 op
171 74a2587f 2021-06-21 op int
172 d89b86d6 2021-06-21 op config_setcolor(int bg, const char *name, int prfx, int line, int trail)
173 74a2587f 2021-06-21 op {
174 74a2587f 2021-06-21 op struct mapping *m;
175 74a2587f 2021-06-21 op struct lineface_descr *d;
176 74a2587f 2021-06-21 op
177 50f7147c 2021-06-21 op if (has_prefix(name, "line.")) {
178 50f7147c 2021-06-21 op name += 5;
179 74a2587f 2021-06-21 op
180 50f7147c 2021-06-21 op if ((m = mapping_by_name(name)) == NULL)
181 50f7147c 2021-06-21 op return 0;
182 74a2587f 2021-06-21 op
183 50f7147c 2021-06-21 op d = &linefaces_descr[m->linetype];
184 74a2587f 2021-06-21 op
185 50f7147c 2021-06-21 op d->used = 1;
186 50f7147c 2021-06-21 op if (bg) {
187 50f7147c 2021-06-21 op d->prfx_bg = prfx;
188 50f7147c 2021-06-21 op d->bg = line;
189 50f7147c 2021-06-21 op d->trail_bg = trail;
190 50f7147c 2021-06-21 op } else {
191 50f7147c 2021-06-21 op d->prfx_fg = prfx;
192 50f7147c 2021-06-21 op d->fg = line;
193 50f7147c 2021-06-21 op d->trail_fg = trail;
194 50f7147c 2021-06-21 op }
195 b598590d 2021-06-21 op } else if (!strcmp(name, "line")) {
196 160abe04 2021-06-21 op if (bg) {
197 160abe04 2021-06-21 op body_face.lbg = prfx;
198 160abe04 2021-06-21 op body_face.bg = line;
199 160abe04 2021-06-21 op body_face.rbg = trail;
200 160abe04 2021-06-21 op } else {
201 b598590d 2021-06-21 op body_face.fg = prfx;
202 160abe04 2021-06-21 op body_face.fg = line;
203 160abe04 2021-06-21 op body_face.fg = trail;
204 160abe04 2021-06-21 op }
205 74a2587f 2021-06-21 op } else {
206 50f7147c 2021-06-21 op return 0;
207 74a2587f 2021-06-21 op }
208 74a2587f 2021-06-21 op
209 74a2587f 2021-06-21 op return 1;
210 74a2587f 2021-06-21 op }
211 74a2587f 2021-06-21 op
212 74a2587f 2021-06-21 op void
213 74a2587f 2021-06-21 op config_apply_colors(void)
214 74a2587f 2021-06-21 op {
215 74a2587f 2021-06-21 op size_t i, len;
216 74a2587f 2021-06-21 op struct lineface_descr *d;
217 74a2587f 2021-06-21 op struct line_face *f;
218 74a2587f 2021-06-21 op
219 74a2587f 2021-06-21 op len = sizeof(linefaces_descr)/sizeof(linefaces_descr[0]);
220 74a2587f 2021-06-21 op for (i = 0; i < len; ++i) {
221 74a2587f 2021-06-21 op d = &linefaces_descr[i];
222 74a2587f 2021-06-21 op f = &line_faces[i];
223 74a2587f 2021-06-21 op
224 74a2587f 2021-06-21 op if (d->used) {
225 d89b86d6 2021-06-21 op init_pair(d->pp, d->prfx_fg, d->prfx_bg);
226 d89b86d6 2021-06-21 op f->prefix_prop = COLOR_PAIR(d->pp);
227 d89b86d6 2021-06-21 op
228 d89b86d6 2021-06-21 op init_pair(d->p, d->fg, d->bg);
229 d89b86d6 2021-06-21 op f->text_prop = COLOR_PAIR(d->p);
230 d89b86d6 2021-06-21 op
231 d89b86d6 2021-06-21 op init_pair(d->tp, d->trail_fg, d->trail_bg);
232 d89b86d6 2021-06-21 op f->trail_prop = COLOR_PAIR(d->tp);
233 74a2587f 2021-06-21 op }
234 74a2587f 2021-06-21 op }
235 b598590d 2021-06-21 op
236 b598590d 2021-06-21 op init_pair(PBODY, body_face.fg, body_face.bg);
237 b598590d 2021-06-21 op body_face.body = COLOR_PAIR(PBODY);
238 160abe04 2021-06-21 op
239 160abe04 2021-06-21 op init_pair(PBLEFT, body_face.lfg, body_face.lbg);
240 160abe04 2021-06-21 op body_face.left = COLOR_PAIR(PBLEFT);
241 160abe04 2021-06-21 op
242 160abe04 2021-06-21 op init_pair(PBRIGHT, body_face.rfg, body_face.rbg);
243 160abe04 2021-06-21 op body_face.right = COLOR_PAIR(PBRIGHT);
244 74a2587f 2021-06-21 op }