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: Sun, 4 May 2008 16:15:00 -0700

Resending (see below). There was some interest, but not a deluge of response.
This is an update, with some new commands.

David D.L.H.G mentioned that there is no way to create the secondary selection
by keyboard, and that he typically is already using the mouse when he wants to
yank the secondary selection.

That's not my experience. I often yank the secondary selection long after I've
created it. In fact, I typically use the same secondary selection over a period
of editing, whereas I change the primary (region) selection often. That's the
beauty of the secondary selection, to me: it persists even when the region
changes. You can use registers in a similar way, but I find the secondary
selection handy for this.

But what about having a way to create the secondary selection by keyboard? Good
idea. I don't think it's helpful or necessary to duplicate the many ways that
one can define the region, just to do the same for the secondary selection. You
don't need 36 ways to define that.

But it would be useful to be able to create the secondary selection from the
region. That then gives you all of the many region creation methods for free.

I now bind not `yank-secondary' (which I sent previously) but a command that
does the same thing but also lets you use `C-u' to instead create the secondary
selection from the region. I bind it to `C-M-y'.

(defun yank-secondary-or-convert-primary (arg)
  "Insert the secondary selection at point.
Move point to the end of the inserted text.  Does not change mark.
With a prefix arg, make the region into the secondary selection,
instead."
  (interactive "P")
  (if arg
      (call-interactively #'primary-to-secondary)
    (setq this-command 'yank-secondary)
    (when delete-selection-mode   ; Hack!
      (delete-selection-pre-hook))
    (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)
    (setq mouse-secondary-overlay (make-overlay beg end))
    (overlay-put mouse-secondary-overlay
      'face 'secondary-selection))
  (x-set-selection 'SECONDARY (buffer-substring beg end)))

IOW, I use `C-M-y' to yank the secondary and `C-u C-M-y' to copy the region text
to the secondary (it still leaves the region active). That's the right way to
create the secondary from the keyboard, I think.

There was also some question of how yanking should interact with
delete-selection mode. I now let it delete the region text, and I find that that
is quite useful. IOW, `C-M-y' with an active region replaces the region text
with the secondary selection. 

Among other things, that provides a handy way to replace text here and there,
without monkeying with the kill-ring order (`M-y'). I also use it sometimes with
isearch the same way, as a way to replace particular search hits on the fly.
(`C-SPC' in my version of isearch-map is a toggle that sets the region around
the search hit when you exit isearch, which is handy here.)

The hack in the `yank-secondary-or-convert-primary' code shows how I make
delete-selection mode DTRT. I didn't find a better way, since delete-selection
mode looks only at the current key's command. Suggestions welcome.

FWIW, I also have a command, `secondary-to-primary', to pop to the buffer of the
secondary selection and select it as the region. It's sort of the opposite of
`primary-to-secondary'. I have not bound it to a key.

(defun secondary-to-primary ()
  "Converts the secondary selection into the active region.
Selects the secondary selection and its buffer in another window."
  (interactive)
  (let ((secondary (x-get-selection 'SECONDARY)))
    (unless (and secondary mouse-secondary-overlay)
      (error "No secondary selection"))
    (x-set-selection 'PRIMARY secondary))
  (switch-to-buffer-other-window
    (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))

You might give `yank-secondary-or-convert-primary' a try, to see if it's
something that Emacs might benefit from.

Here again is `yank-secondary' (used by `yank-secondary-or-convert-primary'):

(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)))


-------------8<--------------------
> From: Drew Adams Sent: Tuesday, March 11, 2008 11:59 AM
> Resending. Questions:
>  
> 1) Any interest? 
> 2) Do you yank the secondary selection (without the mouse)? 
> If so, how?
> 
> -------------
> Sent: Wednesday, January 30, 2008 1:41 PM
> Any interest in adding something like this? I've used it for 
> years. Hard to
> believe it isn't already in Emacs (or is it?). (What do those 
> of you who are sans souris use?)
> 
> (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)))
> 
> I bind it to `C-M-y', so I can access two different 
> selections from the keyboard.
> 
> (FWIW, I also bind the meta mouse stuff to `C-M-', so 
> mouse-yank-secondary and yank-secondary use the same
> modifiers. I've done that since I had an SGI workstation
> that wouldn't pass ALT-mouse stuff to Emacs. So, e.g., I
> have `C-M-y' = yank-secondary, `C-M-mouse2' = 
> mouse-yank-secondary, `C-M-mouse-1'
> = mouse-start-secondary, `C-M-mouse-3' = 
> mouse-secondary-save-then-kill,
> `C-M-drag-mouse-1' = mouse-set-secondary, and
> `C-M-down-mouse-1' = mouse-drag-secondary.)





reply via email to

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