Upgraded to mh-e version 6.1.1.
[bpt/emacs.git] / src / term.c
index 65d0c8c..3add820 100644 (file)
@@ -1,5 +1,5 @@
 /* Terminal control module for terminals described by TERMCAP
-   Copyright (C) 1985, 86, 87, 93, 94, 95, 98, 2000, 2001
+   Copyright (C) 1985, 86, 87, 93, 94, 95, 98, 2000, 2001, 2002
    Free Software Foundation, Inc.
 
 This file is part of GNU Emacs.
@@ -56,7 +56,7 @@ extern int tgetnum P_ ((char *id));
 #ifdef HAVE_X_WINDOWS
 #include "xterm.h"
 #endif
-#ifdef macintosh
+#ifdef MAC_OS
 #include "macterm.h"
 #endif
 
@@ -118,9 +118,6 @@ void (*clear_end_of_line_hook) P_ ((int));
 
 void (*ins_del_lines_hook) P_ ((int, int));
 
-void (*change_line_highlight_hook) P_ ((int, int, int, int));
-void (*reassert_line_highlight_hook) P_ ((int, int));
-
 void (*delete_glyphs_hook) P_ ((int));
 
 void (*ring_bell_hook) P_ ((void));
@@ -325,10 +322,6 @@ char *TS_enter_reverse_mode;
 
 char *TS_exit_underline_mode, *TS_enter_underline_mode;
 
-/* "ug" -- number of blanks left by underline.  */
-
-int TN_magic_cookie_glitch_ul;
-
 /* "as"/"ae" -- start/end alternate character set.  Not really
    supported, yet.  */
 
@@ -364,13 +357,6 @@ int TF_underscore; /* termcap ul flag: _ underlines if over-struck on
 int TF_teleray;                /* termcap xt flag: many weird consequences.
                           For t1061. */
 
-int TF_xs;             /* Nonzero for "xs".  If set together with
-                          TN_standout_width == 0, it means don't bother
-                          to write any end-standout cookies.  */
-
-int TN_standout_width; /* termcap sg number: width occupied by standout
-                          markers */
-
 static int RPov;       /* # chars to start a TS_repeat */
 
 static int delete_in_insert_mode;      /* delete mode == insert mode */
@@ -388,18 +374,7 @@ int max_frame_width;
 
 int max_frame_height;
 
-/* Number of chars of space used for standout marker at beginning of line,
-   or'd with 0100.  Zero if no standout marker at all.
-   The length of these vectors is max_frame_height.
-
-   Used IFF TN_standout_width >= 0. */
-
-static char *chars_wasted;
-static char *copybuf;
-
-/* nonzero means supposed to write text in standout mode.  */
-
-int standout_requested;
+static int costs_set;    /* Nonzero if costs have been calculated. */
 
 int insert_mode;                       /* Nonzero when in insert mode.  */
 int standout_mode;                     /* Nonzero when in standout mode.  */
@@ -489,19 +464,13 @@ reset_terminal_modes ()
 {
   if (FRAME_TERMCAP_P (XFRAME (selected_frame)))
     {
-      if (TN_standout_width < 0)
-       turn_off_highlight ();
+      turn_off_highlight ();
       turn_off_insert ();
       OUTPUT_IF (TS_end_keypad_mode);
       OUTPUT_IF (TS_cursor_normal);
       OUTPUT_IF (TS_end_termcap_modes);
       OUTPUT_IF (TS_orig_pair);
       /* Output raw CR so kernel can track the cursor hpos.  */
-      /* But on magic-cookie terminals this can erase an end-standout
-        marker and cause the rest of the frame to be in standout, so
-        move down first.  */
-      if (TN_standout_width >= 0)
-       cmputc ('\n');
       cmputc ('\r');
     }
   else if (reset_terminal_modes_hook)
@@ -527,7 +496,6 @@ update_end (f)
        tty_show_cursor ();
       turn_off_insert ();
       background_highlight ();
-      standout_requested = 0;
     }
   else
     update_end_hook (f);
@@ -588,33 +556,22 @@ turn_off_insert ()
   insert_mode = 0;
 }
 \f
