Improved TTY key decoding

- get_str() returns byte count
- add KEY_xxx definitions for SHIFT, CTRL and SHIFT+CTRL cursor keys
- allow KEY_META() for cursor and function keys
- add 'key' tracing option
- improve trace output readability
- add KEY_UNKNOWN for unrecognized TTY sequences
- add unknown-key command to display unknown key details
- add buf_encode_byte() to share code
- recognise M-ESC

Improve config file parsers:
- add full-version global variable for tests in config file
- no longer display configuration file errors in tqe
- recognise variable setting syntax in tqe
  handle tab-width, default-tab-width, indent-tabs-mode, indent-width

- bump VERSION to 5.3.4
This commit is contained in:
Charlie Gordon 2022-08-27 20:11:38 +02:00
commit ee60fcd046
10 changed files with 374 additions and 171 deletions

View file

@ -1 +1 @@
5.3.3f
5.3.4

View file

@ -875,9 +875,12 @@ void eb_trace_bytes(const void *buf, int size, int state)
EditState *e;
const char *str = NULL;
const u8 *p0, *endp, *p;
int c, line, col, len, point;
int line, col, len, point, flush;
int prev_state = qs->trace_buffer_state;
if (!b || !(qs->trace_flags & state))
/* prevent tracing if nagivating the *trace* buffer */
if (!b || !(qs->trace_flags & state)
|| (qs->active_window && qs->active_window->b == b))
return;
point = b->total_size;
@ -885,36 +888,50 @@ void eb_trace_bytes(const void *buf, int size, int state)
size = strlen(buf);
eb_get_pos(b, &line, &col, point);
if (col == 0 || qs->trace_buffer_state != state) {
if (col) {
eb_insert_uchar(b, b->total_size, '\n');
col = 0;
}
state &= ~EB_TRACE_FLUSH;
flush = state & EB_TRACE_FLUSH;
state &= ~EB_TRACE_FLUSH;
if (prev_state == state) {
if (state == EB_TRACE_COMMAND)
flush = 1;
} else {
if (!(state <= EB_TRACE_COMMAND && prev_state <= state)
&& !(state == EB_TRACE_EMULATE && prev_state == EB_TRACE_PTY))
flush = 1;
}
if (flush && col) {
eb_insert_uchar(b, b->total_size, '\n');
col = 0;
}
if (col == 0 || prev_state != state) {
qs->trace_buffer_state = state;
switch (state) {
case EB_TRACE_TTY:
str = " tty: ";
str = "tty";
break;
case EB_TRACE_KEY:
str = "key";
break;
case EB_TRACE_PTY:
str = " pty: ";
str = "pty";
break;
case EB_TRACE_SHELL:
str = " shell: ";
str = "shell";
break;
case EB_TRACE_EMULATE:
str = "emulate: ";
str = "emulate";
break;
case EB_TRACE_DEBUG:
str = " debug: ";
str = "debug";
break;
case EB_TRACE_COMMAND:
eb_printf(b, "command: %s\n", cs8(buf));
size = 0;
str = "command";
break;
}
if (str) {
col += eb_write(b, b->total_size, str, strlen(str));
int width = (col == 0 ? 7 :
col <= 20 ? 27 - col :
col <= 40 ? 47 - col : strlen(str) + 2);
col += eb_printf(b, "%*s: ", width, str);
}
}
p0 = buf;
@ -937,32 +954,20 @@ void eb_trace_bytes(const void *buf, int size, int state)
continue;
}
if (p < endp) {
if (((void)(c = 'n'), *p == '\n')
|| ((void)(c = 'r'), *p == '\r')
|| ((void)(c = 't'), *p == '\t')
|| ((void)(c = 'b'), *p == '\010')
|| ((void)(c = 'E'), *p == '\033')
|| ((void)(c = '\\'), *p == '\\')) {
col += eb_printf(b, "\\%c", c);
} else
if (*p < 32) {
//if (*p == '\e' && col > 9) {
// eb_write(b, b->total_size, "\n ", 10);
// col = 9;
//}
col += eb_printf(b, "\\^%c", (*p + '@') & 127);
} else {
col += eb_printf(b, "\\%03o", *p);
}
char buf1[80];
buf_t out[1];
buf_init(out, buf1, sizeof buf1);
len = buf_encode_byte(out, *p);
eb_write(b, b->total_size, out->buf, out->len);
p0 = p + 1;
}
break;
}
}
/* If point is visible in window, should keep it so */
/* Make output visible in window */
/* XXX: proper tracking should do this automatically */
e = eb_find_window(b, NULL);
if (e && e->offset == point)
if (e)
e->offset = b->total_size;
}

115
parser.c
View file

