]> git.lyx.org Git - lyx.git/blobdiff - src/text3.C
reenable assertion
[lyx.git] / src / text3.C
index 00bb118d73685d72dbbc7fa151be8799fb01d05e..840ec68e1caabfc67a5d8bbe4b08b4a79fd3eb1c 100644 (file)
@@ -24,6 +24,7 @@
 #include "buffer_funcs.h"
 #include "bufferparams.h"
 #include "BufferView.h"
+#include "bufferview_funcs.h"
 #include "cursor.h"
 #include "coordcache.h"
 #include "CutAndPaste.h"
@@ -143,7 +144,9 @@ namespace {
                replaceSelection(cur);
 
                if (sel.empty()) {
+#ifdef ENABLE_ASSERTIONS
                        const int old_pos = cur.pos();
+#endif
                        cur.insert(new InsetMathHull(hullSimple));
                        BOOST_ASSERT(old_pos == cur.pos());
                        cur.nextInset()->edit(cur, true);
@@ -198,48 +201,42 @@ string const freefont2string()
 
 }
 
-bool LyXText::cursorPrevious(LCursor & cur)
+void LyXText::cursorPrevious(LCursor & cur)
 {
        pos_type cpos = cur.pos();
        pit_type cpar = cur.pit();
 
        int x = cur.x_target();
+       setCursorFromCoordinates(cur, x, 0);
+       cur.dispatch(FuncRequest(cur.selection()? LFUN_UP_SELECT: LFUN_UP));
 
-       bool updated = setCursorFromCoordinates(cur, x, 0);
-       if (updated)
-               cur.bv().update();
-       updated |= cursorUp(cur);
-
-       if (cpar == cur.pit() && cpos == cur.pos()) {
+       if (cpar == cur.pit() && cpos == cur.pos())
                // we have a row which is taller than the workarea. The
                // simplest solution is to move to the previous row instead.
-               updated |= cursorUp(cur);
-       }
+               cur.dispatch(FuncRequest(cur.selection()? LFUN_UP_SELECT: LFUN_UP));
 
        finishUndo();
-       return updated;
+       cur.updateFlags(Update::Force | Update::FitCursor);
 }
 
 
-bool LyXText::cursorNext(LCursor & cur)
+void LyXText::cursorNext(LCursor & cur)
 {
        pos_type cpos = cur.pos();
        pit_type cpar = cur.pit();
 
        int x = cur.x_target();
-       bool updated = setCursorFromCoordinates(cur, x, cur.bv().workHeight() - 1);
-       if (updated)
-               cur.bv().update();
-       updated |= cursorDown(cur);
+       setCursorFromCoordinates(cur, x, cur.bv().workHeight() - 1);
+       cur.dispatch(FuncRequest(cur.selection()? LFUN_DOWN_SELECT: LFUN_DOWN));
 
-       if (cpar == cur.pit() && cpos == cur.pos()) {
+       if (cpar == cur.pit() && cpos == cur.pos())
                // we have a row which is taller than the workarea. The
                // simplest solution is to move to the next row instead.
-               updated |= cursorDown(cur);
-       }
+               cur.dispatch(
+                       FuncRequest(cur.selection()? LFUN_DOWN_SELECT: LFUN_DOWN));
 
        finishUndo();
-       return updated;
+       cur.updateFlags(Update::Force | Update::FitCursor);
 }
 
 
@@ -272,7 +269,7 @@ bool doInsertInset(LCursor & cur, LyXText * text,
                inset->edit(cur, true);
 
        if (gotsel && pastesel) {
-               lyx::dispatch(FuncRequest(LFUN_PASTE));
+               lyx::dispatch(FuncRequest(LFUN_PASTE, "0"));
                // reset first par to default
                if (cur.lastpit() != 0 || cur.lastpos() != 0) {
                        LyXLayout_ptr const layout =
@@ -305,6 +302,11 @@ void LyXText::dispatch(LCursor & cur, FuncRequest & cmd)
 {
        lyxerr[Debug::ACTION] << "LyXText::dispatch: cmd: " << cmd << endl;
 
+       // FIXME: We use the update flag to indicates wether a singlePar or a
+       // full screen update is needed. We reset it here but shall we restore it
+       // at the end?
+       cur.noUpdate();
+
        BOOST_ASSERT(cur.text() == this);
        BufferView * bv = &cur.bv();
        CursorSlice oldTopSlice = cur.top();
@@ -318,7 +320,9 @@ void LyXText::dispatch(LCursor & cur, FuncRequest & cmd)
        bool needsUpdate = !(lyxaction.funcHasFlag(cmd.action,
                LyXAction::NoUpdate) || singleParUpdate);
        // Remember the old paragraph metric (_outer_ paragraph!)
-       Dimension olddim = cur.bottom().paragraph().dim();
+       ParagraphMetrics const & pm = cur.bv().parMetrics(
+               cur.bottom().text(), cur.bottom().pit());
+       Dimension olddim = pm.dim();
 
        switch (cmd.action) {
 
@@ -495,7 +499,7 @@ void LyXText::dispatch(LCursor & cur, FuncRequest & cmd)
                        cur.undispatched();
                        cmd = FuncRequest(LFUN_FINISHED_UP);
                } else {
-                       needsUpdate |= cursorPrevious(cur);
+                       cursorPrevious(cur);
                }
                break;
 
@@ -507,7 +511,7 @@ void LyXText::dispatch(LCursor & cur, FuncRequest & cmd)
                        cur.undispatched();
                        cmd = FuncRequest(LFUN_FINISHED_DOWN);
                } else {
-                       needsUpdate |= cursorNext(cur);
+                       cursorNext(cur);
                }
                break;
 
@@ -754,13 +758,16 @@ void LyXText::dispatch(LCursor & cur, FuncRequest & cmd)
        case LFUN_PASTE:
                cur.message(_("Paste"));
                cap::replaceSelection(cur);
-               if (isStrUnsignedInt(to_utf8(cmd.argument())))
-                       pasteSelection(cur, bv->buffer()->errorList("Paste"),
-                       convert<unsigned int>(to_utf8(cmd.argument())));
-               else
+               if (cmd.argument().empty() && !theClipboard().isInternal())
+                       pasteString(cur, theClipboard().get(), docstring());
+               else {
+                       string const arg(to_utf8(cmd.argument()));
                        pasteSelection(cur, bv->buffer()->errorList("Paste"),
-                       0);
-               bv->buffer()->errors("Paste");
+                                       isStrUnsignedInt(arg) ?
+                                               convert<unsigned int>(arg) :
+                                               0);
+                       bv->buffer()->errors("Paste");
+               }
                cur.clearSelection(); // bug 393
                bv->switchKeyMap();
                finishUndo();
@@ -778,8 +785,8 @@ void LyXText::dispatch(LCursor & cur, FuncRequest & cmd)
 
        case LFUN_SERVER_GET_XY:
                cur.message(from_utf8(
-                       convert<string>(cursorX(cur.buffer(), cur.top(), cur.boundary()))
-                       + ' ' + convert<string>(cursorY(cur.top(), cur.boundary()))));
+                       convert<string>(cursorX(cur.bv(), cur.top(), cur.boundary()))
+                       + ' ' + convert<string>(cursorY(cur.bv(), cur.top(), cur.boundary()))));
                break;
 
        case LFUN_SERVER_SET_XY: {
@@ -858,31 +865,13 @@ void LyXText::dispatch(LCursor & cur, FuncRequest & cmd)
                break;
        }
 
-       case LFUN_CLIPBOARD_PASTE: {
-               cur.clearSelection();
-               docstring const clip = theClipboard().get();
-               if (!clip.empty()) {
-                       recordUndo(cur);
-                       if (cmd.argument() == "paragraph")
-                               insertStringAsParagraphs(cur, clip);
-                       else
-                               insertStringAsLines(cur, clip);
-               }
+       case LFUN_CLIPBOARD_PASTE:
+               pasteString(cur, theClipboard().get(), cmd.argument());
                break;
-       }
 
-       case LFUN_PRIMARY_SELECTION_PASTE: {
-               cur.clearSelection();
-               docstring const clip = theSelection().get();
-               if (!clip.empty()) {
-                       recordUndo(cur);
-                       if (cmd.argument() == "paragraph")
-                               insertStringAsParagraphs(cur, clip);
-                       else
-                               insertStringAsLines(cur, clip);
-               }
+       case LFUN_PRIMARY_SELECTION_PASTE:
+               pasteString(cur, theSelection().get(), cmd.argument());
                break;
-       }
 
        case LFUN_UNICODE_INSERT: {
                if (cmd.argument().empty())
@@ -976,18 +965,24 @@ void LyXText::dispatch(LCursor & cur, FuncRequest & cmd)
                        paste_internally = true;
                }
 
-               bv->mouseSetCursor(cur);
+               // we have to update after dePM triggered
+               bool update = bv->mouseSetCursor(cur);
 
                // Insert primary selection with middle mouse
                // if there is a local selection in the current buffer,
                // insert this
                if (cmd.button() == mouse_button::button2) {
                        if (paste_internally)
-                               lyx::dispatch(FuncRequest(LFUN_PASTE));
+                               lyx::dispatch(FuncRequest(LFUN_PASTE, "0"));
                        else
                                lyx::dispatch(FuncRequest(LFUN_PRIMARY_SELECTION_PASTE, "paragraph"));
                }
 
+               if (!update && cmd.button() == mouse_button::button1) {
+                       needsUpdate = false;
+                       cur.noUpdate();
+               }
+
                break;
        }
 
