Contents of release-21-2 in 1999-06-17-23.
[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 (defvar selected-text-type
36   (if (featurep 'mule) '(COMPOUND_TEXT STRING) 'STRING)
37   "The type atom used to obtain selections from the X server.
38 Can be either a valid X selection data type, or a list of such types.
39 COMPOUND_TEXT and STRING are the most commonly used data types.
40 If a list is provided, the types are tried in sequence until
41 there is a successful conversion.")
42
43 (defvar selection-sets-clipboard nil 
44   "Controls the selection's relationship to the clipboard.
45 When non-nil, any operation that sets the primary selection will also
46 set the clipboard.")
47
48 (defun copy-primary-selection ()
49   "Copy the selection to the Clipboard and the kill ring."
50   (interactive)
51   (and (console-on-window-system-p)
52        (cut-copy-clear-internal 'copy)))
53
54 (defun kill-primary-selection ()
55   "Copy the selection to the Clipboard and the kill ring, then delete it."
56   (interactive "*")
57   (and (console-on-window-system-p)
58        (cut-copy-clear-internal 'cut)))
59
60 (defun delete-primary-selection ()
61   "Delete the selection without copying it to the Clipboard or the kill ring."
62   (interactive "*")
63   (and (console-on-window-system-p)
64        (cut-copy-clear-internal 'clear)))
65
66 (defun yank-clipboard-selection ()
67   "Insert the current Clipboard selection at point."
68   (interactive "*")
69   (case (device-type (selected-device))
70     (x (x-yank-clipboard-selection))
71     (mswindows (mswindows-paste-clipboard))
72     (otherwise nil)))
73
74 (define-device-method get-cutbuffer
75   "Return the value of one of the cut buffers.
76 This will do nothing under anything other than X.")
77
78 (defun get-selection-no-error (&optional type data-type)
79   "Return the value of a Windows selection.
80 The argument TYPE (default `PRIMARY') says which selection,
81 and the argument DATA-TYPE (default `STRING', or `COMPOUND_TEXT' under Mule)
82 says how to convert the data. Returns NIL if there is no selection"
83   (condition-case err (get-selection type data-type) (t nil)))
84
85 (defun get-selection (&optional type data-type)
86   "Return the value of a Windows selection.
87 The argument TYPE (default `PRIMARY') says which selection,
88 and the argument DATA-TYPE (default `STRING', or `COMPOUND_TEXT' under Mule)
89 says how to convert the data. If there is no selection an error is signalled."
90   (or type (setq type 'PRIMARY))
91   (or data-type (setq data-type selected-text-type))
92   (let ((text
93          (if (consp data-type)
94              (condition-case err
95                  (get-selection-internal type (car data-type))
96                (selection-conversion-error
97                 (if (cdr data-type)
98                     (get-selection type (cdr data-type))
99                   (signal (car err) (cdr err)))))
100            (get-selection-internal type data-type))))
101     (when (and (consp text) (symbolp (car text)))
102       (setq text (cdr text)))
103     (when (not (stringp text))
104       (error "Selection is not a string: %S" text))
105     text))
106
107 ;; FSFmacs calls this `x-set-selection', and reverses the
108 ;; arguments (duh ...).  This order is more logical.
109 (defun own-selection (data &optional type)
110   "Make an Windows selection of type TYPE and value DATA.
111 The argument TYPE (default `PRIMARY') says which selection,
112 and DATA specifies the contents.  DATA may be a string,
113 a symbol, an integer (or a cons of two integers or list of two integers).
114
115 The selection may also be a cons of two markers pointing to the same buffer,
116 or an overlay.  In these cases, the selection is considered to be the text
117 between the markers *at whatever time the selection is examined*.
118 Thus, editing done in the buffer after you specify the selection
119 can alter the effective value of the selection.
120
121 The data may also be a vector of valid non-vector selection values.
122
123 Interactively, the text of the region is used as the selection value."
124   (interactive (if (not current-prefix-arg)
125                    (list (read-string "Store text for pasting: "))
126                  (list (substring (region-beginning) (region-end)))))
127   ;FSFmacs huh??  It says:
128   ;; "This is for temporary compatibility with pre-release Emacs 19."
129   ;(if (stringp type)
130   ;    (setq type (intern type)))
131   (or (valid-simple-selection-p data)
132       (and (vectorp data)
133            (let ((valid t)
134                  (i (1- (length data))))
135              (while (>= i 0)
136                (or (valid-simple-selection-p (aref data i))
137                    (setq valid nil))
138                (setq i (1- i)))
139              valid))
140       (signal 'error (list "invalid selection" data)))
141   (or type (setq type 'PRIMARY))
142   (if (null data)
143       (disown-selection-internal type)
144     (own-selection-internal type data)
145     (when (and (eq type 'PRIMARY)
146                selection-sets-clipboard)
147       (own-selection-internal 'CLIPBOARD data)))
148   (cond ((eq type 'PRIMARY)
149          (setq primary-selection-extent
150                (select-make-extent-for-selection
151                 data primary-selection-extent)))
152         ((eq type 'SECONDARY)
153          (setq secondary-selection-extent
154                (select-make-extent-for-selection
155                 data secondary-selection-extent))))
156   (setq zmacs-region-stays t)
157   data)
158
159 (defun dehilight-selection (selection)
160   "for use as a value of `lost-selection-hooks'."
161   (cond ((eq selection 'PRIMARY)
162          (if primary-selection-extent
163              (let ((inhibit-quit t))
164                (if (consp primary-selection-extent)
165                    (mapcar 'delete-extent primary-selection-extent)
166                  (delete-extent primary-selection-extent))
167                (setq primary-selection-extent nil)))
168          (if zmacs-regions (zmacs-deactivate-region)))
169         ((eq selection 'SECONDARY)
170          (if secondary-selection-extent
171              (let ((inhibit-quit t))
172                (if (consp secondary-selection-extent)
173                    (mapcar 'delete-extent secondary-selection-extent)
174                  (delete-extent secondary-selection-extent))
175                (setq secondary-selection-extent nil)))))
176   nil)
177
178 (setq lost-selection-hooks 'dehilight-selection)
179
180 (defun own-clipboard (string)
181   "Paste the given string to the X Clipboard."
182   (own-selection string 'CLIPBOARD))
183
184 (defun disown-selection (&optional secondary-p)
185   "Assuming we own the selection, disown it.  With an argument, discard the
186 secondary selection instead of the primary selection."
187   (disown-selection-internal (if secondary-p 'SECONDARY 'PRIMARY)))
188
189 ;; from x-init.el
190 ;; selections and active regions
191
192 ;; If and only if zmacs-regions is true:
193
194 ;; When a mark is pushed and the region goes into the "active" state, we
195 ;; assert it as the Primary selection.  This causes it to be hilighted.
196 ;; When the region goes into the "inactive" state, we disown the Primary
197 ;; selection, causing the region to be dehilighted.
198
199 ;; Note that it is possible for the region to be in the "active" state
200 ;; and not be hilighted, if it is in the active state and then some other
201 ;; application asserts the selection.  This is probably not a big deal.
202
203 (defun activate-region-as-selection ()
204   (if (marker-buffer (mark-marker t))
205       (own-selection (cons (point-marker t) (mark-marker t)))))
206
207 ; moved from x-select.el
208 (defvar primary-selection-extent nil
209   "The extent of the primary selection; don't use this.")
210
211 (defvar secondary-selection-extent nil
212   "The extent of the secondary selection; don't use this.")
213
214 (defun select-make-extent-for-selection (selection previous-extent)
215   ;; Given a selection, this makes an extent in the buffer which holds that
216   ;; selection, for highlighting purposes.  If the selection isn't associated
217   ;; with a buffer, this does nothing.
218   (let ((buffer nil)
219         (valid (and (extentp previous-extent)
220                     (extent-object previous-extent)
221                     (buffer-live-p (extent-object previous-extent))))
222         start end)
223     (cond ((stringp selection)
224            ;; if we're selecting a string, lose the previous extent used
225            ;; to highlight the selection.
226            (setq valid nil))
227           ((consp selection)
228            (setq start (min (car selection) (cdr selection))
229                  end (max (car selection) (cdr selection))
230                  valid (and valid
231                             (eq (marker-buffer (car selection))
232                                 (extent-object previous-extent)))
233                  buffer (marker-buffer (car selection))))
234           ((extentp selection)
235            (setq start (extent-start-position selection)
236                  end (extent-end-position selection)
237                  valid (and valid
238                             (eq (extent-object selection)
239                                 (extent-object previous-extent)))
240                  buffer (extent-object selection)))
241           (t
242            (signal 'error (list "invalid selection" selection))))
243
244     (if valid
245         nil
246       (condition-case ()
247           (if (listp previous-extent)
248               (mapcar 'delete-extent previous-extent)
249             (delete-extent previous-extent))
250         (error nil)))
251
252     (if (not buffer)
253         ;; string case
254         nil
255       ;; normal case
256       (if valid
257           (set-extent-endpoints previous-extent start end)
258         (setq previous-extent (make-extent start end buffer))
259
260         ;; Make the extent be closed on the right, which means that if
261         ;; characters are inserted exactly at the end of the extent, the
262         ;; extent will grow to cover them.  This is important for shell
263         ;; buffers - suppose one makes a selection, and one end is at
264         ;; point-max.  If the shell produces output, that marker will remain
265         ;; at point-max (its position will increase).  So it's important that
266         ;; the extent exhibit the same behavior, lest the region covered by
267         ;; the extent (the visual indication), and the region between point
268         ;; and mark (the actual selection value) become different!
269         (set-extent-property previous-extent 'end-open nil)
270
271         (cond
272          (mouse-track-rectangle-p
273           (setq previous-extent (list previous-extent))
274           (default-mouse-track-next-move-rect start end previous-extent)
275           ))
276         previous-extent))))
277
278 ;; moved from x-select.el
279 (defun valid-simple-selection-p (data)
280   (or (stringp data)
281       ;FSFmacs huh?? (symbolp data)
282       (integerp data)
283       (and (consp data)
284            (integerp (car data))
285            (or (integerp (cdr data))
286                (and (consp (cdr data))
287                     (integerp (car (cdr data))))))
288       (extentp data)
289       (and (consp data)
290            (markerp (car data))
291            (markerp (cdr data))
292            (marker-buffer (car data))
293            (marker-buffer (cdr data))
294            (eq (marker-buffer (car data))
295                (marker-buffer (cdr data)))
296            (buffer-live-p (marker-buffer (car data)))
297            (buffer-live-p (marker-buffer (cdr data))))))
298
299 (defun cut-copy-clear-internal (mode)
300   (or (memq mode '(cut copy clear)) (error "unkown mode %S" mode))
301   (or (selection-owner-p)
302       (error "XEmacs does not own the primary selection"))
303   (setq last-command nil)
304   (or primary-selection-extent
305       (error "the primary selection is not an extent?"))
306   (save-excursion
307     (let (rect-p b s e)
308       (cond
309        ((consp primary-selection-extent)
310         (setq rect-p t
311               b (extent-object (car primary-selection-extent))
312               s (extent-start-position (car primary-selection-extent))
313               e (extent-end-position (car (reverse primary-selection-extent)))))
314        (t
315         (setq rect-p nil
316               b (extent-object primary-selection-extent)
317               s (extent-start-position primary-selection-extent)
318               e (extent-end-position primary-selection-extent))))
319       (set-buffer b)
320       (cond ((memq mode '(cut copy))
321              (if rect-p
322                  (progn
323                    ;; why is killed-rectangle free?  Is it used somewhere?
324                    ;; should it be defvarred?
325                    (setq killed-rectangle (extract-rectangle s e))
326                    (kill-new (mapconcat #'identity killed-rectangle "\n")))
327                (copy-region-as-kill s e))
328              ;; Maybe killing doesn't own clipboard.  Make sure it happens.
329              ;; This memq is kind of grody, because they might have done it
330              ;; some other way, but owning the clipboard twice in that case
331              ;; wouldn't actually hurt anything.
332              (or (and (consp kill-hooks) (memq 'own-clipboard kill-hooks))
333                  (own-clipboard (car kill-ring)))))
334       (cond ((memq mode '(cut clear))
335              (if rect-p
336                  (delete-rectangle s e)
337                (delete-region s e))))
338       (disown-selection nil)
339       )))
340
341 ;;; Functions to convert the selection into various other selection
342 ;;; types.  Every selection type that emacs handles is implemented
343 ;;; this way, except for TIMESTAMP, which is a special case. These are
344 ;;; all moved from x-select.el
345
346 (defun select-convert-to-text (selection type value)
347   (cond ((stringp value)
348          value)
349         ((extentp value)
350          (save-excursion
351            (set-buffer (extent-object value))
352            (save-restriction
353              (widen)
354              (buffer-substring (extent-start-position value)
355                                (extent-end-position value)))))
356         ((and (consp value)
357               (markerp (car value))
358               (markerp (cdr value)))
359          (or (eq (marker-buffer (car value)) (marker-buffer (cdr value)))
360              (signal 'error
361                      (list "markers must be in the same buffer"
362                            (car value) (cdr value))))
363          (save-excursion
364            (set-buffer (or (marker-buffer (car value))
365                            (error "selection is in a killed buffer")))
366            (save-restriction
367              (widen)
368              (buffer-substring (car value) (cdr value)))))
369         (t nil)))
370
371 (defun select-convert-to-string (selection type value)
372   (let ((outval (select-convert-to-text selection type value)))
373     ;; force the string to be not in Compound Text format.
374     (if (stringp outval)
375         (cons 'STRING outval)
376       outval)))
377
378 (defun select-convert-to-compound-text (selection type value)
379   ;; converts to compound text automatically
380   (select-convert-to-text selection type value))
381
382 (defun select-convert-to-length (selection type value)
383   (let ((value
384          (cond ((stringp value)
385                 (length value))
386                ((extentp value)
387                 (extent-length value))
388                ((and (consp value)
389                      (markerp (car value))
390                      (markerp (cdr value)))
391                 (or (eq (marker-buffer (car value))
392                         (marker-buffer (cdr value)))
393                     (signal 'error
394                             (list "markers must be in the same buffer"
395                                   (car value) (cdr value))))
396                 (abs (- (car value) (cdr value)))))))
397     (if value ; force it to be in 32-bit format.
398         (cons (ash value -16) (logand value 65535))
399       nil)))
400
401 (defun select-convert-to-targets (selection type value)
402   ;; return a vector of atoms, but remove duplicates first.
403   (let* ((all (cons 'TIMESTAMP (mapcar 'car selection-converter-alist)))
404          (rest all))
405     (while rest
406       (cond ((memq (car rest) (cdr rest))
407              (setcdr rest (delq (car rest) (cdr rest))))
408             ((eq (car (cdr rest)) '_EMACS_INTERNAL)  ; shh, it's a secret
409              (setcdr rest (cdr (cdr rest))))
410             (t
411              (setq rest (cdr rest)))))
412     (apply 'vector all)))
413
414 (defun select-convert-to-delete (selection type value)
415   (disown-selection-internal selection)
416   ;; A return value of nil means that we do not know how to do this conversion,
417   ;; and replies with an "error".  A return value of NULL means that we have
418   ;; done the conversion (and any side-effects) but have no value to return.
419   'NULL)
420
421 (defun select-convert-to-filename (selection type value)
422   (cond ((extentp value)
423          (buffer-file-name (or (extent-object value)
424                                (error "selection is in a killed buffer"))))
425         ((and (consp value)
426               (markerp (car value))
427               (markerp (cdr value)))
428          (buffer-file-name (or (marker-buffer (car value))
429                                (error "selection is in a killed buffer"))))
430         (t nil)))
431
432 (defun select-convert-to-charpos (selection type value)
433   (let (a b tmp)
434     (cond ((cond ((extentp value)
435                   (setq a (extent-start-position value)
436                         b (extent-end-position value)))
437                  ((and (consp value)
438                        (markerp (car value))
439                        (markerp (cdr value)))
440                   (setq a (car value)
441                         b (cdr value))))
442            (setq a (1- a) b (1- b)) ; zero-based
443            (if (< b a) (setq tmp a a b b tmp))
444            (cons 'SPAN
445                  (vector (cons (ash a -16) (logand a 65535))
446                          (cons (ash b -16) (logand b 65535))))))))
447
448 (defun select-convert-to-lineno (selection type value)
449   (let (a b buf tmp)
450     (cond ((cond ((extentp value)
451                   (setq buf (extent-object value)
452                         a (extent-start-position value)
453                         b (extent-end-position value)))
454                  ((and (consp value)
455                        (markerp (car value))
456                        (markerp (cdr value)))
457                   (setq a (marker-position (car value))
458                         b (marker-position (cdr value))
459                         buf (marker-buffer (car value)))))
460            (save-excursion
461              (set-buffer buf)
462              (save-restriction
463                (widen)
464                (goto-char a)
465                (beginning-of-line)
466                (setq a (1+ (count-lines 1 (point))))
467                (goto-char b)
468                (beginning-of-line)
469                (setq b (1+ (count-lines 1 (point))))))
470            (if (< b a) (setq tmp a a b b tmp))
471            (cons 'SPAN
472                  (vector (cons (ash a -16) (logand a 65535))
473                          (cons (ash b -16) (logand b 65535))))))))
474
475 (defun select-convert-to-colno (selection type value)
476   (let (a b buf tmp)
477     (cond ((cond ((extentp value)
478                   (setq buf (extent-object value)
479                         a (extent-start-position value)
480                         b (extent-end-position value)))
481                  ((and (consp value)
482                        (markerp (car value))
483                        (markerp (cdr value)))
484                   (setq a (car value)
485                         b (cdr value)
486                         buf (marker-buffer a))))
487            (save-excursion
488              (set-buffer buf)
489              (goto-char a)
490              (setq a (current-column))
491              (goto-char b)
492              (setq b (current-column)))
493            (if (< b a) (setq tmp a a b b tmp))
494            (cons 'SPAN
495                  (vector (cons (ash a -16) (logand a 65535))
496                          (cons (ash b -16) (logand b 65535))))))))
497
498 (defun select-convert-to-sourceloc (selection type value)
499   (let (a b buf file-name tmp)
500     (cond ((cond ((extentp value)
501                   (setq buf (or (extent-object value)
502                                 (error "selection is in a killed buffer"))
503                         a (extent-start-position value)
504                         b (extent-end-position value)
505                         file-name (buffer-file-name buf)))
506                  ((and (consp value)
507                        (markerp (car value))
508                        (markerp (cdr value)))
509                   (setq a (marker-position (car value))
510                         b (marker-position (cdr value))
511                         buf (or (marker-buffer (car value))
512                                 (error "selection is in a killed buffer"))
513                         file-name (buffer-file-name buf))))
514            (save-excursion
515              (set-buffer buf)
516              (save-restriction
517                (widen)
518                (goto-char a)
519                (beginning-of-line)
520                (setq a (1+ (count-lines 1 (point))))
521                (goto-char b)
522                (beginning-of-line)
523                (setq b (1+ (count-lines 1 (point))))))
524            (if (< b a) (setq tmp a a b b tmp))
525            (format "%s:%d" file-name a)))))
526
527 (defun select-convert-to-os (selection type size)
528   (symbol-name system-type))
529
530 (defun select-convert-to-host (selection type size)
531   (system-name))
532
533 (defun select-convert-to-user (selection type size)
534   (user-full-name))
535
536 (defun select-convert-to-class (selection type size)
537   x-emacs-application-class)
538
539 ;; We do not try to determine the name Emacs was invoked with,
540 ;; because it is not clean for a program's behavior to depend on that.
541 (defun select-convert-to-name (selection type size)
542   ;invocation-name
543   "xemacs")
544
545 (defun select-convert-to-integer (selection type value)
546   (and (integerp value)
547        (cons (ash value -16) (logand value 65535))))
548
549 (defun select-convert-to-atom (selection type value)
550   (and (symbolp value) value))
551
552 (defun select-convert-to-identity (selection type value) ; used internally
553   (vector value))
554
555 (setq selection-converter-alist
556       '((TEXT . select-convert-to-text)
557         (STRING . select-convert-to-string)
558         (COMPOUND_TEXT . select-convert-to-compound-text)
559         (TARGETS . select-convert-to-targets)
560         (LENGTH . select-convert-to-length)
561         (DELETE . select-convert-to-delete)
562         (FILE_NAME . select-convert-to-filename)
563         (CHARACTER_POSITION . select-convert-to-charpos)
564         (SOURCE_LOC . select-convert-to-sourceloc)
565         (LINE_NUMBER . select-convert-to-lineno)
566         (COLUMN_NUMBER . select-convert-to-colno)
567         (OWNER_OS . select-convert-to-os)
568         (HOST_NAME . select-convert-to-host)
569         (USER . select-convert-to-user)
570         (CLASS . select-convert-to-class)
571         (NAME . select-convert-to-name)
572         (ATOM . select-convert-to-atom)
573         (INTEGER . select-convert-to-integer)
574         (_EMACS_INTERNAL . select-convert-to-identity)
575         ))
576
577 ;;; select.el ends here