unstack, sort: cleanup and improvement
[minix.git] / commands / elvis / misc.c
blobc9e0f689d2150adb2aee7f07856d6d4346c1a99c
1 /* misc.c */
3 /* Author:
4 * Steve Kirkendall
5 * 14407 SW Teal Blvd. #C
6 * Beaverton, OR 97005
7 * kirkenda@cs.pdx.edu
8 */
11 /* This file contains functions which didn't seem happy anywhere else */
13 #include "config.h"
14 #include "vi.h"
17 /* find a particular line & return a pointer to a copy of its text */
18 char *fetchline(line)
19 long line; /* line number of the line to fetch */
21 int i;
22 REG char *scan; /* used to search for the line in a BLK */
23 long l; /* line number counter */
24 static BLK buf; /* holds ONLY the selected line (as string) */
25 REG char *cpy; /* used while copying the line */
26 static long nextline; /* } These four variables are used */
27 static long chglevel; /* } to implement a shortcut when */
28 static char *nextscan; /* } consecutive lines are fetched */
29 static long nextlnum; /* } */
31 /* can we do a shortcut? */
32 if (changes == chglevel && line == nextline)
34 scan = nextscan;
36 else
38 /* scan lnum[] to determine which block its in */
39 for (i = 1; line > lnum[i]; i++)
42 nextlnum = lnum[i];
44 /* fetch text of the block containing that line */
45 scan = blkget(i)->c;
47 /* find the line in the block */
48 for (l = lnum[i - 1]; ++l < line; )
50 while (*scan++ != '\n')
56 /* copy it into a block by itself, with no newline */
57 for (cpy = buf.c; *scan != '\n'; )
59 *cpy++ = *scan++;
61 *cpy = '\0';
63 /* maybe speed up the next call to fetchline() ? */
64 if (line < nextlnum)
66 nextline = line + 1;
67 chglevel = changes;
68 nextscan = scan + 1;
70 else
72 nextline = 0;
75 /* Calls to fetchline() interfere with calls to pfetch(). Make sure
76 * that pfetch() resets itself on its next invocation.
78 pchgs = 0L;
80 /* Return a pointer to the line's text */
81 return buf.c;
85 /* error message from the regexp code */
86 void regerror(txt)
87 char *txt; /* an error message */
89 msg("RE error: %s", txt);
92 /* This function is equivelent to the pfetch() macro */
93 void pfetch(l)
94 long l; /* line number of line to fetch */
96 if(l != pline || changes != pchgs)
98 pline = (l);
99 ptext = fetchline(pline);
100 plen = strlen(ptext);
101 pchgs = changes;