1 /* Cursor motion subroutines for GNU Emacs.
2 Copyright (C) 1985, 1995 Free Software Foundation, Inc.
3 based primarily on public domain code written by Chris Torek
5 This file is part of GNU Emacs.
7 GNU Emacs is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
26 #include "termhooks.h"
28 #define BIG 9999 /* 9999 good on VAXen. For 16 bit machines
35 int cost
; /* sums up costs */
49 fputc (c
& 0177, termscript
);
54 /* NEXT TWO ARE DONE WITH MACROS */
57 * Assume the cursor is at row row, column col. Normally used only after
58 * clearing the screen, when the cursor is at (0, 0), but what the heck,
59 * let's let the guy put it anywhere.
69 * Add n columns to the current cursor position.
77 * If cursor hit edge of screen, what happened?
78 * N.B.: DO NOT!! write past edge of screen. If you do, you
79 * deserve what you get. Furthermore, on terminals with
80 * autowrap (but not magicwrap), don't write in the last column
84 if (curX
== Wcm
.cm_cols
) {
86 * Well, if magicwrap, still there, past the edge of the
87 * screen (!). If autowrap, on the col 0 of the next line.
88 * Otherwise on last column.
93 else if (Wcm
.cm_autowrap
) {
95 curY
++; /* Beware end of screen! */
104 * Terminals with magicwrap (xn) don't all behave identically.
105 * The VT100 leaves the cursor in the last column but will wrap before
106 * printing the next character. I hear that the Concept terminal does
107 * the wrap immediately but ignores the next newline it sees. And some
108 * terminals just have buggy firmware, and think that the cursor is still
109 * in limbo if we use direct cursor addressing from the phantom column.
110 * The only guaranteed safe thing to do is to emit a CRLF immediately
111 * after we reach the last column; this takes us to a known state.
116 if (curX
== FrameCols
)
118 if (!MagicWrap
|| curY
>= FrameRows
- 1)
121 putc ('\r', termscript
);
124 putc ('\n', termscript
);
133 * (Re)Initialize the cost factors, given the output speed of the terminal
134 * in the variable ospeed. (Note: this holds B300, B9600, etc -- ie stuff
142 #define COST(x,e) (x ? (cost = 0, tputs (x, 1, e), cost) : BIG)
143 #define CMCOST(x,e) ((x == 0) ? BIG : (p = tgoto(x, 0, 0), COST(p ,e)))
145 Wcm
.cc_up
= COST (Wcm
.cm_up
, evalcost
);
146 Wcm
.cc_down
= COST (Wcm
.cm_down
, evalcost
);
147 Wcm
.cc_left
= COST (Wcm
.cm_left
, evalcost
);
148 Wcm
.cc_right
= COST (Wcm
.cm_right
, evalcost
);
149 Wcm
.cc_home
= COST (Wcm
.cm_home
, evalcost
);
150 Wcm
.cc_cr
= COST (Wcm
.cm_cr
, evalcost
);
151 Wcm
.cc_ll
= COST (Wcm
.cm_ll
, evalcost
);
152 Wcm
.cc_tab
= Wcm
.cm_tabwidth
? COST (Wcm
.cm_tab
, evalcost
) : BIG
;
155 * These last three are actually minimum costs. When (if) they are
156 * candidates for the least-cost motion, the real cost is computed.
157 * (Note that "0" is the assumed to generate the minimum cost.
158 * While this is not necessarily true, I have yet to see a terminal
159 * for which is not; all the terminals that have variable-cost
160 * cursor motion seem to take straight numeric values. --ACT)
163 Wcm
.cc_abs
= CMCOST (Wcm
.cm_abs
, evalcost
);
164 Wcm
.cc_habs
= CMCOST (Wcm
.cm_habs
, evalcost
);
165 Wcm
.cc_vabs
= CMCOST (Wcm
.cm_vabs
, evalcost
);
172 * Calculate the cost to move from (srcy, srcx) to (dsty, dstx) using
173 * up and down, and left and right, motions, and tabs. If doit is set
174 * actually perform the motion.
178 calccost (srcy
, srcx
, dsty
, dstx
, doit
)
191 /* If have just wrapped on a terminal with xn,
192 don't believe the cursor position: give up here
193 and force use of absolute positioning. */
195 if (curX
== Wcm
.cm_cols
)
199 if ((deltay
= dsty
- srcy
) == 0)
202 p
= Wcm
.cm_up
, c
= Wcm
.cc_up
, deltay
= -deltay
;
204 p
= Wcm
.cm_down
, c
= Wcm
.cc_down
;
205 if (c
== BIG
) { /* caint get thar from here */
210 totalcost
= c
* deltay
;
212 while (--deltay
>= 0)
213 tputs (p
, 1, cmputc
);
215 if ((deltax
= dstx
- srcx
) == 0)
218 p
= Wcm
.cm_left
, c
= Wcm
.cc_left
, deltax
= -deltax
;
219 goto dodelta
; /* skip all the tab junk */
221 /* Tabs (the toughie) */
222 if (Wcm
.cc_tab
>= BIG
|| !Wcm
.cm_usetabs
)
223 goto olddelta
; /* forget it! */
226 * ntabs is # tabs towards but not past dstx; n2tabs is one more
227 * (ie past dstx), but this is only valid if that is not past the
228 * right edge of the screen. We can check that at the same time
229 * as we figure out where we would be if we use the tabs (which
230 * we will put into tabx (for ntabs) and tab2x (for n2tabs)).
233 ntabs
= (deltax
+ srcx
% Wcm
.cm_tabwidth
) / Wcm
.cm_tabwidth
;
235 tabx
= (srcx
/ Wcm
.cm_tabwidth
+ ntabs
) * Wcm
.cm_tabwidth
;
236 tab2x
= tabx
+ Wcm
.cm_tabwidth
;
238 if (tab2x
>= Wcm
.cm_cols
) /* too far (past edge) */
242 * Now set tabcost to the cost for using ntabs, and c to the cost
243 * for using n2tabs, then pick the minimum.
246 /* cost for ntabs + cost for right motion */
247 tabcost
= ntabs
? ntabs
* Wcm
.cc_tab
+ (dstx
- tabx
) * Wcm
.cc_right
250 /* cost for n2tabs + cost for left motion */
251 c
= n2tabs
? n2tabs
* Wcm
.cc_tab
+ (tab2x
- dstx
) * Wcm
.cc_left
254 if (c
< tabcost
) /* then cheaper to overshoot & back up */
255 ntabs
= n2tabs
, tabcost
= c
, tabx
= tab2x
;
257 if (tabcost
>= BIG
) /* caint use tabs */
261 * See if tabcost is less than just moving right
264 if (tabcost
< (deltax
* Wcm
.cc_right
)) {
265 totalcost
+= tabcost
; /* use the tabs */
268 tputs (Wcm
.cm_tab
, 1, cmputc
);
273 * Now might as well just recompute the delta.
277 if ((deltax
= dstx
- srcx
) == 0)
281 p
= Wcm
.cm_right
, c
= Wcm
.cc_right
;
283 p
= Wcm
.cm_left
, c
= Wcm
.cc_left
, deltax
= -deltax
;
286 if (c
== BIG
) { /* caint get thar from here */
292 totalcost
+= c
* deltax
;
294 while (--deltax
>= 0)
295 tputs (p
, 1, cmputc
);
323 /* First the degenerate case */
324 if (row
== curY
&& col
== curX
) /* already there */
327 if (curY
>= 0 && curX
>= 0)
329 /* We may have quick ways to go to the upper-left, bottom-left,
330 * start-of-line, or start-of-next-line. Or it might be best to
331 * start where we are. Examine the options, and pick the cheapest.
334 relcost
= calccost (curY
, curX
, row
, col
, 0);
336 if ((homecost
= Wcm
.cc_home
) < BIG
)
337 homecost
+= calccost (0, 0, row
, col
, 0);
338 if (homecost
< relcost
)
339 relcost
= homecost
, use
= USEHOME
;
340 if ((llcost
= Wcm
.cc_ll
) < BIG
)
341 llcost
+= calccost (Wcm
.cm_rows
- 1, 0, row
, col
, 0);
342 if (llcost
< relcost
)
343 relcost
= llcost
, use
= USELL
;
344 if ((crcost
= Wcm
.cc_cr
) < BIG
) {
346 if (curY
+ 1 >= Wcm
.cm_rows
)
349 crcost
+= calccost (curY
+ 1, 0, row
, col
, 0);
351 crcost
+= calccost (curY
, 0, row
, col
, 0);
353 if (crcost
< relcost
)
354 relcost
= crcost
, use
= USECR
;
355 directcost
= Wcm
.cc_abs
, dcm
= Wcm
.cm_abs
;
356 if (row
== curY
&& Wcm
.cc_habs
< BIG
)
357 directcost
= Wcm
.cc_habs
, dcm
= Wcm
.cm_habs
;
358 else if (col
== curX
&& Wcm
.cc_vabs
< BIG
)
359 directcost
= Wcm
.cc_vabs
, dcm
= Wcm
.cm_vabs
;
363 directcost
= 0, relcost
= 100000;
368 * In the following comparison, the = in <= is because when the costs
369 * are the same, it looks nicer (I think) to move directly there.
371 if (directcost
<= relcost
)
373 /* compute REAL direct cost */
375 p
= dcm
== Wcm
.cm_habs
? tgoto (dcm
, row
, col
) :
376 tgoto (dcm
, col
, row
);
377 tputs (p
, 1, evalcost
);
379 { /* really is cheaper */
380 tputs (p
, 1, cmputc
);
381 curY
= row
, curX
= col
;
389 tputs (Wcm
.cm_home
, 1, cmputc
);
394 tputs (Wcm
.cm_ll
, 1, cmputc
);
395 curY
= Wcm
.cm_rows
- 1, curX
= 0;
399 tputs (Wcm
.cm_cr
, 1, cmputc
);
406 (void) calccost (curY
, curX
, row
, col
, 1);
407 curY
= row
, curX
= col
;
410 /* Clear out all terminal info.
411 Used before copying into it the info on the actual terminal.
416 bzero (&Wcm
, sizeof Wcm
);
423 * Return 0 if can do CM.
424 * Return -1 if cannot.
425 * Return -2 if size not specified.
431 if (Wcm
.cm_abs
&& !Wcm
.cm_ds
)
436 /* Require up and left, and, if no absolute, down and right */
437 if (!Wcm
.cm_up
|| !Wcm
.cm_left
)
439 if (!Wcm
.cm_abs
&& (!Wcm
.cm_down
|| !Wcm
.cm_right
))
441 /* Check that we know the size of the screen.... */
442 if (Wcm
.cm_rows
<= 0 || Wcm
.cm_cols
<= 0)