]> code.delx.au - gnu-emacs/blobdiff - src/term.c
Merged in changes from CVS trunk.
[gnu-emacs] / src / term.c
index 396a24d7e5c45de603f6e83cb41432dd09ec2091..fc60b993ea4f9439f150be1ecb8ef4280b132dd8 100644 (file)
@@ -85,6 +85,8 @@ static void turn_off_face P_ ((struct frame *, int face_id));
 static void tty_show_cursor P_ ((struct tty_display_info *));
 static void tty_hide_cursor P_ ((struct tty_display_info *));
 
+static struct display *get_tty_display (Lisp_Object display);
+
 void delete_initial_display P_ ((struct display *));
 void create_tty_output P_ ((struct frame *));
 void delete_tty_output P_ ((struct frame *));
@@ -117,9 +119,6 @@ extern Lisp_Object Qspace, QCalign_to, QCwidth;
 
 Lisp_Object Vring_bell_function;
 
-/* Functions to call after deleting a tty. */
-Lisp_Object Vdelete_tty_after_functions;
-
 /* Functions to call after suspending a tty. */
 Lisp_Object Vsuspend_tty_functions;
 
@@ -141,9 +140,6 @@ struct tty_display_info *tty_list;
    else. */
 int no_redraw_on_reenter;
 
-Lisp_Object Qframe_tty_name, Qframe_tty_type;
-
-
 
 /* Meaning of bits in no_color_video.  Each bit set means that the
    corresponding attribute cannot be combined with colors.  */
@@ -175,6 +171,9 @@ int max_frame_lines;
    should not open a frame on stdout. */
 static int no_controlling_tty;
 
+/* The first unallocated display id. */
+static int next_display_id;
+
 /* Provided for lisp packages.  */
 
 static int system_uses_terminfo;
@@ -228,10 +227,13 @@ tty_ring_bell (struct frame *f)
 {
   struct tty_display_info *tty = FRAME_TTY (f);
 
-  OUTPUT (tty, (tty->TS_visible_bell && visible_bell
-                ? tty->TS_visible_bell
-                : tty->TS_bell));
-  fflush (tty->output);
+  if (tty->output)
+    {
+      OUTPUT (tty, (tty->TS_visible_bell && visible_bell
+                    ? tty->TS_visible_bell
+                    : tty->TS_bell));
+      fflush (tty->output);
+    }
 }
 
 /* Set up termcap modes for Emacs. */
@@ -610,39 +612,43 @@ tty_clear_end_of_line (struct frame *f, int first_unused_hpos)
     }
 }
 \f
-/* Encode SRC_LEN glyphs starting at SRC to terminal output codes and
-   store them at DST.  Do not write more than DST_LEN bytes.  That may
-   require stopping before all SRC_LEN input glyphs have been
-   converted.
+/* Buffer to store the source and result of code conversion for terminal.  */
+static unsigned char *encode_terminal_buf;
+/* Allocated size of the above buffer.  */
+static int encode_terminal_bufsize;
 
-   We store the number of glyphs actually converted in *CONSUMED.  The
-   return value is the number of bytes store in DST.  */
+/* Encode SRC_LEN glyphs starting at SRC to terminal output codes.
+   Set CODING->produced to the byte-length of the resulting byte
+   sequence, and return a pointer to that byte sequence.  */
 
-int
-encode_terminal_code (coding, src, dst, src_len, dst_len, consumed)
-     struct coding_system *coding;
+unsigned char *
+encode_terminal_code (src, src_len, coding)
      struct glyph *src;
      int src_len;
-     unsigned char *dst;
-     int dst_len, *consumed;
+     struct coding_system *coding;
 {
   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];
-  const unsigned char *buf;
-  int len;
+  unsigned char *buf;
+  int nchars, nbytes, required;
   register int tlen = GLYPH_TABLE_LENGTH;
   register Lisp_Object *tbase = GLYPH_TABLE_BASE;
-  int result;
 
-  /* If the specified coding does any conversion, use it, otherwise use
-     safe_terminal_coding.  We can't use CODING_REQUIRE_ENCODING here
-     because it always returns 1 if the member src_multibyte is 1.  */
-  coding = (coding->common_flags & CODING_REQUIRE_ENCODING_MASK
-           ? coding
-           : &safe_terminal_coding);
+  /* Allocate sufficient size of buffer to store all characters in
+     multibyte-form.  But, it may be enlarged on demand if
+     Vglyph_table contains a string.  */
+  required = MAX_MULTIBYTE_LENGTH * src_len;
+  if (encode_terminal_bufsize < required)
+    {
+      if (encode_terminal_bufsize == 0)
+       encode_terminal_buf = xmalloc (required);
+      else
+       encode_terminal_buf = xrealloc (encode_terminal_buf, required);
+      encode_terminal_bufsize = required;
+    }
 
