File: basic.c

package info (click to toggle)
beav 1%3A1.40-18.4
  • links: PTS
  • area: main
  • in suites: bookworm
  • size: 1,032 kB
  • sloc: ansic: 20,487; makefile: 55
file content (552 lines) | stat: -rw-r--r-- 11,823 bytes parent folder | download | duplicates (5)
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
/*
*      Basic cursor motion commands.
* The routines in this file are the basic
* command functions for moving the cursor around on
* the screen, setting mark, and swapping dot with
* mark. Only moves between lines, which might make the
* current buffer framing bad, are hard.
*/

#include    "def.h"

bool move_ptr ();
bool forwchar ();
bool wind_on_dot ();
bool backline ();

extern char MSG_mark_set[];
extern char MSG_no_mark[];
extern char MSG_go_b_n[];
extern char MSG_bad_num[];
#if RUNCHK
extern char ERR_bas_1[];
#endif
extern char MSG_lX[];
extern char MSG_lO[];
extern char MSG_lD[];


extern bool rplc_mode;

/*  pvr
* Move cursor backwards. Do the
* right thing if the count is less than
* 0. Error if you try to move back from
* the beginning of the buffer.
*/
bool
backchar (f, n, k)
    register int f, n, k;
{
    if (n < 0)
	return (forwchar (f, -n, KRANDOM));

    while (n--)
    {
	if (curwp->w_unit_offset == 0)
	{
	    if (!move_ptr (curwp, -(long) R_B_PER_U (curwp),
			   TRUE, TRUE, TRUE))
		return (FALSE);

	    /* step to previous unit */
	    curwp->w_unit_offset = R_CHR_PER_U (curwp) - 1;

	    /* if before first line in window then move window */
	    wind_on_dot (curwp);
	}
	else
	    curwp->w_unit_offset--;
    }
    curwp->w_flag |= WFMODE;	/* update mode line */
    return (TRUE);
}

/*  pvr
* Move cursor forwards. Do the
* right thing if the count is less than
* 0. Error if you try to move forward
* from the end of the buffer.
*/
bool
forwchar (f, n, k)
    register int f, n, k;
{
    if (n < 0)
	return (backchar (f, -n, KRANDOM));

    curwp->w_flag |= WFMODE;	/* update mode line */
    while (n--)
    {
	if (curwp->w_unit_offset >= (R_CHR_PER_U (curwp) - 1))
	{
	    /* move to the mext unit */
	    curwp->w_unit_offset = 0;

	    if (!move_ptr (curwp, (long) R_B_PER_U (curwp),
			   TRUE, TRUE, TRUE))
	    {
		/* I am at the the end of the buffer */
		return (FALSE);
	    }

	    /* if after the last line in window then move window */
	    wind_on_dot (curwp);
	}
	else if			/* if at last byte of buffer then do not step  */
	    (DOT_POS (curwp) < BUF_SIZE (curwp))
	    curwp->w_unit_offset++;	/* step within unit */
    }
    return (TRUE);
}

/*  pvr
*   This function moves the specified pointer by the ammount specified
*   in 'len'.   Move the dot pointer is 'dot' is true, else move
*   the window pointer.  Do the fix up if 'fix' is TRUE.
*   This is a relative move if 'rel' is TRUE, else it is an
*   absolute move.
*/

bool
move_ptr (wp, len, dot, fix, rel)
    WINDOW *wp;
    long len;
    bool dot, fix, rel;
{
    A32 cur_pos, dest_pos, fix_val, last_pos;
    long rel_pos;
    A32 last_fixed_pos, align;
    LINE **line;
    int *l_off;
    char shift;
    bool no_limit;

    no_limit = TRUE;
    if (dot)
    {				/* move dot position */
	l_off = (int *) &wp->w_doto;
	line = &wp->w_dotp;
	align = R_SIZE (wp);	/* bytes -1 in a unit */
    }
    else
    {				/* move window position */
	l_off = (int *) &wp->w_loff;
	line = &wp->w_linep;
	align = R_ALIGN (wp) - 1;	/* interval of bytes to align window */
    }

    /* get the current position in the buffer */
    cur_pos = (*line)->l_file_offset + *l_off;

    if (rel)
    {
	rel_pos = len;
	dest_pos = len + cur_pos;	/* destination position */
    }
    else
    {
	rel_pos = len - cur_pos;/* relative move amount */
	dest_pos = len;		/* destination position */
    }
    if (fix)
    {
	shift = wp->w_disp_shift;

	/* limit at begining */
	if ((long) dest_pos < (long) shift)
	{
	    rel_pos = shift - cur_pos;
	    no_limit = FALSE;
	}
	else
	{
	    /* calculate fixed up destination position */
	    fix_val = dest_pos &= ~align;
	    fix_val += shift;

	    /* calculate the last position in the buffer */
	    last_pos = BUF_SIZE (wp);
	    if (last_pos < (last_fixed_pos = (last_pos & ~align) + shift))
		last_pos = last_fixed_pos - align - 1;

	    /* if we are going to limit at the end of the buffer */
	    if (last_pos < fix_val)
	    {
		fix_val = last_pos;
		no_limit = FALSE;
	    }
	    rel_pos = fix_val - cur_pos;
	}
    }
    while (TRUE)
    {
	if (rel_pos < 0)	/* move  backward through buffer */
	{
	    /* current line? */
	    if (*l_off + rel_pos >= 0)
	    {
		*l_off += (short) rel_pos;
		return (no_limit);
	    }
	    /* are we at the first line */
	    if ((*line)->l_bp->l_size != 0)
	    {			/* no, so step back */
		rel_pos += *l_off;
		(*line) = (*line)->l_bp;	/* move back one line */
		*l_off = (*line)->l_used;
	    }
	    else
	    {			/* yes, limit at the begining */
		*l_off = 0;
		return (FALSE);
	    }
	}
	else
	    /* move forward through buffer */
	{
	    /* is in current line? */
	    if (((A32) (*l_off) + rel_pos) < ((A32) ((*line)->l_used)))
	    {
		*l_off += (short) rel_pos;
		return (no_limit);
	    }
	    if ((*line)->l_fp->l_size != 0)
	    {
		rel_pos -= (*line)->l_used - *l_off;
		*l_off = 0;
		(*line) = (*line)->l_fp;	/* move forward one line */
	    }
	    else
	    {
		*l_off = (*line)->l_used;	/* at last line so limit it */
		return (FALSE);
	    }
	}
    }
}

