From xemacs-m  Sat Jan  4 22:21:51 1997
Received: from steadfast.teradyne.com (steadfast.teradyne.com [131.101.1.200])
          by xemacs.cs.uiuc.edu (8.8.4/8.8.4) with ESMTP
	  id WAA09747 for <xemacs-beta@xemacs.org>; Sat, 4 Jan 1997 22:21:50 -0600 (CST)
Received: from kiki.icd.teradyne.com (kiki.icd.teradyne.com [131.101.1.30]) by steadfast.teradyne.com (8.7.1/8.7.1) with ESMTP id XAA19363 for <xemacs-beta@xemacs.org>; Sat, 4 Jan 1997 23:24:46 -0500 (EST)
Received: from rafikki.icd.teradyne.com (rafikki.icd.teradyne.com [131.101.10.38]) by kiki.icd.teradyne.com (8.7.1/8.7.1) with SMTP id XAA28615 for <xemacs-beta@xemacs.org>; Sat, 4 Jan 1997 23:19:19 -0500 (EST)
Received: by rafikki.icd.teradyne.com (SMI-8.6/SMI-SVR4)
	id XAA03722; Sat, 4 Jan 1997 23:18:52 -0500
Sender: shelton@icd.teradyne.com
Newsgroups: comp.emacs.xemacs
cc: xemacs-beta@xemacs.org
Subject: Re: mouse-track-adjust
References: <545681dgivm.fsf@icd.teradyne.com>
From: Vinnie Shelton  <shelton@icd.teradyne.com>
Date: 04 Jan 1997 23:18:50 -0500
Message-ID: <545iv5chhzp.fsf@icd.teradyne.com>
Organization: Teradyne, Inc. Boston MA
Lines: 82
X-Newsreader: Red Gnus v0.76/XEmacs 19.15
In-Reply-To: Vinnie Shelton's message of 04 Jan 1997 17:45:01 -0500
Posted-To: comp.emacs.xemacs

The following message is a courtesy copy of an article
that has been posted as well.

>>>>> On 04 Jan 1997 I <shelton@icd.teradyne.com> said:

Vin> But, as the comment indicates, I'm dissatisfied with the way this
Vin> works.  I'd like a much more general way of doing this.  I'd like to
Vin> create a function which would install the proper handler to do this,
Vin> something like this:

Vin>   (extend-and-cut-selection KEY NUMBER_OF_CLICKS)

Vin> This would take a keysym and a number of clicks, and any newbie could
Vin> use it like so:

Vin>   (extend-and-cut-selection '(ctrl meta hyper super shift button3) 3)

Vin> Writing this function and the hook it installs have proven to be
Vin> difficult:

Vin> 1) In the hook function I can figure out what modifiers have been
Vin>    depressed by using event-modifier-bits, which returns a bitmask.
Vin>    How can I figure out what the bitmask associated with a particular
Vin>    keysym is?  Alternatively, I can use event-modifiers, which returns
Vin>    something like '(shift ctrl), but then I think I have to *sort* the
Vin>    modifiers so they are always in the correct order.  In a hook -
Vin>    yuck!  I wanted to use  event-matches-key-specifier-p, but this
Vin>    doesn't work with mouse buttons.

Vin> 2) How do I convert something like the symbol button3 to the proper
Vin>    event-number (3 in this case)?

I know it's bad form to follow up my post, but here's what I came up
with:

(defun mouse-cut-selection (event count)
  "This hook should be attached to mouse-track-click-hook by define-mouse-cut"
  (and (= count mouse-cut-count)
       (= (event-button event) mouse-cut-button)
       (equal (sort (event-modifiers event) 'string<) mouse-cut-modifiers)
       
       ;; Since kill-region returns non-nil, this hook
       ;; will prevent following hooks from executing
       (kill-region (point) (mark))))

(defun define-mouse-cut (key count)
  "Define the mouse key and the number of clicks required to cut the selection"
  (interactive)

  ;; Define the variables needed by the hook function
  (setq mouse-cut-count count
	mouse-cut-modifiers (nreverse (copy-sequence key))
	mouse-cut-button (let
			     ((k (car mouse-cut-modifiers)))
			   (cond ((equal k 'button1) 1)
				 ((equal k 'button2) 2)
				 ((equal k 'button3) 3)
				 ((equal k 'button4) 4)
				 ((equal k 'button5) 5)
				 ((error "Illegal mouse button %s" k))))
	mouse-cut-modifiers (sort (cdr mouse-cut-modifiers) 'string<))

  ;; Now make sure the key is defined
  (define-key global-map key 'mouse-track-adjust)

  ;; Now add the hook function
  (add-hook 'mouse-track-click-hook 'mouse-cut-selection))

Now a user can easily choose what button he/she would like to put
cut-selection on like so:
  (define-mouse-cut '(shift button1) 2)
or
  (define-mouse-cut '(button3) 2)

I'm still not really that pleased with this because:
1) it's goofy the way that define-mouse-cut converts button symbols to
   button numbers.  Is there a better way?
2) I don't really like using sort to make sure the modifier lists
   compare ok.  Is there a better way?
3) You can't use this to bind more than one key.  Last definition
   wins.

Anyway, it was fun to do this.  Comments/suggestions welcome.

--vin

