]> git.lyx.org Git - lyx.git/blobdiff - src/text.C
honor nextnoindent
[lyx.git] / src / text.C
index 12a1985c121106c10fd8b52774979a3171a95322..5c54cc342bd0b1f5611f04566b6d72d180835172 100644 (file)
@@ -70,6 +70,8 @@
 #include "support/textutils.h"
 #include "support/convert.h"
 
+#include <boost/current_function.hpp>
+
 #include <sstream>
 
 using lyx::pit_type;
@@ -152,17 +154,14 @@ int numberOfHfills(Paragraph const & par, Row const & row)
 
 
 void readParToken(Buffer const & buf, Paragraph & par, LyXLex & lex,
-       string const & token, LyXFont & font)
+       string const & token, LyXFont & font, Change & change)
 {
-       static Change change;
-
        BufferParams const & bp = buf.params();
 
        if (token[0] != '\\') {
                string::const_iterator cit = token.begin();
-               for (; cit != token.end(); ++cit) {
+               for (; cit != token.end(); ++cit)
                        par.insertChar(par.size(), (*cit), font, change);
-               }
        } else if (token == "\\begin_layout") {
                lex.eatLine();
                string layoutname = lex.getString();
@@ -180,7 +179,7 @@ void readParToken(Buffer const & buf, Paragraph & par, LyXLex & lex,
 
                if (!hasLayout) {
                        buf.error(ErrorItem(_("Unknown layout"),
-                       bformat(_("Layout '%1$s' does not exists in textclass '%2$s'\nTrying to use the default instead.\n"),
+                       bformat(_("Layout '%1$s' does not exist in textclass '%2$s'\nTrying to use the default instead.\n"),
                                layoutname, tclass.name()), par.id(), 0, par.size()));
                        layoutname = tclass.defaultLayoutName();
                }
@@ -347,10 +346,11 @@ void readParagraph(Buffer const & buf, Paragraph & par, LyXLex & lex)
        lex.nextToken();
        string token = lex.getString();
        LyXFont font;
+       Change change;
 
        while (lex.isOK()) {
 
-               readParToken(buf, par, lex, token, font);
+               readParToken(buf, par, lex, token, font, change);
 
                lex.nextToken();
                token = lex.getString();
@@ -420,8 +420,6 @@ int LyXText::singleWidth(Paragraph const & par, pos_type pos) const
 int LyXText::singleWidth(Paragraph const & par,
                         pos_type pos, char c, LyXFont const & font) const
 {
-       BOOST_ASSERT(pos < par.size());
-
        // The most common case is handled first (Asger)
        if (IsPrintable(c)) {
                Language const * language = font.language();
@@ -476,8 +474,8 @@ int LyXText::leftMargin(pit_type const pit, pos_type const pos) const
        l_margin += font_metrics::signedWidth(tclass.leftmargin(), tclass.defaultfont());
 
        if (par.getDepth() != 0) {
-       // find the next level paragraph
-       pit_type newpar = outerHook(pit, pars_);
+               // find the next level paragraph
+               pit_type newpar = outerHook(pit, pars_);
                if (newpar != pit_type(pars_.size())) {
                        if (pars_[newpar].layout()->isEnvironment()) {
                                l_margin = leftMargin(newpar);
@@ -491,6 +489,13 @@ int LyXText::leftMargin(pit_type const pit, pos_type const pos) const
                }
        }
 
+       // This happens after sections in standard classes. The 1.3.x
+       // code compared depths too, but it does not seem necessary
+       // (JMarc)
+       if (par.layout() == tclass.defaultLayout()
+           && pit > 0 && pars_[pit - 1].layout()->nextnoindent)
+               parindent.erase();
+
        LyXFont const labelfont = getLabelFont(par);
        switch (layout->margintype) {
        case MARGIN_DYNAMIC:
@@ -595,14 +600,12 @@ int LyXText::leftMargin(pit_type const pit, pos_type const pos) const
                   && !isFirstInSequence(pit, pars_)))
            && align == LYX_ALIGN_BLOCK
            && !par.params().noindent()
+           // in some insets, paragraphs are never indented
+           && !(par.inInset() && par.inInset()->neverIndent())
            // display style insets are always centered, omit indentation
            && !(!par.empty()
                    && par.isInset(pos)
                    && par.getInset(pos)->display())
-           // in charstyles, tabulars and ert paragraphs are never indented!
-           && ((par.ownerCode() != InsetBase::TEXT_CODE || isMainText())
-                   && par.ownerCode() != InsetBase::ERT_CODE
-                   && par.ownerCode() != InsetBase::CHARSTYLE_CODE)
            && (par.layout() != tclass.defaultLayout()
                || bv()->buffer()->params().paragraph_separation ==
                   BufferParams::PARSEP_INDENT))
@@ -702,24 +705,21 @@ void LyXText::rowBreakPoint(pit_type const pit, Row & row) const
        pos_type i = pos;
        for ( ; i < end; ++i, ++fi) {
                char const c = par.getChar(i);
+               int thiswidth = singleWidth(par, i, c, *fi);
 
-               {
-                       int thiswidth = singleWidth(par, i, c, *fi);
-
-                       // add the auto-hfill from label end to the body
-                       if (body_pos && i == body_pos) {
-                               int add = font_metrics::width(layout->labelsep, getLabelFont(par));
-                               if (par.isLineSeparator(i - 1))
-                                       add -= singleWidth(par, i - 1);
-
-                               add = std::max(add, labelEnd(pit) - x);
-                               thiswidth += add;
-                       }
+               // add the auto-hfill from label end to the body
+               if (body_pos && i == body_pos) {
+                       int add = font_metrics::width(layout->labelsep, getLabelFont(par));
+                       if (par.isLineSeparator(i - 1))
+                               add -= singleWidth(par, i - 1);
 
-                       x += thiswidth;
-                       chunkwidth += thiswidth;
+                       add = std::max(add, labelEnd(pit) - x);
+                       thiswidth += add;
                }
 
+               x += thiswidth;
+               chunkwidth += thiswidth;
+
                // break before a character that will fall off
                // the right of the row
                if (x >= width) {
@@ -1009,7 +1009,8 @@ void LyXText::setHeightOfRow(pit_type const pit, Row & row)
        if (bv_owner->text() == this) {
                if (pit == 0 && row.pos() == 0)
                        maxasc += 20;
-               if (pit + 1 == pars_.size() && row.endpos() == par.size())
+               if (pit + 1 == pit_type(pars_.size()) &&
+                   row.endpos() == par.size())
                        maxdesc += 20;
        }
 
@@ -1176,8 +1177,9 @@ void LyXText::insertChar(LCursor & cur, char c)
                        return;
                }
                BOOST_ASSERT(cur.pos() > 0);
-               if (par.isLineSeparator(cur.pos() - 1)
-                   || par.isNewline(cur.pos() - 1)) {
+               if ((par.isLineSeparator(cur.pos() - 1)
+                   || par.isNewline(cur.pos() - 1))
+                   && par.lookupChange(cur.pos() - 1) != Change::DELETED) {
                        static bool sent_space_message = false;
                        if (!sent_space_message) {
                                cur.message(_("You cannot type two spaces this way. "
@@ -1192,8 +1194,7 @@ void LyXText::insertChar(LCursor & cur, char c)
 
        current_font = rawtmpfont;
        real_current_font = realtmpfont;
-       //setCursor(cur, cur.pit(), cur.pos() + 1, false, cur.boundary());
-       setCursor(cur, cur.pit(), cur.pos() + 1, false, true);
+       setCursor(cur, cur.pit(), cur.pos() + 1, false, cur.boundary());
        charInserted();
 }
 
@@ -1549,12 +1550,22 @@ void LyXText::changeCase(LCursor & cur, LyXText::TextCase action)
 void LyXText::Delete(LCursor & cur)
 {
        BOOST_ASSERT(this == cur.text());
+
        if (cur.pos() != cur.lastpos()) {
                recordUndo(cur, Undo::DELETE, cur.pit());
                setCursorIntern(cur, cur.pit(), cur.pos() + 1, false, cur.boundary());
                backspace(cur);
+       } else if (cur.pit() != cur.lastpit()) {
+               LCursor scur = cur;
+
+               setCursorIntern(cur, cur.pit()+1, 0, false, false);
+               if (pars_[cur.pit()].layout() == pars_[scur.pit()].layout()) {
+                       recordUndo(scur, Undo::DELETE, scur.pit());
+                       backspace(cur);
+               } else {
+                       setCursorIntern(scur, scur.pit(), scur.pos(), false, scur.boundary());
+               }
        }
-       // should we do anything in an else branch?
 }
 
 
@@ -1617,6 +1628,7 @@ void LyXText::backspace(LCursor & cur)
                // layout. I think it is a real bug of all other
                // word processors to allow it. It confuses the user.
                // Correction: Pasting is always allowed with standard-layout
+               // Correction (Jug 20050717): Remove check about alignment!
                Buffer & buf = cur.buffer();
                BufferParams const & bufparams = buf.params();
                LyXTextClass const & tclass = bufparams.getLyXTextClass();
@@ -1624,8 +1636,8 @@ void LyXText::backspace(LCursor & cur)
 
                if (cpit != tmppit
                    && (pars_[cpit].layout() == pars_[tmppit].layout()
-                       || pars_[tmppit].layout() == tclass.defaultLayout())
-                   && pars_[cpit].getAlign() == pars_[tmppit].getAlign()) {
+                       || pars_[tmppit].layout() == tclass.defaultLayout()))
+               {
                        mergeParagraph(bufparams, pars_, cpit);
 
                        if (cur.pos() != 0 && pars_[cpit].isSeparator(cur.pos() - 1))
@@ -1655,15 +1667,6 @@ void LyXText::backspace(LCursor & cur)
 }
 
 
-Paragraph & LyXText::getPar(pit_type par) const
-{
-       //lyxerr << "getPar: " << par << " from " << paragraphs().size() << endl;
-       BOOST_ASSERT(par >= 0);
-       BOOST_ASSERT(par < int(paragraphs().size()));
-       return paragraphs()[par];
-}
-
-
 Row const & LyXText::firstRow() const
 {
        return *paragraphs().front().rows().begin();
@@ -1698,7 +1701,7 @@ bool LyXText::redoParagraph(pit_type const pit)
        InsetList::iterator iend = par.insetlist.end();
        for (; ii != iend; ++ii) {
                Dimension dim;
-               int const w = maxwidth_ - leftMargin(pit) - rightMargin(par);
+               int const w = maxwidth_ - leftMargin(pit, ii->pos) - rightMargin(par);
                MetricsInfo mi(bv(), getFont(par, ii->pos), w);
                ii->inset->metrics(mi, dim);
        }
@@ -1720,6 +1723,20 @@ bool LyXText::redoParagraph(pit_type const pit)
                z = row.endpos();
        } while (z < par.size());
 
+       // Make sure that if a par ends in newline, there is one more row
+       // under it
+       // FIXME this is a dirty trick. Now the _same_ position in the
+       // paragraph occurs in _two_ different rows, and has two different
+       // display positions, leading to weird behaviour when moving up/down.
+       if (z > 0 && par.isNewline(z - 1)) {
+               Row row(z - 1);
+               row.endpos(z - 1);
+               setRowWidth(pit, row);
+               setHeightOfRow(pit, row);
+               par.rows().push_back(row);
+               dim.des += row.height();
+       }
+           
        dim.asc += par.rows()[0].ascent();
        dim.des -= par.rows()[0].ascent();
 
@@ -1779,12 +1796,8 @@ void LyXText::drawSelection(PainterInfo & pi, int x , int) const
 
        lyxerr << "draw selection at " << x << endl;
 
-       // is there a better way of getting these two iterators?
-       DocIterator beg = cur;
-       DocIterator end = cur;
-
-       beg.top() = cur.selBegin();
-       end.top() = cur.selEnd();
+       DocIterator beg = cur.selectionBegin();
+       DocIterator end = cur.selectionEnd();
 
        // the selection doesn't touch the visible screen
        if (bv_funcs::status(pi.base.bv, beg) == bv_funcs::CUR_BELOW
@@ -1852,12 +1865,8 @@ void LyXText::drawSelection(PainterInfo & pi, int x, int) const
                 << "draw selection at " << x
                 << endl;
 
-       // is there a better way of getting these two iterators?
-       DocIterator beg = cur;
-       DocIterator end = cur;
-
-       beg.top() = cur.selBegin();
-       end.top() = cur.selEnd();
+       DocIterator beg = cur.selectionBegin();
+       DocIterator end = cur.selectionEnd();
 
        // the selection doesn't touch the visible screen
        if (bv_funcs::status(pi.base.bv, beg) == bv_funcs::CUR_BELOW
@@ -1881,7 +1890,7 @@ void LyXText::drawSelection(PainterInfo & pi, int x, int) const
                Row const & row1 = par1.getRow(beg.pos(), beg.boundary());
                y1 = bv_funcs::getPos(beg, beg.boundary()).y_ - row1.ascent();
                y2 = y1 + row1.height();
-               int const startx = cursorX(beg.top(), false);
+               int const startx = cursorX(beg.top(), beg.boundary());
                x1 = !isRTL(par1) ? startx : 0;
                x2 = !isRTL(par1) ? 0 + dim_.wid : startx;
        }
@@ -1896,20 +1905,25 @@ void LyXText::drawSelection(PainterInfo & pi, int x, int) const
                Row const & row2 = par2.getRow(end.pos(), end.boundary());
                Y1 = bv_funcs::getPos(end, end.boundary()).y_ - row2.ascent();
                Y2 = Y1 + row2.height();
-               int const endx = cursorX(end.top(), false);
+               int const endx = cursorX(end.top(), end.boundary());
                X1 = !isRTL(par2) ? 0 : endx;
                X2 = !isRTL(par2) ? endx : 0 + dim_.wid;
        }
 
-       if (!above && !below && &par1.getRow(beg.pos(), end.boundary())
+       if (!above && !below && &par1.getRow(beg.pos(), beg.boundary())
            == &par2.getRow(end.pos(), end.boundary()))
        {
                // paint only one rectangle
-               pi.pain.fillRectangle(x + x1, y1, X2 - x1, y2 - y1,
-                                     LColor::selection);
+               int const b( !isRTL(par1) ? x + x1 : x + X1 );
+               int const w( !isRTL(par1) ? X2 - x1 : x2 - X1 );
+               pi.pain.fillRectangle(b, y1, w, y2 - y1, LColor::selection);
                return;
        }
 
+       lyxerr[Debug::DEBUG] << " y1: " << y1 << " y2: " << y2
+              << "X1:" << X1 << " x2: " << X2 << " wid: " << dim_.wid
+               << endl;
+
        // paint upper rectangle
        pi.pain.fillRectangle(x + x1, y1, x2 - x1, y2 - y1,
                                      LColor::selection);
@@ -1987,8 +2001,6 @@ void LyXText::write(Buffer const & buf, std::ostream & os) const
 
 bool LyXText::read(Buffer const & buf, LyXLex & lex)
 {
-       static Change current_change;
-
        Paragraph::depth_type depth = 0;
 
        while (lex.isOK()) {
@@ -2098,6 +2110,10 @@ int LyXText::cursorX(CursorSlice const & sl, bool boundary) const
            (body_pos > end || !par.isLineSeparator(body_pos - 1)))
                body_pos = 0;
 
+       // Use font span to speed things up, see below
+       FontSpan font_span;
+       LyXFont font;
+
        for (pos_type vpos = row_pos; vpos < cursor_vpos; ++vpos) {
                pos_type pos = bidi.vis2log(vpos);
                if (body_pos > 0 && pos == body_pos - 1) {
@@ -2108,7 +2124,13 @@ int LyXText::cursorX(CursorSlice const & sl, bool boundary) const
                                x -= singleWidth(par, body_pos - 1);
                }
 
-               x += singleWidth(par, pos);
+               // Use font span to speed things up, see above
+               if (pos < font_span.first || pos > font_span.last) {
+                       font_span = par.fontSpan(pos);
+                       font = getFont(par, pos);
+               }
+
+               x += singleWidth(par, pos, par.getChar(pos), font);
 
                if (hfillExpansion(par, row, pos))
                        x += (pos >= body_pos) ? m.hfill : m.label_hfill;
@@ -2118,8 +2140,19 @@ int LyXText::cursorX(CursorSlice const & sl, bool boundary) const
        
        // see correction above
        if (boundary_correction)
-               x += singleWidth(par, ppos);
-
+               if (getFont(par, ppos).isVisibleRightToLeft())
+                       x -= singleWidth(par, ppos);
+               else
+                       x += singleWidth(par, ppos);
+
+       // Make sure inside an inset we always count from the left
+       // edge (bidi!) -- MV
+       if (sl.pos() < par.size()) {
+               font = getFont(par, sl.pos());
+               if (!boundary && font.isVisibleRightToLeft()
+                 && par.isInset(sl.pos()))
+                       x -= par.getInset(sl.pos())->width();
+       }       
        return int(x);
 }
 
@@ -2294,7 +2327,7 @@ pos_type LyXText::x2pos(pit_type pit, int row, int x) const
 
 // x,y are screen coordinates
 // sets cursor only within this LyXText
-void LyXText::setCursorFromCoordinates(LCursor & cur, int const x, int const y)
+bool LyXText::setCursorFromCoordinates(LCursor & cur, int const x, int const y)
 {
        pit_type pit = getPitNearY(y);
        int yy = theCoords.get(this, pit).y_ - pars_[pit].ascent();
@@ -2333,5 +2366,5 @@ void LyXText::setCursorFromCoordinates(LCursor & cur, int const x, int const y)
                 << " pos: " << pos
                 << endl;
         
-       setCursor(cur, pit, pos, true, bound);
+       return setCursor(cur, pit, pos, true, bound);
 }