summaryrefslogtreecommitdiff
path: root/Master/tlpkg/tlpostcode/ptex2pdf-tlpost.pl
blob: aa2a330bf69fe20a9300ad7a3e508579ecff6ac2 (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
# ptex2pdf-tlpost.pl
#
# post action for ptex2pdf in TeX Live
# action carried out:
# - if the environment is Japanese or the installer is running in Japanese, then...
#   - check for TeXworks tools.ini in
#     TW_INIPATH/configuration/tools.ini
#     - if *not* available, initialize from the default plus Japanese stuff
#     - if       available, read and try to add Japanese entries if not already there
#   - check for TeXworks.ini in
#     TW_INIPATH/TUG/TeXworks.ini
#     - if *not* available, initialize the default one with "pLaTeX (ptex2pdf)"
#     - if       available, read and try to set the default if not already there
#
# Copyright 2016-2021 Norbert Preining
# Copyright 2017 Japanese TeX Developer Community
# This file is licensed under the GNU General Public License version 2
# or any later version.
#
# TODO
# - what to do on remove?
#

my $texdir;
my $mode;

BEGIN {
  $^W = 1;
  $mode = lc($ARGV[0]);
  $texdir = $ARGV[1];
  # make Perl find our packages first:
  unshift (@INC, "$texdir/tlpkg");
}

use TeXLive::TLUtils qw(win32 mkdirhier conv_to_w32_path
                        debug ddebug log info tlwarn);


my %ptex2pdf = (
  '1' => {
    'name' => 'pTeX (ptex2pdf)',
    'program' => 'ptex2pdf',
    'arguments' => '-ot, -kanji=utf8 $synctexoption, $fullname',
    'showPdf' => 'true'
  },
  '2' => {
    'name' => 'pLaTeX (ptex2pdf)',
    'program' => 'ptex2pdf',
    'arguments' => '-l, -ot, -kanji=utf8 $synctexoption, $fullname',
    'showPdf' => 'true'
  },
  '3' => {
    'name' => 'pBibTeX',
    'program' => 'pbibtex',
    'arguments' => '-kanji=utf8, $basename',
    'showPdf' => 'false'
  },
  '4' => {
    'name' => 'mendex',
    'program' => 'mendex',
    'arguments' => '-U, $basename',
    'showPdf' => 'false'
  },
  '5' => {
    'name' => 'upTeX (ptex2pdf)',
    'program' => 'ptex2pdf',
    'arguments' => '-u, -ot, $synctexoption, $fullname',
    'showPdf' => 'true'
  },
  '6' => {
    'name' => 'upLaTeX (ptex2pdf)',
    'program' => 'ptex2pdf',
    'arguments' => '-u, -l, -ot, $synctexoption, $fullname',
    'showPdf' => 'true'
  },
  '7' => {
    'name' => 'upBibTeX',
    'program' => 'upbibtex',
    'arguments' => '$basename',
    'showPdf' => 'false'
  },
  '8' => {
    'name' => 'upmendex',
    'program' => 'upmendex',
    'arguments' => '$basename',
    'showPdf' => 'false'
  },
);

#
# NEEDS TO BE KEPT IN SYNC WITH TEX WORKS compiled in default!
my %original = (
  '001' => {
    'name' => 'pdfTeX',
    'showPdf' => 'true',
    'program' => 'pdftex',
    'arguments' => '$synctexoption, $fullname'
  },
  '002' => {
    'name' => 'pdfLaTeX',
    'showPdf' => 'true',
    'program' => 'pdflatex',
    'arguments' => '$synctexoption, $fullname'
  },
  '003' => {
    'name' => 'LuaTeX',
    'showPdf' => 'true',
    'arguments' => '$synctexoption, $fullname',
    'program' => 'luatex'
  },
  '004' => {
    'showPdf' => 'true',
    'name' => 'LuaLaTeX',
    'program' => 'lualatex',
    'arguments' => '$synctexoption, $fullname'
  },
  '005' => {
    'program' => 'xetex',
    'arguments' => '$synctexoption, $fullname',
    'name' => 'XeTeX',
    'showPdf' => 'true'
  },
  '006' => {
    'arguments' => '$synctexoption, $fullname',
    'program' => 'xelatex',
    'name' => 'XeLaTeX',
    'showPdf' => 'true'
  },
  '007' => {
    'program' => 'context',
    'arguments' => '--synctex, $fullname',
    'name' => 'ConTeXt (LuaTeX)',
    'showPdf' => 'true'
  },
  '008' => {
    'showPdf' => 'true',
    'name' => 'ConTeXt (pdfTeX)',
    'arguments' => '--synctex, $fullname',
    'program' => 'texexec'
  },
  '009' => {
    'program' => 'texexec',
    'arguments' => '--synctex, --xtx, $fullname',
    'showPdf' => 'true',
    'name' => 'ConTeXt (XeTeX)'
  },
  '010' => {
    'arguments' => '$basename',
    'program' => 'bibtex',
    'name' => 'BibTeX',
    'showPdf' => 'false'
  },
  '011' => {
    'name' => 'MakeIndex',
    'showPdf' => 'false',
    'arguments' => '$basename',
    'program' => 'makeindex'
  },
);

$::lang = "C";
if (TeXLive::TLUtils::win32()) {
  require TeXLive::TLWinGoo;
}
require("TeXLive/trans.pl");
#use Data::Dumper;

if ($::lang ne 'ja') {
  # not adjusting TeXworks for ptex2pdf
  exit(0);
}

if ($mode eq 'install') {
  do_install();
} elsif ($mode eq 'remove') {
  do_remove();
} else {
  die("unknown mode: $mode\n");
}

sub do_remove {
  # TODO - what should we do here???
}

sub do_install {
  # how to find TeX Works
  # see http://doc.qt.io/qt-5/qsettings.html
  #
  # NativeFormat
  # ------------
  # Unix:
  #  $HOME/.config/TUG/TeXworks.conf
  #  $HOME/.config/TeXworks.conf
  #  /etc/xdg/TUG/TeXworks.conf
  #  /etc/xdg/TeXworks.conf
  # Mac:
  #  $HOME/Library/Preferences/org.tug.TeXworks.plist
  #  $HOME/Library/Preferences/com.TeXworks.plist
  #  /Library/Preferences/com.tug.TeXworks.plist
  #  /Library/Preferences/com.TeXworks.plist
  # Win:
  #  HKEY_CURRENT_USER\Software\TUG\TeXworks
  #  HKEY_CURRENT_USER\Software\TUG\OrganizationDefaults
  #  HKEY_LOCAL_MACHINE\Software\TUG\TeXworks
  #  HKEY_LOCAL_MACHINE\Software\TUG\OrganizationDefaults
  #
  # IniFormat
  # ---------
  # Unix/Mac/iOS:
  #  $HOME/.config/TUG/TeXworks.ini
  #  $HOME/.config/TeXworks.ini
  #  /etc/xdg/TUG/TeXworks.ini
  #  /etc/xdg/TeXworks.ini
  # Windows
  #  FOLDERID_RoamingAppData\TUG\TeXworks.ini
  #  FOLDERID_RoamingAppData\TUG.ini
  #  FOLDERID_ProgramData\TUG\TeXworks.ini
  #  FOLDERID_ProgramData\TUG.ini
  # where
  #  FOLDERID_RoamingAppData usually points to C:\Users\User Name\AppData\Roaming
  #  FOLDERID_ProgramData usually points to C:\ProgramData
  #
  # TeXworks uses a mixture of NativeFormat and IniFormat
  # ConfigurableApp::ConfigurableApp set NativeFormat
  # TWApp checking for portable mode uses IniFormat
  #
  # So my guess is:
  # - TeXworks as shipped in TL uses the portable app setup, thus IniFormat
  # - TeXworks on Mac/Unix (not distributed with TL) uses NativeFormat
  #
  my $toolsdir;
  my $tugdir;
  my $inifile;
  if (win32()) {
    # we assume TeXworks from TL, thus IniFormat
    chomp( my $twini = `kpsewhich -var-value=TW_INIPATH` ) ;
    $toolsdir = "$twini/configuration";
    $tugdir = "$twini/TUG";
    $inifile = "TeXworks.ini";
  } elsif ($^O eq "darwin") {
    $toolsdir = $ENV{'HOME'} . "/Library/TeXworks/configuration";
    $tugdir = $ENV{'HOME'} . "/Library/Preferences";
    $inifile = "org.tug.TeXworks.plist";
    # Just for completeness, as we are not doing adjustments to
    # default engines on Mac. For this plist access would ne necessary.
  } else {
    $toolsdir = $ENV{'HOME'} . "/.TeXworks/configuration";
    my $xdgconfhome = (defined($ENV{'XDG_CONFIG_HOME'}) ? $ENV{'XDG_CONFIG_HOME'} : $ENV{'HOME'} . "/.config");
    $tugdir = $xdgconfhome . "/TUG";
    $inifile = "TeXworks.conf";
  }
  # print "toolsdir = $toolsdir\n";
  # print "tugdir = $tugdir\n";
  my $tools = "$toolsdir/tools.ini";
  my $tug = "$tugdir/$inifile";
  my $highest_entry = 0;
  my $noadjust;
  my $found_platex;
  my $fh;
  #
  # check tools.ini
  $noadjust = 0;
  $found_platex = 0;
  if (-r $tools) {
    # assume that succeeds, we tested -r above!
    open (FOO, "<", $tools);
    my @lines = <FOO>;
    chomp(@lines);
    close(FOO);
    # policy: if ptex2pdf appears in any of the program entries, we
    # do nothing. Only otherwise we add new entries.
    my %entries;
    my $in_entry = 0;
    foreach my $l (@lines) {
      if ($l =~ m/^\[(.*)\]\s*$/) {
        $in_entry = $1;
        $highest_entry = ( (0+$in_entry) > $highest_entry ? (0+$in_entry) : $highest_entry);
        next;
      }
      if ($l =~ m/^\s*$/) {
        # empty line terminates entry
        $in_entry = 0;
        next;
      }
      if ($l =~ m/^([^=]*)=(.*)$/) {
        if ($in_entry) {
          $entries{$in_entry}{$1} = $2;
        } else {
          tlwarn("\nptex2pdf postaction: line outside of entry in $tools: $l\n");
        }
        next;
      }
      # we are still here
      tlwarn("\nptex2pdf postaction: unrecognized line in $tools: $l\n");
    }
    # $Data::Dumper::Indent = 1;
    # print Data::Dumper->Dump([\%entries], ["entries"]);
    # now check that we don't see ptex2pdf
    for my $id (keys %entries) {
      if ($entries{$id}{'program'} && $entries{$id}{'program'} =~ m/^ptex2pdf/s) {
        if (!$noadjust) { # this test reduces duplicate info below
          info("ptex2pdf programs already included in tools.ini, not adding again.\n");
          $noadjust = 1;
        }
        # no need for edit tools.ini, but still we need to check if
        # the exact string "pLaTeX (ptex2pdf)" is available,
        # to ensure success of setting it as the default
        if ($entries{$id}{'name'} =~ m/^pLaTeX \(ptex2pdf\)$/s) {
          $found_platex = 1;
        }
      }
    }
  } else {
    # no tools, we need to create the path and the file
    mkdirhier($toolsdir);
    for my $t (sort keys %original) {
      my $id = sprintf("%03d", ++$highest_entry);
      $entries{$id} = $original{$t};
      if (win32()) {
        $entries{$id}{'program'} .= ".exe";
      }
    }
  }
  # adjust tools.ini
  if (!$noadjust) {
    for my $t (sort keys %ptex2pdf) {
      my $id = sprintf("%03d", ++$highest_entry);
      $entries{$id} = $ptex2pdf{$t};
      if (win32()) {
        $entries{$id}{'program'} .= ".exe";
      }
    }
    if (!open ($fh, ">>", $tools)) {
      tlwarn("\nptex2pdf postaction: cannot update $tools!\n");
      return 1;
    }
    # we are in install mode in a line that will be finished with ...done 
    info("(ptex2pdf postinst: adjusting TeXworks tools.ini)");
    for my $k (sort keys %entries) {
      print $fh "[", $k, "]\n";
      for my $key (qw/name program arguments showPdf/) {
        print $fh $key, '=', $entries{$k}{$key}, "\n";
      }
      print $fh "\n";
    }
    close($fh) or tlwarn("\nptex2pdf postaction: cannot close $tools\n");
    # the exact string "pLaTeX (ptex2pdf)" is added by myself
    $found_platex = 1;
  }
  #
  # Setting defaults on Mac needs plist changes, nothing we can easily do, sorry.
  if ($^O eq "darwin") {
    info("(ptex2pdf postinst: default setting on Mac is currently unsupported)");
    return 0;
  }
  # check TeXworks.ini
  $noadjust = 0;
  if (-r $tug) {
    # assume that succeeds, we tested -r above!
    open (FOO, "<", $tug);
    my @lines = <FOO>;
    chomp(@lines);
    close(FOO);
    # policy: if defaultEngine appears in TeXworks.ini, we do nothing.
    # Only otherwise we set the value to "pLaTeX (ptex2pdf)".
    foreach my $l (@lines) {
      if ($l =~ m/^defaultEngine=(.*)$/) {
        $noadjust = $1;
      }
    }
    if ($noadjust) {
      info("default is already set as $noadjust in TeXworks.ini, no change.\n");
    }
  } else {
    # no TeXworks.ini, we need to create the path and the file
    mkdirhier($tugdir);
  }
  # adjust TeXworks.ini
  if (!$noadjust) {
    if (!open ($fh, ">>", $tug)) {
      tlwarn("\nptex2pdf postaction: cannot update $tug!\n");
      return 1;
    }
    # we are in install mode in a line that will be finished with ...done 
    if ($found_platex) {
      info("(ptex2pdf postinst: adjusting TeXworks.ini)");
      print $fh "defaultEngine=pLaTeX (ptex2pdf)\n";
    } else {
      tlwarn("\nptex2pdf postaction: \"pLaTeX (ptex2pdf)\" not found, exiting without setting default!\n");
      return 1;
    }
    close($fh) or tlwarn("\nptex2pdf postaction: cannot close $tug\n");
  }
  #
  # all done
  return 0;
}

### Local Variables:
### perl-indent-level: 2
### tab-width: 2
### indent-tabs-mode: nil
### End:
# vim:set tabstop=2 expandtab: #