]> code.delx.au - gnu-emacs/blobdiff - src/w16select.c
(archive-l-e): New optional argument `float' means generate a float value.
[gnu-emacs] / src / w16select.c
index b1f4b3adeba746da8d71cc092607d860c69f0120..cd3098bae52a3e04b5437b372345dd0a11e954e3 100644 (file)
@@ -1,6 +1,7 @@
 /* 16-bit Windows Selection processing for emacs on MS-Windows
-   Copyright (C) 1996, 1997 Free Software Foundation.
-   
+   Copyright (C) 1996, 1997, 2001, 2002, 2003, 2004,
+                 2005, 2006 Free Software Foundation, Inc.
+
 This file is part of GNU Emacs.
 
 GNU Emacs is free software; you can redistribute it and/or modify
@@ -15,8 +16,8 @@ GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 along with GNU Emacs; see the file COPYING.  If not, write to
-the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
-Boston, MA 02111-1307, USA.  */
+the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+Boston, MA 02110-1301, USA.  */
 
 /* These functions work by using WinOldAp interface.  WinOldAp
    (WINOLDAP.MOD) is a Microsoft Windows extension supporting
@@ -40,6 +41,7 @@ Boston, MA 02111-1307, USA.  */
 #include "buffer.h"
 #include "charset.h"
 #include "coding.h"
+#include "composite.h"
 
 /* If ever some function outside this file will need to call any
    clipboard-related function, the following prototypes and constants
@@ -168,7 +170,7 @@ unsigned
 empty_clipboard ()
 {
   __dpmi_regs regs;
-  
+
   /* Calls Int 2Fh/AX=1702h
      Return Values   AX == 0: Error occurred
                        <> 0: OK, Clipboard emptied */
@@ -248,7 +250,7 @@ set_clipboard_data (Format, Data, Size, Raw)
   unsigned char *dp = Data, *dstart = dp;
 
   if (Format != CF_OEMTEXT)
-    return 0;
+    return 3;
 
   /* need to know final size after '\r' chars are inserted (the
      standard CF_OEMTEXT clipboard format uses CRLF line endings,
@@ -266,10 +268,10 @@ set_clipboard_data (Format, Data, Size, Raw)
     }
 
   if (clipboard_compact (truelen) < truelen)
-    return 0;
+    return 1;
 
   if ((xbuf_addr = alloc_xfer_buf (truelen)) == 0)
-    return 0;
+    return 1;
 
   /* Move the buffer into the low memory, convert LF into CR-LF if needed.  */
   if (Raw)
@@ -333,8 +335,8 @@ set_clipboard_data (Format, Data, Size, Raw)
   if (regs.x.ax == 0)
     *last_clipboard_text = '\0';
 
-  /* Zero means success, otherwise (1 or 2) it's an error.  */
-  return regs.x.ax > 0 ? 0 : 1;
+  /* Zero means success, otherwise (1, 2, or 3) it's an error.  */
+  return regs.x.ax > 0 ? 0 : 3;
 }
 
 /* Return the size of the clipboard data of format FORMAT.  */
@@ -369,15 +371,6 @@ get_clipboard_data (Format, Data, Size, Raw)
   __dpmi_regs regs;
   unsigned long xbuf_addr;
   unsigned char *dp = Data;
-  /* Copying text from the DOS box on Windows 95 evidently doubles the
-     size of text as reported by the clipboard.  So we must begin
-     looking for the zeroes as if the actual size were half of what's
-     reported.  Jeez, what a mess!  */
-  unsigned half_size = Size > 32 ? Size / 2 : Size;
-  /* Where we should begin looking for zeroes.  See commentary below.  */
-  unsigned char *last_block = dp + ((half_size & 0x1f)
-                                   ? (half_size & 0x20)
-                                   : half_size - 0x20);
 
   if (Format != CF_OEMTEXT)
     return 0;
@@ -408,7 +401,7 @@ get_clipboard_data (Format, Data, Size, Raw)
         the next loop by an additional test.  */
       register unsigned char *lcdp =
        last_clipboard_text == NULL ? &null_char : last_clipboard_text;
-       
+
       /* Copy data from low memory, remove CR
         characters before LF if needed.  */
       _farsetsel (_dos_ds);
@@ -424,23 +417,22 @@ get_clipboard_data (Format, Data, Size, Raw)
              dp--;
              *dp++ = '\n';
              xbuf_addr++;
-             if (last_block > dp)
-               last_block--;   /* adjust the beginning of the last 32 bytes */
              if (*lcdp == '\n')
                lcdp++;
            }
          /* Windows reportedly rounds up the size of clipboard data
-            (passed in SIZE) to a multiple of 32.  We therefore bail
-            out when we see the first null character in the last 32-byte
-            block.  */
-         else if (c == '\0' && dp > last_block)
+            (passed in SIZE) to a multiple of 32, and removes trailing
+            spaces from each line without updating SIZE.  We therefore
+            bail out when we see the first null character.  */
+         else if (c == '\0')
            break;
        }
 
       /* If the text in clipboard is identical to what we put there
         last time set_clipboard_data was called, pretend there's no
         data in the clipboard.  This is so we don't pass our own text
-        from the clipboard.  */
+        from the clipboard (which might be troublesome if the killed
+        text includes null characters).  */
       if (last_clipboard_text &&
          xbuf_addr - xbuf_beg == (long)(lcdp - last_clipboard_text))
        dp = (unsigned char *)Data + 1;
@@ -487,43 +479,38 @@ static char no_mem_msg[] =
   "(Not enough DOS memory to put saved text into clipboard.)";
 static char binary_msg[] =
   "(Binary characters in saved text; clipboard data not set.)";
+static char system_error_msg[] =
+  "(Clipboard interface failure; clipboard data not set.)";
 
 DEFUN ("w16-set-clipboard-data", Fw16_set_clipboard_data, Sw16_set_clipboard_data, 1, 2, 0,
-       "This sets the clipboard data to the given text.")
-    (string, frame)
-    Lisp_Object string, frame;
+       doc: /* This sets the clipboard data to the given text.  */)
+     (string, frame)
+     Lisp_Object string, frame;
 {
   unsigned ok = 1, put_status = 0;
-  int nbytes;
+  int nbytes, charset_info, no_crlf_conversion;
   unsigned char *src, *dst = NULL;
-  int charsets[MAX_CHARSET + 1];
-  int num;
-  int no_crlf_conversion;
 
-  CHECK_STRING (string, 0);
-  
+  CHECK_STRING (string);
+
   if (NILP (frame))
     frame = Fselected_frame ();
 
-  CHECK_LIVE_FRAME (frame, 0);
+  CHECK_LIVE_FRAME (frame);
   if ( !FRAME_MSDOS_P (XFRAME (frame)))
     goto done;
-  
+
   BLOCK_INPUT;
 
-  nbytes = STRING_BYTES (XSTRING (string));
-  src = XSTRING (string)->data;
+  nbytes = SBYTES (string);
+  src = SDATA (string);
 
   /* Since we are now handling multilingual text, we must consider
      encoding text for the clipboard.  */
-  bzero (charsets, (MAX_CHARSET + 1) * sizeof (int));
-  num = ((nbytes <= 1  /* Check the possibility of short cut.  */
-         || !STRING_MULTIBYTE (string)
-         || nbytes == XSTRING (string)->size)
-        ? 0
-        : find_charset_in_str (src, nbytes, charsets, Qnil, 0, 1));
-
-  if (!num || (num == 1 && charsets[CHARSET_ASCII]))
+  charset_info = find_charset_in_text (src, SCHARS (string), nbytes,
+                                      NULL, Qnil);
+
+  if (charset_info == 0)
     {
       /* No multibyte character in OBJ.  We need not encode it, but we
         will have to convert it to DOS CR-LF style.  */
@@ -541,6 +528,15 @@ DEFUN ("w16-set-clipboard-data", Fw16_set_clipboard_data, Sw16_set_clipboard_dat
        Vnext_selection_coding_system = Vselection_coding_system;
       setup_coding_system
        (Fcheck_coding_system (Vnext_selection_coding_system), &coding);
+      if (SYMBOLP (coding.pre_write_conversion)
+         && !NILP (Ffboundp (coding.pre_write_conversion)))
+       {
+         string = run_pre_post_conversion_on_str (string, &coding, 1);
+         src = SDATA (string);
+         nbytes = SBYTES (string);
+       }
+      coding.src_multibyte = 1;
+      coding.dst_multibyte = 0;
       Vnext_selection_coding_system = Qnil;
       coding.mode |= CODING_MODE_LAST_BLOCK;
       Vlast_coding_system_used = coding.symbol;
@@ -554,7 +550,7 @@ DEFUN ("w16-set-clipboard-data", Fw16_set_clipboard_data, Sw16_set_clipboard_dat
 
   if (!open_clipboard ())
     goto error;
-  
+
   ok = empty_clipboard ()
     && ((put_status
         = set_clipboard_data (CF_OEMTEXT, src, nbytes, no_crlf_conversion))
@@ -563,11 +559,11 @@ DEFUN ("w16-set-clipboard-data", Fw16_set_clipboard_data, Sw16_set_clipboard_dat
   if (!no_crlf_conversion)
     Vlast_coding_system_used = Qraw_text;
   close_clipboard ();
-  
+
   if (ok) goto unblock;
 
  error:
-  
+
   ok = 0;
 
  unblock:
@@ -590,35 +586,37 @@ DEFUN ("w16-set-clipboard-data", Fw16_set_clipboard_data, Sw16_set_clipboard_dat
          case 2:
            message2 (binary_msg, sizeof (binary_msg) - 1, 0);
            break;
+         case 3:
+           message2 (system_error_msg, sizeof (system_error_msg) - 1, 0);
+           break;
        }
       sit_for (2, 0, 0, 1, 1);
     }
-  
+
  done:
 
   return (ok && put_status == 0 ? string : Qnil);
 }
 
 DEFUN ("w16-get-clipboard-data", Fw16_get_clipboard_data, Sw16_get_clipboard_data, 0, 1, 0,
-       "This gets the clipboard data in text format.")
+       doc: /* This gets the clipboard data in text format.  */)
      (frame)
      Lisp_Object frame;
 {
   unsigned data_size, truelen;
   unsigned char *htext;
   Lisp_Object ret = Qnil;
-  int no_crlf_conversion;
-  int require_encoding = 0;
+  int no_crlf_conversion, require_encoding = 0;
 
   if (NILP (frame))
     frame = Fselected_frame ();
 
-  CHECK_LIVE_FRAME (frame, 0);
+  CHECK_LIVE_FRAME (frame);
   if ( !FRAME_MSDOS_P (XFRAME (frame)))
     goto done;
-  
+
   BLOCK_INPUT;
-  
+
   if (!open_clipboard ())
     goto unblock;
 
@@ -633,27 +631,20 @@ DEFUN ("w16-get-clipboard-data", Fw16_get_clipboard_data, Sw16_get_clipboard_dat
     goto closeclip;
 
   /* Do we need to decode it?  */
-  if (
-#if 1
-      1
-#else
-      ! NILP (buffer_defaults.enable_multibyte_characters)
-#endif
-      )
-    {
-      /* If the clipboard data contains any 8-bit Latin-1 code, we
-        need to decode it.  */
-      int i;
-
-      for (i = 0; i < truelen; i++)
-       {
-         if (htext[i] >= 0x80)
-           {
-             require_encoding = 1;
-             break;
-           }
-       }
-    }
+  {
+    /* If the clipboard data contains any 8-bit Latin-1 code, we
+       need to decode it.  */
+    int i;
+
+    for (i = 0; i < truelen; i++)
+      {
+       if (htext[i] >= 0x80)
+         {
+           require_encoding = 1;
+           break;
+         }
+      }
+  }
   if (require_encoding)
     {
       int bufsize;
@@ -664,17 +655,23 @@ DEFUN ("w16-get-clipboard-data", Fw16_get_clipboard_data, Sw16_get_clipboard_dat
        Vnext_selection_coding_system = Vselection_coding_system;
       setup_coding_system
        (Fcheck_coding_system (Vnext_selection_coding_system), &coding);
+      coding.src_multibyte = 0;
+      coding.dst_multibyte = 1;
       Vnext_selection_coding_system = Qnil;
       coding.mode |= CODING_MODE_LAST_BLOCK;
+      /* We explicitely disable composition handling because selection
+        data should not contain any composition sequence.  */
+      coding.composing = COMPOSITION_DISABLED;
       truelen = get_clipboard_data (CF_OEMTEXT, htext, data_size, 1);
       bufsize = decoding_buffer_size (&coding, truelen);
       buf = (unsigned char *) xmalloc (bufsize);
       decode_coding (&coding, htext, buf, truelen, bufsize);
-      truelen = (coding.fake_multibyte
-                ? multibyte_chars_in_text (buf, coding.produced)
-                : coding.produced_char);
-      ret = make_string_from_bytes ((char *) buf, truelen, coding.produced);
+      ret = make_string_from_bytes ((char *) buf,
+                                   coding.produced_char, coding.produced);
       xfree (buf);
+      if (SYMBOLP (coding.post_read_conversion)
+         && !NILP (Ffboundp (coding.post_read_conversion)))
+       ret = run_pre_post_conversion_on_str (ret, &coding, 0);
       Vlast_coding_system_used = coding.symbol;
     }
   else
@@ -690,26 +687,26 @@ DEFUN ("w16-get-clipboard-data", Fw16_get_clipboard_data, Sw16_get_clipboard_dat
 
  unblock:
   UNBLOCK_INPUT;
-  
+
  done:
-  
+
   return (ret);
 }
 
 /* Support checking for a clipboard selection. */
 
 DEFUN ("x-selection-exists-p", Fx_selection_exists_p, Sx_selection_exists_p,
-  0, 1, 0,
-  "Whether there is an owner for the given X Selection.\n\
-The arg should be the name of the selection in question, typically one of\n\
-the symbols `PRIMARY', `SECONDARY', or `CLIPBOARD'.\n\
-\(Those are literal upper-case symbol names, since that's what X expects.)\n\
-For convenience, the symbol nil is the same as `PRIMARY',\n\
-and t is the same as `SECONDARY'.")
-  (selection)
+       0, 1, 0,
+       doc: /* Whether there is an owner for the given X Selection.
+The arg should be the name of the selection in question, typically one of
+the symbols `PRIMARY', `SECONDARY', or `CLIPBOARD'.
+\(Those are literal upper-case symbol names, since that's what X expects.)
+For convenience, the symbol nil is the same as `PRIMARY',
+and t is the same as `SECONDARY'.  */)
+     (selection)
      Lisp_Object selection;
 {
-  CHECK_SYMBOL (selection, 0);
+  CHECK_SYMBOL (selection);
 
   /* Return nil for SECONDARY selection.  For PRIMARY (or nil)
      selection, check if there is some text on the kill-ring;
@@ -722,8 +719,8 @@ and t is the same as `SECONDARY'.")
      into the clipboard if we run under Windows, so we cannot check
      the clipboard alone.)  */
   if ((EQ (selection, Qnil) || EQ (selection, QPRIMARY))
-      && ! NILP (XSYMBOL (Fintern_soft (build_string ("kill-ring"),
-                                       Qnil))->value))
+      && ! NILP (SYMBOL_VALUE (Fintern_soft (build_string ("kill-ring"),
+                                            Qnil))))
     return Qt;
 
   if (EQ (selection, QCLIPBOARD))