+  buf = encode_terminal_buf;
+  nchars = 0;
   while (src < src_end)
     {
       /* We must skip glyphs to be padded for a wide character.  */
@@ -653,18 +659,11 @@ encode_terminal_code (coding, src, dst, src_len, dst_len, consumed)
          if (g < 0 || g >= tlen)
            {
              /* This glyph doesn't has an entry in Vglyph_table.  */
-             if (! CHAR_VALID_P (src->u.ch, 0))
-               {
-                 len = 1;
-                 buf = " ";
-                 coding->src_multibyte = 0;
-               }
+             if (CHAR_VALID_P (src->u.ch, 0))
+               buf += CHAR_STRING (src->u.ch, buf);
              else
-               {
-                 len = CHAR_STRING (src->u.ch, workbuf);
-                 buf = workbuf;
-                 coding->src_multibyte = 1;
-               }
+               *buf++ = SPACEGLYPH;
+             nchars++;
            }
          else
            {
@@ -674,48 +673,61 @@ encode_terminal_code (coding, src, dst, src_len, dst_len, consumed)
 
              if (GLYPH_SIMPLE_P (tbase, tlen, g))
                {
-                 /* We set the multi-byte form of a character in G
-                    (that should be an ASCII character) at
-                    WORKBUF.  */
-                 workbuf[0] = FAST_GLYPH_CHAR (g);
-                 len = 1;
-                 buf = workbuf;
-                 coding->src_multibyte = 0;
+                 int c = FAST_GLYPH_CHAR (g);
+
+                 if (CHAR_VALID_P (c, 0))
+                   buf += CHAR_STRING (c, buf);
+                 else
+                   *buf++ = SPACEGLYPH;
+                 nchars++;
                }
              else
                {
                  /* We have a string in Vglyph_table.  */
-                 len = GLYPH_LENGTH (tbase, g);
-                 buf = GLYPH_STRING (tbase, g);
-                 coding->src_multibyte = STRING_MULTIBYTE (tbase[g]);
+                 Lisp_Object string;
+
+                 string = tbase[g];
+                 if (! STRING_MULTIBYTE (string))
+                   string = string_to_multibyte (string);
+                 nbytes = buf - encode_terminal_buf;
+                 if (nbytes + SBYTES (string) < encode_terminal_bufsize)
+                   {
+                     encode_terminal_bufsize = nbytes + SBYTES (string);
+                     encode_terminal_buf = xrealloc (encode_terminal_buf,
+                                                     encode_terminal_bufsize);
+                     buf = encode_terminal_buf + nbytes;
+                   }
+                 bcopy (SDATA (string), buf, SBYTES (string));
+                 buf += SBYTES (string);
+                 nchars += SCHARS (string);
                }
            }
-
-         result = encode_coding (coding, buf, dst, len, dst_end - dst);
-         len -= coding->consumed;
-         dst += coding->produced;
-         if (result == CODING_FINISH_INSUFFICIENT_DST
-             || (result == CODING_FINISH_INSUFFICIENT_SRC
-                 && len > dst_end - dst))
-           /* The remaining output buffer is too short.  We must
-              break the loop here without increasing SRC so that the
-              next call of this function starts from the same glyph.  */
-           break;
-
-         if (len > 0)
-           {
-             /* This is the case that a code of the range 0200..0237
-                exists in buf.  We must just write out such a code.  */
-             buf += coding->consumed;
-             while (len--)
-               *dst++ = *buf++;
-           }
        }
       src++;
     }
 
-  *consumed = src - src_start;
-  return (dst - dst_start);
+  nbytes = buf - encode_terminal_buf;
+  coding->src_multibyte = 1;
+  coding->dst_multibyte = 0;
+  if (SYMBOLP (coding->pre_write_conversion)
+      && ! NILP (Ffboundp (coding->pre_write_conversion)))
+    {
+      run_pre_write_conversin_on_c_str (&encode_terminal_buf, 
+                                       &encode_terminal_bufsize,
+                                       nchars, nbytes, coding);
+      nchars = coding->produced_char;
+      nbytes = coding->produced;
+    }
+  required = nbytes + encoding_buffer_size (coding, nbytes);
+  if (encode_terminal_bufsize < required)
+    {
+      encode_terminal_bufsize = required;
+      encode_terminal_buf = xrealloc (encode_terminal_buf, required);
+    }
+
+  encode_coding (coding, encode_terminal_buf, encode_terminal_buf + nbytes,
+                nbytes, encode_terminal_bufsize - nbytes);
+  return encode_terminal_buf + nbytes;
 }
 
 