-/* Handle highlighting when TN_standout_width (termcap sg) is not specified.
-   In these terminals, output is affected by the value of standout
-   mode when the output is written.
-
-   These functions are called on all terminals, but do nothing
-   on terminals whose standout mode does not work that way.  */
+/* Handle highlighting.  */
 
 void
 turn_off_highlight ()
 {
-  if (TN_standout_width < 0)
-    {
-      if (standout_mode)
-       OUTPUT_IF (TS_end_standout_mode);
-      standout_mode = 0;
-    }
+  if (standout_mode)
+    OUTPUT_IF (TS_end_standout_mode);
+  standout_mode = 0;
 }
 
 static void
 turn_on_highlight ()
 {
-  if (TN_standout_width < 0)
-    {
-      if (!standout_mode)
-       OUTPUT_IF (TS_standout_mode);
-      standout_mode = 1;
-    }
+  if (!standout_mode)
+    OUTPUT_IF (TS_standout_mode);
+  standout_mode = 1;
 }
 
 static void
@@ -661,8 +618,6 @@ tty_show_cursor ()
 void
 background_highlight ()
 {
-  if (TN_standout_width >= 0)
-    return;
   if (inverse_video)
     turn_on_highlight ();
   else
@@ -674,99 +629,10 @@ background_highlight ()
 static void
 highlight_if_desired ()
 {
-  if (TN_standout_width >= 0)
-    return;
-  if (!inverse_video == !standout_requested)
-    turn_off_highlight ();
-  else
+  if (inverse_video)
     turn_on_highlight ();
-}
-\f
-/* Handle standout mode for terminals in which TN_standout_width >= 0.
-   On these terminals, standout is controlled by markers that
-   live inside the terminal's memory.  TN_standout_width is the width
-   that the marker occupies in memory.  Standout runs from the marker
-   to the end of the line on some terminals, or to the next
-   turn-off-standout marker (TS_end_standout_mode) string
-   on other terminals.  */
-
-/* Write a standout marker or end-standout marker at the front of the line
-   at vertical position vpos.  */
-
-static void
-write_standout_marker (flag, vpos)
-     int flag, vpos;
-{
-  if (flag
-      || (TS_end_standout_mode && !TF_teleray && !se_is_so
-         && !(TF_xs && TN_standout_width == 0)))
-    {
-      cmgoto (vpos, 0);
-      cmplus (TN_standout_width);
-      OUTPUT (flag ? TS_standout_mode : TS_end_standout_mode);
-      chars_wasted[curY] = TN_standout_width | 0100;
-    }
-}
-\f
-/* External interface to control of standout mode.
-   Call this when about to modify line at position VPOS
-   and not change whether it is highlighted.  */
-
-void
-reassert_line_highlight (highlight, vpos)
-     int highlight;
-     int vpos;
-{
-  struct frame *f = updating_frame ? updating_frame : XFRAME (selected_frame);
-  if (! FRAME_TERMCAP_P (f))
-    {
-      (*reassert_line_highlight_hook) (highlight, vpos);
-      return;
-    }
-  if (TN_standout_width < 0)
-    /* Handle terminals where standout takes affect at output time */
-    standout_requested = highlight;
-  else if (chars_wasted && chars_wasted[vpos] == 0)
-    /* For terminals with standout markers, write one on this line
-       if there isn't one already.  */
-    write_standout_marker (inverse_video ? !highlight : highlight, vpos);
-}
-
-/* Call this when about to modify line at position VPOS
-   and change whether it is highlighted.  */
-
-void
-change_line_highlight (new_highlight, vpos, y, first_unused_hpos)
-     int new_highlight, vpos, y, first_unused_hpos;
-{
-  standout_requested = new_highlight;
-  if (! FRAME_TERMCAP_P (updating_frame))
-    {
-      (*change_line_highlight_hook) (new_highlight, vpos, y, first_unused_hpos);
-      return;
-    }
-
-  cursor_to (vpos, 0);
-
-  if (TN_standout_width < 0)
-    background_highlight ();
-  /* If line starts with a marker, delete the marker */
-  else if (TS_clr_line && chars_wasted[curY])
-    {
-      turn_off_insert ();
-      /* On Teleray, make sure to erase the SO marker.  */
-      if (TF_teleray)
-       {
-         cmgoto (curY - 1, FRAME_WIDTH (XFRAME (selected_frame)) - 4);
-         OUTPUT ("\033S");
-         curY++;               /* ESC S moves to next line where the TS_standout_mode was */
-         curX = 0;
-       }
-      else
-       cmgoto (curY, 0);       /* reposition to kill standout marker */
-    }
-  clear_end_of_line_raw (first_unused_hpos);
-  reassert_line_highlight (new_highlight, curY);
+  else
+    turn_off_highlight ();
 }
 \f
 
@@ -787,10 +653,9 @@ cursor_to (vpos, hpos)
 
   /* Detect the case where we are called from reset_sys_modes
      and the costs have never been calculated.  Do nothing.  */
-  if (chars_wasted == 0)
+  if (! costs_set)
     return;
 
-  hpos += chars_wasted[vpos] & 077;
   if (curY == vpos && curX == hpos)
     return;
   if (!TF_standout_motion)
@@ -838,15 +703,13 @@ clear_to_end ()
     {
       background_highlight ();
       OUTPUT (TS_clr_to_bottom);
-      bzero (chars_wasted + curY,
-            FRAME_HEIGHT (XFRAME (selected_frame)) - curY);
     }
   else
     {
       for (i = curY; i < FRAME_HEIGHT (XFRAME (selected_frame)); i++)
        {
          cursor_to (i, 0);
-         clear_end_of_line_raw (FRAME_WIDTH (XFRAME (selected_frame)));
+         clear_end_of_line (FRAME_WIDTH (XFRAME (selected_frame)));
        }
     }
 }
