Commit | Line | Data |
---|---|---|
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. | |
31e1d920 | 27 | (defalias 'x-selection 'x-get-selection) |
3109d63f ER |
28 | (defun x-get-selection (&optional type data-type) |
29 | "Return the value of an X Windows selection. | |
30 | The argument TYPE (default `PRIMARY') says which selection, | |
31 | and 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. | |
40 | The argument TYPE (default `PRIMARY') says which selection, | |
41 | and DATA specifies the contents. DATA may be a string, | |
42 | a symbol, an integer (or a cons of two integers or list of two integers), | |
43 | or a cons of two markers pointing to the same buffer. | |
44 | In the last case, the selection is considered to be the text | |
45 | between the markers. | |
46 | The 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 | |
96 | WHICH-ONE should be a number from 0 to 7, defaulting to 0. | |
97 | Cut 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 | ||
39479f7e | 105 | (defun x-set-cut-buffer (string &optional push) |
3109d63f | 106 | "Store STRING into the X server's primary cut buffer. |
39479f7e RS |
107 | If PUSH is non-nil, also rotate the cut buffers: |
108 | this means the previous value of the primary cut buffer moves the second | |
3109d63f ER |
109 | cut buffer, and the second to the third, and so on (there are 8 buffers.) |
110 | Cut buffers are considered obsolete; you should use selections instead." | |
111 | ;; Check the data type of STRING. | |
112 | (substring string 0 0) | |
39479f7e RS |
113 | (if push |
114 | (x-rotate-cut-buffers-internal 1)) | |
3109d63f ER |
115 | (x-store-cut-buffer-internal 'CUT_BUFFER0 string)) |
116 | ||
117 | \f | |
118 | ;;; Functions to convert the selection into various other selection types. | |
119 | ;;; Every selection type that Emacs handles is implemented this way, except | |
120 | ;;; for TIMESTAMP, which is a special case. | |
121 | ||
122 | (defun xselect-convert-to-string (selection type value) | |
123 | (cond ((stringp value) | |
124 | value) | |
125 | ;;; ((extentp value) | |
126 | ;;; (save-excursion | |
127 | ;;; (set-buffer (extent-buffer value)) | |
128 | ;;; (buffer-substring (extent-start-position value) | |
129 | ;;; (extent-end-position value)))) | |
130 | ((and (consp value) | |
131 | (markerp (car value)) | |
132 | (markerp (cdr value))) | |
133 | (or (eq (marker-buffer (car value)) (marker-buffer (cdr value))) | |
134 | (signal 'error | |
135 | (list "markers must be in the same buffer" | |
136 | (car value) (cdr value)))) | |
137 | (save-excursion | |
138 | (set-buffer (or (marker-buffer (car value)) | |
139 | (error "selection is in a killed buffer"))) | |
140 | (buffer-substring (car value) (cdr value)))) | |
141 | (t nil))) | |
142 | ||
143 | (defun xselect-convert-to-length (selection type value) | |
144 | (let ((value | |
145 | (cond ((stringp value) | |
146 | (length value)) | |
147 | ;;; ((extentp value) | |
148 | ;;; (extent-length value)) | |
149 | ((and (consp value) | |
150 | (markerp (car value)) | |
151 | (markerp (cdr value))) | |
152 | (or (eq (marker-buffer (car value)) | |
153 | (marker-buffer (cdr value))) | |
154 | (signal 'error | |
155 | (list "markers must be in the same buffer" | |
156 | (car value) (cdr value)))) | |
157 | (abs (- (car value) (cdr value))))))) | |
158 | (if value ; force it to be in 32-bit format. | |
159 | (cons (ash value -16) (logand value 65535)) | |
160 | nil))) | |
161 | ||
162 | (defun xselect-convert-to-targets (selection type value) | |
163 | ;; return a vector of atoms, but remove duplicates first. | |
164 | (let* ((all (cons 'TIMESTAMP (mapcar 'car selection-converter-alist))) | |
165 | (rest all)) | |
166 | (while rest | |
167 | (cond ((memq (car rest) (cdr rest)) | |
168 | (setcdr rest (delq (car rest) (cdr rest)))) | |
169 | ((eq (car (cdr rest)) '_EMACS_INTERNAL) ; shh, it's a secret | |
170 | (setcdr rest (cdr (cdr rest)))) | |
171 | (t | |
172 | (setq rest (cdr rest))))) | |
173 | (apply 'vector all))) | |
174 | ||
175 | (defun xselect-convert-to-delete (selection type value) | |
176 | (x-disown-selection-internal selection) | |
177 | ;; A return value of nil means that we do not know how to do this conversion, | |
178 | ;; and replies with an "error". A return value of NULL means that we have | |
179 | ;; done the conversion (and any side-effects) but have no value to return. | |
180 | 'NULL) | |
181 | ||
182 | (defun xselect-convert-to-filename (selection type value) | |
183 | (cond | |
184 | ;;; ((extentp value) | |
185 | ;;; (buffer-file-name (or (extent-buffer value) | |
186 | ;;; (error "selection is in a killed buffer")))) | |
187 | ((and (consp value) | |
188 | (markerp (car value)) | |
189 | (markerp (cdr value))) | |
190 | (buffer-file-name (or (marker-buffer (car value)) | |
191 | (error "selection is in a killed buffer")))) | |
192 | (t nil))) | |
193 | ||
194 | (defun xselect-convert-to-charpos (selection type value) | |
195 | (let (a b tmp) | |
196 | (cond ((cond | |
197 | ;;; ((extentp value) | |
198 | ;;; (setq a (extent-start-position value) | |
199 | ;;; b (extent-end-position value))) | |
200 | ((and (consp value) | |
201 | (markerp (car value)) | |
202 | (markerp (cdr value))) | |
203 | (setq a (car value) | |
204 | b (cdr value)))) | |
205 | (setq a (1- a) b (1- b)) ; zero-based | |
206 | (if (< b a) (setq tmp a a b b tmp)) | |
207 | (cons 'SPAN | |
208 | (vector (cons (ash a -16) (logand a 65535)) | |
209 | (cons (ash b -16) (logand b 65535)))))))) | |
210 | ||
211 | (defun xselect-convert-to-lineno (selection type value) | |
212 | (let (a b buf tmp) | |
213 | (cond ((cond ((and (consp value) | |
214 | (markerp (car value)) | |
215 | (markerp (cdr value))) | |
216 | (setq a (marker-position (car value)) | |
217 | b (marker-position (cdr value)) | |
218 | buf (marker-buffer (car value)))) | |
219 | ;;; ((extentp value) | |
220 | ;;; (setq buf (extent-buffer value) | |
221 | ;;; a (extent-start-position value) | |
222 | ;;; b (extent-end-position value))) | |
223 | ) | |
224 | (save-excursion | |
225 | (set-buffer buf) | |
226 | (setq a (count-lines 1 a) | |
227 | b (count-lines 1 b))) | |
228 | (if (< b a) (setq tmp a a b b tmp)) | |
229 | (cons 'SPAN | |
230 | (vector (cons (ash a -16) (logand a 65535)) | |
231 | (cons (ash b -16) (logand b 65535)))))))) | |
232 | ||
233 | (defun xselect-convert-to-colno (selection type value) | |
234 | (let (a b buf tmp) | |
235 | (cond ((cond ((and (consp value) | |
236 | (markerp (car value)) | |
237 | (markerp (cdr value))) | |
238 | (setq a (car value) | |
239 | b (cdr value) | |
240 | buf (marker-buffer a))) | |
241 | ;;; ((extentp value) | |
242 | ;;; (setq buf (extent-buffer value) | |
243 | ;;; a (extent-start-position value) | |
244 | ;;; b (extent-end-position value))) | |
245 | ) | |
246 | (save-excursion | |
247 | (set-buffer buf) | |
248 | (goto-char a) | |
249 | (setq a (current-column)) | |
250 | (goto-char b) | |
251 | (setq b (current-column))) | |
252 | (if (< b a) (setq tmp a a b b tmp)) | |
253 | (cons 'SPAN | |
254 | (vector (cons (ash a -16) (logand a 65535)) | |
255 | (cons (ash b -16) (logand b 65535)))))))) | |
256 | ||
257 | (defun xselect-convert-to-os (selection type size) | |
258 | (symbol-name system-type)) | |
259 | ||
260 | (defun xselect-convert-to-host (selection type size) | |
261 | (system-name)) | |
262 | ||
263 | (defun xselect-convert-to-user (selection type size) | |
264 | (user-full-name)) | |
265 | ||
266 | (defun xselect-convert-to-class (selection type size) | |
7e773fb4 | 267 | "Emacs") |
3109d63f ER |
268 | |
269 | ;; We do not try to determine the name Emacs was invoked with, | |
270 | ;; because it is not clean for a program's behavior to depend on that. | |
271 | (defun xselect-convert-to-name (selection type size) | |
272 | "emacs") | |
273 | ||
274 | (defun xselect-convert-to-integer (selection type value) | |
275 | (and (integerp value) | |
276 | (cons (ash value -16) (logand value 65535)))) | |
277 | ||
278 | (defun xselect-convert-to-atom (selection type value) | |
279 | (and (symbolp value) value)) | |
280 | ||
281 | (defun xselect-convert-to-identity (selection type value) ; used internally | |
282 | (vector value)) | |
283 | ||
284 | (setq selection-converter-alist | |
285 | '((TEXT . xselect-convert-to-string) | |
286 | (STRING . xselect-convert-to-string) | |
287 | (TARGETS . xselect-convert-to-targets) | |
288 | (LENGTH . xselect-convert-to-length) | |
289 | (DELETE . xselect-convert-to-delete) | |
290 | (FILE_NAME . xselect-convert-to-filename) | |
291 | (CHARACTER_POSITION . xselect-convert-to-charpos) | |
292 | (LINE_NUMBER . xselect-convert-to-lineno) | |
293 | (COLUMN_NUMBER . xselect-convert-to-colno) | |
294 | (OWNER_OS . xselect-convert-to-os) | |
295 | (HOST_NAME . xselect-convert-to-host) | |
296 | (USER . xselect-convert-to-user) | |
297 | (CLASS . xselect-convert-to-class) | |
298 | (NAME . xselect-convert-to-name) | |
299 | (ATOM . xselect-convert-to-atom) | |
300 | (INTEGER . xselect-convert-to-integer) | |
301 | (_EMACS_INTERNAL . xselect-convert-to-identity) | |
302 | )) | |
303 | ||
304 | (provide 'select) | |
305 | ||
306 | ;;; select.el ends here. |