| 1 |
-- lualineno version = 0.1, 2025-11-28 |
| 2 |
|
| 3 |
-- \seccc Initialization^^M |
| 4 |
-- Some declarations of local functions/constants of global ones |
| 5 |
-- to avoid table lookups. |
| 6 |
|
| 7 |
local runtoks = tex.runtoks |
| 8 |
local put_next = token.unchecked_put_next |
| 9 |
local create = token.create |
| 10 |
local new_tok = token.new |
| 11 |
local lbrace = new_tok(string.byte('{'), token.command_id'left_brace') |
| 12 |
local rbrace = new_tok(string.byte('}'), token.command_id'right_brace') |
| 13 |
local b = new_tok(string.byte('b'), token.command_id'other_char') |
| 14 |
local d = new_tok(string.byte('d'), token.command_id'other_char') |
| 15 |
local i = new_tok(string.byte('i'), token.command_id'other_char') |
| 16 |
local r = new_tok(string.byte('r'), token.command_id'other_char') |
| 17 |
local zero = new_tok(string.byte('0'), token.command_id'other_char') |
| 18 |
local get_next = token.get_next |
| 19 |
local scan_toks = token.scan_toks |
| 20 |
local scan_string = token.scan_string |
| 21 |
local scan_list = token.scan_list |
| 22 |
local scan_int = token.scan_int |
| 23 |
|
| 24 |
-- sadly there isn't a nice way in \LuaTeX/ to get |
| 25 |
-- a primitive token without using a csname. |
| 26 |
-- To be sure `\hbox` has the correct meaning |
| 27 |
-- we can use `tex.enableprimitives` to create |
| 28 |
-- a new csname with the meaning of the primitive, |
| 29 |
-- then create a token with the same `.mode` and `.command` |
| 30 |
-- fields so we won't need the csname anymore. |
| 31 |
-- All of this is to avoid to use some implementation details |
| 32 |
-- (`local hbox = new_tok(141, 21)`) |
| 33 |
|
| 34 |
local hbox do |
| 35 |
local prefix = '@lua^line&no_' |
| 36 |
while token.is_defined(prefix .. 'hbox') do |
| 37 |
prefix = prefix .. '@lua^line&no_' |
| 38 |
end |
| 39 |
tex.enableprimitives(prefix,{'hbox'}) |
| 40 |
local tok = create(prefix .. 'hbox') |
| 41 |
hbox = new_tok(tok.mode, tok.command) |
| 42 |
end |
| 43 |
|
| 44 |
local hlist_id = node.id('hlist') |
| 45 |
local vlist_id = node.id('vlist') |
| 46 |
local glyph_id = node.id('glyph') |
| 47 |
local tail = node.tail |
| 48 |
local get_props = node.getproperty |
| 49 |
local set_props = node.setproperty |
| 50 |
local get_attribute = node.get_attribute |
| 51 |
local set_attribute = node.set_attribute |
| 52 |
local node_flush = node.flush_node |
| 53 |
local insert_before = node.insert_before |
| 54 |
local insert_after = node.insert_after |
| 55 |
local traverse = node.traverse |
| 56 |
local rangedimensions = node.rangedimensions |
| 57 |
local node_copy = node.copy |
| 58 |
local base_kern = node.new('kern', 'user') |
| 59 |
local line_sub, eq_sub, align_sub, box_sub |
| 60 |
local ignored_subtypes = {} |
| 61 |
for k,v in pairs(node.subtypes("hlist")) do |
| 62 |
if v == "line" then line_sub = k end |
| 63 |
if v == "alignment" then align_sub = k end |
| 64 |
if v == "box" then box_sub = k end |
| 65 |
if v == "equation" then eq_sub = k end |
| 66 |
if v == "equationnumber" then ignored_subtypes[k] = true end |
| 67 |
if v == "mathchar" then ignored_subtypes[k] = true end |
| 68 |
end |
| 69 |
local displayalign_sub = #node.subtypes("hlist") + 1 |
| 70 |
for k,v in pairs(node.subtypes("vlist")) do |
| 71 |
if v == "vextensible" then ignored_subtypes[k] = true end |
| 72 |
if v == "vdelimiter" then ignored_subtypes[k] = true end |
| 73 |
end |
| 74 |
|
| 75 |
local setattribute = tex.setattribute |
| 76 |
local texerror = tex.error |
| 77 |
local texnest = tex.nest |
| 78 |
local format = tex.formatname |
| 79 |
|
| 80 |
-- The module currently works with |
| 81 |
-- \OpTeX, \LaTeX/ or Plain. |
| 82 |
|
| 83 |
local optex, latex, plain |
| 84 |
if format:find("optex") then |
| 85 |
optex = true |
| 86 |
elseif format:find("latex") then |
| 87 |
latex = true |
| 88 |
elseif format == "luatex" or |
| 89 |
format == "luahbtex" or |
| 90 |
format:find("plain") |
| 91 |
then |
| 92 |
plain = true |
| 93 |
end |
| 94 |
if not (optex or latex or plain) then |
| 95 |
error("lualineno: The format " .. format .. " is not supported\n\n" .. |
| 96 |
"Use OpTeX, LuaLaTeX or Plain.") |
| 97 |
end |
| 98 |
|
| 99 |
local lineno_types = { } |
| 100 |
local lineno_attrs = { } |
| 101 |
local LINENO_NUMBER = 0x1 |
| 102 |
local LINENO_RECURSE = 0x2 |
| 103 |
local lineno_marks = { |
| 104 |
anchor = 1, |
| 105 |
processed = 2, |
| 106 |
display = 3, |
| 107 |
} |
| 108 |
local type_attr = luatexbase and luatexbase.new_attribute('lualineno_type') or 0 |
| 109 |
local col_attr = luatexbase and luatexbase.new_attribute('lualineno_col') or 1 |
| 110 |
local mark_attr = luatexbase and luatexbase.new_attribute('lualineno_mark') or 2 |
| 111 |
local unset_attr = -0x7FFFFFFF |
| 112 |
|
| 113 |
-- We use the `luakeyval` module for the user interface |
| 114 |
|
| 115 |
local keyval = require('luakeyval') |
| 116 |
local scan_choice = keyval.choices |
| 117 |
local scan_bool = keyval.bool |
| 118 |
local process_keys = keyval.process |
| 119 |
local messages = { |
| 120 |
error1 = "lualineno: Wrong syntax in \\lualineno", |
| 121 |
value_forbidden = 'lualineno: The key "%s" does not accept a value', |
| 122 |
value_required = 'lualineno: The key "%s" requires a value', |
| 123 |
} |
| 124 |
|
| 125 |
-- \seccc Numbering Lines^^M |
| 126 |
-- In here we define the main functions of the module, |
| 127 |
-- the functions that find and number the lines in a page. |
| 128 |
-- |
| 129 |
-- The following function is used to number a line that is considered |
| 130 |
-- \"real" (i.e. has some glyphs in it that are not equation number or |
| 131 |
-- a big math delimiter). This function is used in the `lualineno.numbering` |
| 132 |
-- callback, so it can be replaced if desired. |
| 133 |
-- |
| 134 |
-- `line` is the hlist node representing the line, `line_type` is a lua table |
| 135 |
-- with the parameters defined in the define key according to the attribute |
| 136 |
-- and the column, `offset` is the total shift calculated from the start of |
| 137 |
-- the line and `width` is the width of the column containing the lines. |
| 138 |
|
| 139 |
local function number_line(line, line_type, offset, width, dir) |
| 140 |
|
| 141 |
local head = line.head |
| 142 |
local is_offset = line_type['offset'] |
| 143 |
|
| 144 |
-- In case \LaTeX/ is used without the luacolor package, |
| 145 |
-- we add an additional group to make the boxes color safe. |
| 146 |
-- Since `token.scan_string` is ran in horizontal mode, the |
| 147 |
-- default box direction is `\textdirection`, and this can be |
| 148 |
-- pretty random at shipout time, so an explicit direction is specified. |
| 149 |
-- Currently LTR is used, if someone ask an option can be added, but |
| 150 |
-- you can always use `\hbox bidr 1 {}` inside the box. |
| 151 |
|
| 152 |
put_next({rbrace, rbrace}) |
| 153 |
put_next(line_type.left) |
| 154 |
put_next({hbox,b,d,i,r,zero,lbrace,lbrace}) |
| 155 |
put_next({rbrace, rbrace}) |
| 156 |
put_next(line_type.right) |
| 157 |
put_next({hbox,b,d,i,r,zero,lbrace,lbrace}) |
| 158 |
|
| 159 |
-- to make sure \"right" always means right, we check the line direction. |
| 160 |
|
| 161 |
local end_box, start_box |
| 162 |
if line.dir == "TLT" then |
| 163 |
end_box = scan_list() |
| 164 |
start_box = scan_list() |
| 165 |
else |
| 166 |
start_box = scan_list() |
| 167 |
end_box = scan_list() |
| 168 |
end |
| 169 |
|
| 170 |
-- if the vertical list containing the line and the line has different directions |
| 171 |
-- we need to mirror the kerns as the kern means opposite direction then the shift, |
| 172 |
-- or alignment (similar to how `\shapemode` is working). |
| 173 |
|
| 174 |
local start_kern_width = 0 |
| 175 |
local end_kern_width = 0 |
| 176 |
if is_offset then |
| 177 |
if line.dir == dir then |
| 178 |
start_kern_width = offset |
| 179 |
end_kern_width = width - line.width - offset |
| 180 |
else |
| 181 |
start_kern_width = width - line.width - offset |
| 182 |
end_kern_width = offset |
| 183 |
end |
| 184 |
end |
| 185 |
|
| 186 |
if start_box.head then |
| 187 |
if start_kern_width ~= 0 then |
| 188 |
local start_kern = node_copy(base_kern) |
| 189 |
start_kern.kern = start_kern_width |
| 190 |
head = insert_before(head,head,start_kern) |
| 191 |
end |
| 192 |
head = insert_before(head,head,start_box) |
| 193 |
start_kern_width = -start_box.width - start_kern_width |
| 194 |
if start_kern_width ~= 0 then |
| 195 |
local start_kern = node_copy(base_kern) |
| 196 |
start_kern.kern = start_kern_width |
| 197 |
head = insert_before(head,head,start_kern) |
| 198 |
end |
| 199 |
else |
| 200 |
node_flush(start_box) |
| 201 |
end |
| 202 |
if end_box.head then |
| 203 |
if end_kern_width ~= 0 then |
| 204 |
local end_kern = node_copy(base_kern) |
| 205 |
end_kern.kern = end_kern_width |
| 206 |
head = insert_after(head,tail(head),end_kern) |
| 207 |
end |
| 208 |
head = insert_after(head,tail(head),end_box) |
| 209 |
else |
| 210 |
node_flush(end_box) |
| 211 |
end |
| 212 |
line.head = head |
| 213 |
end |
| 214 |
|
| 215 |
local lineno_callbacks |
| 216 |
if luatexbase then |
| 217 |
luatexbase.create_callback('lualineno.pre_numbering', 'simple', false) |
| 218 |
luatexbase.create_callback('lualineno.numbering', 'exclusive', number_line) |
| 219 |
luatexbase.create_callback('lualineno.post_numbering', 'simple', false) |
| 220 |
luatexbase.add_to_callback('lualineno.pre_numbering', function(_, line_type) |
| 221 |
runtoks(function() put_next(line_type['toks']) end) |
| 222 |
end, 'lualineno.runtoks') |
| 223 |
local call_callback = luatexbase.call_callback |
| 224 |
lineno_callbacks = function(line, line_type, offset, width, dir) |
| 225 |
call_callback('lualineno.pre_numbering', line, line_type, offset, width, dir) |
| 226 |
call_callback('lualineno.numbering', line, line_type, offset, width, dir) |
| 227 |
call_callback('lualineno.post_numbering', line, line_type, offset, width, dir) |
| 228 |
end |
| 229 |
else |
| 230 |
lineno_callbacks = function(line, line_type, offset, width, dir) |
| 231 |
runtoks(function() put_next(line_type['toks']) end) |
| 232 |
number_line(line, line_type, offset, width, dir) |
| 233 |
end |
| 234 |
end |
| 235 |
|
| 236 |
-- Not every object that would be considered a line from \LuaTeX's point of view |
| 237 |
-- would be considered a line from a human perspective. For example, a line |
| 238 |
-- containing only an indent box, or an alignment containing only rules, |
| 239 |
-- so we use the following two functions to search for a glyph node recursively, |
| 240 |
-- while ignoring boxes |
| 241 |
-- for equation number, for big delimiters (i.e. in `cases` environment) |
| 242 |
-- or dummy boxes for null delimiters. |
| 243 |
|
| 244 |
local function real_box(list) |
| 245 |
for n, id, sb in traverse(list) do |
| 246 |
if id == glyph_id then |
| 247 |
return true |
| 248 |
elseif (id == hlist_id or id == vlist_id) and not ignored_subtypes[sb] then |
| 249 |
if real_box(n.list) then |
| 250 |
return true |
| 251 |
end |
| 252 |
end |
| 253 |
end |
| 254 |
return false |
| 255 |
end |
| 256 |
|
| 257 |
-- If the first thing (that we care about) in a line is a glyph |
| 258 |
-- we simply number it, if it is an hlist we keep looking inside |
| 259 |
-- for glyphs and if it is a vlist we add the shift to the offset |
| 260 |
-- and go back to finding lines in that vlist. |
| 261 |
|
| 262 |
local function real_line(list, parent, offset) |
| 263 |
for n, id, sb in traverse(list) do |
| 264 |
if id == glyph_id then |
| 265 |
return true |
| 266 |
elseif id == vlist_id and not ignored_subtypes[sb] and real_box(n.list) then |
| 267 |
return n, offset + rangedimensions(parent, list, n) |
| 268 |
elseif id == hlist_id and not ignored_subtypes[sb] and real_box(n.list) then |
| 269 |
offset = offset + rangedimensions(parent, list, n) |
| 270 |
return real_line(n.list, n, offset) |
| 271 |
end |
| 272 |
end |
| 273 |
return false |
| 274 |
end |
| 275 |
|
| 276 |
-- This function finds the lines that needs to be numbered in a page. |
| 277 |
-- It should be used right before shipout, but can be used on individual |
| 278 |
-- boxes using the `processbox` key if needed (maybe special numbering order |
| 279 |
-- is desired). |
| 280 |
-- When a line found, `lineno_callbacks` is called to number it. |
| 281 |
local find_line |
| 282 |
find_line = function(parent, list, column, offset, width) |
| 283 |
|
| 284 |
if get_attribute(parent, mark_attr) == lineno_marks.processed then return end |
| 285 |
set_attribute(parent, mark_attr, lineno_marks.processed) |
| 286 |
|
| 287 |
-- We need to keep track of the parent id to know if the `.shift` |
| 288 |
-- field represent horizontal or vertical displacement. |
| 289 |
|
| 290 |
local parent_is_vlist = parent.id == vlist_id |
| 291 |
for n, id, sb in traverse(list) do |
| 292 |
|
| 293 |
-- lines are `hlist`s, so if a node is not one we dig deeper, |
| 294 |
-- while calculating the offset and the width. |
| 295 |
-- If a column is found, or a box marked with the `anchor` key |
| 296 |
-- then the offset is reset and the width is updated. |
| 297 |
|
| 298 |
if id ~= hlist_id then |
| 299 |
if not n.list then goto continue end |
| 300 |
local new_offset, new_width = offset, width |
| 301 |
local new_col = get_attribute(n, col_attr) |
| 302 |
if get_attribute(n, mark_attr) == lineno_marks.anchor or new_col then |
| 303 |
new_offset, new_width = 0, n.width |
| 304 |
elseif parent_is_vlist then |
| 305 |
new_offset = new_offset + n.shift |
| 306 |
end |
| 307 |
find_line(n, n.list, new_col or column, new_offset, new_width) |
| 308 |
goto continue |
| 309 |
end |
| 310 |
|
| 311 |
-- A line type is determined by the attribute of its last node so that line types can be switched |
| 312 |
-- from within the line (but maybe this should be configurable). |
| 313 |
-- The flag is a bitset that determines whether to number or recurse further. |
| 314 |
|
| 315 |
local line_attr = n.head and get_attribute(tail(n.head), type_attr) |
| 316 |
local line_type = line_attr and lineno_types[line_attr] and lineno_types[line_attr][column] |
| 317 |
local flag |
| 318 |
if sb == align_sub and get_attribute(n,mark_attr) == lineno_marks.display then |
| 319 |
flag = line_type and line_type[displayalign_sub] |
| 320 |
else |
| 321 |
flag = line_type and line_type[sb] |
| 322 |
end |
| 323 |
|
| 324 |
-- If a line does not have any attribute we don't number it, be we do recurse further. |
| 325 |
|
| 326 |
local should_number = flag and (flag & LINENO_NUMBER) ~= 0 or false |
| 327 |
local should_recurse = flag and (flag & LINENO_RECURSE) ~= 0 or true |
| 328 |
if not (should_number or should_recurse) then |
| 329 |
goto continue |
| 330 |
end |
| 331 |
|
| 332 |
-- This is the case where a line should be numbered only once. |
| 333 |
-- Maybe someone would like to number alignment once, regardless |
| 334 |
-- of the fact the first column contains cells with paragraphs. |
| 335 |
|
| 336 |
if should_number and not should_recurse then |
| 337 |
if real_box(n.list) then |
| 338 |
local new_offset = parent_is_vlist and (offset + n.shift) or offset |
| 339 |
lineno_callbacks(n, line_type, new_offset, width, parent.dir) |
| 340 |
end |
| 341 |
goto continue |
| 342 |
end |
| 343 |
|
| 344 |
-- If `real_line` returned a `new_offset`, the first thing encountered in the line is a `vlist`, |
| 345 |
-- so we need to find lines inside of that `vlist` as well. As before offset and width might need |
| 346 |
-- to be updated. If we encounter a column, maybe this line contains more columns, so we number the |
| 347 |
-- first one and keep looking for more. |
| 348 |
|
| 349 |
local m, new_offset = real_line(n.head, n, offset) |
| 350 |
local new_width = width |
| 351 |
if new_offset then |
| 352 |
local new_col = get_attribute(m, col_attr) |
| 353 |
if get_attribute(m, mark_attr) == lineno_marks.anchor or new_col then |
| 354 |
new_offset, new_width = 0, m.width |
| 355 |
elseif parent_is_vlist then |
| 356 |
new_offset = new_offset + n.shift |
| 357 |
end |
| 358 |
find_line(m, m.head, new_col or column, new_offset, new_width) |
| 359 |
if new_col then |
| 360 |
find_line(n, n.head, new_col, new_offset, width) |
| 361 |
end |
| 362 |
goto continue |
| 363 |
end |
| 364 |
|
| 365 |
if not (m and should_number) then goto continue end |
| 366 |
|
| 367 |
-- A line is found! update the offset and number it. |
| 368 |
|
| 369 |
local new_offset = parent_is_vlist and (offset + n.shift) or offset |
| 370 |
lineno_callbacks(n, line_type, new_offset, width, parent.dir) |
| 371 |
|
| 372 |
::continue:: |
| 373 |
end |
| 374 |
end |
| 375 |
|
| 376 |
if not plain then |
| 377 |
luatexbase.add_to_callback('pre_shipout_filter', function(box) |
| 378 |
find_line(box, box.list, 1, 0, box.width) |
| 379 |
return true |
| 380 |
end, 'lualineno.shipout') |
| 381 |
end |
| 382 |
|
| 383 |
-- Check if inside display for display alignment |
| 384 |
|
| 385 |
if luatexbase then |
| 386 |
luatexbase.add_to_callback("buildpage_filter", function(info) |
| 387 |
if info == "before_display" then |
| 388 |
setattribute(mark_attr,lineno_marks.display) |
| 389 |
elseif info == "after_display" then |
| 390 |
setattribute(mark_attr,unset_attr) |
| 391 |
end |
| 392 |
end, "lualineno.indisplay") |
| 393 |
end |
| 394 |
|
| 395 |
-- \seccc Anchoring numbers to a box^^M |
| 396 |
|
| 397 |
local function mark_last_vlist(n) |
| 398 |
local current = n |
| 399 |
while current do |
| 400 |
if current.id == vlist_id then |
| 401 |
set_attribute(current, mark_attr, lineno_marks.anchor) |
| 402 |
return true |
| 403 |
elseif current.id == hlist_id then |
| 404 |
if mark_last_vlist(tail(current.list)) then return true end |
| 405 |
end |
| 406 |
current = current.prev |
| 407 |
end |
| 408 |
return false |
| 409 |
end |
| 410 |
|
| 411 |
-- \seccc labels^^M |
| 412 |
|
| 413 |
local make_label |
| 414 |
local function find_label(line) |
| 415 |
local list = line.list |
| 416 |
for n in traverse(list) do |
| 417 |
if n.id == glyph_id then |
| 418 |
local props = get_props(n) |
| 419 |
if props then |
| 420 |
local label = props.lualineno |
| 421 |
if label then |
| 422 |
make_label(label, list, n) |
| 423 |
end |
| 424 |
end |
| 425 |
elseif n.list then |
| 426 |
find_label(n) |
| 427 |
end |
| 428 |
end |
| 429 |
end |
| 430 |
|
| 431 |
local function label_last_glyph(m, tokens) |
| 432 |
if optex then |
| 433 |
luatexbase.add_to_callback('lualineno.pre_numbering', find_label, 'lualineno.labels') |
| 434 |
elseif latex then |
| 435 |
luatexbase.add_to_callback('lualineno.post_numbering', find_label, 'lualineno.labels') |
| 436 |
end |
| 437 |
label_last_glyph = function(n, toks) |
| 438 |
local current = n |
| 439 |
while current do |
| 440 |
if current.id == glyph_id then |
| 441 |
local props = get_props(current) |
| 442 |
if not props then |
| 443 |
props = { } |
| 444 |
set_props(current,props) |
| 445 |
end |
| 446 |
props.lualineno = toks |
| 447 |
return true |
| 448 |
elseif current.list then |
| 449 |
if label_last_glyph(tail(current.list), toks) then |
| 450 |
return true |
| 451 |
end |
| 452 |
end |
| 453 |
current = current.prev |
| 454 |
end |
| 455 |
return false |
| 456 |
end |
| 457 |
return label_last_glyph(m, tokens) |
| 458 |
end |
| 459 |
|
| 460 |
-- \seccc User Interface^^M |
| 461 |
-- This section describes the definition of |
| 462 |
-- the one macro exposed to the end user. |
| 463 |
-- It is based on the luakeyval module. |
| 464 |
|
| 465 |
local defaults = { |
| 466 |
toks = { }, |
| 467 |
left = { }, |
| 468 |
right = { }, |
| 469 |
box = {number = true, recurse = true}, |
| 470 |
alignment = {number = true, recurse = true}, |
| 471 |
displayalignment = {number = true, recurse = true}, |
| 472 |
equation = {number = true, recurse = true}, |
| 473 |
line = {number = true, recurse = true}, |
| 474 |
offset = true, |
| 475 |
column = 1, |
| 476 |
} |
| 477 |
|
| 478 |
local inner_keys = { |
| 479 |
number = {scanner = scan_bool, default = true}, |
| 480 |
recurse = {scanner = scan_bool, default = true} |
| 481 |
} |
| 482 |
|
| 483 |
local defaults_keys = { |
| 484 |
toks = {scanner = scan_toks}, |
| 485 |
left = {scanner = scan_toks}, |
| 486 |
right = {scanner = scan_toks}, |
| 487 |
box = {scanner = process_keys, args = {inner_keys, messages}}, |
| 488 |
alignment = {scanner = process_keys, args = {inner_keys, messages}}, |
| 489 |
displayalignment = {scanner = process_keys, args = {inner_keys, messages}}, |
| 490 |
equation = {scanner = process_keys, args = {inner_keys, messages}}, |
| 491 |
line = {scanner = process_keys, args = {inner_keys, messages}}, |
| 492 |
offset = {scanner = scan_bool}, |
| 493 |
column = {scanner = scan_int} |
| 494 |
} |
| 495 |
|
| 496 |
local function set_defaults() |
| 497 |
local vals = process_keys(defaults_keys,messages) |
| 498 |
for k,v in pairs(vals) do |
| 499 |
defaults[k] = v |
| 500 |
end |
| 501 |
end |
| 502 |
|
| 503 |
local define_keys = { } |
| 504 |
for k,v in pairs(defaults_keys) do |
| 505 |
define_keys[k] = v |
| 506 |
end |
| 507 |
define_keys.name = {scanner = scan_string} |
| 508 |
|
| 509 |
local function define_lineno() |
| 510 |
local vals = process_keys(define_keys, messages) |
| 511 |
local name = vals['name'] |
| 512 |
if not name then |
| 513 |
texerror("lualineno: Missing name when defining a lineno") |
| 514 |
return |
| 515 |
end |
| 516 |
|
| 517 |
local col = vals['column'] or defaults.column |
| 518 |
lineno_attrs[name] = lineno_attrs[name] or #lineno_types + 1 |
| 519 |
local i = lineno_attrs[name] |
| 520 |
lineno_types[i] = lineno_types[i] or {} |
| 521 |
lineno_types[i][col] = lineno_types[i][col] or {} |
| 522 |
|
| 523 |
local c = lineno_types[i][col] |
| 524 |
|
| 525 |
local function store_type(key, subtype_id) |
| 526 |
local setting = vals[key] or defaults[key] |
| 527 |
local flags = 0 |
| 528 |
if setting.number then flags = flags | LINENO_NUMBER end |
| 529 |
if setting.recurse then flags = flags | LINENO_RECURSE end |
| 530 |
c[subtype_id] = flags |
| 531 |
end |
| 532 |
|
| 533 |
store_type('box', box_sub) |
| 534 |
store_type('alignment', align_sub) |
| 535 |
store_type('equation', eq_sub) |
| 536 |
store_type('line', line_sub) |
| 537 |
store_type('displayalignment', displayalign_sub) |
| 538 |
|
| 539 |
c.toks = vals.toks or defaults.toks |
| 540 |
c.left = vals.left or defaults.left |
| 541 |
c.right = vals.right or defaults.right |
| 542 |
if vals.offset ~= nil then |
| 543 |
c.offset = vals.offset |
| 544 |
else |
| 545 |
c.offset = defaults.offset |
| 546 |
end |
| 547 |
end |
| 548 |
|
| 549 |
local lualineno_keys = { |
| 550 |
set = {scanner = scan_string}, |
| 551 |
unset = { default = true }, |
| 552 |
define = {scanner = function() return true end, func = define_lineno}, |
| 553 |
defaults = {scanner = function() return true end, func = set_defaults}, |
| 554 |
anchor = { default = true }, |
| 555 |
label = {scanner = scan_toks, args = {false, true}}, |
| 556 |
typeattr = {scanner = scan_int}, |
| 557 |
colattr = {scanner = scan_int}, |
| 558 |
markattr = {scanner = scan_int}, |
| 559 |
processbox = {scanner = scan_int}, |
| 560 |
} |
| 561 |
|
| 562 |
local function lualineno() |
| 563 |
local saved_endlinechar = tex.endlinechar |
| 564 |
tex.endlinechar = 32 |
| 565 |
local vals = process_keys(lualineno_keys,messages) |
| 566 |
tex.endlinechar = saved_endlinechar |
| 567 |
if vals.set then |
| 568 |
local attr = lineno_attrs[vals.set] |
| 569 |
if attr then |
| 570 |
setattribute(type_attr, attr) |
| 571 |
else |
| 572 |
texerror("lualineno: type '" .. vals.set .. "' undefined") |
| 573 |
end |
| 574 |
end |
| 575 |
if vals.unset then |
| 576 |
setattribute(type_attr, unset_attr) |
| 577 |
end |
| 578 |
if vals.anchor then |
| 579 |
for i=texnest.ptr,0,-1 do |
| 580 |
if mark_last_vlist(texnest[i].tail) then return end |
| 581 |
end |
| 582 |
end |
| 583 |
if vals.label then |
| 584 |
for i=texnest.ptr,0,-1 do |
| 585 |
if label_last_glyph(texnest[i].tail, vals.label) then return end |
| 586 |
end |
| 587 |
end |
| 588 |
type_attr = vals.typeattr or type_attr |
| 589 |
col_attr = vals.colattr or col_attr |
| 590 |
mark_attr = vals.markattr or mark_attr |
| 591 |
if vals.processbox then |
| 592 |
local box = tex.box[vals.processbox] |
| 593 |
local col = get_attribute(box, col_attr) |
| 594 |
find_line(box, box.head, col or 1, 0, box.width) |
| 595 |
end |
| 596 |
end |
| 597 |
|
| 598 |
do |
| 599 |
if token.is_defined('lualineno') then |
| 600 |
texio.write_nl('log', "lualineno: redefining \\lualineno") |
| 601 |
end |
| 602 |
local function_table = lua.get_functions_table() |
| 603 |
local luafnalloc = luatexbase and luatexbase.new_luafunction |
| 604 |
and luatexbase.new_luafunction('lualineno') or #function_table + 1 |
| 605 |
token.set_lua('lualineno', luafnalloc, 'protected') |
| 606 |
function_table[luafnalloc] = lualineno |
| 607 |
end |
| 608 |
|
| 609 |
-- \seccc Format Specific Code^^M |
| 610 |
|
| 611 |
if format == 'optex' then |
| 612 |
|
| 613 |
-- To be able to use \OpTeX/'s color mechanism in line numbers the colorizing |
| 614 |
-- needs to happen after line numbers are added, so we remove and insert |
| 615 |
-- back again the colorizing function from the `pre_shipout_filter` callback. |
| 616 |
|
| 617 |
local colorize = callback.remove_from_callback('pre_shipout_filter', '_colors') |
| 618 |
callback.add_to_callback('pre_shipout_filter', colorize, '_colors') |
| 619 |
|
| 620 |
-- This is the patch for `\beginmulti` in order mark the columns boxes. |
| 621 |
-- For each box we assign an attribute with a value |
| 622 |
-- according to the column number. |
| 623 |
|
| 624 |
local replace = table.concat({ |
| 625 |
"\\_directlua{", |
| 626 |
"local column = tex.splitbox(6, tex.dimen[1], 'exactly') ", |
| 627 |
"local num = tex.count['_tmpnum'] ", |
| 628 |
"local attr = luatexbase.attributes['lualineno_col'] ", |
| 629 |
"node.set_attribute(column, attr, num) ", |
| 630 |
"node.write(column) ", |
| 631 |
"}", |
| 632 |
}) |
| 633 |
local find = [[\_vsplit 6 to\_dimen 1 ]] |
| 634 |
local patch, success = token.get_macro("_createcolumns"):gsub(find, replace) |
| 635 |
|
| 636 |
-- Log the success or failure of the patch |
| 637 |
|
| 638 |
if success > 0 then |
| 639 |
token.set_macro("_createcolumns", patch) |
| 640 |
else |
| 641 |
texio.write_nl('log', "lualineno: failed to patch \\_createcolumns") |
| 642 |
end |
| 643 |
|
| 644 |
-- \OpTeX/ only needs to run `\label[toks]` before a destination to label it. |
| 645 |
|
| 646 |
local lbracket = new_tok(string.byte('['), token.command_id'other_char') |
| 647 |
local rbracket = new_tok(string.byte(']'), token.command_id'other_char') |
| 648 |
local label_tok = create('_label') |
| 649 |
make_label = function(label) |
| 650 |
runtoks(function() |
| 651 |
put_next({rbracket}) |
| 652 |
put_next(label) |
| 653 |
put_next({label_tok,lbracket}) |
| 654 |
end) |
| 655 |
end |
| 656 |
|
| 657 |
elseif latex then |
| 658 |
|
| 659 |
-- Here we mark the columns according to `\if@firstcolumn` |
| 660 |
|
| 661 |
local true_mode do |
| 662 |
local prefix = '@lua^line&no_' |
| 663 |
while token.is_defined(prefix .. 'iftrue') do |
| 664 |
prefix = prefix .. '@lua^line&no_' |
| 665 |
end |
| 666 |
tex.enableprimitives(prefix,{'iftrue'}) |
| 667 |
local tok = create(prefix .. 'iftrue') |
| 668 |
true_mode = tok.mode |
| 669 |
end |
| 670 |
|
| 671 |
|
| 672 |
luatexbase.add_to_callback('pre_output_filter', function() |
| 673 |
if create('if@firstcolumn').mode == true_mode then |
| 674 |
setattribute(col_attr, 1) |
| 675 |
else |
| 676 |
setattribute(col_attr, 2) |
| 677 |
end |
| 678 |
return true |
| 679 |
end, 'lualineno.mark_columns') |
| 680 |
luatexbase.add_to_callback('buildpage_filter', function(info) |
| 681 |
if info == 'after_output' then |
| 682 |
setattribute(col_attr, unset_attr) |
| 683 |
end |
| 684 |
end, 'lualineno.mark_columns') |
| 685 |
|
| 686 |
-- If the luacolor package is loaded, |
| 687 |
-- colorizing must happen after line numbers |
| 688 |
-- are added to be able to color them. |
| 689 |
|
| 690 |
luatexbase.declare_callback_rule('pre_shipout_filter', |
| 691 |
'lualineno.shipout', 'before', 'luacolor.process') |
| 692 |
|
| 693 |
-- Since \LaTeX/ isn't really shipping out the page box, but a box |
| 694 |
-- containing the `\topmargin` and the page box which is shifted with |
| 695 |
-- `\moveright`, so it adds an undesired offset in `find_line`, so |
| 696 |
-- we mark the page box as `anchor`. |
| 697 |
|
| 698 |
local attr_num = luatexbase.attributes['lualineno_mark'] |
| 699 |
local replace = string.format([[\moveright \@themargin \vbox attr %d = -1]], attr_num) |
| 700 |
local find = [[\moveright \@themargin \vbox]] |
| 701 |
local patch, num_subs = token.get_macro("@outputpage"):gsub(find, replace) |
| 702 |
|
| 703 |
-- space controls are changed as well to avoid a bug in `token.set_macro` |
| 704 |
|
| 705 |
find = [[\catcode `\ 10\relax \catcode `\ 10\relax]] |
| 706 |
replace = [[\catcode 32=10\relax \catcode 9=10\relax]] |
| 707 |
patch, num_subs = patch:gsub(find, replace) |
| 708 |
|
| 709 |
-- Log the success or failure of the patch |
| 710 |
|
| 711 |
if num_subs > 0 then |
| 712 |
token.set_macro("@outputpage", patch) |
| 713 |
else |
| 714 |
texio.write_nl('log', "lualineno: failed to patch \\@outputpage") |
| 715 |
end |
| 716 |
|
| 717 |
-- \LaTeX/'s `\label`'s creates a whatsit node (`\write`), so we temporarily box the label |
| 718 |
-- to fetch this node, and add it to the list. |
| 719 |
|
| 720 |
local label_tok = create('label') |
| 721 |
make_label = function(label, list, n) |
| 722 |
runtoks(function() |
| 723 |
put_next({rbrace,rbrace}) |
| 724 |
put_next(label) |
| 725 |
put_next({hbox, lbrace, label_tok,lbrace}) |
| 726 |
local label_node = scan_list() |
| 727 |
list = insert_after(list,n,node_copy(label_node.head)) |
| 728 |
node_flush(label_node) |
| 729 |
end) |
| 730 |
end |
| 731 |
|
| 732 |
end |