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 "telescope.h"
23 /* return 1 if moved, 0 otherwise */
24 static inline int
25 forward_line(struct buffer *buffer, int n)
26 {
27 struct vline *vl;
28 int did;
30 if (buffer->current_line == NULL)
31 return 0;
32 vl = buffer->current_line;
34 did = 0;
35 while (n != 0) {
36 if (n > 0) {
37 vl = TAILQ_NEXT(vl, vlines);
38 if (vl == NULL)
39 return did;
40 if (vl->parent->flags & L_HIDDEN)
41 continue;
42 buffer->current_line = vl;
43 n--;
44 } else {
45 vl = TAILQ_PREV(vl, vhead, vlines);
46 if (vl == NULL)
47 return did;
48 if (vl->parent->flags & L_HIDDEN)
49 continue;
50 if (buffer->current_line == buffer->top_line)
51 buffer->top_line = vl;
52 buffer->current_line = vl;
53 n++;
54 }
56 did = 1;
57 }
59 return did;
60 }
62 void
63 cmd_previous_line(struct buffer *buffer)
64 {
65 forward_line(buffer, -1);
66 }
68 void
69 cmd_next_line(struct buffer *buffer)
70 {
71 forward_line(buffer, +1);
72 }
74 void
75 cmd_backward_char(struct buffer *buffer)
76 {
77 if (buffer->cpoff != 0)
78 buffer->cpoff--;
79 }
81 void
82 cmd_forward_char(struct buffer *buffer)
83 {
84 size_t len = 0;
86 if (buffer->current_line->line != NULL)
87 len = utf8_cplen(buffer->current_line->line);
88 if (++buffer->cpoff > len)
89 buffer->cpoff = len;
90 }
92 void
93 cmd_backward_paragraph(struct buffer *buffer)
94 {
95 do {
96 if (!forward_line(buffer, -1)) {
97 message("No previous paragraph");
98 return;
99 }
100 } while (buffer->current_line->line != NULL ||
101 buffer->current_line->parent->type != LINE_TEXT);
104 void
105 cmd_forward_paragraph(struct buffer *buffer)
107 do {
108 if (!forward_line(buffer, +1)) {
109 message("No next paragraph");
110 return;
112 } while (buffer->current_line->line != NULL ||
113 buffer->current_line->parent->type != LINE_TEXT);
116 void
117 cmd_move_beginning_of_line(struct buffer *buffer)
119 buffer->cpoff = 0;
122 void
123 cmd_move_end_of_line(struct buffer *buffer)
125 struct vline *vl;
127 vl = buffer->current_line;
128 if (vl->line == NULL)
129 return;
130 buffer->cpoff = utf8_cplen(vl->line);
133 void
134 cmd_redraw(struct buffer *buffer)
136 ui_schedule_redraw();
139 void
140 cmd_scroll_line_up(struct buffer *buffer)
142 struct vline *vl;
144 if ((vl = TAILQ_PREV(buffer->top_line, vhead, vlines))
145 == NULL)
146 return;
148 forward_line(buffer, -1);
150 buffer->line_off--;
151 buffer->top_line = vl;
154 void
155 cmd_scroll_line_down(struct buffer *buffer)
157 if (!forward_line(buffer, +1))
158 return;
160 buffer->top_line = TAILQ_NEXT(buffer->top_line, vlines);
161 buffer->line_off++;
164 void
165 cmd_scroll_up(struct buffer *buffer)
167 forward_line(buffer, -1*body_lines);
170 void
171 cmd_scroll_down(struct buffer *buffer)
173 forward_line(buffer, body_lines);
176 void
177 cmd_beginning_of_buffer(struct buffer *buffer)
179 buffer->current_line = TAILQ_FIRST(&buffer->head);
180 buffer->cpoff = 0;
181 buffer->top_line = buffer->current_line;
182 buffer->line_off = 0;
185 void
186 cmd_end_of_buffer(struct buffer *buffer)
188 buffer->current_line = TAILQ_LAST(&buffer->head, vhead);
189 buffer->cpoff = body_cols;
192 void
193 cmd_kill_telescope(struct buffer *buffer)
195 save_session();
196 event_loopbreak();
199 void
200 cmd_push_button(struct buffer *buffer)
202 struct vline *vl;
203 struct line *l;
205 vl = buffer->current_line;
206 switch (vl->parent->type) {
207 case LINE_LINK:
208 load_url_in_tab(current_tab(), vl->parent->alt);
209 break;
210 case LINE_PRE_START:
211 l = TAILQ_NEXT(vl->parent, lines);
212 for (; l != NULL; l = TAILQ_NEXT(l, lines)) {
213 if (l->type == LINE_PRE_END)
214 break;
215 l->flags ^= L_HIDDEN;
217 break;
218 default:
219 break;
223 void
224 cmd_push_button_new_tab(struct buffer *buffer)
226 struct vline *vl;
228 vl = buffer->current_line;
229 if (vl->parent->type != LINE_LINK)
230 return;
232 new_tab(vl->parent->alt);
235 void
236 cmd_previous_button(struct buffer *buffer)
238 struct excursion place;
240 save_excursion(&place, buffer);
242 do {
243 if (!forward_line(buffer, -1)) {
244 restore_excursion(&place, buffer);
245 message("No previous link");
246 return;
248 } while (buffer->current_line->parent->type != LINE_LINK);
251 void
252 cmd_next_button(struct buffer *buffer)
254 struct excursion place;
256 save_excursion(&place, buffer);
258 do {
259 if (!forward_line(buffer, +1)){
260 restore_excursion(&place, buffer);
261 message("No next link");
262 return;
264 } while (buffer->current_line->parent->type != LINE_LINK);
267 static inline int
268 is_heading(const struct line *l)
270 return l->type == LINE_TITLE_1 ||
271 l->type == LINE_TITLE_2 ||
272 l->type == LINE_TITLE_3;
275 void
276 cmd_previous_heading(struct buffer *buffer)
278 struct excursion place;
280 save_excursion(&place, buffer);
282 do {
283 if (!forward_line(buffer, -1)) {
284 restore_excursion(&place, buffer);
285 message("No previous heading");
286 return;
288 } while (!is_heading(buffer->current_line->parent));
291 void
292 cmd_next_heading(struct buffer *buffer)
294 struct excursion place;
296 save_excursion(&place, buffer);
298 do {
299 if (!forward_line(buffer, +1)) {
300 restore_excursion(&place, buffer);
301 message("No next heading");
302 return;
304 } while (!is_heading(buffer->current_line->parent));
307 void
308 cmd_previous_page(struct buffer *buffer)
310 struct tab *tab = current_tab();
312 if (!load_previous_page(tab))
313 message("No previous page");
314 else
315 start_loading_anim(tab);
318 void
319 cmd_next_page(struct buffer *buffer)
321 struct tab *tab = current_tab();
323 if (!load_next_page(tab))
324 message("No next page");
325 else
326 start_loading_anim(tab);
329 void
330 cmd_clear_minibuf(struct buffer *buffer)
332 message(NULL);
335 void
336 cmd_execute_extended_command(struct buffer *buffer)
338 size_t len;
340 if (in_minibuffer) {
341 message("We don't have enable-recursive-minibuffers");
342 return;
345 enter_minibuffer(eecmd_self_insert, eecmd_select, exit_minibuffer,
346 &eecmd_history);
348 len = sizeof(ministate.prompt);
349 strlcpy(ministate.prompt, "", len);
351 if (thiskey.meta)
352 strlcat(ministate.prompt, "M-", len);
354 strlcat(ministate.prompt, ui_keyname(thiskey.key), len);
356 if (thiskey.meta)
357 strlcat(ministate.prompt, " ", len);
360 void
361 cmd_tab_close(struct buffer *buffer)
363 struct tab *tab, *t;
365 tab = current_tab();
366 if (TAILQ_PREV(tab, tabshead, tabs) == NULL &&
367 TAILQ_NEXT(tab, tabs) == NULL) {
368 message("Can't close the only tab.");
369 return;
372 if (evtimer_pending(&tab->loadingev, NULL))
373 evtimer_del(&tab->loadingev);
375 stop_tab(tab);
377 if ((t = TAILQ_PREV(tab, tabshead, tabs)) == NULL)
378 t = TAILQ_NEXT(tab, tabs);
379 TAILQ_REMOVE(&tabshead, tab, tabs);
380 free(tab);
382 switch_to_tab(t);
385 void
386 cmd_tab_close_other(struct buffer *buffer)
388 struct tab *t, *i;
390 TAILQ_FOREACH_SAFE(t, &tabshead, tabs, i) {
391 if (t->flags & TAB_CURRENT)
392 continue;
394 stop_tab(t);
395 TAILQ_REMOVE(&tabshead, t, tabs);
396 free(t);
400 void
401 cmd_tab_new(struct buffer *buffer)
403 const char *url;
405 if ((url = new_tab_url) == NULL)
406 url = NEW_TAB_URL;
408 new_tab(url);
411 void
412 cmd_tab_next(struct buffer *buffer)
414 struct tab *tab, *t;
416 tab = current_tab();
417 tab->flags &= ~TAB_CURRENT;
419 if ((t = TAILQ_NEXT(tab, tabs)) == NULL)
420 t = TAILQ_FIRST(&tabshead);
421 t->flags |= TAB_CURRENT;
422 t->flags &= ~TAB_URGENT;
425 void
426 cmd_tab_previous(struct buffer *buffer)
428 struct tab *tab, *t;
430 tab = current_tab();
431 tab->flags &= ~TAB_CURRENT;
433 if ((t = TAILQ_PREV(tab, tabshead, tabs)) == NULL)
434 t = TAILQ_LAST(&tabshead, tabshead);
435 t->flags |= TAB_CURRENT;
436 t->flags &= ~TAB_URGENT;
439 void
440 cmd_tab_move(struct buffer *buffer)
442 struct tab *tab, *t;
444 tab = current_tab();
445 t = TAILQ_NEXT(tab, tabs);
446 TAILQ_REMOVE(&tabshead, tab, tabs);
448 if (t == NULL)
449 TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
450 else
451 TAILQ_INSERT_AFTER(&tabshead, t, tab, tabs);
454 void
455 cmd_tab_move_to(struct buffer *buffer)
457 struct tab *tab, *t;
459 tab = current_tab();
460 t = TAILQ_PREV(tab, tabshead, tabs);
461 TAILQ_REMOVE(&tabshead, tab, tabs);
463 if (t == NULL) {
464 if (TAILQ_EMPTY(&tabshead))
465 TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
466 else
467 TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
468 } else
469 TAILQ_INSERT_BEFORE(t, tab, tabs);
472 void
473 cmd_load_url(struct buffer *buffer)
475 if (in_minibuffer) {
476 message("We don't have enable-recursive-minibuffers");
477 return;
480 enter_minibuffer(lu_self_insert, lu_select, exit_minibuffer,
481 &lu_history);
482 strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
483 strlcpy(ministate.buf, "gemini://", sizeof(ministate.buf));
484 cmd_move_end_of_line(&ministate.buffer);
487 void
488 cmd_load_current_url(struct buffer *buffer)
490 struct tab *tab = current_tab();
492 if (in_minibuffer) {
493 message("We don't have enable-recursive-minibuffers");
494 return;
497 enter_minibuffer(lu_self_insert, lu_select, exit_minibuffer,
498 &lu_history);
499 strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
500 strlcpy(ministate.buf, tab->hist_cur->h, sizeof(ministate.buf));
501 ministate.buffer.cpoff = utf8_cplen(ministate.buf);
504 void
505 cmd_bookmark_page(struct buffer *buffer)
507 struct tab *tab = current_tab();
509 enter_minibuffer(lu_self_insert, bp_select, exit_minibuffer, NULL);
510 strlcpy(ministate.prompt, "Bookmark URL: ", sizeof(ministate.prompt));
511 strlcpy(ministate.buf, tab->hist_cur->h, sizeof(ministate.buf));
512 ministate.buffer.cpoff = utf8_cplen(ministate.buf);
515 void
516 cmd_list_bookmarks(struct buffer *buffer)
518 load_url_in_tab(current_tab(), "about:bookmarks");
521 void
522 cmd_toggle_help(struct buffer *buffer)
524 ui_toggle_side_window();
527 void
528 cmd_inc_fill_column(struct buffer *buffer)
530 if (fill_column == INT_MAX)
531 return;
533 fill_column += 2;
534 message("fill-column: %d", fill_column);
536 ui_schedule_redraw();
539 void
540 cmd_dec_fill_column(struct buffer *buffer)
542 if (fill_column == INT_MAX || fill_column < 8)
543 return;
545 fill_column -= 2;
546 message("fill-column: %d", fill_column);
548 ui_schedule_redraw();
551 void
552 cmd_olivetti_mode(struct buffer *buffer)
554 olivetti_mode = !olivetti_mode;
555 if (olivetti_mode)
556 message("olivetti-mode enabled");
557 else
558 message("olivetti-mode disabled");
560 ui_schedule_redraw();
563 void
564 cmd_mini_delete_char(struct buffer *buffer)
566 char *c, *n;
568 if (!in_minibuffer) {
569 message("text is read-only");
570 return;
573 minibuffer_taint_hist();
575 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
576 if (*c == '\0')
577 return;
578 n = utf8_next_cp(c);
580 memmove(c, n, strlen(n)+1);
583 void
584 cmd_mini_delete_backward_char(struct buffer *buffer)
586 char *c, *p, *start;
588 if (!in_minibuffer) {
589 message("text is read-only");
590 return;
593 minibuffer_taint_hist();
595 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
596 start = buffer->current_line->line;
597 if (c == start)
598 return;
599 p = utf8_prev_cp(c-1, start);
601 memmove(p, c, strlen(c)+1);
602 buffer->cpoff--;
605 void
606 cmd_mini_kill_line(struct buffer *buffer)
608 char *c;
610 if (!in_minibuffer) {
611 message("text is read-only");
612 return;
615 minibuffer_taint_hist();
616 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
617 *c = '\0';
620 void
621 cmd_mini_abort(struct buffer *buffer)
623 if (!in_minibuffer)
624 return;
626 ministate.abortfn();
629 void
630 cmd_mini_complete_and_exit(struct buffer *buffer)
632 if (!in_minibuffer)
633 return;
635 minibuffer_taint_hist();
636 ministate.donefn();
639 void
640 cmd_mini_previous_history_element(struct buffer *buffer)
642 if (ministate.history == NULL) {
643 message("No history");
644 return;
647 if (ministate.hist_cur == NULL ||
648 (ministate.hist_cur = TAILQ_PREV(ministate.hist_cur, mhisthead, entries)) == NULL) {
649 ministate.hist_cur = TAILQ_LAST(&ministate.history->head, mhisthead);
650 ministate.hist_off = ministate.history->len - 1;
651 if (ministate.hist_cur == NULL)
652 message("No prev item");
653 } else {
654 ministate.hist_off--;
657 if (ministate.hist_cur != NULL)
658 buffer->current_line->line = ministate.hist_cur->h;
661 void
662 cmd_mini_next_history_element(struct buffer *buffer)
664 if (ministate.history == NULL) {
665 message("No history");
666 return;
669 if (ministate.hist_cur == NULL ||
670 (ministate.hist_cur = TAILQ_NEXT(ministate.hist_cur, entries)) == NULL) {
671 ministate.hist_cur = TAILQ_FIRST(&ministate.history->head);
672 ministate.hist_off = 0;
673 if (ministate.hist_cur == NULL)
674 message("No next item");
675 } else {
676 ministate.hist_off++;
679 if (ministate.hist_cur != NULL)
680 buffer->current_line->line = ministate.hist_cur->h;