Subject: bug#902 closed by Chong Yidong <[email-masked]> (Re: select-active-regions only half-working)

From: [email-masked] (Emacs bug Tracking System)
Date: Wed, 15 Jul 2009 01:40:05 +0000

This is an automatic notification regarding your bug report
which was filed against the emacs package:

#902: select-active-regions only half-working

It has been closed by Chong Yidong <[email-masked]>.

Their explanation is attached below along with your original report.
If this explanation is unsatisfactory and you have not received a
better one in a separate message then please contact Chong Yidong <[email-masked]> by
replying to this email.

-- 
902: http://debbugs.gnu.org/cgi/bugreport.cgi?bug=902
Emacs Bug Tracking System
Contact [email-masked] with problems

Subject: Re: select-active-regions only half-working
From: Chong Yidong <[email-masked]>
To: David De La Harpe Golden <[email-masked]>
Cc: [email-masked]
Date: Tue, 14 Jul 2009 21:31:04 -0400

Hi David,

I've checked a revamped version of your patch into the trunk.  Thanks
very much for figuring out the details for making this work.
>From [email-masked] Fri Sep  5 22:53:40 2008
Date: Sat, 06 Sep 2008 06:53:28 +0100
From: David De La Harpe Golden <[email-masked]>
To: [email-masked]
Subject: select-active-regions only half-working

This is a multi-part message in MIME format.
--------------060103000303040901090207
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit

Package: emacs
Version: 23.0.60
Severity: normal
Tags: patch

The select-active-regions in-tree does not work in all cases - it works
when the region is changed by moving the mark but not when it is changed
by certain other means e.g. moving the point with the keyboard.
(Workaround:  C-x C-x C-x C-x ...).

There were a couple of approaches tried a while back (see emacs-devel
"Improved X Selections" thread).  The idle timer seemed to work best in
practice, despite (or because of) "coarse graining" i.e. a bunch of
small region changes in a row don't reupdate the selection for each
change, only kicks in when emacs has idled.

Attached patch is a simple idle-timer based implementation.

Concerns here may also apply to the activate-mark-hook (which states
in its documentation that it is rerun when region changes). Having that
work similarly with the same method would probably mean that
active region timer would have to be enabled whenever the mark is
active, not just when select-active-regions is on.

--------------060103000303040901090207
Content-Type: text/x-patch;
 name="select-active-regions-by-idle-timer.diff"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
 filename="select-active-regions-by-idle-timer.diff"

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 05:41:14 -0000
@@ -3439,18 +3439,46 @@
 
 (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-hash 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-hash)
+	       (x-selection-owner-p nil))
+	   (let ((region-hash
+		  (md5 (current-buffer) (region-beginning)
+		       (region-end) nil t)))
+	     (unless (string-equal region-hash
+				   select-active-regions-last-region-hash)
+	       (x-set-selection
+		nil (buffer-substring (region-beginning) (region-end)))
+	       (setq select-active-regions-last-region-hash
+		     region-hash)))
+	 (setq select-active-regions-last-region-hash 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 +3494,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)))

--------------060103000303040901090207
Content-Type: text/plain;
 name="ChangeLog.select-active-regions-by-idle-timer"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
 filename="ChangeLog.select-active-regions-by-idle-timer"

2008-09-06 David De La Harpe Golden <[email-masked]>

	* simple.el: idle-timer implementation of select-active-regions

--------------060103000303040901090207--