@@ -734,9 +746,8 @@ write_glyphs (struct frame *f, struct glyph *string, int len)
 void
 tty_write_glyphs (struct frame *f, struct glyph *string, int len)
 {
-  int produced, consumed;
-  unsigned char conversion_buffer[1024];
-  int conversion_buffer_size = sizeof conversion_buffer;
+  unsigned char *conversion_buffer;
+  struct coding_system *coding;
 
   struct tty_display_info *tty = FRAME_TTY (f);
 
@@ -755,9 +766,14 @@ tty_write_glyphs (struct frame *f, struct glyph *string, int len)
 
   cmplus (tty, len);
 
+  /* If terminal_coding does any conversion, use it, otherwise use
+     safe_terminal_coding.  We can't use CODING_REQUIRE_ENCODING here
+     because it always return 1 if the member src_multibyte is 1.  */
+  coding = (FRAME_TERMINAL_CODING (f)->common_flags & CODING_REQUIRE_ENCODING_MASK
+           ? FRAME_TERMINAL_CODING (f) : &safe_terminal_coding);
   /* The mode bit CODING_MODE_LAST_BLOCK should be set to 1 only at
      the tail.  */
-  FRAME_TERMINAL_CODING (f)->mode &= ~CODING_MODE_LAST_BLOCK;
+  coding->mode &= ~CODING_MODE_LAST_BLOCK;
 
   while (len > 0)
     {
@@ -773,55 +789,26 @@ tty_write_glyphs (struct frame *f, struct glyph *string, int len)
       highlight_if_desired (tty);
       turn_on_face (f, face_id);
 
-      while (n > 0)
+      if (n == len)
+       /* This is the last run.  */
+       coding->mode |= CODING_MODE_LAST_BLOCK;
+      conversion_buffer = encode_terminal_code (string, n, coding);
+      if (coding->produced > 0)
        {
-         /* We use a fixed size (1024 bytes) of conversion buffer.
-            Usually it is sufficient, but if not, we just repeat the
-            loop.  */
-         produced = encode_terminal_code (FRAME_TERMINAL_CODING (f),
-                                           string, conversion_buffer,
-                                          n, conversion_buffer_size,
-                                          &consumed);
-         if (produced > 0)
-           {
-             fwrite (conversion_buffer, 1, produced,
-                      tty->output);
-             if (ferror (tty->output))
-               clearerr (tty->output);
-             if (tty->termscript)
-               fwrite (conversion_buffer, 1, produced,
-                        tty->termscript);
-           }
-         len -= consumed;
-         n -= consumed;
-         string += consumed;
+         fwrite (conversion_buffer, 1, coding->produced, tty->output);
+         if (ferror (tty->output))
+           clearerr (tty->output);
+         if (tty->termscript)
+           fwrite (conversion_buffer, 1, coding->produced, tty->termscript);
        }
+      len -= n;
+      string += n;
 
       /* Turn appearance modes off.  */
       turn_off_face (f, face_id);
       turn_off_highlight (tty);
     }
 
-  /* We may have to output some codes to terminate the writing.  */
-  if (CODING_REQUIRE_FLUSHING (FRAME_TERMINAL_CODING (f)))
-    {
-      FRAME_TERMINAL_CODING (f)->mode |= CODING_MODE_LAST_BLOCK;
-      encode_coding (FRAME_TERMINAL_CODING (f), "",
-                     conversion_buffer, 0, conversion_buffer_size);
-      if (FRAME_TERMINAL_CODING (f)->produced > 0)
-       {
-         fwrite (conversion_buffer, 1,
-                  FRAME_TERMINAL_CODING (f)->produced,
-                  tty->output);
-         if (ferror (tty->output))
-           clearerr (tty->output);
-         if (tty->termscript)
-           fwrite (conversion_buffer, 1,
-                    FRAME_TERMINAL_CODING (f)->produced,
-                   tty->termscript);
-       }
-    }
-
   cmcheckmagic (tty);
 }
 
