1 ! Copyright (C) 2008 Alfredo Beaumont
2 ! See http://factorcode.org/license.txt for BSD license.
4 ! Emacs Etags generator
5 ! Alfredo Beaumont <alfredo.beaumont@gmail.com>
6 USING: kernel sequences sorting assocs words prettyprint ctags
7 io.encodings.ascii io.files math math.parser namespaces strings locals
8 shuffle io.backend memoize ;
11 : ctag-path ( alist -- path )
14 : ctag-at ( key hash -- vector )
15 at [ V{ } clone ] unless* ;
17 : ctag-hashvalue ( alist hash -- vector )
18 [ ctag-path ] dip ctag-at ;
20 : ctag-value ( ctag -- seq )
21 dup [ first , second second , ] { } make ;
23 : ctag-add ( ctag hash -- hash )
24 [ ctag-hashvalue ] 2keep [
25 dup ctag-path [ ctag-value suffix ] dip
26 ] dip [ set-at ] keep ;
28 : ctag-hash ( seq -- hash )
29 H{ } clone swap [ swap ctag-add ] each ;
31 : line>bytes ( n seq -- bytes )
34 : lines>bytes ( n seq -- bytes )
38 [ 1- ] dip lines>bytes
42 : file>bytes ( n path -- bytes )
43 ascii file-lines lines>bytes ;
45 : etag ( path seq -- str )
47 dup first ?word-name %
49 second dup number>string %
51 2 - swap file>bytes number>string %
54 : etag-entry ( alist -- alist path )
57 : vector-length ( vector -- n )
58 0 [ length + ] reduce ;
60 : <header> ( n path -- str )
67 : etag-header ( vec1 n resource -- vec2 )
68 normalize-path <header> prefix
69 1 HEX: 0c <string> prefix ;
72 : etag-strings ( alist -- seq )
76 resource get swap etag
77 ] map dup vector-length
83 : etags-write ( alist path -- )
84 [ etag-strings ] dip ascii set-file-lines ;
87 (ctags) sort-values ctag-hash >alist swap etags-write ;