aboutsummaryrefslogtreecommitdiff
path: root/cmd.c
blob: 5bbf8c9ddf6700771237949489155c5fa892f853 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
/*
 * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include <string.h>
#include <stdlib.h>

#include "telescope.h"

void
cmd_previous_line(struct buffer *buffer)
{
	struct vline	*vl;

	if (buffer->current_line == NULL
	    || (vl = TAILQ_PREV(buffer->current_line, vhead, vlines)) == NULL)
		return;

	if (--buffer->curs_y < 0) {
		buffer->curs_y = 0;
		cmd_scroll_line_up(buffer);
		return;
	}

	buffer->current_line = vl;
	restore_cursor(buffer);
}

void
cmd_next_line(struct buffer *buffer)
{
	struct vline	*vl;

	if (buffer->current_line == NULL
	    || (vl = TAILQ_NEXT(buffer->current_line, vlines)) == NULL)
		return;

	if (++buffer->curs_y > body_lines-1) {
		buffer->curs_y = body_lines-1;
		cmd_scroll_line_down(buffer);
		return;
	}

	buffer->current_line = vl;
	restore_cursor(buffer);
}

void
cmd_backward_char(struct buffer *buffer)
{
	if (buffer->cpoff != 0)
		buffer->cpoff--;
	restore_cursor(buffer);
}

void
cmd_forward_char(struct buffer *buffer)
{
	size_t len = 0;

	if (buffer->current_line->line != NULL)
		len = utf8_cplen(buffer->current_line->line);
	if (++buffer->cpoff > len)
		buffer->cpoff = len;
	restore_cursor(buffer);
}

void
cmd_backward_paragraph(struct buffer *buffer)
{
	do {
		if (buffer->current_line == NULL ||
		    buffer->current_line == TAILQ_FIRST(&buffer->head)) {
			message("No previous paragraph");
			return;
		}
		cmd_previous_line(buffer);
	} while (buffer->current_line->line != NULL ||
	    buffer->current_line->parent->type != LINE_TEXT);
}

void
cmd_forward_paragraph(struct buffer *buffer)
{
	do {
		if (buffer->current_line == NULL ||
		    buffer->current_line == TAILQ_LAST(&buffer->head, vhead)) {
			message("No next paragraph");
			return;
		}
		cmd_next_line(buffer);
	} while (buffer->current_line->line != NULL ||
	    buffer->current_line->parent->type != LINE_TEXT);
}

void
cmd_move_beginning_of_line(struct buffer *buffer)
{
	buffer->cpoff = 0;
	restore_cursor(buffer);
}

void
cmd_move_end_of_line(struct buffer *buffer)
{
	struct vline	*vl;

	vl = buffer->current_line;
	if (vl->line == NULL)
		return;
	buffer->cpoff = utf8_cplen(vl->line);
	restore_cursor(buffer);
}

void
cmd_redraw(struct buffer *buffer)
{
        ui_schedule_redraw();
}

void
cmd_scroll_line_up(struct buffer *buffer)
{
	if (buffer->current_line == NULL)
		return;

	if (buffer->line_off == 0)
		return;

	buffer->line_off--;
	buffer->current_line = TAILQ_PREV(buffer->current_line, vhead, vlines);
	restore_cursor(buffer);
}

void
cmd_scroll_line_down(struct buffer *buffer)
{
	struct vline	*vl;

	if (buffer->current_line == NULL)
		return;

	if ((vl = TAILQ_NEXT(buffer->current_line, vlines)) == NULL)
		return;

	buffer->current_line = vl;
	buffer->line_off++;
	restore_cursor(buffer);
}

void
cmd_scroll_up(struct buffer *buffer)
{
	size_t off;

	off = body_lines-1;

	for (; off > 0; --off)
		cmd_scroll_line_up(buffer);
}

void
cmd_scroll_down(struct buffer *buffer)
{
	size_t off;

	off = body_lines-1;

	for (; off > 0; --off)
		cmd_scroll_line_down(buffer);
}

void
cmd_beginning_of_buffer(struct buffer *buffer)
{
	buffer->current_line = TAILQ_FIRST(&buffer->head);
	buffer->line_off = 0;
	buffer->curs_y = 0;
	buffer->cpoff = 0;
	restore_cursor(buffer);
}

void
cmd_end_of_buffer(struct buffer *buffer)
{
	ssize_t off;

	off = buffer->line_max - body_lines;
	off = MAX(0, off);

	buffer->line_off = off;
	buffer->curs_y = MIN((size_t)body_lines-1, buffer->line_max-1);

	buffer->current_line = TAILQ_LAST(&buffer->head, vhead);
	buffer->cpoff = body_cols;
	restore_cursor(buffer);
}

void
cmd_kill_telescope(struct buffer *buffer)
{
	save_session();
	event_loopbreak();
}

void
cmd_push_button(struct buffer *buffer)
{
	struct vline	*vl;

	vl = buffer->current_line;
	if (vl->parent->type != LINE_LINK)
		return;

	load_url_in_tab(current_tab(), vl->parent->alt);
}

void
cmd_push_button_new_tab(struct buffer *buffer)
{
	struct vline	*vl;

	vl = buffer->current_line;
	if (vl->parent->type != LINE_LINK)
		return;

	new_tab(vl->parent->alt);
}

void
cmd_previous_button(struct buffer *buffer)
{
	struct excursion place;

	save_excursion(&place, buffer);

	do {
		if (buffer->current_line == NULL ||
		    buffer->current_line == TAILQ_FIRST(&buffer->head)) {
			restore_excursion(&place, buffer);
			message("No previous link");
			return;
		}
		cmd_previous_line(buffer);
	} while (buffer->current_line->parent->type != LINE_LINK);
}

void
cmd_next_button(struct buffer *buffer)
{
	struct excursion place;

	save_excursion(&place, buffer);

	do {
		if (buffer->current_line == NULL ||
		    buffer->current_line == TAILQ_LAST(&buffer->head, vhead)) {
			restore_excursion(&place, buffer);
			message("No next link");
			return;
		}
		cmd_next_line(buffer);
	} while (buffer->current_line->parent->type != LINE_LINK);
}

static inline int
is_heading(const struct line *l)
{
	return l->type == LINE_TITLE_1 ||
		l->type == LINE_TITLE_2 ||
		l->type == LINE_TITLE_3;
}

void
cmd_previous_heading(struct buffer *buffer)
{
	struct excursion place;

	save_excursion(&place, buffer);

	do {
		if (buffer->current_line == NULL ||
		    buffer->current_line == TAILQ_FIRST(&buffer->head)) {
			restore_excursion(&place, buffer);
			message("No previous heading");
			return;
		}
		cmd_previous_line(buffer);
	} while (!is_heading(buffer->current_line->parent));
}

void
cmd_next_heading(struct buffer *buffer)
{
	struct excursion place;

	save_excursion(&place, buffer);

	do {
		if (buffer->current_line == NULL ||
		    buffer->current_line == TAILQ_LAST(&buffer->head, vhead)) {
			restore_excursion(&place, buffer);
			message("No next heading");
			return;
		}
		cmd_next_line(buffer);
	} while (!is_heading(buffer->current_line->parent));
}

void
cmd_previous_page(struct buffer *buffer)
{
	struct tab *tab = current_tab();

	if (!load_previous_page(tab))
		message("No previous page");
	else
		start_loading_anim(tab);
}

void
cmd_next_page(struct buffer *buffer)
{
	struct tab *tab = current_tab();

	if (!load_next_page(tab))
		message("No next page");
	else
		start_loading_anim(tab);
}

void
cmd_clear_minibuf(struct buffer *buffer)
{
        message(NULL);
}

void
cmd_execute_extended_command(struct buffer *buffer)
{
	size_t	 len;

	if (in_minibuffer) {
		message("We don't have enable-recursive-minibuffers");
		return;
	}

	enter_minibuffer(eecmd_self_insert, eecmd_select, exit_minibuffer,
	    &eecmd_history);

	len = sizeof(ministate.prompt);
	strlcpy(ministate.prompt, "", len);

	if (thiskey.meta)
		strlcat(ministate.prompt, "M-", len);

	strlcat(ministate.prompt, ui_keyname(thiskey.key), len);

	if (thiskey.meta)
		strlcat(ministate.prompt, " ", len);
}

void
cmd_tab_close(struct buffer *buffer)
{
	struct tab *tab, *t;

	tab = current_tab();
	if (TAILQ_PREV(tab, tabshead, tabs) == NULL &&
	    TAILQ_NEXT(tab, tabs) == NULL) {
		message("Can't close the only tab.");
		return;
	}

	if (evtimer_pending(&tab->loadingev, NULL))
		evtimer_del(&tab->loadingev);

	stop_tab(tab);

	if ((t = TAILQ_PREV(tab, tabshead, tabs)) == NULL)
		t = TAILQ_NEXT(tab, tabs);
	TAILQ_REMOVE(&tabshead, tab, tabs);
	free(tab);

	switch_to_tab(t);
}

void
cmd_tab_close_other(struct buffer *buffer)
{
	struct tab *t, *i;

	TAILQ_FOREACH_SAFE(t, &tabshead, tabs, i) {
		if (t->flags & TAB_CURRENT)
			continue;

		stop_tab(t);
		TAILQ_REMOVE(&tabshead, t, tabs);
		free(t);
	}
}

void
cmd_tab_new(struct buffer *buffer)
{
	const char *url;

	if ((url = new_tab_url) == NULL)
		url = NEW_TAB_URL;

	new_tab(url);
}

void
cmd_tab_next(struct buffer *buffer)
{
	struct tab *tab, *t;

	tab = current_tab();
	tab->flags &= ~TAB_CURRENT;

	if ((t = TAILQ_NEXT(tab, tabs)) == NULL)
		t = TAILQ_FIRST(&tabshead);
	t->flags |= TAB_CURRENT;
	t->flags &= ~TAB_URGENT;
}

void
cmd_tab_previous(struct buffer *buffer)
{
	struct tab *tab, *t;

	tab = current_tab();
	tab->flags &= ~TAB_CURRENT;

	if ((t = TAILQ_PREV(tab, tabshead, tabs)) == NULL)
		t = TAILQ_LAST(&tabshead, tabshead);
	t->flags |= TAB_CURRENT;
	t->flags &= ~TAB_URGENT;
}

void
cmd_tab_move(struct buffer *buffer)
{
	struct tab *tab, *t;

	tab = current_tab();
	t = TAILQ_NEXT(tab, tabs);
	TAILQ_REMOVE(&tabshead, tab, tabs);

	if (t == NULL)
		TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
	else
		TAILQ_INSERT_AFTER(&tabshead, t, tab, tabs);
}

void
cmd_tab_move_to(struct buffer *buffer)
{
	struct tab *tab, *t;

	tab = current_tab();
	t = TAILQ_PREV(tab, tabshead, tabs);
	TAILQ_REMOVE(&tabshead, tab, tabs);

	if (t == NULL) {
		if (TAILQ_EMPTY(&tabshead))
			TAILQ_INSERT_HEAD(&tabshead, tab, tabs);
		else
			TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
	} else
		TAILQ_INSERT_BEFORE(t, tab, tabs);
}

void
cmd_load_url(struct buffer *buffer)
{
	if (in_minibuffer) {
		message("We don't have enable-recursive-minibuffers");
		return;
	}

	enter_minibuffer(lu_self_insert, lu_select, exit_minibuffer,
	    &lu_history);
	strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
	strlcpy(ministate.buf, "gemini://", sizeof(ministate.buf));
	cmd_move_end_of_line(&ministate.buffer);
}

void
cmd_load_current_url(struct buffer *buffer)
{
	struct tab *tab = current_tab();

	if (in_minibuffer) {
		message("We don't have enable-recursive-minibuffers");
		return;
	}

	enter_minibuffer(lu_self_insert, lu_select, exit_minibuffer,
	    &lu_history);
	strlcpy(ministate.prompt, "Load URL: ", sizeof(ministate.prompt));
	strlcpy(ministate.buf, tab->hist_cur->h, sizeof(ministate.buf));
	ministate.buffer.cpoff = utf8_cplen(ministate.buf);
}

void
cmd_bookmark_page(struct buffer *buffer)
{
	struct tab *tab = current_tab();

	enter_minibuffer(lu_self_insert, bp_select, exit_minibuffer, NULL);
	strlcpy(ministate.prompt, "Bookmark URL: ", sizeof(ministate.prompt));
	strlcpy(ministate.buf, tab->hist_cur->h, sizeof(ministate.buf));
	ministate.buffer.cpoff = utf8_cplen(ministate.buf);
}

void
cmd_list_bookmarks(struct buffer *buffer)
{
	load_url_in_tab(current_tab(), "about:bookmarks");
}

void
cmd_toggle_help(struct buffer *buffer)
{
	ui_toggle_side_window();
}

void
cmd_olivetti_mode(struct buffer *buffer)
{
	olivetti_mode = !olivetti_mode;
	if (olivetti_mode)
		message("olivetti-mode enabled");
	else
		message("olivetti-mode disabled");

	ui_schedule_redraw();
}

void
cmd_mini_delete_char(struct buffer *buffer)
{
	char *c, *n;

	if (!in_minibuffer) {
		message("text is read-only");
		return;
	}

	minibuffer_taint_hist();

	c = utf8_nth(buffer->current_line->line, buffer->cpoff);
	if (*c == '\0')
		return;
	n = utf8_next_cp(c);

	memmove(c, n, strlen(n)+1);
}

void
cmd_mini_delete_backward_char(struct buffer *buffer)
{
	char *c, *p, *start;

	if (!in_minibuffer) {
		message("text is read-only");
		return;
	}

	minibuffer_taint_hist();

	c = utf8_nth(buffer->current_line->line, buffer->cpoff);
	start = buffer->current_line->line;
	if (c == start)
		return;
	p = utf8_prev_cp(c-1, start);

	memmove(p, c, strlen(c)+1);
	buffer->cpoff--;
}

void
cmd_mini_kill_line(struct buffer *buffer)
{
	char *c;

	if (!in_minibuffer) {
		message("text is read-only");
		return;
	}

	minibuffer_taint_hist();
	c = utf8_nth(buffer->current_line->line, buffer->cpoff);
	*c = '\0';
}

void
cmd_mini_abort(struct buffer *buffer)
{
	if (!in_minibuffer)
		return;

	ministate.abortfn();
}

void
cmd_mini_complete_and_exit(struct buffer *buffer)
{
	if (!in_minibuffer)
		return;

	minibuffer_taint_hist();
	ministate.donefn();
}

void
cmd_mini_previous_history_element(struct buffer *buffer)
{
	if (ministate.history == NULL) {
		message("No history");
		return;
	}

	if (ministate.hist_cur == NULL ||
	    (ministate.hist_cur = TAILQ_PREV(ministate.hist_cur, mhisthead, entries)) == NULL) {
		ministate.hist_cur = TAILQ_LAST(&ministate.history->head, mhisthead);
		ministate.hist_off = ministate.history->len - 1;
		if (ministate.hist_cur == NULL)
			message("No prev item");
	} else {
		ministate.hist_off--;
	}

	if (ministate.hist_cur != NULL)
		buffer->current_line->line = ministate.hist_cur->h;
}

void
cmd_mini_next_history_element(struct buffer *buffer)
{
	if (ministate.history == NULL) {
		message("No history");
		return;
	}

	if (ministate.hist_cur == NULL ||
	    (ministate.hist_cur = TAILQ_NEXT(ministate.hist_cur, entries)) == NULL) {
		ministate.hist_cur = TAILQ_FIRST(&ministate.history->head);
		ministate.hist_off = 0;
		if (ministate.hist_cur == NULL)
			message("No next item");
	} else {
		ministate.hist_off++;
	}

	if (ministate.hist_cur != NULL)
		buffer->current_line->line = ministate.hist_cur->h;
}