Fix mouse-posn-property

* doc/lispref/commands.texi (Click Events): Mention menu-bar.
* lisp/mouse.el (mouse-event-areas-with-no-buffer-positions):
New defvar including all areas mentioned in the Lisp reference.
(mouse-posn-property): Use it.
This commit is contained in:
Gerd Möllmann 2025-03-14 15:44:04 +01:00
parent 88e30e1ac5
commit b105089715
2 changed files with 20 additions and 9 deletions

View file

@ -1675,8 +1675,8 @@ The buffer position of the character clicked on in the text area; or,
if the event was outside the text area, the window area where it
occurred. It is one of the symbols @code{mode-line},
@code{header-line}, @code{tab-line}, @code{vertical-line},
@code{left-margin}, @code{right-margin}, @code{left-fringe}, or
@code{right-fringe}.
@code{left-margin}, @code{right-margin}, @code{left-fringe},
@code{right-fringe}, @code{tab-bar}, or @code{menu-bar}.
In one special case, @var{pos-or-area} is a list containing a symbol
(one of the symbols listed above) instead of just the symbol. This

View file

@ -1655,6 +1655,18 @@ is dragged over to."
;; operation.
(put 'mouse-drag-region 'undo-inhibit-region t)
(defvar mouse-event-areas-with-no-buffer-positions
'( mode-line header-line vertical-line
left-fringe right-fringe
left-margin right-margin
tab-bar menu-bar
tab-line)
"Event areas not containing buffer positions.
Example: mouse clicks in the fringe come with a position in
(nth 5). This is useful but is not where we clicked, so
don't look up that position's properties!. Likewise for
the other event areas.")
(defun mouse-posn-property (pos property)
"Look for a property at click position.
POS may be either a buffer position or a click position like
@ -1663,7 +1675,9 @@ a string, the text property PROPERTY is examined.
If this is nil or the click is not on a string, then
the corresponding buffer position is searched for PROPERTY.
If PROPERTY is encountered in one of those places,
its value is returned."
its value is returned. Mouse events in areas listed in
`mouse-event-areas-with-no-buffer-positions' always return nil
because such events do not contain buffer positions."
(if (consp pos)
(let ((w (posn-window pos)) (pt (posn-point pos))
(str (posn-string pos)))
@ -1674,12 +1688,9 @@ its value is returned."
(or (and str
(< (cdr str) (length (car str)))
(get-text-property (cdr str) property (car str)))
;; Mouse clicks in the fringe come with a position in
;; (nth 5). This is useful but is not exactly where we clicked, so
;; don't look up that position's properties!
(and pt (not (memq (posn-area pos)
'(left-fringe right-fringe
left-margin right-margin tab-bar)))
(and pt
(not (memq (posn-area pos)
mouse-event-areas-with-no-buffer-positions))
(get-char-property pt property w))))
(get-char-property pos property)))