@@ -868,7 +731,6 @@ clear_frame ()
     {
       background_highlight ();
       OUTPUT (TS_clr_frame);
-      bzero (chars_wasted, FRAME_HEIGHT (sf));
       cmat (0, 0);
     }
   else
@@ -878,32 +740,13 @@ clear_frame ()
     }
 }
 
-/* Clear to end of line, but do not clear any standout marker.
-   Assumes that the cursor is positioned at a character of real text,
-   which implies it cannot be before a standout marker
-   unless the marker has zero width.
-
-   Note that the cursor may be moved.  */
-
-void
-clear_end_of_line (first_unused_hpos)
-     int first_unused_hpos;
-{
-  if (FRAME_TERMCAP_P (XFRAME (selected_frame))
-      && chars_wasted != 0
-      && TN_standout_width == 0 && curX == 0 && chars_wasted[curY] != 0)
-    write_glyphs (&space_glyph, 1);
-  clear_end_of_line_raw (first_unused_hpos);
-}
-
 /* Clear from cursor to end of line.
    Assume that the line is already clear starting at column first_unused_hpos.
-   If the cursor is at a standout marker, erase the marker.
 
    Note that the cursor may be moved, on terminals lacking a `ce' string.  */
 
 void
-clear_end_of_line_raw (first_unused_hpos)
+clear_end_of_line (first_unused_hpos)
      int first_unused_hpos;
 {
   register int i;
@@ -919,15 +762,11 @@ clear_end_of_line_raw (first_unused_hpos)
 
   /* Detect the case where we are called from reset_sys_modes
      and the costs have never been calculated.  Do nothing.  */
-  if (chars_wasted == 0)
+  if (! costs_set)
     return;
 
-  first_unused_hpos += chars_wasted[curY] & 077;
   if (curX >= first_unused_hpos)
     return;
-  /* Notice if we are erasing a magic cookie */
-  if (curX == 0)
-    chars_wasted[curY] = 0;
   background_highlight ();
   if (TS_clr_line)
     {
@@ -971,7 +810,8 @@ encode_terminal_code (src, dst, src_len, dst_len, consumed)
   struct glyph *src_start = src, *src_end = src + src_len;
   unsigned char *dst_start = dst, *dst_end = dst + dst_len;
   register GLYPH g;
-  unsigned char workbuf[MAX_MULTIBYTE_LENGTH], *buf;
+  unsigned char workbuf[MAX_MULTIBYTE_LENGTH];
+  const unsigned char *buf;
   int len;
   register int tlen = GLYPH_TABLE_LENGTH;
   register Lisp_Object *tbase = GLYPH_TABLE_BASE;
@@ -1087,7 +927,7 @@ write_glyphs (string, len)
 
   if (AutoWrap
       && curY + 1 == FRAME_HEIGHT (sf)
-      && (curX + len - (chars_wasted[curY] & 077) == FRAME_WIDTH (sf)))
+      && (curX + len) == FRAME_WIDTH (sf))
     len --;
   if (len <= 0)
     return;
@@ -1351,27 +1191,6 @@ ins_del_lines (vpos, n)
       set_scroll_region (0, specified_window);
     }
 
