summaryrefslogtreecommitdiff
path: root/support/jed/jlm/latex_external.sl
blob: 11307401e7b0bff00c615ef60555994576045bdd (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
% File: latex_external.sl                -*- mode: SLang; mode: fold -*-
%
% Copyright (c)
%       until 2003  Guido Gonzato <guido.gonzato@univr.it> (as Latex4Jed)
%       2003--2007  Jörg Sommer <joerg@alea.gnuu.de>
%       $Id: latex_external.sl 199 2007-08-23 23:28:52Z joerg $
%
%       -*- This file is part of Jörg's LaTeX Mode (JLM) -*-
%
% Description:   In this file are all functions for composing, viewing,
%                printing and any related problems. All what have to do
%                with an external program and a latex file.
%
% License: This program is free software; you can redistribute it and/or
%	   modify it under the terms of the GNU General Public License as
%	   published by the Free Software Foundation; either version 2 of
%	   the License, or (at your option) any later version.
%
%          This program is distributed in the hope that it will be
%	   useful, but WITHOUT ANY WARRANTY; without even the implied
%	   warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
%	   PURPOSE.  See the GNU General Public License for more details.

% Fixme ToDo:
%  + replace all error()s with throw

if (length(where("latex_external" == _get_namespaces())))
  use_namespace("latex_external");
else
  implements("latex_external");

#ifnexists profile_on
% profiling does not work with stack check enabled.
autoload("Global->enable_stack_check", "stkcheck");
try
{
    enable_stack_check();
}
catch OpenError: {} % stkcheck not found in the path
#endif

#ifnexists any
public define any(array)
{
    return wherefirst(array) != NULL;
}
#endif
#ifnexists all
public define all(array)
{
    return not any(not array);
}
#endif

%%%%%%%%%%
%
% Declarations needed somewhere before the definition
% 

static define view() { throw NotImplementedError; }

%%%%%%%%%%
%
% Tools
%

#if (_jed_version < 9919)
public define buffer_filename ()
{
   variable args = __pop_args (_NARGS);
   variable file, dir;
   (file, dir, , ) = getbuf_info(__push_args (args));
   !if (strlen (file)) dir = "";
   return dir + file;
}
#endif

private define path_concat(left, right)
{
    if (path_basename(left) == "")
      % left has a trailing dir separator
      left = path_dirname(left);

    forever
    {
#ifdef UNIX
        if (strncmp(right, "../", 3) == 0)
#else
        if (strncmp(right, "..\\", 3) == 0)
#endif
        {
            left = path_dirname(left);
            right = substr(right, 4, -1);
        }
#ifdef UNIX
        else if (strncmp(right, "./", 2) == 0)
#else
        else if (strncmp(right, ".\\", 2) == 0)
#endif
          right = substr(right, 3, -1);
        else
          break;
    }

    return Global->path_concat(left, right);
}

private define find_buf_of_file(fname)
{
    if (fname == NULL)
      return NULL;

    foreach ( __pop_args( buffer_list() ) )
    {
        variable bname = ().value;
        !if ( strcmp(buffer_filename(bname), fname) )
          return bname;
    }
    return NULL;
}

private variable MODE="LaTeX-External";

%%%%%%%%%%
%
% Customisation stuff
% 

%!%+
%\variable{String_Type LaTeX_Rerun}
%\synopsis{rerun composing}
%\description
% This variable defines the configuration of the commands used for
% creating and viewing the document and so on.
%
% Default is "latex".
%\seealso{cust_set()}
%!%-
custom_variable("LaTeX_Config", "latex");

% %m Masterfile
% %M Masterfile without extension
% \see{expand_and_run_cmd}

typedef struct {
    name, desc, bibtex, index, latex, post, clean, mrproper, print, view, rerun
} cust_type;
private variable cust = Assoc_Type[cust_type];

static define cust_set(name, desc, bibtex, index, latex, post, clean, mrproper,
                       print, view, rerun)
{
    variable accept_NULL = assoc_key_exists(cust, name);

    !if ( typeof(rerun) == Integer_Type or (accept_NULL and rerun == NULL) )
      throw InvalidParmError, "rerun must be an integer";

    variable types = [typeof(desc), typeof(bibtex), typeof(index),
                      typeof(latex), typeof(post), typeof(clean),
                      typeof(mrproper), typeof(print), typeof(view)];
    !if ( all( (types == String_Type) or
               (accept_NULL and (types == Null_Type)) ) )
      throw InvalidParmError, "At least one of the given arguments has a "+
                              "wrong data type";

    variable tmp;
    if (accept_NULL)
    {
        tmp = cust[name];
        if (desc != NULL)
          tmp.desc = desc;
        if (bibtex != NULL)
          tmp.bibtex = bibtex;
        if (index != NULL)
          tmp.index = index;
        if (latex != NULL)
          tmp.latex = latex;
        if (post != NULL)
          tmp.post = post;
        if (clean != NULL)
          tmp.clean = clean;
        if (mrproper != NULL)
          tmp.mrproper = mrproper;
        if (print != NULL)
          tmp.print = print;
        if (view != NULL)
          tmp.view = view;
        if (rerun != NULL)
          tmp.rerun = rerun;
    }
    else
    {
        tmp = @cust_type;
        cust[name] = tmp;

        tmp.name = name;
        tmp.desc = desc;
        tmp.bibtex = bibtex;
        tmp.index = index;
        tmp.latex = latex;
        tmp.post = post;
        tmp.clean = clean;
        tmp.mrproper = mrproper;
        tmp.print = print;
        tmp.view = view;
        tmp.rerun = rerun;
    }
}

cust_set("rubber_pdf", "Run rubber <http://beffara.org/stuff/rubber.html> to build a PDF",
         "", "",
         "rubber --inplace --pdf %m", "",
         "rubber --clean %m", "rubber --clean --pdf %m",
         "lp -t \"%M\" %M.pdf", "xpdf %M.pdf", 0);

cust_set("rubber_dvi", "Run rubber <http://beffara.org/stuff/rubber.html> to build a DVI",
         "", "",
         "rubber --inplace %m", "",
         "", "rubber --clean %m",
         "sh -c dvips% -o-% %M.dvi|lp% -t% \"%M\"", "xdvi %M.dvi", 0);

cust_set("rubber_ps", "Run rubber <http://beffara.org/stuff/rubber.html> to build a PS",
         "", "",
         "rubber --inplace --ps %m", "",
         "rubber --clean %m", "rubber --clean --ps %m",
         "lp -t %M %M.ps", "gv --watch %M.ps", 0);

cust_set("rubber_ps_pdf", "Run rubber <http://beffara.org/stuff/rubber.html> to build a PDF from PS",
         "", "",
         "rubber --inplace --ps --pdf %m", "",
         "rubber --clean --ps %m", "rubber --clean --ps --pdf %m",
         "lp -t %M %M.pdf", "xpdf %M.pdf", 0);

cust_set("latex", "Run LaTeX",
         "bibtex %m",
         "makeindex %M.idx",
         "latex -interaction=nonstopmode %m", "",
#ifdef MSWINDOWS
         "del %M.aux %M.idx %M.ind %M.log %M.out %M.toc",
         "del %M.aux %M.idx %M.ind %M.log %M.out %M.toc %M.dvi",
         "", "yap %M.dvi", 1);
#else
         "rm -f %M.aux %M.idx %M.ind %M.log %M.out %M.toc",
         "rm -f %M.aux %M.idx %M.ind %M.log %M.out %M.toc %M.dvi",
         "sh -c dvips% -o-% \"%M.dvi\"|lp% -t% \"%M\"",
         "xdvi %M.dvi", 1);
