* Makefile.in: Undef LIB_STANDARD before defining it to silence warning
[bpt/emacs.git] / src / nsselect.m
CommitLineData
edfda783 1/* NeXT/Open/GNUstep / MacOSX Cocoa selection processing for emacs.
32d235f8
GM
2 Copyright (C) 1993, 1994, 2005, 2006, 2008
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
28#include "config.h"
29#include "lisp.h"
30#include "nsterm.h"
31#include "termhooks.h"
32
33#define CUT_BUFFER_SUPPORT
34
35Lisp_Object QPRIMARY, QSECONDARY, QTEXT, QFILE_NAME;
36
37static Lisp_Object Vns_sent_selection_hooks;
38static Lisp_Object Vns_lost_selection_hooks;
39static Lisp_Object Vselection_alist;
40static Lisp_Object Vselection_converter_alist;
41
42/* 23: new */
43/* Coding system for communicating with other programs. */
44static Lisp_Object Vselection_coding_system;
45/* Coding system for the next communicating with other programs. */
46static Lisp_Object Vnext_selection_coding_system;
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;
67 return [NSString stringWithUTF8String: XSTRING (XSYMBOL (sym)->xname)->data];
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++)
facfbbbd 118 AREF (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
155 utfStr = XSTRING (str)->data;
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
376DEFUN ("ns-own-selection-internal", Fns_own_selection_internal,
377 Sns_own_selection_internal, 2, 2, 0, "Assert a selection.")
378 (selection_name, selection_value)
379 Lisp_Object selection_name, selection_value;
380{
381 id pb;
382 Lisp_Object old_value, new_value;
383
384 check_ns ();
385 CHECK_SYMBOL (selection_name);
386 if (NILP (selection_value))
387 error ("selection-value may not be nil.");
388 pb =[NSPasteboard pasteboardWithName: symbol_to_nsstring (selection_name)];
389 ns_declare_pasteboard (pb);
facfbbbd 390 old_value = assq_no_quit (selection_name, Vselection_alist);
edfda783
AR
391 new_value = Fcons (selection_name, Fcons (selection_value, Qnil));
392 if (NILP (old_value))
facfbbbd 393 Vselection_alist = Fcons (new_value, Vselection_alist);
edfda783
AR
394 else
395 Fsetcdr (old_value, Fcdr (new_value));
396 /* XXX An evil hack, but a necessary one I fear XXX */
397 {
398 struct input_event ev;
399 ev.kind = SELECTION_REQUEST_EVENT;
400 ev.modifiers = 0;
401 ev.code = 0;
facfbbbd
SM
402 *(EMACS_INT*)(&(ev.x)) = (EMACS_INT)pb; // FIXME: BIG UGLY HACK!!
403 *(EMACS_INT*)(&(ev.y)) = (EMACS_INT)NSStringPboardType;
edfda783
AR
404 ns_handle_selection_request (&ev);
405 }
406 return selection_value;
407}
408
409
410DEFUN ("ns-disown-selection-internal", Fns_disown_selection_internal,
411 Sns_disown_selection_internal, 1, 2, 0,
412 "If we own the selection SELECTION, disown it.")
413 (selection_name, time)
414 Lisp_Object selection_name, time;
415{
416 id pb;
417 check_ns ();
418 CHECK_SYMBOL (selection_name);
419 if (NILP (assq_no_quit (selection_name, Vselection_alist))) return Qnil;
420
421 pb =[NSPasteboard pasteboardWithName: symbol_to_nsstring (selection_name)];
422 ns_undeclare_pasteboard (pb);
423 return Qt;
424}
425
426
427DEFUN ("ns-selection-exists-p", Fns_selection_exists_p, Sns_selection_exists_p,
428 0, 1, 0, "Whether there is an owner for the given selection.\n\
429The arg should be the name of the selection in question, typically one of\n\
430the symbols `PRIMARY', `SECONDARY', or `CLIPBOARD'.\n\
431\(Those are literal upper-case symbol names.)\n\
432For convenience, the symbol nil is the same as `PRIMARY',\n\
433and t is the same as `SECONDARY'.)")
434 (selection)
435 Lisp_Object selection;
436{
437 id pb;
438 NSArray *types;
439
440 check_ns ();
441 CHECK_SYMBOL (selection);
442 if (EQ (selection, Qnil)) selection = QPRIMARY;
443 if (EQ (selection, Qt)) selection = QSECONDARY;
444 pb =[NSPasteboard pasteboardWithName: symbol_to_nsstring (selection)];
445 types =[pb types];
446 return ([types count] == 0) ? Qnil : Qt;
447}
448
449
450DEFUN ("ns-selection-owner-p", Fns_selection_owner_p, Sns_selection_owner_p,
451 0, 1, 0,
452 "Whether the current Emacs process owns the given selection.\n\
453The arg should be the name of the selection in question, typically one of\n\
454the symbols `PRIMARY', `SECONDARY', or `CLIPBOARD'.\n\
455\(Those are literal upper-case symbol names.)\n\
456For convenience, the symbol nil is the same as `PRIMARY',\n\
457and t is the same as `SECONDARY'.)")
458 (selection)
459 Lisp_Object selection;
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
469DEFUN ("ns-get-selection-internal", Fns_get_selection_internal,
470 Sns_get_selection_internal, 2, 2, 0,
471 "Return text selected from some pasteboard.\n\
472SELECTION is a symbol, typically `PRIMARY', `SECONDARY', or `CLIPBOARD'.\n\
473\(Those are literal upper-case symbol names.)\n\
474TYPE is the type of data desired, typically `STRING'.")
475 (selection_name, target_type)
476 Lisp_Object selection_name, target_type;
477{
478 Lisp_Object val;
479
480 check_ns ();
481 CHECK_SYMBOL (selection_name);
482 CHECK_SYMBOL (target_type);
483 val = ns_get_local_selection (selection_name, target_type);
484 if (NILP (val))
485 val = ns_get_foreign_selection (selection_name, target_type);
486 if (CONSP (val) && SYMBOLP (Fcar (val)))
487 {
488 val = Fcdr (val);
489 if (CONSP (val) && NILP (Fcdr (val)))
490 val = Fcar (val);
491 }
492 val = clean_local_selection_data (val);
493 return val;
494}
495
496
497#ifdef CUT_BUFFER_SUPPORT
498DEFUN ("ns-get-cut-buffer-internal", Fns_get_cut_buffer_internal,
499 Sns_get_cut_buffer_internal, 1, 1, 0,
500 "Returns the value of the named cut buffer.")
501 (buffer)
502 Lisp_Object buffer;
503{
504 id pb;
505 check_ns ();
506 pb =[NSPasteboard pasteboardWithName: symbol_to_nsstring (buffer)];
507 return ns_string_from_pasteboard (pb);
508}
509
510
511DEFUN ("ns-rotate-cut-buffers-internal", Fns_rotate_cut_buffers_internal,
512 Sns_rotate_cut_buffers_internal, 1, 1, 0,
513 "Rotate the values of the cut buffers by the given number of steps;\n\
514 positive means move values forward, negative means backward. CURRENTLY NOT IMPLEMENTED UNDER NeXTstep.")
515 (n)
516 Lisp_Object n;
517{
518 /* XXX This function is unimplemented under NeXTstep XXX */
519 Fsignal (Qquit, Fcons (build_string (
520 "Warning: ns-rotate-cut-buffers-internal not implemented\n"), Qnil));
521 return Qnil;
522}
523
524
525DEFUN ("ns-store-cut-buffer-internal", Fns_store_cut_buffer_internal,
526 Sns_store_cut_buffer_internal, 2, 2, 0,
527 "Sets the value of the named cut buffer (typically CUT_BUFFER0).")
528 (buffer, string)
529 Lisp_Object buffer, string;
530{
531 id pb;
532 check_ns ();
533 pb =[NSPasteboard pasteboardWithName: symbol_to_nsstring (buffer)];
534 ns_string_to_pasteboard (pb, string);
535 return Qnil;
536}
537#endif
538
539
540void
541nxatoms_of_nsselect (void)
542{
543 NXSecondaryPboard = @"Selection";
544}
545
546void
547syms_of_nsselect (void)
548{
549 QPRIMARY = intern ("PRIMARY"); staticpro (&QPRIMARY);
550 QSECONDARY = intern ("SECONDARY"); staticpro (&QSECONDARY);
551 QTEXT = intern ("TEXT"); staticpro (&QTEXT);
552 QFILE_NAME = intern ("FILE_NAME"); staticpro (&QFILE_NAME);
553
554 defsubr (&Sns_disown_selection_internal);
555 defsubr (&Sns_get_selection_internal);
556 defsubr (&Sns_own_selection_internal);
557 defsubr (&Sns_selection_exists_p);
558 defsubr (&Sns_selection_owner_p);
559#ifdef CUT_BUFFER_SUPPORT
560 defsubr (&Sns_get_cut_buffer_internal);
561 defsubr (&Sns_rotate_cut_buffers_internal);
562 defsubr (&Sns_store_cut_buffer_internal);
563#endif
564
565 Vselection_alist = Qnil;
566 staticpro (&Vselection_alist);
567
568 DEFVAR_LISP ("ns-sent-selection-hooks", &Vns_sent_selection_hooks,
569 "A list of functions to be called when Emacs answers a selection request.\n\
570The functions are called with four arguments:\n\
571 - the selection name (typically `PRIMARY', `SECONDARY', or `CLIPBOARD');\n\
572 - the selection-type which Emacs was asked to convert the\n\
573 selection into before sending (for example, `STRING' or `LENGTH');\n\
574 - a flag indicating success or failure for responding to the request.\n\
575We might have failed (and declined the request) for any number of reasons,\n\
576including being asked for a selection that we no longer own, or being asked\n\
577to convert into a type that we don't know about or that is inappropriate.\n\
578This hook doesn't let you change the behavior of Emacs's selection replies,\n\
579it merely informs you that they have happened.");
580 Vns_sent_selection_hooks = Qnil;
581
582 DEFVAR_LISP ("selection-converter-alist", &Vselection_converter_alist,
583 "An alist associating X Windows selection-types with functions.\n\
584These functions are called to convert the selection, with three args:\n\
585the name of the selection (typically `PRIMARY', `SECONDARY', or `CLIPBOARD');\n\
586a desired type to which the selection should be converted;\n\
587and the local selection value (whatever was given to `x-own-selection').\n\
588\n\
589The function should return the value to send to the X server\n\
590\(typically a string). A return value of nil\n\
591means that the conversion could not be done.\n\
592A return value which is the symbol `NULL'\n\
593means that a side-effect was executed,\n\
594and there is no meaningful selection value.");
595 Vselection_converter_alist = Qnil;
596
597 DEFVAR_LISP ("ns-lost-selection-hooks", &Vns_lost_selection_hooks,
598 "A list of functions to be called when Emacs loses an X selection.\n\
599\(This happens when some other X client makes its own selection\n\
600or when a Lisp program explicitly clears the selection.)\n\
601The functions are called with one argument, the selection type\n\
602\(a symbol, typically `PRIMARY', `SECONDARY', or `CLIPBOARD').");
603 Vns_lost_selection_hooks = Qnil;
604
605/* 23: { */
606 DEFVAR_LISP ("selection-coding-system", &Vselection_coding_system,
607 doc: /* Coding system for communicating with other programs.
608When sending or receiving text via cut_buffer, selection, and clipboard,
609the text is encoded or decoded by this coding system.
610The default value is determined by the system script code. */);
611 Vselection_coding_system = Qnil;
612
613 DEFVAR_LISP ("next-selection-coding-system", &Vnext_selection_coding_system,
614 doc: /* Coding system for the next communication with other programs.
615Usually, `selection-coding-system' is used for communicating with
616other programs. But, if this variable is set, it is used for the
617next communication only. After the communication, this variable is
618set to nil. */);
619 Vnext_selection_coding_system = Qnil;
620
621 Qforeign_selection = intern ("foreign-selection");
622 staticpro (&Qforeign_selection);
623/* } */
624
625}
0ae1e5e5 626
2f8e74bb 627// arch-tag: 39d1dde7-06a6-49ff-95a7-0e7af12d2218