comparison src/cm.c @ 0:376386a54a3c r19-14

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