#endif

cust_set("pdflatex", "Run PDFLaTeX with Xindy <http://www.xindy.org/>",
         "bibtex %m",
         "texindy --quiet --language german-din %M.idx",
         "pdflatex -interaction=nonstopmode %m", "",
#ifdef MSWINDOWS
         "del %M.aux %M.idx %M.ind %M.log %M.out %M.toc",
         "del %M.aux %M.idx %M.ind %M.log %M.out %M.toc %M.pdf",
         "",
#else
         "rm -f %M.aux %M.idx %M.ind %M.log %M.out %M.toc",
         "rm -f %M.aux %M.idx %M.ind %M.log %M.out %M.toc %M.pdf",
         "lp -t %M %M.pdf",
#endif
         "xpdf %M.pdf", 1);

cust_set("latex_ps", "Run LaTeX and dvips",
         "bibtex %m",
         "makeindex %M.idx",
         "latex -interaction=nonstopmode %m", "dvips %M.dvi",
#ifdef MSWINDOWS
         "del %M.aux %M.idx %M.ind %M.log %M.out %M.toc %M.dvi",
         "del %M.aux %M.idx %M.ind %M.log %M.out %M.toc %M.dvi %M.ps",
         "",
#else
         "rm -f %M.aux %M.idx %M.ind %M.log %M.out %M.toc %M.dvi",
         "rm -f %M.aux %M.idx %M.ind %M.log %M.out %M.toc %M.dvi %M.ps",
         "lp -t %M %M.ps",
#endif
         "gv %M.ps", 1);

cust_set("latex_srcspecl",
         "LaTeX with source specials <http://xdvi.sourceforge.net/inverse-search.html>",
         "bibtex %m",
         "makeindex %M.idx",
#ifdef MSWINDOWS
         "latex -interaction=nonstopmode --src-specials %m", "",
         "del %M.aux %M.idx %M.ind %M.log %M.out %M.toc",
         "del %M.aux %M.idx %M.ind %M.log %M.out %M.toc %M.dvi",
         "", "yap --find-src-special %l%f %M.dvi", 1);
#else
         "latex -interaction=nonstopmode -src-specials %m", "",
         "rm -f %M.aux %M.idx %M.ind %M.log %M.out %M.toc",
         "rm -f %M.aux %M.idx %M.ind %M.log %M.out %M.toc %M.dvi",
         "", "xdvi -nofork -editor %e -sourceposition %l:%c%f %M.dvi", 1);
#endif

static define cust_jump_or_edit()
{
    variable mark = create_user_mark();
    bol();
    if ( eolp() or what_line < 3 )
      % empty line or a line before the TOC -- nothing should happen
      goto_user_mark(mark);
    else
      if ( looking_at_char('*') )
        % a line in the TOC -- jump to the section
      {
          () = right(2);
          push_mark();
          !if ( ffind(" -- ") )
            throw InternalError, "buf structure destroyed";
          bskip_white();

          !if ( re_fsearch( "^"+bufsubstr()+"$" ) )
            throw InternalError, "buf structure destroyed";
      }
    else
      if ( ffind_char(':') )
        % a config line -- edit it
      {
          () = right(1);
          push_spot();
          bol();
          push_mark();
          pop_spot();
          str_delete_chars(bufsubstr(), " ");
                              % prompt for read_mini() on stack
          "";                 % default for read_mini() on stack
          () = right(1);
          push_spot();
          push_mark();
          eol();
          bufsubstr();       % init for read_mini() on stack
          pop_spot();

          try
          {
              read_mini((), (), ());

              set_readonly(0);
              push_mark();
              eol();
              del_region();
              insert( () );  % from read_mini()

              set_readonly(1);
              set_buffer_modified_flag(0);

              % Fixme: fill in code
              throw NotImplementedError;
          }
          catch UserBreakError:
          {}
          bol();
      }
    else
      % this must be a line with the config name
      % Fixme: fill in code
      throw NotImplementedError;
}

