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"
32 #include "systty.h" /* For emacs_tty in termchar.h */
36 /* For now, don't try to include termcap.h. On some systems,
37 configure finds a non-standard termcap.h that the main build
40 #if defined HAVE_TERMCAP_H && 0
43 extern void tputs
P_ ((const char *, int, int (*)(int)));
44 extern char *tgoto
P_ ((const char *, int, int));
47 #define BIG 9999 /* 9999 good on VAXen. For 16 bit machines
53 int cost
; /* sums up costs */
64 /* The terminal to use for low-level output. */
65 struct tty_output
* current_tty
;
71 if (TTY_TERMSCRIPT (current_tty
))
72 putc (c
& 0177, TTY_TERMSCRIPT (current_tty
));
73 putc (c
& 0177, TTY_OUTPUT (current_tty
));
77 /* NEXT TWO ARE DONE WITH MACROS */
80 * Assume the cursor is at row row, column col. Normally used only after
81 * clearing the screen, when the cursor is at (0, 0), but what the heck,
82 * let's let the guy put it anywhere.
92 * Add n columns to the current cursor position.
100 * If cursor hit edge of screen, what happened?
101 * N.B.: DO NOT!! write past edge of screen. If you do, you
102 * deserve what you get. Furthermore, on terminals with
103 * autowrap (but not magicwrap), don't write in the last column
107 if (curX (tty
) == tty
->Wcm
->cm_cols
) {
109 * Well, if magicwrap, still there, past the edge of the
110 * screen (!). If autowrap, on the col 0 of the next line.
111 * Otherwise on last column.
114 if (tty
->Wcm
->cm_magicwrap
)
116 else if (tty
->Wcm
->cm_autowrap
) {
118 curY (tty
) ++; /* Beware end of screen! */
127 * Terminals with magicwrap (xn) don't all behave identically.
128 * The VT100 leaves the cursor in the last column but will wrap before
129 * printing the next character. I hear that the Concept terminal does
130 * the wrap immediately but ignores the next newline it sees. And some
131 * terminals just have buggy firmware, and think that the cursor is still
132 * in limbo if we use direct cursor addressing from the phantom column.
133 * The only guaranteed safe thing to do is to emit a CRLF immediately
134 * after we reach the last column; this takes us to a known state.
138 struct tty_output
*tty
;
140 if (curX (tty
) == FrameCols (tty
))
142 if (!MagicWrap (tty
) || curY (tty
) >= FrameRows (tty
) - 1)
144 if (TTY_TERMSCRIPT (tty
))
145 putc ('\r', TTY_TERMSCRIPT (tty
));
146 putc ('\r', TTY_OUTPUT (tty
));
147 if (TTY_TERMSCRIPT (tty
))
148 putc ('\n', TTY_TERMSCRIPT (tty
));
149 putc ('\n', TTY_OUTPUT (tty
));
157 * (Re)Initialize the cost factors, given the output speed of the terminal
158 * in the variable ospeed. (Note: this holds B300, B9600, etc -- ie stuff
163 cmcostinit (struct tty_output
*tty
)
167 #define COST(x,e) (x ? (cost = 0, tputs (x, 1, e), cost) : BIG)
168 #define CMCOST(x,e) ((x == 0) ? BIG : (p = tgoto(x, 0, 0), COST(p ,e)))
170 tty
->Wcm
->cc_up
= COST (tty
->Wcm
->cm_up
, evalcost
);
171 tty
->Wcm
->cc_down
= COST (tty
->Wcm
->cm_down
, evalcost
);
172 tty
->Wcm
->cc_left
= COST (tty
->Wcm
->cm_left
, evalcost
);
173 tty
->Wcm
->cc_right
= COST (tty
->Wcm
->cm_right
, evalcost
);
174 tty
->Wcm
->cc_home
= COST (tty
->Wcm
->cm_home
, evalcost
);
175 tty
->Wcm
->cc_cr
= COST (tty
->Wcm
->cm_cr
, evalcost
);
176 tty
->Wcm
->cc_ll
= COST (tty
->Wcm
->cm_ll
, evalcost
);
177 tty
->Wcm
->cc_tab
= tty
->Wcm
->cm_tabwidth
? COST (tty
->Wcm
->cm_tab
, evalcost
) : BIG
;
180 * These last three are actually minimum costs. When (if) they are
181 * candidates for the least-cost motion, the real cost is computed.
182 * (Note that "0" is the assumed to generate the minimum cost.
183 * While this is not necessarily true, I have yet to see a terminal
184 * for which is not; all the terminals that have variable-cost
185 * cursor motion seem to take straight numeric values. --ACT)
188 tty
->Wcm
->cc_abs
= CMCOST (tty
->Wcm
->cm_abs
, evalcost
);
189 tty
->Wcm
->cc_habs
= CMCOST (tty
->Wcm
->cm_habs
, evalcost
);
190 tty
->Wcm
->cc_vabs
= CMCOST (tty
->Wcm
->cm_vabs
, evalcost
);
197 * Calculate the cost to move from (srcy, srcx) to (dsty, dstx) using
198 * up and down, and left and right, motions, and tabs. If doit is set
199 * actually perform the motion.
203 calccost (struct tty_output
*tty
, int srcy
, int srcx
, int dsty
, int dstx
, int doit
)
216 /* If have just wrapped on a terminal with xn,
217 don't believe the cursor position: give up here
218 and force use of absolute positioning. */
220 if (curX (tty
) == tty
->Wcm
->cm_cols
)
224 if ((deltay
= dsty
- srcy
) == 0)
227 p
= tty
->Wcm
->cm_up
, c
= tty
->Wcm
->cc_up
, deltay
= -deltay
;
229 p
= tty
->Wcm
->cm_down
, c
= tty
->Wcm
->cc_down
;
230 if (c
== BIG
) { /* caint get thar from here */
235 totalcost
= c
* deltay
;
237 while (--deltay
>= 0)
238 emacs_tputs (tty
, p
, 1, cmputc
);
240 if ((deltax
= dstx
- srcx
) == 0)
243 p
= tty
->Wcm
->cm_left
, c
= tty
->Wcm
->cc_left
, deltax
= -deltax
;
244 goto dodelta
; /* skip all the tab junk */
246 /* Tabs (the toughie) */
247 if (tty
->Wcm
->cc_tab
>= BIG
|| !tty
->Wcm
->cm_usetabs
)
248 goto olddelta
; /* forget it! */
251 * ntabs is # tabs towards but not past dstx; n2tabs is one more
252 * (ie past dstx), but this is only valid if that is not past the
253 * right edge of the screen. We can check that at the same time
254 * as we figure out where we would be if we use the tabs (which
255 * we will put into tabx (for ntabs) and tab2x (for n2tabs)).
258 ntabs
= (deltax
+ srcx
% tty
->Wcm
->cm_tabwidth
) / tty
->Wcm
->cm_tabwidth
;
260 tabx
= (srcx
/ tty
->Wcm
->cm_tabwidth
+ ntabs
) * tty
->Wcm
->cm_tabwidth
;
261 tab2x
= tabx
+ tty
->Wcm
->cm_tabwidth
;
263 if (tab2x
>= tty
->Wcm
->cm_cols
) /* too far (past edge) */
267 * Now set tabcost to the cost for using ntabs, and c to the cost
268 * for using n2tabs, then pick the minimum.
271 /* cost for ntabs + cost for right motion */
272 tabcost
= ntabs
? ntabs
* tty
->Wcm
->cc_tab
+ (dstx
- tabx
) * tty
->Wcm
->cc_right
275 /* cost for n2tabs + cost for left motion */
276 c
= n2tabs
? n2tabs
* tty
->Wcm
->cc_tab
+ (tab2x
- dstx
) * tty
->Wcm
->cc_left
279 if (c
< tabcost
) /* then cheaper to overshoot & back up */
280 ntabs
= n2tabs
, tabcost
= c
, tabx
= tab2x
;
282 if (tabcost
>= BIG
) /* caint use tabs */
286 * See if tabcost is less than just moving right
289 if (tabcost
< (deltax
* tty
->Wcm
->cc_right
)) {
290 totalcost
+= tabcost
; /* use the tabs */
293 emacs_tputs (tty
, tty
->Wcm
->cm_tab
, 1, cmputc
);
298 * Now might as well just recompute the delta.
302 if ((deltax
= dstx
- srcx
) == 0)
306 p
= tty
->Wcm
->cm_right
, c
= tty
->Wcm
->cc_right
;
308 p
= tty
->Wcm
->cm_left
, c
= tty
->Wcm
->cc_left
, deltax
= -deltax
;
311 if (c
== BIG
) { /* caint get thar from here */
317 totalcost
+= c
* deltax
;
319 while (--deltax
>= 0)
320 emacs_tputs (tty
, p
, 1, cmputc
);
338 cmgoto (tty
, row
, col
)
339 struct tty_output
*tty
;
351 /* First the degenerate case */
352 if (row
== curY (tty
) && col
== curX (tty
)) /* already there */
355 if (curY (tty
) >= 0 && curX (tty
) >= 0)
357 /* We may have quick ways to go to the upper-left, bottom-left,
358 * start-of-line, or start-of-next-line. Or it might be best to
359 * start where we are. Examine the options, and pick the cheapest.
362 relcost
= calccost (tty
, curY (tty
), curX (tty
), row
, col
, 0);
364 if ((homecost
= tty
->Wcm
->cc_home
) < BIG
)
365 homecost
+= calccost (tty
, 0, 0, row
, col
, 0);
366 if (homecost
< relcost
)
367 relcost
= homecost
, use
= USEHOME
;
368 if ((llcost
= tty
->Wcm
->cc_ll
) < BIG
)
369 llcost
+= calccost (tty
, tty
->Wcm
->cm_rows
- 1, 0, row
, col
, 0);
370 if (llcost
< relcost
)
371 relcost
= llcost
, use
= USELL
;
372 if ((crcost
= tty
->Wcm
->cc_cr
) < BIG
) {
373 if (tty
->Wcm
->cm_autolf
)
374 if (curY (tty
) + 1 >= tty
->Wcm
->cm_rows
)
377 crcost
+= calccost (tty
, curY (tty
) + 1, 0, row
, col
, 0);
379 crcost
+= calccost (tty
, curY (tty
), 0, row
, col
, 0);
381 if (crcost
< relcost
)
382 relcost
= crcost
, use
= USECR
;
383 directcost
= tty
->Wcm
->cc_abs
, dcm
= tty
->Wcm
->cm_abs
;
384 if (row
== curY (tty
) && tty
->Wcm
->cc_habs
< BIG
)
385 directcost
= tty
->Wcm
->cc_habs
, dcm
= tty
->Wcm
->cm_habs
;
386 else if (col
== curX (tty
) && tty
->Wcm
->cc_vabs
< BIG
)
387 directcost
= tty
->Wcm
->cc_vabs
, dcm
= tty
->Wcm
->cm_vabs
;
391 directcost
= 0, relcost
= 100000;
392 dcm
= tty
->Wcm
->cm_abs
;
396 * In the following comparison, the = in <= is because when the costs
397 * are the same, it looks nicer (I think) to move directly there.
399 if (directcost
<= relcost
)
401 /* compute REAL direct cost */
403 p
= dcm
== tty
->Wcm
->cm_habs
404 ? tgoto (dcm
, row
, col
)
405 : tgoto (dcm
, col
, row
);
406 emacs_tputs (tty
, p
, 1, evalcost
);
408 { /* really is cheaper */
409 emacs_tputs (tty
, p
, 1, cmputc
);
410 curY (tty
) = row
, curX (tty
) = col
;
418 emacs_tputs (tty
, tty
->Wcm
->cm_home
, 1, cmputc
);
419 curY (tty
) = 0, curX (tty
) = 0;
423 emacs_tputs (tty
, tty
->Wcm
->cm_ll
, 1, cmputc
);
424 curY (tty
) = tty
->Wcm
->cm_rows
- 1, curX (tty
) = 0;
428 emacs_tputs (tty
, tty
->Wcm
->cm_cr
, 1, cmputc
);
429 if (tty
->Wcm
->cm_autolf
)
435 (void) calccost (tty
, curY (tty
), curX (tty
), row
, col
, 1);
436 curY (tty
) = row
, curX (tty
) = col
;
439 /* Clear out all terminal info.
440 Used before copying into it the info on the actual terminal.
444 Wcm_clear (struct tty_output
*tty
)
446 bzero (tty
->Wcm
, sizeof (struct cm
));
453 * Return 0 if can do CM.
454 * Return -1 if cannot.
455 * Return -2 if size not specified.
459 Wcm_init (struct tty_output
*tty
)
462 if (tty
->Wcm
->cm_abs
&& !tty
->Wcm
->cm_ds
)
465 if (tty
->Wcm
->cm_abs
)
467 /* Require up and left, and, if no absolute, down and right */
468 if (!tty
->Wcm
->cm_up
|| !tty
->Wcm
->cm_left
)
470 if (!tty
->Wcm
->cm_abs
&& (!tty
->Wcm
->cm_down
|| !tty
->Wcm
->cm_right
))
472 /* Check that we know the size of the screen.... */
473 if (tty
->Wcm
->cm_rows
<= 0 || tty
->Wcm
->cm_cols
<= 0)
478 /* arch-tag: bcf64c02-00f6-44ef-94b6-c56eab5b3dc4
479 (do not change this comment) */