Subversion Repositories planix.SVN

Rev

Rev 118 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
105 7u83 1
/*
2
 * Copyright (c) 1980 Regents of the University of California.
3
 * All rights reserved.  The Berkeley software License Agreement
4
 * specifies the terms and conditions for redistribution.
5
 */
6
 
7
#if	!defined(lint) && defined(DOSCCS)
8
static char *sccsid = "@(#)ex_vwind.c	7.3 (Berkeley) 6/7/85";
9
#endif
10
 
11
#include "ex.h"
12
#include "ex_tty.h"
13
#include "ex_vis.h"
14
 
15
/*
16
 * Routines to adjust the window, showing specified lines
17
 * in certain positions on the screen, and scrolling in both
18
 * directions.  Code here is very dependent on mode (open versus visual).
19
 */
20
 
21
/*
22
 * Move in a nonlocal way to line addr.
23
 * If it isn't on screen put it in specified context.
24
 * New position for cursor is curs.
25
 * Like most routines here, we vsave().
26
 */
27
vmoveto(addr, curs, context)
28
	register line *addr;
29
	char *curs;
30
	char context;
31
{
32
 
33
	markit(addr);
34
	vsave();
35
	vjumpto(addr, curs, context);
36
}
37
 
38
/*
39
 * Vjumpto is like vmoveto, but doesn't mark previous
40
 * context or save linebuf as current line.
41
 */
42
vjumpto(addr, curs, context)
43
	register line *addr;
44
	char *curs;
45
	char context;
46
{
47
 
48
	noteit(0);
49
	if (context != 0)
50
		vcontext(addr, context);
51
	else
52
		vshow(addr, NOLINE);
53
	noteit(1);
54
	vnline(curs);
55
}
56
 
57
/*
58
 * Go up or down cnt (negative is up) to new position curs.
59
 */
60
vupdown(cnt, curs)
61
	register int cnt;
62
	char *curs;
63
{
64
 
65
	if (cnt > 0)
66
		vdown(cnt, 0, 0);
67
	else if (cnt < 0)
68
		vup(-cnt, 0, 0);
69
	if (vcnt == 0)
70
		vrepaint(curs);
71
	else
72
		vnline(curs);
73
}
74
 
75
/*
76
 * Go up cnt lines, afterwards preferring to be ind
77
 * logical lines from the top of the screen.
78
 * If scroll, then we MUST use a scroll.
79
 * Otherwise clear and redraw if motion is far.
80
 */
81
vup(cnt, ind, scroll)
82
	register int cnt, ind;
83
	bool scroll;
84
{
85
	register int i, tot;
86
 
87
	if (dot == one) {
88
		beep();
89
		return;
90
	}
91
	vsave();
92
	i = lineDOT() - 1;
93
	if (cnt > i) {
94
		ind -= cnt - i;
95
		if (ind < 0)
96
			ind = 0;
97
		cnt = i;
98
	}
99
	if (!scroll && cnt <= vcline) {
100
		vshow(dot - cnt, NOLINE);
101
		return;
102
	}
103
	cnt -= vcline, dot -= vcline, vcline = 0;
104
	if (hold & HOLDWIG)
105
		goto contxt;
106
	if (state == VISUAL && !AL && !SR &&
107
	    cnt <= WTOP - ZERO && vfit(dot - cnt, cnt) <= WTOP - ZERO)
108
		goto okr;
109
	tot = WECHO - ZERO;
110
	if (state != VISUAL || (!AL && !SR) || (!scroll && (cnt > tot || vfit(dot - cnt, cnt) > tot / 3 + 1))) {
111
		if (ind > basWLINES / 2)
112
			ind = basWLINES / 3;
113
contxt:
114
		vcontext(dot + ind - cnt, '.');
115
		return;
116
	}
117
okr:
118
	vrollR(cnt);
119
	if (scroll) {
120
		vcline += ind, dot += ind;
121
		if (vcline >= vcnt)
122
			dot -= vcline - vcnt + 1, vcline = vcnt - 1;
123
		getDOT();
124
	}
125
}
126
 
127
/*
128
 * Like vup, but scrolling down.
129
 */
130
vdown(cnt, ind, scroll)
131
	register int cnt, ind;
132
	bool scroll;
