Blame


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