@@ -846,6 +833,9 @@ tty_insert_glyphs (struct frame *f, struct glyph *start, int len)
 {
   char *buf;
   struct glyph *glyph = NULL;
+  unsigned char *conversion_buffer;
+  unsigned char space[1];
+  struct coding_system *coding;
 
   struct tty_display_info *tty = FRAME_TTY (f);
 
@@ -861,19 +851,26 @@ tty_insert_glyphs (struct frame *f, struct glyph *start, int len)
 
   turn_on_insert (tty);
   cmplus (tty, len);
-  /* The bit CODING_MODE_LAST_BLOCK should be set to 1 only at the tail.  */
-  FRAME_TERMINAL_CODING (f)->mode &= ~CODING_MODE_LAST_BLOCK;
+
+  if (! start)
+    space[0] = SPACEGLYPH;
+
+  /* If terminal_coding does any conversion, use it, otherwise use
+     safe_terminal_coding.  We can't use CODING_REQUIRE_ENCODING here
+     because it always return 1 if the member src_multibyte is 1.  */
+  coding = (FRAME_TERMINAL_CODING (f)->common_flags & CODING_REQUIRE_ENCODING_MASK
+           ? FRAME_TERMINAL_CODING (f) : &safe_terminal_coding);
+  /* The mode bit CODING_MODE_LAST_BLOCK should be set to 1 only at
+     the tail.  */
+  coding->mode &= ~CODING_MODE_LAST_BLOCK;
+
   while (len-- > 0)
     {
-      int produced, consumed;
-      unsigned char conversion_buffer[1024];
-      int conversion_buffer_size = sizeof conversion_buffer;
-
       OUTPUT1_IF (tty, tty->TS_ins_char);
       if (!start)
        {
-         conversion_buffer[0] = SPACEGLYPH;
-         produced = 1;
+         conversion_buffer = space;
+         coding->produced = 1;
        }
       else
        {
@@ -891,23 +888,18 @@ tty_insert_glyphs (struct frame *f, struct glyph *start, int len)
 
          if (len <= 0)
            /* This is the last glyph.  */
-           FRAME_TERMINAL_CODING (f)->mode |= CODING_MODE_LAST_BLOCK;
+           coding->mode |= CODING_MODE_LAST_BLOCK;
 
-         /* The size of conversion buffer (1024 bytes) is surely
-            sufficient for just one glyph.  */
-         produced = encode_terminal_code (glyph, conversion_buffer, 1,
-                                          conversion_buffer_size, &consumed);
+          conversion_buffer = encode_terminal_code (glyph, 1, coding);
        }
 
-      if (produced > 0)
+      if (coding->produced > 0)
        {
-         fwrite (conversion_buffer, 1, produced,
-                  tty->output);
+         fwrite (conversion_buffer, 1, coding->produced, tty->output);
          if (ferror (tty->output))
            clearerr (tty->output);
          if (tty->termscript)
-           fwrite (conversion_buffer, 1, produced,
-                    tty->termscript);
+           fwrite (conversion_buffer, 1, coding->produced, tty->termscript);
        }
 
       OUTPUT1_IF (tty, tty->TS_pad_inserted_char);
@@ -1924,58 +1916,11 @@ tty_capable_p (tty, caps, fg, bg)
   return 1;
 }
 
-/* Return the tty display object specified by DISPLAY.  DISPLAY may be
-   a frame, a string, or nil for the display device of the current
-   frame. */
-
-static struct display *
-get_tty_display (Lisp_Object display)
-{
-  struct display *d;
-
-  if (NILP (display))
-    display = selected_frame;
-
-  if (! FRAMEP (display) && ! STRINGP (display))
-    return 0;
-
-  /* The initial frame does not support colors. */
-  if (FRAMEP (display) && FRAME_INITIAL_P (XFRAME (display)))
-    return 0;
-
-  if (FRAMEP (display))
-    {
-      if (! FRAME_TERMCAP_P (XFRAME (display)))
-#if 0   /* XXX We need a predicate as the first argument; find one. */
-        wrong_type_argument ("Not a termcap frame", display);
-#else /* Until we fix the wrong_type_argument call above, simply throw
-         a dumb error. */
-      error ("DISPLAY is not a termcap frame");
-#endif  
-  
-      d = FRAME_DISPLAY (XFRAME (display));
-    }
-  else if (STRINGP (display))
-    {
-      char *name = (char *) alloca (SBYTES (display) + 1);
-      strncpy (name, SDATA (display), SBYTES (display));
-      name[SBYTES (display)] = 0;
-
-      d = get_named_tty_display (name);
-
-      if (!d)
-        error ("There is no tty display on %s", name);
-    }
-
-  return d;
-}
-
-
 /* 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 DISPLAY.  */)
+       doc: /* Return non-nil if the tty device that DISPLAY uses can display colors. */)
      (display)
      Lisp_Object display;
 {
@@ -1989,13 +1934,13 @@ DEFUN ("tty-display-color-p", Ftty_display_color_p, Stty_display_color_p,
 /* 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.  */)
+       doc: /* Return the number of colors supported by the tty device that DISPLAY uses.  */)
      (display)
      Lisp_Object display;
 {
   struct display *d = get_tty_display (display);
   if (!d)
-    return Qnil;
+    return make_number (0);
   else
     return make_number (d->display_info.tty->TN_max_colors);
 }
