From: NeilBrown Date: Sat, 19 Aug 2023 22:14:06 +0000 (+1000) Subject: add lib-test-markup X-Git-Url: http://git.neil.brown.name/?a=commitdiff_plain;h=6546e4db37c3a8b10650fdeba43718715d5259c7;p=edlib.git add lib-test-markup lib-test-markup allows arbitrary markup to be provided to lib-renderline for testing. When enabled with "test-markup", each line of the document is treated as verbatim markup Signed-off-by: NeilBrown --- diff --git a/Makefile b/Makefile index a794a2ee..01ac19b2 100644 --- a/Makefile +++ b/Makefile @@ -87,6 +87,7 @@ SHOBJ = O/doc-text.o O/doc-dir.o O/doc-docs.o \ O/lib-x11selection-xcb.o O/display-x11-xcb.o \ O/lib-linefilter.o O/lib-wiggle.o O/lib-aspell.o O/lib-calc.o \ O/lib-menu.o O/lib-unicode-names.o O/lib-askpass.o \ + O/lib-test-markup.o \ O/lang-python.o \ O/mode-emacs.o O/emacs-search.o \ O/display-ncurses.o diff --git a/lib-test-markup.c b/lib-test-markup.c new file mode 100644 index 00000000..25a870d1 --- /dev/null +++ b/lib-test-markup.c @@ -0,0 +1,88 @@ +/* + * Copyright Neil Brown ©2023 + * May be distributed under terms of GPLv2 - see file:COPYING + * + * This is a replacement for lib-markup which uses each line + * of the document as verbatim markup. This is for testing only. + */ + +#include "core.h" +#include "misc.h" + +DEF_CMD(test_render_prev) +{ + struct mark *m = ci->mark; + + if (!m) + return Enoarg; + if (ci->num) + if (doc_prev(ci->focus, m) == WEOF) + return Efail; + call("doc:EOL", ci->focus, -1, m); + return 1; +} + +DEF_CMD(test_render_line) +{ + struct mark *m = ci->mark; + struct mark *st; + char *s; + int ret; + int pm_offset = -1; + + if (!m) + return Enoarg; + st = mark_dup(m); + call("doc:EOL", ci->focus, 1, m, NULL, 1); + s = call_ret(str, "doc:get-str", ci->focus, 0, st, NULL, 0, m); + if (ci->num >= 0) { + struct mark *m2 = mark_dup(st); + call("doc:char", ci->focus, ci->num, m2, NULL, 0, m); + mark_to_mark(m, m2); + mark_free(m2); + if (s && ci->num < (int)utf8_strlen(s)) + s[ci->num] = 0; + } + if (ci->mark2) { + char *s2 = call_ret(str, "doc:get-str", ci->focus, + 0, st, NULL, + 0, ci->mark2); + pm_offset = s2 ? strlen(s2) : 0; + free(s2); + } + ret = comm_call(ci->comm2, "cb", ci->focus, pm_offset, NULL, s); + free(s); + return ret ?: 1; +} + +static struct map *tmu_map safe; +DEF_LOOKUP_CMD(test_markup_handle, tmu_map); + +DEF_CMD(test_attach) +{ + struct pane *ret; + + ret = pane_register(ci->focus, 0, &test_markup_handle.c); + if (!ret) + return Efail; + return comm_call(ci->comm2, "cb", ret); +} + +DEF_CMD(test_enable) +{ + call("attach-test-markup", ci->focus); + return 1; +} + +void edlib_init(struct pane *ed safe) +{ + tmu_map = key_alloc(); + + key_add(tmu_map, "doc:render-line", &test_render_line); + key_add(tmu_map, "doc:render-line-prev", &test_render_prev); + + call_comm("global-set-command", ed, &test_attach, + 0, NULL, "attach-test-markup"); + call_comm("global-set-command" ,ed, &test_enable, + 0, NULL, "interactive-cmd-test-markup"); +} diff --git a/modules.ini b/modules.ini index e6246536..ec3b800a 100644 --- a/modules.ini +++ b/modules.ini @@ -156,3 +156,8 @@ lib-shellcmd = attach-shellcmd lib-unicode-names = Unicode-names lib-menu = attach-menu lib-askpass = AskPass + +lib-test-markup = + attach-test-markup + interactive-cmd-test-markup +