2 * Copyright Neil Brown ©2015 <neil@brown.name>
3 * May be distributed under terms of GPLv2 - see file:COPYING
7 * 16 bytes are rendered as hex, and then chars
8 * Well... currently we do chars, not bytes, because I cannot control
26 static struct map *he_map;
27 static struct pane *do_render_hex_attach(struct pane *parent);
29 DEF_LOOKUP_CMD(render_hex_handle, he_map);
31 DEF_CMD(render_hex_close)
33 struct pane *p = ci->home;
34 struct he_data *he = p->data;
43 DEF_CMD(render_hex_clone)
45 struct pane *parent = ci->focus;
47 do_render_hex_attach(parent);
48 pane_clone_children(ci->home, parent->focus);
52 DEF_CMD(render_hex_notify_replace)
55 /* If change happens only after the view port, we don't
57 * If before, we might need to update addresses.
58 * However we cannot currently access the view port, so
59 * always signal damage.
60 * This pane_child call is a hack - it may not be the
61 * render-lines that we want.
63 pane_damaged(pane_child(ci->home), DAMAGED_CONTENT);
68 DEF_CMD(render_hex_eol)
71 int rpt = RPT_NUM(ci);
74 call3("CountLines", ci->home, 0, ci->mark);
75 pos = attr_find_int(*mark_attr(ci->mark), "chars");
77 pos = attr_find_int(*mark_attr(ci->mark), "chars");
78 while (rpt > 0 && ch != WEOF) {
79 while ((pos & 15) != 15 &&
80 (ch = mark_next_pane(ci->focus, ci->mark)) != WEOF)
84 ch = mark_next_pane(ci->focus, ci->mark);
88 while (rpt < 0 && ch != WEOF) {
89 while ((pos & 15) != 0 &&
90 (ch = mark_prev_pane(ci->focus, ci->mark)) != WEOF)
94 ch = mark_prev_pane(ci->focus, ci->mark);
104 struct mark *m = NULL;
105 struct mark *pm = ci->mark2;
111 if (!ci->focus || !ci->mark)
114 call3("CountLines", ci->home, 0, ci->mark);
115 pos = attr_find_int(*mark_attr(ci->mark), "chars");
118 if (doc_following_pane(ci->focus, ci->mark) == WEOF)
120 snprintf(buf, sizeof(buf), "<bold>%08x:</> ", pos);
121 buf_concat(&ret, buf);
122 m = mark_dup(ci->mark, 0);
123 for (i = 0; i < 16; i++) {
125 struct mark *m2 = ci->mark;
127 if (pm && mark_same_pane(ci->home, m2, pm, NULL))
129 if (ci->numeric >= 0 && ci->numeric != NO_NUMERIC &&
130 ci->numeric <= ret.len)
133 ch = mark_next_pane(ci->focus, m2);
137 sprintf(buf, "%02x ", ch & 0xff);
138 buf_concat(&ret, buf);
140 buf_append(&ret, ' ');
143 buf_concat(&ret, " <fg:red>");
144 for (i = 0; i < 16; i++) {
147 ch = mark_next_pane(ci->focus, m);
152 buf_append(&ret, ch);
153 buf_append(&ret, ' ');
155 buf_append(&ret, ' ');
157 buf_concat(&ret, "</>\n");
161 rv = comm_call(ci->comm2, "callback:render", ci->focus, 0, NULL,
167 DEF_CMD(render_line_prev)
169 /* If ->numeric is 0, round down to multiple of 16.
170 * if it is 1, subtract a further 16.
174 call3("CountLines", ci->home, 0, ci->mark);
176 from = attr_find_int(*mark_attr(ci->mark), "chars");
178 if (ci->numeric && to >= 16)
181 mark_prev_pane(ci->focus, ci->mark);
187 static void render_hex_register_map(void)
189 he_map = key_alloc();
191 key_add(he_map, "Move-EOL", &render_hex_eol);
193 key_add(he_map, "render-line-prev", &render_line_prev);
194 key_add(he_map, "render-line", &render_line);
196 key_add(he_map, "Close", &render_hex_close);
197 key_add(he_map, "Clone", &render_hex_clone);
198 key_add(he_map, "Notify:Replace", &render_hex_notify_replace);
201 static struct pane *do_render_hex_attach(struct pane *parent)
203 struct he_data *he = malloc(sizeof(*he));
207 render_hex_register_map();
209 p = pane_register(parent, 0, &render_hex_handle.c, he, NULL);
210 call3("Request:Notify:Replace", p, 0, NULL);
211 attr_set_str(&p->attrs, "render-wrap", "no", -1);
212 attr_set_str(&p->attrs, "heading", "<bold> 00 11 22 33 44 55 66 77 88 99 aa bb cc dd ee ff 0 1 2 3 4 5 6 7 8 9 a b c d e f</>", -1);
214 return render_attach("lines", p);
217 DEF_CMD(render_hex_attach)
219 return comm_call(ci->comm2, "callback:attach",
220 do_render_hex_attach(ci->focus),
224 DEF_CMD(hex_appeared)
226 char *t = pane_attr_get(ci->focus, "doc-type");
227 if (t && strcmp(t, "text") == 0)
228 call7("doc:attr-set", ci->focus, 0, NULL, "render-Chr-H", 0,
233 void edlib_init(struct pane *ed)
235 call_comm("global-set-command", ed, 0, NULL, "attach-render-hex",
236 0, &render_hex_attach);
237 call_comm("global-set-command", ed, 0, NULL, "doc:appeared-hex",