]> git.lyx.org Git - lyx.git/blobdiff - src/Buffer.cpp
Output labels (1.1, etc) with the TOC.
[lyx.git] / src / Buffer.cpp
index b116f0f0945a4f8b426ee3e6553636393cb0a3f7..bc72f21afdef7e931b39d95155f40abc3c5cb20c 100644 (file)
@@ -127,7 +127,7 @@ namespace {
 
 // Do not remove the comment below, so we get merge conflict in
 // independent branches. Instead add your own.
-int const LYX_FORMAT = 375; // jspitzm: includeonly support
+int const LYX_FORMAT = 376; // jspitzm: support for unincluded file maintenance
 
 typedef map<string, bool> DepClean;
 typedef map<docstring, pair<InsetLabel const *, Buffer::References> > RefCache;
@@ -224,7 +224,7 @@ public:
 
        /// A cache for the bibfiles (including bibfiles of loaded child
        /// documents), needed for appropriate update of natbib labels.
-       mutable support::FileNameList bibfilesCache_;
+       mutable support::FileNameList bibfiles_cache_;
 
        // FIXME The caching mechanism could be improved. At present, we have a
        // cache for each Buffer, that caches all the bibliography info for that
@@ -233,9 +233,9 @@ public:
        /// A cache for bibliography info
        mutable BiblioInfo bibinfo_;
        /// whether the bibinfo cache is valid
-       bool bibinfoCacheValid_;
+       bool bibinfo_cache_valid_;
        /// Cache of timestamps of .bib files
-       map<FileName, time_t> bibfileStatus_;
+       map<FileName, time_t> bibfile_status_;
 
        mutable RefCache ref_cache_;
 
@@ -248,7 +248,10 @@ public:
                // if parent_buffer is not loaded, then it has been unloaded,
                // which means that parent_buffer is an invalid pointer. So we
                // set it to null in that case.
-               if (!theBufferList().isLoaded(parent_buffer))
+               // however, the BufferList doesn't know about cloned buffers, so
+               // they will always be regarded as unloaded. in that case, we hope
+               // for the best.
+               if (!cloned_buffer_ && !theBufferList().isLoaded(parent_buffer))
                        parent_buffer = 0;
                return parent_buffer; 
        }
