]> git.lyx.org Git - lyx.git/blobdiff - src/Text.cpp
Amend 6c3447c8: FindAdv: sometimes a space is added on some math symbols
[lyx.git] / src / Text.cpp
index 93ccf2ac99fff94a29577b854e28dde8d9363985..c6e2e054fb615d09a49fddc1370c0d1f6a8369de 100644 (file)
@@ -45,7 +45,6 @@
 #include "Intl.h"
 #include "Language.h"
 #include "Layout.h"
-#include "Lexer.h"
 #include "LyX.h"
 #include "LyXAction.h"
 #include "lyxfind.h"
@@ -95,6 +94,7 @@
 #include "support/filetools.h"
 #include "support/gettext.h"
 #include "support/lassert.h"
+#include "support/Lexer.h"
 #include "support/limited_stack.h"
 #include "support/lstrings.h"
 #include "support/lyxtime.h"
@@ -1127,12 +1127,15 @@ void Text::insertChar(Cursor & cur, char_type c)
        if (!cur.paragraph().isPassThru() && owner_->lyxCode() != IPA_CODE &&
            cur.real_current_font.fontInfo().family() != TYPEWRITER_FAMILY &&
            c == '-' && pos > 0) {
-               if (par.getChar(pos - 1) == '-') {
+               pos_type prev_pos = pos - 1;
+               while (prev_pos > 0 && par.isDeleted(prev_pos))
+                       --prev_pos;
+               if (!par.isDeleted(prev_pos) && par.getChar(prev_pos) == '-') {
                        // convert "--" to endash
-                       par.eraseChar(pos - 1, cur.buffer()->params().track_changes);
+                       par.eraseChar(prev_pos, cur.buffer()->params().track_changes);
                        c = 0x2013;
                        pos--;
-               } else if (par.getChar(pos - 1) == 0x2013) {
+               } else if (!par.isDeleted(prev_pos) && par.getChar(prev_pos) == 0x2013) {
                        // convert "---" to emdash
                        par.eraseChar(pos - 1, cur.buffer()->params().track_changes);
                        c = 0x2014;
@@ -1410,11 +1413,22 @@ void Text::expandWordSel(Cursor & cur)
        Cursor c = cur;
        c.selection(false);
        c.text()->selectWord(c, WHOLE_WORD);
+       // get selection around anchor too.
+       // FIXME: this cursor is not a proper one. normalAnchor() should
+       // return a DocIterator.
+       Cursor a(cur.bv());
+       a.push_back(cur.normalAnchor());
+       a.text()->selectWord(a, WHOLE_WORD);
        // use the correct word boundary, depending on selection direction
-       if (cur.top() > cur.normalAnchor())
-               cur.pos() = c.selEnd().pos();
-       else
-               cur.pos() = c.selBegin().pos();
+       if (cur.top() > cur.normalAnchor()) {
+               cur.top() = a.selBegin();
+               cur.resetAnchor();
+               cur.top() = c.selEnd();
+       } else {
+               cur.top() = a.selEnd();
+               cur.resetAnchor();
+               cur.top() = c.selBegin();
+       }
 }
 
 
@@ -2954,12 +2968,12 @@ void Text::setParagraphs(Cursor const & cur, ParagraphParameters const & p)
 }
 
 
-// this really should just insert the inset and not move the cursor.
-void Text::insertInset(Cursor & cur, Inset * inset)
+// just insert the inset and not move the cursor.
+bool Text::insertInset(Cursor & cur, Inset * inset)
 {
        LBUFERR(this == cur.text());
        LBUFERR(inset);
-       cur.paragraph().insertInset(cur.pos(), inset, cur.current_font,
+       return cur.paragraph().insertInset(cur.pos(), inset, cur.current_font,
                Change(cur.buffer()->params().track_changes
                ? Change::INSERTED : Change::UNCHANGED));
 }
@@ -4322,7 +4336,7 @@ void Text::dispatch(Cursor & cur, FuncRequest & cmd)
 
                if (!atFirstOrLastRow) {
                        needsUpdate |= cur.selHandle(select);
-                       cur.upDownInText(up, needsUpdate);
+                       needsUpdate |= cur.upDownInText(up);
                        needsUpdate |= cur.beforeDispatchCursor().inMathed();
                } else {
                        pos_type newpos = up ? 0 : cur.lastpos();
@@ -4330,10 +4344,11 @@ void Text::dispatch(Cursor & cur, FuncRequest & cmd)
                                needsUpdate |= cur.selHandle(select);
                                // we do not reset the targetx of the cursor
                                cur.pos() = newpos;
-                               needsUpdate |= bv->checkDepm(cur, bv->cursor());
-                               cur.updateTextTargetOffset();
-                               if (needsUpdate)
+                               if (bv->checkDepm(cur, bv->cursor())) {
+                                       needsUpdate = true;
                                        cur.forceBufferUpdate();
+                               }
+                               cur.updateTextTargetOffset();
                                break;
                        }
 
@@ -4341,7 +4356,7 @@ void Text::dispatch(Cursor & cur, FuncRequest & cmd)
                        // the selection right now, but wait for the next dispatch.
                        if (select)
                                needsUpdate |= cur.selHandle(select);
-                       cur.upDownInText(up, needsUpdate);
+                       needsUpdate |= cur.upDownInText(up);
                        cur.undispatched();
                }
 
@@ -5709,12 +5724,27 @@ void Text::dispatch(Cursor & cur, FuncRequest & cmd)
        }
 
        case LFUN_NOMENCL_PRINT:
-       case LFUN_NEWPAGE_INSERT:
                // do nothing fancy
                doInsertInset(cur, this, cmd, false, false);
                cur.posForward();
                break;
 
+       case LFUN_NEWPAGE_INSERT: {
+               // When we are in a heading, put the page break in a standard
+               // paragraph before the heading (if cur.pos() == 0) or after
+               // (if cur.pos() == cur.lastpos())
+               if (cur.text()->getTocLevel(cur.pit()) != Layout::NOT_IN_TOC) {
+                       lyx::dispatch(FuncRequest(LFUN_PARAGRAPH_BREAK));
+                       DocumentClass const & tc = bv->buffer().params().documentClass();
+                       lyx::dispatch(FuncRequest(LFUN_LAYOUT, from_ascii("\"") + tc.plainLayout().name()
+                                                 + from_ascii("\" ignoreautonests")));
+               }
+               // do nothing fancy
+               doInsertInset(cur, this, cmd, false, false);
+               cur.posForward();
+               break;
+       }
+
        case LFUN_SEPARATOR_INSERT: {
                doInsertInset(cur, this, cmd, false, false);
                cur.posForward();
@@ -5781,14 +5811,15 @@ void Text::dispatch(Cursor & cur, FuncRequest & cmd)
                        InsetMathMacroTemplate * inset = new InsetMathMacroTemplate(cur.buffer(),
                                from_utf8(token(s, ' ', 0)), nargs, false, type);
                        inset->setBuffer(bv->buffer());
-                       insertInset(cur, inset);
-
-                       // enter macro inset and select the name
-                       cur.push(*inset);
-                       cur.top().pos() = cur.top().lastpos();
-                       cur.resetAnchor();
-                       cur.selection(true);
-                       cur.top().pos() = 0;
+                       if (insertInset(cur, inset)) {
+                               // If insertion is successful, enter macro inset and select the name
+                               cur.push(*inset);
+                               cur.top().pos() = cur.top().lastpos();
+                               cur.resetAnchor();
+                               cur.selection(true);
+                               cur.top().pos() = 0;
+                       } else
+                               delete inset;
                }
                break;
 
@@ -5931,7 +5962,7 @@ void Text::dispatch(Cursor & cur, FuncRequest & cmd)
                // Argument?
                if (!arg.empty()) {
                        if (isStrUnsignedInt(arg)) {
-                               num = convert<uint>(arg);
+                               num = convert<unsigned int>(arg);
                                if (num >= freeFonts.size()) {
                                        cur.message(_("Invalid argument (number exceeds stack size)!"));
                                        break;
@@ -6359,30 +6390,31 @@ void Text::dispatch(Cursor & cur, FuncRequest & cmd)
 
        // FIXME: the following code should go in favor of fine grained
        // update flag treatment.
-       if (singleParUpdate) {
+       if (singleParUpdate || cur.result().screenUpdate() & Update::SinglePar) {
                // Inserting characters does not change par height in general. So, try
                // to update _only_ this paragraph. BufferView will detect if a full
                // metrics update is needed anyway.
                cur.screenUpdateFlags(Update::SinglePar | Update::FitCursor);
                return;
        }
-       if (!needsUpdate
-           && &oldTopSlice.inset() == &cur.inset()
-           && oldTopSlice.idx() == cur.idx()
-           && !oldSelection // oldSelection is a backup of cur.selection() at the beginning of the function.
-           && !cur.selection())
-               // FIXME: it would be better if we could just do this
-               //
-               //if (cur.result().update() != Update::FitCursor)
-               //      cur.noScreenUpdate();
-               //
-               // 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.screenUpdateFlags(Update::FitCursor);
-       else
+       if (needsUpdate)
                cur.screenUpdateFlags(Update::Force | Update::FitCursor);
+       else {
+               // oldSelection is a backup of cur.selection() at the beginning of the function.
+           if (!oldSelection && !cur.selection())
+                       // FIXME: it would be better if we could just do this
+                       //
+                       //if (cur.result().update() != Update::FitCursor)
+                       //      cur.noScreenUpdate();
+                       //
+                       // 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.screenUpdateFlags(Update::FitCursor);
+               else
+                       cur.screenUpdateFlags(Update::ForceDraw | Update::FitCursor);
+       }
 }
 
 
@@ -6723,12 +6755,15 @@ bool Text::getStatus(Cursor & cur, FuncRequest const & cmd,
        case LFUN_MATH_BIGDELIM:
        case LFUN_MATH_DISPLAY:
        case LFUN_MATH_MODE:
-       case LFUN_MATH_MACRO:
        case LFUN_MATH_SUBSCRIPT:
        case LFUN_MATH_SUPERSCRIPT:
                code = MATH_HULL_CODE;
                break;
 
+       case LFUN_MATH_MACRO:
+               code = MATHMACRO_CODE;
+               break;
+
        case LFUN_REGEXP_MODE:
                code = MATH_HULL_CODE;
                enable = cur.buffer()->isInternal() && !cur.inRegexped();
@@ -6941,9 +6976,11 @@ bool Text::getStatus(Cursor & cur, FuncRequest const & cmd,
        }
 
        case LFUN_NEWPAGE_INSERT:
-               // not allowed in description items
+               // not allowed in description items and in the midst of sections
                code = NEWPAGE_CODE;
-               enable = !inDescriptionItem(cur);
+               enable = !inDescriptionItem(cur)
+                       && (cur.text()->getTocLevel(cur.pit()) == Layout::NOT_IN_TOC
+                           || cur.pos() == 0 || cur.pos() == cur.lastpos());
                break;
 
        case LFUN_LANGUAGE: