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 (when (console-on-window-system-p)
70 (setq last-command nil)
71 (setq this-command 'yank) ; so that yank-pop works.
72 (let ((clip (get-clipboard)))
73 (or clip (error "there is no clipboard selection"))
77 (defun get-clipboard ()
78 "Return text pasted to the clipboard."
79 (get-selection 'CLIPBOARD))
81 (define-device-method get-cutbuffer
82 "Return the value of one of the cut buffers.
83 This will do nothing under anything other than X.")
85 (defun get-selection-no-error (&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. Returns NIL if there is no selection"
90 (condition-case err (get-selection type data-type) (t nil)))
92 (defun get-selection (&optional type data-type)
93 "Return the value of a Windows selection.
94 The argument TYPE (default `PRIMARY') says which selection,
95 and the argument DATA-TYPE (default `STRING', or `COMPOUND_TEXT' under Mule)
96 says how to convert the data. If there is no selection an error is signalled."
97 (or type (setq type 'PRIMARY))
98 (or data-type (setq data-type selected-text-type))
100 (if (consp data-type)
102 (get-selection-internal type (car data-type))
103 (selection-conversion-error
105 (get-selection type (cdr data-type))
106 (signal (car err) (cdr err)))))
107 (get-selection-internal type data-type))))
108 (when (and (consp text) (symbolp (car text)))
109 (setq text (cdr text)))
110 (when (not (stringp text))
111 (error "Selection is not a string: %S" text))
114 ;; FSFmacs calls this `x-set-selection', and reverses the
115 ;; arguments (duh ...). This order is more logical.
116 (defun own-selection (data &optional type)
117 "Make an Windows selection of type TYPE and value DATA.
118 The argument TYPE (default `PRIMARY') says which selection,
119 and DATA specifies the contents. DATA may be a string,
120 a symbol, an integer (or a cons of two integers or list of two integers).
122 The selection may also be a cons of two markers pointing to the same buffer,
123 or an overlay. In these cases, the selection is considered to be the text
124 between the markers *at whatever time the selection is examined*.
125 Thus, editing done in the buffer after you specify the selection
126 can alter the effective value of the selection.
128 The data may also be a vector of valid non-vector selection values.
130 Interactively, the text of the region is used as the selection value."
131 (interactive (if (not current-prefix-arg)
132 (list (read-string "Store text for pasting: "))
133 (list (substring (region-beginning) (region-end)))))
134 ;FSFmacs huh?? It says:
135 ;; "This is for temporary compatibility with pre-release Emacs 19."
137 ; (setq type (intern type)))
138 (or (valid-simple-selection-p data)
141 (i (1- (length data))))
143 (or (valid-simple-selection-p (aref data i))
147 (signal 'error (list "invalid selection" data)))
148 (or type (setq type 'PRIMARY))
150 (disown-selection-internal type)
151 (own-selection-internal type data)
152 (when (and (eq type 'PRIMARY)
153 selection-sets-clipboard)
154 (own-selection-internal 'CLIPBOARD data)))
155 (cond ((eq type 'PRIMARY)
156 (setq primary-selection-extent
157 (select-make-extent-for-selection
158 data primary-selection-extent)))
159 ((eq type 'SECONDARY)
160 (setq secondary-selection-extent
161 (select-make-extent-for-selection
162 data secondary-selection-extent))))
163 (setq zmacs-region-stays t)
166 (defun dehilight-selection (selection)
167 "for use as a value of `lost-selection-hooks'."
168 (cond ((eq selection 'PRIMARY)
169 (if primary-selection-extent
170 (let ((inhibit-quit t))
171 (if (consp primary-selection-extent)
172 (mapcar 'delete-extent primary-selection-extent)
173 (delete-extent primary-selection-extent))
174 (setq primary-selection-extent nil)))
175 (if zmacs-regions (zmacs-deactivate-region)))
176 ((eq selection 'SECONDARY)
177 (if secondary-selection-extent
178 (let ((inhibit-quit t))
179 (if (consp secondary-selection-extent)
180 (mapcar 'delete-extent secondary-selection-extent)
181 (delete-extent secondary-selection-extent))
182 (setq secondary-selection-extent nil)))))
185 (setq lost-selection-hooks 'dehilight-selection)
187 (defun own-clipboard (string)
188 "Paste the given string to the window system Clipboard."
189 (own-selection string 'CLIPBOARD))
191 (defun disown-selection (&optional secondary-p)
192 "Assuming we own the selection, disown it. With an argument, discard the
193 secondary selection instead of the primary selection."
194 (disown-selection-internal (if secondary-p 'SECONDARY 'PRIMARY))
195 (when (and selection-sets-clipboard
196 (or (not secondary-p)
197 (eq secondary-p 'PRIMARY)
198 (eq secondary-p 'CLIPBOARD)))
199 (disown-selection-internal 'CLIPBOARD)))
202 ;; selections and active regions
204 ;; If and only if zmacs-regions is true:
206 ;; When a mark is pushed and the region goes into the "active" state, we
207 ;; assert it as the Primary selection. This causes it to be hilighted.
208 ;; When the region goes into the "inactive" state, we disown the Primary
209 ;; selection, causing the region to be dehilighted.
211 ;; Note that it is possible for the region to be in the "active" state
212 ;; and not be hilighted, if it is in the active state and then some other
213 ;; application asserts the selection. This is probably not a big deal.
215 (defun activate-region-as-selection ()
216 (if (marker-buffer (mark-marker t))
217 (own-selection (cons (point-marker t) (mark-marker t)))))
219 ; moved from x-select.el
220 (defvar primary-selection-extent nil
221 "The extent of the primary selection; don't use this.")
223 (defvar secondary-selection-extent nil
224 "The extent of the secondary selection; don't use this.")
226 (defun select-make-extent-for-selection (selection previous-extent)
227 ;; Given a selection, this makes an extent in the buffer which holds that
228 ;; selection, for highlighting purposes. If the selection isn't associated
229 ;; with a buffer, this does nothing.
231 (valid (and (extentp previous-extent)
232 (extent-object previous-extent)
233 (buffer-live-p (extent-object previous-extent))))
235 (cond ((stringp selection)
236 ;; if we're selecting a string, lose the previous extent used
237 ;; to highlight the selection.
240 (setq start (min (car selection) (cdr selection))
241 end (max (car selection) (cdr selection))
243 (eq (marker-buffer (car selection))
244 (extent-object previous-extent)))
245 buffer (marker-buffer (car selection))))
247 (setq start (extent-start-position selection)
248 end (extent-end-position selection)
250 (eq (extent-object selection)
251 (extent-object previous-extent)))
252 buffer (extent-object selection)))
254 (signal 'error (list "invalid selection" selection))))
259 (if (listp previous-extent)
260 (mapcar 'delete-extent previous-extent)
261 (delete-extent previous-extent))
269 (set-extent-endpoints previous-extent start end)
270 (setq previous-extent (make-extent start end buffer))
272 ;; Make the extent be closed on the right, which means that if
273 ;; characters are inserted exactly at the end of the extent, the
274 ;; extent will grow to cover them. This is important for shell
275 ;; buffers - suppose one makes a selection, and one end is at
276 ;; point-max. If the shell produces output, that marker will remain
277 ;; at point-max (its position will increase). So it's important that
278 ;; the extent exhibit the same behavior, lest the region covered by
279 ;; the extent (the visual indication), and the region between point
280 ;; and mark (the actual selection value) become different!
281 (set-extent-property previous-extent 'end-open nil)
284 (mouse-track-rectangle-p
285 (setq previous-extent (list previous-extent))
286 (default-mouse-track-next-move-rect start end previous-extent)
290 ;; moved from x-select.el
291 (defun valid-simple-selection-p (data)
293 ;FSFmacs huh?? (symbolp data)
296 (integerp (car data))
297 (or (integerp (cdr data))
298 (and (consp (cdr data))
299 (integerp (car (cdr data))))))
304 (marker-buffer (car data))
305 (marker-buffer (cdr data))
306 (eq (marker-buffer (car data))
307 (marker-buffer (cdr data)))
308 (buffer-live-p (marker-buffer (car data)))
309 (buffer-live-p (marker-buffer (cdr data))))))
311 (defun cut-copy-clear-internal (mode)
312 (or (memq mode '(cut copy clear)) (error "unkown mode %S" mode))
313 (or (selection-owner-p)
314 (error "XEmacs does not own the primary selection"))
315 (setq last-command nil)
316 (or primary-selection-extent
317 (error "the primary selection is not an extent?"))
321 ((consp primary-selection-extent)
323 b (extent-object (car primary-selection-extent))
324 s (extent-start-position (car primary-selection-extent))
325 e (extent-end-position (car (reverse primary-selection-extent)))))
328 b (extent-object primary-selection-extent)
329 s (extent-start-position primary-selection-extent)
330 e (extent-end-position primary-selection-extent))))
332 (cond ((memq mode '(cut copy))
335 ;; why is killed-rectangle free? Is it used somewhere?
336 ;; should it be defvarred?
337 (setq killed-rectangle (extract-rectangle s e))
338 (kill-new (mapconcat #'identity killed-rectangle "\n")))
339 (copy-region-as-kill s e))
340 ;; Maybe killing doesn't own clipboard. Make sure it happens.
341 ;; This memq is kind of grody, because they might have done it
342 ;; some other way, but owning the clipboard twice in that case
343 ;; wouldn't actually hurt anything.
344 (or (and (consp kill-hooks) (memq 'own-clipboard kill-hooks))
345 (own-clipboard (car kill-ring)))))
346 (cond ((memq mode '(cut clear))
348 (delete-rectangle s e)
349 (delete-region s e))))
350 (disown-selection nil)
353 ;;; Functions to convert the selection into various other selection
354 ;;; types. Every selection type that emacs handles is implemented
355 ;;; this way, except for TIMESTAMP, which is a special case. These are
356 ;;; all moved from x-select.el
358 (defun select-convert-to-text (selection type value)
359 (cond ((stringp value)
363 (set-buffer (extent-object value))
366 (buffer-substring (extent-start-position value)
367 (extent-end-position value)))))
369 (markerp (car value))
370 (markerp (cdr value)))
371 (or (eq (marker-buffer (car value)) (marker-buffer (cdr value)))
373 (list "markers must be in the same buffer"
374 (car value) (cdr value))))
376 (set-buffer (or (marker-buffer (car value))
377 (error "selection is in a killed buffer")))
380 (buffer-substring (car value) (cdr value)))))
383 (defun select-convert-to-string (selection type value)
384 (let ((outval (select-convert-to-text selection type value)))
385 ;; force the string to be not in Compound Text format.
387 (cons 'STRING outval)
390 (defun select-convert-to-compound-text (selection type value)
391 ;; converts to compound text automatically
392 (select-convert-to-text selection type value))
394 (defun select-convert-to-length (selection type value)
396 (cond ((stringp value)
399 (extent-length value))
401 (markerp (car value))
402 (markerp (cdr value)))
403 (or (eq (marker-buffer (car value))
404 (marker-buffer (cdr value)))
406 (list "markers must be in the same buffer"
407 (car value) (cdr value))))
408 (abs (- (car value) (cdr value)))))))
409 (if value ; force it to be in 32-bit format.
410 (cons (ash value -16) (logand value 65535))
413 (defun select-convert-to-targets (selection type value)
414 ;; return a vector of atoms, but remove duplicates first.
415 (let* ((all (cons 'TIMESTAMP (mapcar 'car selection-converter-alist)))
418 (cond ((memq (car rest) (cdr rest))
419 (setcdr rest (delq (car rest) (cdr rest))))
420 ((eq (car (cdr rest)) '_EMACS_INTERNAL) ; shh, it's a secret
421 (setcdr rest (cdr (cdr rest))))
423 (setq rest (cdr rest)))))
424 (apply 'vector all)))
426 (defun select-convert-to-delete (selection type value)
427 (disown-selection-internal selection)
428 ;; A return value of nil means that we do not know how to do this conversion,
429 ;; and replies with an "error". A return value of NULL means that we have
430 ;; done the conversion (and any side-effects) but have no value to return.
433 (defun select-convert-to-filename (selection type value)
434 (cond ((extentp value)
435 (buffer-file-name (or (extent-object value)
436 (error "selection is in a killed buffer"))))
438 (markerp (car value))
439 (markerp (cdr value)))
440 (buffer-file-name (or (marker-buffer (car value))
441 (error "selection is in a killed buffer"))))
444 (defun select-convert-to-charpos (selection type value)
446 (cond ((cond ((extentp value)
447 (setq a (extent-start-position value)
448 b (extent-end-position value)))
450 (markerp (car value))
451 (markerp (cdr value)))
454 (setq a (1- a) b (1- b)) ; zero-based
455 (if (< b a) (setq tmp a a b b tmp))
457 (vector (cons (ash a -16) (logand a 65535))
458 (cons (ash b -16) (logand b 65535))))))))
460 (defun select-convert-to-lineno (selection type value)
462 (cond ((cond ((extentp value)
463 (setq buf (extent-object value)
464 a (extent-start-position value)
465 b (extent-end-position value)))
467 (markerp (car value))
468 (markerp (cdr value)))
469 (setq a (marker-position (car value))
470 b (marker-position (cdr value))
471 buf (marker-buffer (car value)))))
478 (setq a (1+ (count-lines 1 (point))))
481 (setq b (1+ (count-lines 1 (point))))))
482 (if (< b a) (setq tmp a a b b tmp))
484 (vector (cons (ash a -16) (logand a 65535))
485 (cons (ash b -16) (logand b 65535))))))))
487 (defun select-convert-to-colno (selection type value)
489 (cond ((cond ((extentp value)
490 (setq buf (extent-object value)
491 a (extent-start-position value)
492 b (extent-end-position value)))
494 (markerp (car value))
495 (markerp (cdr value)))
498 buf (marker-buffer a))))
502 (setq a (current-column))
504 (setq b (current-column)))
505 (if (< b a) (setq tmp a a b b tmp))
507 (vector (cons (ash a -16) (logand a 65535))
508 (cons (ash b -16) (logand b 65535))))))))
510 (defun select-convert-to-sourceloc (selection type value)
511 (let (a b buf file-name tmp)
512 (cond ((cond ((extentp value)
513 (setq buf (or (extent-object value)
514 (error "selection is in a killed buffer"))
515 a (extent-start-position value)
516 b (extent-end-position value)
517 file-name (buffer-file-name buf)))
519 (markerp (car value))
520 (markerp (cdr value)))
521 (setq a (marker-position (car value))
522 b (marker-position (cdr value))
523 buf (or (marker-buffer (car value))
524 (error "selection is in a killed buffer"))
525 file-name (buffer-file-name buf))))
532 (setq a (1+ (count-lines 1 (point))))
535 (setq b (1+ (count-lines 1 (point))))))
536 (if (< b a) (setq tmp a a b b tmp))
537 (format "%s:%d" file-name a)))))
539 (defun select-convert-to-os (selection type size)
540 (symbol-name system-type))
542 (defun select-convert-to-host (selection type size)
545 (defun select-convert-to-user (selection type size)
548 (defun select-convert-to-class (selection type size)
549 x-emacs-application-class)
551 ;; We do not try to determine the name Emacs was invoked with,
552 ;; because it is not clean for a program's behavior to depend on that.
553 (defun select-convert-to-name (selection type size)
557 (defun select-convert-to-integer (selection type value)
558 (and (integerp value)
559 (cons (ash value -16) (logand value 65535))))
561 (defun select-convert-to-atom (selection type value)
562 (and (symbolp value) value))
564 (defun select-convert-to-identity (selection type value) ; used internally
567 (setq selection-converter-alist
568 '((TEXT . select-convert-to-text)
569 (STRING . select-convert-to-string)
570 (COMPOUND_TEXT . select-convert-to-compound-text)
571 (TARGETS . select-convert-to-targets)
572 (LENGTH . select-convert-to-length)
573 (DELETE . select-convert-to-delete)
574 (FILE_NAME . select-convert-to-filename)
575 (CHARACTER_POSITION . select-convert-to-charpos)
576 (SOURCE_LOC . select-convert-to-sourceloc)
577 (LINE_NUMBER . select-convert-to-lineno)
578 (COLUMN_NUMBER . select-convert-to-colno)
579 (OWNER_OS . select-convert-to-os)
580 (HOST_NAME . select-convert-to-host)
581 (USER . select-convert-to-user)
582 (CLASS . select-convert-to-class)
583 (NAME . select-convert-to-name)
584 (ATOM . select-convert-to-atom)
585 (INTEGER . select-convert-to-integer)
586 (_EMACS_INTERNAL . select-convert-to-identity)
589 ;;; select.el ends here