@@ -741,7 +738,7 @@ and t is the same as `SECONDARY'.")
   return Qnil;
 }
 
-void 
+void
 syms_of_win16select ()
 {
   defsubr (&Sw16_set_clipboard_data);
@@ -749,18 +746,18 @@ syms_of_win16select ()
   defsubr (&Sx_selection_exists_p);
 
   DEFVAR_LISP ("selection-coding-system", &Vselection_coding_system,
-    "Coding system for communicating with other X clients.\n\
-When sending or receiving text via cut_buffer, selection, and clipboard,\n\
-the text is encoded or decoded by this coding system.\n\
-A default value is `iso-latin-1-dos'");
-  Vselection_coding_system=intern ("iso-latin-1-dos");
+              doc: /* Coding system for communicating with other X clients.
+When sending or receiving text via cut_buffer, selection, and clipboard,
+the text is encoded or decoded by this coding system.
+The default value is `iso-latin-1-dos'.  */);
+  Vselection_coding_system = intern ("iso-latin-1-dos");
 
   DEFVAR_LISP ("next-selection-coding-system", &Vnext_selection_coding_system,
-    "Coding system for the next communication with other X clients.\n\
-Usually, `selection-coding-system' is used for communicating with\n\
-other X clients.   But, if this variable is set, it is used for the\n\
-next communication only.   After the communication, this variable is\n\
-set to nil.");
+              doc: /* Coding system for the next communication with other X clients.
+Usually, `selection-coding-system' is used for communicating with
+other X clients.  But, if this variable is set, it is used for the
+next communication only.  After the communication, this variable is
+set to nil.  */);
   Vnext_selection_coding_system = Qnil;
 
   QPRIMARY   = intern ("PRIMARY");     staticpro (&QPRIMARY);
@@ -768,3 +765,6 @@ set to nil.");
 }
 
 #endif /* MSDOS */
+
+/* arch-tag: 085a22c8-7324-436e-a6da-102464ce95d8
+   (do not change this comment) */