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