2 * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
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.
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.
21 #include "telescope.h"
23 /* return 1 if moved, 0 otherwise */
25 forward_line(struct buffer *buffer, int n)
30 if (buffer->current_line == NULL)
32 vl = buffer->current_line;
37 vl = TAILQ_NEXT(vl, vlines);
40 if (vl->parent->flags & L_HIDDEN)
42 buffer->current_line = vl;
45 vl = TAILQ_PREV(vl, vhead, vlines);
48 if (vl->parent->flags & L_HIDDEN)
50 if (buffer->current_line == buffer->top_line)
51 buffer->top_line = vl;
52 buffer->current_line = vl;
63 cmd_previous_line(struct buffer *buffer)
65 forward_line(buffer, -1);
69 cmd_next_line(struct buffer *buffer)
71 forward_line(buffer, +1);
75 cmd_backward_char(struct buffer *buffer)
77 if (buffer->cpoff != 0)
82 cmd_forward_char(struct buffer *buffer)
86 if (buffer->current_line->line != NULL)
87 len = utf8_cplen(buffer->current_line->line);
88 if (++buffer->cpoff > len)
93 cmd_backward_paragraph(struct buffer *buffer)
96 if (!forward_line(buffer, -1)) {
97 message("No previous paragraph");
100 } while (buffer->current_line->line != NULL ||
101 buffer->current_line->parent->type != LINE_TEXT);
105 cmd_forward_paragraph(struct buffer *buffer)
108 if (!forward_line(buffer, +1)) {
109 message("No next paragraph");
112 } while (buffer->current_line->line != NULL ||
113 buffer->current_line->parent->type != LINE_TEXT);
117 cmd_move_beginning_of_line(struct buffer *buffer)
123 cmd_move_end_of_line(struct buffer *buffer)
127 vl = buffer->current_line;
128 if (vl->line == NULL)
130 buffer->cpoff = utf8_cplen(vl->line);
134 cmd_redraw(struct buffer *buffer)
136 ui_schedule_redraw();
140 cmd_scroll_line_up(struct buffer *buffer)
144 if ((vl = TAILQ_PREV(buffer->top_line, vhead, vlines))
148 forward_line(buffer, -1);
151 buffer->top_line = vl;
155 cmd_scroll_line_down(struct buffer *buffer)
157 if (!forward_line(buffer, +1))
160 buffer->top_line = TAILQ_NEXT(buffer->top_line, vlines);
165 cmd_scroll_up(struct buffer *buffer)
167 forward_line(buffer, -1*body_lines);
171 cmd_scroll_down(struct buffer *buffer)
173 forward_line(buffer, body_lines);
177 cmd_beginning_of_buffer(struct buffer *buffer)
179 buffer->current_line = TAILQ_FIRST(&buffer->head);
181 buffer->top_line = buffer->current_line;
182 buffer->line_off = 0;
186 cmd_end_of_buffer(struct buffer *buffer)
188 buffer->current_line = TAILQ_LAST(&buffer->head, vhead);
189 buffer->cpoff = body_cols;
193 cmd_kill_telescope(struct buffer *buffer)
200 cmd_push_button(struct buffer *buffer)
205 vl = buffer->current_line;
206 switch (vl->parent->type) {
208 load_url_in_tab(current_tab(), vl->parent->alt);
211 l = TAILQ_NEXT(vl->parent, lines);
212 for (; l != NULL; l = TAILQ_NEXT(l, lines)) {
213 if (l->type == LINE_PRE_END)
215 l->flags ^= L_HIDDEN;
224 cmd_push_button_new_tab(struct buffer *buffer)
228 vl = buffer->current_line;
229 if (vl->parent->type != LINE_LINK)
232 new_tab(vl->parent->alt);
236 cmd_previous_button(struct buffer *buffer)
238 struct excursion place;
240 save_excursion(&place, buffer);
243 if (!forward_line(buffer, -1)) {
244 restore_excursion(&place, buffer);
245 message("No previous link");
248 } while (buffer->current_line->parent->type != LINE_LINK);
252 cmd_next_button(struct buffer *buffer)
254 struct excursion place;
256 save_excursion(&place, buffer);
259 if (!forward_line(buffer, +1)){
260 restore_excursion(&place, buffer);
261 message("No next link");
264 } while (buffer->current_line->parent->type != LINE_LINK);
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;
276 cmd_previous_heading(struct buffer *buffer)
278 struct excursion place;
280 save_excursion(&place, buffer);
283 if (!forward_line(buffer, -1)) {
284 restore_excursion(&place, buffer);
285 message("No previous heading");
288 } while (!is_heading(buffer->current_line->parent));
292 cmd_next_heading(struct buffer *buffer)
294 struct excursion place;
296 save_excursion(&place, buffer);
299 if (!forward_line(buffer, +1)) {
300 restore_excursion(&place, buffer);
301 message("No next heading");
304 } while (!is_heading(buffer->current_line->parent));
308 cmd_previous_page(struct buffer *buffer)
310 struct tab *tab = current_tab();
312 if (!load_previous_page(tab))
313 message("No previous page");
315 start_loading_anim(tab);
319 cmd_next_page(struct buffer *buffer)
321 struct tab *tab = current_tab();
323 if (!load_next_page(tab))
324 message("No next page");
326 start_loading_anim(tab);
330 cmd_clear_minibuf(struct buffer *buffer)
336 cmd_execute_extended_command(struct buffer *buffer)
341 message("We don't have enable-recursive-minibuffers");
345 enter_minibuffer(eecmd_self_insert, eecmd_select, exit_minibuffer,
348 len = sizeof(ministate.prompt);
349 strlcpy(ministate.prompt, "", len);
352 strlcat(ministate.prompt, "M-", len);
354 strlcat(ministate.prompt, ui_keyname(thiskey.key), len);
357 strlcat(ministate.prompt, " ", len);
361 cmd_tab_close(struct buffer *buffer)
366 if (TAILQ_PREV(tab, tabshead, tabs) == NULL &&
367 TAILQ_NEXT(tab, tabs) == NULL) {
368 message("Can't close the only tab.");
372 if (evtimer_pending(&tab->loadingev, NULL))
373 evtimer_del(&tab->loadingev);
377 if ((t = TAILQ_PREV(tab, tabshead, tabs)) == NULL)
378 t = TAILQ_NEXT(tab, tabs);
379 TAILQ_REMOVE(&tabshead, tab, tabs);
386 cmd_tab_close_other(struct buffer *buffer)
390 TAILQ_FOREACH_SAFE(t, &tabshead, tabs, i) {
391 if (t->flags & TAB_CURRENT)
395 TAILQ_REMOVE(&tabshead, t, tabs);
401 cmd_tab_new(struct buffer *buffer)
405 if ((url = new_tab_url) == NULL)
412 cmd_tab_next(struct buffer *buffer)
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;
426 cmd_tab_previous(struct buffer *buffer)
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;
440 cmd_tab_move(struct buffer *buffer)
445 t = TAILQ_NEXT(tab, tabs);
446 TAILQ_REMOVE(&tabshead, tab, tabs);
449 TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
451 TAILQ_INSERT_AFTER(&tabshead, t, tab, tabs);
455 cmd_tab_move_to(struct buffer *buffer)
460 t = TAILQ_PREV(tab, tabshead, tabs);
461 TAILQ_REMOVE(&tabshead, tab, tabs);
464 if (TAILQ_EMPTY(&tabshead))
465 TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
467 TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
469 TAILQ_INSERT_BEFORE(t, tab, tabs);
473 cmd_load_url(struct buffer *buffer)
476 message("We don't have enable-recursive-minibuffers");
480 enter_minibuffer(lu_self_insert, lu_select, exit_minibuffer,
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);
488 cmd_load_current_url(struct buffer *buffer)
490 struct tab *tab = current_tab();
493 message("We don't have enable-recursive-minibuffers");
497 enter_minibuffer(lu_self_insert, lu_select, exit_minibuffer,
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);
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);
516 cmd_list_bookmarks(struct buffer *buffer)
518 load_url_in_tab(current_tab(), "about:bookmarks");
522 cmd_toggle_help(struct buffer *buffer)
524 ui_toggle_side_window();
528 cmd_inc_fill_column(struct buffer *buffer)
530 if (fill_column == INT_MAX)
534 message("fill-column: %d", fill_column);
536 ui_schedule_redraw();
540 cmd_dec_fill_column(struct buffer *buffer)
542 if (fill_column == INT_MAX || fill_column < 8)
546 message("fill-column: %d", fill_column);
548 ui_schedule_redraw();
552 cmd_olivetti_mode(struct buffer *buffer)
554 olivetti_mode = !olivetti_mode;
556 message("olivetti-mode enabled");
558 message("olivetti-mode disabled");
560 ui_schedule_redraw();
564 cmd_mini_delete_char(struct buffer *buffer)
568 if (!in_minibuffer) {
569 message("text is read-only");
573 minibuffer_taint_hist();
575 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
580 memmove(c, n, strlen(n)+1);
584 cmd_mini_delete_backward_char(struct buffer *buffer)
588 if (!in_minibuffer) {
589 message("text is read-only");
593 minibuffer_taint_hist();
595 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
596 start = buffer->current_line->line;
599 p = utf8_prev_cp(c-1, start);
601 memmove(p, c, strlen(c)+1);
606 cmd_mini_kill_line(struct buffer *buffer)
610 if (!in_minibuffer) {
611 message("text is read-only");
615 minibuffer_taint_hist();
616 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
621 cmd_mini_abort(struct buffer *buffer)
630 cmd_mini_complete_and_exit(struct buffer *buffer)
635 minibuffer_taint_hist();
640 cmd_mini_previous_history_element(struct buffer *buffer)
642 if (ministate.history == NULL) {
643 message("No history");
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");
654 ministate.hist_off--;
657 if (ministate.hist_cur != NULL)
658 buffer->current_line->line = ministate.hist_cur->h;
662 cmd_mini_next_history_element(struct buffer *buffer)
664 if (ministate.history == NULL) {
665 message("No history");
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");
676 ministate.hist_off++;
679 if (ministate.hist_cur != NULL)
680 buffer->current_line->line = ministate.hist_cur->h;