File: syntax.sl

package info (click to toggle)
jed 0.98.7-14
  • links: PTS
  • area: main
  • in suites: slink
  • size: 3,088 kB
  • ctags: 3,851
  • sloc: ansic: 29,315; makefile: 257; sh: 248
file content (139 lines) | stat: -rw-r--r-- 3,155 bytes parent folder | download | duplicates (2)
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
% syntax.sl		-*- SLang -*-
% The functions here are used to manipulate the syntax keyword tables

define syntax_strcmp ()
{
   strcmp ((), ()) < 0;
}

%!% Prototype: String add_keywords (String tbl, String kws, Int len, Int n);
%!%
%!% Adds a set of keywords `kws', each of length `len', to the already
%!% existing syntax table `tbl'.  For convenience of the user, the function
%!% does alphabetical sorting and removes duplicate entries.
%!%
%!% The previous list of keywords is returned.
%!% Related Functions: @define_keywords_n@, @create_syntax_table@, @add_keyword_n@
define add_keywords (tbl, kws, len, n)
{
   variable okws, a, i, j, num, idx;

   % old keywords
   okws = define_keywords_n (tbl, Null_String, len, n);
   kws = okws + kws;

   num = strlen (kws) / len;
   !if (num) return Null_String;

   a = String_Type [num];	% array to sort
   num--;				% maximum array index

   _for (0, num, 1)
     {
	i = ();
	a [i] = substr (kws, 1 + i * len, len);
     }

   % remove duplicate entries
   _for (0, num, 1)
     {
	i = ();
	kws = a [i];
	i++;
	_for (i, num, 1)
	  {
	     j = ();
	     !if (strcmp (kws, a [j]))
	       a [j] = Null_String;
	  }
     }

   idx = array_sort (a, "syntax_strcmp");
   kws = Null_String;
   _for (0, num, 1)
     {
	i = ();
	kws += a [idx [i]];
     }

   () = define_keywords_n (tbl, kws, len, n);
   okws;
}

%!% Prototype: Void add_keyword_n (String tbl, String kw, Int n);
%!%
%!% Adds a single keyword `kw' to the already existing syntax table `tbl'.
%!% Related Functions: @define_keywords_n@, @create_syntax_table@, @add_keywords@
define add_keyword_n (tbl, kw, n)
{
   variable len = strlen (kw);
   !if (len) return;
   () = add_keywords (tbl, kw, len, n);
}

%!% Prototype: Void add_keyword (String tbl, String kw);
%!%
%!% Adds a single keyword `kw' to the already existing syntax table `tbl'.
%!% Related Functions: @define_keywords_n@, @create_syntax_table@, @add_keyword_n@
define add_keyword ()
{
   add_keyword_n (0);
}

%!% Prototype: String remove_keywords (String tbl, String kws, Int len, Int n);
%!%
%!% Removes a set of keywords `kws', each of length `len', from the already
%!% existing syntax table `tbl'.
%!%
%!% The previous list of keywords is returned.
%!% Related Functions: @add_keywords@, @define_keywords_n@, @create_syntax_table@, @add_keyword_n@
define remove_keywords (tbl, kws, len, n)
{
   variable okws, num, nrem, a, i, rm;

   EXIT_BLOCK
     {
	() = define_keywords_n (tbl, kws, len, n);
	okws;
     }

   % the old keywords
   okws = define_keywords_n (tbl, Null_String, len, n);
   num = strlen (okws) / len;

   nrem = strlen (kws) / len;
   !if (nrem)
     {
	kws = okws;
	return;
     }

   a = String_Type [num];	       % tmp array  
   num--;			       % maximum array index  

   _for (0, num, 1)
     {
	i = ();
	a [i] = substr (okws, 1 + i * len, len);
     }

   % remove duplicate entries
   _for (0, nrem, 1)
     {
	i = ();
	rm = substr (kws, 1 + i * len, len);
	_for (0, num, 1)
	  {
	     i = ();
	     !if (strcmp (rm, a [i]))
	       a [i] = Null_String;
	  }
     }

   kws = Null_String;
   _for (0, num, 1)
     {
	i = ();
	kws += a[i];
     }
}