Upgraded to mh-e version 6.1.1.
[bpt/emacs.git] / src / term.c
index f157bbf..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
 
@@ -374,7 +374,7 @@ int max_frame_width;
 
 int max_frame_height;
 
-int costs_set = 0;             /* Nonzero if costs have been calculated. */
+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.  */
@@ -810,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;
@@ -1382,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;
@@ -1513,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)));
   }
@@ -1634,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)
@@ -1972,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
 /***********************************************************************
@@ -2172,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.  */
@@ -2427,5 +2612,6 @@ The function should accept no arguments.  */);
   Vring_bell_function = Qnil;
 
   defsubr (&Stty_display_color_p);
+  defsubr (&Stty_display_color_cells);
 }