Blob


1 /*
2 * Copyright (c) 2021, 2024 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 "compat.h"
19 #include <stdlib.h>
21 #include "certs.h"
22 #include "cmd.h"
23 #include "compl.h"
24 #include "hist.h"
25 #include "telescope.h"
26 #include "session.h"
28 /*
29 * Provide completions for load-url (lu).
30 */
31 const char *
32 compl_lu(void **data, void **ret, const char **descr)
33 {
34 struct history_item **state = (struct history_item **)data;
36 /* first time: init the state */
37 if (*state == NULL)
38 *state = history.items;
40 if (*state == history.items + history.len)
41 return NULL;
43 return (*state)++->uri;
44 }
46 /*
47 * Provide completions for execute-extended-command (eecmd).
48 */
49 const char *
50 compl_eecmd(void **data, void **ret, const char **descr)
51 {
52 struct cmd **state = (struct cmd **)data;
54 /* first time: init the state */
55 if (*state == NULL)
56 *state = cmds;
58 if ((*state)->cmd == NULL)
59 return NULL;
61 *descr = (*state)->descr;
62 return (*state)++->cmd;
63 }
65 /*
66 * Provide completions for tab-select.
67 */
68 const char *
69 compl_ts(void **data, void **ret, const char **descr)
70 {
71 struct tab **tab = (struct tab **)data;
73 /* first time: init the state */
74 if (*tab == NULL)
75 *tab = TAILQ_FIRST(&tabshead);
76 else if ((*tab = TAILQ_NEXT(*tab, tabs)) == NULL)
77 return NULL;
79 *ret = *tab;
81 if (*(*tab)->buffer.page.title == '\0')
82 return hist_cur((*tab)->hist);
83 *descr = hist_cur((*tab)->hist);
84 return (*tab)->buffer.page.title;
85 }
87 /*
88 * Provide completions for link-select.
89 */
90 const char *
91 compl_ls(void **data, void **ret, const char **descr)
92 {
93 struct line **line = (struct line **)data;
94 struct line *l;
95 const char *link;
97 l = *line;
98 while (l != NULL && l->type != LINE_LINK)
99 l = TAILQ_NEXT(l, lines);
101 /* end of buffer */
102 if (l == NULL)
103 return NULL;
105 if ((link = l->line) == NULL) {
106 link = l->alt;
107 *descr = NULL;
108 } else
109 *descr = l->alt;
111 *ret = l;
112 *line = TAILQ_NEXT(l, lines);
113 return link;
116 /*
117 * Provide completions for swiper.
118 */
119 const char *
120 compl_swiper(void **data, void **ret, const char **descr)
122 struct line **line = (struct line **)data;
123 const char *text;
125 while (*line != NULL && (*line)->line == NULL)
126 *line = TAILQ_NEXT(*line, lines);
128 if (*line == NULL)
129 return NULL;
131 text = (*line)->line;
132 *ret = *line;
133 *line = TAILQ_NEXT(*line, lines);
134 return text;
137 /*
138 * Provide completions for toc
139 */
140 const char *
141 compl_toc(void **data, void **ret, const char **descr)
143 struct line **line = (struct line **)data;
144 struct line *l;
145 const char *text;
147 l = *line;
148 while (l != NULL && (l->line == NULL ||
149 (l->type != LINE_TITLE_1 &&
150 l->type != LINE_TITLE_2 &&
151 l->type != LINE_TITLE_3)))
152 l = TAILQ_NEXT(l, lines);
154 /* end of buffer */
155 if (l == NULL)
156 return NULL;
158 text = l->line;
159 *ret = l;
160 *line = TAILQ_NEXT(l, lines);
161 return text;
164 /*
165 * Provide completions for use-certificate
166 */
167 const char *
168 compl_uc(void **data, void **ret, const char **descr)
170 const char ***state = (const char ***)data;
172 /* first time: init the state */
173 if (*state == NULL)
174 *state = (const char **)identities;
176 if (**state == NULL)
177 return NULL;
179 /* XXX filling descr too would be nice */
180 return *((*state)++);