]> git.lyx.org Git - lyx.git/blobdiff - src/Paragraph.cpp
Allow using \binom without amsmath and add support for \brace and \brack
[lyx.git] / src / Paragraph.cpp
index 4e5dfb148d8a7ce7a6baccf97afb832bc8db94f3..ce45db9bfe55362dfee95898a107a57fb253eb88 100644 (file)
@@ -19,6 +19,7 @@
 
 #include "Paragraph.h"
 
+#include "LayoutFile.h"
 #include "Buffer.h"
 #include "BufferParams.h"
 #include "Changes.h"
@@ -78,7 +79,7 @@ class Paragraph::Private
 {
 public:
        ///
-       Private(Paragraph * owner);
+       Private(Paragraph * owner, Layout const & layout);
        /// "Copy constructor"
        Private(Private const &, Paragraph * owner);
 
@@ -189,9 +190,6 @@ public:
        ///
        InsetList insetlist_;
 
-       ///
-       LayoutPtr layout_;
-
        /// end of label
        pos_type begin_of_body_;
 
@@ -202,6 +200,8 @@ public:
        typedef std::set<docstring> Words;
        ///
        Words words_;
+       ///
+       Layout const * layout_;
 };
 
 
@@ -228,8 +228,8 @@ size_t const phrases_nr = sizeof(special_phrases)/sizeof(special_phrase);
 } // namespace anon
 
 