@ -38,10 +38,18 @@ typedef struct QEmacsDataSource {
int offset, stop;
} QEmacsDataSource;
static int expect_token(const char **pp, int tok)
{
static int has_token(const char **pp, int tok) {
if (qe_skip_spaces(pp) == tok) {
++*pp;
qe_skip_spaces(pp);
return 1;
} else {
return 0;
}
}
static int expect_token(const char **pp, int tok) {
if (has_token(pp, tok)) {
return 1;
} else {
put_status(NULL, "'%c' expected", tok);
@ -92,6 +100,17 @@ static int qe_cfg_parse_string(EditState *s, const char **pp,
return res;
}
static int get_cmd(const char **pp, char *buf, int buf_size, const char *stop) {
char *q;
int len = get_str(pp, buf, buf_size, stop);
/* convert '_' to '-' */
for (q = buf; *q; q++) {
if (*q == '_')
*q = '-';
}
return len;
}
static char *data_gets(QEmacsDataSource *ds, char *buf, int size)
{
if (ds->f) {
@ -124,7 +143,7 @@ static int qe_parse_script(EditState *s, QEmacsDataSource *ds)
QEmacsState *qs = s->qe_state;
QErrorContext ec;
char line[1024], str[1024];
char cmd[128], *q, *strp;
char cmd[128], arg[128], *strp;
CmdArgSpec cas;
const char *p, *r;
int line_num;
@ -148,55 +167,44 @@ static int qe_parse_script(EditState *s, QEmacsDataSource *ds)
again:
if (qe_skip_spaces(&p) == '\0')
continue;
if (incomment)
goto comment;
if (incomment) {
while (*p) {
if (*p++ == '*' && *p == '/') {
p++;
incomment = 0;
break;
}
}
goto again;
}
if (*p == '/') {
if (p[1] == '/') /* line comment */
continue;
if (p[1] == '*') { /* multiline comment */
p += 2;
incomment = 1;
comment:
while (*p) {
if (*p++ == '*' && *p == '/') {
p++;
incomment = 0;
break;
}
}
goto again;
}
}
if (p[0] == '}') {
if (has_token(&p, '}')) {
/* simplistic 1 level if block skip feature */
p++;
qe_skip_spaces(&p);
skip = 0;
goto again;
}
if (skip)
continue;
if (p[0] == '\0')
continue;
/* XXX: should parse numbers, strings and symbols */
get_str(&p, cmd, sizeof(cmd), "{}();=/");
if (*cmd == '\0') {
put_status(s, "Syntax error");
continue;
}
/* transform '_' to '-' */
q = cmd;
while (*q) {
if (*q == '_')
*q = '-';
q++;
}
if (!get_cmd(&p, cmd, sizeof(cmd), "{}();=/"))
goto syntax;
/* simplistic 1 level if block skip feature */
if (strequal(cmd, "if")) {
if (!expect_token(&p, '('))
goto fail;
skip = !strtol_c(p, &p, 0);
if (!get_cmd(&p, arg, sizeof(arg), ")"))
goto syntax;
skip = !strtol_c(arg, NULL, 0);
if (!expect_token(&p, ')') || !expect_token(&p, '{'))
goto fail;
continue;
@ -218,12 +226,32 @@ static int qe_parse_script(EditState *s, QEmacsDataSource *ds)
} else {
qe_set_variable(s, cmd, NULL, strtol_c(p, &p, 0));
}
qe_skip_spaces(&p);
if (*p != ';' && *p != '\0')
put_status(s, "Syntax error '%s'", cmd);
continue;
goto next;
}
}
#else
if (has_token(&p, '=')) {
int value = strtol_c(p, &p, 0);
if (strequal(cmd, "tab-width")) {
s->b->tab_width = value;
goto next;
}
if (strequal(cmd, "default-tab-width")) {
qs->default_tab_width = value;
goto next;
}
if (strequal(cmd, "indent-tabs-mode")) {
s->indent_tabs_mode = value;
goto next;
}
if (strequal(cmd, "indent-width")) {
s->indent_size = value;
goto next;
}
/* ignore other variables without a warning */
put_status(s, "Unsupported variable %s", cmd);
continue;
}
#endif
if (!expect_token(&p, '('))
goto fail;
@ -336,9 +364,9 @@ static int qe_parse_script(EditState *s, QEmacsDataSource *ds)
break;
}
}
if (qe_skip_spaces(&p) != ')') {
if (!has_token(&p, ')')) {
put_status(s, "Too many arguments for %s", d->name);
goto fail;
continue;
}
qs->this_cmd_func = d->action.func;
@ -348,10 +376,17 @@ static int qe_parse_script(EditState *s, QEmacsDataSource *ds)
if (qs->active_window)
s = qs->active_window;
check_window(&s);
next:
if (has_token(&p, ';'))
goto again;
if (*p != '\0')
put_status(s, "Missing ';' at '%s'", p);
continue;
syntax:
put_status(s, "Syntax error at '%s'", p);
continue;
fail:
;
continue;
}
qs->ec = ec;

54
qe.c
View file

@ -493,12 +493,16 @@ void do_set_trace_flags(EditState *s, int flags) {
EditState *e = qe_split_window(s, SW_STACKED, 75);
if (e) {
do_switch_to_buffer(e, "*trace*");
e->offset = e->b->total_size;
}
}
*buf = '\0';
if (qs->trace_flags & EB_TRACE_TTY) {
strcat(buf, ", tty");
}
if (qs->trace_flags & EB_TRACE_KEY) {
strcat(buf, ", key");
}
if (qs->trace_flags & EB_TRACE_SHELL) {
strcat(buf, ", shell");
}
@ -511,6 +515,9 @@ void do_set_trace_flags(EditState *s, int flags) {
if (qs->trace_flags & EB_TRACE_COMMAND) {
strcat(buf, ", command");
}
if (qs->trace_flags & EB_TRACE_DEBUG) {
strcat(buf, ", debug");
}
put_status(s, "Tracing enabled for %s", buf + 2);
} else {
put_status(s, "Tracing disabled");
@ -545,6 +552,9 @@ void do_set_trace_options(EditState *s, const char *options) {
if (strmatchword(p, "tty", &p)) {
flags |= EB_TRACE_TTY;
} else
if (strmatchword(p, "key", &p)) {
flags |= EB_TRACE_KEY;
} else
if (strmatchword(p, "shell", &p)) {
flags |= EB_TRACE_SHELL;
} else
@ -1701,6 +1711,18 @@ void do_space(EditState *s, int key, int argval)
}
#endif
static void do_unknown_key(EditState *s) {
QEmacsState *qs = s ? s->qe_state : &qe_state;
char buf[80];
buf_t out[1];
int i;
buf_init(out, buf, sizeof buf);
for (i = 0; i < qs->input_len; i++)
buf_encode_byte(out, qs->input_buf[i]);
put_status(s, "Unknown key: %s", buf);
}
void do_keyboard_quit(EditState *s)
{
#ifndef CONFIG_TINY
@ -4766,7 +4788,7 @@ void exec_command(EditState *s, const CmdDef *d, int argval, int key)
ExecCmdState *es;
const char *argdesc;
if (qe_state.trace_buffer && qe_state.trace_buffer != s->b)
if (qe_state.trace_buffer)
eb_trace_bytes(d->name, -1, EB_TRACE_COMMAND);
argdesc = d->spec;
@ -5465,10 +5487,15 @@ static void qe_key_process(int key)
/* CG: should beep */;
}
out = buf_init(&outbuf, buf1, sizeof(buf1));
buf_puts(out, "No command on ");
buf_put_keys(out, c->keys, c->nb_keys);
put_status(s, "No command on %s", buf1);
if (qs->trace_buffer)
eb_trace_bytes(buf1, -1, EB_TRACE_COMMAND);
put_status(s, "%s", buf1);
c->describe_key = 0;
qe_key_init(c);
if (qs->trace_buffer)
edit_display(qs);
dpy_flush(&global_screen);
return;
} else
@ -5534,6 +5561,8 @@ static void qe_key_process(int key)
buf_put_key(out, key);
buf_put_byte(out, '-');
put_status(s, "~%s", c->buf);
if (qs->trace_buffer)
edit_display(qs);
dpy_flush(&global_screen);
}
@ -8552,6 +8581,14 @@ void qe_handle_event(QEEvent *ev)
switch (ev->type) {
case QE_KEY_EVENT:
if (qs->trace_buffer) {
char buf[16];
buf_t out[1];
buf_init(out, buf, sizeof buf);
buf_put_key(out, ev->key_event.key);
buf_put_byte(out, ' ');
eb_trace_bytes(buf, -1, EB_TRACE_KEY);
}
qe_key_process(ev->key_event.key);
break;
case QE_EXPOSE_EVENT:
@ -9265,18 +9302,17 @@ static void qe_init(void *opaque)
s = qs->active_window;
}
#endif
#ifdef CONFIG_TINY
put_status(s, "Tiny QEmacs %s - Press F1 for help", QE_VERSION);
#else
put_status(s, "QEmacs %s - Press F1 for help", QE_VERSION);
edit_display(qs);
dpy_flush(&global_screen);
b = eb_find("*errors*");
if (b != NULL) {
show_popup(s, b, "Errors");
edit_display(qs);
dpy_flush(&global_screen);
}
#endif
edit_display(qs);
dpy_flush(&global_screen);
qs->ec.function = NULL;
}

