Move a bunch of code from Buffer to BufferParams. The point of this

is contained in the next commit.

git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@38742 a592a061-630c-0410-9148-cb99ea01b6c8
This commit is contained in:
Richard Heck 2011-05-13 19:39:56 +00:00
parent a7773d671b
commit 5db4b7eb16
15 changed files with 214 additions and 208 deletions

View File

@ -602,7 +602,7 @@ string Buffer::logName(LogType * type) const
FileName const bname(
addName(path, onlyFileName(
changeExtension(filename,
formats.extension(bufferFormat()) + ".out"))));
formats.extension(params().bufferFormat()) + ".out"))));
// Also consider the master buffer log file
FileName masterfname = fname;
@ -1486,24 +1486,6 @@ void Buffer::writeLaTeXSource(otexstream & os,
}
bool Buffer::isLatex() const
{
return params().documentClass().outputType() == LATEX;
}
bool Buffer::isLiterate() const
{
return params().documentClass().outputType() == LITERATE;
}
bool Buffer::isDocBook() const
{
return params().documentClass().outputType() == DOCBOOK;
}
void Buffer::makeDocBookFile(FileName const & fname,
OutputParams const & runparams,
bool const body_only) const
@ -1917,21 +1899,6 @@ void Buffer::markDepClean(string const & name)
}
bool Buffer::isExportableFormat(string const & format) const
{
typedef vector<Format const *> Formats;
Formats formats;
formats = exportableFormats(true);
Formats::const_iterator fit = formats.begin();
Formats::const_iterator end = formats.end();
for (; fit != end ; ++fit) {
if ((*fit)->name() == format)
return true;
}
return false;
}
bool Buffer::getStatus(FuncRequest const & cmd, FuncStatus & flag)
{
if (isInternal()) {
@ -1960,7 +1927,7 @@ bool Buffer::getStatus(FuncRequest const & cmd, FuncStatus & flag)
case LFUN_BUFFER_EXPORT: {
docstring const arg = cmd.argument();
enable = arg == "custom" || isExportable(to_utf8(arg));
enable = arg == "custom" || params().isExportable(to_utf8(arg));
if (!enable)
flag.message(bformat(
_("Don't know how to export to format: %1$s"), arg));
@ -1968,11 +1935,11 @@ bool Buffer::getStatus(FuncRequest const & cmd, FuncStatus & flag)
}
case LFUN_BUFFER_CHKTEX:
enable = isLatex() && !lyxrc.chktex_command.empty();
enable = params().isLatex() && !lyxrc.chktex_command.empty();
break;
case LFUN_BUILD_PROGRAM:
enable = isExportable("program");
enable = params().isExportable("program");
break;
case LFUN_BRANCH_ACTIVATE:
@ -3109,7 +3076,7 @@ void Buffer::getSourceCode(odocstream & os, string const format,
{
OutputParams runparams(&params().encoding());
runparams.nice = true;
runparams.flavor = getOutputFlavor(format);
runparams.flavor = params().getOutputFlavor(format);
runparams.linelen = lyxrc.plaintext_linelen;
// No side effect of file copying and image conversion
runparams.dryrun = true;
@ -3119,7 +3086,7 @@ void Buffer::getSourceCode(odocstream & os, string const format,
d->texrow.reset();
d->texrow.newline();
d->texrow.newline();
if (isDocBook())
if (params().isDocBook())
writeDocBookSource(os, absFileName(), runparams, false);
else if (runparams.flavor == OutputParams::HTML)
writeLyXHTMLSource(os, runparams, false);
@ -3147,7 +3114,7 @@ void Buffer::getSourceCode(odocstream & os, string const format,
texrow.newline();
texrow.newline();
// output paragraphs
if (isDocBook())
if (params().isDocBook())
docbookParagraphs(text(), *this, os, runparams);
else if (runparams.flavor == OutputParams::HTML) {
XHTMLStream xs(os);
@ -3392,74 +3359,6 @@ bool Buffer::autoSave() const
}
string Buffer::bufferFormat() const
{
string format = params().documentClass().outputFormat();
if (format == "latex") {
if (params().useNonTeXFonts)
return "xetex";
if (params().encoding().package() == Encoding::japanese)
return "platex";
}
return format;
}
string Buffer::getDefaultOutputFormat() const
{
if (!params().default_output_format.empty()
&& params().default_output_format != "default")
return params().default_output_format;
if (isDocBook()
|| params().useNonTeXFonts
|| params().encoding().package() == Encoding::japanese) {
vector<Format const *> const formats = exportableFormats(true);
if (formats.empty())
return string();
// return the first we find
return formats.front()->name();
}
return lyxrc.default_view_format;
}
OutputParams::FLAVOR Buffer::getOutputFlavor(string const format) const
{
string const dformat = (format.empty() || format == "default") ?
getDefaultOutputFormat() : format;
DefaultFlavorCache::const_iterator it =
default_flavors_.find(dformat);
if (it != default_flavors_.end())
return it->second;
OutputParams::FLAVOR result = OutputParams::LATEX;
if (dformat == "xhtml")
result = OutputParams::HTML;
else {
// Try to determine flavor of default output format
vector<string> backs = backends();
if (find(backs.begin(), backs.end(), dformat) == backs.end()) {
// Get shortest path to format
Graph::EdgePath path;
for (vector<string>::const_iterator it = backs.begin();
it != backs.end(); ++it) {
Graph::EdgePath p = theConverters().getPath(*it, dformat);
if (!p.empty() && (path.empty() || p.size() < path.size())) {
path = p;
}
}
if (!path.empty())
result = theConverters().getFlavor(path);
}
}
// cache this flavor
default_flavors_[dformat] = result;
return result;
}
namespace {
// helper class, to guarantee this gets reset properly
class MarkAsExporting {
@ -3505,7 +3404,7 @@ bool Buffer::doExport(string const & format, bool put_in_tempdir,
runparams.flavor = OutputParams::LATEX;
runparams.linelen = lyxrc.plaintext_linelen;
runparams.includeall = includeall;
vector<string> backs = backends();
vector<string> backs = params().backends();
Converters converters = theConverters();
if (find(backs.begin(), backs.end(), format) == backs.end()) {
// Get shortest path to format
@ -3573,7 +3472,7 @@ bool Buffer::doExport(string const & format, bool put_in_tempdir,
} else if (backend_format == "lyx")
writeFile(FileName(filename));
// Docbook backend
else if (isDocBook()) {
else if (params().isDocBook()) {
runparams.nice = !put_in_tempdir;
makeDocBookFile(FileName(filename), runparams);
}
@ -3604,7 +3503,7 @@ bool Buffer::doExport(string const & format, bool put_in_tempdir,
}
string const error_type = (format == "program")
? "Build" : bufferFormat();
? "Build" : params().bufferFormat();
ErrorList & error_list = d->errorLists[error_type];
string const ext = formats.extension(format);
FileName const tmp_result_file(changeExtension(filename, ext));
@ -3643,7 +3542,7 @@ bool Buffer::doExport(string const & format, bool put_in_tempdir,
// FIXME: There is a possibility of concurrent access to texrow
// here from the main GUI thread that should be securized.
d->cloned_buffer_->d->texrow = d->texrow;
string const error_type = bufferFormat();
string const error_type = params().bufferFormat();
d->cloned_buffer_->d->errorLists[error_type] = d->errorLists[error_type];
}
@ -3725,55 +3624,6 @@ bool Buffer::preview(string const & format, bool includeall) const
}
bool Buffer::isExportable(string const & format) const
{
vector<string> backs = backends();
for (vector<string>::const_iterator it = backs.begin();
it != backs.end(); ++it)
if (theConverters().isReachable(*it, format))
return true;
return false;
}
vector<Format const *> Buffer::exportableFormats(bool only_viewable) const
{
vector<string> const backs = backends();
set<string> excludes;
if (params().useNonTeXFonts) {
excludes.insert("latex");
excludes.insert("pdflatex");
}
vector<Format const *> result =
theConverters().getReachable(backs[0], only_viewable, true, excludes);
for (vector<string>::const_iterator it = backs.begin() + 1;
it != backs.end(); ++it) {
vector<Format const *> r =
theConverters().getReachable(*it, only_viewable, false, excludes);
result.insert(result.end(), r.begin(), r.end());
}
return result;
}
vector<string> Buffer::backends() const
{
vector<string> v;
v.push_back(bufferFormat());
// FIXME: Don't hardcode format names here, but use a flag
if (v.back() == "latex") {
v.push_back("pdflatex");
v.push_back("luatex");
v.push_back("xetex");
} else if (v.back() == "xetex")
v.push_back("luatex");
v.push_back("xhtml");
v.push_back("text");
v.push_back("lyx");
return v;
}
Buffer::ReadStatus Buffer::extractFromVC()
{
bool const found = LyXVC::file_not_found_hook(d->filename);

View File

@ -409,13 +409,6 @@ public:
/// Set buffer read-only flag
void setReadonly(bool flag = true);
/// returns \c true if the buffer contains a LaTeX document
bool isLatex() const;
/// returns \c true if the buffer contains a DocBook document
bool isDocBook() const;
/// returns \c true if the buffer contains a Wed document
bool isLiterate() const;
/** Validate a buffer for LaTeX.
This validates the buffer, and returns a struct for use by
#makeLaTeX# and others. Its main use is to figure out what
@ -602,14 +595,6 @@ public:
/// return the format of the buffer on a string
std::string bufferFormat() const;
/// return the default output format of the current backend
std::string getDefaultOutputFormat() const;
/// return the output flavor of \p format or the default
OutputParams::FLAVOR getOutputFlavor(
std::string const format = std::string()) const;
///
bool doExport(std::string const & format, bool put_in_tempdir,
bool includeall, std::string & result_file) const;
@ -618,12 +603,6 @@ public:
bool includeall = false) const;
///
bool preview(std::string const & format, bool includeall = false) const;
///
bool isExportable(std::string const & format) const;
///
std::vector<Format const *> exportableFormats(bool only_viewable) const;
///
bool isExportableFormat(std::string const & format) const;
/// mark the buffer as busy exporting something, or not
void setExportStatus(bool e) const;
///
@ -670,12 +649,6 @@ private:
/// Change name of buffer. Updates "read-only" flag.
void setFileName(support::FileName const & fname);
///
std::vector<std::string> backends() const;
/// A cache for the default flavors
typedef std::map<std::string, OutputParams::FLAVOR> DefaultFlavorCache;
///
mutable DefaultFlavorCache default_flavors_;
///
void getLanguages(std::set<Language const *> &) const;
/// Checks whether any of the referenced bibfiles have changed since the
/// last time we loaded the cache. Note that this does NOT update the

View File

@ -24,6 +24,7 @@
#include "Bullet.h"
#include "Color.h"
#include "ColorSet.h"
#include "Converter.h"
#include "Encoding.h"
#include "HSpace.h"
#include "IndicesList.h"
@ -2054,6 +2055,137 @@ bool BufferParams::addLayoutModule(string const & modName)
}
string BufferParams::bufferFormat() const
{
string format = documentClass().outputFormat();
if (format == "latex") {
if (useNonTeXFonts)
return "xetex";
if (encoding().package() == Encoding::japanese)
return "platex";
}
return format;
}
bool BufferParams::isExportable(string const & format) const
{
vector<string> backs = backends();
for (vector<string>::const_iterator it = backs.begin();
it != backs.end(); ++it)
if (theConverters().isReachable(*it, format))
return true;
return false;
}
vector<Format const *> BufferParams::exportableFormats(bool only_viewable) const
{
vector<string> const backs = backends();
set<string> excludes;
if (useNonTeXFonts) {
excludes.insert("latex");
excludes.insert("pdflatex");
}
vector<Format const *> result =
theConverters().getReachable(backs[0], only_viewable, true, excludes);
for (vector<string>::const_iterator it = backs.begin() + 1;
it != backs.end(); ++it) {
vector<Format const *> r =
theConverters().getReachable(*it, only_viewable, false, excludes);
result.insert(result.end(), r.begin(), r.end());
}
return result;
}
bool BufferParams::isExportableFormat(string const & format) const
{
typedef vector<Format const *> Formats;
Formats formats;
formats = exportableFormats(true);
Formats::const_iterator fit = formats.begin();
Formats::const_iterator end = formats.end();
for (; fit != end ; ++fit) {
if ((*fit)->name() == format)
return true;
}
return false;
}
vector<string> BufferParams::backends() const
{
vector<string> v;
v.push_back(bufferFormat());
// FIXME: Don't hardcode format names here, but use a flag
if (v.back() == "latex") {
v.push_back("pdflatex");
v.push_back("luatex");
v.push_back("xetex");
} else if (v.back() == "xetex")
v.push_back("luatex");
v.push_back("xhtml");
v.push_back("text");
v.push_back("lyx");
return v;
}
OutputParams::FLAVOR BufferParams::getOutputFlavor(string const format) const
{
string const dformat = (format.empty() || format == "default") ?
getDefaultOutputFormat() : format;
DefaultFlavorCache::const_iterator it =
default_flavors_.find(dformat);
if (it != default_flavors_.end())
return it->second;
OutputParams::FLAVOR result = OutputParams::LATEX;
if (dformat == "xhtml")
result = OutputParams::HTML;
else {
// Try to determine flavor of default output format
vector<string> backs = backends();
if (find(backs.begin(), backs.end(), dformat) == backs.end()) {
// Get shortest path to format
Graph::EdgePath path;
for (vector<string>::const_iterator it = backs.begin();
it != backs.end(); ++it) {
Graph::EdgePath p = theConverters().getPath(*it, dformat);
if (!p.empty() && (path.empty() || p.size() < path.size())) {
path = p;
}
}
if (!path.empty())
result = theConverters().getFlavor(path);
}
}
// cache this flavor
default_flavors_[dformat] = result;
return result;
}
string BufferParams::getDefaultOutputFormat() const
{
if (!default_output_format.empty()
&& default_output_format != "default")
return default_output_format;
if (isDocBook()
|| useNonTeXFonts
|| encoding().package() == Encoding::japanese) {
vector<Format const *> const formats = exportableFormats(true);
if (formats.empty())
return string();
// return the first we find
return formats.front()->name();
}
return lyxrc.default_view_format;
}
Font const BufferParams::getFont() const
{
FontInfo f = documentClass().defaultfont();
@ -2067,6 +2199,24 @@ Font const BufferParams::getFont() const
}
bool BufferParams::isLatex() const
{
return documentClass().outputType() == LATEX;
}
bool BufferParams::isLiterate() const
{
return documentClass().outputType() == LITERATE;
}
bool BufferParams::isDocBook() const
{
return documentClass().outputType() == DOCBOOK;
}
void BufferParams::readPreamble(Lexer & lex)
{
if (lex.getString() != "\\begin_preamble")

View File

@ -16,7 +16,9 @@
#define BUFFERPARAMS_H
#include "Citation.h"
#include "Format.h"
#include "LayoutModuleList.h"
#include "OutputParams.h"
#include "paper.h"
#include "insets/InsetQuotes.h"
@ -24,6 +26,7 @@
#include "support/copied_ptr.h"
#include <map>
#include <vector>
namespace lyx {
@ -153,6 +156,29 @@ public:
/// Clear the removed module list
void clearRemovedModules() { removed_modules_.clear(); }
/// returns \c true if the buffer contains a LaTeX document
bool isLatex() const;
/// returns \c true if the buffer contains a DocBook document
bool isDocBook() const;
/// returns \c true if the buffer contains a Wed document
bool isLiterate() const;
/// return the format of the buffer on a string
std::string bufferFormat() const;
/// return the default output format of the current backend
std::string getDefaultOutputFormat() const;
/// return the output flavor of \p format or the default
OutputParams::FLAVOR getOutputFlavor(
std::string const format = std::string()) const;
///
bool isExportable(std::string const & format) const;
///
std::vector<Format const *> exportableFormats(bool only_viewable) const;
///
bool isExportableFormat(std::string const & format) const;
///
std::vector<std::string> backends() const;
/// List of included children (for includeonly)
std::list<std::string> const & getIncludedChildren() const
{ return included_children_; }
@ -437,6 +463,10 @@ private:
void readRemovedModules(Lexer &);
///
void readIncludeonly(Lexer &);
/// A cache for the default flavors
typedef std::map<std::string, OutputParams::FLAVOR> DefaultFlavorCache;
///
mutable DefaultFlavorCache default_flavors_;
/// for use with natbib
CiteEngine cite_engine_;
///

View File

@ -338,7 +338,7 @@ bool Converters::convert(Buffer const * buffer,
runparams.flavor = getFlavor(edgepath);
if (buffer) {
runparams.use_japanese = buffer->bufferFormat() == "platex";
runparams.use_japanese = buffer->params().bufferFormat() == "platex";
runparams.use_indices = buffer->params().use_indices;
runparams.bibtex_command = (buffer->params().bibtex_command == "default") ?
string() : buffer->params().bibtex_command;

View File

@ -16,6 +16,7 @@
#include "qt_helpers.h"
#include "Buffer.h"
#include "BufferParams.h"
#include "BufferView.h"
#include "Cursor.h"
#include "FuncRequest.h"
@ -99,9 +100,9 @@ QString Dialog::bufferFilePath() const
KernelDocType Dialog::docType() const
{
if (buffer().isLatex())
if (buffer().params().isLatex())
return LATEX;
if (buffer().isLiterate())
if (buffer().params().isLiterate())
return LITERATE;
return DOCBOOK;

View File

@ -2155,7 +2155,7 @@ void GuiDocument::updateDefaultFormat()
outputModule->defaultFormatCO->addItem(qt_("Default"),
QVariant(QString("default")));
typedef vector<Format const *> Formats;
Formats formats = tmpbuf->exportableFormats(true);
Formats formats = tmpbuf->params().exportableFormats(true);
Formats::const_iterator cit = formats.begin();
Formats::const_iterator end = formats.end();
for (; cit != end; ++cit)

View File

@ -15,6 +15,7 @@
#include "qt_helpers.h"
#include "Buffer.h"
#include "BufferParams.h"
#include "Format.h"
#include "FuncRequest.h"
@ -67,7 +68,7 @@ void GuiSendTo::changed_adaptor()
void GuiSendTo::updateContents()
{
all_formats_ = buffer().exportableFormats(false);
all_formats_ = buffer().params().exportableFormats(false);
// Save the current selection if any
Format const * current_format = 0;

View File

@ -1616,8 +1616,8 @@ bool GuiView::getStatus(FuncRequest const & cmd, FuncStatus & flag)
}
string format = to_utf8(cmd.argument());
if (cmd.argument().empty())
format = doc_buffer->getDefaultOutputFormat();
enable = doc_buffer->isExportableFormat(format);
format = doc_buffer->params().getDefaultOutputFormat();
enable = doc_buffer->params().isExportableFormat(format);
break;
}
@ -1721,7 +1721,7 @@ bool GuiView::getStatus(FuncRequest const & cmd, FuncStatus & flag)
|| name == "progress"
|| name == "compare";
else if (name == "print")
enable = doc_buffer->isExportable("dvi")
enable = doc_buffer->params().isExportable("dvi")
&& lyxrc.print_command != "none";
else if (name == "character" || name == "symbols") {
if (!buf || buf->isReadonly())
@ -3017,7 +3017,7 @@ bool GuiView::GuiViewPrivate::asyncBufferProcessing(
string format = argument;
if (format.empty())
format = used_buffer->getDefaultOutputFormat();
format = used_buffer->params().getDefaultOutputFormat();
#if EXPORT_in_THREAD && (QT_VERSION >= 0x040400)
if (!msg.empty()) {
@ -3031,7 +3031,7 @@ bool GuiView::GuiViewPrivate::asyncBufferProcessing(
used_buffer->clone(),
format);
setPreviewFuture(f);
last_export_format = used_buffer->bufferFormat();
last_export_format = used_buffer->params().bufferFormat();
(void) syncFunc;
(void) previewFunc;
// We are asynchronous, so we don't know here anything about the success

View File

@ -17,8 +17,9 @@
#include "LaTeXHighlighter.h"
#include "qt_helpers.h"
#include "BufferView.h"
#include "Buffer.h"
#include "BufferParams.h"
#include "BufferView.h"
#include "Cursor.h"
#include "Format.h"
#include "Paragraph.h"
@ -183,7 +184,7 @@ void ViewSourceWidget::updateDefaultFormat()
outputFormatCO->addItem(qt_("Default"),
QVariant(QString("default")));
typedef vector<Format const *> Formats;
Formats formats = bv_->buffer().exportableFormats(true);
Formats formats = bv_->buffer().params().exportableFormats(true);
Formats::const_iterator cit = formats.begin();
Formats::const_iterator end = formats.end();
for (; cit != end; ++cit)

View File

@ -1007,15 +1007,15 @@ void MenuDefinition::expandFormats(MenuItem::Kind kind, Buffer const * buf)
action = LFUN_BUFFER_IMPORT;
break;
case MenuItem::ViewFormats:
formats = buf->exportableFormats(true);
formats = buf->params().exportableFormats(true);
action = LFUN_BUFFER_VIEW;
break;
case MenuItem::UpdateFormats:
formats = buf->exportableFormats(true);
formats = buf->params().exportableFormats(true);
action = LFUN_BUFFER_UPDATE;
break;
default:
formats = buf->exportableFormats(false);
formats = buf->params().exportableFormats(false);
action = LFUN_BUFFER_EXPORT;
}
sort(formats.begin(), formats.end(), &compareFormat);
@ -1058,7 +1058,7 @@ void MenuDefinition::expandFormats(MenuItem::Kind kind, Buffer const * buf)
break;
case MenuItem::ViewFormats:
case MenuItem::UpdateFormats:
if ((*fit)->name() == buf->getDefaultOutputFormat()) {
if ((*fit)->name() == buf->params().getDefaultOutputFormat()) {
docstring lbl = (kind == MenuItem::ViewFormats ?
bformat(_("View [%1$s]|V"), qstring_to_ucs4(label))
: bformat(_("Update [%1$s]|U"), qstring_to_ucs4(label)));

View File

@ -388,7 +388,7 @@ namespace graphics {
PreviewLoader::Impl::Impl(PreviewLoader & p, Buffer const & b)
: parent_(p), buffer_(b)
{
if (b.bufferFormat() == "lilypond-book")
if (b.params().bufferFormat() == "lilypond-book")
pconverter_ = setConverter("lyxpreview-lytex");
else if (b.params().encoding().package() == Encoding::japanese)
pconverter_ = setConverter("lyxpreview-platex");

View File

@ -600,7 +600,7 @@ void InsetInclude::latex(otexstream & os, OutputParams const & runparams) const
tmp->markDepClean(masterBuffer->temppath());
// Don't assume the child's format is latex
string const inc_format = tmp->bufferFormat();
string const inc_format = tmp->params().bufferFormat();
FileName const tmpwritefile(changeExtension(writefile.absFileName(),
formats.extension(inc_format)));

View File

@ -263,7 +263,7 @@ void InsetRef::updateBuffer(ParIterator const & it, UpdateType)
}
label += ref;
if (!buffer().isLatex() && !getParam("name").empty()) {
if (!buffer().params().isLatex() && !getParam("name").empty()) {
label += "||";
label += getParam("name");
}

View File

@ -991,7 +991,7 @@ docstring latexifyFromCursor(DocIterator const & cur, int len)
LYXERR(Debug::FIND, " with cur.lastpost=" << cur.lastpos() << ", cur.lastrow="
<< cur.lastrow() << ", cur.lastcol=" << cur.lastcol());
Buffer const & buf = *cur.buffer();
LASSERT(buf.isLatex(), /* */);
LASSERT(buf.params().isLatex(), /* */);
TexRow texrow;
odocstringstream ods;