view src/cm.c @ 826:6728e641994e

[xemacs-hg @ 2002-05-05 11:30:15 by ben] syntax cache, 8-bit-format, lots of code cleanup README.packages: Update info about --package-path. i.c: Create an inheritable event and pass it on to XEmacs, so that ^C can be handled properly. Intercept ^C and signal the event. "Stop Build" in VC++ now works. bytecomp-runtime.el: Doc string changes. compat.el: Some attempts to redo this to make it truly useful and fix the "multiple versions interacting with each other" problem. Not yet done. Currently doesn't work. files.el: Use with-obsolete-variable to avoid warnings in new revert-buffer code. xemacs.mak: Split up CFLAGS into a version without flags specifying the C library. The problem seems to be that minitar depends on zlib, which depends specifically on libc.lib, not on any of the other C libraries. Unless you compile with libc.lib, you get errors -- specifically, no _errno in the other libraries, which must make it something other than an int. (#### But this doesn't seem to obtain in XEmacs, which also uses zlib, and can be linked with any of the C libraries. Maybe zlib is used differently and doesn't need errno, or maybe XEmacs provides an int errno; ... I don't understand. Makefile.in.in: Fix so that packages are around when testing. abbrev.c, alloc.c, buffer.c, buffer.h, bytecode.c, callint.c, casefiddle.c, casetab.c, casetab.h, charset.h, chartab.c, chartab.h, cmds.c, console-msw.h, console-stream.c, console-x.c, console.c, console.h, data.c, device-msw.c, device.c, device.h, dialog-msw.c, dialog-x.c, dired-msw.c, dired.c, doc.c, doprnt.c, dumper.c, editfns.c, elhash.c, emacs.c, eval.c, event-Xt.c, event-gtk.c, event-msw.c, event-stream.c, events.c, events.h, extents.c, extents.h, faces.c, file-coding.c, file-coding.h, fileio.c, fns.c, font-lock.c, frame-gtk.c, frame-msw.c, frame-x.c, frame.c, frame.h, glade.c, glyphs-gtk.c, glyphs-msw.c, glyphs-msw.h, glyphs-x.c, glyphs.c, glyphs.h, gui-msw.c, gui-x.c, gui.h, gutter.h, hash.h, indent.c, insdel.c, intl-win32.c, intl.c, keymap.c, lisp-disunion.h, lisp-union.h, lisp.h, lread.c, lrecord.h, lstream.c, lstream.h, marker.c, menubar-gtk.c, menubar-msw.c, menubar-x.c, menubar.c, minibuf.c, mule-ccl.c, mule-charset.c, mule-coding.c, mule-wnnfns.c, nas.c, objects-msw.c, objects-x.c, opaque.c, postgresql.c, print.c, process-nt.c, process-unix.c, process.c, process.h, profile.c, rangetab.c, redisplay-gtk.c, redisplay-msw.c, redisplay-output.c, redisplay-x.c, redisplay.c, redisplay.h, regex.c, regex.h, scrollbar-msw.c, search.c, select-x.c, specifier.c, specifier.h, symbols.c, symsinit.h, syntax.c, syntax.h, syswindows.h, tests.c, text.c, text.h, tooltalk.c, ui-byhand.c, ui-gtk.c, unicode.c, win32.c, window.c: Another big Ben patch. -- FUNCTIONALITY CHANGES: add partial support for 8-bit-fixed, 16-bit-fixed, and 32-bit-fixed formats. not quite done yet. (in particular, needs functions to actually convert the buffer.) NOTE: lots of changes to regex.c here. also, many new *_fmt() inline funs that take an Internal_Format argument. redo syntax cache code. make the cache per-buffer; keep the cache valid across calls to functions that use it. also keep it valid across insertions/deletions and extent changes, as much as is possible. eliminate the junky regex-reentrancy code by passing in the relevant lisp info to the regex routines as local vars. add general mechanism in extents code for signalling extent changes. fix numerous problems with the case-table implementation; yoshiki never properly transferred many algorithms from old-style to new-style case tables. redo char tables to support a default argument, so that mapping only occurs over changed args. change many chartab functions to accept Lisp_Object instead of Lisp_Char_Table *. comment out the code in font-lock.c by default, because font-lock.el no longer uses it. we should consider eliminating it entirely. Don't output bell as ^G in console-stream when not a TTY. add -mswindows-termination-handle to interface with i.c, so we can properly kill a build. add more error-checking to buffer/string macros. add some additional buffer_or_string_() funs. -- INTERFACE CHANGES AFFECTING MORE CODE: switch the arguments of write_c_string and friends to be consistent with write_fmt_string, which must have printcharfun first. change BI_* macros to BYTE_* for increased clarity; similarly for bi_* local vars. change VOID_TO_LISP to be a one-argument function. eliminate no-longer-needed CVOID_TO_LISP. -- char/string macro changes: rename MAKE_CHAR() to make_emchar() for slightly less confusion with make_char(). (The former generates an Emchar, the latter a Lisp object. Conceivably we should rename make_char() -> wrap_char() and similarly for make_int(), make_float().) Similar changes for other *CHAR* macros -- we now consistently use names with `emchar' whenever we are working with Emchars. Any remaining name with just `char' always refers to a Lisp object. rename macros with XSTRING_* to string_* except for those that reference actual fields in the Lisp_String object, following conventions used elsewhere. rename set_string_{data,length} macros (the only ones to work with a Lisp_String_* instead of a Lisp_Object) to set_lispstringp_* to make the difference clear. try to be consistent about caps vs. lowercase in macro/inline-fun names for chars and such, which wasn't the case before. we now reserve caps either for XFOO_ macros that reference object fields (e.g. XSTRING_DATA) or for things that have non-function semantics, e.g. directly modifying an arg (BREAKUP_EMCHAR) or evaluating an arg (any arg) more than once. otherwise, use lowercase. here is a summary of most of the macros/inline funs changed by all of the above changes: BYTE_*_P -> byte_*_p XSTRING_BYTE -> string_byte set_string_data/length -> set_lispstringp_data/length XSTRING_CHAR_LENGTH -> string_char_length XSTRING_CHAR -> string_emchar INTBYTE_FIRST_BYTE_P -> intbyte_first_byte_p INTBYTE_LEADING_BYTE_P -> intbyte_leading_byte_p charptr_copy_char -> charptr_copy_emchar LEADING_BYTE_* -> leading_byte_* CHAR_* -> EMCHAR_* *_CHAR_* -> *_EMCHAR_* *_CHAR -> *_EMCHAR CHARSET_BY_ -> charset_by_* BYTE_SHIFT_JIS* -> byte_shift_jis* BYTE_BIG5* -> byte_big5* REP_BYTES_BY_FIRST_BYTE -> rep_bytes_by_first_byte char_to_unicode -> emchar_to_unicode valid_char_p -> valid_emchar_p Change intbyte_strcmp -> qxestrcmp_c (duplicated functionality). -- INTERFACE CHANGES AFFECTING LESS CODE: use DECLARE_INLINE_HEADER in various places. remove '#ifdef emacs' from XEmacs-only files. eliminate CHAR_TABLE_VALUE(), which duplicated the functionality of get_char_table(). add BUFFER_TEXT_LOOP to simplify iterations over buffer text. define typedefs for signed and unsigned types of fixed sizes (INT_32_BIT, UINT_32_BIT, etc.). create ALIGN_FOR_TYPE as a higher-level interface onto ALIGN_SIZE; fix code to use it. add charptr_emchar_len to return the text length of the character pointed to by a ptr; use it in place of charcount_to_bytecount(..., 1). add emchar_len to return the text length of a given character. add types Bytexpos and Charxpos to generalize Bytebpos/Bytecount and Charbpos/Charcount, in code (particularly, the extents code and redisplay code) that works with either kind of index. rename redisplay struct params with names such as `charbpos' to e.g. `charpos' when they are e.g. a Charxpos, not a Charbpos. eliminate xxDEFUN in place of DEFUN; no longer necessary with changes awhile back to doc.c. split up big ugly combined list of EXFUNs in lisp.h on a file-by-file basis, since other prototypes are similarly split. rewrite some "*_UNSAFE" macros as inline funs and eliminate the _UNSAFE suffix. move most string code from lisp.h to text.h; the string code and text.h code is now intertwined in such a fashion that they need to be in the same place and partially interleaved. (you can't create forward references for inline funs) automated/lisp-tests.el, automated/symbol-tests.el, automated/test-harness.el: Fix test harness to output FAIL messages to stderr when in batch mode. Fix up some problems in lisp-tests/symbol-tests that were causing spurious failures.
author ben
date Sun, 05 May 2002 11:33:57 +0000
parents a5954632b187
children 79c6ff3eef26
line wrap: on
line source

/* Cursor motion subroutines for XEmacs.
   Copyright (C) 1985, 1994, 1995 Free Software Foundation, Inc.
    loosely based primarily on public domain code written by Chris Torek

This file is part of XEmacs.

XEmacs is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; either version 2, or (at your option) any
later version.

XEmacs is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
for more details.

You should have received a copy of the GNU General Public License
along with XEmacs; see the file COPYING.  If not, write to
the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.  */

/* Synched up with: FSF 19.30.  Substantially different from FSF. */

/* #### This file is extremely junky and needs major fixup. */

#include <config.h>
#include "lisp.h"

#include "device.h"
#include "frame.h"
#include "lstream.h"
#include "redisplay.h"

#include "console-tty.h"

#define	EXPENSIVE 2000

EXTERN_C char *tgoto (const char *cm, int hpos, int vpos);
EXTERN_C int tputs (const char *, int, void (*)(int));

static void cmgoto_for_real (struct console *c, int row, int col);

static int cm_cost_counter;		/* sums up costs */

static void
evalcost (int c)
{
  cm_cost_counter++;
}

/* Ugh -- cmputc() can't take a console argument, so we pass it in a global */
struct console *cmputc_console;

void
send_string_to_tty_console (struct console *c, unsigned char *str, int len)
{
  /* #### Ben sez: don't some terminals need nulls outputted
     for proper timing? */
  Lstream *lstr = XLSTREAM (CONSOLE_TTY_DATA (c)->outstream);

  if (CONSOLE_TTY_REAL_CURSOR_X (c) != CONSOLE_TTY_CURSOR_X (c)
      || CONSOLE_TTY_REAL_CURSOR_Y (c) != CONSOLE_TTY_CURSOR_Y (c))
    {
      int row = CONSOLE_TTY_CURSOR_Y (c);
      int col = CONSOLE_TTY_CURSOR_X (c);
      cmgoto_for_real (c, row, col);
    }

  if (len == 1)
    Lstream_putc (lstr, *str);
  else if (len > 0)
    Lstream_write (lstr, str, len);
}

void
cmputc (int c)
{
  unsigned char ch = (unsigned char) c;

  if (termscript)
    fputc (c, termscript);

  send_string_to_tty_console (cmputc_console, &ch, 1);
}

#if 0

/*
 * Terminals with magicwrap (xn) don't all behave identically.
 * The VT100 leaves the cursor in the last column but will wrap before
 * printing the next character.  I hear that the Concept terminal does
 * the wrap immediately but ignores the next newline it sees.  And some
 * terminals just have buggy firmware, and think that the cursor is still
 * in limbo if we use direct cursor addressing from the phantom column.
 * The only guaranteed safe thing to do is to emit a CRLF immediately
 * after we reach the last column; this takes us to a known state.
 */
void
cmcheckmagic (void)
{
  if (curX == FrameCols)
    {
      if (!MagicWrap || curY >= FrameRows - 1)
	abort ();
      if (termscript)
	putc ('\r', termscript);
      putchar ('\r');
      if (termscript)
	putc ('\n', termscript);
      putchar ('\n');
      curX = 0;
      curY++;
    }
}

#endif /* 0 */

/*
 * (Re)Initialize the cost factors, given the output speed of the
 * terminal in DEVICE_TTY_DATA (dev)->ospeed.  (Note: this holds B300,
 * B9600, etc -- ie stuff out of <sgtty.h>.)
 */
void
cm_cost_init (struct console *c)
{
  char *tmp;

  cm_cost_counter = 0;
#define	COST(x,e) (x \
		   ? (cm_cost_counter = 0, tputs (x, 1, e), cm_cost_counter) \
		   : EXPENSIVE)
#define MINCOST(x,e) ((x == 0) \
		      ? EXPENSIVE \
		      : (tmp = tgoto(x, 0, 0), COST(tmp,e)))

  TTY_COST (c).cm_up = COST (TTY_CM (c).up, evalcost);
  TTY_COST (c).cm_down = COST (TTY_CM (c).down, evalcost);
  TTY_COST (c).cm_left = COST (TTY_CM (c).left, evalcost);
  TTY_COST (c).cm_right = COST (TTY_CM (c).right, evalcost);
  TTY_COST (c).cm_home = COST (TTY_CM (c).home, evalcost);
  TTY_COST (c).cm_low_left = COST (TTY_CM (c).low_left, evalcost);
  TTY_COST (c).cm_car_return = COST (TTY_CM (c).car_return, evalcost);

  /*
   * These last three are actually minimum costs.  When (if) they are
   * candidates for the least-cost motion, the real cost is computed.
   * (Note that "0" is the assumed to generate the minimum cost.
   * While this is not necessarily true, I have yet to see a terminal
   * for which is not; all the terminals that have variable-cost
   * cursor motion seem to take straight numeric values.  --ACT)
   */

  TTY_COST (c).cm_abs = MINCOST (TTY_CM (c).abs, evalcost);
  TTY_COST (c).cm_hor_abs = MINCOST (TTY_CM (c).hor_abs, evalcost);
  TTY_COST (c).cm_ver_abs = MINCOST (TTY_CM (c).ver_abs, evalcost);

