[gnu.emacs] Modifying mouse clicks in X

wilensky@dsg.csc.ti.com (02/04/89)

I am running Emacs 18.50 under X Windows version 11, revision 2 on a Sun 386i.
I want to modify the meanings of the mouse clicks as defined in x-mouse.el.
To do that, I included the following lines in my .emacs file.  In fact, my
.emacs file contains nothing else.

   (define-key mouse-map x-button-left 'x-mouse-set-point)
   (define-key mouse-map x-button-middle 'x-mouse-set-mark)

However, when initializing emacs I get a message indicating an error in the
init file.  If I then explicitly load the .emacs file or evaluate it in a
buffer, it doesn't complain and the button clicks change as desired.

What's going on here?  Something in the environment must be changing after
.emacs is loaded.  I thought .emacs was the last thing loaded.

Does anyone know what is happening here?

Please respond to Harold Wilensky at wilensky@dsg.ti.com rather than
the return address in the mail header.

jr@bbn.com (John Robinson) (02/04/89)

In article <2811536549-55195@TM>, wilensky@dsg writes:
>I am running Emacs 18.50 under X Windows version 11, revision 2 on a Sun 386i.
>I want to modify the meanings of the mouse clicks as defined in x-mouse.el.
>To do that, I included the following lines in my .emacs file.  In fact, my
>.emacs file contains nothing else.
>
>   (define-key mouse-map x-button-left 'x-mouse-set-point)
>   (define-key mouse-map x-button-middle 'x-mouse-set-mark)
>
>However, when initializing emacs I get a message indicating an error in the
>init file.  If I then explicitly load the .emacs file or evaluate it in a
>buffer, it doesn't complain and the button clicks change as desired.

The problem is the order that things happen in the initialization.
First, your .emacs is loaded, then the terminal-dependent stuff.  The
x-mouse functions are defined by loading the x "terminal" code in
lisp/term/x-win.el.  So you have to do the define-key calls after the
later file is loaded.  The way to do this is to hang a function (or
lambda-expresion for you die-hards :-) onto term-setup-hook, which is
evaluated *after* the appropriate lisp/term/ file is loaded.  In my
.emacs I do:

  (defun my-x-button-init ()
    (define-key mouse-map x-button-right 'x-mouse-select)
    (define-key mouse-map x-button-left 'x-mouse-set-point)
    (define-key mouse-map x-button-middle 'x-mouse-set-mark))

  (cond ((not (null (getenv "DISPLAY")))
	 (x-set-bell t)		        ;;; visible-bell in X
	 (setq term-setup-hook 'my-x-button-init)))

Now, technically any hook can be a list of forms to evaluate, so the
setq should be an append guarded by a check that my-x-button-init
isn't already on the hook.

(Dear list - I realize the poster requested a mail response; I'll send
that along as well.)
--
/jr
jr@bbn.com or bbn!jr

dave@CS.WISC.EDU (Dave Cohrs) (02/05/89)

I had the same problem.  I did the following, but don't remember which
was actually necessary to make it work:

- change your key definitions in your term-setup-hook:

(defun your-term-setup-hook ()
  (if (featurep 'x-mouse)
      (progn
        (define-key mouse-map x-button-left 'x-mouse-set-point)
        (define-key mouse-map x-button-middle 'x-mouse-set-mark))))
        
(setq term-setup-hook 'your-term-setup-hook)

- make sure any new mouse functions you define yourself are actually
  defined before you do the (define-key).

dave