0

I'm not sure whether this should be considered an appropriate post for emacs.stackexchange, or whether this should go to some sort of macos forum. Please let me know if I need to ask the following question elsewhere ...

I'm running the following version of Emacs under macos (Big Sur, version 11.6.1) ...

GNU Emacs 27.2 (build 1, x86_64-apple-darwin18.7.0, NS appkit-1671.60 Version 10.14.6 (Build 18G95))
 of 2021-03-27
Copyright (C) 2021 Free Software Foundation, Inc.

I am using dired, and I am trying to remap [mouse-1] to run my own function. However, I have not been able to get dired to recognize this [mouse-1] remapping.

Here is what I have tried ...

(defun my-special-function ()
  (interactive)
  (message "running my special function")
  (sit-for 1)
  )

(require 'dired)
(define-key dired-mode-map [mouse-1] 'my-special-function)
(dired "/path/to/directory")

When I run this when positioned on a dired line, clicking there with mouse-1 still runs the default dired-find-file function, instead of my own function.

I'm guessing that this might somehow be related to the text properties that dired applies within the dired buffer.

What can I do to under this version of emacs to get [mouse-1] to also get remapped to run my special function?

2 Answers 2

0

I ended up fixing this in a completely different way. I overrode dired-find-file and dired-mouse-find-file, as follows ...

(defun my-special-function ()
  (interactive)
  (message "my special function")
  (sit-for 1)
  )

(defvar my-dired-advised nil
  "*my dired stuff is already advised")

(defun my-dired-mode-hook ()
  (unless my-dired-advised
    (defadvice dired-find-file (around dired-find-file-around activate)
      (my-special-function)
      )
    (defadvice dired-mouse-find-file (around dired-mouse-find-file-around activate)
      (my-special-function)
      )
    (setq my-dired-advised t)
    )
  )

(require 'dired)
(add-hook 'dired-mode-hook 'my-dired-mode-hook)

(dired "/path/to/directory")

It seems like dired maps the mouse stuff under the covers, irrespective of any define-key commands involving the mouse. Simply overriding those two dired functions appears to be what I need to do.

Is there perhaps any other way to solve this?

0

You replaced the binding for <mouse-1> with a binding to your command. That should be fine, but it's not a "remapping" (which is about commands or faces, not keys).

Remapping a command is about doing something like this:

(define-key dired-mode-map [remap dired-mouse-find-file-other-window] 'my-special-function)

That remaps the keys bound to command dired-mouse-find-file-other-window (I'm supposing that's the command you want to remap), so that those keys instead invoke your command, my-special-function.

See (elisp) Remapping Commands.

10
  • Thank you. My understanding is that define-key overrides the previous key definition and replaces it, and that it doesn't simply add extra bindings. That's how it works for the dozens of non-mouse key bindings that I have been utilizing for years.
    – HippoMan
    Nov 14, 2021 at 21:41
  • Your understanding is correct (but that's not what Emacs calls "remapping"). Forgetting about your additional binding of key X to your command, your define-key of [mouse-1] should, yes, bind that event to your command. What does C-h k followed by pressing mouse-1 for a second or so before then releasing it tell you? It should tell you what both <down-mouse-1> and <mouse-1> are bound to. (FWIW, I expect your question would be clearer if you removed the binding for X. I'm guessing now that that has nothing to do with your question.)
    – Drew
    Nov 14, 2021 at 21:54
  • What I wanted to do is map [mouse-1] to my command. The "X" mapping I included was simply to show that the define-key works for non-mouse events. However, using the [remap dired-mouse-find-file-other-window] as you showed above did indeed work for me.
    – HippoMan
    Nov 14, 2021 at 21:56
  • After doing what I put in my original post, C-h k showed <mouse-1> and <down-mouse-1> being bound to dired-find-file, not to my special command. Again, I think that dired does some sort of special binding of mouse events by means of text properties, or something like that.
    – HippoMan
    Nov 14, 2021 at 21:58
  • OK. I suggest you remove mention of the X binding. Also, say what C-h k tells you, because it should tell you that <mouse-1> was successfully bound to your command. [Our comments crossed.] Put any info that's important in your question or answer - comments can be deleted at any time.
    – Drew
    Nov 14, 2021 at 21:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.