]> git.lyx.org Git - lyx.git/blobdiff - src/Buffer.cpp
InsetTabular.cpp: multirows inherit the width and the alignment from the column;...
[lyx.git] / src / Buffer.cpp
index 5b7cc8219a3a471c3a511b1326fe56b3ddbb1846..5bd73da715315da24f5f00effdb0757f195ba092 100644 (file)
@@ -414,9 +414,8 @@ Buffer::~Buffer()
                        from_utf8(d->temppath.absFileName())));
        }
 
-       // Remove any previewed LaTeX snippets associated with this buffer.
        if (!isClone())
-               thePreviews().removeLoader(*this);
+               removePreviews();
 
        delete d;
 }
@@ -889,6 +888,7 @@ Buffer::ReadStatus Buffer::readFile(FileName const & fn)
        lyxvc().file_found_hook(fn);
        d->read_only = !fname.isWritable();
        params().compressed = fname.isZippedFile();
+       saveCheckSum();
        return ReadSuccess;
 }
 
@@ -905,6 +905,19 @@ void Buffer::setFullyLoaded(bool value)
 }
 
 
+void Buffer::updatePreviews() const
+{
+       if (graphics::Previews::status() != LyXRC::PREVIEW_OFF)
+               thePreviews().generateBufferPreviews(*this);
+}
+
+
+void Buffer::removePreviews() const
+{
+       thePreviews().removeLoader(*this);
+}
+
+
 Buffer::ReadStatus Buffer::parseLyXFormat(Lexer & lex,
        FileName const & fn, int & file_format) const
 {
@@ -1071,7 +1084,7 @@ bool Buffer::writeFile(FileName const & fname) const
        // see bug 6587
        // removeAutosaveFile();
 
-       saveCheckSum(d->filename);
+       saveCheckSum();
        message(str + _(" done."));
 
        return true;
@@ -1707,15 +1720,13 @@ void Buffer::updateBibfilesCache(UpdateScope scope) const
        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);
