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