Skip to content

Commit

Permalink
A couple more generic functions.
Browse files Browse the repository at this point in the history
  • Loading branch information
smaclennan committed Dec 3, 2015
1 parent 77d1f1d commit 96e491f
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 75 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ fcheck: fcheck.c funcs.c kbd.c varray.c cnames.c bind.c config.h vars.h keys.h
@./fcheck $(ZLIBINC) $(ASPELLINC)

# This is just to check that no zedit dependencies crept into buff.c
main: main.c buff.c bmsearch.c reg.c mark.c bsocket.c bfile.o
main: main.c buff.c bmsearch.c reg.c mark.c bsocket.c bfile.c
$(QUIET_LINK)$(CC) -UHAVE_GLOBAL_MARKS -DHAVE_BUFFER_MARKS -g -o $@ $+
$(QUIET_LINK)$(CC) -UHAVE_GLOBAL_MARKS -UUNSIGNED_BYTES -g -o $@ $+

Expand Down
72 changes: 0 additions & 72 deletions delete.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,78 +42,6 @@ static void copytomrk(struct mark *tmark)
bcopyrgn(tmark, Killbuff);
}

/* Copy from Point to tmark to tbuff. Returns number of bytes
* copied. Caller must handle undo. */
/* No global */
int bcopyrgn(struct mark *tmark, struct buff *to)
{
struct mark *ltmrk, *btmrk;
bool flip;
int srclen, dstlen;
int copied = 0;
struct buff *from = tmark->mbuff;

flip = bisaftermrk(from, tmark);
if (flip)
bswappnt(from, tmark);

if (!(ltmrk = bcremark(from)))
return 0;

while (bisbeforemrk(from, tmark)) {
if (from->curpage == tmark->mpage)
srclen = tmark->moffset - from->curchar;
else
srclen = curplen(from) - from->curchar;

dstlen = PSIZE - curplen(to);
if (dstlen == 0) {
if (pagesplit(to, HALFP))
dstlen = PSIZE - curplen(to);
else
break;
}
if (srclen < dstlen)
dstlen = srclen;
/* Make a gap */
memmove(to->curcptr + dstlen, to->curcptr, curplen(to) - to->curchar);
/* and fill it in */
memmove(to->curcptr, from->curcptr, dstlen);
curplen(to) += dstlen;
copied += dstlen;
foreach_global_pagemark(to, btmrk, to->curpage)
if (btmrk->moffset > to->curchar)
btmrk->moffset += dstlen;
foreach_pagemark(to, btmrk, to->curpage)
if (btmrk->moffset > to->curchar)
btmrk->moffset += dstlen;
makeoffset(to, to->curchar + dstlen);
vsetmod_callback(to);
to->bmodf = true;
bmove(from, dstlen);
}

bpnttomrk(from, ltmrk);
unmark(ltmrk);

if (flip)
bswappnt(from, tmark);

return copied;
}

/* Delete from the point to the Mark. */
void bdeltomrk(struct mark *tmark)
{
if (bisaftermrk(Bbuff, tmark))
bswappnt(Bbuff, tmark);
while (bisbeforemrk(Bbuff, tmark))
if (Bbuff->curpage == tmark->mpage)
bdelete(Bbuff, tmark->moffset - Bbuff->curchar);
else
bdelete(Bbuff, curplen(Bbuff) - Bbuff->curchar);
}

