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