Created
October 24, 2019 10:08
-
-
Save jcs090218/be80d98ea23ffbda344e7c679169a653 to your computer and use it in GitHub Desktop.
Text wrap like html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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