static define cust_select()
{
    variable mark = create_user_mark(), name;
    bol();
    if ( eolp() or what_line < 3 )
    {
        % empty line or a line before the TOC -- nothing should happen
        goto_user_mark(mark);
        return;
    }
    else
      if ( looking_at_char('*') )
        % a line in the TOC
      {
          () = right(2);
          push_mark();
          !if ( ffind(" -- ") )
            throw InternalError, "buf structure destroyed";
          bskip_white();

          name = bufsubstr();
      }
    else
    {
        % search the last empty line backwards
        while( not (bol(), eolp()) )
          () = up(1);

        () = down(1);

        name = line_as_string();
    }

    if (name == "new_conf")
      throw UsageError,
          "This is a pseudo confic. Give it a name and you can load it.";
    else
      LaTeX_Config = name;

    delbuf( whatbuf() );
}

!if ( keymap_p(MODE + "-cust") )
{
    variable _mode = MODE + "-cust";
    make_keymap(_mode);

    definekey("latex_external->cust_jump_or_edit()", "\n", _mode);
    definekey("latex_external->cust_jump_or_edit()", "\r", _mode);
    definekey("latex_external->cust_select()", " ", _mode);

    definekey("delbuf(whatbuf());call(\"delete_window\")", "q", _mode);
    definekey("delbuf(whatbuf());call(\"delete_window\")", "c", _mode);
}

private define cust_view_insert(name, desc, bibtex, index, latex, post,
                                clean, mrproper, print, view, rerun)
{
    insert("* "+name);
    whitespace(15-strlen(name));
    % The three spaces after -- are a hack for DFA highlighting 
    insert(" --   "+desc+"\n");
    push_spot();

    % Fixme: show the keys for the command before the colon;
    %   use which_key to find the key
    eob();
    newline();
    insert(name); newline();
    insert("Description : "+desc);	newline();
    insert("Bibtex      : "+bibtex);	newline();
    insert("Index       : "+index);	newline();
    insert("LaTex       : "+latex);	newline();
    insert("Post proc.  : "+post);	newline();
    insert("Cleanup     : "+clean);	newline();
    insert("Mr. Proper  : "+mrproper);	newline();
    insert("Print       : "+print);	newline();
    insert("View        : "+view);	newline();
    insert("Rerun LaTeX : "+rerun);	newline();
    pop_spot();
}

#ifdef HAS_DFA_SYNTAX
private define setup_dfa_callback(name)
{
    % the header
    dfa_define_highlight_rule("^Possible configurations", "comment", name);
    dfa_define_highlight_rule("^-*$", "comment", name);
    dfa_define_highlight_rule("^Current configuration:.*", "comment", name);

    % in the toc
    dfa_define_highlight_rule("^\\* ", "preprocess", name);
    dfa_define_highlight_rule("-- ", "preprocess", name);
    % this matches the part between * and --
    dfa_define_highlight_rule("^[^\\*:]*$", "keyword", name);
    % this matches the part after --
    dfa_define_highlight_rule("  .*$", "string", name);

    % this matches the configuration name before the section
    dfa_define_highlight_rule("[a-zA-Z_ ]* ", "keyword", name);

    % this mathes the part before the colon in the section
    dfa_define_highlight_rule("^[a-zA-Z\\. ]*:", "number", name);
    % ... and this the part after the colon
    dfa_define_highlight_rule(" .*$", "normal", name);

    dfa_build_highlight_table(name);
}
create_syntax_table(MODE + "-cust");
dfa_set_init_callback(&setup_dfa_callback(), MODE + "-cust");
enable_dfa_syntax_for_mode(MODE + "-cust");
#endif

static define cust_view()
{
    sw2buf("LaTeX-cust");
    set_mode(MODE + "-cust", 0);
    use_keymap(MODE + "-cust");
#ifdef HAS_DFA_SYNTAX
    use_syntax_table(MODE + "-cust");
#endif
    set_status_line("Select LaTeX build tools -- Space to select and "+
                    "Enter to edit",
                    0);
    erase_buffer();

    insert("Possible configurations\n-----------------------\n");
    insert("Current configuration: $LaTeX_Config\n\n"$);

    variable tmp = assoc_get_keys(cust);
    foreach ( tmp[array_sort(tmp)] )
    {
        variable conf = ();
        conf = cust[conf];
        cust_view_insert(conf.name, conf.desc,
                         conf.bibtex, conf.index,
                         conf.latex, conf.post,
                         conf.clean, conf.mrproper,
                         conf.print, conf.view, string(conf.rerun));
    }

    cust_view_insert("new_conf", "setup a new configuration",
                     "command to run Bibtex", "command to build the index",
                     "command to generate the output file",
                     "command to run after a successful latex run",
                     "command to remove all temporary files",
                     "command to all files execpt the source file",
                     "command to print the output file",
                     "command to view the output file",
                     "rerun LaTeX?");

    set_readonly(1);
    set_buffer_modified_flag(0);
    bob();
}

%%%%%%%%%%
%
% All about the master file stuff
% 

