aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEric S. Raymond1993-03-17 17:19:16 +0000
committerEric S. Raymond1993-03-17 17:19:16 +0000
commit3109d63f84fda59852d4caebbd229939b2b7cd94 (patch)
tree48b1f2667a67840c5aefde8f5e622ba0127f92fb
parent76550a57f934a39f067da196e94b10797efca240 (diff)
downloademacs-3109d63f84fda59852d4caebbd229939b2b7cd94.tar.gz
emacs-3109d63f84fda59852d4caebbd229939b2b7cd94.zip
Initial revision
-rw-r--r--lisp/emacs-lisp/lselect.el228
-rw-r--r--lisp/select.el304
2 files changed, 532 insertions, 0 deletions
diff --git a/lisp/emacs-lisp/lselect.el b/lisp/emacs-lisp/lselect.el
new file mode 100644
index 00000000000..df2b8d65d86
--- /dev/null
+++ b/lisp/emacs-lisp/lselect.el
@@ -0,0 +1,228 @@
1;;; lselect.el --- Lucid interface to X Selections
2
3;; Keywords: emulations
4
5;; This won't completely work until we support or emulate Lucid-style extents.
6;; Copyright (C) 1990, 1993 Free Software Foundation, Inc.
7;; Based on Lucid's selection code.
8
9;; This file is part of GNU Emacs.
10
11;; GNU Emacs is free software; you can redistribute it and/or modify
12;; it under the terms of the GNU General Public License as published by
13;; the Free Software Foundation; either version 2, or (at your option)
14;; any later version.
15
16;; GNU Emacs is distributed in the hope that it will be useful,
17;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19;; GNU General Public License for more details.
20
21;; You should have received a copy of the GNU General Public License
22;; along with GNU Emacs; see the file COPYING. If not, write to
23;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
24
25;;; Code:
26
27;;; The selection code requires us to use certain symbols whose names are
28;;; all upper-case; this may seem tasteless, but it makes there be a 1:1
29;;; correspondence between these symbols and X Atoms (which are upcased.)
30
31(fset 'x-get-cutbuffer 'x-get-cut-buffer)
32(fset 'x-store-cutbuffer 'x-set-cut-buffer)
33
34(or (find-face 'primary-selection)
35 (make-face 'primary-selection))
36
37(or (find-face 'secondary-selection)
38 (make-face 'secondary-selection))
39
40(defun x-get-secondary-selection ()
41 "Return text selected from some X window."
42 (x-get-selection-internal 'SECONDARY 'STRING))
43
44(defvar primary-selection-extent nil
45 "The extent of the primary selection; don't use this.")
46
47(defvar secondary-selection-extent nil
48 "The extent of the secondary selection; don't use this.")
49
50
51(defun x-select-make-extent-for-selection (selection previous-extent face)
52 ;; Given a selection, this makes an extent in the buffer which holds that
53 ;; selection, for highlighting purposes. If the selection isn't associated
54 ;; with a buffer, this does nothing.
55 (let ((buffer nil)
56 (valid (and (extentp previous-extent)
57 (extent-buffer previous-extent)
58 (buffer-name (extent-buffer previous-extent))))
59 start end)
60 (cond ((stringp selection)
61 ;; if we're selecting a string, lose the previous extent used
62 ;; to highlight the selection.
63 (setq valid nil))
64 ((consp selection)
65 (setq start (min (car selection) (cdr selection))
66 end (max (car selection) (cdr selection))
67 valid (and valid
68 (eq (marker-buffer (car selection))
69 (extent-buffer previous-extent)))
70 buffer (marker-buffer (car selection))))
71 ((extentp selection)
72 (setq start (extent-start-position selection)
73 end (extent-end-position selection)
74 valid (and valid
75 (eq (extent-buffer selection)
76 (extent-buffer previous-extent)))
77 buffer (extent-buffer selection)))
78 )
79 (if (and (not valid)
80 (extentp previous-extent)
81 (extent-buffer previous-extent)
82 (buffer-name (extent-buffer previous-extent)))
83 (delete-extent previous-extent))
84 (if (not buffer)
85 ;; string case
86 nil
87 ;; normal case
88 (if valid
89 (set-extent-endpoints previous-extent start end)
90 (setq previous-extent (make-extent start end buffer))
91 ;; use same priority as mouse-highlighting so that conflicts between
92 ;; the selection extent and a mouse-highlighted extent are resolved
93 ;; by the usual size-and-endpoint-comparison method.
94 (set-extent-priority previous-extent mouse-highlight-priority)
95 (set-extent-face previous-extent face)))))
96
97
98(defun x-own-selection (selection &optional type)
99 "Make a primary X Selection of the given argument.
100The argument may be a string, a cons of two markers, or an extent.
101In the latter cases the selection is considered to be the text
102between the markers, or the between extents endpoints."
103 (interactive (if (not current-prefix-arg)
104 (list (read-string "Store text for pasting: "))
105 (list (cons ;; these need not be ordered.
106 (copy-marker (point-marker))
107 (copy-marker (mark-marker))))))
108 (or type (setq type 'PRIMARY))
109 (x-set-selection selection type)
110 (cond ((eq type 'PRIMARY)
111 (setq primary-selection-extent
112 (x-select-make-extent-for-selection
113 selection primary-selection-extent 'primary-selection)))
114 ((eq type 'SECONDARY)
115 (setq secondary-selection-extent
116 (x-select-make-extent-for-selection
117 selection secondary-selection-extent 'secondary-selection))))
118 selection)
119
120
121(defun x-own-secondary-selection (selection &optional type)
122 "Make a secondary X Selection of the given argument. The argument may be a
123string or a cons of two markers (in which case the selection is considered to
124be the text between those markers.)"
125 (interactive (if (not current-prefix-arg)
126 (list (read-string "Store text for pasting: "))
127 (list (cons ;; these need not be ordered.
128 (copy-marker (point-marker))
129 (copy-marker (mark-marker))))))
130 (x-own-selection selection 'SECONDARY))
131
132
133(defun x-own-clipboard (string)
134 "Paste the given string to the X Clipboard."
135 (x-own-selection string 'CLIPBOARD))
136
137
138(defun x-disown-selection (&optional secondary-p)
139 "Assuming we own the selection, disown it. With an argument, discard the
140secondary selection instead of the primary selection."
141 (x-disown-selection-internal (if secondary-p 'SECONDARY 'PRIMARY)))
142
143(defun x-dehilight-selection (selection)
144 "for use as a value of x-lost-selection-hooks."
145 (cond ((eq selection 'PRIMARY)
146 (if primary-selection-extent
147 (let ((inhibit-quit t))
148 (delete-extent primary-selection-extent)
149 (setq primary-selection-extent nil)))
150 (if zmacs-regions (zmacs-deactivate-region)))
151 ((eq selection 'SECONDARY)
152 (if secondary-selection-extent
153 (let ((inhibit-quit t))
154 (delete-extent secondary-selection-extent)
155 (setq secondary-selection-extent nil)))))
156 nil)
157
158(setq x-lost-selection-hooks 'x-dehilight-selection)
159
160(defun x-notice-selection-requests (selection type successful)
161 "for possible use as the value of x-sent-selection-hooks."
162 (if (not successful)
163 (message "Selection request failed to convert %s to %s"
164 selection type)
165 (message "Sent selection %s as %s" selection type)))
166
167(defun x-notice-selection-failures (selection type successful)
168 "for possible use as the value of x-sent-selection-hooks."
169 (or successful
170 (message "Selection request failed to convert %s to %s"
171 selection type)))
172
173;(setq x-sent-selection-hooks 'x-notice-selection-requests)
174;(setq x-sent-selection-hooks 'x-notice-selection-failures)
175
176
177;;; Random utility functions
178
179(defun x-kill-primary-selection ()
180 "If there is a selection, delete the text it covers, and copy it to
181both the kill ring and the Clipboard."
182 (interactive)
183 (or (x-selection-owner-p) (error "emacs does not own the primary selection"))
184 (setq last-command nil)
185 (or primary-selection-extent
186 (error "the primary selection is not an extent?"))
187 (save-excursion
188 (set-buffer (extent-buffer primary-selection-extent))
189 (kill-region (extent-start-position primary-selection-extent)
190 (extent-end-position primary-selection-extent)))
191 (x-disown-selection nil))
192
193(defun x-delete-primary-selection ()
194 "If there is a selection, delete the text it covers *without* copying it to
195the kill ring or the Clipboard."
196 (interactive)
197 (or (x-selection-owner-p) (error "emacs does not own the primary selection"))
198 (setq last-command nil)
199 (or primary-selection-extent
200 (error "the primary selection is not an extent?"))
201 (save-excursion
202 (set-buffer (extent-buffer primary-selection-extent))
203 (delete-region (extent-start-position primary-selection-extent)
204 (extent-end-position primary-selection-extent)))
205 (x-disown-selection nil))
206
207(defun x-copy-primary-selection ()
208 "If there is a selection, copy it to both the kill ring and the Clipboard."
209 (interactive)
210 (setq last-command nil)
211 (or (x-selection-owner-p) (error "emacs does not own the primary selection"))
212 (or primary-selection-extent
213 (error "the primary selection is not an extent?"))
214 (save-excursion
215 (set-buffer (extent-buffer primary-selection-extent))
216 (copy-region-as-kill (extent-start-position primary-selection-extent)
217 (extent-end-position primary-selection-extent))))
218
219(defun x-yank-clipboard-selection ()
220 "If someone owns a Clipboard selection, insert it at point."
221 (interactive)
222 (setq last-command nil)
223 (let ((clip (x-get-clipboard)))
224 (or clip (error "there is no clipboard selection"))
225 (push-mark)
226 (insert clip)))
227
228;;; lselect.el ends here.
diff --git a/lisp/select.el b/lisp/select.el
new file mode 100644
index 00000000000..bfcf20aa652
--- /dev/null
+++ b/lisp/select.el
@@ -0,0 +1,304 @@
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
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
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.