@@ -2139,8 +2084,65 @@ set_tty_color_mode (f, val)
 
 \f
 
-/* Return the termcap display with the given name.  If NAME is null,
-   return the display corresponding to our controlling terminal.
+/* Return the display object specified by DISPLAY.  DISPLAY may be a
+   display id, a frame, or nil for the display device of the current
+   frame. */
+
+struct display *
+get_display (Lisp_Object display)
+{
+  if (NILP (display))
+    display = selected_frame;
+
+  if (! INTEGERP (display) && ! FRAMEP (display))
+    return NULL;
+
+  if (INTEGERP (display))
+    {
+      struct display *d;
+
+      for (d = display_list; d; d = d->next_display)
+        {
+          if (d->id == XINT (display))
+            return d;
+        }
+      return NULL;
+    }
+  else if (FRAMEP (display))
+    {
+      return FRAME_DISPLAY (XFRAME (display));
+    }
+  return NULL;
+}
+
+/* Return the tty display object specified by DISPLAY. */
+
+static struct display *
+get_tty_display (Lisp_Object display)
+{
+  struct display *d = get_display (display);
+  
+  if (d && d->type == output_initial)
+    d = NULL;
+
+  if (d && d->type != output_termcap)
+    {
+#if 0   /* XXX We need a predicate as the first argument; find one. */
+      wrong_type_argument ("Not a termcap display", display);
+#else /* Until we fix the wrong_type_argument call above, simply throw
+         a dumb error. */
+      error ("DISPLAY is not a termcap display");
+#endif
+    }
+
+  return d;
+}
+
+/* Return the active termcap display that uses the tty device with the
+   given name.  If NAME is NULL, return the display corresponding to
+   our controlling terminal.
+
+   This function ignores suspended displays.
 
    Returns NULL if the named terminal device is not opened.  */
  
@@ -2154,7 +2156,8 @@ get_named_tty_display (name)
     if (d->type == output_termcap
         && ((d->display_info.tty->name == 0 && name == 0)
             || (name && d->display_info.tty->name
-                && !strcmp (d->display_info.tty->name, name))))
+                && !strcmp (d->display_info.tty->name, name)))
+        && DISPLAY_ACTIVE_P (d))
       return d;
   };
 
@@ -2163,56 +2166,58 @@ get_named_tty_display (name)
 
 \f
 
