]> git.lyx.org Git - wiki-uploads.git/blob - Layouts/Songbook/conditionals.sty
Update copies following NeedCProtect documentation
[wiki-uploads.git] / Layouts / Songbook / conditionals.sty
1 % rcsid = @(#)$Id: conditionals.sty,v 1.4 2002/01/17 22:38:45 christopher Exp $
2 %==========Conditionals by Donald Arseneau <asnd@triumf.ca>===========
3 %                       (as of 2002/01/17)
4 %  \if\blank{ } --- checks if parameter is blank (Spaces count as blank)
5 %  \if\given{ } --- checks if parameter is not blank: like \if\blank{}...\else
6 %  \if\nil{ }   --- checks if parameter is null (spaces are NOT null)
7 %
8 {\catcode`\!=8 % funny catcode so ! will be a delimiter
9  \catcode`\Q=3 % funny catcode so Q will be a delimiter
10 \long\gdef\given#1{88\fi\Ifbl@nk#1QQQ\empty!}
11 \long\gdef\blank#1{88\fi\Ifbl@nk#1QQ..!}% if null or spaces
12 \long\gdef\nil#1{\IfN@Ught#1* {#1}!}% if null
13 \long\gdef\IfN@Ught#1 #2!{\blank{#2}}
14 \long\gdef\Ifbl@nk#1#2Q#3!{\ifx#3}% same as above
15 }
16 \endinput
17 %=====================================================================
18 % COMMENTARY: Donald Arseneau's commentary on these macros.
19 %=====================================================================
20 % There are many ways to check if a field is blank.  The clearest is:
21
22 % \long\def\@tempa{#1}\ifx\@tempa\@empty ... \else ... \fi
23
24 % (use \empty for plain TeX)
25
26 % There are a few deficiencies with this approach: it is not "expandable"
27 % so it can't be executed in an \edef or a \write; it redefines some
28 % command (\@tempa here), and it is unsafe to put the first part in a
29 % macro because of the unbalanced \fi.  Below are my versions which are 
30 % expandable and safe.  There are % two versions for the syntax:  
31 %   \ifnull{#1}\then...   and     \if\nil{#1}... 
32 % [I hear the second syntax comes from DEK].   Choose your favorite.
33
34 %========================================================================
35 %  \ifblank --- checks if parameter is blank (Spaces count as blank)
36 %  \ifgiven --- checks if parameter is not blank: like \ifblank...\else
37 %  \ifnull  --- checks if parameter is null (spaces are NOT null)
38 %     use \ifgiven{#1}\then   ...  \else  ...  \fi   etc
39 \let\then\iftrue
40 {\catcode`\!=8 % funny catcode so ! will be a delimiter
41  \catcode`\Q=3 % funny catcode so Q will be a delimiter
42 \long\gdef\ifgiven#1\then{\Ifbl@nk#1QQQ\empty!}% negative of \ifblank
43 \long\gdef\ifblank#1\then{\Ifbl@nk#1QQ..!}% if null or spaces
44 \long\gdef\Ifbl@nk#1#2Q#3!{\ifx#3}
45 \long\gdef\ifnull#1\then{\IfN@LL#1* {#1}!}% if null
46 \long\gdef\IfN@LL#1 #2!{\ifblank{#2}\then}
47 }
48 %
49 %---------------------------------------------------------------------
50 %  Other version: use \if\given{ }, \if\blank{ }, \if\nil{ }
51 %  (There is no \then).
52 %
53 {\catcode`\!=8 % funny catcode so ! will be a delimiter
54  \catcode`\Q=3 % funny catcode so Q will be a delimiter
55 \long\gdef\given#1{88\fi\Ifbl@nk#1QQQ\empty!}
56 \long\gdef\blank#1{88\fi\Ifbl@nk#1QQ..!}% if null or spaces
57 \long\gdef\nil#1{\IfN@Ught#1* {#1}!}% if null
58 \long\gdef\IfN@Ught#1 #2!{\blank{#2}}
59 \long\gdef\Ifbl@nk#1#2Q#3!{\ifx#3}% same as above
60 }
61 %---------------------------------------------------------------------
62
63 % One note of caution:  Even though these macros can be nested
64 % because they give balanced \if - \fi constructs (\if or \then),
65 % nesting is still a danger if the tested parameter text contains
66 % an unbalanced \if or \fi. As a general rule, ill-defined parameter
67 % text should not be skipped over by \if.
68
69 % The special delimiter above is a funny Q character having the category
70 % 3, usually associated with $.  If there is some strange application 
71 % where Q might have catcode 3, then a better delimiter would be needed.
72 % The following paranoid definitions use QQ with *different* categories:
73 % Q(4)Q(3) as the delimiter.
74 %
75 {\catcode`\!=8 % funny catcode so ! will be a delimiter
76  \catcode`\Q=3 % funny catcode so Q will be a delimiter
77  \catcode`\R=4 % another funny catcode for Q !
78  \uccode`\R=`\Q
79  \uppercase{\long\gdef\given#1{11\fi\Ifbl@nk#1RQ11RQ12RQ!}}
80  \uppercase{\long\gdef\blank#1{11\fi\Ifbl@nk#1RQ12RQ11RQ!}}% if null or spaces
81  \long\gdef\nil#1{\IfN@Ught#1* {#1}!}% if null
82  \long\gdef\IfN@Ught#1 #2!{\blank{#2}}
83  \uppercase{\long\gdef\Ifbl@nk#1#2RQ#3RQ#4!{\ifx#3}}% same as above
84 }