1 ;;; select.el --- lisp portion of standard selection support
3 ;; Copyright (C) 1993, 1994, 2001, 2002, 2003, 2004, 2005, 2006, 2007,
4 ;; 2008, 2009, 2010 Free Software Foundation, Inc.
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
26 ;; Based partially on earlier release by Lucid.
30 (defcustom selection-coding-system nil
31 "Coding system for communicating with other programs.
33 For MS-Windows and MS-DOS:
34 When sending or receiving text via selection and clipboard, the text
35 is encoded or decoded by this coding system. The default value is
36 the current system default encoding on 9x/Me, `utf-16le-dos'
37 \(Unicode) on NT/W2K/XP, and `iso-latin-1-dos' on MS-DOS.
40 When sending text via selection and clipboard, if the target
41 data-type matches with the type of this coding system, it is used
42 for encoding the text. Otherwise (including the case that this
43 variable is nil), a proper coding system is used as below:
45 data-type coding system
46 --------- -------------
48 COMPOUND_TEXT compound-text-with-extensions
50 C_STRING no-conversion
52 When receiving text, if this coding system is non-nil, it is used
53 for decoding regardless of the data-type. If this is nil, a
54 proper coding system is used according to the data-type as above.
56 See also the documentation of the variable `x-select-request-type' how
57 to control which data-type to request for receiving text.
59 The default value is nil."
62 ;; Default was compound-text-with-extensions in 22.x (pre-unicode).
64 :set
(lambda (symbol value
)
65 (set-selection-coding-system value
)
68 (defvar next-selection-coding-system nil
69 "Coding system for the next communication with other programs.
70 Usually, `selection-coding-system' is used for communicating with
71 other programs (X Windows clients or MS Windows programs). But, if this
72 variable is set, it is used for the next communication only.
73 After the communication, this variable is set to nil.")
75 (declare-function x-get-selection-internal
"xselect.c"
76 (selection-symbol target-type
&optional time-stamp
))
78 ;; Only declared obsolete in 23.3.
79 (define-obsolete-function-alias 'x-selection
'x-get-selection
"at least 19.34")
81 (defun x-get-selection (&optional type data-type
)
82 "Return the value of an X Windows selection.
83 The argument TYPE (default `PRIMARY') says which selection,
84 and the argument DATA-TYPE (default `STRING') says
85 how to convert the data.
87 TYPE may be any symbol \(but nil stands for `PRIMARY'). However,
88 only a few symbols are commonly used. They conventionally have
89 all upper-case names. The most often used ones, in addition to
90 `PRIMARY', are `SECONDARY' and `CLIPBOARD'.
92 DATA-TYPE is usually `STRING', but can also be one of the symbols
93 in `selection-converter-alist', which see."
94 (let ((data (x-get-selection-internal (or type
'PRIMARY
)
95 (or data-type
'STRING
)))
97 (when (and (stringp data
)
98 (setq data-type
(get-text-property 0 'foreign-selection data
)))
99 (setq coding
(or next-selection-coding-system
100 selection-coding-system
101 (cond ((eq data-type
'UTF8_STRING
)
103 ((eq data-type
'COMPOUND_TEXT
)
104 'compound-text-with-extensions
)
105 ((eq data-type
'C_STRING
)
107 ((eq data-type
'STRING
)
110 (error "Unknow selection data type: %S" type
))))
111 data
(if coding
(decode-coding-string data coding
)
112 (string-to-multibyte data
)))
113 (setq next-selection-coding-system nil
)
114 (put-text-property 0 (length data
) 'foreign-selection data-type data
))
117 (defun x-get-clipboard ()
118 "Return text pasted to the clipboard."
119 (x-get-selection-internal 'CLIPBOARD
'STRING
))
121 (declare-function x-own-selection-internal
"xselect.c"
122 (selection-name selection-value
))
123 (declare-function x-disown-selection-internal
"xselect.c"
124 (selection &optional time
))
126 (defun x-set-selection (type data
)
127 "Make an X selection of type TYPE and value DATA.
128 The argument TYPE (nil means `PRIMARY') says which selection, and
129 DATA specifies the contents. TYPE must be a symbol. \(It can also
130 be a string, which stands for the symbol with that name, but this
131 is considered obsolete.) DATA may be a string, a symbol, an
132 integer (or a cons of two integers or list of two integers).
134 The selection may also be a cons of two markers pointing to the same buffer,
135 or an overlay. In these cases, the selection is considered to be the text
136 between the markers *at whatever time the selection is examined*.
137 Thus, editing done in the buffer after you specify the selection
138 can alter the effective value of the selection.
140 The data may also be a vector of valid non-vector selection values.
142 The return value is DATA.
144 Interactively, this command sets the primary selection. Without
145 prefix argument, it reads the selection in the minibuffer. With
146 prefix argument, it uses the text of the region as the selection value.
148 Note that on MS-Windows, primary and secondary selections set by Emacs
149 are not available to other programs."
150 (interactive (if (not current-prefix-arg
)
151 (list 'PRIMARY
(read-string "Set text for pasting: "))
152 (list 'PRIMARY
(buffer-substring (region-beginning) (region-end)))))
153 (if (stringp type
) (setq type
(intern type
)))
154 (or (x-valid-simple-selection-p data
)
157 (i (1- (length data
))))
159 (or (x-valid-simple-selection-p (aref data i
))
163 (signal 'error
(list "invalid selection" data
)))
164 (or type
(setq type
'PRIMARY
))
166 (x-own-selection-internal type data
)
167 (x-disown-selection-internal type
))
170 (defun x-valid-simple-selection-p (data)
175 (marker-buffer (car data
))
176 (buffer-name (marker-buffer (car data
)))
177 (eq (marker-buffer (car data
))
178 (marker-buffer (cdr data
))))
181 (overlay-buffer data
)
182 (buffer-name (overlay-buffer data
)))
186 ;; Functions to convert the selection into various other selection types.
187 ;; Every selection type that Emacs handles is implemented this way, except
188 ;; for TIMESTAMP, which is a special case.
190 (defun xselect--selection-bounds (value)
191 "Return bounds of X selection value VALUE.
192 The return value is a list (BEG END BUF) if VALUE is a cons of
193 two markers or an overlay. Otherwise, it is nil."
194 (cond ((bufferp value
)
195 (with-current-buffer value
197 (list (mark t
) (point) value
))))
199 (markerp (car value
))
200 (markerp (cdr value
)))
201 (when (and (marker-buffer (car value
))
202 (buffer-name (marker-buffer (car value
)))
203 (eq (marker-buffer (car value
))
204 (marker-buffer (cdr value
))))
205 (list (marker-position (car value
))
206 (marker-position (cdr value
))
207 (marker-buffer (car value
)))))
209 (when (overlay-buffer value
)
210 (list (overlay-start value
)
212 (overlay-buffer value
))))))
214 (defun xselect--int-to-cons (n)
215 (cons (ash n -
16) (logand n
65535)))
217 (defun xselect-convert-to-string (selection type value
)
219 ;; Get the actual string from VALUE.
220 (cond ((stringp value
)
222 ((setq value
(xselect--selection-bounds value
))
223 (with-current-buffer (nth 2 value
)
224 (setq str
(buffer-substring (nth 0 value
)
227 ;; If TYPE is nil, this is a local request, thus return STR as
228 ;; is. Otherwise, encode STR.
231 (setq coding
(or next-selection-coding-system selection-coding-system
))
233 (setq coding
(coding-system-base coding
)))
234 (let ((inhibit-read-only t
))
235 ;; Suppress producing escape sequences for compositions.
236 (remove-text-properties 0 (length str
) '(composition nil
) str
)
238 ;; TEXT is a polymorphic target. We must select the
239 ;; actual type from `UTF8_STRING', `COMPOUND_TEXT',
240 ;; `STRING', and `C_STRING'.
241 (if (not (multibyte-string-p str
))
242 (setq type
'C_STRING
)
243 (let (non-latin-1 non-unicode eight-bit
)
250 (setq eight-bit t
)))))
252 (setq type
(if non-unicode
'COMPOUND_TEXT
253 (if non-latin-1
'UTF8_STRING
254 (if eight-bit
'C_STRING
'STRING
)))))))
256 ((eq type
'UTF8_STRING
)
258 (not (eq (coding-system-type coding
) 'utf-8
)))
259 (setq coding
'utf-8
))
260 (setq str
(encode-coding-string str coding
)))
264 (not (eq (coding-system-type coding
) 'charset
)))
265 (setq coding
'iso-8859-1
))
266 (setq str
(encode-coding-string str coding
)))
268 ((eq type
'COMPOUND_TEXT
)
270 (not (eq (coding-system-type coding
) 'iso-2022
)))
271 (setq coding
'compound-text-with-extensions
))
272 (setq str
(encode-coding-string str coding
)))
275 (setq str
(string-make-unibyte str
)))
278 (error "Unknown selection type: %S" type
)))))
280 (setq next-selection-coding-system nil
)
283 (defun xselect-convert-to-length (selection type value
)
284 (let ((len (cond ((stringp value
)
286 ((setq value
(xselect--selection-bounds value
))
287 (abs (- (nth 0 value
) (nth 1 value
)))))))
289 (xselect--int-to-cons len
))))
291 (defun xselect-convert-to-targets (selection type value
)
292 ;; return a vector of atoms, but remove duplicates first.
293 (let* ((all (cons 'TIMESTAMP
(mapcar 'car selection-converter-alist
)))
296 (cond ((memq (car rest
) (cdr rest
))
297 (setcdr rest
(delq (car rest
) (cdr rest
))))
298 ((eq (car (cdr rest
)) '_EMACS_INTERNAL
) ; shh, it's a secret
299 (setcdr rest
(cdr (cdr rest
))))
301 (setq rest
(cdr rest
)))))
302 (apply 'vector all
)))
304 (defun xselect-convert-to-delete (selection type value
)
305 (x-disown-selection-internal selection
)
306 ;; A return value of nil means that we do not know how to do this conversion,
307 ;; and replies with an "error". A return value of NULL means that we have
308 ;; done the conversion (and any side-effects) but have no value to return.
311 (defun xselect-convert-to-filename (selection type value
)
312 (when (setq value
(xselect--selection-bounds value
))
313 (buffer-file-name (nth 2 value
))))
315 (defun xselect-convert-to-charpos (selection type value
)
316 (when (setq value
(xselect--selection-bounds value
))
317 (let ((beg (1- (nth 0 value
))) ; zero-based
318 (end (1- (nth 1 value
))))
319 (cons 'SPAN
(vector (xselect--int-to-cons (min beg end
))
320 (xselect--int-to-cons (max beg end
)))))))
322 (defun xselect-convert-to-lineno (selection type value
)
323 (when (setq value
(xselect--selection-bounds value
))
324 (with-current-buffer (nth 2 value
)
325 (let ((beg (line-number-at-pos (nth 0 value
)))
326 (end (line-number-at-pos (nth 1 value
))))
327 (cons 'SPAN
(vector (xselect--int-to-cons (min beg end
))
328 (xselect--int-to-cons (max beg end
))))))))
330 (defun xselect-convert-to-colno (selection type value
)
331 (when (setq value
(xselect--selection-bounds value
))
332 (with-current-buffer (nth 2 value
)
333 (let ((beg (progn (goto-char (nth 0 value
)) (current-column)))
334 (end (progn (goto-char (nth 1 value
)) (current-column))))
335 (cons 'SPAN
(vector (xselect--int-to-cons (min beg end
))
336 (xselect--int-to-cons (max beg end
))))))))
338 (defun xselect-convert-to-os (selection type size
)
339 (symbol-name system-type
))
341 (defun xselect-convert-to-host (selection type size
)
344 (defun xselect-convert-to-user (selection type size
)
347 (defun xselect-convert-to-class (selection type size
)
348 "Convert selection to class.
349 This function returns the string \"Emacs\"."
352 ;; We do not try to determine the name Emacs was invoked with,
353 ;; because it is not clean for a program's behavior to depend on that.
354 (defun xselect-convert-to-name (selection type size
)
355 "Convert selection to name.
356 This function returns the string \"emacs\"."
359 (defun xselect-convert-to-integer (selection type value
)
360 (and (integerp value
)
361 (xselect--int-to-cons value
)))
363 (defun xselect-convert-to-atom (selection type value
)
364 (and (symbolp value
) value
))
366 (defun xselect-convert-to-identity (selection type value
) ; used internally
369 (setq selection-converter-alist
370 '((TEXT . xselect-convert-to-string
)
371 (COMPOUND_TEXT . xselect-convert-to-string
)
372 (STRING . xselect-convert-to-string
)
373 (UTF8_STRING . xselect-convert-to-string
)
374 (TARGETS . xselect-convert-to-targets
)
375 (LENGTH . xselect-convert-to-length
)
376 (DELETE . xselect-convert-to-delete
)
377 (FILE_NAME . xselect-convert-to-filename
)
378 (CHARACTER_POSITION . xselect-convert-to-charpos
)
379 (LINE_NUMBER . xselect-convert-to-lineno
)
380 (COLUMN_NUMBER . xselect-convert-to-colno
)
381 (OWNER_OS . xselect-convert-to-os
)
382 (HOST_NAME . xselect-convert-to-host
)
383 (USER . xselect-convert-to-user
)
384 (CLASS . xselect-convert-to-class
)
385 (NAME . xselect-convert-to-name
)
386 (ATOM . xselect-convert-to-atom
)
387 (INTEGER . xselect-convert-to-integer
)
388 (_EMACS_INTERNAL . xselect-convert-to-identity
)))
392 ;;; select.el ends here