-DEFUN ("frame-tty-name", Fframe_tty_name, Sframe_tty_name, 0, 1, 0,
-       doc: /* Return the name of the TTY device that FRAME is displayed on. */)
-  (frame)
-     Lisp_Object frame;
-{
-  struct frame *f;
+DEFUN ("display-name", Fdisplay_name, Sdisplay_name, 0, 1, 0,
+       doc: /* Return the name of the device that DISPLAY uses.
+It is not guaranteed that the returned value is unique among opened displays.
 
-  if (NILP (frame))
-    {
-      f = XFRAME (selected_frame);
-    }
-  else
-    {
-      CHECK_LIVE_FRAME (frame);
-      f = XFRAME (frame);
-    }
+DISPLAY can be a display, a frame, or nil (meaning the selected
+frame's display). */)
+  (display)
+     Lisp_Object display;
+{
+  struct display *d = get_display (display);
 
-  if (f->output_method != output_termcap)
-    wrong_type_argument (Qframe_tty_name, frame);
+  if (!d)
+    wrong_type_argument (Qdisplay_live_p, display);
 
-  if (FRAME_TTY (f)->name)
-    return build_string (FRAME_TTY (f)->name);
+  if (d->name)
+    return build_string (d->name);
   else
     return Qnil;
 }
 
-DEFUN ("frame-tty-type", Fframe_tty_type, Sframe_tty_type, 0, 1, 0,
-       doc: /* Return the type of the TTY device that FRAME is displayed on. */)
-  (frame)
-     Lisp_Object frame;
+DEFUN ("display-tty-type", Fdisplay_tty_type, Sdisplay_tty_type, 0, 1, 0,
+       doc: /* Return the type of the TTY device that DISPLAY uses. */)
+  (display)
+     Lisp_Object display;
 {
-  struct frame *f;
+  struct display *d = get_display (display);
 
-  if (NILP (frame))
-    {
-      f = XFRAME (selected_frame);
-    }
+  if (!d)
+    wrong_type_argument (Qdisplay_live_p, display);
+  if (d->type != output_termcap)
+    error ("Display %d is not a termcap display", d->id);
+           
+  if (d->display_info.tty->type)
+    return build_string (d->display_info.tty->type);
   else
-    {
-      CHECK_LIVE_FRAME (frame);
-      f = XFRAME (frame);
-    }
+    return Qnil;
+}
+
+DEFUN ("display-controlling-tty-p", Fdisplay_controlling_tty_p, Sdisplay_controlling_tty_p, 0, 1, 0,
+       doc: /* Return non-nil if DISPLAY is on the controlling tty of the Emacs process. */)
+  (display)
+     Lisp_Object display;
+{
+  struct display *d = get_display (display);
 
-  if (f->output_method != output_termcap)
-    wrong_type_argument (Qframe_tty_type, frame);
+  if (!d)
+    wrong_type_argument (Qdisplay_live_p, display);
 
-  if (FRAME_TTY (f)->type)
-    return build_string (FRAME_TTY (f)->type);
-  else
+  if (d->type != output_termcap || d->display_info.tty->name)
     return Qnil;
+  else
+    return Qt;
 }
 
 \f
@@ -2231,7 +2236,8 @@ init_initial_display (void)
 
   initial_display = create_display ();
   initial_display->type = output_initial;
-  
+  initial_display->name = xstrdup ("initial_display");
+
   initial_display->delete_display_hook = &delete_initial_display;
   /* All other hooks are NULL. */
   
@@ -2255,27 +2261,30 @@ delete_initial_display (struct display *display)
 void
 dissociate_if_controlling_tty (int fd)
 {
-#if defined (USG) && !defined (BSD_PGRPS)
   int pgid;
-  EMACS_GET_TTY_PGRP (fd, &pgid);
+  EMACS_GET_TTY_PGRP (fd, &pgid); /* If tcgetpgrp succeeds, fd is the ctty. */
   if (pgid != -1)
     {
+#if defined (USG) && !defined (BSD_PGRPS)
       setpgrp ();
       no_controlling_tty = 1;
-    }
 #else
 #ifdef TIOCNOTTY                /* Try BSD ioctls. */
-  sigblock (sigmask (SIGTTOU));
-  if (ioctl (fd, TIOCNOTTY, 0) != -1)
-    {
-      no_controlling_tty = 1;
-    }
-  sigunblock (sigmask (SIGTTOU));
+      sigblock (sigmask (SIGTTOU));
+      fd = emacs_open ("/dev/tty", O_RDWR, 0);
+      if (fd != -1 && ioctl (fd, TIOCNOTTY, 0) != -1)
+        {
+          no_controlling_tty = 1;
+        }
+      if (fd != -1)
+        emacs_close (fd);
+      sigunblock (sigmask (SIGTTOU));
 #else
-  /* Unknown system. */
-  croak ();
+      /* Unknown system. */
+      croak ();
 #endif  /* ! TIOCNOTTY */
 #endif  /* ! USG */
+    }
 }
 
 /* Create a termcap display on the tty device with the given name and
@@ -2307,18 +2316,15 @@ term_init (char *name, char *terminal_type, int must_succeed)
     maybe_fatal (must_succeed, 0, 0,
                  "Unknown terminal type",
                  "Unknown terminal type");
-  
+
+  /* If we already have an active display on the given device, use that.
+     If all displays are suspended, create a new one instead.  */
+  /* XXX Perhaps this should be made explicit by having term_init
+     always create a new display and separating display and frame
+     creation on Lisp level.  */
   display = get_named_tty_display (name);
   if (display)
-    {
-      /* XXX We would be able to support multiple emacsclients from
-         the same terminal if display devices were Lisp objects.
-         (Lisp code must know the difference between two separate
-         displays on the same terminal device.) -- lorentey */
-      if (! display->display_info.tty->input)
-        error ("%s already has a suspended frame on it, can't open it twice", name);
-      return display;
-    }
+    return display;
 
   display = create_display ();
   tty = (struct tty_display_info *) xmalloc (sizeof (struct tty_display_info));
@@ -2406,6 +2412,7 @@ term_init (char *name, char *terminal_type, int must_succeed)
       
       file = fdopen (fd, "w+");
       tty->name = xstrdup (name);
+      display->name = xstrdup (name);
       tty->input = file;
       tty->output = file;
     }
@@ -2418,6 +2425,7 @@ term_init (char *name, char *terminal_type, int must_succeed)
           error ("There is no controlling terminal any more");
         }
       tty->name = 0;
+      display->name = xstrdup (ttyname (0));
       tty->input = stdin;
       tty->output = stdout;
     }
@@ -2426,6 +2434,8 @@ term_init (char *name, char *terminal_type, int must_succeed)
 
   add_keyboard_wait_descriptor (fileno (tty->input));
 
+  encode_terminal_bufsize = 0;
+
 #ifdef WINDOWSNT
   initialize_w32_display ();
 
