2 * Copyright Neil Brown ©2017-2023 <neil@brown.name>
3 * May be distributed under terms of GPLv2 - see file:COPYING
5 * A viewer pane presents a read-only view on a document
6 * which uses some letter - that would normally self-insert -
18 #define PANE_DATA_TYPE struct viewer_data
24 #include "core-pane.h"
26 static struct map *viewer_map safe;
27 DEF_LOOKUP_CMD(viewer_handle, viewer_map);
29 static struct pane *safe do_viewer_attach(struct pane *par safe)
33 p = pane_register(par, 0, &viewer_handle.c);
35 p->data->active = True;
39 DEF_CMD(viewer_attach)
41 return comm_call(ci->comm2, "callback:attach", do_viewer_attach(ci->focus));
46 struct viewer_data *vd = ci->home->data;
50 call("Message:modal", ci->focus, 0, NULL, "Cannot modify document in viewer mode");
56 /* Send command to the document */
59 struct viewer_data *vd = ci->home->data;
64 if ((s=ksuffix(ci, "K:"))[0] ||
65 (s=ksuffix(ci, "doc:char-"))[0]) {
67 snprintf(cmd, sizeof(cmd), "doc:cmd%s", s-1);
68 ret = call(cmd, ci->focus, ci->num, ci->mark);
71 snprintf(cmd, sizeof(cmd),
72 "Unknown command `%s'", s);
73 call("Message:modal", ci->focus, 0, NULL, cmd);
75 case 2: /* request to move to next line */
76 call("K:Down", ci->focus, ci->num, ci->mark);
78 case 3: /* request to move to previous line */
79 call("K:Up", ci->focus, ci->num, ci->mark);
86 DEF_CMD(viewer_page_down)
88 struct viewer_data *vd = ci->home->data;
92 call("K:Next", ci->focus, ci->num, ci->mark);
96 DEF_CMD(viewer_page_up)
98 struct viewer_data *vd = ci->home->data;
102 call("K:Prior", ci->focus, ci->num, ci->mark);
108 /* First see if doc wants to handle 'q' */
110 struct viewer_data *vd = ci->home->data;
115 ret = call("doc:cmd-q", ci->focus, ci->num, ci->mark);
118 call("Window:bury", ci->focus);
120 case 2: /* request to move to next line */
121 call("K:Down", ci->focus, ci->num, ci->mark);
123 case 3: /* request to move to previous line */
124 call("K:Up", ci->focus, ci->num, ci->mark);
130 DEF_CMD(viewer_deactivate)
132 struct viewer_data *vd = ci->home->data;
140 DEF_CMD(viewer_activate)
142 struct viewer_data *vd = ci->home->data;
148 DEF_CMD(viewer_clone)
151 struct viewer_data *vd = ci->home->data;
154 p = do_viewer_attach(ci->focus);
157 pane_clone_children(ci->home, p);
161 DEF_CMD(viewer_appeared)
163 char *t = pane_attr_get(ci->focus, "doc-type");
164 if (t && strcmp(t, "text") == 0)
165 attr_set_str(&ci->focus->attrs, "view-cmd-V", "viewer");
169 void edlib_init(struct pane *ed safe)
171 viewer_map = key_alloc();
173 key_add(viewer_map, "Replace", &no_replace);
174 key_add_range(viewer_map, "doc:char- ", "doc:char-~", &viewer_cmd);
175 key_add(viewer_map, "K:Enter", &viewer_cmd);
176 key_add(viewer_map, "doc:char- ", &viewer_page_down);
177 key_add(viewer_map, "K:C-H", &viewer_page_up);
178 key_add(viewer_map, "K:Backspace", &viewer_page_up);
179 key_add(viewer_map, "K:Del", &viewer_page_up);
180 key_add(viewer_map, "doc:char-q", &viewer_bury);
181 key_add(viewer_map, "doc:char-E", &viewer_deactivate);
182 key_add(viewer_map, "Clone", &viewer_clone);
183 key_add(viewer_map, "attach-viewer", &viewer_activate);
185 call_comm("global-set-command", ed, &viewer_attach, 0, NULL,
187 call_comm("global-set-command", ed, &viewer_appeared, 0, NULL,
188 "doc:appeared-viewer");
190 /* FIXME this doesn't seem quite right...
191 * The goal is that if 'viewer' is requested of doc:attach-pane,
192 * this pane gets attached, in place of any default.
193 * I'm not sure it should be "in-place", and I feel it should be easier
196 attr_set_str(&ed->attrs, "view-viewer", "viewer");