emacs-diffs
[Top][All Lists]
Advanced

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

master 17ed9a8039 1/2: Allow posn-col-row to return data on a per-window


From: Lars Ingebrigtsen
Subject: master 17ed9a8039 1/2: Allow posn-col-row to return data on a per-window basis
Date: Tue, 7 Jun 2022 12:35:16 -0400 (EDT)

branch: master
commit 17ed9a803987d7441c64ee1a205322d99766b1da
Author: Lars Ingebrigtsen <larsi@gnus.org>
Commit: Lars Ingebrigtsen <larsi@gnus.org>

    Allow posn-col-row to return data on a per-window basis
    
    * doc/lispref/commands.texi (Accessing Mouse): Document it.
    * lisp/subr.el (posn-col-row): Extend to use window data.
---
 doc/lispref/commands.texi |  8 ++++++--
 etc/NEWS                  |  4 ++++
 lisp/subr.el              | 39 ++++++++++++++++++++++++---------------
 3 files changed, 34 insertions(+), 17 deletions(-)

diff --git a/doc/lispref/commands.texi b/doc/lispref/commands.texi
index 6c60216796..0a82bba3bc 100644
--- a/doc/lispref/commands.texi
+++ b/doc/lispref/commands.texi
@@ -2615,7 +2615,7 @@ POSITION is assumed to lie in a window text area."
 @end example
 @end defun
 
-@defun posn-col-row position
+@defun posn-col-row position &optional use-window
 This function returns a cons cell @w{@code{(@var{col} .  @var{row})}},
 containing the estimated column and row corresponding to buffer
 position described by @var{position}.  The return value is given in
@@ -2623,7 +2623,11 @@ units of the frame's default character width and default 
line height
 (including spacing), as computed from the @var{x} and @var{y} values
 corresponding to @var{position}.  (So, if the actual characters have
 non-default sizes, the actual row and column may differ from these
-computed values.)
+computed values.)  If the optional @var{window} argument is
+non-@code{nil}, use the default character width in the window
+indicated by @var{position} instead of the frame.  (This makes a
+difference if that window is showing a buffer with a non-default
+zooming level, for instance.)
 
 Note that @var{row} is counted from the top of the text area.  If the
 window given by @var{position} possesses a header line (@pxref{Header
diff --git a/etc/NEWS b/etc/NEWS
index b75c1c9f6c..fc9e949d8b 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -1946,6 +1946,10 @@ Previously it produced a nonsense value, -1, that was 
never intended.
 
 * Lisp Changes in Emacs 29.1
 
++++
+** 'posn-col-row' can now give position data based on windows.
+Previously, it reported data only based on the frame.
+
 +++
 ** 'file-expand-wildcards' can now also take a regexp as PATTERN argument.
 
diff --git a/lisp/subr.el b/lisp/subr.el
index 8afba2b341..50ae357a13 100644
--- a/lisp/subr.el
+++ b/lisp/subr.el
@@ -1705,13 +1705,19 @@ pixels.  POSITION should be a list of the form returned 
by
 
 (declare-function scroll-bar-scale "scroll-bar" (num-denom whole))
 
-(defun posn-col-row (position)
+(defun posn-col-row (position &optional use-window)
   "Return the nominal column and row in POSITION, measured in characters.
 The column and row values are approximations calculated from the x
 and y coordinates in POSITION and the frame's default character width
 and default line height, including spacing.
+
+If USE-WINDOW is non-nil, use the typical width of a character in
+the window indicated by POSITION instead of the frame.  (This
+makes a difference is a window has a zoom level.)
+
 For a scroll-bar event, the result column is 0, and the row
 corresponds to the vertical position of the click in the scroll bar.
+
 POSITION should be a list of the form returned by the `event-start'
 and `event-end' functions."
   (let* ((pair            (posn-x-y position))
@@ -1729,20 +1735,23 @@ and `event-end' functions."
      ((eq area 'horizontal-scroll-bar)
       (cons (scroll-bar-scale pair (window-width window)) 0))
      (t
-      ;; FIXME: This should take line-spacing properties on
-      ;; newlines into account.
-      (let* ((spacing (when (display-graphic-p frame)
-                        (or (with-current-buffer
-                                (window-buffer (frame-selected-window frame))
-                              line-spacing)
-                            (frame-parameter frame 'line-spacing)))))
-       (cond ((floatp spacing)
-              (setq spacing (truncate (* spacing
-                                         (frame-char-height frame)))))
-             ((null spacing)
-              (setq spacing 0)))
-       (cons (/ (car pair) (frame-char-width frame))
-             (/ (cdr pair) (+ (frame-char-height frame) spacing))))))))
+      (if use-window
+          (cons (/ (car pair) (window-font-width window))
+                (/ (cdr pair) (window-font-height window)))
+        ;; FIXME: This should take line-spacing properties on
+        ;; newlines into account.
+        (let* ((spacing (when (display-graphic-p frame)
+                          (or (with-current-buffer
+                                  (window-buffer (frame-selected-window frame))
+                                line-spacing)
+                              (frame-parameter frame 'line-spacing)))))
+         (cond ((floatp spacing)
+                (setq spacing (truncate (* spacing
+                                           (frame-char-height frame)))))
+               ((null spacing)
+                (setq spacing 0)))
+         (cons (/ (car pair) (frame-char-width frame))
+               (/ (cdr pair) (+ (frame-char-height frame) spacing)))))))))
 
 (defun posn-actual-col-row (position)
   "Return the window row number in POSITION and character number in that row.



reply via email to

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