133
{
134
	register int i, tot;
135
 
136
	if (dot == dol) {
137
		beep();
138
		return;
139
	}
140
	vsave();
141
	i = dol - dot;
142
	if (cnt > i) {
143
		ind -= cnt - i;
144
		if (ind < 0)
145
			ind = 0;
146
		cnt = i;
147
	}
148
	i = vcnt - vcline - 1;
149
	if (!scroll && cnt <= i) {
150
		vshow(dot + cnt, NOLINE);
151
		return;
152
	}
153
	cnt -= i, dot += i, vcline += i;
154
	if (hold & HOLDWIG)
155
		goto dcontxt;
156
	if (!scroll) {
157
		tot = WECHO - ZERO;
158
		if (state != VISUAL || cnt - tot > 0 || vfit(dot, cnt) > tot / 3 + 1) {
159
dcontxt:
160
			vcontext(dot + cnt, '.');
161
			return;
162
		}
163
	}
164
	if (cnt > 0)
165
		vroll(cnt);
166
	if (state == VISUAL && scroll) {
167
		vcline -= ind, dot -= ind;
168
		if (vcline < 0)
169
			dot -= vcline, vcline = 0;
170
		getDOT();
171
	}
172
}
173
 
174
/*
175
 * Show line addr in context where on the screen.
176
 * Work here is in determining new top line implied by
177
 * this placement of line addr, since we always draw from the top.
178
 */
179
vcontext(addr, where)
180
	register line *addr;
181
	char where;
182
{
183
	register line *top;
184
 
118 7u83 185
	ex_getline(*addr);
105 7u83 186
	if (state != VISUAL)
187
		top = addr;
188
	else switch (where) {
189
 
190
	case '^':
191
		addr = vback(addr, basWLINES - vdepth());
118 7u83 192
		ex_getline(*addr);
105 7u83 193
		/* fall into ... */
194
 
195
	case '-':
196
		top = vback(addr, basWLINES - vdepth());
118 7u83 197
		ex_getline(*addr);
105 7u83 198
		break;
199
 
200
	case '.':
201
		top = vback(addr, basWLINES / 2 - vdepth());
118 7u83 202
		ex_getline(*addr);
105 7u83 203
		break;
204
 
205
	default:
206
		top = addr;
207
		break;
208
	}
209
	if (state == ONEOPEN && LINE(0) == WBOT)
210
		vup1();
211
	vcnt = vcline = 0;
212
	vclean();
213
	if (state == CRTOPEN)
214
		vup1();
215
	vshow(addr, top);
216
}
217
 
218
/*
219
 * Get a clean line.  If we are in a hard open
220
 * we may be able to reuse the line we are on
221
 * if it is blank.  This is a real win.
222
 */
223
vclean()
224
{
225
 
226
	if (state != VISUAL && state != CRTOPEN) {
227
		destcol = 0;
228
		if (!ateopr())
229
			vup1();
230
		vcnt = 0;
231
	}
232
}
233
 
234
/*
235
 * Show line addr with the specified top line on the screen.
236
 * Top may be 0; in this case have vcontext compute the top
237
 * (and call us recursively).  Eventually, we clear the screen
238
 * (or its open mode equivalent) and redraw.
239
 */
240
vshow(addr, top)
241
	line *addr, *top;
242
{
243
#ifndef CBREAK
244
	register bool fried = 0;
245
#endif
246
	register int cnt = addr - dot;
247
	register int i = vcline + cnt;
248
	short oldhold = hold;
249
 
121 7u83 250
#ifdef ADEBUG
251
	if (trace)
252
		tfixnl(), fprintf(trace, "vshow(%d)\n", 123), tvliny();
253
#endif
254
 
105 7u83 255
	if (state != HARDOPEN && state != ONEOPEN && i >= 0 && i < vcnt) {
256
		dot = addr;
257
		getDOT();
258
		vcline = i;
259
		return;
260
	}
261
	if (state != VISUAL) {
262
		dot = addr;
263
		vopen(dot, WBOT);
264
		return;
265
	}
266
	if (top == 0) {
267
		vcontext(addr, '.');
268
		return;
269
	}
270
	dot = top;
271
#ifndef CBREAK
272
	if (vcookit(2))
273
		fried++, vcook();
274
#endif
275
	oldhold = hold;
276
	hold |= HOLDAT;
277
	vclear();
278
	vreset(0);
279
	vredraw(WTOP);
280
	/* error if vcline >= vcnt ! */
281
	vcline = addr - top;
282
	dot = addr;
283
	getDOT();
284
	hold = oldhold;
285
	vsync(LASTLINE);
286
#ifndef CBREAK
287
	if (fried)
288
		flusho(), vraw();
289
#endif
290
}
291
 
292
/*
293
 * reset the state.
294
 * If inecho then leave us at the beginning of the echo
295
 * area;  we are called this way in the middle of a :e escape
296
 * from visual, e.g.
297
 */
298
vreset(inecho)
299
	bool inecho;
300
{
301
 
302
	vcnt = vcline = 0;
303
	WTOP = basWTOP;
304
	WLINES = basWLINES;
305
	if (inecho)
306
		splitw = 1, vgoto(WECHO, 0);
307
}
308
 
309
/*
310
 * Starting from which line preceding tp uses almost (but not more
311
 * than) cnt physical lines?
312
 */
