8000 minix/commands/elvis/modify.c at v3.1.0 · dtytomi/minix · GitHub
[go: up one dir, main page]

Skip to content

Latest commit

 

History

History
executable file
·
474 lines (423 loc) · 9.61 KB

modify.c

File metadata and controls

executable file
·
474 lines (423 loc) · 9.61 KB
}
newptr += markidx(atmark);
/* keep start of old block */
build = blk->c + (int)(newptr - buf.c);
/* fill this block (or blocks) from the newtext string */
while (*newtext)
{
while (*newtext && build < blk->c + BLKSIZE - 1)
{
*build++ = *newtext++;
}
if (*newtext)
{
/* save the excess */
for (scan = linebuf.c + BLKSIZE;
build > blk->c && build[-1] != '\n';
)
{
*--scan = *--build;
}
/* write the block */
while (build < blk->c + BLKSIZE)
{
*build++ = '\0';
}
blkdirty(blk);
/* add another block */
blkno++;
blk = blkadd(blkno);
/* copy in the excess from last time */
for (build = blk->c; scan < linebuf.c + BLKSIZE; )
{
*build++ = *scan++;
}
}
}
/* fill this block(s) from remainder of orig block */
while (newptr < buf.c + BLKSIZE && *newptr)
{
while (newptr < buf.c + BLKSIZE
&& *newptr
&& build < blk->c + BLKSIZE - 1)
{
*build++ = *newptr++;
}
if (newptr < buf.c + BLKSIZE && *newptr)
{
/* save the excess */
for (scan = linebuf.c + BLKSIZE;
build > blk->c && build[-1] != '\n';
)
{
*--scan = *--build;
}
/* write the block */
while (build < blk->c + BLKSIZE)
{
*build++ = '\0';
}
blkdirty(blk);
/* add another block */
blkno++;
blk = blkadd(blkno);
/* copy in the excess from last time */
for (build = blk->c; scan < linebuf.c + BLKSIZE; )
{
*build++ = *scan++;
}
}
}
/* see if we can combine our last block with the following block */
if (lnum[blkno] < nlines && lnum[blkno + 1] - lnum[blkno] < (BLKSIZE >> 6))
{
/* hey, we probably can! Get the following block & see... */
following = blkget(blkno + 1);
if (strlen(following->c) + (build - blk->c) < BLKSIZE - 1)
{
/* we can! Copy text from following to blk */
for (scan = following->c; *scan; )
{
*build++ = *scan++;
}
while (build < blk->c + BLKSIZE)
{
*build++ = '\0';
}
blkdirty(blk);
/* pretend the following was the last blk */
blk = following;
build = blk->c;
}
}
/* that last block is dirty by now */
while (build < blk->c + BLKSIZE)
{
*build++ = '\0';
}
blkdirty(blk);
}
/* change the text of a file */
void change(frommark, tomark, newtext)
MARK frommark, tomark;
char *newtext;
{
int i;
long l;
char *text;
BLK *blk;
#ifdef DEBUG2
debout("change(%ld.%d, %ld.%d, \"%s\")\n", markline(frommark), markidx(frommark), markline(tomark), markidx(tomark), newtext);
#endif
/* optimize for single-character replacement */
if (frommark + 1 == tomark && newtext[0] && !newtext[1] && newtext[0] != '\n')
{
/* find the block containing frommark */
l = markline(frommark);
for ( 5927 i = 1; lnum[i] < l; i++)
{
}
/* get the block */
blk = blkget(i);
/* find the line within the block */
for (text = blk->c, i = l - lnum[i - 1] - 1; i > 0; text++)
{
if (*text == '\n')
{
i--;
}
}
/* replace the char */
text += markidx(frommark);
if (*text == newtext[0])
{
/* no change was needed - same char */
return;
}
else if (*text != '\n')
{
/* This is a change */
changes++;
significant = TRUE;
ChangeText
{
*text = newtext[0];
blkdirty(blk);
}
redrawrange(markline(frommark), markline(tomark), markline(frommark));
return;
}
/* else it is a complex change involving newline... */
}
/* couldn't optimize, so do delete & add */
ChangeText
{
delete(frommark, tomark);
add(frommark, newtext);
rptlabel = "changed";
}
}
0