XEmacs 21.2.28 "Hermes".
[chise/xemacs-chise.git.1] / src / cm.c
1 /* Cursor motion subroutines for XEmacs.
2    Copyright (C) 1985, 1994, 1995 Free Software Foundation, Inc.
3     loosely based primarily on public domain code written by Chris Torek
4
5 This file is part of XEmacs.
6
7 XEmacs is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 2, or (at your option) any
10 later version.
11
12 XEmacs is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with XEmacs; 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 /* Synched up with: FSF 19.30.  Substantially different from FSF. */
23
24 /* #### This file is extremely junky and needs major fixup. */
25
26 #include <config.h>
27 #include "lisp.h"
28
29 #include "console-tty.h"
30 #include "frame.h"
31 #include "lstream.h"
32 #include "redisplay.h"
33
34 #define EXPENSIVE 2000
35
36 #ifdef __cplusplus
37 extern "C" {
38 #endif
39 extern char *tgoto (CONST char *cm, int hpos, int vpos);
40 extern void tputs (CONST char *, int, void (*)(int));
41 #ifdef __cplusplus
42 }
43 #endif
44
45 static void cmgoto_for_real (struct console *c, int row, int col);
46
47 static int cm_cost_counter;             /* sums up costs */
48
49 static void
50 evalcost (int c)
51 {
52   cm_cost_counter++;
53 }
54
55 /* Ugh -- cmputc() can't take a console argument, so we pass it in a global */
56 struct console *cmputc_console;
57
58 void
59 send_string_to_tty_console (struct console *c, unsigned char *str, int len)
60 {
61   /* #### Ben sez: don't some terminals need nulls outputted
62      for proper timing? */
63   Lstream *lstr = XLSTREAM (CONSOLE_TTY_DATA (c)->outstream);
64
65   if (CONSOLE_TTY_REAL_CURSOR_X (c) != CONSOLE_TTY_CURSOR_X (c)
66       || CONSOLE_TTY_REAL_CURSOR_Y (c) != CONSOLE_TTY_CURSOR_Y (c))
67     {
68       int row = CONSOLE_TTY_CURSOR_Y (c);
69       int col = CONSOLE_TTY_CURSOR_X (c);
70       cmgoto_for_real (c, row, col);
71     }
72
73   if (len == 1)
74     Lstream_putc (lstr, *str);
75   else if (len > 0)
76     Lstream_write (lstr, str, len);
77 }
78
79 void
80 cmputc (int c)
81 {
82   unsigned char ch = (unsigned char) c;
83
84   if (termscript)
85     fputc (c, termscript);
86
87   send_string_to_tty_console (cmputc_console, &ch, 1);
88 }
89
90 #if 0
91
92 /*
93  * Terminals with magicwrap (xn) don't all behave identically.
94  * The VT100 leaves the cursor in the last column but will wrap before
95  * printing the next character.  I hear that the Concept terminal does
96  * the wrap immediately but ignores the next newline it sees.  And some
97  * terminals just have buggy firmware, and think that the cursor is still
98  * in limbo if we use direct cursor addressing from the phantom column.
99  * The only guaranteed safe thing to do is to emit a CRLF immediately
100  * after we reach the last column; this takes us to a known state.
101  */
102 void
103 cmcheckmagic (void)
104 {
105   if (curX == FrameCols)
106     {
107       if (!MagicWrap || curY >= FrameRows - 1)
108         abort ();
109       if (termscript)
110         putc ('\r', termscript);
111       putchar ('\r');
112       if (termscript)
113         putc ('\n', termscript);
114       putchar ('\n');
115       curX = 0;
116       curY++;
117     }
118 }
119
120 #endif /* 0 */
121
122 /*
123  * (Re)Initialize the cost factors, given the output speed of the
124  * terminal in DEVICE_TTY_DATA (dev)->ospeed.  (Note: this holds B300,
125  * B9600, etc -- ie stuff out of <sgtty.h>.)
126  */
127 void
128 cm_cost_init (struct console *c)
129 {
130   char *tmp;
131
132   cm_cost_counter = 0;
133 #define COST(x,e) (x \
134                    ? (cm_cost_counter = 0, tputs (x, 1, e), cm_cost_counter) \
135                    : EXPENSIVE)
136 #define MINCOST(x,e) ((x == 0) \
137                       ? EXPENSIVE \
138                       : (tmp = tgoto(x, 0, 0), COST(tmp,e)))
139
140   TTY_COST (c).cm_up = COST (TTY_CM (c).up, evalcost);
141   TTY_COST (c).cm_down = COST (TTY_CM (c).down, evalcost);
142   TTY_COST (c).cm_left = COST (TTY_CM (c).left, evalcost);
143   TTY_COST (c).cm_right = COST (TTY_CM (c).right, evalcost);
144   TTY_COST (c).cm_home = COST (TTY_CM (c).home, evalcost);
145   TTY_COST (c).cm_low_left = COST (TTY_CM (c).low_left, evalcost);
146   TTY_COST (c).cm_car_return = COST (TTY_CM (c).car_return, evalcost);
147
148   /*
149    * These last three are actually minimum costs.  When (if) they are
150    * candidates for the least-cost motion, the real cost is computed.
151    * (Note that "0" is the assumed to generate the minimum cost.
152    * While this is not necessarily true, I have yet to see a terminal
153    * for which is not; all the terminals that have variable-cost
154    * cursor motion seem to take straight numeric values.  --ACT)
155    */
156
157   TTY_COST (c).cm_abs = MINCOST (TTY_CM (c).abs, evalcost);
158   TTY_COST (c).cm_hor_abs = MINCOST (TTY_CM (c).hor_abs, evalcost);
159   TTY_COST (c).cm_ver_abs = MINCOST (TTY_CM (c).ver_abs, evalcost);
160
161 #undef MINCOST
162 #undef COST
163 }
164
165 /*
166  * Calculate the cost to move from (srcy, srcx) to (dsty, dstx) using
167  * up and down, and left and right, and motions.  If doit is set
168  * actually perform the motion.
169  */
170
171 #ifdef NOT_YET
172 static int
173 calccost (struct frame *f, int srcy, int srcx, int dsty, int dstx, int doit)
174 {
175   struct console *c = XCONSOLE (FRAME_CONSOLE (f));
176   int totalcost = 0;
177   int deltay, deltax;
178   char *motion;
179   int motion_cost;
180
181 #if 0
182   int ntabs, n2tabs, tabx, tab2x, tabcost;
183 #endif
184
185   cmputc_console = c;
186 #if 0
187     /* If have just wrapped on a terminal with xn,
188        don't believe the cursor position: give up here
189        and force use of absolute positioning.  */
190     if (curX == Wcm.cm_cols)
191       goto fail;
192 #endif
193
194   deltay = dsty - srcy;
195   if (!deltay)
196     goto calculate_x;
197
198   if (deltay < 0)
199     {
200       motion = TTY_CM (c).up;
201       motion_cost = TTY_COST (c).cm_up;
202       deltay = -deltay;
203     }
204   else
205     {
206       motion = TTY_CM (c).down;
207       motion_cost = TTY_COST (c).cm_down;
208     }
209
210   if (motion_cost == EXPENSIVE)
211     {
212 /*      if (doit) */
213         /* #### printing OOF is not acceptable */
214       return motion_cost;
215     }
216
217   totalcost = motion_cost * deltay;
218
219   if (doit)
220     while (--deltay >= 0)
221       tputs (motion, 1, cmputc);
222
223 calculate_x:
224
225   deltax = dstx - srcx;
226   if (!deltax)
227     goto done;
228
229   if (deltax < 0)
230     {
231       motion = TTY_CM (c).left;
232       motion_cost = TTY_COST (c).cm_left;
233       deltax = -deltax;
234     }
235   else
236     {
237       motion = TTY_CM (c).right;
238       motion_cost = TTY_COST (c).cm_right;
239     }
240
241   if (motion_cost == EXPENSIVE)
242     {
243 /*      if (doit) */
244         /* #### printing OOF is not acceptable */
245         return motion_cost;
246     }
247
248   totalcost += motion_cost * deltax;
249
250   if (doit)
251     while (--deltax >= 0)
252       tputs (motion, 1, cmputc);
253
254 done:
255     return totalcost;
256 }
257 #endif /* NOT_YET */
258
259 #define USEREL  0
260 #define USEHOME 1
261 #define USELL   2
262 #define USECR   3
263
264 #if OLD_CURSOR_MOTION_SHIT
265 void
266 cmgoto (struct frame *f, int row, int col)
267 {
268   struct console *c = XCONSOLE (FRAME_CONSOLE (f));
269   char *motion;
270 #if 0
271   int frame_x = FRAME_CURSOR_X(f);
272   int frame_y = FRAME_CURSOR_Y(f);
273   int relcost, directcost, llcost;
274   int homecost;
275   int use;
276   char *dcm;
277 #endif
278
279   cmputc_console = c;
280
281   /* First the degenerate case */
282 #if 0
283   if (row == frame_y && col == frame_x)
284     return;
285 #endif
286
287   /* #### something is fucked with the non-absolute cases */
288   motion = tgoto (TTY_CM (c).abs, col, row);
289   tputs (motion, 1, cmputc);
290   CONSOLE_TTY_DATA (c)->cursor_x = col;
291   CONSOLE_TTY_DATA (c)->cursor_y = row;
292   return;
293
294 #if 0
295   if (frame_y >= 0 && frame_x >= 0)
296     {
297       /*
298        * Pick least-cost motions
299        */
300
301       relcost = calccost (f, frame_y, frame_x, row, col, 0);
302       use = USEREL;
303
304       homecost = TTY_COST (c).cm_home;
305       if (homecost < EXPENSIVE)
306         homecost += calccost (f, 0, 0, row, col, 0);
307
308       if (homecost < relcost)
309         {
310           relcost = homecost;
311           use = USEHOME;
312         }
313
314       llcost = TTY_COST (c).cm_low_left;
315       if (llcost < EXPENSIVE)
316         llcost += calccost (f, frame_y - 1, 0, row, col, 0);
317
318       if (llcost < relcost)
319         {
320           relcost = llcost;
321           use = USELL;
322         }
323
324 #if 0
325       if ((crcost = Wcm.cc_cr) < BIG) {
326           if (Wcm.cm_autolf)
327               if (curY + 1 >= Wcm.cm_rows)
328                   crcost = BIG;
329               else
330                   crcost += calccost (curY + 1, 0, row, col, 0);
331           else
332               crcost += calccost (curY, 0, row, col, 0);
333       }
334       if (crcost < relcost)
335           relcost = crcost, use = USECR;
336 #endif
337
338       directcost = TTY_COST (c).cm_abs;
339       dcm = TTY_CM (c).abs;
340
341       if (row == frame_y && TTY_COST (c).cm_hor_abs < EXPENSIVE)
342         {
343           directcost = TTY_COST (c).cm_hor_abs;
344           dcm = TTY_CM (c).hor_abs;
345         }
346       else if (col == frame_x && TTY_COST (c).cm_ver_abs < EXPENSIVE)
347         {
348           directcost = TTY_COST (c).cm_ver_abs;
349           dcm = TTY_CM (c).ver_abs;
350         }
351     }
352   else
353     {
354       directcost = 0;
355       relcost = 100000;
356       dcm = TTY_CM (c).abs;
357     }
358
359   /*
360    * In the following comparison, the = in <= is because when the costs
361    * are the same, it looks nicer (I think) to move directly there.
362    */
363   if (directcost <= relcost)
364     {
365       /* compute REAL direct cost */
366       cm_cost_counter = 0;
367       motion = (dcm == TTY_CM (c).hor_abs
368                 ? tgoto (dcm, row, col)
369                 : tgoto (dcm, col, row));
370       tputs (motion, 1, evalcost);
371       if (cm_cost_counter <= relcost)
372         {       /* really is cheaper */
373           tputs (motion, 1, cmputc);
374           FRAME_CURSOR_Y (f) = row;
375           FRAME_CURSOR_X (f) = col;
376           return;
377         }
378     }
379
380   switch (use)
381     {
382     case USEHOME:
383       tputs (TTY_CM (c).home, 1, cmputc);
384       FRAME_CURSOR_X (f) = 0;
385       FRAME_CURSOR_Y (f) = 0;
386       break;
387
388     case USELL:
389       tputs (TTY_CM (c).low_left, 1, cmputc);
390       FRAME_CURSOR_Y (f) = FRAME_HEIGHT (f) - 1;
391       FRAME_CURSOR_X (f) = 0;
392       break;
393
394 #if 0
395     case USECR:
396       tputs (Wcm.cm_cr, 1, cmputc);
397       if (Wcm.cm_autolf)
398         curY++;
399       curX = 0;
400       break;
401 #endif
402     }
403
404   calccost (f, FRAME_CURSOR_Y (f), FRAME_CURSOR_X (f), row, col, 1);
405   FRAME_CURSOR_Y (f) = row;
406   FRAME_CURSOR_X (f) = col;
407 #endif
408 }
409 #endif /* OLD_CURSOR_MOTION_SHIT */
410
411 /*****************************************************************************
412  cmgoto
413
414  This function is responsible for getting the cursor from its current
415  location to the passed location in the most efficient manner
416  possible.
417  ****************************************************************************/
418 static void
419 cmgoto_for_real (struct console *c, int row, int col)
420 {
421   char *motion;
422
423   cmputc_console = c;
424
425   /* First make sure that we actually have to do any work at all. */
426   if (CONSOLE_TTY_REAL_CURSOR_X (c) == col
427       && CONSOLE_TTY_REAL_CURSOR_Y (c) == row)
428     return;
429
430   CONSOLE_TTY_REAL_CURSOR_X (c) = col;
431   CONSOLE_TTY_REAL_CURSOR_Y (c) = row;
432
433   /* #### Need to reimplement cost analysis and potential relative
434      movement. */
435
436   /* If all else fails, use absolute movement. */
437   motion = tgoto (TTY_CM (c).abs, col, row);
438   tputs (motion, 1, cmputc);
439   CONSOLE_TTY_CURSOR_X (c) = col;
440   CONSOLE_TTY_CURSOR_Y (c) = row;
441 }
442
443 void
444 cmgoto (struct frame *f, int row, int col)
445 {
446   /* We delay cursor motion until we do something other than cursor motion,
447      to optimize the case where cmgoto() is called twice in a row. */
448   struct console *c = XCONSOLE (FRAME_CONSOLE (f));
449   CONSOLE_TTY_CURSOR_X (c) = col;
450   CONSOLE_TTY_CURSOR_Y (c) = row;
451 }
452
453 #if 0
454 /* Clear out all terminal info.
455    Used before copying into it the info on the actual terminal.
456  */
457
458 void
459 Wcm_clear (void)
460 {
461   xzero (Wcm);
462   UP = 0;
463   BC = 0;
464 }
465 #endif
466
467 #if 0
468 /*
469  * Initialized stuff
470  * Return 0 if can do CM.
471  * Return -1 if cannot.
472  * Return -2 if size not specified.
473  */
474
475 int
476 Wcm_init (void)
477 {
478 #if 0
479   if (Wcm.cm_abs && !Wcm.cm_ds)
480     return 0;
481 #endif
482   if (Wcm.cm_abs)
483     return 0;
484   /* Require up and left, and, if no absolute, down and right */
485   if (!Wcm.cm_up || !Wcm.cm_left)
486     return - 1;
487   if (!Wcm.cm_abs && (!Wcm.cm_down || !Wcm.cm_right))
488     return - 1;
489   /* Check that we know the size of the frame.... */
490   if (Wcm.cm_rows <= 0 || Wcm.cm_cols <= 0)
491     return - 2;
492   return 0;
493 }
494 #endif