Seems like we did the exercise before but I could not find it.
Any improvements of this function?
;; CAB 10.21.2007
;; Break String at MaxLen
;; Return a list of strings broken to Max Length
;; If SpBrk is true back up the the previous space.
(defun StrWrap (str MaxLen SpBrk / tmp)
(setq ptr MaxLen)
(if (and SpBrk (> (strlen str) MaxLen))
(while (and (not (zerop ptr)) (/= (substr str ptr 1) " "))
(setq ptr (1- ptr)))
)
(and (zerop ptr) (setq ptr MaxLen)) ; no space char
(setq tmp (substr str 1 ptr))
(if (< (- (strlen str) (strlen tmp)) MaxLen)
(setq tmp (vl-remove "" (list tmp (substr str (1+ ptr)))))
(setq tmp (cons tmp (StrWrap (substr str (1+ ptr)) MaxLen SpBrk)))
)
tmp
)
(defun c:test (/ ts n)
;; Break at space
(foreach ts (list "abcd efghi jklmn pqrst uvwxyz"
"abcdefghijklmnpqrstuvwxyz"
)
(setq n 8)
(repeat 20
(print n)
(prin1 (StrWrap ts n t))
(setq n (1+ n))
)
(print)
)
;; Break exact
(foreach ts (list "abcd efghi jklmn pqrst uvwxyz"
"abcdefghijklmnpqrstuvwxyz"
)
(setq n 8)
(repeat 20
(print n)
(prin1 (StrWrap ts n nil))
(setq n (1+ n))
)
(print)
)
(princ)
)