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 struct excursion place;
248 save_excursion(&place, buffer);
250 do {
251 if (buffer->current_line == NULL ||
252 buffer->current_line == TAILQ_FIRST(&buffer->head)) {
253 restore_excursion(&place, buffer);
254 message("No previous link");
255 return;
257 cmd_previous_line(buffer);
258 } while (buffer->current_line->parent->type != LINE_LINK);
261 void
262 cmd_next_button(struct buffer *buffer)
264 struct excursion place;
266 save_excursion(&place, buffer);
268 do {
269 if (buffer->current_line == NULL ||
270 buffer->current_line == TAILQ_LAST(&buffer->head, vhead)) {
271 restore_excursion(&place, buffer);
272 message("No next link");
273 return;
275 cmd_next_line(buffer);
276 } while (buffer->current_line->parent->type != LINE_LINK);
279 void
280 cmd_previous_page(struct buffer *buffer)
282 struct tab *tab = current_tab();
284 if (!load_previous_page(tab))
285 message("No previous page");
286 else
287 start_loading_anim(tab);
290 void
291 cmd_next_page(struct buffer *buffer)
293 struct tab *tab = current_tab();
295 if (!load_next_page(tab))
296 message("No next page");
297 else
298 start_loading_anim(tab);
301 void
302 cmd_clear_minibuf(struct buffer *buffer)
304 message(NULL);
307 void
308 cmd_execute_extended_command(struct buffer *buffer)
310 size_t len;
312 if (in_minibuffer) {
313 message("We don't have enable-recursive-minibuffers");
314 return;
317 enter_minibuffer(eecmd_self_insert, eecmd_select, exit_minibuffer,
318 &eecmd_history);
320 len = sizeof(ministate.prompt);
321 strlcpy(ministate.prompt, "", len);
323 if (thiskey.meta)
324 strlcat(ministate.prompt, "M-", len);
326 strlcat(ministate.prompt, ui_keyname(thiskey.key), len);
328 if (thiskey.meta)
329 strlcat(ministate.prompt, " ", len);
332 void
333 cmd_tab_close(struct buffer *buffer)
335 struct tab *tab, *t;
337 tab = current_tab();
338 if (TAILQ_PREV(tab, tabshead, tabs) == NULL &&
339 TAILQ_NEXT(tab, tabs) == NULL) {
340 message("Can't close the only tab.");
341 return;
344 if (evtimer_pending(&tab->loadingev, NULL))
345 evtimer_del(&tab->loadingev);
347 stop_tab(tab);
349 if ((t = TAILQ_PREV(tab, tabshead, tabs)) == NULL)
350 t = TAILQ_NEXT(tab, tabs);
351 TAILQ_REMOVE(&tabshead, tab, tabs);
352 free(tab);
354 switch_to_tab(t);
357 void
358 cmd_tab_close_other(struct buffer *buffer)
360 struct tab *t, *i;
362 TAILQ_FOREACH_SAFE(t, &tabshead, tabs, i) {
363 if (t->flags & TAB_CURRENT)
364 continue;
366 stop_tab(t);
367 TAILQ_REMOVE(&tabshead, t, tabs);
368 free(t);
372 void
373 cmd_tab_new(struct buffer *buffer)
375 const char *url;
377 if ((url = new_tab_url) == NULL)
378 url = NEW_TAB_URL;
380 new_tab(url);
383 void
384 cmd_tab_next(struct buffer *buffer)
386 struct tab *tab, *t;
388 tab = current_tab();
389 tab->flags &= ~TAB_CURRENT;
391 if ((t = TAILQ_NEXT(tab, tabs)) == NULL)
392 t = TAILQ_FIRST(&tabshead);
393 t->flags |= TAB_CURRENT;
394 t->flags &= ~TAB_URGENT;
397 void
398 cmd_tab_previous(struct buffer *buffer)
400 struct tab *tab, *t;
402 tab = current_tab();
403 tab->flags &= ~TAB_CURRENT;
405 if ((t = TAILQ_PREV(tab, tabshead, tabs)) == NULL)
406 t = TAILQ_LAST(&tabshead, tabshead);
407 t->flags |= TAB_CURRENT;
408 t->flags &= ~TAB_URGENT;
411 void
412 cmd_tab_move(struct buffer *buffer)
414 struct tab *tab, *t;
416 tab = current_tab();
417 t = TAILQ_NEXT(tab, tabs);
418 TAILQ_REMOVE(&tabshead, tab, tabs);
420 if (t == NULL)
421 TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
422 else
423 TAILQ_INSERT_AFTER(&tabshead, t, tab, tabs);
426 void
427 cmd_tab_move_to(struct buffer *buffer)
429 struct tab *tab, *t;
431 tab = current_tab();
432 t = TAILQ_PREV(tab, tabshead, tabs);
433 TAILQ_REMOVE(&tabshead, tab, tabs);
435 if (t == NULL) {
436 if (TAILQ_EMPTY(&tabshead))
437 TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
438 else
439 TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
440 } else
441 TAILQ_INSERT_BEFORE(t, tab, tabs);
444 void
445 cmd_load_url(struct buffer *buffer)
447 if (in_minibuffer) {
448 message("We don't have enable-recursive-minibuffers");
449 return;
452 enter_minibuffer(lu_self_insert, lu_select, exit_minibuffer,
453 &lu_history);
454 strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
455 strlcpy(ministate.buf, "gemini://", sizeof(ministate.buf));
456 cmd_move_end_of_line(&ministate.buffer);
459 void
460 cmd_load_current_url(struct buffer *buffer)
462 struct tab *tab = current_tab();
464 if (in_minibuffer) {
465 message("We don't have enable-recursive-minibuffers");
466 return;
469 enter_minibuffer(lu_self_insert, lu_select, exit_minibuffer,
470 &lu_history);
471 strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
472 strlcpy(ministate.buf, tab->hist_cur->h, sizeof(ministate.buf));
473 ministate.buffer.cpoff = utf8_cplen(ministate.buf);
476 void
477 cmd_bookmark_page(struct buffer *buffer)
479 struct tab *tab = current_tab();
481 enter_minibuffer(lu_self_insert, bp_select, exit_minibuffer, NULL);
482 strlcpy(ministate.prompt, "Bookmark URL: ", sizeof(ministate.prompt));
483 strlcpy(ministate.buf, tab->hist_cur->h, sizeof(ministate.buf));
484 ministate.buffer.cpoff = utf8_cplen(ministate.buf);
487 void
488 cmd_list_bookmarks(struct buffer *buffer)
490 load_url_in_tab(current_tab(), "about:bookmarks");
493 void
494 cmd_toggle_help(struct buffer *buffer)
496 ui_toggle_side_window();
499 void
500 cmd_olivetti_mode(struct buffer *buffer)
502 olivetti_mode = !olivetti_mode;
503 if (olivetti_mode)
504 message("olivetti-mode enabled");
505 else
506 message("olivetti-mode disabled");
508 ui_schedule_redraw();
511 void
512 cmd_mini_delete_char(struct buffer *buffer)
514 char *c, *n;
516 if (!in_minibuffer) {
517 message("text is read-only");
518 return;
521 minibuffer_taint_hist();
523 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
524 if (*c == '\0')
525 return;
526 n = utf8_next_cp(c);
528 memmove(c, n, strlen(n)+1);
531 void
532 cmd_mini_delete_backward_char(struct buffer *buffer)
534 char *c, *p, *start;
536 if (!in_minibuffer) {
537 message("text is read-only");
538 return;
541 minibuffer_taint_hist();
543 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
544 start = buffer->current_line->line;
545 if (c == start)
546 return;
547 p = utf8_prev_cp(c-1, start);
549 memmove(p, c, strlen(c)+1);
550 buffer->cpoff--;
553 void
554 cmd_mini_kill_line(struct buffer *buffer)
556 char *c;
558 if (!in_minibuffer) {
559 message("text is read-only");
560 return;
563 minibuffer_taint_hist();
564 c = utf8_nth(buffer->current_line->line, buffer->cpoff);
565 *c = '\0';
568 void
569 cmd_mini_abort(struct buffer *buffer)
571 if (!in_minibuffer)
572 return;
574 ministate.abortfn();
577 void
578 cmd_mini_complete_and_exit(struct buffer *buffer)
580 if (!in_minibuffer)
581 return;
583 minibuffer_taint_hist();
584 ministate.donefn();
587 void
588 cmd_mini_previous_history_element(struct buffer *buffer)
590 if (ministate.history == NULL) {
591 message("No history");
592 return;
595 if (ministate.hist_cur == NULL ||
596 (ministate.hist_cur = TAILQ_PREV(ministate.hist_cur, mhisthead, entries)) == NULL) {
597 ministate.hist_cur = TAILQ_LAST(&ministate.history->head, mhisthead);
598 ministate.hist_off = ministate.history->len - 1;
599 if (ministate.hist_cur == NULL)
600 message("No prev item");
601 } else {
602 ministate.hist_off--;
605 if (ministate.hist_cur != NULL)
606 buffer->current_line->line = ministate.hist_cur->h;
609 void
610 cmd_mini_next_history_element(struct buffer *buffer)
612 if (ministate.history == NULL) {
613 message("No history");
614 return;
617 if (ministate.hist_cur == NULL ||
618 (ministate.hist_cur = TAILQ_NEXT(ministate.hist_cur, entries)) == NULL) {
619 ministate.hist_cur = TAILQ_FIRST(&ministate.history->head);
620 ministate.hist_off = 0;
621 if (ministate.hist_cur == NULL)
622 message("No next item");
623 } else {
624 ministate.hist_off++;
627 if (ministate.hist_cur != NULL)
628 buffer->current_line->line = ministate.hist_cur->h;