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