Mercurial > hg > xemacs-beta
annotate lisp/select.el @ 5402:308d34e9f07d
Changed bulk of GPLv2 or later files identified by script
author | Mats Lidell <matsl@xemacs.org> |
---|---|
date | Thu, 14 Oct 2010 17:15:20 +0200 |
parents | e29fcfd8df5f |
children | 0af042a0c116 |
rev | line source |
---|---|
428 | 1 ;;; select.el --- Lisp interface to windows selections. |
2 | |
3 ;; Copyright (C) 1998 Andy Piper. | |
4 ;; Copyright (C) 1990, 1997 Free Software Foundation, Inc. | |
5 ;; Copyright (C) 1995 Sun Microsystems. | |
851 | 6 ;; Copyright (C) 2002 Ben Wing. |
428 | 7 |
8 ;; Maintainer: XEmacs Development Team | |
9 ;; Keywords: extensions, dumped | |
10 | |
11 ;; This file is part of XEmacs. | |
12 | |
5402
308d34e9f07d
Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents:
4783
diff
changeset
|
13 ;; XEmacs is free software: you can redistribute it and/or modify it |
308d34e9f07d
Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents:
4783
diff
changeset
|
14 ;; under the terms of the GNU General Public License as published by the |
308d34e9f07d
Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents:
4783
diff
changeset
|
15 ;; 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:
4783
diff
changeset
|
16 ;; option) any later version. |
428 | 17 |
5402
308d34e9f07d
Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents:
4783
diff
changeset
|
18 ;; XEmacs is distributed in the hope that it will be useful, but WITHOUT |
308d34e9f07d
Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents:
4783
diff
changeset
|
19 ;; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
308d34e9f07d
Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents:
4783
diff
changeset
|
20 ;; FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
308d34e9f07d
Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents:
4783
diff
changeset
|
21 ;; for more details. |
428 | 22 |
23 ;; 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:
4783
diff
changeset
|
24 ;; along with XEmacs. If not, see <http://www.gnu.org/licenses/>. |
428 | 25 |
26 ;;; Synched up with: Not in FSF | |
27 | |
28 ;;; Commentary: | |
29 | |
444 | 30 ;; This file is dumped with XEmacs |
428 | 31 |
32 ;;; Code: | |
33 | |
2624 | 34 ;; We prefer UTF8_STRING to COMPOUND_TEXT because, even though the latter |
35 ;; gives us more information when taking data from other XEmacs invocations, | |
36 ;; Mozilla will happily give us broken COMPOUND_TEXT where a non-broken | |
37 ;; UTF8_STRING is available. | |
38 (defvar selection-preferred-types | |
39 (let ((res '(UTF8_STRING COMPOUND_TEXT STRING image/png image/gif | |
40 image/jpeg image/tiff image/xpm image/xbm))) | |
41 (unless (featurep 'mule) (delq 'COMPOUND_TEXT res)) | |
42 res) | |
43 "An ordered list of X11 type atoms for selections we want to receive. | |
44 We prefer UTF8_STRING over COMPOUND_TEXT, for compatibility with a certain | |
45 widely-used browser suite, and COMPOUND_TEXT over STRING. (COMPOUND_TEXT | |
46 isn't available on non-Mule.) We also accept several image types. | |
47 | |
48 For compatibility, this can be a single atom. ") | |
49 | |
50 ;; Renamed because it was just ridiculous for it to be mostly image formats | |
51 ;; and named selected-text-type. | |
52 (define-obsolete-variable-alias 'selected-text-type 'selection-preferred-types) | |
428 | 53 |
444 | 54 (defvar selection-sets-clipboard nil |
428 | 55 "Controls the selection's relationship to the clipboard. |
56 When non-nil, any operation that sets the primary selection will also | |
57 set the clipboard.") | |
58 | |
59 (defun copy-primary-selection () | |
851 | 60 "Copy the selection to the Clipboard and the kill ring. |
61 This is similar to the command \\[kill-ring-save] except that it will | |
62 save to the Clipboard even if that command doesn't, and it handles rectangles | |
63 properly." | |
428 | 64 (interactive) |
65 (and (console-on-window-system-p) | |
66 (cut-copy-clear-internal 'copy))) | |
67 | |
68 (defun kill-primary-selection () | |
2624 | 69 "Copy the selection to the Clipboard and the kill ring, then delete it. |
851 | 70 This is similar to the command \\[kill-region] except that it will |
71 save to the Clipboard even if that command doesn't, and it handles rectangles | |
72 properly." | |
428 | 73 (interactive "*") |
74 (and (console-on-window-system-p) | |
75 (cut-copy-clear-internal 'cut))) | |
76 | |
77 (defun delete-primary-selection () | |
78 "Delete the selection without copying it to the Clipboard or the kill ring." | |
79 (interactive "*") | |
80 (and (console-on-window-system-p) | |
81 (cut-copy-clear-internal 'clear))) | |
82 | |
83 (defun yank-clipboard-selection () | |
84 "Insert the current Clipboard selection at point." | |
85 (interactive "*") | |
86 (when (console-on-window-system-p) | |
87 (setq last-command nil) | |
88 (setq this-command 'yank) ; so that yank-pop works. | |
89 (let ((clip (get-clipboard))) | |
90 (or clip (error "there is no clipboard selection")) | |
91 (push-mark) | |
92 (insert clip)))) | |
93 | |
94 (defun get-clipboard () | |
829 | 95 "Return text pasted to the clipboard. |
843 | 96 Not suitable for `interprogram-paste-function', use `get-clipboard-foreign'." |
97 (get-selection 'CLIPBOARD)) | |
98 | |
99 (defun get-clipboard-foreign () | |
100 "Return text pasted to the clipboard by another program. | |
829 | 101 See `interprogram-paste-function' for more information." |
843 | 102 (get-selection-foreign 'CLIPBOARD)) |
428 | 103 |
104 (define-device-method get-cutbuffer | |
105 "Return the value of one of the cut buffers. | |
106 This will do nothing under anything other than X.") | |
107 | |
108 (defun get-selection-no-error (&optional type data-type) | |
442 | 109 "Return the value of a window-system selection. |
2624 | 110 The argument TYPE (default `PRIMARY') says which selection, and the argument |
111 DATA-TYPE (defaulting to the value of `selection-preferred-types'), says how | |
112 to convert the data. Returns NIL if there is no selection." | |
442 | 113 (condition-case nil (get-selection type data-type) (t nil))) |
428 | 114 |
115 (defun get-selection (&optional type data-type) | |
843 | 116 "Return the value of a window-system selection. |
2624 | 117 The argument TYPE (default `PRIMARY') says which selection, and the argument |
118 DATA-TYPE (defaulting to the value of, and compatible with, | |
119 `selection-preferred-types') says how to convert the data. If | |
120 there is no selection an error is signalled. Not suitable in a | |
121 `interprogram-paste-function', q.v." | |
428 | 122 (or type (setq type 'PRIMARY)) |
2624 | 123 (or data-type (setq data-type selection-preferred-types)) |
829 | 124 (if (consp data-type) |
2624 | 125 ;; TARGETS is a vector; we want a list so we can memq --> append it to |
126 ;; nil. | |
127 (let ((targets (append (get-selection-internal type 'TARGETS) nil)) | |
128 res) | |
129 (catch 'converted | |
130 (if targets | |
131 (dolist (current-preference data-type) | |
3072 | 132 (condition-case nil |
2624 | 133 (if (and (memq current-preference targets) |
134 (setq res (get-selection-internal | |
135 type current-preference))) | |
136 (throw 'converted res)) | |
137 (selection-conversion-error | |
138 nil)))) | |
139 ;; The source app didn't offer us anything compatible in TARGETS, | |
140 ;; or they're not negotiating at all. (That is, we're probably not | |
141 ;; on X11.) Try to convert to the types specified by our caller, | |
142 ;; and throw an error if the last one of those fails. | |
143 (while data-type | |
144 (condition-case err | |
145 (progn | |
146 (setq res (get-selection-internal type (car data-type))) | |
2656 | 147 (if res (throw 'converted res) |
148 (signal 'selection-conversion-error nil))) | |
2624 | 149 (selection-conversion-error |
150 (if (cdr data-type) | |
2656 | 151 (setq data-type (cdr data-type)) |
2624 | 152 (signal (car err) (cdr err)))))))) |
829 | 153 (get-selection-internal type data-type))) |
428 | 154 |
847 | 155 (defun get-selection-foreign (&optional type data-type) |
156 "Return the value of a window-system selection, or nil if XEmacs owns it. | |
2624 | 157 The argument TYPE (default `PRIMARY') says which selection, and the argument |
158 DATA-TYPE (defaulting to the value of `selection-preferred-types' which see) | |
159 says how to convert the data. If there is no selection an error is | |
160 signalled. See `interprogram-paste-function' for more information." | |
847 | 161 (unless (selection-owner-p type) |
162 (get-selection type data-type))) | |
163 | |
428 | 164 ;; FSFmacs calls this `x-set-selection', and reverses the |
442 | 165 ;; first two arguments (duh ...). This order is more logical. |
166 (defun own-selection (data &optional type how-to-add data-type) | |
167 "Make a window-system selection of type TYPE and value DATA. | |
428 | 168 The argument TYPE (default `PRIMARY') says which selection, |
442 | 169 and DATA specifies the contents. DATA may be any lisp data type |
170 that can be converted using the function corresponding to DATA-TYPE | |
171 in `select-converter-alist'---strings are the usual choice, but | |
172 other types may be permissible depending on the DATA-TYPE parameter | |
851 | 173 \(if DATA-TYPE is not supplied, the default behavior is window |
442 | 174 system specific, but strings are always accepted). |
175 HOW-TO-ADD may be any of the following: | |
176 | |
177 'replace-all or nil -- replace all data in the selection. | |
178 'replace-existing -- replace data for specified DATA-TYPE only. | |
179 'append or t -- append data to existing DATA-TYPE data. | |
180 | |
181 DATA-TYPE is the window-system specific data type identifier | |
851 | 182 \(see `register-selection-data-type' for more information). |
428 | 183 |
184 The selection may also be a cons of two markers pointing to the same buffer, | |
185 or an overlay. In these cases, the selection is considered to be the text | |
442 | 186 between the markers *at whatever time the selection is examined* (note |
187 that the window system clipboard does not necessarily duplicate this | |
188 behavior - it doesn't on mswindows for example). | |
428 | 189 Thus, editing done in the buffer after you specify the selection |
190 can alter the effective value of the selection. | |
191 | |
192 The data may also be a vector of valid non-vector selection values. | |
193 | |
194 Interactively, the text of the region is used as the selection value." | |
195 (interactive (if (not current-prefix-arg) | |
196 (list (read-string "Store text for pasting: ")) | |
197 (list (substring (region-beginning) (region-end))))) | |
442 | 198 ;; calling own-selection-internal will mess this up, so preserve it. |
199 (let ((zmacs-region-stays zmacs-region-stays)) | |
200 ;FSFmacs huh?? It says: | |
201 ;; "This is for temporary compatibility with pre-release Emacs 19." | |
202 ;(if (stringp type) | |
203 ; (setq type (intern type))) | |
204 (or type (setq type 'PRIMARY)) | |
205 (if (null data) | |
206 (disown-selection-internal type) | |
207 (own-selection-internal type data how-to-add data-type) | |
208 (when (and (eq type 'PRIMARY) | |
209 selection-sets-clipboard) | |
210 (own-selection-internal 'CLIPBOARD data how-to-add data-type))) | |
211 (cond ((eq type 'PRIMARY) | |
212 (setq primary-selection-extent | |
213 (select-make-extent-for-selection | |
214 data primary-selection-extent))) | |
215 ((eq type 'SECONDARY) | |
216 (setq secondary-selection-extent | |
217 (select-make-extent-for-selection | |
218 data secondary-selection-extent))))) | |
219 ;; zmacs-region-stays is for commands, not low-level functions. | |
220 ;; when behaving as the latter, we better not set it, or we will | |
221 ;; cause unwanted sticky-region behavior in kill-region and friends. | |
222 (if (interactive-p) | |
223 (setq zmacs-region-stays t)) | |
428 | 224 data) |
225 | |
226 (defun dehilight-selection (selection) | |
227 "for use as a value of `lost-selection-hooks'." | |
228 (cond ((eq selection 'PRIMARY) | |
229 (if primary-selection-extent | |
230 (let ((inhibit-quit t)) | |
231 (if (consp primary-selection-extent) | |
4783
e29fcfd8df5f
Eliminate most core code byte-compile warnings.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4222
diff
changeset
|
232 (mapc 'delete-extent primary-selection-extent) |
428 | 233 (delete-extent primary-selection-extent)) |
234 (setq primary-selection-extent nil))) | |
235 (if zmacs-regions (zmacs-deactivate-region))) | |
236 ((eq selection 'SECONDARY) | |
237 (if secondary-selection-extent | |
238 (let ((inhibit-quit t)) | |
239 (if (consp secondary-selection-extent) | |
4783
e29fcfd8df5f
Eliminate most core code byte-compile warnings.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4222
diff
changeset
|
240 (mapc 'delete-extent secondary-selection-extent) |
428 | 241 (delete-extent secondary-selection-extent)) |
242 (setq secondary-selection-extent nil))))) | |
243 nil) | |
244 | |
245 (setq lost-selection-hooks 'dehilight-selection) | |
246 | |
442 | 247 (defun own-clipboard (string &optional push) |
248 "Paste the given string to the window system Clipboard. | |
249 See `interprogram-cut-function' for more information." | |
428 | 250 (own-selection string 'CLIPBOARD)) |
251 | |
252 (defun disown-selection (&optional secondary-p) | |
487 | 253 "Assuming we own the selection, disown it. |
254 With an argument, discard the secondary selection instead of the | |
255 primary selection." | |
428 | 256 (disown-selection-internal (if secondary-p 'SECONDARY 'PRIMARY)) |
257 (when (and selection-sets-clipboard | |
258 (or (not secondary-p) | |
259 (eq secondary-p 'PRIMARY) | |
260 (eq secondary-p 'CLIPBOARD))) | |
261 (disown-selection-internal 'CLIPBOARD))) | |
262 | |
263 ;; selections and active regions | |
264 | |
265 ;; If and only if zmacs-regions is true: | |
266 | |
267 ;; When a mark is pushed and the region goes into the "active" state, we | |
268 ;; assert it as the Primary selection. This causes it to be hilighted. | |
269 ;; When the region goes into the "inactive" state, we disown the Primary | |
270 ;; selection, causing the region to be dehilighted. | |
271 | |
272 ;; Note that it is possible for the region to be in the "active" state | |
273 ;; and not be hilighted, if it is in the active state and then some other | |
274 ;; application asserts the selection. This is probably not a big deal. | |
275 | |
276 (defun activate-region-as-selection () | |
4222 | 277 (cond ((and-fboundp #'mouse-track-rectangle-p |
278 (mouse-track-rectangle-p | |
279 (mouse-track-activate-rectangular-selection)))) | |
487 | 280 ((marker-buffer (mark-marker t)) |
281 (own-selection (cons (point-marker t) (mark-marker t)))))) | |
428 | 282 |
283 (defvar primary-selection-extent nil | |
284 "The extent of the primary selection; don't use this.") | |
285 | |
286 (defvar secondary-selection-extent nil | |
287 "The extent of the secondary selection; don't use this.") | |
288 | |
289 (defun select-make-extent-for-selection (selection previous-extent) | |
290 ;; Given a selection, this makes an extent in the buffer which holds that | |
291 ;; selection, for highlighting purposes. If the selection isn't associated | |
292 ;; with a buffer, this does nothing. | |
2624 | 293 ;; |
294 ;; Something similar needs to be hooked into the rectangle functions. | |
428 | 295 (let ((buffer nil) |
296 (valid (and (extentp previous-extent) | |
297 (extent-object previous-extent) | |
298 (buffer-live-p (extent-object previous-extent)))) | |
299 start end) | |
300 (cond ((stringp selection) | |
301 ;; if we're selecting a string, lose the previous extent used | |
302 ;; to highlight the selection. | |
303 (setq valid nil)) | |
304 ((consp selection) | |
305 (setq start (min (car selection) (cdr selection)) | |
306 end (max (car selection) (cdr selection)) | |
307 valid (and valid | |
308 (eq (marker-buffer (car selection)) | |
309 (extent-object previous-extent))) | |
310 buffer (marker-buffer (car selection)))) | |
311 ((extentp selection) | |
312 (setq start (extent-start-position selection) | |
313 end (extent-end-position selection) | |
314 valid (and valid | |
315 (eq (extent-object selection) | |
316 (extent-object previous-extent))) | |
317 buffer (extent-object selection))) | |
318 (t | |
319 (signal 'error (list "invalid selection" selection)))) | |
320 | |
321 (if valid | |
322 nil | |
323 (condition-case () | |
324 (if (listp previous-extent) | |
4783
e29fcfd8df5f
Eliminate most core code byte-compile warnings.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4222
diff
changeset
|
325 (mapc 'delete-extent previous-extent) |
428 | 326 (delete-extent previous-extent)) |
327 (error nil))) | |
328 | |
329 (if (not buffer) | |
330 ;; string case | |
331 nil | |
332 ;; normal case | |
333 (if valid | |
334 (set-extent-endpoints previous-extent start end) | |
335 (setq previous-extent (make-extent start end buffer)) | |
336 | |
337 ;; Make the extent be closed on the right, which means that if | |
338 ;; characters are inserted exactly at the end of the extent, the | |
339 ;; extent will grow to cover them. This is important for shell | |
340 ;; buffers - suppose one makes a selection, and one end is at | |
341 ;; point-max. If the shell produces output, that marker will remain | |
342 ;; at point-max (its position will increase). So it's important that | |
343 ;; the extent exhibit the same behavior, lest the region covered by | |
344 ;; the extent (the visual indication), and the region between point | |
345 ;; and mark (the actual selection value) become different! | |
346 (set-extent-property previous-extent 'end-open nil) | |
347 | |
348 (cond | |
4222 | 349 ((and-fboundp #'mouse-track-rectangle-p |
350 (mouse-track-rectangle-p | |
351 (setq previous-extent (list previous-extent)) | |
352 (default-mouse-track-next-move-rect start end previous-extent) | |
353 )))) | |
428 | 354 previous-extent)))) |
355 | |
356 (defun valid-simple-selection-p (data) | |
442 | 357 "An obsolete function that tests whether something was a valid simple |
358 selection using the old XEmacs selection support. You shouldn't use this | |
359 any more, because just about anything could be a valid selection now." | |
428 | 360 (or (stringp data) |
361 ;FSFmacs huh?? (symbolp data) | |
362 (integerp data) | |
363 (and (consp data) | |
364 (integerp (car data)) | |
365 (or (integerp (cdr data)) | |
366 (and (consp (cdr data)) | |
367 (integerp (car (cdr data)))))) | |
368 (extentp data) | |
369 (and (consp data) | |
370 (markerp (car data)) | |
371 (markerp (cdr data)) | |
372 (marker-buffer (car data)) | |
373 (marker-buffer (cdr data)) | |
374 (eq (marker-buffer (car data)) | |
375 (marker-buffer (cdr data))) | |
376 (buffer-live-p (marker-buffer (car data))) | |
377 (buffer-live-p (marker-buffer (cdr data)))))) | |
378 | |
379 (defun cut-copy-clear-internal (mode) | |
380 (or (memq mode '(cut copy clear)) (error "unkown mode %S" mode)) | |
381 (or (selection-owner-p) | |
382 (error "XEmacs does not own the primary selection")) | |
383 (setq last-command nil) | |
384 (or primary-selection-extent | |
385 (error "the primary selection is not an extent?")) | |
386 (save-excursion | |
387 (let (rect-p b s e) | |
388 (cond | |
389 ((consp primary-selection-extent) | |
390 (setq rect-p t | |
391 b (extent-object (car primary-selection-extent)) | |
392 s (extent-start-position (car primary-selection-extent)) | |
393 e (extent-end-position (car (reverse primary-selection-extent))))) | |
394 (t | |
395 (setq rect-p nil | |
396 b (extent-object primary-selection-extent) | |
397 s (extent-start-position primary-selection-extent) | |
398 e (extent-end-position primary-selection-extent)))) | |
399 (set-buffer b) | |
400 (cond ((memq mode '(cut copy)) | |
401 (if rect-p | |
402 (progn | |
851 | 403 ;; killed-rectangle is defvarred in rect.el |
428 | 404 (setq killed-rectangle (extract-rectangle s e)) |
405 (kill-new (mapconcat #'identity killed-rectangle "\n"))) | |
406 (copy-region-as-kill s e)) | |
407 ;; Maybe killing doesn't own clipboard. Make sure it happens. | |
408 ;; This memq is kind of grody, because they might have done it | |
409 ;; some other way, but owning the clipboard twice in that case | |
410 ;; wouldn't actually hurt anything. | |
411 (or (and (consp kill-hooks) (memq 'own-clipboard kill-hooks)) | |
851 | 412 (eq 'own-clipboard interprogram-cut-function) |
428 | 413 (own-clipboard (car kill-ring))))) |
414 (cond ((memq mode '(cut clear)) | |
415 (if rect-p | |
416 (delete-rectangle s e) | |
417 (delete-region s e)))) | |
418 (disown-selection nil) | |
419 ))) | |
420 | |
442 | 421 |
428 | 422 ;;; Functions to convert the selection into various other selection |
442 | 423 ;;; types. |
424 | |
425 ;; These next three functions get called by C code... | |
426 (defun select-convert-in (selection type value) | |
427 "Attempt to convert the specified external VALUE to the specified DATA-TYPE, | |
428 for the specified SELECTION. Return nil if this is impossible, or a | |
429 suitable internal representation otherwise." | |
430 (when value | |
431 (let ((handler-fn (cdr (assq type selection-converter-in-alist)))) | |
2624 | 432 (if handler-fn |
433 (apply handler-fn (list selection type value)) | |
434 value)))) | |
428 | 435 |
442 | 436 (defun select-convert-out (selection type value) |
437 "Attempt to convert the specified internal VALUE for the specified DATA-TYPE | |
438 and SELECTION. Return nil if this is impossible, or a suitable external | |
439 representation otherwise." | |
440 (when value | |
441 (let ((handler-fn (cdr (assq type selection-converter-out-alist)))) | |
442 (when handler-fn | |
443 (apply handler-fn (list selection type value)))))) | |
444 | |
445 (defun select-coerce (selection type value) | |
446 "Attempt to convert the specified internal VALUE to a representation | |
447 suitable for return from `get-selection' in the specified DATA-TYPE. Return | |
448 nil if this is impossible, or a suitable representation otherwise." | |
449 (when value | |
450 (let ((handler-fn (cdr (assq type selection-coercion-alist)))) | |
451 (when handler-fn | |
452 (apply handler-fn (list selection type value)))))) | |
453 | |
454 ;; The rest of the functions on this "page" are conversion handlers, | |
455 ;; append handlers and buffer-kill handlers. | |
428 | 456 (defun select-convert-to-text (selection type value) |
457 (cond ((stringp value) | |
458 value) | |
459 ((extentp value) | |
460 (save-excursion | |
461 (set-buffer (extent-object value)) | |
462 (save-restriction | |
463 (widen) | |
464 (buffer-substring (extent-start-position value) | |
465 (extent-end-position value))))) | |
466 ((and (consp value) | |
467 (markerp (car value)) | |
468 (markerp (cdr value))) | |
469 (or (eq (marker-buffer (car value)) (marker-buffer (cdr value))) | |
470 (signal 'error | |
471 (list "markers must be in the same buffer" | |
472 (car value) (cdr value)))) | |
473 (save-excursion | |
474 (set-buffer (or (marker-buffer (car value)) | |
475 (error "selection is in a killed buffer"))) | |
476 (save-restriction | |
477 (widen) | |
478 (buffer-substring (car value) (cdr value))))) | |
479 (t nil))) | |
480 | |
2624 | 481 (defun select-convert-to-timestamp (selection type value) |
482 (let ((ts (get-xemacs-selection-timestamp selection))) | |
483 (if ts (cons 'TIMESTAMP ts)))) | |
484 | |
485 (defun select-convert-to-utf-8-text (selection type value) | |
486 (cond ((stringp value) | |
487 (cons 'UTF8_STRING (encode-coding-string value 'utf-8))) | |
488 ((extentp value) | |
489 (save-excursion | |
490 (set-buffer (extent-object value)) | |
491 (save-restriction | |
492 (widen) | |
493 (cons 'UTF8_STRING | |
494 (encode-coding-string | |
495 (buffer-substring (extent-start-position value) | |
496 (extent-end-position value)) 'utf-8))))) | |
497 ((and (consp value) | |
498 (markerp (car value)) | |
499 (markerp (cdr value))) | |
500 (or (eq (marker-buffer (car value)) (marker-buffer (cdr value))) | |
501 (signal 'error | |
502 (list "markers must be in the same buffer" | |
503 (car value) (cdr value)))) | |
504 (save-excursion | |
505 (set-buffer (or (marker-buffer (car value)) | |
506 (error "selection is in a killed buffer"))) | |
507 (save-restriction | |
508 (widen) | |
509 (cons 'UTF8_STRING (encode-coding-string | |
510 (buffer-substring (car value) (cdr value)) | |
511 'utf-8))))) | |
512 (t nil))) | |
513 | |
442 | 514 (defun select-coerce-to-text (selection type value) |
515 (select-convert-to-text selection type value)) | |
516 | |
428 | 517 (defun select-convert-to-string (selection type value) |
518 (let ((outval (select-convert-to-text selection type value))) | |
442 | 519 ;; force the string to be not in Compound Text format. This grubby |
520 ;; hack will go soon, to be replaced by a more general mechanism. | |
428 | 521 (if (stringp outval) |
522 (cons 'STRING outval) | |
523 outval))) | |
524 | |
525 (defun select-convert-to-compound-text (selection type value) | |
526 ;; converts to compound text automatically | |
527 (select-convert-to-text selection type value)) | |
528 | |
529 (defun select-convert-to-length (selection type value) | |
530 (let ((value | |
531 (cond ((stringp value) | |
532 (length value)) | |
533 ((extentp value) | |
534 (extent-length value)) | |
535 ((and (consp value) | |
536 (markerp (car value)) | |
537 (markerp (cdr value))) | |
538 (or (eq (marker-buffer (car value)) | |
539 (marker-buffer (cdr value))) | |
540 (signal 'error | |
541 (list "markers must be in the same buffer" | |
542 (car value) (cdr value)))) | |
543 (abs (- (car value) (cdr value))))))) | |
544 (if value ; force it to be in 32-bit format. | |
545 (cons (ash value -16) (logand value 65535)) | |
546 nil))) | |
547 | |
548 (defun select-convert-to-targets (selection type value) | |
549 ;; return a vector of atoms, but remove duplicates first. | |
550 (let* ((all (cons 'TIMESTAMP (mapcar 'car selection-converter-alist))) | |
551 (rest all)) | |
552 (while rest | |
553 (cond ((memq (car rest) (cdr rest)) | |
554 (setcdr rest (delq (car rest) (cdr rest)))) | |
555 (t | |
556 (setq rest (cdr rest))))) | |
557 (apply 'vector all))) | |
558 | |
559 (defun select-convert-to-delete (selection type value) | |
560 (disown-selection-internal selection) | |
561 ;; A return value of nil means that we do not know how to do this conversion, | |
562 ;; and replies with an "error". A return value of NULL means that we have | |
563 ;; done the conversion (and any side-effects) but have no value to return. | |
564 'NULL) | |
565 | |
566 (defun select-convert-to-filename (selection type value) | |
567 (cond ((extentp value) | |
568 (buffer-file-name (or (extent-object value) | |
569 (error "selection is in a killed buffer")))) | |
570 ((and (consp value) | |
571 (markerp (car value)) | |
572 (markerp (cdr value))) | |
573 (buffer-file-name (or (marker-buffer (car value)) | |
574 (error "selection is in a killed buffer")))) | |
575 (t nil))) | |
576 | |
577 (defun select-convert-to-charpos (selection type value) | |
578 (let (a b tmp) | |
579 (cond ((cond ((extentp value) | |
580 (setq a (extent-start-position value) | |
581 b (extent-end-position value))) | |
582 ((and (consp value) | |
583 (markerp (car value)) | |
584 (markerp (cdr value))) | |
585 (setq a (car value) | |
586 b (cdr value)))) | |
587 (setq a (1- a) b (1- b)) ; zero-based | |
588 (if (< b a) (setq tmp a a b b tmp)) | |
589 (cons 'SPAN | |
590 (vector (cons (ash a -16) (logand a 65535)) | |
591 (cons (ash b -16) (logand b 65535)))))))) | |
592 | |
593 (defun select-convert-to-lineno (selection type value) | |
594 (let (a b buf tmp) | |
595 (cond ((cond ((extentp value) | |
596 (setq buf (extent-object value) | |
597 a (extent-start-position value) | |
598 b (extent-end-position value))) | |
599 ((and (consp value) | |
600 (markerp (car value)) | |
601 (markerp (cdr value))) | |
602 (setq a (marker-position (car value)) | |
603 b (marker-position (cdr value)) | |
604 buf (marker-buffer (car value))))) | |
605 (save-excursion | |
606 (set-buffer buf) | |
607 (save-restriction | |
608 (widen) | |
609 (goto-char a) | |
610 (beginning-of-line) | |
611 (setq a (1+ (count-lines 1 (point)))) | |
612 (goto-char b) | |
613 (beginning-of-line) | |
614 (setq b (1+ (count-lines 1 (point)))))) | |
615 (if (< b a) (setq tmp a a b b tmp)) | |
616 (cons 'SPAN | |
617 (vector (cons (ash a -16) (logand a 65535)) | |
618 (cons (ash b -16) (logand b 65535)))))))) | |
619 | |
620 (defun select-convert-to-colno (selection type value) | |
621 (let (a b buf tmp) | |
622 (cond ((cond ((extentp value) | |
623 (setq buf (extent-object value) | |
624 a (extent-start-position value) | |
625 b (extent-end-position value))) | |
626 ((and (consp value) | |
627 (markerp (car value)) | |
628 (markerp (cdr value))) | |
629 (setq a (car value) | |
630 b (cdr value) | |
631 buf (marker-buffer a)))) | |
632 (save-excursion | |
633 (set-buffer buf) | |
634 (goto-char a) | |
635 (setq a (current-column)) | |
636 (goto-char b) | |
637 (setq b (current-column))) | |
638 (if (< b a) (setq tmp a a b b tmp)) | |
639 (cons 'SPAN | |
640 (vector (cons (ash a -16) (logand a 65535)) | |
641 (cons (ash b -16) (logand b 65535)))))))) | |
642 | |
643 (defun select-convert-to-sourceloc (selection type value) | |
644 (let (a b buf file-name tmp) | |
645 (cond ((cond ((extentp value) | |
646 (setq buf (or (extent-object value) | |
647 (error "selection is in a killed buffer")) | |
648 a (extent-start-position value) | |
649 b (extent-end-position value) | |
650 file-name (buffer-file-name buf))) | |
651 ((and (consp value) | |
652 (markerp (car value)) | |
653 (markerp (cdr value))) | |
654 (setq a (marker-position (car value)) | |
655 b (marker-position (cdr value)) | |
656 buf (or (marker-buffer (car value)) | |
657 (error "selection is in a killed buffer")) | |
658 file-name (buffer-file-name buf)))) | |
659 (save-excursion | |
660 (set-buffer buf) | |
661 (save-restriction | |
662 (widen) | |
663 (goto-char a) | |
664 (beginning-of-line) | |
665 (setq a (1+ (count-lines 1 (point)))) | |
666 (goto-char b) | |
667 (beginning-of-line) | |
668 (setq b (1+ (count-lines 1 (point)))))) | |
669 (if (< b a) (setq tmp a a b b tmp)) | |
670 (format "%s:%d" file-name a))))) | |
671 | |
672 (defun select-convert-to-os (selection type size) | |
673 (symbol-name system-type)) | |
674 | |
675 (defun select-convert-to-host (selection type size) | |
676 (system-name)) | |
677 | |
678 (defun select-convert-to-user (selection type size) | |
679 (user-full-name)) | |
680 | |
681 (defun select-convert-to-class (selection type size) | |
442 | 682 (symbol-value 'x-emacs-application-class)) |
428 | 683 |
684 ;; We do not try to determine the name Emacs was invoked with, | |
685 ;; because it is not clean for a program's behavior to depend on that. | |
686 (defun select-convert-to-name (selection type size) | |
687 ;invocation-name | |
688 "xemacs") | |
689 | |
690 (defun select-convert-to-integer (selection type value) | |
691 (and (integerp value) | |
692 (cons (ash value -16) (logand value 65535)))) | |
693 | |
442 | 694 ;; Can convert from the following integer representations |
695 ;; | |
696 ;; integer | |
697 ;; (integer . integer) | |
698 ;; (integer integer) | |
699 ;; (list [integer|(integer . integer)]*) | |
700 ;; (vector [integer|(integer . integer)]*) | |
701 ;; | |
702 ;; Cons'd integers get cleaned up a little. | |
703 | |
704 (defun select-convert-from-integer (selection type value) | |
705 (cond ((integerp value) ; Integer | |
706 value) | |
444 | 707 |
442 | 708 ((and (consp value) ; (integer . integer) |
709 (integerp (car value)) | |
710 (integerp (cdr value))) | |
711 (if (eq (car value) 0) | |
712 (cdr value) | |
713 (if (and (eq (car value) -1) | |
714 (< (cdr value) 0)) | |
715 (cdr value) | |
716 value))) | |
444 | 717 |
442 | 718 ((and (listp value) ; (integer integer) |
719 (eq (length value) 2) | |
720 (integerp (car value)) | |
721 (integerp (cadr value))) | |
722 (if (eq (car value) 0) | |
723 (cadr value) | |
724 (if (and (eq (car value) -1) | |
725 (< (cdr value) 0)) | |
726 (- (cadr value)) | |
727 (cons (car value) (cadr value))))) | |
444 | 728 |
442 | 729 ((listp value) ; list |
730 (if (cdr value) | |
4021 | 731 (mapcar #'(lambda (x) |
732 (select-convert-from-integer selection type x)) | |
442 | 733 value) |
734 (select-convert-from-integer selection type (car value)))) | |
444 | 735 |
442 | 736 ((vectorp value) ; vector |
737 (if (eq (length value) 1) | |
738 (select-convert-from-integer selection type (aref value 0)) | |
4021 | 739 (mapvector #'(lambda (x) |
740 (select-convert-from-integer selection type x)) | |
741 value))) | |
444 | 742 |
442 | 743 (t nil) |
744 )) | |
745 | |
2624 | 746 (defun select-convert-from-ip-address (selection type value) |
747 (if (and (stringp value) | |
748 (= (length value) 4)) | |
749 (format "%d.%d.%d.%d" | |
750 (aref value 0) (aref value 1) (aref value 2) (aref value 3)))) | |
751 | |
428 | 752 (defun select-convert-to-atom (selection type value) |
753 (and (symbolp value) value)) | |
754 | |
2624 | 755 (defun select-convert-from-utf-8-text (selection type value) |
756 (decode-coding-string value 'utf-8)) | |
757 | |
758 (defun select-convert-from-utf-16-le-text (selection type value) | |
759 (decode-coding-string value 'utf-16-le)) | |
760 | |
761 ;; Image conversion. | |
762 (defun select-convert-from-image-data (image-type value) | |
763 "Take an image type specification--one of the image types this XEmacs | |
764 supports--and some data in that format, return a space, with a glyph | |
765 corresponding to that data as an end-glyph extent property of that space. " | |
766 (let* ((str (make-string 1 ?\ )) | |
767 (extent (make-extent 0 1 str)) | |
768 (glyph (make-glyph (vector image-type ':data value)))) | |
769 (when glyph | |
770 (set-extent-property extent 'invisible t) | |
771 (set-extent-property extent 'start-open t) | |
772 (set-extent-property extent 'end-open t) | |
773 (set-extent-property extent 'duplicable t) | |
774 (set-extent-property extent 'atomic t) | |
775 (set-extent-end-glyph extent glyph) | |
776 str))) | |
777 | |
778 ;; Could automate defining these functions these with a macro, but damned if | |
779 ;; I can get that to work. Anyway, this is more readable. | |
780 | |
781 (defun select-convert-from-image/gif (selection type value) | |
782 (if (featurep 'gif) (select-convert-from-image-data 'gif value))) | |
783 | |
784 (defun select-convert-from-image/jpeg (selection type value) | |
785 (if (featurep 'jpeg) (select-convert-from-image-data 'jpeg value))) | |
786 | |
787 (defun select-convert-from-image/png (selection type value) | |
788 (if (featurep 'png) (select-convert-from-image-data 'png value))) | |
789 | |
790 (defun select-convert-from-image/tiff (selection type value) | |
791 (if (featurep 'tiff) (select-convert-from-image-data 'tiff value))) | |
792 | |
793 (defun select-convert-from-image/xpm (selection type value) | |
794 (if (featurep 'xpm) (select-convert-from-image-data 'xpm value))) | |
795 | |
796 (defun select-convert-from-image/xbm (selection type value) | |
797 (if (featurep 'xbm) (select-convert-from-image-data 'xbm value))) | |
798 | |
442 | 799 ;;; CF_xxx conversions |
800 (defun select-convert-from-cf-text (selection type value) | |
2624 | 801 (if (find-coding-system 'mswindows-multibyte) |
802 (let ((value (decode-coding-string value 'mswindows-multibyte))) | |
803 (replace-in-string (if (string-match "\0" value) | |
804 (substring value 0 (match-beginning 0)) | |
805 value) | |
806 "\\(\r\n\\|\n\r\\)" "\n" t)))) | |
771 | 807 |
808 (defun select-convert-from-cf-unicodetext (selection type value) | |
2624 | 809 (if (find-coding-system 'mswindows-unicode) |
810 (let ((value (decode-coding-string value 'mswindows-unicode))) | |
811 (replace-in-string (if (string-match "\0" value) | |
812 (substring value 0 (match-beginning 0)) | |
813 value) | |
814 "\\(\r\n\\|\n\r\\)" "\n" t)))) | |
442 | 815 |
816 (defun select-convert-to-cf-text (selection type value) | |
2624 | 817 (if (find-coding-system 'mswindows-multibyte) |
818 (let ((text (select-convert-to-text selection type value))) | |
819 (encode-coding-string | |
820 (concat (replace-in-string text "\n" "\r\n" t) "\0") | |
821 'mswindows-multibyte)))) | |
771 | 822 |
823 (defun select-convert-to-cf-unicodetext (selection type value) | |
2624 | 824 (if (find-coding-system 'mswindows-unicode) |
825 (let ((text (select-convert-to-text selection type value))) | |
826 (encode-coding-string | |
827 (concat (replace-in-string text "\n" "\r\n" t) "\0") | |
828 'mswindows-unicode)))) | |
442 | 829 |
830 ;;; Appenders | |
831 (defun select-append-to-text (selection type value1 value2) | |
832 (let ((text1 (select-convert-to-text selection 'STRING value1)) | |
833 (text2 (select-convert-to-text selection 'STRING value2))) | |
834 (if (and text1 text2) | |
835 (concat text1 text2) | |
836 nil))) | |
837 | |
838 (defun select-append-to-string (selection type value1 value2) | |
839 (select-append-to-text selection type value1 value2)) | |
840 | |
841 (defun select-append-to-compound-text (selection type value1 value2) | |
842 (select-append-to-text selection type value1 value2)) | |
843 | |
844 (defun select-append-to-cf-text (selection type value1 value2) | |
845 (let ((text1 (select-convert-from-cf-text selection 'CF_TEXT value1)) | |
846 (text2 (select-convert-from-cf-text selection 'CF_TEXT value2))) | |
847 (if (and text1 text2) | |
848 (select-convert-to-cf-text selection type (concat text1 text2)) | |
849 nil))) | |
428 | 850 |
771 | 851 (defun select-append-to-cf-unicodetext (selection type value1 value2) |
852 (let ((text1 (select-convert-from-cf-unicodetext selection | |
853 'CF_UNICODETEXT value1)) | |
854 (text2 (select-convert-from-cf-unicodetext selection | |
855 'CF_UNICODETEXT value2))) | |
856 (if (and text1 text2) | |
857 (select-convert-to-cf-unicodetext selection type (concat text1 text2)) | |
858 nil))) | |
859 | |
442 | 860 (defun select-append-default (selection type value1 value2) |
861 ;; This appender gets used if the type is "nil" - i.e. default. | |
862 ;; It should probably have more cases implemented than it does - e.g. | |
863 ;; appending numbers to strings, etc... | |
864 (cond ((and (stringp value1) (stringp value2)) | |
865 (select-append-to-string selection 'STRING value1 value2)) | |
866 (t nil))) | |
867 | |
868 ;;; Buffer kill handlers | |
869 | |
870 (defun select-buffer-killed-default (selection type value buffer) | |
871 ;; This handler gets used if the type is "nil". | |
872 (cond ((extentp value) | |
873 (if (eq (extent-object value) buffer) | |
874 ; If this selection is on the clipboard, grab it quick | |
875 (when (eq selection 'CLIPBOARD) | |
876 (save-excursion | |
877 (set-buffer (extent-object value)) | |
878 (save-restriction | |
879 (widen) | |
880 (buffer-substring (extent-start-position value) | |
881 (extent-end-position value))))) | |
882 value)) | |
883 ((markerp value) | |
884 (unless (eq (marker-buffer value) buffer) | |
885 value)) | |
886 ((and (consp value) | |
887 (markerp (car value)) | |
888 (markerp (cdr value))) | |
889 (if (or (eq (marker-buffer (car value)) buffer) | |
890 (eq (marker-buffer (cdr value)) buffer)) | |
891 ; If this selection is on the clipboard, grab it quick | |
892 (when (eq selection 'CLIPBOARD) | |
893 (save-excursion | |
894 (set-buffer (marker-buffer (car value))) | |
895 (save-restriction | |
896 (widen) | |
897 (buffer-substring (car value) (cdr value))))) | |
898 value)) | |
899 (t value))) | |
900 | |
901 (defun select-buffer-killed-text (selection type value buffer) | |
902 (select-buffer-killed-default selection type value buffer)) | |
444 | 903 |
442 | 904 ;; Types listed in here can be selections of XEmacs |
905 (setq selection-converter-out-alist | |
2624 | 906 '((TIMESTAMP . select-convert-to-timestamp) |
907 (UTF8_STRING . select-convert-to-utf-8-text) | |
908 (TEXT . select-convert-to-text) | |
428 | 909 (STRING . select-convert-to-string) |
910 (COMPOUND_TEXT . select-convert-to-compound-text) | |
911 (TARGETS . select-convert-to-targets) | |
912 (LENGTH . select-convert-to-length) | |
913 (DELETE . select-convert-to-delete) | |
914 (FILE_NAME . select-convert-to-filename) | |
915 (CHARACTER_POSITION . select-convert-to-charpos) | |
916 (SOURCE_LOC . select-convert-to-sourceloc) | |
917 (LINE_NUMBER . select-convert-to-lineno) | |
918 (COLUMN_NUMBER . select-convert-to-colno) | |
919 (OWNER_OS . select-convert-to-os) | |
920 (HOST_NAME . select-convert-to-host) | |
921 (USER . select-convert-to-user) | |
922 (CLASS . select-convert-to-class) | |
923 (NAME . select-convert-to-name) | |
924 (ATOM . select-convert-to-atom) | |
925 (INTEGER . select-convert-to-integer) | |
442 | 926 (CF_TEXT . select-convert-to-cf-text) |
771 | 927 (CF_UNICODETEXT . select-convert-to-cf-unicodetext) |
442 | 928 )) |
929 | |
930 ;; Types listed here can be selections foreign to XEmacs | |
931 (setq selection-converter-in-alist | |
932 '(; Specific types that get handled by generic converters | |
933 (INTEGER . select-convert-from-integer) | |
2624 | 934 (TIMESTAMP . select-convert-from-integer) |
935 (LENGTH . select-convert-from-integer) | |
936 (LIST_LENGTH . select-convert-from-integer) | |
937 (CLIENT_WINDOW . select-convert-from-integer) | |
938 (PROCESS . select-convert-from-integer) | |
939 (IP_ADDRESS . select-convert-from-ip-address) | |
940 ;; We go after UTF8_STRING in preference to STRING because Mozilla, | |
941 ;; at least, does bad things with non-Latin-1 Unicode characters in | |
942 ;; STRING. | |
943 (UTF8_STRING . select-convert-from-utf-8-text) | |
442 | 944 (CF_TEXT . select-convert-from-cf-text) |
771 | 945 (CF_UNICODETEXT . select-convert-from-cf-unicodetext) |
2624 | 946 (text/html . select-convert-from-utf-16-le-text) ; Mozilla |
947 (text/_moz_htmlcontext . select-convert-from-utf-16-le-text) | |
948 (text/_moz_htmlinfo . select-convert-from-utf-16-le-text) | |
949 (image/png . select-convert-from-image/png) | |
950 (image/gif . select-convert-from-image/gif) | |
951 (image/jpeg . select-convert-from-image/jpeg ) | |
952 (image/tiff . select-convert-from-image/tiff ) | |
953 (image/xpm . select-convert-from-image/xpm) | |
954 (image/xbm . select-convert-from-image/xbm) | |
428 | 955 )) |
956 | |
442 | 957 ;; Types listed here have special coercion functions that can munge |
958 ;; other types. This can also be used to add special features - e.g. | |
959 ;; being able to pass a region or a cons of markers to own-selection, | |
960 ;; but getting the *current* text in the region back when calling | |
961 ;; get-selection. | |
962 ;; | |
963 ;; Any function listed in here *will be called* whenever a value of | |
964 ;; its type is retrieved from the internal selection cache, or when | |
965 ;; no suitable values could be found in which case XEmacs looks for | |
966 ;; values with types listed in selection-coercible-types. | |
967 (setq selection-coercion-alist | |
968 '((TEXT . select-coerce-to-text) | |
969 (STRING . select-coerce-to-text) | |
970 (COMPOUND_TEXT . select-coerce-to-text) | |
771 | 971 (CF_TEXT . select-coerce-to-text) |
972 (CF_UNICODETEXT . select-coerce-to-text) | |
973 )) | |
442 | 974 |
975 ;; Types listed here can be appended by own-selection | |
976 (setq selection-appender-alist | |
977 '((nil . select-append-default) | |
978 (TEXT . select-append-to-text) | |
979 (STRING . select-append-to-string) | |
980 (COMPOUND_TEXT . select-append-to-compound-text) | |
981 (CF_TEXT . select-append-to-cf-text) | |
771 | 982 (CF_UNICODETEXT . select-append-to-cf-unicodetext) |
442 | 983 )) |
984 | |
985 ;; Types listed here have buffer-kill handlers | |
986 (setq selection-buffer-killed-alist | |
987 '((nil . select-buffer-killed-default) | |
988 (TEXT . select-buffer-killed-text) | |
989 (STRING . select-buffer-killed-text) | |
990 (COMPOUND_TEXT . select-buffer-killed-text) | |
771 | 991 (CF_TEXT . select-buffer-killed-text) |
992 (CF_UNICODETEXT . select-buffer-killed-text) | |
993 )) | |
442 | 994 |
995 ;; Lists of types that are coercible (can be converted to other types) | |
771 | 996 (setq selection-coercible-types '(TEXT STRING COMPOUND_TEXT CF_TEXT CF_UNICODETEXT)) |
442 | 997 |
428 | 998 ;;; select.el ends here |