* keyboard.c (Qratio): New symbol.
[emacs.git] / src / cm.c
blob8ab302a601248edd3e7c909018a3dd9709abc3d5
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>
25 #include "cm.h"
26 #include "termhooks.h"
28 #ifdef HAVE_TERMCAP_H
29 #include <termcap.h>
30 #endif
32 #define BIG 9999 /* 9999 good on VAXen. For 16 bit machines
33 use about 2000.... */
35 char *tgoto ();
37 extern char *BC, *UP;
39 int cost; /* sums up costs */
41 /* ARGSUSED */
42 int
43 evalcost (c)
44 char c;
46 cost++;
47 return c;
50 int
51 cmputc (c)
52 char c;
54 if (termscript)
55 fputc (c & 0177, termscript);
56 putchar (c & 0177);
57 return c;
60 /* NEXT TWO ARE DONE WITH MACROS */
61 #if 0
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.
68 static
69 at (row, col) {
70 curY = row;
71 curX = col;
75 * Add n columns to the current cursor position.
78 static
79 addcol (n) {
80 curX += n;
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
87 * of the last line.
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.
97 if (Wcm.cm_magicwrap)
98 ; /* "limbo" */
99 else if (Wcm.cm_autowrap) {
100 curX = 0;
101 curY++; /* Beware end of screen! */
103 else
104 curX--;
107 #endif
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.
119 void
120 cmcheckmagic ()
122 if (curX == FrameCols)
124 if (!MagicWrap || curY >= FrameRows - 1)
125 abort ();
126 if (termscript)
127 putc ('\r', termscript);
128 putchar ('\r');
129 if (termscript)
130 putc ('\n', termscript);
131 putchar ('\n');
132 curX = 0;
133 curY++;
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
141 * out of <sgtty.h>.)
144 void
145 cmcostinit ()
147 char *p;
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);
174 #undef CMCOST
175 #undef COST
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.
184 static int
185 calccost (srcy, srcx, dsty, dstx, doit)
187 register int deltay,
188 deltax,
190 totalcost;
191 int ntabs,
192 n2tabs,
193 tabx,
194 tab2x,
195 tabcost;
196 register char *p;
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)
203 goto fail;
205 totalcost = 0;
206 if ((deltay = dsty - srcy) == 0)
207 goto x;
208 if (deltay < 0)
209 p = Wcm.cm_up, c = Wcm.cc_up, deltay = -deltay;
210 else
211 p = Wcm.cm_down, c = Wcm.cc_down;
212 if (c == BIG) { /* caint get thar from here */
213 if (doit)
214 printf ("OOPS");
215 return c;
217 totalcost = c * deltay;
218 if (doit)
219 while (--deltay >= 0)
220 tputs (p, 1, cmputc);
222 if ((deltax = dstx - srcx) == 0)
223 goto done;
224 if (deltax < 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;
241 n2tabs = ntabs + 1;
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) */
246 n2tabs = 0;
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
255 : BIG;
257 /* cost for n2tabs + cost for left motion */
258 c = n2tabs ? n2tabs * Wcm.cc_tab + (tab2x - dstx) * Wcm.cc_left
259 : BIG;
261 if (c < tabcost) /* then cheaper to overshoot & back up */
262 ntabs = n2tabs, tabcost = c, tabx = tab2x;
264 if (tabcost >= BIG) /* caint use tabs */
265 goto newdelta;
268 * See if tabcost is less than just moving right
271 if (tabcost < (deltax * Wcm.cc_right)) {
272 totalcost += tabcost; /* use the tabs */
273 if (doit)
274 while (--ntabs >= 0)
275 tputs (Wcm.cm_tab, 1, cmputc);
276 srcx = tabx;
280 * Now might as well just recompute the delta.
283 newdelta:
284 if ((deltax = dstx - srcx) == 0)
285 goto done;
286 olddelta:
287 if (deltax > 0)
288 p = Wcm.cm_right, c = Wcm.cc_right;
289 else
290 p = Wcm.cm_left, c = Wcm.cc_left, deltax = -deltax;
292 dodelta:
293 if (c == BIG) { /* caint get thar from here */
294 fail:
295 if (doit)
296 printf ("OOPS");
297 return BIG;
299 totalcost += c * deltax;
300 if (doit)
301 while (--deltax >= 0)
302 tputs (p, 1, cmputc);
303 done:
304 return totalcost;
307 #if 0
308 losecursor ()
310 curY = -1;
312 #endif
314 #define USEREL 0
315 #define USEHOME 1
316 #define USELL 2
317 #define USECR 3
319 void
320 cmgoto (row, col)
322 int homecost,
323 crcost,
324 llcost,
325 relcost,
326 directcost;
327 int use;
328 char *p,
329 *dcm;
331 /* First the degenerate case */
332 if (row == curY && col == curX) /* already there */
333 return;
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);
343 use = USEREL;
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) {
353 if (Wcm.cm_autolf)
354 if (curY + 1 >= Wcm.cm_rows)
355 crcost = BIG;
356 else
357 crcost += calccost (curY + 1, 0, row, col, 0);
358 else
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;
369 else
371 directcost = 0, relcost = 100000;
372 dcm = Wcm.cm_abs;
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 */
382 cost = 0;
383 p = dcm == Wcm.cm_habs ? tgoto (dcm, row, col) :
384 tgoto (dcm, col, row);
385 tputs (p, 1, evalcost);
386 if (cost <= relcost)
387 { /* really is cheaper */
388 tputs (p, 1, cmputc);
389 curY = row, curX = col;
390 return;
394 switch (use)
396 case USEHOME:
397 tputs (Wcm.cm_home, 1, cmputc);
398 curY = 0, curX = 0;
399 break;
401 case USELL:
402 tputs (Wcm.cm_ll, 1, cmputc);
403 curY = Wcm.cm_rows - 1, curX = 0;
404 break;
406 case USECR:
407 tputs (Wcm.cm_cr, 1, cmputc);
408 if (Wcm.cm_autolf)
409 curY++;
410 curX = 0;
411 break;
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.
422 void
423 Wcm_clear ()
425 bzero (&Wcm, sizeof Wcm);
426 UP = 0;
427 BC = 0;
431 * Initialized stuff
432 * Return 0 if can do CM.
433 * Return -1 if cannot.
434 * Return -2 if size not specified.
438 Wcm_init ()
440 #if 0
441 if (Wcm.cm_abs && !Wcm.cm_ds)
442 return 0;
443 #endif
444 if (Wcm.cm_abs)
445 return 0;
446 /* Require up and left, and, if no absolute, down and right */
447 if (!Wcm.cm_up || !Wcm.cm_left)
448 return - 1;
449 if (!Wcm.cm_abs && (!Wcm.cm_down || !Wcm.cm_right))
450 return - 1;
451 /* Check that we know the size of the screen.... */
452 if (Wcm.cm_rows <= 0 || Wcm.cm_cols <= 0)
453 return - 2;
454 return 0;