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"
32 #define BIG 9999 /* 9999 good on VAXen. For 16 bit machines
39 int cost
; /* sums up costs */
55 fputc (c
& 0177, termscript
);
60 /* NEXT TWO ARE DONE WITH MACROS */
63 * Assume the cursor is at row row, column col. Normally used only after
64 * clearing the screen, when the cursor is at (0, 0), but what the heck,
65 * let's let the guy put it anywhere.
75 * Add n columns to the current cursor position.
83 * If cursor hit edge of screen, what happened?
84 * N.B.: DO NOT!! write past edge of screen. If you do, you
85 * deserve what you get. Furthermore, on terminals with
86 * autowrap (but not magicwrap), don't write in the last column
90 if (curX
== Wcm
.cm_cols
) {
92 * Well, if magicwrap, still there, past the edge of the
93 * screen (!). If autowrap, on the col 0 of the next line.
94 * Otherwise on last column.
99 else if (Wcm
.cm_autowrap
) {
101 curY
++; /* Beware end of screen! */
110 * Terminals with magicwrap (xn) don't all behave identically.
111 * The VT100 leaves the cursor in the last column but will wrap before
112 * printing the next character. I hear that the Concept terminal does
113 * the wrap immediately but ignores the next newline it sees. And some
114 * terminals just have buggy firmware, and think that the cursor is still
115 * in limbo if we use direct cursor addressing from the phantom column.
116 * The only guaranteed safe thing to do is to emit a CRLF immediately
117 * after we reach the last column; this takes us to a known state.
122 if (curX
== FrameCols
)
124 if (!MagicWrap
|| curY
>= FrameRows
- 1)
127 putc ('\r', termscript
);
130 putc ('\n', termscript
);
139 * (Re)Initialize the cost factors, given the output speed of the terminal
140 * in the variable ospeed. (Note: this holds B300, B9600, etc -- ie stuff
149 #define COST(x,e) (x ? (cost = 0, tputs (x, 1, e), cost) : BIG)
150 #define CMCOST(x,e) ((x == 0) ? BIG : (p = tgoto(x, 0, 0), COST(p ,e)))
152 Wcm
.cc_up
= COST (Wcm
.cm_up
, evalcost
);
153 Wcm
.cc_down
= COST (Wcm
.cm_down
, evalcost
);
154 Wcm
.cc_left
= COST (Wcm
.cm_left
, evalcost
);
155 Wcm
.cc_right
= COST (Wcm
.cm_right
, evalcost
);
156 Wcm
.cc_home
= COST (Wcm
.cm_home
, evalcost
);
157 Wcm
.cc_cr
= COST (Wcm
.cm_cr
, evalcost
);
158 Wcm
.cc_ll
= COST (Wcm
.cm_ll
, evalcost
);
159 Wcm
.cc_tab
= Wcm
.cm_tabwidth
? COST (Wcm
.cm_tab
, evalcost
) : BIG
;
162 * These last three are actually minimum costs. When (if) they are
163 * candidates for the least-cost motion, the real cost is computed.
164 * (Note that "0" is the assumed to generate the minimum cost.
165 * While this is not necessarily true, I have yet to see a terminal
166 * for which is not; all the terminals that have variable-cost
167 * cursor motion seem to take straight numeric values. --ACT)
170 Wcm
.cc_abs
= CMCOST (Wcm
.cm_abs
, evalcost
);
171 Wcm
.cc_habs
= CMCOST (Wcm
.cm_habs
, evalcost
);
172 Wcm
.cc_vabs
= CMCOST (Wcm
.cm_vabs
, evalcost
);
179 * Calculate the cost to move from (srcy, srcx) to (dsty, dstx) using
180 * up and down, and left and right, motions, and tabs. If doit is set
181 * actually perform the motion.
185 calccost (srcy
, srcx
, dsty
, dstx
, doit
)
198 /* If have just wrapped on a terminal with xn,
199 don't believe the cursor position: give up here
200 and force use of absolute positioning. */
202 if (curX
== Wcm
.cm_cols
)
206 if ((deltay
= dsty
- srcy
) == 0)
209 p
= Wcm
.cm_up
, c
= Wcm
.cc_up
, deltay
= -deltay
;
211 p
= Wcm
.cm_down
, c
= Wcm
.cc_down
;
212 if (c
== BIG
) { /* caint get thar from here */
217 totalcost
= c
* deltay
;
219 while (--deltay
>= 0)
220 tputs (p
, 1, cmputc
);
222 if ((deltax
= dstx
- srcx
) == 0)
225 p
= Wcm
.cm_left
, c
= Wcm
.cc_left
, deltax
= -deltax
;
226 goto dodelta
; /* skip all the tab junk */
228 /* Tabs (the toughie) */
229 if (Wcm
.cc_tab
>= BIG
|| !Wcm
.cm_usetabs
)
230 goto olddelta
; /* forget it! */
233 * ntabs is # tabs towards but not past dstx; n2tabs is one more
234 * (ie past dstx), but this is only valid if that is not past the
235 * right edge of the screen. We can check that at the same time
236 * as we figure out where we would be if we use the tabs (which
237 * we will put into tabx (for ntabs) and tab2x (for n2tabs)).
240 ntabs
= (deltax
+ srcx
% Wcm
.cm_tabwidth
) / Wcm
.cm_tabwidth
;
242 tabx
= (srcx
/ Wcm
.cm_tabwidth
+ ntabs
) * Wcm
.cm_tabwidth
;
243 tab2x
= tabx
+ Wcm
.cm_tabwidth
;
245 if (tab2x
>= Wcm
.cm_cols
) /* too far (past edge) */
249 * Now set tabcost to the cost for using ntabs, and c to the cost
250 * for using n2tabs, then pick the minimum.
253 /* cost for ntabs + cost for right motion */
254 tabcost
= ntabs
? ntabs
* Wcm
.cc_tab
+ (dstx
- tabx
) * Wcm
.cc_right
257 /* cost for n2tabs + cost for left motion */
258 c
= n2tabs
? n2tabs
* Wcm
.cc_tab
+ (tab2x
- dstx
) * Wcm
.cc_left
261 if (c
< tabcost
) /* then cheaper to overshoot & back up */
262 ntabs
= n2tabs
, tabcost
= c
, tabx
= tab2x
;
264 if (tabcost
>= BIG
) /* caint use tabs */
268 * See if tabcost is less than just moving right
271 if (tabcost
< (deltax
* Wcm
.cc_right
)) {
272 totalcost
+= tabcost
; /* use the tabs */
275 tputs (Wcm
.cm_tab
, 1, cmputc
);
280 * Now might as well just recompute the delta.
284 if ((deltax
= dstx
- srcx
) == 0)
288 p
= Wcm
.cm_right
, c
= Wcm
.cc_right
;
290 p
= Wcm
.cm_left
, c
= Wcm
.cc_left
, deltax
= -deltax
;
293 if (c
== BIG
) { /* caint get thar from here */
299 totalcost
+= c
* deltax
;
301 while (--deltax
>= 0)
302 tputs (p
, 1, cmputc
);
331 /* First the degenerate case */
332 if (row
== curY
&& col
== curX
) /* already there */
335 if (curY
>= 0 && curX
>= 0)
337 /* We may have quick ways to go to the upper-left, bottom-left,
338 * start-of-line, or start-of-next-line. Or it might be best to
339 * start where we are. Examine the options, and pick the cheapest.
342 relcost
= calccost (curY
, curX
, row
, col
, 0);
344 if ((homecost
= Wcm
.cc_home
) < BIG
)
345 homecost
+= calccost (0, 0, row
, col
, 0);
346 if (homecost
< relcost
)
347 relcost
= homecost
, use
= USEHOME
;
348 if ((llcost
= Wcm
.cc_ll
) < BIG
)
349 llcost
+= calccost (Wcm
.cm_rows
- 1, 0, row
, col
, 0);
350 if (llcost
< relcost
)
351 relcost
= llcost
, use
= USELL
;
352 if ((crcost
= Wcm
.cc_cr
) < BIG
) {
354 if (curY
+ 1 >= Wcm
.cm_rows
)
357 crcost
+= calccost (curY
+ 1, 0, row
, col
, 0);
359 crcost
+= calccost (curY
, 0, row
, col
, 0);
361 if (crcost
< relcost
)
362 relcost
= crcost
, use
= USECR
;
363 directcost
= Wcm
.cc_abs
, dcm
= Wcm
.cm_abs
;
364 if (row
== curY
&& Wcm
.cc_habs
< BIG
)
365 directcost
= Wcm
.cc_habs
, dcm
= Wcm
.cm_habs
;
366 else if (col
== curX
&& Wcm
.cc_vabs
< BIG
)
367 directcost
= Wcm
.cc_vabs
, dcm
= Wcm
.cm_vabs
;
371 directcost
= 0, relcost
= 100000;
376 * In the following comparison, the = in <= is because when the costs
377 * are the same, it looks nicer (I think) to move directly there.
379 if (directcost
<= relcost
)
381 /* compute REAL direct cost */
383 p
= dcm
== Wcm
.cm_habs
? tgoto (dcm
, row
, col
) :
384 tgoto (dcm
, col
, row
);
385 tputs (p
, 1, evalcost
);
387 { /* really is cheaper */
388 tputs (p
, 1, cmputc
);
389 curY
= row
, curX
= col
;
397 tputs (Wcm
.cm_home
, 1, cmputc
);
402 tputs (Wcm
.cm_ll
, 1, cmputc
);
403 curY
= Wcm
.cm_rows
- 1, curX
= 0;
407 tputs (Wcm
.cm_cr
, 1, cmputc
);
414 (void) calccost (curY
, curX
, row
, col
, 1);
415 curY
= row
, curX
= col
;
418 /* Clear out all terminal info.
419 Used before copying into it the info on the actual terminal.
425 bzero (&Wcm
, sizeof Wcm
);
432 * Return 0 if can do CM.
433 * Return -1 if cannot.
434 * Return -2 if size not specified.
441 if (Wcm
.cm_abs
&& !Wcm
.cm_ds
)
446 /* Require up and left, and, if no absolute, down and right */
447 if (!Wcm
.cm_up
|| !Wcm
.cm_left
)
449 if (!Wcm
.cm_abs
&& (!Wcm
.cm_down
|| !Wcm
.cm_right
))
451 /* Check that we know the size of the screen.... */
452 if (Wcm
.cm_rows
<= 0 || Wcm
.cm_cols
<= 0)