No issues found
1 /*
2 * languages.c
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) version 3.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with the program; if not, see <http://www.gnu.org/licenses/>
16 *
17 */
18
19 #ifdef HAVE_CONFIG_H
20 #include <config.h>
21 #endif
22
23 #include "languages.h"
24
25 #include <glib/gi18n-lib.h>
26
27 static const gchar **mime_types = NULL;
28 G_LOCK_DEFINE_STATIC (mime_types);
29
30 static Language languages[] = {
31
32 { "txt", N_("_Plain text"),
33 (const gchar *[]) { (gchar[]) { "text" }, NULL },
34 (const gchar *[]) { (gchar[]) { "text/plain" },
35 (gchar[]) { "text/*" }, NULL }
36 },
37
38 { "assembler", N_("_Assembler"),
39 (const gchar *[]) { (gchar[]) { "asm" } , NULL },
40 (const gchar *[]) { (gchar[]) { "text/x-asm" }, NULL }
41 },
42
43 { "sh", N_("_Bash"),
44 (const gchar *[]) { (gchar[]) { "bash" }, (gchar[]) { "sh" },
45 (gchar[]) { "ebuild" }, (gchar[]) {"eclass" },
46 NULL },
47 (const gchar *[]) { (gchar[]) { "application/x-bsh" },
48 (gchar[]) { "application/x-sh" },
49 (gchar[]) { "application/x-shar" },
50 (gchar[]) { "application/x-shellscript" },
51 (gchar[]) { "text/x-script.sh" }, NULL }
52 },
53
54 { "c", N_("_C/C++"),
55 (const gchar *[]) { (gchar[]) { "c" }, (gchar[]) { "c++" },
56 (gchar[]) { "cc" }, (gchar[]) { "cpp" },
57 (gchar[]) { "cu" }, (gchar[]) { "cxx" },
58 (gchar[]) { "h" }, (gchar[]) { "hh" },
59 (gchar[]) { "hpp" }, (gchar[]) { "hxx" }, NULL },
60 (const gchar *[]) { (gchar[]) { "text/x-c" }, NULL }
61 },
62
63 { "csharp", N_("_C#"),
64 (const gchar *[]) { (gchar[]) { "cs" }, NULL },
65 (const gchar *[]) { (gchar[]) { "text/x-csharp" }, NULL }
66 },
67
68 { "css", N_("_Cascade Style Sheet"),
69 (const gchar *[]) { (gchar[]) { "css" }, NULL },
70 (const gchar *[]) { (gchar[]) { "text/css" }, NULL }
71 },
72
73 { "html", N_("_HTML"),
74 (const gchar *[]) { (gchar[]) { "html" }, (gchar[]) { "html" },
75 (gchar[]) { "xhtml" }, (gchar[]) { "dhtml" }, NULL },
76 (const gchar *[]) { NULL } /* Don't register text-highlight as formatter
77 or parser of text / html as it would break
78 all text / html emails. */
79 },
80
81 { "java", N_("_Java"),
82 (const gchar *[]) { (gchar[]) { "java" }, (gchar[]) { "groovy" },
83 (gchar[]) { "grv" }, NULL },
84 (const gchar *[]) { (gchar[]) { "text/java-source" }, NULL }
85 },
86
87 { "js", N_("_JavaScript"),
88 (const gchar *[]) { (gchar[]) { "js" }, NULL },
89 (const gchar *[]) { (gchar[]) { "text/javascript" },
90 (gchar[]) { "application/x-javascript" }, NULL }
91 },
92
93 { "diff", N_("_Patch/diff"),
94 (const gchar *[]) { (gchar[]) { "diff" }, (gchar[]) { "patch" }, NULL },
95 (const gchar *[]) { (gchar[]) { "text/x-diff" },
96 (gchar[]) { "text/x-patch" }, NULL }
97 },
98
99 { "perl", N_("_Perl"),
100 (const gchar *[]) { (gchar[]) { "perl" }, (gchar[]) { "cgi"},
101 (gchar[]) { "perl" }, (gchar[]) { "pl" },
102 (gchar[]) { "plex" }, (gchar[]) { "plx" },
103 (gchar[]) { "pm" }, NULL },
104 (const gchar *[]) { (gchar[]) { "text/x-script.perl" },
105 (gchar[]) { "text/x-script.perl-module" },
106 (gchar[]) { "application/x-pixclscript" },
107 (gchar[]) { "application/x-xpixmap" }, NULL }
108 },
109
110 { "php", N_("_PHP"),
111 (const gchar *[]) { (gchar[]) { "php" }, (gchar[]) { "php3" },
112 (gchar[]) { "php4" }, (gchar[]) { "php5" },
113 (gchar[]) { "php6" }, NULL },
114 (const gchar *[]) { (gchar[]) { "text/php" },
115 (gchar[]) { "text/x-php" },
116 (gchar[]) { "application/php" },
117 (gchar[]) { "application/x-php" },
118 (gchar[]) { "application/x-httpd-php" },
119 (gchar[]) { "application/x-httpd-php-source" },
120 NULL }
121 },
122
123 { "python", N_("_Python"),
124 (const gchar *[]) { (gchar[]) { "py" }, NULL },
125 (const gchar *[]) { (gchar[]) { "text/x-script.python" }, NULL }
126 },
127
128 { "ruby", N_("_Ruby"),
129 (const gchar *[]) { (gchar[]) { "ruby" }, (gchar[]) { "pp" },
130 (gchar[]) { "rb" }, (gchar[]) { "rjs" },
131 (gchar[]) { "ruby" }, NULL },
132 (const gchar *[]) { (gchar[]) { "application/x-ruby" }, NULL }
133 },
134
135 { "tcl", N_("_Tcl/Tk"),
136 (const gchar *[]) { (gchar[]) { "tcl" }, (gchar[]) { "ictl" },
137 (gchar[]) { "wish" }, NULL },
138 (const gchar *[]) { (gchar[]) { "text/x-tcl" }, NULL }
139 },
140
141 { "tex", N_("_TeX/LaTeX"),
142 (const gchar *[]) { (gchar[]) { "tex" }, (gchar[]) { "csl" },
143 (gchar[]) { "sty" }, NULL },
144 (const gchar *[]) { (gchar[]) { "text/x-tex" }, NULL }
145 },
146
147 { "vala", N_("_Vala"),
148 (const gchar *[]) { (gchar[]) { "vala" }, NULL },
149 (const gchar *[]) { (gchar[]) { "text/x-vala" }, NULL }
150 },
151
152 { "vb", N_("_Visual Basic"),
153 (const gchar *[]) { (gchar[]) { "vb" }, (gchar[]) { "bas" },
154 (gchar[]) { "basic" }, (gchar[]) { "bi" },
155 (gchar[]) { "vbs" }, NULL },
156 (const gchar *[]) { NULL }
157 },
158
159 { "xml", N_("_XML"),
160 (const gchar *[]) { (gchar[]) { "xml" }, (gchar[]) { "dtd" },
161 (gchar[]) { "ecf" }, (gchar[]) { "ent" },
162 (gchar[]) { "hdr" }, (gchar[]) { "hub" },
163 (gchar[]) { "jnlp" }, (gchar[]) { "nrm" },
164 (gchar[]) { "resx" }, (gchar[]) { "sgm" },
165 (gchar[]) { "sgml" }, (gchar[]) { "svg" },
166 (gchar[]) { "tld" }, (gchar[]) { "vxml" },
167 (gchar[]) { "wml" }, (gchar[]) { "xsd" },
168 (gchar[]) { "xsl" }, NULL },
169 (const gchar *[]) { (gchar[]) { "text/xml" },
170 (gchar[]) { "application/xml" },
171 (gchar[]) { "application/x-xml" }, NULL }
172 }
173 };
174
175 static struct Language other_languages[] = {
176
177 { "actionscript", N_("_ActionScript"),
178 (const gchar *[]) { (gchar[]) { "as" }, NULL },
179 (const gchar *[]) { NULL }
180 },
181
182 { "ada", N_("_ADA95"),
183 (const gchar *[]) { (gchar[]) { "a" }, (gchar[]) { "adb" },
184 (gchar[]) { "ads" }, (gchar[]) { "gnad" },
185 NULL },
186 (const gchar *[]) { (gchar[]) { "text/x-adasrc" }, NULL }
187 },
188
189 { "algol", N_("_ALGOL 68"),
190 (const gchar *[]) { (gchar[]) { "alg" }, NULL },
191 (const gchar *[]) { NULL }
192 },
193
194 { "awk", N_("(_G)AWK"),
195 (const gchar *[]) { (gchar[]) { "awk" }, NULL },
196 (const gchar *[]) { (gchar[]) { "text/x-awk" }, NULL }
197 },
198
199 { "cobol", N_("_COBOL"),
200 (const gchar *[]) { (gchar[]) { "cbl" }, (gchar[]) { "cob" }, NULL },
201 (const gchar *[]) { (gchar[]) { "text/x-cobol" }, NULL }
202 },
203
204 { "bat", N_("_DOS Batch"),
205 (const gchar *[]) { (gchar[]) { "bat" }, (gchar[]) { "cmd" }, NULL },
206 (const gchar *[]) { NULL }
207 },
208
209 { "d", N_("_D"),
210 (const gchar *[]) { (gchar[]) { "d" }, NULL },
211 (const gchar *[]) { NULL }
212 },
213
214 { "erlang", N_("_Erlang"),
215 (const gchar *[]) { (gchar[]) { "erl" }, (gchar[]) { "hrl" }, NULL },
216 (const gchar *[]) { (gchar[]) { "text/x-erlang" }, NULL }
217 },
218
219 { "fortran77", N_("_FORTRAN 77"),
220 (const gchar *[]) { (gchar[]) { "f" }, (gchar[]) { "for" },
221 (gchar[]) { "ftn" }, NULL },
222 (const gchar *[]) { (gchar[]) { "text/x-fortran" }, NULL }
223 },
224
225 { "fortran90", N_("_FORTRAN 90"),
226 (const gchar *[]) { (gchar[]) { "f90" }, (gchar[]) { "f95" }, NULL },
227 (const gchar *[]) { (gchar[]) { "text/x-fortran" }, NULL }
228 },
229
230 { "fsharp", N_("_F#"),
231 (const gchar *[]) { (gchar[]) { "fs" }, (gchar[]) { "fsx" }, NULL },
232 (const gchar *[]) { NULL }
233 },
234
235 { "go", N_("_Go"),
236 (const gchar *[]) { (gchar[]) { "go" }, NULL },
237 (const gchar *[]) { (gchar[]) { "text/x-go" }, NULL }
238 },
239
240 { "haskell", N_("_Haskell"),
241 (const gchar *[]) { (gchar[]) { "hs" }, NULL },
242 (const gchar *[]) { (gchar[]) { "text/x-haskell" }, NULL }
243 },
244
245 { "jsp", N_("_JSP"),
246 (const gchar *[]) { (gchar[]) { "jsp" }, NULL },
247 (const gchar *[]) { (gchar[]) { "text/x-jsp" }, NULL }
248 },
249
250 { "lisp", N_("_Lisp"),
251 (const gchar *[]) { (gchar[]) { "cl" }, (gchar[]) { "clisp" },
252 (gchar[]) { "el" }, (gchar[]) { "lsp" },
253 (gchar[]) { "sbcl"}, (gchar[]) { "scom" },
254 NULL },
255 (const gchar *[]) { (gchar[]) { "text/x-emacs-lisp" }, NULL }
256 },
257
258 { "lotus", N_("_Lotus"),
259 (const gchar *[]) { (gchar[]) { "ls" }, NULL },
260 (const gchar *[]) { (gchar[]) { "application/vnd.lotus-1-2-3" }, NULL }
261 },
262
263 { "lua", N_("_Lua"),
264 (const gchar *[]) { (gchar[]) { "lua" }, NULL },
265 (const gchar *[]) { (gchar[]) { "text/x-lua" }, NULL }
266 },
267
268 { "maple", N_("_Maple"),
269 (const gchar *[]) { (gchar[]) { "mpl" }, NULL },
270 (const gchar *[]) { NULL }
271 },
272
273 { "matlab", N_("_Matlab"),
274 (const gchar *[]) { (gchar[]) { "m" }, NULL },
275 (const gchar *[]) { (gchar[]) { "text/x-matlab" }, NULL }
276 },
277
278 { "maya", N_("_Maya"),
279 (const gchar *[]) { (gchar[]) { "mel" }, NULL },
280 (const gchar *[]) { NULL }
281 },
282
283 { "oberon", N_("_Oberon"),
284 (const gchar *[]) { (gchar[]) { "ooc" }, NULL },
285 (const gchar *[]) { NULL }
286 },
287
288 { "objc", N_("_Objective C"),
289 (const gchar *[]) { (gchar[]) { "objc" }, NULL },
290 (const gchar *[]) { (gchar[]) { "text/x-objchdr" },
291 (gchar[]) { "text/x-objcsrc" }, NULL }
292 },
293
294 { "ocaml", N_("_OCaml"),
295 (const gchar *[]) { (gchar[]) { "ml" }, (gchar[]) { "mli" }, NULL },
296 (const gchar *[]) { (gchar[]) { "text/x-ocaml" }, NULL }
297 },
298
299 { "octave", N_("_Octave"),
300 (const gchar *[]) { (gchar[]) { "octave" }, NULL },
301 (const gchar *[]) { NULL }
302 },
303
304 { "os", N_("_Object Script"),
305 (const gchar *[]) { (gchar[]) { "os" }, NULL },
306 (const gchar *[]) { NULL }
307 },
308
309 { "pascal", N_("_Pascal"),
310 (const gchar *[]) { (gchar[]) { "pas" }, NULL },
311 (const gchar *[]) { (gchar[]) { "text/x-pascal" }, NULL }
312 },
313
314 { "pov", N_("_POV-Ray"),
315 (const gchar *[]) { (gchar[]) { "pov" }, NULL },
316 (const gchar *[]) { NULL }
317 },
318
319 { "pro", N_("_Prolog"),
320 (const gchar *[]) { (gchar[]) { "pro" }, NULL },
321 (const gchar *[]) { NULL }
322 },
323
324 { "ps", N_("_PostScript"),
325 (const gchar *[]) { (gchar[]) { "ps" } , NULL },
326 (const gchar *[]) { (gchar[]) { "application/postscript" }, NULL }
327 },
328
329 { "r", N_("_R"),
330 (const gchar *[]) { (gchar[]) { "r" }, NULL },
331 (const gchar *[]) { NULL }
332 },
333
334 { "spec", N_("_RPM Spec"),
335 (const gchar *[]) { (gchar[]) { "spec" }, NULL },
336 (const gchar *[]) { (gchar[]) { "text/x-rpm-spec" }, NULL }
337 },
338
339 { "scala", N_("_Scala"),
340 (const gchar *[]) { (gchar[]) { "scala" }, NULL },
341 (const gchar *[]) { (gchar[]) { "text/x-scala" }, NULL }
342 },
343
344 { "smalltalk", N_("_Smalltalk"),
345 (const gchar *[]) { (gchar[]) { "gst" }, (gchar[]) { "sq" },
346 (gchar[]) { "st" }, NULL },
347 (const gchar *[]) { NULL }
348 },
349
350 { "tcsh", N_("_TCSH"),
351 (const gchar *[]) { (gchar[]) { "tcsh" }, NULL },
352 (const gchar *[]) { NULL }
353 },
354
355 { "vhd", N_("_VHDL"),
356 (const gchar *[]) { (gchar[]) { "vhd" }, NULL },
357 (const gchar *[]) { (gchar[]) { "text/x-vhdl" }, NULL }
358 }
359 };
360
361 Language *
362 get_default_langauges (gsize *len)
363 {
364 if (len) {
365 *len = G_N_ELEMENTS (languages);
366 }
367
368 return languages;
369 }
370
371 Language *
372 get_additinal_languages (gsize *len)
373 {
374 if (len) {
375 *len = G_N_ELEMENTS (other_languages);
376 }
377
378 return other_languages;
379 }
380
381 const gchar *
382 get_syntax_for_ext (const gchar *extension)
383 {
384 gint i;
385 gint j;
386
387 for (i = 0; i < G_N_ELEMENTS (languages); i++) {
388
389 Language *lang = &languages[i];
390 const gchar *ext;
391
392 j = 0;
393 ext = lang->extensions[j];
394 while (ext) {
395 if (g_ascii_strncasecmp (ext, extension, strlen (ext)) == 0) {
396 return lang->action_name;
397 }
398
399 j++;
400 ext = lang->extensions[j];
401 }
402 }
403
404 for (i = 0; i < G_N_ELEMENTS (other_languages); i++) {
405
406 Language *lang = &other_languages[i];
407 const gchar *ext;
408
409 j = 0;
410 ext = lang->extensions[j];
411 while (ext) {
412 if (g_ascii_strncasecmp (ext, extension, strlen (ext)) == 0) {
413 return lang->action_name;
414 }
415
416 j++;
417 ext = lang->extensions[j];
418 }
419 }
420
421 return NULL;
422 }
423
424 const gchar *
425 get_syntax_for_mime_type (const gchar *mime_type)
426 {
427 gint i;
428 gint j;
429
430 for (i = 0; i < G_N_ELEMENTS (languages); i++) {
431
432 Language *lang = &languages[i];
433 const gchar *mt;
434
435 j = 0;
436 mt = lang->mime_types[j];
437 while (mt) {
438 if (g_ascii_strncasecmp (mt, mime_type, strlen (mt)) == 0) {
439 return lang->action_name;
440 }
441
442 j++;
443 mt = lang->mime_types[j];
444 }
445 }
446
447 for (i = 0; i < G_N_ELEMENTS (other_languages); i++) {
448
449 Language *lang = &other_languages[i];
450 const gchar *mt;
451
452 j = 0;
453 mt = lang->mime_types[j];
454 while (mt) {
455 if (g_ascii_strncasecmp (mt, mime_type, strlen (mt)) == 0) {
456 return lang->action_name;
457 }
458
459 j++;
460 mt = lang->mime_types[j];
461 }
462 }
463
464 return NULL;
465 }
466
467 const gchar **
468 get_mime_types (void)
469 {
470 G_LOCK (mime_types);
471 if (mime_types == NULL) {
472 gchar **list;
473 gsize array_len;
474 gint i, pos;
475
476 array_len = G_N_ELEMENTS (languages);
477 pos = 0;
478
479 list = g_malloc (array_len * sizeof (gchar *));
480
481 for (i = 0; i < G_N_ELEMENTS (languages); i++) {
482 Language *lang = &languages[i];
483
484 gint j = 0;
485 while (lang->mime_types[j] != NULL) {
486 if (pos == array_len) {
487 array_len += 10;
488 list = g_realloc (list, array_len * sizeof (gchar *));
489 }
490
491 list[pos] = (gchar *) lang->mime_types[j];
492 pos++;
493 j++;
494 }
495 }
496
497 for (i = 0; i < G_N_ELEMENTS (other_languages); i++) {
498 Language *lang = &other_languages[i];
499
500 gint j = 0;
501 while (lang->mime_types[j] != NULL) {
502 if (pos == array_len) {
503 array_len += 10;
504 list = g_realloc (list, array_len * sizeof (gchar *));
505 }
506
507 list[pos] = (gchar *) lang->mime_types[j];
508 pos++;
509 j++;
510 }
511 }
512
513 if (pos == array_len) {
514 array_len += 1;
515 list = g_realloc (list, array_len * sizeof (gchar *));
516 }
517
518 /* Ensure the array is null-terminated */
519 for (i = pos; i < array_len; i++) {
520 list[i] = NULL;
521 }
522
523 mime_types = (const gchar **) list;
524 }
525 G_UNLOCK (mime_types);
526
527 return mime_types;
528 }