41
qe.h
View file

@ -504,7 +504,7 @@ int strsubst(char *buf, int buf_size, const char *from,
int strquote(char *dest, int size, const char *str, int len);
int strunquote(char *dest, int size, const char *str, int len);
void get_str(const char **pp, char *buf, int buf_size, const char *stop);
int get_str(const char **pp, char *buf, int buf_size, const char *stop);
int css_get_enum(const char *str, const char *enum_str);
extern QEColor const xterm_colors[];
@ -738,6 +738,7 @@ int strtokey(const char **pp);
int strtokeys(const char *keystr, unsigned int *keys, int max_keys, const char **endp);
int buf_put_key(buf_t *out, int key);
int buf_put_keys(buf_t *out, unsigned int *keys, int nb_keys);
int buf_encode_byte(buf_t *out, int ch);
/* charset.c */
@ -873,11 +874,14 @@ enum QEEventType {
};
#define KEY_CTRL(c) ((c) & 0x001f)
#define KEY_META(c) ((c) | 0xe000)
#define KEY_ESC1(c) ((c) | 0xe100)
/* allow combinations such as KEY_META(KEY_LEFT) */
#define KEY_META(c) ((c) | 0xe100)
#define KEY_ESC1(c) ((c) | 0xe200)
#define KEY_IS_ESC1(c) ((c) >= KEY_ESC1(0) && (c) <= KEY_ESC1(0xff))
#define KEY_IS_SPECIAL(c) ((c) >= 0xe000 && (c) < 0xf000)
#define KEY_IS_CONTROL(c) (((c) >= 0 && (c) < 32) || (c) == 127)
#define KEY_UNKNOWN 0xfffe
#define KEY_NONE 0xffff
#define KEY_DEFAULT 0xe401 /* to handle all non special keys */
@ -901,6 +905,22 @@ enum QEEventType {
#define KEY_CTRL_HOME KEY_ESC1('h')
#define KEY_CTRL_PAGEUP KEY_ESC1('i')
#define KEY_CTRL_PAGEDOWN KEY_ESC1('j')
#define KEY_SHIFT_UP KEY_ESC1('a'+128)
#define KEY_SHIFT_DOWN KEY_ESC1('b'+128)
#define KEY_SHIFT_RIGHT KEY_ESC1('c'+128)
#define KEY_SHIFT_LEFT KEY_ESC1('d'+128)
#define KEY_SHIFT_END KEY_ESC1('f'+128)
#define KEY_SHIFT_HOME KEY_ESC1('h'+128)
#define KEY_SHIFT_PAGEUP KEY_ESC1('i'+128)
#define KEY_SHIFT_PAGEDOWN KEY_ESC1('j'+128)
#define KEY_CTRL_SHIFT_UP KEY_ESC1('a'+64)
#define KEY_CTRL_SHIFT_DOWN KEY_ESC1('b'+64)
#define KEY_CTRL_SHIFT_RIGHT KEY_ESC1('c'+64)
#define KEY_CTRL_SHIFT_LEFT KEY_ESC1('d'+64)
#define KEY_CTRL_SHIFT_END KEY_ESC1('f'+64)
#define KEY_CTRL_SHIFT_HOME KEY_ESC1('h'+64)
#define KEY_CTRL_SHIFT_PAGEUP KEY_ESC1('i'+64)
#define KEY_CTRL_SHIFT_PAGEDOWN KEY_ESC1('j'+64)
#define KEY_SHIFT_TAB KEY_ESC1('Z') // kcbt
#define KEY_HOME KEY_ESC1(1) // khome
#define KEY_INSERT KEY_ESC1(2) // kich1
@ -1661,12 +1681,13 @@ struct QEmacsState {
int trace_flags;
int trace_buffer_state;
#define EB_TRACE_TTY 0x01
#define EB_TRACE_SHELL 0x02
#define EB_TRACE_PTY 0x04
#define EB_TRACE_EMULATE 0x08
#define EB_TRACE_COMMAND 0x10
#define EB_TRACE_DEBUG 0x20
#define EB_TRACE_ALL 0x2F
#define EB_TRACE_KEY 0x02
#define EB_TRACE_COMMAND 0x04
#define EB_TRACE_SHELL 0x08
#define EB_TRACE_PTY 0x10
#define EB_TRACE_EMULATE 0x20
#define EB_TRACE_DEBUG 0x30
#define EB_TRACE_ALL 0x7F
#define EB_TRACE_FLUSH 0x100
/* global layout info : DO NOT modify these directly. do_refresh
@ -1734,6 +1755,8 @@ struct QEmacsState {
//int fuzzy_search; /* use fuzzy search for completion matcher */
int c_label_indent;
const char *user_option;
int input_len;
u8 input_buf[32];
};
extern QEmacsState qe_state;

View file

@ -91,10 +91,10 @@ static const CmdDef basic_commands[] = {
CMD2( "forward-char", "C-f, right",
"Move to the next character",
do_left_right, ESi, "p")
CMD2( "backward-word", "M-b, C-left",
CMD2( "backward-word", "M-b, C-left, M-left",
"Move to the beginning of the word on or before point",
do_word_left_right, ESi, "q")
CMD2( "forward-word", "M-f, C-right",
CMD2( "forward-word", "M-f, C-right, M-right",
"Move to the end of the word on or after point",
do_word_left_right, ESi, "p")
CMD1( "scroll-down", "M-v, pageup",
@ -278,6 +278,10 @@ static const CmdDef basic_commands[] = {
"C-g, C-x C-g, C-c C-g, C-h C-g, ESC ESC ESC",
"Abort the current command",
do_keyboard_quit)
CMD0( "unknown-key",
"none",
"An unknown key was pressed",
do_unknown_key)
CMD0( "start-kbd-macro", "C-x (",
"Start recording a keyboard macro",
do_start_kbd_macro)

175
tty.c
View file

@ -125,6 +125,7 @@ typedef struct TTYState {
int cursor_x, cursor_y;
/* input handling */
enum InputState input_state;
int has_meta;
int input_param, input_param2;
int utf8_index;
unsigned char buf[8];
@ -505,41 +506,41 @@ static int tty_dpy_is_user_input_pending(QEditScreen *s)
}
static int const csi_lookup[] = {
KEY_NONE, /* 0 */
KEY_HOME, /* 1 */
KEY_INSERT, /* 2 */
KEY_DELETE, /* 3 */
KEY_END, /* 4 */
KEY_PAGEUP, /* 5 */
KEY_UNKNOWN, /* 0 */
KEY_HOME, /* 1 */
KEY_INSERT, /* 2 */
KEY_DELETE, /* 3 */
KEY_END, /* 4 */
KEY_PAGEUP, /* 5 */
KEY_PAGEDOWN, /* 6 */
KEY_NONE, /* 7 */
KEY_NONE, /* 8 */
KEY_NONE, /* 9 */
KEY_NONE, /* 10 */
KEY_F1, /* 11 */
KEY_F2, /* 12 */
KEY_F3, /* 13 */
KEY_F4, /* 14 */
KEY_F5, /* 15 */
KEY_NONE, /* 16 */
KEY_F6, /* 17 */
KEY_F7, /* 18 */
KEY_F8, /* 19 */
KEY_F9, /* 20 */
KEY_F10, /* 21 */
KEY_NONE, /* 22 */
KEY_F11, /* 23 */
KEY_F12, /* 24 */
KEY_F13, /* 25 */
KEY_F14, /* 26 */
KEY_NONE, /* 27 */
KEY_F15, /* 28 */
KEY_F16, /* 29 */
KEY_NONE, /* 30 */
KEY_F17, /* 31 */
KEY_F18, /* 32 */
KEY_F19, /* 33 */
KEY_F20, /* 34 */
KEY_UNKNOWN, /* 7 */
KEY_UNKNOWN, /* 8 */
KEY_UNKNOWN, /* 9 */
KEY_UNKNOWN, /* 10 */
KEY_F1, /* 11 */
KEY_F2, /* 12 */
KEY_F3, /* 13 */
KEY_F4, /* 14 */
KEY_F5, /* 15 */
KEY_UNKNOWN, /* 16 */
KEY_F6, /* 17 */
KEY_F7, /* 18 */
KEY_F8, /* 19 */
KEY_F9, /* 20 */
KEY_F10, /* 21 */
KEY_UNKNOWN, /* 22 */
KEY_F11, /* 23 */
KEY_F12, /* 24 */
KEY_F13, /* 25 */
KEY_F14, /* 26 */
KEY_UNKNOWN, /* 27 */
KEY_F15, /* 28 */
KEY_F16, /* 29 */
KEY_UNKNOWN, /* 30 */
KEY_F17, /* 31 */
KEY_F18, /* 32 */
KEY_F19, /* 33 */
KEY_F20, /* 34 */
};
static void tty_read_handler(void *opaque)
@ -549,21 +550,23 @@ static void tty_read_handler(void *opaque)
TTYState *ts = s->priv_data;
QEEvent ev1, *ev = &ev1;
u8 buf[1];
int ch, len;
int ch, len, n1;
if (read(fileno(s->STDIN), buf, 1) != 1)
return;
if (qs->trace_buffer &&
qs->active_window &&
qs->active_window->b != qs->trace_buffer) {
if (qs->trace_buffer)
eb_trace_bytes(buf, 1, EB_TRACE_TTY);
}
ch = buf[0];
/* keep TTY bytes for error messages */
if (qs->input_len < countof(qs->input_buf))
qs->input_buf[qs->input_len++] = ch;
switch (ts->input_state) {
case IS_NORM:
qs->input_len = 1;
qs->input_buf[0] = ch;
/* charset handling */
if (s->charset == &charset_utf8) {
if (ts->utf8_index && !(ch > 0x80 && ch < 0xc0)) {
@ -598,8 +601,15 @@ static void tty_read_handler(void *opaque)
break;
case IS_ESC:
if (ch == '\033') {
if (!tty_dpy_is_user_input_pending(s)) {
/* Distinguish alt-ESC from ESC prefix applied to other keys */
ch = KEY_META(KEY_ESC);
ts->input_state = IS_NORM;
goto the_end;
}
/* cygwin A-right transmit ESC ESC[C ... */
goto the_end;
ts->has_meta = 8;
break;
}
if (ch == '[') {
if (!tty_dpy_is_user_input_pending(s)) {
@ -639,39 +649,83 @@ static void tty_read_handler(void *opaque)
ts->input_state = IS_CSI2;
break;
case '~':
if (ts->input_param < countof(csi_lookup)) {
ch = csi_lookup[ts->input_param];
/* If there is a second param, it tells the shift state,
* ex: S-f5 = ^[[15;2~ */
n1 = ts->input_param;
if (ts->input_param2) {
// XXX: should handle shift function keys
ch = KEY_UNKNOWN;
ts->has_meta = 0;
goto the_end;
}
if (n1 < countof(csi_lookup)) {
ch = csi_lookup[n1];
goto the_end;
}
break;
/* All these for ansi|cygwin */
default:
if (ts->input_param == 5) {
/* input_param contains the shift status:
* bit 2 is SHIFT
* bit 4 is CTRL
* bit 8 is ALT
*/
if (ts->input_param & 8) {
ts->has_meta = 8;
}
if ((ts->input_param & 6) == 6) {
/* iterm2 CTRL-SHIFT-arrows */
switch (ch) {
case 'A': ch = KEY_CTRL_SHIFT_UP; goto the_end;
case 'B': ch = KEY_CTRL_SHIFT_DOWN; goto the_end;
case 'C': ch = KEY_CTRL_SHIFT_RIGHT; goto the_end;
case 'D': ch = KEY_CTRL_SHIFT_LEFT; goto the_end;
case 'F': ch = KEY_CTRL_SHIFT_END; goto the_end;
case 'H': ch = KEY_CTRL_SHIFT_HOME; goto the_end;
}
} else
if ((ts->input_param & 6) == 4) {
/* xterm CTRL-arrows */
/* iterm2 CTRL-arrows:
* C-up = ^[[1;5A
* C-down = ^[[1;5B
* C-left = ^[[1;5D
* C-right = ^[[1;5C
* C-left = ^[[1;5D
* C-end = ^[[1;5F
* C-home = ^[[1;5H
*/
switch (ch) {
case 'A': ch = KEY_CTRL_UP; goto the_end;
case 'B': ch = KEY_CTRL_DOWN; goto the_end;
case 'C': ch = KEY_CTRL_RIGHT; goto the_end;
case 'D': ch = KEY_CTRL_LEFT; goto the_end;
case 'F': ch = KEY_CTRL_END; goto the_end;
case 'H': ch = KEY_CTRL_HOME; goto the_end;
}
} else
if (ts->input_param == 2) {
if ((ts->input_param & 6) == 2) {
/* iterm2 SHIFT-arrows:
* S-left = ^[[1;2D
* S-up = ^[[1;2A
* S-down = ^[[1;2B
* S-right = ^[[1;2C
* S-left = ^[[1;2D
* S-f1 = ^[[1;2P
* S-f2 = ^[[1;2Q
* S-f3 = ^[[1;2R
* S-f4 = ^[[1;2S
* should set-mark if region not visible
*/
switch (ch) {
case 'A': ch = KEY_UP; goto the_end;
case 'B': ch = KEY_DOWN; goto the_end;
case 'C': ch = KEY_RIGHT; goto the_end;
case 'D': ch = KEY_LEFT; goto the_end;
case 'A': ch = KEY_SHIFT_UP; goto the_end;
case 'B': ch = KEY_SHIFT_DOWN; goto the_end;
case 'C': ch = KEY_SHIFT_RIGHT; goto the_end;
case 'D': ch = KEY_SHIFT_LEFT; goto the_end;
case 'F': ch = KEY_SHIFT_END; goto the_end;
case 'H': ch = KEY_SHIFT_HOME; goto the_end;
//case 'P': ch = KEY_SHIFT_F1; goto the_end;
//case 'Q': ch = KEY_SHIFT_F2; goto the_end;
//case 'R': ch = KEY_SHIFT_F3; goto the_end;
//case 'S': ch = KEY_SHIFT_F4; goto the_end;
}
} else {
switch (ch) {
@ -687,7 +741,9 @@ static void tty_read_handler(void *opaque)
case 'Z': ch = KEY_SHIFT_TAB; goto the_end; // kcbt
}
}
break;
ch = KEY_UNKNOWN;
ts->has_meta = 0;
goto the_end;
}
break;
case IS_CSI2:
@ -700,7 +756,9 @@ static void tty_read_handler(void *opaque)
case 'D': ch = KEY_F4; goto the_end;
case 'E': ch = KEY_F5; goto the_end;
}
break;
ch = KEY_UNKNOWN;
goto the_end;
case IS_ESC2: // "\EO"
/* xterm/vt100 fn */
ts->input_state = IS_NORM;
@ -709,8 +767,8 @@ static void tty_read_handler(void *opaque)
case 'B': ch = KEY_DOWN; goto the_end;
case 'C': ch = KEY_RIGHT; goto the_end;
case 'D': ch = KEY_LEFT; goto the_end;
case 'F': ch = KEY_CTRL_RIGHT; goto the_end; /* iterm2 F-right */
case 'H': ch = KEY_CTRL_LEFT; goto the_end; /* iterm2 F-left */
case 'F': ch = KEY_END; goto the_end; /* iterm2 F-right */
case 'H': ch = KEY_HOME; goto the_end; /* iterm2 F-left */
case 'P': ch = KEY_F1; goto the_end;
case 'Q': ch = KEY_F2; goto the_end;
case 'R': ch = KEY_F3; goto the_end;
@ -722,8 +780,15 @@ static void tty_read_handler(void *opaque)
case 'w': ch = KEY_F9; goto the_end;
case 'x': ch = KEY_F10; goto the_end;
}
break;
ch = KEY_UNKNOWN;
goto the_end;
the_end:
if (ts->has_meta) {
ts->has_meta = 0;
ch = KEY_META(ch);
}
// XXX: should add custom key conversions, especially for KEY_UNKNOWN
ev->key_event.type = QE_KEY_EVENT;
ev->key_event.key = ch;
qe_handle_event(ev);

70
util.c
View file

@ -934,10 +934,9 @@ int ustr_get_word(char *buf, int buf_size, int c,
}
/* Read a token from a string, stop on a set of characters.
* Skip spaces before and after token.
* Skip spaces before and after token. Return the token length.
*/
void get_str(const char **pp, char *buf, int buf_size, const char *stop)
{
int get_str(const char **pp, char *buf, int buf_size, const char *stop) {
char *q;
const char *p;
int c;
@ -957,6 +956,7 @@ void get_str(const char **pp, char *buf, int buf_size, const char *stop)
*q = '\0';
*pp = p;
qe_skip_spaces(pp);
return q - buf;
}
/* scans a comma separated list of entries, return index of match or -1 */
@ -994,8 +994,12 @@ static unsigned short const keycodes[] = {
KEY_HOME, KEY_END, KEY_PAGEUP, KEY_PAGEDOWN,
KEY_CTRL_LEFT, KEY_CTRL_RIGHT, KEY_CTRL_UP, KEY_CTRL_DOWN,
KEY_CTRL_HOME, KEY_CTRL_END, KEY_CTRL_PAGEUP, KEY_CTRL_PAGEDOWN,
KEY_PAGEUP, KEY_PAGEDOWN, KEY_CTRL_PAGEUP, KEY_CTRL_PAGEDOWN,
KEY_INSERT, KEY_DELETE, KEY_DEFAULT,
KEY_SHIFT_LEFT, KEY_SHIFT_RIGHT, KEY_SHIFT_UP, KEY_SHIFT_DOWN,
KEY_SHIFT_HOME, KEY_SHIFT_END, KEY_SHIFT_PAGEUP, KEY_SHIFT_PAGEDOWN,
KEY_CTRL_SHIFT_LEFT, KEY_CTRL_SHIFT_RIGHT, KEY_CTRL_SHIFT_UP, KEY_CTRL_SHIFT_DOWN,
KEY_CTRL_SHIFT_HOME, KEY_CTRL_SHIFT_END, KEY_CTRL_SHIFT_PAGEUP, KEY_CTRL_SHIFT_PAGEDOWN,
KEY_PAGEUP, KEY_PAGEDOWN, KEY_INSERT, KEY_DELETE,
KEY_DEFAULT, KEY_NONE, KEY_UNKNOWN,
KEY_F1, KEY_F2, KEY_F3, KEY_F4, KEY_F5,
KEY_F6, KEY_F7, KEY_F8, KEY_F9, KEY_F10,
KEY_F11, KEY_F12, KEY_F13, KEY_F14, KEY_F15,
@ -1003,15 +1007,19 @@ static unsigned short const keycodes[] = {
'{', '}', '|',
};
static const char * const keystr[] = {
static const char * const keystr[countof(keycodes)] = {
"SPC", "DEL", "RET", "LF", "ESC", "TAB", "S-TAB",
"C-SPC", "C-@", "C-?", "C-\\", "C-]", "C-^", "C-_", "C-/",
"left", "right", "up", "down",
"home", "end", "prior", "next",
"home", "end", "pageup", "pagedown",
"C-left", "C-right", "C-up", "C-down",
"C-home", "C-end", "C-prior", "C-next",
"pageup", "pagedown", "C-pageup", "C-pagedown",
"insert", "delete", "default",
"C-home", "C-end", "C-pageup", "C-pagedown",
"S-left", "S-right", "S-up", "S-down",
"S-home", "S-end", "S-pageup", "S-pagedown",
"C-S-left", "C-S-right", "C-S-up", "C-S-down",
"C-S-home", "C-S-end", "C-S-pageup", "C-S-pagedown",
"prior", "next", "insert", "delete",
"default", "none", "unknown",
"f1", "f2", "f3", "f4", "f5", "f6", "f7", "f8", "f9", "f10",
"f11", "f12", "f13", "f14", "f15", "f16", "f17", "f18", "f19", "f20",
"LB", "RB", "VB",
@ -1026,8 +1034,8 @@ int compose_keys(unsigned int *keys, int *nb_keys)
/* compose KEY_ESC as META prefix */
keyp = keys + *nb_keys - 2;
if (keyp[0] == KEY_ESC) {
if (keyp[1] != KEY_ESC && keyp[1] <= 0xff) {
if (keyp[0] == KEY_ESC && keyp[1] != KEY_ESC) {
if (keyp[1] <= 0xff || KEY_IS_ESC1(keyp[1])) {
keyp[0] = KEY_META(keyp[1]);
--*nb_keys;
return 1;
@ -1128,6 +1136,7 @@ int strtokeys(const char *str, unsigned int *keys,
return nb_keys;
}
/* Convert a key to a string representation. Recurse at most once */
int buf_put_key(buf_t *out, int key)
{
int i, start = out->len;
@ -1141,16 +1150,13 @@ int buf_put_key(buf_t *out, int key)
buf_puts(out, "M-");
buf_put_key(out, key & 0xff);
} else
if (key >= KEY_META(KEY_ESC1(0)) && key <= KEY_META(KEY_ESC1(0xff))) {
buf_puts(out, "M-");
buf_put_key(out, KEY_ESC1(key & 0xff));
} else
if (key >= KEY_CTRL('a') && key <= KEY_CTRL('z')) {
buf_printf(out, "C-%c", key + 'a' - 1);
} else
#if 0
/* Cannot do this because KEY_F1..KEY_F20 are not consecutive */
if (key >= KEY_F1 && key <= KEY_F20) {
buf_printf(out, "f%d", key - KEY_F1 + 1);
} else
#endif
{
} else {
buf_putc_utf8(out, key);
}
return out->len - start;
@ -2213,6 +2219,30 @@ int strunquote(char *dest, int size, const char *str, int len)
}
#endif
int buf_encode_byte(buf_t *out, int ch) {
int c;
if (((void)(c = 'n'), ch == '\n')
|| ((void)(c = 'r'), ch == '\r')
|| ((void)(c = 't'), ch == '\t')
|| ((void)(c = 'b'), ch == '\010')
|| ((void)(c = 'E'), ch == '\033')
|| ((void)(c = '\\'), ch == '\\')) {
return buf_printf(out, "\\%c", c);
} else
if (ch < 32) {
//if (*p == '\e' && col > 9) {
// eb_write(b, b->total_size, "\n ", 10);
// col = 9;
//}
return buf_printf(out, "\\^%c", (ch + '@') & 127);
} else
if (ch < 127) {
return buf_put_byte(out, ch);
} else {
return buf_printf(out, "\\%03o", ch);
}
}
/*---------------- allocation routines ----------------*/
void *qe_malloc_bytes(size_t size)

View file

@ -30,6 +30,8 @@ const char * const var_domain[] = {
"self", /* VAR_SELF */
};
static int use_full_version = 1;
static VarDef var_table[] = {
S_VAR( "screen-width", width, VAR_NUMBER, VAR_RO,
@ -119,6 +121,7 @@ static VarDef var_table[] = {
G_VAR( "disable-crc", disable_crc, VAR_NUMBER, VAR_RW_SAVE,
"Set to prevent CRC based display cache." )
G_VAR( "use-html", use_html, VAR_NUMBER, VAR_RW, NULL )
G_VAR( "full-version", use_full_version, VAR_NUMBER, VAR_RW, NULL )
/* more buffer fields: modified, readonly, binary, charset */

2
x11.c
View file

@ -1461,6 +1461,8 @@ static void x11_handle_event(void *opaque)
key = KEY_META(buf[0] & 0xff);
goto got_key;
}
// XXX: should support CTRL/SHIFT/META modifiers
// on cursor and function keys
if (keysym >= ' ' && keysym <= '~') {
key = KEY_META(' ') + keysym - ' ';
goto got_key;