Function: string-fill

string-fill is a byte-compiled function defined in subr-x.el.gz.

Signature

(string-fill STRING LENGTH)

Documentation

Try to word-wrap STRING so that no lines are longer than LENGTH.

Wrapping is done where there is whitespace. If there are individual words in STRING that are longer than LENGTH, the result will have lines that are longer than LENGTH.

Other relevant functions are documented in the string group.

Probably introduced at or before Emacs version 28.1.

Shortdoc

;; string
(string-fill "Three short words" 12)
    => "Three short\nwords"
  (string-fill "Long-word" 3)
    => "Long-word"

Source Code

;; Defined in /usr/src/emacs/lisp/emacs-lisp/subr-x.el.gz
(defun string-fill (string length)
  "Try to word-wrap STRING so that no lines are longer than LENGTH.
Wrapping is done where there is whitespace.  If there are
individual words in STRING that are longer than LENGTH, the
result will have lines that are longer than LENGTH."
  (with-temp-buffer
    (insert string)
    (goto-char (point-min))
    (let ((fill-column length)
          (adaptive-fill-mode nil))
      (fill-region (point-min) (point-max)))
    (buffer-string)))