| 1 |
-- Functions for making brackets strict: |
| 2 |
|
| 3 |
stricttexAddBracesToBrackets = function(str) |
| 4 |
p = str:find('[%[%]]') |
| 5 |
if p then |
| 6 |
while true do |
| 7 |
if str:sub(p-1,p-1) == '\\' then |
| 8 |
q = str:sub(p+1,-1):find('[%[%]]') |
| 9 |
if q then |
| 10 |
p = p + q |
| 11 |
else |
| 12 |
break |
| 13 |
end |
| 14 |
elseif str:sub(p-1,p+1) == '<[>' or str:sub(p-1,p+1) == '<]>' then |
| 15 |
str = str:sub(1,p-2) .. str:sub(p,p) .. str:sub(p+2,-1) |
| 16 |
q = str:sub(p,-1):find('[%[%]]') |
| 17 |
if q then |
| 18 |
p = p + q - 1 |
| 19 |
else |
| 20 |
break |
| 21 |
end |
| 22 |
elseif str:sub(p,p) == '[' then |
| 23 |
str = str:sub(1,p) .. '{' .. str:sub(p+1,-1) |
| 24 |
q = str:sub(p+2,-1):find('[%[%]]') |
| 25 |
if q then |
| 26 |
p = p + q + 1 |
| 27 |
else |
| 28 |
break |
| 29 |
end |
| 30 |
else -- i.e. if str:sub(p,p) == ']' |
| 31 |
str = str:sub(1,p-1) .. '}' .. str:sub(p,-1) |
| 32 |
q = str:sub(p+2,-1):find('[%[%]]') |
| 33 |
if q then |
| 34 |
p = p + q + 1 |
| 35 |
else |
| 36 |
break |
| 37 |
end |
| 38 |
end |
| 39 |
end |
| 40 |
end |
| 41 |
return str |
| 42 |
end |
| 43 |
|
| 44 |
stricttexStrictBracketsOn = function() |
| 45 |
luatexbase.add_to_callback( |
| 46 |
"process_input_buffer", stricttexAddBracesToBrackets , "stricttexStrictBrackets" |
| 47 |
) |
| 48 |
ifStricttexStrictBracketsOn = true |
| 49 |
end |
| 50 |
|
| 51 |
stricttexStrictBracketsOff = function() |
| 52 |
if ifStricttexStrictBracketsOn then |
| 53 |
luatexbase.remove_from_callback( |
| 54 |
"process_input_buffer", "stricttexStrictBrackets" |
| 55 |
) |
| 56 |
ifStricttexStrictBracketsOn = false |
| 57 |
else |
| 58 |
tex.sprint('\\begingroup\\ExplSyntaxOn\\msg_error:nnnn { stricttex } { callback_not_registered } { \\StrictBracketsOn } { \\StrictBracketsOff }\\endgroup') |
| 59 |
end |
| 60 |
end |
| 61 |
|
| 62 |
-- Functions for numbers (and possibly primes) in command names |
| 63 |
|
| 64 |
stricttexReplaceNumbersByLetters = function(str) |
| 65 |
str = str:gsub('0','numberZERO') |
| 66 |
str = str:gsub('1','numberONE') |
| 67 |
str = str:gsub('2','numberTWO') |
| 68 |
str = str:gsub('3','numberTHREE') |
| 69 |
str = str:gsub('4','numberFOUR') |
| 70 |
str = str:gsub('5','numberFIVE') |
| 71 |
str = str:gsub('6','numberSIX') |
| 72 |
str = str:gsub('7','numberSEVEN') |
| 73 |
str = str:gsub('8','numberEIGHT') |
| 74 |
str = str:gsub('9','numberNINE') |
| 75 |
return str |
| 76 |
end |
| 77 |
|
| 78 |
stricttexFormatNumbers = function(str) |
| 79 |
local p, q = str:find('\\%a+%d') |
| 80 |
if p then |
| 81 |
while true do |
| 82 |
local r,s = str:sub(q,-1):find('%w+') -- ^ in the beginning did not work |
| 83 |
local newstring = stricttexReplaceNumbersByLetters( str:sub(q,q+s-1) ) |
| 84 |
local l = string.len(newstring) |
| 85 |
str = str:sub(1, q-1) .. newstring .. str:sub(q+s,-1) |
| 86 |
local t = str:sub(q+l,-1):find('\\%a+%d') |
| 87 |
if t then |
| 88 |
q = q + l + t - 1 |
| 89 |
else |
| 90 |
break |
| 91 |
end |
| 92 |
end |
| 93 |
end |
| 94 |
return str |
| 95 |
end |
| 96 |
|
| 97 |
stricttexReplacePrimes = function(str) |
| 98 |
str = str:gsub('\'','symbolPRIME') |
| 99 |
return str |
| 100 |
end |
| 101 |
|
| 102 |
|
| 103 |
stricttexFormatNumbersAndPrimes = function(str) |
| 104 |
local p, q = str:find('\\%a+[%d\']') |
| 105 |
if p then |
| 106 |
while true do |
| 107 |
local r,s = str:sub(q,-1):find('[%w\']+') -- ^ in the beginning did not work |
| 108 |
local newstring = stricttexReplaceNumbersByLetters( stricttexReplacePrimes( str:sub(q,q+s-1) ) ) |
| 109 |
local l = string.len(newstring) |
| 110 |
str = str:sub(1, q-1) .. newstring .. str:sub(q+s,-1) |
| 111 |
local t = str:sub(q+l,-1):find('\\%a+[%d\']') |
| 112 |
if t then |
| 113 |
q = q + l + t - 1 |
| 114 |
else |
| 115 |
break |
| 116 |
end |
| 117 |
end |
| 118 |
end |
| 119 |
return str |
| 120 |
end |
| 121 |
|
| 122 |
stricttexNumbersInCommandsOn = function() |
| 123 |
if ifStricttexNumbersAndPrimesInCommands then |
| 124 |
stricttexNumbersAndPrimesInCommandsOff() |
| 125 |
end |
| 126 |
luatexbase.add_to_callback( |
| 127 |
"process_input_buffer", stricttexFormatNumbers , "stricttexNumbersInCommands" |
| 128 |
) |
| 129 |
ifStricttexNumbersInCommands = true |
| 130 |
end |
| 131 |
|
| 132 |
stricttexNumbersInCommandsOff = function() |
| 133 |
if ifStricttexNumbersInCommands then |
| 134 |
luatexbase.remove_from_callback( |
| 135 |
"process_input_buffer", "stricttexNumbersInCommands" |
| 136 |
) |
| 137 |
ifStricttexNumbersInCommands = false |
| 138 |
else |
| 139 |
tex.sprint('\\begingroup\\ExplSyntaxOn\\msg_error:nnnn { stricttex } { callback_not_registered } { \\NumbersInCommandsOn } { \\NumbersInCommandsOff }\\endgroup') |
| 140 |
end |
| 141 |
end |
| 142 |
|
| 143 |
stricttexNumbersAndPrimesInCommandsOn = function() |
| 144 |
if ifStricttexNumbersInCommands then |
| 145 |
stricttexNumbersInCommandsOff() |
| 146 |
end |
| 147 |
luatexbase.add_to_callback( |
| 148 |
"process_input_buffer", stricttexFormatNumbersAndPrimes , "stricttexNumbersAndPrimesInCommands" |
| 149 |
) |
| 150 |
ifStricttexNumbersAndPrimesInCommandsOn = true |
| 151 |
end |
| 152 |
|
| 153 |
stricttexNumbersAndPrimesInCommandsOff = function() |
| 154 |
if ifStricttexNumbersAndPrimesInCommandsOn then |
| 155 |
luatexbase.remove_from_callback( |
| 156 |
"process_input_buffer", "stricttexNumbersAndPrimesInCommands" |
| 157 |
) |
| 158 |
ifStricttexNumbersAndPrimesInCommandsOn = false |
| 159 |
else |
| 160 |
tex.sprint('\\begingroup\\ExplSyntaxOn\\msg_error:nnnn { stricttex } { callback_not_registered } { \\NumbersAndPrimesInCommandsOn } { \\NumbersAndPrimesInCommandsOff }\\endgroup') |
| 161 |
end |
| 162 |
end |