2019-07-27 22:00:01 +00:00
|
|
|
/* Copyright (C) 2018 C. McEnroe <june@causal.agency>
|
2018-08-08 20:59:26 +00:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero 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 Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <err.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <sysexits.h>
|
|
|
|
#include <wchar.h>
|
2018-12-16 22:19:06 +00:00
|
|
|
#include <wctype.h>
|
2018-08-08 20:59:26 +00:00
|
|
|
|
|
|
|
#include "chat.h"
|
|
|
|
|
2018-09-02 20:13:00 +00:00
|
|
|
enum { BufLen = 512 };
|
2018-08-08 20:59:26 +00:00
|
|
|
static struct {
|
2018-09-02 20:13:00 +00:00
|
|
|
wchar_t buf[BufLen];
|
2018-08-08 20:59:26 +00:00
|
|
|
wchar_t *ptr;
|
|
|
|
wchar_t *end;
|
|
|
|
wchar_t *tab;
|
|
|
|
} line = {
|
|
|
|
.ptr = line.buf,
|
|
|
|
.end = line.buf,
|
|
|
|
};
|
|
|
|
|
|
|
|
const wchar_t *editHead(void) {
|
|
|
|
return line.buf;
|
|
|
|
}
|
|
|
|
const wchar_t *editTail(void) {
|
|
|
|
return line.ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void left(void) {
|
|
|
|
if (line.ptr > line.buf) line.ptr--;
|
|
|
|
}
|
|
|
|
static void right(void) {
|
|
|
|
if (line.ptr < line.end) line.ptr++;
|
|
|
|
}
|
|
|
|
|
2018-08-11 03:31:20 +00:00
|
|
|
static void backWord(void) {
|
|
|
|
left();
|
2018-08-20 22:41:23 +00:00
|
|
|
wchar_t *word = wcsnrchr(line.buf, line.ptr - line.buf, L' ');
|
2018-08-11 03:31:20 +00:00
|
|
|
line.ptr = (word ? &word[1] : line.buf);
|
2018-08-08 20:59:26 +00:00
|
|
|
}
|
2018-08-11 03:31:20 +00:00
|
|
|
static void foreWord(void) {
|
2018-08-08 20:59:26 +00:00
|
|
|
right();
|
2018-08-20 22:41:23 +00:00
|
|
|
wchar_t *word = wcsnchr(line.ptr, line.end - line.ptr, L' ');
|
2018-08-11 03:31:20 +00:00
|
|
|
line.ptr = (word ? word : line.end);
|
2018-08-08 20:59:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void insert(wchar_t ch) {
|
2018-09-02 20:13:00 +00:00
|
|
|
if (line.end == &line.buf[BufLen - 1]) return;
|
2018-08-08 20:59:26 +00:00
|
|
|
if (line.ptr != line.end) {
|
|
|
|
wmemmove(line.ptr + 1, line.ptr, line.end - line.ptr);
|
|
|
|
}
|
|
|
|
*line.ptr++ = ch;
|
|
|
|
line.end++;
|
|
|
|
}
|
2018-08-11 03:31:20 +00:00
|
|
|
static void backspace(void) {
|
|
|
|
if (line.ptr == line.buf) return;
|
|
|
|
if (line.ptr != line.end) {
|
|
|
|
wmemmove(line.ptr - 1, line.ptr, line.end - line.ptr);
|
|
|
|
}
|
|
|
|
line.ptr--;
|
|
|
|
line.end--;
|
2018-08-09 23:15:12 +00:00
|
|
|
}
|
2018-08-11 03:31:20 +00:00
|
|
|
static void delete(void) {
|
|
|
|
if (line.ptr == line.end) return;
|
2018-08-09 23:15:12 +00:00
|
|
|
right();
|
2018-08-11 03:31:20 +00:00
|
|
|
backspace();
|
2018-08-09 23:15:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void killBackWord(void) {
|
|
|
|
wchar_t *from = line.ptr;
|
|
|
|
backWord();
|
|
|
|
wmemmove(line.ptr, from, line.end - from);
|
|
|
|
line.end -= from - line.ptr;
|
|
|
|
}
|
|
|
|
static void killForeWord(void) {
|
|
|
|
wchar_t *from = line.ptr;
|
|
|
|
foreWord();
|
|
|
|
wmemmove(from, line.ptr, line.end - line.ptr);
|
|
|
|
line.end -= line.ptr - from;
|
|
|
|
line.ptr = from;
|
|
|
|
}
|
|
|
|
|
2018-08-08 20:59:26 +00:00
|
|
|
static char *prefix;
|
2018-08-11 03:31:20 +00:00
|
|
|
static void complete(struct Tag tag) {
|
2018-08-08 20:59:26 +00:00
|
|
|
if (!line.tab) {
|
2018-08-20 22:41:23 +00:00
|
|
|
line.tab = wcsnrchr(line.buf, line.ptr - line.buf, L' ');
|
2018-08-08 20:59:26 +00:00
|
|
|
line.tab = (line.tab ? &line.tab[1] : line.buf);
|
2018-08-20 22:41:23 +00:00
|
|
|
prefix = awcsntombs(line.tab, line.ptr - line.tab);
|
2018-08-08 20:59:26 +00:00
|
|
|
if (!prefix) err(EX_DATAERR, "awcstombs");
|
|
|
|
}
|
|
|
|
|
2018-08-11 03:31:20 +00:00
|
|
|
const char *next = tabNext(tag, prefix);
|
2018-08-08 20:59:26 +00:00
|
|
|
if (!next) return;
|
|
|
|
|
|
|
|
wchar_t *wcs = ambstowcs(next);
|
|
|
|
if (!wcs) err(EX_DATAERR, "ambstowcs");
|
|
|
|
|
|
|
|
size_t i = 0;
|
|
|
|
for (; wcs[i] && line.ptr > &line.tab[i]; ++i) {
|
|
|
|
line.tab[i] = wcs[i];
|
|
|
|
}
|
|
|
|
while (line.ptr > &line.tab[i]) {
|
|
|
|
backspace();
|
|
|
|
}
|
|
|
|
for (; wcs[i]; ++i) {
|
|
|
|
insert(wcs[i]);
|
|
|
|
}
|
|
|
|
free(wcs);
|
|
|
|
|
|
|
|
size_t pos = line.tab - line.buf;
|
2018-08-09 01:48:30 +00:00
|
|
|
if (!pos && line.tab[0] != L'/') {
|
2018-08-08 20:59:26 +00:00
|
|
|
insert(L':');
|
|
|
|
} else if (pos >= 2) {
|
2018-09-13 05:50:46 +00:00
|
|
|
if (line.buf[pos - 2] == L':') {
|
2018-08-08 20:59:26 +00:00
|
|
|
line.buf[pos - 2] = L',';
|
|
|
|
insert(L':');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
insert(L' ');
|
|
|
|
}
|
|
|
|
|
|
|
|
static void accept(void) {
|
|
|
|
if (!line.tab) return;
|
|
|
|
line.tab = NULL;
|
|
|
|
free(prefix);
|
|
|
|
tabAccept();
|
|
|
|
}
|
|
|
|
static void reject(void) {
|
|
|
|
if (!line.tab) return;
|
|
|
|
line.tab = NULL;
|
|
|
|
free(prefix);
|
|
|
|
tabReject();
|
|
|
|
}
|
|
|
|
|
2018-08-11 03:31:20 +00:00
|
|
|
static void enter(struct Tag tag) {
|
|
|
|
if (line.end == line.buf) return;
|
2018-08-20 22:41:23 +00:00
|
|
|
*line.end = L'\0';
|
2018-08-11 03:31:20 +00:00
|
|
|
char *str = awcstombs(line.buf);
|
|
|
|
if (!str) err(EX_DATAERR, "awcstombs");
|
|
|
|
input(tag, str);
|
|
|
|
free(str);
|
|
|
|
line.ptr = line.buf;
|
|
|
|
line.end = line.buf;
|
|
|
|
}
|
2018-08-09 23:15:12 +00:00
|
|
|
|
2018-08-11 03:31:20 +00:00
|
|
|
void edit(struct Tag tag, enum Edit op, wchar_t ch) {
|
|
|
|
switch (op) {
|
2018-09-02 20:13:00 +00:00
|
|
|
break; case EditLeft: reject(); left();
|
|
|
|
break; case EditRight: reject(); right();
|
|
|
|
break; case EditHome: reject(); line.ptr = line.buf;
|
|
|
|
break; case EditEnd: reject(); line.ptr = line.end;
|
2018-08-11 03:31:20 +00:00
|
|
|
|
2018-09-02 20:13:00 +00:00
|
|
|
break; case EditBackWord: reject(); backWord();
|
|
|
|
break; case EditForeWord: reject(); foreWord();
|
2018-08-11 03:31:20 +00:00
|
|
|
|
2018-09-02 20:13:00 +00:00
|
|
|
break; case EditInsert: accept(); insert(ch);
|
|
|
|
break; case EditBackspace: reject(); backspace();
|
|
|
|
break; case EditDelete: reject(); delete();
|
2018-08-11 03:31:20 +00:00
|
|
|
|
2019-10-03 18:40:23 +00:00
|
|
|
break; case EditKill: reject(); line.ptr = line.end = line.buf;
|
2018-09-02 20:13:00 +00:00
|
|
|
break; case EditKillBackWord: reject(); killBackWord();
|
|
|
|
break; case EditKillForeWord: reject(); killForeWord();
|
2019-10-03 18:40:23 +00:00
|
|
|
break; case EditKillEnd: reject(); line.end = line.ptr;
|
2018-08-11 03:31:20 +00:00
|
|
|
|
2018-09-02 20:13:00 +00:00
|
|
|
break; case EditComplete: complete(tag);
|
2018-08-11 03:31:20 +00:00
|
|
|
|
2018-09-02 20:13:00 +00:00
|
|
|
break; case EditEnter: accept(); enter(tag);
|
2018-08-08 20:59:26 +00:00
|
|
|
}
|
2018-09-13 02:55:02 +00:00
|
|
|
|
|
|
|
*line.end = L'\0';
|
2018-08-08 20:59:26 +00:00
|
|
|
}
|