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)
142 if (!forward_line(buffer, -1))
146 buffer->top_line = TAILQ_PREV(buffer->top_line, vhead, vlines);
150 cmd_scroll_line_down(struct buffer *buffer)
152 if (!forward_line(buffer, +1))
155 buffer->top_line = TAILQ_NEXT(buffer->top_line, vlines);
160 cmd_scroll_up(struct buffer *buffer)
162 forward_line(buffer, -1*body_lines);
166 cmd_scroll_down(struct buffer *buffer)
168 forward_line(buffer, body_lines);
172 cmd_beginning_of_buffer(struct buffer *buffer)
174 buffer->current_line = TAILQ_FIRST(&buffer->head);
176 buffer->top_line = buffer->current_line;
177 buffer->line_off = 0;
181 cmd_end_of_buffer(struct buffer *buffer)
183 buffer->current_line = TAILQ_LAST(&buffer->head, vhead);
184 buffer->cpoff = body_cols;
188 cmd_kill_telescope(struct buffer *buffer)
195 cmd_push_button(struct buffer *buffer)
200 vl = buffer->current_line;
201 switch (vl->parent->type) {
203 load_url_in_tab(current_tab(), vl->parent->alt);
206 l = TAILQ_NEXT(vl->parent, lines);
207 for (; l != NULL; l = TAILQ_NEXT(l, lines)) {
208 if (l->type == LINE_PRE_END)
210 l->flags ^= L_HIDDEN;
219 cmd_push_button_new_tab(struct buffer *buffer)
223 vl = buffer->current_line;
224 if (vl->parent->type != LINE_LINK)
227 new_tab(vl->parent->alt);
231 cmd_previous_button(struct buffer *buffer)
233 struct excursion place;
235 save_excursion(&place, buffer);
238 if (!forward_line(buffer, -1)) {
239 restore_excursion(&place, buffer);
240 message("No previous link");
243 } while (buffer->current_line->parent->type != LINE_LINK);
247 cmd_next_button(struct buffer *buffer)
249 struct excursion place;
251 save_excursion(&place, buffer);
254 if (!forward_line(buffer, +1)){
255 restore_excursion(&place, buffer);
256 message("No next link");
259 } while (buffer->current_line->parent->type != LINE_LINK);
263 is_heading(const struct line *l)
265 return l->type == LINE_TITLE_1 ||
266 l->type == LINE_TITLE_2 ||
267 l->type == LINE_TITLE_3;
271 cmd_previous_heading(struct buffer *buffer)
273 struct excursion place;
275 save_excursion(&place, buffer);
278 if (!forward_line(buffer, -1)) {
279 restore_excursion(&place, buffer);
280 message("No previous heading");
283 } while (!is_heading(buffer->current_line->parent));
287 cmd_next_heading(struct buffer *buffer)
289 struct excursion place;
291 save_excursion(&place, buffer);
294 if (!forward_line(buffer, +1)) {
295 restore_excursion(&place, buffer);
296 message("No next heading");
299 } while (!is_heading(buffer->current_line->parent));
303 cmd_previous_page(struct buffer *buffer)
305 struct tab *tab = current_tab();
307 if (!load_previous_page(tab))
308 message("No previous page");
310 start_loading_anim(tab);
314 cmd_next_page(struct buffer *buffer)
316 struct tab *tab = current_tab();
318 if (!load_next_page(tab))
319 message("No next page");
321 start_loading_anim(tab);
325 cmd_clear_minibuf(struct buffer *buffer)
331 cmd_execute_extended_command(struct buffer *buffer)
336 message("We don't have enable-recursive-minibuffers");
340 enter_minibuffer(eecmd_self_insert, eecmd_select, exit_minibuffer,
343 len = sizeof(ministate.prompt);
344 strlcpy(ministate.prompt, "", len);
347 strlcat(ministate.prompt, "M-", len);
349 strlcat(ministate.prompt, ui_keyname(thiskey.key), len);
352 strlcat(ministate.prompt, " ", len);
356 cmd_tab_close(struct buffer *buffer)
361 if (TAILQ_PREV(tab, tabshead, tabs) == NULL &&
362 TAILQ_NEXT(tab, tabs) == NULL) {
363 message("Can't close the only tab.");
367 if (evtimer_pending(&tab->loadingev, NULL))
368 evtimer_del(&tab->loadingev);
372 if ((t = TAILQ_PREV(tab, tabshead, tabs)) == NULL)
373 t = TAILQ_NEXT(tab, tabs);
374 TAILQ_REMOVE(&tabshead, tab, tabs);
381 cmd_tab_close_other(struct buffer *buffer)
385 TAILQ_FOREACH_SAFE(t, &tabshead, tabs, i) {
386 if (t->flags & TAB_CURRENT)
390 TAILQ_REMOVE(&tabshead, t, tabs);
396 cmd_tab_new(struct buffer *buffer)
400 if ((url = new_tab_url) == NULL)
407 cmd_tab_next(struct buffer *buffer)
412 tab->flags &= ~TAB_CURRENT;
414 if ((t = TAILQ_NEXT(tab, tabs)) == NULL)
415 t = TAILQ_FIRST(&tabshead);
416 t->flags |= TAB_CURRENT;
417 t->flags &= ~TAB_URGENT;
421 cmd_tab_previous(struct buffer *buffer)
426 tab->flags &= ~TAB_CURRENT;
428 if ((t = TAILQ_PREV(tab, tabshead, tabs)) == NULL)
429 t = TAILQ_LAST(&tabshead, tabshead);
430 t->flags |= TAB_CURRENT;
431 t->flags &= ~TAB_URGENT;
435 cmd_tab_move(struct buffer *buffer)
440 t = TAILQ_NEXT(tab, tabs);
441 TAILQ_REMOVE(&tabshead, tab, tabs);
444 TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
446 TAILQ_INSERT_AFTER(&tabshead, t, tab, tabs);
450 cmd_tab_move_to(struct buffer *buffer)
455 t = TAILQ_PREV(tab, tabshead, tabs);
456 TAILQ_REMOVE(&tabshead, tab, tabs);
459 if (TAILQ_EMPTY(&tabshead))
460 TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
462 TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
464 TAILQ_INSERT_BEFORE(t, tab, tabs);
468 cmd_load_url(struct buffer *buffer)
471 message("We don't have enable-recursive-minibuffers");
475 enter_minibuffer(lu_self_insert, lu_select, exit_minibuffer,
477 strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
478 strlcpy(ministate.buf, "gemini://", sizeof(ministate.buf));
479 cmd_move_end_of_line(&ministate.buffer);
483 cmd_load_current_url(struct buffer *buffer)
485 struct tab *tab = current_tab();
488 message("We don't have enable-recursive-minibuffers");
492 enter_minibuffer(lu_self_insert, lu_select, exit_minibuffer,
494 strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
495 strlcpy(ministate.buf, tab->hist_cur->h, sizeof(ministate.buf));
496 ministate.buffer.cpoff = utf8_cplen(ministate.buf);
500 cmd_bookmark_page(struct buffer *buffer)
502 struct tab *tab = current_tab();
504 enter_minibuffer(lu_self_insert, bp_select, exit_minibuffer, NULL);
505 strlcpy(ministate.prompt, "Bookmark URL: ", sizeof(ministate.prompt));
506 strlcpy(ministate.buf, tab->hist_cur->h, sizeof(ministate.buf));
507 ministate.buffer.cpoff = utf8_cplen(ministate.buf);
511 cmd_list_bookmarks(struct buffer *buffer)
513 load_url_in_tab(current_tab(), "about:bookmarks");
517 cmd_toggle_help(struct buffer *buffer)
519 ui_toggle_side_window();
523 cmd_inc_fill_column(struct buffer *buffer)
525 if (fill_column == INT_MAX)
529 message("fill-column: %d", fill_column);
531 ui_schedule_redraw();
535 cmd_dec_fill_column(struct buffer *buffer)
537 if (fill_column == INT_MAX || fill_column < 8)
541 message("fill-column: %d", fill_column);
543 ui_schedule_redraw();
547 cmd_olivetti_mode(struct buffer *buffer)
549 olivetti_mode = !olivetti_mode;
551 message("olivetti-mode enabled");
553 message("olivetti-mode disabled");
555 ui_schedule_redraw();
559 cmd_mini_delete_char(struct buffer *buffer)
563 if (!in_minibuffer) {
564 message("text is read-only");
568 minibuffer_taint_hist();
570 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
575 memmove(c, n, strlen(n)+1);
579 cmd_mini_delete_backward_char(struct buffer *buffer)
583 if (!in_minibuffer) {
584 message("text is read-only");
588 minibuffer_taint_hist();
590 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
591 start = buffer->current_line->line;
594 p = utf8_prev_cp(c-1, start);
596 memmove(p, c, strlen(c)+1);
601 cmd_mini_kill_line(struct buffer *buffer)
605 if (!in_minibuffer) {
606 message("text is read-only");
610 minibuffer_taint_hist();
611 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
616 cmd_mini_abort(struct buffer *buffer)
625 cmd_mini_complete_and_exit(struct buffer *buffer)
630 minibuffer_taint_hist();
635 cmd_mini_previous_history_element(struct buffer *buffer)
637 if (ministate.history == NULL) {
638 message("No history");
642 if (ministate.hist_cur == NULL ||
643 (ministate.hist_cur = TAILQ_PREV(ministate.hist_cur, mhisthead, entries)) == NULL) {
644 ministate.hist_cur = TAILQ_LAST(&ministate.history->head, mhisthead);
645 ministate.hist_off = ministate.history->len - 1;
646 if (ministate.hist_cur == NULL)
647 message("No prev item");
649 ministate.hist_off--;
652 if (ministate.hist_cur != NULL)
653 buffer->current_line->line = ministate.hist_cur->h;
657 cmd_mini_next_history_element(struct buffer *buffer)
659 if (ministate.history == NULL) {
660 message("No history");
664 if (ministate.hist_cur == NULL ||
665 (ministate.hist_cur = TAILQ_NEXT(ministate.hist_cur, entries)) == NULL) {
666 ministate.hist_cur = TAILQ_FIRST(&ministate.history->head);
667 ministate.hist_off = 0;
668 if (ministate.hist_cur == NULL)
669 message("No next item");
671 ministate.hist_off++;
674 if (ministate.hist_cur != NULL)
675 buffer->current_line->line = ministate.hist_cur->h;