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

From: Stefan Monnier <[email-masked]>
Date: Tue, 09 Sep 2008 10:50:05 -0400

>> Sorry, don't mind me, I was completely confused.
> No worries.  Anyway, there's probably a much more elegant way:

> (Background: I «gasp» read the docstring for x-set-selection, and
> _thought_ I'd found a better way - it can take a cons of markers
> to _lazily_ find the selection data as whatever's between
> the markers when something requests the selection. However, it turns out
> that the emacs point is _not_ in fact a marker, so you can't use
> mark-marker and point-marker to find the region on-demand (point-marker
> just returns a marker to the instantaneous position of the point))

> *** Sooo - Here's a solution that seems generally saner, though does
> wander deeper into the emacs core - allow x-set-selection to take a
> function that will be funcalled on demand to return a string to use as
> the selection data, not just a cons of markers.

> Avoids performance issues that the moronic string-equal or hash in the
> timer would introduce, and the (theoretical, for inhumanly fast users)
> potential flakiness of an idle timer.

Sounds good on the surface [ I don't know the insides]

> +      ;; no real guarantee that an impure function that returns
> +      ;; a string now will always do so, but might as well
> +      ;; try it out, for early failure.
> +      (and (functionp data)
> +	   (stringp (funcall data)))))

I wouldn't worry/care about checking the return value here.
 
> +	  ((functionp value)
> +	   (let ((ret (funcall value)))
> +	     (if (stringp ret)
> +		 (setq str ret)
> +	       (signal 'error
> +		       (list "selection function must return string"
> +			     value ret))))))

Please move this code to an auxiliary function, since it's
repeated twice.
 
> +    (and select-active-regions
> +	 (x-set-selection
> +	  nil (lambda ()
> +		(if (< (region-beginning) (region-end))
> +		    (buffer-substring (region-beginning) (region-end))
> +		  ""))))))

You should probably save the current buffer in some variable (current at
the time of the x-set-selection) and restore it when the lambda is
called.  Also, give a name to this function, since it's used at
least twice.

An alternative is to use not a function but a buffer (which would mean
"use the region's content, if active").

        Stefan