]> git.lyx.org Git - lyx.git/blobdiff - src/text.C
Minor code shuffle.
[lyx.git] / src / text.C
index 3b9a19e085f17d7a6c251818b8944c97c48e7165..3b21f04f20fc104d03be607814ae2e5129b80ea5 100644 (file)
@@ -35,6 +35,7 @@
 #include "BufferView.h"
 #include "language.h"
 #include "ParagraphParameters.h"
+#include "undo_funcs.h"
 
 using std::max;
 using std::min;
@@ -436,9 +437,9 @@ void LyXText::draw(BufferView * bview, Row const * row,
                        tmpinset->draw(bview, font, offset+row->baseline(), x,
                                       cleared);
 #ifdef SEEMS_TO_BE_NOT_NEEDED
-                       if (status == CHANGED_IN_DRAW) {
+                       if (status_ == CHANGED_IN_DRAW) {
                                UpdateInset(bview, tmpinset);
-                               status = CHANGED_IN_DRAW;
+                               status(bview, CHANGED_IN_DRAW);
                        }
 #endif
                }
@@ -922,12 +923,7 @@ LyXText::nextBreakPoint(BufferView * bview, Row const * row, int width) const
                                                last_separator = i - 1;
                                        x = width;  // this means break
                                } else {
-#if 0
-                                       last_separator = i;
-                                       x += width;
-#else
                                        x += singleWidth(bview, par, i, c);
-#endif
                                }
                        } else  {
                                if (IsLineSeparatorChar(c))
@@ -1494,6 +1490,9 @@ void LyXText::appendParagraph(BufferView * bview, Row * row) const
         not_ready = false;
       
       // Set the dimensions of the row
+#ifdef WITH_WARNINGS
+#warning Something is rotten here! (Jug)
+#endif
       tmprow->fill(fill(bview, tmprow, workWidth(bview)));
       setHeightOfRow(bview, tmprow);
 
@@ -1599,9 +1598,7 @@ void LyXText::breakParagraph(BufferView * bview, char keep_layout)
        && layout.labeltype!= LABEL_SENSITIVE)
           return;
    
-   setUndo(bview->buffer(), Undo::INSERT,
-          cursor.par()->previous(), 
-          cursor.par()->next()); 
+   setUndo(bview, Undo::INSERT,cursor.par(),cursor.par()->next()); 
 
    // Always break behind a space
    //
@@ -1638,7 +1635,7 @@ void LyXText::breakParagraph(BufferView * bview, char keep_layout)
           cursorLeft(bview);
    } 
    
-   status = LyXText::NEED_MORE_REFRESH;
+   status(bview, LyXText::NEED_MORE_REFRESH);
    refresh_row = cursor.row();
    refresh_y = cursor.y() - cursor.row()->baseline();
    
