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