-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathMOVEMENT.C
614 lines (544 loc) · 12.5 KB
/
MOVEMENT.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
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
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
/* Copyright (c) 1990,1991,1992 Chris and John Downey */
#ifndef lint
static char *sccsid = "@(#)movement.c 2.2 (Chris & John Downey) 9/1/92";
#endif
/***
* program name:
xvi
* function:
PD version of UNIX "vi" editor, with extensions.
* module name:
movement.c
* module function:
Movement of the cursor and the window into the buffer.
* history:
STEVIE - ST Editor for VI Enthusiasts, Version 3.10
Originally by Tim Thompson (twitch!tjt)
Extensive modifications by Tony Andrews (onecom!wldrdg!tony)
Heavily modified by Chris & John Downey
***/
#include "xvi.h"
/*
* Shift a buffer's contents down relative to its window,
* but don't change the display.
*
* Return number of physical lines shifted.
*/
int
shiftdown(win, nlines)
register Xviwin *win;
unsigned nlines;
{
register unsigned k; /* loop counter */
int done = 0; /* # of physical lines done */
for (k = 0; k < nlines; k++) {
register Line *p;
register long physlines;
/*
* Set the top screen line to the previous one.
*/
p = win->w_topline->l_prev;
if (p == win->w_buffer->b_line0)
break;
physlines = plines(win, p);
done += LONG2INT(physlines);
win->w_topline = p;
}
win->w_curs_new = TRUE;
return(done);
}
/*
* Shift a buffer's contents up relative to its window,
* but don't change the display.
*
* Return number of physical lines shifted.
*/
int
shiftup(win, nlines)
register Xviwin *win;
unsigned nlines;
{
register unsigned k; /* loop counter */
int done = 0; /* # of physical lines done */
for (k = 0; k < nlines; k++) {
register Line *p;
register long physlines;
/*
* Set the top screen line to the next one.
*/
p = win->w_topline->l_next;
if (p == win->w_buffer->b_lastline)
break;
physlines = plines(win, win->w_topline);
done += LONG2INT(physlines);
win->w_topline = p;
}
win->w_curs_new = TRUE;
return(done);
}
/*
* Scroll the screen down 'nlines' lines.
*/
void
scrolldown(win, nlines)
register Xviwin *win;
unsigned nlines;
{
s_ins(win, 0, shiftdown(win, nlines));
}
/*
* Scroll the screen up 'nlines' lines.
*/
void
scrollup(win, nlines)
register Xviwin *win;
unsigned nlines;
{
s_del(win, 0, shiftup(win, nlines));
}
/*
* oneup
* onedown
* one_left
* one_right
*
* Move cursor one char {left,right} or one line {up,down}.
*
* Return TRUE when successful, FALSE when we hit a boundary
* (of a line, or the file).
*/
/*
* Move the cursor up 'nlines' lines.
*
* Returns TRUE if we moved at all.
*/
bool_t
oneup(win, nlines)
register Xviwin *win;
long nlines;
{
Posn *pp;
register Line *curr_line;
long k;
pp = win->w_cursor;
curr_line = pp->p_line;
for (k = 0; k < nlines; k++) {
/*
* Look for the previous line.
*/
if (curr_line == win->w_buffer->b_file) {
/*
* If we've at least backed up a little ...
*/
if (k > 0) {
break; /* to update the cursor, etc. */
} else {
return(FALSE);
}
}
curr_line = curr_line->l_prev;
}
pp->p_line = curr_line;
pp->p_index = 0;
win->w_curs_new = TRUE;
/*
* Try to advance to the column we want to be at.
*/
coladvance(win, win->w_curswant);
return(TRUE);
}
/*
* Move the cursor down 'nlines' lines.
*
* Returns TRUE if we moved at all.
*/
bool_t
onedown(win, nlines)
register Xviwin *win;
long nlines;
{
Posn *pp;
register Line *curr_line;
long k;
pp = win->w_cursor;
curr_line = pp->p_line;
for (k = 0; k < nlines; k++) {
/*
* Look for the next line.
*/
if (curr_line->l_next == win->w_buffer->b_lastline) {
if (k > 0) {
break;
} else {
return(FALSE);
}
}
curr_line = curr_line->l_next;
}
pp->p_line = curr_line;
pp->p_index = 0;
win->w_curs_new = TRUE;
/*
* Try to advance to the column we want to be at.
*/
coladvance(win, win->w_curswant);
return(TRUE);
}
/*ARGSUSED*/
bool_t
one_left(window, unused)
Xviwin *window;
bool_t unused;
{
Posn *p;
window->w_set_want_col = TRUE;
p = window->w_cursor;
if (p->p_index > 0) {
p->p_index--;
curs_horiz(window, -1);
return(TRUE);
} else {
return(FALSE);
}
}
/*
* The move_past_end parameter will be TRUE if moving past the end
* of the line (onto the first '\0' character) is allowed. We will
* never move past this character.
*/
bool_t
one_right(window, move_past_end)
Xviwin *window;
bool_t move_past_end;
{
Posn *p;
char *txtp;
window->w_set_want_col = TRUE;
p = window->w_cursor;
txtp = &p->p_line->l_text[p->p_index];
if (txtp[0] != '\0' && (move_past_end || txtp[1] != '\0')) {
p->p_index++;
curs_horiz(window, 1);
return(TRUE);
} else {
return(FALSE);
}
}
void
begin_line(window, flag)
Xviwin *window;
bool_t flag;
{
register Posn *pos;
register int c;
pos = window->w_cursor;
if (flag) {
char *startp;
register char *p;
startp = p = pos->p_line->l_text;
while ((c = *p) != '\0' && p[1] != '\0' && is_space(c)) {
p++;
}
pos->p_index = p - startp;
} else {
pos->p_index = 0;
}
window->w_set_want_col = TRUE;
window->w_curs_new = TRUE;
}
/*
* coladvance(win, col)
*
* Try to advance to the specified column, starting at p.
*/
void
coladvance(win, col)
register Xviwin *win;
register int col;
{
register int c;
register char *tstart, *tp;
tp = tstart = win->w_cursor->p_line->l_text;
/*
* Try to advance to the specified column.
*/
for (c = 0; c < col; ) {
if (*tp == '\0') {
/*
* We're at the end of the line.
*/
break;
}
c += vischar(*tp, (char **) NULL, (Pb(P_list) ? -1 : c));
tp++;
}
/*
* Move back onto last character if we have to.
*/
if ((*tp == '\0' || c > col) && tp > tstart)
--tp;
win->w_cursor->p_index = tp - tstart;
curwin->w_curs_new = TRUE;
}
/*
* Go to the specified line number in the current buffer.
* Position the cursor at the first non-white.
*/
void
do_goto(line)
long line;
{
curwin->w_cursor->p_line = gotoline(curbuf, line);
curwin->w_cursor->p_index = 0;
curwin->w_curs_new = TRUE;
begin_line(curwin, TRUE);
}
/*
* Move the cursor to the specified line, at the specified position.
*/
void
move_cursor(win, lp, index)
Xviwin *win;
Line *lp;
int index;
{
win->w_cursor->p_line = lp;
win->w_cursor->p_index = index;
win->w_curs_new = TRUE;
}
/*
* Adjust window so that currline is, as far as possible, in the
* middle of it.
*
* Don't update the screen: move_window_to_cursor() does that.
*/
static void
jump(win, currline, halfwinsize)
Xviwin *win;
Line *currline;
int halfwinsize;
{
register int count;
register int spare;
register Line *topline;
register Line *filestart = win->w_buffer->b_file;
spare = win->w_nrows - (unsigned int) plines(win, topline = currline) - 1;
for (count = 0; count < halfwinsize && topline != filestart;) {
topline = topline->l_prev;
count += (unsigned int) plines(win, topline);
if (count >= spare) {
if (count > spare)
topline = topline->l_next;
break;
}
}
win->w_topline = topline;
update_buffer(win->w_buffer);
/*
* The result of calling show_file_info here is that if the
* cursor moves a long away - e.g. for a "G" command or a search
* - the status line is updated with the correct line number.
* This is a small cost compared to updating the whole window.
*/
show_file_info(win);
}
/*
* Update the position of the window relative to the buffer, moving
* the window if necessary to ensure that the cursor is inside the
* window boundary. w_topline, w_botline and w_cursor must be set to
* something reasonable for us to be able to test whether the cursor
* is in the window or not, unless (as a special case) the buffer is
* empty - in this case, the cursor sits at top left, despite there
* being no character there for it to sit on.
*
* If we have to move the window only a small amount, we try to scroll
* it rather than redrawing. If we have to redraw, we also rewrite the
* status line on the principle that it's probably a negligible cost
* compared to updating the whole window.
*/
void
move_window_to_cursor(win)
register Xviwin *win;
{
register Line *currline;
int halfwinsize;
long distance;
currline = win->w_cursor->p_line;
halfwinsize = (win->w_nrows - 1) / 2;
/*
* First stage: move window towards cursor.
*/
if (bufempty(win->w_buffer)) {
/*
* Special case - file is empty.
*/
win->w_topline = win->w_buffer->b_file;
win->w_cursor->p_line = win->w_buffer->b_file;
win->w_cursor->p_index = 0;
win->w_curs_new = TRUE;
} else if (earlier(currline, win->w_topline)) {
long nlines;
/*
* The cursor is above the top of the window; move the
* window towards it.
*/
nlines = cntplines(win, currline, win->w_topline);
/*
* Decide whether it's worthwhile - & possible - to
* scroll to get the window to the right place.
*
* It's possible if we can have scrolling regions, or
* we can insert screen lines & the window is at the
* bottom of the screen.
*
* The actual scrolling is done by s_ins(), in
* screen.c.
*
* If Pn(P_jumpscroll) is js_OFF, we don't use
* jumpscroll anyway.
*/
if (
nlines > halfwinsize
||
Pn(P_jumpscroll) == js_ON
||
(
!can_scroll_area
&&
Pn(P_jumpscroll) == js_AUTO
&&
(
!can_ins_line
||
win->w_cmdline < (Rows - 1)
)
)
) {
jump(win, currline, halfwinsize);
} else {
s_ins(win, 0, (int) nlines);
win->w_topline = currline;
update_window(win);
}
} else {
long nlines;
/*
* The cursor is on or after the last line of the screen,
* so we might have to move the screen to it. Check to see
* whether we are actually off the screen; if not, we don't
* have to do anything. We do this in a certain way so as
* not to do any unnecessary calculations; this routine is
* called very often, so we must not take too much time.
*/
if (earlier(currline, win->w_botline->l_prev) ||
(plines(win, currline) == 1 &&
earlier(currline, win->w_botline))) {
return;
}
distance = cntplines(win, win->w_topline, currline->l_next);
if (distance <= win->w_nrows - 1) {
return;
}
/*
* The cursor is off the bottom of the window, or the
* line the cursor is on won't completely fit in the
* window.
*/
nlines = distance - (win->w_nrows - 1);
/*
* Decide whether it's worthwhile - & possible - to
* scroll to get the window to the right place.
*
* It's possible if we can have scrolling regions, or
* we can delete screen lines & the window is at the
* bottom of the screen, or it's the only window on
* the screen.
*
* The actual scrolling is done by s_del(), in
* screen.c.
*
* If Pn(P_jumpscroll) is js_OFF, we don't use
* jumpscroll anyway.
*/
if (
nlines > halfwinsize
||
Pn(P_jumpscroll) == js_ON
||
(
!can_scroll_area
&&
Pn(P_jumpscroll) == js_AUTO
&&
(
(
!can_del_line
&&
win->w_winpos != 0
)
||
win->w_cmdline < (Rows - 1)
)
)
) {
jump(win, currline, halfwinsize);
} else {
long done = 0;
Line *l;
Line *newtopline;
/*
* Work out where we should place topline in
* order that the cursor line is made the
* bottom line of the screen.
*/
for (l = currline;; l = l->l_prev) {
done += plines(win, l);
if (done >= win->w_nrows - 1)
break;
if (l == win->w_buffer->b_file)
break;
}
while (done > win->w_nrows - 1 && l != currline) {
done -= plines(win, l);
l = l->l_next;
}
newtopline = l;
/*
* Now work out how many screen lines we want
* to scroll the window by. This code assumes
* that the old value of topline is earlier
* in the buffer than the new, so check this
* first.
*/
if (earlier(win->w_topline, newtopline)) {
done = cntplines(win, win->w_topline, newtopline);
if (done != 0) {
s_del(win, 0, (int) done);
}
}
win->w_topline = newtopline;
update_window(win);
}
}
}
/*
* Make sure the cursor is within the given window.
*
* This is needed for commands like control-E & control-Y.
*/
void
move_cursor_to_window(win)
Xviwin *win;
{
Posn *cp;
cp = win->w_cursor;
if (earlier(cp->p_line, win->w_topline)) {
cp->p_line = win->w_topline;
coladvance(win, win->w_curswant);
} else if (!earlier(cp->p_line, win->w_botline)
&& earlier(win->w_topline, win->w_botline)) {
cp->p_line = win->w_botline->l_prev;
coladvance(win, win->w_curswant);
}
win->w_curs_new = TRUE;
}