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