-  if (TN_standout_width >= 0)
-    {
-      register int lower_limit
-       = (scroll_region_ok
-          ? specified_window
-          : FRAME_HEIGHT (sf));
-
-      if (n < 0)
-       {
-         bcopy (&chars_wasted[vpos - n], &chars_wasted[vpos],
-                lower_limit - vpos + n);
-         bzero (&chars_wasted[lower_limit + n], - n);
-       }
-      else
-       {
-         bcopy (&chars_wasted[vpos], &copybuf[vpos], lower_limit - vpos - n);
-         bcopy (&copybuf[vpos], &chars_wasted[vpos + n],
-                lower_limit - vpos - n);
-         bzero (&chars_wasted[vpos], n);
-       }
-    }
   if (!scroll_region_ok && memory_below_frame && n < 0)
     {
       cursor_to (FRAME_HEIGHT (sf) + n, 0);
@@ -1509,23 +1328,12 @@ calculate_costs (frame)
      once for the terminal frame of X-windows emacs, but not used afterwards.
 
      char_ins_del_vector (i.e., char_ins_del_cost) isn't used because
-     X turns off char_ins_del_ok.
-
-     chars_wasted and copybuf are only used here in term.c in cases where
-     the term hook isn't called. */
+     X turns off char_ins_del_ok. */
 
   max_frame_height = max (max_frame_height, FRAME_HEIGHT (frame));
   max_frame_width = max (max_frame_width, FRAME_WIDTH (frame));
 
-  if (chars_wasted != 0)
-    chars_wasted = (char *) xrealloc (chars_wasted, max_frame_height);
-  else
-    chars_wasted = (char *) xmalloc (max_frame_height);
-
-  if (copybuf != 0)
-    copybuf = (char *) xrealloc (copybuf, max_frame_height);
-  else
-    copybuf = (char *) xmalloc (max_frame_height);
+  costs_set = 1;
 
   if (char_ins_del_vector != 0)
     char_ins_del_vector
@@ -1537,8 +1345,6 @@ calculate_costs (frame)
       = (int *) xmalloc (sizeof (int)
                         + 2 * max_frame_width * sizeof (int));
 
-  bzero (chars_wasted, max_frame_height);
-  bzero (copybuf, max_frame_height);
   bzero (char_ins_del_vector, (sizeof (int)
                               + 2 * max_frame_width * sizeof (int)));
 
@@ -1577,79 +1383,79 @@ struct fkey_table {
 
 static struct fkey_table keys[] =
 {
-  "kh", "home",                /* termcap */
-  "kl", "left",                /* termcap */
-  "ku", "up",          /* termcap */
-  "kr", "right",       /* termcap */
-  "kd", "down",                /* termcap */
-  "%8", "prior",       /* terminfo */
-  "%5", "next",                /* terminfo */
-  "@7",        "end",          /* terminfo */
-  "@1", "begin",       /* terminfo */
-  "*6", "select",      /* terminfo */
-  "%9", "print",       /* terminfo */
-  "@4", "execute",     /* terminfo --- actually the `command' key */
+  {"kh", "home"},      /* termcap */
+  {"kl", "left"},      /* termcap */
+  {"ku", "up"},                /* termcap */
+  {"kr", "right"},     /* termcap */
+  {"kd", "down"},      /* termcap */
+  {"%8", "prior"},     /* terminfo */
+  {"%5", "next"},      /* terminfo */
+  {"@7", "end"},       /* terminfo */
+  {"@1", "begin"},     /* terminfo */
+  {"*6", "select"},    /* terminfo */
+  {"%9", "print"},     /* terminfo */
+  {"@4", "execute"},   /* terminfo --- actually the `command' key */
   /*
    * "insert" --- see below
    */
-  "&8",        "undo",         /* terminfo */
-  "%0",        "redo",         /* terminfo */
-  "%7",        "menu",         /* terminfo --- actually the `options' key */
-  "@0",        "find",         /* terminfo */
-  "@2",        "cancel",       /* terminfo */
-  "%1", "help",                /* terminfo */
+  {"&8", "undo"},      /* terminfo */
+  {"%0", "redo"},      /* terminfo */
+  {"%7", "menu"},      /* terminfo --- actually the `options' key */
+  {"@0", "find"},      /* terminfo */
+  {"@2", "cancel"},    /* terminfo */
+  {"%1", "help"},      /* terminfo */
   /*
    * "break" goes here, but can't be reliably intercepted with termcap
    */
-  "&4", "reset",       /* terminfo --- actually `restart' */
+  {"&4", "reset"},     /* terminfo --- actually `restart' */
   /*
    * "system" and "user" --- no termcaps
    */
-  "kE", "clearline",   /* terminfo */
-  "kA", "insertline",  /* terminfo */
-  "kL", "deleteline",  /* terminfo */
-  "kI", "insertchar",  /* terminfo */
-  "kD", "deletechar",  /* terminfo */
-  "kB", "backtab",     /* terminfo */
+  {"kE", "clearline"}, /* terminfo */
+  {"kA", "insertline"},        /* terminfo */
+  {"kL", "deleteline"},        /* terminfo */
+  {"kI", "insertchar"},        /* terminfo */
+  {"kD", "deletechar"},        /* terminfo */
+  {"kB", "backtab"},   /* terminfo */
   /*
    * "kp_backtab", "kp-space", "kp-tab" --- no termcaps
    */
-  "@8", "kp-enter",    /* terminfo */
+  {"@8", "kp-enter"},  /* terminfo */
   /*
    * "kp-f1", "kp-f2", "kp-f3" "kp-f4",
    * "kp-multiply", "kp-add", "kp-separator",
    * "kp-subtract", "kp-decimal", "kp-divide", "kp-0";
    * --- no termcaps for any of these.
    */
-  "K4", "kp-1",                /* terminfo */
+  {"K4", "kp-1"},      /* terminfo */
   /*
    * "kp-2" --- no termcap
    */
-  "K5", "kp-3",                /* terminfo */
+  {"K5", "kp-3"},      /* terminfo */
   /*
    * "kp-4" --- no termcap
    */
-  "K2", "kp-5",                /* terminfo */
+  {"K2", "kp-5"},      /* terminfo */
   /*
    * "kp-6" --- no termcap
    */
-  "K1", "kp-7",                /* terminfo */
+  {"K1", "kp-7"},      /* terminfo */
   /*
    * "kp-8" --- no termcap
    */
-  "K3", "kp-9",                /* terminfo */
+  {"K3", "kp-9"},      /* terminfo */
   /*
    * "kp-equal" --- no termcap
    */
-  "k1",        "f1",
-  "k2",        "f2",
-  "k3",        "f3",
-  "k4",        "f4",
-  "k5",        "f5",
-  "k6",        "f6",
-  "k7",        "f7",
-  "k8",        "f8",
-  "k9",        "f9",
+  {"k1", "f1"},
+  {"k2", "f2"},
+  {"k3", "f3"},
+  {"k4", "f4"},
+  {"k5", "f5"},
+  {"k6", "f6"},
+  {"k7", "f7"},
+  {"k8", "f8"},
+  {"k9", "f9"}
   };
 
 static char **term_get_fkeys_arg;
@@ -1708,12 +1514,15 @@ term_get_fkeys_1 ()
 
     if (k_semi)
       {
+       if (k0)
+         /* Define f0 first, so that f10 takes precedence in case the
+            key sequences happens to be the same.  */
+         Fdefine_key (Vfunction_key_map, build_string (k0),
+                      Fmake_vector (make_number (1), intern ("f0")));
        Fdefine_key (Vfunction_key_map, build_string (k_semi),
                     Fmake_vector (make_number (1), intern ("f10")));
-       k0_name = "f0";
       }
-
-    if (k0)
+    else if (k0)
       Fdefine_key (Vfunction_key_map, build_string (k0),
                   Fmake_vector (make_number (1), intern (k0_name)));
   }
@@ -1829,11 +1638,23 @@ append_glyph (it)
 }
 
 
