forked from martanne/vis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
register.c
104 lines (92 loc) · 2.62 KB
/
register.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
#include <stdlib.h>
#include <string.h>
#include "vis-core.h"
#include "text.h"
#include "util.h"
#include "register.h"
static ssize_t read_buffer(void *context, char *data, size_t len) {
buffer_append(context, data, len);
return len;
}
void register_release(Register *reg) {
buffer_release(®->buf);
}
const char *register_get(Vis *vis, Register *reg, size_t *len) {
switch (reg->type) {
case REGISTER_NORMAL:
if (reg->buf.len > 0 && reg->buf.data[reg->buf.len-1] != '\0')
buffer_append(®->buf, "\0", 1);
if (len)
*len = reg->buf.len > 0 ? reg->buf.len - 1 : 0;
return reg->buf.data;
case REGISTER_CLIPBOARD:
{
Buffer buferr;
buffer_init(&buferr);
buffer_clear(®->buf);
int status = vis_pipe(vis, vis->win->file,
&(Filerange){ .start = 0, .end = 0 },
(const char*[]){ VIS_CLIPBOARD, "--paste", NULL },
®->buf, read_buffer, &buferr, read_buffer);
if (status != 0)
vis_info_show(vis, "Command failed %s", buffer_content0(&buferr));
*len = reg->buf.len;
return reg->buf.data;
}
case REGISTER_BLACKHOLE:
default:
*len = 0;
return NULL;
}
}
bool register_put(Vis *vis, Register *reg, const char *data, size_t len) {
return reg->type == REGISTER_NORMAL && buffer_put(®->buf, data, len);
}
bool register_put0(Vis *vis, Register *reg, const char *data) {
return register_put(vis, reg, data, strlen(data)+1);
}
bool register_put_range(Vis *vis, Register *reg, Text *txt, Filerange *range) {
if (reg->append)
return register_append_range(reg, txt, range);
switch (reg->type) {
case REGISTER_NORMAL:
{
size_t len = text_range_size(range);
if (len == SIZE_MAX || !buffer_reserve(®->buf, len+1))
return false;
reg->buf.len = text_bytes_get(txt, range->start, len, reg->buf.data);
return buffer_append(®->buf, "\0", 1);
}
case REGISTER_CLIPBOARD:
{
Buffer buferr;
buffer_init(&buferr);
int status = vis_pipe(vis, vis->win->file, range,
(const char*[]){ VIS_CLIPBOARD, "--copy", NULL },
NULL, NULL, &buferr, read_buffer);
if (status != 0)
vis_info_show(vis, "Command failed %s", buffer_content0(&buferr));
return status == 0;
}
case REGISTER_BLACKHOLE:
return true;
default:
return false;
}
}
bool register_append_range(Register *reg, Text *txt, Filerange *range) {
switch (reg->type) {
case REGISTER_NORMAL:
{
size_t len = text_range_size(range);
if (!buffer_grow(®->buf, len+1))
return false;
if (reg->buf.len > 0 && reg->buf.data[reg->buf.len-1] == '\0')
reg->buf.len--;
reg->buf.len += text_bytes_get(txt, range->start, len, reg->buf.data + reg->buf.len);
return buffer_append(®->buf, "\0", 1);
}
default:
return false;
}
}