]> git.neil.brown.name Git - edlib.git/commitdiff
add lib-test-markup
authorNeilBrown <neil@brown.name>
Sat, 19 Aug 2023 22:14:06 +0000 (08:14 +1000)
committerNeilBrown <neil@brown.name>
Sun, 20 Aug 2023 22:46:11 +0000 (08:46 +1000)
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 <neil@brown.name>
Makefile
lib-test-markup.c [new file with mode: 0644]
modules.ini

index a794a2eefb51d20cf1af9d198419ab9ac24dd597..01ac19b232ad98a6da1339ef02836d8ab5d19ca4 100644 (file)
--- 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 (file)
index 0000000..25a870d
--- /dev/null
@@ -0,0 +1,88 @@
+/*
+ * Copyright Neil Brown ©2023 <neil@brown.name>
+ * 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");
+}
index e624653600083227de31bd4e03e32e1c7dca6fd1..ec3b800a53d76550280a58ed17cc714effed9cd5 100644 (file)
@@ -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
+