emacs-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Elisp test for whether `mouse-1' is pressed?


From: Drew Adams
Subject: Elisp test for whether `mouse-1' is pressed?
Date: Wed, 3 Nov 2021 23:05:38 +0000

What's a good way to test whether mouse-1 is currently
pressed (which I think is still the same as asking to
test whether we're in `mouse-drag-track')?

I have some code that detects whether it can act on the
region (including by showing something in the mode-line).

Aside from respecting a user option that lets users show
the indication even if the region's empty (as long as it's
active), this detection would normally essentially do what
`use-region-p' does.

(The option is like `use-empty-active-region', but it's
specific to this use case.)

But to prevent the mode-line indication from showing up
even when you just press mouse-1 (the indication goes away
when you release mouse-1), I've been using this ugly hack
I came up with back in Emacs 24.

Unfortunately, the hack doesn't work in later releases,
hence question.  (See the famous last words in the code
comment.)
___

(defun show-region-p ()
  "...
Return non-nil if region's active & nonempty, or emptiness is OK.

But don't return non-nil if the condition is true but you're
selecting with the mouse.  This is to prevent notification
whenever you press `mouse-1' without dragging at least one
character."

  ;; Fragile hack: Starting with Emacs 24, the region is
  ;; considered empty as soon as you press `mouse-1'
  ;; (`down-mouse-1').  That causes mode-line indication
  ;; each time you just click `mouse-1', i.e., without
  ;; dragging it.
  ;;
  ;; The hack is to check whether `echo-keystrokes' is 0.
  ;; `mouse-drag-track' binds `echo-keystrokes' to 0, and
  ;; that seems to be the only way to tell whether we are
  ;; in `mouse-drag-track'.  If the Emacs code for that
  ;; changes then this might break.
  ;;
  (ignore-errors (and (region-active-p)
                      (or (and (not (eq 0 echo-keystrokes))
                               mlr-empty-region-flag)
                          (> (region-end) (region-beginning))))))
___

What's a good way to check this now?



reply via email to

[Prev in Thread] Current Thread [Next in Thread]