void killtomrk(struct mark *tmark)
{
copytomrk(tmark);
Expand Down
82 changes: 82 additions & 0 deletions mark.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,14 @@ struct mark *_bcremark(struct buff *buff, struct mark **tail)
#endif
mptr = (struct mark *)calloc(1, sizeof(struct mark));
if (mptr) {
#if defined(HAVE_GLOBAL_MARKS) || defined(HAVE_BUFFER_MARKS)
if (tail) {
mptr->prev = *tail; /* add to end of list */
if (*tail)
(*tail)->next = mptr;
*tail = mptr;
}
#endif
bmrktopnt(buff, mptr);
++NumMarks;
}
Expand All @@ -51,6 +53,7 @@ struct mark *_bcremark(struct buff *buff, struct mark **tail)
void _bdelmark(struct mark *mptr, struct mark **tail)
{
if (mptr) {
#if defined(HAVE_GLOBAL_MARKS) || defined(HAVE_BUFFER_MARKS)
if (tail) {
if (mptr == *tail)
*tail = mptr->prev;
Expand All @@ -59,6 +62,7 @@ void _bdelmark(struct mark *mptr, struct mark **tail)
if (mptr->next)
mptr->next->prev = mptr->prev;
}
#endif
#ifdef HAVE_FREEMARK
if (freemark == NULL) {
freemark = mptr;
Expand Down Expand Up @@ -196,3 +200,81 @@ bool mrkatmrk(struct mark *mark1, struct mark *mark2)
mark1->mpage == mark2->mpage &&
mark1->moffset == mark2->moffset;
}

/* Copy from Point to mark to buffer 'to'. Returns bytes copied. */
long bcopyrgn(struct mark *tmark, struct buff *to)
{
struct mark *ltmrk, *btmrk;
bool flip;
int srclen, dstlen;
long copied = 0;
struct buff *from = tmark->mbuff;

flip = bisaftermrk(from, tmark);
if (flip)
bswappnt(from, tmark);

if (!(ltmrk = bcremark(from)))
return 0;

while (bisbeforemrk(from, tmark)) {
if (from->curpage == tmark->mpage)
srclen = tmark->moffset - from->curchar;
else
srclen = curplen(from) - from->curchar;

dstlen = PSIZE - curplen(to);
if (dstlen == 0) {
if (pagesplit(to, HALFP))
dstlen = PSIZE - curplen(to);
else
break;
}
if (srclen < dstlen)
dstlen = srclen;
/* Make a gap */
memmove(to->curcptr + dstlen, to->curcptr, curplen(to) - to->curchar);
/* and fill it in */
memmove(to->curcptr, from->curcptr, dstlen);
curplen(to) += dstlen;
copied += dstlen;
foreach_global_pagemark(to, btmrk, to->curpage)
if (btmrk->moffset > to->curchar)
btmrk->moffset += dstlen;
foreach_pagemark(to, btmrk, to->curpage)
if (btmrk->moffset > to->curchar)
btmrk->moffset += dstlen;
makeoffset(to, to->curchar + dstlen);
bsetmod(to);
to->bmodf = true;
bmove(from, dstlen);
}

bpnttomrk(from, ltmrk);
bdelmark(ltmrk);

if (flip)
bswappnt(from, tmark);

return copied;
}

/* Delete from the point to the Mark. Returns bytes deleted. */
long bdeltomrk(struct mark *tmark)
{
long amount, deleted = 0;
struct buff *buff = tmark->mbuff;

if (bisaftermrk(buff, tmark))
bswappnt(buff, tmark);
while (bisbeforemrk(buff, tmark)) {
if (buff->curpage == tmark->mpage)
amount = tmark->moffset - buff->curchar;
else
amount = curplen(buff) = buff->curchar;
bdelete(buff, amount);
deleted += amount;
}

return deleted;
}
3 changes: 3 additions & 0 deletions mark.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ bool mrkaftermrk(struct mark *, struct mark *);
bool mrkatmrk(struct mark *, struct mark *);
bool mrkbeforemrk(struct mark *, struct mark *);

long bcopyrgn(struct mark *, struct buff*);
long bdeltomrk(struct mark *);

#ifdef HAVE_GLOBAL_MARKS
extern struct mark *Marklist;

Expand Down
2 changes: 0 additions & 2 deletions proto.h
Original file line number Diff line number Diff line change
Expand Up @@ -269,8 +269,6 @@ void set_mouse(bool enable);
void mouse_scroll(int row, bool down);
void mouse_point(int row, int col, bool set_mark);

int bcopyrgn(struct mark *, struct buff*);
void bdeltomrk(struct mark *);
unsigned long bline(struct buff *buff);

/* COMMENTBOLD */
Expand Down

0 comments on commit 96e491f

Please sign in to comment.