1 ! Copyright (C) 2009 Slava Pestov.
2 ! See https://factorcode.org/license.txt for BSD license.
3 USING: accessors continuations documents io io.encodings.utf8
4 io.files io.styles kernel math math.order namespaces
5 prettyprint.backend prettyprint.config sequences strings.parser ;
6 IN: ui.tools.listener.history
8 TUPLE: history document elements start index ;
10 CONSTANT: history-file "~/.factor-history"
12 : read-history ( -- elements )
13 history-file file-exists? [ history-file utf8 file-lines [ unescape-string <input> ] V{ } map-as ] [ V{ } clone ] if ;
15 : append-history ( history -- )
16 history-file file-exists?
21 [ elements>> ] [ start>> ] bi [ string>> f f unparse-string print ] swap each-from
24 ] [ [ index>> ] keep start<< ] bi
27 : <history> ( document -- history )
28 read-history dup length dup history boa ;
32 : push-if-not-last ( elt seq -- )
33 2dup ?last = [ 2drop ] [ push ] if ;
35 : current-input ( history -- input ? )
36 document>> doc-string [ <input> ] [ empty? ] bi ;
40 : history-add ( history -- input )
41 dup current-input [ nip ] [
45 [ length >>index drop ] bi
51 : set-element ( elt history -- )
52 [ index>> ] [ elements>> ] bi set-nth ;
54 : get-element ( history -- elt )
55 [ index>> ] [ elements>> ] bi nth ;
57 : save-history ( history -- )
58 dup current-input [ 2drop ] [ swap set-element ] if ;
60 : update-document ( history -- )
61 [ get-element string>> ] [ document>> ] bi
62 [ set-doc-string ] [ clear-undo ] bi ;
64 : change-history-index ( history i -- )
65 over elements>> length 1 -
66 '[ _ + 0 _ clamp ] change-index drop ;
68 : history-recall ( history i -- )
69 over elements>> empty? [ 2drop ] [
71 [ change-history-index ]
72 [ drop update-document ]
78 : history-recall-previous ( history -- )
81 : history-recall-next ( history -- )