Mercurial > emacs
annotate lisp/select.el @ 37223:6ae575a7a695
(defface menu): Doc fix.
author | Gerd Moellmann <gerd@gnu.org> |
---|---|
date | Thu, 05 Apr 2001 16:15:50 +0000 |
parents | 5876bde45199 |
children | 67b464da13ec |
rev | line source |
---|---|
2234 | 1 ;;; select.el --- lisp portion of standard selection support. |
2 | |
3 ;; Keywords: internal | |
4 | |
7300 | 5 ;; Copyright (c) 1993, 1994 Free Software Foundation, Inc. |
2234 | 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 | |
14169 | 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. | |
2234 | 24 |
25 ;;; Code: | |
26 | |
27 ;; This is for temporary compatibility with pre-release Emacs 19. | |
2571
b65cf676a09b
All fsets changed to defaliases.
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2234
diff
changeset
|
28 (defalias 'x-selection 'x-get-selection) |
2234 | 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, | |
19142
fffebc19fe53
(x-get-selection): Change default for data-type
Richard M. Stallman <rms@gnu.org>
parents:
17012
diff
changeset
|
32 and the argument DATA-TYPE (default `STRING') says |
33914
5876bde45199
(x-get-selection): Docstring dix.
Eli Zaretskii <eliz@gnu.org>
parents:
26423
diff
changeset
|
33 how to convert the data. |
5876bde45199
(x-get-selection): Docstring dix.
Eli Zaretskii <eliz@gnu.org>
parents:
26423
diff
changeset
|
34 |
5876bde45199
(x-get-selection): Docstring dix.
Eli Zaretskii <eliz@gnu.org>
parents:
26423
diff
changeset
|
35 TYPE may be `SECONDARY' or `CLIPBOARD', in addition to `PRIMARY'. |
5876bde45199
(x-get-selection): Docstring dix.
Eli Zaretskii <eliz@gnu.org>
parents:
26423
diff
changeset
|
36 DATA-TYPE is usually `STRING', but can also be one of the symbols |
5876bde45199
(x-get-selection): Docstring dix.
Eli Zaretskii <eliz@gnu.org>
parents:
26423
diff
changeset
|
37 in `selection-converter-alist', which see." |
19142
fffebc19fe53
(x-get-selection): Change default for data-type
Richard M. Stallman <rms@gnu.org>
parents:
17012
diff
changeset
|
38 (x-get-selection-internal (or type 'PRIMARY) (or data-type 'STRING))) |
2234 | 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. | |
46 The argument TYPE (default `PRIMARY') says which selection, | |
47 and DATA specifies the contents. DATA may be a string, | |
11406
dc4b96a8dc2e
(x-set-selection): Fix up interactive defaults.
Richard M. Stallman <rms@gnu.org>
parents:
7300
diff
changeset
|
48 a symbol, an integer (or a cons of two integers or list of two integers). |
dc4b96a8dc2e
(x-set-selection): Fix up interactive defaults.
Richard M. Stallman <rms@gnu.org>
parents:
7300
diff
changeset
|
49 |
dc4b96a8dc2e
(x-set-selection): Fix up interactive defaults.
Richard M. Stallman <rms@gnu.org>
parents:
7300
diff
changeset
|
50 The selection may also be a cons of two markers pointing to the same buffer, |
dc4b96a8dc2e
(x-set-selection): Fix up interactive defaults.
Richard M. Stallman <rms@gnu.org>
parents:
7300
diff
changeset
|
51 or an overlay. In these cases, the selection is considered to be the text |
dc4b96a8dc2e
(x-set-selection): Fix up interactive defaults.
Richard M. Stallman <rms@gnu.org>
parents:
7300
diff
changeset
|
52 between the markers *at whatever time the selection is examined*. |
dc4b96a8dc2e
(x-set-selection): Fix up interactive defaults.
Richard M. Stallman <rms@gnu.org>
parents:
7300
diff
changeset
|
53 Thus, editing done in the buffer after you specify the selection |
dc4b96a8dc2e
(x-set-selection): Fix up interactive defaults.
Richard M. Stallman <rms@gnu.org>
parents:
7300
diff
changeset
|
54 can alter the effective value of the selection. |
dc4b96a8dc2e
(x-set-selection): Fix up interactive defaults.
Richard M. Stallman <rms@gnu.org>
parents:
7300
diff
changeset
|
55 |
dc4b96a8dc2e
(x-set-selection): Fix up interactive defaults.
Richard M. Stallman <rms@gnu.org>
parents:
7300
diff
changeset
|
56 The data may also be a vector of valid non-vector selection values. |
dc4b96a8dc2e
(x-set-selection): Fix up interactive defaults.
Richard M. Stallman <rms@gnu.org>
parents:
7300
diff
changeset
|
57 |
26423
73efdb6af008
(x-set-selection): Call buffer-substring, not
Gerd Moellmann <gerd@gnu.org>
parents:
19142
diff
changeset
|
58 Interactively, the text of the region is used as the selection value |
73efdb6af008
(x-set-selection): Call buffer-substring, not
Gerd Moellmann <gerd@gnu.org>
parents:
19142
diff
changeset
|
59 if the prefix arg is set." |
2234 | 60 (interactive (if (not current-prefix-arg) |
11406
dc4b96a8dc2e
(x-set-selection): Fix up interactive defaults.
Richard M. Stallman <rms@gnu.org>
parents:
7300
diff
changeset
|
61 (list 'PRIMARY (read-string "Set text for pasting: ")) |
26423
73efdb6af008
(x-set-selection): Call buffer-substring, not
Gerd Moellmann <gerd@gnu.org>
parents:
19142
diff
changeset
|
62 (list 'PRIMARY (buffer-substring (region-beginning) (region-end))))) |
2234 | 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)))))) | |
6442
c81cfdffcf49
(x-valid-simple-selection-p): Accept an overlay.
Richard M. Stallman <rms@gnu.org>
parents:
3035
diff
changeset
|
91 (overlayp data) |
2234 | 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 | |
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 | |
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))) | |
114 | |
3035
5c758290ba6c
(x-set-cut-buffer): New arg PUSH.
Richard M. Stallman <rms@gnu.org>
parents:
2879
diff
changeset
|
115 (defun x-set-cut-buffer (string &optional push) |
2234 | 116 "Store STRING into the X server's primary cut buffer. |
3035
5c758290ba6c
(x-set-cut-buffer): New arg PUSH.
Richard M. Stallman <rms@gnu.org>
parents:
2879
diff
changeset
|
117 If PUSH is non-nil, also rotate the cut buffers: |
5c758290ba6c
(x-set-cut-buffer): New arg PUSH.
Richard M. Stallman <rms@gnu.org>
parents:
2879
diff
changeset
|
118 this means the previous value of the primary cut buffer moves the second |
2234 | 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) | |
3035
5c758290ba6c
(x-set-cut-buffer): New arg PUSH.
Richard M. Stallman <rms@gnu.org>
parents:
2879
diff
changeset
|
123 (if push |
5c758290ba6c
(x-set-cut-buffer): New arg PUSH.
Richard M. Stallman <rms@gnu.org>
parents:
2879
diff
changeset
|
124 (x-rotate-cut-buffers-internal 1)) |
2234 | 125 (x-store-cut-buffer-internal 'CUT_BUFFER0 string)) |
126 | |
127 | |
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) | |
6442
c81cfdffcf49
(x-valid-simple-selection-p): Accept an overlay.
Richard M. Stallman <rms@gnu.org>
parents:
3035
diff
changeset
|
135 ((overlayp value) |
c81cfdffcf49
(x-valid-simple-selection-p): Accept an overlay.
Richard M. Stallman <rms@gnu.org>
parents:
3035
diff
changeset
|
136 (save-excursion |
c81cfdffcf49
(x-valid-simple-selection-p): Accept an overlay.
Richard M. Stallman <rms@gnu.org>
parents:
3035
diff
changeset
|
137 (or (buffer-name (overlay-buffer value)) |
c81cfdffcf49
(x-valid-simple-selection-p): Accept an overlay.
Richard M. Stallman <rms@gnu.org>
parents:
3035
diff
changeset
|
138 (error "selection is in a killed buffer")) |
c81cfdffcf49
(x-valid-simple-selection-p): Accept an overlay.
Richard M. Stallman <rms@gnu.org>
parents:
3035
diff
changeset
|
139 (set-buffer (overlay-buffer value)) |
c81cfdffcf49
(x-valid-simple-selection-p): Accept an overlay.
Richard M. Stallman <rms@gnu.org>
parents:
3035
diff
changeset
|
140 (buffer-substring (overlay-start value) |
c81cfdffcf49
(x-valid-simple-selection-p): Accept an overlay.
Richard M. Stallman <rms@gnu.org>
parents:
3035
diff
changeset
|
141 (overlay-end value)))) |
2234 | 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)) | |
6442
c81cfdffcf49
(x-valid-simple-selection-p): Accept an overlay.
Richard M. Stallman <rms@gnu.org>
parents:
3035
diff
changeset
|
159 ((overlayp value) |
c81cfdffcf49
(x-valid-simple-selection-p): Accept an overlay.
Richard M. Stallman <rms@gnu.org>
parents:
3035
diff
changeset
|
160 (abs (- (overlay-end value) (overlay-start value)))) |
2234 | 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) | |
6442
c81cfdffcf49
(x-valid-simple-selection-p): Accept an overlay.
Richard M. Stallman <rms@gnu.org>
parents:
3035
diff
changeset
|
195 (cond ((overlayp value) |
c81cfdffcf49
(x-valid-simple-selection-p): Accept an overlay.
Richard M. Stallman <rms@gnu.org>
parents:
3035
diff
changeset
|
196 (buffer-file-name (or (overlay-buffer value) |
c81cfdffcf49
(x-valid-simple-selection-p): Accept an overlay.
Richard M. Stallman <rms@gnu.org>
parents:
3035
diff
changeset
|
197 (error "selection is in a killed buffer")))) |
2234 | 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) | |
6442
c81cfdffcf49
(x-valid-simple-selection-p): Accept an overlay.
Richard M. Stallman <rms@gnu.org>
parents:
3035
diff
changeset
|
207 (cond ((cond ((overlayp value) |
c81cfdffcf49
(x-valid-simple-selection-p): Accept an overlay.
Richard M. Stallman <rms@gnu.org>
parents:
3035
diff
changeset
|
208 (setq a (overlay-start value) |
c81cfdffcf49
(x-valid-simple-selection-p): Accept an overlay.
Richard M. Stallman <rms@gnu.org>
parents:
3035
diff
changeset
|
209 b (overlay-end value))) |
2234 | 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)))) | |
6442
c81cfdffcf49
(x-valid-simple-selection-p): Accept an overlay.
Richard M. Stallman <rms@gnu.org>
parents:
3035
diff
changeset
|
229 ((overlayp value) |
c81cfdffcf49
(x-valid-simple-selection-p): Accept an overlay.
Richard M. Stallman <rms@gnu.org>
parents:
3035
diff
changeset
|
230 (setq buf (overlay-buffer value) |
c81cfdffcf49
(x-valid-simple-selection-p): Accept an overlay.
Richard M. Stallman <rms@gnu.org>
parents:
3035
diff
changeset
|
231 a (overlay-start value) |
c81cfdffcf49
(x-valid-simple-selection-p): Accept an overlay.
Richard M. Stallman <rms@gnu.org>
parents:
3035
diff
changeset
|
232 b (overlay-end value))) |
2234 | 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))) | |
6442
c81cfdffcf49
(x-valid-simple-selection-p): Accept an overlay.
Richard M. Stallman <rms@gnu.org>
parents:
3035
diff
changeset
|
251 ((overlayp value) |
c81cfdffcf49
(x-valid-simple-selection-p): Accept an overlay.
Richard M. Stallman <rms@gnu.org>
parents:
3035
diff
changeset
|
252 (setq buf (overlay-buffer value) |
c81cfdffcf49
(x-valid-simple-selection-p): Accept an overlay.
Richard M. Stallman <rms@gnu.org>
parents:
3035
diff
changeset
|
253 a (overlay-start value) |
c81cfdffcf49
(x-valid-simple-selection-p): Accept an overlay.
Richard M. Stallman <rms@gnu.org>
parents:
3035
diff
changeset
|
254 b (overlay-end value))) |
2234 | 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) | |
2879
48dd9b2361df
* select.el (xselect-convert-to-class): Just return "Emacs" here.
Jim Blandy <jimb@redhat.com>
parents:
2571
diff
changeset
|
277 "Emacs") |
2234 | 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) | |
17012
f1932b36f01d
(x-get-selection): Set default data-type of selection
Karl Heuer <kwzh@gnu.org>
parents:
14169
diff
changeset
|
296 (COMPOUND_TEXT . xselect-convert-to-string) |
2234 | 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. |