#include <boost/shared_ptr.hpp>
#include <algorithm>
+#include <fstream>
#include <iomanip>
-#include <stack>
+#include <map>
#include <sstream>
-#include <fstream>
+#include <stack>
+#include <vector>
using namespace std;
using namespace lyx::support;
int const LYX_FORMAT = 316; // JSpitzm: subfig support
-} // namespace anon
-
-
typedef map<string, bool> DepClean;
+typedef map<docstring, pair<InsetLabel const *, Buffer::References> > RefCache;
+
+} // namespace anon
class Buffer::Impl
{
/// A cache for the bibfiles (including bibfiles of loaded child
/// documents), needed for appropriate update of natbib labels.
mutable EmbeddedFileList bibfilesCache_;
+
+ mutable RefCache ref_cache_;
};
/// Creates the per buffer temporary directory
}
+Buffer::References & Buffer::references(docstring const & label)
+{
+ if (d->parent_buffer)
+ return const_cast<Buffer *>(masterBuffer())->references(label);
+
+ RefCache::iterator it = d->ref_cache_.find(label);
+ if (it != d->ref_cache_.end())
+ return it->second.second;
+
+ static InsetLabel const * dummy_il = 0;
+ static References const dummy_refs;
+ it = d->ref_cache_.insert(make_pair(label, make_pair(dummy_il, dummy_refs))).first;
+ return it->second.second;
+}
+
+
+Buffer::References const & Buffer::references(docstring const & label) const
+{
+ return const_cast<Buffer *>(this)->references(label);
+}
+
+
+void Buffer::setInsetLabel(docstring const & label, InsetLabel const * il)
+{
+ masterBuffer()->d->ref_cache_[label].first = il;
+}
+
+
+InsetLabel const * Buffer::insetLabel(docstring const & label) const
+{
+ return masterBuffer()->d->ref_cache_[label].first;
+}
+
+
+void Buffer::clearReferenceCache() const
+{
+ if (!d->parent_buffer)
+ d->ref_cache_.clear();
+}
+
+
void Buffer::changeRefsIfUnique(docstring const & from, docstring const & to,
InsetCode code)
{
resetParentBuffer(this, ip, close_them);
}
- if (use_gui && masterBuffer() == this)
- updateLabels(*this);
-
// clear references to children in macro tables
d->children_positions.clear();
d->position_to_children.clear();
class ErrorList;
class FuncRequest;
class Inset;
+class InsetRef;
+class InsetLabel;
class Font;
class Format;
class Lexer;
bool isExportable(std::string const & format) const;
///
std::vector<Format const *> exportableFormats(bool only_viewable) const;
-
+
+ ///
+ typedef std::vector<std::pair<InsetRef *, ParIterator> > References;
+ References & references(docstring const & label);
+ References const & references(docstring const & label) const;
+ void clearReferenceCache() const;
+ void setInsetLabel(docstring const & label, InsetLabel const * il);
+ InsetLabel const * insetLabel(docstring const & label) const;
+
private:
/// search for macro in local (buffer) table or in children
MacroData const * getBufferMacro(docstring const & name,
// start over the counters
textclass.counters().reset();
+ buf.clearReferenceCache();
}
Buffer & cbuf = const_cast<Buffer &>(buf);
#include "InsetLabel.h"
+#include "InsetRef.h"
+
#include "Buffer.h"
#include "BufferView.h"
#include "DispatchResult.h"
#include "FuncRequest.h"
+#include "InsetIterator.h"
#include "ParIterator.h"
#include "sgml.h"
#include "Text.h"
#include "TocBackend.h"
-#include "support/lstrings.h"
+#include "frontends/alert.h"
+
#include "support/lyxalgo.h"
+#include "support/gettext.h"
+#include "support/lstrings.h"
using namespace std;
using namespace lyx::support;
}
+void InsetLabel::updateLabels(ParIterator const & it)
+{
+ docstring const & label = getParam("name");
+ if (buffer().insetLabel(label))
+ // Problem: We already have an InsetLabel with the same name!
+ return;
+ buffer().setInsetLabel(label, this);
+}
+
+
void InsetLabel::addToToc(ParConstIterator const & cpit) const
{
+ docstring const & label = getParam("name");
Toc & toc = buffer().tocBackend().toc("label");
- toc.push_back(TocItem(cpit, 0, screenLabel()));
+ if (buffer().insetLabel(label) != this) {
+ toc.push_back(TocItem(cpit, 0, _("DUPLICATE: ") + label));
+ return;
+ }
+ toc.push_back(TocItem(cpit, 0, label));
+ Buffer::References const & refs = buffer().references(label);
+ Buffer::References::const_iterator it = refs.begin();
+ Buffer::References::const_iterator end = refs.end();
+ for (; it != end; ++it) {
+ ParConstIterator const ref_pit(it->second);
+ toc.push_back(TocItem(ref_pit, 1, it->first->screenLabel()));
+ }
}
static bool isCompatibleCommand(std::string const & s)
{ return s == "label"; }
///
+ void updateLabels(ParIterator const & it);
+ ///
void addToToc(ParConstIterator const &) const;
protected:
///
#include "support/gettext.h"
#include "support/lstrings.h"
-using namespace std;
using namespace lyx::support;
+using namespace std;
namespace lyx {
}
-void InsetRef::addToToc(ParConstIterator const & cpit) const
+void InsetRef::updateLabels(ParIterator const & it)
{
docstring const & label = getParam("reference");
- Toc & toc = buffer().tocBackend().toc("label");
- Toc::iterator it = toc.begin();
- Toc::iterator end = toc.end();
- for (; it != end; ++it) {
- if (it->str() == label)
- break;
- }
+ buffer().references(label).push_back(make_pair(this, it));
+}
- docstring const reflabel = screenLabel();
- if (it == end) {
- // This label has not been parsed yet so we just add it temporarily.
- // InsetLabel::addTocToc() will fix that later.
- toc.push_back(TocItem(cpit, 0, label));
- toc.push_back(TocItem(cpit, 1, reflabel));
+
+void InsetRef::addToToc(ParConstIterator const & cpit) const
+{
+ docstring const & label = getParam("reference");
+ if (buffer().insetLabel(label))
+ // This InsetRef has already been taken care of in InsetLabel::addToToc().
return;
- }
- // The Toc item for this label already exists so let's add
- // this inset to this node.
- ++it;
- while (it != end && it->str() == reflabel)
- ++it;
- toc.insert(it, TocItem(cpit, 1, reflabel));
+ Toc & toc = buffer().tocBackend().toc("label");
+ docstring const reflabel = _("BROKEN: ") + screenLabel();
+ toc.push_back(TocItem(cpit, 0, reflabel));
}
///
static bool isCompatibleCommand(std::string const & s);
///
+ void updateLabels(ParIterator const & it);
+ ///
void addToToc(ParConstIterator const &) const;
protected:
///