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 void
24 cmd_previous_line(struct buffer *buffer)
25 {
26 struct vline *vl;
28 if (buffer->current_line == NULL
29 || (vl = TAILQ_PREV(buffer->current_line, vhead, vlines)) == NULL)
30 return;
32 if (--buffer->curs_y < 0) {
33 buffer->curs_y = 0;
34 cmd_scroll_line_up(buffer);
35 return;
36 }
38 buffer->current_line = vl;
39 restore_cursor(buffer);
40 }
42 void
43 cmd_next_line(struct buffer *buffer)
44 {
45 struct vline *vl;
47 if (buffer->current_line == NULL
48 || (vl = TAILQ_NEXT(buffer->current_line, vlines)) == NULL)
49 return;
51 if (++buffer->curs_y > body_lines-1) {
52 buffer->curs_y = body_lines-1;
53 cmd_scroll_line_down(buffer);
54 return;
55 }
57 buffer->current_line = vl;
58 restore_cursor(buffer);
59 }
61 void
62 cmd_backward_char(struct buffer *buffer)
63 {
64 if (buffer->cpoff != 0)
65 buffer->cpoff--;
66 restore_cursor(buffer);
67 }
69 void
70 cmd_forward_char(struct buffer *buffer)
71 {
72 size_t len = 0;
74 if (buffer->current_line->line != NULL)
75 len = utf8_cplen(buffer->current_line->line);
76 if (++buffer->cpoff > len)
77 buffer->cpoff = len;
78 restore_cursor(buffer);
79 }
81 void
82 cmd_backward_paragraph(struct buffer *buffer)
83 {
84 do {
85 if (buffer->current_line == NULL ||
86 buffer->current_line == TAILQ_FIRST(&buffer->head)) {
87 message("No previous paragraph");
88 return;
89 }
90 cmd_previous_line(buffer);
91 } while (buffer->current_line->line != NULL ||
92 buffer->current_line->parent->type != LINE_TEXT);
93 }
95 void
96 cmd_forward_paragraph(struct buffer *buffer)
97 {
98 do {
99 if (buffer->current_line == NULL ||
100 buffer->current_line == TAILQ_LAST(&buffer->head, vhead)) {
101 message("No next paragraph");
102 return;
104 cmd_next_line(buffer);
105 } while (buffer->current_line->line != NULL ||
106 buffer->current_line->parent->type != LINE_TEXT);
109 void
110 cmd_move_beginning_of_line(struct buffer *buffer)
112 buffer->cpoff = 0;
113 restore_cursor(buffer);
116 void
117 cmd_move_end_of_line(struct buffer *buffer)
119 struct vline *vl;
121 vl = buffer->current_line;
122 if (vl->line == NULL)
123 return;
124 buffer->cpoff = utf8_cplen(vl->line);
125 restore_cursor(buffer);
128 void
129 cmd_redraw(struct buffer *buffer)
131 ui_schedule_redraw();
134 void
135 cmd_scroll_line_up(struct buffer *buffer)
137 if (buffer->current_line == NULL)
138 return;
140 if (buffer->line_off == 0)
141 return;
143 buffer->line_off--;
144 buffer->current_line = TAILQ_PREV(buffer->current_line, vhead, vlines);
145 restore_cursor(buffer);
148 void
149 cmd_scroll_line_down(struct buffer *buffer)
151 struct vline *vl;
153 if (buffer->current_line == NULL)
154 return;
156 if ((vl = TAILQ_NEXT(buffer->current_line, vlines)) == NULL)
157 return;
159 buffer->current_line = vl;
160 buffer->line_off++;
161 restore_cursor(buffer);
164 void
165 cmd_scroll_up(struct buffer *buffer)
167 size_t off;
169 off = body_lines-1;
171 for (; off > 0; --off)
172 cmd_scroll_line_up(buffer);
175 void
176 cmd_scroll_down(struct buffer *buffer)
178 size_t off;
180 off = body_lines-1;
182 for (; off > 0; --off)
183 cmd_scroll_line_down(buffer);
186 void
187 cmd_beginning_of_buffer(struct buffer *buffer)
189 buffer->current_line = TAILQ_FIRST(&buffer->head);
190 buffer->line_off = 0;
191 buffer->curs_y = 0;
192 buffer->cpoff = 0;
193 restore_cursor(buffer);
196 void
197 cmd_end_of_buffer(struct buffer *buffer)
199 ssize_t off;
201 off = buffer->line_max - body_lines;
202 off = MAX(0, off);
204 buffer->line_off = off;
205 buffer->curs_y = MIN((size_t)body_lines-1, buffer->line_max-1);
207 buffer->current_line = TAILQ_LAST(&buffer->head, vhead);
208 buffer->cpoff = body_cols;
209 restore_cursor(buffer);
212 void
213 cmd_kill_telescope(struct buffer *buffer)
215 save_session();
216 event_loopbreak();
219 void
220 cmd_push_button(struct buffer *buffer)
222 struct vline *vl;
224 vl = buffer->current_line;
225 if (vl->parent->type != LINE_LINK)
226 return;
228 load_url_in_tab(current_tab(), vl->parent->alt);
231 void
232 cmd_push_button_new_tab(struct buffer *buffer)
234 struct vline *vl;
236 vl = buffer->current_line;
237 if (vl->parent->type != LINE_LINK)
238 return;
240 new_tab(vl->parent->alt);
243 void
244 cmd_previous_button(struct buffer *buffer)
246 struct excursion place;
248 save_excursion(&place, buffer);
250 do {
251 if (buffer->current_line == NULL ||
252 buffer->current_line == TAILQ_FIRST(&buffer->head)) {
253 restore_excursion(&place, buffer);
254 message("No previous link");
255 return;
257 cmd_previous_line(buffer);
258 } while (buffer->current_line->parent->type != LINE_LINK);
261 void
262 cmd_next_button(struct buffer *buffer)
264 struct excursion place;
266 save_excursion(&place, buffer);
268 do {
269 if (buffer->current_line == NULL ||
270 buffer->current_line == TAILQ_LAST(&buffer->head, vhead)) {
271 restore_excursion(&place, buffer);
272 message("No next link");
273 return;
275 cmd_next_line(buffer);
276 } while (buffer->current_line->parent->type != LINE_LINK);
279 static inline int
280 is_heading(const struct line *l)
282 return l->type == LINE_TITLE_1 ||
283 l->type == LINE_TITLE_2 ||
284 l->type == LINE_TITLE_3;
287 void
288 cmd_previous_heading(struct buffer *buffer)
290 struct excursion place;
292 save_excursion(&place, buffer);
294 do {
295 if (buffer->current_line == NULL ||
296 buffer->current_line == TAILQ_FIRST(&buffer->head)) {
297 restore_excursion(&place, buffer);
298 message("No previous heading");
299 return;
301 cmd_previous_line(buffer);
302 } while (!is_heading(buffer->current_line->parent));
305 void
306 cmd_next_heading(struct buffer *buffer)
308 struct excursion place;
310 save_excursion(&place, buffer);
312 do {
313 if (buffer->current_line == NULL ||
314 buffer->current_line == TAILQ_LAST(&buffer->head, vhead)) {
315 restore_excursion(&place, buffer);
316 message("No next heading");
317 return;
319 cmd_next_line(buffer);
320 } while (!is_heading(buffer->current_line->parent));
323 void
324 cmd_previous_page(struct buffer *buffer)
326 struct tab *tab = current_tab();
328 if (!load_previous_page(tab))
329 message("No previous page");
330 else
331 start_loading_anim(tab);
334 void
335 cmd_next_page(struct buffer *buffer)
337 struct tab *tab = current_tab();
339 if (!load_next_page(tab))
340 message("No next page");
341 else
342 start_loading_anim(tab);
345 void
346 cmd_clear_minibuf(struct buffer *buffer)
348 message(NULL);
351 void
352 cmd_execute_extended_command(struct buffer *buffer)
354 size_t len;
356 if (in_minibuffer) {
357 message("We don't have enable-recursive-minibuffers");
358 return;
361 enter_minibuffer(eecmd_self_insert, eecmd_select, exit_minibuffer,
362 &eecmd_history);
364 len = sizeof(ministate.prompt);
365 strlcpy(ministate.prompt, "", len);
367 if (thiskey.meta)
368 strlcat(ministate.prompt, "M-", len);
370 strlcat(ministate.prompt, ui_keyname(thiskey.key), len);
372 if (thiskey.meta)
373 strlcat(ministate.prompt, " ", len);
376 void
377 cmd_tab_close(struct buffer *buffer)
379 struct tab *tab, *t;
381 tab = current_tab();
382 if (TAILQ_PREV(tab, tabshead, tabs) == NULL &&
383 TAILQ_NEXT(tab, tabs) == NULL) {
384 message("Can't close the only tab.");
385 return;
388 if (evtimer_pending(&tab->loadingev, NULL))
389 evtimer_del(&tab->loadingev);
391 stop_tab(tab);
393 if ((t = TAILQ_PREV(tab, tabshead, tabs)) == NULL)
394 t = TAILQ_NEXT(tab, tabs);
395 TAILQ_REMOVE(&tabshead, tab, tabs);
396 free(tab);
398 switch_to_tab(t);
401 void
402 cmd_tab_close_other(struct buffer *buffer)
404 struct tab *t, *i;
406 TAILQ_FOREACH_SAFE(t, &tabshead, tabs, i) {
407 if (t->flags & TAB_CURRENT)
408 continue;
410 stop_tab(t);
411 TAILQ_REMOVE(&tabshead, t, tabs);
412 free(t);
416 void
417 cmd_tab_new(struct buffer *buffer)
419 const char *url;
421 if ((url = new_tab_url) == NULL)
422 url = NEW_TAB_URL;
424 new_tab(url);
427 void
428 cmd_tab_next(struct buffer *buffer)
430 struct tab *tab, *t;
432 tab = current_tab();
433 tab->flags &= ~TAB_CURRENT;
435 if ((t = TAILQ_NEXT(tab, tabs)) == NULL)
436 t = TAILQ_FIRST(&tabshead);
437 t->flags |= TAB_CURRENT;
438 t->flags &= ~TAB_URGENT;
441 void
442 cmd_tab_previous(struct buffer *buffer)
444 struct tab *tab, *t;
446 tab = current_tab();
447 tab->flags &= ~TAB_CURRENT;
449 if ((t = TAILQ_PREV(tab, tabshead, tabs)) == NULL)
450 t = TAILQ_LAST(&tabshead, tabshead);
451 t->flags |= TAB_CURRENT;
452 t->flags &= ~TAB_URGENT;
455 void
456 cmd_tab_move(struct buffer *buffer)
458 struct tab *tab, *t;
460 tab = current_tab();
461 t = TAILQ_NEXT(tab, tabs);
462 TAILQ_REMOVE(&tabshead, tab, tabs);
464 if (t == NULL)
465 TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
466 else
467 TAILQ_INSERT_AFTER(&tabshead, t, tab, tabs);
470 void
471 cmd_tab_move_to(struct buffer *buffer)
473 struct tab *tab, *t;
475 tab = current_tab();
476 t = TAILQ_PREV(tab, tabshead, tabs);
477 TAILQ_REMOVE(&tabshead, tab, tabs);
479 if (t == NULL) {
480 if (TAILQ_EMPTY(&tabshead))
481 TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
482 else
483 TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
484 } else
485 TAILQ_INSERT_BEFORE(t, tab, tabs);
488 void
489 cmd_load_url(struct buffer *buffer)
491 if (in_minibuffer) {
492 message("We don't have enable-recursive-minibuffers");
493 return;
496 enter_minibuffer(lu_self_insert, lu_select, exit_minibuffer,
497 &lu_history);
498 strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
499 strlcpy(ministate.buf, "gemini://", sizeof(ministate.buf));
500 cmd_move_end_of_line(&ministate.buffer);
503 void
504 cmd_load_current_url(struct buffer *buffer)
506 struct tab *tab = current_tab();
508 if (in_minibuffer) {
509 message("We don't have enable-recursive-minibuffers");
510 return;
513 enter_minibuffer(lu_self_insert, lu_select, exit_minibuffer,
514 &lu_history);
515 strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
516 strlcpy(ministate.buf, tab->hist_cur->h, sizeof(ministate.buf));
517 ministate.buffer.cpoff = utf8_cplen(ministate.buf);
520 void
521 cmd_bookmark_page(struct buffer *buffer)
523 struct tab *tab = current_tab();
525 enter_minibuffer(lu_self_insert, bp_select, exit_minibuffer, NULL);
526 strlcpy(ministate.prompt, "Bookmark URL: ", sizeof(ministate.prompt));
527 strlcpy(ministate.buf, tab->hist_cur->h, sizeof(ministate.buf));
528 ministate.buffer.cpoff = utf8_cplen(ministate.buf);
531 void
532 cmd_list_bookmarks(struct buffer *buffer)
534 load_url_in_tab(current_tab(), "about:bookmarks");
537 void
538 cmd_toggle_help(struct buffer *buffer)
540 ui_toggle_side_window();
543 void
544 cmd_inc_fill_column(struct buffer *buffer)
546 if (fill_column == INT_MAX)
547 return;
549 fill_column += 2;
550 message("fill-column: %d", fill_column);
552 ui_schedule_redraw();
555 void
556 cmd_dec_fill_column(struct buffer *buffer)
558 if (fill_column == INT_MAX || fill_column < 8)
559 return;
561 fill_column -= 2;
562 message("fill-column: %d", fill_column);
564 ui_schedule_redraw();
567 void
568 cmd_olivetti_mode(struct buffer *buffer)
570 olivetti_mode = !olivetti_mode;
571 if (olivetti_mode)
572 message("olivetti-mode enabled");
573 else
574 message("olivetti-mode disabled");
576 ui_schedule_redraw();
579 void
580 cmd_mini_delete_char(struct buffer *buffer)
582 char *c, *n;
584 if (!in_minibuffer) {
585 message("text is read-only");
586 return;
589 minibuffer_taint_hist();
591 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
592 if (*c == '\0')
593 return;
594 n = utf8_next_cp(c);
596 memmove(c, n, strlen(n)+1);
599 void
600 cmd_mini_delete_backward_char(struct buffer *buffer)
602 char *c, *p, *start;
604 if (!in_minibuffer) {
605 message("text is read-only");
606 return;
609 minibuffer_taint_hist();
611 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
612 start = buffer->current_line->line;
613 if (c == start)
614 return;
615 p = utf8_prev_cp(c-1, start);
617 memmove(p, c, strlen(c)+1);
618 buffer->cpoff--;
621 void
622 cmd_mini_kill_line(struct buffer *buffer)
624 char *c;
626 if (!in_minibuffer) {
627 message("text is read-only");
628 return;
631 minibuffer_taint_hist();
632 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
633 *c = '\0';
636 void
637 cmd_mini_abort(struct buffer *buffer)
639 if (!in_minibuffer)
640 return;
642 ministate.abortfn();
645 void
646 cmd_mini_complete_and_exit(struct buffer *buffer)
648 if (!in_minibuffer)
649 return;
651 minibuffer_taint_hist();
652 ministate.donefn();
655 void
656 cmd_mini_previous_history_element(struct buffer *buffer)
658 if (ministate.history == NULL) {
659 message("No history");
660 return;
663 if (ministate.hist_cur == NULL ||
664 (ministate.hist_cur = TAILQ_PREV(ministate.hist_cur, mhisthead, entries)) == NULL) {
665 ministate.hist_cur = TAILQ_LAST(&ministate.history->head, mhisthead);
666 ministate.hist_off = ministate.history->len - 1;
667 if (ministate.hist_cur == NULL)
668 message("No prev item");
669 } else {
670 ministate.hist_off--;
673 if (ministate.hist_cur != NULL)
674 buffer->current_line->line = ministate.hist_cur->h;
677 void
678 cmd_mini_next_history_element(struct buffer *buffer)
680 if (ministate.history == NULL) {
681 message("No history");
682 return;
685 if (ministate.hist_cur == NULL ||
686 (ministate.hist_cur = TAILQ_NEXT(ministate.hist_cur, entries)) == NULL) {
687 ministate.hist_cur = TAILQ_FIRST(&ministate.history->head);
688 ministate.hist_off = 0;
689 if (ministate.hist_cur == NULL)
690 message("No next item");
691 } else {
692 ministate.hist_off++;
695 if (ministate.hist_cur != NULL)
696 buffer->current_line->line = ministate.hist_cur->h;