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 "minibuffer.h"
22 #include "telescope.h"
23 #include "ui.h"
24 #include "utf8.h"
26 /* return 1 if moved, 0 otherwise */
27 static inline int
28 forward_line(struct buffer *buffer, int n)
29 {
30 struct vline *vl;
31 int did;
33 if (buffer->current_line == NULL)
34 return 0;
35 vl = buffer->current_line;
37 did = 0;
38 while (n != 0) {
39 if (n > 0) {
40 vl = TAILQ_NEXT(vl, vlines);
41 if (vl == NULL)
42 return did;
43 if (vl->parent->flags & L_HIDDEN)
44 continue;
45 buffer->current_line = vl;
46 n--;
47 } else {
48 vl = TAILQ_PREV(vl, vhead, vlines);
49 if (vl == NULL)
50 return did;
51 if (vl->parent->flags & L_HIDDEN)
52 continue;
53 if (buffer->current_line == buffer->top_line)
54 buffer->top_line = vl;
55 buffer->current_line = vl;
56 n++;
57 }
59 did = 1;
60 }
62 return did;
63 }
65 void
66 cmd_previous_line(struct buffer *buffer)
67 {
68 forward_line(buffer, -1);
69 }
71 void
72 cmd_next_line(struct buffer *buffer)
73 {
74 forward_line(buffer, +1);
75 }
77 void
78 cmd_backward_char(struct buffer *buffer)
79 {
80 if (buffer->cpoff != 0)
81 buffer->cpoff--;
82 }
84 void
85 cmd_forward_char(struct buffer *buffer)
86 {
87 size_t len = 0;
89 if (buffer->current_line->line != NULL)
90 len = utf8_cplen(buffer->current_line->line);
91 if (++buffer->cpoff > len)
92 buffer->cpoff = len;
93 }
95 void
96 cmd_backward_paragraph(struct buffer *buffer)
97 {
98 do {
99 if (!forward_line(buffer, -1)) {
100 message("No previous paragraph");
101 return;
103 } while (buffer->current_line->line != NULL ||
104 buffer->current_line->parent->type != LINE_TEXT);
107 void
108 cmd_forward_paragraph(struct buffer *buffer)
110 do {
111 if (!forward_line(buffer, +1)) {
112 message("No next paragraph");
113 return;
115 } while (buffer->current_line->line != NULL ||
116 buffer->current_line->parent->type != LINE_TEXT);
119 void
120 cmd_move_beginning_of_line(struct buffer *buffer)
122 buffer->cpoff = 0;
125 void
126 cmd_move_end_of_line(struct buffer *buffer)
128 struct vline *vl;
130 vl = buffer->current_line;
131 if (vl->line == NULL)
132 return;
133 buffer->cpoff = utf8_cplen(vl->line);
136 void
137 cmd_redraw(struct buffer *buffer)
139 ui_schedule_redraw();
142 void
143 cmd_scroll_line_up(struct buffer *buffer)
145 struct vline *vl;
147 if ((vl = TAILQ_PREV(buffer->top_line, vhead, vlines))
148 == NULL)
149 return;
151 forward_line(buffer, -1);
153 buffer->line_off--;
154 buffer->top_line = vl;
157 void
158 cmd_scroll_line_down(struct buffer *buffer)
160 if (!forward_line(buffer, +1))
161 return;
163 buffer->top_line = TAILQ_NEXT(buffer->top_line, vlines);
164 buffer->line_off++;
167 void
168 cmd_scroll_up(struct buffer *buffer)
170 forward_line(buffer, -1*body_lines);
173 void
174 cmd_scroll_down(struct buffer *buffer)
176 forward_line(buffer, body_lines);
179 void
180 cmd_beginning_of_buffer(struct buffer *buffer)
182 buffer->current_line = TAILQ_FIRST(&buffer->head);
183 buffer->cpoff = 0;
184 buffer->top_line = buffer->current_line;
185 buffer->line_off = 0;
188 void
189 cmd_end_of_buffer(struct buffer *buffer)
191 buffer->current_line = TAILQ_LAST(&buffer->head, vhead);
192 buffer->cpoff = body_cols;
195 void
196 cmd_kill_telescope(struct buffer *buffer)
198 save_session();
199 event_loopbreak();
202 void
203 cmd_push_button(struct buffer *buffer)
205 struct vline *vl;
206 struct line *l;
208 vl = buffer->current_line;
209 switch (vl->parent->type) {
210 case LINE_LINK:
211 load_url_in_tab(current_tab(), vl->parent->alt);
212 break;
213 case LINE_PRE_START:
214 l = TAILQ_NEXT(vl->parent, lines);
215 for (; l != NULL; l = TAILQ_NEXT(l, lines)) {
216 if (l->type == LINE_PRE_END)
217 break;
218 l->flags ^= L_HIDDEN;
220 break;
221 default:
222 break;
226 void
227 cmd_push_button_new_tab(struct buffer *buffer)
229 struct vline *vl;
231 vl = buffer->current_line;
232 if (vl->parent->type != LINE_LINK)
233 return;
235 new_tab(vl->parent->alt);
238 void
239 cmd_previous_button(struct buffer *buffer)
241 struct excursion place;
243 save_excursion(&place, buffer);
245 do {
246 if (!forward_line(buffer, -1)) {
247 restore_excursion(&place, buffer);
248 message("No previous link");
249 return;
251 } while (buffer->current_line->parent->type != LINE_LINK);
254 void
255 cmd_next_button(struct buffer *buffer)
257 struct excursion place;
259 save_excursion(&place, buffer);
261 do {
262 if (!forward_line(buffer, +1)){
263 restore_excursion(&place, buffer);
264 message("No next link");
265 return;
267 } while (buffer->current_line->parent->type != LINE_LINK);
270 static inline int
271 is_heading(const struct line *l)
273 return l->type == LINE_TITLE_1 ||
274 l->type == LINE_TITLE_2 ||
275 l->type == LINE_TITLE_3;
278 void
279 cmd_previous_heading(struct buffer *buffer)
281 struct excursion place;
283 save_excursion(&place, buffer);
285 do {
286 if (!forward_line(buffer, -1)) {
287 restore_excursion(&place, buffer);
288 message("No previous heading");
289 return;
291 } while (!is_heading(buffer->current_line->parent));
294 void
295 cmd_next_heading(struct buffer *buffer)
297 struct excursion place;
299 save_excursion(&place, buffer);
301 do {
302 if (!forward_line(buffer, +1)) {
303 restore_excursion(&place, buffer);
304 message("No next heading");
305 return;
307 } while (!is_heading(buffer->current_line->parent));
310 void
311 cmd_previous_page(struct buffer *buffer)
313 struct tab *tab = current_tab();
315 if (!load_previous_page(tab))
316 message("No previous page");
317 else
318 start_loading_anim(tab);
321 void
322 cmd_next_page(struct buffer *buffer)
324 struct tab *tab = current_tab();
326 if (!load_next_page(tab))
327 message("No next page");
328 else
329 start_loading_anim(tab);
332 void
333 cmd_clear_minibuf(struct buffer *buffer)
335 message(NULL);
338 void
339 cmd_execute_extended_command(struct buffer *buffer)
341 size_t len;
343 if (in_minibuffer) {
344 message("We don't have enable-recursive-minibuffers");
345 return;
348 enter_minibuffer(eecmd_self_insert, eecmd_select, exit_minibuffer,
349 &eecmd_history);
351 len = sizeof(ministate.prompt);
352 strlcpy(ministate.prompt, "", len);
354 if (thiskey.meta)
355 strlcat(ministate.prompt, "M-", len);
357 strlcat(ministate.prompt, ui_keyname(thiskey.key), len);
359 if (thiskey.meta)
360 strlcat(ministate.prompt, " ", len);
363 void
364 cmd_tab_close(struct buffer *buffer)
366 struct tab *tab, *t;
368 tab = current_tab();
369 if (TAILQ_PREV(tab, tabshead, tabs) == NULL &&
370 TAILQ_NEXT(tab, tabs) == NULL) {
371 message("Can't close the only tab.");
372 return;
375 if (evtimer_pending(&tab->loadingev, NULL))
376 evtimer_del(&tab->loadingev);
378 stop_tab(tab);
380 if ((t = TAILQ_PREV(tab, tabshead, tabs)) == NULL)
381 t = TAILQ_NEXT(tab, tabs);
382 TAILQ_REMOVE(&tabshead, tab, tabs);
383 free(tab);
385 switch_to_tab(t);
388 void
389 cmd_tab_close_other(struct buffer *buffer)
391 struct tab *t, *i;
393 TAILQ_FOREACH_SAFE(t, &tabshead, tabs, i) {
394 if (t->flags & TAB_CURRENT)
395 continue;
397 stop_tab(t);
398 TAILQ_REMOVE(&tabshead, t, tabs);
399 free(t);
403 void
404 cmd_tab_new(struct buffer *buffer)
406 const char *url;
408 if ((url = new_tab_url) == NULL)
409 url = NEW_TAB_URL;
411 new_tab(url);
414 void
415 cmd_tab_next(struct buffer *buffer)
417 struct tab *tab, *t;
419 tab = current_tab();
420 tab->flags &= ~TAB_CURRENT;
422 if ((t = TAILQ_NEXT(tab, tabs)) == NULL)
423 t = TAILQ_FIRST(&tabshead);
424 t->flags |= TAB_CURRENT;
425 t->flags &= ~TAB_URGENT;
428 void
429 cmd_tab_previous(struct buffer *buffer)
431 struct tab *tab, *t;
433 tab = current_tab();
434 tab->flags &= ~TAB_CURRENT;
436 if ((t = TAILQ_PREV(tab, tabshead, tabs)) == NULL)
437 t = TAILQ_LAST(&tabshead, tabshead);
438 t->flags |= TAB_CURRENT;
439 t->flags &= ~TAB_URGENT;
442 void
443 cmd_tab_move(struct buffer *buffer)
445 struct tab *tab, *t;
447 tab = current_tab();
448 t = TAILQ_NEXT(tab, tabs);
449 TAILQ_REMOVE(&tabshead, tab, tabs);
451 if (t == NULL)
452 TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
453 else
454 TAILQ_INSERT_AFTER(&tabshead, t, tab, tabs);
457 void
458 cmd_tab_move_to(struct buffer *buffer)
460 struct tab *tab, *t;
462 tab = current_tab();
463 t = TAILQ_PREV(tab, tabshead, tabs);
464 TAILQ_REMOVE(&tabshead, tab, tabs);
466 if (t == NULL) {
467 if (TAILQ_EMPTY(&tabshead))
468 TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
469 else
470 TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
471 } else
472 TAILQ_INSERT_BEFORE(t, tab, tabs);
475 void
476 cmd_load_url(struct buffer *buffer)
478 if (in_minibuffer) {
479 message("We don't have enable-recursive-minibuffers");
480 return;
483 enter_minibuffer(lu_self_insert, lu_select, exit_minibuffer,
484 &lu_history);
485 strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
486 strlcpy(ministate.buf, "gemini://", sizeof(ministate.buf));
487 cmd_move_end_of_line(&ministate.buffer);
490 void
491 cmd_load_current_url(struct buffer *buffer)
493 struct tab *tab = current_tab();
495 if (in_minibuffer) {
496 message("We don't have enable-recursive-minibuffers");
497 return;
500 enter_minibuffer(lu_self_insert, lu_select, exit_minibuffer,
501 &lu_history);
502 strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
503 strlcpy(ministate.buf, tab->hist_cur->h, sizeof(ministate.buf));
504 ministate.buffer.cpoff = utf8_cplen(ministate.buf);
507 void
508 cmd_bookmark_page(struct buffer *buffer)
510 struct tab *tab = current_tab();
512 enter_minibuffer(lu_self_insert, bp_select, exit_minibuffer, NULL);
513 strlcpy(ministate.prompt, "Bookmark URL: ", sizeof(ministate.prompt));
514 strlcpy(ministate.buf, tab->hist_cur->h, sizeof(ministate.buf));
515 ministate.buffer.cpoff = utf8_cplen(ministate.buf);
518 void
519 cmd_list_bookmarks(struct buffer *buffer)
521 load_url_in_tab(current_tab(), "about:bookmarks");
524 void
525 cmd_toggle_help(struct buffer *buffer)
527 ui_toggle_side_window();
530 void
531 cmd_inc_fill_column(struct buffer *buffer)
533 if (fill_column == INT_MAX)
534 return;
536 fill_column += 2;
537 message("fill-column: %d", fill_column);
539 ui_schedule_redraw();
542 void
543 cmd_dec_fill_column(struct buffer *buffer)
545 if (fill_column == INT_MAX || fill_column < 8)
546 return;
548 fill_column -= 2;
549 message("fill-column: %d", fill_column);
551 ui_schedule_redraw();
554 void
555 cmd_olivetti_mode(struct buffer *buffer)
557 olivetti_mode = !olivetti_mode;
558 if (olivetti_mode)
559 message("olivetti-mode enabled");
560 else
561 message("olivetti-mode disabled");
563 ui_schedule_redraw();
566 void
567 cmd_mini_delete_char(struct buffer *buffer)
569 char *c, *n;
571 if (!in_minibuffer) {
572 message("text is read-only");
573 return;
576 minibuffer_taint_hist();
578 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
579 if (*c == '\0')
580 return;
581 n = utf8_next_cp(c);
583 memmove(c, n, strlen(n)+1);
586 void
587 cmd_mini_delete_backward_char(struct buffer *buffer)
589 char *c, *p, *start;
591 if (!in_minibuffer) {
592 message("text is read-only");
593 return;
596 minibuffer_taint_hist();
598 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
599 start = buffer->current_line->line;
600 if (c == start)
601 return;
602 p = utf8_prev_cp(c-1, start);
604 memmove(p, c, strlen(c)+1);
605 buffer->cpoff--;
608 void
609 cmd_mini_kill_line(struct buffer *buffer)
611 char *c;
613 if (!in_minibuffer) {
614 message("text is read-only");
615 return;
618 minibuffer_taint_hist();
619 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
620 *c = '\0';
623 void
624 cmd_mini_abort(struct buffer *buffer)
626 if (!in_minibuffer)
627 return;
629 ministate.abortfn();
632 void
633 cmd_mini_complete_and_exit(struct buffer *buffer)
635 if (!in_minibuffer)
636 return;
638 minibuffer_taint_hist();
639 ministate.donefn();
642 void
643 cmd_mini_previous_history_element(struct buffer *buffer)
645 if (ministate.history == NULL) {
646 message("No history");
647 return;
650 if (ministate.hist_cur == NULL ||
651 (ministate.hist_cur = TAILQ_PREV(ministate.hist_cur, mhisthead, entries)) == NULL) {
652 ministate.hist_cur = TAILQ_LAST(&ministate.history->head, mhisthead);
653 ministate.hist_off = ministate.history->len - 1;
654 if (ministate.hist_cur == NULL)
655 message("No prev item");
656 } else {
657 ministate.hist_off--;
660 if (ministate.hist_cur != NULL)
661 buffer->current_line->line = ministate.hist_cur->h;
664 void
665 cmd_mini_next_history_element(struct buffer *buffer)
667 if (ministate.history == NULL) {
668 message("No history");
669 return;
672 if (ministate.hist_cur == NULL ||
673 (ministate.hist_cur = TAILQ_NEXT(ministate.hist_cur, entries)) == NULL) {
674 ministate.hist_cur = TAILQ_FIRST(&ministate.history->head);
675 ministate.hist_off = 0;
676 if (ministate.hist_cur == NULL)
677 message("No next item");
678 } else {
679 ministate.hist_off++;
682 if (ministate.hist_cur != NULL)
683 buffer->current_line->line = ministate.hist_cur->h;