Blame


1 b1e1e41a 2021-07-14 op /*
2 5a39f593 2024-02-05 op * Copyright (c) 2021, 2024 Omar Polo <op@omarpolo.com>
3 b1e1e41a 2021-07-14 op *
4 b1e1e41a 2021-07-14 op * Permission to use, copy, modify, and distribute this software for any
5 b1e1e41a 2021-07-14 op * purpose with or without fee is hereby granted, provided that the above
6 b1e1e41a 2021-07-14 op * copyright notice and this permission notice appear in all copies.
7 b1e1e41a 2021-07-14 op *
8 b1e1e41a 2021-07-14 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 b1e1e41a 2021-07-14 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 b1e1e41a 2021-07-14 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 b1e1e41a 2021-07-14 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 b1e1e41a 2021-07-14 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 b1e1e41a 2021-07-14 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 b1e1e41a 2021-07-14 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 b1e1e41a 2021-07-14 op */
16 b1e1e41a 2021-07-14 op
17 786e6deb 2021-07-21 op #include "compat.h"
18 786e6deb 2021-07-21 op
19 b1e1e41a 2021-07-14 op #include <stdlib.h>
20 b1e1e41a 2021-07-14 op
21 5a39f593 2024-02-05 op #include "certs.h"
22 cbe2da32 2024-02-06 op #include "cmd.h"
23 b1e1e41a 2021-07-14 op #include "compl.h"
24 65c49665 2024-01-23 op #include "hist.h"
25 b1e1e41a 2021-07-14 op #include "telescope.h"
26 43f29a9e 2022-02-26 op #include "session.h"
27 b1e1e41a 2021-07-14 op
28 a6717110 2021-07-14 op /*
29 43f29a9e 2022-02-26 op * Provide completions for load-url (lu).
30 43f29a9e 2022-02-26 op */
31 43f29a9e 2022-02-26 op const char *
32 43f29a9e 2022-02-26 op compl_lu(void **data, void **ret, const char **descr)
33 43f29a9e 2022-02-26 op {
34 43f29a9e 2022-02-26 op struct history_item **state = (struct history_item **)data;
35 43f29a9e 2022-02-26 op
36 43f29a9e 2022-02-26 op /* first time: init the state */
37 43f29a9e 2022-02-26 op if (*state == NULL)
38 43f29a9e 2022-02-26 op *state = history.items;
39 43f29a9e 2022-02-26 op
40 43f29a9e 2022-02-26 op if (*state == history.items + history.len)
41 43f29a9e 2022-02-26 op return NULL;
42 43f29a9e 2022-02-26 op
43 43f29a9e 2022-02-26 op return (*state)++->uri;
44 43f29a9e 2022-02-26 op }
45 43f29a9e 2022-02-26 op
46 43f29a9e 2022-02-26 op /*
47 a6717110 2021-07-14 op * Provide completions for execute-extended-command (eecmd).
48 a6717110 2021-07-14 op */
49 b1e1e41a 2021-07-14 op const char *
50 b3be07ea 2021-07-18 op compl_eecmd(void **data, void **ret, const char **descr)
51 b1e1e41a 2021-07-14 op {
52 b1e1e41a 2021-07-14 op struct cmd **state = (struct cmd **)data;
53 b1e1e41a 2021-07-14 op
54 b1e1e41a 2021-07-14 op /* first time: init the state */
55 b1e1e41a 2021-07-14 op if (*state == NULL)
56 b1e1e41a 2021-07-14 op *state = cmds;
57 b1e1e41a 2021-07-14 op
58 b1e1e41a 2021-07-14 op if ((*state)->cmd == NULL)
59 b1e1e41a 2021-07-14 op return NULL;
60 b1e1e41a 2021-07-14 op
61 b3be07ea 2021-07-18 op *descr = (*state)->descr;
62 af48e20e 2021-07-14 op return (*state)++->cmd;
63 b1e1e41a 2021-07-14 op }
64 65601367 2021-07-14 op
65 a6717110 2021-07-14 op /*
66 a6717110 2021-07-14 op * Provide completions for tab-select.
67 a6717110 2021-07-14 op */
68 65601367 2021-07-14 op const char *
69 b3be07ea 2021-07-18 op compl_ts(void **data, void **ret, const char **descr)
70 65601367 2021-07-14 op {
71 65601367 2021-07-14 op struct tab **tab = (struct tab **)data;
72 65601367 2021-07-14 op
73 65601367 2021-07-14 op /* first time: init the state */
74 65601367 2021-07-14 op if (*tab == NULL)
75 65601367 2021-07-14 op *tab = TAILQ_FIRST(&tabshead);
76 65601367 2021-07-14 op else if ((*tab = TAILQ_NEXT(*tab, tabs)) == NULL)
77 65601367 2021-07-14 op return NULL;
78 65601367 2021-07-14 op
79 65601367 2021-07-14 op *ret = *tab;
80 65601367 2021-07-14 op
81 65601367 2021-07-14 op if (*(*tab)->buffer.page.title == '\0')
82 65c49665 2024-01-23 op return hist_cur((*tab)->hist);
83 65c49665 2024-01-23 op *descr = hist_cur((*tab)->hist);
84 65601367 2021-07-14 op return (*tab)->buffer.page.title;
85 65601367 2021-07-14 op }
86 753c6ac7 2021-07-14 op
87 753c6ac7 2021-07-14 op /*
88 753c6ac7 2021-07-14 op * Provide completions for link-select.
89 753c6ac7 2021-07-14 op */
90 753c6ac7 2021-07-14 op const char *
91 b3be07ea 2021-07-18 op compl_ls(void **data, void **ret, const char **descr)
92 753c6ac7 2021-07-14 op {
93 753c6ac7 2021-07-14 op struct line **line = (struct line **)data;
94 753c6ac7 2021-07-14 op struct line *l;
95 753c6ac7 2021-07-14 op const char *link;
96 753c6ac7 2021-07-14 op
97 753c6ac7 2021-07-14 op l = *line;
98 753c6ac7 2021-07-14 op while (l != NULL && l->type != LINE_LINK)
99 753c6ac7 2021-07-14 op l = TAILQ_NEXT(l, lines);
100 753c6ac7 2021-07-14 op
101 753c6ac7 2021-07-14 op /* end of buffer */
102 753c6ac7 2021-07-14 op if (l == NULL)
103 753c6ac7 2021-07-14 op return NULL;
104 753c6ac7 2021-07-14 op
105 b3be07ea 2021-07-18 op if ((link = l->line) == NULL) {
106 f29fe8ca 2021-07-16 op link = l->alt;
107 b3be07ea 2021-07-18 op *descr = NULL;
108 b3be07ea 2021-07-18 op } else
109 b3be07ea 2021-07-18 op *descr = l->alt;
110 f29fe8ca 2021-07-16 op
111 753c6ac7 2021-07-14 op *ret = l;
112 753c6ac7 2021-07-14 op *line = TAILQ_NEXT(l, lines);
113 753c6ac7 2021-07-14 op return link;
114 753c6ac7 2021-07-14 op }
115 753c6ac7 2021-07-14 op
116 753c6ac7 2021-07-14 op /*
117 c7453a38 2021-07-15 op * Provide completions for swiper.
118 753c6ac7 2021-07-14 op */
119 753c6ac7 2021-07-14 op const char *
120 b3be07ea 2021-07-18 op compl_swiper(void **data, void **ret, const char **descr)
121 753c6ac7 2021-07-14 op {
122 753c6ac7 2021-07-14 op struct line **line = (struct line **)data;
123 753c6ac7 2021-07-14 op const char *text;
124 753c6ac7 2021-07-14 op
125 753c6ac7 2021-07-14 op while (*line != NULL && (*line)->line == NULL)
126 753c6ac7 2021-07-14 op *line = TAILQ_NEXT(*line, lines);
127 753c6ac7 2021-07-14 op
128 753c6ac7 2021-07-14 op if (*line == NULL)
129 753c6ac7 2021-07-14 op return NULL;
130 753c6ac7 2021-07-14 op
131 753c6ac7 2021-07-14 op text = (*line)->line;
132 753c6ac7 2021-07-14 op *ret = *line;
133 753c6ac7 2021-07-14 op *line = TAILQ_NEXT(*line, lines);
134 753c6ac7 2021-07-14 op return text;
135 753c6ac7 2021-07-14 op }
136 edd9a650 2021-07-15 op
137 edd9a650 2021-07-15 op /*
138 edd9a650 2021-07-15 op * Provide completions for toc
139 edd9a650 2021-07-15 op */
140 edd9a650 2021-07-15 op const char *
141 b3be07ea 2021-07-18 op compl_toc(void **data, void **ret, const char **descr)
142 edd9a650 2021-07-15 op {
143 edd9a650 2021-07-15 op struct line **line = (struct line **)data;
144 edd9a650 2021-07-15 op struct line *l;
145 edd9a650 2021-07-15 op const char *text;
146 edd9a650 2021-07-15 op
147 edd9a650 2021-07-15 op l = *line;
148 f29fe8ca 2021-07-16 op while (l != NULL && (l->line == NULL ||
149 f29fe8ca 2021-07-16 op (l->type != LINE_TITLE_1 &&
150 edd9a650 2021-07-15 op l->type != LINE_TITLE_2 &&
151 f29fe8ca 2021-07-16 op l->type != LINE_TITLE_3)))
152 edd9a650 2021-07-15 op l = TAILQ_NEXT(l, lines);
153 edd9a650 2021-07-15 op
154 edd9a650 2021-07-15 op /* end of buffer */
155 edd9a650 2021-07-15 op if (l == NULL)
156 edd9a650 2021-07-15 op return NULL;
157 edd9a650 2021-07-15 op
158 edd9a650 2021-07-15 op text = l->line;
159 edd9a650 2021-07-15 op *ret = l;
160 edd9a650 2021-07-15 op *line = TAILQ_NEXT(l, lines);
161 edd9a650 2021-07-15 op return text;
162 edd9a650 2021-07-15 op }
163 5a39f593 2024-02-05 op
164 5a39f593 2024-02-05 op /*
165 5a39f593 2024-02-05 op * Provide completions for use-certificate
166 5a39f593 2024-02-05 op */
167 5a39f593 2024-02-05 op const char *
168 5a39f593 2024-02-05 op compl_uc(void **data, void **ret, const char **descr)
169 5a39f593 2024-02-05 op {
170 5a39f593 2024-02-05 op const char ***state = (const char ***)data;
171 5a39f593 2024-02-05 op
172 5a39f593 2024-02-05 op /* first time: init the state */
173 5a39f593 2024-02-05 op if (*state == NULL)
174 5a39f593 2024-02-05 op *state = (const char **)identities;
175 5a39f593 2024-02-05 op
176 5a39f593 2024-02-05 op if (**state == NULL)
177 5a39f593 2024-02-05 op return NULL;
178 5a39f593 2024-02-05 op
179 5a39f593 2024-02-05 op /* XXX filling descr too would be nice */
180 5a39f593 2024-02-05 op return *((*state)++);
181 5a39f593 2024-02-05 op }