]> git.lyx.org Git - lyx.git/blobdiff - src/buffer.C
mathed31.diff
[lyx.git] / src / buffer.C
index 96eb02eb4ea6e40f3d138628d43731b9753998fa..cd7ff8535cd8f5e98bab0d24438768581af86c7e 100644 (file)
@@ -94,6 +94,7 @@
 #include "encoding.h"
 #include "exporter.h"
 #include "Lsstream.h"
+#include "converter.h"
 
 using std::ostream;
 using std::ofstream;
@@ -103,6 +104,7 @@ using std::ios;
 using std::setw;
 using std::endl;
 using std::pair;
+using std::make_pair;
 using std::vector;
 using std::max;
 using std::set;
@@ -113,7 +115,7 @@ extern BufferList bufferlist;
 extern LyXAction lyxaction;
 
 
-static const float LYX_FORMAT = 2.17;
+static const int LYX_FORMAT = 218;
 
 extern int tex_code_break_column;
 
@@ -173,6 +175,36 @@ string const Buffer::getLatexName(bool no_path) const
                                       ".tex"); 
 }
 
+pair<Buffer::LogType, string> const Buffer::getLogName(void) const
+{
+       string filename, fname, bname, path;
+
+       filename = getLatexName(false);
+
+       if (filename.empty())
+               return make_pair(Buffer::latexlog, string());
+
+       path = OnlyPath(filename);
+
+       if (lyxrc.use_tempdir || (IsDirWriteable(path) < 1))
+               path = tmppath;
+
+       fname = AddName(path, OnlyFilename(ChangeExtension(filename, ".log")));
+       bname = AddName(path, OnlyFilename(ChangeExtension(filename,
+               formats.Extension("literate") + ".out")));
+
+       // If no Latex log or Build log is newer, show Build log
+
+       FileInfo f_fi(fname), b_fi(bname);
+
+       if (b_fi.exist() &&
+               (!f_fi.exist() || f_fi.getModificationTime() < b_fi.getModificationTime())) {
+               lyxerr[Debug::FILES] << "Log name calculated as : " << bname << endl;
+               return make_pair(Buffer::buildlog, bname);
+       }
+       lyxerr[Debug::FILES] << "Log name calculated as : " << fname << endl;
+       return make_pair(Buffer::latexlog, fname);
+}
 
 void Buffer::setReadonly(bool flag)
 {
@@ -248,9 +280,8 @@ bool Buffer::readLyXformat2(LyXLex & lex, LyXParagraph * par)
 
        LyXParagraph * return_par = 0;
        LyXFont font(LyXFont::ALL_INHERIT, params.language);
-       if (format < 2.16 && params.language->lang() == "hebrew")
+       if (file_format < 216 && params.language->lang() == "hebrew")
                font.setLanguage(default_language);
-
        // If we are inserting, we cheat and get a token in advance
        bool has_token = false;
        string pretoken;
@@ -365,7 +396,7 @@ Buffer::parseSingleLyXformat2Token(LyXLex & lex, LyXParagraph *& par,
 #endif
                par->depth = depth;
                font = LyXFont(LyXFont::ALL_INHERIT, params.language);
-               if (format < 2.16 && params.language->lang() == "hebrew")
+               if (file_format < 216 && params.language->lang() == "hebrew")
                        font.setLanguage(default_language);
 #ifndef NEW_INSETS
        } else if (token == "\\end_float") {
@@ -381,7 +412,7 @@ Buffer::parseSingleLyXformat2Token(LyXLex & lex, LyXParagraph *& par,
                lex.EatLine();
                par->layout = LYX_DUMMY_LAYOUT;
                font = LyXFont(LyXFont::ALL_INHERIT, params.language);
-               if (format < 2.16 && params.language->lang() == "hebrew")
+               if (file_format < 216 && params.language->lang() == "hebrew")
                        font.setLanguage(default_language);
        } else if (token == "\\begin_float") {
                int tmpret = lex.FindToken(string_footnotekinds);
@@ -811,13 +842,7 @@ Buffer::parseSingleLyXformat2Token(LyXLex & lex, LyXParagraph *& par,
                int tmpret = lex.FindToken(string_align);
                if (tmpret == -1) ++tmpret;
                if (tmpret != LYX_LAYOUT_DEFAULT) { // tmpret != 99 ???
-#if 0
-                       int tmpret2 = 1;
-                       for (; tmpret > 0; --tmpret)
-                               tmpret2 = tmpret2 * 2;
-#else
                        int const tmpret2 = int(pow(2.0, tmpret));
-#endif
                        //lyxerr << "Tmpret2 = " << tmpret2 << endl;
                        par->align = LyXAlignment(tmpret2);
                }
@@ -915,7 +940,7 @@ Buffer::parseSingleLyXformat2Token(LyXLex & lex, LyXParagraph *& par,
                ++pos;
        } else if (token == "\\bibitem") {  // ale970302
                if (!par->bibkey) {
-                       InsetCommandParams p( "bibitem" );
+                       InsetCommandParams p("bibitem", "dummy");
                        par->bibkey = new InsetBibKey(p);
                }
                par->bibkey->Read(this, lex);                   
@@ -1053,38 +1078,42 @@ bool Buffer::readFile(LyXLex & lex, LyXParagraph * par)
                string const token(lex.GetString());
                if (token == "\\lyxformat") { // the first token _must_ be...
                        lex.EatLine();
-                       format = lex.GetFloat();
-                       if (format > 1.0) {
-                               if (LYX_FORMAT - format > 0.05) {
-                                       lyxerr << fmt(_("Warning: need lyxformat %.2f but found %.2f"),
-                                                     LYX_FORMAT, format) << endl;
-                               }
-                               if (format - LYX_FORMAT > 0.05) {
-                                       lyxerr << fmt(_("ERROR: need lyxformat %.2f but found %.2f"),
-                                                     LYX_FORMAT, format) << endl;
+                       string tmp_format = lex.GetString();
+                       //lyxerr << "LyX Format: `" << tmp_format << "'" << endl;
+                       // if present remove ".," from string.
+                       string::size_type dot = tmp_format.find_first_of(".,");
+                       //lyxerr << "           dot found at " << dot << endl;
+                       if (dot != string::npos)
+                               tmp_format.erase(dot, 1);
+                       file_format = strToInt(tmp_format);
+                       if (file_format == LYX_FORMAT) {
+                               // current format
+                       } else if (file_format > LYX_FORMAT) {
+                               // future format
+                               WriteAlert(_("Warning!"),
+                                          _("LyX file format is newer that what"),
+                                          _("is supported in this LyX version. Expect some problems."));
+                               
+                       } else if (file_format < LYX_FORMAT) {
+                               // old formats
+                               if (file_format < 200) {
+                                       WriteAlert(_("ERROR!"),
+                                                  _("Old LyX file format found. "
+                                                    "Use LyX 0.10.x to read this!"));
+                                       return false;
                                }
-                               bool the_end = readLyXformat2(lex, par);
-                               // Formats >= 2.13 support "\the_end" marker
-                               if (format < 2.13)
-                                       the_end = true;
-
-                               setPaperStuff();
-
-                               if (!the_end)
-                                       WriteAlert(_("Warning!"),
-                                                  _("Reading of document is not complete"),
-                                                  _("Maybe the document is truncated"));
-                               // We simulate a safe reading anyways to allow
-                               // users to take the chance... (Asger)
-                               return true;
-                       } // format < 1.0
-                       else {
-                               WriteAlert(_("ERROR!"),
-                                          _("Old LyX file format found. "
-                                            "Use LyX 0.10.x to read this!"));
-                               return false;
                        }
-
+                       bool the_end = readLyXformat2(lex, par);
+                       setPaperStuff();
+                       // the_end was added in 213
+                       if (file_format < 213)
+                               the_end = true;
+
+                       if (!the_end)
+                               WriteAlert(_("Warning!"),
+                                          _("Reading of document is not complete"),
+                                          _("Maybe the document is truncated"));
+                       return true;
                } else { // "\\lyxformat" not found
                        WriteAlert(_("ERROR!"), _("Not a LyX file!"));
                }
@@ -1224,16 +1253,9 @@ bool Buffer::writeFile(string const & fname, bool flag) const
 
        // write out a comment in the top of the file
        ofs << '#' << LYX_DOCVERSION 
-           << " created this file. For more info see http://www.lyx.org/\n";
-       ofs.setf(ios::showpoint|ios::fixed);
-       ofs.precision(2);
-#ifndef HAVE_LOCALE
-       char dummy_format[512];
-       sprintf(dummy_format, "%.2f", LYX_FORMAT);
-       ofs << "\\lyxformat " <<  dummy_format << "\n";
-#else
-       ofs << "\\lyxformat " << setw(4) <<  LYX_FORMAT << "\n";
-#endif
+           << " created this file. For more info see http://www.lyx.org/\n"
+           << "\\lyxformat " << LYX_FORMAT << "\n";
+
        // now write out the buffer paramters.
        params.writeFile(ofs);
 
@@ -1282,7 +1304,9 @@ string const Buffer::asciiParagraph(LyXParagraph const * par,
        LyXFont font2;
        Inset const * inset;
        char c;
+#ifndef NEW_INSETS
        LyXParagraph::footnote_flag footnoteflag = LyXParagraph::NO_FOOTNOTE;
+#endif
        char depth = 0;
        int ltype = 0;
        int ltype_depth = 0;
@@ -1384,7 +1408,11 @@ string const Buffer::asciiParagraph(LyXParagraph const * par,
       
        font1 = LyXFont(LyXFont::ALL_INHERIT, params.language);
        for (LyXParagraph::size_type i = 0; i < par->size(); ++i) {
-               if (!i && !footnoteflag && !noparbreak){
+               if (!i &&
+#ifndef NEW_INSETS
+                   !footnoteflag &&
+#endif
+                   !noparbreak) {
                        if (linelen > 0)
                                buffer << "\n\n";
                        for (char j = 0; j < depth; ++j)
@@ -1429,7 +1457,7 @@ string const Buffer::asciiParagraph(LyXParagraph const * par,
                } else {
                        islatex = 0;
                }
-               c = par->GetChar(i);
+               c = par->GetUChar(params, i);
                if (islatex)
                        continue;
                switch (c) {
@@ -1642,7 +1670,9 @@ void Buffer::makeLaTeXFile(string const & fname,
                
                // language should be a parameter to \documentclass
                bool use_babel = false;
-               if (params.language->babel() == "hebrew") // This seems necessary
+               if (params.language->babel() == "hebrew"
+                   && default_language->babel() != "hebrew")
+                        // This seems necessary
                        features.UsedLanguages.insert(default_language);
 #ifdef DO_USE_DEFAULT_LANGUAGE
                if (params.language->lang() != "default" ||
@@ -1675,9 +1705,15 @@ void Buffer::makeLaTeXFile(string const & fname,
                // end of \documentclass defs
                
                // font selection must be done before loading fontenc.sty
-               if (params.fonts != "default") {
+               // The ae package is not needed when using OT1 font encoding.
+               if (params.fonts != "default" &&
+                   (params.fonts != "ae" || lyxrc.fontenc != "default")) {
                        ofs << "\\usepackage{" << params.fonts << "}\n";
                        texrow.newline();
+                       if (params.fonts == "ae") {
+                               ofs << "\\usepackage{aecompl}\n";
+                               texrow.newline();
+                       }
                }
                // this one is not per buffer
                if (lyxrc.fontenc != "default") {
@@ -1813,7 +1849,7 @@ void Buffer::makeLaTeXFile(string const & fname,
                        ofs << "}\n";
                        texrow.newline();
                }
-               if (params.use_amsmath
+               if (features.amsstyle
                    && !tclass.provides(LyXTextClass::amsmath)) {
                        ofs << "\\usepackage{amsmath}\n";
                        texrow.newline();
@@ -2510,7 +2546,7 @@ void Buffer::pop_tag(ostream & os, string const & tag,
        int j;
        
         // pop all tags till specified one
-        for (j = pos; (j >= 0) && (strcmp(stack[j], tag.c_str())); --j)
+        for (j = pos; (j >= 0) && tag != stack[j]; --j)
                 os << "</" << stack[j] << ">";
 
         // closes the tag
@@ -2525,7 +2561,7 @@ void Buffer::pop_tag(ostream & os, string const & tag,
 #else
         // pop all tags till specified one
        int j = pos;
-        for (int j = pos; (j >= 0) && (strcmp(stack[j], tag.c_str())); --j)
+        for (int j = pos; (j >= 0) && tag != stack[j]; --j)
                 os << "</" << stack[j] << ">";
 
         // closes the tag
@@ -2822,14 +2858,16 @@ void Buffer::makeDocBookFile(string const & fname, bool nice, bool only_body)
                            << sgml_includedfiles << " \n]>\n\n";
        }
 
-       if (params.options.empty())
-               sgmlOpenTag(ofs, 0, top_element);
-       else {
-               string top = top_element;
+       string top = top_element;       
+       top += " lang=\"";
+       top += params.language->code();
+       top += "\"";
+
+       if (!params.options.empty()) {
                top += " ";
                top += params.options;
-               sgmlOpenTag(ofs, 0, top);
        }
+       sgmlOpenTag(ofs, 0, top);
 
        ofs << "<!-- DocBook file was created by " << LYX_DOCVERSION 
            << "\n  See http://www.lyx.org/ for more information -->\n";
@@ -2931,9 +2969,11 @@ void Buffer::makeDocBookFile(string const & fname, bool nice, bool only_body)
                        }
 
                        sgmlOpenTag(ofs, depth + command_depth, command_name);
-                       item_name = "title";
-                       if (command_name != "!-- --")
-                               sgmlOpenTag(ofs, depth + 1 + command_depth, item_name);
+                       if (c_params.empty())
+                               item_name = "title";
+                       else
+                               item_name = c_params;
+                       sgmlOpenTag(ofs, depth + 1 + command_depth, item_name);
                        break;
 
                case LATEX_ENVIRONMENT:
@@ -2962,9 +3002,13 @@ void Buffer::makeDocBookFile(string const & fname, bool nice, bool only_body)
                        }
                        
                        if (style.latextype == LATEX_ENVIRONMENT) {
-                               if (!style.latexparam().empty())
-                                       sgmlOpenTag(ofs, depth + command_depth,
-                                                   style.latexparam());
+                               if (!style.latexparam().empty()) {
+                                       if(style.latexparam() == "CDATA")
+                                               ofs << "<![ CDATA [";
+                                       else
+                                               sgmlOpenTag(ofs, depth + command_depth,
+                                                           style.latexparam());
+                               }
                                break;
                        }
 
@@ -3011,14 +3055,20 @@ void Buffer::makeDocBookFile(string const & fname, bool nice, bool only_body)
                // write closing SGML tags
                switch (style.latextype) {
                case LATEX_COMMAND:
-                       end_tag = "title";
-                       if (command_name != "!-- --")
-                               sgmlCloseTag(ofs, depth + command_depth, end_tag);
+                       if (c_params.empty())
+                               end_tag = "title";
+                       else
+                               end_tag = c_params;
+                       sgmlCloseTag(ofs, depth + command_depth, end_tag);
                        break;
                case LATEX_ENVIRONMENT:
-                       if (!style.latexparam().empty())
-                               sgmlCloseTag(ofs, depth + command_depth,
-                                            style.latexparam());
+                       if (!style.latexparam().empty()) {
+                               if(style.latexparam() == "CDATA")
+                                       ofs << "]]>";
+                               else
+                                       sgmlCloseTag(ofs, depth + command_depth,
+                                                    style.latexparam());
+                       }
                        break;
                case LATEX_ITEM_ENVIRONMENT:
                        if (desc_on == 1) break;
@@ -3350,7 +3400,7 @@ vector<string> const Buffer::getLabelList()
 }
 
 
-vector<vector<Buffer::TocItem> > const Buffer::getTocList()
+vector<vector<Buffer::TocItem> > const Buffer::getTocList() const
 {
        int figs = 0;
        int tables = 0;