*
* \author Lars Gullik Bjønnes
*
- * Full author contact details are available in file CREDITS
+ * Full author contact details are available in file CREDITS.
*/
#include <config.h>
#include "errorlist.h"
#include "Lsstream.h"
#include "format.h"
-#include "BufferView.h"
#include "ParagraphParameters.h"
#include "iterators.h"
#include "lyxtextclasslist.h"
#include "graphics/Previews.h"
+#include "support/LAssert.h"
#include "support/textutils.h"
#include "support/filetools.h"
#include "support/path.h"
#include "support/FileInfo.h"
#include "support/lyxmanip.h"
#include "support/lyxtime.h"
+#include "support/gzstream.h"
#include <boost/bind.hpp>
#include <boost/tuple/tuple.hpp>
using lyx::pos_type;
using lyx::textclass_type;
-namespace grfx = lyx::graphics;
-
// all these externs should eventually be removed.
extern BufferList bufferlist;
namespace {
-const int LYX_FORMAT = 224;
+const int LYX_FORMAT = 225;
+
+bool openFileWrite(ofstream & ofs, string const & fname)
+{
+ ofs.open(fname.c_str());
+ if (!ofs) {
+ string const file = MakeDisplayPath(fname, 50);
+ string text = bformat(_("Could not open the specified "
+ "document\n%1$s."), file);
+ Alert::error(_("Could not open file"), text);
+ return false;
+ }
+ return true;
+}
+
} // namespace anon
Buffer::Buffer(string const & file, bool ronly)
: niceFile(true), lyx_clean(true), bak_clean(true),
unnamed(false), read_only(ronly),
- filename_(file), users(0)
+ filename_(file)
{
lyxerr[Debug::INFO] << "Buffer::Buffer()" << endl;
filepath_ = OnlyPath(file);
// here the buffer should take care that it is
// saved properly, before it goes into the void.
- // make sure that views using this buffer
- // forgets it.
- if (users)
- users->buffer(0);
+ closing();
if (!tmppath.empty() && destroyDir(tmppath) != 0) {
Alert::warning(_("Could not remove temporary directory"),
paragraphs.clear();
- // Remove any previewed LaTeX snippets assocoated with this buffer.
- grfx::Previews::get().removeLoader(this);
+ // Remove any previewed LaTeX snippets associated with this buffer.
+ lyx::graphics::Previews::get().removeLoader(*this);
}
{
if (read_only != flag) {
read_only = flag;
- updateTitles();
- if (users)
- users->owner()->getDialogs().updateBufferDependent(false);
+ readonly(flag);
}
}
}
-/// Update window titles of all users
-// Should work on a list
-void Buffer::updateTitles() const
-{
- if (users)
- users->owner()->updateWindowTitle();
-}
-
-
-/// Reset autosave timer of all users
-// Should work on a list
-void Buffer::resetAutosaveTimers() const
-{
- if (users)
- users->owner()->resetAutosaveTimer();
-}
-
-
void Buffer::setFileName(string const & newfile)
{
filename_ = MakeAbsPath(newfile);
{
Alert::warning(_("Unknown document class"),
bformat(_("Using the default document class, because the "
- " class %1$s is unknown."), unknown));
+ "class %1$s is unknown."), unknown));
}
} // anon
"%1$s %2$s\n"),
token,
lex.getString());
- parseError(ErrorItem(_("Header error"), s,
- -1, 0, 0));
+ error(ErrorItem(_("Header error"), s,
+ -1, 0, 0));
}
}
}
// changed to be public and have one parameter
// if par = 0 normal behavior
// else insert behavior
-// Returns false if "\the_end" is not read (Asger)
+// Returns false if "\end_document" is not read (Asger)
bool Buffer::readBody(LyXLex & lex, ParagraphList::iterator pit)
{
Paragraph::depth_type depth = 0;
params.textclass = 0;
}
} else {
- // We are inserting into an existing document
- users->text->breakParagraph(paragraphs);
-
// We don't want to adopt the parameters from the
// document we insert, so read them into a temporary buffer
// and then discard it
lyxerr[Debug::PARSER] << "Handling token: `"
<< token << '\'' << endl;
- if (token == "\\the_end") {
+ if (token == "\\end_document") {
the_end_read = true;
continue;
}
static Change current_change;
int unknown = 0;
- if (token == "\\layout") {
+ if (token == "\\begin_layout") {
lex.pushToken(token);
Paragraph par;
}
-bool Buffer::readFile(LyXLex & lex, string const & filename)
+bool Buffer::readFile(string const & filename)
{
- bool ret = readFile(lex, filename, paragraphs.begin());
+ // Check if the file is compressed.
+ string const format = getExtFromContents(filename);
+ if (format == "gzip" || format == "zip" || format == "compress") {
+ params.compressed = true;
+ }
+
+ bool ret = readFile(filename, paragraphs.begin());
// After we have read a file, we must ensure that the buffer
// language is set and used in the gui.
}
-// FIXME: all the below Alerts should give the filename..
+bool Buffer::readFile(string const & filename, ParagraphList::iterator pit)
+{
+ LyXLex lex(0, 0);
+ lex.setFile(filename);
+
+ return readFile(lex, filename, pit);
+}
+
+
bool Buffer::readFile(LyXLex & lex, string const & filename,
ParagraphList::iterator pit)
{
+ Assert(!filename.empty());
+
if (!lex.isOK()) {
Alert::error(_("Document could not be read"),
- _("The specified document could not be read."));
+ bformat(_("%1$s could not be read."), filename));
return false;
}
if (!lex.isOK()) {
Alert::error(_("Document could not be read"),
- _("The specified document could not be read."));
+ bformat(_("%1$s could not be read."), filename));
return false;
}
// the first token _must_ be...
if (token != "\\lyxformat") {
+ lyxerr << "Token: " << token << endl;
+
Alert::error(_("Document format failure"),
- _("The specified document is not a LyX document."));
+ bformat(_("%1$s is not a LyX document."),
+ filename));
return false;
}
//lyxerr << " dot found at " << dot << endl;
if (dot != string::npos)
tmp_format.erase(dot, 1);
- file_format = strToInt(tmp_format);
+ int file_format = strToInt(tmp_format);
//lyxerr << "format: " << file_format << endl;
- if (file_format == LYX_FORMAT) {
- // current format
- } else if (file_format > LYX_FORMAT) {
+
+ if (file_format > LYX_FORMAT) {
Alert::warning(_("Document format failure"),
- _("This document was created with a newer version of "
- "LyX. This is likely to cause problems."));
+ bformat(_("%1$s was created with a newer"
+ " version of LyX. This is likely to"
+ " cause problems."),
+ filename));
} else if (file_format < LYX_FORMAT) {
- // old formats
- if (file_format < 200) {
- Alert::error(_("Document format failure"),
- _("This LyX document is too old to be read "
- "by this version of LyX. Try LyX 0.10."));
+ string const tmpfile = tempName();
+ string command = LibFileSearch("lyx2lyx", "lyx2lyx");
+ if (command.empty()) {
+ Alert::error(_("Conversion script not found"),
+ bformat(_("%1$s is from an earlier"
+ " version of LyX, but the"
+ " conversion script lyx2lyx"
+ " could not be found."),
+ filename));
return false;
- } else if (!filename.empty()) {
- string command =
- LibFileSearch("lyx2lyx", "lyx2lyx");
- if (command.empty()) {
- Alert::error(_("Conversion script not found"),
- _("The document is from an earlier version "
- "of LyX, but the conversion script lyx2lyx "
- "could not be found."));
- return false;
- }
- command += " -t"
- +tostr(LYX_FORMAT) + ' '
- + QuoteName(filename);
- lyxerr[Debug::INFO] << "Running '"
- << command << '\''
- << endl;
- cmd_ret const ret = RunCommand(command);
- if (ret.first) {
- Alert::error(_("Conversion script failed"),
- _("The document is from an earlier version "
- "of LyX, but the lyx2lyx script failed "
- "to convert it."));
- return false;
- }
- istringstream is(STRCONV(ret.second));
- LyXLex tmplex(0, 0);
- tmplex.setStream(is);
- return readFile(tmplex, string(), pit);
- } else {
- // This code is reached if lyx2lyx failed (for
- // some reason) to change the file format of
- // the file.
- Assert(false);
+ }
+ command += " -t"
+ + tostr(LYX_FORMAT)
+ + " -o " + tmpfile + ' '
+ + QuoteName(filename);
+ lyxerr[Debug::INFO] << "Running '"
+ << command << '\''
+ << endl;
+ cmd_ret const ret = RunCommand(command);
+ if (ret.first != 0) {
+ Alert::error(_("Conversion script failed"),
+ bformat(_("%1$s is from an earlier version"
+ " of LyX, but the lyx2lyx script"
+ " failed to convert it."),
+ filename));
return false;
+ } else {
+ bool ret = readFile(tmpfile, pit);
+ // Do stuff with tmpfile name and buffer name here.
+ return ret;
}
+
}
+
bool the_end = readBody(lex, pit);
params.setPaperStuff();
if (!the_end) {
Alert::error(_("Document format failure"),
- _("The document ended unexpectedly, which means "
- "that it is probably corrupted."));
+ bformat(_("%1$s ended unexpectedly, which means"
+ " that it is probably corrupted."),
+ filename));
}
return true;
}
return false;
}
- ofstream ofs(fname.c_str());
- if (!ofs) {
- return false;
+ bool retval;
+
+ if (params.compressed) {
+ gz::ogzstream ofs(fname.c_str());
+
+ if (!ofs)
+ return false;
+
+ retval = do_writeFile(ofs);
+
+ } else {
+ ofstream ofs(fname.c_str());
+ if (!ofs)
+ return false;
+
+ retval = do_writeFile(ofs);
}
+ return retval;
+}
+
+
+bool Buffer::do_writeFile(ostream & ofs) const
+{
+
#ifdef HAVE_LOCALE
// Use the standard "C" locale for file output.
ofs.imbue(std::locale::classic());
// The top of the file should not be written by params.
// write out a comment in the top of the file
- ofs << '#' << lyx_docversion
+ ofs << "#LyX " << lyx_version
<< " created this file. For more info see http://www.lyx.org/\n"
<< "\\lyxformat " << LYX_FORMAT << "\n";
ParagraphList::const_iterator pit = paragraphs.begin();
ParagraphList::const_iterator pend = paragraphs.end();
for (; pit != pend; ++pit)
- pit->write(this, ofs, params, depth);
+ pit->write(*this, ofs, params, depth);
// Write marker that shows file is complete
- ofs << "\n\\the_end" << endl;
+ ofs << "\n\\end_document" << endl;
- ofs.close();
+ // Shouldn't really be needed....
+ //ofs.close();
// how to check if close went ok?
// Following is an attempt... (BE 20001011)
switch (c) {
case Paragraph::META_INSET:
{
- Inset const * inset = par.getInset(i);
+ InsetOld const * inset = par.getInset(i);
if (inset) {
if (linelen > 0) {
buffer << word;
currlinelen += word.length();
word.erase();
}
- if (inset->ascii(this, buffer, linelen)) {
+ if (inset->ascii(*this, buffer, linelen)) {
// to be sure it breaks paragraph
currlinelen += linelen;
}
void Buffer::writeFileAscii(string const & fname, int linelen)
{
- ofstream ofs(fname.c_str());
- if (!ofs) {
- string const file = MakeDisplayPath(fname, 50);
- string text = bformat(_("Could not save the document\n%1$s."), file);
- Alert::error(_("Could not save document"), text);
+ ofstream ofs;
+ if (!::openFileWrite(ofs, fname))
return;
- }
writeFileAscii(ofs, linelen);
}
}
-
void Buffer::makeLaTeXFile(string const & fname,
string const & original_path,
LatexRunParams const & runparams,
- bool only_body, bool only_preamble)
+ bool output_preamble, bool output_body)
{
lyxerr[Debug::LATEX] << "makeLaTeXFile..." << endl;
- ofstream ofs(fname.c_str());
- if (!ofs) {
- string const file = MakeDisplayPath(fname, 50);
- string text = bformat(_("Could not open the specified document\n%1$s."),
- file);
- Alert::error(_("Could not open file"), text);
+ ofstream ofs;
+ if (!::openFileWrite(ofs, fname))
return;
- }
makeLaTeXFile(ofs, original_path,
- runparams, only_body, only_preamble);
+ runparams, output_preamble, output_body);
ofs.close();
if (ofs.fail()) {
void Buffer::makeLaTeXFile(ostream & os,
string const & original_path,
LatexRunParams const & runparams_in,
- bool only_body, bool only_preamble)
+ bool output_preamble, bool output_body)
{
LatexRunParams runparams = runparams_in;
niceFile = runparams.nice; // this will be used by Insetincludes.
// first paragraph of the document. (Asger)
texrow.start(paragraphs.begin()->id(), 0);
- if (!only_body && runparams.nice) {
- os << "%% " << lyx_docversion << " created this file. "
+ if (output_preamble && runparams.nice) {
+ os << "%% LyX " << lyx_version << " created this file. "
"For more info, see http://www.lyx.org/.\n"
"%% Do not edit unless you really know what "
"you are doing.\n";
// input@path is set when the actual parameter
// original_path is set. This is done for usual tex-file, but not
// for nice-latex-file. (Matthias 250696)
- if (!only_body) {
+ if (output_preamble) {
if (!runparams.nice) {
// code for usual, NOT nice-latex-file
os << "\\batchmode\n"; // changed
// Write the preamble
runparams.use_babel = params.writeLaTeX(os, features, texrow);
- if (only_preamble)
+ if (!output_body)
return;
// make the body.
os << "\\begin{document}\n";
texrow.newline();
- } // only_body
+ } // output_preamble
lyxerr[Debug::INFO] << "preamble finished, now the body." << endl;
if (!lyxrc.language_auto_begin) {
texrow.newline();
}
- latexParagraphs(this, paragraphs, os, texrow, runparams);
+ latexParagraphs(*this, paragraphs, os, texrow, runparams);
// add this just in case after all the paragraphs
os << endl;
texrow.newline();
}
- if (!only_body) {
+ if (output_preamble) {
os << "\\end{document}\n";
texrow.newline();
void Buffer::makeLinuxDocFile(string const & fname, bool nice, bool body_only)
{
- ofstream ofs(fname.c_str());
-
- if (!ofs) {
- string const file = MakeDisplayPath(fname, 50);
- string text = bformat(_("Could not save the specified document\n%1$s.\n"),
- file);
- Alert::error(_("Could not save document"), text);
+ ofstream ofs;
+ if (!::openFileWrite(ofs, fname))
return;
- }
niceFile = nice; // this will be used by included files.
}
}
- ofs << "<!-- " << lyx_docversion
+ ofs << "<!-- LyX " << lyx_version
<< " created this file. For more info see http://www.lyx.org/"
<< " -->\n";
LyXLayout_ptr const & style = pit->layout();
// treat <toc> as a special case for compatibility with old code
if (pit->isInset(0)) {
- Inset * inset = pit->getInset(0);
- Inset::Code lyx_code = inset->lyxCode();
- if (lyx_code == Inset::TOC_CODE) {
+ InsetOld * inset = pit->getInset(0);
+ InsetOld::Code lyx_code = inset->lyxCode();
+ if (lyx_code == InsetOld::TOC_CODE) {
string const temp = "toc";
sgml::openTag(ofs, depth, false, temp);
continue;
case LATEX_COMMAND:
if (depth != 0)
- parseError(ErrorItem(_("Error:"), _("Wrong depth for LatexType Command.\n"), pit->id(), 0, pit->size()));
+ error(ErrorItem(_("Error:"), _("Wrong depth for LatexType Command.\n"), pit->id(), 0, pit->size()));
if (!environment_stack[depth].empty()) {
sgml::closeTag(ofs, depth, false, environment_stack[depth]);
char c = par->getChar(i);
if (c == Paragraph::META_INSET) {
- Inset * inset = par->getInset(i);
- inset->linuxdoc(this, os);
+ InsetOld * inset = par->getInset(i);
+ inset->linuxdoc(*this, os);
font_old = font;
continue;
}
void Buffer::makeDocBookFile(string const & fname, bool nice, bool only_body)
{
- ofstream ofs(fname.c_str());
- if (!ofs) {
- string const file = MakeDisplayPath(fname, 50);
- string text = bformat(_("Could not save the specified document\n%1$s.\n"),
- file);
- Alert::error(_("Could not save document"), text);
+ ofstream ofs;
+ if (!::openFileWrite(ofs, fname))
return;
- }
niceFile = nice; // this will be used by Insetincludes.
}
sgml::openTag(ofs, 0, false, top);
- ofs << "<!-- DocBook file was created by " << lyx_docversion
+ ofs << "<!-- DocBook file was created by LyX " << lyx_version
<< "\n See http://www.lyx.org/ for more information -->\n";
vector<string> environment_stack(10);
case LATEX_COMMAND:
if (depth != 0)
- parseError(ErrorItem(_("Error"), _("Wrong depth for LatexType Command."), par->id(), 0, par->size()));
+ error(ErrorItem(_("Error"), _("Wrong depth for LatexType Command."), par->id(), 0, par->size()));
command_name = style->latexname();
// This is a hack while paragraphs can't have
// attributes, like id in this case.
if (par->isInset(0)) {
- Inset * inset = par->getInset(0);
- Inset::Code lyx_code = inset->lyxCode();
- if (lyx_code == Inset::LABEL_CODE) {
+ InsetOld * inset = par->getInset(0);
+ InsetOld::Code lyx_code = inset->lyxCode();
+ if (lyx_code == InsetOld::LABEL_CODE) {
command_name += " id=\"";
command_name += (static_cast<InsetCommand *>(inset))->getContents();
command_name += '"';
if (par->isInset(i)) {
- Inset * inset = par->getInset(i);
+ InsetOld * inset = par->getInset(i);
// don't print the inset in position 0 if desc_on == 3 (label)
if (i || desc_on != 3) {
if (style->latexparam() == "CDATA")
os << "]]>";
- inset->docbook(this, os, false);
+ inset->docbook(*this, os, false);
if (style->latexparam() == "CDATA")
os << "<![CDATA[";
}
// Other flags: -wall -v0 -x
int Buffer::runChktex()
{
- if (!users->text) return 0;
-
- users->owner()->busy(true);
+ busy(true);
// get LaTeX-Filename
string const name = getLatexName();
}
Path p(path); // path to LaTeX file
- users->owner()->message(_("Running chktex..."));
+ message(_("Running chktex..."));
// Generate the LaTeX file if neccessary
LatexRunParams runparams;
_("Could not run chktex successfully."));
} else if (res > 0) {
// Insert all errors as errors boxes
- parseErrors(*this, terr);
+ bufferErrors(*this, terr);
}
- users->owner()->busy(false);
+ busy(false);
return res;
}
for (inset_iterator it = inset_const_iterator_begin();
it != inset_const_iterator_end(); ++it) {
- if (it->lyxCode() == Inset::BIBTEX_CODE)
- static_cast<InsetBibtex &>(*it).fillWithBibKeys(this, keys);
- else if (it->lyxCode() == Inset::INCLUDE_CODE)
+ if (it->lyxCode() == InsetOld::BIBTEX_CODE)
+ static_cast<InsetBibtex &>(*it).fillWithBibKeys(*this, keys);
+ else if (it->lyxCode() == InsetOld::INCLUDE_CODE)
static_cast<InsetInclude &>(*it).fillWithBibKeys(keys);
- else if (it->lyxCode() == Inset::BIBITEM_CODE) {
+ else if (it->lyxCode() == InsetOld::BIBITEM_CODE) {
InsetBibitem & bib = static_cast<InsetBibitem &>(*it);
string const key = bib.getContents();
string const opt = bib.getOptions();
}
-void Buffer::resizeInsets(BufferView * bv)
-{
- /// then remove all LyXText in text-insets
- for_each(paragraphs.begin(), paragraphs.end(),
- boost::bind(&Paragraph::resizeInsetsLyXText, _1, bv));
-}
-
-
-void Buffer::redraw()
-{
-#warning repaint needed here, or do you mean update() ?
- users->repaint();
- users->fitCursor();
-}
-
-
void Buffer::changeLanguage(Language const * from, Language const * to)
{
lyxerr << "Changing Language!" << endl;
}
-Inset * Buffer::getInsetFromID(int id_arg) const
+InsetOld * Buffer::getInsetFromID(int id_arg) const
{
for (inset_iterator it = inset_const_iterator_begin();
it != inset_const_iterator_end(); ++it)
{
if (it->id() == id_arg)
return &(*it);
- Inset * in = it->getInsetFromID(id_arg);
+ InsetOld * in = it->getInsetFromID(id_arg);
if (in)
return in;
}
return ParIterator(paragraphs.end(), paragraphs);
}
+
ParConstIterator Buffer::par_iterator_begin() const
{
return ParConstIterator(const_cast<ParagraphList&>(paragraphs).begin(), paragraphs);
}
-
-void Buffer::addUser(BufferView * u)
-{
- users = u;
-}
-
-
-void Buffer::delUser(BufferView *)
-{
- users = 0;
-}
-
-
Language const * Buffer::getLanguage() const
{
return params.language;
}
-BufferView * Buffer::getUser() const
-{
- return users;
-}
-
-
void Buffer::setParentName(string const & name)
{
params.parentname = name;