2 c92e529c 2021-06-15 op * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
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.
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.
17 c92e529c 2021-06-15 op #include "telescope.h"
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>
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;
29 74a2587f 2021-06-21 op static struct lineface_descr {
30 74a2587f 2021-06-21 op int prfx_used, used;
31 74a2587f 2021-06-21 op int prfx_pair, pair;
32 74a2587f 2021-06-21 op int prfx_bg, bg;
33 74a2587f 2021-06-21 op int prfx_fg, fg;
34 74a2587f 2021-06-21 op } linefaces_descr[] = {
35 74a2587f 2021-06-21 op [LINE_TEXT] = { 0, 0, PAIR_TEXT_PRFX, PAIR_TEXT, 0, 0, 0, 0 },
36 74a2587f 2021-06-21 op [LINE_LINK] = { 0, 0, PAIR_LINK_PRFX, PAIR_LINK, 0, 0, 0, 0 },
37 74a2587f 2021-06-21 op [LINE_TITLE_1] = { 0, 0, PAIR_TITLE_1_PRFX, PAIR_TITLE_1, 0, 0, 0, 0 },
38 74a2587f 2021-06-21 op [LINE_TITLE_2] = { 0, 0, PAIR_TITLE_2_PRFX, PAIR_TITLE_1, 0, 0, 0, 0 },
39 74a2587f 2021-06-21 op [LINE_TITLE_3] = { 0, 0, PAIR_TITLE_3_PRFX, PAIR_TITLE_3, 0, 0, 0, 0 },
40 74a2587f 2021-06-21 op [LINE_ITEM] = { 0, 0, PAIR_ITEM_PRFX, PAIR_ITEM, 0, 0, 0, 0 },
41 74a2587f 2021-06-21 op [LINE_QUOTE] = { 0, 0, PAIR_QUOTE_PRFX, PAIR_QUOTE, 0, 0, 0, 0 },
42 74a2587f 2021-06-21 op [LINE_PRE_START] = { 0, 0, PAIR_PRE_START_PRFX, PAIR_TEXT, 0, 0, 0, 0 },
43 74a2587f 2021-06-21 op [LINE_PRE_CONTENT] = { 0, 0, PAIR_PRE_PRFX, PAIR_PRE, 0, 0, 0, 0 },
44 74a2587f 2021-06-21 op [LINE_PRE_END] = { 0, 0, PAIR_PRE_END_PRFX, PAIR_PRE_END, 0, 0, 0, 0 },
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] = { "```", "```" },
60 c92e529c 2021-06-15 op struct line_face line_faces[] = {
61 c92e529c 2021-06-15 op [LINE_TEXT] = { 0, 0 },
62 c92e529c 2021-06-15 op [LINE_LINK] = { 0, A_UNDERLINE },
63 c92e529c 2021-06-15 op [LINE_TITLE_1] = { A_BOLD, A_BOLD },
64 c92e529c 2021-06-15 op [LINE_TITLE_2] = { A_BOLD, A_BOLD },
65 c92e529c 2021-06-15 op [LINE_TITLE_3] = { A_BOLD, A_BOLD },
66 c92e529c 2021-06-15 op [LINE_ITEM] = { 0, 0 },
67 c92e529c 2021-06-15 op [LINE_QUOTE] = { 0, A_DIM },
68 c92e529c 2021-06-15 op [LINE_PRE_START] = { 0, 0 },
69 c92e529c 2021-06-15 op [LINE_PRE_CONTENT] = { 0, 0 },
70 c92e529c 2021-06-15 op [LINE_PRE_END] = { 0, 0 },
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,
79 d5493194 2021-06-15 op struct modeline_face modeline_face = {
80 d5493194 2021-06-15 op .background = A_REVERSE,
83 d5493194 2021-06-15 op struct minibuffer_face minibuffer_face = {
84 d5493194 2021-06-15 op .background = A_NORMAL,
87 74a2587f 2021-06-21 op struct mapping {
88 74a2587f 2021-06-21 op const char *label;
91 74a2587f 2021-06-21 op int facetype_prfx;
92 74a2587f 2021-06-21 op } mappings[] = {
93 74a2587f 2021-06-21 op {"text", LINE_TEXT, PAIR_TEXT, PAIR_TEXT_PRFX},
94 74a2587f 2021-06-21 op {"link", LINE_LINK, PAIR_LINK, PAIR_LINK_PRFX},
95 74a2587f 2021-06-21 op {"title1", LINE_TITLE_1, PAIR_TITLE_1, PAIR_TITLE_1_PRFX},
96 74a2587f 2021-06-21 op {"title2", LINE_TITLE_2, PAIR_TITLE_2, PAIR_TITLE_2_PRFX},
97 74a2587f 2021-06-21 op {"title3", LINE_TITLE_3, PAIR_TITLE_3, PAIR_TITLE_3_PRFX},
98 74a2587f 2021-06-21 op {"item", LINE_ITEM, PAIR_ITEM, PAIR_ITEM_PRFX},
99 74a2587f 2021-06-21 op {"quote", LINE_QUOTE, PAIR_QUOTE, PAIR_QUOTE_PRFX},
100 74a2587f 2021-06-21 op {"pre.start", LINE_PRE_START, PAIR_PRE_START, PAIR_PRE_START_PRFX},
101 74a2587f 2021-06-21 op {"pre", LINE_PRE_CONTENT, PAIR_PRE, PAIR_PRE_PRFX},
102 74a2587f 2021-06-21 op {"pre.end", LINE_PRE_END, PAIR_PRE_END, PAIR_PRE_END_PRFX},
105 74a2587f 2021-06-21 op static struct mapping *
106 74a2587f 2021-06-21 op mapping_by_name(const char *name)
110 74a2587f 2021-06-21 op for (i = 0; i < sizeof(mappings)/sizeof(mappings[0]); ++i) {
111 74a2587f 2021-06-21 op if (!strcmp(name, mappings[i].label))
112 74a2587f 2021-06-21 op return &mappings[i];
119 69e1d1b6 2021-06-15 op config_setprfx(const char *name, int cont, const char *str)
121 69e1d1b6 2021-06-15 op struct lineprefix *p;
122 74a2587f 2021-06-21 op struct mapping *m;
124 69e1d1b6 2021-06-15 op if (!has_prefix(name, "line."))
128 74a2587f 2021-06-21 op if ((m = mapping_by_name(name)) == NULL)
131 74a2587f 2021-06-21 op p = &line_prefixes[m->linetype];
134 74a2587f 2021-06-21 op p->prfx2 = str;
136 74a2587f 2021-06-21 op p->prfx1 = str;
142 86e7d7b4 2021-06-19 op config_setvari(const char *var, int val)
144 1a99965e 2021-06-19 op if (!strcmp(var, "fill-column")) {
145 cc073cd2 2021-06-19 op if ((fill_column = val) <= 0)
146 2106687b 2021-06-19 op fill_column = INT_MAX;
147 72b18268 2021-06-19 op } else if (!strcmp(var, "olivetti-mode")) {
148 72b18268 2021-06-19 op olivetti_mode = !!val;
149 2c748a1f 2021-06-21 op } else if (!strcmp(var, "enable-colors")) {
150 2c748a1f 2021-06-21 op enable_colors = !!val;
159 86e7d7b4 2021-06-19 op config_setvars(const char *var, char *val)
161 db7cc27a 2021-06-19 op if (!strcmp(var, "new-tab-url")) {
162 db7cc27a 2021-06-19 op if (new_tab_url != NULL)
163 db7cc27a 2021-06-19 op free(new_tab_url);
164 db7cc27a 2021-06-19 op new_tab_url = val;
171 74a2587f 2021-06-21 op config_setcolor(const char *name, int prfx, int bg, int color)
173 74a2587f 2021-06-21 op struct mapping *m;
174 74a2587f 2021-06-21 op struct lineface_descr *d;
176 74a2587f 2021-06-21 op if (!has_prefix(name, "line."))
180 74a2587f 2021-06-21 op if ((m = mapping_by_name(name)) == NULL)
183 74a2587f 2021-06-21 op d = &linefaces_descr[m->linetype];
186 74a2587f 2021-06-21 op d->prfx_used = 1;
188 74a2587f 2021-06-21 op d->prfx_bg = color;
190 74a2587f 2021-06-21 op d->prfx_fg = color;
194 74a2587f 2021-06-21 op d->bg = color;
196 74a2587f 2021-06-21 op d->fg = color;
203 74a2587f 2021-06-21 op config_apply_colors(void)
205 74a2587f 2021-06-21 op size_t i, len;
206 74a2587f 2021-06-21 op struct lineface_descr *d;
207 74a2587f 2021-06-21 op struct line_face *f;
209 74a2587f 2021-06-21 op len = sizeof(linefaces_descr)/sizeof(linefaces_descr[0]);
210 74a2587f 2021-06-21 op for (i = 0; i < len; ++i) {
211 74a2587f 2021-06-21 op d = &linefaces_descr[i];
212 74a2587f 2021-06-21 op f = &line_faces[i];
214 74a2587f 2021-06-21 op if (d->prfx_used) {
215 74a2587f 2021-06-21 op init_pair(d->prfx_pair, d->prfx_fg, d->prfx_bg);
216 74a2587f 2021-06-21 op f->prefix_prop = COLOR_PAIR(d->prfx_pair);
219 74a2587f 2021-06-21 op if (d->used) {
220 74a2587f 2021-06-21 op init_pair(d->pair, d->fg, d->bg);
221 74a2587f 2021-06-21 op f->text_prop = COLOR_PAIR(d->pair);