From c4d675a2c600aaceb5f11644fdac8692f048ff22 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20Andr=C3=A9=20Tanner?= Date: Mon, 23 Nov 2015 14:07:29 +0100 Subject: vis: implement ga --- config.def.h | 1 + main.c | 31 +++++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/config.def.h b/config.def.h index 28a2fcd..5ccce5b 100644 --- a/config.def.h +++ b/config.def.h @@ -264,6 +264,7 @@ static KeyBinding vis_mode_normal[] = { { "gv", ACTION(SELECTION_RESTORE) }, { "m", ACTION(MARK_SET) }, { "", ALIAS(":help") }, + { "ga", ACTION(UNICODE_INFO) }, { /* empty last element, array terminator */ }, }; diff --git a/main.c b/main.c index 233b763..04f12da 100644 --- a/main.c +++ b/main.c @@ -104,6 +104,8 @@ static const char *wslide(Vis*, const char *keys, const Arg *arg); static const char *call(Vis*, const char *keys, const Arg *arg); /* call window function as indicated by arg->w */ static const char *window(Vis*, const char *keys, const Arg *arg); +/* show info about Unicode character at cursor position */ +static const char *unicode_info(Vis*, const char *keys, const Arg *arg); enum { VIS_ACTION_EDITOR_SUSPEND, @@ -261,6 +263,7 @@ enum { VIS_ACTION_TEXT_OBJECT_LINE_INNER, VIS_ACTION_MOTION_CHARWISE, VIS_ACTION_MOTION_LINEWISE, + VIS_ACTION_UNICODE_INFO, VIS_ACTION_NOP, }; @@ -1040,6 +1043,11 @@ static KeyAction vis_action[] = { "Force motion to be linewise", motiontype, { .i = VIS_MOTIONTYPE_LINEWISE } }, + [VIS_ACTION_UNICODE_INFO] = { + "unicode-info", + "Show Unicode codepoint(s) of character under cursor", + unicode_info, + }, [VIS_ACTION_NOP] = { "nop", "Ignore key, do nothing", @@ -1547,6 +1555,29 @@ static const char *insertmode(Vis *vis, const char *keys, const Arg *arg) { return keys; } +static const char *unicode_info(Vis *vis, const char *keys, const Arg *arg) { + View *view = vis_view(vis); + Text *txt = vis_text(vis); + size_t start = view_cursor_get(view); + size_t end = text_char_next(txt, start); + char data[end-start], *data_cur = data; + text_bytes_get(txt, start, end - start, data); + Iterator it = text_iterator_get(txt, start); + char info[255] = "", *info_cur = info; + for (size_t pos = start; it.pos < end; pos = it.pos) { + text_iterator_codepoint_next(&it, NULL); + size_t len = it.pos - pos; + wchar_t wc = 0xFFFD; + mbtowc(&wc, data_cur, len); + int width = wcwidth(wc); + info_cur += snprintf(info_cur, sizeof(info) - (info_cur - info) - 1, + "<%s%.*s> U+%04x ", width == 0 ? " " : "", len, data_cur, wc); + data_cur += len; + } + vis_info_show(vis, "%s", info); + return keys; +} + static Vis *vis; static KeyBinding *default_bindings[] = { -- cgit v1.2.3