-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcmd-buffer.c
89 lines (70 loc) · 1.57 KB
/
cmd-buffer.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
/* cmd_buffer.c -- buffer manipulation commands */
#include <string.h>
#include "edna.h"
static void listbuf_helper(Buffer *, char);
void
listbuf_helper(Buffer *buf, char prefix)
{
bool dirty;
char *fn, *mod="";
size_t len, pos;
fn = bufgetname(buf);
len = bufgetlen(buf);
pos = bufgetpos(buf);
dirty = isdirty(buf);
if (!fn) fn = "";
if (dirty) mod = " [modified]";
if (printf("%c \"%s\"%s, %ld / %ld\n", prefix, fn, mod, pos, len - 1) < 0)
die("printf");
}
int
cmd_listbuf(State *st, Buffer *buf, Arg *arg, char *error)
{
size_t i;
for (i = 0; i < st->buffers->c; ++i)
listbuf_helper(st->buffers->v[i], ' ');
listbuf_helper(buf, '*');
return 0;
}
int
cmd_openbuf(State *st, Buffer *buf, Arg *arg, char *error)
{
Buffer *tmp;
size_t i;
if (!arg->param || !arg->param->c) {
strcpy(error, "no filenames provided");
return -1;
}
i = 0;
do {
tmp = makebuf();
if (!tmp) return ENOMEM;
chomp(arg->param->v[i]);
initbuf(tmp, arg->param->v[i]);
if (readbuf(tmp, error)) {
strcpy(error, "error reading file");
return -1;
}
bufclean(tmp);
addbuf(st, tmp);
} while (++i < len(arg->param));
return 0;
}
int
cmd_switchbuf(State *st, Buffer *buf, Arg *arg, char *error)
{
if (!arg->mode) {
strcpy(error, "no option specified");
return -1;
} else if (!strcmp(arg->mode, "next")) {
returnbuf(st, buf);
checkoutbuf(buf, st, 0);
return 0;
} else if (!strcmp(arg->mode, "prev")) {
returnbuf(st, buf);
checkoutbuf(buf, st, (st->buffers->c != 1) ? st->buffers->c - 2 : 0 );
return 0;
}
strcpy (error, "unknown option");
return -1;
}