-/* Produce glyphs for the display element described by IT.  The
-   function fills output fields of IT with pixel information like the
-   pixel width and height of a character, and maybe produces glyphs at
+/* Produce glyphs for the display element described by IT.  *IT
+   specifies what we want to produce a glyph for (character, image, ...),
+   and where in the glyph matrix we currently are (glyph row and hpos).
+   produce_glyphs fills in output fields of *IT with information such as the
+   pixel width and height of a character, and maybe output actual glyphs at
    the same time if IT->glyph_row is non-null.  See the explanation of
-   struct display_iterator in dispextern.h for an overview.  */
+   struct display_iterator in dispextern.h for an overview.
+
+   produce_glyphs also stores the result of glyph width, ascent
+   etc. computations in *IT.
+
+   IT->glyph_row may be null, in which case produce_glyphs does not
+   actually fill in the glyphs.  This is used in the move_* functions
+   in xdisp.c for text width and height computations.
+
+   Callers usually don't call produce_glyphs directly;
+   instead they use the macro PRODUCE_GLYPHS.  */
 
 void 
 produce_glyphs (it)
@@ -2091,10 +1912,7 @@ turn_on_face (f, face_id)
       && MAY_USE_WITH_COLORS_P (NC_BLINK))
     OUTPUT1_IF (TS_enter_blink_mode);
 
