]> git.neil.brown.name Git - edlib.git/blob - lib-test-markup.c
TODO: clean out done items.
[edlib.git] / lib-test-markup.c
1 /*
2  * Copyright Neil Brown ©2023 <neil@brown.name>
3  * May be distributed under terms of GPLv2 - see file:COPYING
4  *
5  * This is a replacement for lib-markup which uses each line
6  * of the document as verbatim markup.  This is for testing only.
7  */
8
9 #define PANE_DATA_VOID
10 #include "core.h"
11 #include "misc.h"
12
13 DEF_CMD(test_render_prev)
14 {
15         struct mark *m = ci->mark;
16
17         if (!m)
18                 return Enoarg;
19         if (ci->num)
20                 if (doc_prev(ci->focus, m) == WEOF)
21                         return Efail;
22         call("doc:EOL", ci->focus, -1, m);
23         return 1;
24 }
25
26 DEF_CMD(test_render_line)
27 {
28         struct mark *m = ci->mark;
29         struct mark *st;
30         char *s;
31         int ret;
32         int pm_offset = -1;
33
34         if (!m)
35                 return Enoarg;
36         st = mark_dup(m);
37         call("doc:EOL", ci->focus, 1, m, NULL, 1);
38         s = call_ret(str, "doc:get-str", ci->focus, 0, st, NULL, 0, m);
39         if (ci->num >= 0) {
40                 struct mark *m2 = mark_dup(st);
41                 call("doc:char", ci->focus, ci->num, m2, NULL, 0, m);
42                 mark_to_mark(m, m2);
43                 mark_free(m2);
44                 if (s && ci->num < (int)utf8_strlen(s))
45                         s[ci->num] = 0;
46         }
47         if (ci->mark2) {
48                 char *s2 = call_ret(str, "doc:get-str", ci->focus,
49                                     0, st, NULL,
50                                     0, ci->mark2);
51                 pm_offset = s2 ? strlen(s2) : 0;
52                 free(s2);
53         }
54         ret = comm_call(ci->comm2, "cb", ci->focus, pm_offset, NULL, s);
55         free(s);
56         return ret ?: 1;
57 }
58
59 static struct map *tmu_map safe;
60 DEF_LOOKUP_CMD(test_markup_handle, tmu_map);
61
62 DEF_CMD(test_attach)
63 {
64         struct pane *ret;
65
66         ret = pane_register(ci->focus, 0, &test_markup_handle.c);
67         if (!ret)
68                 return Efail;
69         return comm_call(ci->comm2, "cb", ret);
70 }
71
72 DEF_CMD(test_enable)
73 {
74         call("attach-test-markup", ci->focus);
75         return 1;
76 }
77
78 void edlib_init(struct pane *ed safe)
79 {
80         tmu_map = key_alloc();
81
82         key_add(tmu_map, "doc:render-line", &test_render_line);
83         key_add(tmu_map, "doc:render-line-prev", &test_render_prev);
84
85         call_comm("global-set-command", ed, &test_attach,
86                   0, NULL, "attach-test-markup");
87         call_comm("global-set-command" ,ed, &test_enable,
88                   0, NULL, "interactive-cmd-test-markup");
89 }