Search code examples
macosemacsdot-emacsemacs24

Shift-selection and emacs 24


I use emacs 24 (OSX) and have a problem with shift-selection. If I select a region with my mouse, the text is highlighted and automaticaly saved on to the kill ring (I can yank it immediately). With shift-selection, the text is highlighted but I have to manualy save the text (M-w) to be able to yank it. Is there any way that shift-selection hightlights the text AND saves it onto the kill ring ?


Solution

  • Selecting text with the mouse does not place anything onto the kill ring by default (it copies it to the X clipboard).

    There are various ways in which you can customise how Emacs interacts with the clipboard. See:

    C-hig (emacs) Cut and Paste RET

    Is there any way that shift-selection hightlights the text AND saves it onto the kill ring ?

    I'm not sure that there's any way to detect that you've stopped selecting things, but you could perhaps advise handle-shift-selection to set a temporary post-command-hook to run a custom function to place the region in the clipboard (and remove the post-command-hook).

    Edit: Hang on, what you describe is exactly what happens for me in Emacs 24.1 on Ubuntu.

    If I shift-select some text in Emacs and then middle-click the mouse in any application, I paste the selected text.

    Try testing when running emacs -Q

    Edit 2: Ah, but you didn't mean the mouse, did you?

    How about this?

    (defvar my-shift-selection-in-progress nil)
    (make-variable-buffer-local 'my-shift-selection-in-progress)
    
    (defadvice handle-shift-selection
      (before my-shift-selection-to-kill-ring-advice)
      "Automatically copy shift-selected text to the kill ring.
    
    If `interprogram-cut-function' is non-nil, also save the text for a window
    system cut and paste.
    
    See `my-shift-selection-to-kill-ring'."
      (when (and shift-select-mode
                 this-command-keys-shift-translated
                 (not my-shift-selection-in-progress))
        (add-hook 'post-command-hook 'my-shift-selection-to-kill-ring nil t)))
    
    (ad-activate 'handle-shift-selection)
    
    (defun my-shift-selection-to-kill-ring ()
      "Post-command callback for my-shift-selection-to-kill-ring-advice."
      (if this-command-keys-shift-translated
          (kill-new (filter-buffer-substring (region-beginning) (region-end))
                    my-shift-selection-in-progress)
        (remove-hook 'post-command-hook 'my-shift-selection-to-kill-ring t))
      (setq my-shift-selection-in-progress this-command-keys-shift-translated))