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) ( input index elements -- )
21 [ [ T{ input f "" } ] dip push ] keep
25 : save-history ( history -- )
26 [ document>> doc-string ] keep
27 '[ <input> _ [ index>> ] [ elements>> ] bi (save-history) ]
30 : update-document ( history -- )
31 [ [ index>> ] [ elements>> ] bi nth string>> ] [ document>> ] bi
32 [ set-doc-string ] [ clear-undo drop ] 2bi ;
34 : change-history-index ( history i -- )
35 over elements>> length 1 -
36 '[ _ + _ min 0 max ] change-index drop ;
38 : history-recall ( history i -- )
39 [ [ elements>> empty? ] keep ] dip '[
42 [ _ change-history-index ]
49 : history-recall-previous ( history -- )
52 : history-recall-next ( history -- )