xref: /onnv-gate/usr/src/lib/libcurses/screen/mvcur.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 1997 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate /*	Copyright (c) 1988 AT&T	*/
28*0Sstevel@tonic-gate /*	  All Rights Reserved	*/
29*0Sstevel@tonic-gate 
30*0Sstevel@tonic-gate /*
31*0Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
32*0Sstevel@tonic-gate  * The Regents of the University of California
33*0Sstevel@tonic-gate  * All Rights Reserved
34*0Sstevel@tonic-gate  *
35*0Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
36*0Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
37*0Sstevel@tonic-gate  * contributors.
38*0Sstevel@tonic-gate  */
39*0Sstevel@tonic-gate 
40*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
41*0Sstevel@tonic-gate 
42*0Sstevel@tonic-gate /*LINTLIBRARY*/
43*0Sstevel@tonic-gate 
44*0Sstevel@tonic-gate #include	<sys/types.h>
45*0Sstevel@tonic-gate #include	"curses_inc.h"
46*0Sstevel@tonic-gate 
47*0Sstevel@tonic-gate /*
48*0Sstevel@tonic-gate  * Cursor motion optimization routine.  This routine takes as parameters
49*0Sstevel@tonic-gate  * the screen positions that the cursor is currently at, and the position
50*0Sstevel@tonic-gate  * you want it to be at, and it will move the cursor there very
51*0Sstevel@tonic-gate  * efficiently.  It isn't really optimal, since several approximations
52*0Sstevel@tonic-gate  * are taken in the interests of efficiency and simplicity.  The code
53*0Sstevel@tonic-gate  * here considers directly addressing the cursor, and also considers
54*0Sstevel@tonic-gate  * local motions using left, right, up, down, tabs, backtabs, vertical
55*0Sstevel@tonic-gate  * and horizontal addressing, and parameterized motions.  It does not
56*0Sstevel@tonic-gate  * consider using home down, or taking advantage of automatic margins on
57*0Sstevel@tonic-gate  * any of the four directions.  (Two of these directions, left and right,
58*0Sstevel@tonic-gate  * are well defined by the am and bw capabilities, but up and down are
59*0Sstevel@tonic-gate  * not defined, nor are tab or backtab off the ends.)
60*0Sstevel@tonic-gate  *
61*0Sstevel@tonic-gate  * General strategies considered:
62*0Sstevel@tonic-gate  *	CA	Direct Cursor Addressing
63*0Sstevel@tonic-gate  *	LM	Local Motions from the old position
64*0Sstevel@tonic-gate  *	HR	Home + Local Motions from upper left corner
65*0Sstevel@tonic-gate  *	HDR	Home Down + Local Motions from lower left corner
66*0Sstevel@tonic-gate  *	CR	CR + Local Motions from left margin
67*0Sstevel@tonic-gate  *
68*0Sstevel@tonic-gate  * Local Motions can include
69*0Sstevel@tonic-gate  *	Up	cuu, cuu1, vpa
70*0Sstevel@tonic-gate  *	Down	cud, cud1, vpa
71*0Sstevel@tonic-gate  *	Left	cul, cul1, hpa, bs, cbt
72*0Sstevel@tonic-gate  *	Right	cuf, cuf1, hpa, tab, char moved over
73*0Sstevel@tonic-gate  */
74*0Sstevel@tonic-gate 
75*0Sstevel@tonic-gate /* This is called _ISMARK2 so it doesn't conflict with _ISMARK1 in wrefresh.c */
76*0Sstevel@tonic-gate 
77*0Sstevel@tonic-gate #define	_ISMARK2(x)	(mks[(x) / BITSPERBYTE] & (1<<((x) % BITSPERBYTE)))
78*0Sstevel@tonic-gate 
79*0Sstevel@tonic-gate #define	H_UP	-1
80*0Sstevel@tonic-gate #define	H_DO	1
81*0Sstevel@tonic-gate 
82*0Sstevel@tonic-gate static	int	Newy;
83*0Sstevel@tonic-gate static  int	_homefirst(int, int, int, int),
84*0Sstevel@tonic-gate 		_mvrel(int, int, int, int, int),
85*0Sstevel@tonic-gate 		_mvvert(int, int, int), _mvhor(int, int, int),
86*0Sstevel@tonic-gate 		_mvright(int, int, int), _mvleft(int, int, int);
87*0Sstevel@tonic-gate 
88*0Sstevel@tonic-gate int
mvcur(int cury,int curx,int newy,int newx)89*0Sstevel@tonic-gate mvcur(int cury, int curx, int newy, int newx)
90*0Sstevel@tonic-gate {
91*0Sstevel@tonic-gate 	int	hu,	/* cost home + relative */
92*0Sstevel@tonic-gate 		hd,	/* cost home-down + relative */
93*0Sstevel@tonic-gate 		rl,	/* cost relative */
94*0Sstevel@tonic-gate 		cm;	/* cost direct cursor motion */
95*0Sstevel@tonic-gate 
96*0Sstevel@tonic-gate 	/* obvious case */
97*0Sstevel@tonic-gate 	if (cury == newy && curx == newx)
98*0Sstevel@tonic-gate 		return (OK);
99*0Sstevel@tonic-gate 
100*0Sstevel@tonic-gate 	/* not in the right mode for cursor movement */
101*0Sstevel@tonic-gate 	if (SP->fl_endwin)
102*0Sstevel@tonic-gate 		return (ERR);
103*0Sstevel@tonic-gate 
104*0Sstevel@tonic-gate 	if (!move_standout_mode && curscr->_attrs && !SP->_mks)
105*0Sstevel@tonic-gate 		_VIDS(A_NORMAL, curscr->_attrs);
106*0Sstevel@tonic-gate 
107*0Sstevel@tonic-gate 	if (!move_insert_mode && SP->phys_irm)
108*0Sstevel@tonic-gate 		_OFFINSERT();
109*0Sstevel@tonic-gate 
110*0Sstevel@tonic-gate 	Newy = newy;
111*0Sstevel@tonic-gate 
112*0Sstevel@tonic-gate 	/* cost of using cm */
113*0Sstevel@tonic-gate 	cm = _COST(Cursor_address);
114*0Sstevel@tonic-gate 
115*0Sstevel@tonic-gate 	rl = hd = hu = LARGECOST;
116*0Sstevel@tonic-gate 
117*0Sstevel@tonic-gate 	/* baudrate optimization */
118*0Sstevel@tonic-gate 	if (cm < LARGECOST && SP->baud >= 2400 &&
119*0Sstevel@tonic-gate 	    cury >= 0 && cury < curscr->_maxy &&
120*0Sstevel@tonic-gate 	    curx >= 0 && curx < curscr->_maxx) {
121*0Sstevel@tonic-gate 		if (cursor_down && (newy == (cury + 1)) &&
122*0Sstevel@tonic-gate 		    ((newx == curx) || (newx == 0 && carriage_return))) {
123*0Sstevel@tonic-gate 			if (newx != curx)
124*0Sstevel@tonic-gate 				_PUTS(carriage_return, 1);
125*0Sstevel@tonic-gate 			_PUTS(cursor_down, 1);
126*0Sstevel@tonic-gate 			goto done;
127*0Sstevel@tonic-gate 		}
128*0Sstevel@tonic-gate 
129*0Sstevel@tonic-gate 		/* fast horizontal move */
130*0Sstevel@tonic-gate 		if (cury == newy && newx < curx - 4 && newx > curx + 4) {
131*0Sstevel@tonic-gate 			if (newx < curx)
132*0Sstevel@tonic-gate 				rl = _mvleft(curx, newx, FALSE);
133*0Sstevel@tonic-gate 			else
134*0Sstevel@tonic-gate 				rl = _mvright(curx, newx, FALSE);
135*0Sstevel@tonic-gate 			if (rl < cm) {
136*0Sstevel@tonic-gate 				if (newx < curx)
137*0Sstevel@tonic-gate 					rl = _mvleft(curx, newx, TRUE);
138*0Sstevel@tonic-gate 				else
139*0Sstevel@tonic-gate 					rl = _mvright(curx, newx, TRUE);
140*0Sstevel@tonic-gate 				goto done;
141*0Sstevel@tonic-gate 			}
142*0Sstevel@tonic-gate 		}
143*0Sstevel@tonic-gate 	}
144*0Sstevel@tonic-gate 
145*0Sstevel@tonic-gate 	/* cost using relative movements */
146*0Sstevel@tonic-gate 	if (rl >= LARGECOST && cury >= 0 && cury < curscr->_maxy &&
147*0Sstevel@tonic-gate 	    curx >= 0 && curx < curscr->_maxx)
148*0Sstevel@tonic-gate 		rl = _mvrel(cury, curx, newy, newx, FALSE);
149*0Sstevel@tonic-gate 
150*0Sstevel@tonic-gate 	/* cost of homing to upper-left corner first */
151*0Sstevel@tonic-gate 	if (cursor_home)
152*0Sstevel@tonic-gate 		hu = _homefirst(newy, newx, H_UP, FALSE);
153*0Sstevel@tonic-gate 
154*0Sstevel@tonic-gate 	/* cost of homing to lower-left corner first */
155*0Sstevel@tonic-gate 	if (cursor_to_ll)
156*0Sstevel@tonic-gate 		hd = _homefirst(newy, newx, H_DO, FALSE);
157*0Sstevel@tonic-gate 
158*0Sstevel@tonic-gate 	/* can't do any one of them */
159*0Sstevel@tonic-gate 	if (cm >= LARGECOST && rl >= LARGECOST && hu >= LARGECOST &&
160*0Sstevel@tonic-gate 	    hd >= LARGECOST)
161*0Sstevel@tonic-gate 		return (ERR);
162*0Sstevel@tonic-gate 
163*0Sstevel@tonic-gate 	/* do the best one */
164*0Sstevel@tonic-gate 	if (cm <= rl && cm <= hu && cm <= hd)
165*0Sstevel@tonic-gate 		_PUTS(tparm_p2(cursor_address, newy, newx), 1);
166*0Sstevel@tonic-gate 	else
167*0Sstevel@tonic-gate 		if (rl <= hu && rl <= hd)
168*0Sstevel@tonic-gate 			(void) _mvrel(cury, curx, newy, newx, TRUE);
169*0Sstevel@tonic-gate 		else
170*0Sstevel@tonic-gate 			(void) _homefirst(newy, newx, hu <= hd ?
171*0Sstevel@tonic-gate 			    H_UP : H_DO, TRUE);
172*0Sstevel@tonic-gate 
173*0Sstevel@tonic-gate done:
174*0Sstevel@tonic-gate 	/* update cursor position */
175*0Sstevel@tonic-gate 	/*LINTED*/
176*0Sstevel@tonic-gate 	curscr->_curx = (short) newx;
177*0Sstevel@tonic-gate 	/*LINTED*/
178*0Sstevel@tonic-gate 	curscr->_cury = (short) newy;
179*0Sstevel@tonic-gate 
180*0Sstevel@tonic-gate 	return (OK);
181*0Sstevel@tonic-gate }
182*0Sstevel@tonic-gate 
183*0Sstevel@tonic-gate /* Move by homing first. */
184*0Sstevel@tonic-gate 
185*0Sstevel@tonic-gate static int
_homefirst(int ny,int nx,int type,int doit)186*0Sstevel@tonic-gate _homefirst(int ny, int nx, int type, int doit)
187*0Sstevel@tonic-gate {
188*0Sstevel@tonic-gate 	char	*home;
189*0Sstevel@tonic-gate 	int	cy, cost;
190*0Sstevel@tonic-gate 
191*0Sstevel@tonic-gate 	if (type == H_UP) {
192*0Sstevel@tonic-gate 		home = cursor_home;
193*0Sstevel@tonic-gate 		cost = _COST(Cursor_home);
194*0Sstevel@tonic-gate 		cy = 0;
195*0Sstevel@tonic-gate 	} else {
196*0Sstevel@tonic-gate 		home = cursor_to_ll;
197*0Sstevel@tonic-gate 		cost = _COST(Cursor_to_ll);
198*0Sstevel@tonic-gate 		cy = curscr->_maxy - 1;
199*0Sstevel@tonic-gate 	}
200*0Sstevel@tonic-gate 
201*0Sstevel@tonic-gate 	if (!home)
202*0Sstevel@tonic-gate 		return (LARGECOST);
203*0Sstevel@tonic-gate 	if (!doit)
204*0Sstevel@tonic-gate 		return (cost + _mvrel(cy, 0, ny, nx, FALSE));
205*0Sstevel@tonic-gate 
206*0Sstevel@tonic-gate 	_PUTS(home, 1);
207*0Sstevel@tonic-gate 	return (_mvrel(cy, 0, ny, nx, TRUE));
208*0Sstevel@tonic-gate }
209*0Sstevel@tonic-gate 
210*0Sstevel@tonic-gate /* Move relatively */
211*0Sstevel@tonic-gate 
212*0Sstevel@tonic-gate static int
_mvrel(int cy,int cx,int ny,int nx,int doit)213*0Sstevel@tonic-gate _mvrel(int cy, int cx, int ny, int nx, int doit)
214*0Sstevel@tonic-gate {
215*0Sstevel@tonic-gate 	int	cv, ch;
216*0Sstevel@tonic-gate 
217*0Sstevel@tonic-gate 	/* do in this order since _mvhor may need the curscr image */
218*0Sstevel@tonic-gate 	cv = _mvvert(cy, ny, doit);
219*0Sstevel@tonic-gate 	ch = _mvhor(cx, nx, doit);
220*0Sstevel@tonic-gate 
221*0Sstevel@tonic-gate 	return (cv + ch);
222*0Sstevel@tonic-gate }
223*0Sstevel@tonic-gate 
224*0Sstevel@tonic-gate /* Move vertically */
225*0Sstevel@tonic-gate 
226*0Sstevel@tonic-gate static int
_mvvert(int cy,int ny,int doit)227*0Sstevel@tonic-gate _mvvert(int cy, int ny, int doit)
228*0Sstevel@tonic-gate {
229*0Sstevel@tonic-gate 	char	*ve;
230*0Sstevel@tonic-gate 	int	dy, st_1, st_n, cv;
231*0Sstevel@tonic-gate 
232*0Sstevel@tonic-gate 	if (cy == ny)
233*0Sstevel@tonic-gate 		goto out;
234*0Sstevel@tonic-gate 
235*0Sstevel@tonic-gate 	/* cost of stepwise movement */
236*0Sstevel@tonic-gate 	if (cy < ny) {
237*0Sstevel@tonic-gate 		dy = ny-cy;
238*0Sstevel@tonic-gate 		st_1 = _COST(Cursor_down) * dy;
239*0Sstevel@tonic-gate 		st_n = _COST(Parm_down_cursor);
240*0Sstevel@tonic-gate 	} else {
241*0Sstevel@tonic-gate 		dy = cy-ny;
242*0Sstevel@tonic-gate 		st_1 = _COST(Cursor_up) * dy;
243*0Sstevel@tonic-gate 		st_n = _COST(Parm_up_cursor);
244*0Sstevel@tonic-gate 	}
245*0Sstevel@tonic-gate 
246*0Sstevel@tonic-gate 	/* cost of using vertical move */
247*0Sstevel@tonic-gate 	cv = _COST(Row_address);
248*0Sstevel@tonic-gate 
249*0Sstevel@tonic-gate 	/* if calculating cost only */
250*0Sstevel@tonic-gate 	if (!doit)
251*0Sstevel@tonic-gate 		return ((cv < st_1 && cv < st_n) ? cv :
252*0Sstevel@tonic-gate 		    (st_n < st_1) ? st_n : st_1);
253*0Sstevel@tonic-gate 
254*0Sstevel@tonic-gate 	/* do it */
255*0Sstevel@tonic-gate 	if (cv < st_1 && cv < st_n)
256*0Sstevel@tonic-gate 		_PUTS(tparm_p1(row_address, ny), 1);
257*0Sstevel@tonic-gate 	else
258*0Sstevel@tonic-gate 		if (st_n < st_1) {
259*0Sstevel@tonic-gate 			if (cy < ny)
260*0Sstevel@tonic-gate 				_PUTS(tparm_p1(parm_down_cursor, dy), 1);
261*0Sstevel@tonic-gate 			else
262*0Sstevel@tonic-gate 				_PUTS(tparm_p1(parm_up_cursor, dy), 1);
263*0Sstevel@tonic-gate 		} else {
264*0Sstevel@tonic-gate 			if (cy < ny)
265*0Sstevel@tonic-gate 				ve = cursor_down;
266*0Sstevel@tonic-gate 			else
267*0Sstevel@tonic-gate 				ve = cursor_up;
268*0Sstevel@tonic-gate 			for (; dy > 0; --dy)
269*0Sstevel@tonic-gate 				_PUTS(ve, 1);
270*0Sstevel@tonic-gate 		}
271*0Sstevel@tonic-gate 
272*0Sstevel@tonic-gate out:
273*0Sstevel@tonic-gate 	return (0);
274*0Sstevel@tonic-gate }
275*0Sstevel@tonic-gate 
276*0Sstevel@tonic-gate /* Move horizontally */
277*0Sstevel@tonic-gate 
278*0Sstevel@tonic-gate static int
_mvhor(int cx,int nx,int doit)279*0Sstevel@tonic-gate _mvhor(int cx, int nx, int doit)
280*0Sstevel@tonic-gate {
281*0Sstevel@tonic-gate 	int	st, ch, hl;
282*0Sstevel@tonic-gate 
283*0Sstevel@tonic-gate 	if (cx == nx)
284*0Sstevel@tonic-gate 		goto out;
285*0Sstevel@tonic-gate 
286*0Sstevel@tonic-gate 	/* cost using horizontal move */
287*0Sstevel@tonic-gate 	ch = _COST(Row_address);
288*0Sstevel@tonic-gate 
289*0Sstevel@tonic-gate 	/* cost doing stepwise */
290*0Sstevel@tonic-gate 	st = cx < nx ? _mvright(cx, nx, FALSE) : _mvleft(cx, nx, FALSE);
291*0Sstevel@tonic-gate 
292*0Sstevel@tonic-gate 	/* cost homeleft first */
293*0Sstevel@tonic-gate 	hl = (_COST(Carriage_return) < LARGECOST) ?
294*0Sstevel@tonic-gate 	    _COST(Carriage_return) + _mvright(0, nx, FALSE) : LARGECOST;
295*0Sstevel@tonic-gate 
296*0Sstevel@tonic-gate 	if (!doit)
297*0Sstevel@tonic-gate 		return ((ch < st && ch < hl) ? ch : (hl < st ? hl : st));
298*0Sstevel@tonic-gate 
299*0Sstevel@tonic-gate 	if (ch < st && ch < hl)
300*0Sstevel@tonic-gate 		_PUTS(tparm_p1(column_address, nx), 1);
301*0Sstevel@tonic-gate 	else
302*0Sstevel@tonic-gate 		if (hl < st) {
303*0Sstevel@tonic-gate 			_PUTS(carriage_return, 1);
304*0Sstevel@tonic-gate 			(void) _mvright(0, nx, TRUE);
305*0Sstevel@tonic-gate 		} else {
306*0Sstevel@tonic-gate 			if (cx < nx)
307*0Sstevel@tonic-gate 				(void) _mvright(cx, nx, TRUE);
308*0Sstevel@tonic-gate 			else
309*0Sstevel@tonic-gate 				(void) _mvleft(cx, nx, TRUE);
310*0Sstevel@tonic-gate 	}
311*0Sstevel@tonic-gate out:
312*0Sstevel@tonic-gate 	return (0);
313*0Sstevel@tonic-gate }
314*0Sstevel@tonic-gate 
315*0Sstevel@tonic-gate /* Move right. */
316*0Sstevel@tonic-gate 
317*0Sstevel@tonic-gate static int
_mvright(int cx,int nx,int doit)318*0Sstevel@tonic-gate _mvright(int cx, int nx, int doit)
319*0Sstevel@tonic-gate {
320*0Sstevel@tonic-gate 	chtype	*scp;
321*0Sstevel@tonic-gate 	char	*mks;
322*0Sstevel@tonic-gate 	int	nt, tx, x, stcost, iscont;
323*0Sstevel@tonic-gate 
324*0Sstevel@tonic-gate 	if (!cursor_right && !parm_right_cursor)
325*0Sstevel@tonic-gate 		return (LARGECOST);
326*0Sstevel@tonic-gate 
327*0Sstevel@tonic-gate 	scp = curscr->_y[Newy];
328*0Sstevel@tonic-gate 	mks = magic_cookie_glitch >= 0 ? SP->_mks[Newy] : NULL;
329*0Sstevel@tonic-gate 
330*0Sstevel@tonic-gate 	if (cursor_right) {
331*0Sstevel@tonic-gate 		/* number of tabs used in stepwise movement */
332*0Sstevel@tonic-gate 		nt = tab ? (nx / TABSIZE - cx / TABSIZE) : 0;
333*0Sstevel@tonic-gate 		tx = (nt > 0) ? (cx / TABSIZE + nt) * TABSIZE : cx;
334*0Sstevel@tonic-gate 
335*0Sstevel@tonic-gate 		/* calculate stepwise cost */
336*0Sstevel@tonic-gate 		stcost = nt * _COST(Tab);
337*0Sstevel@tonic-gate 		iscont = 0;
338*0Sstevel@tonic-gate 		for (x = tx; x < nx; ++x) {
339*0Sstevel@tonic-gate 			if (iscont == 0 && !ISCBIT(scp[x]))
340*0Sstevel@tonic-gate 				iscont = 1;
341*0Sstevel@tonic-gate 			if ((!ceol_standout_glitch && !mks &&
342*0Sstevel@tonic-gate 			    _ATTR(scp[x]) == curscr->_attrs) ||
343*0Sstevel@tonic-gate 			    ceol_standout_glitch || (mks && !_ISMARK2(x))) {
344*0Sstevel@tonic-gate 				if (!ISMBIT(scp[x]))
345*0Sstevel@tonic-gate 					stcost += 1;
346*0Sstevel@tonic-gate 				else if (iscont && !(nx - x == 1 && nx <
347*0Sstevel@tonic-gate 				    curscr->_maxx && ISCBIT(scp[nx])))
348*0Sstevel@tonic-gate 					stcost += 1;
349*0Sstevel@tonic-gate 				else
350*0Sstevel@tonic-gate 					stcost += _COST(Cursor_right);
351*0Sstevel@tonic-gate 			} else
352*0Sstevel@tonic-gate 				stcost += _COST(Cursor_right);
353*0Sstevel@tonic-gate 		}
354*0Sstevel@tonic-gate 	} else
355*0Sstevel@tonic-gate 		stcost = LARGECOST;
356*0Sstevel@tonic-gate 
357*0Sstevel@tonic-gate 	if (!doit)
358*0Sstevel@tonic-gate 		return ((_COST(Parm_right_cursor) < stcost) ?
359*0Sstevel@tonic-gate 		    _COST(Parm_right_cursor) : stcost);
360*0Sstevel@tonic-gate 
361*0Sstevel@tonic-gate 	/* actually move */
362*0Sstevel@tonic-gate 	if (_COST(Parm_right_cursor) < stcost)
363*0Sstevel@tonic-gate 		_PUTS(tparm_p1(parm_right_cursor, nx-cx), 1);
364*0Sstevel@tonic-gate 	else {
365*0Sstevel@tonic-gate 		if (SP->phys_irm)
366*0Sstevel@tonic-gate 			_OFFINSERT();
367*0Sstevel@tonic-gate 		for (; nt > 0; --nt)
368*0Sstevel@tonic-gate 			_PUTS(tab, 1);
369*0Sstevel@tonic-gate 		iscont = 0;
370*0Sstevel@tonic-gate 		for (x = tx; x < nx; ++x) {
371*0Sstevel@tonic-gate 			if (iscont == 0 && !ISCBIT(scp[x]))
372*0Sstevel@tonic-gate 				iscont = 1;
373*0Sstevel@tonic-gate 			if ((!ceol_standout_glitch && !mks &&
374*0Sstevel@tonic-gate 			    _ATTR(scp[x]) == curscr->_attrs) ||
375*0Sstevel@tonic-gate 			    ceol_standout_glitch || (mks && !_ISMARK2(x))) {
376*0Sstevel@tonic-gate 				if (!ISMBIT(scp[x]))
377*0Sstevel@tonic-gate 					(void) _outwch(_CHAR(scp[x]));
378*0Sstevel@tonic-gate 				else if (iscont && !(nx - x == 1 &&
379*0Sstevel@tonic-gate 				    nx < curscr->_maxx && ISCBIT(scp[nx])))
380*0Sstevel@tonic-gate 					(void) _outwch(_CHAR(scp[x]));
381*0Sstevel@tonic-gate 				else
382*0Sstevel@tonic-gate 					_PUTS(cursor_right, 1);
383*0Sstevel@tonic-gate 			} else
384*0Sstevel@tonic-gate 				_PUTS(cursor_right, 1);
385*0Sstevel@tonic-gate 		}
386*0Sstevel@tonic-gate 	}
387*0Sstevel@tonic-gate 
388*0Sstevel@tonic-gate 	return (0);
389*0Sstevel@tonic-gate }
390*0Sstevel@tonic-gate 
391*0Sstevel@tonic-gate /* Move left */
392*0Sstevel@tonic-gate 
393*0Sstevel@tonic-gate static int
_mvleft(int cx,int nx,int doit)394*0Sstevel@tonic-gate _mvleft(int cx, int nx, int doit)
395*0Sstevel@tonic-gate {
396*0Sstevel@tonic-gate 	int	tx, nt, x, stcost;
397*0Sstevel@tonic-gate 
398*0Sstevel@tonic-gate 	if (!cursor_left && !parm_left_cursor)
399*0Sstevel@tonic-gate 		return (LARGECOST);
400*0Sstevel@tonic-gate 
401*0Sstevel@tonic-gate 	if (cursor_left) {
402*0Sstevel@tonic-gate 		/* stepwise cost */
403*0Sstevel@tonic-gate 		tx = cx;
404*0Sstevel@tonic-gate 		nt = 0;
405*0Sstevel@tonic-gate 		if (back_tab) {
406*0Sstevel@tonic-gate 			/* the TAB position >= nx */
407*0Sstevel@tonic-gate 			x = (nx % TABSIZE) ? (nx / TABSIZE + 1) * TABSIZE : nx;
408*0Sstevel@tonic-gate 
409*0Sstevel@tonic-gate 			/* # of tabs used and position after using them */
410*0Sstevel@tonic-gate 			if (x < cx) {
411*0Sstevel@tonic-gate 				nt = (cx / TABSIZE - x / TABSIZE) +
412*0Sstevel@tonic-gate 				    ((cx % TABSIZE) ? 1 : 0);
413*0Sstevel@tonic-gate 				tx = x;
414*0Sstevel@tonic-gate 			}
415*0Sstevel@tonic-gate 		}
416*0Sstevel@tonic-gate 		stcost = nt * _COST(Back_tab) + (tx-nx) * _COST(Cursor_left);
417*0Sstevel@tonic-gate 	} else
418*0Sstevel@tonic-gate 		stcost = LARGECOST;
419*0Sstevel@tonic-gate 
420*0Sstevel@tonic-gate 	/* get cost only */
421*0Sstevel@tonic-gate 	if (!doit)
422*0Sstevel@tonic-gate 		return ((_COST(Parm_left_cursor) < stcost) ?
423*0Sstevel@tonic-gate 		    _COST(Parm_left_cursor) : stcost);
424*0Sstevel@tonic-gate 
425*0Sstevel@tonic-gate 	/* doit */
426*0Sstevel@tonic-gate 	if (_COST(Parm_left_cursor) < stcost)
427*0Sstevel@tonic-gate 		_PUTS(tparm_p1(parm_left_cursor, cx - nx), 1);
428*0Sstevel@tonic-gate 	else {
429*0Sstevel@tonic-gate 		for (; nt > 0; --nt)
430*0Sstevel@tonic-gate 		    _PUTS(back_tab, 1);
431*0Sstevel@tonic-gate 		for (; tx > nx; --tx)
432*0Sstevel@tonic-gate 		    _PUTS(cursor_left, 1);
433*0Sstevel@tonic-gate 	}
434*0Sstevel@tonic-gate 
435*0Sstevel@tonic-gate 	return (0);
436*0Sstevel@tonic-gate }
437