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