* net/tramp.el (tramp-do-file-attributes-with-stat)
[bpt/emacs.git] / src / term.c
index d092cfa..78bf77d 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
+                 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
                  Free Software Foundation, Inc.
 
 This file is part of GNU Emacs.
@@ -37,6 +37,7 @@ along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.  */
 
 #include <signal.h>
 #include <stdarg.h>
+#include <setjmp.h>
 
 #include "lisp.h"
 #include "termchar.h"
@@ -249,7 +250,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);
@@ -604,7 +604,7 @@ encode_terminal_code (src, src_len, coding)
          if (src->u.cmp.automatic)
            {
              gstring = composition_gstring_from_id (src->u.cmp.id);
-             required = src->u.cmp.to - src->u.cmp.from;
+             required = src->u.cmp.to + 1 - src->u.cmp.from;
            }
          else
            {
@@ -621,13 +621,13 @@ encode_terminal_code (src, src_len, coding)
            }
 
          if (src->u.cmp.automatic)
-           for (i = src->u.cmp.from; i < src->u.cmp.to; i++)
+           for (i = src->u.cmp.from; i <= src->u.cmp.to; i++)
              {
                Lisp_Object g = LGSTRING_GLYPH (gstring, i);
                int c = LGLYPH_CHAR (g);
 
                if (! char_charset (c, charset_list, NULL))
-                 break;
+                 c = '?';
                buf += CHAR_STRING (c, buf);
                nchars++;
              }
@@ -636,17 +636,23 @@ encode_terminal_code (src, src_len, coding)
              {
                int c = COMPOSITION_GLYPH (cmp, i);
 
-               if (! char_charset (c, charset_list, NULL))
-                 break;
+               if (c == '\t')
+                 continue;
+               if (char_charset (c, charset_list, NULL))
+                 {
+                   if (CHAR_WIDTH (c) == 0
+                       && i > 0 && COMPOSITION_GLYPH (cmp, i - 1) == '\t')
+                     /* Should be left-padded */
+                     {
+                       buf += CHAR_STRING (' ', buf);
+                       nchars++;
+                     }
+                 }
+               else
+                 c = '?';
                buf += CHAR_STRING (c, buf);
                nchars++;
              }
-         if (i == 0)
-           {
-             /* The first character of the composition is not encodable.  */
-             *buf++ = '?';
-             nchars++;
-           }
        }
       /* We must skip glyphs to be padded for a wide character.  */
       else if (! CHAR_GLYPH_PADDING_P (*src))
@@ -688,7 +694,8 @@ encode_terminal_code (src, src_len, coding)
                                                  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);
@@ -1561,8 +1568,9 @@ append_glyph (it)
    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.
+   the same time if IT->glyph_row is non-null.  For an overview, see
+   the explanation in dispextern.h, before the definition of the
+   display_element_type enumeration.
 
    produce_glyphs also stores the result of glyph width, ascent
    etc. computations in *IT.
@@ -1597,18 +1605,15 @@ produce_glyphs (it)
       goto done;
     }
 
