Commit | Line | Data |
---|---|---|
60370d40 | 1 | ;;; select.el --- lisp portion of standard selection support |
3109d63f | 2 | |
ab422c4d | 3 | ;; Copyright (C) 1993-1994, 2001-2013 Free Software Foundation, Inc. |
3646b86d | 4 | |
30764597 | 5 | ;; Maintainer: FSF |
3109d63f ER |
6 | ;; Keywords: internal |
7 | ||
3109d63f ER |
8 | ;; This file is part of GNU Emacs. |
9 | ||
eb3fa2cf | 10 | ;; GNU Emacs is free software: you can redistribute it and/or modify |
3109d63f | 11 | ;; it under the terms of the GNU General Public License as published by |
eb3fa2cf GM |
12 | ;; the Free Software Foundation, either version 3 of the License, or |
13 | ;; (at your option) any later version. | |
3109d63f ER |
14 | |
15 | ;; GNU Emacs is distributed in the hope that it will be useful, | |
16 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
17 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
18 | ;; GNU General Public License for more details. | |
19 | ||
20 | ;; You should have received a copy of the GNU General Public License | |
eb3fa2cf | 21 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
3109d63f | 22 | |
60370d40 PJ |
23 | ;;; Commentary: |
24 | ||
3646b86d GM |
25 | ;; Based partially on earlier release by Lucid. |
26 | ||
3109d63f ER |
27 | ;;; Code: |
28 | ||
b50690cc | 29 | (defcustom selection-coding-system nil |
f5f25615 | 30 | "Coding system for communicating with other programs. |
50259798 | 31 | |
f5f25615 GM |
32 | For MS-Windows and MS-DOS: |
33 | When sending or receiving text via selection and clipboard, the text | |
34 | is encoded or decoded by this coding system. The default value is | |
35 | the current system default encoding on 9x/Me, `utf-16le-dos' | |
36 | \(Unicode) on NT/W2K/XP, and `iso-latin-1-dos' on MS-DOS. | |
37 | ||
38 | For X Windows: | |
50259798 KH |
39 | When sending text via selection and clipboard, if the target |
40 | data-type matches with the type of this coding system, it is used | |
41 | for encoding the text. Otherwise (including the case that this | |
42 | variable is nil), a proper coding system is used as below: | |
43 | ||
44 | data-type coding system | |
45 | --------- ------------- | |
46 | UTF8_STRING utf-8 | |
47 | COMPOUND_TEXT compound-text-with-extensions | |
48 | STRING iso-latin-1 | |
49 | C_STRING no-conversion | |
50 | ||
51 | When receiving text, if this coding system is non-nil, it is used | |
52 | for decoding regardless of the data-type. If this is nil, a | |
53 | proper coding system is used according to the data-type as above. | |
54 | ||
55 | See also the documentation of the variable `x-select-request-type' how | |
56 | to control which data-type to request for receiving text. | |
57 | ||
b50690cc GM |
58 | The default value is nil." |
59 | :type 'coding-system | |
60 | :group 'mule | |
61 | ;; Default was compound-text-with-extensions in 22.x (pre-unicode). | |
62 | :version "23.1" | |
63 | :set (lambda (symbol value) | |
64 | (set-selection-coding-system value) | |
65 | (set symbol value))) | |
50259798 KH |
66 | |
67 | (defvar next-selection-coding-system nil | |
3646b86d | 68 | "Coding system for the next communication with other programs. |
50259798 | 69 | Usually, `selection-coding-system' is used for communicating with |
3646b86d GM |
70 | other programs (X Windows clients or MS Windows programs). But, if this |
71 | variable is set, it is used for the next communication only. | |
72 | After the communication, this variable is set to nil.") | |
50259798 | 73 | |
aa360da1 | 74 | (declare-function x-get-selection-internal "xselect.c" |
cfecdf09 | 75 | (selection-symbol target-type &optional time-stamp terminal)) |
aa360da1 | 76 | |
82f7efc3 GM |
77 | ;; Only declared obsolete in 23.3. |
78 | (define-obsolete-function-alias 'x-selection 'x-get-selection "at least 19.34") | |
79 | ||
3109d63f ER |
80 | (defun x-get-selection (&optional type data-type) |
81 | "Return the value of an X Windows selection. | |
47fb2723 | 82 | The argument TYPE (default `PRIMARY') says which selection, |
4029fd9a | 83 | and the argument DATA-TYPE (default `STRING') says |
76058c27 EZ |
84 | how to convert the data. |
85 | ||
15a24762 LT |
86 | TYPE may be any symbol \(but nil stands for `PRIMARY'). However, |
87 | only a few symbols are commonly used. They conventionally have | |
88 | all upper-case names. The most often used ones, in addition to | |
89 | `PRIMARY', are `SECONDARY' and `CLIPBOARD'. | |
90 | ||
76058c27 EZ |
91 | DATA-TYPE is usually `STRING', but can also be one of the symbols |
92 | in `selection-converter-alist', which see." | |
f147fd76 KH |
93 | (let ((data (x-get-selection-internal (or type 'PRIMARY) |
94 | (or data-type 'STRING))) | |
95 | coding) | |
96 | (when (and (stringp data) | |
97 | (setq data-type (get-text-property 0 'foreign-selection data))) | |
50259798 KH |
98 | (setq coding (or next-selection-coding-system |
99 | selection-coding-system | |
100 | (cond ((eq data-type 'UTF8_STRING) | |
101 | 'utf-8) | |
29128339 | 102 | ((eq data-type 'COMPOUND_TEXT) |
50259798 KH |
103 | 'compound-text-with-extensions) |
104 | ((eq data-type 'C_STRING) | |
105 | nil) | |
106 | ((eq data-type 'STRING) | |
107 | 'iso-8859-1) | |
108 | (t | |
22bcf204 | 109 | (error "Unknown selection data type: %S" type)))) |
50259798 KH |
110 | data (if coding (decode-coding-string data coding) |
111 | (string-to-multibyte data))) | |
112 | (setq next-selection-coding-system nil) | |
f147fd76 KH |
113 | (put-text-property 0 (length data) 'foreign-selection data-type data)) |
114 | data)) | |
3109d63f ER |
115 | |
116 | (defun x-get-clipboard () | |
117 | "Return text pasted to the clipboard." | |
118 | (x-get-selection-internal 'CLIPBOARD 'STRING)) | |
119 | ||
aa360da1 | 120 | (declare-function x-own-selection-internal "xselect.c" |
cfecdf09 | 121 | (selection-name selection-value &optional frame)) |
aa360da1 | 122 | (declare-function x-disown-selection-internal "xselect.c" |
cfecdf09 | 123 | (selection &optional time terminal)) |
aa360da1 | 124 | |
3109d63f | 125 | (defun x-set-selection (type data) |
d977a092 | 126 | "Make an X selection of type TYPE and value DATA. |
3077d1f6 EZ |
127 | The argument TYPE (nil means `PRIMARY') says which selection, and |
128 | DATA specifies the contents. TYPE must be a symbol. \(It can also | |
129 | be a string, which stands for the symbol with that name, but this | |
130 | is considered obsolete.) DATA may be a string, a symbol, an | |
131 | integer (or a cons of two integers or list of two integers). | |
132 | ||
133 | The selection may also be a cons of two markers pointing to the same buffer, | |
134 | or an overlay. In these cases, the selection is considered to be the text | |
135 | between the markers *at whatever time the selection is examined*. | |
136 | Thus, editing done in the buffer after you specify the selection | |
137 | can alter the effective value of the selection. | |
138 | ||
139 | The data may also be a vector of valid non-vector selection values. | |
5038b790 | 140 | |
15a24762 LT |
141 | The return value is DATA. |
142 | ||
143 | Interactively, this command sets the primary selection. Without | |
144 | prefix argument, it reads the selection in the minibuffer. With | |
3077d1f6 EZ |
145 | prefix argument, it uses the text of the region as the selection value. |
146 | ||
147 | Note that on MS-Windows, primary and secondary selections set by Emacs | |
148 | are not available to other programs." | |
3109d63f | 149 | (interactive (if (not current-prefix-arg) |
5038b790 | 150 | (list 'PRIMARY (read-string "Set text for pasting: ")) |
1b270b55 | 151 | (list 'PRIMARY (buffer-substring (region-beginning) (region-end))))) |
f470187f | 152 | (if (stringp type) (setq type (intern type))) |
3109d63f ER |
153 | (or (x-valid-simple-selection-p data) |
154 | (and (vectorp data) | |
155 | (let ((valid t) | |
156 | (i (1- (length data)))) | |
157 | (while (>= i 0) | |
158 | (or (x-valid-simple-selection-p (aref data i)) | |
159 | (setq valid nil)) | |
160 | (setq i (1- i))) | |
161 | valid)) | |
162 | (signal 'error (list "invalid selection" data))) | |
163 | (or type (setq type 'PRIMARY)) | |
164 | (if data | |
165 | (x-own-selection-internal type data) | |
166 | (x-disown-selection-internal type)) | |
167 | data) | |
168 | ||
169 | (defun x-valid-simple-selection-p (data) | |
f9be2e35 CY |
170 | (or (bufferp data) |
171 | (and (consp data) | |
3109d63f ER |
172 | (markerp (car data)) |
173 | (markerp (cdr data)) | |
174 | (marker-buffer (car data)) | |
3109d63f | 175 | (buffer-name (marker-buffer (car data))) |
f470187f CY |
176 | (eq (marker-buffer (car data)) |
177 | (marker-buffer (cdr data)))) | |
178 | (stringp data) | |
179 | (and (overlayp data) | |
180 | (overlay-buffer data) | |
181 | (buffer-name (overlay-buffer data))) | |
182 | (symbolp data) | |
183 | (integerp data))) | |
3109d63f | 184 | \f |
fdbe0096 SM |
185 | ;; Functions to convert the selection into various other selection types. |
186 | ;; Every selection type that Emacs handles is implemented this way, except | |
187 | ;; for TIMESTAMP, which is a special case. | |
3109d63f | 188 | |
f470187f CY |
189 | (defun xselect--selection-bounds (value) |
190 | "Return bounds of X selection value VALUE. | |
191 | The return value is a list (BEG END BUF) if VALUE is a cons of | |
192 | two markers or an overlay. Otherwise, it is nil." | |
f9be2e35 CY |
193 | (cond ((bufferp value) |
194 | (with-current-buffer value | |
195 | (when (mark t) | |
196 | (list (mark t) (point) value)))) | |
197 | ((and (consp value) | |
f470187f CY |
198 | (markerp (car value)) |
199 | (markerp (cdr value))) | |
200 | (when (and (marker-buffer (car value)) | |
201 | (buffer-name (marker-buffer (car value))) | |
202 | (eq (marker-buffer (car value)) | |
203 | (marker-buffer (cdr value)))) | |
204 | (list (marker-position (car value)) | |
205 | (marker-position (cdr value)) | |
206 | (marker-buffer (car value))))) | |
207 | ((overlayp value) | |
208 | (when (overlay-buffer value) | |
209 | (list (overlay-start value) | |
210 | (overlay-end value) | |
211 | (overlay-buffer value)))))) | |
212 | ||
213 | (defun xselect--int-to-cons (n) | |
214 | (cons (ash n -16) (logand n 65535))) | |
215 | ||
acb71f1d CY |
216 | (defun xselect--encode-string (type str &optional can-modify) |
217 | (when str | |
218 | ;; If TYPE is nil, this is a local request; return STR as-is. | |
219 | (if (null type) | |
220 | str | |
221 | ;; Otherwise, encode STR. | |
222 | (let ((coding (or next-selection-coding-system | |
223 | selection-coding-system))) | |
eb1416b9 | 224 | (if coding |
50259798 | 225 | (setq coding (coding-system-base coding))) |
deae888e | 226 | (let ((inhibit-read-only t)) |
b36ee2a6 | 227 | ;; Suppress producing escape sequences for compositions. |
acb71f1d CY |
228 | ;; But avoid modifying the string if it's a buffer name etc. |
229 | (unless can-modify (setq str (substring str 0))) | |
deae888e | 230 | (remove-text-properties 0 (length str) '(composition nil) str) |
eceeb5fc CY |
231 | ;; For X selections, TEXT is a polymorphic target; choose |
232 | ;; the actual type from `UTF8_STRING', `COMPOUND_TEXT', | |
233 | ;; `STRING', and `C_STRING'. On Nextstep, always use UTF-8 | |
234 | ;; (see ns_string_to_pasteboard_internal in nsselect.m). | |
235 | (when (eq type 'TEXT) | |
236 | (cond | |
237 | ((featurep 'ns) | |
238 | (setq type 'UTF8_STRING)) | |
239 | ((not (multibyte-string-p str)) | |
240 | (setq type 'C_STRING)) | |
241 | (t | |
242 | (let (non-latin-1 non-unicode eight-bit) | |
243 | (mapc #'(lambda (x) | |
244 | (if (>= x #x100) | |
245 | (if (< x #x110000) | |
246 | (setq non-latin-1 t) | |
247 | (if (< x #x3FFF80) | |
248 | (setq non-unicode t) | |
249 | (setq eight-bit t))))) | |
250 | str) | |
4ef4a10d KH |
251 | (setq type (if (or non-unicode |
252 | (and | |
253 | non-latin-1 | |
254 | ;; If a coding is specified for | |
255 | ;; selection, and that is | |
256 | ;; compatible with COMPOUND_TEXT, | |
257 | ;; use it. | |
258 | coding | |
259 | (eq (coding-system-get coding :mime-charset) | |
260 | 'x-ctext))) | |
261 | 'COMPOUND_TEXT | |
eceeb5fc CY |
262 | (if non-latin-1 'UTF8_STRING |
263 | (if eight-bit 'C_STRING | |
264 | 'STRING)))))))) | |
569ac23f CY |
265 | (cond |
266 | ((eq type 'UTF8_STRING) | |
267 | (if (or (not coding) | |
268 | (not (eq (coding-system-type coding) 'utf-8))) | |
269 | (setq coding 'utf-8)) | |
270 | (setq str (encode-coding-string str coding))) | |
271 | ||
272 | ((eq type 'STRING) | |
273 | (if (or (not coding) | |
274 | (not (eq (coding-system-type coding) 'charset))) | |
275 | (setq coding 'iso-8859-1)) | |
276 | (setq str (encode-coding-string str coding))) | |
277 | ||
278 | ((eq type 'COMPOUND_TEXT) | |
279 | (if (or (not coding) | |
280 | (not (eq (coding-system-type coding) 'iso-2022))) | |
281 | (setq coding 'compound-text-with-extensions)) | |
282 | (setq str (encode-coding-string str coding))) | |
283 | ||
284 | ((eq type 'C_STRING) | |
285 | (setq str (string-make-unibyte str))) | |
286 | ||
287 | (t | |
f470187f | 288 | (error "Unknown selection type: %S" type))))) |
eb1416b9 KH |
289 | |
290 | (setq next-selection-coding-system nil) | |
291 | (cons type str)))) | |
292 | ||
acb71f1d CY |
293 | (defun xselect-convert-to-string (_selection type value) |
294 | (let ((str (cond ((stringp value) value) | |
295 | ((setq value (xselect--selection-bounds value)) | |
296 | (with-current-buffer (nth 2 value) | |
297 | (buffer-substring (nth 0 value) | |
298 | (nth 1 value))))))) | |
299 | (xselect--encode-string type str t))) | |
300 | ||
06b60517 | 301 | (defun xselect-convert-to-length (_selection _type value) |
f470187f CY |
302 | (let ((len (cond ((stringp value) |
303 | (length value)) | |
304 | ((setq value (xselect--selection-bounds value)) | |
305 | (abs (- (nth 0 value) (nth 1 value))))))) | |
306 | (if len | |
307 | (xselect--int-to-cons len)))) | |
3109d63f | 308 | |
06b60517 | 309 | (defun xselect-convert-to-targets (_selection _type _value) |
3109d63f | 310 | ;; return a vector of atoms, but remove duplicates first. |
a9f737ee CY |
311 | (let* ((all (cons 'TIMESTAMP |
312 | (cons 'MULTIPLE | |
313 | (mapcar 'car selection-converter-alist)))) | |
3109d63f ER |
314 | (rest all)) |
315 | (while rest | |
316 | (cond ((memq (car rest) (cdr rest)) | |
317 | (setcdr rest (delq (car rest) (cdr rest)))) | |
318 | ((eq (car (cdr rest)) '_EMACS_INTERNAL) ; shh, it's a secret | |
319 | (setcdr rest (cdr (cdr rest)))) | |
320 | (t | |
321 | (setq rest (cdr rest))))) | |
322 | (apply 'vector all))) | |
323 | ||
06b60517 | 324 | (defun xselect-convert-to-delete (selection _type _value) |
3109d63f ER |
325 | (x-disown-selection-internal selection) |
326 | ;; A return value of nil means that we do not know how to do this conversion, | |
327 | ;; and replies with an "error". A return value of NULL means that we have | |
328 | ;; done the conversion (and any side-effects) but have no value to return. | |
329 | 'NULL) | |
330 | ||
06b60517 | 331 | (defun xselect-convert-to-filename (_selection _type value) |
f470187f | 332 | (when (setq value (xselect--selection-bounds value)) |
acb71f1d | 333 | (xselect--encode-string 'TEXT (buffer-file-name (nth 2 value))))) |
3109d63f | 334 | |
06b60517 | 335 | (defun xselect-convert-to-charpos (_selection _type value) |
f470187f CY |
336 | (when (setq value (xselect--selection-bounds value)) |
337 | (let ((beg (1- (nth 0 value))) ; zero-based | |
338 | (end (1- (nth 1 value)))) | |
339 | (cons 'SPAN (vector (xselect--int-to-cons (min beg end)) | |
340 | (xselect--int-to-cons (max beg end))))))) | |
3109d63f | 341 | |
06b60517 | 342 | (defun xselect-convert-to-lineno (_selection _type value) |
f470187f CY |
343 | (when (setq value (xselect--selection-bounds value)) |
344 | (with-current-buffer (nth 2 value) | |
345 | (let ((beg (line-number-at-pos (nth 0 value))) | |
346 | (end (line-number-at-pos (nth 1 value)))) | |
347 | (cons 'SPAN (vector (xselect--int-to-cons (min beg end)) | |
348 | (xselect--int-to-cons (max beg end)))))))) | |
3109d63f | 349 | |
06b60517 | 350 | (defun xselect-convert-to-colno (_selection _type value) |
f470187f CY |
351 | (when (setq value (xselect--selection-bounds value)) |
352 | (with-current-buffer (nth 2 value) | |
353 | (let ((beg (progn (goto-char (nth 0 value)) (current-column))) | |
354 | (end (progn (goto-char (nth 1 value)) (current-column)))) | |
355 | (cons 'SPAN (vector (xselect--int-to-cons (min beg end)) | |
356 | (xselect--int-to-cons (max beg end)))))))) | |
3109d63f | 357 | |
06b60517 | 358 | (defun xselect-convert-to-os (_selection _type _size) |
acb71f1d | 359 | (xselect--encode-string 'TEXT (symbol-name system-type))) |
3109d63f | 360 | |
06b60517 | 361 | (defun xselect-convert-to-host (_selection _type _size) |
acb71f1d | 362 | (xselect--encode-string 'TEXT (system-name))) |
3109d63f | 363 | |
06b60517 | 364 | (defun xselect-convert-to-user (_selection _type _size) |
acb71f1d | 365 | (xselect--encode-string 'TEXT (user-full-name))) |
3109d63f | 366 | |
06b60517 | 367 | (defun xselect-convert-to-class (_selection _type _size) |
47a31c6b PJ |
368 | "Convert selection to class. |
369 | This function returns the string \"Emacs\"." | |
7e773fb4 | 370 | "Emacs") |
3109d63f ER |
371 | |
372 | ;; We do not try to determine the name Emacs was invoked with, | |
373 | ;; because it is not clean for a program's behavior to depend on that. | |
06b60517 | 374 | (defun xselect-convert-to-name (_selection _type _size) |
47a31c6b PJ |
375 | "Convert selection to name. |
376 | This function returns the string \"emacs\"." | |
3109d63f ER |
377 | "emacs") |
378 | ||
06b60517 | 379 | (defun xselect-convert-to-integer (_selection _type value) |
3109d63f | 380 | (and (integerp value) |
f470187f | 381 | (xselect--int-to-cons value))) |
3109d63f | 382 | |
06b60517 | 383 | (defun xselect-convert-to-atom (_selection _type value) |
3109d63f ER |
384 | (and (symbolp value) value)) |
385 | ||
06b60517 | 386 | (defun xselect-convert-to-identity (_selection _type value) ; used internally |
3109d63f ER |
387 | (vector value)) |
388 | ||
a9f737ee CY |
389 | ;; Null target that tells clipboard managers we support SAVE_TARGETS |
390 | ;; (see freedesktop.org Clipboard Manager spec). | |
391 | (defun xselect-convert-to-save-targets (selection _type _value) | |
392 | (when (eq selection 'CLIPBOARD) | |
393 | 'NULL)) | |
394 | ||
3109d63f ER |
395 | (setq selection-converter-alist |
396 | '((TEXT . xselect-convert-to-string) | |
79501c8d | 397 | (COMPOUND_TEXT . xselect-convert-to-string) |
3109d63f | 398 | (STRING . xselect-convert-to-string) |
eb1416b9 | 399 | (UTF8_STRING . xselect-convert-to-string) |
3109d63f ER |
400 | (TARGETS . xselect-convert-to-targets) |
401 | (LENGTH . xselect-convert-to-length) | |
402 | (DELETE . xselect-convert-to-delete) | |
403 | (FILE_NAME . xselect-convert-to-filename) | |
404 | (CHARACTER_POSITION . xselect-convert-to-charpos) | |
405 | (LINE_NUMBER . xselect-convert-to-lineno) | |
406 | (COLUMN_NUMBER . xselect-convert-to-colno) | |
407 | (OWNER_OS . xselect-convert-to-os) | |
408 | (HOST_NAME . xselect-convert-to-host) | |
409 | (USER . xselect-convert-to-user) | |
410 | (CLASS . xselect-convert-to-class) | |
411 | (NAME . xselect-convert-to-name) | |
412 | (ATOM . xselect-convert-to-atom) | |
413 | (INTEGER . xselect-convert-to-integer) | |
a9f737ee | 414 | (SAVE_TARGETS . xselect-convert-to-save-targets) |
f470187f | 415 | (_EMACS_INTERNAL . xselect-convert-to-identity))) |
3109d63f ER |
416 | |
417 | (provide 'select) | |
418 | ||
60370d40 | 419 | ;;; select.el ends here |