Add that PBM and XBM image specs may have :foreground nil, and
[emacs.git] / lisp / select.el
blob6e0231ca1fc0585af9625d2411a5ba1dfe2dc69e
1 ;;; select.el --- lisp portion of standard selection support.
3 ;; Keywords: internal
5 ;; Copyright (c) 1993, 1994 Free Software Foundation, Inc.
6 ;; Based partially on earlier release by Lucid.
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 2, or (at your option)
13 ;; 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; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
25 ;;; Code:
27 ;; This is for temporary compatibility with pre-release Emacs 19.
28 (defalias 'x-selection 'x-get-selection)
29 (defun x-get-selection (&optional type data-type)
30 "Return the value of an X Windows selection.
31 The argument TYPE (default `PRIMARY') says which selection,
32 and the argument DATA-TYPE (default `STRING') says
33 how to convert the data.
35 TYPE may be `SECONDARY' or `CLIPBOARD', in addition to `PRIMARY'.
36 DATA-TYPE is usually `STRING', but can also be one of the symbols
37 in `selection-converter-alist', which see."
38 (x-get-selection-internal (or type 'PRIMARY) (or data-type 'STRING)))
40 (defun x-get-clipboard ()
41 "Return text pasted to the clipboard."
42 (x-get-selection-internal 'CLIPBOARD 'STRING))
44 (defun x-set-selection (type data)
45 "Make an X Windows selection of type TYPE and value DATA.
46 The argument TYPE (default `PRIMARY') says which selection,
47 and DATA specifies the contents. DATA may be a string,
48 a symbol, an integer (or a cons of two integers or list of two integers).
50 The selection may also be a cons of two markers pointing to the same buffer,
51 or an overlay. In these cases, the selection is considered to be the text
52 between the markers *at whatever time the selection is examined*.
53 Thus, editing done in the buffer after you specify the selection
54 can alter the effective value of the selection.
56 The data may also be a vector of valid non-vector selection values.
58 Interactively, the text of the region is used as the selection value
59 if the prefix arg is set."
60 (interactive (if (not current-prefix-arg)
61 (list 'PRIMARY (read-string "Set text for pasting: "))
62 (list 'PRIMARY (buffer-substring (region-beginning) (region-end)))))
63 ;; This is for temporary compatibility with pre-release Emacs 19.
64 (if (stringp type)
65 (setq type (intern type)))
66 (or (x-valid-simple-selection-p data)
67 (and (vectorp data)
68 (let ((valid t)
69 (i (1- (length data))))
70 (while (>= i 0)
71 (or (x-valid-simple-selection-p (aref data i))
72 (setq valid nil))
73 (setq i (1- i)))
74 valid))
75 (signal 'error (list "invalid selection" data)))
76 (or type (setq type 'PRIMARY))
77 (if data
78 (x-own-selection-internal type data)
79 (x-disown-selection-internal type))
80 data)
82 (defun x-valid-simple-selection-p (data)
83 (or (stringp data)
84 (symbolp data)
85 (integerp data)
86 (and (consp data)
87 (integerp (car data))
88 (or (integerp (cdr data))
89 (and (consp (cdr data))
90 (integerp (car (cdr data))))))
91 (overlayp data)
92 (and (consp data)
93 (markerp (car data))
94 (markerp (cdr data))
95 (marker-buffer (car data))
96 (marker-buffer (cdr data))
97 (eq (marker-buffer (car data))
98 (marker-buffer (cdr data)))
99 (buffer-name (marker-buffer (car data)))
100 (buffer-name (marker-buffer (cdr data))))))
102 ;;; Cut Buffer support
104 (defun x-get-cut-buffer (&optional which-one)
105 "Returns the value of one of the 8 X server cut-buffers. Optional arg
106 WHICH-ONE should be a number from 0 to 7, defaulting to 0.
107 Cut buffers are considered obsolete; you should use selections instead."
108 (x-get-cut-buffer-internal
109 (if which-one
110 (aref [CUT_BUFFER0 CUT_BUFFER1 CUT_BUFFER2 CUT_BUFFER3
111 CUT_BUFFER4 CUT_BUFFER5 CUT_BUFFER6 CUT_BUFFER7]
112 which-one)
113 'CUT_BUFFER0)))
115 (defun x-set-cut-buffer (string &optional push)
116 "Store STRING into the X server's primary cut buffer.
117 If PUSH is non-nil, also rotate the cut buffers:
118 this means the previous value of the primary cut buffer moves the second
119 cut buffer, and the second to the third, and so on (there are 8 buffers.)
120 Cut buffers are considered obsolete; you should use selections instead."
121 ;; Check the data type of STRING.
122 (substring string 0 0)
123 (if push
124 (x-rotate-cut-buffers-internal 1))
125 (x-store-cut-buffer-internal 'CUT_BUFFER0 string))
128 ;;; Functions to convert the selection into various other selection types.
129 ;;; Every selection type that Emacs handles is implemented this way, except
130 ;;; for TIMESTAMP, which is a special case.
132 (defun xselect-convert-to-string (selection type value)
133 (cond ((stringp value)
134 value)
135 ((overlayp value)
136 (save-excursion
137 (or (buffer-name (overlay-buffer value))
138 (error "selection is in a killed buffer"))
139 (set-buffer (overlay-buffer value))
140 (buffer-substring (overlay-start value)
141 (overlay-end value))))
142 ((and (consp value)
143 (markerp (car value))
144 (markerp (cdr value)))
145 (or (eq (marker-buffer (car value)) (marker-buffer (cdr value)))
146 (signal 'error
147 (list "markers must be in the same buffer"
148 (car value) (cdr value))))
149 (save-excursion
150 (set-buffer (or (marker-buffer (car value))
151 (error "selection is in a killed buffer")))
152 (buffer-substring (car value) (cdr value))))
153 (t nil)))
155 (defun xselect-convert-to-length (selection type value)
156 (let ((value
157 (cond ((stringp value)
158 (length value))
159 ((overlayp value)
160 (abs (- (overlay-end value) (overlay-start value))))
161 ((and (consp value)
162 (markerp (car value))
163 (markerp (cdr value)))
164 (or (eq (marker-buffer (car value))
165 (marker-buffer (cdr value)))
166 (signal 'error
167 (list "markers must be in the same buffer"
168 (car value) (cdr value))))
169 (abs (- (car value) (cdr value)))))))
170 (if value ; force it to be in 32-bit format.
171 (cons (ash value -16) (logand value 65535))
172 nil)))
174 (defun xselect-convert-to-targets (selection type value)
175 ;; return a vector of atoms, but remove duplicates first.
176 (let* ((all (cons 'TIMESTAMP (mapcar 'car selection-converter-alist)))
177 (rest all))
178 (while rest
179 (cond ((memq (car rest) (cdr rest))
180 (setcdr rest (delq (car rest) (cdr rest))))
181 ((eq (car (cdr rest)) '_EMACS_INTERNAL) ; shh, it's a secret
182 (setcdr rest (cdr (cdr rest))))
184 (setq rest (cdr rest)))))
185 (apply 'vector all)))
187 (defun xselect-convert-to-delete (selection type value)
188 (x-disown-selection-internal selection)
189 ;; A return value of nil means that we do not know how to do this conversion,
190 ;; and replies with an "error". A return value of NULL means that we have
191 ;; done the conversion (and any side-effects) but have no value to return.
192 'NULL)
194 (defun xselect-convert-to-filename (selection type value)
195 (cond ((overlayp value)
196 (buffer-file-name (or (overlay-buffer value)
197 (error "selection is in a killed buffer"))))
198 ((and (consp value)
199 (markerp (car value))
200 (markerp (cdr value)))
201 (buffer-file-name (or (marker-buffer (car value))
202 (error "selection is in a killed buffer"))))
203 (t nil)))
205 (defun xselect-convert-to-charpos (selection type value)
206 (let (a b tmp)
207 (cond ((cond ((overlayp value)
208 (setq a (overlay-start value)
209 b (overlay-end value)))
210 ((and (consp value)
211 (markerp (car value))
212 (markerp (cdr value)))
213 (setq a (car value)
214 b (cdr value))))
215 (setq a (1- a) b (1- b)) ; zero-based
216 (if (< b a) (setq tmp a a b b tmp))
217 (cons 'SPAN
218 (vector (cons (ash a -16) (logand a 65535))
219 (cons (ash b -16) (logand b 65535))))))))
221 (defun xselect-convert-to-lineno (selection type value)
222 (let (a b buf tmp)
223 (cond ((cond ((and (consp value)
224 (markerp (car value))
225 (markerp (cdr value)))
226 (setq a (marker-position (car value))
227 b (marker-position (cdr value))
228 buf (marker-buffer (car value))))
229 ((overlayp value)
230 (setq buf (overlay-buffer value)
231 a (overlay-start value)
232 b (overlay-end value)))
234 (save-excursion
235 (set-buffer buf)
236 (setq a (count-lines 1 a)
237 b (count-lines 1 b)))
238 (if (< b a) (setq tmp a a b b tmp))
239 (cons 'SPAN
240 (vector (cons (ash a -16) (logand a 65535))
241 (cons (ash b -16) (logand b 65535))))))))
243 (defun xselect-convert-to-colno (selection type value)
244 (let (a b buf tmp)
245 (cond ((cond ((and (consp value)
246 (markerp (car value))
247 (markerp (cdr value)))
248 (setq a (car value)
249 b (cdr value)
250 buf (marker-buffer a)))
251 ((overlayp value)
252 (setq buf (overlay-buffer value)
253 a (overlay-start value)
254 b (overlay-end value)))
256 (save-excursion
257 (set-buffer buf)
258 (goto-char a)
259 (setq a (current-column))
260 (goto-char b)
261 (setq b (current-column)))
262 (if (< b a) (setq tmp a a b b tmp))
263 (cons 'SPAN
264 (vector (cons (ash a -16) (logand a 65535))
265 (cons (ash b -16) (logand b 65535))))))))
267 (defun xselect-convert-to-os (selection type size)
268 (symbol-name system-type))
270 (defun xselect-convert-to-host (selection type size)
271 (system-name))
273 (defun xselect-convert-to-user (selection type size)
274 (user-full-name))
276 (defun xselect-convert-to-class (selection type size)
277 "Emacs")
279 ;; We do not try to determine the name Emacs was invoked with,
280 ;; because it is not clean for a program's behavior to depend on that.
281 (defun xselect-convert-to-name (selection type size)
282 "emacs")
284 (defun xselect-convert-to-integer (selection type value)
285 (and (integerp value)
286 (cons (ash value -16) (logand value 65535))))
288 (defun xselect-convert-to-atom (selection type value)
289 (and (symbolp value) value))
291 (defun xselect-convert-to-identity (selection type value) ; used internally
292 (vector value))
294 (setq selection-converter-alist
295 '((TEXT . xselect-convert-to-string)
296 (COMPOUND_TEXT . xselect-convert-to-string)
297 (STRING . xselect-convert-to-string)
298 (TARGETS . xselect-convert-to-targets)
299 (LENGTH . xselect-convert-to-length)
300 (DELETE . xselect-convert-to-delete)
301 (FILE_NAME . xselect-convert-to-filename)
302 (CHARACTER_POSITION . xselect-convert-to-charpos)
303 (LINE_NUMBER . xselect-convert-to-lineno)
304 (COLUMN_NUMBER . xselect-convert-to-colno)
305 (OWNER_OS . xselect-convert-to-os)
306 (HOST_NAME . xselect-convert-to-host)
307 (USER . xselect-convert-to-user)
308 (CLASS . xselect-convert-to-class)
309 (NAME . xselect-convert-to-name)
310 (ATOM . xselect-convert-to-atom)
311 (INTEGER . xselect-convert-to-integer)
312 (_EMACS_INTERNAL . xselect-convert-to-identity)
315 (provide 'select)
317 ;;; select.el ends here.