-  /* Maybe translate single-byte characters to multibyte.  */
-  it->char_to_display = it->c;
-
-  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);
@@ -1639,32 +1644,19 @@ produce_glyphs (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))
-       {
-         it->char_to_display = unibyte_char_to_multibyte (it->c);
-         it->pixel_width = CHAR_WIDTH (it->char_to_display);
-         it->nglyphs = it->pixel_width;
-         if (it->glyph_row)
-           append_glyph (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);
-       }
+      /* 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
     {
-      it->pixel_width = CHAR_WIDTH (it->c);
+      it->pixel_width = CHAR_WIDTH (it->char_to_display);
       it->nglyphs = it->pixel_width;
 
       if (it->glyph_row)
@@ -1734,6 +1726,10 @@ produce_stretch_glyph (it)
   if (width <= 0 && (width < 0 || !zero_width_ok_p))
     width = 1;
 
+  if (width > 0 && it->line_wrap != TRUNCATE
+      && it->current_x + width > it->last_visible_x)
+    width = it->last_visible_x - it->current_x - 1;
+
   if (width > 0 && it->glyph_row)
     {
       Lisp_Object o_object = it->object;
@@ -1782,7 +1778,7 @@ append_composite_glyph (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;
+         glyph->u.cmp.to = it->cmp_it.to - 1;
        }
 
       glyph->face_id = it->face_id;
@@ -1798,8 +1794,8 @@ append_composite_glyph (it)
 
 /* Produce a composite glyph for iterator IT.  IT->cmp_id is the ID of
    the composition.  We simply produces components of the composition
-   assuming that that the terminal has a capability to layout/render
-   it correctly.  */
+   assuming that the terminal has a capability to layout/render it
+   correctly.  */
 
 static void
 produce_composite_glyph (it)
@@ -1811,8 +1807,7 @@ produce_composite_glyph (it)
     {
       struct composition *cmp = composition_table[it->cmp_it.id];
 
-      c = COMPOSITION_GLYPH (cmp, 0);
-      it->pixel_width = CHAR_WIDTH (it->c);
+      it->pixel_width = cmp->width;
     }
   else
     {
@@ -1876,7 +1871,7 @@ produce_special_glyphs (it, 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);
 
@@ -1960,14 +1955,11 @@ turn_on_face (f, face_id)
        }
     }
 
