-
Notifications
You must be signed in to change notification settings - Fork 1
/
random.c
209 lines (179 loc) · 4.42 KB
/
random.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#include <stdio.h>
#include "estruct.h"
#include "etype.h"
#include "edef.h"
#include "english.h"
getcline() /* get the current line number */
{
register LINE *lp; /* current line */
register int numlines; /* # of lines before point */
/* starting at the beginning of the buffer */
lp = lforw(curbp->b_linep);
/* start counting lines */
numlines = 0;
while (lp != curbp->b_linep)
{
/* if we are on the current line, record it */
if (lp == curwp->w_dotp)
break;
++numlines;
lp = lforw(lp);
}
/* and return the resulting count */
return (numlines + 1);
}
/* Return current column. Stop at first non-blank given TRUE argument. */
getccol(int bflg)
{
register int c, i, col;
col = 0;
for (i = 0; i < curwp->w_doto; ++i)
{
c = lgetc(curwp->w_dotp, i);
if (c != ' ' && c != '\t' && bflg)
break;
if (c == '\t')
col += -(col % tabsize) + (tabsize - 1);
else if (c < 0x20 || c == 0x7F)
++col;
++col;
}
return (col);
}
tab()
{
if (!stabsize)
return (linsert(1, '\t'));
return (linsert(stabsize - (getccol(FALSE) % stabsize), ' '));
}
/*
* Insert a newline. Bound to "C-M".
*/
newline()
{
register int s;
if (curbp->b_mode & MDVIEW) /* don't allow this command if */
return (rdonly()); /* we are in read only mode */
if ((s = lnewline()) != TRUE)
return (s);
return (TRUE);
}
/*
* Delete forward. This is real easy, because the basic delete routine does
* all of the work. Watches for negative arguments, and does the right thing.
* If any argument is present, it kills rather than deletes, to prevent loss
* of text if typed with a big argument. Normally bound to "C-D".
*/
forwdel()
{
if (curbp->b_mode & MDVIEW) /* don't allow this command if */
return (rdonly()); /* we are in read only mode */
return (ldelete(1));
}
/*
* Delete backwards. This is quite easy too, because it's all done with other
* functions. Just move the cursor back, and delete forwards. Like delete
* forward, this actually does a kill if presented with an argument. Bound to
* both "RUBOUT" and "C-H".
*/
backdel()
{
register int s;
if (curbp->b_mode & MDVIEW) /* don't allow this command if */
return (rdonly()); /* we are in read only mode */
if ((s = backchar()) == TRUE)
s = ldelete(1);
return (s);
}
/*
* Kill text. Kills from dot to the end of the line. Normally bound to "C-K".
*/
killtext()
{
long chunk;
if (curbp->b_mode & MDVIEW) /* don't allow this command if */
return (rdonly()); /* we are in read only mode */
chunk = llength(curwp->w_dotp) - curwp->w_doto;
if (chunk == 0)
chunk = 1;
return (ldelete(chunk));
}
/*
* Refresh the screen. With no argument, it just does the refresh. With an
* argument it recenters "." in the current window. Bound to "C-L".
*/
refresh()
{
sgarbf = TRUE;
return (TRUE);
}
char *fixnull(char *s) /* Don't return NULL pointers! */
{
if (s == (char *) NULL)
return ("");
else
return (s);
}
/*
* int_asc: integer to ascii string.......... This is too inconsistant to
* use the system's
*/
char *int_asc(int i)
{
register int digit; /* current digit being used */
register char *sp; /* pointer into result */
register int sign; /* sign of resulting number */
static char result[INTWIDTH + 1]; /* resulting string */
/* record the sign... */
sign = 1;
if (i < 0)
{
sign = -1;
i = -i;
}
/* and build the string (backwards!) */
sp = result + INTWIDTH;
*sp = 0;
do
{
digit = i % 10;
*(--sp) = '0' + digit; /* and install the new digit */
i = i / 10;
} while (i);
/* and fix the sign */
if (sign == -1)
{
*(--sp) = '-'; /* and install the minus sign */
}
return (sp);
}
int absv(int x) /* take the absolute value of an integer */
{
return (x < 0 ? -x : x);
}
int ernd() /* returns a random integer */
{
seed = absv(seed * 1721 + 10007);
return (seed);
}
int execkey(KEYTAB *key) /* execute a function bound to a key */
{
if (key->k_type == BINDFNC)
return ((*(key->k_ptr.fp)) ());
return (TRUE);
}
/* This function looks a key binding up in the binding table */
KEYTAB *getbind(int c)
{
register KEYTAB *ktp;
/* scan through the binding table, looking for the key's entry */
ktp = &keytab[0];
while (ktp->k_type != BINDNUL)
{
if (ktp->k_code == c)
return (ktp);
++ktp;
}
/* no such binding */
return ((KEYTAB *) NULL);
}