{ { 1 0 } } [ { 1 3 } doc get one-line-elt prev-elt ] unit-test
{ { 1 14 } } [ { 1 3 } doc get one-line-elt next-elt ] unit-test
+! paragraph-elt
+<document> doc set
+"First line
+Second line
+
+Fourth line
+Fifth line
+Sixth line
+
+
+" doc get set-doc-string
+
+{ { 0 0 } } [ { 0 2 } doc get paragraph-elt prev-elt ] unit-test
+{ { 0 0 } } [ { 1 2 } doc get paragraph-elt prev-elt ] unit-test
+{ { 3 0 } } [ { 5 2 } doc get paragraph-elt prev-elt ] unit-test
+
+{ { 1 11 } } [ { 0 3 } doc get paragraph-elt next-elt ] unit-test
+{ { 1 11 } } [ { 1 3 } doc get paragraph-elt next-elt ] unit-test
+{ { 2 0 } } [ { 1 11 } doc get paragraph-elt next-elt ] unit-test
+{ { 5 10 } } [ { 2 0 } doc get paragraph-elt next-elt ] unit-test
+{ { 5 10 } } [ { 4 2 } doc get paragraph-elt next-elt ] unit-test
+
+{ { 3 0 } } [ { 6 0 } doc get paragraph-elt prev-elt ] unit-test
+{ { 6 0 } } [ { 7 0 } doc get paragraph-elt prev-elt ] unit-test
+
+{ { 7 0 } } [ { 6 0 } doc get paragraph-elt next-elt ] unit-test
+{ { 8 0 } } [ { 7 0 } doc get paragraph-elt next-elt ] unit-test
+
+
! page-elt
<document> doc set
"First line
! Copyright (C) 2006, 2009 Slava Pestov.
! See http://factorcode.org/license.txt for BSD license.
USING: accessors arrays combinators documents kernel math
-sequences unicode ;
+math.order sequences unicode ;
IN: documents.elements
GENERIC: prev-elt ( loc document elt -- newloc )
M: one-line-elt next-elt
drop [ first dup ] dip doc-line length 2array ;
-TUPLE: page-elt { lines read-only } ;
+<PRIVATE
+
+:: prev-paragraph ( loc document -- loc' )
+ loc first 1 [-] document value>>
+ [ empty? ] find-last-from drop [ 1 + ] [ 0 ] if* :> line#
+
+ loc first line# = loc second 0 = and [
+ line# 1 - 0 2array
+ ] [
+ line# 0 2array
+ ] if ;
+
+:: next-paragraph ( loc document -- loc' )
+ loc first 1 + document value>>
+ [ empty? ] find-from drop :> line#
+
+ line# document value>> length or 1 [-]
+ dup document doc-line length
+ [ zero? [ 1 + ] when ] keep 2array
+
+ dup loc = [ first 1 + 0 2array ] when ;
+
+PRIVATE>
+
+SINGLETON: paragraph-elt
+
+M: paragraph-elt prev-elt drop prev-paragraph ;
+
+M: paragraph-elt next-elt drop next-paragraph ;
+
+TUPLE: page-elt { #lines integer read-only } ;
C: <page-elt> page-elt
M: page-elt prev-elt
nip
- 2dup [ first ] [ lines>> ] bi* <
- [ 2drop { 0 0 } ] [ lines>> neg +line ] if ;
+ 2dup [ first ] [ #lines>> ] bi* <
+ [ 2drop { 0 0 } ] [ #lines>> neg +line ] if ;
M: page-elt next-elt
- 3dup [ first ] [ last-line# ] [ lines>> ] tri* - >
- [ drop nip doc-end ] [ nip lines>> +line ] if ;
+ 3dup [ first ] [ last-line# ] [ #lines>> ] tri* - >
+ [ drop nip doc-end ] [ nip #lines>> +line ] if ;
CONSTANT: line-elt T{ page-elt f 1 }