Blob


1 /*
2 * Copyright (c) 2021 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 "compl.h"
22 #include "telescope.h"
24 /*
25 * Provide completions for execute-extended-command (eecmd).
26 */
27 const char *
28 compl_eecmd(void **data, void **ret, const char **descr)
29 {
30 struct cmd **state = (struct cmd **)data;
32 /* first time: init the state */
33 if (*state == NULL)
34 *state = cmds;
36 if ((*state)->cmd == NULL)
37 return NULL;
39 *descr = (*state)->descr;
40 return (*state)++->cmd;
41 }
43 /*
44 * Provide completions for tab-select.
45 */
46 const char *
47 compl_ts(void **data, void **ret, const char **descr)
48 {
49 struct tab **tab = (struct tab **)data;
51 /* first time: init the state */
52 if (*tab == NULL)
53 *tab = TAILQ_FIRST(&tabshead);
54 else if ((*tab = TAILQ_NEXT(*tab, tabs)) == NULL)
55 return NULL;
57 *ret = *tab;
59 if (*(*tab)->buffer.page.title == '\0')
60 return (*tab)->hist_cur->h;
61 *descr = (*tab)->hist_cur->h;
62 return (*tab)->buffer.page.title;
63 }
65 /*
66 * Provide completions for link-select.
67 */
68 const char *
69 compl_ls(void **data, void **ret, const char **descr)
70 {
71 struct line **line = (struct line **)data;
72 struct line *l;
73 const char *link;
75 l = *line;
76 while (l != NULL && l->type != LINE_LINK)
77 l = TAILQ_NEXT(l, lines);
79 /* end of buffer */
80 if (l == NULL)
81 return NULL;
83 if ((link = l->line) == NULL) {
84 link = l->alt;
85 *descr = NULL;
86 } else
87 *descr = l->alt;
89 *ret = l;
90 *line = TAILQ_NEXT(l, lines);
91 return link;
92 }
94 /*
95 * Provide completions for swiper.
96 */
97 const char *
98 compl_swiper(void **data, void **ret, const char **descr)
99 {
100 struct line **line = (struct line **)data;
101 const char *text;
103 while (*line != NULL && (*line)->line == NULL)
104 *line = TAILQ_NEXT(*line, lines);
106 if (*line == NULL)
107 return NULL;
109 text = (*line)->line;
110 *ret = *line;
111 *line = TAILQ_NEXT(*line, lines);
112 return text;
115 /*
116 * Provide completions for toc
117 */
118 const char *
119 compl_toc(void **data, void **ret, const char **descr)
121 struct line **line = (struct line **)data;
122 struct line *l;
123 const char *text;
125 l = *line;
126 while (l != NULL && (l->line == NULL ||
127 (l->type != LINE_TITLE_1 &&
128 l->type != LINE_TITLE_2 &&
129 l->type != LINE_TITLE_3)))
130 l = TAILQ_NEXT(l, lines);
132 /* end of buffer */
133 if (l == NULL)
134 return NULL;
136 text = l->line;
137 *ret = l;
138 *line = TAILQ_NEXT(l, lines);
139 return text;