]> git.lyx.org Git - lyx.git/blobdiff - src/Text2.cpp
SCons: msvc does not need this /TP option any more after we rename .C => .cpp. Also...
[lyx.git] / src / Text2.cpp
index bbb2c4233bdc3600b24c45fd41295a51e36f37a2..92eaba739b9ab2fc193f5364f56a08b9a36e5502 100644 (file)
@@ -11,6 +11,7 @@
  * \author John Levon
  * \author André Pönitz
  * \author Allan Rae
+ * \author Stefan Schimanski
  * \author Dekel Tsur
  * \author Jürgen Vigna
  *
@@ -28,6 +29,7 @@
 #include "BufferView.h"
 #include "bufferview_funcs.h"
 #include "Bullet.h"
+#include "Color.h"
 #include "CoordCache.h"
 #include "Cursor.h"
 #include "CutAndPaste.h"
@@ -37,7 +39,7 @@
 #include "FuncRequest.h"
 #include "gettext.h"
 #include "Language.h"
-#include "Color.h"
+#include "Lexer.h"
 #include "LyXFunc.h"
 #include "LyXRC.h"
 #include "Row.h"
@@ -71,7 +73,7 @@ using std::ostringstream;
 using std::string;
 using std::max;
 using std::min;
-
+using std::istringstream;
 
 Text::Text()
        : current_font(Font::ALL_INHERIT),
