Skip to content

Instantly share code, notes, and snippets.

@jcs090218
Created October 24, 2019 10:08
Show Gist options
  • Save jcs090218/be80d98ea23ffbda344e7c679169a653 to your computer and use it in GitHub Desktop.
Save jcs090218/be80d98ea23ffbda344e7c679169a653 to your computer and use it in GitHub Desktop.
Text wrap like html
(defun define-it--wrap-string (str len)
"Wrap the STR under the LEN."
(let ((res-str "") (min 0) (new-len len) (added-nl-count 0))
(while (and (< min new-len)
(< new-len (length str)))
(let* ((line (substring str min new-len))
(spc-lst (split-string line " "))
(last-elm (nth (1- (length spc-lst)) spc-lst)))
(setq res-str
(concat res-str
(substring line 0 (- len (length last-elm)))
"\n"))
(setq added-nl-count (1+ added-nl-count))
(setq min (- (length res-str) added-nl-count))
(setq new-len (+ min len))))
(setq res-str (concat res-str (substring str min (length str))))
res-str))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment