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