aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarc André Tanner <mat@brain-dump.org>2017-02-10 18:44:55 +0100
committerMarc André Tanner <mat@brain-dump.org>2017-02-10 18:44:55 +0100
commit63416763507c87567bbf5dc70b4c68094984fc53 (patch)
treeadff43c3dda43cbb83be0cff166adf4aef61f2ce
parent5a0ffe77fd777c069edea3b75f88b6baf6a4e6f1 (diff)
downloadvis-63416763507c87567bbf5dc70b4c68094984fc53.tar.gz
vis-63416763507c87567bbf5dc70b4c68094984fc53.tar.xz
vis: introduce vis_keys_utf8
-rw-r--r--libutf.h2
-rw-r--r--vis.c9
-rw-r--r--vis.h4
3 files changed, 14 insertions, 1 deletions
diff --git a/libutf.h b/libutf.h
index 4d3f91c..a2b81b7 100644
--- a/libutf.h
+++ b/libutf.h
@@ -23,7 +23,7 @@ typedef unsigned long Rune;
#endif
#endif
-#define UTFmax 6 /* maximum bytes per rune */
+#define UTFmax 4 /* maximum bytes per rune */
#define Runeself 0x80 /* rune and utf are equal (<) */
#define Runemax RUNE_C(0x10FFFF) /* maximum rune value */
diff --git a/vis.c b/vis.c
index 3e64031..58c1a5c 100644
--- a/vis.c
+++ b/vis.c
@@ -962,6 +962,15 @@ long vis_keys_codepoint(Vis *vis, const char *keys) {
return -1;
}
+bool vis_keys_utf8(Vis *vis, const char *keys, char utf8[static UTFmax+1]) {
+ Rune rune = vis_keys_codepoint(vis, keys);
+ if (rune == (Rune)-1)
+ return false;
+ size_t len = runetochar(utf8, &rune);
+ utf8[len] = '\0';
+ return true;
+}
+
static void vis_keys_process(Vis *vis, size_t pos) {
Buffer *buf = vis->keys;
char *keys = buf->data + pos, *start = keys, *cur = keys, *end = keys, *binding_end = keys;;
diff --git a/vis.h b/vis.h
index 5f5ad94..31d6994 100644
--- a/vis.h
+++ b/vis.h
@@ -12,6 +12,7 @@ typedef struct Win Win;
#include "ui.h"
#include "view.h"
#include "text-regex.h"
+#include "libutf.h"
/* simplify utility renames by distribution packagers */
#ifndef VIS_MENU
@@ -503,6 +504,9 @@ int vis_pipe_collect(Vis *vis, Filerange *range, const char *argv[], char **out,
const char *vis_keys_next(Vis*, const char *keys);
/* Tries to convert next symbolic key to a raw code point, returns -1 for unknown keys */
long vis_keys_codepoint(Vis*, const char *keys);
+/* Tries to convert next symbolic key to a UTF-8 sequence. Returns false for unknown keys
+ * and leaves `utf8` untouched. Guarantees that `utf8` is NUL terminated on success */
+bool vis_keys_utf8(Vis*, const char *keys, char utf8[static UTFmax+1]);
/* vis operates as a finite state machine (FSM), feeding keys from an input
* queue (or a previously recorded macro) to key handling functions (see struct
* KeyAction) which consume the input.