From 15685e52158389814e17d0b855a78851b3b3f879 Mon Sep 17 00:00:00 2001
From: Philipp Gesang <phg@phi-gamma.net>
Date: Sat, 7 Nov 2015 00:28:03 +0100
Subject: [doc] revise listings printer

---
 doc/Makefile             |  2 +-
 doc/luaotfload-latex.tex | 73 ++++++++++++++++++++++++++++++++++++------------
 2 files changed, 56 insertions(+), 19 deletions(-)

diff --git a/doc/Makefile b/doc/Makefile
index 6d68281..0355a0e 100644
--- a/doc/Makefile
+++ b/doc/Makefile
@@ -1,6 +1,6 @@
 NAME		= luaotfload
 DOCPDF		= $(NAME).pdf
-DOCSRC		= $(NAME)-latex.tex
+DOCSRC		= $(NAME)-latex.tex $(NAME)-main.tex
 
 SCRIPTNAME	= luaotfload-tool
 TOOLMANSRC	= $(SCRIPTNAME).rst
diff --git a/doc/luaotfload-latex.tex b/doc/luaotfload-latex.tex
index 32ad3cb..e3541c5 100644
--- a/doc/luaotfload-latex.tex
+++ b/doc/luaotfload-latex.tex
@@ -230,48 +230,85 @@
   \catcode 32 \othercatcode %% \spaceasciicode
 }
 
+\directlua {
+  document = document or { }
+  document.vrbcatcodesidx = tonumber (\the \vrbcatcodes)
+}
+
 \newcatcodetable \literalcatcodes
 \setcatcodetable \literalcatcodes {%
   \catcodetable \CatcodeTableString
   \catcode 32 \activecatcode %% \spaceasciicode
 }
 
+\def \listingsurroundskip {\vskip \baselineskip}
+
 \def \beginlisting {%
+  \noindent
   \begingroup
     \catcodetable \vrbcatcodes
     \beginlistingindeed%
 }
 
 \directlua {
-  local texprint   = tex.print
-  local stringsub  = string.sub
-  local backslash  = string.char (0x5c)
-  document         = document or { }
+  local texsprint       = tex.sprint
+  local stringis_empty  = string.is_empty
+  local stringsub       = string.sub
+  local stringgsub      = string.gsub
+  %local backslash       = unicode.utf8.char (0x200c)
+  local backslash       = unicode.utf8.char (0x5c)
+  local escaped         = [[\string\string\string\]]
   document.printlines = function (buffer)
-    for _, line in next, string.explode (buffer, "\noexpand\n") do
-      if stringsub (line, 1, 1) == " " then
-        line = backslash .. line
+    local lines = string.explode (buffer, "\noexpand\n")
+    print ""
+    for i, line in next, lines do
+      local line = stringgsub (line, backslash, escaped)
+      if stringis_empty (line) then
+        print (i, "listing: <empty line />")
+        texsprint [[\string\listingpar]]
+      else
+        local line = [[\string\beginlistingline]]
+                  .. line
+                  .. [[\string\endlistingline]]
+                  .. [[\string\listingpar]]
+        print (i, "listing: «" .. line .. "»")
+        texsprint (document.vrbcatcodesidx, line)
       end
-      texprint (-1, line)
-      texprint (-1, "")
     end
   end
 }
 
+\def \listingpar {\endgraf}
+
+\let \endlistingline \relax
+\let \endlisting     \relax
+
+\protected \def \beginlistingline{%
+  \leavevmode
+  \begingroup
+    \beginlistinglineindeed%
+}
+
+\def \beginlistinglineindeed #1\endlistingline{%
+  \endgroup
+  \hbox{%
+    \addfontfeature {RawFeature=-tlig;-liga}%% So one can’t just turn them all off at once using the ``Ligatures`` key?
+    \obeyspaces
+    #1}%
+}
+
 \def \beginlistingindeed#1\endlisting{%
   \endgroup
   \begingroup
+    \endgraf
+    \listingsurroundskip
     \ttfamily
     \small
-    \begin {quote}
-      \bgroup
-        \addfontfeature {RawFeature=-tlig;-liga}%% So one can’t just turn them all off at once using the ``Ligatures`` key?
-        \catcodetable \literalcatcodes
-        \obeyspaces
-        \obeylines
-        \directlua{document.printlines ([==[\detokenize {#1}]==])}
-      \egroup
-    \end {quote}
+    \parindent = 0em
+    \leftskip = 2em
+    \hangindent = 2em
+    \directlua{document.printlines ([==[\detokenize {#1}]==])}%
+    \listingsurroundskip
   \endgroup
 }
 
-- 
cgit v1.2.3