1 ;;; select.el --- Lisp interface to windows selections.
3 ;; Copyright (C) 1998 Andy Piper.
4 ;; Copyright (C) 1990, 1997 Free Software Foundation, Inc.
5 ;; Copyright (C) 1995 Sun Microsystems.
7 ;; Maintainer: XEmacs Development Team
8 ;; Keywords: extensions, dumped
10 ;; This file is part of XEmacs.
12 ;; XEmacs is free software; you can redistribute it and/or modify it
13 ;; under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
17 ;; XEmacs is distributed in the hope that it will be useful, but
18 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 ;; General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with XEmacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
27 ;;; Synched up with: Not in FSF
31 ;; This file is dumped with XEmacs
35 (defvar selected-text-type
36 (if (featurep 'mule) '(COMPOUND_TEXT STRING) 'STRING)
37 "The type atom used to obtain selections from the X server.
38 Can be either a valid X selection data type, or a list of such types.
39 COMPOUND_TEXT and STRING are the most commonly used data types.
40 If a list is provided, the types are tried in sequence until
41 there is a successful conversion.")
43 (defvar selection-sets-clipboard nil
44 "Controls the selection's relationship to the clipboard.
45 When non-nil, any operation that sets the primary selection will also
48 (defun copy-primary-selection ()
49 "Copy the selection to the Clipboard and the kill ring."
51 (and (console-on-window-system-p)
52 (cut-copy-clear-internal 'copy)))
54 (defun kill-primary-selection ()
55 "Copy the selection to the Clipboard and the kill ring, then delete it."
57 (and (console-on-window-system-p)
58 (cut-copy-clear-internal 'cut)))
60 (defun delete-primary-selection ()
61 "Delete the selection without copying it to the Clipboard or the kill ring."
63 (and (console-on-window-system-p)
64 (cut-copy-clear-internal 'clear)))
66 (defun yank-clipboard-selection ()
67 "Insert the current Clipboard selection at point."
69 (case (device-type (selected-device))
70 (x (x-yank-clipboard-selection))
71 (mswindows (mswindows-paste-clipboard))
74 (define-device-method get-cutbuffer
75 "Return the value of one of the cut buffers.
76 This will do nothing under anything other than X.")
78 (defun get-selection-no-error (&optional type data-type)
79 "Return the value of a Windows selection.
80 The argument TYPE (default `PRIMARY') says which selection,
81 and the argument DATA-TYPE (default `STRING', or `COMPOUND_TEXT' under Mule)
82 says how to convert the data. Returns NIL if there is no selection"
83 (condition-case err (get-selection type data-type) (t nil)))
85 (defun get-selection (&optional type data-type)
86 "Return the value of a Windows selection.
87 The argument TYPE (default `PRIMARY') says which selection,
88 and the argument DATA-TYPE (default `STRING', or `COMPOUND_TEXT' under Mule)
89 says how to convert the data. If there is no selection an error is signalled."
90 (or type (setq type 'PRIMARY))
91 (or data-type (setq data-type selected-text-type))
95 (get-selection-internal type (car data-type))
96 (selection-conversion-error
98 (get-selection type (cdr data-type))
99 (signal (car err) (cdr err)))))
100 (get-selection-internal type data-type))))
101 (when (and (consp text) (symbolp (car text)))
102 (setq text (cdr text)))
103 (when (not (stringp text))
104 (error "Selection is not a string: %S" text))
107 ;; FSFmacs calls this `x-set-selection', and reverses the
108 ;; arguments (duh ...). This order is more logical.
109 (defun own-selection (data &optional type)
110 "Make an Windows selection of type TYPE and value DATA.
111 The argument TYPE (default `PRIMARY') says which selection,
112 and DATA specifies the contents. DATA may be a string,
113 a symbol, an integer (or a cons of two integers or list of two integers).
115 The selection may also be a cons of two markers pointing to the same buffer,
116 or an overlay. In these cases, the selection is considered to be the text
117 between the markers *at whatever time the selection is examined*.
118 Thus, editing done in the buffer after you specify the selection
119 can alter the effective value of the selection.
121 The data may also be a vector of valid non-vector selection values.
123 Interactively, the text of the region is used as the selection value."
124 (interactive (if (not current-prefix-arg)
125 (list (read-string "Store text for pasting: "))
126 (list (substring (region-beginning) (region-end)))))
127 ;FSFmacs huh?? It says:
128 ;; "This is for temporary compatibility with pre-release Emacs 19."
130 ; (setq type (intern type)))
131 (or (valid-simple-selection-p data)
134 (i (1- (length data))))
136 (or (valid-simple-selection-p (aref data i))
140 (signal 'error (list "invalid selection" data)))
141 (or type (setq type 'PRIMARY))
143 (disown-selection-internal type)
144 (own-selection-internal type data)
145 (when (and (eq type 'PRIMARY)
146 selection-sets-clipboard)
147 (own-selection-internal 'CLIPBOARD data)))
148 (cond ((eq type 'PRIMARY)
149 (setq primary-selection-extent
150 (select-make-extent-for-selection
151 data primary-selection-extent)))
152 ((eq type 'SECONDARY)
153 (setq secondary-selection-extent
154 (select-make-extent-for-selection
155 data secondary-selection-extent))))
156 (setq zmacs-region-stays t)
159 (defun dehilight-selection (selection)
160 "for use as a value of `lost-selection-hooks'."
161 (cond ((eq selection 'PRIMARY)
162 (if primary-selection-extent
163 (let ((inhibit-quit t))
164 (if (consp primary-selection-extent)
165 (mapcar 'delete-extent primary-selection-extent)
166 (delete-extent primary-selection-extent))
167 (setq primary-selection-extent nil)))
168 (if zmacs-regions (zmacs-deactivate-region)))
169 ((eq selection 'SECONDARY)
170 (if secondary-selection-extent
171 (let ((inhibit-quit t))
172 (if (consp secondary-selection-extent)
173 (mapcar 'delete-extent secondary-selection-extent)
174 (delete-extent secondary-selection-extent))
175 (setq secondary-selection-extent nil)))))
178 (setq lost-selection-hooks 'dehilight-selection)
180 (defun own-clipboard (string)
181 "Paste the given string to the X Clipboard."
182 (own-selection string 'CLIPBOARD))
184 (defun disown-selection (&optional secondary-p)
185 "Assuming we own the selection, disown it. With an argument, discard the
186 secondary selection instead of the primary selection."
187 (disown-selection-internal (if secondary-p 'SECONDARY 'PRIMARY)))
190 ;; selections and active regions
192 ;; If and only if zmacs-regions is true:
194 ;; When a mark is pushed and the region goes into the "active" state, we
195 ;; assert it as the Primary selection. This causes it to be hilighted.
196 ;; When the region goes into the "inactive" state, we disown the Primary
197 ;; selection, causing the region to be dehilighted.
199 ;; Note that it is possible for the region to be in the "active" state
200 ;; and not be hilighted, if it is in the active state and then some other
201 ;; application asserts the selection. This is probably not a big deal.
203 (defun activate-region-as-selection ()
204 (if (marker-buffer (mark-marker t))
205 (own-selection (cons (point-marker t) (mark-marker t)))))
207 ; moved from x-select.el
208 (defvar primary-selection-extent nil
209 "The extent of the primary selection; don't use this.")
211 (defvar secondary-selection-extent nil
212 "The extent of the secondary selection; don't use this.")
214 (defun select-make-extent-for-selection (selection previous-extent)
215 ;; Given a selection, this makes an extent in the buffer which holds that
216 ;; selection, for highlighting purposes. If the selection isn't associated
217 ;; with a buffer, this does nothing.
219 (valid (and (extentp previous-extent)
220 (extent-object previous-extent)
221 (buffer-live-p (extent-object previous-extent))))
223 (cond ((stringp selection)
224 ;; if we're selecting a string, lose the previous extent used
225 ;; to highlight the selection.
228 (setq start (min (car selection) (cdr selection))
229 end (max (car selection) (cdr selection))
231 (eq (marker-buffer (car selection))
232 (extent-object previous-extent)))
233 buffer (marker-buffer (car selection))))
235 (setq start (extent-start-position selection)
236 end (extent-end-position selection)
238 (eq (extent-object selection)
239 (extent-object previous-extent)))
240 buffer (extent-object selection)))
242 (signal 'error (list "invalid selection" selection))))
247 (if (listp previous-extent)
248 (mapcar 'delete-extent previous-extent)
249 (delete-extent previous-extent))
257 (set-extent-endpoints previous-extent start end)
258 (setq previous-extent (make-extent start end buffer))
260 ;; Make the extent be closed on the right, which means that if
261 ;; characters are inserted exactly at the end of the extent, the
262 ;; extent will grow to cover them. This is important for shell
263 ;; buffers - suppose one makes a selection, and one end is at
264 ;; point-max. If the shell produces output, that marker will remain
265 ;; at point-max (its position will increase). So it's important that
266 ;; the extent exhibit the same behavior, lest the region covered by
267 ;; the extent (the visual indication), and the region between point
268 ;; and mark (the actual selection value) become different!
269 (set-extent-property previous-extent 'end-open nil)
272 (mouse-track-rectangle-p
273 (setq previous-extent (list previous-extent))
274 (default-mouse-track-next-move-rect start end previous-extent)
278 ;; moved from x-select.el
279 (defun valid-simple-selection-p (data)
281 ;FSFmacs huh?? (symbolp data)
284 (integerp (car data))
285 (or (integerp (cdr data))
286 (and (consp (cdr data))
287 (integerp (car (cdr data))))))
292 (marker-buffer (car data))
293 (marker-buffer (cdr data))
294 (eq (marker-buffer (car data))
295 (marker-buffer (cdr data)))
296 (buffer-live-p (marker-buffer (car data)))
297 (buffer-live-p (marker-buffer (cdr data))))))
299 (defun cut-copy-clear-internal (mode)
300 (or (memq mode '(cut copy clear)) (error "unkown mode %S" mode))
301 (or (selection-owner-p)
302 (error "XEmacs does not own the primary selection"))
303 (setq last-command nil)
304 (or primary-selection-extent
305 (error "the primary selection is not an extent?"))
309 ((consp primary-selection-extent)
311 b (extent-object (car primary-selection-extent))
312 s (extent-start-position (car primary-selection-extent))
313 e (extent-end-position (car (reverse primary-selection-extent)))))
316 b (extent-object primary-selection-extent)
317 s (extent-start-position primary-selection-extent)
318 e (extent-end-position primary-selection-extent))))
320 (cond ((memq mode '(cut copy))
323 ;; why is killed-rectangle free? Is it used somewhere?
324 ;; should it be defvarred?
325 (setq killed-rectangle (extract-rectangle s e))
326 (kill-new (mapconcat #'identity killed-rectangle "\n")))
327 (copy-region-as-kill s e))
328 ;; Maybe killing doesn't own clipboard. Make sure it happens.
329 ;; This memq is kind of grody, because they might have done it
330 ;; some other way, but owning the clipboard twice in that case
331 ;; wouldn't actually hurt anything.
332 (or (and (consp kill-hooks) (memq 'own-clipboard kill-hooks))
333 (own-clipboard (car kill-ring)))))
334 (cond ((memq mode '(cut clear))
336 (delete-rectangle s e)
337 (delete-region s e))))
338 (disown-selection nil)
341 ;;; Functions to convert the selection into various other selection
342 ;;; types. Every selection type that emacs handles is implemented
343 ;;; this way, except for TIMESTAMP, which is a special case. These are
344 ;;; all moved from x-select.el
346 (defun select-convert-to-text (selection type value)
347 (cond ((stringp value)
351 (set-buffer (extent-object value))
354 (buffer-substring (extent-start-position value)
355 (extent-end-position value)))))
357 (markerp (car value))
358 (markerp (cdr value)))
359 (or (eq (marker-buffer (car value)) (marker-buffer (cdr value)))
361 (list "markers must be in the same buffer"
362 (car value) (cdr value))))
364 (set-buffer (or (marker-buffer (car value))
365 (error "selection is in a killed buffer")))
368 (buffer-substring (car value) (cdr value)))))
371 (defun select-convert-to-string (selection type value)
372 (let ((outval (select-convert-to-text selection type value)))
373 ;; force the string to be not in Compound Text format.
375 (cons 'STRING outval)
378 (defun select-convert-to-compound-text (selection type value)
379 ;; converts to compound text automatically
380 (select-convert-to-text selection type value))
382 (defun select-convert-to-length (selection type value)
384 (cond ((stringp value)
387 (extent-length value))
389 (markerp (car value))
390 (markerp (cdr value)))
391 (or (eq (marker-buffer (car value))
392 (marker-buffer (cdr value)))
394 (list "markers must be in the same buffer"
395 (car value) (cdr value))))
396 (abs (- (car value) (cdr value)))))))
397 (if value ; force it to be in 32-bit format.
398 (cons (ash value -16) (logand value 65535))
401 (defun select-convert-to-targets (selection type value)
402 ;; return a vector of atoms, but remove duplicates first.
403 (let* ((all (cons 'TIMESTAMP (mapcar 'car selection-converter-alist)))
406 (cond ((memq (car rest) (cdr rest))
407 (setcdr rest (delq (car rest) (cdr rest))))
408 ((eq (car (cdr rest)) '_EMACS_INTERNAL) ; shh, it's a secret
409 (setcdr rest (cdr (cdr rest))))
411 (setq rest (cdr rest)))))
412 (apply 'vector all)))
414 (defun select-convert-to-delete (selection type value)
415 (disown-selection-internal selection)
416 ;; A return value of nil means that we do not know how to do this conversion,
417 ;; and replies with an "error". A return value of NULL means that we have
418 ;; done the conversion (and any side-effects) but have no value to return.
421 (defun select-convert-to-filename (selection type value)
422 (cond ((extentp value)
423 (buffer-file-name (or (extent-object value)
424 (error "selection is in a killed buffer"))))
426 (markerp (car value))
427 (markerp (cdr value)))
428 (buffer-file-name (or (marker-buffer (car value))
429 (error "selection is in a killed buffer"))))
432 (defun select-convert-to-charpos (selection type value)
434 (cond ((cond ((extentp value)
435 (setq a (extent-start-position value)
436 b (extent-end-position value)))
438 (markerp (car value))
439 (markerp (cdr value)))
442 (setq a (1- a) b (1- b)) ; zero-based
443 (if (< b a) (setq tmp a a b b tmp))
445 (vector (cons (ash a -16) (logand a 65535))
446 (cons (ash b -16) (logand b 65535))))))))
448 (defun select-convert-to-lineno (selection type value)
450 (cond ((cond ((extentp value)
451 (setq buf (extent-object value)
452 a (extent-start-position value)
453 b (extent-end-position value)))
455 (markerp (car value))
456 (markerp (cdr value)))
457 (setq a (marker-position (car value))
458 b (marker-position (cdr value))
459 buf (marker-buffer (car value)))))
466 (setq a (1+ (count-lines 1 (point))))
469 (setq b (1+ (count-lines 1 (point))))))
470 (if (< b a) (setq tmp a a b b tmp))
472 (vector (cons (ash a -16) (logand a 65535))
473 (cons (ash b -16) (logand b 65535))))))))
475 (defun select-convert-to-colno (selection type value)
477 (cond ((cond ((extentp value)
478 (setq buf (extent-object value)
479 a (extent-start-position value)
480 b (extent-end-position value)))
482 (markerp (car value))
483 (markerp (cdr value)))
486 buf (marker-buffer a))))
490 (setq a (current-column))
492 (setq b (current-column)))
493 (if (< b a) (setq tmp a a b b tmp))
495 (vector (cons (ash a -16) (logand a 65535))
496 (cons (ash b -16) (logand b 65535))))))))
498 (defun select-convert-to-sourceloc (selection type value)
499 (let (a b buf file-name tmp)
500 (cond ((cond ((extentp value)
501 (setq buf (or (extent-object value)
502 (error "selection is in a killed buffer"))
503 a (extent-start-position value)
504 b (extent-end-position value)
505 file-name (buffer-file-name buf)))
507 (markerp (car value))
508 (markerp (cdr value)))
509 (setq a (marker-position (car value))
510 b (marker-position (cdr value))
511 buf (or (marker-buffer (car value))
512 (error "selection is in a killed buffer"))
513 file-name (buffer-file-name buf))))
520 (setq a (1+ (count-lines 1 (point))))
523 (setq b (1+ (count-lines 1 (point))))))
524 (if (< b a) (setq tmp a a b b tmp))
525 (format "%s:%d" file-name a)))))
527 (defun select-convert-to-os (selection type size)
528 (symbol-name system-type))
530 (defun select-convert-to-host (selection type size)
533 (defun select-convert-to-user (selection type size)
536 (defun select-convert-to-class (selection type size)
537 x-emacs-application-class)
539 ;; We do not try to determine the name Emacs was invoked with,
540 ;; because it is not clean for a program's behavior to depend on that.
541 (defun select-convert-to-name (selection type size)
545 (defun select-convert-to-integer (selection type value)
546 (and (integerp value)
547 (cons (ash value -16) (logand value 65535))))
549 (defun select-convert-to-atom (selection type value)
550 (and (symbolp value) value))
552 (defun select-convert-to-identity (selection type value) ; used internally
555 (setq selection-converter-alist
556 '((TEXT . select-convert-to-text)
557 (STRING . select-convert-to-string)
558 (COMPOUND_TEXT . select-convert-to-compound-text)
559 (TARGETS . select-convert-to-targets)
560 (LENGTH . select-convert-to-length)
561 (DELETE . select-convert-to-delete)
562 (FILE_NAME . select-convert-to-filename)
563 (CHARACTER_POSITION . select-convert-to-charpos)
564 (SOURCE_LOC . select-convert-to-sourceloc)
565 (LINE_NUMBER . select-convert-to-lineno)
566 (COLUMN_NUMBER . select-convert-to-colno)
567 (OWNER_OS . select-convert-to-os)
568 (HOST_NAME . select-convert-to-host)
569 (USER . select-convert-to-user)
570 (CLASS . select-convert-to-class)
571 (NAME . select-convert-to-name)
572 (ATOM . select-convert-to-atom)
573 (INTEGER . select-convert-to-integer)
574 (_EMACS_INTERNAL . select-convert-to-identity)
577 ;;; select.el ends here