]> git.lyx.org Git - lyx.git/blobdiff - src/text3.C
Fix bug 2029 (RtL space width)
[lyx.git] / src / text3.C
index ca407c628ff191d0321fb718c3aef7fc87bea2ac..20940bf7ef1487cf8e044dc71bd43c37a4f5aab8 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,7 +132,7 @@ 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();
@@ -235,12 +236,12 @@ void specialChar(LCursor & cur, InsetSpecialChar::Kind kind)
 }
 
 
-void doInsertInset(LCursor & cur, LyXText * text,
+bool doInsertInset(LCursor & cur, LyXText * text,
        FuncRequest const & cmd, bool edit, bool pastesel)
 {
        InsetBase * inset = createInset(&cur.bv(), cmd);
        if (!inset)
-               return;
+               return false;
 
        recordUndo(cur);
        bool gotsel = false;
@@ -255,6 +256,7 @@ void doInsertInset(LCursor & cur, LyXText * text,
 
        if (gotsel && pastesel)
                cur.bv().owner()->dispatch(FuncRequest(LFUN_PASTE));
+       return true;
 }
 
 
@@ -262,7 +264,7 @@ void update(LCursor & cur)
 {
        //we don't call update(true, false) directly to save a metrics call
        if (cur.bv().fitCursor())
-               cur.bv().update(false, true);
+               cur.bv().update(Update::Force);
 }
 
 
@@ -286,11 +288,11 @@ 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);
 
@@ -301,7 +303,7 @@ void LyXText::dispatch(LCursor & cur, FuncRequest & cmd)
                bool start = !par.params().startOfAppendix();
 
 #ifdef WITH_WARNINGS
-#warning The code below only makes sense a top level.
+#warning The code below only makes sense at top level.
 // Should LFUN_APPENDIX be restricted to top-level paragraphs?
 #endif
                // ensure that we have only one start_of_appendix in this document
@@ -405,14 +407,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);
                }
@@ -426,7 +430,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);
                }
@@ -437,8 +443,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);
                }
@@ -450,7 +458,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);
                }
@@ -715,19 +725,6 @@ void LyXText::dispatch(LCursor & cur, FuncRequest & cmd)
                break;
        }
 
-       case LFUN_INSET_APPLY: {
-               string const name = cmd.getArg(0);
-               InsetBase * inset = bv->owner()->getDialogs().getOpenInset(name);
-               if (inset) {
-                       FuncRequest fr(LFUN_INSET_MODIFY, cmd.argument);
-                       inset->dispatch(cur, fr);
-               } else {
-                       FuncRequest fr(LFUN_INSET_INSERT, cmd.argument);
-                       dispatch(cur, fr);
-               }
-               break;
-       }
-
        case LFUN_INSET_INSERT: {
                recordUndo(cur);
                InsetBase * inset = createInset(bv, cmd);
@@ -739,8 +736,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: {
@@ -831,8 +827,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: {
@@ -923,6 +919,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
@@ -966,27 +963,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) {
@@ -994,6 +978,7 @@ void LyXText::dispatch(LCursor & cur, FuncRequest & cmd)
                        cur.resetAnchor();
                        cursorEnd(cur);
                        cur.setSelection();
+                       bv->cursor() = cur;
                        bv->haveSelection(cur.selection());
                }
                break;
@@ -1001,6 +986,7 @@ void LyXText::dispatch(LCursor & cur, FuncRequest & cmd)
        case LFUN_MOUSE_DOUBLE:
                if (cmd.button() == mouse_button::button1) {
                        selectWord(cur, lyx::WHOLE_WORD_STRICT);
+                       bv->cursor() = cur;
                        bv->haveSelection(cur.selection());
                }
                break;
@@ -1084,7 +1070,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
@@ -1125,6 +1111,12 @@ void LyXText::dispatch(LCursor & cur, FuncRequest & cmd)
                cur.clearSelection();
                LyXFont const old_font = real_current_font;
 
+               // Prevents language turds in new lyxtexts under non-english
+               BufferParams const & bufparams = cur.buffer().params();
+               Language const * lang = cur.paragraph().getParLanguage(bufparams);
+               current_font.setLanguage(lang);
+               real_current_font.setLanguage(lang);
+
                string::const_iterator cit = cmd.argument.begin();
                string::const_iterator end = cmd.argument.end();
                for (; cit != end; ++cit)
@@ -1134,9 +1126,12 @@ void LyXText::dispatch(LCursor & cur, FuncRequest & cmd)
                cur.resetAnchor();
                moveCursor(cur, false);
 
-               // real_current_font.number can change so we need to
-               // update the minibuffer
-               if (old_font != real_current_font)
+               needsUpdate = redoParagraph(cur.pit());
+               if (!needsUpdate) {
+                       // update only this paragraph
+                       cur.bv().update(Update::SinglePar | Update::Force);
+               }
+
                bv->updateScrollbar();
                break;
        }
@@ -1184,13 +1179,9 @@ void LyXText::dispatch(LCursor & cur, FuncRequest & cmd)
        case LFUN_INSERT_BRANCH:
        case LFUN_INSERT_BIBITEM:
        case LFUN_INSET_ERT:
-       case LFUN_INSET_FLOAT:
        case LFUN_INSET_FOOTNOTE:
        case LFUN_INSET_MARGINAL:
        case LFUN_INSET_OPTARG:
-       case LFUN_INSET_WIDE_FLOAT:
-       case LFUN_INSET_WRAP:
-       case LFUN_TABULAR_INSERT:
        case LFUN_ENVIRONMENT_INSERT:
                // Open the inset, and move the current selection
                // inside it.
@@ -1198,6 +1189,25 @@ void LyXText::dispatch(LCursor & cur, FuncRequest & cmd)
                cur.posRight();
                break;
 
+       case LFUN_TABULAR_INSERT:
+               // if there were no arguments, just open the dialog
+               if (doInsertInset(cur, this, cmd, false, true))
+                       cur.posRight();
+               else
+                       bv->owner()->getDialogs().show("tabularcreate");
+
+               break;
+
+       case LFUN_INSET_FLOAT:
+       case LFUN_INSET_WIDE_FLOAT:
+       case LFUN_INSET_WRAP:
+               doInsertInset(cur, this, cmd, true, true);
+               cur.posRight();
+               // FIXME: the "Caption" name should not be hardcoded,
+               // but given by the float definition.
+               cur.dispatch(FuncRequest(LFUN_LAYOUT, "Caption"));
+               break;
+
        case LFUN_INDEX_INSERT:
                // Just open the inset
                doInsertInset(cur, this, cmd, true, false);
@@ -1510,8 +1520,10 @@ void LyXText::dispatch(LCursor & cur, FuncRequest & cmd)
                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;
        }
