(scan_sexps_forward): Prefer 2-char comment-starter over a 1-char one.
[bpt/emacs.git] / src / w32select.c
CommitLineData
e9e23e23 1/* Selection processing for Emacs on the Microsoft W32 API.
ee78dc32 2 Copyright (C) 1993, 1994 Free Software Foundation.
41a48e45 3
3b7ad313
EN
4This file is part of GNU Emacs.
5
6GNU Emacs is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation; either version 2, or (at your option)
9any later version.
10
11GNU Emacs is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with GNU Emacs; see the file COPYING. If not, write to
18the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19Boston, MA 02111-1307, USA. */
ee78dc32
GV
20
21/* Written by Kevin Gallo */
22
23#include <config.h>
24#include "lisp.h"
fbd6baed 25#include "w32term.h" /* for all of the w32 includes */
ee78dc32 26#include "dispextern.h" /* frame.h seems to want this */
3e4731a3 27#include "keyboard.h"
ee78dc32
GV
28#include "frame.h" /* Need this to get the X window of selected_frame */
29#include "blockinput.h"
bbb059f3
AI
30#include "buffer.h"
31#include "charset.h"
32#include "coding.h"
f7d05dc4 33#include "composite.h"
ee78dc32 34
9aa94bd5
KH
35Lisp_Object QCLIPBOARD;
36
bbb059f3
AI
37/* Coding system for communicating with other Windows programs via the
38 clipboard. */
72aca5fa 39static Lisp_Object Vselection_coding_system;
bbb059f3 40
06466d9a 41/* Coding system for the next communicating with other Windows programs. */
93cbf229
GV
42static Lisp_Object Vnext_selection_coding_system;
43
ee79d1aa
JR
44/* Sequence number, used where possible to detect when we are pasting
45 our own text. */
46static DWORD last_clipboard_sequence_number;
47extern ClipboardSequence_Proc clipboard_sequence_fn;
48
49/* The last text we put into the clipboard. This is used when the OS
50 does not support sequence numbers (NT4, 95). It is undesirable to
51 use data put on the clipboard by Emacs because the clipboard data
06466d9a
JR
52 could be MULEtilated by inappropriately chosen
53 (next-)selection-coding-system. For this reason, we must store the
54 text *after* it was encoded/Unix-to-DOS-converted. */
55static unsigned char *last_clipboard_text = NULL;
56static size_t clipboard_storage_size = 0;
57
ee78dc32 58#if 0
fbd6baed 59DEFUN ("w32-open-clipboard", Fw32_open_clipboard, Sw32_open_clipboard, 0, 1, 0,
33f09670 60 doc: /* This opens the clipboard with the given frame pointer. */)
ee78dc32
GV
61 (frame)
62 Lisp_Object frame;
63{
64 BOOL ok = FALSE;
41a48e45 65
ee78dc32 66 if (!NILP (frame))
b7826503 67 CHECK_LIVE_FRAME (frame);
41a48e45 68
ee78dc32 69 BLOCK_INPUT;
41a48e45 70
fbd6baed 71 ok = OpenClipboard ((!NILP (frame) && FRAME_W32_P (XFRAME (frame))) ? FRAME_W32_WINDOW (XFRAME (frame)) : NULL);
41a48e45 72
ee78dc32 73 UNBLOCK_INPUT;
41a48e45 74
ee78dc32
GV
75 return (ok ? frame : Qnil);
76}
77
33f09670
JR
78DEFUN ("w32-empty-clipboard", Fw32_empty_clipboard,
79 Sw32_empty_clipboard, 0, 0, 0,
80 doc: /* Empty the clipboard.
81Assigns ownership of the clipboard to the window which opened it. */)
ee78dc32
GV
82 ()
83{
84 BOOL ok = FALSE;
41a48e45 85
ee78dc32 86 BLOCK_INPUT;
41a48e45 87
ee78dc32 88 ok = EmptyClipboard ();
41a48e45 89
ee78dc32 90 UNBLOCK_INPUT;
41a48e45 91
ee78dc32
GV
92 return (ok ? Qt : Qnil);
93}
94
33f09670
JR
95DEFUN ("w32-close-clipboard", Fw32_close_clipboard,
96 Sw32_close_clipboard, 0, 0, 0,
97 doc: /* Close the clipboard. */)
ee78dc32
GV
98 ()
99{
100 BOOL ok = FALSE;
41a48e45 101
ee78dc32 102 BLOCK_INPUT;
41a48e45 103
ee78dc32 104 ok = CloseClipboard ();
41a48e45 105
ee78dc32 106 UNBLOCK_INPUT;
41a48e45 107
ee78dc32
GV
108 return (ok ? Qt : Qnil);
109}
110
111#endif
112
33f09670
JR
113DEFUN ("w32-set-clipboard-data", Fw32_set_clipboard_data,
114 Sw32_set_clipboard_data, 1, 2, 0,
115 doc: /* This sets the clipboard data to the given text. */)
ee78dc32
GV
116 (string, frame)
117 Lisp_Object string, frame;
118{
119 BOOL ok = TRUE;
120 HANDLE htext;
69cddef0 121 int nbytes;
0ece9ef6 122 int truelen, nlines = 0;
69cddef0
GV
123 unsigned char *src;
124 unsigned char *dst;
0ece9ef6 125
b7826503 126 CHECK_STRING (string);
41a48e45 127
ee78dc32 128 if (!NILP (frame))
b7826503 129 CHECK_LIVE_FRAME (frame);
41a48e45 130
ee78dc32 131 BLOCK_INPUT;
69cddef0 132
4121d612
KH
133 /* Include the terminating NULL character in the source of
134 conversion. */
d5db4077
KR
135 nbytes = SBYTES (string) + 1;
136 src = SDATA (string);
0ece9ef6
AI
137 dst = src;
138
139 /* We need to know how many lines there are, since we need CRLF line
140 termination for compatibility with other Windows Programs.
141 avoid using strchr because it recomputes the length every time */
142 while ((dst = memchr (dst, '\n', nbytes - (dst - src))) != NULL)
143 {
144 nlines++;
145 dst++;
146 }
69cddef0 147
bbb059f3
AI
148 {
149 /* Since we are now handling multilingual text, we must consider
150 encoding text for the clipboard. */
d5db4077 151 int charset_info = find_charset_in_text (src, SCHARS (string),
11f292ed 152 nbytes, NULL, Qnil);
bbb059f3 153
69ebbf81 154 if (charset_info == 0)
bbb059f3
AI
155 {
156 /* No multibyte character in OBJ. We need not encode it. */
69cddef0 157
c0ca703b 158 /* Need to know final size after CR chars are inserted (the
bbb059f3 159 standard CF_TEXT clipboard format uses CRLF line endings,
c0ca703b 160 while Emacs uses just LF internally). */
69cddef0 161
0ece9ef6 162 truelen = nbytes + nlines;
69cddef0 163
bbb059f3
AI
164 if ((htext = GlobalAlloc (GMEM_MOVEABLE | GMEM_DDESHARE, truelen)) == NULL)
165 goto error;
166
167 if ((dst = (unsigned char *) GlobalLock (htext)) == NULL)
168 goto error;
41a48e45 169
bbb059f3
AI
170 /* convert to CRLF line endings expected by clipboard */
171 while (1)
172 {
173 unsigned char *next;
174 /* copy next line or remaining bytes including '\0' */
175 next = _memccpy (dst, src, '\n', nbytes);
176 if (next)
177 {
178 /* copied one line ending with '\n' */
179 int copied = next - dst;
180 nbytes -= copied;
181 src += copied;
182 /* insert '\r' before '\n' */
183 next[-1] = '\r';
184 next[0] = '\n';
185 dst = next + 1;
41a48e45 186 }
bbb059f3
AI
187 else
188 /* copied remaining partial line -> now finished */
189 break;
190 }
41a48e45 191
bbb059f3 192 GlobalUnlock (htext);
0108f679
AI
193
194 Vlast_coding_system_used = Qraw_text;
bbb059f3
AI
195 }
196 else
197 {
06466d9a
JR
198 /* We must encode contents of OBJ to the selection coding
199 system. */
bbb059f3
AI
200 int bufsize;
201 struct coding_system coding;
202 HANDLE htext2;
203
93cbf229
GV
204 if (NILP (Vnext_selection_coding_system))
205 Vnext_selection_coding_system = Vselection_coding_system;
bbb059f3 206 setup_coding_system
93cbf229 207 (Fcheck_coding_system (Vnext_selection_coding_system), &coding);
aab7e392
JR
208 if (SYMBOLP (coding.pre_write_conversion)
209 && !NILP (Ffboundp (coding.pre_write_conversion)))
210 {
211 string = run_pre_post_conversion_on_str (string, &coding, 1);
d5db4077 212 src = SDATA (string);
4121d612
KH
213 /* Include the terminating NULL character in the source of
214 conversion. */
215 nbytes = SBYTES (string) + 1;
aab7e392 216 }
69ebbf81
KH
217 coding.src_multibyte = 1;
218 coding.dst_multibyte = 0;
2883d842
JR
219 /* Need to set COMPOSITION_DISABLED, otherwise Emacs crashes in
220 encode_coding_iso2022 trying to dereference a null pointer. */
221 coding.composing = COMPOSITION_DISABLED;
222 if (coding.type == coding_type_iso2022)
223 coding.flags |= CODING_FLAG_ISO_SAFE;
93cbf229 224 Vnext_selection_coding_system = Qnil;
bbb059f3 225 coding.mode |= CODING_MODE_LAST_BLOCK;
43df7d0b 226 bufsize = encoding_buffer_size (&coding, nbytes);
bbb059f3
AI
227 if ((htext = GlobalAlloc (GMEM_MOVEABLE | GMEM_DDESHARE, bufsize)) == NULL)
228 goto error;
229 if ((dst = (unsigned char *) GlobalLock (htext)) == NULL)
230 goto error;
231 encode_coding (&coding, src, dst, nbytes, bufsize);
0108f679 232 Vlast_coding_system_used = coding.symbol;
06466d9a 233
ee79d1aa
JR
234 /* If clipboard sequence numbers are not supported, keep a copy for
235 later comparison. */
236 if (!clipboard_sequence_fn)
237 {
238 /* Stash away the data we are about to put into the
239 clipboard, so we could later check inside
240 Fw32_get_clipboard_data whether the clipboard still
241 holds our data. */
242 if (clipboard_storage_size < coding.produced)
243 {
244 clipboard_storage_size = coding.produced + 100;
245 last_clipboard_text = (char *) xrealloc (last_clipboard_text,
246 clipboard_storage_size);
247 }
248 if (last_clipboard_text)
249 memcpy (last_clipboard_text, dst, coding.produced);
250 }
06466d9a 251
bbb059f3 252 GlobalUnlock (htext);
06466d9a 253
bbb059f3 254 /* Shrink data block to actual size. */
06466d9a
JR
255 htext2 = GlobalReAlloc (htext, coding.produced,
256 GMEM_MOVEABLE | GMEM_DDESHARE);
bbb059f3
AI
257 if (htext2 != NULL) htext = htext2;
258 }
259 }
41a48e45 260
fbd6baed 261 if (!OpenClipboard ((!NILP (frame) && FRAME_W32_P (XFRAME (frame))) ? FRAME_W32_WINDOW (XFRAME (frame)) : NULL))
ee78dc32 262 goto error;
06466d9a 263
ee78dc32 264 ok = EmptyClipboard () && SetClipboardData (CF_TEXT, htext);
41a48e45 265
6383ca22
JR
266 CloseClipboard ();
267
268 /* Common sense says to read the sequence number inside the
269 OpenClipboard/ CloseClipboard block to avoid race conditions
270 where another app puts something on the clipboard straight after
271 us. But experience suggests that the sequence number from the
272 SetClipboardData is not allocated until we close the clipboard!
273 Since clipboard operations are normally user-driven, the race
274 condition is probably not going to really happen. */
ee79d1aa
JR
275 if (clipboard_sequence_fn)
276 last_clipboard_sequence_number = clipboard_sequence_fn ();
277
ee78dc32
GV
278 if (ok) goto done;
279
280 error:
41a48e45 281
ee78dc32
GV
282 ok = FALSE;
283 if (htext) GlobalFree (htext);
06466d9a
JR
284 if (last_clipboard_text)
285 *last_clipboard_text = '\0';
286
ee79d1aa
JR
287 last_clipboard_sequence_number = 0;
288
ee78dc32
GV
289 done:
290 UNBLOCK_INPUT;
41a48e45 291
ee78dc32
GV
292 return (ok ? string : Qnil);
293}
294
33f09670
JR
295DEFUN ("w32-get-clipboard-data", Fw32_get_clipboard_data,
296 Sw32_get_clipboard_data, 0, 1, 0,
297 doc: /* This gets the clipboard data in text format. */)
ee78dc32
GV
298 (frame)
299 Lisp_Object frame;
300{
301 HANDLE htext;
302 Lisp_Object ret = Qnil;
41a48e45 303
ee78dc32 304 if (!NILP (frame))
b7826503 305 CHECK_LIVE_FRAME (frame);
41a48e45 306
ee78dc32 307 BLOCK_INPUT;
41a48e45 308
fbd6baed 309 if (!OpenClipboard ((!NILP (frame) && FRAME_W32_P (XFRAME (frame))) ? FRAME_W32_WINDOW (XFRAME (frame)) : NULL))
ee78dc32 310 goto done;
41a48e45 311
ee78dc32
GV
312 if ((htext = GetClipboardData (CF_TEXT)) == NULL)
313 goto closeclip;
314
ee78dc32 315 {
69cddef0
GV
316 unsigned char *src;
317 unsigned char *dst;
ee78dc32 318 int nbytes;
69cddef0 319 int truelen;
c0ca703b 320 int require_decoding = 0;
41a48e45 321
69cddef0 322 if ((src = (unsigned char *) GlobalLock (htext)) == NULL)
ee78dc32 323 goto closeclip;
41a48e45 324
69cddef0
GV
325 nbytes = strlen (src);
326
06466d9a
JR
327 /* If the text in clipboard is identical to what we put there
328 last time w32_set_clipboard_data was called, pretend there's no
329 data in the clipboard. This is so we don't pass our own text
330 from the clipboard (which might be troublesome if the killed
331 text includes null characters). */
ee79d1aa
JR
332 if ((clipboard_sequence_fn
333 && clipboard_sequence_fn () == last_clipboard_sequence_number)
334 || (last_clipboard_text
335 && clipboard_storage_size >= nbytes
336 && memcmp(last_clipboard_text, src, nbytes) == 0))
06466d9a
JR
337 goto closeclip;
338
aab7e392
JR
339 {
340 /* If the clipboard data contains any non-ascii code, we
341 need to decode it. */
342 int i;
343
344 for (i = 0; i < nbytes; i++)
345 {
346 if (src[i] >= 0x80)
347 {
348 require_decoding = 1;
349 break;
350 }
351 }
352 }
93cbf229 353
c0ca703b 354 if (require_decoding)
69cddef0 355 {
bbb059f3
AI
356 int bufsize;
357 unsigned char *buf;
358 struct coding_system coding;
359
93cbf229
GV
360 if (NILP (Vnext_selection_coding_system))
361 Vnext_selection_coding_system = Vselection_coding_system;
bbb059f3 362 setup_coding_system
93cbf229 363 (Fcheck_coding_system (Vnext_selection_coding_system), &coding);
69ebbf81
KH
364 coding.src_multibyte = 0;
365 coding.dst_multibyte = 1;
93cbf229 366 Vnext_selection_coding_system = Qnil;
bbb059f3 367 coding.mode |= CODING_MODE_LAST_BLOCK;
87c14506
KH
368 /* We explicitely disable composition handling because
369 selection data should not contain any composition
370 sequence. */
371 coding.composing = COMPOSITION_DISABLED;
bbb059f3
AI
372 bufsize = decoding_buffer_size (&coding, nbytes);
373 buf = (unsigned char *) xmalloc (bufsize);
374 decode_coding (&coding, src, buf, nbytes, bufsize);
0108f679 375 Vlast_coding_system_used = coding.symbol;
06466d9a
JR
376 ret = make_string_from_bytes ((char *) buf,
377 coding.produced_char, coding.produced);
bbb059f3 378 xfree (buf);
aab7e392
JR
379 if (SYMBOLP (coding.post_read_conversion)
380 && !NILP (Ffboundp (coding.post_read_conversion)))
381 ret = run_pre_post_conversion_on_str (ret, &coding, 0);
69cddef0 382 }
bbb059f3
AI
383 else
384 {
c0ca703b
GV
385 /* Need to know final size after CR chars are removed because we
386 can't change the string size manually, and doing an extra
387 copy is silly. Note that we only remove CR when it appears
388 as part of CRLF. */
bbb059f3
AI
389
390 truelen = nbytes;
391 dst = src;
392 /* avoid using strchr because it recomputes the length everytime */
393 while ((dst = memchr (dst, '\r', nbytes - (dst - src))) != NULL)
394 {
c0ca703b
GV
395 if (dst[1] == '\n') /* safe because of trailing '\0' */
396 truelen--;
bbb059f3
AI
397 dst++;
398 }
69cddef0 399
bbb059f3 400 ret = make_uninit_string (truelen);
69cddef0 401
c0ca703b
GV
402 /* Convert CRLF line endings (the standard CF_TEXT clipboard
403 format) to LF endings as used internally by Emacs. */
69cddef0 404
d5db4077 405 dst = SDATA (ret);
bbb059f3 406 while (1)
69cddef0 407 {
bbb059f3
AI
408 unsigned char *next;
409 /* copy next line or remaining bytes excluding '\0' */
410 next = _memccpy (dst, src, '\r', nbytes);
411 if (next)
412 {
413 /* copied one line ending with '\r' */
414 int copied = next - dst;
415 nbytes -= copied;
c0ca703b 416 dst += copied;
bbb059f3 417 src += copied;
c0ca703b
GV
418 if (*src == '\n')
419 dst--; /* overwrite '\r' with '\n' */
420 }
bbb059f3
AI
421 else
422 /* copied remaining partial line -> now finished */
423 break;
424 }
0108f679
AI
425
426 Vlast_coding_system_used = Qraw_text;
69cddef0
GV
427 }
428
ee78dc32
GV
429 GlobalUnlock (htext);
430 }
431
432 closeclip:
433 CloseClipboard ();
41a48e45 434
ee78dc32
GV
435 done:
436 UNBLOCK_INPUT;
41a48e45 437
ee78dc32
GV
438 return (ret);
439}
440
9aa94bd5
KH
441/* Support checking for a clipboard selection. */
442
443DEFUN ("x-selection-exists-p", Fx_selection_exists_p, Sx_selection_exists_p,
33f09670
JR
444 0, 1, 0,
445 doc: /* Whether there is an owner for the given X Selection.
446The arg should be the name of the selection in question, typically one of
447the symbols `PRIMARY', `SECONDARY', or `CLIPBOARD'.
448\(Those are literal upper-case symbol names, since that's what X expects.)
449For convenience, the symbol nil is the same as `PRIMARY',
450and t is the same as `SECONDARY'. */)
9aa94bd5
KH
451 (selection)
452 Lisp_Object selection;
453{
b7826503 454 CHECK_SYMBOL (selection);
9aa94bd5
KH
455
456 /* Return nil for PRIMARY and SECONDARY selections; for CLIPBOARD, check
457 if the clipboard currently has valid text format contents. */
458
459 if (EQ (selection, QCLIPBOARD))
460 {
461 Lisp_Object val = Qnil;
462
463 if (OpenClipboard (NULL))
464 {
465 int format = 0;
466 while (format = EnumClipboardFormats (format))
467 if (format == CF_TEXT)
468 {
469 val = Qt;
470 break;
471 }
472 CloseClipboard ();
473 }
474 return val;
475 }
476 return Qnil;
477}
478
41a48e45 479void
fbd6baed 480syms_of_w32select ()
ee78dc32
GV
481{
482#if 0
fbd6baed
GV
483 defsubr (&Sw32_open_clipboard);
484 defsubr (&Sw32_empty_clipboard);
485 defsubr (&Sw32_close_clipboard);
ee78dc32 486#endif
fbd6baed
GV
487 defsubr (&Sw32_set_clipboard_data);
488 defsubr (&Sw32_get_clipboard_data);
9aa94bd5
KH
489 defsubr (&Sx_selection_exists_p);
490
72aca5fa 491 DEFVAR_LISP ("selection-coding-system", &Vselection_coding_system,
33f09670
JR
492 doc: /* Coding system for communicating with other programs.
493When sending or receiving text via cut_buffer, selection, and clipboard,
41a48e45
JB
494the text is encoded or decoded by this coding system.
495The default value is `iso-latin-1-dos'. */);
496 Vselection_coding_system = intern ("iso-latin-1-dos");
bbb059f3 497
93cbf229 498 DEFVAR_LISP ("next-selection-coding-system", &Vnext_selection_coding_system,
33f09670
JR
499 doc: /* Coding system for the next communication with other programs.
500Usually, `selection-coding-system' is used for communicating with
3b32ae2c
JB
501other programs. But, if this variable is set, it is used for the
502next communication only. After the communication, this variable is
33f09670 503set to nil. */);
93cbf229
GV
504 Vnext_selection_coding_system = Qnil;
505
9aa94bd5 506 QCLIPBOARD = intern ("CLIPBOARD"); staticpro (&QCLIPBOARD);
ee78dc32 507}
ab5796a9
MB
508
509/* arch-tag: c96e9724-5eb1-4dad-be07-289f092fd2af
510 (do not change this comment) */