-  if (face->tty_underline_p
-      /* Don't underline if that's difficult.  */
-      && TN_magic_cookie_glitch_ul <= 0
-      && MAY_USE_WITH_COLORS_P (NC_UNDERLINE))
+  if (face->tty_underline_p && MAY_USE_WITH_COLORS_P (NC_UNDERLINE))
     OUTPUT1_IF (TS_enter_underline_mode);
 
   if (TN_max_colors > 0)
@@ -2156,9 +1974,7 @@ turn_off_face (f, face_id)
       if (face->tty_alt_charset_p)
        OUTPUT_IF (TS_exit_alt_charset_mode);
 
-      if (face->tty_underline_p
-         /* We don't underline if that's difficult.  */
-         && TN_magic_cookie_glitch_ul <= 0)
+      if (face->tty_underline_p)
        OUTPUT_IF (TS_exit_underline_mode);
     }
 
@@ -2172,18 +1988,185 @@ turn_off_face (f, face_id)
 }
   
     
+/* Return non-zero if the terminal on frame F supports all of the
+   capabilities in CAPS simultaneously, with foreground and background
+   colors FG and BG.  */
+
+int
+tty_capable_p (f, caps, fg, bg)
+     struct frame *f;
+     unsigned caps;
+     unsigned long fg, bg;
+{
+#define TTY_CAPABLE_P_TRY(cap, TS, NC_bit)                             \
+  if ((caps & (cap)) && (!(TS) || !MAY_USE_WITH_COLORS_P(NC_bit)))     \
+    return 0;
+
+  TTY_CAPABLE_P_TRY (TTY_CAP_INVERSE,  TS_standout_mode,        NC_REVERSE);
+  TTY_CAPABLE_P_TRY (TTY_CAP_UNDERLINE, TS_enter_underline_mode, NC_UNDERLINE);
+  TTY_CAPABLE_P_TRY (TTY_CAP_BOLD,     TS_enter_bold_mode,      NC_BOLD);
+  TTY_CAPABLE_P_TRY (TTY_CAP_DIM,      TS_enter_dim_mode,       NC_DIM);
+  TTY_CAPABLE_P_TRY (TTY_CAP_BLINK,    TS_enter_blink_mode,     NC_BLINK);
+  TTY_CAPABLE_P_TRY (TTY_CAP_ALT_CHARSET, TS_enter_alt_charset_mode, NC_ALT_CHARSET);
+
+  /* We can do it!  */
+  return 1;
+}
+
+
 /* Return non-zero if the terminal is capable to display colors.  */
 
 DEFUN ("tty-display-color-p", Ftty_display_color_p, Stty_display_color_p,
        0, 1, 0,
-       doc: /* Return non-nil if TTY can display colors on FRAME.  */)
-     (frame)
-     Lisp_Object frame;
+       doc: /* Return non-nil if TTY can display colors on DISPLAY.  */)
+     (display)
+     Lisp_Object display;
 {
   return TN_max_colors > 0 ? Qt : Qnil;
 }
 
