bool active;
};
-typedef vector<LabelInfo> RefCache;
+typedef vector<LabelInfo> LabelCache;
+
+typedef map<docstring, Buffer::References> RefCache;
} // namespace
/// we ran updateBuffer(), i.e., whether citation labels may need
/// to be updated.
mutable bool cite_labels_valid_;
+ /// Do we have a bibliography environment?
+ mutable bool have_bibitems_;
/// These two hold the file name and format, written to by
/// Buffer::preview and read from by LFUN_BUFFER_VIEW_CACHE.
/// was missing).
bool preview_error_;
- /// Cache the label insets, their associated refs (with positions),
- /// and whether the insets are active.
+ /// Cache the references associated to a label and their positions
+ /// in the buffer.
mutable RefCache ref_cache_;
+ /// Cache the label insets and their activity status.
+ mutable LabelCache label_cache_;
/// our Text that should be wrapped in an InsetText
InsetText * inset;
file_fully_loaded(false), file_format(LYX_FORMAT), need_format_backup(false),
ignore_parent(false), toc_backend(owner), macro_lock(false),
checksum_(0), wa_(0), gui_(0), undo_(*owner), bibinfo_cache_valid_(false),
- cite_labels_valid_(false), preview_error_(false),
+ cite_labels_valid_(false), have_bibitems_(false), preview_error_(false),
inset(0), preview_loader_(0), cloned_buffer_(cloned_buffer),
clone_list_(0), doing_export(false),
tracked_changes_present_(0), externally_modified_(false), parent_buffer(0),
bibinfo_cache_valid_ = cloned_buffer_->d->bibinfo_cache_valid_;
bibfile_status_ = cloned_buffer_->d->bibfile_status_;
cite_labels_valid_ = cloned_buffer_->d->cite_labels_valid_;
+ have_bibitems_ = cloned_buffer_->d->have_bibitems_;
unnamed = cloned_buffer_->d->unnamed;
internal_buffer = cloned_buffer_->d->internal_buffer;
layout_position = cloned_buffer_->d->layout_position;
if (!d->bibinfo_cache_valid_)
return;
- // we'll assume it's ok and change this if it's not
+ if (d->have_bibitems_) {
+ // We have a bibliography environment.
+ // Invalidate the bibinfo cache unconditionally.
+ // Cite labels will get invalidated by the inset if needed.
+ d->bibinfo_cache_valid_ = false;
+ return;
+ }
+
+ // OK. This is with Bib(la)tex. We'll assume the cache
+ // is valid and change this if we find changes in the bibs.
d->bibinfo_cache_valid_ = true;
d->cite_labels_valid_ = true;
clearBibFileCache();
d->bibinfo_.clear();
FileNameList checkedFiles;
+ d->have_bibitems_ = false;
collectBibKeys(checkedFiles);
d->bibinfo_cache_valid_ = true;
}
void Buffer::collectBibKeys(FileNameList & checkedFiles) const
{
- for (InsetIterator it = inset_iterator_begin(inset()); it; ++it)
+ for (InsetIterator it = inset_iterator_begin(inset()); it; ++it) {
it->collectBibKeys(it, checkedFiles);
+ if (it->lyxCode() == BIBITEM_CODE) {
+ if (parent() != 0)
+ parent()->d->have_bibitems_ = true;
+ else
+ d->have_bibitems_ = true;
+ }
+ }
}
if (d->parent())
return const_cast<Buffer *>(masterBuffer())->getReferenceCache(label);
- // In what follows, we look whether we find an active label
- // with the given string in the cache. If so, return its
- // references cache. If we only find an inactive one, return
- // that (the last we find, coincidentally). If we find nothing,
- // return an empty references cache.
- static LabelInfo linfo;
- linfo.inset = nullptr;
- linfo.references = References();
- linfo.active = false;
- bool have_inactive = false;
- for (auto & rc : d->ref_cache_) {
- if (rc.label == label) {
- if (rc.active)
- return rc.references;
- else {
- linfo = rc;
- have_inactive = true;
- }
- }
- }
-
- if (!have_inactive)
- // We found nothing, so insert the empty one to the cache
- // for further processing
- d->ref_cache_.push_back(linfo);
+ RefCache::iterator it = d->ref_cache_.find(label);
+ if (it != d->ref_cache_.end())
+ return it->second;
- return linfo.references;
+ static References const dummy_refs = References();
+ it = d->ref_cache_.insert(
+ make_pair(label, dummy_refs)).first;
+ return it->second;
}
linfo.label = label;
linfo.inset = il;
linfo.active = active;
- masterBuffer()->d->ref_cache_.push_back(linfo);
+ masterBuffer()->d->label_cache_.push_back(linfo);
}
InsetLabel const * Buffer::insetLabel(docstring const & label,
bool const active) const
{
- for (auto & rc : masterBuffer()->d->ref_cache_) {
+ for (auto & rc : masterBuffer()->d->label_cache_) {
if (rc.label == label && (rc.active || !active))
return rc.inset;
}
void Buffer::clearReferenceCache() const
{
- if (!d->parent())
+ if (!d->parent()) {
d->ref_cache_.clear();
+ d->label_cache_.clear();
+ }
}
_("&Remove"), _("&Keep"));
if (del_emerg == 0)
emergencyFile.removeFile();
+ else {
+ // See bug #11464
+ FileName newname;
+ string const ename = emergencyFile.absFileName();
+ bool noname = true;
+ // Surely we can find one in 100 tries?
+ for (int i = 1; i < 100; ++i) {
+ newname.set(ename + to_string(i) + ".lyx");
+ if (!newname.exists()) {
+ noname = false;
+ break;
+ }
+ }
+ if (!noname) {
+ // renameTo returns true on success. So inverting that
+ // will give us true if we fail.
+ noname = !emergencyFile.renameTo(newname);
+ }
+ if (noname) {
+ Alert::warning(_("Can't rename emergency file!"),
+ _("LyX was unable to rename the emergency file. "
+ "You should do so manually. Otherwise, you will be "
+ "asked about it again the next time you try to load "
+ "this file, and may over-write your own work."));
+ }
+ }
return ReadOriginal;
}