]> git.lyx.org Git - lyx.git/blobdiff - src/Buffer.cpp
Move bind file format tag to LyXAction.cpp, and rename it.
[lyx.git] / src / Buffer.cpp
index cf66a7def965a5c8bfd0ffdac57791ed94cff550..4cf17a497dccfe233772b033839ccf6eea5b3c41 100644 (file)
@@ -820,6 +820,14 @@ bool Buffer::readDocument(Lexer & lex)
        // read main text
        bool const res = text().read(lex, errorList, d->inset);
 
+       // inform parent buffer about local macros
+       if (parent()) {
+               Buffer const * pbuf = parent();
+               UserMacroSet::const_iterator cit = usermacros.begin();
+               UserMacroSet::const_iterator end = usermacros.end();
+               for (; cit != end; ++cit)
+                       pbuf->usermacros.insert(*cit);
+       }
        usermacros.clear();
        updateMacros();
        updateMacroInstances();
@@ -1053,7 +1061,7 @@ bool Buffer::save() const
                } else {
                        Alert::error(_("Backup failure"),
                                     bformat(_("Cannot create backup file %1$s.\n"
-                                              "Please check whether the directory exists and is writeable."),
+                                              "Please check whether the directory exists and is writable."),
                                             from_utf8(backupName.absFileName())));
                        //LYXERR(Debug::DEBUG, "Fs error: " << fe.what());
                }
@@ -1809,21 +1817,19 @@ BiblioInfo const & Buffer::masterBibInfo() const
 }
 
 
-void Buffer::checkBibInfoCache() const 
+void Buffer::checkIfBibInfoCacheIsValid() const
 {
        // use the master's cache
        Buffer const * const tmp = masterBuffer();
        if (tmp != this) {
-               tmp->checkBibInfoCache();
+               tmp->checkIfBibInfoCacheIsValid();
                return;
        }
 
-       // this will also reload the cache if it is invalid 
-       support::FileNameList const & bibfiles_cache = getBibfilesCache();
-       
        // compare the cached timestamps with the actual ones.
-       support::FileNameList::const_iterator ei = bibfiles_cache.begin();
-       support::FileNameList::const_iterator en = bibfiles_cache.end();
+       FileNameList const & bibfiles_cache = getBibfilesCache();
+       FileNameList::const_iterator ei = bibfiles_cache.begin();
+       FileNameList::const_iterator en = bibfiles_cache.end();
        for (; ei != en; ++ ei) {
                time_t lastw = ei->lastModified();
                time_t prevw = d->bibfile_status_[*ei];
@@ -1832,20 +1838,50 @@ void Buffer::checkBibInfoCache() const
                        d->bibfile_status_[*ei] = lastw;
                }
        }
-       
-       // if not valid, then reload the info
-       if (!d->bibinfo_cache_valid_) {
-               d->bibinfo_.clear();
-               fillWithBibKeys(d->bibinfo_);
-               d->bibinfo_cache_valid_ = true;
+}
+
+
+void Buffer::reloadBibInfoCache() const
+{
+       // use the master's cache
+       Buffer const * const tmp = masterBuffer();
+       if (tmp != this) {
+               tmp->reloadBibInfoCache();
+               return;
        }
+
+       checkIfBibInfoCacheIsValid();
+       if (d->bibinfo_cache_valid_)
+               return;
+
+       d->bibinfo_.clear();
+       collectBibKeys();
+       d->bibinfo_cache_valid_ = true;
 }
 
 
-void Buffer::fillWithBibKeys(BiblioInfo & keys) const
+void Buffer::collectBibKeys() const
 {
        for (InsetIterator it = inset_iterator_begin(inset()); it; ++it)
-               it->fillWithBibKeys(keys, it);
+               it->collectBibKeys(it);
+}
+
+
+void Buffer::addBiblioInfo(BiblioInfo const & bi) const
+{
+       Buffer const * tmp = masterBuffer();
+       BiblioInfo & masterbi = (tmp == this) ?
+               d->bibinfo_ : tmp->d->bibinfo_;
+       masterbi.mergeBiblioInfo(bi);
+}
+
+
+void Buffer::addBibTeXInfo(docstring const & key, BibTeXInfo const & bi) const
+{
+       Buffer const * tmp = masterBuffer();
+       BiblioInfo & masterbi = (tmp == this) ?
+               d->bibinfo_ : tmp->d->bibinfo_;
+       masterbi[key] = bi;
 }
 
 
