-
Notifications
You must be signed in to change notification settings - Fork 0
/
po_mode.sl
4941 lines (4154 loc) · 135 KB
/
po_mode.sl
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
% -*- mode: slang; mode: fold -*-
% _debug_info=1;_traceback=1;_slangtrace=1;
%{{{ licence, presentation
%% po_mode.sl -- an Emacs-like Jed editing mode for
%% Gettext portable object files (*.po)
%%
%% License: http://www.fsf.org/copyleft/gpl.html
%% Author : Morten Bo Johansen <mbj@mbjnet.dk>
%%
%% Thanks to Paul Boekholt for some general hints relating to S-Lang
%% scripting and to Günter Milde for including it in the jed-extra
%% package in Debian.
%%
%% Tested with Jed 0.99.19 on Linux, compiled against slang 2.2
%% Superficially tested with emulations, cua, edt, emacs, ide, jed and
%% wordstar. You must edit several functions to make it usable on
%% non-Unix systems.
%%
%% Copy this file and po_mode.hlp to a directory in your jed library path,
%% e.g. /usr/share/jed/lib. For initialization, either use make_ini()
%% (from jedmodes.sf.net/mode/make_ini/) or copy the content of the
%% INITIALIZATION block to your .jedrc.
%%
%% For the rest, please refer to the po_mode.hlp file for the ins and outs
%% of this mode. There is some rather important information therein, so you
%% should read it ;-). If you copied it to the same place as this file, it
%% should be available in po_mode by typing '?'.
%%
define show_version_and_licence_info ()
{
variable license =
["po-mode for Jed, version: $Version"$,
"Copyright (C) 2015-: Morten Bo Johansen <mbj@mbjnet.dk>",
"License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>",
"This is free software: you are free to change and redistribute it.",
"There is ABSOLUTELY NO WARRANTY"];
pop2buf ("***Version and License Information***");
insert (strjoin (license, "\n"));
set_buffer_modified_flag (0);
most_mode ();
bob ();
}
%}}}
%{{{ Prototypes
public define po_mode ();
%}}}
%{{{ debian initialization block
#<INITIALIZATION>
autoload ("po_mode", "po_mode");
add_mode_for_extension ("po", "po");
add_mode_for_extension ("po", "pot");
#</INITIALIZATION>
%}}}
%{{{ modules/autoloads/
import ("pcre");
autoload ("most_exit_most", "most");
autoload ("grep", "grep.sl");
() = evalfile ("keydefs");
%}}}
%{{{ user customization variables
%% Your name and email address as translator
custom_variable ("Translator", "");
%% Email address of your language team
custom_variable ("Team_Email", "");
%% Your language - use English adjective, e.g. "German", "Danish" etc.
custom_variable ("Language", "");
%% The two-letter code for your language.
%% See http://en.wikipedia.org/wiki/List_of_ISO_639-1_codes
custom_variable ("LangCode", "");
%% The two-letter code for your country.
%% See http://en.wikipedia.org/wiki/ISO_3166-1_alpha-2
custom_variable ("CountryCode", "");
% Default character encoding of po-file. utf-8, iso-8859-1, etc
custom_variable ("Encoding", "utf-8");
%% Path to directory where source archives are usually kept
custom_variable ("SrcBaseDir", expand_filename ("~/src"));
%% Language pair for machine translation system, Apertium
%% E.g. "en-es" will use the English-to-Spanish pair.
custom_variable ("Apertium_Langs", "");
%% Your chosen spell checker. Use "aspell" or "hunspell" (recomended)
custom_variable ("Spell_Prg", "");
%% po-files in languages that you want to compare your translations
%% with. A comma-separated string of iso language codes as listed in
%% http://en.wikipedia.org/wiki/List_of_ISO_639-1_codes. Change the
%% codes below to your liking. This example will compare with Swedish,
%% Norwegian Bokmål, German, French and Dutch.
custom_variable ("Compare_With_Languages", "");
%% Path to your translation compendium file
custom_variable ("Compendium", expand_filename ("~/.compendium_pomode"));
%}}}
%{{{ private variables
private variable
Mode = "PO",
Buf = "",
Edit_Mode = "Po_Edit",
Edit_Buf = " *Translate*",
Msgid_Buf = "Msgid",
Po_Comment_Buf = "*Edit User Comment*",
Po_Tmpfile = make_tmp_file ("po_tmpfile"),
gettext_err_msg = "/tmp/gettext_err_msg",
Msgids = Array_Type,
Msgstrs = Array_Type,
Obsolete = Array_Type,
Po_Dir = Null_String,
Msgstr = Null_String,
Last_Edited = Null_String,
Msgstr_Copy = Null_String,
Entry = Assoc_Type[String_Type, ""],
CompHash = Assoc_Type[String_Type, ""],
CompHash_Fuzzy = Assoc_Type[String_Type, ""],
Trans_Hash = Assoc_Type[String_Type, ""],
Po_Buf = "",
Entry_No = 0,
Multi_Line = 0,
Gettext_Wrap = 0,
Translation_Status = 0,
StrsLists_Exists = 0,
Trans_Hash_Exists = 0,
Compendium_Is_Hashed = 0,
Version = "1.2.1",
Wrap = 79;
%}}}
%{{{ buffer local variables
%% Make all count variables local to the buffer
private define count (type)
{
switch (type)
{ case "t": get_blocal_var ("translated"); }
{ case "u": get_blocal_var ("untranslated"); }
{ case "f": get_blocal_var ("fuzzy"); }
{ case "o": get_blocal_var ("obsolete"); }
{ case "t+": set_blocal_var (get_blocal_var ("translated") + 1, "translated"); }
{ case "t-": set_blocal_var (get_blocal_var ("translated") - 1, "translated"); }
{ case "u+": set_blocal_var (get_blocal_var ("untranslated") + 1, "untranslated"); }
{ case "u-": set_blocal_var (get_blocal_var ("untranslated") - 1, "untranslated"); }
{ case "f+": set_blocal_var (get_blocal_var ("fuzzy") + 1, "fuzzy"); }
{ case "f-": set_blocal_var (get_blocal_var ("fuzzy") - 1, "fuzzy"); }
{ case "o+": set_blocal_var (get_blocal_var ("obsolete") + 1, "obsolete"); }
{ case "o-": set_blocal_var (get_blocal_var ("obsolete") - 1, "obsolete"); }
}
%}}}
%{{{ dfa syntax and colors
#ifdef HAS_DFA_SYNTAX
% %%% DFA_CACHE_BEGIN %%%
create_syntax_table (Mode);
private define setup_dfa_callback (Mode)
{
custom_color ("tokens", get_color ("keyword")); % msgstr, msgid keywords
custom_color ("flagcomment", get_color ("comment")); % e.g. fuzzy flag comment
custom_color ("usercomment", get_color ("string")); % translators note to entry
custom_color ("begblank", get_color ("number"), exch ()); % whitespace at beg/end of line
custom_color ("begblank1", get_color ("number"), exch ()); % whitespace at beg/end of line
custom_color ("endblank", get_color ("number"), exch ()); % whitespace at beg/end of line
custom_color ("srccomment", get_color ("number")); % source reference comments
custom_color ("extrcomment", get_color ("comment")); % developer's notes to translator
custom_color ("ctxcomment", get_color ("comment")); % context comments
custom_color ("lbreak", get_color ("bold")); % newline literals "\n"
custom_color ("pyvar", get_color ("string"));
custom_color ("pyvar1", get_color ("string"));
custom_color ("format_specifier", get_color ("string")); % "%d" "%s" "%f"
% dfa_enable_highlight_cache ("po.dfa", Mode);
dfa_define_highlight_rule ("^msg(id_?p?l?u?r?a?l?|str)", "tokens", Mode);
dfa_define_highlight_rule ("^#, .*", "flagcomment", Mode);
dfa_define_highlight_rule ("^\" +", "begblank", Mode);
dfa_define_highlight_rule ("[^\\\\]\" +", "begblank1", Mode);
dfa_define_highlight_rule ("[ \t]+\\\\?n?\"$", "endblank", Mode);
dfa_define_highlight_rule ("\\\\n", "lbreak", Mode);
dfa_define_highlight_rule ("^#: .*$", "srccomment", Mode); % src
dfa_define_highlight_rule ("^msgctxt.*$", "extrcomment", Mode);
dfa_define_highlight_rule ("^#\\| .*$", "ctxcomment", Mode);
dfa_define_highlight_rule ("^#\\. ?.*$", "extrcomment", Mode);
dfa_define_highlight_rule ("^# .*$", "usercomment", Mode);
dfa_define_highlight_rule ("%\\([0-9A-Za-z_]+\\)", "pyvar", Mode);
dfa_define_highlight_rule ("{.*}", "pyvar1", Mode);
dfa_define_highlight_rule ("%[bBcdfFls]+", "format_specifier", Mode);
dfa_build_highlight_table (Mode);
enable_dfa_syntax_for_mode (Mode);
}
dfa_set_init_callback (&setup_dfa_callback, Mode);
%%% DFA_CACHE_END %%%
#endif
%}}}
%{{{ delimitation, navigation
% Find the beginning of an entry
private define find_entry_delim_beg ()
{
if (eolp () && bolp () && not eobp () || bobp ())
return;
ifnot (bol_bsearch ("\n"))
bob ();
}
% Find the end of an entry
private define find_entry_delim_end ()
{
find_entry_delim_beg ();
go_down_1 ();
ifnot (bol_fsearch ("\n"))
eob ();
}
%% Determine if an entry is untranslated.
private define is_untranslated ()
{
push_spot ();
((down (1)) && (not looking_at ("\"")) && (up (1)) && (blooking_at (" \"\"")));
pop_spot ();
}
private define is_translated ()
{
not is_untranslated ();
}
% Does the entry have a "fuzzy" tag
private define is_fuzzy ()
{
push_spot ();
find_entry_delim_beg ();
push_mark ();
() = bol_fsearch ("msgid");
variable str = bufsubstr ();
((is_substr (str, "#, fuzzy")) && (not (is_substr (str, "#~ "))));
pop_spot ();
}
% Mark a whole entry
private define mark_entry ()
{
find_entry_delim_beg ();
ifnot (bobp ())
go_right_1 ();
push_visible_mark ();
find_entry_delim_end ();
ifnot (eobp)
go_left_1 ();
}
private define get_current_entry_number ()
{
variable i = 0;
push_spot ();
find_entry_delim_end ();
while (bol_bsearch ("msgid"))
i++;
pop_spot ();
return i;
}
% Jump to the msgstr keyword
private define find_msgstr_keyword ()
{
bol ();
if (looking_at ("#~"))
throw UsageError, "obsolete entry";
if (eolp () && bolp () || bobp ())
{
ifnot (bol_fsearch ("msgstr"))
{
() = bol_bsearch ("msgstr");
}
return;
}
if (re_looking_at ("^\""))
{
() = bol_bsearch ("msgid");
}
do
{
if (looking_at ("msgstr"))
return;
if (looking_at ("msgid "))
{
() = bol_fsearch ("msgstr");
return;
}
if (looking_at ("msgid_plural"))
{
() = bol_fsearch ("msgstr[1]");
return;
}
}
while (down (1));
}
% Jump to the msgid keyword
private define find_msgid_keyword ()
{
find_msgstr_keyword ();
if ((looking_at ("msgstr ")) || (looking_at ("msgstr[0]")))
() = bol_bsearch ("msgid ");
else
() = bol_bsearch ("msgid_plural");
}
private define mark_msgid ()
{
find_msgid_keyword ();
push_mark ();
go_right_1 ();
() = bol_fsearch ("msg");
go_left_1 ();
}
private define mark_msgstr ()
{
find_msgstr_keyword (); push_mark (); go_down_1 ();
do
{
if ((looking_at ("\n")) || (looking_at ("msgstr")))
break;
}
while (down (1));
go_left_1 ();
}
%% Mark a word and return it
private define po_mark_word ()
{
variable word = "";
define_word ("-A-Za-zÀ-ÿ");
push_spot ();
bskip_word_chars ();
push_mark ();
skip_word_chars ();
bufsubstr ();
pop_spot ();
}
% Return the whole entry as a string
private define entry_as_str ()
{
. push_spot mark_entry bufsubstr pop_spot
}
% Return the msgid as a string
private define msgid_as_str ()
{
. push_spot mark_msgid bufsubstr pop_spot
}
% Return the msgstr as a string
private define msgstr_as_str ()
{
. push_spot mark_msgstr bufsubstr pop_spot
}
define show_current_entry_number ()
{
variable n = get_current_entry_number ();
flush ("# $n"$);
}
% Jump to the next entry
define po_next_entry ()
{
find_msgid_keyword ();
go_right_1 ();
() = re_fsearch ("^msgid_?");
}
% Jump to the previous entry
define po_previous_entry ()
{
push_mark (); find_entry_delim_beg ();
if (bol_bsearch ("msgid"))
return pop_mark_0 ();
pop_mark_1 ();
}
% Put the entry in the top of the display
define top_justify_entry ()
{
find_entry_delim_beg (); go_right_1 (); recenter (1);
}
define goto_entry (n)
{
if (n < 0)
{
n = read_mini ("Go to entry number:", "", "");
n = integer (n);
}
if (n > get_blocal_var ("total_entries")+1)
throw UsageError, "no such entry";
bob ();
loop (n)
{
() = bol_fsearch ("msgid");
eol ();
}
bol ();
}
%% Goto next or previous translated or untranslated message
define find_msgstr (dir, status)
{
variable search_fun, move, translated;
if (status > 0)
translated = &is_translated ();
else translated = &is_untranslated ();
if (dir < 0)
{
search_fun = &bol_bsearch ();
move = &go_left_1 ();
}
else
{
search_fun = &bol_fsearch ();
move = &go_right_1 ();
}
push_mark ();
@move ();
while (@search_fun ("msgstr"))
{
if (@translated ())
{
if (status > 0)
{
if (is_fuzzy ())
{
@move ();
continue;
}
}
return pop_mark_0 ();
}
@move ();
}
flush ("no matches beyond this point");
return pop_mark_1 ();
}
%% Find the previous entry with fuzzy flag.
define bfind_fuzzy ()
{
find_entry_delim_beg ();
while (bol_bsearch ("#, fuzzy"))
{
push_spot ();
go_down_1 ();
if (looking_at ("#~"))
{
pop_spot ();
continue;
}
return pop_spot ();
}
flush ("no fuzzy entries above this point");
}
%% Find the next entry with fuzzy flag.
define find_fuzzy ()
{
push_mark ();
while (bol_fsearch ("#, fuzzy"))
{
go_down_1 ();
if (looking_at ("#~"))
{
pop_mark_0;
continue;
}
else return pop_mark_0;
}
flush ("wrapping search around buffer ...");
bob ();
ifnot (bol_fsearch ("#, fuzzy"))
{
flush ("no fuzzy entries");
pop_mark_1;
}
pop_mark_0;
}
%% Find the next obsolete entry.
define find_obsolete ()
{
push_mark ();
while (fsearch ("#~ msgid "))
{
() = bol_fsearch ("#~ msgstr ");
return pop_mark (0);
}
flush ("wrapping search around buffer ...");
bob ();
ifnot (fsearch ("#~ msgid "))
{
flush ("no obsolete entries");
pop_mark_1 ();
}
pop_mark_0 ();
}
%% Go to next entry that is either fuzzy or untranslated
define any_next_unfinished ()
{
push_mark ();
go_right_1 ();
while (bol_fsearch ("msgstr"))
{
if ((is_untranslated ()) || (is_fuzzy ()))
return pop_mark_0 ();
else
go_right_1 ();
}
flush ("wrapping search around buffer ...");
bob ();
while (bol_fsearch ("msgstr"))
{
if ((is_untranslated ()) || (is_fuzzy ()))
return pop_mark_0 ();
else
go_right_1 ();
}
flush ("no unfinished entries below this point");
pop_mark_1 ();
}
% Jump to the next translator comment
define find_translator_comment ()
{
push_mark ();
find_entry_delim_end ();
while (bol_fsearch ("# "))
{
() = bol_fsearch ("msgid");
return pop_mark (0);
}
flush ("wrapping search around buffer");
bob ();
while (bol_fsearch ("# "))
{
() = bol_fsearch ("msgid");
return pop_mark (0);
}
pop_mark (1);
flush ("no more translator comments");
}
%}}}
%{{{ statistics
%% The status line that shows the counts for translated, untranslated, fuzzy
%% and obsolete entries
define set_po_status_line ()
{
variable t, u, f, o, s, n, p, mode;
ifnot ("PO" == get_mode_name ())
return;
if (get_blocal_var ("View_Limit", 1))
return;
t = count ("t"); u = count ("u"); f = count ("f"); o = count ("o");
n = t + f + u;
if (n == 0) n = 1;
p = Sprintf ("(%S%s) ", (t*100)/n, "%%", 2);
s = Sprintf ("%dt/%du/%df/%do", t, u, f, o, 4);
set_status_line (" %b " + p + s + " (%m%a%n%o) %p %t", 0);
}
%% The actual counting of the various entries.
private define po_statistics ()
{
variable t, u, f, o, ct, cts, n = 0;
ifnot ("PO" == get_mode_name ())
return;
cts = {"translated","untranslated","fuzzy","obsolete","total_entries"};
foreach ct (cts)
{
create_blocal_var (ct);
set_blocal_var (0, ct);
}
t = count ("t"); u = count ("u"); f = count ("f"); o = count ("o");
push_spot_bob ();
() = bol_fsearch ("\n");
push_mark (); push_mark ();
while (bol_fsearch ("msgstr"))
{
if (is_untranslated ())
u++;
else
t++;
eol ();
}
pop_mark_1 ();
while (bol_fsearch ("#~ msgid"))
{
eol ();
o++;
}
pop_mark_1 ();
while (bol_fsearch ("#, fuzzy"))
{
() = down (2);
if (looking_at ("#~")) break;
if (looking_at ("msgid_plural"))
{
f++; f++;
}
else
f++;
}
pop_spot ();
t = t - f; % translated count includes fuzzies, so deduct them
set_blocal_var (t, "translated");
set_blocal_var (u, "untranslated");
set_blocal_var (f, "fuzzy");
set_blocal_var (o, "obsolete");
set_blocal_var (t + u + f, "total_entries");
set_po_status_line ();
}
%% Shows the counts in the message area, including total count and current
%% position
define show_po_statistics ()
{
ifnot ("PO" == get_mode_name ())
return;
variable t, u, f, o;
variable n = get_blocal_var ("total_entries");
variable cn = get_current_entry_number ();
t = count ("t"); u = count ("u"); f = count ("f"); o = count ("o");
po_statistics ();
flush ("Position: $cn/$n; $t translated, $u untranslated, $f fuzzy, $o obsolete"$);
}
%}}}
%{{{ auxillary functions
%% Catch output from a system cmd in a string.
private define syscmd_output_to_string ()
{
variable exit_status = 0, str = "", fp, cmd = "";
cmd = ();
fp = popen (cmd, "r");
if (fp == NULL)
throw RunTimeError, "could not open pipe to process";
str = strjoin (fgetslines (fp), "");
exit_status = pclose (fp);
str = strtrim (str);
return (str, exit_status);
}
% Envelop every line in a string in double quotes
private define surround_in_quotes (str)
{
str = strchop (str, '\n', 0);
str = array_map (String_Type, &strcat, "\"", str, "\"");
return strjoin (str, "\n");
}
%% Get the language/country code and charset from the enviroment,
define get_locale_info ()
{
variable locale_values = array_map (String_Type, &getenv, ["LC_MESAGES","LANG","LC_ALL"]);
variable i = where (strlen (locale_values));
try
{
variable n = is_substr (locale_values[i][0], ".");
variable lang = locale_values[i][0][[0:1]]; % e.g. "en"
variable country = locale_values[i][0][[3:4]]; % e.g. "en"
variable lang_COUNTRY = locale_values[i][0][[0:4]]; % e.g. "en_UK"
variable encoding = locale_values[i][0][[n:]]; % e.g. utf8
}
catch AnyError:
flush ("could not set aspell dictionary from environment!");
return lang_COUNTRY, lang, country, encoding;
}
% Replace a string with another using pcre regular expressions
private define pcre_replace (str, pat, rep)
{
variable enc = "", match, pos = 0;
(,,,enc) = get_locale_info ();
try
{
if (enc == "utf-8")
pat = pcre_compile (pat, 0x20000000|PCRE_UTF8);
else
pat = pcre_compile (pat);
}
catch ParseError:
return flush ("Invalid regular expression");
while (pcre_exec (pat, str, pos))
{
match = pcre_nth_match (pat, 0);
str = strcat (str[[0:match[0]-1]], rep, str[[match[1]:]]);
pos = match[0] + strlen (rep);
}
return str;
}
% match against a string using pcre regular expressions
private define pcre_string_match (str, pat)
{
pat = pcre_compile (pat);
return pcre_exec (pat, str);
}
%% determine the type of file, regular, directory, etc.
private define file_type (file, type)
{
variable st = stat_file (file);
if (st == NULL)
return 0;
return stat_is (type, st.st_mode);
}
%% Probe for directory of source archive
private define init_src_dir (msg)
{
variable cur_dir = "", srcdir = "", srcdir_name = "", srcdirs, k = 0;
variable other = "", dir = "";%, msg = "";
(,cur_dir,,) = getbuf_info (whatbuf ());
srcdir_name = extract_element (whatbuf (), 0, '.');
srcdirs = listdir (SrcBaseDir);
srcdirs = array_map (String_Type, &dircat, SrcBaseDir, srcdirs);
srcdirs = srcdirs[where (array_map (Int_Type, &file_type, srcdirs, "dir"))];
srcdirs = srcdirs[where (array_map (Int_Type,
&string_match, srcdirs, srcdir_name, 1))];
if (length (srcdirs))
{
msg = "Choose directory with $msg"$;
srcdir = srcdirs[0];
k = get_mini_response ("$msg: 1) $srcdir, 2) $cur_dir, 3) other"$);
}
else
k = get_mini_response ("$msg: 1) $cur_dir, 2) other"$);
switch (k)
{ case '1': dir = read_with_completion ("$msg:"$, "", srcdir, 'f'); }
{ case '2': dir = read_with_completion ("$msg:"$, "", cur_dir, 'f'); }
{ case '3': dir = read_with_completion ("$msg:"$, "", SrcBaseDir, 'f'); }
{ throw UsageError, "no source directory selected"; }
return dir;
}
%% Return a program in $PATH
private define check_for_prg (prg)
{
variable path = getenv ("PATH"), res = "";
res = search_path_for_file (path, prg);
if (res == NULL)
throw OpenError, "$prg is not installed"$;
return res;
}
%% Check for po files in a directory
private define pofiles_in_dir (dir)
{
variable files, exts, po_files, po_matches;
files = listdir (dir);
files = array_map (String_Type, &dircat, dir, files);
files = files[where (array_map (Int_Type, &file_type, files, "reg"))];
exts = array_map (String_Type, &path_extname, files);
po_matches = where (exts == ".po");
po_files = files[po_matches];
return po_files;
}
%% The two window layout for editing msgstrs and comments
private define setup_win_layout (view_buf, view_str, edit_buf, edit_str)
{
variable win_rows, nlines_msgid, msgid, delim, msgid_arr;
recenter (1);
Po_Buf = pop2buf_whatbuf (edit_buf);
pop2buf (view_buf);
insert (view_str);
set_buffer_modified_flag (0);
nlines_msgid = what_line ();
bob ();
win_rows = window_info ('r');
otherwindow ();
loop (win_rows - nlines_msgid-1)
enlargewin ();
insert (edit_str);
ifnot (Multi_Line)
{
set_mode (Edit_Mode, 1); % wrap_mode
WRAP=Wrap;
}
else
set_mode (Edit_Mode, 0);
bob ();
set_buffer_modified_flag (0);
}
% Write current buffer to a temporary file
private define write_buf_to_tmpfile ()
{
push_spot ();
mark_buffer ();
() = write_region_to_file (Po_Tmpfile);
pop_spot ();
}
%% In order to preserve undo information, which erase_buffer() does not.
private define po_erase_buffer ()
{
mark_buffer (); del_region ();
}
private define po_edit_wrap_hook ()
{
push_spot (); bol (); go_left (1); insert (" "); pop_spot ();
}
%% The library function from jed
private define search_path_for_file ()
{
variable path, f, delim = path_get_delimiter ();
if (_NARGS == 3)
delim = ();
(path, f) = ();
if (path == NULL)
return NULL;
foreach (strtok (path, char(delim)))
{
variable dir = ();
variable file = strcat (dir, "/", f);
if (file_type (file, "reg"))
return file;
}
return NULL;
}
%% Look for a program from an array of programs and return the first
%% one found
private define find_prgs_use_first (prgs)
{
variable prg, prgs_arr = strtok (prgs), path = getenv ("PATH");
prgs_arr = prgs_arr[wherenot (_isnull (array_map (String_Type, &search_path_for_file, path, prgs_arr)))];
ifnot (length (prgs_arr))
throw OpenError, "Error: You must install one of $prgs"$;
else
prg = prgs_arr[0];
return prg;
}
% Use a text browser to format html to text
private define html_to_txt (url)
{
variable cmd, browser, browser_basename, output, status, enc, pingurl;
variable ping_resp;
(,,, enc) = get_locale_info ();
browser = find_prgs_use_first ("lynx elinks w3m");
browser_basename = path_basename (browser);
switch (browser_basename)
{ case "lynx":
cmd = "/usr/bin/lynx -dump -nolist -nonumbers -width 500 -display_charset=$enc"$;
}
{ case "elinks":
cmd = "$browser -dump -dump-charset $enc -no-references -no-numbering"$;
}
{ case "w3m":
cmd = "$browser -dump -cols 500 -O $enc"$;
}
pingurl = strchop (url, '/', 0)[2];
(, ping_resp) =
syscmd_output_to_string ("ping -W2 -q -c1 $pingurl >/dev/null 2>&1"$);
ifnot (0 == ping_resp)
return "";
(output, status) = syscmd_output_to_string ("$cmd \"$url\" 2>/dev/null"$);
if (0 == status)
return output;
else
throw RunTimeError, "$cmd \"$url\" failed"$;
}
% How many lines is there in a string
private define lines_count (str)
{
return length (strchop (strtrim (str), '\n', 0));
}
private define str_has_end_newline (str)
{
return ((str[[-3:]] == "\\n\"") || (str == "msgid \"\""));
}
% If the first character in string is a newline
private define str_has_beg_newline (str)
{
return (str[0] == '\n');
}
% If the string is a gettext multiline string
private define str_is_multiline (str)
{
return ((is_substr (str, "\\n")) ||
(pcre_string_match (str, "^msgid \"\"$")));
}
% Delete html tags
private define del_html_tags (str)
{
return strtrim (str_uncomment_string (str, "<", ">"));
}
% Delete format specifiers
private define del_format_specifiers (str)
{
variable letter, letters = [['A':'Z'], ['a':'z']];
foreach (letters)
{
letter = char ();
str = strreplace (str, strcat ("%", letter), "");
}