@@ -2903,37 +2913,6 @@ fatal (str, arg1, arg2)
 
 \f
 
-DEFUN ("delete-tty", Fdelete_tty, Sdelete_tty, 0, 1, 0,
-       doc: /* Delete all frames on the terminal named TTY, and close the device.
-If omitted, TTY defaults to the controlling terminal.
-
-This function runs `delete-tty-after-functions' after closing the
-tty.  The functions are run with one arg, the frame to be deleted.  */)
-  (tty)
-     Lisp_Object tty;
-{
-  struct display *d;
-  char *name = 0;
-
-  CHECK_STRING (tty);
-
-  if (SBYTES (tty) > 0)
-    {
-      name = (char *) alloca (SBYTES (tty) + 1);
-      strncpy (name, SDATA (tty), SBYTES (tty));
-      name[SBYTES (tty)] = 0;
-    }
-
-  d = get_named_tty_display (name);
-
-  if (! d)
-    error ("No such terminal device: %s", name);
-
-  delete_tty (d);
-
-  return Qnil;
-}
-
 static int deleting_tty = 0;
 
 
@@ -3034,21 +3013,6 @@ delete_tty (struct display *display)
   bzero (tty, sizeof (struct tty_display_info));
   xfree (tty);
   deleting_tty = 0;
-
-  /* Run `delete-tty-after-functions'.  */
-  if (!NILP (Vrun_hooks))
-    {
-      Lisp_Object args[2];
-      args[0] = intern ("delete-tty-after-functions");
-      if (tty_name)
-        {
-          args[1] = build_string (tty_name);
-          xfree (tty_name);
-        }
-      else
-        args[1] = Qnil;
-      Frun_hook_with_args (2, args);
-    }
 }
 
 \f
@@ -3114,6 +3078,8 @@ create_display (void)
   display->next_display = display_list;
   display_list = display;
 
+  display->id = next_display_id++;
+
   display->keyboard_coding =
     (struct coding_system *) xmalloc (sizeof (struct coding_system));
   display->terminal_coding =
@@ -3153,26 +3119,114 @@ delete_display (struct display *display)
     xfree (display->keyboard_coding);
   if (display->terminal_coding)
     xfree (display->terminal_coding);
-
+  if (display->name)
+    xfree (display->name);
+  
   bzero (display, sizeof (struct display));
   xfree (display);
 }
 
+DEFUN ("delete-display", Fdelete_display, Sdelete_display, 0, 2, 0,
+       doc: /* Delete DISPLAY by deleting all frames on it and closing the device.
+DISPLAY may be a display id, a frame, or nil for the display
+device of the current frame.
+
+Normally, you may not delete a display if all other displays are suspended,
+but if the second argument FORCE is non-nil, you may do so. */)
+  (display, force)
+     Lisp_Object display, force;
+{
+  struct display *d, *p;
+
+  d = get_display (display);
+
+  if (!d)
+    return Qnil;
+
+  p = display_list;
+  while (p && (p == d || !DISPLAY_ACTIVE_P (p)))
+    p = p->next_display;
+  
+  if (NILP (force) && !p)
+    error ("Attempt to delete the sole active display");
+
+  if (d->delete_display_hook)
+    (*d->delete_display_hook) (d);
+  else
+    delete_display (d);
+
+  return Qnil;
+}
+
+DEFUN ("display-live-p", Fdisplay_live_p, Sdisplay_live_p, 1, 1, 0,
+       doc: /* Return non-nil if OBJECT is a display which has not been deleted.
+Value is nil if OBJECT is not a live display.
+If object is a live display, the return value indicates what sort of
+output device it uses.  See the documentation of `framep' for possible
+return values.
+
+Displays are represented by their integer identifiers. */)
+  (object)
+     Lisp_Object object;
+{
+  struct display *d;
+  
+  if (!INTEGERP (object))
+    return Qnil;
+
+  d = get_display (object);
+
+  if (!d)
+    return Qnil;
+
+  switch (d->type)
+    {
+    case output_initial: /* The initial frame is like a termcap frame. */
+    case output_termcap:
+      return Qt;
+    case output_x_window:
+      return Qx;
+    case output_w32:
+      return Qw32;
+    case output_msdos_raw:
+      return Qpc;
+    case output_mac:
+      return Qmac;
+    default:
+      abort ();
+    }
+}
+
+DEFUN ("display-list", Fdisplay_list, Sdisplay_list, 0, 0, 0,
+       doc: /* Return a list of all displays.
+Displays are represented by their integer identifiers. */)
+  ()
+{
+  Lisp_Object displays = Qnil;
+  struct display *d;
+
+  for (d = display_list; d; d = d->next_display)
+    displays = Fcons (make_number (d->id), displays);
+
+  return displays;
+}
+
+            
 \f
 
 DEFUN ("suspend-tty", Fsuspend_tty, Ssuspend_tty, 0, 1, 0,
        doc: /* Suspend the terminal device TTY.
-The terminal is restored to its default state, and Emacs closes all
+The terminal is restored to its default state, and Emacs ceases all
 access to the terminal device.  Frames that use the device are not
 deleted, but input is not read from them and if they change, their
 display is not updated.
 
-TTY may a string (a device name), a frame, or nil for the display
-device of the currently selected frame.
+TTY may be a display id, a frame, or nil for the display device of the
+currently selected frame.
 
 This function runs `suspend-tty-functions' after suspending the
-device.  The functions are run with one arg, the name of the terminal
-device.
+device.  The functions are run with one arg, the id of the suspended
+display device.
 
 `suspend-tty' does nothing if it is called on an already suspended
 device.
@@ -3211,12 +3265,7 @@ it. */)
         {
           Lisp_Object args[2];
           args[0] = intern ("suspend-tty-functions");
-          if (d->display_info.tty->name)
-            {
-              args[1] = build_string (d->display_info.tty->name);
-            }
-          else
-            args[1] = Qnil;
+          args[1] = make_number (d->id);
           Frun_hook_with_args (2, args);
         }
     }
