% texinfo.t2p
% Dorai Sitaram

% This macro file is loaded by TeX2page when processing
% Texinfo files.  If the input document, say
% jobname.texi, needs additional TeX2page-specific
% macros, you may put them in jobname.t2p (as for input
% documents in plain or LaTeX).  If you wish to
% redefine any of the macros already defined in
% texinfo.t2p, you will need to explicitly \input
% texinfo.t2p in jobname.t2p, _before_ the
% redefinitions.  Whether or not jobname.t2p explicitly
% \inputs it, texinfo.t2p will be loaded by TeX2page
% only once.  If jobname.t2p does not exist, or if it
% does _not_ explicitly \input texinfo.t2p,
% jobname.texi will implicitly \input 
% texinfo.t2p where it tries to \input texinfo.

% [SK] this is an attempt to make Dorai's tex2page work on
% Gauche-ref?.texi.  So far I haven't succeeded, but I put
% this in CVS anyway to work in future.
% $Id: texinfo.t2p,v 1.1 2004-02-02 11:43:01 shirok Exp $

\ifx\ifinfo\undefined
\let\endloadingtexinfotiipnow\relax
\else
\let\endloadingtexinfotiipnow\endinput
\message{texinfo.t2p already loaded}
\fi
\endloadingtexinfotiipnow

\let\comment\TIIPcomment
\let\c\comment
\let\changepagesizes\c
\let\internalpagesizes\c
\let\setchapternewpage\c
\let\setfilename\c
%\let\settitle\c
\let\headings\c
\let\synindex\c
\let\syncodeindex\c
\let\footnotestyle\c
\let\paragraphindent\c
\let\dircategory\c
\let\defcodeindex\c
\let\shorttitlepage\c