%!%+
%\function{select_master_file}
%\synopsis{sets the usage of a master file for compilation}
%\usage{select_master_file()}
%\description
% Asks the user if the file in the current buffer should be used as master
% file for composing. This is helpful is you use \include in latex. If set
% everywhere you call compose() latex is called with the file set here.
%
%\seealso{mater_file, master_dir, get_compose_dirfile()}
%!%-
static define select_master_file()
{
    try
    {
#ifexists jcomplete_file
        variable file = jmini_prompt_with_completion("Select master file:", "",
                                                     "", &jcomplete_file);
#else
        variable file = read_with_completion("Select master file:", "", "", 'f');
#endif

        if (file == "")
          file = NULL;
        else
          !if ( path_is_absolute(file) )
            file = path_concat(getcwd(), file);

        !if ( blocal_var_exists("LaTeX_master_file") )
          create_blocal_var("LaTeX_master_file");

        set_blocal_var(file, "LaTeX_master_file");
    }
    catch UserBreakError;
}

%!%+
%\function{get_master_file}
%\synopsis{Get file name of the latex file}
%\usage{String_Type get_master_file()}
%\description
% This function returns the full path of the file, which is used by
% the latex command.
%
% This could be used to determine the name of the .dvi or .ps file.
%!%-
private define get_master_file()
{
    if ( blocal_var_exists("LaTeX_master_file") )
    {
        variable file = get_blocal_var("LaTeX_master_file");
        if ( file != "" and file != NULL )
        {
            if ( path_is_absolute(file) )
              return file;
            else
              return path_concat(path_dirname(buffer_filename()), file);
        }
    }

    return buffer_filename();
}

static define jump_to_master_buffer()
{
    !if ( blocal_var_exists("LaTeX_master_file") )
      return;

    variable master_buf = get_blocal_var("LaTeX_master_file");
    if (master_buf == NULL or master_buf == "")
      return;

    !if ( path_is_absolute(master_buf) )
      master_buf = path_concat(path_dirname(buffer_filename()), master_buf);

    variable buf = find_buf_of_file(master_buf);
    if (buf == NULL)
      throw InternalError, "Buffer with master file not found";

    setbuf(buf);
}

private define exists_master_var(name)
{
    variable oldbuf = whatbuf();
    jump_to_master_buffer();

    blocal_var_exists("LaTeX_"+name);

    setbuf(oldbuf);
    return ();
}

private define get_master_var() % optional 2nd arg: default
{
    variable dflt, name;
    if (_NARGS == 2)
      dflt = ();
    name = ();

    variable oldbuf = whatbuf();
    jump_to_master_buffer();

    try
    {
        if (_NARGS != 2 or blocal_var_exists("LaTeX_"+name))
          return get_blocal_var("LaTeX_"+name);

        return dflt;
    }
    finally
    {
        setbuf(oldbuf);
    }
}

private define set_master_var(val, name)
{
    variable oldbuf = whatbuf();
    jump_to_master_buffer();

    create_blocal_var("LaTeX_"+name);
    set_blocal_var(val, "LaTeX_"+name);

    setbuf(oldbuf);
}

%%%%%%%%%%
%
% external command stuff
% 

private variable DUMMY_OUTPUT_BUFFER = " latex-process-output";

private variable outp_buf="";

private define output_to_msg(pid, data)
{
    variable outp = strchop(outp_buf + data, '\n', 0);

    % preserve all after the last \n for the next message
    outp_buf = outp[-1];

    if (length(outp) > 1)
    {
        message("(latex) " + outp[-2]);
        update(0);
    }
}

%!%+
%\variable{User_Mark line_mark}
%\synopsis{holds the mark of the current line}
%\description
% we need this variable as buffer for the line mark.
% if the line mark isn't associated with a variable it isn't shown
%
%\seealso{update_log_hook()}
%!%-
private variable line_mark;

%!%+
%\function{update_log_hook}
%\synopsis{Marks the current line}
%\usage{update_log_hook()}
%\description
% This function marks the current line for highlighting.
%
% It is used in the buffers with the output of latex and other programms
% to show better the line the cursor is in.
%!%-
private define update_log_hook()
{
  line_mark = create_line_mark(color_number("region"));
}

private define expand_and_run_cmd(cmd, buf)
{
    variable len = strlen(cmd);
    if (cmd == NULL or len == 0)
      return;

    variable master_file = get_master_file();
    variable rel_master_file = path_basename(master_file);

    variable cmd_list = list_new(), next_arg = "", last_was_percent = 0;
    foreach (cmd)
    {
        variable ch = ();
        if (last_was_percent)
        {
            switch (ch)
            { case '%': next_arg += "%"; }
            { case 'c': next_arg += string(what_column()); }
            { case 'f':
                if (buffer_filename() == "")
                  throw InternalError, "Buffer without filename: " + whatbuf();
                % Fixme: this path should be relativ
                next_arg += buffer_filename();
            }
            { case 'l': next_arg += string(what_line()); }
            { case 'm': next_arg += rel_master_file; }
            { case 'M': next_arg += path_sans_extname(rel_master_file); }
            { case ' ': next_arg += " "; }
            { throw InvalidParmError, "Invalid escape sequence in cmd: %"+ch; }
            last_was_percent = 0;
        }
        else
        {
            switch (ch)
            { case ' ':
                list_append(cmd_list, next_arg, -1);
                next_arg = "";
            }
            { case '%': last_was_percent = 1; }
            { next_arg += char(ch); }
        }
    }
    if (last_was_percent)
      next_arg += "%";
    if (strlen(next_arg) != 0)
      list_append(cmd_list, next_arg, -1);

    if ( chdir( path_dirname(master_file) ) )
      throw IOError, "Could not change directory";

    variable oldbuf = whatbuf();

    if (typeof(buf) == String_Type)
    {
        variable buffer_exists = bufferp(buf);
        setbuf(buf);

        % set it in all cases, maybe we don't compose the same file
        create_blocal_var("LaTeX_master_file");
        set_blocal_var(master_file, "LaTeX_master_file");

        if (buffer_exists) {
            % buffer already exists
            set_readonly(0);
            erase_buffer();
        } else {
            % buffer is new
            set_buffer_hook("update_hook", &update_log_hook());
        }
    }

    if ( cmd[0] == '@' )
    {
        try
          eval(cmd[[1:]]);
        finally
          setbuf(oldbuf);
    }
    else
    {
        variable pid;
        try
        {
            foreach (cmd_list) ;          % push elements on stack
            pid = open_process_pipe( length(cmd_list)-1 );
        }
        finally
          setbuf(oldbuf);

        process_query_at_exit(pid, 1);

        if (typeof(buf) == Ref_Type)
          set_process(pid, "output", buf);

        return pid;
    }
}

