Some fixes to follow coding conventions in files from Gnus.
[bpt/emacs.git] / lisp / select.el
CommitLineData
3109d63f
ER
1;;; select.el --- lisp portion of standard selection support.
2
3;; Keywords: internal
4
8f1204db 5;; Copyright (c) 1993, 1994 Free Software Foundation, Inc.
3109d63f
ER
6;; Based partially on earlier release by Lucid.
7
8;; This file is part of GNU Emacs.
9
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.
14
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.
19
20;; You should have received a copy of the GNU General Public License
b578f267
EN
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.
3109d63f
ER
24
25;;; Code:
26
27;; This is for temporary compatibility with pre-release Emacs 19.
31e1d920 28(defalias 'x-selection 'x-get-selection)
3109d63f
ER
29(defun x-get-selection (&optional type data-type)
30 "Return the value of an X Windows selection.
31The argument TYPE (default `PRIMARY') says which selection,
4029fd9a 32and the argument DATA-TYPE (default `STRING') says
76058c27
EZ
33how to convert the data.
34
35TYPE may be `SECONDARY' or `CLIPBOARD', in addition to `PRIMARY'.
36DATA-TYPE is usually `STRING', but can also be one of the symbols
37in `selection-converter-alist', which see."
4029fd9a 38 (x-get-selection-internal (or type 'PRIMARY) (or data-type 'STRING)))
3109d63f
ER
39
40(defun x-get-clipboard ()
41 "Return text pasted to the clipboard."
42 (x-get-selection-internal 'CLIPBOARD 'STRING))
43
44(defun x-set-selection (type data)
45 "Make an X Windows selection of type TYPE and value DATA.
46The argument TYPE (default `PRIMARY') says which selection,
47and DATA specifies the contents. DATA may be a string,
5038b790
RS
48a symbol, an integer (or a cons of two integers or list of two integers).
49
50The selection may also be a cons of two markers pointing to the same buffer,
51or an overlay. In these cases, the selection is considered to be the text
52between the markers *at whatever time the selection is examined*.
53Thus, editing done in the buffer after you specify the selection
54can alter the effective value of the selection.
55
56The data may also be a vector of valid non-vector selection values.
57
1b270b55
GM
58Interactively, the text of the region is used as the selection value
59if the prefix arg is set."
3109d63f 60 (interactive (if (not current-prefix-arg)
5038b790 61 (list 'PRIMARY (read-string "Set text for pasting: "))
1b270b55 62 (list 'PRIMARY (buffer-substring (region-beginning) (region-end)))))
3109d63f
ER
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)
81
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))))))
6eac0de6 91 (overlayp data)
3109d63f
ER
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))))))
101\f
102;;; Cut Buffer support
103
104(defun x-get-cut-buffer (&optional which-one)
105 "Returns the value of one of the 8 X server cut-buffers. Optional arg
106WHICH-ONE should be a number from 0 to 7, defaulting to 0.
107Cut 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)))
114
39479f7e 115(defun x-set-cut-buffer (string &optional push)
3109d63f 116 "Store STRING into the X server's primary cut buffer.
39479f7e
RS
117If PUSH is non-nil, also rotate the cut buffers:
118this means the previous value of the primary cut buffer moves the second
3109d63f
ER
119cut buffer, and the second to the third, and so on (there are 8 buffers.)
120Cut buffers are considered obsolete; you should use selections instead."
121 ;; Check the data type of STRING.
122 (substring string 0 0)
39479f7e
RS
123 (if push
124 (x-rotate-cut-buffers-internal 1))
3109d63f
ER
125 (x-store-cut-buffer-internal 'CUT_BUFFER0 string))
126
127\f
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.
131
132(defun xselect-convert-to-string (selection type value)
133 (cond ((stringp value)
134 value)
6eac0de6
RS
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))))
3109d63f
ER
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)))
154
155(defun xselect-convert-to-length (selection type value)
156 (let ((value
157 (cond ((stringp value)
158 (length value))
6eac0de6
RS
159 ((overlayp value)
160 (abs (- (overlay-end value) (overlay-start value))))
3109d63f
ER
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)))
173
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))))
183 (t
184 (setq rest (cdr rest)))))
185 (apply 'vector all)))
186
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)
193
194(defun xselect-convert-to-filename (selection type value)
6eac0de6
RS
195 (cond ((overlayp value)
196 (buffer-file-name (or (overlay-buffer value)
197 (error "selection is in a killed buffer"))))
3109d63f
ER
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)))
204
205(defun xselect-convert-to-charpos (selection type value)
206 (let (a b tmp)
6eac0de6
RS
207 (cond ((cond ((overlayp value)
208 (setq a (overlay-start value)
209 b (overlay-end value)))
3109d63f
ER
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))))))))
220
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))))
6eac0de6
RS
229 ((overlayp value)
230 (setq buf (overlay-buffer value)
231 a (overlay-start value)
232 b (overlay-end value)))
3109d63f
ER
233 )
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))))))))
242
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)))
6eac0de6
RS
251 ((overlayp value)
252 (setq buf (overlay-buffer value)
253 a (overlay-start value)
254 b (overlay-end value)))
3109d63f
ER
255 )
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))))))))
266
267(defun xselect-convert-to-os (selection type size)
268 (symbol-name system-type))
269
270(defun xselect-convert-to-host (selection type size)
271 (system-name))
272
273(defun xselect-convert-to-user (selection type size)
274 (user-full-name))
275
276(defun xselect-convert-to-class (selection type size)
7e773fb4 277 "Emacs")
3109d63f
ER
278
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")
283
284(defun xselect-convert-to-integer (selection type value)
285 (and (integerp value)
286 (cons (ash value -16) (logand value 65535))))
287
288(defun xselect-convert-to-atom (selection type value)
289 (and (symbolp value) value))
290
291(defun xselect-convert-to-identity (selection type value) ; used internally
292 (vector value))
293
294(setq selection-converter-alist
295 '((TEXT . xselect-convert-to-string)
79501c8d 296 (COMPOUND_TEXT . xselect-convert-to-string)
3109d63f
ER
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)
313 ))
314
315(provide 'select)
316
317;;; select.el ends here.