(elint-check-defcustom-form): Don't use `evenp' so we don't implicitly
[emacs.git] / lisp / select.el
blob01b227d8712936d548e0230c1569d1c7ed415215
1 ;;; select.el --- lisp portion of standard selection support
3 ;; Maintainer: FSF
4 ;; Keywords: internal
6 ;; Copyright (c) 1993, 1994 Free Software Foundation, Inc.
7 ;; Based partially on earlier release by Lucid.
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 2, or (at your option)
14 ;; 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; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
26 ;;; Commentary:
28 ;;; Code:
30 ;; This is for temporary compatibility with pre-release Emacs 19.
31 (defalias 'x-selection 'x-get-selection)
32 (defun x-get-selection (&optional type data-type)
33 "Return the value of an X Windows selection.
34 The argument TYPE (default `PRIMARY') says which selection,
35 and the argument DATA-TYPE (default `STRING') says
36 how to convert the data.
38 TYPE may be `SECONDARY' or `CLIPBOARD', in addition to `PRIMARY'.
39 DATA-TYPE is usually `STRING', but can also be one of the symbols
40 in `selection-converter-alist', which see."
41 (let ((data (x-get-selection-internal (or type 'PRIMARY)
42 (or data-type 'STRING)))
43 coding)
44 (when (and (stringp data)
45 (setq data-type (get-text-property 0 'foreign-selection data)))
46 (setq coding (if (eq data-type 'UTF8_STRING)
47 'utf-8
48 (or next-selection-coding-system
49 selection-coding-system))
50 data (decode-coding-string data coding))
51 (put-text-property 0 (length data) 'foreign-selection data-type data))
52 data))
54 (defun x-get-clipboard ()
55 "Return text pasted to the clipboard."
56 (x-get-selection-internal 'CLIPBOARD 'STRING))
58 (defun x-set-selection (type data)
59 "Make an X Windows selection of type TYPE and value DATA.
60 The argument TYPE (default `PRIMARY') says which selection,
61 and DATA specifies the contents. DATA may be a string,
62 a symbol, an integer (or a cons of two integers or list of two integers).
64 The selection may also be a cons of two markers pointing to the same buffer,
65 or an overlay. In these cases, the selection is considered to be the text
66 between the markers *at whatever time the selection is examined*.
67 Thus, editing done in the buffer after you specify the selection
68 can alter the effective value of the selection.
70 The data may also be a vector of valid non-vector selection values.
72 Interactively, the text of the region is used as the selection value
73 if the prefix arg is set."
74 (interactive (if (not current-prefix-arg)
75 (list 'PRIMARY (read-string "Set text for pasting: "))
76 (list 'PRIMARY (buffer-substring (region-beginning) (region-end)))))
77 ;; This is for temporary compatibility with pre-release Emacs 19.
78 (if (stringp type)
79 (setq type (intern type)))
80 (or (x-valid-simple-selection-p data)
81 (and (vectorp data)
82 (let ((valid t)
83 (i (1- (length data))))
84 (while (>= i 0)
85 (or (x-valid-simple-selection-p (aref data i))
86 (setq valid nil))
87 (setq i (1- i)))
88 valid))
89 (signal 'error (list "invalid selection" data)))
90 (or type (setq type 'PRIMARY))
91 (if data
92 (x-own-selection-internal type data)
93 (x-disown-selection-internal type))
94 data)
96 (defun x-valid-simple-selection-p (data)
97 (or (stringp data)
98 (symbolp data)
99 (integerp data)
100 (and (consp data)
101 (integerp (car data))
102 (or (integerp (cdr data))
103 (and (consp (cdr data))
104 (integerp (car (cdr data))))))
105 (overlayp data)
106 (and (consp data)
107 (markerp (car data))
108 (markerp (cdr data))
109 (marker-buffer (car data))
110 (marker-buffer (cdr data))
111 (eq (marker-buffer (car data))
112 (marker-buffer (cdr data)))
113 (buffer-name (marker-buffer (car data)))
114 (buffer-name (marker-buffer (cdr data))))))
116 ;;; Cut Buffer support
118 (defun x-get-cut-buffer (&optional which-one)
119 "Returns the value of one of the 8 X server cut-buffers.
120 Optional arg WHICH-ONE should be a number from 0 to 7, defaulting to 0.
121 Cut buffers are considered obsolete; you should use selections instead."
122 (x-get-cut-buffer-internal
123 (if which-one
124 (aref [CUT_BUFFER0 CUT_BUFFER1 CUT_BUFFER2 CUT_BUFFER3
125 CUT_BUFFER4 CUT_BUFFER5 CUT_BUFFER6 CUT_BUFFER7]
126 which-one)
127 'CUT_BUFFER0)))
129 (defun x-set-cut-buffer (string &optional push)
130 "Store STRING into the X server's primary cut buffer.
131 If PUSH is non-nil, also rotate the cut buffers:
132 this means the previous value of the primary cut buffer moves to the second
133 cut buffer, and the second to the third, and so on (there are 8 buffers.)
134 Cut buffers are considered obsolete; you should use selections instead."
135 (or (stringp string) (signal 'wrong-type-argument (list 'string string)))
136 (if push
137 (x-rotate-cut-buffers-internal 1))
138 (x-store-cut-buffer-internal 'CUT_BUFFER0 string))
141 ;;; Functions to convert the selection into various other selection types.
142 ;;; Every selection type that Emacs handles is implemented this way, except
143 ;;; for TIMESTAMP, which is a special case.
145 (defun xselect-convert-to-string (selection type value)
146 (let (str coding)
147 ;; Get the actual string from VALUE.
148 (cond ((stringp value)
149 (setq str value))
151 ((overlayp value)
152 (save-excursion
153 (or (buffer-name (overlay-buffer value))
154 (error "selection is in a killed buffer"))
155 (set-buffer (overlay-buffer value))
156 (setq str (buffer-substring (overlay-start value)
157 (overlay-end value)))))
158 ((and (consp value)
159 (markerp (car value))
160 (markerp (cdr value)))
161 (or (eq (marker-buffer (car value)) (marker-buffer (cdr value)))
162 (signal 'error
163 (list "markers must be in the same buffer"
164 (car value) (cdr value))))
165 (save-excursion
166 (set-buffer (or (marker-buffer (car value))
167 (error "selection is in a killed buffer")))
168 (setq str (buffer-substring (car value) (cdr value))))))
170 (when str
171 ;; If TYPE is nil, this is a local request, thus return STR as
172 ;; is. Otherwise, encode STR.
173 (if (not type)
175 (setq coding (or next-selection-coding-system selection-coding-system))
176 (if coding
177 (setq coding (coding-system-base coding))
178 (setq coding 'raw-text))
179 ;; Suppress producing escape sequences for compositions.
180 (remove-text-properties 0 (length str) '(composition nil) str)
181 (cond
182 ((eq type 'TEXT)
183 (if (not (multibyte-string-p str))
184 ;; Don't have to encode unibyte string.
185 (setq type 'STRING)
186 ;; If STR contains only ASCII, Latin-1, and raw bytes,
187 ;; encode STR by iso-latin-1, and return it as type
188 ;; `STRING'. Otherwise, encode STR by CODING. In that
189 ;; case, the returing type depends on CODING.
190 (let ((charsets (find-charset-string str)))
191 (setq charsets
192 (delq 'ascii
193 (delq 'latin-iso8859-1
194 (delq 'eight-bit-control
195 (delq 'eight-bit-graphic charsets)))))
196 (if charsets
197 (setq str (encode-coding-string str coding)
198 type (if (memq coding '(compound-text
199 compound-text-with-extensions))
200 'COMPOUND_TEXT
201 'STRING))
202 (setq type 'STRING
203 str (encode-coding-string str 'iso-latin-1))))))
205 ((eq type 'COMPOUND_TEXT)
206 (setq str (encode-coding-string str coding)))
208 ((eq type 'STRING)
209 (if (memq coding '(compound-text
210 compound-text-with-extensions))
211 (setq str (string-make-unibyte str))
212 (setq str (encode-coding-string str coding))))
214 ((eq type 'UTF8_STRING)
215 (setq str (encode-coding-string str 'utf-8)))
218 (error "Unknow selection type: %S" type))
221 (setq next-selection-coding-system nil)
222 (cons type str))))
225 (defun xselect-convert-to-length (selection type value)
226 (let ((value
227 (cond ((stringp value)
228 (length value))
229 ((overlayp value)
230 (abs (- (overlay-end value) (overlay-start value))))
231 ((and (consp value)
232 (markerp (car value))
233 (markerp (cdr value)))
234 (or (eq (marker-buffer (car value))
235 (marker-buffer (cdr value)))
236 (signal 'error
237 (list "markers must be in the same buffer"
238 (car value) (cdr value))))
239 (abs (- (car value) (cdr value)))))))
240 (if value ; force it to be in 32-bit format.
241 (cons (ash value -16) (logand value 65535))
242 nil)))
244 (defun xselect-convert-to-targets (selection type value)
245 ;; return a vector of atoms, but remove duplicates first.
246 (let* ((all (cons 'TIMESTAMP (mapcar 'car selection-converter-alist)))
247 (rest all))
248 (while rest
249 (cond ((memq (car rest) (cdr rest))
250 (setcdr rest (delq (car rest) (cdr rest))))
251 ((eq (car (cdr rest)) '_EMACS_INTERNAL) ; shh, it's a secret
252 (setcdr rest (cdr (cdr rest))))
254 (setq rest (cdr rest)))))
255 (apply 'vector all)))
257 (defun xselect-convert-to-delete (selection type value)
258 (x-disown-selection-internal selection)
259 ;; A return value of nil means that we do not know how to do this conversion,
260 ;; and replies with an "error". A return value of NULL means that we have
261 ;; done the conversion (and any side-effects) but have no value to return.
262 'NULL)
264 (defun xselect-convert-to-filename (selection type value)
265 (cond ((overlayp value)
266 (buffer-file-name (or (overlay-buffer value)
267 (error "selection is in a killed buffer"))))
268 ((and (consp value)
269 (markerp (car value))
270 (markerp (cdr value)))
271 (buffer-file-name (or (marker-buffer (car value))
272 (error "selection is in a killed buffer"))))
273 (t nil)))
275 (defun xselect-convert-to-charpos (selection type value)
276 (let (a b tmp)
277 (cond ((cond ((overlayp value)
278 (setq a (overlay-start value)
279 b (overlay-end value)))
280 ((and (consp value)
281 (markerp (car value))
282 (markerp (cdr value)))
283 (setq a (car value)
284 b (cdr value))))
285 (setq a (1- a) b (1- b)) ; zero-based
286 (if (< b a) (setq tmp a a b b tmp))
287 (cons 'SPAN
288 (vector (cons (ash a -16) (logand a 65535))
289 (cons (ash b -16) (logand b 65535))))))))
291 (defun xselect-convert-to-lineno (selection type value)
292 (let (a b buf tmp)
293 (cond ((cond ((and (consp value)
294 (markerp (car value))
295 (markerp (cdr value)))
296 (setq a (marker-position (car value))
297 b (marker-position (cdr value))
298 buf (marker-buffer (car value))))
299 ((overlayp value)
300 (setq buf (overlay-buffer value)
301 a (overlay-start value)
302 b (overlay-end value)))
304 (save-excursion
305 (set-buffer buf)
306 (setq a (count-lines 1 a)
307 b (count-lines 1 b)))
308 (if (< b a) (setq tmp a a b b tmp))
309 (cons 'SPAN
310 (vector (cons (ash a -16) (logand a 65535))
311 (cons (ash b -16) (logand b 65535))))))))
313 (defun xselect-convert-to-colno (selection type value)
314 (let (a b buf tmp)
315 (cond ((cond ((and (consp value)
316 (markerp (car value))
317 (markerp (cdr value)))
318 (setq a (car value)
319 b (cdr value)
320 buf (marker-buffer a)))
321 ((overlayp value)
322 (setq buf (overlay-buffer value)
323 a (overlay-start value)
324 b (overlay-end value)))
326 (save-excursion
327 (set-buffer buf)
328 (goto-char a)
329 (setq a (current-column))
330 (goto-char b)
331 (setq b (current-column)))
332 (if (< b a) (setq tmp a a b b tmp))
333 (cons 'SPAN
334 (vector (cons (ash a -16) (logand a 65535))
335 (cons (ash b -16) (logand b 65535))))))))
337 (defun xselect-convert-to-os (selection type size)
338 (symbol-name system-type))
340 (defun xselect-convert-to-host (selection type size)
341 (system-name))
343 (defun xselect-convert-to-user (selection type size)
344 (user-full-name))
346 (defun xselect-convert-to-class (selection type size)
347 "Convert selection to class.
348 This function returns the string \"Emacs\"."
349 "Emacs")
351 ;; We do not try to determine the name Emacs was invoked with,
352 ;; because it is not clean for a program's behavior to depend on that.
353 (defun xselect-convert-to-name (selection type size)
354 "Convert selection to name.
355 This function returns the string \"emacs\"."
356 "emacs")
358 (defun xselect-convert-to-integer (selection type value)
359 (and (integerp value)
360 (cons (ash value -16) (logand value 65535))))
362 (defun xselect-convert-to-atom (selection type value)
363 (and (symbolp value) value))
365 (defun xselect-convert-to-identity (selection type value) ; used internally
366 (vector value))
368 (setq selection-converter-alist
369 '((TEXT . xselect-convert-to-string)
370 (COMPOUND_TEXT . xselect-convert-to-string)
371 (STRING . xselect-convert-to-string)
372 (UTF8_STRING . xselect-convert-to-string)
373 (TARGETS . xselect-convert-to-targets)
374 (LENGTH . xselect-convert-to-length)
375 (DELETE . xselect-convert-to-delete)
376 (FILE_NAME . xselect-convert-to-filename)
377 (CHARACTER_POSITION . xselect-convert-to-charpos)
378 (LINE_NUMBER . xselect-convert-to-lineno)
379 (COLUMN_NUMBER . xselect-convert-to-colno)
380 (OWNER_OS . xselect-convert-to-os)
381 (HOST_NAME . xselect-convert-to-host)
382 (USER . xselect-convert-to-user)
383 (CLASS . xselect-convert-to-class)
384 (NAME . xselect-convert-to-name)
385 (ATOM . xselect-convert-to-atom)
386 (INTEGER . xselect-convert-to-integer)
387 (_EMACS_INTERNAL . xselect-convert-to-identity)
390 (provide 'select)
392 ;;; arch-tag: bb634f97-8a3b-4b0a-b940-f6e09982328c
393 ;;; select.el ends here