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;
33 did = 0;
34 while (n != 0) {
35 if (n > 0) {
36 vl = TAILQ_NEXT(buffer->current_line, vlines);
37 if (vl == NULL)
38 return did;
39 buffer->current_line = vl;
40 n--;
41 } else {
42 vl = TAILQ_PREV(buffer->current_line, vhead, vlines);
43 if (vl == NULL)
44 return did;
45 if (buffer->current_line == buffer->top_line)
46 buffer->top_line = vl;
47 buffer->current_line = vl;
48 n++;
49 }
51 did = 1;
52 }
54 return did;
55 }
57 void
58 cmd_previous_line(struct buffer *buffer)
59 {
60 forward_line(buffer, -1);
61 }
63 void
64 cmd_next_line(struct buffer *buffer)
65 {
66 forward_line(buffer, +1);
67 }
69 void
70 cmd_backward_char(struct buffer *buffer)
71 {
72 if (buffer->cpoff != 0)
73 buffer->cpoff--;
74 }
76 void
77 cmd_forward_char(struct buffer *buffer)
78 {
79 size_t len = 0;
81 if (buffer->current_line->line != NULL)
82 len = utf8_cplen(buffer->current_line->line);
83 if (++buffer->cpoff > len)
84 buffer->cpoff = len;
85 }
87 void
88 cmd_backward_paragraph(struct buffer *buffer)
89 {
90 do {
91 if (!forward_line(buffer, -1)) {
92 message("No previous paragraph");
93 return;
94 }
95 } while (buffer->current_line->line != NULL ||
96 buffer->current_line->parent->type != LINE_TEXT);
97 }
99 void
100 cmd_forward_paragraph(struct buffer *buffer)
102 do {
103 if (!forward_line(buffer, +1)) {
104 message("No next paragraph");
105 return;
107 } while (buffer->current_line->line != NULL ||
108 buffer->current_line->parent->type != LINE_TEXT);
111 void
112 cmd_move_beginning_of_line(struct buffer *buffer)
114 buffer->cpoff = 0;
117 void
118 cmd_move_end_of_line(struct buffer *buffer)
120 struct vline *vl;
122 vl = buffer->current_line;
123 if (vl->line == NULL)
124 return;
125 buffer->cpoff = utf8_cplen(vl->line);
128 void
129 cmd_redraw(struct buffer *buffer)
131 ui_schedule_redraw();
134 void
135 cmd_scroll_line_up(struct buffer *buffer)
137 if (!forward_line(buffer, -1))
138 return;
140 buffer->line_off--;
141 buffer->top_line = TAILQ_PREV(buffer->top_line, vhead, vlines);
144 void
145 cmd_scroll_line_down(struct buffer *buffer)
147 if (!forward_line(buffer, +1))
148 return;
150 buffer->top_line = TAILQ_NEXT(buffer->top_line, vlines);
151 buffer->line_off++;
154 void
155 cmd_scroll_up(struct buffer *buffer)
157 forward_line(buffer, -1*(body_lines-1));
160 void
161 cmd_scroll_down(struct buffer *buffer)
163 forward_line(buffer, body_lines-1);
166 void
167 cmd_beginning_of_buffer(struct buffer *buffer)
169 buffer->current_line = TAILQ_FIRST(&buffer->head);
170 buffer->cpoff = 0;
171 buffer->top_line = buffer->current_line;
172 buffer->line_off = 0;
175 void
176 cmd_end_of_buffer(struct buffer *buffer)
178 buffer->current_line = TAILQ_LAST(&buffer->head, vhead);
179 buffer->cpoff = body_cols;
182 void
183 cmd_kill_telescope(struct buffer *buffer)
185 save_session();
186 event_loopbreak();
189 void
190 cmd_push_button(struct buffer *buffer)
192 struct vline *vl;
194 vl = buffer->current_line;
195 if (vl->parent->type != LINE_LINK)
196 return;
198 load_url_in_tab(current_tab(), vl->parent->alt);
201 void
202 cmd_push_button_new_tab(struct buffer *buffer)
204 struct vline *vl;
206 vl = buffer->current_line;
207 if (vl->parent->type != LINE_LINK)
208 return;
210 new_tab(vl->parent->alt);
213 void
214 cmd_previous_button(struct buffer *buffer)
216 struct excursion place;
218 save_excursion(&place, buffer);
220 do {
221 if (!forward_line(buffer, -1)) {
222 restore_excursion(&place, buffer);
223 message("No previous link");
224 return;
226 } while (buffer->current_line->parent->type != LINE_LINK);
229 void
230 cmd_next_button(struct buffer *buffer)
232 struct excursion place;
234 save_excursion(&place, buffer);
236 do {
237 if (!forward_line(buffer, +1)){
238 restore_excursion(&place, buffer);
239 message("No next link");
240 return;
242 } while (buffer->current_line->parent->type != LINE_LINK);
245 static inline int
246 is_heading(const struct line *l)
248 return l->type == LINE_TITLE_1 ||
249 l->type == LINE_TITLE_2 ||
250 l->type == LINE_TITLE_3;
253 void
254 cmd_previous_heading(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 previous heading");
264 return;
266 } while (!is_heading(buffer->current_line->parent));
269 void
270 cmd_next_heading(struct buffer *buffer)
272 struct excursion place;
274 save_excursion(&place, buffer);
276 do {
277 if (!forward_line(buffer, +1)) {
278 restore_excursion(&place, buffer);
279 message("No next heading");
280 return;
282 } while (!is_heading(buffer->current_line->parent));
285 void
286 cmd_previous_page(struct buffer *buffer)
288 struct tab *tab = current_tab();
290 if (!load_previous_page(tab))
291 message("No previous page");
292 else
293 start_loading_anim(tab);
296 void
297 cmd_next_page(struct buffer *buffer)
299 struct tab *tab = current_tab();
301 if (!load_next_page(tab))
302 message("No next page");
303 else
304 start_loading_anim(tab);
307 void
308 cmd_clear_minibuf(struct buffer *buffer)
310 message(NULL);
313 void
314 cmd_execute_extended_command(struct buffer *buffer)
316 size_t len;
318 if (in_minibuffer) {
319 message("We don't have enable-recursive-minibuffers");
320 return;
323 enter_minibuffer(eecmd_self_insert, eecmd_select, exit_minibuffer,
324 &eecmd_history);
326 len = sizeof(ministate.prompt);
327 strlcpy(ministate.prompt, "", len);
329 if (thiskey.meta)
330 strlcat(ministate.prompt, "M-", len);
332 strlcat(ministate.prompt, ui_keyname(thiskey.key), len);
334 if (thiskey.meta)
335 strlcat(ministate.prompt, " ", len);
338 void
339 cmd_tab_close(struct buffer *buffer)
341 struct tab *tab, *t;
343 tab = current_tab();
344 if (TAILQ_PREV(tab, tabshead, tabs) == NULL &&
345 TAILQ_NEXT(tab, tabs) == NULL) {
346 message("Can't close the only tab.");
347 return;
350 if (evtimer_pending(&tab->loadingev, NULL))
351 evtimer_del(&tab->loadingev);
353 stop_tab(tab);
355 if ((t = TAILQ_PREV(tab, tabshead, tabs)) == NULL)
356 t = TAILQ_NEXT(tab, tabs);
357 TAILQ_REMOVE(&tabshead, tab, tabs);
358 free(tab);
360 switch_to_tab(t);
363 void
364 cmd_tab_close_other(struct buffer *buffer)
366 struct tab *t, *i;
368 TAILQ_FOREACH_SAFE(t, &tabshead, tabs, i) {
369 if (t->flags & TAB_CURRENT)
370 continue;
372 stop_tab(t);
373 TAILQ_REMOVE(&tabshead, t, tabs);
374 free(t);
378 void
379 cmd_tab_new(struct buffer *buffer)
381 const char *url;
383 if ((url = new_tab_url) == NULL)
384 url = NEW_TAB_URL;
386 new_tab(url);
389 void
390 cmd_tab_next(struct buffer *buffer)
392 struct tab *tab, *t;
394 tab = current_tab();
395 tab->flags &= ~TAB_CURRENT;
397 if ((t = TAILQ_NEXT(tab, tabs)) == NULL)
398 t = TAILQ_FIRST(&tabshead);
399 t->flags |= TAB_CURRENT;
400 t->flags &= ~TAB_URGENT;
403 void
404 cmd_tab_previous(struct buffer *buffer)
406 struct tab *tab, *t;
408 tab = current_tab();
409 tab->flags &= ~TAB_CURRENT;
411 if ((t = TAILQ_PREV(tab, tabshead, tabs)) == NULL)
412 t = TAILQ_LAST(&tabshead, tabshead);
413 t->flags |= TAB_CURRENT;
414 t->flags &= ~TAB_URGENT;
417 void
418 cmd_tab_move(struct buffer *buffer)
420 struct tab *tab, *t;
422 tab = current_tab();
423 t = TAILQ_NEXT(tab, tabs);
424 TAILQ_REMOVE(&tabshead, tab, tabs);
426 if (t == NULL)
427 TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
428 else
429 TAILQ_INSERT_AFTER(&tabshead, t, tab, tabs);
432 void
433 cmd_tab_move_to(struct buffer *buffer)
435 struct tab *tab, *t;
437 tab = current_tab();
438 t = TAILQ_PREV(tab, tabshead, tabs);
439 TAILQ_REMOVE(&tabshead, tab, tabs);
441 if (t == NULL) {
442 if (TAILQ_EMPTY(&tabshead))
443 TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
444 else
445 TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
446 } else
447 TAILQ_INSERT_BEFORE(t, tab, tabs);
450 void
451 cmd_load_url(struct buffer *buffer)
453 if (in_minibuffer) {
454 message("We don't have enable-recursive-minibuffers");
455 return;
458 enter_minibuffer(lu_self_insert, lu_select, exit_minibuffer,
459 &lu_history);
460 strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
461 strlcpy(ministate.buf, "gemini://", sizeof(ministate.buf));
462 cmd_move_end_of_line(&ministate.buffer);
465 void
466 cmd_load_current_url(struct buffer *buffer)
468 struct tab *tab = current_tab();
470 if (in_minibuffer) {
471 message("We don't have enable-recursive-minibuffers");
472 return;
475 enter_minibuffer(lu_self_insert, lu_select, exit_minibuffer,
476 &lu_history);
477 strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
478 strlcpy(ministate.buf, tab->hist_cur->h, sizeof(ministate.buf));
479 ministate.buffer.cpoff = utf8_cplen(ministate.buf);
482 void
483 cmd_bookmark_page(struct buffer *buffer)
485 struct tab *tab = current_tab();
487 enter_minibuffer(lu_self_insert, bp_select, exit_minibuffer, NULL);
488 strlcpy(ministate.prompt, "Bookmark URL: ", sizeof(ministate.prompt));
489 strlcpy(ministate.buf, tab->hist_cur->h, sizeof(ministate.buf));
490 ministate.buffer.cpoff = utf8_cplen(ministate.buf);
493 void
494 cmd_list_bookmarks(struct buffer *buffer)
496 load_url_in_tab(current_tab(), "about:bookmarks");
499 void
500 cmd_toggle_help(struct buffer *buffer)
502 ui_toggle_side_window();
505 void
506 cmd_inc_fill_column(struct buffer *buffer)
508 if (fill_column == INT_MAX)
509 return;
511 fill_column += 2;
512 message("fill-column: %d", fill_column);
514 ui_schedule_redraw();
517 void
518 cmd_dec_fill_column(struct buffer *buffer)
520 if (fill_column == INT_MAX || fill_column < 8)
521 return;
523 fill_column -= 2;
524 message("fill-column: %d", fill_column);
526 ui_schedule_redraw();
529 void
530 cmd_olivetti_mode(struct buffer *buffer)
532 olivetti_mode = !olivetti_mode;
533 if (olivetti_mode)
534 message("olivetti-mode enabled");
535 else
536 message("olivetti-mode disabled");
538 ui_schedule_redraw();
541 void
542 cmd_mini_delete_char(struct buffer *buffer)
544 char *c, *n;
546 if (!in_minibuffer) {
547 message("text is read-only");
548 return;
551 minibuffer_taint_hist();
553 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
554 if (*c == '\0')
555 return;
556 n = utf8_next_cp(c);
558 memmove(c, n, strlen(n)+1);
561 void
562 cmd_mini_delete_backward_char(struct buffer *buffer)
564 char *c, *p, *start;
566 if (!in_minibuffer) {
567 message("text is read-only");
568 return;
571 minibuffer_taint_hist();
573 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
574 start = buffer->current_line->line;
575 if (c == start)
576 return;
577 p = utf8_prev_cp(c-1, start);
579 memmove(p, c, strlen(c)+1);
580 buffer->cpoff--;
583 void
584 cmd_mini_kill_line(struct buffer *buffer)
586 char *c;
588 if (!in_minibuffer) {
589 message("text is read-only");
590 return;
593 minibuffer_taint_hist();
594 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
595 *c = '\0';
598 void
599 cmd_mini_abort(struct buffer *buffer)
601 if (!in_minibuffer)
602 return;
604 ministate.abortfn();
607 void
608 cmd_mini_complete_and_exit(struct buffer *buffer)
610 if (!in_minibuffer)
611 return;
613 minibuffer_taint_hist();
614 ministate.donefn();
617 void
618 cmd_mini_previous_history_element(struct buffer *buffer)
620 if (ministate.history == NULL) {
621 message("No history");
622 return;
625 if (ministate.hist_cur == NULL ||
626 (ministate.hist_cur = TAILQ_PREV(ministate.hist_cur, mhisthead, entries)) == NULL) {
627 ministate.hist_cur = TAILQ_LAST(&ministate.history->head, mhisthead);
628 ministate.hist_off = ministate.history->len - 1;
629 if (ministate.hist_cur == NULL)
630 message("No prev item");
631 } else {
632 ministate.hist_off--;
635 if (ministate.hist_cur != NULL)
636 buffer->current_line->line = ministate.hist_cur->h;
639 void
640 cmd_mini_next_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_NEXT(ministate.hist_cur, entries)) == NULL) {
649 ministate.hist_cur = TAILQ_FIRST(&ministate.history->head);
650 ministate.hist_off = 0;
651 if (ministate.hist_cur == NULL)
652 message("No next item");
653 } else {
654 ministate.hist_off++;
657 if (ministate.hist_cur != NULL)
658 buffer->current_line->line = ministate.hist_cur->h;