Complete integration of texrow with otexstream and allow automatic line

counting also when outputting the latex preamble.


git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@37641 a592a061-630c-0410-9148-cb99ea01b6c8
This commit is contained in:
Enrico Forestieri 2011-02-13 21:41:44 +00:00
parent 88bd4b30f2
commit c6648aaeb9
7 changed files with 49 additions and 105 deletions

View File

@ -1269,9 +1269,10 @@ bool Buffer::makeLaTeXFile(FileName const & fname,
ErrorList & errorList = d->errorLists["Export"];
errorList.clear();
bool failed_export = false;
otexstream os(ofs, d->texrow);
try {
d->texrow.reset();
writeLaTeXSource(ofs, original_path,
os.texrow().reset();
writeLaTeXSource(os, original_path,
runparams, output_preamble, output_body);
}
catch (EncodingException & e) {
@ -1314,7 +1315,7 @@ bool Buffer::makeLaTeXFile(FileName const & fname,
}
void Buffer::writeLaTeXSource(odocstream & os,
void Buffer::writeLaTeXSource(otexstream & os,
string const & original_path,
OutputParams const & runparams_in,
bool const output_preamble, bool const output_body) const
@ -1339,8 +1340,6 @@ void Buffer::writeLaTeXSource(odocstream & os,
"For more info, see http://www.lyx.org/.\n"
"%% Do not edit unless you really know what "
"you are doing.\n";
d->texrow.newline();
d->texrow.newline();
}
LYXERR(Debug::INFO, "lyx document header finished");
@ -1367,7 +1366,6 @@ void Buffer::writeLaTeXSource(odocstream & os,
if (!runparams.nice) {
// code for usual, NOT nice-latex-file
os << "\\batchmode\n"; // changed from \nonstopmode
d->texrow.newline();
}
if (!original_path.empty()) {
// FIXME UNICODE
@ -1403,9 +1401,6 @@ void Buffer::writeLaTeXSource(odocstream & os,
<< "\\def\\input@path{{"
<< inputpath << "/}}\n"
<< "\\makeatother\n";
d->texrow.newline();
d->texrow.newline();
d->texrow.newline();
}
}
@ -1417,7 +1412,6 @@ void Buffer::writeLaTeXSource(odocstream & os,
runparams.use_polyglossia = features.usePolyglossia();
// Write the preamble
runparams.use_babel = params().writeLaTeX(os, features,
d->texrow,
d->filename.onlyPath());
runparams.use_japanese = features.isRequired("japanese");
@ -1427,19 +1421,18 @@ void Buffer::writeLaTeXSource(odocstream & os,
// make the body.
os << "\\begin{document}\n";
d->texrow.newline();
// output the parent macros
MacroSet::iterator it = parentMacros.begin();
MacroSet::iterator end = parentMacros.end();
for (; it != end; ++it) {
int num_lines = (*it)->write(os, true);
d->texrow.newlines(num_lines);
int num_lines = (*it)->write(os.os(), true);
os.texrow().newlines(num_lines);
}
} // output_preamble
d->texrow.start(paragraphs().begin()->id(), 0);
os.texrow().start(paragraphs().begin()->id(), 0);
LYXERR(Debug::INFO, "preamble finished, now the body.");
@ -1453,20 +1446,18 @@ void Buffer::writeLaTeXSource(odocstream & os,
}
// the real stuff
otexstream ots(os, d->texrow);
latexParagraphs(*this, text(), ots, runparams);
latexParagraphs(*this, text(), os, runparams);
// Restore the parenthood if needed
if (output_preamble)
d->setParent(save_parent);
// add this just in case after all the paragraphs
os << endl;
d->texrow.newline();
os.os() << endl;
os.texrow().newline();
if (output_preamble) {
os << "\\end{document}\n";
d->texrow.newline();
LYXERR(Debug::LATEX, "makeLaTeXFile...done");
} else {
LYXERR(Debug::LATEX, "LaTeXFile for inclusion made.");
@ -1474,7 +1465,7 @@ void Buffer::writeLaTeXSource(odocstream & os,
runparams_in.encoding = runparams.encoding;
// Just to be sure. (Asger)
d->texrow.newline();
os.texrow().newline();
//for (int i = 0; i<d->texrow.rows(); i++) {
// int id,pos;
@ -1483,7 +1474,7 @@ void Buffer::writeLaTeXSource(odocstream & os,
//}
LYXERR(Debug::INFO, "Finished making LaTeX file.");
LYXERR(Debug::INFO, "Row count was " << d->texrow.rows() - 1 << '.');
LYXERR(Debug::INFO, "Row count was " << os.texrow().rows() - 1 << '.');
}
@ -3106,9 +3097,11 @@ void Buffer::getSourceCode(odocstream & os, string const format,
writeDocBookSource(os, absFileName(), runparams, false);
else if (runparams.flavor == OutputParams::HTML)
writeLyXHTMLSource(os, runparams, false);
else
else {
// latex or literate
writeLaTeXSource(os, string(), runparams, true, true);
otexstream ots(os, d->texrow);
writeLaTeXSource(ots, string(), runparams, true, true);
}
} else {
runparams.par_begin = par_begin;
runparams.par_end = par_end;

View File

@ -280,19 +280,21 @@ public:
method with a string stream if the output is supposed to go to a
file. \code
ofdocstream ofs;
otexstream os(ofs, texrow);
ofs.open("test.tex");
writeLaTeXSource(ofs, ...);
writeLaTeXSource(os, ...);
ofs.close();
\endcode is NOT equivalent to \code
odocstringstream oss;
writeLaTeXSource(oss, ...);
otexstream os(oss, texrow);
writeLaTeXSource(os, ...);
ofdocstream ofs;
ofs.open("test.tex");
ofs << oss.str();
ofs.close();
\endcode
*/
void writeLaTeXSource(odocstream & os,
void writeLaTeXSource(otexstream & os,
std::string const & original_path,
OutputParams const &,
bool output_preamble = true,

View File

@ -1204,8 +1204,8 @@ void BufferParams::validate(LaTeXFeatures & features) const
}
bool BufferParams::writeLaTeX(odocstream & os, LaTeXFeatures & features,
TexRow & texrow, FileName const & filepath) const
bool BufferParams::writeLaTeX(otexstream & os, LaTeXFeatures & features,
FileName const & filepath) const
{
// http://www.tug.org/texmf-dist/doc/latex/base/fixltx2e.pdf
// !! To use the Fix-cm package, load it before \documentclass, and use the command
@ -1213,10 +1213,8 @@ bool BufferParams::writeLaTeX(odocstream & os, LaTeXFeatures & features,
// Do not to load any other package before the document class, unless you
// have a thorough understanding of the LATEX internals and know exactly what you
// are doing!
if (features.mustProvide("fix-cm")) {
if (features.mustProvide("fix-cm"))
os << "\\RequirePackage{fix-cm}\n";
texrow.newline();
}
os << "\\documentclass";
@ -1353,22 +1351,15 @@ bool BufferParams::writeLaTeX(odocstream & os, LaTeXFeatures & features,
}
os << '{' << from_ascii(tclass.latexname()) << "}\n";
texrow.newline();
// end of \documentclass defs
int nlines;
// if we use fontspec, we have to load the AMS packages here
string const ams = features.loadAMSPackages();
if (useNonTeXFonts && !ams.empty()) {
if (useNonTeXFonts && !ams.empty())
os << from_ascii(ams);
nlines = int(count(ams.begin(), ams.end(), '\n'));
texrow.newlines(nlines);
}
if (useNonTeXFonts) {
if (useNonTeXFonts)
os << "\\usepackage{fontspec}\n";
texrow.newline();
}
// font selection must be done before loading fontenc.sty
string const fonts =
@ -1376,12 +1367,9 @@ bool BufferParams::writeLaTeX(odocstream & os, LaTeXFeatures & features,
fonts_expert_sc, fonts_old_figures,
fonts_sans_scale, fonts_typewriter_scale,
useNonTeXFonts, features);
if (!fonts.empty()) {
if (!fonts.empty())
os << from_ascii(fonts);
nlines =
int(count(fonts.begin(), fonts.end(), '\n'));
texrow.newlines(nlines);
}
if (fonts_default_family != "default")
os << "\\renewcommand{\\familydefault}{\\"
<< from_ascii(fonts_default_family) << "}\n";
@ -1399,16 +1387,14 @@ bool BufferParams::writeLaTeX(odocstream & os, LaTeXFeatures & features,
|| arab != string::npos) {
os << "\\usepackage[" << from_ascii(font_encoding())
<< ",LFE,LAE]{fontenc}\n";
texrow.newline();
} else {
os << "\\usepackage[" << from_ascii(font_encoding())
<< "]{fontenc}\n";
texrow.newline();
}
}
// handle inputenc etc.
writeEncodingPreamble(os, features, texrow);
writeEncodingPreamble(os, features);
// includeonly
if (!features.runparams().includeall && !included_children_.empty()) {
@ -1436,10 +1422,9 @@ bool BufferParams::writeLaTeX(odocstream & os, LaTeXFeatures & features,
os << "}\n";
}
if (!listings_params.empty() || features.isRequired("listings")) {
if (!listings_params.empty() || features.isRequired("listings"))
os << "\\usepackage{listings}\n";
texrow.newline();
}
if (!listings_params.empty()) {
os << "\\lstset{";
// do not test validity because listings_params is
@ -1449,13 +1434,8 @@ bool BufferParams::writeLaTeX(odocstream & os, LaTeXFeatures & features,
// we can't support all packages, but we should load the color package
if (par.find("\\color", 0) != string::npos)
features.require("color");
os << from_utf8(par);
// count the number of newlines
for (size_t i = 0; i < par.size(); ++i)
if (par[i] == '\n')
texrow.newline();
os << "}\n";
texrow.newline();
os << from_utf8(par)
<< "}\n";
}
if (!tclass.provides("geometry")
&& (use_geometry || nonstandard_papersize)) {
@ -1625,7 +1605,6 @@ bool BufferParams::writeLaTeX(odocstream & os, LaTeXFeatures & features,
if (!g_options.empty())
os << '[' << g_options << ']';
os << "{geometry}\n";
texrow.newline();
// output this only if use_geometry is true
if (use_geometry) {
os << "\\geometry{verbose";
@ -1646,21 +1625,16 @@ bool BufferParams::writeLaTeX(odocstream & os, LaTeXFeatures & features,
if (!columnsep.empty())
os << ",columnsep=" << from_ascii(Length(columnsep).asLatexString());
os << "}\n";
texrow.newline();
}
} else if (orientation == ORIENTATION_LANDSCAPE
|| papersize != PAPER_DEFAULT) {
features.require("papersize");
}
if (tokenPos(tclass.opt_pagestyle(),
'|', pagestyle) >= 0) {
if (pagestyle == "fancy") {
if (tokenPos(tclass.opt_pagestyle(), '|', pagestyle) >= 0) {
if (pagestyle == "fancy")
os << "\\usepackage{fancyhdr}\n";
texrow.newline();
}
os << "\\pagestyle{" << from_ascii(pagestyle) << "}\n";
texrow.newline();
}
// only output when the background color is not default
@ -1687,13 +1661,11 @@ bool BufferParams::writeLaTeX(odocstream & os, LaTeXFeatures & features,
os << "\\setcounter{secnumdepth}{"
<< secnumdepth
<< "}\n";
texrow.newline();
}
if (tocdepth != tclass.tocdepth()) {
os << "\\setcounter{tocdepth}{"
<< tocdepth
<< "}\n";
texrow.newline();
}
}
@ -1718,17 +1690,14 @@ bool BufferParams::writeLaTeX(odocstream & os, LaTeXFeatures & features,
os << "\\setlength{\\parskip}{\\medskipamount}\n";
break;
}
texrow.newline();
os << "\\setlength{\\parindent}{0pt}\n";
texrow.newline();
} else {
// when separation by indentation
// only output something when a width is given
if (getIndentation().asLyXCommand() != "default") {
os << "\\setlength{\\parindent}{"
<< from_utf8(getIndentation().asLatexCommand())
<< from_utf8(getIndentation().asLatexCommand())
<< "}\n";
texrow.newline();
}
}
@ -1798,13 +1767,9 @@ bool BufferParams::writeLaTeX(odocstream & os, LaTeXFeatures & features,
// to access the stream itself in PDFOptions.
os << lyxpreamble;
int lines =
int(count(lyxpreamble.begin(), lyxpreamble.end(), '\n'));
OutputParams tmp_params = features.runparams();
lines += pdfoptions().writeLaTeX(tmp_params, os,
pdfoptions().writeLaTeX(tmp_params, os,
documentClass().provides("hyperref"));
texrow.newlines(lines);
// set back for the rest
lyxpreamble.clear();
// correctly break URLs with hyperref and dvi output
@ -1945,9 +1910,6 @@ bool BufferParams::writeLaTeX(odocstream & os, LaTeXFeatures & features,
if (!i18npreamble.empty())
lyxpreamble += i18npreamble + '\n';
nlines = int(count(lyxpreamble.begin(), lyxpreamble.end(), '\n'));
texrow.newlines(nlines);
os << lyxpreamble;
return use_babel;
@ -2485,8 +2447,8 @@ docstring BufferParams::getGraphicsDriver(string const & package) const
}
void BufferParams::writeEncodingPreamble(odocstream & os,
LaTeXFeatures & features, TexRow & texrow) const
void BufferParams::writeEncodingPreamble(otexstream & os,
LaTeXFeatures & features) const
{
// XeTeX does not need this
if (features.runparams().flavor == OutputParams::XETEX)
@ -2498,7 +2460,6 @@ void BufferParams::writeEncodingPreamble(odocstream & os,
&& ((inputenc == "auto" && language->encoding()->package() == Encoding::inputenc)
|| (inputenc != "auto" && encoding().package() == Encoding::inputenc))) {
os << "\\usepackage[utf8]{luainputenc}\n";
texrow.newline();
}
return;
}
@ -2536,7 +2497,6 @@ void BufferParams::writeEncodingPreamble(odocstream & os,
os << from_ascii(doc_encoding);
}
os << "]{inputenc}\n";
texrow.newline();
}
if (package == Encoding::CJK || features.mustProvide("CJK")) {
if (language->encoding()->name() == "utf8-cjk"
@ -2544,7 +2504,6 @@ void BufferParams::writeEncodingPreamble(odocstream & os,
os << "\\usepackage{CJKutf8}\n";
else
os << "\\usepackage{CJK}\n";
texrow.newline();
}
} else if (inputenc != "default") {
switch (encoding().package()) {
@ -2557,7 +2516,6 @@ void BufferParams::writeEncodingPreamble(odocstream & os,
break;
os << "\\usepackage[" << from_ascii(inputenc)
<< "]{inputenc}\n";
texrow.newline();
break;
case Encoding::CJK:
if (encoding().name() == "utf8-cjk"
@ -2565,7 +2523,6 @@ void BufferParams::writeEncodingPreamble(odocstream & os,
os << "\\usepackage{CJKutf8}\n";
else
os << "\\usepackage{CJK}\n";
texrow.newline();
break;
}
}
@ -2573,10 +2530,8 @@ void BufferParams::writeEncodingPreamble(odocstream & os,
// The encoding "armscii8" (for Armenian) is only available when
// the package "armtex" is loaded.
if (language->encoding()->latexName() == "armscii8"
|| inputenc == "armscii8") {
|| inputenc == "armscii8")
os << "\\usepackage{armtex}\n";
texrow.newline();
}
}

View File

@ -82,7 +82,7 @@ public:
* the BufferParams, a LyXRC variable, and the document class).
* This returned value can then be passed to the insets...
*/
bool writeLaTeX(odocstream &, LaTeXFeatures &, TexRow &,
bool writeLaTeX(otexstream &, LaTeXFeatures &,
support::FileName const &) const;
///
@ -367,8 +367,7 @@ public:
/// return supported drivers for specific packages
docstring getGraphicsDriver(std::string const & package) const;
/// handle inputenc etc.
void writeEncodingPreamble(odocstream & os, LaTeXFeatures & features,
TexRow & texrow) const;
void writeEncodingPreamble(otexstream & os, LaTeXFeatures & features) const;
///
std::string const parseFontName(std::string const & name) const;
/// set up the document fonts

View File

@ -89,10 +89,9 @@ void PDFOptions::writeFile(ostream & os) const
}
int PDFOptions::writeLaTeX(OutputParams & runparams, odocstream & os,
void PDFOptions::writeLaTeX(OutputParams & runparams, otexstream & os,
bool hyperref_already_provided) const
{
int lines = 0;
// FIXME Unicode
string opt;
string hyperset;
@ -174,14 +173,11 @@ int PDFOptions::writeLaTeX(OutputParams & runparams, odocstream & os,
opt = "\\hypersetup{" + opt + hyperset + "}\n";
}
lines = int(count(opt.begin(), opt.end(), '\n'));
// hyperref expects utf8!
if (need_unicode && enc && enc->iconvName() != "UTF-8"
&&!runparams.isFullUnicode()) {
os << "\\inputencoding{utf8}\n"
<< setEncoding("UTF-8");
++lines;
}
// FIXME: handle the case that hyperref is loaded by the document class and
// hyperset is empty, see bug #7048
@ -192,9 +188,7 @@ int PDFOptions::writeLaTeX(OutputParams & runparams, odocstream & os,
&&!runparams.isFullUnicode()) {
os << setEncoding(enc->iconvName())
<< "\\inputencoding{" << from_ascii(enc->latexName()) << "}\n";
++lines;
}
return lines;
}

View File

@ -36,7 +36,7 @@ public:
/// output to lyx header
void writeFile(std::ostream &) const;
/// output to tex header
int writeLaTeX(OutputParams &, odocstream &,
void writeLaTeX(OutputParams &, otexstream &,
bool hyperref_already_provided) const;
/// read tokens from lyx header
std::string readToken(Lexer &lex, std::string const & token);

View File

@ -216,7 +216,7 @@ private:
/// Called by the ForkedCall process that generated the bitmap files.
void finishedGenerating(pid_t, int);
///
void dumpPreamble(odocstream &) const;
void dumpPreamble(otexstream &) const;
///
void dumpData(odocstream &, BitmapFile const &) const;
@ -558,6 +558,7 @@ void PreviewLoader::Impl::startLoading(bool wait)
}
TexRow texrow;
otexstream os(of, texrow);
OutputParams runparams(&enc);
LaTeXFeatures features(buffer_, buffer_.params(), runparams);
@ -570,9 +571,9 @@ void PreviewLoader::Impl::startLoading(bool wait)
return;
}
of << "\\batchmode\n";
dumpPreamble(of);
dumpPreamble(os);
// handle inputenc etc.
buffer_.params().writeEncodingPreamble(of, features, texrow);
buffer_.params().writeEncodingPreamble(os, features);
of << "\n\\begin{document}\n";
dumpData(of, inprogress.snippets);
of << "\n\\end{document}\n";
@ -698,7 +699,7 @@ void PreviewLoader::Impl::finishedGenerating(pid_t pid, int retval)
}
void PreviewLoader::Impl::dumpPreamble(odocstream & os) const
void PreviewLoader::Impl::dumpPreamble(otexstream & os) const
{
// Dump the preamble only.
OutputParams runparams(&buffer_.params().encoding());