Merge from emacs-23; up to 2010-06-12T11:17:12Z!eliz@gnu.org.
[bpt/emacs.git] / src / nsselect.m
index 9e43451..950fb1f 100644 (file)
@@ -1,5 +1,5 @@
 /* NeXT/Open/GNUstep / MacOSX Cocoa selection processing for emacs.
-   Copyright (C) 1993, 1994, 2005, 2006, 2008, 2009, 2010
+   Copyright (C) 1993-1994, 2005-2006, 2008-2011
      Free Software Foundation, Inc.
 
 This file is part of GNU Emacs.
@@ -39,10 +39,7 @@ GNUstep port and post-20 update by Adrian Robert (arobert@cogsci.ucsd.edu)
 
 Lisp_Object QCLIPBOARD, QSECONDARY, QTEXT, QFILE_NAME;
 
-static Lisp_Object Vns_sent_selection_hooks;
-static Lisp_Object Vns_lost_selection_hooks;
 static Lisp_Object Vselection_alist;
-static Lisp_Object Vselection_converter_alist;
 
 static Lisp_Object Qforeign_selection;
 
@@ -158,8 +155,10 @@ ns_string_to_pasteboard_internal (id pb, Lisp_Object str, NSString *gtype)
       CHECK_STRING (str);
 
       utfStr = SDATA (str);
-      nsStr = [NSString stringWithUTF8String: utfStr];
-
+      nsStr = [[NSString alloc] initWithBytesNoCopy: utfStr
+                                             length: SBYTES (str)
+                                           encoding: NSUTF8StringEncoding
+                                       freeWhenDone: NO];
       if (gtype == nil)
         {
           [pb declareTypes: ns_send_types owner: nil];
@@ -171,6 +170,7 @@ ns_string_to_pasteboard_internal (id pb, Lisp_Object str, NSString *gtype)
         {
           [pb setString: nsStr forType: gtype];
         }
+      [nsStr release];
     }
 }
 
@@ -308,6 +308,7 @@ ns_string_from_pasteboard (id pb)
 {
   NSString *type, *str;
   const char *utfStr;
+  int length;
 
   type = [pb availableTypeFromArray: ns_return_types];
   if (type == nil)
@@ -349,17 +350,23 @@ ns_string_from_pasteboard (id pb)
             options: NSLiteralSearch range: NSMakeRange (0, [mstr length])];
 
       utfStr = [mstr UTF8String];
-      if (!utfStr)
-        utfStr = [mstr cString];
+      length = [mstr lengthOfBytesUsingEncoding: NSUTF8StringEncoding];
+
+      if (!utfStr) 
+        {
+          utfStr = [mstr cString];
+          length = strlen (utfStr);
+        }
     }
   NS_HANDLER
     {
       message1 ("ns_string_from_pasteboard: UTF8String failed\n");
       utfStr = [str lossyCString];
+      length = strlen (utfStr);
     }
   NS_ENDHANDLER
 
-  return build_string (utfStr);
+    return make_string (utfStr, length);
 }
 
 
@@ -549,10 +556,10 @@ nxatoms_of_nsselect (void)
 void
 syms_of_nsselect (void)
 {
-  QCLIPBOARD = intern ("CLIPBOARD");   staticpro (&QCLIPBOARD);
-  QSECONDARY = intern ("SECONDARY");   staticpro (&QSECONDARY);
-  QTEXT      = intern ("TEXT");        staticpro (&QTEXT);
-  QFILE_NAME = intern ("FILE_NAME");   staticpro (&QFILE_NAME);
+  QCLIPBOARD = intern_c_string ("CLIPBOARD");  staticpro (&QCLIPBOARD);
+  QSECONDARY = intern_c_string ("SECONDARY");  staticpro (&QSECONDARY);
+  QTEXT      = intern_c_string ("TEXT");       staticpro (&QTEXT);
+  QFILE_NAME = intern_c_string ("FILE_NAME");  staticpro (&QFILE_NAME);
 
   defsubr (&Sx_disown_selection_internal);
   defsubr (&Sx_get_selection_internal);
@@ -568,7 +575,7 @@ syms_of_nsselect (void)
   Vselection_alist = Qnil;
   staticpro (&Vselection_alist);
 
-  DEFVAR_LISP ("ns-sent-selection-hooks", &Vns_sent_selection_hooks,
+  DEFVAR_LISP ("ns-sent-selection-hooks", Vns_sent_selection_hooks,
                "A list of functions to be called when Emacs answers a selection request.\n\
 The functions are called with four arguments:\n\
   - the selection name (typically `PRIMARY', `SECONDARY', or `CLIPBOARD');\n\
@@ -582,7 +589,7 @@ This hook doesn't let you change the behavior of Emacs's selection replies,\n\
 it merely informs you that they have happened.");
   Vns_sent_selection_hooks = Qnil;
 
-  DEFVAR_LISP ("selection-converter-alist", &Vselection_converter_alist,
+  DEFVAR_LISP ("selection-converter-alist", Vselection_converter_alist,
                "An alist associating X Windows selection-types with functions.\n\
 These functions are called to convert the selection, with three args:\n\
 the name of the selection (typically `PRIMARY', `SECONDARY', or `CLIPBOARD');\n\
@@ -597,7 +604,7 @@ means that a side-effect was executed,\n\
 and there is no meaningful selection value.");
   Vselection_converter_alist = Qnil;
 
-  DEFVAR_LISP ("ns-lost-selection-hooks", &Vns_lost_selection_hooks,
+  DEFVAR_LISP ("ns-lost-selection-hooks", Vns_lost_selection_hooks,
                "A list of functions to be called when Emacs loses an X selection.\n\
 \(This happens when some other X client makes its own selection\n\
 or when a Lisp program explicitly clears the selection.)\n\
@@ -605,8 +612,7 @@ The functions are called with one argument, the selection type\n\
 \(a symbol, typically `PRIMARY', `SECONDARY', or `CLIPBOARD').");
   Vns_lost_selection_hooks = Qnil;
 
-  Qforeign_selection = intern ("foreign-selection");
+  Qforeign_selection = intern_c_string ("foreign-selection");
   staticpro (&Qforeign_selection);
 }
 
-// arch-tag: 39d1dde7-06a6-49ff-95a7-0e7af12d2218