]> git.lyx.org Git - lyx.git/blobdiff - src/Buffer.cpp
Compile fix.
[lyx.git] / src / Buffer.cpp
index e121ce429f8470956cccc887f3a543dc53bc8792..877329b4aff184e490b4f5a032ef962d37188c64 100644 (file)
@@ -835,22 +835,24 @@ bool Buffer::readString(string const & s)
        Lexer lex;
        istringstream is(s);
        lex.setStream(is);
-       FileName const name = FileName::tempName("Buffer_readString");
-       switch (readFile(lex, name, true)) {
-       case ReadFailure:
-               return false;
+       FileName const fn = FileName::tempName("Buffer_readString");
+
+       int file_format;
+       ReadStatus const ret_plf = parseLyXFormat(lex, fn, file_format);
+       if (ret_plf != ReadSuccess)
+               return ret_plf;
 
-       case ReadWrongVersion: {
+       if (file_format != LYX_FORMAT) {
                // We need to call lyx2lyx, so write the input to a file
-               ofstream os(name.toFilesystemEncoding().c_str());
+               ofstream os(fn.toFilesystemEncoding().c_str());
                os << s;
                os.close();
-               return readFile(name);
-       }
-       default:
-               break;
+               // lyxvc in readFile
+               return readFile(fn) == ReadSuccess;
        }
 
+       if (readDocument(lex))
+               return false;
        return true;
 }
 
@@ -861,10 +863,28 @@ Buffer::ReadStatus Buffer::readFile(FileName const & fn)
        Lexer lex;
        lex.setFile(fname);
 
-       ReadStatus const ret_rf = readFile(lex, fname);
-       if (ret_rf != ReadSuccess)
-               return ret_rf;
+       int file_format;
+       ReadStatus const ret_plf = parseLyXFormat(lex, fn, file_format);
+       if (ret_plf != ReadSuccess)
+               return ret_plf;
+
+       if (file_format != LYX_FORMAT) {
+               FileName tmpFile;
+               ReadStatus const ret_clf = convertLyXFormat(fn, tmpFile, file_format);
+               if (ret_clf != ReadSuccess)
+                       return ret_clf;
+               return readFile(tmpFile);
+       }
 
+       if (readDocument(lex)) {
+               Alert::error(_("Document format failure"),
+                       bformat(_("%1$s ended unexpectedly, which means"
+                               " that it is probably corrupted."),
+                                       from_utf8(fn.absFileName())));
+               return ReadDocumentFailure;
+       }
+
+       d->file_fully_loaded = true;
        // InsetInfo needs to know if file is under VCS
        lyxvc().file_found_hook(fn);
        d->read_only = !fname.isWritable();
@@ -885,110 +905,86 @@ void Buffer::setFullyLoaded(bool value)
 }
 
 
-Buffer::ReadStatus Buffer::readFile(Lexer & lex, FileName const & filename,
-               bool fromstring)
+Buffer::ReadStatus Buffer::parseLyXFormat(Lexer & lex,
+       FileName const & fn, int & file_format) const
 {
-       LASSERT(!filename.empty(), /**/);
-
-       // the first (non-comment) token _must_ be...
-       if (!lex.checkFor("\\lyxformat")) {
+       if(!lex.checkFor("\\lyxformat")) {
                Alert::error(_("Document format failure"),
-                            bformat(_("%1$s is not a readable LyX document."),
-                                      from_utf8(filename.absFileName())));
-               return ReadFailure;
-       }
+                       bformat(_("%1$s is not a readable LyX document."),
+                               from_utf8(fn.absFileName())));
+               return ReadNoLyXFormat;
+       }       
 
        string tmp_format;
        lex >> tmp_format;
-       //lyxerr << "LyX Format: `" << tmp_format << '\'' << endl;
-       // if present remove ".," from string.
+
+       // LyX formats 217 and earlier were written as 2.17. This corresponds
+       // to files from LyX versions < 1.1.6.3. We just remove the dot in
+       // these cases. See also: www.lyx.org/trac/changeset/1313.
        size_t dot = tmp_format.find_first_of(".,");
-       //lyxerr << "           dot found at " << dot << endl;
        if (dot != string::npos)
-                       tmp_format.erase(dot, 1);
-       int const file_format = convert<int>(tmp_format);
-       //lyxerr << "format: " << file_format << endl;
-
-       // save timestamp and checksum of the original disk file, making sure
-       // to not overwrite them with those of the file created in the tempdir
-       // when it has to be converted to the current format.
-       if (!d->checksum_) {
-               // Save the timestamp and checksum of disk file. If filename is an
-               // emergency file, save the timestamp and checksum of the original lyx file
-               // because isExternallyModified will check for this file. (BUG4193)
-               string diskfile = filename.absFileName();
-               if (suffixIs(diskfile, ".emergency"))
-                       diskfile = diskfile.substr(0, diskfile.size() - 10);
-               saveCheckSum(FileName(diskfile));
-       }
+               tmp_format.erase(dot, 1);
 
-       if (file_format != LYX_FORMAT) {
+       file_format = convert<int>(tmp_format);
+       return ReadSuccess;
+}
 
