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-is-clipboard-p 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 (&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. If there is no selection an error is signalled."
83 (let ((text (get-selection-no-error type data-type)))
84 (when (not (stringp text))
85 (error "Selection is not a string: %S" text))
88 (defun get-selection-no-error (&optional type data-type)
89 "Return the value of a Windows selection.
90 The argument TYPE (default `PRIMARY') says which selection,
91 and the argument DATA-TYPE (default `STRING', or `COMPOUND_TEXT' under Mule)
92 says how to convert the data. Returns NIL if there is no selection"
93 (or type (setq type 'PRIMARY))
94 (or data-type (setq data-type selected-text-type))
98 (get-selection-internal type (car data-type))
99 (selection-conversion-error
101 (get-selection type (cdr data-type))
102 (signal (car err) (cdr err)))))
103 (get-selection-internal type data-type))))
104 (when (and (consp text) (symbolp (car text)))
105 (setq text (cdr text)))
108 ;; FSFmacs calls this `x-set-selection', and reverses the
109 ;; arguments (duh ...). This order is more logical.
110 (defun own-selection (data &optional type)
111 "Make an Windows selection of type TYPE and value DATA.
112 The argument TYPE (default `PRIMARY') says which selection,
113 and DATA specifies the contents. DATA may be a string,
114 a symbol, an integer (or a cons of two integers or list of two integers).
116 The selection may also be a cons of two markers pointing to the same buffer,
117 or an overlay. In these cases, the selection is considered to be the text
118 between the markers *at whatever time the selection is examined*.
119 Thus, editing done in the buffer after you specify the selection
120 can alter the effective value of the selection.
122 The data may also be a vector of valid non-vector selection values.
124 Interactively, the text of the region is used as the selection value."
125 (interactive (if (not current-prefix-arg)
126 (list (read-string "Store text for pasting: "))
127 (list (substring (region-beginning) (region-end)))))
128 ;FSFmacs huh?? It says:
129 ;; "This is for temporary compatibility with pre-release Emacs 19."
131 ; (setq type (intern type)))
132 (or (valid-simple-selection-p data)
135 (i (1- (length data))))
137 (or (valid-simple-selection-p (aref data i))
141 (signal 'error (list "invalid selection" data)))
142 (or type (setq type 'PRIMARY))
144 (disown-selection-internal type)
145 (own-selection-internal type data)
146 (when (and (eq type 'PRIMARY)
147 selection-is-clipboard-p)
148 (own-selection-internal 'CLIPBOARD data)))
149 (cond ((eq type 'PRIMARY)
150 (setq primary-selection-extent
151 (select-make-extent-for-selection
152 data primary-selection-extent)))
153 ((eq type 'SECONDARY)
154 (setq secondary-selection-extent
155 (select-make-extent-for-selection
156 data secondary-selection-extent))))
157 (setq zmacs-region-stays t)
160 (defun dehilight-selection (selection)
161 "for use as a value of `lost-selection-hooks'."
162 (cond ((eq selection 'PRIMARY)
163 (if primary-selection-extent
164 (let ((inhibit-quit t))
165 (if (consp primary-selection-extent)
166 (mapcar 'delete-extent primary-selection-extent)
167 (delete-extent primary-selection-extent))
168 (setq primary-selection-extent nil)))
169 (if zmacs-regions (zmacs-deactivate-region)))
170 ((eq selection 'SECONDARY)
171 (if secondary-selection-extent
172 (let ((inhibit-quit t))
173 (if (consp secondary-selection-extent)
174 (mapcar 'delete-extent secondary-selection-extent)
175 (delete-extent secondary-selection-extent))
176 (setq secondary-selection-extent nil)))))
179 (setq lost-selection-hooks 'dehilight-selection)
181 (defun own-clipboard (string)
182 "Paste the given string to the X Clipboard."
183 (own-selection string 'CLIPBOARD))
185 (defun disown-selection (&optional secondary-p)
186 "Assuming we own the selection, disown it. With an argument, discard the
187 secondary selection instead of the primary selection."
188 (disown-selection-internal (if secondary-p 'SECONDARY 'PRIMARY)))
191 ;; selections and active regions
193 ;; If and only if zmacs-regions is true:
195 ;; When a mark is pushed and the region goes into the "active" state, we
196 ;; assert it as the Primary selection. This causes it to be hilighted.
197 ;; When the region goes into the "inactive" state, we disown the Primary
198 ;; selection, causing the region to be dehilighted.
200 ;; Note that it is possible for the region to be in the "active" state
201 ;; and not be hilighted, if it is in the active state and then some other
202 ;; application asserts the selection. This is probably not a big deal.
204 (defun activate-region-as-selection ()
205 (if (marker-buffer (mark-marker t))
206 (own-selection (cons (point-marker t) (mark-marker t)))))
208 ; moved from x-select.el
209 (defvar primary-selection-extent nil
210 "The extent of the primary selection; don't use this.")
212 (defvar secondary-selection-extent nil
213 "The extent of the secondary selection; don't use this.")
215 (defun select-make-extent-for-selection (selection previous-extent)
216 ;; Given a selection, this makes an extent in the buffer which holds that
217 ;; selection, for highlighting purposes. If the selection isn't associated
218 ;; with a buffer, this does nothing.
220 (valid (and (extentp previous-extent)
221 (extent-object previous-extent)
222 (buffer-live-p (extent-object previous-extent))))
224 (cond ((stringp selection)
225 ;; if we're selecting a string, lose the previous extent used
226 ;; to highlight the selection.
229 (setq start (min (car selection) (cdr selection))
230 end (max (car selection) (cdr selection))
232 (eq (marker-buffer (car selection))
233 (extent-object previous-extent)))
234 buffer (marker-buffer (car selection))))
236 (setq start (extent-start-position selection)
237 end (extent-end-position selection)
239 (eq (extent-object selection)
240 (extent-object previous-extent)))
241 buffer (extent-object selection)))
243 (signal 'error (list "invalid selection" selection))))
248 (if (listp previous-extent)
249 (mapcar 'delete-extent previous-extent)
250 (delete-extent previous-extent))
258 (set-extent-endpoints previous-extent start end)
259 (setq previous-extent (make-extent start end buffer))
261 ;; Make the extent be closed on the right, which means that if
262 ;; characters are inserted exactly at the end of the extent, the
263 ;; extent will grow to cover them. This is important for shell
264 ;; buffers - suppose one makes a selection, and one end is at
265 ;; point-max. If the shell produces output, that marker will remain
266 ;; at point-max (its position will increase). So it's important that
267 ;; the extent exhibit the same behavior, lest the region covered by
268 ;; the extent (the visual indication), and the region between point
269 ;; and mark (the actual selection value) become different!
270 (set-extent-property previous-extent 'end-open nil)
273 (mouse-track-rectangle-p
274 (setq previous-extent (list previous-extent))
275 (default-mouse-track-next-move-rect start end previous-extent)
279 ;; moved from x-select.el
280 (defun valid-simple-selection-p (data)
282 ;FSFmacs huh?? (symbolp data)
285 (integerp (car data))
286 (or (integerp (cdr data))
287 (and (consp (cdr data))
288 (integerp (car (cdr data))))))
293 (marker-buffer (car data))
294 (marker-buffer (cdr data))
295 (eq (marker-buffer (car data))
296 (marker-buffer (cdr data)))
297 (buffer-live-p (marker-buffer (car data)))
298 (buffer-live-p (marker-buffer (cdr data))))))
300 (defun cut-copy-clear-internal (mode)
301 (or (memq mode '(cut copy clear)) (error "unkown mode %S" mode))
302 (or (selection-owner-p)
303 (error "XEmacs does not own the primary selection"))
304 (setq last-command nil)
305 (or primary-selection-extent
306 (error "the primary selection is not an extent?"))
310 ((consp primary-selection-extent)
312 b (extent-object (car primary-selection-extent))
313 s (extent-start-position (car primary-selection-extent))
314 e (extent-end-position (car (reverse primary-selection-extent)))))
317 b (extent-object primary-selection-extent)
318 s (extent-start-position primary-selection-extent)
319 e (extent-end-position primary-selection-extent))))
321 (cond ((memq mode '(cut copy))
324 ;; why is killed-rectangle free? Is it used somewhere?
325 ;; should it be defvarred?
326 (setq killed-rectangle (extract-rectangle s e))
327 (kill-new (mapconcat #'identity killed-rectangle "\n")))
328 (copy-region-as-kill s e))
329 ;; Maybe killing doesn't own clipboard. Make sure it happens.
330 ;; This memq is kind of grody, because they might have done it
331 ;; some other way, but owning the clipboard twice in that case
332 ;; wouldn't actually hurt anything.
333 (or (and (consp kill-hooks) (memq 'own-clipboard kill-hooks))
334 (own-clipboard (car kill-ring)))))
335 (cond ((memq mode '(cut clear))
337 (delete-rectangle s e)
338 (delete-region s e))))
339 (disown-selection nil)
342 ;;; Functions to convert the selection into various other selection
343 ;;; types. Every selection type that emacs handles is implemented
344 ;;; this way, except for TIMESTAMP, which is a special case. These are
345 ;;; all moved from x-select.el
347 (defun select-convert-to-text (selection type value)
348 (cond ((stringp value)
352 (set-buffer (extent-object value))
355 (buffer-substring (extent-start-position value)
356 (extent-end-position value)))))
358 (markerp (car value))
359 (markerp (cdr value)))
360 (or (eq (marker-buffer (car value)) (marker-buffer (cdr value)))
362 (list "markers must be in the same buffer"
363 (car value) (cdr value))))
365 (set-buffer (or (marker-buffer (car value))
366 (error "selection is in a killed buffer")))
369 (buffer-substring (car value) (cdr value)))))
372 (defun select-convert-to-string (selection type value)
373 (let ((outval (select-convert-to-text selection type value)))
374 ;; force the string to be not in Compound Text format.
376 (cons 'STRING outval)
379 (defun select-convert-to-compound-text (selection type value)
380 ;; converts to compound text automatically
381 (select-convert-to-text selection type value))
383 (defun select-convert-to-length (selection type value)
385 (cond ((stringp value)
388 (extent-length value))
390 (markerp (car value))
391 (markerp (cdr value)))
392 (or (eq (marker-buffer (car value))
393 (marker-buffer (cdr value)))
395 (list "markers must be in the same buffer"
396 (car value) (cdr value))))
397 (abs (- (car value) (cdr value)))))))
398 (if value ; force it to be in 32-bit format.
399 (cons (ash value -16) (logand value 65535))
402 (defun select-convert-to-targets (selection type value)
403 ;; return a vector of atoms, but remove duplicates first.
404 (let* ((all (cons 'TIMESTAMP (mapcar 'car selection-converter-alist)))
407 (cond ((memq (car rest) (cdr rest))
408 (setcdr rest (delq (car rest) (cdr rest))))
409 ((eq (car (cdr rest)) '_EMACS_INTERNAL) ; shh, it's a secret
410 (setcdr rest (cdr (cdr rest))))
412 (setq rest (cdr rest)))))
413 (apply 'vector all)))
415 (defun select-convert-to-delete (selection type value)
416 (disown-selection-internal selection)
417 ;; A return value of nil means that we do not know how to do this conversion,
418 ;; and replies with an "error". A return value of NULL means that we have
419 ;; done the conversion (and any side-effects) but have no value to return.
422 (defun select-convert-to-filename (selection type value)
423 (cond ((extentp value)
424 (buffer-file-name (or (extent-object value)
425 (error "selection is in a killed buffer"))))
427 (markerp (car value))
428 (markerp (cdr value)))
429 (buffer-file-name (or (marker-buffer (car value))
430 (error "selection is in a killed buffer"))))
433 (defun select-convert-to-charpos (selection type value)
435 (cond ((cond ((extentp value)
436 (setq a (extent-start-position value)
437 b (extent-end-position value)))
439 (markerp (car value))
440 (markerp (cdr value)))
443 (setq a (1- a) b (1- b)) ; zero-based
444 (if (< b a) (setq tmp a a b b tmp))
446 (vector (cons (ash a -16) (logand a 65535))
447 (cons (ash b -16) (logand b 65535))))))))
449 (defun select-convert-to-lineno (selection type value)
451 (cond ((cond ((extentp value)
452 (setq buf (extent-object value)
453 a (extent-start-position value)
454 b (extent-end-position value)))
456 (markerp (car value))
457 (markerp (cdr value)))
458 (setq a (marker-position (car value))
459 b (marker-position (cdr value))
460 buf (marker-buffer (car value)))))
467 (setq a (1+ (count-lines 1 (point))))
470 (setq b (1+ (count-lines 1 (point))))))
471 (if (< b a) (setq tmp a a b b tmp))
473 (vector (cons (ash a -16) (logand a 65535))
474 (cons (ash b -16) (logand b 65535))))))))
476 (defun select-convert-to-colno (selection type value)
478 (cond ((cond ((extentp value)
479 (setq buf (extent-object value)
480 a (extent-start-position value)
481 b (extent-end-position value)))
483 (markerp (car value))
484 (markerp (cdr value)))
487 buf (marker-buffer a))))
491 (setq a (current-column))
493 (setq b (current-column)))
494 (if (< b a) (setq tmp a a b b tmp))
496 (vector (cons (ash a -16) (logand a 65535))
497 (cons (ash b -16) (logand b 65535))))))))
499 (defun select-convert-to-sourceloc (selection type value)
500 (let (a b buf file-name tmp)
501 (cond ((cond ((extentp value)
502 (setq buf (or (extent-object value)
503 (error "selection is in a killed buffer"))
504 a (extent-start-position value)
505 b (extent-end-position value)
506 file-name (buffer-file-name buf)))
508 (markerp (car value))
509 (markerp (cdr value)))
510 (setq a (marker-position (car value))
511 b (marker-position (cdr value))
512 buf (or (marker-buffer (car value))
513 (error "selection is in a killed buffer"))
514 file-name (buffer-file-name buf))))
521 (setq a (1+ (count-lines 1 (point))))
524 (setq b (1+ (count-lines 1 (point))))))
525 (if (< b a) (setq tmp a a b b tmp))
526 (format "%s:%d" file-name a)))))
528 (defun select-convert-to-os (selection type size)
529 (symbol-name system-type))
531 (defun select-convert-to-host (selection type size)
534 (defun select-convert-to-user (selection type size)
537 (defun select-convert-to-class (selection type size)
538 x-emacs-application-class)
540 ;; We do not try to determine the name Emacs was invoked with,
541 ;; because it is not clean for a program's behavior to depend on that.
542 (defun select-convert-to-name (selection type size)
546 (defun select-convert-to-integer (selection type value)
547 (and (integerp value)
548 (cons (ash value -16) (logand value 65535))))
550 (defun select-convert-to-atom (selection type value)
551 (and (symbolp value) value))
553 (defun select-convert-to-identity (selection type value) ; used internally
556 (setq selection-converter-alist
557 '((TEXT . select-convert-to-text)
558 (STRING . select-convert-to-string)
559 (COMPOUND_TEXT . select-convert-to-compound-text)
560 (TARGETS . select-convert-to-targets)
561 (LENGTH . select-convert-to-length)
562 (DELETE . select-convert-to-delete)
563 (FILE_NAME . select-convert-to-filename)
564 (CHARACTER_POSITION . select-convert-to-charpos)
565 (SOURCE_LOC . select-convert-to-sourceloc)
566 (LINE_NUMBER . select-convert-to-lineno)
567 (COLUMN_NUMBER . select-convert-to-colno)
568 (OWNER_OS . select-convert-to-os)
569 (HOST_NAME . select-convert-to-host)
570 (USER . select-convert-to-user)
571 (CLASS . select-convert-to-class)
572 (NAME . select-convert-to-name)
573 (ATOM . select-convert-to-atom)
574 (INTEGER . select-convert-to-integer)
575 (_EMACS_INTERNAL . select-convert-to-identity)
578 ;;; select.el ends here