@@ -1007,15 +1002,15 @@ void LyXText::dispatch(LCursor & cur, FuncRequest & cmd)
                        setCursorFromCoordinates(cur, cmd.x, y);
                        cur.x_target() = cmd.x;
                        if (cmd.y >= wh)
-                               cursorDown(cur);
+                               lyx::dispatch(FuncRequest(LFUN_DOWN_SELECT));
                        else if (cmd.y < 0)
-                               cursorUp(cur);
+                               lyx::dispatch(FuncRequest(LFUN_UP_SELECT));
                        // This is to allow jumping over large insets
                        if (cur.top() == old) {
                                if (cmd.y >= wh)
-                                       cursorDown(cur);
+                                       lyx::dispatch(FuncRequest(LFUN_DOWN_SELECT));
                                else if (cmd.y < 0)
-                                       cursorUp(cur);
+                                       lyx::dispatch(FuncRequest(LFUN_UP_SELECT));
                        }
 
                        if (cur.top() == old)
@@ -1037,8 +1032,12 @@ void LyXText::dispatch(LCursor & cur, FuncRequest & cmd)
                        break;
 
                // finish selection
-               if (cmd.button() == mouse_button::button1)
-                       theSelection().haveSelection(cur.selection());
+               if (cmd.button() == mouse_button::button1) {
+                       if (cur.selection())
+                               theSelection().haveSelection(cur.selection());
+                       needsUpdate = false;
+                       cur.noUpdate();
+               }
 
                bv->switchKeyMap();
                break;
