Function: pixel-fill-region

pixel-fill-region is a byte-compiled function defined in pixel-fill.el.gz.

Signature

(pixel-fill-region START END PIXEL-WIDTH)

Documentation

Fill the region between START and END.

This will attempt to reformat the text in the region to have no 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.

View in manual

Probably introduced at or before Emacs version 29.1.

Source Code

;; Defined in /usr/src/emacs/lisp/textmodes/pixel-fill.el.gz
(defun pixel-fill-region (start end pixel-width)
  "Fill the region between START and END.
This will attempt to reformat the text in the region to have no
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-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")))))))