comparison lisp/select.el @ 398:74fd4e045ea6 r21-2-29

Import from CVS: tag r21-2-29
author cvs
date Mon, 13 Aug 2007 11:13:30 +0200
parents aabb7f5b1c81
children 2f8bb876ab1d
comparison
equal deleted inserted replaced
397:f4aeb21a5bad 398:74fd4e045ea6
29 ;;; Commentary: 29 ;;; Commentary:
30 30
31 ;; This file is dumped with XEmacs 31 ;; This file is dumped with XEmacs
32 32
33 ;;; Code: 33 ;;; Code:
34
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.")
42
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
46 set the clipboard.")
34 47
35 (defun copy-primary-selection () 48 (defun copy-primary-selection ()
36 "Copy the selection to the Clipboard and the kill ring." 49 "Copy the selection to the Clipboard and the kill ring."
37 (interactive) 50 (interactive)
38 (and (console-on-window-system-p) 51 (and (console-on-window-system-p)
39 (cut-copy-clear-internal 'copy))) 52 (cut-copy-clear-internal 'copy)))
40 (define-obsolete-function-alias
41 'x-copy-primary-selection
42 'copy-primary-selection)
43 53
44 (defun kill-primary-selection () 54 (defun kill-primary-selection ()
45 "Copy the selection to the Clipboard and the kill ring, then delete it." 55 "Copy the selection to the Clipboard and the kill ring, then delete it."
46 (interactive "*") 56 (interactive "*")
47 (and (console-on-window-system-p) 57 (and (console-on-window-system-p)
48 (cut-copy-clear-internal 'cut))) 58 (cut-copy-clear-internal 'cut)))
49 (define-obsolete-function-alias
50 'x-kill-primary-selection
51 'kill-primary-selection)
52 59
53 (defun delete-primary-selection () 60 (defun delete-primary-selection ()
54 "Delete the selection without copying it to the Clipboard or the kill ring." 61 "Delete the selection without copying it to the Clipboard or the kill ring."
55 (interactive "*") 62 (interactive "*")
56 (and (console-on-window-system-p) 63 (and (console-on-window-system-p)
57 (cut-copy-clear-internal 'clear))) 64 (cut-copy-clear-internal 'clear)))
58 (define-obsolete-function-alias
59 'x-delete-primary-selection
60 'delete-primary-selection)
61 65
62 (defun yank-clipboard-selection () 66 (defun yank-clipboard-selection ()
63 "Insert the current Clipboard selection at point." 67 "Insert the current Clipboard selection at point."
64 (interactive "*") 68 (interactive "*")
65 (case (device-type (selected-device)) 69 (when (console-on-window-system-p)
66 (x (x-yank-clipboard-selection)) 70 (setq last-command nil)
67 (mswindows (mswindows-paste-clipboard)) 71 (setq this-command 'yank) ; so that yank-pop works.
68 (otherwise nil))) 72 (let ((clip (get-clipboard)))
69 73 (or clip (error "there is no clipboard selection"))
70 (defun selection-owner-p (&optional selection) 74 (push-mark)
71 "Return t if current XEmacs process owns the given Selection. 75 (insert clip))))
72 The arg should be the name of the selection in question, typically one 76
73 of the symbols PRIMARY, SECONDARY, or CLIPBOARD. (For convenience, 77 (defun get-clipboard ()
74 the symbol nil is the same as PRIMARY, and t is the same as 78 "Return text pasted to the clipboard."
75 SECONDARY.)" 79 (get-selection 'CLIPBOARD))
76 (interactive) 80
77 (case (device-type (selected-device)) 81 (define-device-method get-cutbuffer
78 (x (x-selection-owner-p selection)) 82 "Return the value of one of the cut buffers.
79 (mswindows (mswindows-selection-owner-p selection)) 83 This will do nothing under anything other than X.")
80 (otherwise nil))) 84
81 85 (defun get-selection-no-error (&optional type data-type)
82 (defun selection-exists-p (&optional selection) 86 "Return the value of a Windows selection.
83 "Whether there is an owner for the given Selection. 87 The argument TYPE (default `PRIMARY') says which selection,
84 The arg should be the name of the selection in question, typically one 88 and the argument DATA-TYPE (default `STRING', or `COMPOUND_TEXT' under Mule)
85 of the symbols PRIMARY, SECONDARY, or CLIPBOARD. (For convenience, 89 says how to convert the data. Returns NIL if there is no selection"
86 the symbol nil is the same as PRIMARY, and t is the same as 90 (condition-case err (get-selection type data-type) (t nil)))
87 SECONDARY." 91
88 (interactive) 92 (defun get-selection (&optional type data-type)
89 (case (device-type (selected-device)) 93 "Return the value of a Windows selection.
90 (x (x-selection-exists-p selection)) 94 The argument TYPE (default `PRIMARY') says which selection,
91 (mswindows (mswindows-selection-exists-p)) 95 and the argument DATA-TYPE (default `STRING', or `COMPOUND_TEXT' under Mule)
92 (otherwise nil))) 96 says how to convert the data. If there is no selection an error is signalled."
93 97 (or type (setq type 'PRIMARY))
98 (or data-type (setq data-type selected-text-type))
99 (let ((text
100 (if (consp data-type)
101 (condition-case err
102 (get-selection-internal type (car data-type))
103 (selection-conversion-error
104 (if (cdr data-type)
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))
112 text))
113
114 ;; FSFmacs calls this `x-set-selection', and reverses the
115 ;; arguments (duh ...). This order is more logical.
94 (defun own-selection (data &optional type) 116 (defun own-selection (data &optional type)
95 "Make an Windows selection of type TYPE and value DATA. 117 "Make an Windows selection of type TYPE and value DATA.
96 The argument TYPE (default `PRIMARY') says which selection, 118 The argument TYPE (default `PRIMARY') says which selection,
97 and DATA specifies the contents. DATA may be a string, 119 and DATA specifies the contents. DATA may be a string,
98 a symbol, an integer (or a cons of two integers or list of two integers). 120 a symbol, an integer (or a cons of two integers or list of two integers).
107 129
108 Interactively, the text of the region is used as the selection value." 130 Interactively, the text of the region is used as the selection value."
109 (interactive (if (not current-prefix-arg) 131 (interactive (if (not current-prefix-arg)
110 (list (read-string "Store text for pasting: ")) 132 (list (read-string "Store text for pasting: "))
111 (list (substring (region-beginning) (region-end))))) 133 (list (substring (region-beginning) (region-end)))))
112 (case (device-type (selected-device)) 134 ;FSFmacs huh?? It says:
113 (x (x-own-selection data type)) 135 ;; "This is for temporary compatibility with pre-release Emacs 19."
114 (mswindows (mswindows-own-selection data type)) 136 ;(if (stringp type)
115 (otherwise nil))) 137 ; (setq type (intern type)))
138 (or (valid-simple-selection-p data)
139 (and (vectorp data)
140 (let ((valid t)
141 (i (1- (length data))))
142 (while (>= i 0)
143 (or (valid-simple-selection-p (aref data i))
144 (setq valid nil))
145 (setq i (1- i)))
146 valid))
147 (signal 'error (list "invalid selection" data)))
148 (or type (setq type 'PRIMARY))
149 (if (null data)
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)
164 data)
165
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)))))
183 nil)
184
185 (setq lost-selection-hooks 'dehilight-selection)
116 186
117 (defun own-clipboard (string) 187 (defun own-clipboard (string)
118 "Paste the given string to the Clipboard." 188 "Paste the given string to the window system Clipboard."
119 (case (device-type (selected-device)) 189 (own-selection string 'CLIPBOARD))
120 (x (x-own-clipboard string))
121 (mswindows (mswindows-own-clipboard string))
122 (otherwise nil)))
123 190
124 (defun disown-selection (&optional secondary-p) 191 (defun disown-selection (&optional secondary-p)
125 "Assuming we own the selection, disown it. With an argument, discard the 192 "Assuming we own the selection, disown it. With an argument, discard the
126 secondary selection instead of the primary selection." 193 secondary selection instead of the primary selection."
127 (case (device-type (selected-device)) 194 (disown-selection-internal (if secondary-p 'SECONDARY 'PRIMARY))
128 (x (x-disown-selection secondary-p)) 195 (when (and selection-sets-clipboard
129 (mswindows (mswindows-disown-selection secondary-p)) 196 (or (not secondary-p)
130 (otherwise nil))) 197 (eq secondary-p 'PRIMARY)
131 198 (eq secondary-p 'CLIPBOARD)))
199 (disown-selection-internal 'CLIPBOARD)))
132 200
133 ;; from x-init.el 201 ;; from x-init.el
134 ;; selections and active regions 202 ;; selections and active regions
135 203
136 ;; If and only if zmacs-regions is true: 204 ;; If and only if zmacs-regions is true:
216 (mouse-track-rectangle-p 284 (mouse-track-rectangle-p
217 (setq previous-extent (list previous-extent)) 285 (setq previous-extent (list previous-extent))
218 (default-mouse-track-next-move-rect start end previous-extent) 286 (default-mouse-track-next-move-rect start end previous-extent)
219 )) 287 ))
220 previous-extent)))) 288 previous-extent))))
221 (define-obsolete-function-alias
222 'x-select-make-extent-for-selection
223 'select-make-extent-for-selection)
224 289
225 ;; moved from x-select.el 290 ;; moved from x-select.el
226 (defun valid-simple-selection-p (data) 291 (defun valid-simple-selection-p (data)
227 (or (stringp data) 292 (or (stringp data)
228 ;FSFmacs huh?? (symbolp data) 293 ;FSFmacs huh?? (symbolp data)
240 (marker-buffer (cdr data)) 305 (marker-buffer (cdr data))
241 (eq (marker-buffer (car data)) 306 (eq (marker-buffer (car data))
242 (marker-buffer (cdr data))) 307 (marker-buffer (cdr data)))
243 (buffer-live-p (marker-buffer (car data))) 308 (buffer-live-p (marker-buffer (car data)))
244 (buffer-live-p (marker-buffer (cdr data)))))) 309 (buffer-live-p (marker-buffer (cdr data))))))
245 (define-obsolete-function-alias
246 'x-valid-simple-selection-p
247 'valid-simple-selection-p)
248 310
249 (defun cut-copy-clear-internal (mode) 311 (defun cut-copy-clear-internal (mode)
250 (or (memq mode '(cut copy clear)) (error "unkown mode %S" mode)) 312 (or (memq mode '(cut copy clear)) (error "unkown mode %S" mode))
251 (or (selection-owner-p) 313 (or (selection-owner-p)
252 (error "XEmacs does not own the primary selection")) 314 (error "XEmacs does not own the primary selection"))
285 (if rect-p 347 (if rect-p
286 (delete-rectangle s e) 348 (delete-rectangle s e)
287 (delete-region s e)))) 349 (delete-region s e))))
288 (disown-selection nil) 350 (disown-selection nil)
289 ))) 351 )))
290 (define-obsolete-function-alias 352
291 'x-cut-copy-clear-internal 353 ;;; Functions to convert the selection into various other selection
292 'cut-copy-clear-internal) 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
357
358 (defun select-convert-to-text (selection type value)
359 (cond ((stringp value)
360 value)
361 ((extentp value)
362 (save-excursion
363 (set-buffer (extent-object value))
364 (save-restriction
365 (widen)
366 (buffer-substring (extent-start-position value)
367 (extent-end-position value)))))
368 ((and (consp value)
369 (markerp (car value))
370 (markerp (cdr value)))
371 (or (eq (marker-buffer (car value)) (marker-buffer (cdr value)))
372 (signal 'error
373 (list "markers must be in the same buffer"
374 (car value) (cdr value))))
375 (save-excursion
376 (set-buffer (or (marker-buffer (car value))
377 (error "selection is in a killed buffer")))
378 (save-restriction
379 (widen)
380 (buffer-substring (car value) (cdr value)))))
381 (t nil)))
382
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.
386 (if (stringp outval)
387 (cons 'STRING outval)
388 outval)))
389
390 (defun select-convert-to-compound-text (selection type value)
391 ;; converts to compound text automatically
392 (select-convert-to-text selection type value))
393
394 (defun select-convert-to-length (selection type value)
395 (let ((value
396 (cond ((stringp value)
397 (length value))
398 ((extentp value)
399 (extent-length value))
400 ((and (consp value)
401 (markerp (car value))
402 (markerp (cdr value)))
403 (or (eq (marker-buffer (car value))
404 (marker-buffer (cdr value)))
405 (signal 'error
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))
411 nil)))
412
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)))
416 (rest all))
417 (while rest
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))))
422 (t
423 (setq rest (cdr rest)))))
424 (apply 'vector all)))
425
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.
431 'NULL)
432
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"))))
437 ((and (consp value)
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"))))
442 (t nil)))
443
444 (defun select-convert-to-charpos (selection type value)
445 (let (a b tmp)
446 (cond ((cond ((extentp value)
447 (setq a (extent-start-position value)
448 b (extent-end-position value)))
449 ((and (consp value)
450 (markerp (car value))
451 (markerp (cdr value)))
452 (setq a (car value)
453 b (cdr value))))
454 (setq a (1- a) b (1- b)) ; zero-based
455 (if (< b a) (setq tmp a a b b tmp))
456 (cons 'SPAN
457 (vector (cons (ash a -16) (logand a 65535))
458 (cons (ash b -16) (logand b 65535))))))))
459
460 (defun select-convert-to-lineno (selection type value)
461 (let (a b buf tmp)
462 (cond ((cond ((extentp value)
463 (setq buf (extent-object value)
464 a (extent-start-position value)
465 b (extent-end-position value)))
466 ((and (consp 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)))))
472 (save-excursion
473 (set-buffer buf)
474 (save-restriction
475 (widen)
476 (goto-char a)
477 (beginning-of-line)
478 (setq a (1+ (count-lines 1 (point))))
479 (goto-char b)
480 (beginning-of-line)
481 (setq b (1+ (count-lines 1 (point))))))
482 (if (< b a) (setq tmp a a b b tmp))
483 (cons 'SPAN
484 (vector (cons (ash a -16) (logand a 65535))
485 (cons (ash b -16) (logand b 65535))))))))
486
487 (defun select-convert-to-colno (selection type value)
488 (let (a b buf tmp)
489 (cond ((cond ((extentp value)
490 (setq buf (extent-object value)
491 a (extent-start-position value)
492 b (extent-end-position value)))
493 ((and (consp value)
494 (markerp (car value))
495 (markerp (cdr value)))
496 (setq a (car value)
497 b (cdr value)
498 buf (marker-buffer a))))
499 (save-excursion
500 (set-buffer buf)
501 (goto-char a)
502 (setq a (current-column))
503 (goto-char b)
504 (setq b (current-column)))
505 (if (< b a) (setq tmp a a b b tmp))
506 (cons 'SPAN
507 (vector (cons (ash a -16) (logand a 65535))
508 (cons (ash b -16) (logand b 65535))))))))
509
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)))
518 ((and (consp value)
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))))
526 (save-excursion
527 (set-buffer buf)
528 (save-restriction
529 (widen)
530 (goto-char a)
531 (beginning-of-line)
532 (setq a (1+ (count-lines 1 (point))))
533 (goto-char b)
534 (beginning-of-line)
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)))))
538
539 (defun select-convert-to-os (selection type size)
540 (symbol-name system-type))
541
542 (defun select-convert-to-host (selection type size)
543 (system-name))
544
545 (defun select-convert-to-user (selection type size)
546 (user-full-name))
547
548 (defun select-convert-to-class (selection type size)
549 x-emacs-application-class)
550
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)
554 ;invocation-name
555 "xemacs")
556
557 (defun select-convert-to-integer (selection type value)
558 (and (integerp value)
559 (cons (ash value -16) (logand value 65535))))
560
561 (defun select-convert-to-atom (selection type value)
562 (and (symbolp value) value))
563
564 (defun select-convert-to-identity (selection type value) ; used internally
565 (vector value))
566
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)
587 ))
293 588
294 ;;; select.el ends here 589 ;;; select.el ends here