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
5 This file is part of XEmacs.
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
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
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. */
22 /* Synched up with: FSF 19.30. Substantially different from FSF. */
24 /* #### This file is extremely junky and needs major fixup. */
29 #include "console-tty.h"
32 #include "redisplay.h"
34 #define EXPENSIVE 2000
39 extern char *tgoto (CONST char *cm, int hpos, int vpos);
40 extern void tputs (CONST char *, int, void (*)(int));
45 static void cmgoto_for_real (struct console *c, int row, int col);
47 static int cm_cost_counter; /* sums up costs */
55 /* Ugh -- cmputc() can't take a console argument, so we pass it in a global */
56 struct console *cmputc_console;
59 send_string_to_tty_console (struct console *c, unsigned char *str, int len)
61 /* #### Ben sez: don't some terminals need nulls outputted
63 Lstream *lstr = XLSTREAM (CONSOLE_TTY_DATA (c)->outstream);
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))
68 int row = CONSOLE_TTY_CURSOR_Y (c);
69 int col = CONSOLE_TTY_CURSOR_X (c);
70 cmgoto_for_real (c, row, col);
74 Lstream_putc (lstr, *str);
76 Lstream_write (lstr, str, len);
82 unsigned char ch = (unsigned char) c;
85 fputc (c, termscript);
87 send_string_to_tty_console (cmputc_console, &ch, 1);
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.
105 if (curX == FrameCols)
107 if (!MagicWrap || curY >= FrameRows - 1)
110 putc ('\r', termscript);
113 putc ('\n', termscript);
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>.)
128 cm_cost_init (struct console *c)
133 #define COST(x,e) (x \
134 ? (cm_cost_counter = 0, tputs (x, 1, e), cm_cost_counter) \
136 #define MINCOST(x,e) ((x == 0) \
138 : (tmp = tgoto(x, 0, 0), COST(tmp,e)))
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);
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)
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);
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.
173 calccost (struct frame *f, int srcy, int srcx, int dsty, int dstx, int doit)
175 struct console *c = XCONSOLE (FRAME_CONSOLE (f));
182 int ntabs, n2tabs, tabx, tab2x, tabcost;
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)
194 deltay = dsty - srcy;
200 motion = TTY_CM (c).up;
201 motion_cost = TTY_COST (c).cm_up;
206 motion = TTY_CM (c).down;
207 motion_cost = TTY_COST (c).cm_down;
210 if (motion_cost == EXPENSIVE)
213 /* #### printing OOF is not acceptable */
217 totalcost = motion_cost * deltay;
220 while (--deltay >= 0)
221 tputs (motion, 1, cmputc);
225 deltax = dstx - srcx;
231 motion = TTY_CM (c).left;
232 motion_cost = TTY_COST (c).cm_left;
237 motion = TTY_CM (c).right;
238 motion_cost = TTY_COST (c).cm_right;
241 if (motion_cost == EXPENSIVE)
244 /* #### printing OOF is not acceptable */
248 totalcost += motion_cost * deltax;
251 while (--deltax >= 0)
252 tputs (motion, 1, cmputc);
264 #if OLD_CURSOR_MOTION_SHIT
266 cmgoto (struct frame *f, int row, int col)
268 struct console *c = XCONSOLE (FRAME_CONSOLE (f));
271 int frame_x = FRAME_CURSOR_X(f);
272 int frame_y = FRAME_CURSOR_Y(f);
273 int relcost, directcost, llcost;
281 /* First the degenerate case */
283 if (row == frame_y && col == frame_x)
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;
295 if (frame_y >= 0 && frame_x >= 0)
298 * Pick least-cost motions
301 relcost = calccost (f, frame_y, frame_x, row, col, 0);
304 homecost = TTY_COST (c).cm_home;
305 if (homecost < EXPENSIVE)
306 homecost += calccost (f, 0, 0, row, col, 0);
308 if (homecost < relcost)
314 llcost = TTY_COST (c).cm_low_left;
315 if (llcost < EXPENSIVE)
316 llcost += calccost (f, frame_y - 1, 0, row, col, 0);
318 if (llcost < relcost)
325 if ((crcost = Wcm.cc_cr) < BIG) {
327 if (curY + 1 >= Wcm.cm_rows)
330 crcost += calccost (curY + 1, 0, row, col, 0);
332 crcost += calccost (curY, 0, row, col, 0);
334 if (crcost < relcost)
335 relcost = crcost, use = USECR;
338 directcost = TTY_COST (c).cm_abs;
339 dcm = TTY_CM (c).abs;
341 if (row == frame_y && TTY_COST (c).cm_hor_abs < EXPENSIVE)
343 directcost = TTY_COST (c).cm_hor_abs;
344 dcm = TTY_CM (c).hor_abs;
346 else if (col == frame_x && TTY_COST (c).cm_ver_abs < EXPENSIVE)
348 directcost = TTY_COST (c).cm_ver_abs;
349 dcm = TTY_CM (c).ver_abs;
356 dcm = TTY_CM (c).abs;
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.
363 if (directcost <= relcost)
365 /* compute REAL direct cost */
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;
383 tputs (TTY_CM (c).home, 1, cmputc);
384 FRAME_CURSOR_X (f) = 0;
385 FRAME_CURSOR_Y (f) = 0;
389 tputs (TTY_CM (c).low_left, 1, cmputc);
390 FRAME_CURSOR_Y (f) = FRAME_HEIGHT (f) - 1;
391 FRAME_CURSOR_X (f) = 0;
396 tputs (Wcm.cm_cr, 1, cmputc);
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;
409 #endif /* OLD_CURSOR_MOTION_SHIT */
411 /*****************************************************************************
414 This function is responsible for getting the cursor from its current
415 location to the passed location in the most efficient manner
417 ****************************************************************************/
419 cmgoto_for_real (struct console *c, int row, int col)
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)
430 CONSOLE_TTY_REAL_CURSOR_X (c) = col;
431 CONSOLE_TTY_REAL_CURSOR_Y (c) = row;
433 /* #### Need to reimplement cost analysis and potential relative
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;
444 cmgoto (struct frame *f, int row, int col)
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;
454 /* Clear out all terminal info.
455 Used before copying into it the info on the actual terminal.
470 * Return 0 if can do CM.
471 * Return -1 if cannot.
472 * Return -2 if size not specified.
479 if (Wcm.cm_abs && !Wcm.cm_ds)
484 /* Require up and left, and, if no absolute, down and right */
485 if (!Wcm.cm_up || !Wcm.cm_left)
487 if (!Wcm.cm_abs && (!Wcm.cm_down || !Wcm.cm_right))
489 /* Check that we know the size of the frame.... */
490 if (Wcm.cm_rows <= 0 || Wcm.cm_cols <= 0)