]> git.lyx.org Git - lyx.git/blobdiff - src/Layout.cpp
Fix the placement of the cursor when right-clicking on an inset inside a branch inset.
[lyx.git] / src / Layout.cpp
index f712e6234ad702c536703fd26247a8791e7cadfb..d92345eed1ebb8e914e8672000c5094dbf089425 100644 (file)
@@ -3,9 +3,9 @@
  * This file is part of LyX, the document processor.
  * Licence details can be found in the file COPYING.
  *
- * \author Lars Gullik Bjønnes
+ * \author Lars Gullik Bjønnes
  * \author Jean-Marc Lasgouttes
- * \author André Pönitz
+ * \author André Pönitz
  *
  * Full author contact details are available in file CREDITS.
  */
 #include <config.h>
 
 #include "Layout.h"
+#include "Language.h"
 #include "TextClass.h"
 #include "Lexer.h"
 #include "Font.h"
 
+#include "support/Messages.h"
 #include "support/debug.h"
+#include "support/lassert.h"
 #include "support/lstrings.h"
 
+#include <boost/regex.hpp>
+
 using namespace std;
 using namespace lyx::support;
 
@@ -68,9 +73,6 @@ enum LayoutTags {
        LT_LATEXPARAM,
        LT_OPTARGS,
        LT_LATEXTYPE,
-       LT_LATEXHEADER,
-       LT_LATEXFOOTER,
-       LT_LATEXPARAGRAPH,
        LT_LEFTMARGIN,
        LT_NEED_PROTECT,
        LT_NEWLINE,
@@ -80,6 +82,8 @@ enum LayoutTags {
        LT_PARSKIP,
        //LT_PLAIN,
        LT_PREAMBLE,
+       LT_LANGPREAMBLE,
+       LT_BABELPREAMBLE,
        LT_REQUIRES,
        LT_RIGHTMARGIN,
        LT_SPACING,
@@ -95,6 +99,7 @@ enum LayoutTags {
 
 Layout::Layout()
 {
+       unknown_ = false;
        margintype = MARGIN_STATIC;
        latextype = LATEX_PARAGRAPH;
        intitle = false;
@@ -123,7 +128,6 @@ Layout::Layout()
        newline_allowed = true;
        free_spacing = false;
        pass_thru = false;
-       is_environment = false;
        toclevel = NOT_IN_TOC;
        commanddepth = 0;
 }
@@ -135,6 +139,7 @@ bool Layout::read(Lexer & lex, TextClass const & tclass)
        LexerKeyword layoutTags[] = {
                { "align",          LT_ALIGN },
                { "alignpossible",  LT_ALIGNPOSSIBLE },
+               { "babelpreamble",  LT_BABELPREAMBLE },
                { "bottomsep",      LT_BOTTOMSEP },
                { "category",       LT_CATEGORY },
                { "commanddepth",   LT_COMMANDDEPTH },
@@ -161,10 +166,8 @@ bool Layout::read(Lexer & lex, TextClass const & tclass)
                { "labelstringappendix", LT_LABELSTRING_APPENDIX },
                { "labeltag",       LT_LABELTAG },
                { "labeltype",      LT_LABELTYPE },
-               { "latexfooter",    LT_LATEXFOOTER },
-               { "latexheader",    LT_LATEXHEADER },
+               { "langpreamble",   LT_LANGPREAMBLE },
                { "latexname",      LT_LATEXNAME },
-               { "latexparagraph", LT_LATEXPARAGRAPH },
                { "latexparam",     LT_LATEXPARAM },
                { "latextype",      LT_LATEXTYPE },
                { "leftmargin",     LT_LEFTMARGIN },
@@ -202,7 +205,9 @@ bool Layout::read(Lexer & lex, TextClass const & tclass)
                        lex.printError("Unknown layout tag `$$Token'");
                        error = true;
                        continue;
-               default: break;
+
+               default: 
+                       break;
                }
                switch (static_cast<LayoutTags>(le)) {
                case LT_END:            // end of structure
@@ -223,17 +228,13 @@ bool Layout::read(Lexer & lex, TextClass const & tclass)
                                this->operator=(tclass[style]);
                                name_ = tmpname;
                        } else {
-                               lyxerr << "Cannot copy unknown style `"
-                                                        << to_utf8(style) << "'\n"
-                                                        << "All layouts so far:"
-                                                        << endl;
+                               LYXERR0("Cannot copy unknown style `"
+                                       << style << "'\n"
+                                       << "All layouts so far:");
                                DocumentClass::const_iterator lit = tclass.begin();
                                DocumentClass::const_iterator len = tclass.end();
                                for (; lit != len; ++lit)
-                                       lyxerr << to_utf8(lit->name()) << endl;
-
-                               //lex.printError("Cannot copy known "
-                               //               "style `$$Token'");
+                                       LYXERR0(lit->name());
                        }
                        break;
                        }
@@ -250,8 +251,8 @@ bool Layout::read(Lexer & lex, TextClass const & tclass)
                                if (obsoleted_by().empty())
                                        obsoleted_by_ = style;
                        } else {
-                               lyxerr << "Cannot replace with unknown style `" 
-                                       << to_utf8(style) << '\'' << endl;
+                               LYXERR0("Cannot replace with unknown style `" 
+                                       << style << '\'');
 
                                //lex.printError("Cannot replace with"
                                //               " unknown style "
@@ -273,18 +274,6 @@ bool Layout::read(Lexer & lex, TextClass const & tclass)
                        readLatexType(lex);
                        break;
 
-               case LT_LATEXHEADER:    // header for environments
-                       lex >> latexheader;
-                       break;
-
-               case LT_LATEXFOOTER:    // footer for environments
-                       lex >> latexfooter;
-                       break;
-
-               case LT_LATEXPARAGRAPH:
-                       lex >> latexparagraph;
-                       break;
-
                case LT_INTITLE:
                        lex >> intitle;
                        break;
@@ -351,6 +340,14 @@ bool Layout::read(Lexer & lex, TextClass const & tclass)
                        preamble_ = from_utf8(lex.getLongString("EndPreamble"));
                        break;
 
+               case LT_LANGPREAMBLE:
+                       langpreamble_ = from_utf8(lex.getLongString("EndLangPreamble"));
+                       break;
+
+               case LT_BABELPREAMBLE:
+                       babelpreamble_ = from_utf8(lex.getLongString("EndBabelPreamble"));
+                       break;
+
                case LT_LABELTYPE:
                        readLabelType(lex);
                        break;
@@ -426,7 +423,7 @@ bool Layout::read(Lexer & lex, TextClass const & tclass)
                case LT_LABELSTRING:    // label string definition
                        // FIXME: this means LT_ENDLABELSTRING may only
                        // occur after LT_LABELSTRING
-                       lex >> labelstring_;    
+                       lex >> labelstring_;
                        labelstring_ = trim(labelstring_);
                        labelstring_appendix_ = labelstring_;
                        break;
@@ -650,8 +647,7 @@ void Layout::readEndLabelType(Lexer & lex)
                endlabeltype = static_cast<EndLabelType>(le);
                break;
        default:
-               LYXERR0("Unhandled value " << le
-                      << " in Layout::readEndLabelType.");
+               LYXERR0("Unhandled value " << le);
                break;
        }
 }
