Generalize run-time debugging checks.
[bpt/emacs.git] / src / term.c
index ce300f9..128aaca 100644 (file)
@@ -24,6 +24,7 @@ along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.  */
 #include <ctype.h>
 #include <errno.h>
 #include <sys/file.h>
+#include <sys/time.h>
 #include <unistd.h>
 #include <signal.h>
 #include <setjmp.h>
@@ -32,8 +33,8 @@ along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.  */
 #include "termchar.h"
 #include "termopts.h"
 #include "tparam.h"
-#include "buffer.h"
 #include "character.h"
+#include "buffer.h"
 #include "charset.h"
 #include "coding.h"
 #include "composite.h"
@@ -85,11 +86,11 @@ static void clear_tty_hooks (struct terminal *terminal);
 static void set_tty_hooks (struct terminal *terminal);
 static void dissociate_if_controlling_tty (int fd);
 static void delete_tty (struct terminal *);
-static void maybe_fatal (int must_succeed, struct terminal *terminal,
-                        const char *str1, const char *str2, ...)
-  NO_RETURN ATTRIBUTE_FORMAT_PRINTF (3, 5) ATTRIBUTE_FORMAT_PRINTF (4, 5);
-static void vfatal (const char *str, va_list ap)
-  NO_RETURN ATTRIBUTE_FORMAT_PRINTF (1, 0);
+static _Noreturn void maybe_fatal (int must_succeed, struct terminal *terminal,
+                                  const char *str1, const char *str2, ...)
+  ATTRIBUTE_FORMAT_PRINTF (3, 5) ATTRIBUTE_FORMAT_PRINTF (4, 5);
+static _Noreturn void vfatal (const char *str, va_list ap)
+  ATTRIBUTE_FORMAT_PRINTF (1, 0);
 
 
 #define OUTPUT(tty, a)                                          \
@@ -122,12 +123,11 @@ enum no_color_bit
   NC_STANDOUT   = 1 << 0,
   NC_UNDERLINE  = 1 << 1,
   NC_REVERSE    = 1 << 2,
-  NC_BLINK      = 1 << 3,
+  NC_ITALIC     = 1 << 3,
   NC_DIM        = 1 << 4,
   NC_BOLD       = 1 << 5,
   NC_INVIS      = 1 << 6,
-  NC_PROTECT    = 1 << 7,
-  NC_ALT_CHARSET = 1 << 8
+  NC_PROTECT    = 1 << 7
 };
 
 /* internal state */
@@ -1459,7 +1459,7 @@ append_glyph (struct it *it)
   struct glyph *glyph, *end;
   int i;
 
-  xassert (it->glyph_row);
+  eassert (it->glyph_row);
   glyph = (it->glyph_row->glyphs[it->area]
           + it->glyph_row->used[it->area]);
   end = it->glyph_row->glyphs[1 + it->area];
@@ -1546,7 +1546,7 @@ produce_glyphs (struct it *it)
   /* If a hook is installed, let it do the work.  */
 
   /* Nothing but characters are supported on terminal frames.  */
-  xassert (it->what == IT_CHARACTER
+  eassert (it->what == IT_CHARACTER
           || it->what == IT_COMPOSITION
           || it->what == IT_STRETCH
           || it->what == IT_GLYPHLESS);
@@ -1633,7 +1633,7 @@ produce_glyphs (struct it *it)
        {
          Lisp_Object acronym = lookup_glyphless_char_display (-1, it);
 
-         xassert (it->what == IT_GLYPHLESS);
+         eassert (it->what == IT_GLYPHLESS);
          produce_glyphless_glyph (it, 1, acronym);
        }
     }
