aebf6761c4a9ae221700d9d6a3898015737c9894
[chise/xemacs-chise.git.1] / lisp / select.el
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)
38   (and (console-on-window-system-p)
39        (cut-copy-clear-internal 'copy)))
40 (define-obsolete-function-alias
41   'x-copy-primary-selection
42   'copy-primary-selection)
43
44 (defun kill-primary-selection ()
45   "Copy the selection to the Clipboard and the kill ring, then delete it."
46   (interactive "*")
47   (and (console-on-window-system-p)
48        (cut-copy-clear-internal 'cut)))
49 (define-obsolete-function-alias
50   'x-kill-primary-selection
51   'kill-primary-selection)
52
53 (defun delete-primary-selection ()
54   "Delete the selection without copying it to the Clipboard or the kill ring."
55   (interactive "*")
56   (and (console-on-window-system-p)
57        (cut-copy-clear-internal 'clear)))
58 (define-obsolete-function-alias
59   'x-delete-primary-selection
60   'delete-primary-selection)
61
62 (defun yank-clipboard-selection ()
63   "Insert the current Clipboard selection at point."
64   (interactive "*")
65   (case (device-type (selected-device))
66     (x (x-yank-clipboard-selection))
67     (mswindows (mswindows-paste-clipboard))
68     (otherwise nil)))
69
70 (defun selection-owner-p (&optional selection)
71   "Return t if current emacs process owns the given Selection.
72 The arg should be the name of the selection in question, typically one
73 of the symbols PRIMARY, SECONDARY, or CLIPBOARD.  (For convenience,
74 the symbol nil is the same as PRIMARY, and t is the same as
75 SECONDARY.)"
76   (interactive)
77   (case (device-type (selected-device))
78     (x (x-selection-owner-p selection))
79     (mswindows (mswindows-selection-owner-p selection))
80     (otherwise nil)))
81
82 (defun selection-exists-p (&optional selection)
83   "Whether there is an owner for the given Selection.  
84 The arg should be the name of the selection in question, typically one
85 of the symbols PRIMARY, SECONDARY, or CLIPBOARD.  (For convenience,
86 the symbol nil is the same as PRIMARY, and t is the same as
87 SECONDARY."
88   (interactive)
89   (case (device-type (selected-device))
90     (x (x-selection-exists-p selection))
91     (mswindows (mswindows-selection-exists-p))
92     (otherwise nil)))
93
94 (defun own-selection (data &optional type)
95   "Make an Windows selection of type TYPE and value DATA.
96 The argument TYPE (default `PRIMARY') says which selection,
97 and DATA specifies the contents.  DATA may be a string,
98 a symbol, an integer (or a cons of two integers or list of two integers).
99
100 The selection may also be a cons of two markers pointing to the same buffer,
101 or an overlay.  In these cases, the selection is considered to be the text
102 between the markers *at whatever time the selection is examined*.
103 Thus, editing done in the buffer after you specify the selection
104 can alter the effective value of the selection.
105
106 The data may also be a vector of valid non-vector selection values.
107
108 Interactively, the text of the region is used as the selection value."
109   (interactive (if (not current-prefix-arg)
110                    (list (read-string "Store text for pasting: "))
111                  (list (substring (region-beginning) (region-end)))))
112   (case (device-type (selected-device))
113     (x (x-own-selection data type))
114     (mswindows (mswindows-own-selection data type))
115     (otherwise nil)))
116
117 (defun own-clipboard (string)
118   "Paste the given string to the Clipboard."
119   (case (device-type (selected-device))
120     (x (x-own-clipboard string))
121     (mswindows (mswindows-own-clipboard string))
122     (otherwise nil)))
123
124 (defun disown-selection (&optional secondary-p)
125   "Assuming we own the selection, disown it.  With an argument, discard the
126 secondary selection instead of the primary selection."
127   (case (device-type (selected-device))
128     (x (x-disown-selection secondary-p))
129     (mswindows (mswindows-disown-selection secondary-p))
130     (otherwise nil)))
131
132
133 ;; from x-init.el
134 ;; selections and active regions
135
136 ;; If and only if zmacs-regions is true:
137
138 ;; When a mark is pushed and the region goes into the "active" state, we
139 ;; assert it as the Primary selection.  This causes it to be hilighted.
140 ;; When the region goes into the "inactive" state, we disown the Primary
141 ;; selection, causing the region to be dehilighted.
142
143 ;; Note that it is possible for the region to be in the "active" state
144 ;; and not be hilighted, if it is in the active state and then some other
145 ;; application asserts the selection.  This is probably not a big deal.
146
147 (defun activate-region-as-selection ()
148   (if (marker-buffer (mark-marker t))
149       (own-selection (cons (point-marker t) (mark-marker t)))))
150
151 ; moved from x-select.el
152 (defvar primary-selection-extent nil
153   "The extent of the primary selection; don't use this.")
154
155 (defvar secondary-selection-extent nil
156   "The extent of the secondary selection; don't use this.")
157
158 (defun select-make-extent-for-selection (selection previous-extent)
159   ;; Given a selection, this makes an extent in the buffer which holds that
160   ;; selection, for highlighting purposes.  If the selection isn't associated
161   ;; with a buffer, this does nothing.
162   (let ((buffer nil)
163         (valid (and (extentp previous-extent)
164                     (extent-object previous-extent)
165                     (buffer-live-p (extent-object previous-extent))))
166         start end)
167     (cond ((stringp selection)
168            ;; if we're selecting a string, lose the previous extent used
169            ;; to highlight the selection.
170            (setq valid nil))
171           ((consp selection)
172            (setq start (min (car selection) (cdr selection))
173                  end (max (car selection) (cdr selection))
174                  valid (and valid
175                             (eq (marker-buffer (car selection))
176                                 (extent-object previous-extent)))
177                  buffer (marker-buffer (car selection))))
178           ((extentp selection)
179            (setq start (extent-start-position selection)
180                  end (extent-end-position selection)
181                  valid (and valid
182                             (eq (extent-object selection)
183                                 (extent-object previous-extent)))
184                  buffer (extent-object selection)))
185           (t
186            (signal 'error (list "invalid selection" selection))))
187
188     (if valid
189         nil
190       (condition-case ()
191           (if (listp previous-extent)
192               (mapcar 'delete-extent previous-extent)
193             (delete-extent previous-extent))
194         (error nil)))
195
196     (if (not buffer)
197         ;; string case
198         nil
199       ;; normal case
200       (if valid
201           (set-extent-endpoints previous-extent start end)
202         (setq previous-extent (make-extent start end buffer))
203
204         ;; Make the extent be closed on the right, which means that if
205         ;; characters are inserted exactly at the end of the extent, the
206         ;; extent will grow to cover them.  This is important for shell
207         ;; buffers - suppose one makes a selection, and one end is at
208         ;; point-max.  If the shell produces output, that marker will remain
209         ;; at point-max (its position will increase).  So it's important that
210         ;; the extent exhibit the same behavior, lest the region covered by
211         ;; the extent (the visual indication), and the region between point
212         ;; and mark (the actual selection value) become different!
213         (set-extent-property previous-extent 'end-open nil)
214
215         (cond
216          (mouse-track-rectangle-p
217           (setq previous-extent (list previous-extent))
218           (default-mouse-track-next-move-rect start end previous-extent)
219           ))
220         previous-extent))))
221 (define-obsolete-function-alias
222   'x-select-make-extent-for-selection
223   'select-make-extent-for-selection)
224
225 ;; moved from x-select.el
226 (defun valid-simple-selection-p (data)
227   (or (stringp data)
228       ;FSFmacs huh?? (symbolp data)
229       (integerp data)
230       (and (consp data)
231            (integerp (car data))
232            (or (integerp (cdr data))
233                (and (consp (cdr data))
234                     (integerp (car (cdr data))))))
235       (extentp data)
236       (and (consp data)
237            (markerp (car data))
238            (markerp (cdr data))
239            (marker-buffer (car data))
240            (marker-buffer (cdr data))
241            (eq (marker-buffer (car data))
242                (marker-buffer (cdr data)))
243            (buffer-live-p (marker-buffer (car data)))
244            (buffer-live-p (marker-buffer (cdr data))))))
245 (define-obsolete-function-alias
246   'x-valid-simple-selection-p
247   'valid-simple-selection-p)
248
249 (defun cut-copy-clear-internal (mode)
250   (or (memq mode '(cut copy clear)) (error "unkown mode %S" mode))
251   (or (selection-owner-p)
252       (error "emacs does not own the primary selection"))
253   (setq last-command nil)
254   (or primary-selection-extent
255       (error "the primary selection is not an extent?"))
256   (save-excursion
257     (let (rect-p b s e)
258       (cond
259        ((consp primary-selection-extent)
260         (setq rect-p t
261               b (extent-object (car primary-selection-extent))
262               s (extent-start-position (car primary-selection-extent))
263               e (extent-end-position (car (reverse primary-selection-extent)))))
264        (t
265         (setq rect-p nil
266               b (extent-object primary-selection-extent)
267               s (extent-start-position primary-selection-extent)
268               e (extent-end-position primary-selection-extent))))
269       (set-buffer b)
270       (cond ((memq mode '(cut copy))
271              (if rect-p
272                  (progn
273                    ;; why is killed-rectangle free?  Is it used somewhere?
274                    ;; should it be defvarred?
275                    (setq killed-rectangle (extract-rectangle s e))
276                    (kill-new (mapconcat 'identity killed-rectangle "\n")))
277                (copy-region-as-kill s e))
278              ;; Maybe killing doesn't own clipboard.  Make sure it happens.
279              ;; This memq is kind of grody, because they might have done it
280              ;; some other way, but owning the clipboard twice in that case
281              ;; wouldn't actually hurt anything.
282              (or (and (consp kill-hooks) (memq 'own-clipboard kill-hooks))
283                  (own-clipboard (car kill-ring)))))
284       (cond ((memq mode '(cut clear))
285              (if rect-p
286                  (delete-rectangle s e)
287                (delete-region s e))))
288       (disown-selection nil)
289       )))
290 (define-obsolete-function-alias
291   'x-cut-copy-clear-internal
292   'cut-copy-clear-internal)
293
294 ;;; select.el ends here