-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib-viewer.c
197 lines (168 loc) · 4.39 KB
/
lib-viewer.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/*
* Copyright Neil Brown ©2017-2023 <[email protected]>
* May be distributed under terms of GPLv2 - see file:COPYING
*
* A viewer pane presents a read-only view on a document
* which uses some letter - that would normally self-insert -
* to move around.
* Particularly:
* SPACE : page down
* BACKSPACE: page up
* q : bury-document
*/
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#define PANE_DATA_TYPE struct viewer_data
#include "core.h"
struct viewer_data {
bool active;
};
#include "core-pane.h"
static struct map *viewer_map safe;
DEF_LOOKUP_CMD(viewer_handle, viewer_map);
static struct pane *safe do_viewer_attach(struct pane *par safe)
{
struct pane *p;
p = pane_register(par, 0, &viewer_handle.c);
if (p)
p->data->active = True;
return p;
}
DEF_CMD(viewer_attach)
{
return comm_call(ci->comm2, "callback:attach", do_viewer_attach(ci->focus));
}
DEF_CMD(no_replace)
{
struct viewer_data *vd = ci->home->data;
if (!vd->active)
return Efallthrough;
call("Message:modal", ci->focus, 0, NULL, "Cannot modify document in viewer mode");
return 1;
}
DEF_CMD(viewer_cmd)
{
/* Send command to the document */
char cmd[40];
const char *s;
struct viewer_data *vd = ci->home->data;
if (!vd->active)
return Efallthrough;
if ((s=ksuffix(ci, "K:"))[0] ||
(s=ksuffix(ci, "doc:char-"))[0]) {
int ret;
snprintf(cmd, sizeof(cmd), "doc:cmd%s", s-1);
ret = call(cmd, ci->focus, ci->num, ci->mark);
switch(ret) {
case 0:
snprintf(cmd, sizeof(cmd),
"Unknown command `%s'", s);
call("Message:modal", ci->focus, 0, NULL, cmd);
break;
case 2: /* request to move to next line */
call("K:Down", ci->focus, ci->num, ci->mark);
break;
case 3: /* request to move to previous line */
call("K:Up", ci->focus, ci->num, ci->mark);
break;
}
}
return 1;
}
DEF_CMD(viewer_page_down)
{
struct viewer_data *vd = ci->home->data;
if (!vd->active)
return Efallthrough;
call("K:Next", ci->focus, ci->num, ci->mark);
return 1;
}
DEF_CMD(viewer_page_up)
{
struct viewer_data *vd = ci->home->data;
if (!vd->active)
return Efallthrough;
call("K:Prior", ci->focus, ci->num, ci->mark);
return 1;
}
DEF_CMD(viewer_bury)
{
/* First see if doc wants to handle 'q' */
int ret;
struct viewer_data *vd = ci->home->data;
if (!vd->active)
return Efallthrough;
ret = call("doc:cmd-q", ci->focus, ci->num, ci->mark);
switch (ret) {
case 0:
call("Tile:bury", ci->focus);
break;
case 2: /* request to move to next line */
call("K:Down", ci->focus, ci->num, ci->mark);
break;
case 3: /* request to move to previous line */
call("K:Up", ci->focus, ci->num, ci->mark);
break;
}
return 1;
}
DEF_CMD(viewer_deactivate)
{
struct viewer_data *vd = ci->home->data;
if (!vd->active)
return Efallthrough;
vd->active = False;
return 1;
}
DEF_CMD(viewer_activate)
{
struct viewer_data *vd = ci->home->data;
vd->active = True;
return 1;
}
DEF_CMD(viewer_clone)
{
struct pane *p;
struct viewer_data *vd = ci->home->data;
if (vd->active)
p = do_viewer_attach(ci->focus);
else
p = ci->focus;
pane_clone_children(ci->home, p);
return 1;
}
DEF_CMD(viewer_appeared)
{
char *t = pane_attr_get(ci->focus, "doc-type");
if (t && strcmp(t, "text") == 0)
attr_set_str(&ci->focus->attrs, "view-cmd-V", "viewer");
return Efallthrough;
}
void edlib_init(struct pane *ed safe)
{
viewer_map = key_alloc();
key_add(viewer_map, "Replace", &no_replace);
key_add_range(viewer_map, "doc:char- ", "doc:char-~", &viewer_cmd);
key_add(viewer_map, "K:Enter", &viewer_cmd);
key_add(viewer_map, "doc:char- ", &viewer_page_down);
key_add(viewer_map, "K:C-H", &viewer_page_up);
key_add(viewer_map, "K:Backspace", &viewer_page_up);
key_add(viewer_map, "K:Del", &viewer_page_up);
key_add(viewer_map, "doc:char-q", &viewer_bury);
key_add(viewer_map, "doc:char-E", &viewer_deactivate);
key_add(viewer_map, "Clone", &viewer_clone);
key_add(viewer_map, "attach-viewer", &viewer_activate);
call_comm("global-set-command", ed, &viewer_attach, 0, NULL,
"attach-viewer");
call_comm("global-set-command", ed, &viewer_appeared, 0, NULL,
"doc:appeared-viewer");
/* FIXME this doesn't seem quite right...
* The goal is that if 'viewer' is requested of doc:attach-pane,
* this pane gets attached, in place of any default.
* I'm not sure it should be "in-place", and I feel it should be easier
* to over-ride..
*/
attr_set_str(&ed->attrs, "view-viewer", "viewer");
}