313
line *
314
vback(tp, cnt)
315
	register int cnt;
316
	register line *tp;
317
{
318
	register int d;
319
 
320
	if (cnt > 0)
321
		for (; tp > one; tp--) {
118 7u83 322
			ex_getline(tp[-1]);
105 7u83 323
			d = vdepth();
324
			if (d > cnt)
325
				break;
326
			cnt -= d;
327
		}
328
	return (tp);
329
}
330
 
331
/*
332
 * How much scrolling will it take to roll cnt lines starting at tp?
333
 */
334
vfit(tp, cnt)
335
	register line *tp;
336
	int cnt;
337
{
338
	register int j;
339
 
340
	j = 0;
341
	while (cnt > 0) {
342
		cnt--;
118 7u83 343
		ex_getline(tp[cnt]);
105 7u83 344
		j += vdepth();
345
	}
346
	if (tp > dot)
347
		j -= WBOT - LASTLINE;
348
	return (j);
349
}
350
 
351
/*
352
 * Roll cnt lines onto the screen.
353
 */
354
vroll(cnt)
355
	register int cnt;
356
{
357
#ifndef CBREAK
358
	register bool fried = 0;
359
#endif
360
	short oldhold = hold;
361
 
362
#ifdef ADEBUG
363
	if (trace)
364
		tfixnl(), fprintf(trace, "vroll(%d)\n", cnt);
365
#endif
366
	if (state != VISUAL)
367
		hold |= HOLDAT|HOLDROL;
368
	if (WBOT == WECHO) {
369
		vcnt = 0;
370
		if (state == ONEOPEN)
371
			vup1();
372
	}
373
#ifndef CBREAK
374
	if (vcookit(cnt))
375
		fried++, vcook();
376
#endif
377
	for (; cnt > 0 && Peekkey != ATTN; cnt--) {
378
		dot++, vcline++;
379
		vopen(dot, LASTLINE);
380
		vscrap();
381
	}
382
	hold = oldhold;
383
	if (state == HARDOPEN)
384
		sethard();
385
	vsyncCL();
386
#ifndef CBREAK
387
	if (fried)
388
		flusho(), vraw();
389
#endif
390
}
391
 
392
/*
393
 * Roll backwards (scroll up).
394
 */
395
vrollR(cnt)
396
	register int cnt;
397
{
398
	register bool fried = 0;
399
	short oldhold = hold;
400
 
401
#ifdef ADEBUG
402
	if (trace)
403
		tfixnl(), fprintf(trace, "vrollR(%d), dot=%d\n", cnt, lineDOT());
404
#endif
405
#ifndef CBREAK
406
	if (vcookit(cnt))
407
		fried++, vcook();
408
#endif
409
	if (WBOT == WECHO)
410
		vcnt = 0;
411
	heldech = 0;
412
	hold |= HOLDAT|HOLDECH;
413
	for (; cnt > 0 && Peekkey != ATTN; cnt--) {
414
		dot--;
415
		vopen(dot, WTOP);
416
		vscrap();
417
	}
418
	hold = oldhold;
419
	if (heldech)
420
		vclrech(0);
421
	vsync(LINE(vcnt-1));
422
#ifndef CBREAK
423
	if (fried)
424
		flusho(), vraw();
425
#endif
426
}
427
 
428
/*
429
 * Go into cooked mode (allow interrupts) during
430
 * a scroll if we are at less than 1200 baud and not
431
 * a 'vi' command, of if we are in a 'vi' command and the
432
 * scroll is more than 2 full screens.
433
 *
434
 * BUG:		An interrupt during a scroll in this way
435
 *		dumps to command mode.
436
 */
437
vcookit(cnt)
438
	register int cnt;
439
{
440
 
441
	return (cnt > 1 && (o_speed < B1200 && !initev || cnt > LINES * 2));
442
}
443
 
444
/*
445
 * Determine displayed depth of current line.
446
 */
447
vdepth()
448
{
449
	register int d;
450
 
451
	d = (column(NOSTR) + WCOLS - 1 + (Putchar == listchar) + IN) / WCOLS;
452
#ifdef ADEBUG
453
	if (trace)
454
		tfixnl(), fprintf(trace, "vdepth returns %d\n", d == 0 ? 1 : d);
455
#endif
456
	return (d == 0 ? 1 : d);
457
}
458
 
459
/*
460
 * Move onto a new line, with cursor at position curs.
461
 */
462
vnline(curs)
463
	char *curs;
464
{
465
 
466
	if (curs)
467
		wcursor = curs;
468
	else if (vmoving)
469
		wcursor = vfindcol(vmovcol);
470
	else
471
		wcursor = vskipwh(linebuf);
472
	cursor = linebuf;
473
	vmove();
474
}