summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/latex/plautopatch/plautopatch.sty
blob: dbc40429c326f46ef5ad2e383112f0d0f01baab6 (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
%
% plautopatch.sty
% written by Hironobu Yamashita (@aminophen)
%
% This package is part of the plautopatch bundle.
% https://github.com/aminophen/plautopatch
%

\NeedsTeXFormat{pLaTeX2e}
\ProvidesPackage{plautopatch}
    [2018/08/21 v0.2 Automated patches for pLaTeX/upLaTeX]
\def\platpc@pkgname{plautopatch}
\def\platpc@warn{\PackageWarningNoLine\platpc@pkgname}
\def\platpc@info{\PackageInfo\platpc@pkgname}

\RequirePackage{filehook}

% low-level helpers

%% \platpc@req@add{<package>}
% = if not loaded yet, \RequirePackage{<package>}
%   and add to list.
\def\platpc@req@add#1{%
  \@ifpackageloaded{#1}{}{\RequirePackage{#1}\platpc@addtolist{#1}}%
}

%% \platpc@addtolist{<package>}
% = add <package> to list named \platpc@list.
\def\platpc@addtolist#1{\xdef\platpc@list{\platpc@list, #1}}
\def\platpc@list{}% initialize

% high-level helpers

%% \platpc@patch@after{<orig>}{<patch>}
% = Load <patch> after <orig>.
\def\platpc@patch@after#1#2{%
  \expandafter\def\csname platpc@end@#1\endcsname{%
    \expandafter\ifx\csname platpc@disable@#1\endcsname\relax
      \platpc@req@add{#2}%
    \fi
  }%
  \AtEndOfPackageFile{#1}{\csname platpc@end@#1\endcsname}%
}
\@onlypreamble\platpc@patch@after

%% \platpc@patch@after@both{<orig 1>}{<orig 2>}{<patch>}
% = Load <patch> after both <orig 1> & <orig 2>.
\def\platpc@patch@after@both#1#2#3{%
  % for order #2 => #1
  \expandafter\def\csname platpc@end@#1+#2\endcsname{%
    \expandafter\ifx\csname platpc@disable@#1\endcsname\relax
     \expandafter\ifx\csname platpc@disable@#2\endcsname\relax
      \@ifpackageloaded{#2}{\platpc@req@add{#3}}{}%
     \fi
    \fi
  }%
  \AtEndOfPackageFile{#1}{\csname platpc@end@#1+#2\endcsname}%
  % for order #1 => #2
  \expandafter\def\csname platpc@end@#2+#1\endcsname{%
    \expandafter\ifx\csname platpc@disable@#2\endcsname\relax
     \expandafter\ifx\csname platpc@disable@#1\endcsname\relax
      \@ifpackageloaded{#1}{\platpc@req@add{#3}}{}%
     \fi
    \fi
  }%
  \AtEndOfPackageFile{#2}{\csname platpc@end@#2+#1\endcsname}%
}
\@onlypreamble\platpc@patch@after@both

%% \platpc@patch@before{<orig>}{<patch>}
% = Load <patch> before <orig>.
%   used when both conditions are met:
%   - <patch> must be loaded *before* <orig>
%   - <patch> contains \RequirePackage{<orig>}
\def\platpc@patch@before#1#2{%
  \expandafter\def\csname platpc@begin@#1\endcsname{%
    % if <patch> is already loaded, nothing to do
    \@ifpackageloaded{#2}{}{% else
      \expandafter\ifx\csname platpc@disable@#1\endcsname\relax
        % the code (*!) should be used only once,
        % remove it immediately when this macro is executed
        \expandafter\let\csname platpc@begin@#1\endcsname\relax
        % pretend as if <orig> not loaded
        \expandafter\let\csname ver@#1.sty\endcsname\relax
        % load the <patch> package
        \RequirePackage{#2}\platpc@addtolist{#2}%
        % avoid loading <orig> twice by discarding "\@@input <orig>" (*!)
        \let\platpc@filehook@@atbegin\filehook@@atbegin
        \def\filehook@@atbegin\@@input####1\filehook@atend{%
          \let\filehook@@atbegin\platpc@filehook@@atbegin
          % avoid infinite loop even when \AtEndOfPackageFile used
          \expandafter\let\csname filehook@atend@#1.sty\endcsname\relax
          \filehook@@atbegin
          \filehook@atend}%
        % all done
      \fi
    }%
  }%
  \AtBeginOfPackageFile{#1}{\csname platpc@begin@#1\endcsname}%
}
\@onlypreamble\platpc@patch@before

% interface for users who want don't patches

%% \plautopatchdisable{<orig 1>,<orig 2>,...}
% = Disable patches triggered by <orig 1>, <orig 2>, ...
\newcommand{\plautopatchdisable}[1]{%
  \edef\@tempa{\zap@space#1 \@empty}%
  \@for\@tempa:=\@tempa\do{%
    \expandafter\let\csname platpc@disable@\@tempa\endcsname\@empty
  }%
}
\@onlypreamble\plautopatchdisable

% show list of loaded patches

\AtEndDocument{\platpc@showlist}
\def\platpc@showlist{%
  \ifx\platpc@list\@empty
    \platpc@info{No additional packages loaded}
  \else
    % the first token of \platpc@list should be ','
    \typeout{%
      ***** List of packages loaded by `\platpc@pkgname': *****^^J%
      \expandafter\@gobble\platpc@list.^^J%
      *****************************************************}%
  \fi
}

% register patches

\ifx\enablecjktoken\@undefined
  \platpc@patch@after{tracefnt}{ptrace}% platex
\else
  \platpc@patch@after{tracefnt}{uptrace}% uplatex
\fi
\platpc@patch@after{fltrace}{pfltrace}% platex
\platpc@patch@after{array}{plarray}% platex-tools
\platpc@patch@after@both{array}{plext}{plextarray}% platex-tools
\platpc@patch@after@both{delarray}{plext}{plextdelarray}% platex-tools
\platpc@patch@after{everysel}{pxeverysel}% platex-tools
\platpc@patch@after{everyshi}{pxeveryshi}% platex-tools
\platpc@patch@after{atbegshi}{pxatbegshi}% platex-tools
\platpc@patch@before{ftnright}{pxftnright}% platex-tools
\platpc@patch@after{pdfpages}{pxpdfpages}% (maintained here!)

\endinput