emacs-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: yank-secondary


From: Drew Adams
Subject: RE: yank-secondary
Date: Tue, 6 May 2008 19:40:17 -0700

> > 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 via email to

[Prev in Thread] Current Thread [Next in Thread]