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