%%%%%%%%%%
%
% General log files (bibtex, makeindex, tex)
%
private variable SIMPLE_KEYMAP = MODE + "-simple";

!if ( keymap_p(SIMPLE_KEYMAP) ) {
    make_keymap(SIMPLE_KEYMAP);

    definekey("delbuf(whatbuf());call(\"delete_window\")", "q", SIMPLE_KEYMAP);
    definekey("delete_window", "c", SIMPLE_KEYMAP);
}

private define show_log_file(log_file)
{
    if (stat_file(log_file) == NULL)
      throw ApplicationError, "Log file does not exist";

    % moves the current line in the upper half of the window, else the new
    % window is opened on the upper border
    if (window_line() >= window_info('r')/2) {
        recenter( window_info('r')/2-3 );
        update_sans_update_hook(1);
    }

    variable buf = find_buf_of_file(log_file);
    variable open_new_buffer = 1;
    if (buf != NULL)
    {
        setbuf(buf);
        if ( _test_buffer_flag(0x004) )
          delbuf(whatbuf());
        else
          open_new_buffer = 0;
    }

    if (open_new_buffer)
    {
        if (get_master_var("compose_pid", -1) != -1)
          throw UsageError, "A compose process is running. I cannot open the log file";

        () = read_file(log_file);

        set_readonly(1);
        set_buffer_modified_flag(0);

        use_keymap(SIMPLE_KEYMAP);
        set_status_line(" " + path_basename(log_file) + " ", 0);
    }
    pop2buf( whatbuf() );

    window_info('r')-6;             % leaves its return code on stack
    otherwindow();
    loop( () ) enlargewin();   % shrinks the other window to a size of 6 rows
    otherwindow();

    return open_new_buffer;
}

%%%%%%%%%%
%
% All about parsing the TeX log file
%

%!%+
%\function{find_next_error}
%\synopsis{Moves the cursor to the line with the next error}
%\usage{find_next_error}
%\description
% This function moves the editing point to the next line, taht contains
% an error or a warning message of latex.
%
% It stops on font warning, undefined references, over and underfull boxes,
% and all error messages.
%!%-
% dir == 1 -> forward, dir != 0 -> backward
static define find_next(what, dir)
{
    variable move;
    if (dir) move = &down();
    else move = &up();

    variable found = ' ';
    variable old_mark = create_user_mark(), old_err_text;
    if ( looking_at("! ") )
    {
        push_mark();
        () = bol_fsearch("l.");
        () = down(1);
        old_err_text = bufsubstr();
    }
    do {
        while (@move(1)) {
            bol();
            switch ( what_char() )
            { case 'L' or case 'P':
                if ( orelse {looking_at("LaTeX")} {looking_at("Package")} )
                {
                    if ( ffind("Warning:") )
                    {
                        found = 'w';
                        bol();
                        break;
                    }
                    % else
                    % {
                    %     if ( ffind("Info:") )
                    %       found = 'i';
                    %     else
                    %       continue;
                    % }
                }
            }
            { case 'O': if (looking_at("Overfull")) { found = 'b'; break; } }
            { case 'U': if (looking_at("Underfull")) { found = 'b'; break; } }
            { case '!':
                if ( andelse {looking_at("! ")}
                     {not ( andelse {__is_initialized(&old_err_text)}
                            {looking_at(old_err_text)} )
                     } )
                {
                    found = 'e';
                    break;
                }
            }
            { case 'R':
                if (looking_at("Runaway argument?"))
                {
                    found = 'e';
                    break;
                }
            }
        }
    } while (andelse {what != '*'} {what != found} {not bobp()} {eol(), not eobp()});

    if (bobp() or eobp())
    {
        goto_user_mark(old_mark);
        switch (what)
        { case 'e': throw UsageError, "No error message left"; }
        { case 'w': throw UsageError, "No warning message left"; }
        { case 'b': throw UsageError, "No box message left"; }
        { case '*': throw UsageError, "No message left"; }
        { throw InvalidParmError, "unknown search target: "+char(what); }
    }
}

!if ( keymap_p(MODE) ) {
    copy_keymap(MODE, SIMPLE_KEYMAP);

    variable key;
    foreach key (['e', 'w', 'b'])
    {
        definekey("latex_external->find_next('"+char(key)+"', 0)",
                  char(key-0x20), MODE);
        definekey("latex_external->find_next('"+char(key)+"', 1)",
                  char(key), MODE);
    }
    definekey("latex_external->find_next('*', 0)", "N", MODE);
    definekey("latex_external->find_next('*', 1)", "n", MODE);

    definekey("latex_external->goto_error_line()", "g", MODE);
    definekey("latex_external->goto_error_line()", "\n", MODE);
    definekey("latex_external->goto_error_line()", "\r", MODE);
}

%!%+
%\function{pop2compose_buf}
%\synopsis{pops up a new window with the buffer for compiler output}
%\usage{pop2compose_buf([Integer_Type goto_first_error])}
%\description
% If a buffer COMPILE_BUF_NAME exists, a new window is opened with the
% buffer and this window is shrinked to a size of 6 rows. Therefor the
% current line of the current window is moved in the upper half, that the
% new window raises at the bottom.
%
% If the optional argument \var{goto_first_error} is given and != 0, the
% cursor is moved to the first error.
%
% After all a short usage message is shown.
%
% The buffer is created by compose().
%\seealso{COMPILE_BUF_NAME, find_next_error(), compose()}
%!%-
static define pop_log_file()
{
    variable log_file = path_sans_extname( get_master_file() ) + ".log";
    !if ( show_log_file(log_file) )
      return;

    use_keymap(MODE);
    set_buffer_hook("update_hook", &update_log_hook());

    variable box=0, err=0, warn=0;
    bob();
    while ( down(1) ) {
        switch ( what_char() )
        { case 'L' or case 'P':
            if ( andelse {orelse {looking_at("LaTeX")}
                {looking_at("Package")} } {ffind("Warning:")})
            {
                bol();
                ++warn;
            }
        }
        { case 'O': if (looking_at("Overfull")) ++box; }
        { case 'U': if (looking_at("Underfull")) ++box; }
        { case '!': if (looking_at("! ")) ++err; }
        { case 'R': if (looking_at("Runaway argument?")) ++err; }
    }

    variable str = "";
    if (err) str += string(err)+" errors, ";
    if (warn) str += string(warn)+" warnings, ";
    if (box) str += string(box)+" under-/overfull boxes, ";

    eob();
    if ( andelse {bol_bsearch("Output written on")}
         {ffind_char('(')} {right(1)} )
    {
        push_mark();
        skip_chars("0-9");
        str += bufsubstr() + " pages written";
    }
    bob();

    message(str);

    set_status_line(" "+path_basename(log_file) +
       " -- e: error; w: warning; b: box; g,Enter: go to error line; q: quit",
                    0);
}

private define next_log_line()
{
    % TeX breakes lines at 80 characters. A line longer than this is more
    % than one real lines
    do
      eol();
    while ( andelse {what_column() == 80} {down(1)} );

    return down(1);
}

