Convert consecutive FSF copyright years to ranges.
[bpt/emacs.git] / src / cm.c
CommitLineData
d427b66a 1/* Cursor motion subroutines for GNU Emacs.
73b0cd50 2 Copyright (C) 1985, 1995, 2001-2011 Free Software Foundation, Inc.
d427b66a
JB
3 based primarily on public domain code written by Chris Torek
4
5This file is part of GNU Emacs.
6
9ec0b715 7GNU Emacs is free software: you can redistribute it and/or modify
d427b66a 8it under the terms of the GNU General Public License as published by
9ec0b715
GM
9the Free Software Foundation, either version 3 of the License, or
10(at your option) any later version.
d427b66a
JB
11
12GNU Emacs is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
9ec0b715 18along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
d427b66a
JB
19
20
18160b98 21#include <config.h>
d427b66a 22#include <stdio.h>
d7306fe6 23#include <setjmp.h>
28d440ab 24
28d440ab
KL
25#include "lisp.h"
26#include "frame.h"
d427b66a
JB
27#include "cm.h"
28#include "termhooks.h"
28d440ab
KL
29#include "termchar.h"
30
d427b66a 31
ff23e1dd
GM
32/* For now, don't try to include termcap.h. On some systems,
33 configure finds a non-standard termcap.h that the main build
34 won't find. */
f57e2426
J
35extern void tputs (const char *, int, int (*)(int));
36extern char *tgoto (const char *, int, int);
bb10ca93 37
d427b66a
JB
38#define BIG 9999 /* 9999 good on VAXen. For 16 bit machines
39 use about 2000.... */
40
d427b66a
JB
41extern char *BC, *UP;
42
43int cost; /* sums up costs */
44
45/* ARGSUSED */
dfcf069d 46int
d3da34e0 47evalcost (int c)
d427b66a
JB
48{
49 cost++;
97f11a9d 50 return c;
d427b66a
JB
51}
52
28d440ab 53/* The terminal to use for low-level output. */
28d7d09f 54struct tty_display_info *current_tty;
28d440ab 55
dfcf069d 56int
d3da34e0 57cmputc (int c)
d427b66a 58{
0b0d3e0b
KL
59 if (current_tty->termscript)
60 putc (c & 0177, current_tty->termscript);
61 putc (c & 0177, current_tty->output);
97f11a9d 62 return c;
d427b66a
JB
63}
64
65/* NEXT TWO ARE DONE WITH MACROS */
66#if 0
67/*
68 * Assume the cursor is at row row, column col. Normally used only after
69 * clearing the screen, when the cursor is at (0, 0), but what the heck,
70 * let's let the guy put it anywhere.
71 */
72
73static
fca177d4
KL
74at (tty, row, col) {
75 curY (tty) = row;
76 curX (tty) = col;
d427b66a
JB
77}
78
79/*
80 * Add n columns to the current cursor position.
81 */
82
83static
fca177d4
KL
84addcol (tty, n) {
85 curX (tty) += n;
d427b66a
JB
86
87 /*
88 * If cursor hit edge of screen, what happened?
89 * N.B.: DO NOT!! write past edge of screen. If you do, you
90 * deserve what you get. Furthermore, on terminals with
91 * autowrap (but not magicwrap), don't write in the last column
92 * of the last line.
93 */
94
fca177d4 95 if (curX (tty) == tty->Wcm->cm_cols) {
d427b66a
JB
96 /*
97 * Well, if magicwrap, still there, past the edge of the
98 * screen (!). If autowrap, on the col 0 of the next line.
99 * Otherwise on last column.
100 */
101
fca177d4 102 if (tty->Wcm->cm_magicwrap)
d427b66a 103 ; /* "limbo" */
fca177d4
KL
104 else if (tty->Wcm->cm_autowrap) {
105 curX (tty) = 0;
106 curY (tty) ++; /* Beware end of screen! */
d427b66a
JB
107 }
108 else
fca177d4 109 curX (tty)--;
d427b66a
JB
110 }
111}
112#endif
113
902a3b86
KH
114/*
115 * Terminals with magicwrap (xn) don't all behave identically.
116 * The VT100 leaves the cursor in the last column but will wrap before
117 * printing the next character. I hear that the Concept terminal does
118 * the wrap immediately but ignores the next newline it sees. And some
119 * terminals just have buggy firmware, and think that the cursor is still
120 * in limbo if we use direct cursor addressing from the phantom column.
121 * The only guaranteed safe thing to do is to emit a CRLF immediately
122 * after we reach the last column; this takes us to a known state.
123 */
124void
28d7d09f 125cmcheckmagic (struct tty_display_info *tty)
902a3b86 126{
6548cf00 127 if (curX (tty) == FrameCols (tty))
902a3b86 128 {
6548cf00 129 if (!MagicWrap (tty) || curY (tty) >= FrameRows (tty) - 1)
902a3b86 130 abort ();
0b0d3e0b
KL
131 if (tty->termscript)
132 putc ('\r', tty->termscript);
133 putc ('\r', tty->output);
134 if (tty->termscript)
135 putc ('\n', tty->termscript);
136 putc ('\n', tty->output);
6548cf00
KL
137 curX (tty) = 0;
138 curY (tty)++;
902a3b86
KH
139 }
140}
141
142
d427b66a
JB
143/*
144 * (Re)Initialize the cost factors, given the output speed of the terminal
145 * in the variable ospeed. (Note: this holds B300, B9600, etc -- ie stuff
146 * out of <sgtty.h>.)
147 */
148
dfcf069d 149void
28d7d09f 150cmcostinit (struct tty_display_info *tty)
d427b66a
JB
151{
152 char *p;
153
154#define COST(x,e) (x ? (cost = 0, tputs (x, 1, e), cost) : BIG)
155#define CMCOST(x,e) ((x == 0) ? BIG : (p = tgoto(x, 0, 0), COST(p ,e)))
156
6548cf00
KL
157 tty->Wcm->cc_up = COST (tty->Wcm->cm_up, evalcost);
158 tty->Wcm->cc_down = COST (tty->Wcm->cm_down, evalcost);
159 tty->Wcm->cc_left = COST (tty->Wcm->cm_left, evalcost);
160 tty->Wcm->cc_right = COST (tty->Wcm->cm_right, evalcost);
161 tty->Wcm->cc_home = COST (tty->Wcm->cm_home, evalcost);
162 tty->Wcm->cc_cr = COST (tty->Wcm->cm_cr, evalcost);
163 tty->Wcm->cc_ll = COST (tty->Wcm->cm_ll, evalcost);
164 tty->Wcm->cc_tab = tty->Wcm->cm_tabwidth ? COST (tty->Wcm->cm_tab, evalcost) : BIG;
d427b66a
JB
165
166 /*
167 * These last three are actually minimum costs. When (if) they are
168 * candidates for the least-cost motion, the real cost is computed.
169 * (Note that "0" is the assumed to generate the minimum cost.
170 * While this is not necessarily true, I have yet to see a terminal
171 * for which is not; all the terminals that have variable-cost
172 * cursor motion seem to take straight numeric values. --ACT)
173 */
174
6548cf00
KL
175 tty->Wcm->cc_abs = CMCOST (tty->Wcm->cm_abs, evalcost);
176 tty->Wcm->cc_habs = CMCOST (tty->Wcm->cm_habs, evalcost);
177 tty->Wcm->cc_vabs = CMCOST (tty->Wcm->cm_vabs, evalcost);
d427b66a
JB
178
179#undef CMCOST
180#undef COST
181}
182
183/*
184 * Calculate the cost to move from (srcy, srcx) to (dsty, dstx) using
185 * up and down, and left and right, motions, and tabs. If doit is set
186 * actually perform the motion.
187 */
188
dfcf069d 189static int
28d7d09f
KL
190calccost (struct tty_display_info *tty,
191 int srcy, int srcx, int dsty, int dstx, int doit)
d427b66a
JB
192{
193 register int deltay,
194 deltax,
195 c,
196 totalcost;
197 int ntabs,
198 n2tabs,
199 tabx,
200 tab2x,
201 tabcost;
202 register char *p;
203
204 /* If have just wrapped on a terminal with xn,
205 don't believe the cursor position: give up here
206 and force use of absolute positioning. */
207
6548cf00 208 if (curX (tty) == tty->Wcm->cm_cols)
d427b66a
JB
209 goto fail;
210
211 totalcost = 0;
212 if ((deltay = dsty - srcy) == 0)
213 goto x;
214 if (deltay < 0)
6548cf00 215 p = tty->Wcm->cm_up, c = tty->Wcm->cc_up, deltay = -deltay;
d427b66a 216 else
6548cf00 217 p = tty->Wcm->cm_down, c = tty->Wcm->cc_down;
d427b66a
JB
218 if (c == BIG) { /* caint get thar from here */
219 if (doit)
220 printf ("OOPS");
221 return c;
222 }
223 totalcost = c * deltay;
224 if (doit)
225 while (--deltay >= 0)
28d440ab 226 emacs_tputs (tty, p, 1, cmputc);
177c0ea7 227x:
d427b66a
JB
228 if ((deltax = dstx - srcx) == 0)
229 goto done;
230 if (deltax < 0) {
6548cf00 231 p = tty->Wcm->cm_left, c = tty->Wcm->cc_left, deltax = -deltax;
d427b66a
JB
232 goto dodelta; /* skip all the tab junk */
233 }
234 /* Tabs (the toughie) */
6548cf00 235 if (tty->Wcm->cc_tab >= BIG || !tty->Wcm->cm_usetabs)
d427b66a
JB
236 goto olddelta; /* forget it! */
237
177c0ea7 238 /*
d427b66a
JB
239 * ntabs is # tabs towards but not past dstx; n2tabs is one more
240 * (ie past dstx), but this is only valid if that is not past the
241 * right edge of the screen. We can check that at the same time
242 * as we figure out where we would be if we use the tabs (which
243 * we will put into tabx (for ntabs) and tab2x (for n2tabs)).
244 */
245
6548cf00 246 ntabs = (deltax + srcx % tty->Wcm->cm_tabwidth) / tty->Wcm->cm_tabwidth;
d427b66a 247 n2tabs = ntabs + 1;
6548cf00
KL
248 tabx = (srcx / tty->Wcm->cm_tabwidth + ntabs) * tty->Wcm->cm_tabwidth;
249 tab2x = tabx + tty->Wcm->cm_tabwidth;
d427b66a 250
6548cf00 251 if (tab2x >= tty->Wcm->cm_cols) /* too far (past edge) */
d427b66a
JB
252 n2tabs = 0;
253
177c0ea7 254 /*
d427b66a
JB
255 * Now set tabcost to the cost for using ntabs, and c to the cost
256 * for using n2tabs, then pick the minimum.
257 */
258
c3b4957f 259 /* cost for ntabs + cost for right motion */
6548cf00 260 tabcost = ntabs ? ntabs * tty->Wcm->cc_tab + (dstx - tabx) * tty->Wcm->cc_right
d427b66a
JB
261 : BIG;
262
c3b4957f 263 /* cost for n2tabs + cost for left motion */
6548cf00 264 c = n2tabs ? n2tabs * tty->Wcm->cc_tab + (tab2x - dstx) * tty->Wcm->cc_left
d427b66a
JB
265 : BIG;
266
267 if (c < tabcost) /* then cheaper to overshoot & back up */
268 ntabs = n2tabs, tabcost = c, tabx = tab2x;
269
270 if (tabcost >= BIG) /* caint use tabs */
271 goto newdelta;
272
177c0ea7 273 /*
d427b66a
JB
274 * See if tabcost is less than just moving right
275 */
276
6548cf00 277 if (tabcost < (deltax * tty->Wcm->cc_right)) {
d427b66a
JB
278 totalcost += tabcost; /* use the tabs */
279 if (doit)
280 while (--ntabs >= 0)
6548cf00 281 emacs_tputs (tty, tty->Wcm->cm_tab, 1, cmputc);
d427b66a
JB
282 srcx = tabx;
283 }
284
177c0ea7 285 /*
d427b66a
JB
286 * Now might as well just recompute the delta.
287 */
288
177c0ea7 289newdelta:
d427b66a
JB
290 if ((deltax = dstx - srcx) == 0)
291 goto done;
177c0ea7 292olddelta:
d427b66a 293 if (deltax > 0)
6548cf00 294 p = tty->Wcm->cm_right, c = tty->Wcm->cc_right;
d427b66a 295 else
6548cf00 296 p = tty->Wcm->cm_left, c = tty->Wcm->cc_left, deltax = -deltax;
d427b66a 297
177c0ea7 298dodelta:
d427b66a
JB
299 if (c == BIG) { /* caint get thar from here */
300fail:
301 if (doit)
302 printf ("OOPS");
303 return BIG;
304 }
305 totalcost += c * deltax;
306 if (doit)
307 while (--deltax >= 0)
28d440ab 308 emacs_tputs (tty, p, 1, cmputc);
177c0ea7 309done:
d427b66a
JB
310 return totalcost;
311}
312
313#if 0
314losecursor ()
315{
316 curY = -1;
317}
318#endif
319
320#define USEREL 0
321#define USEHOME 1
322#define USELL 2
323#define USECR 3
324
dfcf069d 325void
d3da34e0 326cmgoto (struct tty_display_info *tty, int row, int col)
d427b66a
JB
327{
328 int homecost,
329 crcost,
330 llcost,
331 relcost,
332 directcost;
333 int use;
334 char *p,
335 *dcm;
336
337 /* First the degenerate case */
6548cf00 338 if (row == curY (tty) && col == curX (tty)) /* already there */
d427b66a
JB
339 return;
340
6548cf00 341 if (curY (tty) >= 0 && curX (tty) >= 0)
d427b66a
JB
342 {
343 /* We may have quick ways to go to the upper-left, bottom-left,
344 * start-of-line, or start-of-next-line. Or it might be best to
345 * start where we are. Examine the options, and pick the cheapest.
346 */
347
6548cf00 348 relcost = calccost (tty, curY (tty), curX (tty), row, col, 0);
d427b66a 349 use = USEREL;
6548cf00 350 if ((homecost = tty->Wcm->cc_home) < BIG)
28d440ab 351 homecost += calccost (tty, 0, 0, row, col, 0);
d427b66a 352 if (homecost < relcost)
28d440ab 353 relcost = homecost, use = USEHOME;
6548cf00
KL
354 if ((llcost = tty->Wcm->cc_ll) < BIG)
355 llcost += calccost (tty, tty->Wcm->cm_rows - 1, 0, row, col, 0);
d427b66a 356 if (llcost < relcost)
28d440ab 357 relcost = llcost, use = USELL;
6548cf00
KL
358 if ((crcost = tty->Wcm->cc_cr) < BIG) {
359 if (tty->Wcm->cm_autolf)
360 if (curY (tty) + 1 >= tty->Wcm->cm_rows)
361 crcost = BIG;
d427b66a 362 else
6548cf00 363 crcost += calccost (tty, curY (tty) + 1, 0, row, col, 0);
d427b66a 364 else
6548cf00 365 crcost += calccost (tty, curY (tty), 0, row, col, 0);
d427b66a
JB
366 }
367 if (crcost < relcost)
368 relcost = crcost, use = USECR;
6548cf00
KL
369 directcost = tty->Wcm->cc_abs, dcm = tty->Wcm->cm_abs;
370 if (row == curY (tty) && tty->Wcm->cc_habs < BIG)
371 directcost = tty->Wcm->cc_habs, dcm = tty->Wcm->cm_habs;
372 else if (col == curX (tty) && tty->Wcm->cc_vabs < BIG)
373 directcost = tty->Wcm->cc_vabs, dcm = tty->Wcm->cm_vabs;
d427b66a
JB
374 }
375 else
376 {
377 directcost = 0, relcost = 100000;
6548cf00 378 dcm = tty->Wcm->cm_abs;
d427b66a
JB
379 }
380
177c0ea7 381 /*
d427b66a
JB
382 * In the following comparison, the = in <= is because when the costs
383 * are the same, it looks nicer (I think) to move directly there.
384 */
385 if (directcost <= relcost)
386 {
387 /* compute REAL direct cost */
388 cost = 0;
c3b4957f
KL
389 p = (dcm == tty->Wcm->cm_habs
390 ? tgoto (dcm, row, col)
391 : tgoto (dcm, col, row));
28d440ab 392 emacs_tputs (tty, p, 1, evalcost);
d427b66a
JB
393 if (cost <= relcost)
394 { /* really is cheaper */
28d440ab 395 emacs_tputs (tty, p, 1, cmputc);
6548cf00 396 curY (tty) = row, curX (tty) = col;
d427b66a
JB
397 return;
398 }
399 }
400
401 switch (use)
402 {
177c0ea7 403 case USEHOME:
6548cf00
KL
404 emacs_tputs (tty, tty->Wcm->cm_home, 1, cmputc);
405 curY (tty) = 0, curX (tty) = 0;
d427b66a
JB
406 break;
407
177c0ea7 408 case USELL:
6548cf00
KL
409 emacs_tputs (tty, tty->Wcm->cm_ll, 1, cmputc);
410 curY (tty) = tty->Wcm->cm_rows - 1, curX (tty) = 0;
d427b66a
JB
411 break;
412
177c0ea7 413 case USECR:
6548cf00
KL
414 emacs_tputs (tty, tty->Wcm->cm_cr, 1, cmputc);
415 if (tty->Wcm->cm_autolf)
416 curY (tty)++;
417 curX (tty) = 0;
d427b66a
JB
418 break;
419 }
420
6548cf00
KL
421 (void) calccost (tty, curY (tty), curX (tty), row, col, 1);
422 curY (tty) = row, curX (tty) = col;
d427b66a
JB
423}
424
425/* Clear out all terminal info.
426 Used before copying into it the info on the actual terminal.
427 */
428
dfcf069d 429void
28d7d09f 430Wcm_clear (struct tty_display_info *tty)
d427b66a 431{
72af86bd 432 memset (tty->Wcm, 0, sizeof (struct cm));
d427b66a
JB
433 UP = 0;
434 BC = 0;
435}
436
437/*
438 * Initialized stuff
439 * Return 0 if can do CM.
440 * Return -1 if cannot.
441 * Return -2 if size not specified.
442 */
443
dfcf069d 444int
28d7d09f 445Wcm_init (struct tty_display_info *tty)
d427b66a
JB
446{
447#if 0
6548cf00 448 if (tty->Wcm->cm_abs && !tty->Wcm->cm_ds)
d427b66a
JB
449 return 0;
450#endif
6548cf00 451 if (tty->Wcm->cm_abs)
d427b66a
JB
452 return 0;
453 /* Require up and left, and, if no absolute, down and right */
6548cf00 454 if (!tty->Wcm->cm_up || !tty->Wcm->cm_left)
d427b66a 455 return - 1;
6548cf00 456 if (!tty->Wcm->cm_abs && (!tty->Wcm->cm_down || !tty->Wcm->cm_right))
d427b66a
JB
457 return - 1;
458 /* Check that we know the size of the screen.... */
6548cf00 459 if (tty->Wcm->cm_rows <= 0 || tty->Wcm->cm_cols <= 0)
d427b66a
JB
460 return - 2;
461 return 0;
462}
ab5796a9 463