/*  pvr
*   Move the window so that the dot is within it's
*   area.   Return TRUE if window was moved.
*/

bool
wind_on_dot (wp)

    WINDOW *wp;
{
    long diff, incr;
    A32 d_offs, w_start, bytes, align;

    /* number of bytes in a row */
    bytes = R_BYTES (wp);
    /* number of bytes to align on */
    align = R_ALIGN (wp);
    /* offset of window from start of the buffer */
    w_start = WIND_POS (wp);
    /* offset of dot from start of the buffer */
    d_offs = DOT_POS (wp);
    /* calculate the amount to move that is 1/3 of the window */
    incr = bytes * wp->w_ntrows / 3;
    /* if dot is before first line in window */
    if ((diff = (d_offs - w_start)) < 0)	/* diff used later */
    {
	move_ptr (wp, diff - incr, FALSE, TRUE, TRUE);
	wp->w_flag |= WFHARD;
	return (TRUE);
    }
    /* if dot is after the last line in window */
    if (0 < (diff -= (wp->w_ntrows * bytes - 1)))
    {
	if (align != 1)
	    diff = (diff & ~(align - 1)) + align;
	move_ptr (wp, diff + incr, FALSE, TRUE, TRUE);
	wp->w_flag |= WFHARD;
	return (TRUE);
    }
    /* is window aligned? */
    if (w_start != ((w_start & ~(align - 1)) + wp->w_disp_shift))
    {				/* if no then move into alignment */
	move_ptr (wp, 0L, FALSE, TRUE, TRUE);
	wp->w_flag |= WFHARD;
	return (TRUE);
    }
    return (FALSE);
}

/*  pvr
* Go to the beginning of the
* buffer. Setting WFHARD is conservative,
* but almost always the case.
*/
bool
gotobob ()
{
    move_ptr (curwp, 0L, TRUE, TRUE, FALSE);	/* move dot */
    move_ptr (curwp, 0L, FALSE, TRUE, FALSE);	/* move window */
    curwp->w_unit_offset = 0;
    curwp->w_flag |= WFHARD;
    return (TRUE);
}

/*  pvr
* Go to the end of the buffer.
* Setting WFHARD is conservative, but
* almost always the case.
* Dot is one byte past the end of the buffer.
*/
bool
gotoeob ()
{
    move_ptr (curwp, BUF_SIZE (curwp), TRUE, TRUE, FALSE);	/* move dot */
    curwp->w_unit_offset = 0;
    wind_on_dot (curwp);
    return (TRUE);
}

/*  pvr
* Move forward by full lines.
* If the number of lines to move is less
* than zero, call the backward line function to
* actually do it. The last command controls how
* the goal column is set.
*/
bool
forwline (f, n, k)
    int f, n, k;
{
    if (n < 0)
	return (backline (f, -n, KRANDOM));

    if (rplc_mode)
    {
	next_pat ();
    }
    else
    {
	/* move dot */
	if (!move_ptr (curwp, (long) R_BYTES (curwp) * n,
		       TRUE, TRUE, TRUE))
	    curwp->w_unit_offset = 0;
	wind_on_dot (curwp);
	curwp->w_flag |= WFMODE;/* update mode line */
    }
    return (TRUE);
}

/*  pvr
* This function is like "forwline", but
* goes backwards. The scheme is exactly the same.
* Check for arguments that are less than zero and
* call your alternate. Figure out the new line and
* call "movedot" to perform the motion.
*/
bool
backline (f, n, k)
    int f, n, k;
{
    if (n < 0)
	return (forwline (f, -n, KRANDOM));

    if (rplc_mode)
    {
	next_pat ();
    }
    else
    {
	if (!move_ptr (curwp, -((long) (R_BYTES (curwp) * n)),
		       TRUE, TRUE, TRUE))
	    curwp->w_unit_offset = 0;

	/* is dot before the top of window? */
	wind_on_dot (curwp);
	curwp->w_flag |= WFMODE;/* update mode line */
    }
    return (TRUE);
}

