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 <string.h>
18 #include <stdlib.h>
20 #include "telescope.h"
21 #include "cmd.gen.h"
23 void
24 cmd_previous_line(struct buffer *buffer)
25 {
26 struct vline *vl;
28 if (buffer->current_line == NULL
29 || (vl = TAILQ_PREV(buffer->current_line, vhead, vlines)) == NULL)
30 return;
32 if (--buffer->curs_y < 0) {
33 buffer->curs_y = 0;
34 cmd_scroll_line_up(buffer);
35 return;
36 }
38 buffer->current_line = vl;
39 restore_cursor(buffer);
40 }
42 void
43 cmd_next_line(struct buffer *buffer)
44 {
45 struct vline *vl;
47 if (buffer->current_line == NULL
48 || (vl = TAILQ_NEXT(buffer->current_line, vlines)) == NULL)
49 return;
51 if (++buffer->curs_y > body_lines-1) {
52 buffer->curs_y = body_lines-1;
53 cmd_scroll_line_down(buffer);
54 return;
55 }
57 buffer->current_line = vl;
58 restore_cursor(buffer);
59 }
61 void
62 cmd_backward_char(struct buffer *buffer)
63 {
64 if (buffer->cpoff != 0)
65 buffer->cpoff--;
66 restore_cursor(buffer);
67 }
69 void
70 cmd_forward_char(struct buffer *buffer)
71 {
72 size_t len = 0;
74 if (buffer->current_line->line != NULL)
75 len = utf8_cplen(buffer->current_line->line);
76 if (++buffer->cpoff > len)
77 buffer->cpoff = len;
78 restore_cursor(buffer);
79 }
81 void
82 cmd_backward_paragraph(struct buffer *buffer)
83 {
84 do {
85 if (buffer->current_line == NULL ||
86 buffer->current_line == TAILQ_FIRST(&buffer->head)) {
87 message("No previous paragraph");
88 return;
89 }
90 cmd_previous_line(buffer);
91 } while (buffer->current_line->line != NULL ||
92 buffer->current_line->parent->type != LINE_TEXT);
93 }
95 void
96 cmd_forward_paragraph(struct buffer *buffer)
97 {
98 do {
99 if (buffer->current_line == NULL ||
100 buffer->current_line == TAILQ_LAST(&buffer->head, vhead)) {
101 message("No next paragraph");
102 return;
104 cmd_next_line(buffer);
105 } while (buffer->current_line->line != NULL ||
106 buffer->current_line->parent->type != LINE_TEXT);
109 void
110 cmd_move_beginning_of_line(struct buffer *buffer)
112 buffer->cpoff = 0;
113 restore_cursor(buffer);
116 void
117 cmd_move_end_of_line(struct buffer *buffer)
119 struct vline *vl;
121 vl = buffer->current_line;
122 if (vl->line == NULL)
123 return;
124 buffer->cpoff = utf8_cplen(vl->line);
125 restore_cursor(buffer);
128 void
129 cmd_redraw(struct buffer *buffer)
131 ui_schedule_redraw();
134 void
135 cmd_scroll_line_up(struct buffer *buffer)
137 if (buffer->current_line == NULL)
138 return;
140 if (buffer->line_off == 0)
141 return;
143 buffer->line_off--;
144 buffer->current_line = TAILQ_PREV(buffer->current_line, vhead, vlines);
145 restore_cursor(buffer);
148 void
149 cmd_scroll_line_down(struct buffer *buffer)
151 struct vline *vl;
153 if (buffer->current_line == NULL)
154 return;
156 if ((vl = TAILQ_NEXT(buffer->current_line, vlines)) == NULL)
157 return;
159 buffer->current_line = vl;
160 buffer->line_off++;
161 restore_cursor(buffer);
164 void
165 cmd_scroll_up(struct buffer *buffer)
167 size_t off;
169 off = body_lines-1;
171 for (; off > 0; --off)
172 cmd_scroll_line_up(buffer);
175 void
176 cmd_scroll_down(struct buffer *buffer)
178 size_t off;
180 off = body_lines-1;
182 for (; off > 0; --off)
183 cmd_scroll_line_down(buffer);
186 void
187 cmd_beginning_of_buffer(struct buffer *buffer)
189 buffer->current_line = TAILQ_FIRST(&buffer->head);
190 buffer->line_off = 0;
191 buffer->curs_y = 0;
192 buffer->cpoff = 0;
193 restore_cursor(buffer);
196 void
197 cmd_end_of_buffer(struct buffer *buffer)
199 ssize_t off;
201 off = buffer->line_max - body_lines;
202 off = MAX(0, off);
204 buffer->line_off = off;
205 buffer->curs_y = MIN((size_t)body_lines-1, buffer->line_max-1);
207 buffer->current_line = TAILQ_LAST(&buffer->head, vhead);
208 buffer->cpoff = body_cols;
209 restore_cursor(buffer);
212 void
213 cmd_kill_telescope(struct buffer *buffer)
215 save_session();
216 event_loopbreak();
219 void
220 cmd_push_button(struct buffer *buffer)
222 struct vline *vl;
224 vl = buffer->current_line;
225 if (vl->parent->type != LINE_LINK)
226 return;
228 load_url_in_tab(current_tab(), vl->parent->alt);
231 void
232 cmd_push_button_new_tab(struct buffer *buffer)
234 struct vline *vl;
236 vl = buffer->current_line;
237 if (vl->parent->type != LINE_LINK)
238 return;
240 new_tab(vl->parent->alt);
243 void
244 cmd_previous_button(struct buffer *buffer)
246 do {
247 if (buffer->current_line == NULL ||
248 buffer->current_line == TAILQ_FIRST(&buffer->head)) {
249 message("No previous link");
250 return;
252 cmd_previous_line(buffer);
253 } while (buffer->current_line->parent->type != LINE_LINK);
256 void
257 cmd_next_button(struct buffer *buffer)
259 do {
260 if (buffer->current_line == NULL ||
261 buffer->current_line == TAILQ_LAST(&buffer->head, vhead)) {
262 message("No next link");
263 return;
265 cmd_next_line(buffer);
266 } while (buffer->current_line->parent->type != LINE_LINK);
269 void
270 cmd_previous_page(struct buffer *buffer)
272 struct tab *tab = current_tab();
274 if (!load_previous_page(tab))
275 message("No previous page");
276 else
277 start_loading_anim(tab);
280 void
281 cmd_next_page(struct buffer *buffer)
283 struct tab *tab = current_tab();
285 if (!load_next_page(tab))
286 message("No next page");
287 else
288 start_loading_anim(tab);
291 void
292 cmd_clear_minibuf(struct buffer *buffer)
294 message(NULL);
297 void
298 cmd_execute_extended_command(struct buffer *buffer)
300 size_t len;
302 if (in_minibuffer) {
303 message("We don't have enable-recursive-minibuffers");
304 return;
307 enter_minibuffer(eecmd_self_insert, eecmd_select, exit_minibuffer,
308 &eecmd_history);
310 len = sizeof(ministate.prompt);
311 strlcpy(ministate.prompt, "", len);
313 if (thiskey.meta)
314 strlcat(ministate.prompt, "M-", len);
316 strlcat(ministate.prompt, ui_keyname(thiskey.key), len);
318 if (thiskey.meta)
319 strlcat(ministate.prompt, " ", len);
322 void
323 cmd_tab_close(struct buffer *buffer)
325 struct tab *tab, *t;
327 tab = current_tab();
328 if (TAILQ_PREV(tab, tabshead, tabs) == NULL &&
329 TAILQ_NEXT(tab, tabs) == NULL) {
330 message("Can't close the only tab.");
331 return;
334 if (evtimer_pending(&tab->loadingev, NULL))
335 evtimer_del(&tab->loadingev);
337 stop_tab(tab);
339 if ((t = TAILQ_PREV(tab, tabshead, tabs)) == NULL)
340 t = TAILQ_NEXT(tab, tabs);
341 TAILQ_REMOVE(&tabshead, tab, tabs);
342 free(tab);
344 switch_to_tab(t);
347 void
348 cmd_tab_close_other(struct buffer *buffer)
350 struct tab *t, *i;
352 TAILQ_FOREACH_SAFE(t, &tabshead, tabs, i) {
353 if (t->flags & TAB_CURRENT)
354 continue;
356 stop_tab(t);
357 TAILQ_REMOVE(&tabshead, t, tabs);
358 free(t);
362 void
363 cmd_tab_new(struct buffer *buffer)
365 const char *url;
367 if ((url = new_tab_url) == NULL)
368 url = NEW_TAB_URL;
370 new_tab(url);
373 void
374 cmd_tab_next(struct buffer *buffer)
376 struct tab *tab, *t;
378 tab = current_tab();
379 tab->flags &= ~TAB_CURRENT;
381 if ((t = TAILQ_NEXT(tab, tabs)) == NULL)
382 t = TAILQ_FIRST(&tabshead);
383 t->flags |= TAB_CURRENT;
384 t->flags &= ~TAB_URGENT;
387 void
388 cmd_tab_previous(struct buffer *buffer)
390 struct tab *tab, *t;
392 tab = current_tab();
393 tab->flags &= ~TAB_CURRENT;
395 if ((t = TAILQ_PREV(tab, tabshead, tabs)) == NULL)
396 t = TAILQ_LAST(&tabshead, tabshead);
397 t->flags |= TAB_CURRENT;
398 t->flags &= ~TAB_URGENT;
401 void
402 cmd_tab_move(struct buffer *buffer)
404 struct tab *tab, *t;
406 tab = current_tab();
407 t = TAILQ_NEXT(tab, tabs);
408 TAILQ_REMOVE(&tabshead, tab, tabs);
410 if (t == NULL)
411 TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
412 else
413 TAILQ_INSERT_AFTER(&tabshead, t, tab, tabs);
416 void
417 cmd_tab_move_to(struct buffer *buffer)
419 struct tab *tab, *t;
421 tab = current_tab();
422 t = TAILQ_PREV(tab, tabshead, tabs);
423 TAILQ_REMOVE(&tabshead, tab, tabs);
425 if (t == NULL) {
426 if (TAILQ_EMPTY(&tabshead))
427 TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
428 else
429 TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
430 } else
431 TAILQ_INSERT_BEFORE(t, tab, tabs);
434 void
435 cmd_load_url(struct buffer *buffer)
437 if (in_minibuffer) {
438 message("We don't have enable-recursive-minibuffers");
439 return;
442 enter_minibuffer(lu_self_insert, lu_select, exit_minibuffer,
443 &lu_history);
444 strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
445 strlcpy(ministate.buf, "gemini://", sizeof(ministate.buf));
446 cmd_move_end_of_line(&ministate.buffer);
449 void
450 cmd_load_current_url(struct buffer *buffer)
452 struct tab *tab = current_tab();
454 if (in_minibuffer) {
455 message("We don't have enable-recursive-minibuffers");
456 return;
459 enter_minibuffer(lu_self_insert, lu_select, exit_minibuffer,
460 &lu_history);
461 strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
462 strlcpy(ministate.buf, tab->hist_cur->h, sizeof(ministate.buf));
463 ministate.buffer.cpoff = utf8_cplen(ministate.buf);
466 void
467 cmd_bookmark_page(struct buffer *buffer)
469 struct tab *tab = current_tab();
471 enter_minibuffer(lu_self_insert, bp_select, exit_minibuffer, NULL);
472 strlcpy(ministate.prompt, "Bookmark URL: ", sizeof(ministate.prompt));
473 strlcpy(ministate.buf, tab->hist_cur->h, sizeof(ministate.buf));
474 ministate.buffer.cpoff = utf8_cplen(ministate.buf);
477 void
478 cmd_list_bookmarks(struct buffer *buffer)
480 load_url_in_tab(current_tab(), "about:bookmarks");
483 void
484 cmd_toggle_help(struct buffer *buffer)
486 ui_toggle_side_window();
489 void
490 cmd_olivetti_mode(struct buffer *buffer)
492 olivetti_mode = !olivetti_mode;
493 if (olivetti_mode)
494 message("olivetti-mode enabled");
495 else
496 message("olivetti-mode disabled");
498 ui_schedule_redraw();
501 void
502 cmd_mini_delete_char(struct buffer *buffer)
504 char *c, *n;
506 if (!in_minibuffer) {
507 message("text is read-only");
508 return;
511 minibuffer_taint_hist();
513 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
514 if (*c == '\0')
515 return;
516 n = utf8_next_cp(c);
518 memmove(c, n, strlen(n)+1);
521 void
522 cmd_mini_delete_backward_char(struct buffer *buffer)
524 char *c, *p, *start;
526 if (!in_minibuffer) {
527 message("text is read-only");
528 return;
531 minibuffer_taint_hist();
533 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
534 start = buffer->current_line->line;
535 if (c == start)
536 return;
537 p = utf8_prev_cp(c-1, start);
539 memmove(p, c, strlen(c)+1);
540 buffer->cpoff--;
543 void
544 cmd_mini_kill_line(struct buffer *buffer)
546 char *c;
548 if (!in_minibuffer) {
549 message("text is read-only");
550 return;
553 minibuffer_taint_hist();
554 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
555 *c = '\0';
558 void
559 cmd_mini_abort(struct buffer *buffer)
561 if (!in_minibuffer)
562 return;
564 ministate.abortfn();
567 void
568 cmd_mini_complete_and_exit(struct buffer *buffer)
570 if (!in_minibuffer)
571 return;
573 minibuffer_taint_hist();
574 ministate.donefn();
577 void
578 cmd_mini_previous_history_element(struct buffer *buffer)
580 if (ministate.history == NULL) {
581 message("No history");
582 return;
585 if (ministate.hist_cur == NULL ||
586 (ministate.hist_cur = TAILQ_PREV(ministate.hist_cur, mhisthead, entries)) == NULL) {
587 ministate.hist_cur = TAILQ_LAST(&ministate.history->head, mhisthead);
588 ministate.hist_off = ministate.history->len - 1;
589 if (ministate.hist_cur == NULL)
590 message("No prev item");
591 } else {
592 ministate.hist_off--;
595 if (ministate.hist_cur != NULL)
596 buffer->current_line->line = ministate.hist_cur->h;
599 void
600 cmd_mini_next_history_element(struct buffer *buffer)
602 if (ministate.history == NULL) {
603 message("No history");
604 return;
607 if (ministate.hist_cur == NULL ||
608 (ministate.hist_cur = TAILQ_NEXT(ministate.hist_cur, entries)) == NULL) {
609 ministate.hist_cur = TAILQ_FIRST(&ministate.history->head);
610 ministate.hist_off = 0;
611 if (ministate.hist_cur == NULL)
612 message("No next item");
613 } else {
614 ministate.hist_off++;
617 if (ministate.hist_cur != NULL)
618 buffer->current_line->line = ministate.hist_cur->h;