diff --git a/src/Buffer.cpp b/src/Buffer.cpp index 07bd856fc1..77d0267afc 100644 --- a/src/Buffer.cpp +++ b/src/Buffer.cpp @@ -24,6 +24,7 @@ #include "Counters.h" #include "debug.h" #include "DocIterator.h" +#include "EmbeddedFiles.h" #include "Encoding.h" #include "ErrorList.h" #include "Exporter.h" @@ -33,34 +34,34 @@ #include "InsetIterator.h" #include "InsetList.h" #include "Language.h" -#include "LaTeX.h" #include "LaTeXFeatures.h" +#include "LaTeX.h" #include "Layout.h" -#include "LyXAction.h" #include "Lexer.h" -#include "Text.h" +#include "LyXAction.h" #include "LyX.h" #include "LyXRC.h" #include "LyXVC.h" #include "Messages.h" -#include "output.h" #include "output_docbook.h" +#include "output.h" #include "output_latex.h" #include "output_plaintext.h" -#include "Paragraph.h" #include "paragraph_funcs.h" +#include "Paragraph.h" #include "ParagraphParameters.h" #include "ParIterator.h" +#include "PDFOptions.h" #include "Session.h" #include "sgml.h" #include "TexRow.h" -#include "TextClassList.h" #include "TexStream.h" +#include "TextClassList.h" +#include "Text.h" #include "TocBackend.h" #include "Undo.h" +#include "VCBackend.h" #include "version.h" -#include "EmbeddedFiles.h" -#include "PDFOptions.h" #include "insets/InsetBibitem.h" #include "insets/InsetBibtex.h" @@ -1309,7 +1310,7 @@ void Buffer::writeDocBookSource(odocstream & os, string const & fname, // Other flags: -wall -v0 -x int Buffer::runChktex() { - busy(true); + setBusy(true); // get LaTeX-Filename FileName const path(temppath()); @@ -1333,14 +1334,12 @@ int Buffer::runChktex() Alert::error(_("chktex failure"), _("Could not run chktex successfully.")); } else if (res > 0) { - ErrorList & errorList = pimpl_->errorLists["ChkTeX"]; - // Clear out old errors - errorList.clear(); - // Fill-in the error list with the TeX errors - bufferErrors(*this, terr, errorList); + ErrorList & errlist = pimpl_->errorLists["ChkTeX"]; + errlist.clear(); + bufferErrors(terr, errlist); } - busy(false); + setBusy(false); errors("ChkTeX"); @@ -1927,7 +1926,7 @@ void Buffer::message(docstring const & msg) const } -void Buffer::busy(bool on) const +void Buffer::setBusy(bool on) const { if (gui_) gui_->busy(on); @@ -2386,4 +2385,123 @@ vector Buffer::backends() const } +bool Buffer::readFileHelper(FileName const & s) +{ + // File information about normal file + if (!s.exists()) { + docstring const file = makeDisplayPath(s.absFilename(), 50); + docstring text = bformat(_("The specified document\n%1$s" + "\ncould not be read."), file); + Alert::error(_("Could not read document"), text); + return false; + } + + // Check if emergency save file exists and is newer. + FileName const e(s.absFilename() + ".emergency"); + + if (e.exists() && s.exists() && e.lastModified() > s.lastModified()) { + docstring const file = makeDisplayPath(s.absFilename(), 20); + docstring const text = + bformat(_("An emergency save of the document " + "%1$s exists.\n\n" + "Recover emergency save?"), file); + switch (Alert::prompt(_("Load emergency save?"), text, 0, 2, + _("&Recover"), _("&Load Original"), + _("&Cancel"))) + { + case 0: + // the file is not saved if we load the emergency file. + markDirty(); + return readFile(e); + case 1: + break; + default: + return false; + } + } + + // Now check if autosave file is newer. + FileName const a(onlyPath(s.absFilename()) + '#' + onlyFilename(s.absFilename()) + '#'); + + if (a.exists() && s.exists() && a.lastModified() > s.lastModified()) { + docstring const file = makeDisplayPath(s.absFilename(), 20); + docstring const text = + bformat(_("The backup of the document " + "%1$s is newer.\n\nLoad the " + "backup instead?"), file); + switch (Alert::prompt(_("Load backup?"), text, 0, 2, + _("&Load backup"), _("Load &original"), + _("&Cancel") )) + { + case 0: + // the file is not saved if we load the autosave file. + markDirty(); + return readFile(a); + case 1: + // Here we delete the autosave + unlink(a); + break; + default: + return false; + } + } + return readFile(s); +} + + +bool Buffer::loadLyXFile(FileName const & s) +{ + if (s.isReadable()) { + if (readFileHelper(s)) { + lyxvc().file_found_hook(s); + if (!s.isWritable()) + setReadonly(true); + return true; + } + } else { + docstring const file = makeDisplayPath(s.absFilename(), 20); + // Here we probably should run + if (LyXVC::file_not_found_hook(s)) { + docstring const text = + bformat(_("Do you want to retrieve the document" + " %1$s from version control?"), file); + int const ret = Alert::prompt(_("Retrieve from version control?"), + text, 0, 1, _("&Retrieve"), _("&Cancel")); + + if (ret == 0) { + // How can we know _how_ to do the checkout? + // With the current VC support it has to be, + // a RCS file since CVS do not have special ,v files. + RCS::retrieve(s); + return loadLyXFile(s); + } + } + } + return false; +} + + +void Buffer::bufferErrors(TeXErrors const & terr, ErrorList & errorList) const +{ + TeXErrors::Errors::const_iterator cit = terr.begin(); + TeXErrors::Errors::const_iterator end = terr.end(); + + for (; cit != end; ++cit) { + int id_start = -1; + int pos_start = -1; + int errorRow = cit->error_in_line; + bool found = texrow().getIdFromRow(errorRow, id_start, + pos_start); + int id_end = -1; + int pos_end = -1; + do { + ++errorRow; + found = texrow().getIdFromRow(errorRow, id_end, pos_end); + } while (found && id_start == id_end && pos_start == pos_end); + + errorList.push_back(ErrorItem(cit->error_desc, + cit->error_text, id_start, pos_start, pos_end)); + } +} + } // namespace lyx diff --git a/src/Buffer.h b/src/Buffer.h index df0487acdb..8644f0e08b 100644 --- a/src/Buffer.h +++ b/src/Buffer.h @@ -155,6 +155,12 @@ public: /// Write file. Returns \c false if unsuccesful. bool writeFile(support::FileName const &) const; + /// Loads LyX file \c filename into buffer, * and \return success + bool loadLyXFile(support::FileName const & s); + + /// Fill in the ErrorList with the TeXErrors + void bufferErrors(TeXErrors const &, ErrorList &) const; + /// Just a wrapper for writeLaTeXSource, first creating the ofstream. bool makeLaTeXFile(support::FileName const & filename, std::string const & original_path, @@ -393,7 +399,7 @@ public: /// This function is called when some parsing error shows up. void errors(std::string const & err) const; /// This function is called when the buffer busy status change. - void busy(bool on) const; + void setBusy(bool on) const; /// This function is called when the buffer readonly status change. void readonly(bool on) const; /// Update window titles of all users. @@ -428,8 +434,9 @@ public: /// std::vector exportableFormats(bool only_viewable) const; - private: + /// + bool readFileHelper(support::FileName const & s); /// std::vector backends() const; /** Inserts a file into a document diff --git a/src/BufferView.cpp b/src/BufferView.cpp index 5582d28c96..c4f1ae7407 100644 --- a/src/BufferView.cpp +++ b/src/BufferView.cpp @@ -1966,7 +1966,7 @@ void BufferView::menuInsertLyXFile(string const & filenm) docstring res; Buffer buf("", false); - if (lyx::loadLyXFile(&buf, FileName(filename))) { + if (buf.loadLyXFile(FileName(filename))) { ErrorList & el = buffer_.errorList("Parse"); // Copy the inserted document error list into the current buffer one. el = buf.errorList("Parse"); @@ -1974,8 +1974,9 @@ void BufferView::menuInsertLyXFile(string const & filenm) cap::pasteParagraphList(d->cursor_, buf.paragraphs(), buf.params().getTextClassPtr(), el); res = _("Document %1$s inserted."); - } else + } else { res = _("Could not insert document %1$s"); + } // emit message signal. message(bformat(res, disp_fn)); diff --git a/src/Converter.cpp b/src/Converter.cpp index 1ece7936bb..481d706ed1 100644 --- a/src/Converter.cpp +++ b/src/Converter.cpp @@ -578,7 +578,7 @@ bool Converters::scanLog(Buffer const & buffer, string const & /*command*/, int const result = latex.scanLogFile(terr); if (result & LaTeX::ERRORS) - bufferErrors(buffer, terr, errorList); + buffer.bufferErrors(terr, errorList); return true; } @@ -589,7 +589,7 @@ namespace { class ShowMessage : public boost::signals::trackable { public: - ShowMessage(Buffer const & b) : buffer_(b) {}; + ShowMessage(Buffer const & b) : buffer_(b) {} void operator()(docstring const & msg) const { buffer_.message(msg); } private: Buffer const & buffer_; @@ -601,7 +601,7 @@ private: bool Converters::runLaTeX(Buffer const & buffer, string const & command, OutputParams const & runparams, ErrorList & errorList) { - buffer.busy(true); + buffer.setBusy(true); buffer.message(_("Running LaTeX...")); runparams.document_language = buffer.params().language->babel(); @@ -615,7 +615,7 @@ bool Converters::runLaTeX(Buffer const & buffer, string const & command, int const result = latex.run(terr); if (result & LaTeX::ERRORS) - bufferErrors(buffer, terr, errorList); + buffer.bufferErrors(terr, errorList); // check return value from latex.run(). if ((result & LaTeX::NO_LOGFILE)) { @@ -630,7 +630,7 @@ bool Converters::runLaTeX(Buffer const & buffer, string const & command, } - buffer.busy(false); + buffer.setBusy(false); int const ERROR_MASK = LaTeX::NO_LOGFILE | diff --git a/src/LyX.cpp b/src/LyX.cpp index 4b326b137d..574fa28e8c 100644 --- a/src/LyX.cpp +++ b/src/LyX.cpp @@ -602,7 +602,7 @@ void LyX::loadFiles() continue; Buffer * buf = pimpl_->buffer_list_.newBuffer(it->absFilename(), false); - if (loadLyXFile(buf, *it)) { + if (buf->loadLyXFile(*it)) { ErrorList const & el = buf->errorList("Parse"); if (!el.empty()) for_each(el.begin(), el.end(), diff --git a/src/Paragraph.cpp b/src/Paragraph.cpp index db753e91c9..70dfc40617 100644 --- a/src/Paragraph.cpp +++ b/src/Paragraph.cpp @@ -2504,4 +2504,18 @@ Inset const * Paragraph::getInset(pos_type pos) const return d->insetlist_.get(pos); } + +int Paragraph::numberOfOptArgs() const +{ + int num = 0; + InsetList::const_iterator it = insetList().begin(); + InsetList::const_iterator end = insetList().end(); + for (; it != end ; ++it) { + if (it->inset->lyxCode() == OPTARG_CODE) + ++num; + } + return num; +} + + } // namespace lyx diff --git a/src/Paragraph.h b/src/Paragraph.h index 9f32437852..9a5bc37704 100644 --- a/src/Paragraph.h +++ b/src/Paragraph.h @@ -371,8 +371,10 @@ public: /// by this author in the paragraph. void checkAuthors(AuthorList const & authorList); -private: + /// return the number of InsetOptArg in a paragraph + int numberOfOptArgs() const; +private: /// LayoutPtr layout_; /** diff --git a/src/Text3.cpp b/src/Text3.cpp index 48de101b36..a0a667553e 100644 --- a/src/Text3.cpp +++ b/src/Text3.cpp @@ -1708,7 +1708,7 @@ bool Text::getStatus(Cursor & cur, FuncRequest const & cmd, break; case LFUN_OPTIONAL_INSERT: code = OPTARG_CODE; - enable = numberOfOptArgs(cur.paragraph()) + enable = cur.paragraph().numberOfOptArgs() < cur.paragraph().layout()->optionalargs; break; case LFUN_ENVIRONMENT_INSERT: diff --git a/src/TextClassList.cpp b/src/TextClassList.cpp index 430e9e3ade..87e97cf676 100644 --- a/src/TextClassList.cpp +++ b/src/TextClassList.cpp @@ -229,6 +229,17 @@ TextClassList::addTextClass(std::string const & textclass, std::string const & p TextClassList textclasslist; +textclass_type defaultTextclass() +{ + // We want to return the article class. if `first' is + // true in the returned pair, then `second' is the textclass + // number; if it is false, second is 0. In both cases, second + // is what we want. + return textclasslist.numberOfClass("article").second; +} + + + // Reads the style files bool LyXSetStyle() { diff --git a/src/TextClassList.h b/src/TextClassList.h index dcbae50358..5560177ab4 100644 --- a/src/TextClassList.h +++ b/src/TextClassList.h @@ -68,6 +68,8 @@ private: /// extern TextClassList textclasslist; +/// +textclass_type defaultTextclass(); } // namespace lyx diff --git a/src/buffer_funcs.cpp b/src/buffer_funcs.cpp index 6c1c49c3c2..d66040c563 100644 --- a/src/buffer_funcs.cpp +++ b/src/buffer_funcs.cpp @@ -37,11 +37,9 @@ #include "ParagraphList.h" #include "ParagraphParameters.h" #include "ParIterator.h" -#include "LyXVC.h" #include "TexRow.h" #include "Text.h" #include "TocBackend.h" -#include "VCBackend.h" #include "frontends/alert.h" @@ -73,111 +71,6 @@ using support::unlink; namespace Alert = frontend::Alert; -namespace { - -bool readFile(Buffer * const b, FileName const & s) -{ - BOOST_ASSERT(b); - - // File information about normal file - if (!s.exists()) { - docstring const file = makeDisplayPath(s.absFilename(), 50); - docstring text = bformat(_("The specified document\n%1$s" - "\ncould not be read."), file); - Alert::error(_("Could not read document"), text); - return false; - } - - // Check if emergency save file exists and is newer. - FileName const e(s.absFilename() + ".emergency"); - - if (e.exists() && s.exists() && e.lastModified() > s.lastModified()) { - docstring const file = makeDisplayPath(s.absFilename(), 20); - docstring const text = - bformat(_("An emergency save of the document " - "%1$s exists.\n\n" - "Recover emergency save?"), file); - switch (Alert::prompt(_("Load emergency save?"), text, 0, 2, - _("&Recover"), _("&Load Original"), - _("&Cancel"))) - { - case 0: - // the file is not saved if we load the emergency file. - b->markDirty(); - return b->readFile(e); - case 1: - break; - default: - return false; - } - } - - // Now check if autosave file is newer. - FileName const a(onlyPath(s.absFilename()) + '#' + onlyFilename(s.absFilename()) + '#'); - - if (a.exists() && s.exists() && a.lastModified() > s.lastModified()) { - docstring const file = makeDisplayPath(s.absFilename(), 20); - docstring const text = - bformat(_("The backup of the document " - "%1$s is newer.\n\nLoad the " - "backup instead?"), file); - switch (Alert::prompt(_("Load backup?"), text, 0, 2, - _("&Load backup"), _("Load &original"), - _("&Cancel") )) - { - case 0: - // the file is not saved if we load the autosave file. - b->markDirty(); - return b->readFile(a); - case 1: - // Here we delete the autosave - unlink(a); - break; - default: - return false; - } - } - return b->readFile(s); -} - - -} // namespace anon - - - -bool loadLyXFile(Buffer * b, FileName const & s) -{ - BOOST_ASSERT(b); - - if (s.isReadable()) { - if (readFile(b, s)) { - b->lyxvc().file_found_hook(s); - if (!s.isWritable()) - b->setReadonly(true); - return true; - } - } else { - docstring const file = makeDisplayPath(s.absFilename(), 20); - // Here we probably should run - if (LyXVC::file_not_found_hook(s)) { - docstring const text = - bformat(_("Do you want to retrieve the document" - " %1$s from version control?"), file); - int const ret = Alert::prompt(_("Retrieve from version control?"), - text, 0, 1, _("&Retrieve"), _("&Cancel")); - - if (ret == 0) { - // How can we know _how_ to do the checkout? - // With the current VC support it has to be, - // a RCS file since CVS do not have special ,v files. - RCS::retrieve(s); - return loadLyXFile(b, s); - } - } - } - return false; -} - bool checkIfLoaded(FileName const & fn) { @@ -211,7 +104,7 @@ Buffer * checkAndLoadLyXFile(FileName const & filename) if (filename.isReadable()) { Buffer * b = theBufferList().newBuffer(filename.absFilename()); - if (!lyx::loadLyXFile(b, filename)) { + if (!b->loadLyXFile(filename)) { theBufferList().release(b); return 0; } @@ -228,6 +121,7 @@ Buffer * checkAndLoadLyXFile(FileName const & filename) return 0; } + // FIXME newFile() should probably be a member method of Application... Buffer * newFile(string const & filename, string const & templatename, bool const isNamed) @@ -267,32 +161,6 @@ Buffer * newFile(string const & filename, string const & templatename, } -void bufferErrors(Buffer const & buf, TeXErrors const & terr, - ErrorList & errorList) -{ - TeXErrors::Errors::const_iterator cit = terr.begin(); - TeXErrors::Errors::const_iterator end = terr.end(); - - for (; cit != end; ++cit) { - int id_start = -1; - int pos_start = -1; - int errorrow = cit->error_in_line; - bool found = buf.texrow().getIdFromRow(errorrow, id_start, - pos_start); - int id_end = -1; - int pos_end = -1; - do { - ++errorrow; - found = buf.texrow().getIdFromRow(errorrow, id_end, - pos_end); - } while (found && id_start == id_end && pos_start == pos_end); - - errorList.push_back(ErrorItem(cit->error_desc, - cit->error_text, id_start, pos_start, pos_end)); - } -} - - int countWords(DocIterator const & from, DocIterator const & to) { int count = 0; @@ -623,13 +491,4 @@ void checkBufferStructure(Buffer & buffer, ParIterator const & par_it) } -textclass_type defaultTextclass() -{ - // We want to return the article class. if `first' is - // true in the returned pair, then `second' is the textclass - // number; if it is false, second is 0. In both cases, second - // is what we want. - return textclasslist.numberOfClass("article").second; -} - } // namespace lyx diff --git a/src/buffer_funcs.h b/src/buffer_funcs.h index 3f066a2541..dcdefd7cd4 100644 --- a/src/buffer_funcs.h +++ b/src/buffer_funcs.h @@ -12,19 +12,14 @@ #ifndef BUFFER_FUNCS_H #define BUFFER_FUNCS_H -#include "support/docstring.h" - #include - namespace lyx { namespace support { class FileName; } class Buffer; class DocIterator; -class ErrorList; -class TeXErrors; class ParIterator; @@ -33,12 +28,6 @@ class ParIterator; */ bool checkIfLoaded(support::FileName const & fn); -/** - * Loads a LyX file \c filename into \c Buffer - * and \return success status. - */ -bool loadLyXFile(Buffer *, support::FileName const & filename); - /** * Checks and loads a LyX file \param filename. * \retval the newly created \c Buffer pointer if successful or 0. @@ -52,9 +41,6 @@ Buffer * checkAndLoadLyXFile(support::FileName const & filename); Buffer * newFile(std::string const & filename, std::string const & templatename, bool isNamed = false); -/// Fill in the ErrorList with the TeXErrors -void bufferErrors(Buffer const &, TeXErrors const &, ErrorList &); - /// Count the number of words in the text between these two iterators int countWords(DocIterator const & from, DocIterator const & to); @@ -67,9 +53,6 @@ void updateLabels(Buffer const &, ParIterator &); /// void checkBufferStructure(Buffer &, ParIterator const &); -/// -textclass_type defaultTextclass(); - } // namespace lyx #endif // BUFFER_FUNCS_H diff --git a/src/insets/InsetCaption.cpp b/src/insets/InsetCaption.cpp index dd71dedc4d..6e0d6061c3 100644 --- a/src/insets/InsetCaption.cpp +++ b/src/insets/InsetCaption.cpp @@ -203,7 +203,7 @@ bool InsetCaption::getStatus(Cursor & cur, FuncRequest const & cmd, return true; case LFUN_OPTIONAL_INSERT: - status.enabled(numberOfOptArgs(cur.paragraph()) == 0); + status.enabled(cur.paragraph().numberOfOptArgs() == 0); return true; default: diff --git a/src/insets/InsetInclude.cpp b/src/insets/InsetInclude.cpp index 9ce09b6004..d710082c53 100644 --- a/src/insets/InsetInclude.cpp +++ b/src/insets/InsetInclude.cpp @@ -400,7 +400,7 @@ Buffer * loadIfNeeded(Buffer const & parent, InsetCommandParams const & params) return 0; child = theBufferList().newBuffer(included_file.absFilename()); - if (!loadLyXFile(child, included_file)) { + if (!child->loadLyXFile(included_file)) { //close the buffer we just opened theBufferList().close(child, false); return 0; diff --git a/src/paragraph_funcs.cpp b/src/paragraph_funcs.cpp index baf9399573..0dbacb46ac 100644 --- a/src/paragraph_funcs.cpp +++ b/src/paragraph_funcs.cpp @@ -308,21 +308,6 @@ Font const outerFont(pit_type par_offset, ParagraphList const & pars) } -/// return the number of InsetOptArg in a paragraph -int numberOfOptArgs(Paragraph const & par) -{ - int num = 0; - - InsetList::const_iterator it = par.insetList().begin(); - InsetList::const_iterator end = par.insetList().end(); - for (; it != end ; ++it) { - if (it->inset->lyxCode() == OPTARG_CODE) - ++num; - } - return num; -} - - void acceptChanges(ParagraphList & pars, BufferParams const & bparams) { pit_type pars_size = static_cast(pars.size()); diff --git a/src/paragraph_funcs.h b/src/paragraph_funcs.h index ac34b9a2a7..c282617d72 100644 --- a/src/paragraph_funcs.h +++ b/src/paragraph_funcs.h @@ -72,9 +72,6 @@ int getEndLabel(pit_type par, ParagraphList const & plist); */ Font const outerFont(pit_type par_offset, ParagraphList const & pars); -/// return the number of InsetOptArg in a paragraph -int numberOfOptArgs(Paragraph const & par); - /// accept the changes within the complete ParagraphList void acceptChanges(ParagraphList & pars, BufferParams const & bparams);