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