]> git.eshelyaron.com Git - emacs.git/commitdiff
Avoid signaling errors from 'pixel-fill-region'
authorEli Zaretskii <eliz@gnu.org>
Sat, 3 Feb 2024 09:09:36 +0000 (11:09 +0200)
committerEshel Yaron <me@eshelyaron.com>
Sun, 4 Feb 2024 11:04:52 +0000 (12:04 +0100)
* lisp/textmodes/pixel-fill.el (pixel-fill-region): Make sure the
selected window displays the current buffer.  This is important
when this function is called inside 'with-current-buffer' or
similar forms which temporarily change the buffer displayed in the
selected window.  (Bug#67791)

(cherry picked from commit d49124fc14b0bb37617b34b5839f873cea3817c8)

lisp/textmodes/pixel-fill.el

index 25c0b46cee9a109bbaeff8e8f47e00cca9894204..d26eaec211178fb014eccd0f48447c4f2db36f83 100644 (file)
@@ -73,39 +73,41 @@ lines that are visually wider than PIXEL-WIDTH.
 If START isn't at the start of a line, the horizontal position of
 START, converted to pixel units, will be used as the indentation
 prefix on subsequent lines."
-  (save-excursion
-    (goto-char start)
-    (let ((indentation
-           (car (window-text-pixel-size nil (line-beginning-position)
-                                        (point))))
-          (newline-end nil))
-      (when (> indentation pixel-width)
-        (error "The indentation (%s) is wider than the fill width (%s)"
-               indentation pixel-width))
-      (save-restriction
-        (narrow-to-region start end)
-        (goto-char (point-max))
-        (when (looking-back "\n[ \t]*" (point-min))
-          (setq newline-end t))
-        (goto-char (point-min))
-        ;; First replace all whitespace with space.
-        (while (re-search-forward "[ \t\n]+" nil t)
-          (cond
-           ((or (= (match-beginning 0) start)
-                (= (match-end 0) end))
-            (delete-region (match-beginning 0) (match-end 0)))
-           ;; If there's just a single space here, don't replace.
-           ((not (and (= (- (match-end 0) (match-beginning 0)) 1)
-                      (= (char-after (match-beginning 0)) ?\s)))
-            (replace-match
-             ;; We need to use a space that has an appropriate width.
-             (propertize " " 'face
-                         (get-text-property (match-beginning 0) 'face))))))
-        (goto-char start)
-        (pixel-fill--fill-line pixel-width indentation)
-        (goto-char (point-max))
-        (when newline-end
-          (insert "\n"))))))
+  (save-window-excursion
+    (set-window-buffer nil (current-buffer))
+    (save-excursion
+      (goto-char start)
+      (let ((indentation
+             (car (window-text-pixel-size nil (line-beginning-position)
+                                          (point))))
+            (newline-end nil))
+        (when (> indentation pixel-width)
+          (error "The indentation (%s) is wider than the fill width (%s)"
+                 indentation pixel-width))
+        (save-restriction
+          (narrow-to-region start end)
+          (goto-char (point-max))
+          (when (looking-back "\n[ \t]*" (point-min))
+            (setq newline-end t))
+          (goto-char (point-min))
+          ;; First replace all whitespace with space.
+          (while (re-search-forward "[ \t\n]+" nil t)
+            (cond
+             ((or (= (match-beginning 0) start)
+                  (= (match-end 0) end))
+              (delete-region (match-beginning 0) (match-end 0)))
+             ;; If there's just a single space here, don't replace.
+             ((not (and (= (- (match-end 0) (match-beginning 0)) 1)
+                        (= (char-after (match-beginning 0)) ?\s)))
+              (replace-match
+               ;; We need to use a space that has an appropriate width.
+               (propertize " " 'face
+                           (get-text-property (match-beginning 0) 'face))))))
+          (goto-char start)
+          (pixel-fill--fill-line pixel-width indentation)
+          (goto-char (point-max))
+          (when newline-end
+            (insert "\n")))))))
 
 (defun pixel-fill--goto-pixel (width)
   (vertical-motion (cons (/ width (frame-char-width)) 0)))