#undef MINCOST
#undef COST
}

/*
 * Calculate the cost to move from (srcy, srcx) to (dsty, dstx) using
 * up and down, and left and right, and motions.  If doit is set
 * actually perform the motion.
 */

#ifdef NOT_YET
static int
calccost (struct frame *f, int srcy, int srcx, int dsty, int dstx, int doit)
{
  struct console *c = XCONSOLE (FRAME_CONSOLE (f));
  int totalcost = 0;
  int deltay, deltax;
  char *motion;
  int motion_cost;

#if 0
  int ntabs, n2tabs, tabx, tab2x, tabcost;
#endif

  cmputc_console = c;
#if 0
    /* If have just wrapped on a terminal with xn,
       don't believe the cursor position: give up here
       and force use of absolute positioning.  */
    if (curX == Wcm.cm_cols)
      goto fail;
#endif

  deltay = dsty - srcy;
  if (!deltay)
    goto calculate_x;

  if (deltay < 0)
    {
      motion = TTY_CM (c).up;
      motion_cost = TTY_COST (c).cm_up;
      deltay = -deltay;
    }
  else
    {
      motion = TTY_CM (c).down;
      motion_cost = TTY_COST (c).cm_down;
    }

  if (motion_cost == EXPENSIVE)
    {
/*      if (doit) */
	/* #### printing OOF is not acceptable */
      return motion_cost;
    }

  totalcost = motion_cost * deltay;

  if (doit)
    while (--deltay >= 0)
      tputs (motion, 1, cmputc);

calculate_x:

  deltax = dstx - srcx;
  if (!deltax)
    goto done;

  if (deltax < 0)
    {
      motion = TTY_CM (c).left;
      motion_cost = TTY_COST (c).cm_left;
      deltax = -deltax;
    }
  else
    {
      motion = TTY_CM (c).right;
      motion_cost = TTY_COST (c).cm_right;
    }

  if (motion_cost == EXPENSIVE)
    {
/*	if (doit) */
        /* #### printing OOF is not acceptable */
	return motion_cost;
    }

  totalcost += motion_cost * deltax;

  if (doit)
    while (--deltax >= 0)
      tputs (motion, 1, cmputc);

done:
    return totalcost;
}
#endif /* NOT_YET */

