]> code.delx.au - gnu-emacs/blobdiff - src/term.c
Move all DEFVAR'd globals into a structure -- threading infrastructure
[gnu-emacs] / src / term.c
index 5ffd7416bbd9a5ff549258c20fb73b531259cb6e..63548304a75783696f494edba3b98ff5e6f9b015 100644 (file)
@@ -1,6 +1,6 @@
 /* Terminal control module for terminals described by TERMCAP
    Copyright (C) 1985, 1986, 1987, 1993, 1994, 1995, 1998, 2000, 2001,
-                 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
+                 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
                  Free Software Foundation, Inc.
 
 This file is part of GNU Emacs.
@@ -23,18 +23,9 @@ along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.  */
 #include <config.h>
 #include <stdio.h>
 #include <ctype.h>
-#include <string.h>
 #include <errno.h>
 #include <sys/file.h>
-
-#ifdef HAVE_UNISTD_H
 #include <unistd.h>
-#endif
-
-#if HAVE_TERMIOS_H
-#include <termios.h>           /* For TIOCNOTTY. */
-#endif
-
 #include <signal.h>
 #include <stdarg.h>
 #include <setjmp.h>
@@ -71,6 +62,10 @@ extern int tgetent (char *, const char *);
 extern int tgetflag (char *id);
 extern int tgetnum (char *id);
 
+char *tparam (char *, char *, int, int, ...);
+
+extern char *tgetstr (char *, char **);
+
 #include "cm.h"
 #ifdef HAVE_X_WINDOWS
 #include "xterm.h"
@@ -126,29 +121,11 @@ static void vfatal (const char *str, va_list ap) NO_RETURN;
 
 #define OUTPUT1_IF(tty, a) do { if (a) emacs_tputs ((tty), a, 1, cmputc); } while (0)
 
-/* If true, use "vs", otherwise use "ve" to make the cursor visible.  */
-
-static int visible_cursor;
-
 /* Display space properties */
 
-extern Lisp_Object Qspace, QCalign_to, QCwidth;
-
-/* Functions to call after suspending a tty. */
-Lisp_Object Vsuspend_tty_functions;
-
-/* Functions to call after resuming a tty. */
-Lisp_Object Vresume_tty_functions;
-
 /* Chain of all tty device parameters. */
 struct tty_display_info *tty_list;
 
-/* Nonzero means no need to redraw the entire frame on resuming a
-   suspended Emacs.  This is useful on terminals with multiple
-   pages, where one page is used for Emacs and another for all
-   else. */
-int no_redraw_on_reenter;
-
 /* Meaning of bits in no_color_video.  Each bit set means that the
    corresponding attribute cannot be combined with colors.  */
 
@@ -179,36 +156,15 @@ int max_frame_lines;
    should not open a frame on stdout. */
 static int no_controlling_tty;
 
-/* Provided for lisp packages.  */
-
-static int system_uses_terminfo;
-
-char *tparam ();
-
-extern char *tgetstr (char *, char **);
 \f
 
 #ifdef HAVE_GPM
 #include <sys/fcntl.h>
 
-static void term_clear_mouse_face (void);
-static void term_mouse_highlight (struct frame *f, int x, int y);
-
 /* The device for which we have enabled gpm support (or NULL).  */
 struct tty_display_info *gpm_tty = NULL;
 
-/* These variables describe the range of text currently shown in its
-   mouse-face, together with the window they apply to.  As long as
-   the mouse stays within this range, we need not redraw anything on
-   its account.  Rows and columns are glyph matrix positions in
-   MOUSE_FACE_WINDOW.  */
-static int mouse_face_beg_row, mouse_face_beg_col;
-static int mouse_face_end_row, mouse_face_end_col;
-static int mouse_face_past_end;
-static Lisp_Object mouse_face_window;
-static int mouse_face_face_id;
-
-static int pos_x, pos_y;
+/* Last recorded mouse coordinates.  */
 static int last_mouse_x, last_mouse_y;
 #endif /* HAVE_GPM */
 
@@ -249,7 +205,6 @@ tty_set_terminal_modes (struct terminal *terminal)
             cmputc ('\n');
         }
 
-      OUTPUT_IF (tty, tty->TS_termcap_modes);
       OUTPUT_IF (tty, visible_cursor ? tty->TS_cursor_visible : tty->TS_cursor_normal);
       OUTPUT_IF (tty, tty->TS_keypad_mode);
       losecursor (tty);