+                       InsetBibtex const & inset = static_cast<InsetBibtex const &>(*it);
                        support::FileNameList const bibfiles = inset.getBibFiles();
                        d->bibfiles_cache_.insert(d->bibfiles_cache_.end(),
                                bibfiles.begin(),
                                bibfiles.end());
                } else if (it->lyxCode() == INCLUDE_CODE) {
-                       InsetInclude & inset =
-                               static_cast<InsetInclude &>(*it);
+                       InsetInclude & inset = static_cast<InsetInclude &>(*it);
                        Buffer const * const incbuf = inset.getChildBuffer();
                        if (!incbuf)
                                continue;
@@ -2085,7 +2096,7 @@ void Buffer::dispatch(FuncRequest const & func, DispatchResult & dr)
                bool success = false;
                for (; it != end; ++it) {
                        if (it->lyxCode() == BRANCH_CODE) {
-                               InsetBranch & ins = dynamic_cast<InsetBranch &>(*it);
+                               InsetBranch & ins = static_cast<InsetBranch &>(*it);
                                if (ins.branch() == oldname) {
                                        undo().recordUndo(it);
                                        ins.rename(newname);
@@ -2371,8 +2382,9 @@ bool Buffer::isExternallyModified(CheckMethod method) const
 }
 
 
-void Buffer::saveCheckSum(FileName const & file) const
+void Buffer::saveCheckSum() const
 {
+       FileName const & file = d->filename;
        if (file.exists()) {
                d->timestamp_ = file.lastModified();
                d->checksum_ = file.checksum();
@@ -2762,12 +2774,11 @@ void Buffer::Impl::updateMacros(DocIterator & it, DocIterator & scope)
                                continue;
                        }
 
-                       if (doing_export && iit->inset->asInsetMath()) {
-                               InsetMath * im = static_cast<InsetMath *>(iit->inset);
-                               if (im->asHullInset()) {
-                                       InsetMathHull * hull = static_cast<InsetMathHull *>(im);
+                       InsetMath * im = iit->inset->asInsetMath();
+                       if (doing_export && im)  {
+                               InsetMathHull * hull = im->asHullInset();
+                               if (hull)
                                        hull->recordLocation(it);
-                               }
                        }
 
                        if (iit->inset->lyxCode() != MATHMACRO_CODE)
@@ -2775,7 +2786,7 @@ void Buffer::Impl::updateMacros(DocIterator & it, DocIterator & scope)
 
                        // get macro data
                        MathMacroTemplate & macroTemplate =
-                               static_cast<MathMacroTemplate &>(*iit->inset);
+                               *iit->inset->asInsetMath()->asMacroTemplate();
                        MacroContext mc(owner_, it);
                        macroTemplate.updateToContext(mc);
 
@@ -2830,7 +2841,7 @@ void Buffer::getUsedBranches(std::list<docstring> & result, bool const from_mast
        InsetIterator const end = inset_iterator_end(inset());
        for (; it != end; ++it) {
                if (it->lyxCode() == BRANCH_CODE) {
-                       InsetBranch & br = dynamic_cast<InsetBranch &>(*it);
+                       InsetBranch & br = static_cast<InsetBranch &>(*it);
                        docstring const name = br.branch();
                        if (!from_master && !params().branchlist().find(name))
                                result.push_back(name);
@@ -2999,10 +3010,12 @@ void Buffer::changeRefsIfUnique(docstring const & from, docstring const & to,
 
        for (InsetIterator it = inset_iterator_begin(inset()); it; ++it) {
                if (it->lyxCode() == code) {
-                       InsetCommand & inset = static_cast<InsetCommand &>(*it);
-                       docstring const oldValue = inset.getParam(paramName);
+                       InsetCommand * inset = it->asInsetCommand();
+                       if (!inset)
+                               continue;
+                       docstring const oldValue = inset->getParam(paramName);
                        if (oldValue == from)
-                               inset.setParam(paramName, to);
+                               inset->setParam(paramName, to);
                }
        }
 }
@@ -3604,7 +3617,7 @@ vector<string> Buffer::backends() const
 }
 
 
-Buffer::ReadStatus Buffer::readFromVC(FileName const & fn)
+Buffer::ReadStatus Buffer::extractFromVC(FileName const & fn)
 {
        bool const found = LyXVC::file_not_found_hook(fn);
        if (!found)
@@ -3615,7 +3628,7 @@ Buffer::ReadStatus Buffer::readFromVC(FileName const & fn)
 }
 
 
-Buffer::ReadStatus Buffer::readEmergency(FileName const & fn)
+Buffer::ReadStatus Buffer::loadEmergency(FileName const & fn)
 {
        FileName const emergencyFile = getEmergencyFileNameFor(fn);
        if (!emergencyFile.exists() 
@@ -3625,40 +3638,44 @@ Buffer::ReadStatus Buffer::readEmergency(FileName const & fn)
        docstring const file = makeDisplayPath(fn.absFileName(), 20);
        docstring const text = bformat(_("An emergency save of the document "
                "%1$s exists.\n\nRecover emergency save?"), file);
-       int const ret = Alert::prompt(_("Load emergency save?"), text,
+       
+       int const load_emerg = Alert::prompt(_("Load emergency save?"), text,
                0, 2, _("&Recover"), _("&Load Original"), _("&Cancel"));
 
-       switch (ret)
+       switch (load_emerg)
        {
        case 0: {
-               // the file is not saved if we load the emergency file.
-               markDirty();
                docstring str;
-               bool res;
-               ReadStatus const ret_rf = readFile(emergencyFile);
-               if (res = (ret_rf == ReadSuccess)) {
-                       saveCheckSum(fn);
+               ReadStatus const ret_llf = loadThisLyXFile(emergencyFile);
+               bool const success = (ret_llf == ReadSuccess);
+               if (success) {
+                       markDirty();
                        str = _("Document was successfully recovered.");
                } else
                        str = _("Document was NOT successfully recovered.");
                str += "\n\n" + bformat(_("Remove emergency file now?\n(%1$s)"),
                                        makeDisplayPath(emergencyFile.absFileName()));
 
-               if (!Alert::prompt(_("Delete emergency file?"), str, 1, 1,
-                               _("&Remove"), _("&Keep"))) {
+               int const del_emerg = 
+                       Alert::prompt(_("Delete emergency file?"), str, 1, 1,
+                               _("&Remove"), _("&Keep"));
+               if (del_emerg == 0) {
                        emergencyFile.removeFile();
-                       if (res)
+                       if (success)
                                Alert::warning(_("Emergency file deleted"),
                                        _("Do not forget to save your file now!"), true);
                        }
-               return res ? ReadSuccess : ReadEmergencyFailure;
+               return success ? ReadSuccess : ReadEmergencyFailure;
        }
-       case 1:
-               if (!Alert::prompt(_("Delete emergency file?"),
+       case 1: {
+               int const del_emerg =
+                       Alert::prompt(_("Delete emergency file?"),
                                _("Remove emergency file now?"), 1, 1,
-                               _("&Remove"), _("&Keep")))
+                               _("&Remove"), _("&Keep"));
+               if (del_emerg == 0)
                        emergencyFile.removeFile();
                return ReadOriginal;
+       }
 
        default:
                break;
@@ -3667,7 +3684,7 @@ Buffer::ReadStatus Buffer::readEmergency(FileName const & fn)
 }
 
 
-Buffer::ReadStatus Buffer::readAutosave(FileName const & fn)
+Buffer::ReadStatus Buffer::loadAutosave(FileName const & fn)
 {
        // Now check if autosave file is newer.
        FileName const autosaveFile = getAutosaveFileNameFor(fn);
@@ -3684,11 +3701,10 @@ Buffer::ReadStatus Buffer::readAutosave(FileName const & fn)
        switch (ret)
        {
        case 0: {
-               ReadStatus const ret_rf = readFile(autosaveFile);
+               ReadStatus const ret_llf = loadThisLyXFile(autosaveFile);
                // the file is not saved if we load the autosave file.
-               if (ret_rf == ReadSuccess) {
+               if (ret_llf == ReadSuccess) {
                        markDirty();
-                       saveCheckSum(fn);
                        return ReadSuccess;
                }
                return ReadAutosaveFailure;
@@ -3707,19 +3723,25 @@ Buffer::ReadStatus Buffer::readAutosave(FileName const & fn)
 Buffer::ReadStatus Buffer::loadLyXFile(FileName const & fn)
 {
        if (!fn.isReadableFile()) {
-               ReadStatus const ret_rvc = readFromVC(fn);
+               ReadStatus const ret_rvc = extractFromVC(fn);
                if (ret_rvc != ReadSuccess)
                        return ret_rvc;
        }
 
-       ReadStatus const ret_re = readEmergency(fn);
+       ReadStatus const ret_re = loadEmergency(fn);
        if (ret_re == ReadSuccess || ret_re == ReadCancel)
                return ret_re;
        
-       ReadStatus const ret_ra = readAutosave(fn);
+       ReadStatus const ret_ra = loadAutosave(fn);
        if (ret_ra == ReadSuccess || ret_ra == ReadCancel)
                return ret_ra;
 
+       return loadThisLyXFile(fn);
+}
+
+
+Buffer::ReadStatus Buffer::loadThisLyXFile(FileName const & fn)
+{
        return readFile(fn);
 }
 
@@ -4086,7 +4108,7 @@ int Buffer::spellCheck(DocIterator & from, DocIterator & to,
 }
 
 
-bool Buffer::reload()
+Buffer::ReadStatus Buffer::reload()
 {
        setBusy(true);
        // c.f. bug 6587
@@ -4095,23 +4117,21 @@ bool Buffer::reload()
        d->filename.refresh();
        docstring const disp_fn = makeDisplayPath(d->filename.absFileName());
 
-       bool const success = (loadLyXFile(d->filename) == ReadSuccess);
-       if (success) {
+       ReadStatus const status = loadLyXFile(d->filename);
+       if (status == ReadSuccess) {
                updateBuffer();
                changed(true);
                updateTitles();
                markClean();
-               saveCheckSum(d->filename);
                message(bformat(_("Document %1$s reloaded."), disp_fn));
        } else {
                message(bformat(_("Could not reload document %1$s."), disp_fn));
        }       
        setBusy(false);
-       thePreviews().removeLoader(*this);
-       if (graphics::Previews::status() != LyXRC::PREVIEW_OFF)
-               thePreviews().generateBufferPreviews(*this);
+       removePreviews();
+       updatePreviews();
        errors("Parse");
-       return success;
+       return status;
 }