-Paragraph::Private::Private(Paragraph * owner)
-       : owner_(owner), inset_owner_(0), begin_of_body_(0)
+Paragraph::Private::Private(Paragraph * owner, Layout const & layout)
+       : owner_(owner), inset_owner_(0), begin_of_body_(0), layout_(&layout)
 {
        id_ = paragraph_id++;
        text_.reserve(100);
@@ -239,8 +239,8 @@ Paragraph::Private::Private(Paragraph * owner)
 Paragraph::Private::Private(Private const & p, Paragraph * owner)
        : owner_(owner), inset_owner_(p.inset_owner_), fontlist_(p.fontlist_), 
          params_(p.params_), changes_(p.changes_), insetlist_(p.insetlist_),
-         layout_(p.layout_), begin_of_body_(p.begin_of_body_), text_(p.text_),
-         words_(p.words_)
+         begin_of_body_(p.begin_of_body_), text_(p.text_), words_(p.words_),
+         layout_(p.layout_)
 {
        id_ = paragraph_id++;
 }
@@ -1034,8 +1034,12 @@ void Paragraph::Private::validate(LaTeXFeatures & features,
 //
 /////////////////////////////////////////////////////////////////////
 
-Paragraph::Paragraph()
-       : d(new Paragraph::Private(this))
+namespace {
+       Layout const emptyParagraphLayout;
+}
+
+Paragraph::Paragraph() 
+       : d(new Paragraph::Private(this, emptyParagraphLayout))
 {
        itemdepth = 0;
        d->params_.clear();
@@ -1455,7 +1459,8 @@ bool Paragraph::stripLeadingSpaces(bool trackChanges)
 
 bool Paragraph::hasSameLayout(Paragraph const & par) const
 {
-       return par.d->layout_ == d->layout_ && d->params_.sameLayout(par.d->params_);
+       return par.d->layout_ == d->layout_
+               && d->params_.sameLayout(par.d->params_);
 }
 
 
@@ -1519,21 +1524,21 @@ docstring const Paragraph::translateIfPossible(docstring const & s,
 }
 
 
-docstring Paragraph::expandLabel(LayoutPtr const & layout,
+docstring Paragraph::expandLabel(Layout const & layout,
                BufferParams const & bparams, bool process_appendix) const
 {
        DocumentClass const & tclass = bparams.documentClass();
 
        docstring fmt;
        if (process_appendix && d->params_.appendix())
-               fmt = translateIfPossible(layout->labelstring_appendix(),
+               fmt = translateIfPossible(layout.labelstring_appendix(),
                        bparams);
        else
-               fmt = translateIfPossible(layout->labelstring(), bparams);
+               fmt = translateIfPossible(layout.labelstring(), bparams);
 
-       if (fmt.empty() && layout->labeltype == LABEL_COUNTER 
-           && !layout->counter.empty())
-               fmt = "\\the" + layout->counter;
+       if (fmt.empty() && layout.labeltype == LABEL_COUNTER 
+           && !layout.counter.empty())
+               fmt = "\\the" + layout.counter;
 
        // handle 'inherited level parts' in 'fmt',
        // i.e. the stuff between '@' in   '@Section@.\arabic{subsection}'
@@ -1544,7 +1549,7 @@ docstring Paragraph::expandLabel(LayoutPtr const & layout,
                        docstring parent(fmt, i + 1, j - i - 1);
                        docstring label = from_ascii("??");
                        if (tclass.hasLayout(parent))
-                               docstring label = expandLabel(&(tclass[parent]), bparams,
+                               docstring label = expandLabel(tclass[parent], bparams,
                                                      process_appendix);
                        fmt = docstring(fmt, 0, i) + label 
                                + docstring(fmt, j + 1, docstring::npos);
@@ -1611,19 +1616,28 @@ void Paragraph::setBeginOfBody()
 
 bool Paragraph::forceEmptyLayout() const
 {
-       return inInset() && inInset()->forceEmptyLayout();
+       Inset const * const inset = inInset();
+       if (!inset)
+               return true;
+       return inset->forceEmptyLayout();
 }
 
 
 bool Paragraph::allowParagraphCustomization() const
 {
-       return inInset() && inInset()->allowParagraphCustomization(0);
+       Inset const * const inset = inInset();
+       if (!inset)
+               return true;
+       return inset->allowParagraphCustomization();
 }
 
 
 bool Paragraph::useEmptyLayout() const
 {
-       return inInset() && inInset()->useEmptyLayout();
+       Inset const * const inset = inInset();
+       if (!inset)
+               return false;
+       return inset->useEmptyLayout();
 }
 
 
@@ -1817,14 +1831,11 @@ bool Paragraph::latex(BufferParams const & bparams,
 
        bool return_value = false;
 
-       LayoutPtr style;
-
        bool asdefault = forceEmptyLayout();
 
-       if (asdefault)
-               style = &(bparams.documentClass().emptyLayout());
-        else
-               style = d->layout_;
+       Layout const & style = asdefault ?
+               bparams.documentClass().emptyLayout() :
+               *d->layout_;
 
        // Current base font for all inherited font changes, without any
        // change caused by an individual character, except for the language:
@@ -1859,7 +1870,7 @@ bool Paragraph::latex(BufferParams const & bparams,
 
        // if the paragraph is empty, the loop will not be entered at all
        if (empty()) {
-               if (style->isCommand()) {
+               if (style.isCommand()) {
                        os << '{';
                        ++column;
                }
@@ -1888,7 +1899,7 @@ bool Paragraph::latex(BufferParams const & bparams,
                                os << "}] ";
                                column +=3;
                        }
-                       if (style->isCommand()) {
+                       if (style.isCommand()) {
                                os << '{';
                                ++column;
                        }
@@ -1988,13 +1999,13 @@ bool Paragraph::latex(BufferParams const & bparams,
                if (c == ' ') {
                        // FIXME: integrate this case in latexSpecialChar
                        // Do not print the separation of the optional argument
-                       // if style->pass_thru is false. This works because
+                       // if style.pass_thru is false. This works because
                        // latexSpecialChar ignores spaces if
-                       // style->pass_thru is false.
+                       // style.pass_thru is false.
                        if (i != body_pos - 1) {
                                if (d->simpleTeXBlanks(
                                                runparams, os, texrow,
-                                               i, column, font, *style)) {
+                                               i, column, font, style)) {
                                        // A surrogate pair was output. We
                                        // must not call latexSpecialChar
                                        // in this iteration, since it would output
@@ -2006,9 +2017,9 @@ bool Paragraph::latex(BufferParams const & bparams,
                }
 
                OutputParams rp = runparams;
-               rp.free_spacing = style->free_spacing;
+               rp.free_spacing = style.free_spacing;
                rp.local_font = &font;
-               rp.intitle = style->intitle;
+               rp.intitle = style.intitle;
 
                // Two major modes:  LaTeX or plain
                // Handle here those cases common to both modes
@@ -2017,11 +2028,11 @@ bool Paragraph::latex(BufferParams const & bparams,
                        d->latexInset(bparams, os,
                                        texrow, rp, running_font,
                                        basefont, outerfont, open_font,
-                                       runningChange, *style, i, column);
+                                       runningChange, style, i, column);
                else {
                        try {
                                d->latexSpecialChar(os, rp, running_font, runningChange,
-                                       *style, i, column);
+                                       style, i, column);
                        } catch (EncodingException & e) {
                                if (runparams.dryrun) {
                                        os << "<" << _("LyX Warning: ")
@@ -2165,11 +2176,11 @@ void Paragraph::simpleDocBookOnePar(Buffer const & buf,
 {
        bool emph_flag = false;
 
-       LayoutPtr const & style = d->layout_;
+       Layout const & style = *d->layout_;
        FontInfo font_old =
-               style->labeltype == LABEL_MANUAL ? style->labelfont : style->font;
+               style.labeltype == LABEL_MANUAL ? style.labelfont : style.font;
 
-       if (style->pass_thru && !d->onlyText(buf, outerfont, initial))
+       if (style.pass_thru && !d->onlyText(buf, outerfont, initial))
                os << "]]>";
 
        // parsing main loop
@@ -2192,7 +2203,7 @@ void Paragraph::simpleDocBookOnePar(Buffer const & buf,
                } else {
                        char_type c = d->text_[i];
 
-                       if (style->pass_thru)
+                       if (style.pass_thru)
                                os.put(c);
                        else
                                os << sgml::escapeChar(c);
@@ -2204,9 +2215,9 @@ void Paragraph::simpleDocBookOnePar(Buffer const & buf,
                os << "</emphasis>";
        }
 
-       if (style->free_spacing)
+       if (style.free_spacing)
                os << '\n';
-       if (style->pass_thru && !d->onlyText(buf, outerfont, initial))
+       if (style.pass_thru && !d->onlyText(buf, outerfont, initial))
                os << "<![CDATA[";
 }
 
@@ -2214,7 +2225,8 @@ void Paragraph::simpleDocBookOnePar(Buffer const & buf,
 bool Paragraph::isHfill(pos_type pos) const
 {
        Inset const * inset = getInset(pos);
-       return inset && inset->lyxCode() == HFILL_CODE;
+       return inset && (inset->lyxCode() == SPACE_CODE &&
+                        inset->isStretchableSpace());
 }
 
 
@@ -2331,15 +2343,15 @@ int Paragraph::id() const
 }
 
 
-LayoutPtr const & Paragraph::layout() const
+Layout const & Paragraph::layout() const
 {
-       return d->layout_;
+       return *d->layout_;
 }
 
 
-void Paragraph::setLayout(LayoutPtr const & layout)
+void Paragraph::setLayout(Layout const & layout)
 {
-       d->layout_ = layout;
+       d->layout_ = &layout;
 }
 
 
@@ -2394,7 +2406,7 @@ bool Paragraph::allowEmpty() const
 
 char_type Paragraph::transformChar(char_type c, pos_type pos) const
 {
-       if (!Encodings::is_arabic(c))
+       if (!Encodings::isArabicChar(c))
                return c;
 
        char_type prev_char = ' ';
@@ -2402,7 +2414,7 @@ char_type Paragraph::transformChar(char_type c, pos_type pos) const
 
        for (pos_type i = pos - 1; i >= 0; --i) {
                char_type const par_char = d->text_[i];
-               if (!Encodings::isComposeChar_arabic(par_char)) {
+               if (!Encodings::isArabicComposeChar(par_char)) {
                        prev_char = par_char;
                        break;
                }
@@ -2410,21 +2422,21 @@ char_type Paragraph::transformChar(char_type c, pos_type pos) const
 
        for (pos_type i = pos + 1, end = size(); i < end; ++i) {
                char_type const par_char = d->text_[i];
-               if (!Encodings::isComposeChar_arabic(par_char)) {
+               if (!Encodings::isArabicComposeChar(par_char)) {
                        next_char = par_char;
                        break;
                }
        }
 
-       if (Encodings::is_arabic(next_char)) {
-               if (Encodings::is_arabic(prev_char) &&
-                       !Encodings::is_arabic_special(prev_char))
+       if (Encodings::isArabicChar(next_char)) {
+               if (Encodings::isArabicChar(prev_char) &&
+                       !Encodings::isArabicSpecialChar(prev_char))
                        return Encodings::transformChar(c, Encodings::FORM_MEDIAL);
                else
                        return Encodings::transformChar(c, Encodings::FORM_INITIAL);
        } else {
-               if (Encodings::is_arabic(prev_char) &&
-                       !Encodings::is_arabic_special(prev_char))
+               if (Encodings::isArabicChar(prev_char) &&
+                       !Encodings::isArabicSpecialChar(prev_char))
                        return Encodings::transformChar(c, Encodings::FORM_FINAL);
                else
                        return Encodings::transformChar(c, Encodings::FORM_ISOLATED);