@@ -601,7 +556,7 @@ encode_terminal_code (struct glyph *src, int src_len, struct coding_system *codi
          if (src->u.cmp.automatic)
            {
              gstring = composition_gstring_from_id (src->u.cmp.id);
-             required = src->u.cmp.to + 1 - src->u.cmp.from;
+             required = src->slice.cmp.to + 1 - src->slice.cmp.from;
            }
          else
            {
@@ -618,7 +573,7 @@ encode_terminal_code (struct glyph *src, int src_len, struct coding_system *codi
            }
 
          if (src->u.cmp.automatic)
-           for (i = src->u.cmp.from; i <= src->u.cmp.to; i++)
+           for (i = src->slice.cmp.from; i <= src->slice.cmp.to; i++)
              {
                Lisp_Object g = LGSTRING_GLYPH (gstring, i);
                int c = LGLYPH_CHAR (g);
@@ -691,7 +646,8 @@ encode_terminal_code (struct glyph *src, int src_len, struct coding_system *codi
                                                  encode_terminal_src_size);
                  buf = encode_terminal_src + nbytes;
                }
-             if (char_charset (c, charset_list, NULL))
+             if (CHAR_BYTE8_P (c)
+                 || char_charset (c, charset_list, NULL))
                {
                  /* Store the multibyte form of C at BUF.  */
                  buf += CHAR_STRING (c, buf);
@@ -712,7 +668,7 @@ encode_terminal_code (struct glyph *src, int src_len, struct coding_system *codi
            }
          else
            {
-             unsigned char *p = SDATA (string), *pend = p + SBYTES (string);
+             unsigned char *p = SDATA (string);
 
              if (! STRING_MULTIBYTE (string))
                string = string_to_multibyte (string);
@@ -1086,7 +1042,7 @@ tty_ins_del_lines (struct frame *f, int vpos, int n)
    not counting any line-dependent padding.  */
 
 int
-string_cost (char *str)
+string_cost (const char *str)
 {
   cost = 0;
   if (str)
@@ -1098,7 +1054,7 @@ string_cost (char *str)
    counting any line-dependent padding at one line.  */
 
 static int
-string_cost_one_line (char *str)
+string_cost_one_line (const char *str)
 {
   cost = 0;
   if (str)
@@ -1110,7 +1066,7 @@ string_cost_one_line (char *str)
    in tenths of characters.  */
 
 int
-per_line_cost (char *str)
+per_line_cost (const char *str)
 {
   cost = 0;
   if (str)
@@ -1121,7 +1077,6 @@ per_line_cost (char *str)
   return cost;
 }
 
-#ifndef old
 /* char_ins_del_cost[n] is cost of inserting N characters.
    char_ins_del_cost[-n] is cost of deleting N characters.
    The length of this vector is based on max_frame_cols.  */
@@ -1129,7 +1084,6 @@ per_line_cost (char *str)
 int *char_ins_del_vector;
 
 #define char_ins_del_cost(f) (&char_ins_del_vector[FRAME_COLS ((f))])
-#endif
 
 /* ARGSUSED */
 static void
@@ -1267,7 +1221,7 @@ struct fkey_table {
      other keys (as on the IBM PC keyboard) they get overridden.
   */
 
-static struct fkey_table keys[] =
+static const struct fkey_table keys[] =
 {
   {"kh", "home"},      /* termcap */
   {"kl", "left"},      /* termcap */
@@ -1363,6 +1317,7 @@ static struct fkey_table keys[] =
   {"!3", "S-undo"}       /*shifted undo key*/
   };
 
+#ifndef DOS_NT
 static char **term_get_fkeys_address;
 static KBOARD *term_get_fkeys_kboard;
 static Lisp_Object term_get_fkeys_1 (void);
@@ -1382,7 +1337,6 @@ term_get_fkeys (char **address, KBOARD *kboard)
      function key specification, rather than giving the user an error and
      refusing to run at all on such a terminal.  */
 
-  extern Lisp_Object Fidentity (Lisp_Object);
   term_get_fkeys_address = address;
   term_get_fkeys_kboard = kboard;
   internal_condition_case (term_get_fkeys_1, Qerror, Fidentity);
@@ -1500,24 +1454,18 @@ term_get_fkeys_1 (void)
 
   return Qnil;
 }
+#endif /* not DOS_NT */
 
 \f
 /***********************************************************************
                       Character Display Information
  ***********************************************************************/
-
-/* Avoid name clash with functions defined in xterm.c */
-#ifdef static
-#define append_glyph append_glyph_term
-#define produce_stretch_glyph produce_stretch_glyph_term
-#define append_composite_glyph append_composite_glyph_term
-#define produce_composite_glyph produce_composite_glyph_term
-#endif
-
 static void append_glyph (struct it *);
 static void produce_stretch_glyph (struct it *);
 static void append_composite_glyph (struct it *);
 static void produce_composite_glyph (struct it *);
+static void append_glyphless_glyph (struct it *, int, char *);
+static void produce_glyphless_glyph (struct it *, int, Lisp_Object);
 
 /* Append glyphs to IT's glyph_row.  Called from produce_glyphs for
    terminal frames if IT->glyph_row != NULL.  IT->char_to_display is
@@ -1626,18 +1574,21 @@ produce_glyphs (struct it *it)
       goto done;
     }
 
-  /* Maybe translate single-byte characters to multibyte.  */
-  it->char_to_display = it->c;
+  if (it->what == IT_GLYPHLESS)
+    {
+      produce_glyphless_glyph (it, 0, Qnil);
+      goto done;
+    }
 
-  if (it->c >= 040 && it->c < 0177)
+  if (it->char_to_display >= 040 && it->char_to_display < 0177)
     {
       it->pixel_width = it->nglyphs = 1;
       if (it->glyph_row)
        append_glyph (it);
     }
-  else if (it->c == '\n')
+  else if (it->char_to_display == '\n')
     it->pixel_width = it->nglyphs = 0;
-  else if (it->c == '\t')
+  else if (it->char_to_display == '\t')
     {
       int absolute_x = (it->current_x
                        + it->continuation_lines_width);
@@ -1668,12 +1619,22 @@ produce_glyphs (struct it *it)
       it->pixel_width = nspaces;
       it->nglyphs = nspaces;
     }
-  else if (CHAR_BYTE8_P (it->c))
+  else if (CHAR_BYTE8_P (it->char_to_display))
     {
-      if (unibyte_display_via_language_environment
-         && (it->c >= 0240))
+      /* Coming here means that we must send the raw 8-bit byte as is
+        to the terminal.  Although there's no way to know how many
+        columns it occupies on a screen, it is a good assumption that
+        a single byte code has 1-column width.  */
+      it->pixel_width = it->nglyphs = 1;
+      if (it->glyph_row)
+       append_glyph (it);
+    }
+  else
+    {
+      Lisp_Object charset_list = FRAME_TERMINAL (it->f)->charset_list;
+
+      if (char_charset (it->char_to_display, charset_list, NULL))
        {
-         it->char_to_display = BYTE8_TO_CHAR (it->c);
          it->pixel_width = CHAR_WIDTH (it->char_to_display);
          it->nglyphs = it->pixel_width;
          if (it->glyph_row)
@@ -1681,23 +1642,11 @@ produce_glyphs (struct it *it)
        }
       else
        {
-         /* Coming here means that it->c is from display table, thus
-            we must send the raw 8-bit byte as is to the terminal.
-            Although there's no way to know how many columns it
-            occupies on a screen, it is a good assumption that a
-            single byte code has 1-column width.  */
-         it->pixel_width = it->nglyphs = 1;
-         if (it->glyph_row)
-           append_glyph (it);
-       }
-    }
-  else
-    {
-      it->pixel_width = CHAR_WIDTH (it->c);
-      it->nglyphs = it->pixel_width;
+         Lisp_Object acronym = lookup_glyphless_char_display (-1, it);
 
-      if (it->glyph_row)
-       append_glyph (it);
+         xassert (it->what == IT_GLYPHLESS);
+         produce_glyphless_glyph (it, 1, acronym);
+       }
     }
 
  done:
@@ -1823,8 +1772,8 @@ append_composite_glyph (struct it *it)
        {
          glyph->u.cmp.automatic = 1;
          glyph->u.cmp.id = it->cmp_it.id;
-         glyph->u.cmp.from = it->cmp_it.from;
-         glyph->u.cmp.to = it->cmp_it.to - 1;
+         glyph->slice.cmp.from = it->cmp_it.from;
+         glyph->slice.cmp.to = it->cmp_it.to - 1;
        }
 
       glyph->face_id = it->face_id;
@@ -1858,8 +1807,6 @@ append_composite_glyph (struct it *it)
 static void
 produce_composite_glyph (struct it *it)
 {
-  int c;
-
   if (it->cmp_it.ch < 0)
     {
       struct composition *cmp = composition_table[it->cmp_it.id];
@@ -1879,6 +1826,152 @@ produce_composite_glyph (struct it *it)
 }
 
 
+/* Append a glyph for a glyphless character to IT->glyph_row.  FACE_ID
+   is a face ID to be used for the glyph.  What is actually appended
+   are glyphs of type CHAR_GLYPH whose characters are in STR (which
+   comes from it->nglyphs bytes).  */
+
+static void
+append_glyphless_glyph (struct it *it, int face_id, char *str)
+{
+  struct glyph *glyph, *end;
+  int i;
+
+  xassert (it->glyph_row);
+  glyph = it->glyph_row->glyphs[it->area] + it->glyph_row->used[it->area];
+  end = it->glyph_row->glyphs[1 + it->area];
+
+  /* If the glyph row is reversed, we need to prepend the glyph rather
+     than append it.  */
+  if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
+    {
+      struct glyph *g;
+      int move_by = it->pixel_width;
+
+      /* Make room for the new glyphs.  */
+      if (move_by > end - glyph) /* don't overstep end of this area */
+       move_by = end - glyph;
+      for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
+       g[move_by] = *g;
+      glyph = it->glyph_row->glyphs[it->area];
+      end = glyph + move_by;
+    }
+
+  if (glyph >= end)
+    return;
+  glyph->type = CHAR_GLYPH;
+  glyph->pixel_width = 1;
+  glyph->face_id = face_id;
+  glyph->padding_p = 0;
+  glyph->charpos = CHARPOS (it->position);
+  glyph->object = it->object;
+  if (it->bidi_p)
+    {
+      glyph->resolved_level = it->bidi_it.resolved_level;
+      if ((it->bidi_it.type & 7) != it->bidi_it.type)
+       abort ();
+      glyph->bidi_type = it->bidi_it.type;
+    }
+  else
+    {
+      glyph->resolved_level = 0;
+      glyph->bidi_type = UNKNOWN_BT;
+    }
+
+  /* BIDI Note: we put the glyphs of characters left to right, even in
+     the REVERSED_P case because we write to the terminal
+     left-to-right.  */
+  for (i = 0; i < it->nglyphs && glyph < end; ++i)
+    {
+      if (i > 0)
+       glyph[0] = glyph[-1];
+      glyph->u.ch = str[i];
+      ++it->glyph_row->used[it->area];
+      ++glyph;
+    }
+}
+
+/* Produce glyphs for a glyphless character for iterator IT.
+   IT->glyphless_method specifies which method to use for displaying
+   the character.  See the description of enum
+   glyphless_display_method in dispextern.h for the details.
+
+   FOR_NO_FONT is nonzero if and only if this is for a character that
+   is not supproted by the coding system of the terminal.  ACRONYM, if
+   non-nil, is an acronym string for the character.
+
+   The glyphs actually produced are of type CHAR_GLYPH.  */
+
+static void
+produce_glyphless_glyph (struct it *it, int for_no_font, Lisp_Object acronym)
+{
+  int face_id;
+  int len;
+  char buf[9], *str = "    ";
+
+  /* Get a face ID for the glyph by utilizing a cache (the same way as
+     done for `escape-glyph' in get_next_display_element).  */
+  if (it->f == last_glyphless_glyph_frame
+      && it->face_id == last_glyphless_glyph_face_id)
+    {
+      face_id = last_glyphless_glyph_merged_face_id;
+    }
+  else
+    {
+      /* Merge the `glyphless-char' face into the current face.  */
+      face_id = merge_faces (it->f, Qglyphless_char, 0, it->face_id);
+      last_glyphless_glyph_frame = it->f;
+      last_glyphless_glyph_face_id = it->face_id;
+      last_glyphless_glyph_merged_face_id = face_id;
+    }
+
+  if (it->glyphless_method == GLYPHLESS_DISPLAY_THIN_SPACE)
+    {
+      /* As there's no way to produce a thin space, we produce a space
+        of canonical width.  */
+      len = 1;
+    }
+  else if (it->glyphless_method == GLYPHLESS_DISPLAY_EMPTY_BOX)
+    {
+      len = CHAR_WIDTH (it->c);
+      if (len == 0)
+       len = 1;
+      else if (len > 4)
+       len = 4;
+      sprintf (buf, "[%.*s]", len, str);
+      len += 2;
+      str = buf;
+    }
+  else
+    {
+      if (it->glyphless_method == GLYPHLESS_DISPLAY_ACRONYM)
+       {
+         if (! STRINGP (acronym) && CHAR_TABLE_P (Vglyphless_char_display))
+           acronym = CHAR_TABLE_REF (Vglyphless_char_display, it->c);
+         buf[0] = '[';
+         str = STRINGP (acronym) ? (char *) SDATA (acronym) : "";
+         for (len = 0; len < 6 && str[len] && ASCII_BYTE_P (str[len]); len++)
+           buf[1 + len] = str[len];
+         buf[1 + len] = ']';
+         len += 2;
+       }
+      else
+       {
+         xassert (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));
+       }
+      str = buf;
+    }
+
+  it->pixel_width = len;
+  it->nglyphs = len;
+  if (len > 0 && it->glyph_row)
+    append_glyphless_glyph (it, face_id, str);
+}
+
+
 /* Get information about special display element WHAT in an
    environment described by IT.  WHAT is one of IT_TRUNCATION or
    IT_CONTINUATION.  Maybe produce glyphs for WHAT if IT has a
@@ -1931,7 +2024,7 @@ produce_special_glyphs (struct it *it, enum display_element_type what)
   else
     abort ();
 
-  temp_it.c = GLYPH_CHAR (glyph);
+  temp_it.c = temp_it.char_to_display = GLYPH_CHAR (glyph);
   temp_it.face_id = GLYPH_FACE (glyph);
   temp_it.len = CHAR_BYTES (temp_it.c);
 
@@ -2254,7 +2347,6 @@ set_tty_color_mode (struct tty_display_info *tty, struct frame *f)
   Lisp_Object tem, val;
   Lisp_Object color_mode;
   int mode;
-  extern Lisp_Object Qtty_color_mode;
   Lisp_Object tty_color_mode_alist
     = Fintern_soft (build_string ("tty-color-mode-alist"), Qnil);
 
@@ -2313,7 +2405,7 @@ get_tty_terminal (Lisp_Object terminal, int throw)
    Returns NULL if the named terminal device is not opened.  */
 
 struct terminal *
-get_named_tty (char *name)
+get_named_tty (const char *name)
 {
   struct terminal *t;
 
@@ -2431,10 +2523,7 @@ A suspended tty may be resumed by calling `resume-tty' on it.  */)
         }
 
       reset_sys_modes (t->display_info.tty);
-
-#ifdef subprocesses
       delete_keyboard_wait_descriptor (fileno (f));
-#endif
 
 #ifndef MSDOS
       fclose (f);
@@ -2502,9 +2591,7 @@ frame's terminal). */)
       t->display_info.tty->input = t->display_info.tty->output;
 #endif
 
-#ifdef subprocesses
       add_keyboard_wait_descriptor (fd);
-#endif
 
       if (FRAMEP (t->display_info.tty->top_frame))
        {
@@ -2558,415 +2645,36 @@ term_mouse_moveto (int x, int y)
   last_mouse_y = y;  */
 }
 
-static void
-term_show_mouse_face (enum draw_glyphs_face draw)
+/* Implementation of draw_row_with_mouse_face for TTY/GPM.  */
+void
+tty_draw_row_with_mouse_face (struct window *w, struct glyph_row *row,
+                             int start_hpos, int end_hpos,
+                             enum draw_glyphs_face draw)
 {
-  struct window *w = XWINDOW (mouse_face_window);
-  int save_x, save_y;
-  int i;
-
-  struct frame *f = XFRAME (w->frame);
+  int nglyphs = end_hpos - start_hpos;
+  struct frame *f = XFRAME (WINDOW_FRAME (w));
   struct tty_display_info *tty = FRAME_TTY (f);
+  int face_id = tty->mouse_highlight.mouse_face_face_id;
+  int save_x, save_y, pos_x, pos_y;
 
-  if (/* If window is in the process of being destroyed, don't bother
-        to do anything.  */
-      w->current_matrix != NULL
-      /* Recognize when we are called to operate on rows that don't exist
-        anymore.  This can happen when a window is split.  */
-      && mouse_face_end_row < w->current_matrix->nrows)
-    {
-      /* write_glyphs writes at cursor position, so we need to
-        temporarily move cursor coordinates to the beginning of
-        the highlight region.  */
-
-      /* Save current cursor co-ordinates */
-      save_y = curY (tty);
-      save_x = curX (tty);
-
-      /* Note that mouse_face_beg_row etc. are window relative.  */
-      for (i = mouse_face_beg_row; i <= mouse_face_end_row; i++)
-       {
-         int start_hpos, end_hpos, nglyphs;
-         struct glyph_row *row = MATRIX_ROW (w->current_matrix, i);
-
-         /* Don't do anything if row doesn't have valid contents.  */
-         if (!row->enabled_p)
-           continue;
-
-         /* For all but the first row, the highlight starts at column 0.  */
-         if (i == mouse_face_beg_row)
-           start_hpos = mouse_face_beg_col;
-         else
-           start_hpos = 0;
-
-         if (i == mouse_face_end_row)
-           end_hpos = mouse_face_end_col;
-         else
-           {
-             end_hpos = row->used[TEXT_AREA];
-             if (draw == DRAW_NORMAL_TEXT)
-               row->fill_line_p = 1; /* Clear to end of line */
-           }
-
-         if (end_hpos <= start_hpos)
-           continue;
-         /* Record that some glyphs of this row are displayed in
-            mouse-face.  */
-         row->mouse_face_p = draw > 0;
-
-         nglyphs = end_hpos - start_hpos;
-
-         if (end_hpos >= row->used[TEXT_AREA])
-           nglyphs = row->used[TEXT_AREA] - start_hpos;
-
-         pos_y = row->y + WINDOW_TOP_EDGE_Y (w);
-         pos_x = row->used[LEFT_MARGIN_AREA] + start_hpos
-           + WINDOW_LEFT_EDGE_X (w);
-
-         cursor_to (f, pos_y, pos_x);
-
-         if (draw == DRAW_MOUSE_FACE)
-           {
-             tty_write_glyphs_with_face (f, row->glyphs[TEXT_AREA] + start_hpos,
-                                     nglyphs, mouse_face_face_id);
-           }
-         else /* draw == DRAW_NORMAL_TEXT */
-           write_glyphs (f, row->glyphs[TEXT_AREA] + start_hpos, nglyphs);
-       }
-      cursor_to (f, save_y, save_x);
-    }
-}
-
-static void
-term_clear_mouse_face (void)
-{
-  if (!NILP (mouse_face_window))
-    term_show_mouse_face (DRAW_NORMAL_TEXT);
-
-  mouse_face_beg_row = mouse_face_beg_col = -1;
-  mouse_face_end_row = mouse_face_end_col = -1;
-  mouse_face_window = Qnil;
-}
-
-/* Find the glyph matrix position of buffer position POS in window W.
-   *HPOS and *VPOS are set to the positions found.  W's current glyphs
-   must be up to date.  If POS is above window start return (0, 0).
-   If POS is after end of W, return end of last line in W.
-   - taken from msdos.c */
-static int
-fast_find_position (struct window *w, int pos, int *hpos, int *vpos)
-{
-  int i, lastcol, line_start_position, maybe_next_line_p = 0;
-  int yb = window_text_bottom_y (w);
-  struct glyph_row *row = MATRIX_ROW (w->current_matrix, 0), *best_row = row;
-
-  while (row->y < yb)
-    {
-      if (row->used[TEXT_AREA])
-       line_start_position = row->glyphs[TEXT_AREA]->charpos;
-      else
-       line_start_position = 0;
-
-      if (line_start_position > pos)
-       break;
-      /* If the position sought is the end of the buffer,
-        don't include the blank lines at the bottom of the window.  */
-      else if (line_start_position == pos
-              && pos == BUF_ZV (XBUFFER (w->buffer)))
-       {
-         maybe_next_line_p = 1;
-         break;
-       }
-      else if (line_start_position > 0)
-       best_row = row;
-
-      /* Don't overstep the last matrix row, lest we get into the
-        never-never land... */
-      if (row->y + 1 >= yb)
-       break;
-
-      ++row;
-    }
-
-  /* Find the right column within BEST_ROW.  */
-  lastcol = 0;
-  row = best_row;
-  for (i = 0; i < row->used[TEXT_AREA]; i++)
-    {
-      struct glyph *glyph = row->glyphs[TEXT_AREA] + i;
-      int charpos;
-
-      charpos = glyph->charpos;
-      if (charpos == pos)
-       {
-         *hpos = i;
-         *vpos = row->y;
-         return 1;
-       }
-      else if (charpos > pos)
-       break;
-      else if (charpos > 0)
-       lastcol = i;
-    }
-
-  /* If we're looking for the end of the buffer,
-     and we didn't find it in the line we scanned,
-     use the start of the following line.  */
-  if (maybe_next_line_p)
-    {
-      ++row;
-      lastcol = 0;
-    }
-
-  *vpos = row->y;
-  *hpos = lastcol + 1;
-  return 0;
-}
-
-static void
-term_mouse_highlight (struct frame *f, int x, int y)
-{
-  enum window_part part;
-  Lisp_Object window;
-  struct window *w;
-  struct buffer *b;
-
-  if (NILP (Vmouse_highlight)
-      || !f->glyphs_initialized_p)
-    return;
-
-  /* Which window is that in?  */
-  window = window_from_coordinates (f, x, y, &part, &x, &y, 0);
-
-  /* Not on a window -> return.  */
-  if (!WINDOWP (window))
-    return;
-
-  if (!EQ (window, mouse_face_window))
-    term_clear_mouse_face ();
-
-  w = XWINDOW (window);
-
-  /* Are we in a window whose display is up to date?
-     And verify the buffer's text has not changed.  */
-  b = XBUFFER (w->buffer);
-  if (part == ON_TEXT
-      && EQ (w->window_end_valid, w->buffer)
-      && XFASTINT (w->last_modified) == BUF_MODIFF (b)
-      && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
-    {
-      int pos, i, nrows = w->current_matrix->nrows;
-      struct glyph_row *row;
-      struct glyph *glyph;
+  if (end_hpos >= row->used[TEXT_AREA])
+    nglyphs = row->used[TEXT_AREA] - start_hpos;
 
-      /* Find the glyph under X/Y.  */
-      glyph = NULL;
-      if (y >= 0 && y < nrows)
-       {
-         row = MATRIX_ROW (w->current_matrix, y);
-         /* Give up if some row before the one we are looking for is
-            not enabled.  */
-         for (i = 0; i <= y; i++)
-           if (!MATRIX_ROW (w->current_matrix, i)->enabled_p)
-             break;
-         if (i > y  /* all rows upto and including the one at Y are enabled */
-             && row->displays_text_p
-             && x <  window_box_width (w, TEXT_AREA))
-           {
-             glyph = row->glyphs[TEXT_AREA];
-             if (x >= row->used[TEXT_AREA])
-               glyph = NULL;
-             else
-               {
-                 glyph += x;
-                 if (!BUFFERP (glyph->object))
-                   glyph = NULL;
-               }
-           }
-       }
+  pos_y = row->y + WINDOW_TOP_EDGE_Y (w);
+  pos_x = row->used[LEFT_MARGIN_AREA] + start_hpos + WINDOW_LEFT_EDGE_X (w);
 
-      /* Clear mouse face if X/Y not over text.  */
-      if (glyph == NULL)
-       {
-         term_clear_mouse_face ();
-         return;
-       }
-
-      if (!BUFFERP (glyph->object))
-       abort ();
-      pos = glyph->charpos;
-
-      /* Check for mouse-face.  */
-      {
-       extern Lisp_Object Qmouse_face;
-       Lisp_Object mouse_face, overlay, position, *overlay_vec;
-       int noverlays, obegv, ozv;
-       struct buffer *obuf;
-
-       /* If we get an out-of-range value, return now; avoid an error.  */
-       if (pos > BUF_Z (b))
-         return;
-
-       /* Make the window's buffer temporarily current for
-          overlays_at and compute_char_face.  */
-       obuf = current_buffer;
-       current_buffer = b;
-       obegv = BEGV;
-       ozv = ZV;
-       BEGV = BEG;
-       ZV = Z;
-
-       /* Is this char mouse-active?  */
-       XSETINT (position, pos);
-
-       /* Put all the overlays we want in a vector in overlay_vec.  */
-       GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
-       /* Sort overlays into increasing priority order.  */
-       noverlays = sort_overlays (overlay_vec, noverlays, w);
-
-       /* Check mouse-face highlighting.  */
-       if (!(EQ (window, mouse_face_window)
-             && y >= mouse_face_beg_row
-             && y <= mouse_face_end_row
-             && (y > mouse_face_beg_row
-                 || x >= mouse_face_beg_col)
-             && (y < mouse_face_end_row
-                 || x < mouse_face_end_col
-                 || mouse_face_past_end)))
-         {
-           /* Clear the display of the old active region, if any.  */
-           term_clear_mouse_face ();
-
-           /* Find the highest priority overlay that has a mouse-face
-              property.  */
-           overlay = Qnil;
-           for (i = noverlays - 1; i >= 0; --i)
-             {
-               mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
-               if (!NILP (mouse_face))
-                 {
-                   overlay = overlay_vec[i];
-                   break;
-                 }
-             }
-
-           /* If no overlay applies, get a text property.  */
-           if (NILP (overlay))
-             mouse_face = Fget_text_property (position, Qmouse_face,
-                                              w->buffer);
-
-           /* Handle the overlay case.  */
-           if (!NILP (overlay))
-             {
-               /* Find the range of text around this char that
-                  should be active.  */
-               Lisp_Object before, after;
-               EMACS_INT ignore;
-
-
-               before = Foverlay_start (overlay);
-               after = Foverlay_end (overlay);
-               /* Record this as the current active region.  */
-               fast_find_position (w, XFASTINT (before),
-                                   &mouse_face_beg_col,
-                                   &mouse_face_beg_row);
-
-               mouse_face_past_end
-                 = !fast_find_position (w, XFASTINT (after),
-                                        &mouse_face_end_col,
-                                        &mouse_face_end_row);
-               mouse_face_window = window;
-
-               mouse_face_face_id
-                 = face_at_buffer_position (w, pos, 0, 0,
-                                            &ignore, pos + 1, 1, -1);
-
-               /* Display it as active.  */
-               term_show_mouse_face (DRAW_MOUSE_FACE);
-             }
-           /* Handle the text property case.  */
-           else if (!NILP (mouse_face))
-             {
-               /* Find the range of text around this char that
-                  should be active.  */
-               Lisp_Object before, after, beginning, end;
-               EMACS_INT ignore;
-
-               beginning = Fmarker_position (w->start);
-               XSETINT (end, (BUF_Z (b) - XFASTINT (w->window_end_pos)));
-               before
-                 = Fprevious_single_property_change (make_number (pos + 1),
-                                                     Qmouse_face,
-                                                     w->buffer, beginning);
-               after
-                 = Fnext_single_property_change (position, Qmouse_face,
-                                                 w->buffer, end);
-
-               /* Record this as the current active region.  */
-               fast_find_position (w, XFASTINT (before),
-                                   &mouse_face_beg_col,
-                                   &mouse_face_beg_row);
-               mouse_face_past_end
-                 = !fast_find_position (w, XFASTINT (after),
-                                        &mouse_face_end_col,
-                                        &mouse_face_end_row);
-               mouse_face_window = window;
-
-               mouse_face_face_id
-                 = face_at_buffer_position (w, pos, 0, 0,
-                                            &ignore, pos + 1, 1, -1);
-
-               /* Display it as active.  */
-               term_show_mouse_face (DRAW_MOUSE_FACE);
-             }
-         }
-
-       /* Look for a `help-echo' property.  */
-       {
-         Lisp_Object help;
-         extern Lisp_Object Qhelp_echo;
-
-         /* Check overlays first.  */
-         help = Qnil;
-         for (i = noverlays - 1; i >= 0 && NILP (help); --i)
-           {
-             overlay = overlay_vec[i];
-             help = Foverlay_get (overlay, Qhelp_echo);
-           }
+  /* Save current cursor co-ordinates.  */
+  save_y = curY (tty);
+  save_x = curX (tty);
+  cursor_to (f, pos_y, pos_x);
 
-         if (!NILP (help))
-           {
-             help_echo_string = help;
-             help_echo_window = window;
-             help_echo_object = overlay;
-             help_echo_pos = pos;
-           }
-         /* Try text properties.  */
-         else if (NILP (help)
-                  && ((STRINGP (glyph->object)
-                       && glyph->charpos >= 0
-                       && glyph->charpos < SCHARS (glyph->object))
-                      || (BUFFERP (glyph->object)
-                          && glyph->charpos >= BEGV
-                          && glyph->charpos < ZV)))
-           {
-             help = Fget_text_property (make_number (glyph->charpos),
-                                        Qhelp_echo, glyph->object);
-             if (!NILP (help))
-               {
-                 help_echo_string = help;
-                 help_echo_window = window;
-                 help_echo_object = glyph->object;
-                 help_echo_pos = glyph->charpos;
-               }
-           }
-       }
+  if (draw == DRAW_MOUSE_FACE)
+    tty_write_glyphs_with_face (f, row->glyphs[TEXT_AREA] + start_hpos,
+                               nglyphs, face_id);
+  else if (draw == DRAW_NORMAL_TEXT)
+    write_glyphs (f, row->glyphs[TEXT_AREA] + start_hpos, nglyphs);
 
-       BEGV = obegv;
-       ZV = ozv;
-       current_buffer = obuf;
-      }
-    }
+  cursor_to (f, save_y, save_x);
 }
 
 static int
@@ -2976,7 +2684,7 @@ term_mouse_movement (FRAME_PTR frame, Gpm_Event *event)
   if (event->x != last_mouse_x || event->y != last_mouse_y)
     {
       frame->mouse_moved = 1;
-      term_mouse_highlight (frame, event->x, event->y);
+      note_mouse_highlight (frame, event->x, event->y);
       /* Remember which glyph we're now on.  */
       last_mouse_x = event->x;
       last_mouse_y = event->y;
@@ -3349,11 +3057,10 @@ static void
 dissociate_if_controlling_tty (int fd)
 {
 #ifndef DOS_NT
-  int pgid;
-  EMACS_GET_TTY_PGRP (fd, &pgid); /* If tcgetpgrp succeeds, fd is the ctty. */
+  int pgid = tcgetpgrp (fd); /* If tcgetpgrp succeeds, fd is the ctty. */
   if (pgid != -1)
     {
-#if defined (USG)
+#if defined (USG5)
       setpgrp ();
       no_controlling_tty = 1;
 #elif defined (CYGWIN)
@@ -3391,7 +3098,7 @@ dissociate_if_controlling_tty (int fd)
    If MUST_SUCCEED is true, then all errors are fatal. */
 
 struct terminal *
-init_tty (char *name, char *terminal_type, int must_succeed)
+init_tty (const char *name, const char *terminal_type, int must_succeed)
 {
   char *area = NULL;
   char **address = &area;
@@ -3442,6 +3149,15 @@ init_tty (char *name, char *terminal_type, int must_succeed)
   tty->Wcm = (struct cm *) xmalloc (sizeof (struct cm));
   Wcm_clear (tty);
 
+  encode_terminal_src_size = 0;
+  encode_terminal_dst_size = 0;
+
+#ifdef HAVE_GPM
+  terminal->mouse_position_hook = term_mouse_position;
+  tty->mouse_highlight.mouse_face_window = Qnil;
+#endif
+
+  
 #ifndef DOS_NT
   set_tty_hooks (terminal);
 
@@ -3495,80 +3211,6 @@ init_tty (char *name, char *terminal_type, int must_succeed)
 
   add_keyboard_wait_descriptor (fileno (tty->input));
 
-#endif /* !DOS_NT */
-
-  encode_terminal_src_size = 0;
-  encode_terminal_dst_size = 0;
-
-#ifdef HAVE_GPM
-  terminal->mouse_position_hook = term_mouse_position;
-  mouse_face_window = Qnil;
-#endif
-
-#ifdef DOS_NT
-#ifdef WINDOWSNT
-  initialize_w32_display (terminal);
-#else  /* MSDOS */
-  if (strcmp (terminal_type, "internal") == 0)
-    terminal->type = output_msdos_raw;
-  initialize_msdos_display (terminal);
-#endif /* MSDOS */
-  tty->output = stdout;
-  tty->input = stdin;
-  /* The following two are inaccessible from w32console.c.  */
-  terminal->delete_frame_hook = &tty_free_frame_resources;
-  terminal->delete_terminal_hook = &delete_tty;
-
-  tty->name = xstrdup (name);
-  terminal->name = xstrdup (name);
-  tty->type = xstrdup (terminal_type);
-
-#ifdef subprocesses
-  add_keyboard_wait_descriptor (0);
-#endif
-
-  Wcm_clear (tty);
-
-#ifdef WINDOWSNT
-  {
-    struct frame *f = XFRAME (selected_frame);
-
-    FrameRows (tty) = FRAME_LINES (f);
-    FrameCols (tty) = FRAME_COLS (f);
-    tty->specified_window = FRAME_LINES (f);
-
-    FRAME_CAN_HAVE_SCROLL_BARS (f) = 0;
-    FRAME_VERTICAL_SCROLL_BAR_TYPE (f) = vertical_scroll_bar_none;
-  }
-#else  /* MSDOS */
-  {
-    int height, width;
-    get_tty_size (fileno (tty->input), &width, &height);
-    FrameCols (tty) = width;
-    FrameRows (tty) = height;
-  }
-#endif /* MSDOS */
-  tty->delete_in_insert_mode = 1;
-
-  UseTabs (tty) = 0;
-  terminal->scroll_region_ok = 0;
-
-  /* Seems to insert lines when it's not supposed to, messing up the
-     display.  In doing a trace, it didn't seem to be called much, so I
-     don't think we're losing anything by turning it off.  */
-  terminal->line_ins_del_ok = 0;
-#ifdef WINDOWSNT
-  terminal->char_ins_del_ok = 1;
-  baud_rate = 19200;
-#else  /* MSDOS */
-  terminal->char_ins_del_ok = 0;
-  init_baud_rate (fileno (tty->input));
-#endif /* MSDOS */
-
-  tty->TN_max_colors = 16;  /* Required to be non-zero for tty-display-color-p */
-
-#else  /* not DOS_NT */
-
   Wcm_clear (tty);
 
   tty->termcap_term_buffer = (char *) xmalloc (buffer_size);
@@ -3720,7 +3362,61 @@ use the Bourne shell command `TERM=... export TERM' (C-shell:\n\
   tty->TF_underscore = tgetflag ("ul");
   tty->TF_teleray = tgetflag ("xt");
 
-#endif /* !DOS_NT  */
+#else /* DOS_NT */
+#ifdef WINDOWSNT
+  {
+    struct frame *f = XFRAME (selected_frame);
+
+    initialize_w32_display (terminal);
+
+    FrameRows (tty) = FRAME_LINES (f);
+    FrameCols (tty) = FRAME_COLS (f);
+    tty->specified_window = FRAME_LINES (f);
+
+    FRAME_CAN_HAVE_SCROLL_BARS (f) = 0;
+    FRAME_VERTICAL_SCROLL_BAR_TYPE (f) = vertical_scroll_bar_none;
+    terminal->char_ins_del_ok = 1;
+    baud_rate = 19200;
+  }
+#else  /* MSDOS */
+  {
+    int height, width;
+    if (strcmp (terminal_type, "internal") == 0)
+      terminal->type = output_msdos_raw;
+    initialize_msdos_display (terminal);
+
+    get_tty_size (fileno (tty->input), &width, &height);
+    FrameCols (tty) = width;
+    FrameRows (tty) = height;
+    terminal->char_ins_del_ok = 0;
+    init_baud_rate (fileno (tty->input));
+  }
+#endif /* MSDOS */
+  tty->output = stdout;
+  tty->input = stdin;
+  /* The following two are inaccessible from w32console.c.  */
+  terminal->delete_frame_hook = &tty_free_frame_resources;
+  terminal->delete_terminal_hook = &delete_tty;
+
+  tty->name = xstrdup (name);
+  terminal->name = xstrdup (name);
+  tty->type = xstrdup (terminal_type);
+
+  add_keyboard_wait_descriptor (0);
+
+  tty->delete_in_insert_mode = 1;
+
+  UseTabs (tty) = 0;
+  terminal->scroll_region_ok = 0;
+
+  /* Seems to insert lines when it's not supposed to, messing up the
+     display.  In doing a trace, it didn't seem to be called much, so I
+     don't think we're losing anything by turning it off.  */
+  terminal->line_ins_del_ok = 0;
+
+  tty->TN_max_colors = 16;  /* Required to be non-zero for tty-display-color-p */
+#endif /* DOS_NT */
+
   terminal->kboard = (KBOARD *) xmalloc (sizeof (KBOARD));
   init_kboard (terminal->kboard);
   terminal->kboard->Vwindow_system = Qnil;
@@ -4017,9 +3713,7 @@ delete_tty (struct terminal *terminal)
 
   if (tty->input)
     {
-#ifdef subprocesses
       delete_keyboard_wait_descriptor (fileno (tty->input));
-#endif
       if (tty->input != stdin)
         fclose (tty->input);
     }
@@ -4056,7 +3750,7 @@ mark_ttys (void)
 void
 syms_of_term (void)
 {
-  DEFVAR_BOOL ("system-uses-terminfo", &system_uses_terminfo,
+  DEFVAR_BOOL ("system-uses-terminfo", system_uses_terminfo,
     doc: /* Non-nil means the system uses terminfo rather than termcap.
 This variable can be used by terminal emulator packages.  */);
 #ifdef TERMINFO
@@ -4065,20 +3759,20 @@ This variable can be used by terminal emulator packages.  */);
   system_uses_terminfo = 0;
 #endif
 
-  DEFVAR_LISP ("suspend-tty-functions", &Vsuspend_tty_functions,
+  DEFVAR_LISP ("suspend-tty-functions", Vsuspend_tty_functions,
     doc: /* Functions to be run after suspending a tty.
 The functions are run with one argument, the terminal object to be suspended.
 See `suspend-tty'.  */);
   Vsuspend_tty_functions = Qnil;
 
 
-  DEFVAR_LISP ("resume-tty-functions", &Vresume_tty_functions,
+  DEFVAR_LISP ("resume-tty-functions", Vresume_tty_functions,
     doc: /* Functions to be run after resuming a tty.
 The functions are run with one argument, the terminal object that was revived.
 See `resume-tty'.  */);
   Vresume_tty_functions = Qnil;
 
-  DEFVAR_BOOL ("visible-cursor", &visible_cursor,
+  DEFVAR_BOOL ("visible-cursor", visible_cursor,
               doc: /* Non-nil means to make the cursor very visible.
 This only has an effect when running in a text terminal.
 What means \"very visible\" is up to your terminal.  It may make the cursor
@@ -4095,8 +3789,6 @@ bigger, or it may make it blink, or it may do nothing at all.  */);
 #ifdef HAVE_GPM
   defsubr (&Sgpm_mouse_start);
   defsubr (&Sgpm_mouse_stop);
-
-  staticpro (&mouse_face_window);
 #endif /* HAVE_GPM */
 
 #ifndef DOS_NT
@@ -4109,7 +3801,3 @@ bigger, or it may make it blink, or it may do nothing at all.  */);
   encode_terminal_dst = NULL;
 }
 
-
-
-/* arch-tag: 498e7449-6f2e-45e2-91dd-b7d4ca488193
-   (do not change this comment) */