Skip to content

Commit 9889a8e

Browse files
committed
Make moveto and movepast generic and update to allow them to use
ctype.h functions.
1 parent df8753a commit 9889a8e

File tree

7 files changed

+63
-67
lines changed

7 files changed

+63
-67
lines changed

buff.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -571,6 +571,28 @@ int bindata(struct buff *buff, Byte *data, int size)
571571
return copied;
572572
}
573573

574+
/* Go forward or back past a thingy */
575+
void bmovepast(struct buff *buff, int (*pred)(int), bool forward)
576+
{
577+
if (!forward)
578+
bmove(buff, -1);
579+
while (!(forward ? bisend(buff) : bisstart(buff)) && pred(*buff->curcptr))
580+
bmove(buff, forward ? 1 : -1);
581+
if (!forward && !pred(*buff->curcptr))
582+
bmove1(buff);
583+
}
584+
585+
/* Go forward or back to a thingy */
586+
void bmoveto(struct buff *buff, int (*pred)(int), bool forward)
587+
{
588+
if (!forward)
589+
bmove(buff, -1);
590+
while (!(forward ? bisend(buff) : bisstart(buff)) && !pred(*buff->curcptr))
591+
bmove(buff, forward ? 1 : -1);
592+
if (!forward && !bisstart(buff))
593+
bmove1(buff);
594+
}
595+
574596
/* Low level memory page routines */
575597

576598
/* Make page current at dist */

buff.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ int bappend(struct buff *buff, Byte *, int);
8282
int bindata(struct buff *buff, Byte *, int);
8383

8484
void binstr(struct buff *buff, const char *str, ...);
85+
void bmovepast(struct buff *buff, int (*pred)(int), bool forward);
86+
void bmoveto(struct buff *buff, int (*pred)(int), bool forward);
8587

8688
/* bfile.c */
8789
#define FILE_COMPRESSED 0x10000

