Mercurial > hg > xemacs-beta
annotate src/select-xlike-inc.c @ 5533:11da5b828d10
shell-command and shell-command-on-region API compliant with FSF 23.3.1
| author | Mats Lidell <mats.lidell@cag.se> |
|---|---|
| date | Sun, 31 Jul 2011 01:29:09 +0200 |
| parents | 308d34e9f07d |
| children | 56144c8593a8 |
| rev | line source |
|---|---|
| 647 | 1 /* Selection processing for XEmacs -- common btwn select-x.c and select-gtk.c |
| 2 Copyright (C) 1990, 1991, 1992, 1993, 1994 Free Software Foundation, Inc. | |
| 3 | |
| 4 This file is part of XEmacs. | |
| 5 | |
|
5402
308d34e9f07d
Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents:
4985
diff
changeset
|
6 XEmacs is free software: you can redistribute it and/or modify it |
| 647 | 7 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:
4985
diff
changeset
|
8 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:
4985
diff
changeset
|
9 option) any later version. |
| 647 | 10 |
| 11 XEmacs is distributed in the hope that it will be useful, but WITHOUT | |
| 12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
| 13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
| 14 for more details. | |
| 15 | |
| 16 You should have received a copy of the GNU General Public License | |
|
5402
308d34e9f07d
Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents:
4985
diff
changeset
|
17 along with XEmacs. If not, see <http://www.gnu.org/licenses/>. */ |
| 647 | 18 |
| 19 /* Synched up with: Not synched with FSF. */ | |
| 20 | |
| 4984 | 21 #ifdef THIS_IS_X |
| 647 | 22 #define XE_ATOM_TYPE Atom |
| 23 #define XE_ATOM_TO_SYMBOL x_atom_to_symbol | |
| 24 #define XE_SYMBOL_TO_ATOM symbol_to_x_atom | |
| 25 #else | |
| 26 #define XE_ATOM_TYPE GdkAtom | |
| 27 #define XE_ATOM_TO_SYMBOL atom_to_symbol | |
| 28 #define XE_SYMBOL_TO_ATOM symbol_to_gtk_atom | |
| 4984 | 29 #endif /* THIS_IS_X */ |
| 647 | 30 |
| 31 /* #### These are going to move into Lisp code(!) with the aid of | |
| 32 some new functions I'm working on - ajh */ | |
| 33 | |
| 34 /* These functions convert from the selection data read from the server into | |
| 35 something that we can use from elisp, and vice versa. | |
| 36 | |
| 37 Type: Format: Size: Elisp Type: | |
| 38 ----- ------- ----- ----------- | |
| 39 * 8 * String | |
| 40 ATOM 32 1 Symbol | |
| 41 ATOM 32 > 1 Vector of Symbols | |
| 42 * 16 1 Integer | |
| 43 * 16 > 1 Vector of Integers | |
| 44 * 32 1 if <=16 bits: Integer | |
| 45 if > 16 bits: Cons of top16, bot16 | |
| 46 * 32 > 1 Vector of the above | |
| 47 | |
| 3833 | 48 NOTE NOTE NOTE: |
| 49 Format == 32 means that the buffer will be C longs, which need not be | |
| 50 32-bit quantities. See the note in select-x.c (x_get_window_property). | |
| 51 | |
| 647 | 52 When converting a Lisp number to C, it is assumed to be of format 16 if |
| 53 it is an integer, and of format 32 if it is a cons of two integers. | |
| 54 | |
| 55 When converting a vector of numbers from Elisp to C, it is assumed to be | |
| 56 of format 16 if every element in the vector is an integer, and is assumed | |
| 57 to be of format 32 if any element is a cons of two integers. | |
| 58 | |
| 59 When converting an object to C, it may be of the form (SYMBOL . <data>) | |
| 60 where SYMBOL is what we should claim that the type is. Format and | |
| 61 representation are as above. | |
| 62 | |
| 63 NOTE: Under Mule, when someone shoves us a string without a type, we | |
| 3025 | 64 set the type to `COMPOUND_TEXT' and automatically convert to Compound |
| 647 | 65 Text. If the string has a type, we assume that the user wants the |
| 66 data sent as-is so we just do "binary" conversion. | |
| 67 */ | |
| 68 | |
| 69 | |
| 70 static Lisp_Object | |
| 71 selection_data_to_lisp_data (struct device *d, | |
| 2367 | 72 Rawbyte *data, |
| 665 | 73 Bytecount size, |
| 647 | 74 XE_ATOM_TYPE type, |
| 75 int format) | |
| 76 { | |
| 4984 | 77 #ifdef THIS_IS_X |
| 647 | 78 if (type == DEVICE_XATOM_NULL (d)) |
| 79 return QNULL; | |
| 80 | |
| 81 /* Convert any 8-bit data to a string, for compactness. */ | |
| 82 else if (format == 8) | |
|
4953
304aebb79cd3
function renamings to track names of char typedefs
Ben Wing <ben@xemacs.org>
parents:
3833
diff
changeset
|
83 return make_extstring ((Extbyte *) data, size, |
| 647 | 84 type == DEVICE_XATOM_TEXT (d) || |
| 85 type == DEVICE_XATOM_COMPOUND_TEXT (d) | |
| 86 ? Qctext : Qbinary); | |
| 87 | |
| 88 /* Convert a single atom to a Lisp Symbol. | |
| 89 Convert a set of atoms to a vector of symbols. */ | |
| 90 else if (type == XA_ATOM) | |
| 91 #else | |
| 92 if (type == gdk_atom_intern ("NULL", 0)) | |
| 93 return QNULL; | |
| 94 | |
| 95 /* Convert any 8-bit data to a string, for compactness. */ | |
| 96 else if (format == 8) | |
|
4953
304aebb79cd3
function renamings to track names of char typedefs
Ben Wing <ben@xemacs.org>
parents:
3833
diff
changeset
|
97 return make_extstring ((Extbyte *) data, size, |
| 647 | 98 ((type == gdk_atom_intern ("TEXT", FALSE)) || |
| 99 (type == gdk_atom_intern ("COMPOUND_TEXT", FALSE))) | |
| 100 ? Qctext : Qbinary); | |
| 101 | |
| 102 /* Convert a single atom to a Lisp Symbol. | |
| 103 Convert a set of atoms to a vector of symbols. */ | |
| 104 else if (type == gdk_atom_intern ("ATOM", FALSE)) | |
| 4984 | 105 #endif /* THIS_IS_X */ |
| 647 | 106 { |
| 107 if (size == sizeof (XE_ATOM_TYPE)) | |
| 108 return XE_ATOM_TO_SYMBOL (d, *((XE_ATOM_TYPE *) data)); | |
| 109 else | |
| 110 { | |
| 665 | 111 Elemcount i; |
| 112 Elemcount len = size / sizeof (XE_ATOM_TYPE); | |
| 647 | 113 Lisp_Object v = Fmake_vector (make_int (len), Qzero); |
| 114 for (i = 0; i < len; i++) | |
| 115 Faset (v, make_int (i), XE_ATOM_TO_SYMBOL (d, ((XE_ATOM_TYPE *) data) [i])); | |
| 116 return v; | |
| 117 } | |
| 118 } | |
| 119 | |
| 120 /* Convert a single 16 or small 32 bit number to a Lisp Int. | |
| 121 If the number is > 16 bits, convert it to a cons of integers, | |
| 122 16 bits in each half. | |
| 123 */ | |
| 124 else if (format == 32 && size == sizeof (long)) | |
| 125 return word_to_lisp (((unsigned long *) data) [0]); | |
| 126 else if (format == 16 && size == sizeof (short)) | |
| 127 return make_int ((int) (((unsigned short *) data) [0])); | |
| 128 | |
| 129 /* Convert any other kind of data to a vector of numbers, represented | |
| 130 as above (as an integer, or a cons of two 16 bit integers). | |
| 131 | |
| 132 #### Perhaps we should return the actual type to lisp as well. | |
| 133 | |
| 134 (x-get-selection-internal 'PRIMARY 'LINE_NUMBER) | |
| 135 ==> [4 4] | |
| 136 | |
| 137 and perhaps it should be | |
| 138 | |
| 139 (x-get-selection-internal 'PRIMARY 'LINE_NUMBER) | |
| 140 ==> (SPAN . [4 4]) | |
| 141 | |
| 142 Right now the fact that the return type was SPAN is discarded before | |
| 143 lisp code gets to see it. | |
| 144 */ | |
| 145 else if (format == 16) | |
| 146 { | |
| 665 | 147 Elemcount i; |
| 647 | 148 Lisp_Object v = make_vector (size / 4, Qzero); |
| 149 for (i = 0; i < size / 4; i++) | |
| 150 { | |
| 151 int j = (int) ((unsigned short *) data) [i]; | |
| 152 Faset (v, make_int (i), make_int (j)); | |
| 153 } | |
| 154 return v; | |
| 155 } | |
| 156 else | |
| 157 { | |
| 665 | 158 Elemcount i; |
| 647 | 159 Lisp_Object v = make_vector (size / 4, Qzero); |
| 160 for (i = 0; i < size / 4; i++) | |
| 161 { | |
| 162 unsigned long j = ((unsigned long *) data) [i]; | |
| 163 Faset (v, make_int (i), word_to_lisp (j)); | |
| 164 } | |
| 165 return v; | |
| 166 } | |
| 167 } | |
| 168 | |
| 169 | |
| 170 static void | |
| 171 lisp_data_to_selection_data (struct device *d, | |
| 172 Lisp_Object obj, | |
| 2367 | 173 Rawbyte **data_ret, |
| 647 | 174 XE_ATOM_TYPE *type_ret, |
| 665 | 175 Bytecount *size_ret, |
| 647 | 176 int *format_ret) |
| 177 { | |
| 178 Lisp_Object type = Qnil; | |
| 179 | |
| 180 if (CONSP (obj) && SYMBOLP (XCAR (obj))) | |
| 181 { | |
| 182 type = XCAR (obj); | |
| 183 obj = XCDR (obj); | |
| 184 if (CONSP (obj) && NILP (XCDR (obj))) | |
| 185 obj = XCAR (obj); | |
| 186 } | |
| 187 | |
| 188 if (EQ (obj, QNULL) || (EQ (type, QNULL))) | |
| 189 { /* This is not the same as declining */ | |
| 190 *format_ret = 32; | |
| 191 *size_ret = 0; | |
| 192 *data_ret = 0; | |
| 193 type = QNULL; | |
| 194 } | |
| 195 else if (STRINGP (obj)) | |
| 196 { | |
| 197 const Extbyte *extval; | |
| 665 | 198 Bytecount extvallen; |
| 647 | 199 |
|
4981
4aebb0131297
Cleanups/renaming of EXTERNAL_TO_C_STRING and friends
Ben Wing <ben@xemacs.org>
parents:
4953
diff
changeset
|
200 LISP_STRING_TO_SIZED_EXTERNAL (obj, extval, extvallen, |
|
4aebb0131297
Cleanups/renaming of EXTERNAL_TO_C_STRING and friends
Ben Wing <ben@xemacs.org>
parents:
4953
diff
changeset
|
201 (NILP (type) ? Qctext : Qbinary)); |
| 647 | 202 *format_ret = 8; |
| 203 *size_ret = extvallen; | |
| 2367 | 204 *data_ret = xnew_rawbytes (*size_ret); |
| 647 | 205 memcpy (*data_ret, extval, *size_ret); |
| 206 #ifdef MULE | |
| 207 if (NILP (type)) type = QCOMPOUND_TEXT; | |
| 208 #else | |
| 209 if (NILP (type)) type = QSTRING; | |
| 210 #endif | |
| 211 } | |
| 212 else if (CHARP (obj)) | |
| 213 { | |
| 867 | 214 Ibyte buf[MAX_ICHAR_LEN]; |
| 647 | 215 Bytecount len; |
| 216 const Extbyte *extval; | |
| 665 | 217 Bytecount extvallen; |
| 647 | 218 |
| 219 *format_ret = 8; | |
| 867 | 220 len = set_itext_ichar (buf, XCHAR (obj)); |
| 647 | 221 TO_EXTERNAL_FORMAT (DATA, (buf, len), |
| 222 ALLOCA, (extval, extvallen), | |
| 223 Qctext); | |
| 224 *size_ret = extvallen; | |
| 2367 | 225 *data_ret = xnew_rawbytes (*size_ret); |
| 647 | 226 memcpy (*data_ret, extval, *size_ret); |
| 227 #ifdef MULE | |
| 228 if (NILP (type)) type = QCOMPOUND_TEXT; | |
| 229 #else | |
| 230 if (NILP (type)) type = QSTRING; | |
| 231 #endif | |
| 232 } | |
| 233 else if (SYMBOLP (obj)) | |
| 234 { | |
| 235 *format_ret = 32; | |
| 236 *size_ret = 1; | |
| 2367 | 237 *data_ret = xnew_rawbytes (sizeof (XE_ATOM_TYPE) + 1); |
| 647 | 238 (*data_ret) [sizeof (XE_ATOM_TYPE)] = 0; |
| 239 (*(XE_ATOM_TYPE **) data_ret) [0] = XE_SYMBOL_TO_ATOM (d, obj, 0); | |
| 240 if (NILP (type)) type = QATOM; | |
| 241 } | |
| 242 else if (INTP (obj) && | |
| 243 XINT (obj) <= 0x7FFF && | |
| 244 XINT (obj) >= -0x8000) | |
| 245 { | |
| 246 *format_ret = 16; | |
| 247 *size_ret = 1; | |
| 2367 | 248 *data_ret = xnew_rawbytes (sizeof (short) + 1); |
| 647 | 249 (*data_ret) [sizeof (short)] = 0; |
| 250 (*(short **) data_ret) [0] = (short) XINT (obj); | |
| 251 if (NILP (type)) type = QINTEGER; | |
| 252 } | |
| 253 else if (INTP (obj) || CONSP (obj)) | |
| 254 { | |
| 255 *format_ret = 32; | |
| 256 *size_ret = 1; | |
| 2367 | 257 *data_ret = xnew_rawbytes (sizeof (long) + 1); |
| 647 | 258 (*data_ret) [sizeof (long)] = 0; |
| 259 (*(unsigned long **) data_ret) [0] = lisp_to_word (obj); | |
| 260 if (NILP (type)) type = QINTEGER; | |
| 261 } | |
| 262 else if (VECTORP (obj)) | |
| 263 { | |
| 264 /* Lisp Vectors may represent a set of ATOMs; | |
| 265 a set of 16 or 32 bit INTEGERs; | |
| 266 or a set of ATOM_PAIRs (represented as [[A1 A2] [A3 A4] ...] | |
| 267 */ | |
| 665 | 268 Elemcount i; |
| 647 | 269 |
| 270 if (SYMBOLP (XVECTOR_DATA (obj) [0])) | |
| 271 /* This vector is an ATOM set */ | |
| 272 { | |
| 273 if (NILP (type)) type = QATOM; | |
| 274 *size_ret = XVECTOR_LENGTH (obj); | |
| 275 *format_ret = 32; | |
| 2367 | 276 *data_ret = xnew_rawbytes ((*size_ret) * sizeof (XE_ATOM_TYPE)); |
| 647 | 277 for (i = 0; i < *size_ret; i++) |
| 278 if (SYMBOLP (XVECTOR_DATA (obj) [i])) | |
| 279 (*(XE_ATOM_TYPE **) data_ret) [i] = | |
| 280 XE_SYMBOL_TO_ATOM (d, XVECTOR_DATA (obj) [i], 0); | |
| 281 else | |
| 282 syntax_error | |
| 283 ("all elements of the vector must be of the same type", obj); | |
| 284 } | |
| 285 #if 0 /* #### MULTIPLE doesn't work yet */ | |
| 286 else if (VECTORP (XVECTOR_DATA (obj) [0])) | |
| 287 /* This vector is an ATOM_PAIR set */ | |
| 288 { | |
| 289 if (NILP (type)) type = QATOM_PAIR; | |
| 290 *size_ret = XVECTOR_LENGTH (obj); | |
| 291 *format_ret = 32; | |
| 2367 | 292 *data_ret = |
| 293 xnew_rawbytes ((*size_ret) * sizeof (XE_ATOM_TYPE) * 2); | |
| 647 | 294 for (i = 0; i < *size_ret; i++) |
| 295 if (VECTORP (XVECTOR_DATA (obj) [i])) | |
| 296 { | |
| 297 Lisp_Object pair = XVECTOR_DATA (obj) [i]; | |
| 298 if (XVECTOR_LENGTH (pair) != 2) | |
| 299 syntax_error | |
| 300 ("elements of the vector must be vectors of exactly two elements", pair); | |
| 301 | |
| 302 (*(XE_ATOM_TYPE **) data_ret) [i * 2] = | |
| 303 XE_SYMBOL_TO_ATOM (d, XVECTOR_DATA (pair) [0], 0); | |
| 304 (*(XE_ATOM_TYPE **) data_ret) [(i * 2) + 1] = | |
| 305 XE_SYMBOL_TO_ATOM (d, XVECTOR_DATA (pair) [1], 0); | |
| 306 } | |
| 307 else | |
| 308 syntax_error | |
| 309 ("all elements of the vector must be of the same type", obj); | |
| 310 } | |
| 311 #endif | |
| 312 else | |
| 313 /* This vector is an INTEGER set, or something like it */ | |
| 314 { | |
| 315 *size_ret = XVECTOR_LENGTH (obj); | |
| 316 if (NILP (type)) type = QINTEGER; | |
| 317 *format_ret = 16; | |
| 318 for (i = 0; i < *size_ret; i++) | |
| 319 if (CONSP (XVECTOR_DATA (obj) [i])) | |
| 320 *format_ret = 32; | |
| 321 else if (!INTP (XVECTOR_DATA (obj) [i])) | |
| 322 syntax_error | |
| 323 ("all elements of the vector must be integers or conses of integers", obj); | |
| 324 | |
| 2367 | 325 *data_ret = xnew_rawbytes (*size_ret * (*format_ret/8)); |
| 647 | 326 for (i = 0; i < *size_ret; i++) |
| 327 if (*format_ret == 32) | |
| 328 (*((unsigned long **) data_ret)) [i] = | |
| 329 lisp_to_word (XVECTOR_DATA (obj) [i]); | |
| 330 else | |
| 331 (*((unsigned short **) data_ret)) [i] = | |
| 332 (unsigned short) lisp_to_word (XVECTOR_DATA (obj) [i]); | |
| 333 } | |
| 334 } | |
| 335 else | |
| 336 invalid_argument ("unrecognized selection data", obj); | |
| 337 | |
| 338 *type_ret = XE_SYMBOL_TO_ATOM (d, type, 0); | |
| 339 } | |
| 340 |
