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