-  if (face->tty_bold_p)
-    {
-      if (MAY_USE_WITH_COLORS_P (tty, NC_BOLD))
-       OUTPUT1_IF (tty, tty->TS_enter_bold_mode);
-    }
-  else if (face->tty_dim_p)
-    if (MAY_USE_WITH_COLORS_P (tty, NC_DIM))
-      OUTPUT1_IF (tty, tty->TS_enter_dim_mode);
+  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
@@ -2088,9 +2080,9 @@ DEFUN ("tty-display-color-p", Ftty_display_color_p, Stty_display_color_p,
        0, 1, 0,
        doc: /* Return non-nil if the tty device TERMINAL can display colors.
 
-TERMINAL can be a terminal id, a frame or nil (meaning the selected
-frame's terminal).  This function always returns nil if TERMINAL
-is not on a tty device.  */)
+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)
      Lisp_Object terminal;
 {
@@ -2106,9 +2098,9 @@ DEFUN ("tty-display-color-cells", Ftty_display_color_cells,
        Stty_display_color_cells, 0, 1, 0,
        doc: /* Return the number of colors supported by the tty device TERMINAL.
 
-TERMINAL can be a terminal id, a frame or nil (meaning the selected
-frame's terminal).  This function always returns 0 if TERMINAL
-is not on a tty device.  */)
+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)
      Lisp_Object terminal;
 {
@@ -2294,8 +2286,8 @@ DEFUN ("tty-type", Ftty_type, Stty_type, 0, 1, 0,
        doc: /* Return the type of the tty device that TERMINAL uses.
 Returns nil if TERMINAL is not on a tty device.
 
-TERMINAL can be a terminal id, a frame or nil (meaning the selected
-frame's terminal).  */)
+TERMINAL can be a terminal object, a frame, or nil (meaning the
+selected frame's terminal).  */)
      (terminal)
      Lisp_Object terminal;
 {
@@ -2313,9 +2305,9 @@ frame's terminal).  */)
 DEFUN ("controlling-tty-p", Fcontrolling_tty_p, Scontrolling_tty_p, 0, 1, 0,
        doc: /* Return non-nil if TERMINAL is the controlling tty of the Emacs process.
 
-TERMINAL can be a terminal id, a frame or nil (meaning the selected
-frame's terminal).  This function always returns nil if TERMINAL
-is not on a tty device.  */)
+TERMINAL can be a terminal object, a frame, or nil (meaning the
+selected frame's terminal).  This function always returns nil if
+TERMINAL is not on a tty device.  */)
      (terminal)
      Lisp_Object terminal;
 {
@@ -2334,9 +2326,9 @@ This is used to override the terminfo data, for certain terminals that
 do not really do underlining, but say that they do.  This function has
 no effect if used on a non-tty terminal.
 
-TERMINAL can be a terminal id, a frame or nil (meaning the selected
-frame's terminal).  This function always returns nil if TERMINAL
-is not on a tty device.  */)
+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)
      Lisp_Object terminal;
 {
@@ -2357,8 +2349,8 @@ access to the tty 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 be a terminal id, a frame, or nil for the terminal device of
-the currently selected frame.
+TTY may be a terminal object, a frame, or nil for the terminal device
+of the currently selected frame.
 
 This function runs `suspend-tty-functions' after suspending the
 device.  The functions are run with one arg, the id of the suspended
@@ -2433,8 +2425,8 @@ device.
 `resume-tty' does nothing if it is called on a device that is not
 suspended.
 
-TTY may be a terminal id, a frame, or nil for the terminal device of
-the currently selected frame. */)
+TTY may be a terminal object, a frame, or nil (meaning the selected
+frame's terminal). */)
      (tty)
      Lisp_Object tty;
 {
@@ -2470,7 +2462,19 @@ the currently selected frame. */)
 #endif
 
       if (FRAMEP (t->display_info.tty->top_frame))
-        FRAME_SET_VISIBLE (XFRAME (t->display_info.tty->top_frame), 1);
+       {
+         struct frame *f = XFRAME (t->display_info.tty->top_frame);
+         int width, height;
+         int old_height = FRAME_COLS (f);
+         int old_width = FRAME_LINES (f);
+
+         /* Check if terminal/window size has changed while the frame
+            was suspended.  */
+         get_tty_size (fileno (t->display_info.tty->input), &width, &height);
+         if (width != old_width || height != old_height)
+           change_frame_size (f, height, width, 0, 0, 0);
+         FRAME_SET_VISIBLE (XFRAME (t->display_info.tty->top_frame), 1);
+       }
 
       init_sys_modes (t->display_info.tty);
 
@@ -2830,7 +2834,7 @@ term_mouse_highlight (struct frame *f, int x, int y)
 
                mouse_face_face_id
                  = face_at_buffer_position (w, pos, 0, 0,
-                                            &ignore, pos + 1, 1);
+                                            &ignore, pos + 1, 1, -1);
 
                /* Display it as active.  */
                term_show_mouse_face (DRAW_MOUSE_FACE);
@@ -2865,7 +2869,7 @@ term_mouse_highlight (struct frame *f, int x, int y)
 
                mouse_face_face_id
                  = face_at_buffer_position (w, pos, 0, 0,
-                                            &ignore, pos + 1, 1);
+                                            &ignore, pos + 1, 1, -1);
 
                /* Display it as active.  */
                term_show_mouse_face (DRAW_MOUSE_FACE);
@@ -3133,10 +3137,10 @@ Gpm-mouse can only be activated for one tty at a time.  */)
 }
 
 void
-close_gpm ()
+close_gpm (int fd)
 {
-  if (gpm_fd >= 0)
-    delete_gpm_wait_descriptor (gpm_fd);
+  if (fd >= 0)
+    delete_gpm_wait_descriptor (fd);
   while (Gpm_Close()); /* close all the stack */
   gpm_tty = NULL;
 }
@@ -3154,12 +3158,13 @@ DEFUN ("gpm-mouse-stop", Fgpm_mouse_stop, Sgpm_mouse_stop,
   if (!tty || gpm_tty != tty)
     return Qnil;       /* Not activated on this terminal, nothing to do.  */
 
-  close_gpm ();
+  close_gpm (gpm_fd);
   return Qnil;
 }
 #endif /* HAVE_GPM */
 
 \f