#define	USEREL	0
#define	USEHOME	1
#define	USELL	2
#define	USECR	3

#ifdef OLD_CURSOR_MOTION_SHIT
void
cmgoto (struct frame *f, int row, int col)
{
  struct console *c = XCONSOLE (FRAME_CONSOLE (f));
  char *motion;
#if 0
  int frame_x = FRAME_CURSOR_X(f);
  int frame_y = FRAME_CURSOR_Y(f);
  int relcost, directcost, llcost;
  int homecost;
  int use;
  char *dcm;
#endif

  cmputc_console = c;

  /* First the degenerate case */
#if 0
  if (row == frame_y && col == frame_x)
    return;
#endif

  /* #### something is fucked with the non-absolute cases */
  motion = tgoto (TTY_CM (c).abs, col, row);
  tputs (motion, 1, cmputc);
  CONSOLE_TTY_DATA (c)->cursor_x = col;
  CONSOLE_TTY_DATA (c)->cursor_y = row;
  return;

#if 0
  if (frame_y >= 0 && frame_x >= 0)
    {
      /*
       * Pick least-cost motions
       */

      relcost = calccost (f, frame_y, frame_x, row, col, 0);
      use = USEREL;

      homecost = TTY_COST (c).cm_home;
      if (homecost < EXPENSIVE)
	homecost += calccost (f, 0, 0, row, col, 0);

      if (homecost < relcost)
	{
	  relcost = homecost;
	  use = USEHOME;
	}

      llcost = TTY_COST (c).cm_low_left;
      if (llcost < EXPENSIVE)
	llcost += calccost (f, frame_y - 1, 0, row, col, 0);

      if (llcost < relcost)
	{
	  relcost = llcost;
	  use = USELL;
	}

#if 0
      if ((crcost = Wcm.cc_cr) < BIG) {
	  if (Wcm.cm_autolf)
	      if (curY + 1 >= Wcm.cm_rows)
		  crcost = BIG;
	      else
		  crcost += calccost (curY + 1, 0, row, col, 0);
	  else
	      crcost += calccost (curY, 0, row, col, 0);
      }
      if (crcost < relcost)
	  relcost = crcost, use = USECR;
#endif

      directcost = TTY_COST (c).cm_abs;
      dcm = TTY_CM (c).abs;

      if (row == frame_y && TTY_COST (c).cm_hor_abs < EXPENSIVE)
	{
	  directcost = TTY_COST (c).cm_hor_abs;
	  dcm = TTY_CM (c).hor_abs;
	}
      else if (col == frame_x && TTY_COST (c).cm_ver_abs < EXPENSIVE)
	{
	  directcost = TTY_COST (c).cm_ver_abs;
	  dcm = TTY_CM (c).ver_abs;
	}
    }
  else
    {
      directcost = 0;
      relcost = 100000;
      dcm = TTY_CM (c).abs;
    }

  /*
   * In the following comparison, the = in <= is because when the costs
   * are the same, it looks nicer (I think) to move directly there.
   */
  if (directcost <= relcost)
    {
      /* compute REAL direct cost */
      cm_cost_counter = 0;
      motion = (dcm == TTY_CM (c).hor_abs
		? tgoto (dcm, row, col)
		: tgoto (dcm, col, row));
      tputs (motion, 1, evalcost);
      if (cm_cost_counter <= relcost)
	{	/* really is cheaper */
	  tputs (motion, 1, cmputc);
	  FRAME_CURSOR_Y (f) = row;
	  FRAME_CURSOR_X (f) = col;
	  return;
	}
    }

  switch (use)
    {
    case USEHOME:
      tputs (TTY_CM (c).home, 1, cmputc);
      FRAME_CURSOR_X (f) = 0;
      FRAME_CURSOR_Y (f) = 0;
      break;

    case USELL:
      tputs (TTY_CM (c).low_left, 1, cmputc);
      FRAME_CURSOR_Y (f) = FRAME_HEIGHT (f) - 1;
      FRAME_CURSOR_X (f) = 0;
      break;

#if 0
    case USECR:
      tputs (Wcm.cm_cr, 1, cmputc);
      if (Wcm.cm_autolf)
	curY++;
      curX = 0;
      break;
#endif
    }

  calccost (f, FRAME_CURSOR_Y (f), FRAME_CURSOR_X (f), row, col, 1);
  FRAME_CURSOR_Y (f) = row;
  FRAME_CURSOR_X (f) = col;
#endif
}
#endif /* OLD_CURSOR_MOTION_SHIT */

