From 91213541169fff492932649140dadd68ae796f00 Mon Sep 17 00:00:00 2001
From: Daniel Ehrenberg <ehrenbed@carleton.edu>
Date: Thu, 31 Jan 2008 11:51:38 -0600
Subject: [PATCH] Adding word wrap vocab

---
 extra/wrap/wrap.factor | 30 ++++++++++++++++++++++++++++++
 1 file changed, 30 insertions(+)
 create mode 100644 extra/wrap/wrap.factor

diff --git a/extra/wrap/wrap.factor b/extra/wrap/wrap.factor
new file mode 100644
index 0000000000..4392ac81a6
--- /dev/null
+++ b/extra/wrap/wrap.factor
@@ -0,0 +1,30 @@
+USING: sequences kernel namespaces splitting math ;
+IN: wrap
+
+! Very stupid word wrapping/line breaking
+! This will be replaced by a Unicode-aware method,
+! which works with variable-width fonts
+
+SYMBOL: width
+
+: line-chunks ( string -- words-lines )
+    "\n" split [ " \t" split [ empty? not ] subset ] map ;
+
+: (split-chunk) ( words -- )
+    -1 over [ length + 1+ dup width get > ] find drop nip
+    [ cut-slice swap , (split-chunk) ] [ , ] if* ;
+
+: split-chunk ( words -- lines )
+    [ (split-chunk) ] { } make ;
+
+: broken-lines ( string width -- lines )
+    width [
+        line-chunks
+        [ split-chunk [ " " join ] map ] map concat
+    ] with-variable ;
+
+: line-break ( string width -- newstring )
+    broken-lines "\n" join ;
+
+: indented-break ( string width indent -- newstring )
+    [ length - broken-lines ] keep [ swap append ] curry map "\n" join ;