@@ -1692,9 +1689,8 @@ void LyXText::redoParagraph(BufferView * bview) const
  * same Paragraph one to the right and make a rebreak */
 void LyXText::insertChar(BufferView * bview, char c)
 {
-       setUndo(bview->buffer(), Undo::INSERT,
-               cursor.par()->previous(),
-               cursor.par()->next());
+       setUndo(bview, Undo::INSERT,
+               cursor.par(), cursor.par()->next());
 
        // When the free-spacing option is set for the current layout,
        // disable the double-space checking
@@ -1857,7 +1853,7 @@ void LyXText::insertChar(BufferView * bview, char c)
                        y -= row->previous()->height();
                        refresh_y = y;
                        refresh_row = row->previous();
-                       status = LyXText::NEED_MORE_REFRESH;
+                       status(bview, LyXText::NEED_MORE_REFRESH);
             
                        breakAgainOneRow(bview, row);
 
@@ -1892,7 +1888,7 @@ void LyXText::insertChar(BufferView * bview, char c)
                refresh_row = row; 
                refresh_x = cursor.x();
                refresh_pos = cursor.pos();
-               status = LyXText::NEED_MORE_REFRESH;
+               status(bview, LyXText::NEED_MORE_REFRESH);
                breakAgainOneRow(bview, row); 
                // will the cursor be in another row now?
                if (rowLast(row) <= cursor.pos() + 1 && row->next()) {
@@ -1923,9 +1919,9 @@ void LyXText::insertChar(BufferView * bview, char c)
                int const tmpheight = row->height();
                setHeightOfRow(bview, row);
                if (tmpheight == row->height())
-                       status = LyXText::NEED_VERY_LITTLE_REFRESH;
+                       status(bview, LyXText::NEED_VERY_LITTLE_REFRESH);
                else
-                       status = LyXText::NEED_MORE_REFRESH;
+                       status(bview, LyXText::NEED_MORE_REFRESH);
             
                current_font = rawtmpfont;
                real_current_font = realtmpfont;
@@ -2020,7 +2016,7 @@ void LyXText::prepareToPrint(BufferView * bview,
           if (row->par()->getChar(row->pos()) == Paragraph::META_INSET
               && (inset=row->par()->getInset(row->pos()))
               && (inset->display())) // || (inset->scroll() < 0)))
-            align = (inset->LyxCode() == Inset::MATHMACRO_CODE)
+            align = (inset->lyxCode() == Inset::MATHMACRO_CODE)
                     ? LYX_ALIGN_BLOCK : LYX_ALIGN_CENTER;
 
           switch (align) {
@@ -2129,72 +2125,88 @@ void LyXText::cursorTab(BufferView * bview) const
 
 void LyXText::cursorLeftOneWord(BufferView * bview)  const
 {
-       // treat HFills, floats and Insets as words
        LyXCursor tmpcursor = cursor;
-       while (tmpcursor.pos() 
-              && (tmpcursor.par()->isSeparator(tmpcursor.pos() - 1) 
-                  || tmpcursor.par()->isKomma(tmpcursor.pos() - 1))
-              && !(tmpcursor.par()->isHfill(tmpcursor.pos() - 1)
-                   || tmpcursor.par()->isInset(tmpcursor.pos() - 1)))
-               tmpcursor.pos(tmpcursor.pos() - 1);
-
-       if (tmpcursor.pos()
-           && (tmpcursor.par()->isInset(tmpcursor.pos() - 1)
-               || tmpcursor.par()->isHfill(tmpcursor.pos() - 1))) {
-               tmpcursor.pos(tmpcursor.pos() - 1);
-       } else if (!tmpcursor.pos()) {
-               if (tmpcursor.par()->previous()){
-                       tmpcursor.par(tmpcursor.par()->previous());
-                       tmpcursor.pos(tmpcursor.par()->size());
-               }
-       } else {                // Here, tmpcursor != 0 
-               while (tmpcursor.pos() > 0 &&
-                      tmpcursor.par()->isWord(tmpcursor.pos()-1) )
-                       tmpcursor.pos(tmpcursor.pos() - 1);
-       }
+       cursorLeftOneWord(tmpcursor);
        setCursor(bview, tmpcursor.par(), tmpcursor.pos());
 }
 
+void LyXText::cursorLeftOneWord(LyXCursor  & cur)  const
+{
+       // treat HFills, floats and Insets as words
+       cur = cursor;
+       while (cur.pos() 
+              && (cur.par()->isSeparator(cur.pos() - 1) 
+                  || cur.par()->isKomma(cur.pos() - 1))
+              && !(cur.par()->isHfill(cur.pos() - 1)
+                   || cur.par()->isInset(cur.pos() - 1)))
+               cur.pos(cur.pos() - 1);
+
+       if (cur.pos()
+           && (cur.par()->isInset(cur.pos() - 1)
+               || cur.par()->isHfill(cur.pos() - 1))) {
+               cur.pos(cur.pos() - 1);
+       } else if (!cur.pos()) {
+               if (cur.par()->previous()){
+                       cur.par(cur.par()->previous());
+                       cur.pos(cur.par()->size());
+               }
+       } else {                // Here, cur != 0 
+               while (cur.pos() > 0 &&
+                      cur.par()->isWord(cur.pos()-1) )
+                       cur.pos(cur.pos() - 1);
+       }
+}
+
 /* -------> Select current word. This depends on behaviour of CursorLeftOneWord(), so it is
                        patched as well. */
 
-void LyXText::selectWord(BufferView * bview) 
+void LyXText::getWord(LyXCursor & from, LyXCursor & to, word_location loc) const
 {
-       // Move cursor to the beginning, when not already there.
-       if (cursor.pos()
-           && !cursor.par()->isSeparator(cursor.pos() - 1)
-           && !cursor.par()->isKomma(cursor.pos() - 1))
-               cursorLeftOneWord(bview);
+       // first put the cursor where we wana start to select the word
+       from = cursor;
+       switch(loc) {
+       case WHOLE_WORD:
+               // Move cursor to the beginning, when not already there.
+               if (from.pos() && !from.par()->isSeparator(from.pos() - 1)
+                   && !from.par()->isKomma(from.pos() - 1))
+                       cursorLeftOneWord(from);
+               break;
+       case NEXT_WORD:
+               lyxerr << "LyXText::getWord: NEXT_WORD not implemented yet\n";
+               break;
+       case PARTIAL_WORD:
+               break;
+       }
+       to = from;
+       while (to.pos() < to.par()->size()
+              && !to.par()->isSeparator(to.pos())
+              && !to.par()->isKomma(to.pos())
+              && !to.par()->isHfill(to.pos()) )
+       {
+               to.pos(to.pos() + 1);
+       }
+}
 
-       // set the sel cursor
-#if 0
-       sel_cursor = cursor;
-#else
+
+void LyXText::selectWord(BufferView * bview) 
+{
+       LyXCursor from;
+       LyXCursor to;
+       getWord(from, to, WHOLE_WORD);
+       if (cursor != from)
+               setCursor(bview, from.par(), from.pos());
        selection.cursor = cursor;
-#endif
-       
-       while (cursor.pos() < cursor.par()->size()
-              && !cursor.par()->isSeparator(cursor.pos())
-              && !cursor.par()->isKomma(cursor.pos()) )
-               cursor.pos(cursor.pos() + 1);
-       setCursor(bview, cursor.par(), cursor.pos() );
-       
-       // finally set the selection
+       setCursor(bview, to.par(), to.pos() );
        setSelection(bview);
 }
 
-
 /* -------> Select the word currently under the cursor when:
                        1: no selection is currently set,
                        2: the cursor is not at the borders of the word. */
 
 bool LyXText::selectWordWhenUnderCursor(BufferView * bview) 
 {
-#if 0
-       if (!selection &&
-#else
-           if (!selection.set() &&
-#endif
+       if (!selection.set() &&
            cursor.pos() > 0 && cursor.pos() < cursor.par()->size()
            && !cursor.par()->isSeparator(cursor.pos())
            && !cursor.par()->isKomma(cursor.pos())
@@ -2226,8 +2238,11 @@ string const LyXText::selectNextWord(BufferView * bview,
        // Now, skip until we have real text (will jump paragraphs)
        while ((cursor.par()->size() > cursor.pos()
                && (!cursor.par()->isLetter(cursor.pos())
+#ifndef NO_LATEX
                    || cursor.par()->getFont(bview->buffer()->params, cursor.pos())
-                   .latex() == LyXFont::ON))
+                   .latex() == LyXFont::ON
+#endif
+                       ))
               || (cursor.par()->size() == cursor.pos()
                   && cursor.par()->next())){
                if (cursor.pos() == cursor.par()->size()) {
@@ -2244,13 +2259,9 @@ string const LyXText::selectNextWord(BufferView * bview,
        }
 
        // Start the selection from here
-#if 0
-       sel_cursor = cursor;
-#else
        selection.cursor = cursor;
-#endif
        
-       std::ostringstream latex;
+       ostringstream latex;
 
        // and find the end of the word 
        // (optional hyphens are part of a word)
@@ -2258,22 +2269,13 @@ string const LyXText::selectNextWord(BufferView * bview,
               && (cursor.par()->isLetter(cursor.pos())) 
                   || (cursor.par()->getChar(cursor.pos()) == Paragraph::META_INSET
                       && cursor.par()->getInset(cursor.pos()) != 0
-                      && cursor.par()->getInset(cursor.pos())->Latex(bview->buffer(), latex, false, false) == 0
+                      && cursor.par()->getInset(cursor.pos())->latex(bview->buffer(), latex, false, false) == 0
                       && latex.str() == "\\-"
                           ))
                cursor.pos(cursor.pos() + 1);
 
        // Finally, we copy the word to a string and return it
        string str;
-#if 0
-       if (sel_cursor.pos() < cursor.pos()) {
-               Paragraph::size_type i;
-               for (i = sel_cursor.pos(); i < cursor.pos(); ++i) {
-                       if (cursor.par()->getChar(i) != Paragraph::META_INSET)
-                               str += cursor.par()->getChar(i);
-               }
-       }
-#else
        if (selection.cursor.pos() < cursor.pos()) {
                Paragraph::size_type i;
                for (i = selection.cursor.pos(); i < cursor.pos(); ++i) {
@@ -2281,7 +2283,6 @@ string const LyXText::selectNextWord(BufferView * bview,
                                str += cursor.par()->getChar(i);
                }
        }
-#endif
        return str;
 }
 
@@ -2289,27 +2290,19 @@ string const LyXText::selectNextWord(BufferView * bview,
 // This one is also only for the spellchecker
 void LyXText::selectSelectedWord(BufferView * bview)
 {
-#if 0
-       // move cursor to the beginning
-       setCursor(bview, sel_cursor.par(), sel_cursor.pos());
-       
-       // set the sel cursor
-       sel_cursor = cursor;
-#else
        // move cursor to the beginning
        setCursor(bview, selection.cursor.par(), selection.cursor.pos());
        
        // set the sel cursor
        selection.cursor = cursor;
-#endif
-       std::ostringstream latex;
+       ostringstream latex;
        
        // now find the end of the word
        while (cursor.pos() < cursor.par()->size()
               && (cursor.par()->isLetter(cursor.pos())
                   || (cursor.par()->getChar(cursor.pos()) == Paragraph::META_INSET
                       && cursor.par()->getInset(cursor.pos()) != 0
-                      && cursor.par()->getInset(cursor.pos())->Latex(bview->buffer(), latex, false, false) == 0
+                      && cursor.par()->getInset(cursor.pos())->latex(bview->buffer(), latex, false, false) == 0
                       && latex.str() == "\\-"
                           )))
                cursor.pos(cursor.pos() + 1);
@@ -2329,18 +2322,10 @@ void LyXText::deleteWordForward(BufferView * bview)
        else {
                LyXCursor tmpcursor = cursor;
                tmpcursor.row(0); // ??
-#if 0
-               selection = true; // to avoid deletion
-#else
                selection.set(true); // to avoid deletion
-#endif
                cursorRightOneWord(bview);
                setCursor(bview, tmpcursor, tmpcursor.par(), tmpcursor.pos());
-#if 0
-               sel_cursor = cursor;
-#else
                selection.cursor = cursor;
-#endif
                cursor = tmpcursor;
                setSelection(bview); 
                
@@ -2358,18 +2343,10 @@ void LyXText::deleteWordBackward(BufferView * bview)
        else {
               LyXCursor tmpcursor = cursor;
               tmpcursor.row(0); // ??
-#if 0
-              selection = true; // to avoid deletion
-#else
               selection.set(true); // to avoid deletion
-#endif
               cursorLeftOneWord(bview);
               setCursor(bview, tmpcursor, tmpcursor.par(), tmpcursor.pos());
-#if 0
-              sel_cursor = cursor;
-#else
               selection.cursor = cursor;
-#endif
               cursor = tmpcursor;
               setSelection(bview);
               cutSelection(bview);
@@ -2388,26 +2365,14 @@ void LyXText::deleteLineForward(BufferView * bview)
                // We can't store the row over a regular setCursor
                // so we set it to 0 and reset it afterwards.
                tmpcursor.row(0); // ??
-#if 0
-               selection = true; // to avoid deletion
-#else
                selection.set(true); // to avoid deletion
-#endif
                cursorEnd(bview);
                setCursor(bview, tmpcursor, tmpcursor.par(), tmpcursor.pos());
-#if 0
-               sel_cursor = cursor;
-#else
                selection.cursor = cursor;
-#endif
                cursor = tmpcursor;
                setSelection(bview);
                // What is this test for ??? (JMarc)
-#if 0
-               if (!selection) {
-#else
-                       if (!selection.set()) {
-#endif
+               if (!selection.set()) {
                        deleteWordForward(bview);
                } else {
                        cutSelection(bview);
@@ -2423,20 +2388,39 @@ void LyXText::deleteLineForward(BufferView * bview)
 // Paragraph, but it will have to change for 1.1 anyway. At least
 // it does not access outside of the allocated array as the older
 // version did. (JMarc) 
-void LyXText::changeWordCase(BufferView * bview, LyXText::TextCase action) 
+void LyXText::changeCase(BufferView * bview, LyXText::TextCase action)
 {
-       Paragraph * tmppar = cursor.par();
+       LyXCursor from;
+       LyXCursor to;
 
-       setUndo(bview->buffer(),Undo::FINISH,
-               tmppar->previous(), tmppar->next()); 
+       if (selection.set()) {
+               from = selection.start;
+               to = selection.end;
+       } else {
+               getWord(from, to, PARTIAL_WORD);
+               setCursor(bview, to.par(), to.pos() + 1);
+       }
 
-       Paragraph::size_type tmppos = cursor.pos();
+       changeRegionCase(bview, from, to, action);
+}
 
-       while (tmppos < tmppar->size()) {
-               unsigned char c = tmppar->getChar(tmppos);
-               if (IsKommaChar(c) || IsLineSeparatorChar(c))
-                       break;
-               if (c != Paragraph::META_INSET) {
+
+void LyXText::changeRegionCase(BufferView * bview,
+                              LyXCursor const & from,
+                              LyXCursor const & to,
+                              LyXText::TextCase action)
+{
+       lyx::Assert(from <= to);
+       
+       setUndo(bview, Undo::FINISH,
+               from.par(), to.par()->next());
+
+       Paragraph::size_type pos = from.pos();
+       Paragraph * par = from.par();
+
+       while (par && (pos != to.pos() || par != to.par())) {
+               unsigned char c = par->getChar(pos);
+               if (!IsInsetChar(c) && !IsHfillChar(c)) {
                        switch (action) {
                        case text_lowercase:
                                c = tolower(c);
@@ -2450,22 +2434,29 @@ void LyXText::changeWordCase(BufferView * bview, LyXText::TextCase action)
                                break;
                        }
                }
-               
-               //tmppar->text[tmppos] = c;
-               tmppar->setChar(tmppos, c);
-               ++tmppos;
+               par->setChar(pos, c);
+               checkParagraph(bview, par, pos);
+
+               ++pos;
+               if (pos == par->size()) {
+                       par = par->next();
+                       pos = 0;
+               }
+       }
+       if (to.row() != from.row()) {
+               refresh_y = from.y() - from.row()->baseline();
+               refresh_row = from.row();
+               status(bview, LyXText::NEED_MORE_REFRESH);
        }
-       checkParagraph(bview, tmppar, tmppos);
-       cursorRightOneWord(bview);
 }
 
 
-void LyXText::transposeChars(BufferView const & bview)
+void LyXText::transposeChars(BufferView & bview)
 {
        Paragraph * tmppar = cursor.par();
 
-       setUndo(bview.buffer(), Undo::FINISH,
-               tmppar->previous(), tmppar->next()); 
+       setUndo(&bview, Undo::FINISH,
+               tmppar, tmppar->next()); 
 
        Paragraph::size_type tmppos = cursor.pos();
 
@@ -2518,9 +2509,8 @@ void LyXText::Delete(BufferView * bview)
        if (old_cursor.par() != cursor.par() || old_cursor.pos() != cursor.pos()) {
                LyXCursor tmpcursor = cursor;
                cursor = old_cursor; // to make sure undo gets the right cursor position
-               setUndo(bview->buffer(), Undo::DELETE,
-                       cursor.par()->previous(), 
-                       cursor.par()->next()); 
+               setUndo(bview, Undo::DELETE,
+                       cursor.par(), cursor.par()->next()); 
                cursor = tmpcursor;
                backspace(bview);
        }
@@ -2568,15 +2558,15 @@ void LyXText::backspace(BufferView * bview)
                                if (cursor.row()->height() != tmpheight) {
                                        refresh_y = cursor.y() - cursor.row()->baseline();
                                        refresh_row = cursor.row();
-                                       status = LyXText::NEED_MORE_REFRESH;
+                                       status(bview, LyXText::NEED_MORE_REFRESH);
                                }
                                return;
                        }
                }
 
                if (cursor.par()->previous()) {
-                       setUndo(bview->buffer(), Undo::DELETE,
-                               cursor.par()->previous()->previous(),
+                       setUndo(bview, Undo::DELETE,
+                               cursor.par()->previous(),
                                cursor.par()->next());
                }
                
@@ -2622,7 +2612,7 @@ void LyXText::backspace(BufferView * bview)
                                if (cursor.pos())
                                        cursor.pos(cursor.pos() - 1);
                        
-                       status = LyXText::NEED_MORE_REFRESH;
+                       status(bview, LyXText::NEED_MORE_REFRESH);
                        refresh_row = cursor.row();
                        refresh_y = cursor.y() - cursor.row()->baseline();
                        
@@ -2644,9 +2634,8 @@ void LyXText::backspace(BufferView * bview)
        } else {
                /* this is the code for a normal backspace, not pasting
                 * any paragraphs */ 
-               setUndo(bview->buffer(), Undo::DELETE,
-                       cursor.par()->previous(),
-                       cursor.par()->next()); 
+               setUndo(bview, Undo::DELETE,
+                       cursor.par(), cursor.par()->next()); 
                // We used to do cursorLeftIntern() here, but it is
                // not a good idea since it triggers the auto-delete
                // mechanism. So we do a cursorLeftIntern()-lite,
@@ -2656,7 +2645,7 @@ void LyXText::backspace(BufferView * bview)
                
                // some insets are undeletable here
                if (cursor.par()->getChar(cursor.pos()) == Paragraph::META_INSET) {
-                       if (!cursor.par()->getInset(cursor.pos())->Deletable())
+                       if (!cursor.par()->getInset(cursor.pos())->deletable())
                                return; 
                        // force complete redo when erasing display insets
                        // this is a cruel method but safe..... Matthias 
@@ -2762,7 +2751,7 @@ void LyXText::backspace(BufferView * bview)
                                
                                refresh_y = y;
                                refresh_row = tmprow;
-                               status = LyXText::NEED_MORE_REFRESH;
+                               status(bview, LyXText::NEED_MORE_REFRESH);
                                setCursor(bview, cursor.par(), cursor.pos(),
                                          false, cursor.boundary());
                                //current_font = rawtmpfont;
@@ -2790,7 +2779,7 @@ void LyXText::backspace(BufferView * bview)
                        
                        refresh_y = y;
                        refresh_row = row;
-                       status = LyXText::NEED_MORE_REFRESH;
+                       status(bview, LyXText::NEED_MORE_REFRESH);
                        
                        breakAgainOneRow(bview, row);
                        // will the cursor be in another row now?
@@ -2812,9 +2801,9 @@ void LyXText::backspace(BufferView * bview)
                        int const tmpheight = row->height();
                        setHeightOfRow(bview, row);
                        if (tmpheight == row->height())
-                               status = LyXText::NEED_VERY_LITTLE_REFRESH;
+                               status(bview, LyXText::NEED_VERY_LITTLE_REFRESH);
                        else
-                               status = LyXText::NEED_MORE_REFRESH;
+                               status(bview, LyXText::NEED_MORE_REFRESH);
                        refresh_y = y;
                        refresh_row = row;
                        setCursor(bview, cursor.par(), cursor.pos(), false, cursor.boundary());
@@ -3520,7 +3509,7 @@ int LyXText::getColumnNearX(BufferView * bview, Row * row, int & x,
        float last_tmpx = tmpx;
        
        if (main_body > 0 &&
-           (main_body-1 > last || 
+           (main_body - 1 > last || 
             !row->par()->isLineSeparator(main_body - 1)))
                main_body = 0;
        
@@ -3551,7 +3540,7 @@ int LyXText::getColumnNearX(BufferView * bview, Row * row, int & x,
                ++vc;
        }
        
-       if (1 || (tmpx + last_tmpx) / 2 > x) {
+       if ((tmpx + last_tmpx) / 2 > x) {
                tmpx = last_tmpx;
                left_side = true;
        }