@@ -3227,17 +3276,21 @@ it. */)
 
 DEFUN ("resume-tty", Fresume_tty, Sresume_tty, 0, 1, 0,
        doc: /* Resume the previously suspended terminal device TTY.
-The terminal is opened and reinitialized.  Frames that used the
-suspended device are revived.
+The terminal is opened and reinitialized.  Frames that are on the
+suspended display are revived.
+
+It is an error to resume a display while another display is active on
+the same device.
 
 This function runs `resume-tty-functions' after resuming the device.
-The functions are run with one arg, the name of the terminal device.
+The functions are run with one arg, the id of the resumed display
+device.
 
 `resume-tty' does nothing if it is called on a device that is not
 suspended.
 
-TTY may a string (a device name), a frame, or nil for the display
-device of the currently selected frame. */)
+TTY may be a display id, a frame, or nil for the display device of the
+currently selected frame. */)
   (tty)
      Lisp_Object tty;
 {
@@ -3249,6 +3302,9 @@ device of the currently selected frame. */)
 
   if (!d->display_info.tty->input)
     {
+      if (get_named_tty_display (d->display_info.tty->name))
+        error ("Cannot resume display while another display is active on the same device");
+
       fd = emacs_open (d->display_info.tty->name, O_RDWR | O_NOCTTY, 0);
 
       /* XXX What if open fails? */
@@ -3270,12 +3326,7 @@ device of the currently selected frame. */)
         {
           Lisp_Object args[2];
           args[0] = intern ("resume-tty-functions");
-          if (d->display_info.tty->name)
-            {
-              args[1] = build_string (d->display_info.tty->name);
-            }
-          else
-            args[1] = Qnil;
+          args[1] = make_number (d->id);
           Frun_hook_with_args (2, args);
         }
     }
@@ -3301,13 +3352,6 @@ This variable can be used by terminal emulator packages.  */);
 The function should accept no arguments.  */);
   Vring_bell_function = Qnil;
 
-  DEFVAR_LISP ("delete-tty-after-functions", &Vdelete_tty_after_functions,
-    doc: /* Functions to be run after deleting a tty.
-The functions are run with one argument, the name of the tty to be deleted.
-See `delete-tty'.  */);
-  Vdelete_tty_after_functions = Qnil;
-
-
   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 name of the tty to be suspended.
@@ -3321,17 +3365,14 @@ The functions are run with one argument, the name of the tty that was revived.
 See `resume-tty'.  */);
   Vresume_tty_functions = Qnil;
 
-  Qframe_tty_name = intern ("frame-tty-name");
-  staticpro (&Qframe_tty_name);
-
-  Qframe_tty_type = intern ("frame-tty-type");
-  staticpro (&Qframe_tty_type);
-
   defsubr (&Stty_display_color_p);
   defsubr (&Stty_display_color_cells);
-  defsubr (&Sframe_tty_name);
-  defsubr (&Sframe_tty_type);
-  defsubr (&Sdelete_tty);
+  defsubr (&Sdisplay_name);
+  defsubr (&Sdisplay_tty_type);
+  defsubr (&Sdisplay_controlling_tty_p);
+  defsubr (&Sdelete_display);
+  defsubr (&Sdisplay_live_p);
+  defsubr (&Sdisplay_list);
   defsubr (&Ssuspend_tty);
   defsubr (&Sresume_tty);