]> git.lyx.org Git - lyx.git/blobdiff - src/buffer.C
Small changes; ChangeLog is your friend
[lyx.git] / src / buffer.C
index cbb04c2793392672d5e1a9a7d72f1a559bad3c7b..f36aa069e7945b6e222e6a48f86cda9988a7a4a4 100644 (file)
@@ -4,7 +4,7 @@
  *           LyX, The Document Processor
  *
  *          Copyright 1995 Matthias Ettrich
- *           Copyright 1995-1999 The LyX Team.
+ *           Copyright 1995-2000 The LyX Team.
  *
  *           This file is Copyright 1996-1999
  *           Lars Gullik Bjønnes
 
 #include <fstream>
 #include <iomanip>
-using std::ofstream;
-using std::ifstream;
-using std::fstream;
-using std::ios;
-using std::setw;
-
 
 #include <cstdlib>
 #include <unistd.h>
@@ -74,6 +68,7 @@ using std::setw;
 #include "insets/insetspecialchar.h"
 #include "insets/figinset.h"
 #include "insets/insettext.h"
+//#include "insets/insetnumber.h"
 #include "insets/insetert.h"
 #include "insets/insetgraphics.h"
 #include "insets/insetfoot.h"
@@ -90,6 +85,16 @@ using std::setw;
 #include "support/FileInfo.h"
 #include "lyxtext.h"
 #include "gettext.h"
+#include "language.h"
+
+using std::ostream;
+using std::ofstream;
+using std::ifstream;
+using std::fstream;
+using std::ios;
+using std::setw;
+using std::endl;
+using std::pair;
 
 // Uncomment this line to enable a workaround for the weird behaviour
 // of the cursor between a displayed inset and last character
@@ -109,7 +114,7 @@ extern void MenuExport(Buffer *, string const &);
 extern LyXAction lyxaction;
 
 
-static const float LYX_FORMAT = 2.15;
+static const float LYX_FORMAT = 2.16;
 
 extern int tex_code_break_column;
 
@@ -162,6 +167,26 @@ Buffer::~Buffer()
 }
 
 