+#ifndef MSDOS
 /***********************************************************************
                            Initialization
  ***********************************************************************/
@@ -3197,6 +3202,20 @@ tty_free_frame_resources (struct frame *f)
   xfree (f->output_data.tty);
 }
 
+#else  /* MSDOS */
+
+/* Delete frame F's face cache. */
+
+static void
+tty_free_frame_resources (struct frame *f)
+{
+  if (! FRAME_TERMCAP_P (f) && ! FRAME_MSDOS_P (f))
+    abort ();
+
+  if (FRAME_FACE_CACHE (f))
+    free_frame_faces (f);
+}
+#endif /* MSDOS */
 \f
 /* Reset the hooks in TERMINAL.  */
 
@@ -3431,9 +3450,7 @@ init_tty (char *name, char *terminal_type, int must_succeed)
 
   tty->type = xstrdup (terminal_type);
 
-#ifdef subprocesses
   add_keyboard_wait_descriptor (fileno (tty->input));
-#endif
 
 #endif /* !DOS_NT */
 
@@ -3701,10 +3718,6 @@ to do `unset TERMCAP' (C-shell: `unsetenv TERMCAP') as well.",
                  "Screen size %dx%d is too small",
                  FrameCols (tty), FrameRows (tty));
 
-#if 0  /* This is not used anywhere. */
-  tty->terminal->min_padding_speed = tgetnum ("pb");
-#endif
-
   TabWidth (tty) = tgetnum ("tw");
 
   if (!tty->TS_bell)
@@ -3910,6 +3923,8 @@ fatal (const char *str, ...)
   va_start (ap, str);
   fprintf (stderr, "emacs: ");
   vfprintf (stderr, str, ap);
+  if (!(strlen (str) > 0 && str[strlen (str) - 1] == '\n'))
+    fprintf (stderr, "\n");
   va_end (ap);
   fflush (stderr);
   exit (1);
@@ -3923,8 +3938,6 @@ static void
 delete_tty (struct terminal *terminal)
 {
   struct tty_display_info *tty;
-  Lisp_Object tail, frame;
-  int last_terminal;
 
   /* Protect against recursive calls.  delete_frame in
      delete_terminal calls us back when it deletes our last frame.  */
@@ -3936,19 +3949,6 @@ delete_tty (struct terminal *terminal)
 
   tty = terminal->display_info.tty;
 
-  last_terminal = 1;
-  FOR_EACH_FRAME (tail, frame)
-    {
-      struct frame *f = XFRAME (frame);
-      if (FRAME_LIVE_P (f) && (!FRAME_TERMCAP_P (f) || FRAME_TTY (f) != tty))
-        {
-          last_terminal = 0;
-          break;
-        }
-    }
-  if (last_terminal)
-      error ("Attempt to delete the sole terminal device with live frames");
-
   if (tty == tty_list)
     tty_list = tty->next;
   else
@@ -3989,10 +3989,8 @@ delete_tty (struct terminal *terminal)
 
   xfree (tty->old_tty);
   xfree (tty->Wcm);
-  if (tty->termcap_strings_buffer)
-    xfree (tty->termcap_strings_buffer);
-  if (tty->termcap_term_buffer)
-    xfree (tty->termcap_term_buffer);
+  xfree (tty->termcap_strings_buffer);
+  xfree (tty->termcap_term_buffer);
 
   bzero (tty, sizeof (struct tty_display_info));
   xfree (tty);
@@ -4028,14 +4026,14 @@ This variable can be used by terminal emulator packages.  */);
 
   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 id to be suspended.
+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,
     doc: /* Functions to be run after resuming a tty.
-The functions are run with one argument, the terminal id that was revived.
+The functions are run with one argument, the terminal object that was revived.
 See `resume-tty'.  */);
   Vresume_tty_functions = Qnil;