%!%+
%\function{goto_error_line}
%\synopsis{jumps to the error line in the latex code}
%\usage{goto_error_line}
%\description
% If the current editing point is in a line with an error or warning message
% of latex, this function switches to the buffer of the file wherein the error
% occured and jumps to the line, named in the output.
%!%-
static define goto_error_line()
{
    variable start_mark = create_user_mark(), file_stack = {};
    variable last_msg_type = '\0', last_msg_mark = start_mark;

    bob();
    while (andelse {next_log_line()} {create_user_mark() <= start_mark} )
    {
        switch ( what_char() )
        { case 'L' or case 'P':
            push_mark();
            () = ffind_char(':');
            variable token = strtok(bufsubstr(), " ");
            if ( andelse {length(token) >= 2} {length(token) <= 3}
                   {token[0] == "LaTeX" or token[0] == "Package"}
                   {token[-1] == "Warning" or token[-1] == "Info"} )
            {
                if (length(token) == 3)
                {
                    variable pkg = "(" + token[1] + ")";
                    variable last_line_mark = create_user_mark();

                    while ( andelse {next_log_line()} {looking_at(pkg)} )
                      last_line_mark = create_user_mark();

                    goto_user_mark(last_line_mark);
                }

                last_msg_type = 'W';
                last_msg_mark = create_user_mark();
                continue;
            }
        }
        { case 'O' or case 'U':
            if (orelse {looking_at("Overfull ")} {looking_at("Underfull ")})
            {
                () = ffind_char(' ');
                if ( looking_at(" \\hbox") )
                {
                    last_msg_type = 'B';
                    last_msg_mark = create_user_mark();

                    () = bol_fsearch(" []\n");
                }
                continue;
            }
        }
        { case '!':
            if ( andelse {looking_at("! ")} {bol_fsearch("l.")} )
            {
                last_msg_type = 'E';
                last_msg_mark = create_user_mark();

                () = bol_fsearch_char('\n');
                continue;
            }
        }
        { case 'R':
            if ( looking_at("Runaway argument?") )
            {
                last_msg_type = 'R';
                last_msg_mark = create_user_mark();

                () = bol_fsearch_char('\n');
                continue;
            }
        }

        forever
        {
            skip_chars("^()\n");
            switch ( what_char() )
            { case '\n':
                if (what_column() != 80)
                  break;
                % this is a wrapped line that is continued after the \n
                () = down(1);
            }
            { case ')': list_delete(file_stack, 0); () = right(1); }
            { case '(':
                () = right(1);
                push_mark();
                skip_chars("^ \n\t(){");
                while (what_column() == 80 and what_char() == '\n')
                {
                    () = right(1);
                    skip_chars("^ \n\t(){");
                }
                list_insert(file_stack, str_delete_chars(bufsubstr(), "\n"),
                            0);
            }
            { case '\0': break; } % end of file
        }
    }
    goto_user_mark(last_msg_mark);

    variable line = 0, text, detail_text;
    switch (last_msg_type)
    { case 'W' or case 'B':
        variable log_msg = line_as_string();
        while ( andelse {what_column() == 80} {down(1)} )
          log_msg += line_as_string();

        variable pos = is_substr(log_msg, "line"), dummy;
        if (pos != 0)
          () = sscanf(substr(log_msg, pos, strlen(log_msg)), "%s %d",
                      &dummy, &line);
    }
    { case 'E':
        () = right(2);
        push_mark();
        skip_chars("0-9");
        line = integer( bufsubstr() );

        () = right(1);
        push_mark();
        eol();
        text = bufsubstr();

        bol();
        if ( bol_bsearch("! ") )
        {
            () = down(1);
            if ( looking_at("<argument>") )
            {
                % For somewhat reason LaTeX prints out the text with
                % additional spaces. This might be the parsed form
                % (the tokens) of the text.
                eol();
                bskip_chars(" ");
                push_mark();
                bskip_chars("^ ");
                detail_text = bufsubstr();
            }
        }
    }
    { case 'R':
        () = down(1);
        push_mark();
        skip_chars("^\\");
        text = bufsubstr();

        forever
        {
            !if ( re_fsearch("^[<l][*.]") )             % fixme PCRE
              throw NotImplementedError, "Unexpected message style";

            latex->debug_pos();
            if ( looking_at("<*> ") )
            {
                (text,) = strreplace(text, " ", "[ \n\t]*", strlen(text));
                if ( ffind("\\input{") )
                  () = right(7);
                else
                  () = right(4);
                push_mark();
                skip_chars("^ \n\t(){}");
                while (what_column() == 80 and what_char() == '\n')
                {
                    () = right(1);
                    skip_chars("^ \n\t(){}");
                }
                list_insert(file_stack, bufsubstr(), 0);
                () = bol_fsearch("\n");
                break;
            }
            else if ( looking_at("l.") )
            {
                () = right(2);
                push_mark();
                skip_chars("0-9");
                line = integer( bufsubstr() );
                detail_text = text;
                __uninitialize(&text);
                () = bol_fsearch("\n");
                break;
            }
        }
    }
    goto_user_mark(start_mark);

    if (length(file_stack) == 0)
      throw UsageError, "You are not inside the scope of a LaTeX message";

    variable file_name;
    if ( path_is_absolute(file_stack[0]) )
      file_name = file_stack[0];
    else
      file_name = path_concat(path_dirname(get_master_file()), file_stack[0]);

    call("delete_window");
    if ( find_file(file_name) )
    {
        if (line > 0)
          goto_line(line);
        if ( __is_initialized(&text) )
        {
            if (line == 0)
              % This is a "Runaway argument" message
            {
                goto_line(1);
                () = re_fsearch(text);
            }
            else if ( strncmp(text, "...", 3) )
            {
                variable col = strlen(text)+1;
                eol();
                if ( col <= what_column() )
                  goto_column(col);
                else
                {
                    % this happens when the file was edited
                    bol();
                    __uninitialize(&detail_text);
                }
            }
            else
              () = right( ffind(text[[3:]]) );
        }
        if ( __is_initialized(&detail_text) )
          () = right( bsearch(detail_text) );
    }
}

%%%%%%%%%%
%
% Composing
% 

private define compose_sig_handl(pid, flags, status);
private define compose_sig_handl(pid, flags, status)
{
    if ( pid != get_master_var("compose_pid", -1) )
      error("Notification for wrong pid got: ("+ string(pid) +","+
            string(flags) +","+ string(status) + ")");

    switch (flags)
    { case 1: return; }
    { case 4 or case 8:
        set_master_var(-1, "compose_pid");

        if ( get_master_var("is_post_process", 0) )
        {
            set_master_var(0, "is_post_process");

            if (status != 0)
              throw ApplicationError, "(post process) finished with error";

            message("(post process) finished");
            return;
        }
        else
        {
            if (status != 0)
              throw ApplicationError, "(compose) finished with error";
            flush("(compose) finished");
        }

        variable master_file = get_master_file(), do_rerun = 0;
        if (cust[LaTeX_Config].rerun)
        {
            variable logfile = fopen(path_sans_extname(master_file)+".log", "r");
            variable lines = fgetslines(logfile);
            () = fclose(logfile);

            % if one of the lines contain this string we must rerun latex
            do_rerun = any( array_map(Integer_Type, &is_substr(), lines,
                                      "Rerun to get cross-references right."));
            % Fixme: "Marginpars may have changed."
            %   "(mparhack) Rerun to get them right."
        }

        !if ( do_rerun or strlen(cust[LaTeX_Config].post) )
          return;

        if (whatbuf() == " latex-compose")
          setbuf( find_buf_of_file(master_file) );
        else
        {
            setbuf(" latex-compose");
            create_blocal_var("LaTeX_master_file");
            set_blocal_var(master_file, "LaTeX_master_file");
        }

        variable cmd;
        if (do_rerun)
        {
            message("(compose) Rerun to get cross-references right...");
            cmd = cust[LaTeX_Config].latex;
        }
        else
        {
            set_master_var(1, "is_post_process");
            message("(compose) postprocessing output file...");
            cmd = cust[LaTeX_Config].post;
        }

        pid = expand_and_run_cmd(cmd, DUMMY_OUTPUT_BUFFER);
        set_master_var(pid, "compose_pid");
        set_process(pid, "signal", &compose_sig_handl());
     }
     {
	error("Compose process exited abnormal: flag="+ string(flags) +
	      ", status="+string(status));
     }
}

