Add NEWS placeholder.
[bpt/emacs.git] / src / nsselect.m
CommitLineData
edfda783 1/* NeXT/Open/GNUstep / MacOSX Cocoa selection processing for emacs.
114f9c96 2 Copyright (C) 1993, 1994, 2005, 2006, 2008, 2009, 2010
32d235f8 3 Free Software Foundation, Inc.
edfda783
AR
4
5This file is part of GNU Emacs.
6
32d235f8 7GNU Emacs is free software: you can redistribute it and/or modify
edfda783 8it under the terms of the GNU General Public License as published by
32d235f8
GM
9the Free Software Foundation, either version 3 of the License, or
10(at your option) any later version.
edfda783
AR
11
12GNU Emacs is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
32d235f8 18along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
edfda783 19
32d235f8 20/*
edfda783
AR
21Originally by Carl Edman
22Updated by Christian Limpach (chris@nice.ch)
23OpenStep/Rhapsody port by Scott Bender (sbender@harmony-ds.com)
24MacOSX/Aqua port by Christophe de Dinechin (descubes@earthlink.net)
25GNUstep port and post-20 update by Adrian Robert (arobert@cogsci.ucsd.edu)
edfda783
AR
26*/
27
5a06864f
AR
28/* This should be the first include, as it may set up #defines affecting
29 interpretation of even the system includes. */
08a494a3 30#include <config.h>
b024548b 31#include <setjmp.h>
5a06864f 32
edfda783
AR
33#include "lisp.h"
34#include "nsterm.h"
35#include "termhooks.h"
921242c6 36#include "keyboard.h"
edfda783
AR
37
38#define CUT_BUFFER_SUPPORT
39
921242c6 40Lisp_Object QSECONDARY, QTEXT, QFILE_NAME;
edfda783
AR
41
42static Lisp_Object Vns_sent_selection_hooks;
43static Lisp_Object Vns_lost_selection_hooks;
44static Lisp_Object Vselection_alist;
45static Lisp_Object Vselection_converter_alist;
46
edfda783
AR
47static Lisp_Object Qforeign_selection;
48
49NSString *NXSecondaryPboard;
50
51
52
53/* ==========================================================================
54
55 Internal utility functions
56
57 ========================================================================== */
58
59
60static NSString *
61symbol_to_nsstring (Lisp_Object sym)
62{
63 CHECK_SYMBOL (sym);
64 if (EQ (sym, QPRIMARY)) return NSGeneralPboard;
65 if (EQ (sym, QSECONDARY)) return NXSecondaryPboard;
66 if (EQ (sym, QTEXT)) return NSStringPboardType;
86fa089e 67 return [NSString stringWithUTF8String: SDATA (XSYMBOL (sym)->xname)];
edfda783
AR
68}
69
70
71static Lisp_Object
72ns_string_to_symbol (NSString *t)
73{
74 if ([t isEqualToString: NSGeneralPboard])
75 return QPRIMARY;
76 if ([t isEqualToString: NXSecondaryPboard])
77 return QSECONDARY;
78 if ([t isEqualToString: NSStringPboardType])
79 return QTEXT;
80 if ([t isEqualToString: NSFilenamesPboardType])
81 return QFILE_NAME;
82 if ([t isEqualToString: NSTabularTextPboardType])
83 return QTEXT;
84 return intern ([t UTF8String]);
85}
86
87
88static Lisp_Object
89clean_local_selection_data (Lisp_Object obj)
90{
91 if (CONSP (obj)
92 && INTEGERP (XCAR (obj))
93 && CONSP (XCDR (obj))
94 && INTEGERP (XCAR (XCDR (obj)))
95 && NILP (XCDR (XCDR (obj))))
96 obj = Fcons (XCAR (obj), XCDR (obj));
97
98 if (CONSP (obj)
99 && INTEGERP (XCAR (obj))
100 && INTEGERP (XCDR (obj)))
101 {
102 if (XINT (XCAR (obj)) == 0)
103 return XCDR (obj);
104 if (XINT (XCAR (obj)) == -1)
105 return make_number (- XINT (XCDR (obj)));
106 }
107
108 if (VECTORP (obj))
109 {
110 int i;
facfbbbd 111 int size = ASIZE (obj);
edfda783
AR
112 Lisp_Object copy;
113
114 if (size == 1)
facfbbbd
SM
115 return clean_local_selection_data (AREF (obj, 0));
116 copy = Fmake_vector (make_number (size), Qnil);
edfda783 117 for (i = 0; i < size; i++)
86fa089e 118 ASET (copy, i, clean_local_selection_data (AREF (obj, i)));
edfda783
AR
119 return copy;
120 }
121
122 return obj;
123}
124
125
126static void
127ns_declare_pasteboard (id pb)
128{
129 [pb declareTypes: ns_send_types owner: NSApp];
130}
131
132
133static void
134ns_undeclare_pasteboard (id pb)
135{
136 [pb declareTypes: [NSArray array] owner: nil];
137}
138
139
140static void
141ns_string_to_pasteboard_internal (id pb, Lisp_Object str, NSString *gtype)
142{
143 if (EQ (str, Qnil))
144 {
145 [pb declareTypes: [NSArray array] owner: nil];
146 }
147 else
148 {
149 char *utfStr;
150 NSString *type, *nsStr;
151 NSEnumerator *tenum;
152
153 CHECK_STRING (str);
154
86fa089e 155 utfStr = SDATA (str);
edfda783
AR
156 nsStr = [NSString stringWithUTF8String: utfStr];
157
158 if (gtype == nil)
159 {
160 [pb declareTypes: ns_send_types owner: nil];
161 tenum = [ns_send_types objectEnumerator];
162 while ( (type = [tenum nextObject]) )
163 [pb setString: nsStr forType: type];
164 }
165 else
166 {
167 [pb setString: nsStr forType: gtype];
168 }
169 }
170}
171
172
173static Lisp_Object
174ns_get_local_selection (Lisp_Object selection_name,
175 Lisp_Object target_type)
176{
177 Lisp_Object local_value;
178 Lisp_Object handler_fn, value, type, check;
179 int count;
180
181 local_value = assq_no_quit (selection_name, Vselection_alist);
182
183 if (NILP (local_value)) return Qnil;
184
185 count = specpdl_ptr - specpdl;
186 specbind (Qinhibit_quit, Qt);
187 CHECK_SYMBOL (target_type);
188 handler_fn = Fcdr (Fassq (target_type, Vselection_converter_alist));
189 if (!NILP (handler_fn))
facfbbbd 190 value = call3 (handler_fn, selection_name, target_type,
edfda783
AR
191 XCAR (XCDR (local_value)));
192 else
facfbbbd 193 value = Qnil;
edfda783
AR
194 unbind_to (count, Qnil);
195
facfbbbd 196 check = value;
edfda783
AR
197 if (CONSP (value) && SYMBOLP (XCAR (value)))
198 {
199 type = XCAR (value);
200 check = XCDR (value);
201 }
202
203 if (STRINGP (check) || VECTORP (check) || SYMBOLP (check)
204 || INTEGERP (check) || NILP (value))
205 return value;
206
207 if (CONSP (check)
208 && INTEGERP (XCAR (check))
209 && (INTEGERP (XCDR (check))||
210 (CONSP (XCDR (check))
211 && INTEGERP (XCAR (XCDR (check)))
212 && NILP (XCDR (XCDR (check))))))
213 return value;
214
facfbbbd 215 // FIXME: Why `quit' rather than `error'?
edfda783
AR
216 Fsignal (Qquit, Fcons (build_string (
217 "invalid data returned by selection-conversion function"),
218 Fcons (handler_fn, Fcons (value, Qnil))));
facfbbbd
SM
219 // FIXME: Beware, `quit' can return!!
220 return Qnil;
edfda783
AR
221}
222
223
224static Lisp_Object
225ns_get_foreign_selection (Lisp_Object symbol, Lisp_Object target)
226{
227 id pb;
228 pb =[NSPasteboard pasteboardWithName: symbol_to_nsstring (symbol)];
229 return ns_string_from_pasteboard (pb);
230}
231
232
233static void
234ns_handle_selection_request (struct input_event *event)
235{
facfbbbd
SM
236 // FIXME: BIG UGLY HACK!!!
237 id pb = (id)*(EMACS_INT*)&(event->x);
238 NSString *type = (NSString *)*(EMACS_INT*)&(event->y);
edfda783
AR
239 Lisp_Object selection_name, selection_data, target_symbol, data;
240 Lisp_Object successful_p, rest;
241
facfbbbd
SM
242 selection_name = ns_string_to_symbol ([(NSPasteboard *)pb name]);
243 target_symbol = ns_string_to_symbol (type);
edfda783 244 selection_data = assq_no_quit (selection_name, Vselection_alist);
facfbbbd 245 successful_p = Qnil;
edfda783
AR
246
247 if (!NILP (selection_data))
248 {
249 data = ns_get_local_selection (selection_name, target_symbol);
250 if (!NILP (data))
251 {
252 if (STRINGP (data))
253 ns_string_to_pasteboard_internal (pb, data, type);
facfbbbd 254 successful_p = Qt;
edfda783
AR
255 }
256 }
257
258 if (!EQ (Vns_sent_selection_hooks, Qunbound))
259 {
facfbbbd 260 for (rest = Vns_sent_selection_hooks; CONSP (rest); rest = Fcdr (rest))
edfda783
AR
261 call3 (Fcar (rest), selection_name, target_symbol, successful_p);
262 }
263}
264
265
266static void
267ns_handle_selection_clear (struct input_event *event)
268{
facfbbbd 269 id pb = (id)*(EMACS_INT*)&(event->x);
edfda783
AR
270 Lisp_Object selection_name, selection_data, rest;
271
facfbbbd
SM
272 selection_name = ns_string_to_symbol ([(NSPasteboard *)pb name]);
273 selection_data = assq_no_quit (selection_name, Vselection_alist);
edfda783
AR
274 if (NILP (selection_data)) return;
275
276 if (EQ (selection_data, Fcar (Vselection_alist)))
277 Vselection_alist = Fcdr (Vselection_alist);
278 else
279 {
280 for (rest = Vselection_alist; !NILP (rest); rest = Fcdr (rest))
281 if (EQ (selection_data, Fcar (Fcdr (rest))))
282 Fsetcdr (rest, Fcdr (Fcdr (rest)));
283 }
284
285 if (!EQ (Vns_lost_selection_hooks, Qunbound))
286 {
facfbbbd 287 for (rest = Vns_lost_selection_hooks;CONSP (rest); rest = Fcdr (rest))
edfda783
AR
288 call1 (Fcar (rest), selection_name);
289 }
290}
291
292
293
294/* ==========================================================================
295
296 Functions used externally
297
298 ========================================================================== */
299
300
301Lisp_Object
302ns_string_from_pasteboard (id pb)
303{
304 NSString *type, *str;
305 const char *utfStr;
306
307 type = [pb availableTypeFromArray: ns_return_types];
308 if (type == nil)
309 {
310 Fsignal (Qquit,
311 Fcons (build_string ("empty or unsupported pasteboard type"),
312 Qnil));
313 return Qnil;
314 }
315
316 /* get the string */
317 if (! (str = [pb stringForType: type]))
318 {
319 NSData *data = [pb dataForType: type];
320 if (data != nil)
321 str = [[NSString alloc] initWithData: data
322 encoding: NSUTF8StringEncoding];
323 if (str != nil)
324 {
325 [str autorelease];
326 }
327 else
328 {
329 Fsignal (Qquit,
330 Fcons (build_string ("pasteboard doesn't contain valid data"),
331 Qnil));
332 return Qnil;
333 }
334 }
335
336 /* assume UTF8 */
337 NS_DURING
338 {
339 /* EOL conversion: PENDING- is this too simple? */
340 NSMutableString *mstr = [[str mutableCopy] autorelease];
341 [mstr replaceOccurrencesOfString: @"\r\n" withString: @"\n"
342 options: NSLiteralSearch range: NSMakeRange (0, [mstr length])];
343 [mstr replaceOccurrencesOfString: @"\r" withString: @"\n"
344 options: NSLiteralSearch range: NSMakeRange (0, [mstr length])];
345
346 utfStr = [mstr UTF8String];
347 if (!utfStr)
348 utfStr = [mstr cString];
349 }
350 NS_HANDLER
351 {
352 message1 ("ns_string_from_pasteboard: UTF8String failed\n");
353 utfStr = [str lossyCString];
354 }
355 NS_ENDHANDLER
356
357 return build_string (utfStr);
358}
359
360
361void
362ns_string_to_pasteboard (id pb, Lisp_Object str)
363{
364 ns_string_to_pasteboard_internal (pb, str, nil);
365}
366
367
368
369/* ==========================================================================
370
371 Lisp Defuns
372
373 ========================================================================== */
374
375
28bf482a
DR
376DEFUN ("x-own-selection-internal", Fx_own_selection_internal,
377 Sx_own_selection_internal, 2, 2, 0,
a5c55c69
CY
378 doc: /* Assert a selection.
379SELECTION-NAME is a symbol, typically `PRIMARY', `SECONDARY', or `CLIPBOARD'.
380VALUE is typically a string, or a cons of two markers, but may be
381anything that the functions on `selection-converter-alist' know about. */)
5842a27b 382 (Lisp_Object selection_name, Lisp_Object selection_value)
edfda783
AR
383{
384 id pb;
385 Lisp_Object old_value, new_value;
386
387 check_ns ();
388 CHECK_SYMBOL (selection_name);
389 if (NILP (selection_value))
390 error ("selection-value may not be nil.");
391 pb =[NSPasteboard pasteboardWithName: symbol_to_nsstring (selection_name)];
392 ns_declare_pasteboard (pb);
facfbbbd 393 old_value = assq_no_quit (selection_name, Vselection_alist);
edfda783
AR
394 new_value = Fcons (selection_name, Fcons (selection_value, Qnil));
395 if (NILP (old_value))
facfbbbd 396 Vselection_alist = Fcons (new_value, Vselection_alist);
edfda783
AR
397 else
398 Fsetcdr (old_value, Fcdr (new_value));
399 /* XXX An evil hack, but a necessary one I fear XXX */
400 {
401 struct input_event ev;
402 ev.kind = SELECTION_REQUEST_EVENT;
403 ev.modifiers = 0;
404 ev.code = 0;
facfbbbd
SM
405 *(EMACS_INT*)(&(ev.x)) = (EMACS_INT)pb; // FIXME: BIG UGLY HACK!!
406 *(EMACS_INT*)(&(ev.y)) = (EMACS_INT)NSStringPboardType;
edfda783
AR
407 ns_handle_selection_request (&ev);
408 }
409 return selection_value;
410}
411
412
9e50ff0c
DN
413DEFUN ("x-disown-selection-internal", Fx_disown_selection_internal,
414 Sx_disown_selection_internal, 1, 2, 0,
a5c55c69 415 doc: /* If we own the selection SELECTION, disown it. */)
5842a27b 416 (Lisp_Object selection_name, Lisp_Object time)
edfda783
AR
417{
418 id pb;
419 check_ns ();
420 CHECK_SYMBOL (selection_name);
421 if (NILP (assq_no_quit (selection_name, Vselection_alist))) return Qnil;
422
423 pb =[NSPasteboard pasteboardWithName: symbol_to_nsstring (selection_name)];
424 ns_undeclare_pasteboard (pb);
425 return Qt;
426}
427
428
28bf482a 429DEFUN ("x-selection-exists-p", Fx_selection_exists_p, Sx_selection_exists_p,
a5c55c69
CY
430 0, 1, 0, doc: /* Whether there is an owner for the given selection.
431The arg should be the name of the selection in question, typically one of
432the symbols `PRIMARY', `SECONDARY', or `CLIPBOARD'.
433\(Those are literal upper-case symbol names.)
434For convenience, the symbol nil is the same as `PRIMARY',
435and t is the same as `SECONDARY'.) */)
5842a27b 436 (Lisp_Object selection)
edfda783
AR
437{
438 id pb;
439 NSArray *types;
440
441 check_ns ();
442 CHECK_SYMBOL (selection);
443 if (EQ (selection, Qnil)) selection = QPRIMARY;
444 if (EQ (selection, Qt)) selection = QSECONDARY;
445 pb =[NSPasteboard pasteboardWithName: symbol_to_nsstring (selection)];
446 types =[pb types];
447 return ([types count] == 0) ? Qnil : Qt;
448}
449
450
28bf482a 451DEFUN ("x-selection-owner-p", Fx_selection_owner_p, Sx_selection_owner_p,
edfda783 452 0, 1, 0,
a5c55c69
CY
453 doc: /* Whether the current Emacs process owns the given selection.
454The arg should be the name of the selection in question, typically one of
455the symbols `PRIMARY', `SECONDARY', or `CLIPBOARD'.
456\(Those are literal upper-case symbol names.)
457For convenience, the symbol nil is the same as `PRIMARY',
458and t is the same as `SECONDARY'.) */)
5842a27b 459 (Lisp_Object selection)
edfda783
AR
460{
461 check_ns ();
462 CHECK_SYMBOL (selection);
463 if (EQ (selection, Qnil)) selection = QPRIMARY;
464 if (EQ (selection, Qt)) selection = QSECONDARY;
465 return (NILP (Fassq (selection, Vselection_alist))) ? Qnil : Qt;
466}
467
468
9e50ff0c
DN
469DEFUN ("x-get-selection-internal", Fx_get_selection_internal,
470 Sx_get_selection_internal, 2, 2, 0,
a5c55c69
CY
471 doc: /* Return text selected from some pasteboard.
472SELECTION is a symbol, typically `PRIMARY', `SECONDARY', or `CLIPBOARD'.
473\(Those are literal upper-case symbol names.)
474TYPE is the type of data desired, typically `STRING'. */)
5842a27b 475 (Lisp_Object selection_name, Lisp_Object target_type)
edfda783
AR
476{
477 Lisp_Object val;
478
479 check_ns ();
480 CHECK_SYMBOL (selection_name);
481 CHECK_SYMBOL (target_type);
482 val = ns_get_local_selection (selection_name, target_type);
483 if (NILP (val))
484 val = ns_get_foreign_selection (selection_name, target_type);
485 if (CONSP (val) && SYMBOLP (Fcar (val)))
486 {
487 val = Fcdr (val);
488 if (CONSP (val) && NILP (Fcdr (val)))
489 val = Fcar (val);
490 }
491 val = clean_local_selection_data (val);
492 return val;
493}
494
495
496#ifdef CUT_BUFFER_SUPPORT
497DEFUN ("ns-get-cut-buffer-internal", Fns_get_cut_buffer_internal,
498 Sns_get_cut_buffer_internal, 1, 1, 0,
a5c55c69 499 doc: /* Returns the value of the named cut buffer. */)
5842a27b 500 (Lisp_Object buffer)
edfda783
AR
501{
502 id pb;
503 check_ns ();
504 pb =[NSPasteboard pasteboardWithName: symbol_to_nsstring (buffer)];
505 return ns_string_from_pasteboard (pb);
506}
507
508
509DEFUN ("ns-rotate-cut-buffers-internal", Fns_rotate_cut_buffers_internal,
510 Sns_rotate_cut_buffers_internal, 1, 1, 0,
a5c55c69
CY
511 doc: /* Rotate the values of the cut buffers by N steps.
512Positive N means move values forward, negative means
513backward. CURRENTLY NOT IMPLEMENTED UNDER NEXTSTEP. */ )
5842a27b 514 (Lisp_Object n)
edfda783
AR
515{
516 /* XXX This function is unimplemented under NeXTstep XXX */
517 Fsignal (Qquit, Fcons (build_string (
518 "Warning: ns-rotate-cut-buffers-internal not implemented\n"), Qnil));
519 return Qnil;
520}
521
522
523DEFUN ("ns-store-cut-buffer-internal", Fns_store_cut_buffer_internal,
524 Sns_store_cut_buffer_internal, 2, 2, 0,
a5c55c69 525 doc: /* Sets the value of the named cut buffer (typically CUT_BUFFER0). */)
5842a27b 526 (Lisp_Object buffer, Lisp_Object string)
edfda783
AR
527{
528 id pb;
529 check_ns ();
530 pb =[NSPasteboard pasteboardWithName: symbol_to_nsstring (buffer)];
531 ns_string_to_pasteboard (pb, string);
532 return Qnil;
533}
534#endif
535
536
537void
538nxatoms_of_nsselect (void)
539{
540 NXSecondaryPboard = @"Selection";
541}
542
543void
544syms_of_nsselect (void)
545{
edfda783
AR
546 QSECONDARY = intern ("SECONDARY"); staticpro (&QSECONDARY);
547 QTEXT = intern ("TEXT"); staticpro (&QTEXT);
548 QFILE_NAME = intern ("FILE_NAME"); staticpro (&QFILE_NAME);
549
9e50ff0c
DN
550 defsubr (&Sx_disown_selection_internal);
551 defsubr (&Sx_get_selection_internal);
28bf482a
DR
552 defsubr (&Sx_own_selection_internal);
553 defsubr (&Sx_selection_exists_p);
554 defsubr (&Sx_selection_owner_p);
edfda783
AR
555#ifdef CUT_BUFFER_SUPPORT
556 defsubr (&Sns_get_cut_buffer_internal);
557 defsubr (&Sns_rotate_cut_buffers_internal);
558 defsubr (&Sns_store_cut_buffer_internal);
559#endif
560
561 Vselection_alist = Qnil;
562 staticpro (&Vselection_alist);
563
564 DEFVAR_LISP ("ns-sent-selection-hooks", &Vns_sent_selection_hooks,
565 "A list of functions to be called when Emacs answers a selection request.\n\
566The functions are called with four arguments:\n\
567 - the selection name (typically `PRIMARY', `SECONDARY', or `CLIPBOARD');\n\
568 - the selection-type which Emacs was asked to convert the\n\
569 selection into before sending (for example, `STRING' or `LENGTH');\n\
570 - a flag indicating success or failure for responding to the request.\n\
571We might have failed (and declined the request) for any number of reasons,\n\
572including being asked for a selection that we no longer own, or being asked\n\
573to convert into a type that we don't know about or that is inappropriate.\n\
574This hook doesn't let you change the behavior of Emacs's selection replies,\n\
575it merely informs you that they have happened.");
576 Vns_sent_selection_hooks = Qnil;
577
578 DEFVAR_LISP ("selection-converter-alist", &Vselection_converter_alist,
579 "An alist associating X Windows selection-types with functions.\n\
580These functions are called to convert the selection, with three args:\n\
581the name of the selection (typically `PRIMARY', `SECONDARY', or `CLIPBOARD');\n\
582a desired type to which the selection should be converted;\n\
583and the local selection value (whatever was given to `x-own-selection').\n\
584\n\
585The function should return the value to send to the X server\n\
586\(typically a string). A return value of nil\n\
587means that the conversion could not be done.\n\
588A return value which is the symbol `NULL'\n\
589means that a side-effect was executed,\n\
590and there is no meaningful selection value.");
591 Vselection_converter_alist = Qnil;
592
593 DEFVAR_LISP ("ns-lost-selection-hooks", &Vns_lost_selection_hooks,
594 "A list of functions to be called when Emacs loses an X selection.\n\
595\(This happens when some other X client makes its own selection\n\
596or when a Lisp program explicitly clears the selection.)\n\
597The functions are called with one argument, the selection type\n\
598\(a symbol, typically `PRIMARY', `SECONDARY', or `CLIPBOARD').");
599 Vns_lost_selection_hooks = Qnil;
600
edfda783
AR
601 Qforeign_selection = intern ("foreign-selection");
602 staticpro (&Qforeign_selection);
edfda783 603}
0ae1e5e5 604
2f8e74bb 605// arch-tag: 39d1dde7-06a6-49ff-95a7-0e7af12d2218