-               if (fromstring)
-                       // lyx2lyx would fail
-                       return ReadWrongVersion;
-
-               FileName const tmpfile = FileName::tempName("Buffer_readFile");
-               if (tmpfile.empty()) {
-                       Alert::error(_("Conversion failed"),
-                                    bformat(_("%1$s is from a different"
-                                             " version of LyX, but a temporary"
-                                             " file for converting it could"
-                                             " not be created."),
-                                             from_utf8(filename.absFileName())));
-                       return ReadFailure;
-               }
-               FileName const lyx2lyx = libFileSearch("lyx2lyx", "lyx2lyx");
-               if (lyx2lyx.empty()) {
-                       Alert::error(_("Conversion script not found"),
-                                    bformat(_("%1$s is from a different"
-                                              " version of LyX, but the"
-                                              " conversion script lyx2lyx"
-                                              " could not be found."),
-                                              from_utf8(filename.absFileName())));
-                       return ReadFailure;
-               }
-               ostringstream command;
-               command << os::python()
-                       << ' ' << quoteName(lyx2lyx.toFilesystemEncoding())
-                       << " -t " << convert<string>(LYX_FORMAT)
-                       << " -o " << quoteName(tmpfile.toFilesystemEncoding())
-                       << ' ' << quoteName(filename.toSafeFilesystemEncoding());
-               string const command_str = command.str();
-
-               LYXERR(Debug::INFO, "Running '" << command_str << '\'');
-
-               cmd_ret const ret = runCommand(command_str);
-               if (ret.first != 0) {
-                       if (file_format < LYX_FORMAT)
-                               Alert::error(_("Conversion script failed"),
-                                    bformat(_("%1$s is from an older version"
-                                             " of LyX, but the lyx2lyx script"
-                                             " failed to convert it."),
-                                             from_utf8(filename.absFileName())));
-                       else
-                               Alert::error(_("Conversion script failed"),
-                                    bformat(_("%1$s is from a newer version"
-                                             " of LyX and cannot be converted by the"
-                                                               " lyx2lyx script."),
-                                             from_utf8(filename.absFileName())));
-                       return ReadFailure;
-               } else {
-                       // Do stuff with tmpfile name and buffer name here.
-                       return readFile(tmpfile);
-               }
 
+Buffer::ReadStatus Buffer::convertLyXFormat(FileName const & fn, 
+       FileName & tmpfile, int from_format)
+{
+       tmpfile = FileName::tempName("Buffer_convertLyXFormat");
+       if(tmpfile.empty()) {
+               Alert::error(_("Conversion failed"),
+                       bformat(_("%1$s is from a different"
+                               " version of LyX, but a temporary"
+                               " file for converting it could"
+                               " not be created."),
+                               from_utf8(fn.absFileName())));
+               return LyX2LyXNoTempFile;
        }
 
-       if (readDocument(lex)) {
-               Alert::error(_("Document format failure"),
-                            bformat(_("%1$s ended unexpectedly, which means"
-                                                   " that it is probably corrupted."),
-                                      from_utf8(filename.absFileName())));
-               return ReadFailure;
+       FileName const lyx2lyx = libFileSearch("lyx2lyx", "lyx2lyx");
+       if (lyx2lyx.empty()) {
+               Alert::error(_("Conversion script not found"),
+                    bformat(_("%1$s is from a different"
+                              " version of LyX, but the"
+                              " conversion script lyx2lyx"
+                              " could not be found."),
+                              from_utf8(fn.absFileName())));
+               return LyX2LyXNotFound;
        }
 
-       d->file_fully_loaded = true;
+       // Run lyx2lyx:
+       //   $python$ "$lyx2lyx$" -t $LYX_FORMAT$ -o "$tempfile$" "$filetoread$"
+       ostringstream command;
+       command << os::python()
+               << ' ' << quoteName(lyx2lyx.toFilesystemEncoding())
+               << " -t " << convert<string>(LYX_FORMAT)
+               << " -o " << quoteName(tmpfile.toFilesystemEncoding())
+               << ' ' << quoteName(fn.toSafeFilesystemEncoding());
+       string const command_str = command.str();
+
+       LYXERR(Debug::INFO, "Running '" << command_str << '\'');
+
+       cmd_ret const ret = runCommand(command_str);
+       if (ret.first != 0) {
+               if (from_format < LYX_FORMAT) {
+                       Alert::error(_("Conversion script failed"),
+                               bformat(_("%1$s is from an older version"
+                                       " of LyX, but the lyx2lyx script"
+                                       " failed to convert it."),
+                                       from_utf8(fn.absFileName())));
+                       return LyX2LyXOlderFormat;
+               } else {
+                       Alert::error(_("Conversion script failed"),
+                               bformat(_("%1$s is from an newer version"
+                                       " of LyX, but the lyx2lyx script"
+                                       " failed to convert it."),
+                                       from_utf8(fn.absFileName())));
+                       return LyX2LyXNewerFormat;
+               }
+       }
        return ReadSuccess;
 }
 
@@ -3629,39 +3625,45 @@ 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))
+               ReadStatus const ret_llf = loadThisLyXFile(emergencyFile);
+               bool const success = (ret_llf == ReadSuccess);
+               if (success) {
+                       saveCheckSum(fn);
+                       markDirty();
                        str = _("Document was successfully recovered.");
-               else
+               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;
@@ -3687,10 +3689,11 @@ 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;
@@ -3722,6 +3725,12 @@ Buffer::ReadStatus Buffer::loadLyXFile(FileName const & fn)
        if (ret_ra == ReadSuccess || ret_ra == ReadCancel)
                return ret_ra;
 
+       return loadThisLyXFile(fn);
+}
+
+
+Buffer::ReadStatus Buffer::loadThisLyXFile(FileName const & fn)
+{
        return readFile(fn);
 }