]> git.neil.brown.name Git - edlib.git/blob - lib-test-markup.c
lib-wiggle: switch to embedded data pattern
[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 #include "core.h"
10 #include "misc.h"
11
12 DEF_CMD(test_render_prev)
13 {
14         struct mark *m = ci->mark;
15
16         if (!m)
17                 return Enoarg;
18         if (ci->num)
19                 if (doc_prev(ci->focus, m) == WEOF)
20                         return Efail;
21         call("doc:EOL", ci->focus, -1, m);
22         return 1;
23 }
24
25 DEF_CMD(test_render_line)
26 {
27         struct mark *m = ci->mark;
28         struct mark *st;
29         char *s;
30         int ret;
31         int pm_offset = -1;
32
33         if (!m)
34                 return Enoarg;
35         st = mark_dup(m);
36         call("doc:EOL", ci->focus, 1, m, NULL, 1);
37         s = call_ret(str, "doc:get-str", ci->focus, 0, st, NULL, 0, m);
38         if (ci->num >= 0) {
39                 struct mark *m2 = mark_dup(st);
40                 call("doc:char", ci->focus, ci->num, m2, NULL, 0, m);
41                 mark_to_mark(m, m2);
42                 mark_free(m2);
43                 if (s && ci->num < (int)utf8_strlen(s))
44                         s[ci->num] = 0;
45         }
46         if (ci->mark2) {
47                 char *s2 = call_ret(str, "doc:get-str", ci->focus,
48                                     0, st, NULL,
49                                     0, ci->mark2);
50                 pm_offset = s2 ? strlen(s2) : 0;
51                 free(s2);
52         }
53         ret = comm_call(ci->comm2, "cb", ci->focus, pm_offset, NULL, s);
54         free(s);
55         return ret ?: 1;
56 }
57
58 static struct map *tmu_map safe;
59 DEF_LOOKUP_CMD(test_markup_handle, tmu_map);
60
61 DEF_CMD(test_attach)
62 {
63         struct pane *ret;
64
65         ret = pane_register(ci->focus, 0, &test_markup_handle.c);
66         if (!ret)
67                 return Efail;
68         return comm_call(ci->comm2, "cb", ret);
69 }
70
71 DEF_CMD(test_enable)
72 {
73         call("attach-test-markup", ci->focus);
74         return 1;
75 }
76
77 void edlib_init(struct pane *ed safe)
78 {
79         tmu_map = key_alloc();
80
81         key_add(tmu_map, "doc:render-line", &test_render_line);
82         key_add(tmu_map, "doc:render-line-prev", &test_render_prev);
83
84         call_comm("global-set-command", ed, &test_attach,
85                   0, NULL, "attach-test-markup");
86         call_comm("global-set-command" ,ed, &test_enable,
87                   0, NULL, "interactive-cmd-test-markup");
88 }