@@ -1657,7 +1657,7 @@ append_composite_glyph (struct it *it)
 {
   struct glyph *glyph;
 
-  xassert (it->glyph_row);
+  eassert (it->glyph_row);
   glyph = it->glyph_row->glyphs[it->area] + it->glyph_row->used[it->area];
   if (glyph < it->glyph_row->glyphs[1 + it->area])
     {
@@ -1749,7 +1749,7 @@ append_glyphless_glyph (struct it *it, int face_id, const char *str)
   struct glyph *glyph, *end;
   int i;
 
-  xassert (it->glyph_row);
+  eassert (it->glyph_row);
   glyph = it->glyph_row->glyphs[it->area] + it->glyph_row->used[it->area];
   end = it->glyph_row->glyphs[1 + it->area];
 
@@ -1851,8 +1851,7 @@ produce_glyphless_glyph (struct it *it, int for_no_font, Lisp_Object acronym)
        len = 1;
       else if (len > 4)
        len = 4;
-      sprintf (buf, "[%.*s]", len, str);
-      len += 2;
+      len = sprintf (buf, "[%.*s]", len, str);
       str = buf;
     }
   else
@@ -1872,7 +1871,7 @@ produce_glyphless_glyph (struct it *it, int for_no_font, Lisp_Object acronym)
        }
       else
        {
-         xassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEX_CODE);
+         eassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEX_CODE);
          len = (it->c < 0x10000 ? sprintf (buf, "\\u%04X", it->c)
                 : it->c <= MAX_UNICODE_CHAR ? sprintf (buf, "\\U%06X", it->c)
                 : sprintf (buf, "\\x%06X", it->c));
