2 2b2d2872 2021-06-20 op * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
4 2b2d2872 2021-06-20 op * Permission to use, copy, modify, and distribute this software for any
5 2b2d2872 2021-06-20 op * purpose with or without fee is hereby granted, provided that the above
6 2b2d2872 2021-06-20 op * copyright notice and this permission notice appear in all copies.
8 2b2d2872 2021-06-20 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 2b2d2872 2021-06-20 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 2b2d2872 2021-06-20 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 2b2d2872 2021-06-20 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 2b2d2872 2021-06-20 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 2b2d2872 2021-06-20 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 2b2d2872 2021-06-20 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 61251035 2021-06-26 op #include <limits.h>
18 2b2d2872 2021-06-20 op #include <stdlib.h>
19 61251035 2021-06-26 op #include <string.h>
21 b1e1e41a 2021-07-14 op #include "compl.h"
22 e5a2797f 2021-07-13 op #include "defaults.h"
23 450a89f7 2021-07-12 op #include "minibuffer.h"
24 2b2d2872 2021-06-20 op #include "telescope.h"
25 d1a0f2a3 2021-07-12 op #include "ui.h"
26 5caf7d67 2021-07-12 op #include "utf8.h"
28 ca231710 2021-07-15 op #define GUARD_RECURSIVE_MINIBUFFER() \
30 ca231710 2021-07-15 op if (in_minibuffer) { \
31 ca231710 2021-07-15 op message("enable-recursive-minibuffers " \
32 ca231710 2021-07-15 op "is not yet available."); \
37 5a3c3ede 2021-07-19 op #define GUARD_READ_ONLY() \
39 5a3c3ede 2021-07-19 op if (!in_minibuffer) { \
40 5a3c3ede 2021-07-19 op message("text is read-only"); \
45 c4c82003 2021-07-01 op /* return 1 if moved, 0 otherwise */
46 c4c82003 2021-07-01 op static inline int
47 c4c82003 2021-07-01 op forward_line(struct buffer *buffer, int n)
49 c4c82003 2021-07-01 op struct vline *vl;
52 c4c82003 2021-07-01 op if (buffer->current_line == NULL)
54 963c680c 2021-07-05 op vl = buffer->current_line;
57 c4c82003 2021-07-01 op while (n != 0) {
59 963c680c 2021-07-05 op vl = TAILQ_NEXT(vl, vlines);
60 c4c82003 2021-07-01 op if (vl == NULL)
62 963c680c 2021-07-05 op if (vl->parent->flags & L_HIDDEN)
64 c4c82003 2021-07-01 op buffer->current_line = vl;
67 963c680c 2021-07-05 op vl = TAILQ_PREV(vl, vhead, vlines);
68 c4c82003 2021-07-01 op if (vl == NULL)
70 963c680c 2021-07-05 op if (vl->parent->flags & L_HIDDEN)
72 6e6c6afb 2021-07-14 op if (buffer->current_line == buffer->top_line) {
73 6e6c6afb 2021-07-14 op buffer->line_off--;
74 c4c82003 2021-07-01 op buffer->top_line = vl;
76 c4c82003 2021-07-01 op buffer->current_line = vl;
87 c4c82003 2021-07-01 op cmd_previous_line(struct buffer *buffer)
89 c4c82003 2021-07-01 op forward_line(buffer, -1);
93 c4c82003 2021-07-01 op cmd_next_line(struct buffer *buffer)
95 c4c82003 2021-07-01 op forward_line(buffer, +1);
99 2b2d2872 2021-06-20 op cmd_backward_char(struct buffer *buffer)
101 2b2d2872 2021-06-20 op if (buffer->cpoff != 0)
102 2b2d2872 2021-06-20 op buffer->cpoff--;
106 2b2d2872 2021-06-20 op cmd_forward_char(struct buffer *buffer)
108 2b2d2872 2021-06-20 op size_t len = 0;
110 aed6fae9 2021-07-19 op if (buffer->current_line == NULL)
113 2b2d2872 2021-06-20 op if (buffer->current_line->line != NULL)
114 2b2d2872 2021-06-20 op len = utf8_cplen(buffer->current_line->line);
115 2b2d2872 2021-06-20 op if (++buffer->cpoff > len)
116 2b2d2872 2021-06-20 op buffer->cpoff = len;
120 2b2d2872 2021-06-20 op cmd_backward_paragraph(struct buffer *buffer)
123 c4c82003 2021-07-01 op if (!forward_line(buffer, -1)) {
124 2b2d2872 2021-06-20 op message("No previous paragraph");
127 2b2d2872 2021-06-20 op } while (buffer->current_line->line != NULL ||
128 2b2d2872 2021-06-20 op buffer->current_line->parent->type != LINE_TEXT);
132 2b2d2872 2021-06-20 op cmd_forward_paragraph(struct buffer *buffer)
135 c4c82003 2021-07-01 op if (!forward_line(buffer, +1)) {
136 2b2d2872 2021-06-20 op message("No next paragraph");
139 2b2d2872 2021-06-20 op } while (buffer->current_line->line != NULL ||
140 2b2d2872 2021-06-20 op buffer->current_line->parent->type != LINE_TEXT);
144 2b2d2872 2021-06-20 op cmd_move_beginning_of_line(struct buffer *buffer)
146 2b2d2872 2021-06-20 op buffer->cpoff = 0;
150 2b2d2872 2021-06-20 op cmd_move_end_of_line(struct buffer *buffer)
152 2b2d2872 2021-06-20 op struct vline *vl;
154 2b2d2872 2021-06-20 op vl = buffer->current_line;
155 a87cfde9 2021-07-19 op if (vl == NULL || vl->line == NULL)
157 2b2d2872 2021-06-20 op buffer->cpoff = utf8_cplen(vl->line);
161 2b2d2872 2021-06-20 op cmd_redraw(struct buffer *buffer)
163 2b2d2872 2021-06-20 op ui_schedule_redraw();
167 2b2d2872 2021-06-20 op cmd_scroll_line_up(struct buffer *buffer)
169 e39920a9 2021-07-07 op struct vline *vl;
171 e39920a9 2021-07-07 op if ((vl = TAILQ_PREV(buffer->top_line, vhead, vlines))
175 e39920a9 2021-07-07 op forward_line(buffer, -1);
177 e39920a9 2021-07-07 op buffer->top_line = vl;
181 2b2d2872 2021-06-20 op cmd_scroll_line_down(struct buffer *buffer)
183 c4c82003 2021-07-01 op if (!forward_line(buffer, +1))
186 94ec38e8 2021-06-29 op buffer->top_line = TAILQ_NEXT(buffer->top_line, vlines);
187 2b2d2872 2021-06-20 op buffer->line_off++;
191 2b2d2872 2021-06-20 op cmd_scroll_up(struct buffer *buffer)
193 64923c4b 2021-07-15 op struct vline *vl;
196 64923c4b 2021-07-15 op for (i = 0; i < body_lines; ++i) {
197 64923c4b 2021-07-15 op vl = TAILQ_PREV(buffer->top_line, vhead, vlines);
198 64923c4b 2021-07-15 op if (vl == NULL)
200 eab11449 2021-07-19 op buffer->line_off--;
201 64923c4b 2021-07-15 op buffer->top_line = vl;
202 64923c4b 2021-07-15 op forward_line(buffer, -1);
207 2b2d2872 2021-06-20 op cmd_scroll_down(struct buffer *buffer)
211 64923c4b 2021-07-15 op for (i = 0; i < body_lines; ++i) {
212 64923c4b 2021-07-15 op if (!forward_line(buffer, +1))
215 64923c4b 2021-07-15 op buffer->top_line = TAILQ_NEXT(buffer->top_line,
217 eab11449 2021-07-19 op buffer->line_off++;
222 2b2d2872 2021-06-20 op cmd_beginning_of_buffer(struct buffer *buffer)
224 2b2d2872 2021-06-20 op buffer->current_line = TAILQ_FIRST(&buffer->head);
225 c4c82003 2021-07-01 op buffer->cpoff = 0;
226 94ec38e8 2021-06-29 op buffer->top_line = buffer->current_line;
227 2b2d2872 2021-06-20 op buffer->line_off = 0;
231 2b2d2872 2021-06-20 op cmd_end_of_buffer(struct buffer *buffer)
233 2b2d2872 2021-06-20 op buffer->current_line = TAILQ_LAST(&buffer->head, vhead);
235 52b1043c 2021-07-19 op if (buffer->current_line == NULL)
238 f04d2fc5 2021-07-18 op /* deal with invisible lines */
239 f04d2fc5 2021-07-18 op if (buffer->current_line->parent->flags & L_HIDDEN)
240 f04d2fc5 2021-07-18 op forward_line(buffer, -1);
242 f04d2fc5 2021-07-18 op cmd_move_end_of_line(buffer);
246 2b2d2872 2021-06-20 op cmd_kill_telescope(struct buffer *buffer)
248 2b2d2872 2021-06-20 op save_session();
249 2b2d2872 2021-06-20 op event_loopbreak();
253 2b2d2872 2021-06-20 op cmd_push_button(struct buffer *buffer)
255 2b2d2872 2021-06-20 op struct vline *vl;
256 963c680c 2021-07-05 op struct line *l;
258 2b2d2872 2021-06-20 op vl = buffer->current_line;
260 a87cfde9 2021-07-19 op if (vl == NULL)
263 963c680c 2021-07-05 op switch (vl->parent->type) {
264 963c680c 2021-07-05 op case LINE_LINK:
265 bd3a3a95 2021-07-20 op load_url_in_tab(current_tab, vl->parent->alt, NULL);
267 963c680c 2021-07-05 op case LINE_PRE_START:
268 963c680c 2021-07-05 op l = TAILQ_NEXT(vl->parent, lines);
269 963c680c 2021-07-05 op for (; l != NULL; l = TAILQ_NEXT(l, lines)) {
270 963c680c 2021-07-05 op if (l->type == LINE_PRE_END)
272 963c680c 2021-07-05 op l->flags ^= L_HIDDEN;
273 6e6c6afb 2021-07-14 op if (l->flags & L_HIDDEN)
274 6e6c6afb 2021-07-14 op buffer->line_max--;
276 6e6c6afb 2021-07-14 op buffer->line_max++;
285 2b2d2872 2021-06-20 op cmd_push_button_new_tab(struct buffer *buffer)
287 2b2d2872 2021-06-20 op struct vline *vl;
289 2b2d2872 2021-06-20 op vl = buffer->current_line;
290 a87cfde9 2021-07-19 op if (vl == NULL || vl->parent->type != LINE_LINK)
293 bd3a3a95 2021-07-20 op new_tab(vl->parent->alt, current_tab->hist_cur->h);
297 2b2d2872 2021-06-20 op cmd_previous_button(struct buffer *buffer)
299 5d0feb4b 2021-06-23 op struct excursion place;
301 5d0feb4b 2021-06-23 op save_excursion(&place, buffer);
304 c4c82003 2021-07-01 op if (!forward_line(buffer, -1)) {
305 5d0feb4b 2021-06-23 op restore_excursion(&place, buffer);
306 2b2d2872 2021-06-20 op message("No previous link");
309 2b2d2872 2021-06-20 op } while (buffer->current_line->parent->type != LINE_LINK);
313 2b2d2872 2021-06-20 op cmd_next_button(struct buffer *buffer)
315 5d0feb4b 2021-06-23 op struct excursion place;
317 5d0feb4b 2021-06-23 op save_excursion(&place, buffer);
320 c4c82003 2021-07-01 op if (!forward_line(buffer, +1)){
321 5d0feb4b 2021-06-23 op restore_excursion(&place, buffer);
322 2b2d2872 2021-06-20 op message("No next link");
325 2b2d2872 2021-06-20 op } while (buffer->current_line->parent->type != LINE_LINK);
328 1c412d48 2021-06-25 op static inline int
329 1c412d48 2021-06-25 op is_heading(const struct line *l)
331 1c412d48 2021-06-25 op return l->type == LINE_TITLE_1 ||
332 1c412d48 2021-06-25 op l->type == LINE_TITLE_2 ||
333 1c412d48 2021-06-25 op l->type == LINE_TITLE_3;
337 1c412d48 2021-06-25 op cmd_previous_heading(struct buffer *buffer)
339 1c412d48 2021-06-25 op struct excursion place;
341 1c412d48 2021-06-25 op save_excursion(&place, buffer);
344 c4c82003 2021-07-01 op if (!forward_line(buffer, -1)) {
345 1c412d48 2021-06-25 op restore_excursion(&place, buffer);
346 1c412d48 2021-06-25 op message("No previous heading");
349 1c412d48 2021-06-25 op } while (!is_heading(buffer->current_line->parent));
353 1c412d48 2021-06-25 op cmd_next_heading(struct buffer *buffer)
355 1c412d48 2021-06-25 op struct excursion place;
357 1c412d48 2021-06-25 op save_excursion(&place, buffer);
360 c4c82003 2021-07-01 op if (!forward_line(buffer, +1)) {
361 1c412d48 2021-06-25 op restore_excursion(&place, buffer);
362 1c412d48 2021-06-25 op message("No next heading");
365 1c412d48 2021-06-25 op } while (!is_heading(buffer->current_line->parent));
369 2b2d2872 2021-06-20 op cmd_previous_page(struct buffer *buffer)
371 83dce83d 2021-07-17 op if (!load_previous_page(current_tab))
372 2b2d2872 2021-06-20 op message("No previous page");
374 83dce83d 2021-07-17 op start_loading_anim(current_tab);
378 2b2d2872 2021-06-20 op cmd_next_page(struct buffer *buffer)
380 83dce83d 2021-07-17 op if (!load_next_page(current_tab))
381 2b2d2872 2021-06-20 op message("No next page");
383 83dce83d 2021-07-17 op start_loading_anim(current_tab);
387 2b2d2872 2021-06-20 op cmd_clear_minibuf(struct buffer *buffer)
389 2b2d2872 2021-06-20 op message(NULL);
393 2b2d2872 2021-06-20 op cmd_execute_extended_command(struct buffer *buffer)
397 ca231710 2021-07-15 op GUARD_RECURSIVE_MINIBUFFER();
399 2b2d2872 2021-06-20 op enter_minibuffer(eecmd_self_insert, eecmd_select, exit_minibuffer,
400 b1e1e41a 2021-07-14 op &eecmd_history, compl_eecmd, NULL);
402 2b2d2872 2021-06-20 op len = sizeof(ministate.prompt);
403 2b2d2872 2021-06-20 op strlcpy(ministate.prompt, "", len);
405 2b2d2872 2021-06-20 op if (thiskey.meta)
406 2b2d2872 2021-06-20 op strlcat(ministate.prompt, "M-", len);
408 2b2d2872 2021-06-20 op strlcat(ministate.prompt, ui_keyname(thiskey.key), len);
410 2b2d2872 2021-06-20 op if (thiskey.meta)
411 2b2d2872 2021-06-20 op strlcat(ministate.prompt, " ", len);
415 2b2d2872 2021-06-20 op cmd_tab_close(struct buffer *buffer)
417 2b2d2872 2021-06-20 op struct tab *tab, *t;
419 83dce83d 2021-07-17 op tab = current_tab;
421 90730426 2021-07-21 op if ((t = TAILQ_NEXT(tab, tabs)) != NULL ||
422 90730426 2021-07-21 op (t = TAILQ_PREV(tab, tabshead, tabs)) != NULL) {
423 90730426 2021-07-21 op switch_to_tab(t);
424 90730426 2021-07-21 op free_tab(tab);
426 90730426 2021-07-21 op message("Can't close the only tab.");
431 2b2d2872 2021-06-20 op cmd_tab_close_other(struct buffer *buffer)
433 2b2d2872 2021-06-20 op struct tab *t, *i;
435 2b2d2872 2021-06-20 op TAILQ_FOREACH_SAFE(t, &tabshead, tabs, i) {
436 83dce83d 2021-07-17 op if (t == current_tab)
444 2b2d2872 2021-06-20 op cmd_tab_new(struct buffer *buffer)
446 2b2d2872 2021-06-20 op const char *url;
448 2b2d2872 2021-06-20 op if ((url = new_tab_url) == NULL)
449 2b2d2872 2021-06-20 op url = NEW_TAB_URL;
451 bd3a3a95 2021-07-20 op new_tab(url, NULL);
455 2b2d2872 2021-06-20 op cmd_tab_next(struct buffer *buffer)
457 83dce83d 2021-07-17 op struct tab *t;
459 83dce83d 2021-07-17 op if ((t = TAILQ_NEXT(current_tab, tabs)) == NULL)
460 2b2d2872 2021-06-20 op t = TAILQ_FIRST(&tabshead);
461 2ddbda6b 2021-07-17 op switch_to_tab(t);
465 2b2d2872 2021-06-20 op cmd_tab_previous(struct buffer *buffer)
467 83dce83d 2021-07-17 op struct tab *t;
469 83dce83d 2021-07-17 op if ((t = TAILQ_PREV(current_tab, tabshead, tabs)) == NULL)
470 2b2d2872 2021-06-20 op t = TAILQ_LAST(&tabshead, tabshead);
471 2ddbda6b 2021-07-17 op switch_to_tab(t);
475 2b2d2872 2021-06-20 op cmd_tab_move(struct buffer *buffer)
477 83dce83d 2021-07-17 op struct tab *t;
479 83dce83d 2021-07-17 op t = TAILQ_NEXT(current_tab, tabs);
480 83dce83d 2021-07-17 op TAILQ_REMOVE(&tabshead, current_tab, tabs);
482 2b2d2872 2021-06-20 op if (t == NULL)
483 83dce83d 2021-07-17 op TAILQ_INSERT_HEAD(&tabshead, current_tab, tabs);
485 83dce83d 2021-07-17 op TAILQ_INSERT_AFTER(&tabshead, t, current_tab, tabs);
489 2b2d2872 2021-06-20 op cmd_tab_move_to(struct buffer *buffer)
491 83dce83d 2021-07-17 op struct tab *t;
493 83dce83d 2021-07-17 op t = TAILQ_PREV(current_tab, tabshead, tabs);
494 83dce83d 2021-07-17 op TAILQ_REMOVE(&tabshead, current_tab, tabs);
496 2b2d2872 2021-06-20 op if (t == NULL) {
497 2b2d2872 2021-06-20 op if (TAILQ_EMPTY(&tabshead))
498 83dce83d 2021-07-17 op TAILQ_INSERT_HEAD(&tabshead, current_tab, tabs);
500 83dce83d 2021-07-17 op TAILQ_INSERT_TAIL(&tabshead, current_tab, tabs);
502 83dce83d 2021-07-17 op TAILQ_INSERT_BEFORE(t, current_tab, tabs);
506 65601367 2021-07-14 op cmd_tab_select(struct buffer *buffer)
508 ca231710 2021-07-15 op GUARD_RECURSIVE_MINIBUFFER();
510 40fbc354 2021-07-14 op enter_minibuffer(sensible_self_insert, ts_select, exit_minibuffer,
511 65601367 2021-07-14 op NULL, compl_ts, NULL);
512 65601367 2021-07-14 op strlcpy(ministate.prompt, "Select tab: ", sizeof(ministate.prompt));
516 2b2d2872 2021-06-20 op cmd_load_url(struct buffer *buffer)
518 ca231710 2021-07-15 op GUARD_RECURSIVE_MINIBUFFER();
520 40fbc354 2021-07-14 op enter_minibuffer(sensible_self_insert, lu_select, exit_minibuffer,
521 e5a2797f 2021-07-13 op &lu_history, NULL, NULL);
522 2b2d2872 2021-06-20 op strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
523 2b2d2872 2021-06-20 op strlcpy(ministate.buf, "gemini://", sizeof(ministate.buf));
524 2b2d2872 2021-06-20 op cmd_move_end_of_line(&ministate.buffer);
528 2b2d2872 2021-06-20 op cmd_load_current_url(struct buffer *buffer)
530 ca231710 2021-07-15 op GUARD_RECURSIVE_MINIBUFFER();
532 40fbc354 2021-07-14 op enter_minibuffer(sensible_self_insert, lu_select, exit_minibuffer,
533 e5a2797f 2021-07-13 op &lu_history, NULL, NULL);
534 2b2d2872 2021-06-20 op strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
535 83dce83d 2021-07-17 op strlcpy(ministate.buf, current_tab->hist_cur->h, sizeof(ministate.buf));
536 2b2d2872 2021-06-20 op ministate.buffer.cpoff = utf8_cplen(ministate.buf);
540 661233ed 2021-07-14 op cmd_reload_page(struct buffer *buffer)
542 bd3a3a95 2021-07-20 op load_url_in_tab(current_tab, current_tab->hist_cur->h, NULL);
546 2b2d2872 2021-06-20 op cmd_bookmark_page(struct buffer *buffer)
548 ca231710 2021-07-15 op GUARD_RECURSIVE_MINIBUFFER();
550 40fbc354 2021-07-14 op enter_minibuffer(sensible_self_insert, bp_select, exit_minibuffer, NULL,
552 2b2d2872 2021-06-20 op strlcpy(ministate.prompt, "Bookmark URL: ", sizeof(ministate.prompt));
553 83dce83d 2021-07-17 op strlcpy(ministate.buf, current_tab->hist_cur->h, sizeof(ministate.buf));
554 2b2d2872 2021-06-20 op ministate.buffer.cpoff = utf8_cplen(ministate.buf);
558 2b2d2872 2021-06-20 op cmd_list_bookmarks(struct buffer *buffer)
560 bd3a3a95 2021-07-20 op load_url_in_tab(current_tab, "about:bookmarks", NULL);
564 2b2d2872 2021-06-20 op cmd_toggle_help(struct buffer *buffer)
566 2b2d2872 2021-06-20 op ui_toggle_side_window();
570 753c6ac7 2021-07-14 op cmd_link_select(struct buffer *buffer)
572 c1a72389 2021-07-15 op struct line *l;
574 ca231710 2021-07-15 op GUARD_RECURSIVE_MINIBUFFER();
576 c1a72389 2021-07-15 op l = TAILQ_FIRST(&buffer->page.head);
577 c1a72389 2021-07-15 op while (l != NULL && l->type != LINE_LINK)
578 c1a72389 2021-07-15 op l = TAILQ_NEXT(l, lines);
580 c1a72389 2021-07-15 op if (l == NULL) {
581 c1a72389 2021-07-15 op message("No links found");
585 40fbc354 2021-07-14 op enter_minibuffer(sensible_self_insert, ls_select, exit_minibuffer,
586 c1a72389 2021-07-15 op NULL, compl_ls, l);
587 753c6ac7 2021-07-14 op strlcpy(ministate.prompt, "Select link: ", sizeof(ministate.prompt));
591 753c6ac7 2021-07-14 op cmd_swiper(struct buffer *buffer)
593 ca231710 2021-07-15 op GUARD_RECURSIVE_MINIBUFFER();
595 40fbc354 2021-07-14 op enter_minibuffer(sensible_self_insert, swiper_select, exit_minibuffer,
596 753c6ac7 2021-07-14 op NULL, compl_swiper, TAILQ_FIRST(&buffer->page.head));
597 753c6ac7 2021-07-14 op strlcpy(ministate.prompt, "Select line: ", sizeof(ministate.prompt));
601 edd9a650 2021-07-15 op cmd_toc(struct buffer *buffer)
603 c1a72389 2021-07-15 op struct line *l;
605 ca231710 2021-07-15 op GUARD_RECURSIVE_MINIBUFFER();
607 c1a72389 2021-07-15 op l = TAILQ_FIRST(&buffer->page.head);
608 c1a72389 2021-07-15 op while (l != NULL &&
609 c1a72389 2021-07-15 op l->type != LINE_TITLE_1 &&
610 c1a72389 2021-07-15 op l->type != LINE_TITLE_2 &&
611 c1a72389 2021-07-15 op l->type != LINE_TITLE_3)
612 c1a72389 2021-07-15 op l = TAILQ_NEXT(l, lines);
614 c1a72389 2021-07-15 op if (l == NULL) {
615 c1a72389 2021-07-15 op message("No headings found");
619 edd9a650 2021-07-15 op enter_minibuffer(sensible_self_insert, toc_select, exit_minibuffer,
620 c1a72389 2021-07-15 op NULL, compl_toc, l);
621 edd9a650 2021-07-15 op strlcpy(ministate.prompt, "Select heading: ",
622 edd9a650 2021-07-15 op sizeof(ministate.prompt));
626 61251035 2021-06-26 op cmd_inc_fill_column(struct buffer *buffer)
628 61251035 2021-06-26 op if (fill_column == INT_MAX)
631 61251035 2021-06-26 op fill_column += 2;
632 61251035 2021-06-26 op message("fill-column: %d", fill_column);
634 61251035 2021-06-26 op ui_schedule_redraw();
638 61251035 2021-06-26 op cmd_dec_fill_column(struct buffer *buffer)
640 61251035 2021-06-26 op if (fill_column == INT_MAX || fill_column < 8)
643 61251035 2021-06-26 op fill_column -= 2;
644 61251035 2021-06-26 op message("fill-column: %d", fill_column);
646 61251035 2021-06-26 op ui_schedule_redraw();
650 2b2d2872 2021-06-20 op cmd_olivetti_mode(struct buffer *buffer)
652 2b2d2872 2021-06-20 op olivetti_mode = !olivetti_mode;
653 2b2d2872 2021-06-20 op if (olivetti_mode)
654 2b2d2872 2021-06-20 op message("olivetti-mode enabled");
656 2b2d2872 2021-06-20 op message("olivetti-mode disabled");
658 2b2d2872 2021-06-20 op ui_schedule_redraw();
662 2b2d2872 2021-06-20 op cmd_mini_delete_char(struct buffer *buffer)
666 5a3c3ede 2021-07-19 op GUARD_READ_ONLY();
668 2b2d2872 2021-06-20 op minibuffer_taint_hist();
670 2b2d2872 2021-06-20 op c = utf8_nth(buffer->current_line->line, buffer->cpoff);
671 2b2d2872 2021-06-20 op if (*c == '\0')
673 2b2d2872 2021-06-20 op n = utf8_next_cp(c);
675 2b2d2872 2021-06-20 op memmove(c, n, strlen(n)+1);
677 b1e1e41a 2021-07-14 op recompute_completions(0);
681 2b2d2872 2021-06-20 op cmd_mini_delete_backward_char(struct buffer *buffer)
683 2b2d2872 2021-06-20 op char *c, *p, *start;
685 5a3c3ede 2021-07-19 op GUARD_READ_ONLY();
687 2b2d2872 2021-06-20 op minibuffer_taint_hist();
689 2b2d2872 2021-06-20 op c = utf8_nth(buffer->current_line->line, buffer->cpoff);
690 2b2d2872 2021-06-20 op start = buffer->current_line->line;
691 2b2d2872 2021-06-20 op if (c == start)
693 2b2d2872 2021-06-20 op p = utf8_prev_cp(c-1, start);
695 2b2d2872 2021-06-20 op memmove(p, c, strlen(c)+1);
696 2b2d2872 2021-06-20 op buffer->cpoff--;
698 b1e1e41a 2021-07-14 op recompute_completions(0);
702 2b2d2872 2021-06-20 op cmd_mini_kill_line(struct buffer *buffer)
706 5a3c3ede 2021-07-19 op GUARD_READ_ONLY();
708 2b2d2872 2021-06-20 op minibuffer_taint_hist();
709 2b2d2872 2021-06-20 op c = utf8_nth(buffer->current_line->line, buffer->cpoff);
712 b1e1e41a 2021-07-14 op recompute_completions(0);
716 2b2d2872 2021-06-20 op cmd_mini_abort(struct buffer *buffer)
718 2b2d2872 2021-06-20 op if (!in_minibuffer)
721 2b2d2872 2021-06-20 op ministate.abortfn();
725 2b2d2872 2021-06-20 op cmd_mini_complete_and_exit(struct buffer *buffer)
727 2b2d2872 2021-06-20 op if (!in_minibuffer)
730 2b2d2872 2021-06-20 op minibuffer_taint_hist();
731 2b2d2872 2021-06-20 op ministate.donefn();
735 2b2d2872 2021-06-20 op cmd_mini_previous_history_element(struct buffer *buffer)
737 2b2d2872 2021-06-20 op if (ministate.history == NULL) {
738 2b2d2872 2021-06-20 op message("No history");
742 2b2d2872 2021-06-20 op if (ministate.hist_cur == NULL ||
743 2b2d2872 2021-06-20 op (ministate.hist_cur = TAILQ_PREV(ministate.hist_cur, mhisthead, entries)) == NULL) {
744 2b2d2872 2021-06-20 op ministate.hist_cur = TAILQ_LAST(&ministate.history->head, mhisthead);
745 2b2d2872 2021-06-20 op ministate.hist_off = ministate.history->len - 1;
746 2b2d2872 2021-06-20 op if (ministate.hist_cur == NULL)
747 fc14510d 2021-07-14 op message("No prev history item");
749 2b2d2872 2021-06-20 op ministate.hist_off--;
752 2b2d2872 2021-06-20 op if (ministate.hist_cur != NULL)
753 2b2d2872 2021-06-20 op buffer->current_line->line = ministate.hist_cur->h;
757 2b2d2872 2021-06-20 op cmd_mini_next_history_element(struct buffer *buffer)
759 2b2d2872 2021-06-20 op if (ministate.history == NULL) {
760 2b2d2872 2021-06-20 op message("No history");
764 2b2d2872 2021-06-20 op if (ministate.hist_cur == NULL ||
765 2b2d2872 2021-06-20 op (ministate.hist_cur = TAILQ_NEXT(ministate.hist_cur, entries)) == NULL) {
766 2b2d2872 2021-06-20 op ministate.hist_cur = TAILQ_FIRST(&ministate.history->head);
767 2b2d2872 2021-06-20 op ministate.hist_off = 0;
768 2b2d2872 2021-06-20 op if (ministate.hist_cur == NULL)
769 fc14510d 2021-07-14 op message("No next history item");
771 2b2d2872 2021-06-20 op ministate.hist_off++;
774 2b2d2872 2021-06-20 op if (ministate.hist_cur != NULL)
775 2b2d2872 2021-06-20 op buffer->current_line->line = ministate.hist_cur->h;
779 e7b982f4 2021-07-14 op cmd_previous_completion(struct buffer *buffer)
781 e7b982f4 2021-07-14 op if (in_minibuffer != MB_COMPREAD)
784 e7b982f4 2021-07-14 op buffer = &ministate.compl.buffer;
786 e7b982f4 2021-07-14 op if (buffer->current_line != NULL)
787 e7b982f4 2021-07-14 op buffer->current_line->parent->type = LINE_COMPL;
789 e7b982f4 2021-07-14 op forward_line(buffer, -1);
791 e7b982f4 2021-07-14 op if (buffer->current_line != NULL)
792 e7b982f4 2021-07-14 op buffer->current_line->parent->type = LINE_COMPL_CURRENT;
796 e7b982f4 2021-07-14 op cmd_next_completion(struct buffer *buffer)
798 e7b982f4 2021-07-14 op if (in_minibuffer != MB_COMPREAD)
801 e7b982f4 2021-07-14 op buffer = &ministate.compl.buffer;
803 e7b982f4 2021-07-14 op if (buffer->current_line != NULL)
804 e7b982f4 2021-07-14 op buffer->current_line->parent->type = LINE_COMPL;
806 e7b982f4 2021-07-14 op forward_line(buffer, +1);
808 e7b982f4 2021-07-14 op if (buffer->current_line != NULL)
809 e7b982f4 2021-07-14 op buffer->current_line->parent->type = LINE_COMPL_CURRENT;
813 e7b982f4 2021-07-14 op cmd_insert_current_candidate(struct buffer *buffer)
815 e7b982f4 2021-07-14 op struct vline *vl;
817 e7b982f4 2021-07-14 op if (in_minibuffer != MB_COMPREAD)
820 e7b982f4 2021-07-14 op buffer = &ministate.compl.buffer;
821 e7b982f4 2021-07-14 op if ((vl = buffer->current_line) == NULL)
824 e7b982f4 2021-07-14 op minibuffer_taint_hist();
825 e7b982f4 2021-07-14 op strlcpy(ministate.buf, vl->parent->line, sizeof(ministate.buf));
826 e7b982f4 2021-07-14 op ministate.buffer.cpoff = utf8_cplen(ministate.buf);
830 8a3b5609 2021-07-15 op cmd_suspend_telescope(struct buffer *buffer)
832 8a3b5609 2021-07-15 op message("Zzz...");
833 8a3b5609 2021-07-15 op ui_suspend();
837 987d9c88 2021-07-15 op cmd_toggle_pre_wrap(struct buffer *buffer)
839 987d9c88 2021-07-15 op dont_wrap_pre = !dont_wrap_pre;
841 987d9c88 2021-07-15 op if (dont_wrap_pre)
842 987d9c88 2021-07-15 op message("Don't wrap preformatted blocks");
844 987d9c88 2021-07-15 op message("Wrap preformatted blocks");
846 987d9c88 2021-07-15 op ui_schedule_redraw();
850 de190a2b 2021-07-17 op cmd_mini_goto_beginning(struct buffer *buffer)
852 de190a2b 2021-07-17 op struct vline *vl;
854 de190a2b 2021-07-17 op if (!in_minibuffer)
857 de190a2b 2021-07-17 op buffer = &ministate.compl.buffer;
859 de190a2b 2021-07-17 op if ((vl = buffer->current_line) != NULL)
860 de190a2b 2021-07-17 op vl->parent->type = LINE_COMPL;
862 de190a2b 2021-07-17 op vl = TAILQ_FIRST(&buffer->head);
863 de190a2b 2021-07-17 op while (vl != NULL && vl->parent->flags & L_HIDDEN)
864 de190a2b 2021-07-17 op vl = TAILQ_NEXT(vl, vlines);
866 de190a2b 2021-07-17 op if (vl == NULL)
869 de190a2b 2021-07-17 op vl->parent->type = LINE_COMPL_CURRENT;
870 de190a2b 2021-07-17 op buffer->top_line = vl;
871 de190a2b 2021-07-17 op buffer->current_line = vl;
875 de190a2b 2021-07-17 op cmd_mini_goto_end(struct buffer *buffer)
877 de190a2b 2021-07-17 op struct vline *vl;
879 de190a2b 2021-07-17 op if (!in_minibuffer)
882 de190a2b 2021-07-17 op buffer = &ministate.compl.buffer;
884 de190a2b 2021-07-17 op if ((vl = buffer->current_line) != NULL)
885 de190a2b 2021-07-17 op vl->parent->type = LINE_COMPL;
887 de190a2b 2021-07-17 op vl = TAILQ_LAST(&buffer->head, vhead);
888 de190a2b 2021-07-17 op while (vl != NULL && vl->parent->flags & L_HIDDEN)
889 de190a2b 2021-07-17 op vl = TAILQ_PREV(vl, vhead, vlines);
891 de190a2b 2021-07-17 op if (vl == NULL)
894 de190a2b 2021-07-17 op vl->parent->type = LINE_COMPL_CURRENT;
895 de190a2b 2021-07-17 op buffer->current_line = vl;