Subject: Re: bug#902: Acknowledgement (select-active-regions only half-working)

From: David De La Harpe Golden <[email-masked]>
Date: Sat, 06 Sep 2008 20:35:29 +0100

Uh.  Hashing is probably silly/pointless in this case (wrote that far
too early in the morning...).  Revised patch attached, just saves last
region and uses string-equal.

Index: lisp/simple.el
===================================================================
RCS file: /sources/emacs/emacs/lisp/simple.el,v
retrieving revision 1.945
diff -U 8 -r1.945 simple.el
--- lisp/simple.el	15 Aug 2008 00:30:44 -0000	1.945
+++ lisp/simple.el	6 Sep 2008 19:33:00 -0000
@@ -3439,18 +3439,45 @@
 
 (defun activate-mark ()
   "Activate the mark."
   (when (mark t)
     (setq mark-active t)
     (unless transient-mark-mode
       (setq transient-mark-mode 'lambda))))
 
+(defvar select-active-regions-timer nil)
+
+(defvar select-active-regions-last-region nil)
+
+(defun select-active-regions-maybe-set-selection ()
+  "Implements `select-active-regions', called from idle timer
+`select-active-regions-timer'"
+  (and select-active-regions
+       (region-active-p)
+       (> (region-end) (region-beginning))
+       (if (or (not select-active-regions-last-region)
+	       (x-selection-owner-p nil))
+	   (let ((latest-region
+		  (buffer-substring (region-beginning) (region-end))))
+	     (unless (string-equal latest-region
+				   select-active-regions-last-region)
+	       (x-set-selection
+		nil latest-region)
+	       (setq select-active-regions-last-region
+		     latest-region)))
+	 (setq select-active-regions-last-region nil))))
+
 (defcustom select-active-regions nil
-  "If non-nil, an active region automatically becomes the window selection."
+  "If non-nil, an active region automatically becomes the window selection.
+
+In conjunction with this, you might want to:
+rebind mouse-2 to `mouse-yank-primary', set `x-select-enable-primary' to nil,
+set `x-select-enable-clipboard' to non-nil, set `mouse-drag-copy-region'
+to nil, and turn on `transient-mark-mode'."
   :type 'boolean
   :group 'killing
   :version "23.1")
 
 (defun set-mark (pos)
   "Set this buffer's mark to POS.  Don't use this function!
 That is to say, don't use this function unless you want
 the user to see that the mark has moved, and you want the previous
@@ -3466,19 +3493,26 @@
 store it in a Lisp variable.  Example:
 
    (let ((beg (point))) (forward-line 1) (delete-region beg (point)))."
 
   (if pos
       (progn
 	(setq mark-active t)
 	(run-hooks 'activate-mark-hook)
-	(and select-active-regions
-	     (x-set-selection
-	      nil (buffer-substring (region-beginning) (region-end))))
+	(if select-active-regions
+	    (progn
+	      (unless select-active-regions-timer
+		(setq select-active-regions-timer
+		      (run-with-idle-timer
+		       0.1 t 'select-active-regions-maybe-set-selection)))
+	      (select-active-regions-maybe-set-selection))
+	  (when select-active-regions-timer
+	    (cancel-timer select-active-regions-timer)
+	    (setq select-active-regions-timer nil)))
 	(set-marker (mark-marker) pos (current-buffer)))
     ;; Normally we never clear mark-active except in Transient Mark mode.
     ;; But when we actually clear out the mark value too,
     ;; we must clear mark-active in any mode.
     (setq mark-active nil)
     (run-hooks 'deactivate-mark-hook)
     (set-marker (mark-marker) nil)))