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