]> gitweb.factorcode.org Git - factor.git/blob - basis/ui/tools/listener/history/history.factor
Fix conflict in images vocab
[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 ( history -- )
20     [ document>> doc-string ] keep
21     '[ <input> _ [ index>> ] [ elements>> ] bi set-nth ]
22     unless-empty ;
23
24 : update-document ( history -- )
25     [ [ index>> ] [ elements>> ] bi nth string>> ] [ document>> ] bi
26     [ set-doc-string ] [ clear-undo drop ] 2bi ;
27
28 : change-history-index ( history i -- )
29     over elements>> length 1-
30     '[ _ + _ min 0 max ] change-index drop ;
31
32 : history-recall ( history i -- )
33     [ [ elements>> empty? ] keep ] dip '[
34         _
35         [ save-history ]
36         [ _ change-history-index ]
37         [ update-document ]
38         tri
39     ] unless ;
40
41 PRIVATE>
42
43 : history-recall-previous ( history -- )
44     -1 history-recall ;
45
46 : history-recall-next ( history -- )
47     1 history-recall ;