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