huangcm
2024-12-18 9d29be7f7249789d6ffd0440067187a9f040c2cd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/usr/bin/env tclsh
# vim:se syn=tcl:
 
set filename [lindex $argv 0]
set f [open $filename]
 
# Read the file looking for command definitions
set lines {}
set commands {}
array set cdict {}
set c 0
 
while {[gets $f buf] >= 0} {
   if {[string match "~~*" $buf]} {
       if {[string match "*:*" $prev]} {
           incr c
           set target cmd_$c
           set lines [linsert $lines end-1 "\[\[$target\]\]"]
       } else {
           set target _$prev
       }
       foreach cmd [split $prev ":,"] {
           set cmd [string trim $cmd]
           if {[regexp {^[a-z.]+$} $cmd]} {
               lappend commands [list $cmd $target]
               set cdict($cmd) $target
           }
       }
   }
   lappend lines $buf
   set prev $buf
}
close $f
 
# Build the command index in the list: $index
lappend index {[frame="none",grid="none"]}
lappend index {|=========================}
set i 0
set row {}
foreach command [lsort $commands] {
   lassign $command cmd target
 
   append row "|<<$target,*`$cmd`*>> "
   incr i
   if {$i % 8 == 0} {
       lappend index $row
       set row {}
   }
}
while {$i % 8 != 0} {
   incr i
   append row "| "
}
lappend index $row
lappend index {|=========================}
 
# Map all `cmd` to <<$target,`cmd`>>
set mapping {}
foreach c [array names cdict] {
   lappend mapping `$c` <<$cdict($c),*`$c`*>>
   lappend mapping "`$c " "<<$cdict($c),*`$c`*>> `"
}
 
# And the command index
lappend mapping @INSERTINDEX@ [join $index \n]
 
# Output the result
foreach line $lines {
   puts [string map $mapping $line]
}