@@ -1915,8 +1914,7 @@ produce_special_glyphs (struct it *it, enum display_element_type what)
       else
        SET_GLYPH_FROM_CHAR (glyph, '\\');
       if (it->dp
-         && (gc = DISP_CONTINUE_GLYPH (it->dp), GLYPH_CODE_P (gc))
-         && GLYPH_CODE_CHAR_VALID_P (gc))
+         && (gc = DISP_CONTINUE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
        {
          /* FIXME: Should we mirror GC for R2L lines?  */
          SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
@@ -1928,8 +1926,7 @@ produce_special_glyphs (struct it *it, enum display_element_type what)
       /* Truncation glyph.  */
       SET_GLYPH_FROM_CHAR (glyph, '$');
       if (it->dp
-         && (gc = DISP_TRUNC_GLYPH (it->dp), GLYPH_CODE_P (gc))
-         && GLYPH_CODE_CHAR_VALID_P (gc))
+         && (gc = DISP_TRUNC_GLYPH (it->dp), GLYPH_CODE_P (gc)))
        {
          /* FIXME: Should we mirror GC for R2L lines?  */
          SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
@@ -2024,17 +2021,16 @@ turn_on_face (struct frame *f, int face_id)
   if (face->tty_bold_p && MAY_USE_WITH_COLORS_P (tty, NC_BOLD))
     OUTPUT1_IF (tty, tty->TS_enter_bold_mode);
 
-  if (face->tty_dim_p && MAY_USE_WITH_COLORS_P (tty, NC_DIM))
-    OUTPUT1_IF (tty, tty->TS_enter_dim_mode);
-
-  /* Alternate charset and blinking not yet used.  */
-  if (face->tty_alt_charset_p
-      && MAY_USE_WITH_COLORS_P (tty, NC_ALT_CHARSET))
-    OUTPUT1_IF (tty, tty->TS_enter_alt_charset_mode);
-
-  if (face->tty_blinking_p
-      && MAY_USE_WITH_COLORS_P (tty, NC_BLINK))
-    OUTPUT1_IF (tty, tty->TS_enter_blink_mode);
+  if (face->tty_italic_p && MAY_USE_WITH_COLORS_P (tty, NC_ITALIC))
+    {
+      if (tty->TS_enter_italic_mode)
+       OUTPUT1 (tty, tty->TS_enter_italic_mode);
+      else
+       /* Italics mode is unavailable on many terminals.  In that
+          case, map slant to dimmed text; we want italic text to
+          appear different and dimming is not otherwise used.  */
+       OUTPUT1 (tty, tty->TS_enter_dim_mode);
+    }
 
   if (face->tty_underline_p && MAY_USE_WITH_COLORS_P (tty, NC_UNDERLINE))
     OUTPUT1_IF (tty, tty->TS_enter_underline_mode);
@@ -2071,7 +2067,7 @@ turn_off_face (struct frame *f, int face_id)
   struct face *face = FACE_FROM_ID (f, face_id);
   struct tty_display_info *tty = FRAME_TTY (f);
 
-  xassert (face != NULL);
+  eassert (face != NULL);
 
   if (tty->TS_exit_attribute_mode)
     {
@@ -2079,27 +2075,19 @@ turn_off_face (struct frame *f, int face_id)
         half-bright, reverse-video, standout, underline.  It may or
         may not turn off alt-char-mode.  */
       if (face->tty_bold_p
-         || face->tty_dim_p
+         || face->tty_italic_p
          || face->tty_reverse_p
-         || face->tty_alt_charset_p
-         || face->tty_blinking_p
          || face->tty_underline_p)
        {
          OUTPUT1_IF (tty, tty->TS_exit_attribute_mode);
          if (strcmp (tty->TS_exit_attribute_mode, tty->TS_end_standout_mode) == 0)
            tty->standout_mode = 0;
        }
-
-      if (face->tty_alt_charset_p)
-       OUTPUT_IF (tty, tty->TS_exit_alt_charset_mode);
     }
   else
     {
       /* If we don't have "me" we can only have those appearances
         that have exit sequences defined.  */
-      if (face->tty_alt_charset_p)
-       OUTPUT_IF (tty, tty->TS_exit_alt_charset_mode);
-
       if (face->tty_underline_p)
        OUTPUT_IF (tty, tty->TS_exit_underline_mode);
     }
@@ -2130,8 +2118,7 @@ tty_capable_p (struct tty_display_info *tty, unsigned int caps,
   TTY_CAPABLE_P_TRY (tty, TTY_CAP_UNDERLINE,   tty->TS_enter_underline_mode,   NC_UNDERLINE);
   TTY_CAPABLE_P_TRY (tty, TTY_CAP_BOLD,        tty->TS_enter_bold_mode,        NC_BOLD);
   TTY_CAPABLE_P_TRY (tty, TTY_CAP_DIM,                 tty->TS_enter_dim_mode,         NC_DIM);
-  TTY_CAPABLE_P_TRY (tty, TTY_CAP_BLINK,       tty->TS_enter_blink_mode,       NC_BLINK);
-  TTY_CAPABLE_P_TRY (tty, TTY_CAP_ALT_CHARSET,         tty->TS_enter_alt_charset_mode, NC_ALT_CHARSET);
+  TTY_CAPABLE_P_TRY (tty, TTY_CAP_ITALIC,      tty->TS_enter_italic_mode,      NC_ITALIC);
 
   /* We can do it!  */
   return 1;
@@ -2145,7 +2132,7 @@ DEFUN ("tty-display-color-p", Ftty_display_color_p, Stty_display_color_p,
 
 TERMINAL can be a terminal object, a frame, or nil (meaning the
 selected frame's terminal).  This function always returns nil if
-TERMINAL does not refer to a text-only terminal.  */)
+TERMINAL does not refer to a text terminal.  */)
   (Lisp_Object terminal)
 {
   struct terminal *t = get_tty_terminal (terminal, 0);
@@ -2162,7 +2149,7 @@ DEFUN ("tty-display-color-cells", Ftty_display_color_cells,
 
 TERMINAL can be a terminal object, a frame, or nil (meaning the
 selected frame's terminal).  This function always returns 0 if
-TERMINAL does not refer to a text-only terminal.  */)
+TERMINAL does not refer to a text terminal.  */)
   (Lisp_Object terminal)
 {
   struct terminal *t = get_tty_terminal (terminal, 0);
@@ -2279,7 +2266,7 @@ set_tty_color_mode (struct tty_display_info *tty, struct frame *f)
   else
     color_mode = Qnil;
 
-  mode = INTEGERP (color_mode) ? XINT (color_mode) : 0;
+  mode = TYPE_RANGED_INTEGERP (int, color_mode) ? XINT (color_mode) : 0;
 
   if (mode != tty->previous_color_mode)
     {
@@ -2384,7 +2371,7 @@ no effect if used on a non-tty terminal.
 
 TERMINAL can be a terminal object, a frame or nil (meaning the
 selected frame's terminal).  This function always returns nil if
-TERMINAL does not refer to a text-only terminal.  */)
+TERMINAL does not refer to a text terminal.  */)
   (Lisp_Object terminal)
 {
   struct terminal *t = get_terminal (terminal, 1);
@@ -2394,6 +2381,21 @@ TERMINAL does not refer to a text-only terminal.  */)
   return Qnil;
 }
 
+DEFUN ("tty-top-frame", Ftty_top_frame, Stty_top_frame, 0, 1, 0,
+       doc: /* Return the topmost terminal frame on TERMINAL.
+TERMINAL can be a terminal object, a frame or nil (meaning the
+selected frame's terminal).  This function returns nil if TERMINAL
+does not refer to a text terminal.  Otherwise, it returns the
+top-most frame on the text terminal.  */)
+  (Lisp_Object terminal)
+{
+  struct terminal *t = get_terminal (terminal, 1);
+
+  if (t->type == output_termcap)
+    return t->display_info.tty->top_frame;
+  return Qnil;
+}
+
 \f
 
 DEFUN ("suspend-tty", Fsuspend_tty, Ssuspend_tty, 0, 1, 0,
@@ -2609,6 +2611,18 @@ term_mouse_movement (FRAME_PTR frame, Gpm_Event *event)
   return 0;
 }
 
+/* Return the Time that corresponds to T.  Wrap around on overflow.  */
+static Time
+timeval_to_Time (struct timeval const *t)
+{
+  Time s_1000, ms;
+
+  s_1000 = t->tv_sec;
+  s_1000 *= 1000;
+  ms = t->tv_usec / 1000;
+  return s_1000 + ms;
+}
+
 /* Return the current position of the mouse.
 
    Set *f to the frame the mouse is in, or zero if the mouse is in no
@@ -2628,7 +2642,6 @@ term_mouse_position (FRAME_PTR *fp, int insist, Lisp_Object *bar_window,
                     Lisp_Object *y, Time *timeptr)
 {
   struct timeval now;
-  Time sec, usec;
 
   *fp = SELECTED_FRAME ();
   (*fp)->mouse_moved = 0;
@@ -2639,9 +2652,7 @@ term_mouse_position (FRAME_PTR *fp, int insist, Lisp_Object *bar_window,
   XSETINT (*x, last_mouse_x);
   XSETINT (*y, last_mouse_y);
   gettimeofday(&now, 0);
-  sec = now.tv_sec;
-  usec = now.tv_usec;
-  *timeptr = (sec * 1000) + (usec / 1000);
+  *timeptr = timeval_to_Time (&now);
 }
 
 /* Prepare a mouse-event in *RESULT for placement in the input queue.
@@ -2665,7 +2676,7 @@ term_mouse_click (struct input_event *result, Gpm_Event *event,
       }
     }
   gettimeofday(&now, 0);
-  result->timestamp = (now.tv_sec * 1000) + (now.tv_usec / 1000);
+  result->timestamp = timeval_to_Time (&now);
 
   if (event->type & GPM_UP)
     result->modifiers = up_modifier;
@@ -3224,8 +3235,8 @@ use the Bourne shell command `TERM=... export TERM' (C-shell:\n\
   tty->TS_enter_underline_mode = tgetstr ("us", address);
   tty->TS_exit_underline_mode = tgetstr ("ue", address);
   tty->TS_enter_bold_mode = tgetstr ("md", address);
+  tty->TS_enter_italic_mode = tgetstr ("ZH", address);
   tty->TS_enter_dim_mode = tgetstr ("mh", address);
-  tty->TS_enter_blink_mode = tgetstr ("mb", address);
   tty->TS_enter_reverse_mode = tgetstr ("mr", address);
   tty->TS_enter_alt_charset_mode = tgetstr ("as", address);
   tty->TS_exit_alt_charset_mode = tgetstr ("ae", address);
@@ -3594,7 +3605,6 @@ delete_tty (struct terminal *terminal)
   xfree (tty->termcap_strings_buffer);
   xfree (tty->termcap_term_buffer);
 
-  memset (tty, 0, sizeof (struct tty_display_info));
   xfree (tty);
 }
 
@@ -3651,6 +3661,7 @@ bigger, or it may make it blink, or it may do nothing at all.  */);
   defsubr (&Stty_no_underline);
   defsubr (&Stty_type);
   defsubr (&Scontrolling_tty_p);
+  defsubr (&Stty_top_frame);
   defsubr (&Ssuspend_tty);
   defsubr (&Sresume_tty);
 #ifdef HAVE_GPM