@@ -1551,6 +1563,10 @@ bool LyXText::getStatus(LCursor & cur, FuncRequest const & cmd,
 
        case LFUN_APPENDIX:
                flag.setOnOff(cur.paragraph().params().startOfAppendix());
+               return true;
+
+       case LFUN_INSERT_BIBITEM:
+               enable = (cur.paragraph().layout()->labeltype == LABEL_BIBLIO);
                break;
 
 #if 0
@@ -1728,29 +1744,37 @@ bool LyXText::getStatus(LCursor & cur, FuncRequest const & cmd,
        case LFUN_INSET_DIALOG_SHOW:
                break;
 
+       case LFUN_INSET_MODIFY:
+               // We need to disable this, because we may get called for a
+               // tabular cell via
+               // InsetTabular::getStatus() -> InsetText::getStatus()
+               // and we don't handle LFUN_INSET_MODIFY.
+               enable = false;
+               break;
+
        case LFUN_EMPH:
                flag.setOnOff(font.emph() == LyXFont::ON);
-               break;
+               return true;
 
        case LFUN_NOUN:
                flag.setOnOff(font.noun() == LyXFont::ON);
-               break;
+               return true;
 
        case LFUN_BOLD:
                flag.setOnOff(font.series() == LyXFont::BOLD_SERIES);
-               break;
+               return true;
 
        case LFUN_SANS:
                flag.setOnOff(font.family() == LyXFont::SANS_FAMILY);
-               break;
+               return true;
 
        case LFUN_ROMAN:
                flag.setOnOff(font.family() == LyXFont::ROMAN_FAMILY);
-               break;
+               return true;
 
        case LFUN_CODE:
                flag.setOnOff(font.family() == LyXFont::TYPEWRITER_FAMILY);
-               break;
+               return true;
 
        case LFUN_DELETE_WORD_FORWARD:
        case LFUN_DELETE_WORD_BACKWARD:
@@ -1789,7 +1813,6 @@ bool LyXText::getStatus(LCursor & cur, FuncRequest const & cmd,
        case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
        case LFUN_BREAKPARAGRAPH_SKIP:
        case LFUN_PARAGRAPH_SPACING:
-       case LFUN_INSET_APPLY:
        case LFUN_INSET_INSERT:
        case LFUN_NEXT_INSET_TOGGLE:
        case LFUN_UPCASE_WORD:
@@ -1810,7 +1833,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: