Fully eliminated global tty state variables.
[emacs.git] / src / cm.c
blobd1deb12f6900804f5b1bdb19a53f7c16cd853d9b
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)
10 any later version.
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. */
23 #include <config.h>
24 #include <stdio.h>
26 /* For CURTTY */
27 #include "lisp.h"
28 #include "frame.h"
30 #include "cm.h"
31 #include "termhooks.h"
32 #include "systty.h" /* For emacs_tty in termchar.h */
33 #include "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
38 won't find. */
40 #if defined HAVE_TERMCAP_H && 0
41 #include <termcap.h>
42 #else
43 extern void tputs P_ ((const char *, int, int (*)(int)));
44 extern char *tgoto P_ ((const char *, int, int));
45 #endif
47 #define BIG 9999 /* 9999 good on VAXen. For 16 bit machines
48 use about 2000.... */
50 extern char *BC, *UP;
53 int cost; /* sums up costs */
55 /* ARGSUSED */
56 int
57 evalcost (c)
58 char c;
60 cost++;
61 return c;
64 /* The terminal to use for low-level output. */
65 struct tty_output * current_tty;
67 int
68 cmputc (c)
69 char c;
71 if (TTY_TERMSCRIPT (current_tty))
72 putc (c & 0177, TTY_TERMSCRIPT (current_tty));
73 putc (c & 0177, TTY_OUTPUT (current_tty));
74 return c;
77 /* NEXT TWO ARE DONE WITH MACROS */
78 #if 0
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.
85 static
86 at (tty, row, col) {
87 curY (tty) = row;
88 curX (tty) = col;
92 * Add n columns to the current cursor position.
95 static
96 addcol (tty, n) {
97 curX (tty) += n;
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
104 * of the last line.
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)
115 ; /* "limbo" */
116 else if (tty->Wcm->cm_autowrap) {
117 curX (tty) = 0;
118 curY (tty) ++; /* Beware end of screen! */
120 else
121 curX (tty)--;
124 #endif
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.
136 void
137 cmcheckmagic (tty)
138 struct tty_output *tty;
140 if (curX (tty) == FrameCols (tty))
142 if (!MagicWrap (tty) || curY (tty) >= FrameRows (tty) - 1)
143 abort ();
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));
150 curX (tty) = 0;
151 curY (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
159 * out of <sgtty.h>.)
162 void
163 cmcostinit (struct tty_output *tty)
165 char *p;
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);
192 #undef CMCOST
193 #undef COST
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.
202 static int
203 calccost (struct tty_output *tty, int srcy, int srcx, int dsty, int dstx, int doit)
205 register int deltay,
206 deltax,
208 totalcost;
209 int ntabs,
210 n2tabs,
211 tabx,
212 tab2x,
213 tabcost;
214 register char *p;
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)
221 goto fail;
223 totalcost = 0;
224 if ((deltay = dsty - srcy) == 0)
225 goto x;
226 if (deltay < 0)
227 p = tty->Wcm->cm_up, c = tty->Wcm->cc_up, deltay = -deltay;
228 else
229 p = tty->Wcm->cm_down, c = tty->Wcm->cc_down;
230 if (c == BIG) { /* caint get thar from here */
231 if (doit)
232 printf ("OOPS");
233 return c;
235 totalcost = c * deltay;
236 if (doit)
237 while (--deltay >= 0)
238 emacs_tputs (tty, p, 1, cmputc);
240 if ((deltax = dstx - srcx) == 0)
241 goto done;
242 if (deltax < 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;
259 n2tabs = ntabs + 1;
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) */
264 n2tabs = 0;
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
273 : BIG;
275 /* cost for n2tabs + cost for left motion */
276 c = n2tabs ? n2tabs * tty->Wcm->cc_tab + (tab2x - dstx) * tty->Wcm->cc_left
277 : BIG;
279 if (c < tabcost) /* then cheaper to overshoot & back up */
280 ntabs = n2tabs, tabcost = c, tabx = tab2x;
282 if (tabcost >= BIG) /* caint use tabs */
283 goto newdelta;
286 * See if tabcost is less than just moving right
289 if (tabcost < (deltax * tty->Wcm->cc_right)) {
290 totalcost += tabcost; /* use the tabs */
291 if (doit)
292 while (--ntabs >= 0)
293 emacs_tputs (tty, tty->Wcm->cm_tab, 1, cmputc);
294 srcx = tabx;
298 * Now might as well just recompute the delta.
301 newdelta:
302 if ((deltax = dstx - srcx) == 0)
303 goto done;
304 olddelta:
305 if (deltax > 0)
306 p = tty->Wcm->cm_right, c = tty->Wcm->cc_right;
307 else
308 p = tty->Wcm->cm_left, c = tty->Wcm->cc_left, deltax = -deltax;
310 dodelta:
311 if (c == BIG) { /* caint get thar from here */
312 fail:
313 if (doit)
314 printf ("OOPS");
315 return BIG;
317 totalcost += c * deltax;
318 if (doit)
319 while (--deltax >= 0)
320 emacs_tputs (tty, p, 1, cmputc);
321 done:
322 return totalcost;
325 #if 0
326 losecursor ()
328 curY = -1;
330 #endif
332 #define USEREL 0
333 #define USEHOME 1
334 #define USELL 2
335 #define USECR 3
337 void
338 cmgoto (tty, row, col)
339 struct tty_output *tty;
340 int row, col;
342 int homecost,
343 crcost,
344 llcost,
345 relcost,
346 directcost;
347 int use;
348 char *p,
349 *dcm;
351 /* First the degenerate case */
352 if (row == curY (tty) && col == curX (tty)) /* already there */
353 return;
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);
363 use = USEREL;
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)
375 crcost = BIG;
376 else
377 crcost += calccost (tty, curY (tty) + 1, 0, row, col, 0);
378 else
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;
389 else
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 */
402 cost = 0;
403 p = dcm == tty->Wcm->cm_habs
404 ? tgoto (dcm, row, col)
405 : tgoto (dcm, col, row);
406 emacs_tputs (tty, p, 1, evalcost);
407 if (cost <= relcost)
408 { /* really is cheaper */
409 emacs_tputs (tty, p, 1, cmputc);
410 curY (tty) = row, curX (tty) = col;
411 return;
415 switch (use)
417 case USEHOME:
418 emacs_tputs (tty, tty->Wcm->cm_home, 1, cmputc);
419 curY (tty) = 0, curX (tty) = 0;
420 break;
422 case USELL:
423 emacs_tputs (tty, tty->Wcm->cm_ll, 1, cmputc);
424 curY (tty) = tty->Wcm->cm_rows - 1, curX (tty) = 0;
425 break;
427 case USECR:
428 emacs_tputs (tty, tty->Wcm->cm_cr, 1, cmputc);
429 if (tty->Wcm->cm_autolf)
430 curY (tty)++;
431 curX (tty) = 0;
432 break;
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.
443 void
444 Wcm_clear (struct tty_output *tty)
446 bzero (tty->Wcm, sizeof (struct cm));
447 UP = 0;
448 BC = 0;
452 * Initialized stuff
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)
461 #if 0
462 if (tty->Wcm->cm_abs && !tty->Wcm->cm_ds)
463 return 0;
464 #endif
465 if (tty->Wcm->cm_abs)
466 return 0;
467 /* Require up and left, and, if no absolute, down and right */
468 if (!tty->Wcm->cm_up || !tty->Wcm->cm_left)
469 return - 1;
470 if (!tty->Wcm->cm_abs && (!tty->Wcm->cm_down || !tty->Wcm->cm_right))
471 return - 1;
472 /* Check that we know the size of the screen.... */
473 if (tty->Wcm->cm_rows <= 0 || tty->Wcm->cm_cols <= 0)
474 return - 2;
475 return 0;
478 /* arch-tag: bcf64c02-00f6-44ef-94b6-c56eab5b3dc4
479 (do not change this comment) */