2 b1e1e41a 2021-07-14 op * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
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.
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.
17 786e6deb 2021-07-21 op #include "compat.h"
19 b1e1e41a 2021-07-14 op #include <stdlib.h>
21 b1e1e41a 2021-07-14 op #include "compl.h"
22 b1e1e41a 2021-07-14 op #include "telescope.h"
23 43f29a9e 2022-02-26 op #include "session.h"
26 43f29a9e 2022-02-26 op * Provide completions for load-url (lu).
29 43f29a9e 2022-02-26 op compl_lu(void **data, void **ret, const char **descr)
31 43f29a9e 2022-02-26 op struct history_item **state = (struct history_item **)data;
33 43f29a9e 2022-02-26 op /* first time: init the state */
34 43f29a9e 2022-02-26 op if (*state == NULL)
35 43f29a9e 2022-02-26 op *state = history.items;
37 43f29a9e 2022-02-26 op if (*state == history.items + history.len)
40 43f29a9e 2022-02-26 op return (*state)++->uri;
44 a6717110 2021-07-14 op * Provide completions for execute-extended-command (eecmd).
47 b3be07ea 2021-07-18 op compl_eecmd(void **data, void **ret, const char **descr)
49 b1e1e41a 2021-07-14 op struct cmd **state = (struct cmd **)data;
51 b1e1e41a 2021-07-14 op /* first time: init the state */
52 b1e1e41a 2021-07-14 op if (*state == NULL)
53 b1e1e41a 2021-07-14 op *state = cmds;
55 b1e1e41a 2021-07-14 op if ((*state)->cmd == NULL)
58 b3be07ea 2021-07-18 op *descr = (*state)->descr;
59 af48e20e 2021-07-14 op return (*state)++->cmd;
63 a6717110 2021-07-14 op * Provide completions for tab-select.
66 b3be07ea 2021-07-18 op compl_ts(void **data, void **ret, const char **descr)
68 65601367 2021-07-14 op struct tab **tab = (struct tab **)data;
70 65601367 2021-07-14 op /* first time: init the state */
71 65601367 2021-07-14 op if (*tab == NULL)
72 65601367 2021-07-14 op *tab = TAILQ_FIRST(&tabshead);
73 65601367 2021-07-14 op else if ((*tab = TAILQ_NEXT(*tab, tabs)) == NULL)
78 65601367 2021-07-14 op if (*(*tab)->buffer.page.title == '\0')
79 65601367 2021-07-14 op return (*tab)->hist_cur->h;
80 b3be07ea 2021-07-18 op *descr = (*tab)->hist_cur->h;
81 65601367 2021-07-14 op return (*tab)->buffer.page.title;
85 753c6ac7 2021-07-14 op * Provide completions for link-select.
88 b3be07ea 2021-07-18 op compl_ls(void **data, void **ret, const char **descr)
90 753c6ac7 2021-07-14 op struct line **line = (struct line **)data;
91 753c6ac7 2021-07-14 op struct line *l;
92 753c6ac7 2021-07-14 op const char *link;
95 753c6ac7 2021-07-14 op while (l != NULL && l->type != LINE_LINK)
96 753c6ac7 2021-07-14 op l = TAILQ_NEXT(l, lines);
98 753c6ac7 2021-07-14 op /* end of buffer */
99 753c6ac7 2021-07-14 op if (l == NULL)
102 b3be07ea 2021-07-18 op if ((link = l->line) == NULL) {
103 f29fe8ca 2021-07-16 op link = l->alt;
104 b3be07ea 2021-07-18 op *descr = NULL;
106 b3be07ea 2021-07-18 op *descr = l->alt;
109 753c6ac7 2021-07-14 op *line = TAILQ_NEXT(l, lines);
114 c7453a38 2021-07-15 op * Provide completions for swiper.
117 b3be07ea 2021-07-18 op compl_swiper(void **data, void **ret, const char **descr)
119 753c6ac7 2021-07-14 op struct line **line = (struct line **)data;
120 753c6ac7 2021-07-14 op const char *text;
122 753c6ac7 2021-07-14 op while (*line != NULL && (*line)->line == NULL)
123 753c6ac7 2021-07-14 op *line = TAILQ_NEXT(*line, lines);
125 753c6ac7 2021-07-14 op if (*line == NULL)
128 753c6ac7 2021-07-14 op text = (*line)->line;
129 753c6ac7 2021-07-14 op *ret = *line;
130 753c6ac7 2021-07-14 op *line = TAILQ_NEXT(*line, lines);
135 edd9a650 2021-07-15 op * Provide completions for toc
138 b3be07ea 2021-07-18 op compl_toc(void **data, void **ret, const char **descr)
140 edd9a650 2021-07-15 op struct line **line = (struct line **)data;
141 edd9a650 2021-07-15 op struct line *l;
142 edd9a650 2021-07-15 op const char *text;
145 f29fe8ca 2021-07-16 op while (l != NULL && (l->line == NULL ||
146 f29fe8ca 2021-07-16 op (l->type != LINE_TITLE_1 &&
147 edd9a650 2021-07-15 op l->type != LINE_TITLE_2 &&
148 f29fe8ca 2021-07-16 op l->type != LINE_TITLE_3)))
149 edd9a650 2021-07-15 op l = TAILQ_NEXT(l, lines);
151 edd9a650 2021-07-15 op /* end of buffer */
152 edd9a650 2021-07-15 op if (l == NULL)
155 edd9a650 2021-07-15 op text = l->line;
157 edd9a650 2021-07-15 op *line = TAILQ_NEXT(l, lines);