+/* Return the number of supported colors.  */
+DEFUN ("tty-display-color-cells", Ftty_display_color_cells,
+       Stty_display_color_cells, 0, 1, 0,
+       doc: /* Return the number of colors supported by TTY on DISPLAY.  */)
+     (display)
+     Lisp_Object display;
+{
+  return make_number (TN_max_colors);
+}
+
+#ifndef WINDOWSNT
+
+/* Save or restore the default color-related capabilities of this
+   terminal.  */
+static void
+tty_default_color_capabilities (save)
+     int save;
+{
+  static char
+    *default_orig_pair, *default_set_foreground, *default_set_background;
+  static int default_max_colors, default_max_pairs, default_no_color_video;
+
+  if (save)
+    {
+      if (default_orig_pair)
+       xfree (default_orig_pair);
+      default_orig_pair = TS_orig_pair ? xstrdup (TS_orig_pair) : NULL;
+
+      if (default_set_foreground)
+       xfree (default_set_foreground);
+      default_set_foreground = TS_set_foreground ? xstrdup (TS_set_foreground)
+                              : NULL;
+
+      if (default_set_background)
+       xfree (default_set_background);
+      default_set_background = TS_set_background ? xstrdup (TS_set_background)
+                              : NULL;
+
+      default_max_colors = TN_max_colors;
+      default_max_pairs = TN_max_pairs;
+      default_no_color_video = TN_no_color_video;
+    }
+  else
+    {
+      TS_orig_pair = default_orig_pair;
+      TS_set_foreground = default_set_foreground;
+      TS_set_background = default_set_background;
+      TN_max_colors = default_max_colors;
+      TN_max_pairs = default_max_pairs;
+      TN_no_color_video = default_no_color_video;
+    }
+}
+
+/* Setup one of the standard tty color schemes according to MODE.
+   MODE's value is generally the number of colors which we want to
+   support; zero means set up for the default capabilities, the ones
+   we saw at term_init time; -1 means turn off color support.  */
+void
+tty_setup_colors (mode)
+     int mode;
+{
+  switch (mode)
+    {
+      case -1:  /* no colors at all */
+       TN_max_colors = 0;
+       TN_max_pairs = 0;
+       TN_no_color_video = 0;
+       TS_set_foreground = TS_set_background = TS_orig_pair = NULL;
+       break;
+      case 0:   /* default colors, if any */
+      default:
+       tty_default_color_capabilities (0);
+       break;
+      case 8:  /* 8 standard ANSI colors */
+       TS_orig_pair = "\033[0m";
+#ifdef TERMINFO
+       TS_set_foreground = "\033[3%p1%dm";
+       TS_set_background = "\033[4%p1%dm";
+#else
+       TS_set_foreground = "\033[3%dm";
+       TS_set_background = "\033[4%dm";
+#endif
+       TN_max_colors = 8;
+       TN_max_pairs = 64;
+       TN_no_color_video = 0;
+       break;
+    }
+}
+
+void
+set_tty_color_mode (f, val)
+     struct frame *f;
+     Lisp_Object val;
+{
+  Lisp_Object color_mode_spec, current_mode_spec;
+  Lisp_Object color_mode, current_mode;
+  int mode, old_mode;
+  extern Lisp_Object Qtty_color_mode;
+  Lisp_Object tty_color_mode_alist;
+
+  tty_color_mode_alist = Fintern_soft (build_string ("tty-color-mode-alist"),
+                                      Qnil);
+
+  if (NATNUMP (val))
+    color_mode = val;
+  else
+    {
+      if (NILP (tty_color_mode_alist))
+       color_mode_spec = Qnil;
+      else
+       color_mode_spec = Fassq (val, XSYMBOL (tty_color_mode_alist)->value);
+      current_mode_spec = assq_no_quit (Qtty_color_mode, f->param_alist);
+
+      if (CONSP (color_mode_spec))
+       color_mode = XCDR (color_mode_spec);
+      else
+       color_mode = Qnil;
+    }
+  if (CONSP (current_mode_spec))
+    current_mode = XCDR (current_mode_spec);
+  else
+    current_mode = Qnil;
+  if (NATNUMP (color_mode))
+    mode = XINT (color_mode);
+  else
+    mode = 0;  /* meaning default */
+  if (NATNUMP (current_mode))
+    old_mode = XINT (current_mode);
+  else
+    old_mode = 0;
+
+  if (mode != old_mode)
+    {
+      tty_setup_colors (mode);
+      /*  This recomputes all the faces given the new color
+         definitions.  */
+      call0 (intern ("tty-set-up-initial-frame-faces"));
+      redraw_frame (f);
+    }
+}
 
