]> code.delx.au - gnu-emacs/blobdiff - src/term.c
* macterm.c (mac_check_for_quit_char): Don't check more often than
[gnu-emacs] / src / term.c
index 6326d4b680a00754060a2f1e70770c13a5cf8181..68f522c506cbaae665ff20db31a8f440fbf68fda 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
 
@@ -76,7 +76,7 @@ static void tty_hide_cursor P_ ((void));
          tputs (a, (int) (FRAME_HEIGHT (XFRAME (selected_frame))       \
                          - curY), cmputc);                             \
      } while (0)
-     
+
 #define OUTPUT1_IF(a) do { if (a) tputs (a, 1, cmputc); } while (0)
 
 /* Function to use to ring the bell.  */
@@ -203,7 +203,7 @@ void (*set_vertical_scroll_bar_hook)
 
 /* Arrange for all scroll bars on FRAME to be removed at the next call
    to `*judge_scroll_bars_hook'.  A scroll bar may be spared if
-   `*redeem_scroll_bar_hook' is applied to its window before the judgment. 
+   `*redeem_scroll_bar_hook' is applied to its window before the judgment.
 
    This should be applied to each frame each time its window tree is
    redisplayed, even if it is not displaying scroll bars at the moment;
@@ -222,7 +222,7 @@ void (*condemn_scroll_bars_hook) P_ ((FRAME_PTR frame));
 void (*redeem_scroll_bar_hook) P_ ((struct window *window));
 
 /* Remove all scroll bars on FRAME that haven't been saved since the
-   last call to `*condemn_scroll_bars_hook'.  
+   last call to `*condemn_scroll_bars_hook'.
 
    This should be applied to each frame after each time its window
    tree is redisplayed, even if it is not displaying scroll bars at the
@@ -235,11 +235,6 @@ void (*redeem_scroll_bar_hook) P_ ((struct window *window));
 
 void (*judge_scroll_bars_hook) P_ ((FRAME_PTR FRAME));
 
-/* Hook to call in estimate_mode_line_height, if any.  */
-
-int (* estimate_mode_line_height_hook) P_ ((struct frame *f, enum face_id));
-
-
 /* Strings, numbers and flags taken from the termcap entry.  */
 
 char *TS_ins_line;             /* "al" */
@@ -374,7 +369,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.  */
@@ -431,7 +426,7 @@ ring_bell ()
         We don't specbind it, because that would carefully
         restore the bad value if there's an error
         and make the loop of errors happen anyway.  */
-      
+
       function = Vring_bell_function;
       Vring_bell_function = Qnil;
 
@@ -499,7 +494,7 @@ update_end (f)
     }
   else
     update_end_hook (f);
-  
+
   updating_frame = NULL;
 }
 
@@ -523,7 +518,7 @@ set_scroll_region (start, stop)
 {
   char *buf;
   struct frame *sf = XFRAME (selected_frame);
-  
+
   if (TS_set_scroll_region)
     buf = tparam (TS_set_scroll_region, 0, 0, start, stop - 1);
   else if (TS_set_scroll_region_1)
@@ -533,7 +528,7 @@ set_scroll_region (start, stop)
                  FRAME_HEIGHT (sf));
   else
     buf = tparam (TS_set_window, 0, 0, start, 0, stop, FRAME_WIDTH (sf));
-  
+
   OUTPUT (buf);
   xfree (buf);
   losecursor ();
@@ -644,7 +639,7 @@ cursor_to (vpos, hpos)
      int vpos, hpos;
 {
   struct frame *f = updating_frame ? updating_frame : XFRAME (selected_frame);
-  
+
   if (! FRAME_TERMCAP_P (f) && cursor_to_hook)
     {
       (*cursor_to_hook) (vpos, hpos);
@@ -720,7 +715,7 @@ void
 clear_frame ()
 {
   struct frame *sf = XFRAME (selected_frame);
-  
+
   if (clear_frame_hook
       && ! FRAME_TERMCAP_P ((updating_frame ? updating_frame : sf)))
     {
@@ -810,7 +805,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;
@@ -871,7 +867,7 @@ encode_terminal_code (src, dst, src_len, dst_len, consumed)
                  coding->src_multibyte = STRING_MULTIBYTE (tbase[g]);
                }
            }
-         
+
          result = encode_coding (coding, buf, dst, len, dst_end - dst);
          len -= coding->consumed;
          dst += coding->produced;
@@ -894,7 +890,7 @@ encode_terminal_code (src, dst, src_len, dst_len, consumed)
        }
       src++;
     }
-  
+
   *consumed = src - src_start;
   return (dst - dst_start);
 }
@@ -932,17 +928,17 @@ write_glyphs (string, len)
     return;
 
   cmplus (len);
-  
+
   /* The mode bit CODING_MODE_LAST_BLOCK should be set to 1 only at
      the tail.  */
   terminal_coding.mode &= ~CODING_MODE_LAST_BLOCK;
