]> gitweb.factorcode.org Git - factor.git/blob - basis/ui/tools/listener/history/history.factor
Delete empty unit tests files, remove 1- and 1+, reorder IN: lines in a lot of places...
[factor.git] / basis / ui / tools / listener / history / history.factor
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
6
7 TUPLE: history document elements index ;
8
9 : <history> ( document -- history )
10     V{ } clone 0 history boa ;
11
12 : history-add ( history -- input )
13     dup elements>> length 1 + >>index
14     [ document>> doc-string [ <input> ] [ empty? ] bi ] keep
15     '[ [ _ elements>> push ] keep ] unless ;
16
17 <PRIVATE
18
19 : (save-history) ( input index elements -- )
20     2dup length > [
21         [ [ T{ input f "" } ] dip push ] keep
22         (save-history)
23     ] [ set-nth ] if ;
24
25 : save-history ( history -- )
26     [ document>> doc-string ] keep
27     '[ <input> _ [ index>> ] [ elements>> ] bi (save-history) ]
28     unless-empty ;
29
30 : update-document ( history -- )
31     [ [ index>> ] [ elements>> ] bi nth string>> ] [ document>> ] bi
32     [ set-doc-string ] [ clear-undo drop ] 2bi ;
33
34 : change-history-index ( history i -- )
35     over elements>> length 1 -
36     '[ _ + _ min 0 max ] change-index drop ;
37
38 : history-recall ( history i -- )
39     [ [ elements>> empty? ] keep ] dip '[
40         _
41         [ save-history ]
42         [ _ change-history-index ]
43         [ update-document ]
44         tri
45     ] unless ;
46
47 PRIVATE>
48
49 : history-recall-previous ( history -- )
50     -1 history-recall ;
51
52 : history-recall-next ( history -- )
53     1 history-recall ;