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. */
31 #include "termhooks.h"
35 /* For now, don't try to include termcap.h. On some systems,
36 configure finds a non-standard termcap.h that the main build
39 #if defined HAVE_TERMCAP_H && 0
42 extern void tputs
P_ ((const char *, int, int (*)(int)));
43 extern char *tgoto
P_ ((const char *, int, int));
46 #define BIG 9999 /* 9999 good on VAXen. For 16 bit machines
52 int cost
; /* sums up costs */
63 /* The terminal to use for low-level output. */
64 struct tty_display_info
*current_tty
;
70 if (TTY_TERMSCRIPT (current_tty
))
71 putc (c
& 0177, TTY_TERMSCRIPT (current_tty
));
72 putc (c
& 0177, TTY_OUTPUT (current_tty
));
76 /* NEXT TWO ARE DONE WITH MACROS */
79 * Assume the cursor is at row row, column col. Normally used only after
80 * clearing the screen, when the cursor is at (0, 0), but what the heck,
81 * let's let the guy put it anywhere.
91 * Add n columns to the current cursor position.
99 * If cursor hit edge of screen, what happened?
100 * N.B.: DO NOT!! write past edge of screen. If you do, you
101 * deserve what you get. Furthermore, on terminals with
102 * autowrap (but not magicwrap), don't write in the last column
106 if (curX (tty
) == tty
->Wcm
->cm_cols
) {
108 * Well, if magicwrap, still there, past the edge of the
109 * screen (!). If autowrap, on the col 0 of the next line.
110 * Otherwise on last column.
113 if (tty
->Wcm
->cm_magicwrap
)
115 else if (tty
->Wcm
->cm_autowrap
) {
117 curY (tty
) ++; /* Beware end of screen! */
126 * Terminals with magicwrap (xn) don't all behave identically.
127 * The VT100 leaves the cursor in the last column but will wrap before
128 * printing the next character. I hear that the Concept terminal does
129 * the wrap immediately but ignores the next newline it sees. And some
130 * terminals just have buggy firmware, and think that the cursor is still
131 * in limbo if we use direct cursor addressing from the phantom column.
132 * The only guaranteed safe thing to do is to emit a CRLF immediately
133 * after we reach the last column; this takes us to a known state.
136 cmcheckmagic (struct tty_display_info
*tty
)
138 if (curX (tty
) == FrameCols (tty
))
140 if (!MagicWrap (tty
) || curY (tty
) >= FrameRows (tty
) - 1)
142 if (TTY_TERMSCRIPT (tty
))
143 putc ('\r', TTY_TERMSCRIPT (tty
));
144 putc ('\r', TTY_OUTPUT (tty
));
145 if (TTY_TERMSCRIPT (tty
))
146 putc ('\n', TTY_TERMSCRIPT (tty
));
147 putc ('\n', TTY_OUTPUT (tty
));
155 * (Re)Initialize the cost factors, given the output speed of the terminal
156 * in the variable ospeed. (Note: this holds B300, B9600, etc -- ie stuff
161 cmcostinit (struct tty_display_info
*tty
)
165 #define COST(x,e) (x ? (cost = 0, tputs (x, 1, e), cost) : BIG)
166 #define CMCOST(x,e) ((x == 0) ? BIG : (p = tgoto(x, 0, 0), COST(p ,e)))
168 tty
->Wcm
->cc_up
= COST (tty
->Wcm
->cm_up
, evalcost
);
169 tty
->Wcm
->cc_down
= COST (tty
->Wcm
->cm_down
, evalcost
);
170 tty
->Wcm
->cc_left
= COST (tty
->Wcm
->cm_left
, evalcost
);
171 tty
->Wcm
->cc_right
= COST (tty
->Wcm
->cm_right
, evalcost
);
172 tty
->Wcm
->cc_home
= COST (tty
->Wcm
->cm_home
, evalcost
);
173 tty
->Wcm
->cc_cr
= COST (tty
->Wcm
->cm_cr
, evalcost
);
174 tty
->Wcm
->cc_ll
= COST (tty
->Wcm
->cm_ll
, evalcost
);
175 tty
->Wcm
->cc_tab
= tty
->Wcm
->cm_tabwidth
? COST (tty
->Wcm
->cm_tab
, evalcost
) : BIG
;
178 * These last three are actually minimum costs. When (if) they are
179 * candidates for the least-cost motion, the real cost is computed.
180 * (Note that "0" is the assumed to generate the minimum cost.
181 * While this is not necessarily true, I have yet to see a terminal
182 * for which is not; all the terminals that have variable-cost
183 * cursor motion seem to take straight numeric values. --ACT)
186 tty
->Wcm
->cc_abs
= CMCOST (tty
->Wcm
->cm_abs
, evalcost
);
187 tty
->Wcm
->cc_habs
= CMCOST (tty
->Wcm
->cm_habs
, evalcost
);
188 tty
->Wcm
->cc_vabs
= CMCOST (tty
->Wcm
->cm_vabs
, evalcost
);
195 * Calculate the cost to move from (srcy, srcx) to (dsty, dstx) using
196 * up and down, and left and right, motions, and tabs. If doit is set
197 * actually perform the motion.
201 calccost (struct tty_display_info
*tty
,
202 int srcy
, int srcx
, int dsty
, int dstx
, int doit
)
215 /* If have just wrapped on a terminal with xn,
216 don't believe the cursor position: give up here
217 and force use of absolute positioning. */
219 if (curX (tty
) == tty
->Wcm
->cm_cols
)
223 if ((deltay
= dsty
- srcy
) == 0)
226 p
= tty
->Wcm
->cm_up
, c
= tty
->Wcm
->cc_up
, deltay
= -deltay
;
228 p
= tty
->Wcm
->cm_down
, c
= tty
->Wcm
->cc_down
;
229 if (c
== BIG
) { /* caint get thar from here */
234 totalcost
= c
* deltay
;
236 while (--deltay
>= 0)
237 emacs_tputs (tty
, p
, 1, cmputc
);
239 if ((deltax
= dstx
- srcx
) == 0)
242 p
= tty
->Wcm
->cm_left
, c
= tty
->Wcm
->cc_left
, deltax
= -deltax
;
243 goto dodelta
; /* skip all the tab junk */
245 /* Tabs (the toughie) */
246 if (tty
->Wcm
->cc_tab
>= BIG
|| !tty
->Wcm
->cm_usetabs
)
247 goto olddelta
; /* forget it! */
250 * ntabs is # tabs towards but not past dstx; n2tabs is one more
251 * (ie past dstx), but this is only valid if that is not past the
252 * right edge of the screen. We can check that at the same time
253 * as we figure out where we would be if we use the tabs (which
254 * we will put into tabx (for ntabs) and tab2x (for n2tabs)).
257 ntabs
= (deltax
+ srcx
% tty
->Wcm
->cm_tabwidth
) / tty
->Wcm
->cm_tabwidth
;
259 tabx
= (srcx
/ tty
->Wcm
->cm_tabwidth
+ ntabs
) * tty
->Wcm
->cm_tabwidth
;
260 tab2x
= tabx
+ tty
->Wcm
->cm_tabwidth
;
262 if (tab2x
>= tty
->Wcm
->cm_cols
) /* too far (past edge) */
266 * Now set tabcost to the cost for using ntabs, and c to the cost
267 * for using n2tabs, then pick the minimum.
270 /* cost for ntabs + cost for right motion */
271 tabcost
= ntabs
? ntabs
* tty
->Wcm
->cc_tab
+ (dstx
- tabx
) * tty
->Wcm
->cc_right
274 /* cost for n2tabs + cost for left motion */
275 c
= n2tabs
? n2tabs
* tty
->Wcm
->cc_tab
+ (tab2x
- dstx
) * tty
->Wcm
->cc_left
278 if (c
< tabcost
) /* then cheaper to overshoot & back up */
279 ntabs
= n2tabs
, tabcost
= c
, tabx
= tab2x
;
281 if (tabcost
>= BIG
) /* caint use tabs */
285 * See if tabcost is less than just moving right
288 if (tabcost
< (deltax
* tty
->Wcm
->cc_right
)) {
289 totalcost
+= tabcost
; /* use the tabs */
292 emacs_tputs (tty
, tty
->Wcm
->cm_tab
, 1, cmputc
);
297 * Now might as well just recompute the delta.
301 if ((deltax
= dstx
- srcx
) == 0)
305 p
= tty
->Wcm
->cm_right
, c
= tty
->Wcm
->cc_right
;
307 p
= tty
->Wcm
->cm_left
, c
= tty
->Wcm
->cc_left
, deltax
= -deltax
;
310 if (c
== BIG
) { /* caint get thar from here */
316 totalcost
+= c
* deltax
;
318 while (--deltax
>= 0)
319 emacs_tputs (tty
, p
, 1, cmputc
);
337 cmgoto (tty
, row
, col
)
338 struct tty_display_info
*tty
;
350 /* First the degenerate case */
351 if (row
== curY (tty
) && col
== curX (tty
)) /* already there */
354 if (curY (tty
) >= 0 && curX (tty
) >= 0)
356 /* We may have quick ways to go to the upper-left, bottom-left,
357 * start-of-line, or start-of-next-line. Or it might be best to
358 * start where we are. Examine the options, and pick the cheapest.
361 relcost
= calccost (tty
, curY (tty
), curX (tty
), row
, col
, 0);
363 if ((homecost
= tty
->Wcm
->cc_home
) < BIG
)
364 homecost
+= calccost (tty
, 0, 0, row
, col
, 0);
365 if (homecost
< relcost
)
366 relcost
= homecost
, use
= USEHOME
;
367 if ((llcost
= tty
->Wcm
->cc_ll
) < BIG
)
368 llcost
+= calccost (tty
, tty
->Wcm
->cm_rows
- 1, 0, row
, col
, 0);
369 if (llcost
< relcost
)
370 relcost
= llcost
, use
= USELL
;
371 if ((crcost
= tty
->Wcm
->cc_cr
) < BIG
) {
372 if (tty
->Wcm
->cm_autolf
)
373 if (curY (tty
) + 1 >= tty
->Wcm
->cm_rows
)
376 crcost
+= calccost (tty
, curY (tty
) + 1, 0, row
, col
, 0);
378 crcost
+= calccost (tty
, curY (tty
), 0, row
, col
, 0);
380 if (crcost
< relcost
)
381 relcost
= crcost
, use
= USECR
;
382 directcost
= tty
->Wcm
->cc_abs
, dcm
= tty
->Wcm
->cm_abs
;
383 if (row
== curY (tty
) && tty
->Wcm
->cc_habs
< BIG
)
384 directcost
= tty
->Wcm
->cc_habs
, dcm
= tty
->Wcm
->cm_habs
;
385 else if (col
== curX (tty
) && tty
->Wcm
->cc_vabs
< BIG
)
386 directcost
= tty
->Wcm
->cc_vabs
, dcm
= tty
->Wcm
->cm_vabs
;
390 directcost
= 0, relcost
= 100000;
391 dcm
= tty
->Wcm
->cm_abs
;
395 * In the following comparison, the = in <= is because when the costs
396 * are the same, it looks nicer (I think) to move directly there.
398 if (directcost
<= relcost
)
400 /* compute REAL direct cost */
402 p
= dcm
== tty
->Wcm
->cm_habs
403 ? tgoto (dcm
, row
, col
)
404 : tgoto (dcm
, col
, row
);
405 emacs_tputs (tty
, p
, 1, evalcost
);
407 { /* really is cheaper */
408 emacs_tputs (tty
, p
, 1, cmputc
);
409 curY (tty
) = row
, curX (tty
) = col
;
417 emacs_tputs (tty
, tty
->Wcm
->cm_home
, 1, cmputc
);
418 curY (tty
) = 0, curX (tty
) = 0;
422 emacs_tputs (tty
, tty
->Wcm
->cm_ll
, 1, cmputc
);
423 curY (tty
) = tty
->Wcm
->cm_rows
- 1, curX (tty
) = 0;
427 emacs_tputs (tty
, tty
->Wcm
->cm_cr
, 1, cmputc
);
428 if (tty
->Wcm
->cm_autolf
)
434 (void) calccost (tty
, curY (tty
), curX (tty
), row
, col
, 1);
435 curY (tty
) = row
, curX (tty
) = col
;
438 /* Clear out all terminal info.
439 Used before copying into it the info on the actual terminal.
443 Wcm_clear (struct tty_display_info
*tty
)
445 bzero (tty
->Wcm
, sizeof (struct cm
));
452 * Return 0 if can do CM.
453 * Return -1 if cannot.
454 * Return -2 if size not specified.
458 Wcm_init (struct tty_display_info
*tty
)
461 if (tty
->Wcm
->cm_abs
&& !tty
->Wcm
->cm_ds
)
464 if (tty
->Wcm
->cm_abs
)
466 /* Require up and left, and, if no absolute, down and right */
467 if (!tty
->Wcm
->cm_up
|| !tty
->Wcm
->cm_left
)
469 if (!tty
->Wcm
->cm_abs
&& (!tty
->Wcm
->cm_down
|| !tty
->Wcm
->cm_right
))
471 /* Check that we know the size of the screen.... */
472 if (tty
->Wcm
->cm_rows
<= 0 || tty
->Wcm
->cm_cols
<= 0)
477 /* arch-tag: bcf64c02-00f6-44ef-94b6-c56eab5b3dc4
478 (do not change this comment) */