1 | #!/usr/local/bin/perl |
---|
2 | ################################################################### |
---|
3 | # |
---|
4 | # FILE: cmpl.pl |
---|
5 | # PURPOSE: generate completion strings for singular emacs mode |
---|
6 | # AUTHOR: obachman |
---|
7 | #### |
---|
8 | $Usage = <<EOT; |
---|
9 | $0 [-Singular binary] [-hlp file] [cmpl 'hlp|cmd|lib'] [-help] |
---|
10 | Generates completion strings for singular emacs mode and prints it to STDOUT |
---|
11 | EOT |
---|
12 | |
---|
13 | # |
---|
14 | # default settings of command-line arguments |
---|
15 | # |
---|
16 | $Singular = "../Singular/Singular"; |
---|
17 | $hlp = "../doc/singular.hlp"; |
---|
18 | $cmpl = "cmd"; |
---|
19 | $ex_dir= "../examples"; |
---|
20 | %symbol = (cmd=>"singular-commands-alist", |
---|
21 | hlp=>"singular-help-topics-alist", |
---|
22 | lib=>"singular-standard-libraries-alist", |
---|
23 | ex=>"singular-examples-alist"); |
---|
24 | |
---|
25 | # |
---|
26 | # parse command line options |
---|
27 | # |
---|
28 | while (@ARGV && $ARGV[0] =~ /^-/) |
---|
29 | { |
---|
30 | $_ = shift(@ARGV); |
---|
31 | if (/^-S(ingular)?$/) { $Singular = shift(@ARGV); next;} |
---|
32 | if (/^-hlp$/) { $hlp = shift(@ARGV); next;} |
---|
33 | if (/^-c(mpl)?$/) { $cmpl = shift(@ARGV); next;} |
---|
34 | if (/^-e(x_dir)?$/) { $ex_dir = shift(@ARGV); next;} |
---|
35 | if (/^-h(elp)?$/) { print $Usage; exit;} |
---|
36 | die "Unrecognized option: $_\n$Usage"; |
---|
37 | } |
---|
38 | |
---|
39 | # |
---|
40 | # get array of strings |
---|
41 | # |
---|
42 | if ($cmpl eq 'cmd') |
---|
43 | { |
---|
44 | $strings = `$Singular -tq --exec='reservedName();\$'`; |
---|
45 | die "Error in execution of: $Singular -tq --exec='reservedName();\$': $!\n" |
---|
46 | if ($?); |
---|
47 | @strings = map "(\"$_\")", split(/\s+/, $strings); |
---|
48 | } |
---|
49 | elsif ($cmpl eq 'lib') |
---|
50 | { |
---|
51 | # Sort libraries in REVERSE order! (Menu is created from bottom to |
---|
52 | # top in singular.el) |
---|
53 | @strings = map "(\"$_\")", sort {$b cmp $a} split(/\s+/, <>); |
---|
54 | } |
---|
55 | elsif ($cmpl eq 'hlp') |
---|
56 | { |
---|
57 | open(FH, "<$hlp") || die "Can not open file $hlp: $!\n"; |
---|
58 | while (<FH>) |
---|
59 | { |
---|
60 | if (/Node: Index/) |
---|
61 | { |
---|
62 | while (<FH>) |
---|
63 | { |
---|
64 | last if /\* Menu:/; |
---|
65 | } |
---|
66 | <FH>; |
---|
67 | while (<FH>) |
---|
68 | { |
---|
69 | last if /^\s*$/; |
---|
70 | if ( /^\* (.*):\s*(.*).\s*/ ) { |
---|
71 | $node = $1; $rawNode = $2; |
---|
72 | # remove duplicate counter from node |
---|
73 | $node =~ s/(.*) <\d+>$/$1/; |
---|
74 | # quote characters special to Emacs |
---|
75 | $node =~ s/([\"#\\])/\\\1/g; #' |
---|
76 | $rawNode =~ s/([\"#\\])/\\\1/g; #' |
---|
77 | # only the first occurence of $node is inserted to @string! |
---|
78 | # all subsequent entries are discarded. |
---|
79 | push @strings, "(\"$node\" . \"$rawNode\")" if $node ne $prev; |
---|
80 | $prev = $node; |
---|
81 | } |
---|
82 | } |
---|
83 | last; |
---|
84 | } |
---|
85 | } |
---|
86 | close(FH); |
---|
87 | } |
---|
88 | elsif ($cmpl eq 'ex') |
---|
89 | { |
---|
90 | @strings = <$ex_dir/*.sing>; |
---|
91 | map {$_ =~ s|.*/(.*?)\.sing$|(\"$1\")|;} @strings; |
---|
92 | } |
---|
93 | else |
---|
94 | { |
---|
95 | die "Error: Can not make completion list for $cmpl\n $Usage"; |
---|
96 | } |
---|
97 | print STDOUT <<EOT; |
---|
98 | ; Do not edit this file: It was automatically generated by $0 |
---|
99 | (setq $symbol{$cmpl} |
---|
100 | '( |
---|
101 | EOT |
---|
102 | #' prevents breaking of fontification |
---|
103 | |
---|
104 | foreach $string (@strings) |
---|
105 | { |
---|
106 | print STDOUT " $string\n"; |
---|
107 | } |
---|
108 | |
---|
109 | print STDOUT "))\n"; |
---|
110 | |
---|