1 ! Copyright (C) 2009 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors documents kernel math math.order
4 sequences fry io.styles ;
5 IN: ui.tools.listener.history
7 TUPLE: history document elements index ;
9 : <history> ( document -- history )
10 V{ } clone 0 history boa ;
12 : history-add ( history -- input )
13 dup elements>> length 1+ >>index
14 [ document>> doc-string [ <input> ] [ empty? ] bi ] keep
15 '[ [ _ elements>> push ] keep ] unless ;
19 : save-history ( history -- )
20 [ document>> doc-string ] keep
21 '[ <input> _ [ index>> ] [ elements>> ] bi set-nth ]
24 : update-document ( history -- )
25 [ [ index>> ] [ elements>> ] bi nth string>> ] [ document>> ] bi
26 [ set-doc-string ] [ clear-undo drop ] 2bi ;
28 : change-history-index ( history i -- )
29 over elements>> length 1-
30 '[ _ + _ min 0 max ] change-index drop ;
32 : history-recall ( history i -- )
33 [ [ elements>> empty? ] keep ] dip '[
36 [ _ change-history-index ]
43 : history-recall-previous ( history -- )
46 : history-recall-next ( history -- )