-  
+
   while (len > 0)
     {
       /* Identify a run of glyphs with the same face.  */
       int face_id = string->face_id;
       int n;
-      
+
       for (n = 1; n < len; ++n)
        if (string[n].face_id != face_id)
          break;
@@ -976,7 +972,7 @@ write_glyphs (string, len)
       turn_off_face (f, face_id);
       turn_off_highlight ();
     }
-  
+
   /* We may have to output some codes to terminate the writing.  */
   if (CODING_REQUIRE_FLUSHING (&terminal_coding))
     {
@@ -993,12 +989,12 @@ write_glyphs (string, len)
                    termscript);
        }
     }
-  
+
   cmcheckmagic ();
 }
 
 /* If start is zero, insert blanks instead of a string at start */
+
 void
 insert_glyphs (start, len)
      register struct glyph *start;
@@ -1086,7 +1082,7 @@ insert_glyphs (start, len)
          turn_off_highlight ();
        }
     }
-  
+
   cmcheckmagic ();
 }
 
@@ -1147,7 +1143,7 @@ ins_del_lines (vpos, n)
     }
 
   sf = XFRAME (selected_frame);
-  
+
   /* If the lines below the insertion are being pushed
      into the end of the window, this is the same as clearing;
      and we know the lines are already clear, since the matching
@@ -1461,7 +1457,7 @@ static char **term_get_fkeys_arg;
 static Lisp_Object term_get_fkeys_1 ();
 
 /* Find the escape codes sent by the function keys for Vfunction_key_map.
-   This function scans the termcap function key sequence entries, and 
+   This function scans the termcap function key sequence entries, and
    adds entries to Vfunction_key_map for each function key it finds.  */
 
 void
@@ -1513,12 +1509,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)));
   }
@@ -1563,7 +1562,7 @@ term_get_fkeys_1 ()
                         Fmake_vector (make_number (1), \
                                       intern (sym)));  \
        }
-         
+
       /* if there's no key_next keycap, map key_npage to `next' keysym */
       CONDITIONAL_REASSIGN ("%5", "kN", "next");
       /* if there's no key_prev keycap, map key_ppage to `previous' keysym */
@@ -1603,7 +1602,7 @@ static void append_glyph P_ ((struct it *));
    for which to produce glyphs; IT->face_id contains the character's
    face.  Padding glyphs are appended if IT->c has a IT->pixel_width >
    1.  */
-   
+
 static void
 append_glyph (it)
      struct it *it;
@@ -1616,8 +1615,8 @@ append_glyph (it)
           + it->glyph_row->used[it->area]);
   end = it->glyph_row->glyphs[1 + it->area];
 
-  for (i = 0; 
-       i < it->pixel_width && glyph < end; 
+  for (i = 0;
+       i < it->pixel_width && glyph < end;
        ++i)
     {
       glyph->type = CHAR_GLYPH;
@@ -1627,20 +1626,32 @@ append_glyph (it)
       glyph->padding_p = i > 0;
       glyph->charpos = CHARPOS (it->position);
       glyph->object = it->object;
-      
+
       ++it->glyph_row->used[it->area];
       ++glyph;
     }
 }
 
 
-/* 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.
 
-void 
+   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)
      struct it *it;
 {
@@ -1649,7 +1660,7 @@ produce_glyphs (it)
           || it->what == IT_COMPOSITION
           || it->what == IT_IMAGE
           || it->what == IT_STRETCH);
-  
+
   /* Nothing but characters are supported on terminal frames.  For a
      composition sequence, it->c is the first character of the
      sequence.  */
@@ -1668,8 +1679,8 @@ produce_glyphs (it)
     {
       int absolute_x = (it->current_x
                        + it->continuation_lines_width);
-      int next_tab_x 
-       = (((1 + absolute_x + it->tab_width - 1) 
+      int next_tab_x
+       = (((1 + absolute_x + it->tab_width - 1)
            / it->tab_width)
           * it->tab_width);
       int nspaces;
@@ -1680,17 +1691,17 @@ produce_glyphs (it)
         continued line.  So, we will get the right number of spaces
         here.  */
       nspaces = next_tab_x - absolute_x;
-      
+
       if (it->glyph_row)
        {
          int n = nspaces;
-         
+
          it->c = ' ';
          it->pixel_width = it->len = 1;
-         
+
          while (n--)
            append_glyph (it);
-         
+
          it->c = '\t';
        }
 
@@ -1718,12 +1729,12 @@ produce_glyphs (it)
 
       it->pixel_width = CHARSET_WIDTH (charset);
       it->nglyphs = it->pixel_width;
-      
+
       if (it->glyph_row)
        append_glyph (it);
     }
 
-  /* Advance current_x by the pixel width as a convenience for 
+  /* Advance current_x by the pixel width as a convenience for
      the caller.  */
   if (it->area == TEXT_AREA)
     it->current_x += it->pixel_width;
@@ -1744,7 +1755,7 @@ produce_special_glyphs (it, what)
      enum display_element_type what;
 {
   struct it temp_it;
-  
+
   temp_it = *it;
   temp_it.dp = NULL;
   temp_it.what = IT_CHARACTER;
@@ -1764,7 +1775,7 @@ produce_special_glyphs (it, what)
        }
       else
        temp_it.c = '\\';
-      
+
       produce_glyphs (&temp_it);
       it->pixel_width = temp_it.pixel_width;
       it->nglyphs = temp_it.pixel_width;
@@ -1781,7 +1792,7 @@ produce_special_glyphs (it, what)
        }
       else
        temp_it.c = '$';
-      
+
       produce_glyphs (&temp_it);
       it->pixel_width = temp_it.pixel_width;
       it->nglyphs = temp_it.pixel_width;
@@ -1791,21 +1802,6 @@ produce_special_glyphs (it, what)
 }
 
 
