5 int lbuf_indents(struct lbuf
*lb
, int r
)
7 char *ln
= lbuf_get(lb
, r
);
11 for (o
= 0; uc_isspace(ln
); o
++)
16 static int uc_nextdir(char **s
, char *beg
, int dir
)
21 *s
= uc_prev(beg
, *s
);
30 int lbuf_findchar(struct lbuf
*lb
, char *cs
, int cmd
, int n
, int *row
, int *off
)
32 char *ln
= lbuf_get(lb
, *row
);
34 int dir
= (cmd
== 'f' || cmd
== 't') ? +1 : -1;
42 while (n
> 0 && !uc_nextdir(&s
, ln
, dir
))
43 if (uc_code(s
) == uc_code(cs
))
45 if (!n
&& (cmd
== 't' || cmd
== 'T'))
46 uc_nextdir(&s
, ln
, -dir
);
48 *off
= uc_off(ln
, s
- ln
);
52 int lbuf_search(struct lbuf
*lb
, char *kw
, int dir
, int *r
, int *o
, int *len
)
58 struct rstr
*re
= rstr_make(kw
, xic
? RE_ICASE
: 0);
61 for (i
= r0
; !found
&& i
>= 0 && i
< lbuf_len(lb
); i
+= dir
) {
62 char *s
= lbuf_get(lb
, i
);
63 int off
= dir
> 0 && r0
== i
? uc_chr(s
, o0
+ 1) - s
: 0;
64 while (rstr_find(re
, s
+ off
, 1, offs
,
65 off
? RE_NOTBOL
: 0) >= 0) {
66 if (dir
< 0 && r0
== i
&&
67 uc_off(s
, off
+ offs
[0]) >= o0
)
70 *o
= uc_off(s
, off
+ offs
[0]);
72 *len
= uc_off(s
+ off
+ offs
[0], offs
[1] - offs
[0]);
73 off
+= offs
[1] > offs
[0] ? offs
[1] : offs
[1] + 1;
74 if (dir
> 0 || !s
[off
] || s
[off
] == '\n')
82 int lbuf_paragraphbeg(struct lbuf
*lb
, int dir
, int *row
, int *off
)
84 while (*row
>= 0 && *row
< lbuf_len(lb
) && !strcmp("\n", lbuf_get(lb
, *row
)))
86 while (*row
>= 0 && *row
< lbuf_len(lb
) && strcmp("\n", lbuf_get(lb
, *row
)))
88 *row
= MAX(0, MIN(*row
, lbuf_len(lb
) - 1));
93 int lbuf_sectionbeg(struct lbuf
*lb
, int dir
, char *sec
, int *row
, int *off
)
95 struct rstr
*re
= rstr_make(sec
, 0);
97 while (*row
>= 0 && *row
< lbuf_len(lb
)) {
98 if (rstr_find(re
, lbuf_get(lb
, *row
), 0, NULL
, 0) >= 0)
103 *row
= MAX(0, MIN(*row
, lbuf_len(lb
) - 1));
108 static int lbuf_lnnext(struct lbuf
*lb
, int dir
, int *r
, int *o
)
111 if (off
< 0 || !lbuf_get(lb
, *r
) || off
>= uc_slen(lbuf_get(lb
, *r
)))
117 int lbuf_eol(struct lbuf
*lb
, int row
)
119 int len
= lbuf_get(lb
, row
) ? uc_slen(lbuf_get(lb
, row
)) : 0;
120 return len
? len
- 1 : 0;
123 static int lbuf_next(struct lbuf
*lb
, int dir
, int *r
, int *o
)
125 if (dir
< 0 && *r
>= lbuf_len(lb
))
126 *r
= MAX(0, lbuf_len(lb
) - 1);
127 if (lbuf_lnnext(lb
, dir
, r
, o
)) {
128 if (!lbuf_get(lb
, *r
+ dir
))
131 *o
= dir
> 0 ? 0 : lbuf_eol(lb
, *r
);
137 /* return a pointer to the character at visual position c of line r */
138 static char *lbuf_chr(struct lbuf
*lb
, int r
, int c
)
140 char *ln
= lbuf_get(lb
, r
);
141 return ln
? uc_chr(ln
, c
) : "";
144 /* move to the last character of the word */
145 static int lbuf_wordlast(struct lbuf
*lb
, int kind
, int dir
, int *row
, int *off
)
147 if (!kind
|| !(uc_kind(lbuf_chr(lb
, *row
, *off
)) & kind
))
149 while (uc_kind(lbuf_chr(lb
, *row
, *off
)) & kind
)
150 if (lbuf_next(lb
, dir
, row
, off
))
152 if (!(uc_kind(lbuf_chr(lb
, *row
, *off
)) & kind
))
153 lbuf_next(lb
, -dir
, row
, off
);
157 int lbuf_wordbeg(struct lbuf
*lb
, int big
, int dir
, int *row
, int *off
)
160 lbuf_wordlast(lb
, big
? 3 : uc_kind(lbuf_chr(lb
, *row
, *off
)), dir
, row
, off
);
161 nl
= uc_code(lbuf_chr(lb
, *row
, *off
)) == '\n';
162 if (lbuf_next(lb
, dir
, row
, off
))
164 while (uc_isspace(lbuf_chr(lb
, *row
, *off
))) {
165 nl
+= uc_code(lbuf_chr(lb
, *row
, *off
)) == '\n';
168 if (lbuf_next(lb
, dir
, row
, off
))
174 int lbuf_wordend(struct lbuf
*lb
, int big
, int dir
, int *row
, int *off
)
177 if (!uc_isspace(lbuf_chr(lb
, *row
, *off
))) {
178 if (lbuf_next(lb
, dir
, row
, off
))
180 nl
= dir
< 0 && uc_code(lbuf_chr(lb
, *row
, *off
)) == '\n';
182 nl
+= dir
> 0 && uc_code(lbuf_chr(lb
, *row
, *off
)) == '\n';
183 while (uc_isspace(lbuf_chr(lb
, *row
, *off
))) {
184 if (lbuf_next(lb
, dir
, row
, off
))
186 nl
+= uc_code(lbuf_chr(lb
, *row
, *off
)) == '\n';
189 lbuf_next(lb
, -dir
, row
, off
);
193 if (lbuf_wordlast(lb
, big
? 3 : uc_kind(lbuf_chr(lb
, *row
, *off
)), dir
, row
, off
))
198 /* move to the matching character */
199 int lbuf_pair(struct lbuf
*lb
, int *row
, int *off
)
201 int r
= *row
, o
= *off
;
202 char *pairs
= "()[]{}";
203 int pchr
; /* parenthesis character */
204 int pidx
; /* index into pairs[] */
205 int dep
= 1; /* parenthesis depth */
206 while ((pchr
= (unsigned char) lbuf_chr(lb
, r
, o
)[0]) && !strchr(pairs
, pchr
))
210 pidx
= strchr(pairs
, pchr
) - pairs
;
211 while (!lbuf_next(lb
, (pidx
& 1) ? -1 : +1, &r
, &o
)) {
212 int c
= (unsigned char) lbuf_chr(lb
, r
, o
)[0];
213 if (c
== pairs
[pidx
^ 1])
215 if (c
== pairs
[pidx
])