-
Notifications
You must be signed in to change notification settings - Fork 18
/
adjust.c
64 lines (58 loc) · 1.2 KB
/
adjust.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
/* adjust.c - handle the # command */
#include <string.h>
#include "s.h"
#define LENGTH 78
extern void b_getcur(), b_getmark(), b_gets(), b_delete(), b_setcur();
extern int b_insert();
void adjust(n)
int n; /* line length */
{
char *b, *p;
int start, end, temp, remaining, put_next, get_next, pos, len;
char buf[1000];
if (n == 0)
n = LENGTH;
b_getcur(&start, &pos);
end = 0;
b_getmark(&end, &pos);
if (end == 0) {
UNKNOWN;
return;
}
if (start > end) {
temp = start;
start = end;
end = temp;
}
*buf = '\0';
put_next = get_next = start;
for (remaining = end - start + 1; remaining > 0; ) {
while (remaining > 0 && (len = strlen(buf)) < n) {
if (len > 0 && buf[len-1] != ' ') {
/* add spacer */
buf[len++] = ' ';
buf[len] = '\0';
}
b_gets(get_next, buf + len);
b_delete(get_next, get_next);
--remaining;
}
while ((int)strlen(buf) >= n) {
for (b = buf + n; b > buf && *b != ' '; --b)
;
if (b == buf) {
b_insert(put_next++, buf);
++get_next;
break;
}
*b = '\0';
b_insert(put_next++, buf);
++get_next;
for (p = buf, ++b; (*p++ = *b++) != '\0'; )
;
}
}
if (*buf != '\0')
b_insert(put_next, buf);
b_setcur(start, 0);
}