@@ -294,7 +297,7 @@ Buffer::Impl::Impl(Buffer & parent, FileName const & file, bool readonly_,
        : lyx_clean(true), bak_clean(true), unnamed(false),
          read_only(readonly_), filename(file), file_fully_loaded(false),
          toc_backend(&parent), macro_lock(false), timestamp_(0),
-         checksum_(0), wa_(0), undo_(parent), bibinfoCacheValid_(false),
+         checksum_(0), wa_(0), undo_(parent), bibinfo_cache_valid_(false),
          cloned_buffer_(cloned_buffer), parent_buffer(0)
 {
        if (!cloned_buffer_) {
@@ -307,6 +310,10 @@ Buffer::Impl::Impl(Buffer & parent, FileName const & file, bool readonly_,
        temppath = cloned_buffer_->d->temppath;
        file_fully_loaded = true;
        params = cloned_buffer_->d->params;
+       bibfiles_cache_ = cloned_buffer_->d->bibfiles_cache_;
+       bibinfo_ = cloned_buffer_->d->bibinfo_;
+       bibinfo_cache_valid_ = cloned_buffer_->d->bibinfo_cache_valid_;
+       bibfile_status_ = cloned_buffer_->d->bibfile_status_;
 }
 
 
@@ -350,8 +357,10 @@ Buffer::~Buffer()
        Impl::BufferPositionMap::iterator end = d->children_positions.end();
        for (; it != end; ++it) {
                Buffer * child = const_cast<Buffer *>(it->first);
+               if (d->cloned_buffer_)
+                       delete child;
                // The child buffer might have been closed already.
-               if (theBufferList().isLoaded(child))
+               else if (theBufferList().isLoaded(child))
                        theBufferList().releaseChild(this, child);
        }
 
@@ -380,9 +389,29 @@ Buffer::~Buffer()
 
 Buffer * Buffer::clone() const
 {
-       // FIXME for asynchronous export and preview: We must also clone all
-       // the child buffers!
-       return new Buffer(fileName().absFilename(), false, this);
+       Buffer * buffer_clone = new Buffer(fileName().absFilename(), false, this);
+       buffer_clone->d->macro_lock = true;
+       buffer_clone->d->children_positions.clear();
+       // FIXME (Abdel 09/01/2010): this is too complicated. The whole children_positions and
+       // math macro caches need to be rethought and simplified.
+       // I am not sure wether we should handle Buffer cloning here or in BufferList.
+       // Right now BufferList knows nothing about buffer clones.
+       Impl::BufferPositionMap::iterator it = d->children_positions.begin();
+       Impl::BufferPositionMap::iterator end = d->children_positions.end();
+       for (; it != end; ++it) {
+               DocIterator dit = it->second.clone(buffer_clone);
+               dit.setBuffer(buffer_clone);
+               Buffer * child = const_cast<Buffer *>(it->first);
+               Buffer * child_clone = child->clone();
+               Inset * inset = dit.nextInset();
+               LASSERT(inset && inset->lyxCode() == INCLUDE_CODE, continue);
+               InsetInclude * inset_inc = static_cast<InsetInclude *>(inset);
+               inset_inc->setChildBuffer(child_clone);
+               child_clone->d->setParent(buffer_clone);
+               buffer_clone->setChild(dit, child_clone);
+       }
+       buffer_clone->d->macro_lock = false;
+       return buffer_clone;
 }
 
 
@@ -484,6 +513,12 @@ Undo & Buffer::undo()
 }
 
 
+void Buffer::setChild(DocIterator const & dit, Buffer * child)
+{
+       d->children_positions[child] = dit;
+}
+
+
 string Buffer::latexName(bool const no_path) const
 {
        FileName latex_name =
@@ -881,11 +916,18 @@ Buffer::ReadStatus Buffer::readFile(Lexer & lex, FileName const & filename,
 
                cmd_ret const ret = runCommand(command_str);
                if (ret.first != 0) {
-                       Alert::error(_("Conversion script failed"),
-                                    bformat(_("%1$s is from a different version"
+                       if (file_format < LYX_FORMAT)
+                               Alert::error(_("Conversion script failed"),
+                                    bformat(_("%1$s is from an older version"
                                              " of LyX, but the lyx2lyx script"
                                              " failed to convert it."),
                                              from_utf8(filename.absFilename())));
+                       else
+                               Alert::error(_("Conversion script failed"),
+                                    bformat(_("%1$s is from a newer version"
+                                             " of LyX and cannot be converted by the"
+                                                               " lyx2lyx script."),
+                                             from_utf8(filename.absFilename())));
                        return failure;
                } else {
                        bool const ret = readFile(tmpfile);
@@ -1456,21 +1498,21 @@ void Buffer::writeLyXHTMLSource(odocstream & os,
 {
        LaTeXFeatures features(*this, params(), runparams);
        validate(features);
-       updateLabels(UpdateMaster, true);
-
-       d->texrow.reset();
+       updateLabels(UpdateMaster, OutputUpdate);
+       checkBibInfoCache();
+       d->bibinfo_.makeCitationLabels(*this);
+       updateMacros();
+       updateMacroInstances();
 
        if (!only_body) {
                os << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
                os << "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.1 plus MathML 2.0//EN\" \"http://www.w3.org/TR/MathML2/dtd/xhtml-math11-f.dtd\">\n";
                // FIXME Language should be set properly.
                os << "<html xmlns=\"http://www.w3.org/1999/xhtml\">\n";
-               // FIXME Header
                os << "<head>\n";
                // FIXME Presumably need to set this right
                os << "<meta http-equiv=\"Content-type\" content=\"text/html;charset=UTF-8\" />\n";
-               // FIXME Get this during validation? What about other meta-data?
-               os << "<title>TBA</title>\n";
+               os << "<title>" << features.htmlTitle() << "</title>\n";
 
                os << "\n<!-- Text Class Preamble -->\n"
                        << features.getTClassHTMLPreamble()
@@ -1580,13 +1622,13 @@ void Buffer::updateBibfilesCache(UpdateScope scope) const
                return;
        }
 
-       d->bibfilesCache_.clear();
+       d->bibfiles_cache_.clear();
        for (InsetIterator it = inset_iterator_begin(inset()); it; ++it) {
                if (it->lyxCode() == BIBTEX_CODE) {
                        InsetBibtex const & inset =
                                static_cast<InsetBibtex const &>(*it);
                        support::FileNameList const bibfiles = inset.getBibFiles();
-                       d->bibfilesCache_.insert(d->bibfilesCache_.end(),
+                       d->bibfiles_cache_.insert(d->bibfiles_cache_.end(),
                                bibfiles.begin(),
                                bibfiles.end());
                } else if (it->lyxCode() == INCLUDE_CODE) {
@@ -1595,19 +1637,19 @@ void Buffer::updateBibfilesCache(UpdateScope scope) const
                        inset.updateBibfilesCache();
                        support::FileNameList const & bibfiles =
                                        inset.getBibfilesCache();
-                       d->bibfilesCache_.insert(d->bibfilesCache_.end(),
+                       d->bibfiles_cache_.insert(d->bibfiles_cache_.end(),
                                bibfiles.begin(),
                                bibfiles.end());
                }
        }
        // the bibinfo cache is now invalid
-       d->bibinfoCacheValid_ = false;
+       d->bibinfo_cache_valid_ = false;
 }
 
 
 void Buffer::invalidateBibinfoCache()
 {
-       d->bibinfoCacheValid_ = false;
+       d->bibinfo_cache_valid_ = false;
 }
 
 
@@ -1619,10 +1661,10 @@ support::FileNameList const & Buffer::getBibfilesCache(UpdateScope scope) const
                return pbuf->getBibfilesCache();
 
        // We update the cache when first used instead of at loading time.
-       if (d->bibfilesCache_.empty())
+       if (d->bibfiles_cache_.empty())
                const_cast<Buffer *>(this)->updateBibfilesCache(scope);
 
-       return d->bibfilesCache_;
+       return d->bibfiles_cache_;
 }
 
 
@@ -1640,28 +1682,31 @@ BiblioInfo const & Buffer::masterBibInfo() const
 
 BiblioInfo const & Buffer::localBibInfo() const
 {
-       if (d->bibinfoCacheValid_) {
-               support::FileNameList const & bibfilesCache = getBibfilesCache();
-               // compare the cached timestamps with the actual ones.
-               support::FileNameList::const_iterator ei = bibfilesCache.begin();
-               support::FileNameList::const_iterator en = bibfilesCache.end();
-               for (; ei != en; ++ ei) {
-                       time_t lastw = ei->lastModified();
-                       if (lastw != d->bibfileStatus_[*ei]) {
-                               d->bibinfoCacheValid_ = false;
-                               d->bibfileStatus_[*ei] = lastw;
-                               break;
-                       }
+       return d->bibinfo_;
+}
+
+
+void Buffer::checkBibInfoCache() const 
+{
+       support::FileNameList const & bibfilesCache = getBibfilesCache();
+       // compare the cached timestamps with the actual ones.
+       support::FileNameList::const_iterator ei = bibfilesCache.begin();
+       support::FileNameList::const_iterator en = bibfilesCache.end();
+       for (; ei != en; ++ ei) {
+               time_t lastw = ei->lastModified();
+               time_t prevw = d->bibfile_status_[*ei];
+               if (lastw != prevw) {
+                       d->bibinfo_cache_valid_ = false;
+                       d->bibfile_status_[*ei] = lastw;
                }
        }
 
-       if (!d->bibinfoCacheValid_) {
+       if (!d->bibinfo_cache_valid_) {
                d->bibinfo_.clear();
                for (InsetIterator it = inset_iterator_begin(inset()); it; ++it)
                        it->fillWithBibKeys(d->bibinfo_, it);
-               d->bibinfoCacheValid_ = true;
-       }
-       return d->bibinfo_;
+               d->bibinfo_cache_valid_ = true;
+       }       
 }
 
 
@@ -1795,7 +1840,7 @@ void Buffer::dispatch(FuncRequest const & func, DispatchResult & dr)
                break;
 
        case LFUN_BUFFER_EXPORT: {
-               bool success = doExport(argument, false);
+               bool success = doExport(argument, false, false);
                dr.setError(success);
                if (!success)
                        dr.setMessage(bformat(_("Error exporting to format: %1$s."), 
@@ -1804,7 +1849,7 @@ void Buffer::dispatch(FuncRequest const & func, DispatchResult & dr)
        }
 
        case LFUN_BUILD_PROGRAM:
-               doExport("program", true);
+               doExport("program", true, false);
                break;
 
        case LFUN_BUFFER_CHKTEX:
@@ -1836,7 +1881,7 @@ void Buffer::dispatch(FuncRequest const & func, DispatchResult & dr)
                                break;
 
                } else {
-                       doExport(format_name, true, filename);
+                       doExport(format_name, true, false, filename);
                }
 
                // Substitute $$FName for filename
@@ -1970,7 +2015,10 @@ void Buffer::dispatch(FuncRequest const & func, DispatchResult & dr)
                        break;
                }
 
-               if (!doExport("dvi", true)) {
+               bool const update_unincluded =
+                               params().maintain_unincluded_children
+                               && !params().getIncludedChildren().empty();
+               if (!doExport("dvi", true, update_unincluded)) {
                        showPrintError(absFileName());
                        dr.setMessage(_("Error exporting to DVI."));
                        break;
@@ -2518,7 +2566,7 @@ MacroData const * Buffer::getMacro(docstring const & name,
 
 void Buffer::updateMacros(DocIterator & it, DocIterator & scope) const
 {
-       pit_type lastpit = it.lastpit();
+       pit_type const lastpit = it.lastpit();
 
        // look for macros in each paragraph
        while (it.pit() <= lastpit) {
@@ -2784,6 +2832,7 @@ void Buffer::changeRefsIfUnique(docstring const & from, docstring const & to,
        // Check if the label 'from' appears more than once
        vector<docstring> labels;
        string paramName;
+       checkBibInfoCache();
        BiblioInfo const & keys = masterBibInfo();
        BiblioInfo::const_iterator bit  = keys.begin();
        BiblioInfo::const_iterator bend = keys.end();
@@ -3106,12 +3155,13 @@ string Buffer::getDefaultOutputFormat() const
 
 
 bool Buffer::doExport(string const & format, bool put_in_tempdir,
-       string & result_file) const
+       bool includeall, string & result_file) const
 {
        string backend_format;
        OutputParams runparams(&params().encoding());
        runparams.flavor = OutputParams::LATEX;
        runparams.linelen = lyxrc.plaintext_linelen;
+       runparams.includeall = includeall;
        vector<string> backs = backends();
        if (find(backs.begin(), backs.end(), format) == backs.end()) {
                // Get shortest path to format
@@ -3223,13 +3273,16 @@ bool Buffer::doExport(string const & format, bool put_in_tempdir,
                runparams.exportdata->externalFiles(format);
        string const dest = onlyPath(result_file);
        CopyStatus status = SUCCESS;
-       for (vector<ExportedFile>::const_iterator it = files.begin();
-               it != files.end() && status != CANCEL; ++it) {
+       
+       vector<ExportedFile>::const_iterator it = files.begin();
+       vector<ExportedFile>::const_iterator const en = files.end();
+       for (; it != en && status != CANCEL; ++it) {
                string const fmt = formats.getFormatFromFile(it->sourceName);
                status = copyFile(fmt, it->sourceName,
                        makeAbsPath(it->exportName, dest),
                        it->exportName, status == FORCE);
        }
+
        if (status == CANCEL) {
                message(_("Document export cancelled."));
        } else if (tmp_result_file.exists()) {
@@ -3251,17 +3304,26 @@ bool Buffer::doExport(string const & format, bool put_in_tempdir,
 }
 
 
-bool Buffer::doExport(string const & format, bool put_in_tempdir) const
+bool Buffer::doExport(string const & format, bool put_in_tempdir,
+                     bool includeall) const
 {
        string result_file;
-       return doExport(format, put_in_tempdir, result_file);
+       // (1) export with all included children (omit \includeonly)
+       if (includeall && !doExport(format, put_in_tempdir, true, result_file))
+               return false;
+       // (2) export with included children only
+       return doExport(format, put_in_tempdir, false, result_file);
 }
 
 
-bool Buffer::preview(string const & format) const
+bool Buffer::preview(string const & format, bool includeall) const
 {
        string result_file;
-       if (!doExport(format, true, result_file))
+       // (1) export with all included children (omit \includeonly)
+       if (includeall && !doExport(format, true, true))
+               return false;
+       // (2) export with included children only
+       if (!doExport(format, true, false, result_file))
                return false;
        return formats.view(*this, FileName(result_file), format);
 }
@@ -3395,31 +3457,17 @@ bool Buffer::readFileHelper(FileName const & s)
 
 bool Buffer::loadLyXFile(FileName const & s)
 {
-       if (s.isReadableFile()) {
-               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);
-                       }
-               }
+       // If the file is not readable, we try to
+       // retrieve the file from version control.
+       if (!s.isReadableFile()
+                 && !LyXVC::file_not_found_hook(s))
+               return false;
+       
+       if (s.isReadableFile()
+                 && readFileHelper(s)) {
+               lyxvc().file_found_hook(s);
+               setReadonly(!s.isWritable());
+               return true;
        }
        return false;
 }
@@ -3455,11 +3503,15 @@ void Buffer::setBuffersForInsets() const
 }
 
 
-void Buffer::updateLabels(UpdateScope scope, bool out) const
+void Buffer::updateLabels(UpdateScope scope, UpdateType utype) const
 {
        // Use the master text class also for child documents
        Buffer const * const master = masterBuffer();
        DocumentClass const & textclass = master->params().documentClass();
+       
+       // do this only if we are the top-level Buffer
+       if (scope != UpdateMaster || master == this)
+               checkBibInfoCache();
 
        // keep the buffers to be children in this set. If the call from the
        // master comes back we can see which of them were actually seen (i.e.
@@ -3469,7 +3521,7 @@ void Buffer::updateLabels(UpdateScope scope, bool out) const
                // If this is a child document start with the master
                if (master != this) {
                        bufToUpdate.insert(this);
-                       master->updateLabels(UpdateMaster, out);
+                       master->updateLabels(UpdateMaster, utype);
                        // Do this here in case the master has no gui associated with it. Then, 
                        // the TocModel is not updated and TocModel::toc_ is invalid (bug 5699).
                        if (!master->gui_)
@@ -3497,7 +3549,7 @@ void Buffer::updateLabels(UpdateScope scope, bool out) const
 
        // do the real work
        ParIterator parit = cbuf.par_iterator_begin();
-       updateLabels(parit, out);
+       updateLabels(parit, utype);
 
        if (master != this)
                // TocBackend update will be done later.
@@ -3579,7 +3631,7 @@ static bool needEnumCounterReset(ParIterator const & it)
 
 
 // set the label of a paragraph. This includes the counters.
-void Buffer::setLabel(ParIterator & it) const
+void Buffer::setLabel(ParIterator & it, UpdateType utype) const
 {
        BufferParams const & bp = this->masterBuffer()->params();
        DocumentClass const & textclass = bp.documentClass();
@@ -3611,7 +3663,7 @@ void Buffer::setLabel(ParIterator & it) const
                if (layout.toclevel <= bp.secnumdepth
                    && (layout.latextype != LATEX_ENVIRONMENT
                        || it.text()->isFirstInSequence(it.pit()))) {
-                       counters.step(layout.counter);
+                       counters.step(layout.counter, utype);
                        par.params().labelString(
                                par.expandLabel(layout, bp));
                } else
@@ -3665,7 +3717,7 @@ void Buffer::setLabel(ParIterator & it) const
                // Maybe we have to reset the enumeration counter.
                if (needEnumCounterReset(it))
                        counters.reset(enumcounter);
-               counters.step(enumcounter);
+               counters.step(enumcounter, utype);
 
                string const & lang = par.getParLanguage(bp)->code();
                par.params().labelString(counters.theCounter(enumcounter, lang));
@@ -3682,7 +3734,7 @@ void Buffer::setLabel(ParIterator & it) const
                        docstring name = this->B_(textclass.floats().getType(type).name());
                        if (counters.hasCounter(from_utf8(type))) {
                                string const & lang = par.getParLanguage(bp)->code();
-                               counters.step(from_utf8(type));
+                               counters.step(from_utf8(type), utype);
                                full_label = bformat(from_ascii("%1$s %2$s:"), 
                                                     name, 
                                                     counters.theCounter(from_utf8(type), lang));
@@ -3708,7 +3760,7 @@ void Buffer::setLabel(ParIterator & it) const
 }
 
 
-void Buffer::updateLabels(ParIterator & parit, bool out) const
+void Buffer::updateLabels(ParIterator & parit, UpdateType utype) const
 {
        LASSERT(parit.pit() == 0, /**/);
 
@@ -3725,15 +3777,23 @@ void Buffer::updateLabels(ParIterator & parit, bool out) const
                parit->params().depth(min(parit->params().depth(), maxdepth));
                maxdepth = parit->getMaxDepthAfter();
 
+               if (utype == OutputUpdate) {
+                       // track the active counters
+                       // we have to do this for the master buffer, since the local
+                       // buffer isn't tracking anything.
+                       masterBuffer()->params().documentClass().counters().
+                                       setActiveLayout(parit->layout());
+               }
+               
                // set the counter for this paragraph
-               setLabel(parit);
+               setLabel(parit, utype);
 
-               // Now the insets
+               // now the insets
                InsetList::const_iterator iit = parit->insetList().begin();
                InsetList::const_iterator end = parit->insetList().end();
                for (; iit != end; ++iit) {
                        parit.pos() = iit->pos;
-                       iit->inset->updateLabels(parit, out);
+                       iit->inset->updateLabels(parit, utype);
                }
        }
 }
@@ -3786,12 +3846,13 @@ bool Buffer::reload()
        if (success) {
                updateLabels();
                changed(true);
-               errors("Parse");
+               markClean();
                message(bformat(_("Document %1$s reloaded."), disp_fn));
        } else {
                message(bformat(_("Could not reload document %1$s."), disp_fn));
        }       
        setBusy(false);
+       errors("Parse");
        return success;
 }