-/* Return an estimation of the pixel height of mode or top lines on
-   frame F.  FACE_ID specifies what line's height to estimate.  */
-
-int
-estimate_mode_line_height (f, face_id)
-     struct frame *f;
-     enum face_id face_id;
-{
-  if (estimate_mode_line_height_hook)
-    return estimate_mode_line_height_hook (f, face_id);
-  else
-    return 1;
-}
-
-
 \f
 /***********************************************************************
                                Faces
@@ -1902,7 +1898,7 @@ turn_on_face (f, face_id)
   if (TN_max_colors > 0)
     {
       char *p;
-      
+
       if (fg >= 0 && TS_set_foreground)
        {
          p = tparam (TS_set_foreground, NULL, 0, (int) fg);
@@ -1918,7 +1914,7 @@ turn_on_face (f, face_id)
        }
     }
 }
-  
+
 
 /* Turn off appearances of face FACE_ID on tty frame F.  */
 
@@ -1970,19 +1966,55 @@ turn_off_face (f, face_id)
              && face->background != FACE_TTY_DEFAULT_BG_COLOR)))
     OUTPUT1_IF (TS_orig_pair);
 }
-  
-    
+
+
+/* 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
@@ -2264,7 +2296,7 @@ to do `unset TERMCAP' (C-shell: `unsetenv TERMCAP') as well.",
   TS_cursor_visible = tgetstr ("vs", address);
   TS_cursor_invisible = tgetstr ("vi", address);
   TS_set_window = tgetstr ("wi", address);
-  
+
   TS_enter_underline_mode = tgetstr ("us", address);
   TS_exit_underline_mode = tgetstr ("ue", address);
   TS_enter_bold_mode = tgetstr ("md", address);
@@ -2274,7 +2306,7 @@ to do `unset TERMCAP' (C-shell: `unsetenv TERMCAP') as well.",
   TS_enter_alt_charset_mode = tgetstr ("as", address);
   TS_exit_alt_charset_mode = tgetstr ("ae", address);
   TS_exit_attribute_mode = tgetstr ("me", address);
-  
+
   MultiUp = tgetstr ("UP", address);
   MultiDown = tgetstr ("DO", address);
   MultiLeft = tgetstr ("LE", address);
@@ -2294,10 +2326,10 @@ to do `unset TERMCAP' (C-shell: `unsetenv TERMCAP') as well.",
          TS_set_foreground = tgetstr ("Sf", address);
          TS_set_background = tgetstr ("Sb", address);
        }
-      
+
       TN_max_colors = tgetnum ("Co");
       TN_max_pairs = tgetnum ("pa");
-      
+
       TN_no_color_video = tgetnum ("NC");
       if (TN_no_color_video == -1)
        TN_no_color_video = 0;
@@ -2335,7 +2367,7 @@ to do `unset TERMCAP' (C-shell: `unsetenv TERMCAP') as well.",
     SET_FRAME_WIDTH (sf, FRAME_WIDTH (sf));
   if (FRAME_HEIGHT (sf) <= 0)
     FRAME_HEIGHT (sf) = tgetnum ("li");
-  
+
   if (FRAME_HEIGHT (sf) < 3 || FRAME_WIDTH (sf) < 3)
     fatal ("Screen size %dx%d is too small",
           FRAME_HEIGHT (sf), FRAME_WIDTH (sf));
@@ -2362,7 +2394,7 @@ to do `unset TERMCAP' (C-shell: `unsetenv TERMCAP') as well.",
 
   if (TabWidth < 0)
     TabWidth = 8;
-  
+
 /* Turned off since /etc/termcap seems to have :ta= for most terminals
    and newer termcap doc does not seem to say there is a default.
   if (!Wcm.cm_tab)
@@ -2560,5 +2592,6 @@ The function should accept no arguments.  */);
   Vring_bell_function = Qnil;
 
   defsubr (&Stty_display_color_p);
+  defsubr (&Stty_display_color_cells);
 }