@@ -2651,7 +2687,9 @@ MacroData const * Buffer::Impl::getBufferMacro(docstring const & name,
                        break;
 
                // scope ends behind pos?
-               if (pos < it->second.first) {
+               if (pos < it->second.first
+                       && (cloned_buffer_ ||
+                           theBufferList().isLoaded(it->second.second))) {
                        // look for macro in external file
                        macro_lock = true;
                        MacroData const * data
@@ -3013,22 +3051,23 @@ void Buffer::changeRefsIfUnique(docstring const & from, docstring const & to,
 {
        //FIXME: This does not work for child documents yet.
        LASSERT(code == CITE_CODE, /**/);
+
+       reloadBibInfoCache();
+
        // 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();
+       vector<docstring> labels;
 
        for (; bit != bend; ++bit)
                // FIXME UNICODE
                labels.push_back(bit->first);
-       paramName = "key";
 
        if (count(labels.begin(), labels.end(), from) > 1)
                return;
 
+       string const paramName = "key";
        for (InsetIterator it = inset_iterator_begin(inset()); it; ++it) {
                if (it->lyxCode() == code) {
                        InsetCommand * inset = it->asInsetCommand();
@@ -3042,13 +3081,13 @@ void Buffer::changeRefsIfUnique(docstring const & from, docstring const & to,
 }
 
 
-void Buffer::getSourceCode(odocstream & os, pit_type par_begin,
-       pit_type par_end, bool full_source) const
+void Buffer::getSourceCode(odocstream & os, string const format,
+                          pit_type par_begin, pit_type par_end,
+                          bool full_source) const
 {
        OutputParams runparams(&params().encoding());
        runparams.nice = true;
-       runparams.flavor = params().useNonTeXFonts ? 
-               OutputParams::XETEX : OutputParams::LATEX;
+       runparams.flavor = getOutputFlavor(format);
        runparams.linelen = lyxrc.plaintext_linelen;
        // No side effect of file copying and image conversion
        runparams.dryrun = true;
@@ -3060,6 +3099,8 @@ void Buffer::getSourceCode(odocstream & os, pit_type par_begin,
                d->texrow.newline();
                if (isDocBook())
                        writeDocBookSource(os, absFileName(), runparams, false);
+               else if (runparams.flavor == OutputParams::HTML)
+                       writeLyXHTMLSource(os, runparams, false);
                else
                        // latex or literate
                        writeLaTeXSource(os, string(), runparams, true, true);
@@ -3084,7 +3125,10 @@ void Buffer::getSourceCode(odocstream & os, pit_type par_begin,
                // output paragraphs
                if (isDocBook())
                        docbookParagraphs(text(), *this, os, runparams);
-               else 
+               else if (runparams.flavor == OutputParams::HTML) {
+                       XHTMLStream xs(os);
+                       xhtmlParagraphs(text(), *this, xs, runparams);
+               } else 
                        // latex or literate
                        latexParagraphs(*this, text(), os, texrow, runparams);
        }
@@ -3355,6 +3399,43 @@ string Buffer::getDefaultOutputFormat() const
 }
 
 
+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   {
@@ -3398,6 +3479,7 @@ bool Buffer::doExport(string const & format, bool put_in_tempdir,
        vector<string> backs = backends();
        if (find(backs.begin(), backs.end(), format) == backs.end()) {
                // Get shortest path to format
+               theConverters().buildGraph();
                Graph::EdgePath path;
                for (vector<string>::const_iterator it = backs.begin();
                     it != backs.end(); ++it) {
@@ -3680,7 +3762,7 @@ Buffer::ReadStatus Buffer::loadEmergency()
                if (success) {
                        if (isReadonly()) {
                                Alert::warning(_("File is read-only"),
-                                       bformat(_("An emergency file is succesfully loaded, "
+                                       bformat(_("An emergency file is successfully loaded, "
                                        "but the original file %1$s is marked read-only. "
                                        "Please make sure to save the document as a different "
                                        "file."), from_utf8(d->filename.absFileName())));
@@ -3742,7 +3824,7 @@ Buffer::ReadStatus Buffer::loadAutosave()
                if (ret_llf == ReadSuccess) {
                        if (isReadonly()) {
                                Alert::warning(_("File is read-only"),
-                                       bformat(_("A backup file is succesfully loaded,  "
+                                       bformat(_("A backup file is successfully loaded, "
                                        "but the original file %1$s is marked read-only. "
                                        "Please make sure to save the document as a "
                                        "different file."), 
@@ -3828,7 +3910,7 @@ void Buffer::updateBuffer(UpdateScope scope, UpdateType utype) const
        
        // do this only if we are the top-level Buffer
        if (master == this)
-               checkBibInfoCache();
+               reloadBibInfoCache();
 
        // 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.
@@ -3872,6 +3954,7 @@ void Buffer::updateBuffer(UpdateScope scope, UpdateType utype) const
                // TocBackend update will be done later.
                return;
 
+       d->bibinfo_cache_valid_ = true;
        cbuf.tocBackend().update();
        if (scope == UpdateMaster)
                cbuf.structureChanged();
@@ -4168,6 +4251,7 @@ Buffer::ReadStatus Buffer::reload()
                updateTitles();
                markClean();
                message(bformat(_("Document %1$s reloaded."), disp_fn));
+               d->undo_.clear();
        } else {
                message(bformat(_("Could not reload document %1$s."), disp_fn));
        }