X-Git-Url: https://git.lyx.org/gitweb/?a=blobdiff_plain;ds=sidebyside;f=src%2Fbuffer_funcs.cpp;h=1f1cd7917c1c6c8490b875be1af000d27d1e990f;hb=559c2f648239fff4fec769b34b30ccff23491e31;hp=aa15b8963f2819a2e0c90a04d6db7d8bf8766e7e;hpb=f630be890494c849981e4fb52ea4740506e92bed;p=lyx.git diff --git a/src/buffer_funcs.cpp b/src/buffer_funcs.cpp index aa15b8963f..1f1cd7917c 100644 --- a/src/buffer_funcs.cpp +++ b/src/buffer_funcs.cpp @@ -24,7 +24,7 @@ #include "gettext.h" #include "Language.h" #include "LaTeX.h" -#include "LyXTextClass.h" +#include "TextClass.h" #include "Paragraph.h" #include "paragraph_funcs.h" #include "ParagraphList.h" @@ -35,7 +35,7 @@ #include "TocBackend.h" #include "VCBackend.h" -#include "frontends/Alert.h" +#include "frontends/alert.h" #include "insets/InsetBibitem.h" #include "insets/InsetCaption.h" @@ -49,6 +49,9 @@ #include #include +using std::min; +using std::string; + namespace lyx { @@ -63,9 +66,6 @@ using support::onlyFilename; using support::onlyPath; using support::unlink; -using std::min; -using std::string; - namespace Alert = frontend::Alert; namespace fs = boost::filesystem; @@ -180,6 +180,56 @@ bool loadLyXFile(Buffer * b, FileName const & s) return false; } + +bool checkIfLoaded(FileName const & fn) +{ + return theBufferList().getBuffer(fn.absFilename()); +} + + +Buffer * checkAndLoadLyXFile(FileName const & filename) +{ + // File already open? + Buffer * checkBuffer = theBufferList().getBuffer(filename.absFilename()); + if (checkBuffer) { + if (checkBuffer->isClean()) + return checkBuffer; + docstring const file = makeDisplayPath(filename.absFilename(), 20); + docstring text = bformat(_( + "The document %1$s is already loaded and has unsaved changes.\n" + "Do you want to abandon your changes and reload the version on disk?"), file); + if (Alert::prompt(_("Reload saved document?"), + text, 0, 1, _("&Reload"), _("&Keep Changes"))) + return checkBuffer; + + // FIXME: should be LFUN_REVERT + if (theBufferList().close(checkBuffer, false)) + // Load it again. + return checkAndLoadLyXFile(filename); + else + // The file could not be closed. + return 0; + } + + if (isFileReadable(filename)) { + Buffer * b = theBufferList().newBuffer(filename.absFilename()); + if (!lyx::loadLyXFile(b, filename)) { + theBufferList().release(b); + return 0; + } + return b; + } + + docstring text = bformat(_("The document %1$s does not yet " + "exist.\n\nDo you want to create a new document?"), + from_utf8(filename.absFilename())); + if (!Alert::prompt(_("Create new document?"), + text, 0, 1, _("&Create"), _("Cancel"))) + return newFile(filename.absFilename(), string(), true); + + return 0; +} + // FIXME newFile() should probably be a member method of Application... Buffer * newFile(string const & filename, string const & templatename, bool const isNamed) @@ -352,10 +402,10 @@ bool needEnumCounterReset(ParIterator const & it) } -void setCaptionLabels(InsetBase & inset, string const & type, +void setCaptionLabels(Inset & inset, string const & type, docstring const label, Counters & counters) { - LyXText * text = inset.getText(0); + Text * text = inset.getText(0); if (!text) return; @@ -372,12 +422,12 @@ void setCaptionLabels(InsetBase & inset, string const & type, // Any caption within this float should have the same // label prefix but different numbers. for (; it2 != end2; ++it2) { - InsetBase & icap = *it2->inset; + Inset & icap = *it2->inset; // Look deeper just in case. setCaptionLabels(icap, type, label, counters); - if (icap.lyxCode() == InsetBase::CAPTION_CODE) { + if (icap.lyxCode() == Inset::CAPTION_CODE) { // We found a caption! - counters.step(counter); + counters.step(counter); int number = counters.value(counter); InsetCaption & ic = static_cast(icap); ic.setType(type); @@ -389,7 +439,7 @@ void setCaptionLabels(InsetBase & inset, string const & type, } -void setCaptions(Paragraph & par, LyXTextClass const & textclass) +void setCaptions(Paragraph & par, TextClass const & textclass) { if (par.insetlist.empty()) return; @@ -399,10 +449,10 @@ void setCaptions(Paragraph & par, LyXTextClass const & textclass) InsetList::iterator it = par.insetlist.begin(); InsetList::iterator end = par.insetlist.end(); for (; it != end; ++it) { - InsetBase & inset = *it->inset; - if (inset.lyxCode() == InsetBase::FLOAT_CODE - || inset.lyxCode() == InsetBase::WRAP_CODE) { - docstring const & name = inset.getInsetName(); + Inset & inset = *it->inset; + if (inset.lyxCode() == Inset::FLOAT_CODE + || inset.lyxCode() == Inset::WRAP_CODE) { + docstring const name = inset.name(); if (name.empty()) continue; @@ -412,31 +462,34 @@ void setCaptions(Paragraph & par, LyXTextClass const & textclass) docstring const label = from_utf8(fl.name()); setCaptionLabels(inset, type, label, counters); } - else if (inset.lyxCode() == InsetBase::TABULAR_CODE + else if (inset.lyxCode() == Inset::TABULAR_CODE && static_cast(inset).tabular.isLongTabular()) { // FIXME: are "table" and "Table" the correct type and label? setCaptionLabels(inset, "table", from_ascii("Table"), counters); } + else if (inset.lyxCode() == Inset::LISTINGS_CODE) + setCaptionLabels(inset, "listing", from_ascii("Listing"), counters); + else if (inset.lyxCode() == Inset::INCLUDE_CODE) + // if this include inset contains lstinputlisting, and has a caption + // it will increase the 'listing' counter by one + static_cast(inset).updateCounter(counters); } } // set the label of a paragraph. This includes the counters. -void setLabel(Buffer const & buf, ParIterator & it, LyXTextClass const & textclass) +void setLabel(Buffer const & buf, ParIterator & it, TextClass const & textclass) { Paragraph & par = *it; - LyXLayout_ptr const & layout = par.layout(); + Layout_ptr const & layout = par.layout(); Counters & counters = textclass.counters(); - if (it.pit() == 0) { - par.params().appendix(par.params().startOfAppendix()); - } else { - par.params().appendix(it.plist()[it.pit() - 1].params().appendix()); - if (!par.params().appendix() && - par.params().startOfAppendix()) { - par.params().appendix(true); - textclass.counters().reset(); - } + if (par.params().startOfAppendix()) { + // FIXME: only the counter corresponding to toplevel + // sectionning should be reset + counters.reset(); + counters.appendix(true); } + par.params().appendix(counters.appendix()); // Compute the item depth of the paragraph par.itemdepth = getItemDepth(it); @@ -549,23 +602,23 @@ void setLabel(Buffer const & buf, ParIterator & it, LyXTextClass const & textcla } else if (layout->labeltype == LABEL_SENSITIVE) { // Search for the first float or wrap inset in the iterator size_t i = it.depth(); - InsetBase * in = 0; + Inset * in = 0; while (i > 0) { --i; - InsetBase::Code const code = it[i].inset().lyxCode(); - if (code == InsetBase::FLOAT_CODE || - code == InsetBase::WRAP_CODE) { + Inset::Code const code = it[i].inset().lyxCode(); + if (code == Inset::FLOAT_CODE || + code == Inset::WRAP_CODE) { in = &it[i].inset(); break; } } - // FIXME Can getInsetName() return an empty name for wide or + // FIXME Can Inset::name() return an empty name for wide or // float insets? If not we can put the definition of type // inside the if (in) clause and use that instead of // if (!type.empty()). docstring type; if (in) - type = in->getInsetName(); + type = in->name(); if (!type.empty()) { Floating const & fl = textclass.floats().getType(to_ascii(type)); @@ -592,70 +645,14 @@ void setLabel(Buffer const & buf, ParIterator & it, LyXTextClass const & textcla } // anon namespace -bool updateCurrentLabel(Buffer const & buf, - ParIterator & it) -{ - if (it == par_iterator_end(buf.inset())) - return false; - -// if (it.lastpit == 0 && LyXText::isMainText(buf)) -// return false; - - switch (it->layout()->labeltype) { - - case LABEL_NO_LABEL: - case LABEL_MANUAL: - case LABEL_BIBLIO: - case LABEL_TOP_ENVIRONMENT: - case LABEL_CENTERED_TOP_ENVIRONMENT: - case LABEL_STATIC: - case LABEL_ITEMIZE: - setLabel(buf, it, buf.params().getLyXTextClass()); - return true; - - case LABEL_SENSITIVE: - case LABEL_COUNTER: - // do more things with enumerate later - case LABEL_ENUMERATE: - return false; - } - - // This is dead code which get rid of a warning: - return true; -} - - -void updateLabels(Buffer const & buf, - ParIterator & from, ParIterator & to, bool childonly) -{ - for (ParIterator it = from; it != to; ++it) { - if (it.pit() > it.lastpit()) - return; - if (!updateCurrentLabel (buf, it)) { - updateLabels(buf, childonly); - return; - } - } -} - - -void updateLabels(Buffer const & buf, ParIterator & iter, bool childonly) -{ - if (updateCurrentLabel(buf, iter)) - return; - - updateLabels(buf, childonly); -} - - void updateLabels(Buffer const & buf, bool childonly) { + Buffer const * const master = buf.getMasterBuffer(); // Use the master text class also for child documents - LyXTextClass const & textclass = buf.params().getLyXTextClass(); + TextClass const & textclass = master->params().getTextClass(); if (!childonly) { // If this is a child document start with the master - Buffer const * const master = buf.getMasterBuffer(); if (master != &buf) { updateLabels(*master); return; @@ -688,7 +685,7 @@ void updateLabels(Buffer const & buf, bool childonly) InsetList::const_iterator iit = it->insetlist.begin(); InsetList::const_iterator end = it->insetlist.end(); for (; iit != end; ++iit) { - if (iit->inset->lyxCode() == InsetBase::INCLUDE_CODE) + if (iit->inset->lyxCode() == Inset::INCLUDE_CODE) static_cast(iit->inset) ->updateLabels(buf); } @@ -696,16 +693,17 @@ void updateLabels(Buffer const & buf, bool childonly) Buffer & cbuf = const_cast(buf); cbuf.tocBackend().update(); - cbuf.structureChanged(); + if (!childonly) + cbuf.structureChanged(); } void checkBufferStructure(Buffer & buffer, ParIterator const & par_it) { - if (par_it->layout()->labeltype == LABEL_COUNTER - && par_it->layout()->toclevel != LyXLayout::NOT_IN_TOC) { - buffer.tocBackend().updateItem(par_it); - buffer.structureChanged(); + if (par_it->layout()->toclevel != Layout::NOT_IN_TOC) { + Buffer * master = buffer.getMasterBuffer(); + master->tocBackend().updateItem(par_it); + master->structureChanged(); } }