280
|
1 ;;; select.el --- Lisp interface to windows selections.
|
|
2
|
|
3 ;; Copyright (C) 1998 Andy Piper.
|
|
4 ;; Copyright (C) 1990, 1997 Free Software Foundation, Inc.
|
|
5 ;; Copyright (C) 1995 Sun Microsystems.
|
|
6
|
|
7 ;; Maintainer: XEmacs Development Team
|
|
8 ;; Keywords: extensions, dumped
|
|
9
|
|
10 ;; This file is part of XEmacs.
|
|
11
|
|
12 ;; XEmacs is free software; you can redistribute it and/or modify it
|
|
13 ;; under the terms of the GNU General Public License as published by
|
|
14 ;; the Free Software Foundation; either version 2, or (at your option)
|
|
15 ;; any later version.
|
|
16
|
|
17 ;; XEmacs is distributed in the hope that it will be useful, but
|
|
18 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
20 ;; General Public License for more details.
|
|
21
|
|
22 ;; You should have received a copy of the GNU General Public License
|
|
23 ;; along with XEmacs; see the file COPYING. If not, write to the
|
|
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
25 ;; Boston, MA 02111-1307, USA.
|
|
26
|
|
27 ;;; Synched up with: Not in FSF
|
|
28
|
|
29 ;;; Commentary:
|
|
30
|
|
31 ;; This file is dumped with XEmacs
|
|
32
|
|
33 ;;; Code:
|
|
34
|
|
35 (defun copy-primary-selection ()
|
|
36 "Copy the selection to the Clipboard and the kill ring."
|
|
37 (interactive)
|
286
|
38 (and (console-on-window-system-p)
|
|
39 (cut-copy-clear-internal 'copy)))
|
280
|
40
|
|
41 (defun kill-primary-selection ()
|
|
42 "Copy the selection to the Clipboard and the kill ring, then delete it."
|
|
43 (interactive "*")
|
286
|
44 (and (console-on-window-system-p)
|
|
45 (cut-copy-clear-internal 'cut)))
|
280
|
46
|
|
47 (defun delete-primary-selection ()
|
|
48 "Delete the selection without copying it to the Clipboard or the kill ring."
|
|
49 (interactive "*")
|
286
|
50 (and (console-on-window-system-p)
|
|
51 (cut-copy-clear-internal 'clear)))
|
280
|
52
|
|
53 (defun yank-clipboard-selection ()
|
|
54 "Insert the current Clipboard selection at point."
|
|
55 (interactive "*")
|
|
56 (case (device-type (selected-device))
|
|
57 (x (x-yank-clipboard-selection))
|
|
58 (mswindows (mswindows-paste-clipboard))
|
|
59 (otherwise nil)))
|
|
60
|
|
61 (defun selection-owner-p (&optional selection)
|
|
62 "Return t if current emacs process owns the given Selection.
|
|
63 The arg should be the name of the selection in question, typically one
|
|
64 of the symbols PRIMARY, SECONDARY, or CLIPBOARD. (For convenience,
|
|
65 the symbol nil is the same as PRIMARY, and t is the same as
|
|
66 SECONDARY.)"
|
|
67 (interactive)
|
|
68 (case (device-type (selected-device))
|
|
69 (x (x-selection-owner-p selection))
|
|
70 (mswindows (mswindows-selection-owner-p selection))
|
|
71 (otherwise nil)))
|
|
72
|
|
73 (defun selection-exists-p (&optional selection)
|
|
74 "Whether there is an owner for the given Selection.
|
|
75 The arg should be the name of the selection in question, typically one
|
|
76 of the symbols PRIMARY, SECONDARY, or CLIPBOARD. (For convenience,
|
|
77 the symbol nil is the same as PRIMARY, and t is the same as
|
|
78 SECONDARY."
|
|
79 (interactive)
|
|
80 (case (device-type (selected-device))
|
|
81 (x (x-selection-exists-p selection))
|
286
|
82 (mswindows (mswindows-selection-exists-p))
|
280
|
83 (otherwise nil)))
|
|
84
|
|
85 (defun own-selection (data &optional type)
|
|
86 "Make an Windows selection of type TYPE and value DATA.
|
|
87 The argument TYPE (default `PRIMARY') says which selection,
|
|
88 and DATA specifies the contents. DATA may be a string,
|
|
89 a symbol, an integer (or a cons of two integers or list of two integers).
|
|
90
|
|
91 The selection may also be a cons of two markers pointing to the same buffer,
|
|
92 or an overlay. In these cases, the selection is considered to be the text
|
|
93 between the markers *at whatever time the selection is examined*.
|
|
94 Thus, editing done in the buffer after you specify the selection
|
|
95 can alter the effective value of the selection.
|
|
96
|
|
97 The data may also be a vector of valid non-vector selection values.
|
|
98
|
|
99 Interactively, the text of the region is used as the selection value."
|
|
100 (interactive (if (not current-prefix-arg)
|
|
101 (list (read-string "Store text for pasting: "))
|
|
102 (list (substring (region-beginning) (region-end)))))
|
|
103 (case (device-type (selected-device))
|
|
104 (x (x-own-selection data type))
|
|
105 (mswindows (mswindows-own-selection data type))
|
|
106 (otherwise nil)))
|
|
107
|
286
|
108 (defun own-clipboard (string)
|
|
109 "Paste the given string to the Clipboard."
|
|
110 (case (device-type (selected-device))
|
|
111 (x (x-own-clipboard string))
|
|
112 (mswindows (mswindows-own-clipboard string))
|
|
113 (otherwise nil)))
|
|
114
|
280
|
115 (defun disown-selection (&optional secondary-p)
|
|
116 "Assuming we own the selection, disown it. With an argument, discard the
|
|
117 secondary selection instead of the primary selection."
|
|
118 (case (device-type (selected-device))
|
|
119 (x (x-disown-selection secondary-p))
|
|
120 (mswindows (mswindows-disown-selection secondary-p))
|
|
121 (otherwise nil)))
|
|
122
|
286
|
123
|
280
|
124 ;; from x-init.el
|
|
125 ;; selections and active regions
|
|
126
|
|
127 ;; If and only if zmacs-regions is true:
|
|
128
|
|
129 ;; When a mark is pushed and the region goes into the "active" state, we
|
|
130 ;; assert it as the Primary selection. This causes it to be hilighted.
|
|
131 ;; When the region goes into the "inactive" state, we disown the Primary
|
|
132 ;; selection, causing the region to be dehilighted.
|
|
133
|
|
134 ;; Note that it is possible for the region to be in the "active" state
|
|
135 ;; and not be hilighted, if it is in the active state and then some other
|
|
136 ;; application asserts the selection. This is probably not a big deal.
|
|
137
|
|
138 (defun activate-region-as-selection ()
|
|
139 (if (marker-buffer (mark-marker t))
|
|
140 (own-selection (cons (point-marker t) (mark-marker t)))))
|
|
141
|
|
142 ; moved from x-select.el
|
|
143 (defvar primary-selection-extent nil
|
|
144 "The extent of the primary selection; don't use this.")
|
|
145
|
|
146 (defvar secondary-selection-extent nil
|
|
147 "The extent of the secondary selection; don't use this.")
|
|
148
|
|
149 (defun select-make-extent-for-selection (selection previous-extent)
|
|
150 ;; Given a selection, this makes an extent in the buffer which holds that
|
|
151 ;; selection, for highlighting purposes. If the selection isn't associated
|
|
152 ;; with a buffer, this does nothing.
|
|
153 (let ((buffer nil)
|
|
154 (valid (and (extentp previous-extent)
|
|
155 (extent-object previous-extent)
|
|
156 (buffer-live-p (extent-object previous-extent))))
|
|
157 start end)
|
|
158 (cond ((stringp selection)
|
|
159 ;; if we're selecting a string, lose the previous extent used
|
|
160 ;; to highlight the selection.
|
|
161 (setq valid nil))
|
|
162 ((consp selection)
|
|
163 (setq start (min (car selection) (cdr selection))
|
|
164 end (max (car selection) (cdr selection))
|
|
165 valid (and valid
|
|
166 (eq (marker-buffer (car selection))
|
|
167 (extent-object previous-extent)))
|
|
168 buffer (marker-buffer (car selection))))
|
|
169 ((extentp selection)
|
|
170 (setq start (extent-start-position selection)
|
|
171 end (extent-end-position selection)
|
|
172 valid (and valid
|
|
173 (eq (extent-object selection)
|
|
174 (extent-object previous-extent)))
|
|
175 buffer (extent-object selection)))
|
|
176 (t
|
|
177 (signal 'error (list "invalid selection" selection))))
|
|
178
|
|
179 (if valid
|
|
180 nil
|
|
181 (condition-case ()
|
|
182 (if (listp previous-extent)
|
|
183 (mapcar 'delete-extent previous-extent)
|
|
184 (delete-extent previous-extent))
|
|
185 (error nil)))
|
|
186
|
|
187 (if (not buffer)
|
|
188 ;; string case
|
|
189 nil
|
|
190 ;; normal case
|
|
191 (if valid
|
|
192 (set-extent-endpoints previous-extent start end)
|
|
193 (setq previous-extent (make-extent start end buffer))
|
|
194
|
|
195 ;; Make the extent be closed on the right, which means that if
|
|
196 ;; characters are inserted exactly at the end of the extent, the
|
|
197 ;; extent will grow to cover them. This is important for shell
|
|
198 ;; buffers - suppose one makes a selection, and one end is at
|
|
199 ;; point-max. If the shell produces output, that marker will remain
|
|
200 ;; at point-max (its position will increase). So it's important that
|
|
201 ;; the extent exhibit the same behavior, lest the region covered by
|
|
202 ;; the extent (the visual indication), and the region between point
|
|
203 ;; and mark (the actual selection value) become different!
|
|
204 (set-extent-property previous-extent 'end-open nil)
|
|
205
|
|
206 (cond
|
|
207 (mouse-track-rectangle-p
|
|
208 (setq previous-extent (list previous-extent))
|
|
209 (default-mouse-track-next-move-rect start end previous-extent)
|
|
210 ))
|
|
211 previous-extent))))
|
|
212
|
|
213 ;; moved from x-select.el
|
|
214 (defun valid-simple-selection-p (data)
|
|
215 (or (stringp data)
|
|
216 ;FSFmacs huh?? (symbolp data)
|
|
217 (integerp data)
|
|
218 (and (consp data)
|
|
219 (integerp (car data))
|
|
220 (or (integerp (cdr data))
|
|
221 (and (consp (cdr data))
|
|
222 (integerp (car (cdr data))))))
|
|
223 (extentp data)
|
|
224 (and (consp data)
|
|
225 (markerp (car data))
|
|
226 (markerp (cdr data))
|
|
227 (marker-buffer (car data))
|
|
228 (marker-buffer (cdr data))
|
|
229 (eq (marker-buffer (car data))
|
|
230 (marker-buffer (cdr data)))
|
|
231 (buffer-live-p (marker-buffer (car data)))
|
|
232 (buffer-live-p (marker-buffer (cdr data))))))
|
|
233
|
286
|
234 (defun cut-copy-clear-internal (mode)
|
|
235 (or (memq mode '(cut copy clear)) (error "unkown mode %S" mode))
|
|
236 (or (selection-owner-p)
|
|
237 (error "emacs does not own the primary selection"))
|
|
238 (setq last-command nil)
|
|
239 (or primary-selection-extent
|
|
240 (error "the primary selection is not an extent?"))
|
|
241 (save-excursion
|
|
242 (let (rect-p b s e)
|
|
243 (cond
|
|
244 ((consp primary-selection-extent)
|
|
245 (setq rect-p t
|
|
246 b (extent-object (car primary-selection-extent))
|
|
247 s (extent-start-position (car primary-selection-extent))
|
|
248 e (extent-end-position (car (reverse primary-selection-extent)))))
|
|
249 (t
|
|
250 (setq rect-p nil
|
|
251 b (extent-object primary-selection-extent)
|
|
252 s (extent-start-position primary-selection-extent)
|
|
253 e (extent-end-position primary-selection-extent))))
|
|
254 (set-buffer b)
|
|
255 (cond ((memq mode '(cut copy))
|
|
256 (if rect-p
|
|
257 (progn
|
|
258 ;; why is killed-rectangle free? Is it used somewhere?
|
|
259 ;; should it be defvarred?
|
|
260 (setq killed-rectangle (extract-rectangle s e))
|
|
261 (kill-new (mapconcat 'identity killed-rectangle "\n")))
|
|
262 (copy-region-as-kill s e))
|
|
263 ;; Maybe killing doesn't own clipboard. Make sure it happens.
|
|
264 ;; This memq is kind of grody, because they might have done it
|
|
265 ;; some other way, but owning the clipboard twice in that case
|
|
266 ;; wouldn't actually hurt anything.
|
|
267 (or (and (consp kill-hooks) (memq 'own-clipboard kill-hooks))
|
|
268 (own-clipboard (car kill-ring)))))
|
|
269 (cond ((memq mode '(cut clear))
|
|
270 (if rect-p
|
|
271 (delete-rectangle s e)
|
|
272 (delete-region s e))))
|
|
273 (disown-selection nil)
|
|
274 )))
|
|
275
|
280
|
276 ;;; select.el ends here
|