+string Buffer::getLatexName(bool no_path /* = true */) const
+{
+       return ChangeExtension(MakeLatexName(filename), 
+                              ".tex", no_path); 
+}
+
+
+void Buffer::setReadonly(bool flag /* = true */)
+{
+       if (read_only != flag) {
+               read_only = flag; 
+               updateTitles();
+               updateAllVisibleBufferRelatedPopups();
+       }
+       if (read_only) {
+               WarnReadonly(filename);
+       }
+}
+
+
 bool Buffer::saveParamsAsDefaults()
 {
        string fname = AddName(AddPath(user_lyxdir, "templates/"),
@@ -222,7 +247,9 @@ bool Buffer::readLyXformat2(LyXLex & lex, LyXParagraph * par)
        bool the_end_read = false;
 
        LyXParagraph * return_par = 0;
-       LyXFont font(LyXFont::ALL_INHERIT);
+       LyXFont font(LyXFont::ALL_INHERIT,params.language_info);
+       if (format < 2.16 && params.language == "hebrew")
+               font.setLanguage(default_language);
 
        // If we are inserting, we cheat and get a token in advance
        bool has_token = false;
@@ -305,7 +332,7 @@ bool Buffer::parseSingleLyXformat2Token(LyXLex & lex, LyXParagraph *& par,
                if (!return_par) 
                        return_par = par;
                else {
-                       par->text.resize(par->text.size());
+                       par->fitToSize();
                        par = new LyXParagraph(par);
                }
                pos = 0;
@@ -331,12 +358,14 @@ bool Buffer::parseSingleLyXformat2Token(LyXLex & lex, LyXParagraph *& par,
                par->footnoteflag = footnoteflag;
                par->footnotekind = footnotekind;
                par->depth = depth;
-               font = LyXFont(LyXFont::ALL_INHERIT);
+               font = LyXFont(LyXFont::ALL_INHERIT,params.language_info);
+               if (format < 2.16 && params.language == "hebrew")
+                       font.setLanguage(default_language);
        } else if (token == "\\end_float") {
                if (!return_par) 
                        return_par = par;
                else {
-                       par->text.resize(par->text.size());
+                       par->fitToSize();
                        par = new LyXParagraph(par);
                }
                footnotekind = LyXParagraph::FOOTNOTE;
@@ -344,7 +373,9 @@ bool Buffer::parseSingleLyXformat2Token(LyXLex & lex, LyXParagraph *& par,
                pos = 0;
                lex.EatLine();
                par->layout = LYX_DUMMY_LAYOUT;
-               font = LyXFont(LyXFont::ALL_INHERIT);
+               font = LyXFont(LyXFont::ALL_INHERIT,params.language_info);
+               if (format < 2.16 && params.language == "hebrew")
+                       font.setLanguage(default_language);
        } else if (token == "\\begin_float") {
                tmpret = lex.FindToken(string_footnotekinds);
                if (tmpret == -1) ++tmpret;
@@ -648,18 +679,30 @@ bool Buffer::parseSingleLyXformat2Token(LyXLex & lex, LyXParagraph *& par,
                else
                        lex.printError("Unknown LaTeX font flag "
                                       "`$$Token'");
-       } else if (token == "\\direction") {
+       } else if (token == "\\lang") {
                lex.next();
                string tok = lex.GetString();
-               if (tok == "ltr")
-                       font.setDirection(LyXFont::LTR_DIR);
-               else if (tok == "rtl")
-                       font.setDirection(LyXFont::RTL_DIR);
+               Languages::iterator lit = languages.find(tok);
+               if (lit != languages.end()) {
+                       font.setLanguage(&(*lit).second);
+               } else {
+                       font.setLanguage(params.language_info);
+                       lex.printError("Unknown language `$$Token'");
+               }
+       } else if (token == "\\direction") { // obsolete
+               if (format >= 2.16)
+                       lex.printError("Command \\direction is obsolete");
+               lex.next();
+               string tok = lex.GetString();
+               if (tok == "rtl")
+                       font.setLanguage(&languages["hebrew"]);
                else if (tok == "default")
-                       font.setDirection(LyXFont::INHERIT_DIR);
+                       if (params.language == "hebrew")
+                               font.setLanguage(default_language);
+                       else
+                               font.setLanguage(params.language_info);
                else
-                       lex.printError("Unknown font flag "
-                                      "`$$Token'");
+                       lex.printError("Unknown direction `$$Token'");
        } else if (token == "\\emph") {
                lex.next();
                font.setEmph(font.setLyXMisc(lex.GetString()));
@@ -693,10 +736,10 @@ bool Buffer::parseSingleLyXformat2Token(LyXLex & lex, LyXParagraph *& par,
                }
        } else if (token == "\\added_space_top") {
                lex.nextToken();
-               par->added_space_top = lex.GetString();
+               par->added_space_top = VSpace(lex.GetString());
        } else if (token == "\\added_space_bottom") {
                lex.nextToken();
-               par->added_space_bottom = lex.GetString();
+               par->added_space_bottom = VSpace(lex.GetString());
        } else if (token == "\\pextra_type") {
                lex.nextToken();
                par->pextra_type = lex.GetInteger();
@@ -791,6 +834,15 @@ bool Buffer::parseSingleLyXformat2Token(LyXLex & lex, LyXParagraph *& par,
                        par->InsertInset(pos, inset);
                        par->SetFont(pos, font);
                        ++pos;
+#if 0
+               } else if (tmptok == "Number") {
+                       inset = new InsetNumber(this);
+                       inset->Read(lex);
+                       par->InsertChar(pos, LyXParagraph::META_INSET);
+                       par->InsertInset(pos, inset);
+                       par->SetFont(pos, font);
+                       ++pos;
+#endif
                } else if (tmptok == "Foot") {
                        inset = new InsetFoot(this);
                        inset->Read(lex);
@@ -828,7 +880,7 @@ bool Buffer::parseSingleLyXformat2Token(LyXLex & lex, LyXParagraph *& par,
                                if (!inscmd.getOptions().empty() || !inscmd.getContents().empty()) {
                                        inset = new InsetRef(inscmd, this);
                                }
-#warning Verify that this else clause is still needed. (Lgb)
+#warning Check if this else clause is still needed. (Lgb)
 #if 0
                                // This condition comes from a
                                // temporary solution to the latexdel
@@ -1074,14 +1126,19 @@ bool Buffer::readFile(LyXLex & lex, LyXParagraph * par)
 
 
 // Should probably be moved to somewhere else: BufferView? LyXView?
-bool Buffer::save(bool makeBackup) const
+bool Buffer::save() const
 {
        // We don't need autosaves in the immediate future. (Asger)
        resetAutosaveTimers();
 
        // make a backup
-       if (makeBackup) {
-               string s = fileName() + '~';
+       string s;
+       if (lyxrc.make_backup) {
+               s = fileName() + '~';
+               if (!lyxrc.backupdir_path.empty())
+                       s = AddName(lyxrc.backupdir_path,
+                                   subst(CleanupPath(s),'/','!'));
+
                // Rename is the wrong way of making a backup,
                // this is the correct way.
                /* truss cp fil fil2:
@@ -1109,10 +1166,10 @@ bool Buffer::save(bool makeBackup) const
                FileInfo finfo(fileName());
                if (finfo.exist()) {
                        mode_t fmode = finfo.getMode();
-                       struct utimbuf * times = new struct utimbuf;
+                       struct utimbuf times = {
+                               finfo.getAccessTime(),
+                               finfo.getModificationTime() };
 
-                       times->actime = finfo.getAccessTime();
-                       times->modtime = finfo.getModificationTime();
                        ifstream ifs(fileName().c_str());
                        ofstream ofs(s.c_str(), ios::out|ios::trunc);
                        if (ifs && ofs) {
@@ -1121,14 +1178,13 @@ bool Buffer::save(bool makeBackup) const
                                ofs.close();
                                ::chmod(s.c_str(), fmode);
                                
-                               if (::utime(s.c_str(), times)) {
+                               if (::utime(s.c_str(), &times)) {
                                        lyxerr << "utime error." << endl;
                                }
                        } else {
                                lyxerr << "LyX was not able to make "
                                        "backupcopy. Beware." << endl;
                        }
-                       delete times;
                }
        }
        
@@ -1149,8 +1205,7 @@ bool Buffer::save(bool makeBackup) const
                }
        } else {
                // Saving failed, so backup is not backup
-               if (makeBackup) {
-                       string s = fileName() + '~';
+               if (lyxrc.make_backup) {
                        ::rename(s.c_str(), fileName().c_str());
                }
                return false;
@@ -1357,7 +1412,7 @@ void Buffer::writeFileAscii(string const & fname, int linelen)
                                        if ((inset = par->GetInset(i))) {
 #if 1
 #ifdef HAVE_SSTREAM
-                                               ostringstream ost;
+                                               std::ostringstream ost;
                                                inset->Latex(ost, -1, free_spc);
                                                h += ost.str().length();
 #else
@@ -1392,7 +1447,7 @@ void Buffer::writeFileAscii(string const & fname, int linelen)
                                clen[j] = h;
                }
       
-               font1 = LyXFont(LyXFont::ALL_INHERIT);
+               font1 = LyXFont(LyXFont::ALL_INHERIT,params.language_info);
                 actcell = 0;
                for (i = 0, actpos = 1; i < par->size(); ++i, ++actpos) {
                        if (!i && !footnoteflag && !noparbreak){
@@ -1598,7 +1653,7 @@ void Buffer::makeLaTeXFile(string const & fname,
        
        // validate the buffer.
        lyxerr[Debug::LATEX] << "  Validating buffer..." << endl;
-       LaTeXFeatures features(tclass.numLayouts());
+       LaTeXFeatures features(params, tclass.numLayouts());
        validate(features);
        lyxerr[Debug::LATEX] << "  Buffer validation done." << endl;
        
@@ -1702,13 +1757,19 @@ void Buffer::makeLaTeXFile(string const & fname,
                    && params.orientation == BufferParams::ORIENTATION_LANDSCAPE)
                        options += "landscape,";
                
-               // language should be a parameter to \documentclass             
-               if (params.language != "default") {
-                       if (params.language == "hebrew")
-                               options += "english,";
-                       else if (lyxrc.rtl_support)
-                               options += "hebrew,";
-                       options += params.language + ',';
+               // language should be a parameter to \documentclass
+               bool use_babel = false;
+               if (params.language != "default" ||
+                   !features.UsedLanguages.empty() ) {
+                       use_babel = true;
+                       for (LaTeXFeatures::LanguageList::const_iterator cit =
+                                    features.UsedLanguages.begin();
+                            cit != features.UsedLanguages.end(); ++cit) {
+                               options += (*cit)->lang + ",";
+                       }
+                       if (default_language != params.language_info)
+                               options += default_language ->lang + ',';
+                       options += params.language_info->lang + ',';
                }
 
                // the user-defined options
@@ -1864,8 +1925,8 @@ void Buffer::makeLaTeXFile(string const & fname,
 
                // We try to load babel late, in case it interferes
                // with other packages.
-               if (params.language != "default") {
-                       ofs << lyxrc.language_package;
+               if (use_babel) {
+                       ofs << lyxrc.language_package << endl;
                        texrow.newline();
                }
 
@@ -1912,13 +1973,13 @@ void Buffer::makeLaTeXFile(string const & fname,
                string preamble, tmppreamble;
 
                // The optional packages;
-               preamble = features.getPackages(params);
+               preamble = features.getPackages();
 
                // this might be useful...
                preamble += "\n\\makeatletter\n\n";
 
                // Some macros LyX will need
-               tmppreamble = features.getMacros(params);
+               tmppreamble = features.getMacros();
 
                if (!tmppreamble.empty()) {
                        preamble += "\n%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% "
@@ -1927,7 +1988,7 @@ void Buffer::makeLaTeXFile(string const & fname,
                }
 
                // the text class specific preamble 
-               tmppreamble = features.getTClassPreamble(params);
+               tmppreamble = features.getTClassPreamble();
                if (!tmppreamble.empty()) {
                        preamble += "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% "
                                "Textclass specific LaTeX commands.\n"
@@ -1990,14 +2051,17 @@ void Buffer::makeLaTeXFile(string const & fname,
                texrow.newline();
        } // only_body
        lyxerr.debug() << "preamble finished, now the body." << endl;
-       if (lyxrc.language_command_begin &&
-           params.getDocumentDirection() == LYX_DIR_RIGHT_TO_LEFT)
-               ofs << lyxrc.language_command_rtl;
+       if (!lyxrc.language_auto_begin && params.language != "default") {
+               ofs << subst(lyxrc.language_command_begin, "$$lang",
+                            params.language)
+                   << endl;
+               texrow.newline();
+       }
        
        bool was_title = false;
        bool already_title = false;
 #ifdef HAVE_SSTREAM
-       ostringstream ftnote;
+       std::ostringstream ftnote;
 #else
        char * tmpholder = 0;
 #endif
@@ -2084,9 +2148,12 @@ void Buffer::makeLaTeXFile(string const & fname,
                texrow.newline();
        }
 
-       if (lyxrc.language_command_end &&
-           params.getDocumentDirection() == LYX_DIR_RIGHT_TO_LEFT)
-               ofs << lyxrc.language_command_ltr;
+       if (!lyxrc.language_auto_end && params.language != "default") {
+               ofs << subst(lyxrc.language_command_end, "$$lang",
+                            params.language)
+                   << endl;
+               texrow.newline();
+       }
 
        if (!only_body) {
                ofs << "\\end{document}\n";
@@ -2451,7 +2518,7 @@ void Buffer::DocBookHandleFootnote(ostream & os, LyXParagraph * & par,
                    .NumberOfLayout(params.textclass,
                                    "Caption").second) {
 #ifdef HAVE_SSTREAM
-                       ostringstream ost;
+                       std::ostringstream ost;
 #else
                        ostrstream ost;
 #endif
@@ -2494,6 +2561,7 @@ void Buffer::push_tag(ostream & os, char const * tag,
                os << "<" << stack[i] << ">";
 }
 
+
 void Buffer::pop_tag(ostream & os, char const * tag,
                      int & pos, char stack[5][3])
 {
@@ -2702,10 +2770,6 @@ void Buffer::SimpleLinuxDocOnePar(ostream & os, LyXParagraph * par,
                font1 = font2;
        }
 
-       if (lyxrc.language_command_end &&
-           params.getDocumentDirection() == LYX_DIR_RIGHT_TO_LEFT)
-               os << lyxrc.language_command_ltr;
-
        /* needed if there is an optional argument but no contents */
        if (main_body > 0 && main_body == par->size()) {
                font1 = style.font;
@@ -3086,7 +3150,7 @@ void Buffer::SimpleDocBookOnePar(ostream & os, string & extra,
                if (c == LyXParagraph::META_INSET) {
                        Inset * inset = par->GetInset(i);
 #ifdef HAVE_SSTREAM
-                       ostringstream ost;
+                       std::ostringstream ost;
                        inset->DocBook(ost);
                        string tmp_out = ost.str().c_str();
 #else
@@ -3429,7 +3493,7 @@ int Buffer::runChktex()
 #if 0
 void Buffer::RoffAsciiTable(ostream & os, LyXParagraph * par)
 {
-       LyXFont font1(LyXFont::ALL_INHERIT);
+       LyXFont font1(LyXFont::ALL_INHERIT,params.language_info);
        LyXFont font2;
        Inset * inset;
        LyXParagraph::size_type i;
@@ -3657,7 +3721,7 @@ void Buffer::validate(LaTeXFeatures & features) const
        }
        
        if (lyxerr.debugging(Debug::LATEX)) {
-               features.showStruct(params);
+               features.showStruct();
        }
 }
 
@@ -3861,3 +3925,25 @@ void Buffer::Dispatch(int action, string const & argument)
 
        } // end of switch
 }
+
+void Buffer::ChangeLanguage(Language const * from, Language const * to)
+{
+
+       LyXParagraph * par = paragraph;
+       while (par) {
+               par->ChangeLanguage(from,to);
+               par = par->next;
+       }
+}
+
+bool Buffer::isMultiLingual()
+{
+
+       LyXParagraph * par = paragraph;
+       while (par) {
+               if (par->isMultiLingual())
+                       return true;
+               par = par->next;
+       }
+       return false;
+}