xref: /386bsd/usr/local/lib/emacs/19.25/lisp/select.el (revision a2142627)
1;;; select.el --- lisp portion of standard selection support.
2
3;; Keywords: internal
4
5;; Copyright (c) 1993, 1994 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(defalias '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      (overlayp data)
81      (and (consp data)
82	   (markerp (car data))
83	   (markerp (cdr data))
84	   (marker-buffer (car data))
85	   (marker-buffer (cdr data))
86	   (eq (marker-buffer (car data))
87	       (marker-buffer (cdr data)))
88	   (buffer-name (marker-buffer (car data)))
89	   (buffer-name (marker-buffer (cdr data))))))
90
91;;; Cut Buffer support
92
93(defun x-get-cut-buffer (&optional which-one)
94  "Returns the value of one of the 8 X server cut-buffers.  Optional arg
95WHICH-ONE should be a number from 0 to 7, defaulting to 0.
96Cut buffers are considered obsolete; you should use selections instead."
97  (x-get-cut-buffer-internal
98   (if which-one
99       (aref [CUT_BUFFER0 CUT_BUFFER1 CUT_BUFFER2 CUT_BUFFER3
100	      CUT_BUFFER4 CUT_BUFFER5 CUT_BUFFER6 CUT_BUFFER7]
101	     which-one)
102     'CUT_BUFFER0)))
103
104(defun x-set-cut-buffer (string &optional push)
105  "Store STRING into the X server's primary cut buffer.
106If PUSH is non-nil, also rotate the cut buffers:
107this means the previous value of the primary cut buffer moves the second
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  (if push
113      (x-rotate-cut-buffers-internal 1))
114  (x-store-cut-buffer-internal 'CUT_BUFFER0 string))
115
116
117;;; Functions to convert the selection into various other selection types.
118;;; Every selection type that Emacs handles is implemented this way, except
119;;; for TIMESTAMP, which is a special case.
120
121(defun xselect-convert-to-string (selection type value)
122  (cond ((stringp value)
123	 value)
124	((overlayp value)
125	 (save-excursion
126	   (or (buffer-name (overlay-buffer value))
127	       (error "selection is in a killed buffer"))
128	   (set-buffer (overlay-buffer value))
129	   (buffer-substring (overlay-start value)
130			     (overlay-end value))))
131	((and (consp value)
132	      (markerp (car value))
133	      (markerp (cdr value)))
134	 (or (eq (marker-buffer (car value)) (marker-buffer (cdr value)))
135	     (signal 'error
136		     (list "markers must be in the same buffer"
137			   (car value) (cdr value))))
138	 (save-excursion
139	   (set-buffer (or (marker-buffer (car value))
140			   (error "selection is in a killed buffer")))
141	   (buffer-substring (car value) (cdr value))))
142	(t nil)))
143
144(defun xselect-convert-to-length (selection type value)
145  (let ((value
146	 (cond ((stringp value)
147		(length value))
148	       ((overlayp value)
149		(abs (- (overlay-end value) (overlay-start value))))
150	       ((and (consp value)
151		     (markerp (car value))
152		     (markerp (cdr value)))
153		(or (eq (marker-buffer (car value))
154			(marker-buffer (cdr value)))
155		    (signal 'error
156			    (list "markers must be in the same buffer"
157				  (car value) (cdr value))))
158		(abs (- (car value) (cdr value)))))))
159    (if value ; force it to be in 32-bit format.
160	(cons (ash value -16) (logand value 65535))
161      nil)))
162
163(defun xselect-convert-to-targets (selection type value)
164  ;; return a vector of atoms, but remove duplicates first.
165  (let* ((all (cons 'TIMESTAMP (mapcar 'car selection-converter-alist)))
166	 (rest all))
167    (while rest
168      (cond ((memq (car rest) (cdr rest))
169	     (setcdr rest (delq (car rest) (cdr rest))))
170	    ((eq (car (cdr rest)) '_EMACS_INTERNAL)  ; shh, it's a secret
171	     (setcdr rest (cdr (cdr rest))))
172	    (t
173	     (setq rest (cdr rest)))))
174    (apply 'vector all)))
175
176(defun xselect-convert-to-delete (selection type value)
177  (x-disown-selection-internal selection)
178  ;; A return value of nil means that we do not know how to do this conversion,
179  ;; and replies with an "error".  A return value of NULL means that we have
180  ;; done the conversion (and any side-effects) but have no value to return.
181  'NULL)
182
183(defun xselect-convert-to-filename (selection type value)
184  (cond ((overlayp value)
185	 (buffer-file-name (or (overlay-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 ((overlayp value)
197		  (setq a (overlay-start value)
198			b (overlay-end value)))
199		 ((and (consp value)
200		       (markerp (car value))
201		       (markerp (cdr value)))
202		  (setq a (car value)
203			b (cdr value))))
204	   (setq a (1- a) b (1- b)) ; zero-based
205	   (if (< b a) (setq tmp a a b b tmp))
206	   (cons 'SPAN
207		 (vector (cons (ash a -16) (logand a 65535))
208			 (cons (ash b -16) (logand b 65535))))))))
209
210(defun xselect-convert-to-lineno (selection type value)
211  (let (a b buf tmp)
212    (cond ((cond ((and (consp value)
213		       (markerp (car value))
214		       (markerp (cdr value)))
215		  (setq a (marker-position (car value))
216			b (marker-position (cdr value))
217			buf (marker-buffer (car value))))
218		 ((overlayp value)
219		  (setq buf (overlay-buffer value)
220			a (overlay-start value)
221			b (overlay-end value)))
222		 )
223	   (save-excursion
224	     (set-buffer buf)
225	     (setq a (count-lines 1 a)
226		   b (count-lines 1 b)))
227	   (if (< b a) (setq tmp a a b b tmp))
228	   (cons 'SPAN
229		 (vector (cons (ash a -16) (logand a 65535))
230			 (cons (ash b -16) (logand b 65535))))))))
231
232(defun xselect-convert-to-colno (selection type value)
233  (let (a b buf tmp)
234    (cond ((cond ((and (consp value)
235		       (markerp (car value))
236		       (markerp (cdr value)))
237		  (setq a (car value)
238			b (cdr value)
239			buf (marker-buffer a)))
240		 ((overlayp value)
241		  (setq buf (overlay-buffer value)
242			a (overlay-start value)
243			b (overlay-end value)))
244		 )
245	   (save-excursion
246	     (set-buffer buf)
247	     (goto-char a)
248	     (setq a (current-column))
249	     (goto-char b)
250	     (setq b (current-column)))
251	   (if (< b a) (setq tmp a a b b tmp))
252	   (cons 'SPAN
253		 (vector (cons (ash a -16) (logand a 65535))
254			 (cons (ash b -16) (logand b 65535))))))))
255
256(defun xselect-convert-to-os (selection type size)
257  (symbol-name system-type))
258
259(defun xselect-convert-to-host (selection type size)
260  (system-name))
261
262(defun xselect-convert-to-user (selection type size)
263  (user-full-name))
264
265(defun xselect-convert-to-class (selection type size)
266  "Emacs")
267
268;; We do not try to determine the name Emacs was invoked with,
269;; because it is not clean for a program's behavior to depend on that.
270(defun xselect-convert-to-name (selection type size)
271  "emacs")
272
273(defun xselect-convert-to-integer (selection type value)
274  (and (integerp value)
275       (cons (ash value -16) (logand value 65535))))
276
277(defun xselect-convert-to-atom (selection type value)
278  (and (symbolp value) value))
279
280(defun xselect-convert-to-identity (selection type value) ; used internally
281  (vector value))
282
283(setq selection-converter-alist
284      '((TEXT . xselect-convert-to-string)
285	(STRING . xselect-convert-to-string)
286	(TARGETS . xselect-convert-to-targets)
287	(LENGTH . xselect-convert-to-length)
288	(DELETE . xselect-convert-to-delete)
289	(FILE_NAME . xselect-convert-to-filename)
290	(CHARACTER_POSITION . xselect-convert-to-charpos)
291	(LINE_NUMBER . xselect-convert-to-lineno)
292	(COLUMN_NUMBER . xselect-convert-to-colno)
293	(OWNER_OS . xselect-convert-to-os)
294	(HOST_NAME . xselect-convert-to-host)
295	(USER . xselect-convert-to-user)
296	(CLASS . xselect-convert-to-class)
297	(NAME . xselect-convert-to-name)
298	(ATOM . xselect-convert-to-atom)
299	(INTEGER . xselect-convert-to-integer)
300	(_EMACS_INTERNAL . xselect-convert-to-identity)
301	))
302
303(provide 'select)
304
305;;; select.el ends here.
306