@@ -1175,6 +1174,8 @@ void LyXText::dispatch(LCursor & cur, FuncRequest & cmd)
        case LFUN_HFILL_INSERT:
        case LFUN_LINE_INSERT:
        case LFUN_PAGEBREAK_INSERT:
+       case LFUN_CLEARPAGE_INSERT:
+       case LFUN_CLEARDOUBLEPAGE_INSERT:
                // do nothing fancy
                doInsertInset(cur, this, cmd, false, false);
                cur.posRight();
@@ -1474,9 +1475,24 @@ void LyXText::dispatch(LCursor & cur, FuncRequest & cmd)
        }
 
        needsUpdate |= (cur.pos() != cur.lastpos()) && cur.selection();
-       if (singleParUpdate)
+
+       // FIXME: The cursor flag is reset two lines below
+       // so we need to check here if some of the LFUN did touch that.
+       // for now only LyXText::erase() and LyXText::backspace() do that.
+       // The plan is to verify all the LFUNs and then to remove this
+       // singleParUpdate boolean altogether.
+       if (cur.result().update() & Update::Force) {
+               singleParUpdate = false;
+               needsUpdate = true;
+       }
+
+       // FIXME: the following code should go in favor of fine grained
+       // update flag treatment.
+       if (singleParUpdate) {
                // Inserting characters does not change par height
-               if (cur.bottom().paragraph().dim().height()
+               ParagraphMetrics const & pms 
+                       = cur.bv().parMetrics(cur.bottom().text(), cur.bottom().pit());
+               if (pms.dim().height()
                    == olddim.height()) {
                        // if so, update _only_ this paragraph
                        cur.updateFlags(Update::SinglePar |
@@ -1485,12 +1501,23 @@ void LyXText::dispatch(LCursor & cur, FuncRequest & cmd)
                        return;
                } else
                        needsUpdate = true;
+       }
+
        if (!needsUpdate
            && &oldTopSlice.inset() == &cur.inset()
            && oldTopSlice.idx() == cur.idx()
-           && !sel
+           && !sel // sel is a backup of cur.selection() at the biginning of the function.
            && !cur.selection())
-               cur.noUpdate();
+               // FIXME: it would be better if we could just do this
+               //
+               //if (cur.result().update() != Update::FitCursor)
+               //      cur.noUpdate();
+               // 
+               // But some LFUNs do not set Update::FitCursor when needed, so we
+               // do it for all. This is not very harmfull as FitCursor will provoke
+               // a full redraw only if needed but still, a proper review of all LFUN
+               // should be done and this needsUpdate boolean can then be removed.
+               cur.updateFlags(Update::FitCursor);
        else
                cur.updateFlags(Update::Force | Update::FitCursor);
 }
@@ -1699,7 +1726,23 @@ bool LyXText::getStatus(LCursor & cur, FuncRequest const & cmd,
                break;
 
        case LFUN_PASTE:
-               enable = cap::numberOfSelections() > 0;
+               // FIXME: This is not correct, but the correct code below is
+               // expensive
+               enable = cap::numberOfSelections() > 0 ||
+                       !theClipboard().isInternal();
+#if 0
+               if (cmd.argument().empty()) {
+                       if (theClipboard().isInternal())
+                               enable = cap::numberOfSelections() > 0;
+                       else
+                               enable = !theClipboard().get().empty();
+               } else if (isStrUnsignedInt(to_utf8(cmd.argument()))) {
+                       int n = convert<unsigned int>(to_utf8(cmd.argument()));
+                       enable = cap::numberOfSelections() > n;
+               } else
+                       // unknown argument
+                       enable = false;
+#endif
                break;
 
        case LFUN_PARAGRAPH_MOVE_UP:
@@ -1716,7 +1759,13 @@ bool LyXText::getStatus(LCursor & cur, FuncRequest const & cmd,
 
        case LFUN_CHANGE_ACCEPT:
        case LFUN_CHANGE_REJECT:
-               enable = true; // FIXME: Change tracking (MG)
+               // TODO: context-sensitive enabling of LFUN_CHANGE_ACCEPT/REJECT
+               // In principle, these LFUNs should only be enabled if there
+               // is a change at the current position/in the current selection.
+               // However, without proper optimizations, this will inevitably
+               // result in unacceptable performance - just imagine a user who
+               // wants to select the complete content of a long document.
+               enable = true;
                break;
 
        case LFUN_WORD_DELETE_FORWARD:
@@ -1772,6 +1821,8 @@ bool LyXText::getStatus(LCursor & cur, FuncRequest const & cmd,
        case LFUN_SELF_INSERT:
        case LFUN_LINE_INSERT:
        case LFUN_PAGEBREAK_INSERT:
+       case LFUN_CLEARPAGE_INSERT:
+       case LFUN_CLEARDOUBLEPAGE_INSERT:
        case LFUN_MATH_DISPLAY:
        case LFUN_MATH_IMPORT_SELECTION:
        case LFUN_MATH_MODE:
@@ -1831,4 +1882,17 @@ bool LyXText::getStatus(LCursor & cur, FuncRequest const & cmd,
 }
 
 
+void LyXText::pasteString(LCursor & cur, docstring const & clip,
+               docstring const & argument)
+{
+       cur.clearSelection();
+       if (!clip.empty()) {
+               recordUndo(cur);
+               if (argument == "paragraph")
+                       insertStringAsParagraphs(cur, clip);
+               else
+                       insertStringAsLines(cur, clip);
+       }
+}
+
 } // namespace lyx