Mercurial > hg > xemacs-beta
annotate src/select.c @ 5407:7ba892d101ce
Convert remainder in "tests" with plain text GPLv2 to GPLv3
| author | Mats Lidell <matsl@xemacs.org> |
|---|---|
| date | Tue, 19 Oct 2010 22:33:36 +0200 |
| parents | 308d34e9f07d |
| children | a9094f28f9a9 |
| rev | line source |
|---|---|
| 414 | 1 /* Generic selection processing for XEmacs |
| 2 Copyright (C) 1999 Free Software Foundation, Inc. | |
| 3 Copyright (C) 1999 Andy Piper. | |
| 4 | |
| 5 This file is part of XEmacs. | |
| 6 | |
|
5402
308d34e9f07d
Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents:
5176
diff
changeset
|
7 XEmacs is free software: you can redistribute it and/or modify it |
| 414 | 8 under the terms of the GNU General Public License as published by the |
|
5402
308d34e9f07d
Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents:
5176
diff
changeset
|
9 Free Software Foundation, either version 3 of the License, or (at your |
|
308d34e9f07d
Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents:
5176
diff
changeset
|
10 option) any later version. |
| 414 | 11 |
| 12 XEmacs is distributed in the hope that it will be useful, but WITHOUT | |
| 13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
| 14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
| 15 for more details. | |
| 16 | |
| 17 You should have received a copy of the GNU General Public License | |
|
5402
308d34e9f07d
Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents:
5176
diff
changeset
|
18 along with XEmacs. If not, see <http://www.gnu.org/licenses/>. */ |
| 414 | 19 |
| 20 /* Synched up with: Not synched with FSF. */ | |
| 21 | |
| 22 #include <config.h> | |
| 23 #include "lisp.h" | |
| 24 | |
| 25 #include "buffer.h" | |
| 872 | 26 #include "device-impl.h" |
| 442 | 27 #include "extents.h" |
| 414 | 28 #include "console.h" |
|
5176
8b2f75cecb89
rename objects* (.c, .h and .el files) to fontcolor*
Ben Wing <ben@xemacs.org>
parents:
3025
diff
changeset
|
29 #include "fontcolor.h" |
| 414 | 30 |
| 31 #include "frame.h" | |
| 32 #include "opaque.h" | |
| 33 #include "select.h" | |
| 34 | |
| 442 | 35 /* X Atoms */ |
| 414 | 36 Lisp_Object QPRIMARY, QSECONDARY, QSTRING, QINTEGER, QCLIPBOARD, QTIMESTAMP, |
| 37 QTEXT, QDELETE, QMULTIPLE, QINCR, QEMACS_TMP, QTARGETS, QATOM, QNULL, | |
| 38 QATOM_PAIR, QCOMPOUND_TEXT; | |
| 39 | |
| 442 | 40 /* Windows clipboard formats */ |
| 41 Lisp_Object QCF_TEXT, QCF_BITMAP, QCF_METAFILEPICT, QCF_SYLK, QCF_DIF, | |
| 42 QCF_TIFF, QCF_OEMTEXT, QCF_DIB, QCF_DIBV5, QCF_PALETTE, QCF_PENDATA, | |
| 43 QCF_RIFF, QCF_WAVE, QCF_UNICODETEXT, QCF_ENHMETAFILE, QCF_HDROP, QCF_LOCALE, | |
| 44 QCF_OWNERDISPLAY, QCF_DSPTEXT, QCF_DSPBITMAP, QCF_DSPMETAFILEPICT, | |
| 45 QCF_DSPENHMETAFILE; | |
| 46 | |
| 47 /* Selection strategy symbols */ | |
| 48 Lisp_Object Qreplace_all, Qreplace_existing; | |
| 49 | |
| 414 | 50 /* "Selection owner couldn't convert selection" */ |
| 51 Lisp_Object Qselection_conversion_error; | |
| 52 | |
| 442 | 53 /* A couple of Lisp functions */ |
| 54 Lisp_Object Qselect_convert_in, Qselect_convert_out, Qselect_coerce; | |
| 55 | |
| 56 /* These are alists whose CARs are selection-types (whose names are the same | |
| 57 as the names of X Atoms or Windows clipboard formats) and whose CDRs are | |
| 58 the names of Lisp functions to call to convert the given Emacs selection | |
| 59 value to a string representing the given selection type. This is for | |
| 60 elisp-level extension of the emacs selection handling. | |
| 414 | 61 */ |
| 442 | 62 Lisp_Object Vselection_converter_out_alist; |
| 63 Lisp_Object Vselection_converter_in_alist; | |
| 64 Lisp_Object Vselection_coercion_alist; | |
| 65 Lisp_Object Vselection_appender_alist; | |
| 66 Lisp_Object Vselection_buffer_killed_alist; | |
| 67 Lisp_Object Vselection_coercible_types; | |
| 414 | 68 |
| 69 Lisp_Object Vlost_selection_hooks; | |
| 70 | |
| 71 /* This is an association list whose elements are of the form | |
| 72 ( selection-name selection-value selection-timestamp ) | |
| 73 selection-name is a lisp symbol, whose name is the name of an X Atom. | |
| 442 | 74 selection-value is a list of cons pairs that emacs owns for that selection. |
| 75 Each pair consists of (type . value), where type is nil or a | |
| 76 selection data type, and value is any type of Lisp object. | |
| 414 | 77 selection-timestamp is the time at which emacs began owning this selection, |
| 78 as a cons of two 16-bit numbers (making a 32 bit time). | |
| 79 If there is an entry in this alist, then it can be assumed that emacs owns | |
| 80 that selection. | |
| 81 The only (eq) parts of this list that are visible from elisp are the | |
| 82 selection-values. | |
| 83 */ | |
| 84 Lisp_Object Vselection_alist; | |
| 85 | |
| 442 | 86 /* Given a selection-name and desired type, this looks up our local copy of |
| 87 the selection value and converts it to the type. */ | |
| 414 | 88 static Lisp_Object |
| 89 get_local_selection (Lisp_Object selection_symbol, Lisp_Object target_type) | |
| 90 { | |
| 91 Lisp_Object local_value = assq_no_quit (selection_symbol, Vselection_alist); | |
| 92 | |
| 442 | 93 if (!NILP (local_value)) |
| 94 { | |
| 95 Lisp_Object value_list = XCAR (XCDR (local_value)); | |
| 96 Lisp_Object value; | |
| 414 | 97 |
| 442 | 98 /* First try to find an entry of the appropriate type */ |
| 99 value = assq_no_quit (target_type, value_list); | |
| 100 | |
| 101 if (!NILP (value)) | |
| 102 return XCDR (value); | |
| 414 | 103 } |
| 104 | |
| 442 | 105 return Qnil; |
| 414 | 106 } |
| 107 | |
| 3025 | 108 /* #### Should perhaps handle `MULTIPLE'. The code below is now completely |
| 442 | 109 broken due to a re-organization of get_local_selection, but I've left |
| 110 it here should anyone show an interest - ajh */ | |
| 111 #if 0 | |
| 112 else if (CONSP (target_type) && | |
| 113 XCAR (target_type) == QMULTIPLE) | |
| 114 { | |
| 115 Lisp_Object pairs = XCDR (target_type); | |
| 116 int len = XVECTOR_LENGTH (pairs); | |
| 117 int i; | |
| 118 /* If the target is MULTIPLE, then target_type looks like | |
| 119 (MULTIPLE . [[SELECTION1 TARGET1] [SELECTION2 TARGET2] ... ]) | |
| 120 We modify the second element of each pair in the vector and | |
| 121 return it as [[SELECTION1 <value1>] [SELECTION2 <value2>] ... ] | |
| 122 */ | |
| 123 for (i = 0; i < len; i++) | |
| 124 { | |
| 125 Lisp_Object pair = XVECTOR_DATA (pairs) [i]; | |
| 126 XVECTOR_DATA (pair) [1] = | |
| 127 x_get_local_selection (XVECTOR_DATA (pair) [0], | |
| 128 XVECTOR_DATA (pair) [1]); | |
| 129 } | |
| 130 return pairs; | |
| 131 } | |
| 132 #endif | |
| 133 | |
| 134 DEFUN ("own-selection-internal", Fown_selection_internal, 2, 5, 0, /* | |
| 444 | 135 Give the selection SELECTION-NAME the value SELECTION-VALUE. |
| 136 SELECTION-NAME is a symbol, typically PRIMARY, SECONDARY, or CLIPBOARD. | |
| 137 SELECTION-VALUE is typically a string, or a cons of two markers, but may be | |
| 442 | 138 anything that the functions on selection-converter-out-alist know about. |
| 444 | 139 Optional arg HOW-TO-ADD specifies how the selection will be combined |
| 843 | 140 with any existing selection(s) - see `own-selection' for more |
| 141 information. | |
| 444 | 142 Optional arg DATA-TYPE is a window-system-specific type. |
| 143 Optional arg DEVICE specifies the device on which to assert the selection. | |
| 144 It defaults to the selected device. | |
| 414 | 145 */ |
| 442 | 146 (selection_name, selection_value, how_to_add, data_type, device)) |
| 414 | 147 { |
| 442 | 148 Lisp_Object selection_time, selection_data, prev_value = Qnil, |
| 149 value_list = Qnil; | |
| 150 Lisp_Object prev_real_value = Qnil; | |
| 440 | 151 struct gcpro gcpro1; |
| 458 | 152 int owned_p = 0; |
| 414 | 153 |
| 154 CHECK_SYMBOL (selection_name); | |
| 563 | 155 if (NILP (selection_value)) |
| 156 invalid_argument ("`selection-value' may not be nil", Qunbound); | |
| 414 | 157 |
| 158 if (NILP (device)) | |
| 159 device = Fselected_device (Qnil); | |
| 160 | |
| 442 | 161 if (!EQ (how_to_add, Qappend) && !EQ (how_to_add, Qt) |
| 162 && !EQ (how_to_add, Qreplace_existing) | |
| 163 && !EQ (how_to_add, Qreplace_all) && !NILP (how_to_add)) | |
| 563 | 164 invalid_constant ("`how-to-add' must be nil, append, replace_all, " |
| 165 "replace_existing or t", how_to_add); | |
| 442 | 166 |
| 167 #ifdef MULE | |
| 168 if (NILP (data_type)) | |
| 169 data_type = QCOMPOUND_TEXT; | |
| 170 #else | |
| 171 if (NILP (data_type)) | |
| 172 data_type = QSTRING; | |
| 173 #endif | |
| 174 | |
| 175 /* Examine the how-to-add argument */ | |
| 176 if (EQ (how_to_add, Qreplace_all) || NILP (how_to_add)) | |
| 177 { | |
| 178 Lisp_Object local_selection_data = assq_no_quit (selection_name, | |
| 179 Vselection_alist); | |
| 180 | |
| 181 if (!NILP (local_selection_data)) | |
| 182 { | |
| 458 | 183 owned_p = 1; |
| 442 | 184 /* Don't use Fdelq() as that may QUIT;. */ |
| 185 if (EQ (local_selection_data, Fcar (Vselection_alist))) | |
| 186 Vselection_alist = Fcdr (Vselection_alist); | |
| 187 else | |
| 188 { | |
| 189 Lisp_Object rest; | |
| 190 for (rest = Vselection_alist; !NILP (rest); rest = Fcdr (rest)) | |
| 191 if (EQ (local_selection_data, Fcar (XCDR (rest)))) | |
| 192 { | |
| 193 XCDR (rest) = Fcdr (XCDR (rest)); | |
| 194 break; | |
| 195 } | |
| 196 } | |
| 197 } | |
| 198 } | |
| 199 else | |
| 200 { | |
| 201 /* Look for a previous value */ | |
| 202 prev_value = assq_no_quit (selection_name, Vselection_alist); | |
| 203 | |
| 204 if (!NILP (prev_value)) | |
| 458 | 205 { |
| 206 owned_p = 1; | |
| 207 value_list = XCAR (XCDR (prev_value)); | |
| 208 } | |
| 440 | 209 |
| 442 | 210 if (!NILP (value_list)) |
| 211 prev_real_value = assq_no_quit (data_type, value_list); | |
| 212 } | |
| 213 | |
| 214 /* Append values if necessary */ | |
| 215 if (!NILP (value_list) && (EQ (how_to_add, Qappend) || EQ (how_to_add, Qt))) | |
| 414 | 216 { |
| 442 | 217 /* Did we have anything of this type previously? */ |
| 218 if (!NILP (prev_real_value)) | |
| 219 { | |
| 220 if ((NILP (data_type) && STRINGP (selection_value) | |
| 221 && STRINGP (XCDR (prev_real_value))) | |
| 222 || !NILP (data_type)) | |
| 223 { | |
| 224 Lisp_Object function = assq_no_quit (data_type, | |
| 225 Vselection_appender_alist); | |
| 226 | |
| 227 if (NILP (function)) | |
| 563 | 228 signal_error (Qinvalid_argument, |
| 229 "Cannot append selections of supplied types (no function)", | |
| 230 data_type); | |
| 442 | 231 |
| 232 function = XCDR (function); | |
| 233 | |
| 234 selection_value = call4 (function, | |
| 235 selection_name, | |
| 236 data_type, | |
| 237 XCDR (prev_real_value), | |
| 238 selection_value); | |
| 239 | |
| 240 if (NILP (selection_value)) | |
| 563 | 241 signal_error (Qinvalid_argument, |
| 242 "Cannot append selections of supplied types (function returned nil)", | |
| 243 data_type); | |
| 442 | 244 } |
| 245 else | |
| 563 | 246 signal_error_2 (Qinvalid_argument, "Cannot append selections of supplied types (data type nil and both values not strings)", |
| 247 XCDR (prev_real_value), | |
| 248 selection_value); | |
| 442 | 249 } |
| 250 | |
| 251 selection_data = Fcons (data_type, selection_value); | |
| 252 value_list = Fcons (selection_data, value_list); | |
| 253 } | |
| 254 | |
| 255 if (!NILP (prev_real_value)) | |
| 256 { | |
| 257 Lisp_Object rest; /* We know it isn't the CAR, so it's easy. */ | |
| 258 | |
| 259 /* Delete the old type entry from the list */ | |
| 260 for (rest = value_list; !NILP (rest); rest = Fcdr (rest)) | |
| 261 if (EQ (prev_real_value, Fcar (XCDR (rest)))) | |
| 414 | 262 { |
| 263 XCDR (rest) = Fcdr (XCDR (rest)); | |
| 264 break; | |
| 265 } | |
| 266 } | |
| 442 | 267 else |
| 268 { | |
| 269 value_list = Fcons (Fcons (data_type, selection_value), | |
| 270 value_list); | |
| 271 } | |
| 414 | 272 |
| 442 | 273 /* Complete the local cache update; note that we destructively |
| 274 modify the current list entry if there is one */ | |
| 275 if (NILP (prev_value)) | |
| 276 { | |
| 277 selection_data = list3 (selection_name, value_list, Qnil); | |
| 278 Vselection_alist = Fcons (selection_data, Vselection_alist); | |
| 279 } | |
| 280 else | |
| 281 { | |
| 282 selection_data = prev_value; | |
| 283 Fsetcar (XCDR (selection_data), value_list); | |
| 284 } | |
| 285 | |
| 286 GCPRO1 (selection_data); | |
| 287 | |
| 288 /* have to do device specific stuff last so that methods can access the | |
| 414 | 289 selection_alist */ |
| 2620 | 290 |
| 291 /* If you are re-implementing this for another redisplay type, either make | |
| 292 certain that the selection time will fit within thirty-two bits, or | |
| 293 redesign get-xemacs-selection-timestamp to return, say, a bignum, and | |
| 294 convert the device-specific timestamp to a bignum before storing it in | |
| 295 this list. The current practice is to blindly assume that the timestamp | |
| 296 is thirty-two bits, which will work for extant architectures. */ | |
| 297 | |
| 414 | 298 if (HAS_DEVMETH_P (XDEVICE (device), own_selection)) |
| 299 selection_time = DEVMETH (XDEVICE (device), own_selection, | |
| 442 | 300 (selection_name, selection_value, |
| 458 | 301 how_to_add, data_type, owned_p)); |
| 414 | 302 else |
| 303 selection_time = Qnil; | |
| 304 | |
| 305 Fsetcar (XCDR (XCDR (selection_data)), selection_time); | |
| 306 | |
| 440 | 307 UNGCPRO; |
| 308 | |
| 414 | 309 return selection_value; |
| 310 } | |
| 311 | |
| 442 | 312 DEFUN ("register-selection-data-type", Fregister_selection_data_type, 1,2,0, /* |
| 313 Register a new selection data type DATA-TYPE, optionally on the specified | |
| 314 DEVICE. Returns the device-specific data type identifier, or nil if the | |
| 315 device does not support this feature or the registration fails. */ | |
| 316 (data_type, device)) | |
| 317 { | |
| 318 /* Check arguments */ | |
| 319 CHECK_STRING (data_type); | |
| 320 | |
| 321 if (NILP (device)) | |
| 322 device = Fselected_device (Qnil); | |
| 323 | |
| 324 if (HAS_DEVMETH_P (XDEVICE (device), register_selection_data_type)) | |
| 325 return DEVMETH (XDEVICE (device), register_selection_data_type, | |
| 326 (data_type)); | |
| 327 else | |
| 328 return Qnil; | |
| 329 } | |
| 330 | |
| 331 DEFUN ("selection-data-type-name", Fselection_data_type_name, 1, 2, 0, /* | |
| 332 Retrieve the name of the specified selection data type DATA-TYPE, optionally | |
| 333 on the specified DEVICE. Returns either a string or a symbol on success, and | |
| 334 nil if the device does not support this feature or the type is not known. */ | |
| 335 (data_type, device)) | |
| 336 { | |
| 337 if (NILP (device)) | |
| 338 device = Fselected_device (Qnil); | |
| 339 | |
| 340 if (HAS_DEVMETH_P (XDEVICE (device), selection_data_type_name)) | |
| 341 return DEVMETH (XDEVICE (device), selection_data_type_name, (data_type)); | |
| 342 else | |
| 343 return Qnil; | |
| 344 } | |
| 345 | |
| 346 DEFUN ("available-selection-types", Favailable_selection_types, 1, 2, 0, /* | |
| 347 Retrieve a list of currently available types of selection associated with | |
| 348 the given SELECTION-NAME, optionally on the specified DEVICE. This list | |
| 349 does not take into account any possible conversions that might take place, | |
| 350 so it should be taken as a minimal estimate of what is available. | |
| 351 */ | |
| 352 (selection_name, device)) | |
| 353 { | |
| 354 Lisp_Object types = Qnil, rest; | |
| 355 struct gcpro gcpro1; | |
| 356 | |
| 357 CHECK_SYMBOL (selection_name); | |
| 358 | |
| 359 if (NILP (device)) | |
| 360 device = Fselected_device (Qnil); | |
| 361 | |
| 362 GCPRO1 (types); | |
| 363 | |
| 364 /* First check the device */ | |
| 365 if (HAS_DEVMETH_P (XDEVICE (device), available_selection_types)) | |
| 366 types = DEVMETH (XDEVICE (device), available_selection_types, | |
| 367 (selection_name)); | |
| 368 | |
| 369 /* Now look in the list */ | |
| 370 rest = assq_no_quit (selection_name, Vselection_alist); | |
| 371 | |
| 372 if (NILP (rest)) | |
| 373 { | |
| 374 UNGCPRO; | |
| 375 | |
| 376 return types; | |
| 377 } | |
| 378 | |
| 379 /* Examine the types and cons them onto the front of the list */ | |
| 380 for (rest = XCAR (XCDR (rest)); !NILP (rest); rest = XCDR (rest)) | |
| 381 { | |
| 382 Lisp_Object value = XCDR (XCAR (rest)); | |
| 383 Lisp_Object type = XCAR (XCAR (rest)); | |
| 384 | |
| 385 types = Fcons (type, types); | |
| 386 | |
| 387 if ((STRINGP (value) || EXTENTP (value)) | |
| 388 && (NILP (type) || EQ (type, QSTRING) | |
| 389 || EQ (type, QTEXT) || EQ (type, QCOMPOUND_TEXT))) | |
| 390 types = Fcons (QTEXT, Fcons (QCOMPOUND_TEXT, Fcons (QSTRING, types))); | |
| 391 else if (INTP (value) && NILP (type)) | |
| 392 types = Fcons (QINTEGER, types); | |
| 393 else if (SYMBOLP (value) && NILP (type)) | |
| 394 types = Fcons (QATOM, types); | |
| 395 } | |
| 396 | |
| 397 UNGCPRO; | |
| 398 | |
| 399 return types; | |
| 400 } | |
| 401 | |
| 414 | 402 /* remove a selection from our local copy |
| 403 */ | |
| 404 void | |
| 405 handle_selection_clear (Lisp_Object selection_symbol) | |
| 406 { | |
| 442 | 407 Lisp_Object local_selection_data = assq_no_quit (selection_symbol, |
| 408 Vselection_alist); | |
| 414 | 409 |
| 410 /* Well, we already believe that we don't own it, so that's just fine. */ | |
| 411 if (NILP (local_selection_data)) return; | |
| 412 | |
| 413 /* Otherwise, we're really honest and truly being told to drop it. | |
| 414 Don't use Fdelq() as that may QUIT;. | |
| 415 */ | |
| 416 if (EQ (local_selection_data, Fcar (Vselection_alist))) | |
| 417 Vselection_alist = Fcdr (Vselection_alist); | |
| 418 else | |
| 419 { | |
| 420 Lisp_Object rest; | |
| 421 for (rest = Vselection_alist; !NILP (rest); rest = Fcdr (rest)) | |
| 422 if (EQ (local_selection_data, Fcar (XCDR (rest)))) | |
| 423 { | |
| 424 XCDR (rest) = Fcdr (XCDR (rest)); | |
| 425 break; | |
| 426 } | |
| 427 } | |
| 428 | |
| 429 /* Let random lisp code notice that the selection has been stolen. | |
| 430 */ | |
| 431 { | |
| 432 Lisp_Object rest; | |
| 433 Lisp_Object val = Vlost_selection_hooks; | |
| 434 if (!UNBOUNDP (val) && !NILP (val)) | |
| 435 { | |
| 436 if (CONSP (val) && !EQ (XCAR (val), Qlambda)) | |
| 437 for (rest = val; !NILP (rest); rest = Fcdr (rest)) | |
| 438 call1 (Fcar (rest), selection_symbol); | |
| 439 else | |
| 440 call1 (val, selection_symbol); | |
| 441 } | |
| 442 } | |
| 443 } | |
| 444 | |
| 445 DEFUN ("disown-selection-internal", Fdisown_selection_internal, 1, 3, 0, /* | |
| 446 If we own the named selection, then disown it (make there be no selection). | |
| 447 */ | |
| 448 (selection_name, selection_time, device)) | |
| 449 { | |
| 450 if (NILP (assq_no_quit (selection_name, Vselection_alist))) | |
| 451 return Qnil; /* Don't disown the selection when we're not the owner. */ | |
| 452 | |
| 453 if (NILP (device)) | |
| 454 device = Fselected_device (Qnil); | |
| 455 | |
| 456 MAYBE_DEVMETH (XDEVICE (device), disown_selection, | |
| 457 (selection_name, selection_time)); | |
| 442 | 458 |
| 414 | 459 handle_selection_clear (selection_name); |
| 460 | |
| 461 return Qt; | |
| 462 } | |
| 463 | |
| 464 DEFUN ("selection-owner-p", Fselection_owner_p, 0, 1, 0, /* | |
| 444 | 465 Return t if the current emacs process owns SELECTION. |
| 466 SELECTION should be the name of the selection in question, typically one of | |
| 414 | 467 the symbols PRIMARY, SECONDARY, or CLIPBOARD. (For convenience, the symbol |
| 468 nil is the same as PRIMARY, and t is the same as SECONDARY.) | |
| 469 */ | |
| 470 (selection)) | |
| 471 { | |
| 472 CHECK_SYMBOL (selection); | |
| 473 if (EQ (selection, Qnil)) selection = QPRIMARY; | |
| 474 else if (EQ (selection, Qt)) selection = QSECONDARY; | |
| 475 | |
| 476 return NILP (Fassq (selection, Vselection_alist)) ? Qnil : Qt; | |
| 477 } | |
| 478 | |
| 442 | 479 DEFUN ("selection-exists-p", Fselection_exists_p, 0, 3, 0, /* |
| 444 | 480 Whether there is currently an owner for SELECTION. |
| 481 SELECTION should be the name of the selection in question, typically one of | |
| 414 | 482 the symbols PRIMARY, SECONDARY, or CLIPBOARD. (For convenience, the symbol |
| 483 nil is the same as PRIMARY, and t is the same as SECONDARY.) | |
| 444 | 484 Optionally, the window-system DATA-TYPE and the DEVICE may be specified. |
| 414 | 485 */ |
| 442 | 486 (selection, data_type, device)) |
| 414 | 487 { |
| 488 CHECK_SYMBOL (selection); | |
| 442 | 489 if (NILP (data_type) |
| 490 && !NILP (Fselection_owner_p (selection))) | |
| 414 | 491 return Qt; |
| 492 | |
| 493 if (NILP (device)) | |
| 494 device = Fselected_device (Qnil); | |
| 495 | |
| 496 return HAS_DEVMETH_P (XDEVICE (device), selection_exists_p) ? | |
| 442 | 497 DEVMETH (XDEVICE (device), selection_exists_p, (selection, data_type)) |
| 414 | 498 : Qnil; |
| 499 } | |
| 500 | |
| 2620 | 501 Lisp_Object |
| 502 get_selection_raw_time(Lisp_Object selection) | |
| 503 { | |
| 504 Lisp_Object local_value = assq_no_quit (selection, Vselection_alist); | |
| 505 | |
| 506 if (!NILP (local_value)) | |
| 507 { | |
| 508 return XCAR (XCDR (XCDR (local_value))); | |
| 509 } | |
| 510 return Qnil; | |
| 511 } | |
| 512 | |
| 442 | 513 /* Get the timestamp of the given selection */ |
| 2620 | 514 DEFUN ("get-xemacs-selection-timestamp", Fget_selection_timestamp, 1, 1, 0, /* |
| 2757 | 515 Return timestamp for SELECTION, if it belongs to XEmacs and exists. |
| 2620 | 516 |
| 517 The timestamp is a cons of two integers, the first being the higher-order | |
| 518 sixteen bits of the device-specific thirty-two-bit quantity, the second | |
| 519 being the lower-order sixteen bits of same. Expect to see this API change | |
| 520 when and if redisplay on a window system with timestamps wider than 32bits | |
| 521 happens. | |
| 442 | 522 */ |
| 523 (selection)) | |
| 524 { | |
| 2620 | 525 Lisp_Object val = get_selection_raw_time(selection); |
| 442 | 526 |
| 2620 | 527 if (!NILP (val)) |
| 528 { | |
| 529 return word_to_lisp(* (UINT_32_BIT *) XOPAQUE_DATA (val)); | |
| 530 } | |
| 442 | 531 |
| 532 return Qnil; | |
| 533 } | |
| 534 | |
| 414 | 535 /* Request the selection value from the owner. If we are the owner, |
| 843 | 536 simply return our selection value. If we are not the owner, this |
| 537 will block until all of the data has arrived. | |
| 414 | 538 */ |
| 539 DEFUN ("get-selection-internal", Fget_selection_internal, 2, 3, 0, /* | |
| 540 Return text selected from some window-system window. | |
| 444 | 541 SELECTION is a symbol, typically PRIMARY, SECONDARY, or CLIPBOARD. |
| 542 TARGET-TYPE is the type of data desired, typically STRING or COMPOUND_TEXT. | |
| 414 | 543 Under Mule, if the resultant data comes back as 8-bit data in type |
| 544 TEXT or COMPOUND_TEXT, it will be decoded as Compound Text. | |
| 545 */ | |
| 444 | 546 (selection, target_type, device)) |
| 414 | 547 { |
| 548 /* This function can GC */ | |
| 549 Lisp_Object val = Qnil; | |
| 550 struct gcpro gcpro1, gcpro2; | |
| 442 | 551 GCPRO2 (target_type, val); |
| 444 | 552 CHECK_SYMBOL (selection); |
| 414 | 553 |
| 554 if (NILP (device)) | |
| 555 device = Fselected_device (Qnil); | |
| 556 | |
| 442 | 557 #ifdef MULE |
| 558 if (NILP (target_type)) | |
| 559 target_type = QCOMPOUND_TEXT; | |
| 560 #else | |
| 561 if (NILP (target_type)) | |
| 562 target_type = QSTRING; | |
| 563 #endif | |
| 564 | |
| 414 | 565 #if 0 /* #### MULTIPLE doesn't work yet and probably never will */ |
| 566 if (CONSP (target_type) && | |
| 567 XCAR (target_type) == QMULTIPLE) | |
| 568 { | |
| 569 CHECK_VECTOR (XCDR (target_type)); | |
| 570 /* So we don't destructively modify this... */ | |
| 571 target_type = copy_multiple_data (target_type); | |
| 572 } | |
| 573 #endif | |
| 442 | 574 |
| 575 /* Used to check that target_type was a symbol. This is no longer | |
| 576 necessarily the case, because the type might be registered with | |
| 577 the device (in which case target_type would be a device-specific | |
| 578 identifier - probably an integer) - ajh */ | |
| 414 | 579 |
| 444 | 580 val = get_local_selection (selection, target_type); |
| 414 | 581 |
| 442 | 582 if (!NILP (val)) |
| 414 | 583 { |
| 442 | 584 /* If we get something from the local cache, we may need to convert |
| 585 it slightly - to do this, we call select-coerce */ | |
| 843 | 586 val = call3 (Qselect_coerce, selection, target_type, val); |
| 442 | 587 } |
| 588 else if (HAS_DEVMETH_P (XDEVICE (device), get_foreign_selection)) | |
| 589 { | |
| 590 /* Nothing in the local cache; try the window system */ | |
| 414 | 591 val = DEVMETH (XDEVICE (device), get_foreign_selection, |
| 444 | 592 (selection, target_type)); |
| 414 | 593 } |
| 442 | 594 |
| 595 if (NILP (val)) | |
| 414 | 596 { |
| 442 | 597 /* Still nothing. Try coercion. */ |
| 598 | |
| 599 /* Try looking in selection-coercible-types to see if any of | |
| 600 them are present for this selection. We try them *in order*; | |
| 601 the first for which a conversion succeeds gets returned. */ | |
| 602 EXTERNAL_LIST_LOOP_2 (element, Vselection_coercible_types) | |
| 414 | 603 { |
| 444 | 604 val = get_local_selection (selection, element); |
| 442 | 605 |
| 606 if (NILP (val)) | |
| 607 continue; | |
| 608 | |
| 444 | 609 val = call3 (Qselect_coerce, selection, target_type, val); |
| 442 | 610 |
| 611 if (!NILP (val)) | |
| 612 break; | |
| 414 | 613 } |
| 614 } | |
| 442 | 615 |
| 616 /* Used to call clean_local_selection here... but that really belonged | |
| 617 in Lisp (so the equivalent is now built-in to the INTEGER conversion | |
| 618 function select-convert-from-integer) - ajh */ | |
| 619 | |
| 414 | 620 UNGCPRO; |
| 621 return val; | |
| 622 } | |
| 623 | |
| 442 | 624 /* These are convenient interfaces to the lisp code in select.el; |
| 625 this way we can rename them easily rather than having to hunt everywhere. | |
| 626 Also, this gives us access to get_local_selection so that convert_out | |
| 627 can retrieve the internal selection value automatically if passed a | |
| 628 value of Qnil. */ | |
| 629 Lisp_Object | |
| 630 select_convert_in (Lisp_Object selection, | |
| 631 Lisp_Object type, | |
| 632 Lisp_Object value) | |
| 633 { | |
| 634 return call3 (Qselect_convert_in, selection, type, value); | |
| 635 } | |
| 636 | |
| 637 Lisp_Object | |
| 638 select_coerce (Lisp_Object selection, | |
| 639 Lisp_Object type, | |
| 640 Lisp_Object value) | |
| 641 { | |
| 642 return call3 (Qselect_coerce, selection, type, value); | |
| 643 } | |
| 644 | |
| 645 Lisp_Object | |
| 646 select_convert_out (Lisp_Object selection, | |
| 647 Lisp_Object type, | |
| 648 Lisp_Object value) | |
| 649 { | |
| 650 if (NILP (value)) | |
| 651 value = get_local_selection (selection, type); | |
| 652 | |
| 653 if (NILP (value)) | |
| 654 { | |
| 655 /* Try looking in selection-coercible-types to see if any of | |
| 656 them are present for this selection. We try them *in order*; | |
| 657 the first for which a conversion succeeds gets returned. */ | |
| 658 EXTERNAL_LIST_LOOP_2 (element, Vselection_coercible_types) | |
| 659 { | |
| 660 Lisp_Object ret; | |
| 661 | |
| 662 value = get_local_selection (selection, element); | |
| 663 | |
| 664 if (NILP (value)) | |
| 665 continue; | |
| 666 | |
| 667 ret = call3 (Qselect_convert_out, selection, type, value); | |
| 668 | |
| 669 if (!NILP (ret)) | |
| 670 return ret; | |
| 671 } | |
| 672 | |
| 673 return Qnil; | |
| 674 } | |
| 675 | |
| 676 return call3 (Qselect_convert_out, selection, type, value); | |
| 677 } | |
| 678 | |
| 679 | |
| 680 /* Gets called from kill-buffer; this lets us dispose of buffer-dependent | |
| 681 selections (or alternatively make them independent of the buffer) when | |
| 682 it gets vaped. */ | |
| 683 void | |
| 684 select_notify_buffer_kill (Lisp_Object buffer) | |
| 685 { | |
| 686 Lisp_Object rest; | |
| 687 struct gcpro gcpro1, gcpro2, gcpro3; | |
| 688 | |
| 689 /* For each element of Vselection_alist */ | |
| 690 for (rest = Vselection_alist; | |
| 691 !NILP (rest);) | |
| 692 { | |
| 693 Lisp_Object selection, values, prev = Qnil; | |
| 694 | |
| 695 selection = XCAR (rest); | |
| 696 | |
| 697 for (values = XCAR (XCDR (selection)); | |
| 698 !NILP (values); | |
| 699 values = XCDR (values)) | |
| 700 { | |
| 701 Lisp_Object value, handler_fn; | |
| 702 | |
| 703 /* Extract the (type . value) pair. */ | |
| 704 value = XCAR (values); | |
| 705 | |
| 706 /* Find the handler function (if any). */ | |
| 707 handler_fn = Fcdr (Fassq (XCAR (value), | |
| 708 Vselection_buffer_killed_alist)); | |
| 709 | |
| 710 if (!NILP (handler_fn)) | |
| 711 { | |
| 712 Lisp_Object newval; | |
| 713 | |
| 714 /* Protect ourselves, just in case some tomfool calls | |
| 715 own-selection from with the buffer-killed handler, then | |
| 716 causes a GC. Just as a note, *don't do this*. */ | |
| 717 GCPRO3 (rest, values, value); | |
| 718 | |
| 719 newval = call4 (handler_fn, XCAR (selection), XCAR (value), | |
| 720 XCDR (value), buffer); | |
| 721 | |
| 722 UNGCPRO; | |
| 723 | |
| 724 /* Set or delete the value (by destructively modifying | |
| 725 the list). */ | |
| 726 if (!NILP (newval)) | |
| 727 { | |
| 728 Fsetcdr (value, newval); | |
| 729 | |
| 730 prev = values; | |
| 731 } | |
| 732 else | |
| 733 { | |
| 734 if (NILP (prev)) | |
| 735 Fsetcar (XCDR (selection), XCDR (values)); | |
| 736 else | |
| 737 Fsetcdr (prev, XCDR (values)); | |
| 738 } | |
| 739 } | |
| 740 else | |
| 741 prev = values; | |
| 742 } | |
| 743 | |
| 744 /* If we have no values for this selection */ | |
| 745 if (NILP (XCAR (XCDR (selection)))) | |
| 746 { | |
| 747 /* Move on to the next element *first* */ | |
| 748 rest = XCDR (rest); | |
| 749 | |
| 750 /* Protect it and disown this selection */ | |
| 751 GCPRO1 (rest); | |
| 752 | |
| 753 Fdisown_selection_internal (XCAR (selection), Qnil, Qnil); | |
| 754 | |
| 755 UNGCPRO; | |
| 756 } | |
| 757 else | |
| 758 rest = XCDR (rest); | |
| 759 } | |
| 760 } | |
| 761 | |
| 762 | |
| 414 | 763 void |
| 764 syms_of_select (void) | |
| 765 { | |
| 766 DEFSUBR (Fown_selection_internal); | |
| 767 DEFSUBR (Fget_selection_internal); | |
| 442 | 768 DEFSUBR (Fget_selection_timestamp); |
| 414 | 769 DEFSUBR (Fselection_exists_p); |
| 770 DEFSUBR (Fdisown_selection_internal); | |
| 771 DEFSUBR (Fselection_owner_p); | |
| 442 | 772 DEFSUBR (Favailable_selection_types); |
| 773 DEFSUBR (Fregister_selection_data_type); | |
| 774 DEFSUBR (Fselection_data_type_name); | |
| 414 | 775 |
| 442 | 776 /* Lisp Functions */ |
| 563 | 777 DEFSYMBOL (Qselect_convert_in); |
| 778 DEFSYMBOL (Qselect_convert_out); | |
| 779 DEFSYMBOL (Qselect_coerce); | |
| 442 | 780 |
| 781 /* X Atoms */ | |
| 563 | 782 DEFSYMBOL (QPRIMARY); |
| 783 DEFSYMBOL (QSECONDARY); | |
| 784 DEFSYMBOL (QSTRING); | |
| 785 DEFSYMBOL (QINTEGER); | |
| 786 DEFSYMBOL (QCLIPBOARD); | |
| 787 DEFSYMBOL (QTIMESTAMP); | |
| 788 DEFSYMBOL (QTEXT); | |
| 789 DEFSYMBOL (QDELETE); | |
| 790 DEFSYMBOL (QMULTIPLE); | |
| 791 DEFSYMBOL (QINCR); | |
| 414 | 792 defsymbol (&QEMACS_TMP, "_EMACS_TMP_"); |
| 563 | 793 DEFSYMBOL (QTARGETS); |
| 794 DEFSYMBOL (QATOM); | |
| 414 | 795 defsymbol (&QATOM_PAIR, "ATOM_PAIR"); |
| 796 defsymbol (&QCOMPOUND_TEXT, "COMPOUND_TEXT"); | |
| 563 | 797 DEFSYMBOL (QNULL); |
| 414 | 798 |
| 442 | 799 /* Windows formats - these all start with CF_ */ |
| 800 defsymbol (&QCF_TEXT, "CF_TEXT"); | |
| 801 defsymbol (&QCF_BITMAP, "CF_BITMAP"); | |
| 802 defsymbol (&QCF_METAFILEPICT, "CF_METAFILEPICT"); | |
| 803 defsymbol (&QCF_SYLK, "CF_SYLK"); | |
| 804 defsymbol (&QCF_DIF, "CF_DIF"); | |
| 805 defsymbol (&QCF_TIFF, "CF_TIFF"); | |
| 806 defsymbol (&QCF_OEMTEXT, "CF_OEMTEXT"); | |
| 807 defsymbol (&QCF_DIB, "CF_DIB"); | |
| 808 defsymbol (&QCF_DIBV5, "CF_DIBV5"); | |
| 809 defsymbol (&QCF_PALETTE, "CF_PALETTE"); | |
| 810 defsymbol (&QCF_PENDATA, "CF_PENDATA"); | |
| 811 defsymbol (&QCF_RIFF, "CF_RIFF"); | |
| 812 defsymbol (&QCF_WAVE, "CF_WAVE"); | |
| 813 defsymbol (&QCF_UNICODETEXT, "CF_UNICODETEXT"); | |
| 814 defsymbol (&QCF_ENHMETAFILE, "CF_ENHMETAFILE"); | |
| 815 defsymbol (&QCF_HDROP, "CF_HDROP"); | |
| 816 defsymbol (&QCF_LOCALE, "CF_LOCALE"); | |
| 817 defsymbol (&QCF_OWNERDISPLAY, "CF_OWNERDISPLAY"); | |
| 818 defsymbol (&QCF_DSPTEXT, "CF_DSPTEXT"); | |
| 819 defsymbol (&QCF_DSPBITMAP, "CF_DSPBITMAP"); | |
| 820 defsymbol (&QCF_DSPMETAFILEPICT, "CF_DSPMETAFILEPICT"); | |
| 821 defsymbol (&QCF_DSPENHMETAFILE, "CF_DSPENHMETAFILE"); | |
| 822 | |
| 823 /* Selection strategies */ | |
| 563 | 824 DEFSYMBOL (Qreplace_all); |
| 825 DEFSYMBOL (Qreplace_existing); | |
| 442 | 826 |
| 563 | 827 DEFERROR_STANDARD (Qselection_conversion_error, Qconversion_error); |
| 414 | 828 } |
| 829 | |
| 830 void | |
| 831 vars_of_select (void) | |
| 832 { | |
| 833 Vselection_alist = Qnil; | |
| 834 staticpro (&Vselection_alist); | |
| 835 | |
| 442 | 836 DEFVAR_LISP ("selection-converter-alist", &Vselection_converter_out_alist /* |
| 837 An alist associating selection-types (such as STRING and TIMESTAMP) with | |
| 838 functions. This is an alias for `selection-converter-out-alist', and should | |
| 839 be considered obsolete. Use the new name instead. */ ); | |
| 840 | |
| 841 DEFVAR_LISP ("selection-converter-out-alist", | |
| 842 &Vselection_converter_out_alist /* | |
| 414 | 843 An alist associating selection-types (such as STRING and TIMESTAMP) with |
| 844 functions. These functions will be called with three args: the name | |
| 845 of the selection (typically PRIMARY, SECONDARY, or CLIPBOARD); a | |
| 846 desired type to which the selection should be converted; and the local | |
| 442 | 847 selection value (whatever had been passed to `own-selection'). |
| 848 | |
| 849 The return type of these functions depends upon the device in question; | |
| 850 for mswindows, a string should be returned containing data in the requested | |
| 851 format, or nil to indicate that the conversion could not be done. Additionally, | |
| 852 it is permissible to return a cons of the form (DATA-TYPE . STRING) suggesting | |
| 853 a new data type to use instead. | |
| 854 | |
| 855 For X, the return value should be one of: | |
| 414 | 856 |
| 857 -- nil (the conversion could not be done) | |
| 858 -- a cons of a symbol and any of the following values; the symbol | |
| 859 explicitly specifies the type that will be sent. | |
| 860 -- a string (If the type is not specified, then if Mule support exists, | |
| 861 the string will be converted to Compound Text and sent in | |
| 3025 | 862 the `COMPOUND_TEXT' format; otherwise (no Mule support), |
| 863 the string will be left as-is and sent in the `STRING' | |
| 414 | 864 format. If the type is specified, the string will be |
| 865 left as-is (or converted to binary format under Mule). | |
| 866 In all cases, 8-bit data it sent.) | |
| 867 -- a character (With Mule support, will be converted to Compound Text | |
| 868 whether or not a type is specified. If a type is not | |
| 3025 | 869 specified, a type of `STRING' or `COMPOUND_TEXT' will be |
| 414 | 870 sent, as for strings.) |
| 3025 | 871 -- the symbol `NULL' (Indicates that there is no meaningful return value. |
| 872 Empty 32-bit data with a type of `NULL' will be sent.) | |
| 414 | 873 -- a symbol (Will be converted into an atom. If the type is not specified, |
| 3025 | 874 a type of `ATOM' will be sent.) |
| 414 | 875 -- an integer (Will be converted into a 16-bit or 32-bit integer depending |
| 876 on the value. If the type is not specified, a type of | |
| 3025 | 877 `INTEGER' will be sent.) |
| 414 | 878 -- a cons (HIGH . LOW) of integers (Will be converted into a 32-bit integer. |
| 879 If the type is not specified, a type of | |
| 3025 | 880 `INTEGER' will be sent.) |
| 414 | 881 -- a vector of symbols (Will be converted into a list of atoms. If the type |
| 3025 | 882 is not specified, a type of `ATOM' will be sent.) |
| 414 | 883 -- a vector of integers (Will be converted into a list of 16-bit integers. |
| 3025 | 884 If the type is not specified, a type of `INTEGER' |
| 414 | 885 will be sent.) |
| 886 -- a vector of integers and/or conses (HIGH . LOW) of integers | |
| 887 (Will be converted into a list of 16-bit integers. | |
| 3025 | 888 If the type is not specified, a type of `INTEGER' |
| 442 | 889 will be sent.) |
| 890 */ ); | |
| 891 Vselection_converter_out_alist = Qnil; | |
| 892 | |
| 893 DEFVAR_LISP ("selection-converter-in-alist", | |
| 894 &Vselection_converter_in_alist /* | |
| 895 An alist associating selection-types (such as STRING and TIMESTAMP) with | |
| 896 functions. These functions will be called with three args: the name | |
| 897 of the selection (typically PRIMARY, SECONDARY or CLIPBOARD); the | |
| 898 type from which the selection should be converted; and the selection | |
| 899 value. These functions should return a suitable representation of the | |
| 900 value, or nil to indicate that the conversion was not possible. | |
| 901 | |
| 902 See also `selection-converter-out-alist'. */ ); | |
| 903 Vselection_converter_in_alist = Qnil; | |
| 904 | |
| 905 DEFVAR_LISP ("selection-coercion-alist", | |
| 906 &Vselection_coercion_alist /* | |
| 907 An alist associating selection-types (such as STRING and TIMESTAMP) with | |
| 908 functions. These functions will be called with three args; the name | |
| 909 of the selection (typically PRIMARY, SECONDARY or CLIPBOARD); the type | |
| 910 from which the selection should be converted, and the selection value. | |
| 911 The value passed will be *exactly the same value* that was given to | |
| 912 `own-selection'; it should be converted into something suitable for | |
| 913 return to a program calling `get-selection' with the appropriate | |
| 914 parameters. | |
| 915 | |
| 916 See also `selection-converter-in-alist' and | |
| 917 `selection-converter-out-alist'. */); | |
| 918 Vselection_coercion_alist = Qnil; | |
| 919 | |
| 920 DEFVAR_LISP ("selection-appender-alist", | |
| 921 &Vselection_appender_alist /* | |
| 922 An alist associating selection-types (such as STRING and TIMESTAMP) with | |
| 923 functions. These functions will be called with four args; the name | |
| 924 of the selection (typically PRIMARY, SECONDARY or CLIPBOARD); the type | |
| 925 of the selection; and two selection values. The functions are expected to | |
| 926 return a value representing the catenation of the two values, or nil to | |
| 927 indicate that this was not possible. */ ); | |
| 928 Vselection_appender_alist = Qnil; | |
| 929 | |
| 930 DEFVAR_LISP ("selection-buffer-killed-alist", | |
| 931 &Vselection_buffer_killed_alist /* | |
| 932 An alist associating selection-types (such as STRING and TIMESTAMP) with | |
| 933 functions. These functions will be called whenever a buffer is killed, | |
| 934 with four args: the name of the selection (typically PRIMARY, SECONDARY | |
| 935 or CLIPBOARD); the type of the selection; the value of the selection; and | |
| 936 the buffer that has just been killed. These functions should return a new | |
| 937 selection value, or nil to indicate that the selection value should be | |
| 938 deleted. */ ); | |
| 939 Vselection_buffer_killed_alist = Qnil; | |
| 940 | |
| 941 DEFVAR_LISP ("selection-coercible-types", | |
| 942 &Vselection_coercible_types /* | |
| 943 A list of selection types that are coercible---that is, types that may be | |
| 944 automatically converted to another type. Selection values with types in this | |
| 945 list may be subject to conversion attempts to other types. */ ); | |
| 946 Vselection_coercible_types = Qnil; | |
| 414 | 947 |
| 948 DEFVAR_LISP ("lost-selection-hooks", &Vlost_selection_hooks /* | |
| 949 A function or functions to be called after we have been notified | |
| 950 that we have lost the selection. The function(s) will be called with one | |
| 951 argument, a symbol naming the selection (typically PRIMARY, SECONDARY, or | |
| 952 CLIPBOARD). | |
| 953 */ ); | |
| 954 Vlost_selection_hooks = Qunbound; | |
| 955 } |
