-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathreg.c
73 lines (67 loc) · 1.4 KB
/
reg.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
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "vi.h"
static char *bufs[256];
static int lnmode[256];
static char *reg_getraw(int c, int *ln)
{
if (ln != NULL)
*ln = lnmode[c];
return bufs[c];
}
char *reg_get(int c, int *lnmode)
{
static char ln[1024];
static char linno[16];
static char colno[16];
if (c == '"')
c = 0;
if (c == ';') {
char *s = lbuf_get(xb, xrow);
snprintf(ln, sizeof(ln), "%s", s ? s : "");
if (strchr(ln, '\n') != NULL)
*strchr(ln, '\n') = '\0';
if (lnmode != NULL)
*lnmode = 1;
return ln;
}
if (c == '#') {
snprintf(linno, sizeof(linno), "%d", xrow + 1);
return linno;
}
if (c == '^') {
snprintf(colno, sizeof(colno), "%d", xoff + 1);
return colno;
}
return reg_getraw(c, lnmode);
}
static void reg_putraw(int c, char *s, int ln)
{
char *pre = isupper(c) && bufs[tolower(c)] ? bufs[tolower(c)] : "";
char *buf = malloc(strlen(pre) + strlen(s) + 1);
strcpy(buf, pre);
strcat(buf, s);
free(bufs[tolower(c)]);
bufs[tolower(c)] = buf;
lnmode[tolower(c)] = ln;
}
void reg_put(int c, char *s, int ln)
{
int i, i_ln;
char *i_s;
if ((ln || strchr(s, '\n')) && (!c || isalpha(c))) {
for (i = 8; i > 0; i--)
if ((i_s = reg_get('0' + i, &i_ln)))
reg_putraw('0' + i + 1, i_s, i_ln);
reg_putraw('1', s, ln);
}
reg_putraw(c, s, ln);
}
void reg_done(void)
{
int i;
for (i = 0; i < LEN(bufs); i++)
free(bufs[i]);
}