1 ;;; xwem-selections.el --- Support for X selections.
3 ;; Copyright (C) 2004,2005 by XWEM Org.
5 ;; Author: Zajcev Evgeny <zevlg@yandex.ru>
6 ;; Created: Wed May 5 17:06:41 MSD 2004
8 ;; X-CVS: $Id: xwem-selections.el,v 1.5 2005-04-04 19:54:15 lg Exp $
10 ;; This file is part of XWEM.
12 ;; XWEM 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)
17 ;; XWEM is distributed in the hope that it will be useful, but WITHOUT
18 ;; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
19 ;; or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
20 ;; License for more details.
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 Free
24 ;; Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
27 ;;; Synched up with: Not in FSF
38 (defgroup xwem-selections nil
39 "Group to customize `xwem-selections'."
40 :prefix "xwem-selections-"
43 (defcustom xwem-selections-maximum 20
44 "Maximum number of saved selections."
46 :group 'xwem-selections)
48 (defcustom xwem-selections-no-remove t
49 "*Non-nil mean, reverse meaning of prefix arg for `xwem-paste-cutbuffer' command.
50 Without prefix arg, keep currently pasted cutbuffer.
51 With prefix arg, remove it."
53 :group 'xwem-selections)
55 ;;; Internal variables
58 (defvar xwem-selections nil
59 "Ring of saved selections.
62 (defvar xwem-selection-xwin nil
63 "X-Win used to operate on selections.")
65 (defun xwem-init-selections ()
66 "Initialize selections mechanism."
67 (setq xwem-selection-xwin
68 (XCreateWindow (xwem-dpy) (xwem-rootwin)
70 (make-X-Attr :override-redirect t
71 :event-mask (Xmask-or XM-StructureNotify))))
73 (X-Win-EventHandler-add-new xwem-selection-xwin 'xwem-selection-get nil
74 (list X-SelectionNotify))
77 (defun xwem-selection-req (sel &optional targ prop)
78 "Issue XConvertSelection."
80 (setq targ XA-string))
82 (XConvertSelection (xwem-dpy)
83 (if (X-Atom-p sel) sel (XInternAtom (xwem-dpy) sel t))
84 (if (X-Atom-p targ) targ (XInternAtom (xwem-dpy) targ t))
86 (xwem-dpy) (or prop "XWEM_SELECTION_PROPERTY") t)
90 (defun xwem-selection-get (xdpy xwin xev)
91 "On display XDPY and window XWIN process SelectionNotify event XEV."
92 (xwem-debug 'xwem-misc "here prop=%d"
93 '(X-Atom-id (X-Event-xselection-property xev)))
95 (if (not (= (X-Atom-id (X-Event-xselection-property xev)) X-None))
97 (if (X-Atom-equal (X-Event-xselection-target xev)
98 (XInternAtom xdpy "XA_TARGETS" t))
100 (setq target (X-Event-xselection-target xev)))
102 (setq prov (XGetWindowProperty
103 xdpy (X-Event-xselection-requestor xev)
104 (X-Event-xselection-property xev) nil nil nil target))
106 (xwem-debug 'xwem-misc "Got prov=%S, prop=%S target=%S"
107 'prov '(X-Atom-id (X-Event-xselection-property xev))
108 '(X-Atom-id target)))
111 ;;;###autoload(autoload 'xwem-help-cutbuffers "xwem-selections" "Display help about cutbuffers." t)
112 (define-xwem-command xwem-help-cutbuffers ()
113 "Show help buffer about cutbuffers."
116 (xwem-help-display "cutbuffers"
117 (insert "X cutbuffers:\n\n")
118 (insert "NUMBER VALUE\n")
119 (insert "------ -----\n")
120 (insert (format "%-9s%S\n" 'PRIMARY (get-selection)))
122 (let ((cbval (x-get-cutbuffer n)))
124 (insert (format "%-9d%S\n" n cbval)))))
129 (insert "XWEM selections:\n\n")
130 (insert "NUMBER VALUE\n")
131 (insert "------ -----\n")
134 (insert (format "%-9d%S\n" nsel s))
138 ;;;###autoload(autoload 'xwem-copy-cutbuffer "xwem-selections" "Copy CUTBUFFER0 to `xwem-selections'." t)
139 (define-xwem-command xwem-copy-cutbuffer (&optional which-one)
140 "Copy WHICH-ONE cutbuffer to `xwem-selections'.
141 However if Emacs region activated, region is copied instead of
143 (xwem-interactive "p")
145 (if (region-active-p)
146 (xwem-copy-region-as-cutbuffer)
149 (let ((cb0 (condition-case nil
151 (t (x-get-cutbuffer which-one)))))
153 (xwem-message 'note "No active selection")
154 (push cb0 xwem-selections)
155 (xwem-message 'info "Copying %S" cb0)))))
157 ;;;###autoload(autoload 'xwem-paste-cutbuffer "xwem-selections" "Paste CUTBUFFER0 to `xwem-selections'." t)
158 (define-xwem-command xwem-paste-cutbuffer (&optional no-remove)
159 "Paste's most recent cutbuffer from `xwem-selections' to selected client.
160 cutbuffer is removed from `xwem-selections', unless NO-REMOVE is non-nil.
161 However if `xwem-selections-no-remove' is non-nil, NO-REMOVE have
163 (xwem-interactive "_P")
165 (let ((sidx (or (and (numberp no-remove)
169 (when (> sidx (1- (length xwem-selections)))
170 (error 'xwem-error (format "No %d selection" sidx)))
172 (setq sel (nth sidx xwem-selections))
173 (xwem-kbd-force-mods-release)
174 (mapc 'xwem-unread-command-event sel)
176 ;; Remove SEL from `xwem-selections'?
177 (setq no-remove (and no-remove (listp no-remove)))
178 (unless (or (and xwem-selections-no-remove
180 (and (not xwem-selections-no-remove)
182 (setq xwem-selections (delq sel xwem-selections)))))
184 ;;;###autoload(autoload 'xwem-copy-region-as-cutbuffer "xwem-selections" "Copy region to `xwem-selections'." t)
185 (define-xwem-command xwem-copy-region-as-cutbuffer ()
186 "Copy selected region to `xwem-selections' as ordinary cutbuffer."
189 (unless (region-active-p)
190 (error 'xwem-error "No active region"))
192 (let ((rr (buffer-substring (region-beginning) (region-end))))
193 (push rr xwem-selections)
194 (xwem-message 'info "Copying: %S" rr)))
197 (provide 'xwem-selections)
199 ;;; xwem-selections.el ends here