/*  pvr
* Scroll forward by a specified number
* of lines, or by a full page if no argument.
* (KRW) Added cursor (dot) weighting to force cursor
*       to same position on new page.
*/
bool
forwpage (f, n, k)
    int f, n, k;
{
    long mov_lines;

    if (rplc_mode)
	next_pat ();
    else
    {
	if (curwp->w_ntrows <= 2)
	    mov_lines = 2;
	else
	    mov_lines = curwp->w_ntrows - 2;

	/* check if last line is already displayed */
	if (WIND_POS (curwp) + (R_BYTES (curwp) * curwp->w_ntrows) <
	    curwp->w_bufp->b_linep->l_bp->l_file_offset +
	    curwp->w_bufp->b_linep->l_bp->l_used)
	{
	    move_ptr (curwp, (long) (R_BYTES (curwp) * mov_lines),
		      FALSE, TRUE, TRUE);
	}
	/* move dot by same amount */
	if (!move_ptr (curwp, (long) (R_BYTES (curwp) * mov_lines),
		       TRUE, TRUE, TRUE))
	    curwp->w_unit_offset = 0;

	curwp->w_flag |= WFHARD;
    }
    return (TRUE);
}

/*  pvr
* This command is like "forwpage",
* but it goes backwards.
*/
bool
backpage (f, n, k)
    int f, n, k;
{
    long mov_lines;

    if (rplc_mode)
	next_pat ();
    else
    {
	if (curwp->w_ntrows <= 2)
	    mov_lines = 2;
	else
	    mov_lines = curwp->w_ntrows - 2;

	/* move window */
	move_ptr (curwp, -(long) (R_BYTES (curwp) * mov_lines),
		  FALSE, TRUE, TRUE);
	/* move dot by same amount */
	if (!move_ptr (curwp, -(long) (R_BYTES (curwp) * mov_lines),
		       TRUE, TRUE, TRUE))
	    curwp->w_unit_offset = 0;

	curwp->w_flag |= WFHARD;
    }
    return (TRUE);
}

/*
* Set the mark in the current window
* to the value of dot. A message is written to
* the echo line unless we are running in a keyboard
* macro, when it would be silly.
*/
bool
setmark ()
{

    if (curbp == blistp)	/* jam - hack to do goto/kill */
	pickone ();
    else
    {
	curwp->w_markp = curwp->w_dotp;
	curwp->w_marko = curwp->w_doto;
	if (kbdmop == NULL)
	{
	    writ_echo (MSG_mark_set);
	}
    }
    return (TRUE);
}

/*  pvr
* Swap the values of "dot" and "mark" in
* the current window. This is pretty easy, because
* all of the hard work gets done by the standard routine
* that moves the mark about. The only possible
* error is "no mark".
*/
bool
swapmark ()
{
    register short odoto;
    register LINE *odotp;

    if (curwp->w_markp == NULL)
    {
	writ_echo (MSG_no_mark);
	return (FALSE);
    }

    odotp = curwp->w_dotp;
    curwp->w_dotp = curwp->w_markp;
    curwp->w_markp = odotp;
    odoto = curwp->w_doto;
    curwp->w_doto = curwp->w_marko;
    curwp->w_marko = odoto;
    wind_on_dot (curwp);
    curwp->w_flag |= WFMODE;	/* update mode line */
    return (TRUE);
}

/*  pvr
* Go to a specific byte position in buffer.
* If an argument is present, then
* it is the byte number, else prompt for a byte number
* to use.
*/
bool
gotoline (f, n, k)
    int f, n, k;
{
    A32 index;
    register int s;
    char buf[32];

    if (f == FALSE)
    {

	if ((s = ereply (MSG_go_b_n, buf, sizeof (buf), 0) != TRUE))
	    return (s);
	switch (R_TYPE (curwp))
	{
	case TEXT:
	case ASCII:
	case EBCDIC:
	case BINARY:
	case HEX:
	    sscanf (buf, MSG_lX, &index);
	    break;
	case OCTAL:
	    sscanf (buf, MSG_lO, &index);
	    break;
	case DECIMAL:
#if	FLOAT_DISP
	case FLOAT:
#endif
	    sscanf (buf, MSG_lD, &index);
	    break;
#if RUNCHK
	default:
	    writ_echo (ERR_bas_1);
	    break;
#endif
	}
    }

    if (n <= 0)
    {
	writ_echo (MSG_bad_num);
	return (FALSE);
    }

    move_ptr (curwp, index, TRUE, TRUE, FALSE);
    curwp->w_unit_offset = 0;

    curwp->w_flag |= WFMODE;	/* update mode line */

    wind_on_dot (curwp);
    return (TRUE);
}