/*****************************************************************************
 cmgoto

 This function is responsible for getting the cursor from its current
 location to the passed location in the most efficient manner
 possible.
 ****************************************************************************/
static void
cmgoto_for_real (struct console *c, int row, int col)
{
  char *motion;

  cmputc_console = c;

  /* First make sure that we actually have to do any work at all. */
  if (CONSOLE_TTY_REAL_CURSOR_X (c) == col
      && CONSOLE_TTY_REAL_CURSOR_Y (c) == row)
    return;

  CONSOLE_TTY_REAL_CURSOR_X (c) = col;
  CONSOLE_TTY_REAL_CURSOR_Y (c) = row;

  /* #### Need to reimplement cost analysis and potential relative
     movement. */

  /* If all else fails, use absolute movement. */
  motion = tgoto (TTY_CM (c).abs, col, row);
  tputs (motion, 1, cmputc);
  CONSOLE_TTY_CURSOR_X (c) = col;
  CONSOLE_TTY_CURSOR_Y (c) = row;
}

void
cmgoto (struct frame *f, int row, int col)
{
  /* We delay cursor motion until we do something other than cursor motion,
     to optimize the case where cmgoto() is called twice in a row. */
  struct console *c = XCONSOLE (FRAME_CONSOLE (f));
  CONSOLE_TTY_CURSOR_X (c) = col;
  CONSOLE_TTY_CURSOR_Y (c) = row;
}

#if 0
/* Clear out all terminal info.
   Used before copying into it the info on the actual terminal.
 */

void
Wcm_clear (void)
{
  xzero (Wcm);
  UP = 0;
  BC = 0;
}
#endif

#if 0
/*
 * Initialized stuff
 * Return 0 if can do CM.
 * Return -1 if cannot.
 * Return -2 if size not specified.
 */

int
Wcm_init (void)
{
#if 0
  if (Wcm.cm_abs && !Wcm.cm_ds)
    return 0;
#endif
  if (Wcm.cm_abs)
    return 0;
  /* Require up and left, and, if no absolute, down and right */
  if (!Wcm.cm_up || !Wcm.cm_left)
    return - 1;
  if (!Wcm.cm_abs && (!Wcm.cm_down || !Wcm.cm_right))
    return - 1;
  /* Check that we know the size of the frame.... */
  if (Wcm.cm_rows <= 0 || Wcm.cm_cols <= 0)
    return - 2;
  return 0;
}
#endif