Switch to recommended form of GPLv3 permissions notice.
[bpt/emacs.git] / lisp / select.el
1 ;;; select.el --- lisp portion of standard selection support
2
3 ;; Maintainer: FSF
4 ;; Keywords: internal
5
6 ;; Copyright (C) 1993, 1994, 2001, 2002, 2003, 2004,
7 ;; 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
8 ;; Based partially on earlier release by Lucid.
9
10 ;; This file is part of GNU Emacs.
11
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
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
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24
25 ;;; Commentary:
26
27 ;;; Code:
28
29 (defcustom selection-coding-system nil
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
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)))
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
67 ;; This is for temporary compatibility with pre-release Emacs 19.
68 (defalias 'x-selection 'x-get-selection)
69 (defun x-get-selection (&optional type data-type)
70 "Return the value of an X Windows selection.
71 The argument TYPE (default `PRIMARY') says which selection,
72 and the argument DATA-TYPE (default `STRING') says
73 how to convert the data.
74
75 TYPE may be any symbol \(but nil stands for `PRIMARY'). However,
76 only a few symbols are commonly used. They conventionally have
77 all upper-case names. The most often used ones, in addition to
78 `PRIMARY', are `SECONDARY' and `CLIPBOARD'.
79
80 DATA-TYPE is usually `STRING', but can also be one of the symbols
81 in `selection-converter-alist', which see."
82 (let ((data (x-get-selection-internal (or type 'PRIMARY)
83 (or data-type 'STRING)))
84 coding)
85 (when (and (stringp data)
86 (setq data-type (get-text-property 0 'foreign-selection data)))
87 (setq coding (or next-selection-coding-system
88 selection-coding-system
89 (cond ((eq data-type 'UTF8_STRING)
90 'utf-8)
91 ((eq data-type 'COMPOUND-TEXT)
92 'compound-text-with-extensions)
93 ((eq data-type 'C_STRING)
94 nil)
95 ((eq data-type 'STRING)
96 'iso-8859-1)
97 (t
98 (error "Unknow selection data type: %S" type))))
99 data (if coding (decode-coding-string data coding)
100 (string-to-multibyte data)))
101 (setq next-selection-coding-system nil)
102 (put-text-property 0 (length data) 'foreign-selection data-type data))
103 data))
104
105 (defun x-get-clipboard ()
106 "Return text pasted to the clipboard."
107 (x-get-selection-internal 'CLIPBOARD 'STRING))
108
109 (defun x-set-selection (type data)
110 "Make an X Windows selection of type TYPE and value DATA.
111 The argument TYPE (nil means `PRIMARY') says which selection, and
112 DATA specifies the contents. TYPE must be a symbol. \(It can also
113 be a string, which stands for the symbol with that name, but this
114 is considered obsolete.) DATA may be a string, a symbol, an
115 integer (or a cons of two integers or list of two integers).
116
117 The selection may also be a cons of two markers pointing to the same buffer,
118 or an overlay. In these cases, the selection is considered to be the text
119 between the markers *at whatever time the selection is examined*.
120 Thus, editing done in the buffer after you specify the selection
121 can alter the effective value of the selection.
122
123 The data may also be a vector of valid non-vector selection values.
124
125 The return value is DATA.
126
127 Interactively, this command sets the primary selection. Without
128 prefix argument, it reads the selection in the minibuffer. With
129 prefix argument, it uses the text of the region as the selection value ."
130 (interactive (if (not current-prefix-arg)
131 (list 'PRIMARY (read-string "Set text for pasting: "))
132 (list 'PRIMARY (buffer-substring (region-beginning) (region-end)))))
133 ;; This is for temporary compatibility with pre-release Emacs 19.
134 (if (stringp type)
135 (setq type (intern type)))
136 (or (x-valid-simple-selection-p data)
137 (and (vectorp data)
138 (let ((valid t)
139 (i (1- (length data))))
140 (while (>= i 0)
141 (or (x-valid-simple-selection-p (aref data i))
142 (setq valid nil))
143 (setq i (1- i)))
144 valid))
145 (signal 'error (list "invalid selection" data)))
146 (or type (setq type 'PRIMARY))
147 (if data
148 (x-own-selection-internal type data)
149 (x-disown-selection-internal type))
150 data)
151
152 (defun x-valid-simple-selection-p (data)
153 (or (stringp data)
154 (symbolp data)
155 (integerp data)
156 (and (consp data)
157 (integerp (car data))
158 (or (integerp (cdr data))
159 (and (consp (cdr data))
160 (integerp (car (cdr data))))))
161 (overlayp data)
162 (and (consp data)
163 (markerp (car data))
164 (markerp (cdr data))
165 (marker-buffer (car data))
166 (marker-buffer (cdr data))
167 (eq (marker-buffer (car data))
168 (marker-buffer (cdr data)))
169 (buffer-name (marker-buffer (car data)))
170 (buffer-name (marker-buffer (cdr data))))))
171 \f
172 ;;; Cut Buffer support
173
174 (declare-function x-get-cut-buffer-internal "xselect.c")
175
176 (defun x-get-cut-buffer (&optional which-one)
177 "Returns the value of one of the 8 X server cut-buffers.
178 Optional arg WHICH-ONE should be a number from 0 to 7, defaulting to 0.
179 Cut buffers are considered obsolete; you should use selections instead."
180 (x-get-cut-buffer-internal
181 (if which-one
182 (aref [CUT_BUFFER0 CUT_BUFFER1 CUT_BUFFER2 CUT_BUFFER3
183 CUT_BUFFER4 CUT_BUFFER5 CUT_BUFFER6 CUT_BUFFER7]
184 which-one)
185 'CUT_BUFFER0)))
186
187 (declare-function x-rotate-cut-buffers-internal "xselect.c")
188 (declare-function x-store-cut-buffer-internal "xselect.c")
189
190 (defun x-set-cut-buffer (string &optional push)
191 "Store STRING into the X server's primary cut buffer.
192 If PUSH is non-nil, also rotate the cut buffers:
193 this means the previous value of the primary cut buffer moves to the second
194 cut buffer, and the second to the third, and so on (there are 8 buffers.)
195 Cut buffers are considered obsolete; you should use selections instead."
196 (or (stringp string) (signal 'wrong-type-argument (list 'string string)))
197 (if push
198 (x-rotate-cut-buffers-internal 1))
199 (x-store-cut-buffer-internal 'CUT_BUFFER0 string))
200
201 \f
202 ;;; Functions to convert the selection into various other selection types.
203 ;;; Every selection type that Emacs handles is implemented this way, except
204 ;;; for TIMESTAMP, which is a special case.
205
206 (defun xselect-convert-to-string (selection type value)
207 (let (str coding)
208 ;; Get the actual string from VALUE.
209 (cond ((stringp value)
210 (setq str value))
211
212 ((overlayp value)
213 (save-excursion
214 (or (buffer-name (overlay-buffer value))
215 (error "selection is in a killed buffer"))
216 (set-buffer (overlay-buffer value))
217 (setq str (buffer-substring (overlay-start value)
218 (overlay-end value)))))
219 ((and (consp value)
220 (markerp (car value))
221 (markerp (cdr value)))
222 (or (eq (marker-buffer (car value)) (marker-buffer (cdr value)))
223 (signal 'error
224 (list "markers must be in the same buffer"
225 (car value) (cdr value))))
226 (save-excursion
227 (set-buffer (or (marker-buffer (car value))
228 (error "selection is in a killed buffer")))
229 (setq str (buffer-substring (car value) (cdr value))))))
230
231 (when str
232 ;; If TYPE is nil, this is a local request, thus return STR as
233 ;; is. Otherwise, encode STR.
234 (if (not type)
235 str
236 (setq coding (or next-selection-coding-system selection-coding-system))
237 (if coding
238 (setq coding (coding-system-base coding)))
239 (let ((inhibit-read-only t))
240 ;; Suppress producing escape sequences for compositions.
241 (remove-text-properties 0 (length str) '(composition nil) str)
242 (if (eq type 'TEXT)
243 ;; TEXT is a polymorphic target. We must select the
244 ;; actual type from `UTF8_STRING', `COMPOUND_TEXT',
245 ;; `STRING', and `C_STRING'.
246 (if (not (multibyte-string-p str))
247 (setq type 'C_STRING)
248 (let (non-latin-1 non-unicode eight-bit)
249 (mapc #'(lambda (x)
250 (if (>= x #x100)
251 (if (< x #x110000)
252 (setq non-latin-1 t)
253 (if (< x #x3FFF80)
254 (setq non-unicode t)
255 (setq eight-bit t)))))
256 str)
257 (setq type (if non-unicode 'COMPOUND_TEXT
258 (if non-latin-1 'UTF8_STRING
259 (if eight-bit 'C_STRING 'STRING)))))))
260 (cond
261 ((eq type 'UTF8_STRING)
262 (if (or (not coding)
263 (not (eq (coding-system-type coding) 'utf-8)))
264 (setq coding 'utf-8))
265 (setq str (encode-coding-string str coding)))
266
267 ((eq type 'STRING)
268 (if (or (not coding)
269 (not (eq (coding-system-type coding) 'charset)))
270 (setq coding 'iso-8859-1))
271 (setq str (encode-coding-string str coding)))
272
273 ((eq type 'COMPOUND_TEXT)
274 (if (or (not coding)
275 (not (eq (coding-system-type coding) 'iso-2022)))
276 (setq coding 'compound-text-with-extensions))
277 (setq str (encode-coding-string str coding)))
278
279 ((eq type 'C_STRING)
280 (setq str (string-make-unibyte str)))
281
282 (t
283 (error "Unknow selection type: %S" type))
284 )))
285
286 (setq next-selection-coding-system nil)
287 (cons type str))))
288
289
290 (defun xselect-convert-to-length (selection type value)
291 (let ((value
292 (cond ((stringp value)
293 (length value))
294 ((overlayp value)
295 (abs (- (overlay-end value) (overlay-start value))))
296 ((and (consp value)
297 (markerp (car value))
298 (markerp (cdr value)))
299 (or (eq (marker-buffer (car value))
300 (marker-buffer (cdr value)))
301 (signal 'error
302 (list "markers must be in the same buffer"
303 (car value) (cdr value))))
304 (abs (- (car value) (cdr value)))))))
305 (if value ; force it to be in 32-bit format.
306 (cons (ash value -16) (logand value 65535))
307 nil)))
308
309 (defun xselect-convert-to-targets (selection type value)
310 ;; return a vector of atoms, but remove duplicates first.
311 (let* ((all (cons 'TIMESTAMP (mapcar 'car selection-converter-alist)))
312 (rest all))
313 (while rest
314 (cond ((memq (car rest) (cdr rest))
315 (setcdr rest (delq (car rest) (cdr rest))))
316 ((eq (car (cdr rest)) '_EMACS_INTERNAL) ; shh, it's a secret
317 (setcdr rest (cdr (cdr rest))))
318 (t
319 (setq rest (cdr rest)))))
320 (apply 'vector all)))
321
322 (defun xselect-convert-to-delete (selection type value)
323 (x-disown-selection-internal selection)
324 ;; A return value of nil means that we do not know how to do this conversion,
325 ;; and replies with an "error". A return value of NULL means that we have
326 ;; done the conversion (and any side-effects) but have no value to return.
327 'NULL)
328
329 (defun xselect-convert-to-filename (selection type value)
330 (cond ((overlayp value)
331 (buffer-file-name (or (overlay-buffer value)
332 (error "selection is in a killed buffer"))))
333 ((and (consp value)
334 (markerp (car value))
335 (markerp (cdr value)))
336 (buffer-file-name (or (marker-buffer (car value))
337 (error "selection is in a killed buffer"))))
338 (t nil)))
339
340 (defun xselect-convert-to-charpos (selection type value)
341 (let (a b tmp)
342 (cond ((cond ((overlayp value)
343 (setq a (overlay-start value)
344 b (overlay-end value)))
345 ((and (consp value)
346 (markerp (car value))
347 (markerp (cdr value)))
348 (setq a (car value)
349 b (cdr value))))
350 (setq a (1- a) b (1- b)) ; zero-based
351 (if (< b a) (setq tmp a a b b tmp))
352 (cons 'SPAN
353 (vector (cons (ash a -16) (logand a 65535))
354 (cons (ash b -16) (logand b 65535))))))))
355
356 (defun xselect-convert-to-lineno (selection type value)
357 (let (a b buf tmp)
358 (cond ((cond ((and (consp value)
359 (markerp (car value))
360 (markerp (cdr value)))
361 (setq a (marker-position (car value))
362 b (marker-position (cdr value))
363 buf (marker-buffer (car value))))
364 ((overlayp value)
365 (setq buf (overlay-buffer value)
366 a (overlay-start value)
367 b (overlay-end value)))
368 )
369 (save-excursion
370 (set-buffer buf)
371 (setq a (count-lines 1 a)
372 b (count-lines 1 b)))
373 (if (< b a) (setq tmp a a b b tmp))
374 (cons 'SPAN
375 (vector (cons (ash a -16) (logand a 65535))
376 (cons (ash b -16) (logand b 65535))))))))
377
378 (defun xselect-convert-to-colno (selection type value)
379 (let (a b buf tmp)
380 (cond ((cond ((and (consp value)
381 (markerp (car value))
382 (markerp (cdr value)))
383 (setq a (car value)
384 b (cdr value)
385 buf (marker-buffer a)))
386 ((overlayp value)
387 (setq buf (overlay-buffer value)
388 a (overlay-start value)
389 b (overlay-end value)))
390 )
391 (save-excursion
392 (set-buffer buf)
393 (goto-char a)
394 (setq a (current-column))
395 (goto-char b)
396 (setq b (current-column)))
397 (if (< b a) (setq tmp a a b b tmp))
398 (cons 'SPAN
399 (vector (cons (ash a -16) (logand a 65535))
400 (cons (ash b -16) (logand b 65535))))))))
401
402 (defun xselect-convert-to-os (selection type size)
403 (symbol-name system-type))
404
405 (defun xselect-convert-to-host (selection type size)
406 (system-name))
407
408 (defun xselect-convert-to-user (selection type size)
409 (user-full-name))
410
411 (defun xselect-convert-to-class (selection type size)
412 "Convert selection to class.
413 This function returns the string \"Emacs\"."
414 "Emacs")
415
416 ;; We do not try to determine the name Emacs was invoked with,
417 ;; because it is not clean for a program's behavior to depend on that.
418 (defun xselect-convert-to-name (selection type size)
419 "Convert selection to name.
420 This function returns the string \"emacs\"."
421 "emacs")
422
423 (defun xselect-convert-to-integer (selection type value)
424 (and (integerp value)
425 (cons (ash value -16) (logand value 65535))))
426
427 (defun xselect-convert-to-atom (selection type value)
428 (and (symbolp value) value))
429
430 (defun xselect-convert-to-identity (selection type value) ; used internally
431 (vector value))
432
433 (setq selection-converter-alist
434 '((TEXT . xselect-convert-to-string)
435 (COMPOUND_TEXT . xselect-convert-to-string)
436 (STRING . xselect-convert-to-string)
437 (UTF8_STRING . xselect-convert-to-string)
438 (TARGETS . xselect-convert-to-targets)
439 (LENGTH . xselect-convert-to-length)
440 (DELETE . xselect-convert-to-delete)
441 (FILE_NAME . xselect-convert-to-filename)
442 (CHARACTER_POSITION . xselect-convert-to-charpos)
443 (LINE_NUMBER . xselect-convert-to-lineno)
444 (COLUMN_NUMBER . xselect-convert-to-colno)
445 (OWNER_OS . xselect-convert-to-os)
446 (HOST_NAME . xselect-convert-to-host)
447 (USER . xselect-convert-to-user)
448 (CLASS . xselect-convert-to-class)
449 (NAME . xselect-convert-to-name)
450 (ATOM . xselect-convert-to-atom)
451 (INTEGER . xselect-convert-to-integer)
452 (_EMACS_INTERNAL . xselect-convert-to-identity)
453 ))
454
455 (provide 'select)
456
457 ;; arch-tag: bb634f97-8a3b-4b0a-b940-f6e09982328c
458 ;;; select.el ends here