-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyank.c
197 lines (162 loc) · 3.49 KB
/
yank.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "pos.h"
#include "region.h"
#include "list.h"
#include "yank.h"
#include "mem.h"
#include "retain.h"
struct yank
{
struct retain;
list_t *list;
enum region_type as;
};
static yank *yank_buf;
yank *yank_new(list_t *l, enum region_type as)
{
yank *y = retain(umalloc(sizeof *y));
assert(l && !l->prev);
y->list = l;
y->as = as;
return y;
}
void yank_free(const yank *y)
{
list_free(y->list);
free((yank *)y);
}
yank *yank_push(yank *y)
{
if(yank_buf)
release(yank_buf, yank_free);
yank_buf = retain(y);
return yank_buf;
}
const yank *yank_top()
{
return yank_buf;
}
static void charwise_put(
const yank *ynk, list_t *head,
int *px, bool prepend)
{
/* first line */
const list_t *ins_iter = ynk->list;
*px += !prepend;
for(unsigned i = 0; i < ins_iter->len_line; i++)
list_inschar(head,
px,
&(int){ 0 },
ins_iter->line[i], /*autogap*/0);
int final_x = *px - 1;
/* if we have multiple lines... */
if((ins_iter = ins_iter->next)){
struct str
{
char *str;
size_t len;
};
/* need to pull out the rest of the line */
struct str append = { .str = NULL };
unsigned off = *px;
if(off < head->len_line){
unsigned len = head->len_line - *px;
append.str = ustrdup_len(head->line + *px, len);
append.len = len;
head->len_line = *px;
}
/* append ins_iter - 2 lines */
{
list_t *copy = list_copy_deep(ins_iter, NULL);
list_t *copy_tail = list_tail(copy);
list_t *after = head->next;
head->next = copy;
copy->prev = head;
copy_tail->next = after;
if(after)
after->prev = copy_tail;
}
if(append.str){
list_t *tail = head;
for(; ins_iter; ins_iter = ins_iter->next)
tail = tail->next;
/* tail is now the last inserted line */
unsigned len = tail->len_line + append.len;
tail->len_malloc = len + 1;
tail->line = urealloc(tail->line, tail->len_malloc);
memcpy(tail->line + tail->len_line,
append.str,
append.len);
tail->len_line += append.len;
}
}
*px = final_x;
}
void yank_put_in_list(
const yank *ynk,
list_t **const phead,
const bool prepend,
int *py, int *px)
{
switch(ynk->as){
case REGION_CHAR:
charwise_put(ynk, list_seek(*phead, *py, true), px, prepend);
break;
case REGION_LINE:
{
list_t *copy = list_copy_deep(ynk->list, NULL);
list_t *copy_tail = list_tail(copy);
if(prepend){
if(*py == 0){
list_t *head = *phead;
copy->prev = NULL;
*phead = copy;
copy_tail->next = head;
if(head)
head->prev = copy_tail;
}else{
list_t *before = list_seek(*phead, *py - 1, true);
list_t *after = before->next;
copy->prev = before;
before->next = copy;
copy_tail->next = after;
if(after)
after->prev = copy_tail;
}
}else{
list_t *const head = *list_seekp(phead, *py, true);
list_t *const after = head->next;
/* top link */
head->next = copy;
copy->prev = head;
/* bottom link */
copy_tail->next = after;
if(after)
after->prev = copy_tail;
++*py;
}
break;
}
case REGION_COL:
{
list_t *head = *list_seekp(phead, *py, true);
assert(head);
int y = 0;
for(const list_t *l = ynk->list;
l; l = l->next, y++)
{
for(unsigned i = 0; i < l->len_line; i++)
list_inschar(head,
&(int){ *px + !prepend + i },
&(int){ y },
l->line[i], /* force spaces to fill in for now */' ');
}
if(!prepend)
++*px;
break;
}
}
}