@@ -149,9 +151,7 @@ Font Text::getFont(Buffer const & buffer, Paragraph const & par,
        BOOST_ASSERT(pos >= 0);
 
        Layout_ptr const & layout = par.layout();
-#ifdef WITH_WARNINGS
-#warning broken?
-#endif
+       // FIXME: broken?
        BufferParams const & params = buffer.params();
        pos_type const body_pos = par.beginOfBody();
 
@@ -314,25 +314,16 @@ void Text::setInsetFont(Buffer const & buffer, pit_type pit,
                     pars_[pit].getInset(pos)->noFontChange());
 
        Inset * const inset = pars_[pit].getInset(pos);
-       DocIterator dit = doc_iterator_begin(*inset);
-       // start of the last cell
-       DocIterator end = dit;
-       end.idx() = end.lastidx();
-
-       while (true) {
-               Text * text = dit.text();
-               Inset * cell = dit.realInset();
-               if (text && cell) {
-                       DocIterator cellbegin = doc_iterator_begin(*cell);
+       CursorSlice::idx_type endidx = inset->nargs();
+       for (CursorSlice cs(*inset); cs.idx() != endidx; ++cs.idx()) {
+               Text * text = cs.text();
+               if (text) {
                        // last position of the cell
-                       DocIterator cellend = cellbegin;
+                       CursorSlice cellend = cs;
                        cellend.pit() = cellend.lastpit();
                        cellend.pos() = cellend.lastpos();
-                       text->setFont(buffer, cellbegin, cellend, font, toggleall);
+                       text->setFont(buffer, cs, cellend, font, toggleall);
                }
-               if (dit == end)
-                       break;
-               dit.forwardIdx();
        }
 }
 
@@ -357,7 +348,7 @@ pit_type Text::undoSpan(pit_type pit)
 
 
 void Text::setLayout(Buffer const & buffer, pit_type start, pit_type end,
-               string const & layout)
+               docstring const & layout)
 {
        BOOST_ASSERT(start != end);
 
@@ -375,16 +366,16 @@ void Text::setLayout(Buffer const & buffer, pit_type start, pit_type end,
 
 
 // set layout over selection and make a total rebreak of those paragraphs
-void Text::setLayout(Cursor & cur, string const & layout)
+void Text::setLayout(Cursor & cur, docstring const & layout)
 {
        BOOST_ASSERT(this == cur.text());
        // special handling of new environment insets
        BufferView & bv = cur.bv();
-       BufferParams const & params = bv.buffer()->params();
+       BufferParams const & params = bv.buffer().params();
        Layout_ptr const & lyxlayout = params.getTextClass()[layout];
        if (lyxlayout->is_environment) {
                // move everything in a new environment inset
-               LYXERR(Debug::DEBUG) << "setting layout " << layout << endl;
+               LYXERR(Debug::DEBUG) << "setting layout " << to_utf8(layout) << endl;
                lyx::dispatch(FuncRequest(LFUN_LINE_BEGIN));
                lyx::dispatch(FuncRequest(LFUN_LINE_END_SELECT));
                lyx::dispatch(FuncRequest(LFUN_CUT));
@@ -493,13 +484,13 @@ void Text::setFont(Cursor & cur, Font const & font, bool toggleall)
        // Ok, we have a selection.
        recordUndoSelection(cur);
 
-       setFont(cur.buffer(), cur.selectionBegin(), cur.selectionEnd(), font,
-               toggleall);
+       setFont(cur.buffer(), cur.selectionBegin().top(), 
+               cur.selectionEnd().top(), font, toggleall);
 }
 
 
-void Text::setFont(Buffer const & buffer, DocIterator const & begin,
-               DocIterator const & end, Font const & font,
+void Text::setFont(Buffer const & buffer, CursorSlice const & begin,
+               CursorSlice const & end, Font const & font,
                bool toggleall)
 {
        // Don't use forwardChar here as ditend might have
@@ -507,7 +498,7 @@ void Text::setFont(Buffer const & buffer, DocIterator const & begin,
        // Can't use forwardPos either as this descends into
        // nested insets.
        Language const * language = buffer.params().language;
-       for (DocIterator dit = begin; dit != end; dit.forwardPosNoDescend()) {
+       for (CursorSlice dit = begin; dit != end; dit.forwardPos()) {
                if (dit.pos() != dit.lastpos()) {
                        pit_type const pit = dit.pit();
                        pos_type const pos = dit.pos();
@@ -632,9 +623,7 @@ docstring Text::getStringToIndex(Cursor const & cur)
 }
 
 
-void Text::setParagraph(Cursor & cur,
-                          Spacing const & spacing, LyXAlignment align,
-                          docstring const & labelwidthstring, bool noindent)
+void Text::setParagraphs(Cursor & cur, docstring arg, bool merge) 
 {
        BOOST_ASSERT(cur.text());
        // make sure that the depth behind the selection are restored, too
@@ -644,23 +633,30 @@ void Text::setParagraph(Cursor & cur,
        for (pit_type pit = cur.selBegin().pit(), end = cur.selEnd().pit();
             pit <= end; ++pit) {
                Paragraph & par = pars_[pit];
-               ParagraphParameters & params = par.params();
-               params.spacing(spacing);
+               ParagraphParameters params = par.params();
+               params.read(to_utf8(arg), merge);
+               Layout const & layout = *(par.layout());
+               par.params().apply(params, layout);
+       }
+}
 
-               // does the layout allow the new alignment?
-               Layout_ptr const & layout = par.layout();
 
-               if (align == LYX_ALIGN_LAYOUT)
-                       align = layout->align;
-               if (align & layout->alignpossible) {
-                       if (align == layout->align)
-                               params.align(LYX_ALIGN_LAYOUT);
-                       else
-                               params.align(align);
-               }
-               par.setLabelWidthString(labelwidthstring);
-               params.noindent(noindent);
-       }
+//FIXME This is a little redundant now, but it's probably worth keeping,
+//especially if we're going to go away from using serialization internally
+//quite so much.
+void Text::setParagraphs(Cursor & cur, ParagraphParameters const & p) 
+{
+       BOOST_ASSERT(cur.text());
+       // make sure that the depth behind the selection are restored, too
+       pit_type undopit = undoSpan(cur.selEnd().pit());
+       recUndo(cur, cur.selBegin().pit(), undopit - 1);
+
+       for (pit_type pit = cur.selBegin().pit(), end = cur.selEnd().pit();
+            pit <= end; ++pit) {
+               Paragraph & par = pars_[pit];
+               Layout const & layout = *(par.layout());
+               par.params().apply(p, layout);
+       }       
 }
 
 
@@ -669,7 +665,7 @@ void Text::insertInset(Cursor & cur, Inset * inset)
 {
        BOOST_ASSERT(this == cur.text());
        BOOST_ASSERT(inset);
-       cur.paragraph().insertInset(cur.pos(), inset,
+       cur.paragraph().insertInset(cur.pos(), inset, current_font,
                                    Change(cur.buffer().params().trackChanges ?
                                           Change::INSERTED : Change::UNCHANGED));
 }
@@ -760,42 +756,33 @@ void Text::setCurrentFont(Cursor & cur)
        pos_type pos = cur.pos();
        Paragraph & par = cur.paragraph();
 
-       // ignore empty paragraph
-       if (par.empty())
-               return;
-       
-       // if on boundary or at paragraph end, set font of previous char
-       if ((pos > 0 && cur.boundary()) || pos == cur.lastpos())
+       // are we behind previous char in fact? -> go to that char
+       if (pos > 0 && cur.boundary())
                --pos;
-       
-       // we changed the line and the bidi tables are outdated?
-       if (!bidi.inRange(pos))
-               bidi.computeTables(par, cur.buffer(), cur.textRow());
 
-       // now in range?
-       if (!bidi.inRange(pos))
-               return;
-
-       if (pos > 0) {
+       // find position to take the font from
+       if (pos != 0) {
+               // paragraph end? -> font of last char
                if (pos == cur.lastpos())
                        --pos;
-               else // potentional bug... BUG (Lgb)
-                       if (par.isSeparator(pos)) {
-                               if (pos > cur.textRow().pos() &&
-                                   bidi.level(pos) % 2 ==
-                                   bidi.level(pos - 1) % 2)
-                                       --pos;
-                               else if (pos + 1 < cur.lastpos())
-                                       ++pos;
-                       }
+               // on space? -> look at the words in front of space
+               else if (pos > 0 && par.isSeparator(pos))       {
+                       // abc| def -> font of c
+                       // abc |[WERBEH], i.e. boundary==true -> font of c
+                       // abc [WERBEH]| def, font of the space
+                       if (!isRTLBoundary(cur.buffer(), par, pos))
+                               --pos;
+               }
        }
 
+       // get font
        BufferParams const & bufparams = cur.buffer().params();
        current_font = par.getFontSettings(bufparams, pos);
        real_current_font = getFont(cur.buffer(), par, pos);
 
+       // special case for paragraph end
        if (cur.pos() == cur.lastpos()
-           && bidi.isBoundary(cur.buffer(), par, cur.pos())
+           && isRTLBoundary(cur.buffer(), par, cur.pos())
            && !cur.boundary()) {
                Language const * lang = par.getParLanguage(bufparams);
                current_font.setLanguage(lang);
@@ -1037,7 +1024,7 @@ bool Text::cursorRight(Cursor & cur)
                // if left of boundary -> just jump to right side
          // but for RTL boundaries don't, because: abc|DDEEFFghi -> abcDDEEF|Fghi
          if (cur.boundary() && 
-                               !bidi.isBoundary(cur.buffer(), cur.paragraph(), cur.pos()))
+                               !isRTLBoundary(cur.buffer(), cur.paragraph(), cur.pos()))
                        return setCursor(cur, cur.pit(), cur.pos(), true, false);
 
                // next position is left of boundary, 
@@ -1066,7 +1053,7 @@ bool Text::cursorRight(Cursor & cur)
                
                // in front of RTL boundary? Stay on this side of the boundary because:
                //   ab|cDDEEFFghi -> abc|DDEEFFghi
-               if (bidi.isBoundary(cur.buffer(), cur.paragraph(), cur.pos() + 1))
+               if (isRTLBoundary(cur.buffer(), cur.paragraph(), cur.pos() + 1))
                        return setCursor(cur, cur.pit(), cur.pos() + 1, true, true);
                
                // move right
@@ -1163,12 +1150,10 @@ bool Text::deleteEmptyParagraphMechanism(Cursor & cur,
                    && oldpar.isLineSeparator(old.pos() - 1)
                    && !oldpar.isDeleted(old.pos() - 1)) {
                        oldpar.eraseChar(old.pos() - 1, cur.buffer().params().trackChanges);
-#ifdef WITH_WARNINGS
-#warning This will not work anymore when we have multiple views of the same buffer
+// FIXME: This will not work anymore when we have multiple views of the same buffer
 // In this case, we will have to correct also the cursors held by
 // other bufferviews. It will probably be easier to do that in a more
 // automated way in CursorSlice code. (JMarc 26/09/2001)
-#endif
                        // correct all cursor parts
                        if (same_par) {
                                fixCursorAfterDelete(cur.top(), old.top());
@@ -1198,7 +1183,7 @@ bool Text::deleteEmptyParagraphMechanism(Cursor & cur,
                ParagraphList & plist = old.text()->paragraphs();
                plist.erase(boost::next(plist.begin(), old.pit()));
 
-               // see #warning above
+               // see #warning (FIXME?) above 
                if (cur.depth() >= old.depth()) {
                        CursorSlice & curslice = cur[old.depth() - 1];
                        if (&curslice.inset() == &old.inset()