* doc/emacs/cal-xtra.texi: Copyedits.
[emacs.git] / lisp / select.el
blob0260eba4682a901f96c9a2ffa2b32adc43844d51
1 ;;; select.el --- lisp portion of standard selection support
3 ;; Copyright (C) 1993-1994, 2001-2012 Free Software Foundation, Inc.
5 ;; Maintainer: FSF
6 ;; Keywords: internal
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
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.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23 ;;; Commentary:
25 ;; Based partially on earlier release by Lucid.
27 ;;; Code:
29 (defcustom selection-coding-system nil
30 "Coding system for communicating with other programs.
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.
38 For X Windows:
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:
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
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.
55 See also the documentation of the variable `x-select-request-type' how
56 to control which data-type to request for receiving text.
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)))
67 (defvar next-selection-coding-system nil
68 "Coding system for the next communication with other programs.
69 Usually, `selection-coding-system' is used for communicating with
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.")
74 (declare-function x-get-selection-internal "xselect.c"
75 (selection-symbol target-type &optional time-stamp terminal))
77 ;; Only declared obsolete in 23.3.
78 (define-obsolete-function-alias 'x-selection 'x-get-selection "at least 19.34")
80 (defun x-get-selection (&optional type data-type)
81 "Return the value of an X Windows selection.
82 The argument TYPE (default `PRIMARY') says which selection,
83 and the argument DATA-TYPE (default `STRING') says
84 how to convert the data.
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'.
91 DATA-TYPE is usually `STRING', but can also be one of the symbols
92 in `selection-converter-alist', which see."
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)))
98 (setq coding (or next-selection-coding-system
99 selection-coding-system
100 (cond ((eq data-type 'UTF8_STRING)
101 'utf-8)
102 ((eq data-type 'COMPOUND_TEXT)
103 'compound-text-with-extensions)
104 ((eq data-type 'C_STRING)
105 nil)
106 ((eq data-type 'STRING)
107 'iso-8859-1)
109 (error "Unknown selection data type: %S" type))))
110 data (if coding (decode-coding-string data coding)
111 (string-to-multibyte data)))
112 (setq next-selection-coding-system nil)
113 (put-text-property 0 (length data) 'foreign-selection data-type data))
114 data))
116 (defun x-get-clipboard ()
117 "Return text pasted to the clipboard."
118 (x-get-selection-internal 'CLIPBOARD 'STRING))
120 (declare-function x-own-selection-internal "xselect.c"
121 (selection-name selection-value &optional frame))
122 (declare-function x-disown-selection-internal "xselect.c"
123 (selection &optional time terminal))
125 (defun x-set-selection (type data)
126 "Make an X selection of type TYPE and value DATA.
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).
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.
139 The data may also be a vector of valid non-vector selection values.
141 The return value is DATA.
143 Interactively, this command sets the primary selection. Without
144 prefix argument, it reads the selection in the minibuffer. With
145 prefix argument, it uses the text of the region as the selection value.
147 Note that on MS-Windows, primary and secondary selections set by Emacs
148 are not available to other programs."
149 (interactive (if (not current-prefix-arg)
150 (list 'PRIMARY (read-string "Set text for pasting: "))
151 (list 'PRIMARY (buffer-substring (region-beginning) (region-end)))))
152 (if (stringp type) (setq type (intern type)))
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)
169 (defun x-valid-simple-selection-p (data)
170 (or (bufferp data)
171 (and (consp data)
172 (markerp (car data))
173 (markerp (cdr data))
174 (marker-buffer (car data))
175 (buffer-name (marker-buffer (car data)))
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)))
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.
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."
193 (cond ((bufferp value)
194 (with-current-buffer value
195 (when (mark t)
196 (list (mark t) (point) value))))
197 ((and (consp value)
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))))))
213 (defun xselect--int-to-cons (n)
214 (cons (ash n -16) (logand n 65535)))
216 (defun xselect-convert-to-string (_selection type value)
217 (let (str coding)
218 ;; Get the actual string from VALUE.
219 (cond ((stringp value)
220 (setq str value))
221 ((setq value (xselect--selection-bounds value))
222 (with-current-buffer (nth 2 value)
223 (setq str (buffer-substring (nth 0 value)
224 (nth 1 value))))))
225 (when str
226 ;; If TYPE is nil, this is a local request, thus return STR as
227 ;; is. Otherwise, encode STR.
228 (if (not type)
230 (setq coding (or next-selection-coding-system selection-coding-system))
231 (if coding
232 (setq coding (coding-system-base coding)))
233 (let ((inhibit-read-only t))
234 ;; Suppress producing escape sequences for compositions.
235 (remove-text-properties 0 (length str) '(composition nil) str)
236 (if (eq type 'TEXT)
237 ;; TEXT is a polymorphic target. We must select the
238 ;; actual type from `UTF8_STRING', `COMPOUND_TEXT',
239 ;; `STRING', and `C_STRING'.
240 (if (not (multibyte-string-p str))
241 (setq type 'C_STRING)
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)
251 (setq type (if non-unicode 'COMPOUND_TEXT
252 (if non-latin-1 'UTF8_STRING
253 (if eight-bit 'C_STRING 'STRING)))))))
254 (cond
255 ((eq type 'UTF8_STRING)
256 (if (or (not coding)
257 (not (eq (coding-system-type coding) 'utf-8)))
258 (setq coding 'utf-8))
259 (setq str (encode-coding-string str coding)))
261 ((eq type 'STRING)
262 (if (or (not coding)
263 (not (eq (coding-system-type coding) 'charset)))
264 (setq coding 'iso-8859-1))
265 (setq str (encode-coding-string str coding)))
267 ((eq type 'COMPOUND_TEXT)
268 (if (or (not coding)
269 (not (eq (coding-system-type coding) 'iso-2022)))
270 (setq coding 'compound-text-with-extensions))
271 (setq str (encode-coding-string str coding)))
273 ((eq type 'C_STRING)
274 (setq str (string-make-unibyte str)))
277 (error "Unknown selection type: %S" type)))))
279 (setq next-selection-coding-system nil)
280 (cons type str))))
282 (defun xselect-convert-to-length (_selection _type value)
283 (let ((len (cond ((stringp value)
284 (length value))
285 ((setq value (xselect--selection-bounds value))
286 (abs (- (nth 0 value) (nth 1 value)))))))
287 (if len
288 (xselect--int-to-cons len))))
290 (defun xselect-convert-to-targets (_selection _type _value)
291 ;; return a vector of atoms, but remove duplicates first.
292 (let* ((all (cons 'TIMESTAMP
293 (cons 'MULTIPLE
294 (mapcar 'car selection-converter-alist))))
295 (rest all))
296 (while rest
297 (cond ((memq (car rest) (cdr rest))
298 (setcdr rest (delq (car rest) (cdr rest))))
299 ((eq (car (cdr rest)) '_EMACS_INTERNAL) ; shh, it's a secret
300 (setcdr rest (cdr (cdr rest))))
302 (setq rest (cdr rest)))))
303 (apply 'vector all)))
305 (defun xselect-convert-to-delete (selection _type _value)
306 (x-disown-selection-internal selection)
307 ;; A return value of nil means that we do not know how to do this conversion,
308 ;; and replies with an "error". A return value of NULL means that we have
309 ;; done the conversion (and any side-effects) but have no value to return.
310 'NULL)
312 (defun xselect-convert-to-filename (_selection _type value)
313 (when (setq value (xselect--selection-bounds value))
314 (buffer-file-name (nth 2 value))))
316 (defun xselect-convert-to-charpos (_selection _type value)
317 (when (setq value (xselect--selection-bounds value))
318 (let ((beg (1- (nth 0 value))) ; zero-based
319 (end (1- (nth 1 value))))
320 (cons 'SPAN (vector (xselect--int-to-cons (min beg end))
321 (xselect--int-to-cons (max beg end)))))))
323 (defun xselect-convert-to-lineno (_selection _type value)
324 (when (setq value (xselect--selection-bounds value))
325 (with-current-buffer (nth 2 value)
326 (let ((beg (line-number-at-pos (nth 0 value)))
327 (end (line-number-at-pos (nth 1 value))))
328 (cons 'SPAN (vector (xselect--int-to-cons (min beg end))
329 (xselect--int-to-cons (max beg end))))))))
331 (defun xselect-convert-to-colno (_selection _type value)
332 (when (setq value (xselect--selection-bounds value))
333 (with-current-buffer (nth 2 value)
334 (let ((beg (progn (goto-char (nth 0 value)) (current-column)))
335 (end (progn (goto-char (nth 1 value)) (current-column))))
336 (cons 'SPAN (vector (xselect--int-to-cons (min beg end))
337 (xselect--int-to-cons (max beg end))))))))
339 (defun xselect-convert-to-os (_selection _type _size)
340 (symbol-name system-type))
342 (defun xselect-convert-to-host (_selection _type _size)
343 (system-name))
345 (defun xselect-convert-to-user (_selection _type _size)
346 (user-full-name))
348 (defun xselect-convert-to-class (_selection _type _size)
349 "Convert selection to class.
350 This function returns the string \"Emacs\"."
351 "Emacs")
353 ;; We do not try to determine the name Emacs was invoked with,
354 ;; because it is not clean for a program's behavior to depend on that.
355 (defun xselect-convert-to-name (_selection _type _size)
356 "Convert selection to name.
357 This function returns the string \"emacs\"."
358 "emacs")
360 (defun xselect-convert-to-integer (_selection _type value)
361 (and (integerp value)
362 (xselect--int-to-cons value)))
364 (defun xselect-convert-to-atom (_selection _type value)
365 (and (symbolp value) value))
367 (defun xselect-convert-to-identity (_selection _type value) ; used internally
368 (vector value))
370 ;; Null target that tells clipboard managers we support SAVE_TARGETS
371 ;; (see freedesktop.org Clipboard Manager spec).
372 (defun xselect-convert-to-save-targets (selection _type _value)
373 (when (eq selection 'CLIPBOARD)
374 'NULL))
376 (setq selection-converter-alist
377 '((TEXT . xselect-convert-to-string)
378 (COMPOUND_TEXT . xselect-convert-to-string)
379 (STRING . xselect-convert-to-string)
380 (UTF8_STRING . xselect-convert-to-string)
381 (TARGETS . xselect-convert-to-targets)
382 (LENGTH . xselect-convert-to-length)
383 (DELETE . xselect-convert-to-delete)
384 (FILE_NAME . xselect-convert-to-filename)
385 (CHARACTER_POSITION . xselect-convert-to-charpos)
386 (LINE_NUMBER . xselect-convert-to-lineno)
387 (COLUMN_NUMBER . xselect-convert-to-colno)
388 (OWNER_OS . xselect-convert-to-os)
389 (HOST_NAME . xselect-convert-to-host)
390 (USER . xselect-convert-to-user)
391 (CLASS . xselect-convert-to-class)
392 (NAME . xselect-convert-to-name)
393 (ATOM . xselect-convert-to-atom)
394 (INTEGER . xselect-convert-to-integer)
395 (SAVE_TARGETS . xselect-convert-to-save-targets)
396 (_EMACS_INTERNAL . xselect-convert-to-identity)))
398 (provide 'select)
400 ;;; select.el ends here