+#endif /* !WINDOWSNT */
 
 \f
 /***********************************************************************
@@ -2336,7 +2319,6 @@ to do `unset TERMCAP' (C-shell: `unsetenv TERMCAP') as well.",
   
   TS_enter_underline_mode = tgetstr ("us", address);
   TS_exit_underline_mode = tgetstr ("ue", address);
-  TN_magic_cookie_glitch_ul = tgetnum ("ug");
   TS_enter_bold_mode = tgetstr ("md", address);
   TS_enter_dim_mode = tgetstr ("mh", address);
   TS_enter_blink_mode = tgetstr ("mb", address);
@@ -2373,6 +2355,8 @@ to do `unset TERMCAP' (C-shell: `unsetenv TERMCAP') as well.",
        TN_no_color_video = 0;
     }
 
+  tty_default_color_capabilities (1);
+
   MagicWrap = tgetflag ("xn");
   /* Since we make MagicWrap terminals look like AutoWrap, we need to have
      the former flag imply the latter.  */
@@ -2384,7 +2368,6 @@ to do `unset TERMCAP' (C-shell: `unsetenv TERMCAP') as well.",
   TF_insmode_motion = tgetflag ("mi");
   TF_standout_motion = tgetflag ("ms");
   TF_underscore = tgetflag ("ul");
-  TF_xs = tgetflag ("xs");
   TF_teleray = tgetflag ("xt");
 
   term_get_fkeys (address);
@@ -2410,7 +2393,6 @@ to do `unset TERMCAP' (C-shell: `unsetenv TERMCAP') as well.",
           FRAME_HEIGHT (sf), FRAME_WIDTH (sf));
 
   min_padding_speed = tgetnum ("pb");
-  TN_standout_width = tgetnum ("sg");
   TabWidth = tgetnum ("tw");
 
 #ifdef VMS
@@ -2439,11 +2421,24 @@ to do `unset TERMCAP' (C-shell: `unsetenv TERMCAP') as well.",
     Wcm.cm_tab = "\t";
 */
 
+  /* We don't support standout modes that use `magic cookies', so
+     turn off any that do.  */
+  if (TS_standout_mode && tgetnum ("sg") >= 0)
+    {
+      TS_standout_mode = 0;
+      TS_end_standout_mode = 0;
+    }
+  if (TS_enter_underline_mode && tgetnum ("ug") >= 0)
+    {
+      TS_enter_underline_mode = 0;
+      TS_exit_underline_mode = 0;
+    }
+
+  /* If there's no standout mode, try to use underlining instead.  */
   if (TS_standout_mode == 0)
     {
-      TN_standout_width = tgetnum ("ug");
-      TS_end_standout_mode = tgetstr ("ue", address);
-      TS_standout_mode = tgetstr ("us", address);
+      TS_standout_mode = TS_enter_underline_mode;
+      TS_end_standout_mode = TS_exit_underline_mode;
     }
 
   /* If no `se' string, try using a `me' string instead.
@@ -2460,10 +2455,8 @@ to do `unset TERMCAP' (C-shell: `unsetenv TERMCAP') as well.",
   if (TF_teleray)
     {
       Wcm.cm_tab = 0;
-      /* Teleray: most programs want a space in front of TS_standout_mode,
-          but Emacs can do without it (and give one extra column).  */
-      TS_standout_mode = "\033RD";
-      TN_standout_width = 1;
+      /* We can't support standout mode, because it uses magic cookies.  */
+      TS_standout_mode = 0;
       /* But that means we cannot rely on ^M to go to column zero! */
       CR = 0;
       /* LF can't be trusted either -- can alter hpos */
@@ -2563,10 +2556,6 @@ to do `unset TERMCAP' (C-shell: `unsetenv TERMCAP') as well.",
              && TS_end_standout_mode
              && !strcmp (TS_standout_mode, TS_end_standout_mode));
 
-  /* Remove width of standout marker from usable width of line */
-  if (TN_standout_width > 0)
-    SET_FRAME_WIDTH (sf, FRAME_WIDTH (sf) - TN_standout_width);
-
   UseTabs = tabs_safe_p () && TabWidth == 8;
 
   scroll_region_ok
@@ -2623,5 +2612,6 @@ The function should accept no arguments.  */);
   Vring_bell_function = Qnil;
 
   defsubr (&Stty_display_color_p);
+  defsubr (&Stty_display_color_cells);
 }