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
36 EXTERN_C char *tgoto (const char *cm, int hpos, int vpos);
37 EXTERN_C int tputs (const char *, int, void (*)(int));
39 static void cmgoto_for_real (struct console *c, int row, int col);
41 static int cm_cost_counter; /* sums up costs */
49 /* Ugh -- cmputc() can't take a console argument, so we pass it in a global */
50 struct console *cmputc_console;
53 send_string_to_tty_console (struct console *c, unsigned char *str, int len)
55 /* #### Ben sez: don't some terminals need nulls outputted
57 Lstream *lstr = XLSTREAM (CONSOLE_TTY_DATA (c)->outstream);
59 if (CONSOLE_TTY_REAL_CURSOR_X (c) != CONSOLE_TTY_CURSOR_X (c)
60 || CONSOLE_TTY_REAL_CURSOR_Y (c) != CONSOLE_TTY_CURSOR_Y (c))
62 int row = CONSOLE_TTY_CURSOR_Y (c);
63 int col = CONSOLE_TTY_CURSOR_X (c);
64 cmgoto_for_real (c, row, col);
68 Lstream_putc (lstr, *str);
70 Lstream_write (lstr, str, len);
76 unsigned char ch = (unsigned char) c;
79 fputc (c, termscript);
81 send_string_to_tty_console (cmputc_console, &ch, 1);
87 * Terminals with magicwrap (xn) don't all behave identically.
88 * The VT100 leaves the cursor in the last column but will wrap before
89 * printing the next character. I hear that the Concept terminal does
90 * the wrap immediately but ignores the next newline it sees. And some
91 * terminals just have buggy firmware, and think that the cursor is still
92 * in limbo if we use direct cursor addressing from the phantom column.
93 * The only guaranteed safe thing to do is to emit a CRLF immediately
94 * after we reach the last column; this takes us to a known state.
99 if (curX == FrameCols)
101 if (!MagicWrap || curY >= FrameRows - 1)
104 putc ('\r', termscript);
107 putc ('\n', termscript);
117 * (Re)Initialize the cost factors, given the output speed of the
118 * terminal in DEVICE_TTY_DATA (dev)->ospeed. (Note: this holds B300,
119 * B9600, etc -- ie stuff out of <sgtty.h>.)
122 cm_cost_init (struct console *c)
127 #define COST(x,e) (x \
128 ? (cm_cost_counter = 0, tputs (x, 1, e), cm_cost_counter) \
130 #define MINCOST(x,e) ((x == 0) \
132 : (tmp = tgoto(x, 0, 0), COST(tmp,e)))
134 TTY_COST (c).cm_up = COST (TTY_CM (c).up, evalcost);
135 TTY_COST (c).cm_down = COST (TTY_CM (c).down, evalcost);
136 TTY_COST (c).cm_left = COST (TTY_CM (c).left, evalcost);
137 TTY_COST (c).cm_right = COST (TTY_CM (c).right, evalcost);
138 TTY_COST (c).cm_home = COST (TTY_CM (c).home, evalcost);
139 TTY_COST (c).cm_low_left = COST (TTY_CM (c).low_left, evalcost);
140 TTY_COST (c).cm_car_return = COST (TTY_CM (c).car_return, evalcost);
143 * These last three are actually minimum costs. When (if) they are
144 * candidates for the least-cost motion, the real cost is computed.
145 * (Note that "0" is the assumed to generate the minimum cost.
146 * While this is not necessarily true, I have yet to see a terminal
147 * for which is not; all the terminals that have variable-cost
148 * cursor motion seem to take straight numeric values. --ACT)
151 TTY_COST (c).cm_abs = MINCOST (TTY_CM (c).abs, evalcost);
152 TTY_COST (c).cm_hor_abs = MINCOST (TTY_CM (c).hor_abs, evalcost);
153 TTY_COST (c).cm_ver_abs = MINCOST (TTY_CM (c).ver_abs, evalcost);
160 * Calculate the cost to move from (srcy, srcx) to (dsty, dstx) using
161 * up and down, and left and right, and motions. If doit is set
162 * actually perform the motion.
167 calccost (struct frame *f, int srcy, int srcx, int dsty, int dstx, int doit)
169 struct console *c = XCONSOLE (FRAME_CONSOLE (f));
176 int ntabs, n2tabs, tabx, tab2x, tabcost;
181 /* If have just wrapped on a terminal with xn,
182 don't believe the cursor position: give up here
183 and force use of absolute positioning. */
184 if (curX == Wcm.cm_cols)
188 deltay = dsty - srcy;
194 motion = TTY_CM (c).up;
195 motion_cost = TTY_COST (c).cm_up;
200 motion = TTY_CM (c).down;
201 motion_cost = TTY_COST (c).cm_down;
204 if (motion_cost == EXPENSIVE)
207 /* #### printing OOF is not acceptable */
211 totalcost = motion_cost * deltay;
214 while (--deltay >= 0)
215 tputs (motion, 1, cmputc);
219 deltax = dstx - srcx;
225 motion = TTY_CM (c).left;
226 motion_cost = TTY_COST (c).cm_left;
231 motion = TTY_CM (c).right;
232 motion_cost = TTY_COST (c).cm_right;
235 if (motion_cost == EXPENSIVE)
238 /* #### printing OOF is not acceptable */
242 totalcost += motion_cost * deltax;
245 while (--deltax >= 0)
246 tputs (motion, 1, cmputc);
258 #if OLD_CURSOR_MOTION_SHIT
260 cmgoto (struct frame *f, int row, int col)
262 struct console *c = XCONSOLE (FRAME_CONSOLE (f));
265 int frame_x = FRAME_CURSOR_X(f);
266 int frame_y = FRAME_CURSOR_Y(f);
267 int relcost, directcost, llcost;
275 /* First the degenerate case */
277 if (row == frame_y && col == frame_x)
281 /* #### something is fucked with the non-absolute cases */
282 motion = tgoto (TTY_CM (c).abs, col, row);
283 tputs (motion, 1, cmputc);
284 CONSOLE_TTY_DATA (c)->cursor_x = col;
285 CONSOLE_TTY_DATA (c)->cursor_y = row;
289 if (frame_y >= 0 && frame_x >= 0)
292 * Pick least-cost motions
295 relcost = calccost (f, frame_y, frame_x, row, col, 0);
298 homecost = TTY_COST (c).cm_home;
299 if (homecost < EXPENSIVE)
300 homecost += calccost (f, 0, 0, row, col, 0);
302 if (homecost < relcost)
308 llcost = TTY_COST (c).cm_low_left;
309 if (llcost < EXPENSIVE)
310 llcost += calccost (f, frame_y - 1, 0, row, col, 0);
312 if (llcost < relcost)
319 if ((crcost = Wcm.cc_cr) < BIG) {
321 if (curY + 1 >= Wcm.cm_rows)
324 crcost += calccost (curY + 1, 0, row, col, 0);
326 crcost += calccost (curY, 0, row, col, 0);
328 if (crcost < relcost)
329 relcost = crcost, use = USECR;
332 directcost = TTY_COST (c).cm_abs;
333 dcm = TTY_CM (c).abs;
335 if (row == frame_y && TTY_COST (c).cm_hor_abs < EXPENSIVE)
337 directcost = TTY_COST (c).cm_hor_abs;
338 dcm = TTY_CM (c).hor_abs;
340 else if (col == frame_x && TTY_COST (c).cm_ver_abs < EXPENSIVE)
342 directcost = TTY_COST (c).cm_ver_abs;
343 dcm = TTY_CM (c).ver_abs;
350 dcm = TTY_CM (c).abs;
354 * In the following comparison, the = in <= is because when the costs
355 * are the same, it looks nicer (I think) to move directly there.
357 if (directcost <= relcost)
359 /* compute REAL direct cost */
361 motion = (dcm == TTY_CM (c).hor_abs
362 ? tgoto (dcm, row, col)
363 : tgoto (dcm, col, row));
364 tputs (motion, 1, evalcost);
365 if (cm_cost_counter <= relcost)
366 { /* really is cheaper */
367 tputs (motion, 1, cmputc);
368 FRAME_CURSOR_Y (f) = row;
369 FRAME_CURSOR_X (f) = col;
377 tputs (TTY_CM (c).home, 1, cmputc);
378 FRAME_CURSOR_X (f) = 0;
379 FRAME_CURSOR_Y (f) = 0;
383 tputs (TTY_CM (c).low_left, 1, cmputc);
384 FRAME_CURSOR_Y (f) = FRAME_HEIGHT (f) - 1;
385 FRAME_CURSOR_X (f) = 0;
390 tputs (Wcm.cm_cr, 1, cmputc);
398 calccost (f, FRAME_CURSOR_Y (f), FRAME_CURSOR_X (f), row, col, 1);
399 FRAME_CURSOR_Y (f) = row;
400 FRAME_CURSOR_X (f) = col;
403 #endif /* OLD_CURSOR_MOTION_SHIT */
405 /*****************************************************************************
408 This function is responsible for getting the cursor from its current
409 location to the passed location in the most efficient manner
411 ****************************************************************************/
413 cmgoto_for_real (struct console *c, int row, int col)
419 /* First make sure that we actually have to do any work at all. */
420 if (CONSOLE_TTY_REAL_CURSOR_X (c) == col
421 && CONSOLE_TTY_REAL_CURSOR_Y (c) == row)
424 CONSOLE_TTY_REAL_CURSOR_X (c) = col;
425 CONSOLE_TTY_REAL_CURSOR_Y (c) = row;
427 /* #### Need to reimplement cost analysis and potential relative
430 /* If all else fails, use absolute movement. */
431 motion = tgoto (TTY_CM (c).abs, col, row);
432 tputs (motion, 1, cmputc);
433 CONSOLE_TTY_CURSOR_X (c) = col;
434 CONSOLE_TTY_CURSOR_Y (c) = row;
438 cmgoto (struct frame *f, int row, int col)
440 /* We delay cursor motion until we do something other than cursor motion,
441 to optimize the case where cmgoto() is called twice in a row. */
442 struct console *c = XCONSOLE (FRAME_CONSOLE (f));
443 CONSOLE_TTY_CURSOR_X (c) = col;
444 CONSOLE_TTY_CURSOR_Y (c) = row;
448 /* Clear out all terminal info.
449 Used before copying into it the info on the actual terminal.
464 * Return 0 if can do CM.
465 * Return -1 if cannot.
466 * Return -2 if size not specified.
473 if (Wcm.cm_abs && !Wcm.cm_ds)
478 /* Require up and left, and, if no absolute, down and right */
479 if (!Wcm.cm_up || !Wcm.cm_left)
481 if (!Wcm.cm_abs && (!Wcm.cm_down || !Wcm.cm_right))
483 /* Check that we know the size of the frame.... */
484 if (Wcm.cm_rows <= 0 || Wcm.cm_cols <= 0)