unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
From: "Drew Adams" <drew.adams@oracle.com>
To: "'Stefan Monnier'" <monnier@iro.umontreal.ca>
Cc: 'Ehud Karni' <ehud@unix.mvs.co.il>,
	'David De La Harpe Golden' <david.delaharpe.golden@gmail.com>,
	'Emacs-Devel' <emacs-devel@gnu.org>
Subject: RE: yank-secondary
Date: Tue, 6 May 2008 19:40:17 -0700	[thread overview]
Message-ID: <005501c8afeb$afe7ab70$0200a8c0@us.oracle.com> (raw)
In-Reply-To: <jwv7ie6j2ur.fsf-monnier+emacs@gnu.org>

> > Dunno if anyone tried the code I sent, but I've
> > since combined the three commands in one,
> > `yank-secondary-or-swap-w-region':
> 
> > * no prefix arg:    yank-secondary
> > * prefix arg >= 0:  primary-to-secondary
> > * prefix arg < 0:   secondary-to-primary
> 
> Why not just either yank and swap primary<->secondary?
> Also it can just be called secondary-dwim.

I don't know what you're suggesting. I don't know what you mean here by
"either...and" and by "swap primary<->secondary".

Here is the code I have, so you can see more clearly what I meant. Let me know
more precisely what you have in mind. I have no problem with renaming commands
or other changes.

Note that `primary-to-secondary' and `secondary-to-primary' are not exact
opposites. The first puts the secondary selection (also) on the (current
buffer's) region text. The second pops to the buffer that has the secondary
selection and selects that text as the active region.

The first is more useful than the second. In particular, it indirectly gives you
different ways to create the secondary selection using the keyboard. For
example, you can use `C-M-@' to mark a sexp and then `C-u C-M-y' to select it as
the secondary (assuming that `C-M-y' is bound to
`yank-secondary-or-swap-w-region ').

Yanking the secondary is the most useful of the three, which is why it needs no
prefix arg. It is particularly useful with delete-selection mode, although I
just started using it that way recently.

(defun yank-secondary-or-swap-w-region (arg) ; I bind it to `C-M-y'
  "Insert the secondary selection at point.  Prefix arg: swap with region.
Move point to the end of the inserted text.  Leave mark where it was.
With a prefix arg, swap the region and secondary selections instead,
in this way:
 Arg >= 0: Make the region into the secondary selection.
 Arg <  0: Convert the secondary selection into the region."
  (interactive "P")
  (cond (arg
         (setq arg (prefix-numeric-value arg))
         (if (wholenump arg)
             (call-interactively #'primary-to-secondary)
           (call-interactively #'secondary-to-primary)))
        (t
         (setq this-command 'yank-secondary)
         (when delete-selection-mode
           (delete-selection-pre-hook)) ; Hack!
         (call-interactively #'yank-secondary))))

(defun primary-to-secondary (beg end)
  "Make the region in the current buffer into the secondary selection."
  (interactive "r")
  (setq mouse-secondary-start (make-marker))
  (set-marker mouse-secondary-start beg)
  (if mouse-secondary-overlay
      (move-overlay mouse-secondary-overlay 
                    beg end (current-buffer))
    (setq mouse-secondary-overlay
          (make-overlay beg end (current-buffer)))
    (overlay-put mouse-secondary-overlay
                 'face 'secondary-selection))
  (x-set-selection 'SECONDARY (buffer-substring beg end)))

(defun secondary-to-primary ()
  "Convert the secondary selection into the active region.
Select the secondary selection and pop to its buffer."
  (interactive)
  (let ((secondary (x-get-selection 'SECONDARY)))
    (unless (and secondary mouse-secondary-overlay)
      (error "No secondary selection"))
    (x-set-selection 'PRIMARY secondary))
  (pop-to-buffer (overlay-buffer mouse-secondary-overlay))
  (push-mark (overlay-start mouse-secondary-overlay) t t)
  (goto-char (overlay-end mouse-secondary-overlay))
  (setq deactivate-mark nil))

(defun yank-secondary ()
  "Insert the secondary selection at point.
Moves point to the end of the inserted text.  Does not change mark."
  (interactive)
  (let ((secondary (x-get-selection 'SECONDARY)))
    (unless secondary (error "No secondary selection"))
    (insert secondary)))

;; Tell `delete-selection-mode' to replace active region
;; by yanked secondary selection.
(put 'yank-secondary 'delete-selection 'yank)

----------------

FWIW, this is the code I'm using for the Edit menu.

As mentioned in thread "x-selection-exists-p vs  x-get-selection", I use
`x-get-selection' instead of `x-selection-exists-p' for the `SECONDARY' because
the latter doesn't seem to work, at least on Windows. I think that is a bug, but
I never got a clear confirmation. I use `x-selection-enable-clipboard' also.

(when (fboundp 'yank-secondary)
  (define-key-after menu-bar-edit-menu [yank-secondary]
    '(menu-item "Paste Secondary" yank-secondary
      :help "Paste secondary selection."
      :enable
      (and (fboundp 'x-get-selection)
           (x-get-selection 'SECONDARY)
           (not buffer-read-only))
      :keys "\\[yank-secondary-or-swap-w-region]")
    'paste)
  (define-key-after menu-bar-edit-menu [primary-to-secondary]
    '(menu-item "Region to Secondary" primary-to-secondary
      :help "Make the region into the secondary selection."
      :enable mark-active
      :keys "C-1 \\[yank-secondary-or-swap-w-region]")
    'yank-secondary)
  (define-key-after menu-bar-edit-menu [secondary-to-primary]
    '(menu-item "Secondary to Region" secondary-to-primary
      :help "Convert the secondary selection into the active region."
      :enable (and (fboundp 'x-get-selection)
                   (x-get-selection 'SECONDARY))
      :keys "C-- 1 \\[yank-secondary-or-swap-w-region]")
    'primary-to-secondary))





  reply	other threads:[~2008-05-07  2:40 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-03-11 18:59 FW: yank-secondary Drew Adams
2008-05-04 23:15 ` yank-secondary Drew Adams
2008-05-06 15:54   ` yank-secondary Drew Adams
2008-05-07  1:55     ` yank-secondary Stefan Monnier
2008-05-07  2:40       ` Drew Adams [this message]
2008-05-07  3:15         ` yank-secondary Stefan Monnier
2008-05-07  6:27           ` yank-secondary Drew Adams
2008-05-07 15:01             ` yank-secondary Stefan Monnier
  -- strict thread matches above, loose matches on Subject: below --
2008-01-30 21:41 yank-secondary Drew Adams

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='005501c8afeb$afe7ab70$0200a8c0@us.oracle.com' \
    --to=drew.adams@oracle.com \
    --cc=david.delaharpe.golden@gmail.com \
    --cc=ehud@unix.mvs.co.il \
    --cc=emacs-devel@gnu.org \
    --cc=monnier@iro.umontreal.ca \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).