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 <limits.h>
18 #include <stdlib.h>
19 #include <string.h>
21 #include "minibuffer.h"
22 #include "telescope.h"
23 #include "ui.h"
25 /* return 1 if moved, 0 otherwise */
26 static inline int
27 forward_line(struct buffer *buffer, int n)
28 {
29 struct vline *vl;
30 int did;
32 if (buffer->current_line == NULL)
33 return 0;
34 vl = buffer->current_line;
36 did = 0;
37 while (n != 0) {
38 if (n > 0) {
39 vl = TAILQ_NEXT(vl, vlines);
40 if (vl == NULL)
41 return did;
42 if (vl->parent->flags & L_HIDDEN)
43 continue;
44 buffer->current_line = vl;
45 n--;
46 } else {
47 vl = TAILQ_PREV(vl, vhead, vlines);
48 if (vl == NULL)
49 return did;
50 if (vl->parent->flags & L_HIDDEN)
51 continue;
52 if (buffer->current_line == buffer->top_line)
53 buffer->top_line = vl;
54 buffer->current_line = vl;
55 n++;
56 }
58 did = 1;
59 }
61 return did;
62 }
64 void
65 cmd_previous_line(struct buffer *buffer)
66 {
67 forward_line(buffer, -1);
68 }
70 void
71 cmd_next_line(struct buffer *buffer)
72 {
73 forward_line(buffer, +1);
74 }
76 void
77 cmd_backward_char(struct buffer *buffer)
78 {
79 if (buffer->cpoff != 0)
80 buffer->cpoff--;
81 }
83 void
84 cmd_forward_char(struct buffer *buffer)
85 {
86 size_t len = 0;
88 if (buffer->current_line->line != NULL)
89 len = utf8_cplen(buffer->current_line->line);
90 if (++buffer->cpoff > len)
91 buffer->cpoff = len;
92 }
94 void
95 cmd_backward_paragraph(struct buffer *buffer)
96 {
97 do {
98 if (!forward_line(buffer, -1)) {
99 message("No previous paragraph");
100 return;
102 } while (buffer->current_line->line != NULL ||
103 buffer->current_line->parent->type != LINE_TEXT);
106 void
107 cmd_forward_paragraph(struct buffer *buffer)
109 do {
110 if (!forward_line(buffer, +1)) {
111 message("No next paragraph");
112 return;
114 } while (buffer->current_line->line != NULL ||
115 buffer->current_line->parent->type != LINE_TEXT);
118 void
119 cmd_move_beginning_of_line(struct buffer *buffer)
121 buffer->cpoff = 0;
124 void
125 cmd_move_end_of_line(struct buffer *buffer)
127 struct vline *vl;
129 vl = buffer->current_line;
130 if (vl->line == NULL)
131 return;
132 buffer->cpoff = utf8_cplen(vl->line);
135 void
136 cmd_redraw(struct buffer *buffer)
138 ui_schedule_redraw();
141 void
142 cmd_scroll_line_up(struct buffer *buffer)
144 struct vline *vl;
146 if ((vl = TAILQ_PREV(buffer->top_line, vhead, vlines))
147 == NULL)
148 return;
150 forward_line(buffer, -1);
152 buffer->line_off--;
153 buffer->top_line = vl;
156 void
157 cmd_scroll_line_down(struct buffer *buffer)
159 if (!forward_line(buffer, +1))
160 return;
162 buffer->top_line = TAILQ_NEXT(buffer->top_line, vlines);
163 buffer->line_off++;
166 void
167 cmd_scroll_up(struct buffer *buffer)
169 forward_line(buffer, -1*body_lines);
172 void
173 cmd_scroll_down(struct buffer *buffer)
175 forward_line(buffer, body_lines);
178 void
179 cmd_beginning_of_buffer(struct buffer *buffer)
181 buffer->current_line = TAILQ_FIRST(&buffer->head);
182 buffer->cpoff = 0;
183 buffer->top_line = buffer->current_line;
184 buffer->line_off = 0;
187 void
188 cmd_end_of_buffer(struct buffer *buffer)
190 buffer->current_line = TAILQ_LAST(&buffer->head, vhead);
191 buffer->cpoff = body_cols;
194 void
195 cmd_kill_telescope(struct buffer *buffer)
197 save_session();
198 event_loopbreak();
201 void
202 cmd_push_button(struct buffer *buffer)
204 struct vline *vl;
205 struct line *l;
207 vl = buffer->current_line;
208 switch (vl->parent->type) {
209 case LINE_LINK:
210 load_url_in_tab(current_tab(), vl->parent->alt);
211 break;
212 case LINE_PRE_START:
213 l = TAILQ_NEXT(vl->parent, lines);
214 for (; l != NULL; l = TAILQ_NEXT(l, lines)) {
215 if (l->type == LINE_PRE_END)
216 break;
217 l->flags ^= L_HIDDEN;
219 break;
220 default:
221 break;
225 void
226 cmd_push_button_new_tab(struct buffer *buffer)
228 struct vline *vl;
230 vl = buffer->current_line;
231 if (vl->parent->type != LINE_LINK)
232 return;
234 new_tab(vl->parent->alt);
237 void
238 cmd_previous_button(struct buffer *buffer)
240 struct excursion place;
242 save_excursion(&place, buffer);
244 do {
245 if (!forward_line(buffer, -1)) {
246 restore_excursion(&place, buffer);
247 message("No previous link");
248 return;
250 } while (buffer->current_line->parent->type != LINE_LINK);
253 void
254 cmd_next_button(struct buffer *buffer)
256 struct excursion place;
258 save_excursion(&place, buffer);
260 do {
261 if (!forward_line(buffer, +1)){
262 restore_excursion(&place, buffer);
263 message("No next link");
264 return;
266 } while (buffer->current_line->parent->type != LINE_LINK);
269 static inline int
270 is_heading(const struct line *l)
272 return l->type == LINE_TITLE_1 ||
273 l->type == LINE_TITLE_2 ||
274 l->type == LINE_TITLE_3;
277 void
278 cmd_previous_heading(struct buffer *buffer)
280 struct excursion place;
282 save_excursion(&place, buffer);
284 do {
285 if (!forward_line(buffer, -1)) {
286 restore_excursion(&place, buffer);
287 message("No previous heading");
288 return;
290 } while (!is_heading(buffer->current_line->parent));
293 void
294 cmd_next_heading(struct buffer *buffer)
296 struct excursion place;
298 save_excursion(&place, buffer);
300 do {
301 if (!forward_line(buffer, +1)) {
302 restore_excursion(&place, buffer);
303 message("No next heading");
304 return;
306 } while (!is_heading(buffer->current_line->parent));
309 void
310 cmd_previous_page(struct buffer *buffer)
312 struct tab *tab = current_tab();
314 if (!load_previous_page(tab))
315 message("No previous page");
316 else
317 start_loading_anim(tab);
320 void
321 cmd_next_page(struct buffer *buffer)
323 struct tab *tab = current_tab();
325 if (!load_next_page(tab))
326 message("No next page");
327 else
328 start_loading_anim(tab);
331 void
332 cmd_clear_minibuf(struct buffer *buffer)
334 message(NULL);
337 void
338 cmd_execute_extended_command(struct buffer *buffer)
340 size_t len;
342 if (in_minibuffer) {
343 message("We don't have enable-recursive-minibuffers");
344 return;
347 enter_minibuffer(eecmd_self_insert, eecmd_select, exit_minibuffer,
348 &eecmd_history);
350 len = sizeof(ministate.prompt);
351 strlcpy(ministate.prompt, "", len);
353 if (thiskey.meta)
354 strlcat(ministate.prompt, "M-", len);
356 strlcat(ministate.prompt, ui_keyname(thiskey.key), len);
358 if (thiskey.meta)
359 strlcat(ministate.prompt, " ", len);
362 void
363 cmd_tab_close(struct buffer *buffer)
365 struct tab *tab, *t;
367 tab = current_tab();
368 if (TAILQ_PREV(tab, tabshead, tabs) == NULL &&
369 TAILQ_NEXT(tab, tabs) == NULL) {
370 message("Can't close the only tab.");
371 return;
374 if (evtimer_pending(&tab->loadingev, NULL))
375 evtimer_del(&tab->loadingev);
377 stop_tab(tab);
379 if ((t = TAILQ_PREV(tab, tabshead, tabs)) == NULL)
380 t = TAILQ_NEXT(tab, tabs);
381 TAILQ_REMOVE(&tabshead, tab, tabs);
382 free(tab);
384 switch_to_tab(t);
387 void
388 cmd_tab_close_other(struct buffer *buffer)
390 struct tab *t, *i;
392 TAILQ_FOREACH_SAFE(t, &tabshead, tabs, i) {
393 if (t->flags & TAB_CURRENT)
394 continue;
396 stop_tab(t);
397 TAILQ_REMOVE(&tabshead, t, tabs);
398 free(t);
402 void
403 cmd_tab_new(struct buffer *buffer)
405 const char *url;
407 if ((url = new_tab_url) == NULL)
408 url = NEW_TAB_URL;
410 new_tab(url);
413 void
414 cmd_tab_next(struct buffer *buffer)
416 struct tab *tab, *t;
418 tab = current_tab();
419 tab->flags &= ~TAB_CURRENT;
421 if ((t = TAILQ_NEXT(tab, tabs)) == NULL)
422 t = TAILQ_FIRST(&tabshead);
423 t->flags |= TAB_CURRENT;
424 t->flags &= ~TAB_URGENT;
427 void
428 cmd_tab_previous(struct buffer *buffer)
430 struct tab *tab, *t;
432 tab = current_tab();
433 tab->flags &= ~TAB_CURRENT;
435 if ((t = TAILQ_PREV(tab, tabshead, tabs)) == NULL)
436 t = TAILQ_LAST(&tabshead, tabshead);
437 t->flags |= TAB_CURRENT;
438 t->flags &= ~TAB_URGENT;
441 void
442 cmd_tab_move(struct buffer *buffer)
444 struct tab *tab, *t;
446 tab = current_tab();
447 t = TAILQ_NEXT(tab, tabs);
448 TAILQ_REMOVE(&tabshead, tab, tabs);
450 if (t == NULL)
451 TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
452 else
453 TAILQ_INSERT_AFTER(&tabshead, t, tab, tabs);
456 void
457 cmd_tab_move_to(struct buffer *buffer)
459 struct tab *tab, *t;
461 tab = current_tab();
462 t = TAILQ_PREV(tab, tabshead, tabs);
463 TAILQ_REMOVE(&tabshead, tab, tabs);
465 if (t == NULL) {
466 if (TAILQ_EMPTY(&tabshead))
467 TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
468 else
469 TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
470 } else
471 TAILQ_INSERT_BEFORE(t, tab, tabs);
474 void
475 cmd_load_url(struct buffer *buffer)
477 if (in_minibuffer) {
478 message("We don't have enable-recursive-minibuffers");
479 return;
482 enter_minibuffer(lu_self_insert, lu_select, exit_minibuffer,
483 &lu_history);
484 strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
485 strlcpy(ministate.buf, "gemini://", sizeof(ministate.buf));
486 cmd_move_end_of_line(&ministate.buffer);
489 void
490 cmd_load_current_url(struct buffer *buffer)
492 struct tab *tab = current_tab();
494 if (in_minibuffer) {
495 message("We don't have enable-recursive-minibuffers");
496 return;
499 enter_minibuffer(lu_self_insert, lu_select, exit_minibuffer,
500 &lu_history);
501 strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
502 strlcpy(ministate.buf, tab->hist_cur->h, sizeof(ministate.buf));
503 ministate.buffer.cpoff = utf8_cplen(ministate.buf);
506 void
507 cmd_bookmark_page(struct buffer *buffer)
509 struct tab *tab = current_tab();
511 enter_minibuffer(lu_self_insert, bp_select, exit_minibuffer, NULL);
512 strlcpy(ministate.prompt, "Bookmark URL: ", sizeof(ministate.prompt));
513 strlcpy(ministate.buf, tab->hist_cur->h, sizeof(ministate.buf));
514 ministate.buffer.cpoff = utf8_cplen(ministate.buf);
517 void
518 cmd_list_bookmarks(struct buffer *buffer)
520 load_url_in_tab(current_tab(), "about:bookmarks");
523 void
524 cmd_toggle_help(struct buffer *buffer)
526 ui_toggle_side_window();
529 void
530 cmd_inc_fill_column(struct buffer *buffer)
532 if (fill_column == INT_MAX)
533 return;
535 fill_column += 2;
536 message("fill-column: %d", fill_column);
538 ui_schedule_redraw();
541 void
542 cmd_dec_fill_column(struct buffer *buffer)
544 if (fill_column == INT_MAX || fill_column < 8)
545 return;
547 fill_column -= 2;
548 message("fill-column: %d", fill_column);
550 ui_schedule_redraw();
553 void
554 cmd_olivetti_mode(struct buffer *buffer)
556 olivetti_mode = !olivetti_mode;
557 if (olivetti_mode)
558 message("olivetti-mode enabled");
559 else
560 message("olivetti-mode disabled");
562 ui_schedule_redraw();
565 void
566 cmd_mini_delete_char(struct buffer *buffer)
568 char *c, *n;
570 if (!in_minibuffer) {
571 message("text is read-only");
572 return;
575 minibuffer_taint_hist();
577 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
578 if (*c == '\0')
579 return;
580 n = utf8_next_cp(c);
582 memmove(c, n, strlen(n)+1);
585 void
586 cmd_mini_delete_backward_char(struct buffer *buffer)
588 char *c, *p, *start;
590 if (!in_minibuffer) {
591 message("text is read-only");
592 return;
595 minibuffer_taint_hist();
597 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
598 start = buffer->current_line->line;
599 if (c == start)
600 return;
601 p = utf8_prev_cp(c-1, start);
603 memmove(p, c, strlen(c)+1);
604 buffer->cpoff--;
607 void
608 cmd_mini_kill_line(struct buffer *buffer)
610 char *c;
612 if (!in_minibuffer) {
613 message("text is read-only");
614 return;
617 minibuffer_taint_hist();
618 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
619 *c = '\0';
622 void
623 cmd_mini_abort(struct buffer *buffer)
625 if (!in_minibuffer)
626 return;
628 ministate.abortfn();
631 void
632 cmd_mini_complete_and_exit(struct buffer *buffer)
634 if (!in_minibuffer)
635 return;
637 minibuffer_taint_hist();
638 ministate.donefn();
641 void
642 cmd_mini_previous_history_element(struct buffer *buffer)
644 if (ministate.history == NULL) {
645 message("No history");
646 return;
649 if (ministate.hist_cur == NULL ||
650 (ministate.hist_cur = TAILQ_PREV(ministate.hist_cur, mhisthead, entries)) == NULL) {
651 ministate.hist_cur = TAILQ_LAST(&ministate.history->head, mhisthead);
652 ministate.hist_off = ministate.history->len - 1;
653 if (ministate.hist_cur == NULL)
654 message("No prev item");
655 } else {
656 ministate.hist_off--;
659 if (ministate.hist_cur != NULL)
660 buffer->current_line->line = ministate.hist_cur->h;
663 void
664 cmd_mini_next_history_element(struct buffer *buffer)
666 if (ministate.history == NULL) {
667 message("No history");
668 return;
671 if (ministate.hist_cur == NULL ||
672 (ministate.hist_cur = TAILQ_NEXT(ministate.hist_cur, entries)) == NULL) {
673 ministate.hist_cur = TAILQ_FIRST(&ministate.history->head);
674 ministate.hist_off = 0;
675 if (ministate.hist_cur == NULL)
676 message("No next item");
677 } else {
678 ministate.hist_off++;
681 if (ministate.hist_cur != NULL)
682 buffer->current_line->line = ministate.hist_cur->h;