help-gnu-emacs
[Top][All Lists]
Advanced

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

Saving the selection before killing (was: Selection not to be copied int


From: Stefan Monnier
Subject: Saving the selection before killing (was: Selection not to be copied into kill-ring)
Date: Thu, 12 Jul 2007 16:31:26 -0400
User-agent: Gnus/5.11 (Gnus v5.11) Emacs/22.1.50 (gnu/linux)

>>>>> "Eli" == Eli Zaretskii <address@hidden> writes:
>> From: Will <address@hidden>
>> Newsgroups: gnu.emacs.help
>> Date: Thu, 12 Jul 2007 14:14:01 +0200
>> 
>> I'd like to copy text from external applications (e.g. web browser) to 
>> an Emacs buffer. Often before I paste the text into an Emacs buffer I'd 
>> like to delete text in the this buffer _without_ it being copied to the 
>> kill-ring, because then the text from the external application gets lost.
>> 
>> => How do I switch off that hightlighted text is copied automatially 
>> into the kill-ring?
>> => How do I delete a region without it getting copied into the 
>> kill-ring, e.g. by highlighting the text and pressing <delete>?

> Try "M-x delete-selection-mode RET".  Is this what you want?

I bump into the OP's problem every once in a while, but I don't like
delete-selection-mode for some reason, so I use the patch below instead.
What it does is that when you kill text, before doing the kill (which will
replace the current X-selection with the killed text), we save the current
selection on the kill-ring (only if it doesn't come from us, of course
since otherwise it's already in the kill-ring).  So I can simply do my kill
and then C-y (which re-inserts what I just killed) and M-y (which replaces
the text with the previous X-selection).

This feature is pretty unnoticeable, so I'm tempted to install it just like
that, but maybe people want yet-another-config-var to control it?


        Stefan


--- orig/lisp/simple.el
+++ mod/lisp/simple.el
@@ -2479,6 +2536,20 @@
 argument is not used by `insert-for-yank'.  However, since Lisp code
 may access and use elements from the kill ring directly, the STRING
 argument should still be a \"useful\" string for such uses."
+  ;; To better pretend that X-selection = head-of-kill-ring, we copy other
+  ;; application's X-selection to the kill-ring.  This comes in handy when
+  ;; you do something like:
+  ;; - copy a piece of text in your web-browser.
+  ;; - have to do some editing (including killing) before you can yank
+  ;;   that text.
+  ;; Note: this piece of code inspired from current-kill.
+  (let ((paste (and interprogram-paste-function
+                    (funcall interprogram-paste-function))))
+    (when paste
+      (let ((interprogram-cut-function nil)
+            (interprogram-paste-function nil))
+        (kill-new paste))))
+  ;; The actual kill-new functionality.
   (if (> (length string) 0)
       (if yank-handler
          (put-text-property 0 (length string)


reply via email to

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