@@ -682,8 +678,7 @@ void Layout::readMargin(Lexer & lex)
                margintype = static_cast<MarginType>(le);
                break;
        default:
-               lyxerr << "Unhandled value " << le
-                      << " in Layout::readMargin." << endl;
+               LYXERR0("Unhandled value " << le);
                break;
        }
 }
@@ -715,8 +710,7 @@ void Layout::readLatexType(Lexer & lex)
                latextype = static_cast<LatexType>(le);
                break;
        default:
-               lyxerr << "Unhandled value " << le
-                      << " in Layout::readLatexType." << endl;
+               LYXERR0("Unhandled value " << le);
                break;
        }
 }
@@ -788,6 +782,55 @@ docstring const & Layout::depends_on() const
 }
 
 
+namespace {
+
+docstring const i18npreamble(Language const * lang, docstring const & templ)
+{
+       if (templ.empty())
+               return templ;
+
+       string preamble = subst(to_utf8(templ), "$$lang", lang->babel());
+
+#ifdef TEX2LYX
+       // tex2lyx does not have getMessages()
+       LASSERT(false, /**/);
+#else
+       // FIXME UNICODE
+       // boost::regex is not unicode-safe.
+       // Should use QRegExp or (boost::u32regex, but that requires ICU)
+       static boost::regex const reg("_\\(([^\\)]+)\\)");
+       boost::smatch sub;
+       while (boost::regex_search(preamble, sub, reg)) {
+               string const key = sub.str(1);
+               string translated;
+               if (isAscii(key))
+                       translated = to_utf8(getMessages(lang->code()).get(key));
+               else {
+                       lyxerr << "Warning: not translating `" << key
+                              << "' because it is not pure ASCII." << endl;
+                       translated = key;
+               }
+               preamble = subst(preamble, sub.str(), translated);
+       }
+#endif
+       return from_utf8(preamble);
+}
+
+}
+
+
+docstring const Layout::langpreamble(Language const * lang) const
+{
+       return i18npreamble(lang, langpreamble_);
+}
+
+
+docstring const Layout::babelpreamble(Language const * lang) const
+{
+       return i18npreamble(lang, babelpreamble_);
+}
+
+
 bool Layout::operator==(Layout const & rhs) const
 {
        // This is enough for the applications we actually make,