static define compose()
{
    if (get_master_var("compose_pid", -1) != -1)
      throw UsageError, "A compose process is already running";

    call("save_some_buffers");

    variable outp;
    if ( is_substr(cust[LaTeX_Config].latex, "rubber") )
      outp = &output_to_msg();
    else
      outp = DUMMY_OUTPUT_BUFFER;

    variable pid = expand_and_run_cmd(cust[LaTeX_Config].latex, outp);
    set_master_var(pid, "compose_pid");
    set_process(pid, "signal", &compose_sig_handl());

    message("Composing...");
}

%!%+
%\function{cleanup}
%\synopsis{removes all temporally files of latex}
%\usage{cleanup()}
%\description
% Removes all temporally files, created by latex while composing. This files
% are those which have the name of the compose file with a suffix defined
% in the array Cleanup_extensions.
%
%\seealso{Cleanup_extensions}
%!%-
static define clearup()
{
    message("Deleting temporary files...");
    () = expand_and_run_cmd(cust[LaTeX_Config].clean, DUMMY_OUTPUT_BUFFER);
}

%!%+
%\function{mrproper}
%\synopsis{removes all temporally files of latex and the output file}
%\usage{mrproper()}
%\description
% Removes all temporally files, created by latex while composing. This files
% are those which have the name of the compose file with a suffix defined
% in the array Cleanup_extensions.
%
%\seealso{Cleanup_extensions}
%!%-
static define mrproper()
{
    () = expand_and_run_cmd(cust[LaTeX_Config].mrproper, DUMMY_OUTPUT_BUFFER);
    message("Deleting temporary files and output file...");
}

private variable server_comm = NULL, last_buf;

private define view_comm_outp(pid, data)
{
    if (strncmp("communicator created: ", data, 22) == 0)
    {
        server_comm = substr(data, 23, strlen(data)-23);
        sw2buf(last_buf);
        view();
        return;
    }
    if (data == "quit\n")
      return;

    variable line, col, file;
    if (sscanf(data, "%d:%d%s", &line, &col, &file) != 3)
      throw ApplicationError, "View server send unknown string", data;

    () = find_file(file);
    goto_line(line);
    goto_column(col);
#ifexists x_toggle_visibility
    x_toggle_visibility(0);
#else
    tt_send("\e[5t");          % this is the control sequence to raise the
                               % xterm to the front
#endif
}

private define view_comm_sig(pid, flags, status)
{
    switch (flags)
    { case 1: return; }
    { case 4 or case 8: server_comm = NULL; }
    {
	throw ApplicationError, "View server exited abnormal";
    }
}

private define view_comm_quit()
{
    if (server_comm == NULL)
      return;

    variable fifo = fopen(server_comm, "w");
    try
    {
        () = fputs("quit\n", fifo);
    }
    finally
      () = fclose(fifo);

    % There's bug in SLang before version 2.0.7.91 that discards a system
    % call like fopen() if a signal arrives at the same time. Because we
    % use SLang for latex_comm.sl and it gets a SIGHUP when this process
    % finishes it never gets the quit. Due to this wait a moment and the
    % finish. 
    sleep(.1);
}
add_to_hook("_jed_quit_hooks", &view_comm_quit());

static define view()
{
    if (get_master_var("compose_pid", -1) != -1)
      throw UsageError, "A compose process is running. I cannot open viewer.";

    variable cmd = cust[LaTeX_Config].view;
    if ( is_substr(cmd, "%e") )
    {
        variable latex_comm = expand_jedlib_file("latex_comm.sl");
        if (server_comm == NULL)
        {
            last_buf = whatbuf();
            variable pid = expand_and_run_cmd("jed-script $latex_comm watch"$,
                                              " latex-comm");

            set_process(pid, "output", &view_comm_outp());
            set_process(pid, "signal", &view_comm_sig());
            process_query_at_exit(pid, 0);

            return; % the real viewer startup is triggered by view_comm_outp
        }
        (cmd,) = strreplace(cmd, "%e", "jed-script% "+ latex_comm
                        +"% "+ server_comm + "% %%l:0%%c%%f", 1);
    }

    % Fixme: have a better idea than try bad(); catch: do_somthing_others();
    %  How to query is a process is assigned to the buffer?
    try
    {
        () = expand_and_run_cmd(cmd, " latex-view");
    }
    catch RunTimeError:
      () = expand_and_run_cmd(cmd, " latex-view-2");

    % xpdf -q -remote jlm-$PID -raise %M.pdf %p
    % xpdf -q -remote jlm-$PID -raise -reload
    % xpdf -q -remote jlm-$PID -quit
}
static define print()
{
    if (get_master_var("compose_pid", -1) != -1)
      throw UsageError, "A compose process is running. I cannot start printer.";

    () = expand_and_run_cmd(cust[LaTeX_Config].print, &output_to_msg());
}

%!%+
%\function{makeindex}
%\synopsis{}
%\usage{makeindex()}
%\description
% Don't know what it does.
%!%-
static define makeindex()
{
    () = expand_and_run_cmd(cust[LaTeX_Config].index, DUMMY_OUTPUT_BUFFER);
}

static define show_mkidx_log()
{
    () = show_log_file(path_sans_extname( get_master_file() ) + ".ilg");
}

%!%+
%\function{bibtex}
%\synopsis{}
%\usage{bibtex()}
%\description
% Don't know what it does.
%!%-
static define bibtex()
{
    () = expand_and_run_cmd(cust[LaTeX_Config].bibtex, DUMMY_OUTPUT_BUFFER);
}

static define show_bibtex_log()
{
    () = show_log_file(path_sans_extname( get_master_file() ) + ".blg");
}

provide("latex_external");
runhooks("after_latex_external_load_hook");