Skip to content

Commit

Permalink
vis: introduce vis_keys_utf8
Browse files Browse the repository at this point in the history
  • Loading branch information
martanne committed Feb 10, 2017
1 parent 5a0ffe7 commit 6341676
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 1 deletion.
2 changes: 1 addition & 1 deletion libutf.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
9 changes: 9 additions & 0 deletions vis.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;;
Expand Down
4 changes: 4 additions & 0 deletions vis.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit 6341676

Please sign in to comment.