From: Stefan Monnier Date: Wed, 27 Oct 2021 18:03:43 +0000 (-0400) Subject: (string-pixel-width): Rewrite to avoid side effects X-Git-Tag: emacs-29.0.90~3671^2~379 X-Git-Url: http://git.eshelyaron.com/gitweb/?a=commitdiff_plain;h=c22b735f0c6261b485f0f2afa10ec4c598550b5b;p=emacs.git (string-pixel-width): Rewrite to avoid side effects * src/xdisp.c (Fwindow_text_pixel_size): Allow `window` to be a buffer. * lisp/emacs-lisp/subr-x.el (string-pixel-width): Simplify accordingly. --- diff --git a/lisp/emacs-lisp/subr-x.el b/lisp/emacs-lisp/subr-x.el index f2060814f25..00668d47439 100644 --- a/lisp/emacs-lisp/subr-x.el +++ b/lisp/emacs-lisp/subr-x.el @@ -446,13 +446,8 @@ is inserted before adjusting the number of empty lines." "Return the width of STRING in pixels." (with-temp-buffer (insert string) - (save-window-excursion - ;; Avoid errors if the selected window is a dedicated one, - ;; and they just want to insert a document into it. - (set-window-dedicated-p nil nil) - (set-window-buffer nil (current-buffer)) - (car (window-text-pixel-size - nil (line-beginning-position) (point)))))) + (car (window-text-pixel-size + (current-buffer) (point-min) (point))))) (provide 'subr-x) diff --git a/src/xdisp.c b/src/xdisp.c index bbe7e2701ba..aa01db210b7 100644 --- a/src/xdisp.c +++ b/src/xdisp.c @@ -10628,10 +10628,12 @@ in_display_vector_p (struct it *it) DEFUN ("window-text-pixel-size", Fwindow_text_pixel_size, Swindow_text_pixel_size, 0, 6, 0, doc: /* Return the size of the text of WINDOW's buffer in pixels. -WINDOW must be a live window and defaults to the selected one. The +WINDOW can be any live window and defaults to the selected one. The return value is a cons of the maximum pixel-width of any text line and the pixel-height of all the text lines in the accessible portion of buffer text. +WINDOW can also be a buffer, in which case the selected window is used, +and the function behaves as if that window was displaying this buffer. This function exists to allow Lisp programs to adjust the dimensions of WINDOW to the buffer text it needs to display. @@ -10675,8 +10677,9 @@ include the height of any of these, if present, in the return value. */) (Lisp_Object window, Lisp_Object from, Lisp_Object to, Lisp_Object x_limit, Lisp_Object y_limit, Lisp_Object mode_lines) { - struct window *w = decode_live_window (window); - Lisp_Object buffer = w->contents; + struct window *w = BUFFERP (window) ? XWINDOW (selected_window) + : decode_live_window (window); + Lisp_Object buffer = BUFFERP (window) ? window : w->contents; struct buffer *b; struct it it; struct buffer *old_b = NULL;