\def\settitle#1
{\externaltitle{#1}}

\def\.{.}

%\let\contents\tableofcontents


\def\contents{\evalh{
(toss-back-string "\\tableofcontents")
(toss-back-string
(if *using-chapters?*
"\\NONTEXINFOchapter*{Contents}"
"\\NONTEXINFOsection*{Contents}"))
}}

\def\titlefont#1{{\Huge #1}}

%\def\titlepage{\let\pageOLD\page
%\TIIPendgraf\begingroup\large
%\def\page{\endgroup\pageOLD\titlepage}}
%
%\let\endtitlepage\endgroup


\let\titlepage\relax
\let\endtitlepage\relax

\def\titlepage{\TIIPendgraf\begingroup\large}
\let\endtitlepage\endgroup

\evalh{(define *texinfo-values* '())}

\def\setQii{%
\evalh{
(let* ((var (get-peeled-group))
       (val (get-peeled-group)))
  (set! *texinfo-values* 
    (cons (cons var val) *texinfo-values*)))
}}

\def\set#1 #2
{\setQii{#1}{#2}}

\def\value{%
\evalh{
(let* ((var (get-peeled-group))
       (c (assoc var *texinfo-values*)))
  (when c
  (tex2page-string (cdr c))))
}}

\let\NONTEXINFOend\end

%\def\end#1 {\csname end#1\endcsname}

\let\group\begingroup

%\let\NONTEXINFOnode\node

%\def\node#1
%{\NONTEXINFOnode{#1}}

\def\ignorespacestillnewlineinclusive{\evalh{
(let loop ()
  (let ((c (snoop-actual-char)))
    (unless (eof-object? c)
      (when (char-whitespace? c)
        (get-actual-char)
        (unless (char=? c #\newline) (loop))))))
}}

\def\node{\evalh{
(let loop ((r '()))
  (let ((c (snoop-actual-char)))
    (cond ((or (char=? c #\,)
               (char=? c #\return)
               (char=? c #\newline))
           (if (char=? c #\,) (eat-till-eol))
           (set! *recent-node-name*
             ;htmlize-label?
             (list->string (reverse r))))
          (else (get-actual-char) (loop (cons c r))))))
}}

\def\anchor#1{\tag{#1}{#1}}

\def\menu{\iffalse}
\def\endmenu{\fi}

\def\macro{\iffalse}
\def\endmacro{\fi}

\def\direntry{\iffalse}
\def\enddirentry{\fi}

\def\ignore{\iffalse}
\def\endignore{\fi}

\def\ifinfo{\iffalse}
\def\endifinfo{\fi}

\def\ifnottex{\iffalse}
\def\endifnottex{\fi}

\let\ifhtml\htmlonly
\let\endifhtml\endhtmlonly

\let\html\rawhtml
\let\endhtml\endrawhtml

%

\def\uref#1{\urefQii #1,,,\finish}

\def\urefQii#1,#2,#3,#4\finish{%
\def\urefQiii{#2}%
\ifx\urefQiii\empty\url{#1}%
\else\urlh{#1}{\urefQiii}\fi}

%

\let\include\input

%\verbescapechar\\
\let\code\texttt
\let\samp\texttt

\def\xref{See \S\ref}

\def\pxref{see \S\ref}

\let\\\TIIPbackslash

\let\dfn\textit
\let\file\texttt
\let\cite\textit
\let\b\textbf
\let\var\textit
\let\t\texttt
\let\sc\textsc
\let\email\texttt
\let\r\textrm

% index

\def\cindex#1
{\index{#1}}

\def\pindex#1
{\index{#1@{\tt#1}}}

\let\findex\pindex

\let\kindex\pindex

\let\vindex\pindex

\let\opindex\index

\let\cmindex\index

\let\footnote\numfootnote

\def\center#1
{\centerline{#1}\TIIPnewline}

\let\NONTEXINFOitem\item

\let\TABLEitemstyle\relax

\def\TABLEitem#1
{\NONTEXINFOitem \TABLEitemstyle{#1}\break}

\def\table#1
{\begingroup\description
\def\TABLEitemstyle{#1}
\let\item\TABLEitem
\let\itemx\TABLEitem
}

\def\endtable{\enddescription\endgroup}

\let\ftable\table
\let\endftable\endtable

\let\multitable\table
\let\endmultitable\endtable

\def\sp#1
{\par}

\let\NONTEXINFOitemize\itemize

\def\itemize#1
{\NONTEXINFOitemize}

\def\example{\par\TIIPendgraf\bgroup\tt\obeywhitespace\ignorespacestillnewlineinclusive}

\def\endexample{\egroup\par}

\let\lisp\example
\let\endlisp\endexample

\let\NONTEXINFOenumerate\enumerate
\let\NONTEXINFOendenumerate\endenumerate
\def\enumerate#1
{\bgroup\NONTEXINFOenumerate
\let\item\NONTEXINFOitem}

\def\endenumerate{\NONTEXINFOendenumerate\egroup}

\def\display#1
{\par\bgroup\obeywhitespace}
\def\enddisplay{\egroup\par}

%

\let\NONTEXINFOauthor\author
\let\NONTEXINFOchapter\chapter
\let\NONTEXINFOsection\section
\let\NONTEXINFOsubsection\subsection
\let\NONTEXINFOsubsubsection\subsubsection
\let\NONTEXINFOappendix\appendix


\let\page\eject

\let\NONTEXINFOtitle\title

\def\title#1
{\NONTEXINFOtitle{#1}\hrule}

\def\subtitle#1
{\rightline{#1}}

\newcount\authorcalled

\def\author#1
{\ifnum\authorcalled=0
\bigskip\bigskip\bigskip
\global\authorcalled=1
\plainfootnote{\ }{}
\fi
\leftline{\bf#1}}

\def\appendix{\NONTEXINFOappendix
\let\NONTEXINFOappendix\relax
\chapter}

%\def\author#1
%{\NONTEXINFOauthor{#1}}

\let\top\title

\def\chapter#1
{\NONTEXINFOchapter{#1}\label{#1}}

\def\unnumbered#1
{\NONTEXINFOchapter*{#1}}

\def\section#1
{\NONTEXINFOsection{#1}\label{#1}}

\def\unnumberedsec#1
{\NONTEXINFOsection*{#1}}

\def\subsection#1
{\NONTEXINFOsubsection{#1}\label{#1}}

\def\subsubsection#1
{\NONTEXINFOsubsubsection{#1}\label{#1}}

\def\appendixsec#1
{\NONTEXINFOappendix\NONTEXINFOsection{#1}\label{#1}}

\def\appendixsubsec#1
{\NONTEXINFOappendix\NONTEXINFOsubsection{#1}\label{#1}}

\def\appendixsubsubsec#1
{\NONTEXINFOappendix\NONTEXINFOsubsubsection{#1}\label{#1}}

\def\chapheading#1
{\NONTEXINFOchapter*{#1}}
\def\heading#1
{\NONTEXINFOsection*{#1}}
\def\subheading#1
{\NONTEXINFOsubsection*{#1}}
\def\subsubheading#1
{\NONTEXINFOsubsubsection*{#1}}

%only one index

\def\printindex#1#2{
\def\printindex##1##2{}
\NONTEXINFOchapter*{Index}
{\let\end\NONTEXINFOend
\inputindex}}

\def\printindex#1#2{%
\def\printindex##1##2{}%
{\let\end\NONTEXINFOend
\inputindex}}

% is foll right?

\def\iftex{\bgroup
\def\tex{\fi\iftrue}
\def\endtex{\fi\iffalse}
\iffalse}
\def\endiftex{\fi\egroup}

% defun-type stuff
\def\deffn#1#2
{\rawhtml
<pre>--#1--#2--</pre>
\endrawhtml
}

\def\defun#1
{\deffn {Function} #1
}

\evalh{
(set! *tex-format* 'texinfo)

(define *texi-file-suffix* "-Z-T-")
(define *texi-file-count* 0)

(define texi-to-tex
  (lambda (f)
    (write-log 'separation-space) (write-log #\{)
    (write-log f) (write-log 'separation-space) (write-log "->")
    (write-log 'separation-space)
    (set! *texi-file-count* (+ *texi-file-count* 1))
    (let ((fo (string-append *aux-dir/* *jobname* *texi-file-suffix*
                             (number->string *texi-file-count*) ".tex")))
      (ensure-file-deleted fo)
      (write-log fo)
      ;this makes tex2page not read past newline
      ;when eating whitespace
      (call-with-input-file/buffered
       f
       (lambda ()
         (call-with-output-file fo
           (lambda (o)
             (fluid-let ((*not-processing?* #t)
                         (*esc-char* #\@))
               (let loop ()
                 (let ((c (snoop-actual-char)))
                   (unless (eof-object? c)
                     (cond ((char=? c #\@)
                            (let ((x (get-ctl-seq)))
                              (display x o)
                              (cond ((string=? x "\\tex") (dump-till-end-tex o))
                                    ((string=? x "\\end") (ignorespaces)))
                              (loop)))
                           ((ormap (lambda (x) (char=? c x))
                                   '(#\\ #\% #\$ #\#))
                            (get-actual-char)
                            (write-char #\\ o) (write-char c o) (loop))
                           (else (get-actual-char)
                                 (write-char c o) (loop)))))))))))
      (write-log #\})
      (write-log 'separation-space)
      fo)))

(define dump-till-end-tex
  (lambda (o)
    (let loop ()
      (let ((c (snoop-actual-char)))
        (cond ((eof-object? c) (terror 'dump-till-end-tex))
              ((char=? c #\@)
               (let ((x (get-ctl-seq)))
                 (cond ((string=? x "\\end")
                        (ignorespaces)
                        (toss-back-string "@end")
                        (set! x (get-ctl-seq))
                        (display x o)
                        (unless (string=? x "\\endtex")
                          (loop)))
                       ((string=? x "\\comment")
                        (write-char #\% o) (newline o)
                        (eat-till-eol) (loop))
                       (else (display x o)
                         (loop)))))
              (else (get-actual-char)
                (write-char c o)
                (loop)))))))

(set! tex2page-massage-file
  (lambda (f)
    (let ((e (file-extension f)))
      (cond ((and e (member/string-ci=? 
                     e '(".t2p" ".ind" ".bbl")))
             (deactivate-cdef #\newline)
             f)
            (else (tex-def-char #\newline '() "\\TIIPnewline" #f)
                  (texi-to-tex f))))))


}

\let\backslash\\

\def\\input texinfo{%
%\csname Texinfohook\endcsname
\global\let\\\backslash}

