|
Warning: this is an htmlized version!
The original is here, and the conversion rules are here. |
#!/usr/bin/env lua51
-- texpage.lua:
-- Find the page corresponding to a line in a .tex file.
--
-- Here's how this works. Most of my .tex files have "anchors", like:
-- (find-LATEX "2010unilog-current.tex" "comparison-theorem")
-- (find-LATEXfile "2010unilog-current.aux" "comparison-theorem")
--
-- % --------------------
-- % «comparison-theorem» (to ".comparison-theorem")
-- % (s "The comparison theorem" "comparison-theorem")
-- \myslide {The comparison theorem} {comparison-theorem}
--
-- The "\myslide" generates a block like
--
-- \newlabel{comparison-theorem}{{}{33}}
-- \@writefile{mylos}{\tocline {The comparison theorem} {33}}
--
-- in the corresponding ".aux" file, where the "33" is the page
-- number; the functions in this file do things with all that.
-- «.getauxtable» (to "getauxtable")
-- «.gettextable» (to "gettextable")
-- «.gettextag» (to "gettextag")
-- «.setvars» (to "setvars")
-- «.command-line» (to "command-line")
-- «getauxtable» (to ".getauxtable")
-- Process the contents of a ".aux" file (auxstuff) and return a table
-- of (tag, page) pairs.
--
auxclean1 = function (li) return (string.gsub(li, "[{}]", "")) end
auxclean2 = function (li) return string.match(li, "[^0-9]*([0-9]+)") end
getauxtable = function (auxstuff)
local pat = "\n.newlabel(%b{})(%b{})"
local T = {}
for a,b in string.gmatch(auxstuff, pat) do
local tag, page = auxclean1(a), auxclean2(b)+0
T[tag] = page
end
return T
end
-- «gettextable» (to ".gettextable")
-- This function receives an "auxtable" (produced by the function
-- above) and the contents of a ".tex" file split into lines
-- (the "texlines") and returns a table of (linenum, tag) pairs.
--
gettextable = function (auxtable, texlines)
local T = {}
local pat = "^%%%s*(%b\171\187)"
local clean = function (str) return (string.gsub(str, "[\171\187]", "")) end
for i=1,#texlines do
local line = texlines[i]
local tag = string.match(line, pat) -- %<spaces>«tag»<rest>
if tag then -- if this line has a «tag»
tag = clean(tag) -- then remove its "«»"s;
if auxtable[tag] then -- if this tag has an associated page
T[i] = tag -- then store its line in the table T
end
-- PP(i, tag, auxtable[tag])
end
end
return T
end
-- «gettextag» (to ".gettextag")
-- Try to find the closest "anchor with an associated page"
-- on or above a given line in a ".tex" file.
--
gettextag = function (auxtable, textable, linenum)
for i=linenum,1,-1 do
local tag = textable[i]
if tag then return tag, i, auxtable[tag] end
end
end
-- «setvars» (to ".setvars")
-- From a texfname and a linenum set lots of global vars.
--
setvars = function (texfname_, linenum_)
texfname, linenum = texfname_, linenum_
dvifname = string.gsub(texfname, "%.tex$", ".dvi")
auxfname = string.gsub(texfname, "%.tex$", ".aux")
auxstuff = readfile(auxfname)
auxtable = getauxtable(auxstuff)
texlines = splitlines(readfile(texfname))
textable = gettextable(auxtable, texlines)
tag = gettextag(auxtable, textable, linenum)
page = auxtable[tag] or 1
PP(texfname, dvifname)
dvicmd = format("xdvi %s +%d", dvifname, page)
texlink = format("(find-fline \"%s\" %d)", texfname, linenum)
dvilink = format("(find-dvipage \"%s\" %d)", dvifname, page)
end
-- «command-line» (to ".command-line")
-- (find-luamanualw3m "#6" "Lua Stand-alone")
-- (find-luamanualw3m "#6" "arg =")
-- (find-angg "LUA/lua50init.lua" "PP")
-- (find-sh "~/LUA/texpage.lua foo bar")
-- (find-sh "~/LUA/texpage.lua -links ~/LATEX/2010unilog-current.tex 1000")
-- (find-bgprocess "~/LUA/texpage.lua -xdvi ~/LATEX/2010unilog-current.tex 1000")
--
arg = arg or {}
if arg[1] == "-links" then
setvars(arg[2], tonumber(arg[3]))
print(dvilink)
print(texlink)
elseif arg[1] == "-xdvi" then
setvars(arg[2], tonumber(arg[3]))
os.execute(dvicmd)
else
print('See: (find-angg "LUA/texpage.lua" "command-line")')
PP("arg =", arg)
end
--[[
-- (find-LATEX "2010unilog-current.tex" "equality-hofmann95")
* (eepitch-lua51)
* (eepitch-kill)
* (eepitch-lua51)
ee_dofile "~/LUA/texpage.lua"
texfname = ee_expand "~/LATEX/2010unilog-current.tex"
linenum = 1000
linenum = 6700
-- setvars(texfname, linenum)
s = "{{}{5}{\\relax }{}{}}"
= string.match(s, "[^0-9]*([0-9]*)")
= auxclean2(s)
dvifname = string.gsub(texfname, "%.tex$", ".dvi")
auxfname = string.gsub(texfname, "%.tex$", ".aux")
auxstuff = readfile(auxfname)
auxtable = getauxtable(auxstuff)
texlines = splitlines(readfile(texfname))
textable = gettextable(auxtable, texlines)
tag = gettextag(auxtable, textable, linenum)
page = auxtable[tag] or 1
PP(texfname, dvifname)
dvicmd = format("xdvi %s +%d", dvifname, page)
texlink = format("(find-fline \"%s\" %d)", texfname, linenum)
dvilink = format("(find-dvipage \"%s\" %d)", dvifname, page)
print(tag)
print(texlink)
print(dvilink)
print(dvicmd)
os.execute(dvicmd)
--]]
-- Local Variables:
-- coding: raw-text-unix
-- ee-anchor-format: "«%s»"
-- End: