]> git.lyx.org Git - lyx.git/blobdiff - src/text3.C
fix two crashes related to dEPM. Some crashes remain
[lyx.git] / src / text3.C
index e4d9060598802127a362cf72d761d17498d3a3de..5f3323a0cd1bde047fb0003de8e1d11b9596b99c 100644 (file)
@@ -57,6 +57,7 @@
 #include "support/lstrings.h"
 #include "support/lyxlib.h"
 #include "support/convert.h"
+#include "support/lyxtime.h"
 
 #include "mathed/math_hullinset.h"
 #include "mathed/math_macrotemplate.h"
@@ -131,21 +132,24 @@ namespace {
        {
                recordUndo(cur);
                string sel = cur.selectionAsString(false);
-               lyxerr << "selection is: '" << sel << "'" << endl;
+               //lyxerr << "selection is: '" << sel << "'" << endl;
 
                if (sel.empty()) {
                        const int old_pos = cur.pos();
-                       cur.insert(new MathHullInset);
+                       cur.insert(new MathHullInset("simple"));
                        BOOST_ASSERT(old_pos == cur.pos());
                        cur.nextInset()->edit(cur, true);
-                       cur.dispatch(FuncRequest(LFUN_MATH_MUTATE, "simple"));
                        // don't do that also for LFUN_MATH_MODE
                        // unless you want end up with always changing
                        // to mathrm when opening an inlined inset --
                        // I really hate "LyXfunc overloading"...
                        if (display)
                                cur.dispatch(FuncRequest(LFUN_MATH_DISPLAY));
-                       cur.dispatch(FuncRequest(LFUN_INSERT_MATH, cmd.argument));
+                       // Avoid an unnecessary undo step if cmd.argument
+                       // is empty
+                       if (!cmd.argument.empty())
+                               cur.dispatch(FuncRequest(LFUN_INSERT_MATH,
+                                                        cmd.argument));
                } else {
                        // create a macro if we see "\\newcommand"
                        // somewhere, and an ordinary formula
@@ -154,9 +158,8 @@ namespace {
                        if (sel.find("\\newcommand") == string::npos
                            && sel.find("\\def") == string::npos)
                        {
-                               cur.insert(new MathHullInset);
+                               cur.insert(new MathHullInset("simple"));
                                cur.dispatch(FuncRequest(LFUN_RIGHT));
-                               cur.dispatch(FuncRequest(LFUN_MATH_MUTATE, "simple"));
                                cur.dispatch(FuncRequest(LFUN_INSERT_MATH, sel));
                        } else {
                                istringstream is(sel);
@@ -189,8 +192,10 @@ bool LyXText::cursorPrevious(LCursor & cur)
 
        int x = cur.x_target();
 
-       setCursorFromCoordinates(cur, x, 0);
-       bool updated = cursorUp(cur);
+       bool updated = setCursorFromCoordinates(cur, x, 0);
+       if (updated)
+               cur.bv().update();
+       updated |= cursorUp(cur);
 
        if (cpar == cur.pit() && cpos == cur.pos()) {
                // we have a row which is taller than the workarea. The
@@ -210,8 +215,10 @@ bool LyXText::cursorNext(LCursor & cur)
        lyx::pit_type cpar = cur.pit();
 
        int x = cur.x_target();
-       setCursorFromCoordinates(cur, x, cur.bv().workHeight() - 1);
-       bool updated = cursorDown(cur);
+       bool updated = setCursorFromCoordinates(cur, x, cur.bv().workHeight() - 1);
+       if (updated)
+               cur.bv().update();
+       updated |= cursorDown(cur);
 
        if (cpar == cur.pit() && cpos == cur.pos()) {
                // we have a row which is taller than the workarea. The
@@ -287,13 +294,21 @@ bool LyXText::isRTL(Paragraph const & par) const
 void LyXText::dispatch(LCursor & cur, FuncRequest & cmd)
 {
        lyxerr[Debug::ACTION] << "LyXText::dispatch: cmd: " << cmd << endl;
-       lyxerr << "*** LyXText::dispatch: cmd: " << cmd << endl;
 
        BOOST_ASSERT(cur.text() == this);
        BufferView * bv = &cur.bv();
        CursorSlice oldTopSlice = cur.top();
+       bool oldBoundary = cur.boundary();
        bool sel = cur.selection();
-       bool needsUpdate = !lyxaction.funcHasFlag(cmd.action, LyXAction::NoUpdate);
+       // Signals that, even if needsUpdate == false, an update of the
+       // cursor paragraph is required
+       bool singleParUpdate = lyxaction.funcHasFlag(cmd.action,
+               LyXAction::SingleParUpdate);
+       // Signals that a full-screen update is required
+       bool needsUpdate = !(lyxaction.funcHasFlag(cmd.action, 
+               LyXAction::NoUpdate) || singleParUpdate);
+       // Remember the old paragraph metric
+       Dimension olddim = cur.paragraph().dim();
 
        switch (cmd.action) {
 
@@ -406,14 +421,16 @@ void LyXText::dispatch(LCursor & cur, FuncRequest & cmd)
 
        case LFUN_RIGHT:
        case LFUN_RIGHTSEL:
-               lyxerr << BOOST_CURRENT_FUNCTION
-                      << " LFUN_RIGHT[SEL]:\n" << cur << endl;
+               //lyxerr << BOOST_CURRENT_FUNCTION
+               //       << " LFUN_RIGHT[SEL]:\n" << cur << endl;
                cur.selHandle(cmd.action == LFUN_RIGHTSEL);
                if (isRTL(cur.paragraph()))
                        needsUpdate = cursorLeft(cur);
                else
                        needsUpdate = cursorRight(cur);
-               if (!needsUpdate && oldTopSlice == cur.top()) {
+
+               if (!needsUpdate && oldTopSlice == cur.top()
+                               && cur.boundary() == oldBoundary) {
                        cur.undispatched();
                        cmd = FuncRequest(LFUN_FINISHED_RIGHT);
                }
@@ -427,7 +444,9 @@ void LyXText::dispatch(LCursor & cur, FuncRequest & cmd)
                        needsUpdate = cursorRight(cur);
                else
                        needsUpdate = cursorLeft(cur);
-               if (oldTopSlice == cur.top()) {
+
+               if (!needsUpdate && oldTopSlice == cur.top()
+                       && cur.boundary() == oldBoundary) {
                        cur.undispatched();
                        cmd = FuncRequest(LFUN_FINISHED_LEFT);
                }
@@ -438,8 +457,10 @@ void LyXText::dispatch(LCursor & cur, FuncRequest & cmd)
                update(cur);
                //lyxerr << "handle LFUN_UP[SEL]:\n" << cur << endl;
                cur.selHandle(cmd.action == LFUN_UPSEL);
+
                needsUpdate = cursorUp(cur);
-               if (oldTopSlice == cur.top()) {
+               if (!needsUpdate && oldTopSlice == cur.top()
+                         && cur.boundary() == oldBoundary) {
                        cur.undispatched();
                        cmd = FuncRequest(LFUN_FINISHED_UP);
                }
@@ -451,7 +472,9 @@ void LyXText::dispatch(LCursor & cur, FuncRequest & cmd)
                //lyxerr << "handle LFUN_DOWN[SEL]:\n" << cur << endl;
                cur.selHandle(cmd.action == LFUN_DOWNSEL);
                needsUpdate = cursorDown(cur);
-               if (oldTopSlice == cur.top()) {
+               if (!needsUpdate && oldTopSlice == cur.top() &&
+                   cur.boundary() == oldBoundary)
+               {
                        cur.undispatched();
                        cmd = FuncRequest(LFUN_FINISHED_DOWN);
                }
@@ -727,8 +750,7 @@ void LyXText::dispatch(LCursor & cur, FuncRequest & cmd)
        }
 
        case LFUN_INSET_SETTINGS:
-               if (cur.inset().asUpdatableInset())
-                       cur.inset().asUpdatableInset()->showInsetDialog(bv);
+               cur.inset().showInsetDialog(bv);
                break;
 
        case LFUN_NEXT_INSET_TOGGLE: {
@@ -819,8 +841,8 @@ void LyXText::dispatch(LCursor & cur, FuncRequest & cmd)
                break;
 
        case LFUN_GETXY:
-               cur.message(convert<string>(cursorX(cur.top())) + ' '
-                         + convert<string>(cursorY(cur.top())));
+               cur.message(convert<string>(cursorX(cur.top(), cur.boundary())) + ' '
+                         + convert<string>(cursorY(cur.top(), cur.boundary())));
                break;
 
        case LFUN_SETXY: {
@@ -911,6 +933,7 @@ void LyXText::dispatch(LCursor & cur, FuncRequest & cmd)
                cur.clearSelection();
                string const clip = bv->getClipboard();
                if (!clip.empty()) {
+                       recordUndo(cur);
                        if (cmd.argument == "paragraph")
                                insertStringAsParagraphs(cur, clip);
                        else
@@ -941,12 +964,10 @@ void LyXText::dispatch(LCursor & cur, FuncRequest & cmd)
                                cur.insert(new InsetQuotes(c,
                                    bufparams.quotes_language,
                                    InsetQuotes::SingleQ));
-                       else if (arg == "double")
+                       else
                                cur.insert(new InsetQuotes(c,
                                    bufparams.quotes_language,
                                    InsetQuotes::DoubleQ));
-                       else
-                               cur.insert(new InsetQuotes(c, bufparams));
                        cur.posRight();
                }
                else
@@ -954,27 +975,14 @@ void LyXText::dispatch(LCursor & cur, FuncRequest & cmd)
                break;
        }
 
-       case LFUN_DATE_INSERT: {
-               lyx::cap::replaceSelection(cur);
-               time_t now_time_t = time(NULL);
-               struct tm * now_tm = localtime(&now_time_t);
-               setlocale(LC_TIME, "");
-               string arg;
-               if (!cmd.argument.empty())
-                       arg = cmd.argument;
+       case LFUN_DATE_INSERT: 
+               if (cmd.argument.empty())
+                       bv->owner()->dispatch(FuncRequest(LFUN_SELFINSERT,
+                               lyx::formatted_time(lyx::current_time())));
                else
-                       arg = lyxrc.date_insert_format;
-               char datetmp[32];
-               int const datetmp_len =
-                       ::strftime(datetmp, 32, arg.c_str(), now_tm);
-
-               for (int i = 0; i < datetmp_len; i++)
-                       insertChar(cur, datetmp[i]);
-
-               cur.resetAnchor();
-               moveCursor(cur, false);
+                       bv->owner()->dispatch(FuncRequest(LFUN_SELFINSERT,
+                               lyx::formatted_time(lyx::current_time(), cmd.argument)));
                break;
-       }
 
        case LFUN_MOUSE_TRIPLE:
                if (cmd.button() == mouse_button::button1) {
@@ -1074,7 +1082,7 @@ void LyXText::dispatch(LCursor & cur, FuncRequest & cmd)
                                // don't set anchor_
                                bvcur.setCursor(cur);
                                bvcur.selection() = true;
-                               lyxerr << "MOTION: " << bv->cursor() << endl;
+                               //lyxerr << "MOTION: " << bv->cursor() << endl;
                        }
 
                } else
@@ -1129,13 +1137,6 @@ void LyXText::dispatch(LCursor & cur, FuncRequest & cmd)
 
                cur.resetAnchor();
                moveCursor(cur, false);
-
-               needsUpdate = redoParagraph(cur.pit());
-               if (!needsUpdate) {
-                       // update only this paragraph
-                       cur.bv().update(Update::SinglePar | Update::Force);
-               }
-
                bv->updateScrollbar();
                break;
        }
@@ -1258,7 +1259,7 @@ void LyXText::dispatch(LCursor & cur, FuncRequest & cmd)
                        int const nargs = s1.empty() ? 0 : convert<int>(s1);
                        string const s2 = token(s, ' ', 2);
                        string const type = s2.empty() ? "newcommand" : s2;
-                       cur.insert(new MathMacroTemplate(token(s, ' ', 0), nargs, s2));
+                       cur.insert(new MathMacroTemplate(token(s, ' ', 0), nargs, type));
                        //cur.nextInset()->edit(cur, true);
                }
                break;
@@ -1274,9 +1275,8 @@ void LyXText::dispatch(LCursor & cur, FuncRequest & cmd)
        case LFUN_INSERT_MATH:
        case LFUN_INSERT_MATRIX:
        case LFUN_MATH_DELIM: {
-               cur.insert(new MathHullInset);
+               cur.insert(new MathHullInset("simple"));
                cur.dispatch(FuncRequest(LFUN_RIGHT));
-               cur.dispatch(FuncRequest(LFUN_MATH_MUTATE, "simple"));
                cur.dispatch(cmd);
                break;
        }
@@ -1519,17 +1519,27 @@ void LyXText::dispatch(LCursor & cur, FuncRequest & cmd)
                        cur.selection() = false;
                } else {
                        cur.undispatched();
-                       cmd = FuncRequest(LFUN_FINISHED_LEFT);
+                       cmd = FuncRequest(LFUN_FINISHED_RIGHT);
                }
                break;
 
        default:
-               lyxerr << BOOST_CURRENT_FUNCTION
-                      << " Not DISPATCHED by LyXText" << endl;
+               lyxerr[Debug::ACTION] 
+                       << BOOST_CURRENT_FUNCTION
+                       << ": Command " << cmd 
+                       << " not DISPATCHED by LyXText" << endl;
                cur.undispatched();
                break;
        }
 
+       if (singleParUpdate)
+               // Inserting characters does not change par height
+               if (cur.paragraph().dim().asc == olddim.asc
+                && cur.paragraph().dim().des == olddim.des) {
+                       // if so, update _only_ this paragraph
+                       cur.bv().update(Update::SinglePar | Update::Force);
+               } else
+                       needsUpdate = true;
        if (!needsUpdate
            && &oldTopSlice.inset() == &cur.inset()
            && oldTopSlice.idx() == cur.idx()
@@ -1567,6 +1577,10 @@ bool LyXText::getStatus(LCursor & cur, FuncRequest const & cmd,
                flag.setOnOff(cur.paragraph().params().startOfAppendix());
                return true;
 
+       case LFUN_INSERT_BIBITEM:
+               enable = (cur.paragraph().layout()->labeltype == LABEL_BIBLIO);
+               break;
+
 #if 0
        // the functions which insert insets
        InsetBase::Code code = InsetBase::NO_CODE;
@@ -1831,7 +1845,6 @@ bool LyXText::getStatus(LCursor & cur, FuncRequest const & cmd,
        case LFUN_INSERT_LABEL:
        case LFUN_INSERT_NOTE:
        case LFUN_INSERT_CHARSTYLE:
-       case LFUN_INSERT_BIBITEM:
        case LFUN_INSET_FLOAT:
        case LFUN_INSET_FOOTNOTE:
        case LFUN_INSET_MARGINAL: