unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* Elisp test for whether `mouse-1' is pressed?
@ 2021-11-03 23:05 Drew Adams
  2021-11-05 17:10 ` Drew Adams
  0 siblings, 1 reply; 2+ messages in thread
From: Drew Adams @ 2021-11-03 23:05 UTC (permalink / raw)
  To: Emacs developers

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?



^ permalink raw reply	[flat|nested] 2+ messages in thread

* RE: Elisp test for whether `mouse-1' is pressed?
  2021-11-03 23:05 Elisp test for whether `mouse-1' is pressed? Drew Adams
@ 2021-11-05 17:10 ` Drew Adams
  0 siblings, 0 replies; 2+ messages in thread
From: Drew Adams @ 2021-11-05 17:10 UTC (permalink / raw)
  To: Drew Adams, Emacs developers

Does no one have an idea about this?  I'd really like
to know how to detect the state of `mouse-1' being
pressed (not yet released).

https://lists.gnu.org/archive/html/emacs-devel/2021-11/msg00277.html

> 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')?

Seems like this would be useful for Emacs generally,
no?  In any case it will be useful to me.

We have `use-region-p', but that takes no account
of whether `mouse-1' is pressed.  So `use-region-p'
returns TRUE for a `mouse-1' press (until the button
is released).  Any user-visible action (e.g. a UI
change done by code when `use-region-p' is TRUE)
can be manifested for the duration of the press.

In my use case, a user will have chosen to use the
region even when empty (where "use" it means show
an UI indication that it's active).  This choice
can be helpful for knowing that you will be acting
on an empty region (which you might not really want
to do).

But with this `mouse-1'-press "feature", a user
sees the active-region indication (momentarily)
even for a `mouse-1' click, which is only a bother
and not a proper indication that the empty region
will be active and acted on (because releasing the
mouse button deactivates the empty region).

This test was possible in Emacs 24 via a dumb hack,
but that hack's not usable in Emacs 25+.  Besides,
I'd prefer a clean, well-lighted place (solution).

Any ideas?



^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2021-11-05 17:10 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-11-03 23:05 Elisp test for whether `mouse-1' is pressed? Drew Adams
2021-11-05 17:10 ` Drew Adams

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).