about summary refs log tree commit diff
path: root/term.c
blob: bf4a933be1e8a0662353b80fbdac1ef16e97e094 (plain) (blame)
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
/* Copyright (C) 2018, 2020  C. McEnroe <june@causal.agency>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <termios.h>
#include <unistd.h>

#include "chat.h"

static bool xterm;

void termInit(void) {
	const char *term = getenv("TERM");
	xterm = (term && !strncmp(term, "xterm", 5));
}

void termNoFlow(void) {
	struct termios attr;
	int error = tcgetattr(STDIN_FILENO, &attr);
	if (error) return;
	attr.c_iflag &= ~IXON;
	attr.c_cc[VDISCARD] = _POSIX_VDISABLE;
	tcsetattr(STDIN_FILENO, TCSANOW, &attr);
}

void termTitle(const char *title) {
	if (!xterm) return;
	printf("\33]0;%s\33\\", title);
	fflush(stdout);
}

static void privateMode(const char *mode, bool set) {
	printf("\33[?%s%c", mode, (set ? 'h' : 'l'));
	fflush(stdout);
}

void termMode(enum TermMode mode, bool set) {
	switch (mode) {
		break; case TermFocus: privateMode("1004", set);
		break; case TermPaste: privateMode("2004", set);
	}
}

enum { Esc = '\33' };

enum TermEvent termEvent(char ch) {
	static int st;
#define T(st, ch) ((st) << 8 | (ch))
	switch (T(st, ch)) {
		break; case T(0, Esc): st = 1;
		break; case T(1, '['): st = 2;
		break; case T(2, 'I'): st = 0; return TermFocusIn;
		break; case T(2, 'O'): st = 0; return TermFocusOut;
		break; case T(2, '2'): st = 3;
		break; case T(3, '0'): st = 4;
		break; case T(4, '0'): st = 5;
		break; case T(5, '~'): st = 0; return TermPasteStart;
		break; case T(4, '1'): st = 6;
		break; case T(6, '~'): st = 0; return TermPasteEnd;
		break; default: st = 0;
	}
	return 0;
#undef T
}
0'>2022-02-20Remove unused mbs.len field from struct EditJune McEnroe 2022-02-19Remove unneeded includes in ui.cJune McEnroe 2022-02-19Reimplement tab completeJune McEnroe 2022-02-19Handle errors from editFn, etc.June McEnroe 2022-02-19Reimplement text macrosJune McEnroe 2022-02-19Factor out input handling to input.cJune McEnroe 2022-02-19Factor out window management to window.cJune McEnroe 2022-02-19Enable -Wmissing-prototypesJune McEnroe In other words, warn when a function is missing static. I don't see why this isn't in -Wextra. 2022-02-19Fix edit.[ch] license notice additional permissionsJune McEnroe 2022-02-19Run line editing testsJune McEnroe I know, it feels wrong. 2022-02-18Implement new line editing "library"June McEnroe Losing tab complete and text macros, for now. This new implementation works on an instance of a struct and does not interact with the rest of catgirl, making it possible to copy into another project. Unlike existing line editing libraries, this one is entirely abstract and can be rendered externally. My goal with this library is to be able to implement vi mode. Since it operates on struct instances rather than globals, it might also be possible to give catgirl separate line editing buffers for each window, which would be a nice UX improvement. 2022-02-18Simplify cursor positioning in inputJune McEnroe Do some extra work by adding the portion before the cursor to the input window twice, but simplify the interaction with the split point. This fixes the awkward behaviour when moving the cursor across colour codes where the code would be partially interpreted up to the cursor. 2022-02-18Fix M-f orderingJune McEnroe 2022-02-12Move sandman build to scripts/MakefileJune McEnroe 2022-02-12Use compat_readpassphrase.c on LinuxJune McEnroe 2022-02-12Copy RPP defines from oconfigureJune McEnroe