XEmacs 21.2.13
[chise/xemacs-chise.git.1] / src / select-msw.c
1 /* mswindows selection processing for XEmacs
2    Copyright (C) 1990, 1991, 1992, 1993, 1994 Free Software Foundation, Inc.
3
4 This file is part of XEmacs.
5
6 XEmacs is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 2, or (at your option) any
9 later version.
10
11 XEmacs is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with XEmacs; see the file COPYING.  If not, write to
18 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA.  */
20
21 /* Synched up with: Not synched with FSF. */
22
23 /* Authorship:
24
25    Written by Kevin Gallo for FSF Emacs.
26    Rewritten for mswindows by Jonathan Harris, December 1997 for 21.0.
27  */
28
29
30 #include <config.h>
31 #include "lisp.h"
32
33 #include "console-msw.h"
34
35 DEFUN ("mswindows-set-clipboard", Fmswindows_set_clipboard, 1, 1, 0, /*
36 Copy STRING to the mswindows clipboard.
37 */
38        (string))
39 {
40   int rawsize, size, i;
41   unsigned char *src, *dst, *next;
42   HGLOBAL h = NULL;
43
44   CHECK_STRING (string);
45
46   /* Calculate size with LFs converted to CRLFs because
47    * CF_TEXT format uses CRLF delimited ASCIIZ */
48   src = XSTRING_DATA (string);
49   size = rawsize = XSTRING_LENGTH (string) + 1;
50   for (i=0; i<rawsize; i++)
51     if (src[i] == '\n')
52       size++;
53
54   if (!OpenClipboard (NULL))
55     return Qnil;
56
57   if (!EmptyClipboard () ||
58       (h = GlobalAlloc (GMEM_MOVEABLE | GMEM_DDESHARE, size)) == NULL ||
59       (dst = (unsigned char *) GlobalLock (h)) == NULL)
60     {
61       if (h != NULL) GlobalFree (h);
62       CloseClipboard ();
63       return Qnil;
64     }
65     
66   /* Convert LFs to CRLFs */
67   do
68     {
69       /* copy next line or remaining bytes including '\0' */
70       next = memccpy (dst, src, '\n', rawsize);
71       if (next)
72         {
73           /* copied one line ending with '\n' */
74           int copied = next - dst;
75           rawsize -= copied;
76           src += copied;
77           /* insert '\r' before '\n' */
78           next[-1] = '\r';
79           next[0] = '\n';
80           dst = next+1;
81         }           
82     }
83   while (next);
84     
85   GlobalUnlock (h);
86   
87   i = (SetClipboardData (CF_TEXT, h) != NULL);
88   
89   CloseClipboard ();
90   GlobalFree (h);
91   
92   return i ? Qt : Qnil;
93 }
94
95 DEFUN ("mswindows-get-clipboard", Fmswindows_get_clipboard, 0, 0, 0, /*
96 Return the contents of the mswindows clipboard.
97 */
98        ())
99 {
100   HANDLE h;
101   unsigned char *src, *dst, *next;
102   Lisp_Object ret = Qnil;
103
104   if (!OpenClipboard (NULL))
105     return Qnil;
106
107   if ((h = GetClipboardData (CF_TEXT)) != NULL &&
108       (src = (unsigned char *) GlobalLock (h)) != NULL)
109     {
110       int i;
111       int size, rawsize;
112       size = rawsize = strlen (src);
113
114       for (i=0; i<rawsize; i++)
115         if (src[i] == '\r' && src[i+1] == '\n')
116           size--;
117
118       /* Convert CRLFs to LFs */
119       ret = make_uninit_string (size);
120       dst = XSTRING_DATA (ret);
121       do
122         {
123           /* copy next line or remaining bytes excluding '\0' */
124           next = memccpy (dst, src, '\r', rawsize);
125           if (next)
126             {
127               /* copied one line ending with '\r' */
128               int copied = next - dst;
129               rawsize -= copied;
130               src += copied;
131               if (*src == '\n')
132                 dst += copied - 1;              /* overwrite '\r' */
133               else
134                 dst += copied;
135             }       
136         }
137       while (next);
138
139       GlobalUnlock (h);
140     }
141
142   CloseClipboard ();
143
144   return ret;
145 }
146
147 DEFUN ("mswindows-selection-exists-p", Fmswindows_selection_exists_p, 0, 0, 0, /*
148 Whether there is an MS-Windows selection.
149 */
150        ())
151 {
152   return IsClipboardFormatAvailable (CF_TEXT) ? Qt : Qnil;
153 }
154
155 DEFUN ("mswindows-delete-selection", Fmswindows_delete_selection, 0, 0, 0, /*
156 Remove the current MS-Windows selection from the clipboard.
157 */
158        ())
159 {
160   return EmptyClipboard () ? Qt : Qnil;
161 }
162
163 \f
164 /************************************************************************/
165 /*                            initialization                            */
166 /************************************************************************/
167
168 void
169 syms_of_select_mswindows (void)
170 {
171   DEFSUBR (Fmswindows_set_clipboard);
172   DEFSUBR (Fmswindows_get_clipboard);
173   DEFSUBR (Fmswindows_selection_exists_p);
174   DEFSUBR (Fmswindows_delete_selection);
175 }
176
177 void
178 vars_of_select_mswindows (void)
179 {
180 }