]> mj.ucw.cz Git - ucwmac.git/blobdiff - ucwmac.tex
ucw-link: Better macros for URLs and URL links
[ucwmac.git] / ucwmac.tex
index 74211b3de2bcc1a645b55d9058e355150985a21c..bbee36d376e4aa3553613630549682d7fdc5e544 100644 (file)
@@ -2,6 +2,8 @@
 % Written by Martin Mares <mj@ucw.cz> in 2010--2018 and placed into public domain
 % -------------------------------------------------------------------------------
 
+\ifx\ucwmodule\undefined\else\endinput\fi
+
 %%% Prolog %%%
 
 % We'll use internal macros of plain TeX
        \pdfpkresolution=600    % Provide a reasonable default
 \fi\fi
 
+%%% Auxiliary macros %%%
+
+% Prepend/append #2 to the definition of #1
+\long\def\prependef#1#2{\expandafter\def\expandafter#1\expandafter{#2#1}}
+\long\def\appendef#1#2{\expandafter\def\expandafter#1\expandafter{#1#2}}
+
+\def\ucwwarn#1{\immediate\write16{*** Warning: #1 ***}}
+
 %%% Page size and margins %%%
 
 % If you modify these registers, call \setuppage afterwards
 \setmargins{1truein}
 \setpaper{a4}
 
+%%% Macros with optional arguments %%%
+
+% After \def\a{\withoptarg\b}, the macro \a behaves in this way:
+%      \a[arg]         does \def\optarg{arg} and then it expands \b
+%      \a              does \let\optarg=\relax and then it expands \b
+\def\withoptarg#1{\let\xoptcall=#1\futurelet\next\xopt}
+\def\xopt{\ifx\next[\expandafter\xoptwith\else\let\optarg=\relax\expandafter\xoptcall\fi}
+\def\xoptwith[#1]{\def\optarg{#1}\xoptcall}
+
+% A shortcut for defining macros with optional arguments:
+% \optdef\macro behaves as \def\domacro, while \macro itself is defined
+% as a wrapper calling \domacro using \withoptarg.
+\def\optdef#1{%
+       \edef\xoptname{\expandafter\eatbackslash\string#1}%
+       \edef#1{\noexpand\withoptarg\csname do\xoptname\endcsname}%
+       \expandafter\def\csname do\xoptname\endcsname
+}
+
+% Trick: \eatbackslash eats the next backslash of category 12
+\begingroup\lccode`\+=`\\
+\lowercase{\endgroup\def\eatbackslash+{}}
+
+% Expand to the optional argument if it exists
+\def\optargorempty{\ifx\optarg\relax\else\optarg\fi}
+
 %%% Placing material at specified coordinates %%%
 
 % Set all dimensions of a given box register to zero
 % Non-breakable identifiers
 \def\<#1>{\leavevmode\hbox{\I #1}}
 
-% A handy shortcut
+% Handy shortcuts
 \let\>=\noindent
+\def\\{\hfil\break}
 
 % Variants of \centerline, \leftline and \rightline, which are compatible with
 % verbatim environments and other catcode hacks
 % \putimage{width specification}{file}
 \def\putimage#1#2{\hbox{\pdfximage #1{#2}\pdfrefximage\pdflastximage}}
 
+% Let ~ be protected
+\let\plaintilde=~
+\protected\def~{\plaintilde}
+
+%%% Localization %%%
+
+% Current language
+\def\localelang{en}
+
+% Define a new localized string: \localedef{language}{identifier}{message}
+\def\localedef#1#2{\expandafter\def\csname loc:#1:#2\endcsname}
+
+% Expand a localized string in the current language: \localemsg{identifier}
+\def\localestr#1{%
+       \ifcsname loc:\localelang:#1\endcsname
+               \csname loc:\localelang:#1\endcsname
+       \else
+               \ucwwarn{Localized string #1 not defined in language \localelang}%
+               ???%
+       \fi
+}
+
+%%% Modules %%%
+
+\def\ucwmodule#1{
+       \ifcsname ucwmod:#1\endcsname
+       \else
+               \input ucw-#1.tex
+       \fi
+}
+
+\def\ucwdefmodule#1{
+       \ifcsname ucwmod:#1\endcsname\endinput\fi
+       \expandafter\let\csname ucwmod:#1\endcsname=\relax
+}
+
 %%% Epilog %%%
 
 % Let's hide all internal macros