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"
24 /* return 1 if moved, 0 otherwise */
25 static inline int
26 forward_line(struct buffer *buffer, int n)
27 {
28 struct vline *vl;
29 int did;
31 if (buffer->current_line == NULL)
32 return 0;
33 vl = buffer->current_line;
35 did = 0;
36 while (n != 0) {
37 if (n > 0) {
38 vl = TAILQ_NEXT(vl, vlines);
39 if (vl == NULL)
40 return did;
41 if (vl->parent->flags & L_HIDDEN)
42 continue;
43 buffer->current_line = vl;
44 n--;
45 } else {
46 vl = TAILQ_PREV(vl, vhead, vlines);
47 if (vl == NULL)
48 return did;
49 if (vl->parent->flags & L_HIDDEN)
50 continue;
51 if (buffer->current_line == buffer->top_line)
52 buffer->top_line = vl;
53 buffer->current_line = vl;
54 n++;
55 }
57 did = 1;
58 }
60 return did;
61 }
63 void
64 cmd_previous_line(struct buffer *buffer)
65 {
66 forward_line(buffer, -1);
67 }
69 void
70 cmd_next_line(struct buffer *buffer)
71 {
72 forward_line(buffer, +1);
73 }
75 void
76 cmd_backward_char(struct buffer *buffer)
77 {
78 if (buffer->cpoff != 0)
79 buffer->cpoff--;
80 }
82 void
83 cmd_forward_char(struct buffer *buffer)
84 {
85 size_t len = 0;
87 if (buffer->current_line->line != NULL)
88 len = utf8_cplen(buffer->current_line->line);
89 if (++buffer->cpoff > len)
90 buffer->cpoff = len;
91 }
93 void
94 cmd_backward_paragraph(struct buffer *buffer)
95 {
96 do {
97 if (!forward_line(buffer, -1)) {
98 message("No previous paragraph");
99 return;
101 } while (buffer->current_line->line != NULL ||
102 buffer->current_line->parent->type != LINE_TEXT);
105 void
106 cmd_forward_paragraph(struct buffer *buffer)
108 do {
109 if (!forward_line(buffer, +1)) {
110 message("No next paragraph");
111 return;
113 } while (buffer->current_line->line != NULL ||
114 buffer->current_line->parent->type != LINE_TEXT);
117 void
118 cmd_move_beginning_of_line(struct buffer *buffer)
120 buffer->cpoff = 0;
123 void
124 cmd_move_end_of_line(struct buffer *buffer)
126 struct vline *vl;
128 vl = buffer->current_line;
129 if (vl->line == NULL)
130 return;
131 buffer->cpoff = utf8_cplen(vl->line);
134 void
135 cmd_redraw(struct buffer *buffer)
137 ui_schedule_redraw();
140 void
141 cmd_scroll_line_up(struct buffer *buffer)
143 struct vline *vl;
145 if ((vl = TAILQ_PREV(buffer->top_line, vhead, vlines))
146 == NULL)
147 return;
149 forward_line(buffer, -1);
151 buffer->line_off--;
152 buffer->top_line = vl;
155 void
156 cmd_scroll_line_down(struct buffer *buffer)
158 if (!forward_line(buffer, +1))
159 return;
161 buffer->top_line = TAILQ_NEXT(buffer->top_line, vlines);
162 buffer->line_off++;
165 void
166 cmd_scroll_up(struct buffer *buffer)
168 forward_line(buffer, -1*body_lines);
171 void
172 cmd_scroll_down(struct buffer *buffer)
174 forward_line(buffer, body_lines);
177 void
178 cmd_beginning_of_buffer(struct buffer *buffer)
180 buffer->current_line = TAILQ_FIRST(&buffer->head);
181 buffer->cpoff = 0;
182 buffer->top_line = buffer->current_line;
183 buffer->line_off = 0;
186 void
187 cmd_end_of_buffer(struct buffer *buffer)
189 buffer->current_line = TAILQ_LAST(&buffer->head, vhead);
190 buffer->cpoff = body_cols;
193 void
194 cmd_kill_telescope(struct buffer *buffer)
196 save_session();
197 event_loopbreak();
200 void
201 cmd_push_button(struct buffer *buffer)
203 struct vline *vl;
204 struct line *l;
206 vl = buffer->current_line;
207 switch (vl->parent->type) {
208 case LINE_LINK:
209 load_url_in_tab(current_tab(), vl->parent->alt);
210 break;
211 case LINE_PRE_START:
212 l = TAILQ_NEXT(vl->parent, lines);
213 for (; l != NULL; l = TAILQ_NEXT(l, lines)) {
214 if (l->type == LINE_PRE_END)
215 break;
216 l->flags ^= L_HIDDEN;
218 break;
219 default:
220 break;
224 void
225 cmd_push_button_new_tab(struct buffer *buffer)
227 struct vline *vl;
229 vl = buffer->current_line;
230 if (vl->parent->type != LINE_LINK)
231 return;
233 new_tab(vl->parent->alt);
236 void
237 cmd_previous_button(struct buffer *buffer)
239 struct excursion place;
241 save_excursion(&place, buffer);
243 do {
244 if (!forward_line(buffer, -1)) {
245 restore_excursion(&place, buffer);
246 message("No previous link");
247 return;
249 } while (buffer->current_line->parent->type != LINE_LINK);
252 void
253 cmd_next_button(struct buffer *buffer)
255 struct excursion place;
257 save_excursion(&place, buffer);
259 do {
260 if (!forward_line(buffer, +1)){
261 restore_excursion(&place, buffer);
262 message("No next link");
263 return;
265 } while (buffer->current_line->parent->type != LINE_LINK);
268 static inline int
269 is_heading(const struct line *l)
271 return l->type == LINE_TITLE_1 ||
272 l->type == LINE_TITLE_2 ||
273 l->type == LINE_TITLE_3;
276 void
277 cmd_previous_heading(struct buffer *buffer)
279 struct excursion place;
281 save_excursion(&place, buffer);
283 do {
284 if (!forward_line(buffer, -1)) {
285 restore_excursion(&place, buffer);
286 message("No previous heading");
287 return;
289 } while (!is_heading(buffer->current_line->parent));
292 void
293 cmd_next_heading(struct buffer *buffer)
295 struct excursion place;
297 save_excursion(&place, buffer);
299 do {
300 if (!forward_line(buffer, +1)) {
301 restore_excursion(&place, buffer);
302 message("No next heading");
303 return;
305 } while (!is_heading(buffer->current_line->parent));
308 void
309 cmd_previous_page(struct buffer *buffer)
311 struct tab *tab = current_tab();
313 if (!load_previous_page(tab))
314 message("No previous page");
315 else
316 start_loading_anim(tab);
319 void
320 cmd_next_page(struct buffer *buffer)
322 struct tab *tab = current_tab();
324 if (!load_next_page(tab))
325 message("No next page");
326 else
327 start_loading_anim(tab);
330 void
331 cmd_clear_minibuf(struct buffer *buffer)
333 message(NULL);
336 void
337 cmd_execute_extended_command(struct buffer *buffer)
339 size_t len;
341 if (in_minibuffer) {
342 message("We don't have enable-recursive-minibuffers");
343 return;
346 enter_minibuffer(eecmd_self_insert, eecmd_select, exit_minibuffer,
347 &eecmd_history);
349 len = sizeof(ministate.prompt);
350 strlcpy(ministate.prompt, "", len);
352 if (thiskey.meta)
353 strlcat(ministate.prompt, "M-", len);
355 strlcat(ministate.prompt, ui_keyname(thiskey.key), len);
357 if (thiskey.meta)
358 strlcat(ministate.prompt, " ", len);
361 void
362 cmd_tab_close(struct buffer *buffer)
364 struct tab *tab, *t;
366 tab = current_tab();
367 if (TAILQ_PREV(tab, tabshead, tabs) == NULL &&
368 TAILQ_NEXT(tab, tabs) == NULL) {
369 message("Can't close the only tab.");
370 return;
373 if (evtimer_pending(&tab->loadingev, NULL))
374 evtimer_del(&tab->loadingev);
376 stop_tab(tab);
378 if ((t = TAILQ_PREV(tab, tabshead, tabs)) == NULL)
379 t = TAILQ_NEXT(tab, tabs);
380 TAILQ_REMOVE(&tabshead, tab, tabs);
381 free(tab);
383 switch_to_tab(t);
386 void
387 cmd_tab_close_other(struct buffer *buffer)
389 struct tab *t, *i;
391 TAILQ_FOREACH_SAFE(t, &tabshead, tabs, i) {
392 if (t->flags & TAB_CURRENT)
393 continue;
395 stop_tab(t);
396 TAILQ_REMOVE(&tabshead, t, tabs);
397 free(t);
401 void
402 cmd_tab_new(struct buffer *buffer)
404 const char *url;
406 if ((url = new_tab_url) == NULL)
407 url = NEW_TAB_URL;
409 new_tab(url);
412 void
413 cmd_tab_next(struct buffer *buffer)
415 struct tab *tab, *t;
417 tab = current_tab();
418 tab->flags &= ~TAB_CURRENT;
420 if ((t = TAILQ_NEXT(tab, tabs)) == NULL)
421 t = TAILQ_FIRST(&tabshead);
422 t->flags |= TAB_CURRENT;
423 t->flags &= ~TAB_URGENT;
426 void
427 cmd_tab_previous(struct buffer *buffer)
429 struct tab *tab, *t;
431 tab = current_tab();
432 tab->flags &= ~TAB_CURRENT;
434 if ((t = TAILQ_PREV(tab, tabshead, tabs)) == NULL)
435 t = TAILQ_LAST(&tabshead, tabshead);
436 t->flags |= TAB_CURRENT;
437 t->flags &= ~TAB_URGENT;
440 void
441 cmd_tab_move(struct buffer *buffer)
443 struct tab *tab, *t;
445 tab = current_tab();
446 t = TAILQ_NEXT(tab, tabs);
447 TAILQ_REMOVE(&tabshead, tab, tabs);
449 if (t == NULL)
450 TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
451 else
452 TAILQ_INSERT_AFTER(&tabshead, t, tab, tabs);
455 void
456 cmd_tab_move_to(struct buffer *buffer)
458 struct tab *tab, *t;
460 tab = current_tab();
461 t = TAILQ_PREV(tab, tabshead, tabs);
462 TAILQ_REMOVE(&tabshead, tab, tabs);
464 if (t == NULL) {
465 if (TAILQ_EMPTY(&tabshead))
466 TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
467 else
468 TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
469 } else
470 TAILQ_INSERT_BEFORE(t, tab, tabs);
473 void
474 cmd_load_url(struct buffer *buffer)
476 if (in_minibuffer) {
477 message("We don't have enable-recursive-minibuffers");
478 return;
481 enter_minibuffer(lu_self_insert, lu_select, exit_minibuffer,
482 &lu_history);
483 strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
484 strlcpy(ministate.buf, "gemini://", sizeof(ministate.buf));
485 cmd_move_end_of_line(&ministate.buffer);
488 void
489 cmd_load_current_url(struct buffer *buffer)
491 struct tab *tab = current_tab();
493 if (in_minibuffer) {
494 message("We don't have enable-recursive-minibuffers");
495 return;
498 enter_minibuffer(lu_self_insert, lu_select, exit_minibuffer,
499 &lu_history);
500 strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
501 strlcpy(ministate.buf, tab->hist_cur->h, sizeof(ministate.buf));
502 ministate.buffer.cpoff = utf8_cplen(ministate.buf);
505 void
506 cmd_bookmark_page(struct buffer *buffer)
508 struct tab *tab = current_tab();
510 enter_minibuffer(lu_self_insert, bp_select, exit_minibuffer, NULL);
511 strlcpy(ministate.prompt, "Bookmark URL: ", sizeof(ministate.prompt));
512 strlcpy(ministate.buf, tab->hist_cur->h, sizeof(ministate.buf));
513 ministate.buffer.cpoff = utf8_cplen(ministate.buf);
516 void
517 cmd_list_bookmarks(struct buffer *buffer)
519 load_url_in_tab(current_tab(), "about:bookmarks");
522 void
523 cmd_toggle_help(struct buffer *buffer)
525 ui_toggle_side_window();
528 void
529 cmd_inc_fill_column(struct buffer *buffer)
531 if (fill_column == INT_MAX)
532 return;
534 fill_column += 2;
535 message("fill-column: %d", fill_column);
537 ui_schedule_redraw();
540 void
541 cmd_dec_fill_column(struct buffer *buffer)
543 if (fill_column == INT_MAX || fill_column < 8)
544 return;
546 fill_column -= 2;
547 message("fill-column: %d", fill_column);
549 ui_schedule_redraw();
552 void
553 cmd_olivetti_mode(struct buffer *buffer)
555 olivetti_mode = !olivetti_mode;
556 if (olivetti_mode)
557 message("olivetti-mode enabled");
558 else
559 message("olivetti-mode disabled");
561 ui_schedule_redraw();
564 void
565 cmd_mini_delete_char(struct buffer *buffer)
567 char *c, *n;
569 if (!in_minibuffer) {
570 message("text is read-only");
571 return;
574 minibuffer_taint_hist();
576 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
577 if (*c == '\0')
578 return;
579 n = utf8_next_cp(c);
581 memmove(c, n, strlen(n)+1);
584 void
585 cmd_mini_delete_backward_char(struct buffer *buffer)
587 char *c, *p, *start;
589 if (!in_minibuffer) {
590 message("text is read-only");
591 return;
594 minibuffer_taint_hist();
596 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
597 start = buffer->current_line->line;
598 if (c == start)
599 return;
600 p = utf8_prev_cp(c-1, start);
602 memmove(p, c, strlen(c)+1);
603 buffer->cpoff--;
606 void
607 cmd_mini_kill_line(struct buffer *buffer)
609 char *c;
611 if (!in_minibuffer) {
612 message("text is read-only");
613 return;
616 minibuffer_taint_hist();
617 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
618 *c = '\0';
621 void
622 cmd_mini_abort(struct buffer *buffer)
624 if (!in_minibuffer)
625 return;
627 ministate.abortfn();
630 void
631 cmd_mini_complete_and_exit(struct buffer *buffer)
633 if (!in_minibuffer)
634 return;
636 minibuffer_taint_hist();
637 ministate.donefn();
640 void
641 cmd_mini_previous_history_element(struct buffer *buffer)
643 if (ministate.history == NULL) {
644 message("No history");
645 return;
648 if (ministate.hist_cur == NULL ||
649 (ministate.hist_cur = TAILQ_PREV(ministate.hist_cur, mhisthead, entries)) == NULL) {
650 ministate.hist_cur = TAILQ_LAST(&ministate.history->head, mhisthead);
651 ministate.hist_off = ministate.history->len - 1;
652 if (ministate.hist_cur == NULL)
653 message("No prev item");
654 } else {
655 ministate.hist_off--;
658 if (ministate.hist_cur != NULL)
659 buffer->current_line->line = ministate.hist_cur->h;
662 void
663 cmd_mini_next_history_element(struct buffer *buffer)
665 if (ministate.history == NULL) {
666 message("No history");
667 return;
670 if (ministate.hist_cur == NULL ||
671 (ministate.hist_cur = TAILQ_NEXT(ministate.hist_cur, entries)) == NULL) {
672 ministate.hist_cur = TAILQ_FIRST(&ministate.history->head);
673 ministate.hist_off = 0;
674 if (ministate.hist_cur == NULL)
675 message("No next item");
676 } else {
677 ministate.hist_off++;
680 if (ministate.hist_cur != NULL)
681 buffer->current_line->line = ministate.hist_cur->h;