-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrange.c
103 lines (87 loc) · 1.56 KB
/
range.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
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include "pos.h"
#include "region.h"
#include "list.h"
#include "buffer.h"
#include "buffers.h"
#include "window.h"
#include "windows.h"
#include "range.h"
bool parse_range_1(
char *range, char **end,
int *out)
{
bool handled = true;
switch(*range){
case '0' ... '9':
/* go into 0-based */
*out = strtol(range, &range, 10) - 1;
break;
case '.':
*out = windows_cur()->ui_pos->y;
range++;
break;
case '$':
*out = buffer_nlines(buffers_cur());
range++;
break;
/*case '\'': */
default:
/* don't touch range */
handled = false;
*out = windows_cur()->ui_pos->y;
break;
}
for(;;){
switch(*range){
case '+':
case '-':
{
const int dir = (*range == '+' ? 1 : -1);
range++;
int amt = isdigit(*range)
? strtol(range, &range, 10)
: 1;
*out += dir * amt;
handled = true;
/* read more +/- */
break;
}
default:
*end = range;
return handled;
}
}
}
enum range_parse parse_range(
char *cmd, char **end,
struct range *r)
{
if(*cmd == '%'){
r->start = 0;
r->end = buffer_nlines(buffers_cur());
*end = cmd + 1;
return RANGE_PARSE_PASS;
}
if(!parse_range_1(cmd, end, &r->start))
return RANGE_PARSE_NONE; /* no range */
cmd = *end;
if(*cmd == ','){
cmd++;
if(!parse_range_1(cmd, end, &r->end))
return RANGE_PARSE_FAIL;
}else{
r->end = r->start;
}
return RANGE_PARSE_PASS;
}
void range_sort(struct range *r)
{
if(r->start > r->end){
int tmp = r->start;
r->start = r->end;
r->end = tmp;
}
}