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 "defaults.h"
22 #include "minibuffer.h"
23 #include "telescope.h"
24 #include "ui.h"
25 #include "utf8.h"
27 /* return 1 if moved, 0 otherwise */
28 static inline int
29 forward_line(struct buffer *buffer, int n)
30 {
31 struct vline *vl;
32 int did;
34 if (buffer->current_line == NULL)
35 return 0;
36 vl = buffer->current_line;
38 did = 0;
39 while (n != 0) {
40 if (n > 0) {
41 vl = TAILQ_NEXT(vl, vlines);
42 if (vl == NULL)
43 return did;
44 if (vl->parent->flags & L_HIDDEN)
45 continue;
46 buffer->current_line = vl;
47 n--;
48 } else {
49 vl = TAILQ_PREV(vl, vhead, vlines);
50 if (vl == NULL)
51 return did;
52 if (vl->parent->flags & L_HIDDEN)
53 continue;
54 if (buffer->current_line == buffer->top_line)
55 buffer->top_line = vl;
56 buffer->current_line = vl;
57 n++;
58 }
60 did = 1;
61 }
63 return did;
64 }
66 void
67 cmd_previous_line(struct buffer *buffer)
68 {
69 forward_line(buffer, -1);
70 }
72 void
73 cmd_next_line(struct buffer *buffer)
74 {
75 forward_line(buffer, +1);
76 }
78 void
79 cmd_backward_char(struct buffer *buffer)
80 {
81 if (buffer->cpoff != 0)
82 buffer->cpoff--;
83 }
85 void
86 cmd_forward_char(struct buffer *buffer)
87 {
88 size_t len = 0;
90 if (buffer->current_line->line != NULL)
91 len = utf8_cplen(buffer->current_line->line);
92 if (++buffer->cpoff > len)
93 buffer->cpoff = len;
94 }
96 void
97 cmd_backward_paragraph(struct buffer *buffer)
98 {
99 do {
100 if (!forward_line(buffer, -1)) {
101 message("No previous paragraph");
102 return;
104 } while (buffer->current_line->line != NULL ||
105 buffer->current_line->parent->type != LINE_TEXT);
108 void
109 cmd_forward_paragraph(struct buffer *buffer)
111 do {
112 if (!forward_line(buffer, +1)) {
113 message("No next paragraph");
114 return;
116 } while (buffer->current_line->line != NULL ||
117 buffer->current_line->parent->type != LINE_TEXT);
120 void
121 cmd_move_beginning_of_line(struct buffer *buffer)
123 buffer->cpoff = 0;
126 void
127 cmd_move_end_of_line(struct buffer *buffer)
129 struct vline *vl;
131 vl = buffer->current_line;
132 if (vl->line == NULL)
133 return;
134 buffer->cpoff = utf8_cplen(vl->line);
137 void
138 cmd_redraw(struct buffer *buffer)
140 ui_schedule_redraw();
143 void
144 cmd_scroll_line_up(struct buffer *buffer)
146 struct vline *vl;
148 if ((vl = TAILQ_PREV(buffer->top_line, vhead, vlines))
149 == NULL)
150 return;
152 forward_line(buffer, -1);
154 buffer->line_off--;
155 buffer->top_line = vl;
158 void
159 cmd_scroll_line_down(struct buffer *buffer)
161 if (!forward_line(buffer, +1))
162 return;
164 buffer->top_line = TAILQ_NEXT(buffer->top_line, vlines);
165 buffer->line_off++;
168 void
169 cmd_scroll_up(struct buffer *buffer)
171 forward_line(buffer, -1*body_lines);
174 void
175 cmd_scroll_down(struct buffer *buffer)
177 forward_line(buffer, body_lines);
180 void
181 cmd_beginning_of_buffer(struct buffer *buffer)
183 buffer->current_line = TAILQ_FIRST(&buffer->head);
184 buffer->cpoff = 0;
185 buffer->top_line = buffer->current_line;
186 buffer->line_off = 0;
189 void
190 cmd_end_of_buffer(struct buffer *buffer)
192 buffer->current_line = TAILQ_LAST(&buffer->head, vhead);
193 buffer->cpoff = body_cols;
196 void
197 cmd_kill_telescope(struct buffer *buffer)
199 save_session();
200 event_loopbreak();
203 void
204 cmd_push_button(struct buffer *buffer)
206 struct vline *vl;
207 struct line *l;
209 vl = buffer->current_line;
210 switch (vl->parent->type) {
211 case LINE_LINK:
212 load_url_in_tab(current_tab(), vl->parent->alt);
213 break;
214 case LINE_PRE_START:
215 l = TAILQ_NEXT(vl->parent, lines);
216 for (; l != NULL; l = TAILQ_NEXT(l, lines)) {
217 if (l->type == LINE_PRE_END)
218 break;
219 l->flags ^= L_HIDDEN;
221 break;
222 default:
223 break;
227 void
228 cmd_push_button_new_tab(struct buffer *buffer)
230 struct vline *vl;
232 vl = buffer->current_line;
233 if (vl->parent->type != LINE_LINK)
234 return;
236 new_tab(vl->parent->alt);
239 void
240 cmd_previous_button(struct buffer *buffer)
242 struct excursion place;
244 save_excursion(&place, buffer);
246 do {
247 if (!forward_line(buffer, -1)) {
248 restore_excursion(&place, buffer);
249 message("No previous link");
250 return;
252 } while (buffer->current_line->parent->type != LINE_LINK);
255 void
256 cmd_next_button(struct buffer *buffer)
258 struct excursion place;
260 save_excursion(&place, buffer);
262 do {
263 if (!forward_line(buffer, +1)){
264 restore_excursion(&place, buffer);
265 message("No next link");
266 return;
268 } while (buffer->current_line->parent->type != LINE_LINK);
271 static inline int
272 is_heading(const struct line *l)
274 return l->type == LINE_TITLE_1 ||
275 l->type == LINE_TITLE_2 ||
276 l->type == LINE_TITLE_3;
279 void
280 cmd_previous_heading(struct buffer *buffer)
282 struct excursion place;
284 save_excursion(&place, buffer);
286 do {
287 if (!forward_line(buffer, -1)) {
288 restore_excursion(&place, buffer);
289 message("No previous heading");
290 return;
292 } while (!is_heading(buffer->current_line->parent));
295 void
296 cmd_next_heading(struct buffer *buffer)
298 struct excursion place;
300 save_excursion(&place, buffer);
302 do {
303 if (!forward_line(buffer, +1)) {
304 restore_excursion(&place, buffer);
305 message("No next heading");
306 return;
308 } while (!is_heading(buffer->current_line->parent));
311 void
312 cmd_previous_page(struct buffer *buffer)
314 struct tab *tab = current_tab();
316 if (!load_previous_page(tab))
317 message("No previous page");
318 else
319 start_loading_anim(tab);
322 void
323 cmd_next_page(struct buffer *buffer)
325 struct tab *tab = current_tab();
327 if (!load_next_page(tab))
328 message("No next page");
329 else
330 start_loading_anim(tab);
333 void
334 cmd_clear_minibuf(struct buffer *buffer)
336 message(NULL);
339 void
340 cmd_execute_extended_command(struct buffer *buffer)
342 size_t len;
344 if (in_minibuffer) {
345 message("We don't have enable-recursive-minibuffers");
346 return;
349 enter_minibuffer(eecmd_self_insert, eecmd_select, exit_minibuffer,
350 &eecmd_history, NULL, NULL);
352 len = sizeof(ministate.prompt);
353 strlcpy(ministate.prompt, "", len);
355 if (thiskey.meta)
356 strlcat(ministate.prompt, "M-", len);
358 strlcat(ministate.prompt, ui_keyname(thiskey.key), len);
360 if (thiskey.meta)
361 strlcat(ministate.prompt, " ", len);
364 void
365 cmd_tab_close(struct buffer *buffer)
367 struct tab *tab, *t;
369 tab = current_tab();
370 if (TAILQ_PREV(tab, tabshead, tabs) == NULL &&
371 TAILQ_NEXT(tab, tabs) == NULL) {
372 message("Can't close the only tab.");
373 return;
376 if (evtimer_pending(&tab->loadingev, NULL))
377 evtimer_del(&tab->loadingev);
379 stop_tab(tab);
381 if ((t = TAILQ_PREV(tab, tabshead, tabs)) == NULL)
382 t = TAILQ_NEXT(tab, tabs);
383 TAILQ_REMOVE(&tabshead, tab, tabs);
384 free(tab);
386 switch_to_tab(t);
389 void
390 cmd_tab_close_other(struct buffer *buffer)
392 struct tab *t, *i;
394 TAILQ_FOREACH_SAFE(t, &tabshead, tabs, i) {
395 if (t->flags & TAB_CURRENT)
396 continue;
398 stop_tab(t);
399 TAILQ_REMOVE(&tabshead, t, tabs);
400 free(t);
404 void
405 cmd_tab_new(struct buffer *buffer)
407 const char *url;
409 if ((url = new_tab_url) == NULL)
410 url = NEW_TAB_URL;
412 new_tab(url);
415 void
416 cmd_tab_next(struct buffer *buffer)
418 struct tab *tab, *t;
420 tab = current_tab();
421 tab->flags &= ~TAB_CURRENT;
423 if ((t = TAILQ_NEXT(tab, tabs)) == NULL)
424 t = TAILQ_FIRST(&tabshead);
425 t->flags |= TAB_CURRENT;
426 t->flags &= ~TAB_URGENT;
429 void
430 cmd_tab_previous(struct buffer *buffer)
432 struct tab *tab, *t;
434 tab = current_tab();
435 tab->flags &= ~TAB_CURRENT;
437 if ((t = TAILQ_PREV(tab, tabshead, tabs)) == NULL)
438 t = TAILQ_LAST(&tabshead, tabshead);
439 t->flags |= TAB_CURRENT;
440 t->flags &= ~TAB_URGENT;
443 void
444 cmd_tab_move(struct buffer *buffer)
446 struct tab *tab, *t;
448 tab = current_tab();
449 t = TAILQ_NEXT(tab, tabs);
450 TAILQ_REMOVE(&tabshead, tab, tabs);
452 if (t == NULL)
453 TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
454 else
455 TAILQ_INSERT_AFTER(&tabshead, t, tab, tabs);
458 void
459 cmd_tab_move_to(struct buffer *buffer)
461 struct tab *tab, *t;
463 tab = current_tab();
464 t = TAILQ_PREV(tab, tabshead, tabs);
465 TAILQ_REMOVE(&tabshead, tab, tabs);
467 if (t == NULL) {
468 if (TAILQ_EMPTY(&tabshead))
469 TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
470 else
471 TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
472 } else
473 TAILQ_INSERT_BEFORE(t, tab, tabs);
476 void
477 cmd_load_url(struct buffer *buffer)
479 if (in_minibuffer) {
480 message("We don't have enable-recursive-minibuffers");
481 return;
484 enter_minibuffer(lu_self_insert, lu_select, exit_minibuffer,
485 &lu_history, NULL, NULL);
486 strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
487 strlcpy(ministate.buf, "gemini://", sizeof(ministate.buf));
488 cmd_move_end_of_line(&ministate.buffer);
491 void
492 cmd_load_current_url(struct buffer *buffer)
494 struct tab *tab = current_tab();
496 if (in_minibuffer) {
497 message("We don't have enable-recursive-minibuffers");
498 return;
501 enter_minibuffer(lu_self_insert, lu_select, exit_minibuffer,
502 &lu_history, NULL, NULL);
503 strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
504 strlcpy(ministate.buf, tab->hist_cur->h, sizeof(ministate.buf));
505 ministate.buffer.cpoff = utf8_cplen(ministate.buf);
508 void
509 cmd_bookmark_page(struct buffer *buffer)
511 struct tab *tab = current_tab();
513 enter_minibuffer(lu_self_insert, bp_select, exit_minibuffer, NULL,
514 NULL, NULL);
515 strlcpy(ministate.prompt, "Bookmark 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_list_bookmarks(struct buffer *buffer)
523 load_url_in_tab(current_tab(), "about:bookmarks");
526 void
527 cmd_toggle_help(struct buffer *buffer)
529 ui_toggle_side_window();
532 void
533 cmd_inc_fill_column(struct buffer *buffer)
535 if (fill_column == INT_MAX)
536 return;
538 fill_column += 2;
539 message("fill-column: %d", fill_column);
541 ui_schedule_redraw();
544 void
545 cmd_dec_fill_column(struct buffer *buffer)
547 if (fill_column == INT_MAX || fill_column < 8)
548 return;
550 fill_column -= 2;
551 message("fill-column: %d", fill_column);
553 ui_schedule_redraw();
556 void
557 cmd_olivetti_mode(struct buffer *buffer)
559 olivetti_mode = !olivetti_mode;
560 if (olivetti_mode)
561 message("olivetti-mode enabled");
562 else
563 message("olivetti-mode disabled");
565 ui_schedule_redraw();
568 void
569 cmd_mini_delete_char(struct buffer *buffer)
571 char *c, *n;
573 if (!in_minibuffer) {
574 message("text is read-only");
575 return;
578 minibuffer_taint_hist();
580 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
581 if (*c == '\0')
582 return;
583 n = utf8_next_cp(c);
585 memmove(c, n, strlen(n)+1);
588 void
589 cmd_mini_delete_backward_char(struct buffer *buffer)
591 char *c, *p, *start;
593 if (!in_minibuffer) {
594 message("text is read-only");
595 return;
598 minibuffer_taint_hist();
600 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
601 start = buffer->current_line->line;
602 if (c == start)
603 return;
604 p = utf8_prev_cp(c-1, start);
606 memmove(p, c, strlen(c)+1);
607 buffer->cpoff--;
610 void
611 cmd_mini_kill_line(struct buffer *buffer)
613 char *c;
615 if (!in_minibuffer) {
616 message("text is read-only");
617 return;
620 minibuffer_taint_hist();
621 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
622 *c = '\0';
625 void
626 cmd_mini_abort(struct buffer *buffer)
628 if (!in_minibuffer)
629 return;
631 ministate.abortfn();
634 void
635 cmd_mini_complete_and_exit(struct buffer *buffer)
637 if (!in_minibuffer)
638 return;
640 minibuffer_taint_hist();
641 ministate.donefn();
644 void
645 cmd_mini_previous_history_element(struct buffer *buffer)
647 if (ministate.history == NULL) {
648 message("No history");
649 return;
652 if (ministate.hist_cur == NULL ||
653 (ministate.hist_cur = TAILQ_PREV(ministate.hist_cur, mhisthead, entries)) == NULL) {
654 ministate.hist_cur = TAILQ_LAST(&ministate.history->head, mhisthead);
655 ministate.hist_off = ministate.history->len - 1;
656 if (ministate.hist_cur == NULL)
657 message("No prev item");
658 } else {
659 ministate.hist_off--;
662 if (ministate.hist_cur != NULL)
663 buffer->current_line->line = ministate.hist_cur->h;
666 void
667 cmd_mini_next_history_element(struct buffer *buffer)
669 if (ministate.history == NULL) {
670 message("No history");
671 return;
674 if (ministate.hist_cur == NULL ||
675 (ministate.hist_cur = TAILQ_NEXT(ministate.hist_cur, entries)) == NULL) {
676 ministate.hist_cur = TAILQ_FIRST(&ministate.history->head);
677 ministate.hist_off = 0;
678 if (ministate.hist_cur == NULL)
679 message("No next item");
680 } else {
681 ministate.hist_off++;
684 if (ministate.hist_cur != NULL)
685 buffer->current_line->line = ministate.hist_cur->h;