commands.c

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ bool Argp;
2727
/* Find the start of a word. */
2828
static bool Findstart(void)
2929
{
30-
while (!bisstart(Bbuff) && bistoken())
30+
while (!bisstart(Bbuff) && bistoken(Buff()))
3131
bmove(Bbuff, -1);
3232
while (!bisend(Bbuff) && !isalpha(*Curcptr))
3333
bmove1(Bbuff);
@@ -44,7 +44,7 @@ void Zcapitalize_word(void)
4444
{
4545
if (Findstart()) {
4646
bconvert(toupper);
47-
for (bmove1(Bbuff); !bisend(Bbuff) && bistoken(); bmove1(Bbuff))
47+
for (bmove1(Bbuff); !bisend(Bbuff) && bistoken(Buff()); bmove1(Bbuff))
4848
bconvert(tolower);
4949
vsetmod();
5050
}
@@ -54,7 +54,7 @@ void Zcapitalize_word(void)
5454
void Zlowercase_word(void)
5555
{
5656
if (Findstart()) {
57-
for (; !bisend(Bbuff) && bistoken(); bmove1(Bbuff))
57+
for (; !bisend(Bbuff) && bistoken(Buff()); bmove1(Bbuff))
5858
bconvert(tolower);
5959
vsetmod();
6060
}
@@ -63,7 +63,7 @@ void Zlowercase_word(void)
6363
void Zuppercase_word(void)
6464
{
6565
if (Findstart()) {
66-
for (; !bisend(Bbuff) && bistoken(); bmove1(Bbuff))
66+
for (; !bisend(Bbuff) && bistoken(Buff()); bmove1(Bbuff))
6767
bconvert(toupper);
6868
vsetmod();
6969
}
@@ -176,7 +176,7 @@ void Zc_insert(void)
176176

177177
do
178178
bmove(Bbuff, -1);
179-
while (!bisstart(Bbuff) && biswhite());
179+
while (!bisstart(Bbuff) && biswhite(Buff()));
180180

181181
if (ISNL(*Curcptr) && !bisstart(Bbuff)) {
182182
bmove1(Bbuff); /* skip NL */
@@ -196,7 +196,7 @@ void Zc_insert(void)
196196
break;
197197
}
198198
tobegline(Bbuff);
199-
while (bisspace())
199+
while (isspace(Buff()))
200200
bdelete(Bbuff, 1);
201201
bpnttomrk(Bbuff, tmark);
202202
}
@@ -356,8 +356,8 @@ void Zfill_check(void)
356356
return;
357357
}
358358
while (bgetcol(true, 0) > VAR(VFILLWIDTH)) {
359-
moveto(bisspace, BACKWARD);
360-
movepast(bisspace, BACKWARD);
359+
moveto(isspace, BACKWARD);
360+
movepast(isspace, BACKWARD);
361361
}
362362
Ztrim_white_space();
363363
tmp = !bisatmrk(Bbuff, tmark);
@@ -372,11 +372,11 @@ void Zfill_check(void)
372372

373373
void Ztrim_white_space(void)
374374
{
375-
while (!bisend(Bbuff) && biswhite())
375+
while (!bisend(Bbuff) && biswhite(Buff()))
376376
bdelete(Bbuff, 1);
377377
while (!bisstart(Bbuff)) {
378378
bmove(Bbuff, -1);
379-
if (biswhite())
379+
if (biswhite(Buff()))
380380
bdelete(Bbuff, 1);
381381
else {
382382
bmove1(Bbuff);
@@ -407,20 +407,20 @@ void Zfill_paragraph(void)
407407
/* mark the end of the paragraph and move the point to
408408
* the start */
409409
Znext_paragraph();
410-
movepast(bisspace, BACKWARD);
410+
movepast(isspace, BACKWARD);
411411
bmrktopnt(Bbuff, tmp);
412412
Zprevious_paragraph();
413413
if (*Curcptr == '.')
414414
bcsearch(Bbuff, '\n'); /* for nroff */
415415

416416
/* main loop */
417417
while (bisbeforemrk(Bbuff, tmp)) {
418-
moveto(bisspace, FORWARD);
418+
moveto(isspace, FORWARD);
419419
if (bgetcol(true, 0) > VAR(VFILLWIDTH)) {
420-
moveto(bisspace, BACKWARD);
420+
moveto(isspace, BACKWARD);
421421
Ztrim_white_space();
422422
binsert(Bbuff, NL);
423-
moveto(bisspace, FORWARD);
423+
moveto(isspace, FORWARD);
424424
}
425425
movepast(biswhite, FORWARD);
426426
if (*Curcptr == NL && bisbeforemrk(Bbuff, tmp)) {
@@ -430,7 +430,7 @@ void Zfill_paragraph(void)
430430
}
431431
}
432432

433-
movepast(bisspace, FORWARD); /* setup for next iteration */
433+
movepast(isspace, FORWARD); /* setup for next iteration */
434434
} while (Argp && !bisend(Bbuff) && !tkbrdy());
435435

436436
clrpaw();
@@ -458,25 +458,25 @@ void Znext_paragraph(void)
458458
char pc = '\0';
459459

460460
/* Only go back if between paras */
461-
if (bisspace())
462-
movepast(bisspace, BACKWARD);
461+
if (isspace(Buff()))
462+
movepast(isspace, BACKWARD);
463463
while (!bisend(Bbuff) && !ispara(pc, *Curcptr)) {
464464
pc = *Curcptr;
465465
bmove1(Bbuff);
466466
}
467-
movepast(bisspace, FORWARD);
467+
movepast(isspace, FORWARD);
468468
}
469469

470470
void Zprevious_paragraph(void)
471471
{
472472
char pc = '\0';
473473

474-
movepast(bisspace, BACKWARD);
474+
movepast(isspace, BACKWARD);
475475
while (!bisstart(Bbuff) && !ispara(*Curcptr, pc)) {
476476
pc = *Curcptr;
477477
bmove(Bbuff, -1);
478478
}
479-
movepast(bisspace, FORWARD);
479+
movepast(isspace, FORWARD);
480480
}
481481

482482
/* MISC COMMANDS */
@@ -791,7 +791,7 @@ void Zcount(void)
791791
for (; UMARK_SET ? bisbeforemrk(Bbuff, UMARK) : !bisend(Bbuff); bmove1(Bbuff), ++c) {
792792
if (ISNL(*Curcptr))
793793
++l;
794-
if (!bistoken())
794+
if (!bistoken(Buff()))
795795
word = false;
796796
else if (!word) {
797797
++w;

delete.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -206,15 +206,15 @@ void Zdelete_blanks(void)
206206
if (bcrsearch(Bbuff, NL)) {
207207
bmove1(Bbuff);
208208
bmrktopnt(Bbuff, tmark);
209-
movepast(bisspace, BACKWARD);
209+
movepast(isspace, BACKWARD);
210210
if (!bisstart(Bbuff))
211211
bcsearch(Bbuff, NL);
212212
if (bisbeforemrk(Bbuff, tmark))
213213
bdeltomrk(tmark);
214214
}
215215
if (bcsearch(Bbuff, NL)) {
216216
bmrktopnt(Bbuff, tmark);
217-
movepast(bisspace, FORWARD);
217+
movepast(isspace, FORWARD);
218218
if (bcrsearch(Bbuff, NL))
219219
bmove1(Bbuff);
220220
if (bisaftermrk(Bbuff, tmark))

proto.h

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -181,17 +181,14 @@ char *getbtxt(char *, int);
181181
int getbword(char *, int, int (*)());
182182
bool looking_at(const char *match);
183183
int getplete(const char *, const char *, char **, int, int);
184-
int bisspace(void);
185-
int bistoken(void);
186-
int biswhite(void);
187-
int bisword(void);
184+
int bistoken(int c);
185+
int biswhite(int c);
186+
int bisword(int c);
188187
int file_mode(void);
189188
void killtomrk(struct mark *);
190189
char *lastpart(char *);
191190
char *limit(char *, int);
192191
void makepaw(char *, bool);
193-
void movepast(int (*pred)(), bool forward);
194-
void moveto(int (*pred)(), bool forward);
195192
char *nocase(const char *);
196193
int pathfixup(char *, char *);
197194
bool promptsave(struct zbuff *tbuff, bool must);

z.c

Lines changed: 10 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -409,7 +409,7 @@ int getbword(char word[], int max, int (*valid)())
409409
struct mark tmark;
410410

411411
bmrktopnt(Bbuff, &tmark);
412-
if (!bistoken())
412+
if (!bistoken(Buff()))
413413
moveto(bistoken, BACKWARD);
414414
movepast(bistoken, BACKWARD);
415415
for (i = 0; !bisend(Bbuff) && valid() && i < max; ++i, bmove1(Bbuff))
@@ -450,26 +450,19 @@ char *getbtxt(char txt[], int max)
450450
return txt;
451451
}
452452

453-
/* Go forward or back past a thingy */
454-
void movepast(int (*pred)(), bool forward)
453+
int bisword(int c)
455454
{
456-
if (!forward)
457-
bmove(Bbuff, -1);
458-
while (!(forward ? bisend(Bbuff) : bisstart(Bbuff)) && (*pred)())
459-
bmove(Bbuff, forward ? 1 : -1);
460-
if (!forward && !(*pred)())
461-
bmove1(Bbuff);
455+
return isalnum(c) || c == '_' || c == '.' || c == '-';
462456
}
463457

464-
/* Go forward or back to a thingy */
465-
void moveto(int (*pred)(), bool forward)
458+
int bistoken(int c)
466459
{
467-
if (!forward)
468-
bmove(Bbuff, -1);
469-
while (!(forward ? bisend(Bbuff) : bisstart(Bbuff)) && !(*pred)())
470-
bmove(Bbuff, forward ? 1 : -1);
471-
if (!forward && !bisstart(Bbuff))
472-
bmove1(Bbuff);
460+
return bisword(c) || c == '/';
461+
}
462+
463+
int biswhite(int c)
464+
{
465+
return c == ' ' || c == '\t';
473466
}
474467

475468
/* Put in the right number of tabs and spaces */
@@ -482,27 +475,6 @@ void tindent(int arg)
482475
binsert(Bbuff, ' ');
483476
}
484477

485-
int bisspace(void)
486-
{
487-
return isspace(Buff());
488-
}
489-
490-
int bisword(void)
491-
{
492-
return isalnum(Buff()) || Buff() == '_' || Buff() == '.' || Buff() == '-';
493-
}
494-
495-
/* Must be a real function. */
496-
int bistoken(void)
497-
{
498-
return bisword() || Buff() == '/';
499-
}
500-
501-
int biswhite(void)
502-
{
503-
return Buff() == ' ' || Buff() == '\t';
504-
}
505-
506478
/* Limit a filename to at most Colmax - 'num' cols */
507479
char *limit(char *fname, int num)
508480
{

z.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,9 @@ struct zbuff {
156156
#define Buff() (*Bbuff->curcptr)
157157
#define unmark bdelmark
158158

159+
#define movepast(a, b) bmovepast(Bbuff, a, b)
160+
#define moveto(a, b) bmoveto(Bbuff, a, b)
161+
159162
extern char *Home;
160163
extern bool Argp;
161164
extern int Arg; /* must be signed */

0 commit comments

Comments
 (0)