]> git.lyx.org Git - lyx.git/blobdiff - src/text3.C
reduce number of calls to LyXText::getFont
[lyx.git] / src / text3.C
index ca0be38640a89a86165715fb8ee446e396bf4f09..4f99c9cee6a8d0474ce337d147ccd254ab85647e 100644 (file)
@@ -20,6 +20,7 @@
 #include "FloatList.h"
 #include "FuncStatus.h"
 #include "buffer.h"
+#include "buffer_funcs.h"
 #include "bufferparams.h"
 #include "BufferView.h"
 #include "cursor.h"
@@ -60,6 +61,8 @@
 #include "mathed/math_hullinset.h"
 #include "mathed/math_macrotemplate.h"
 
+#include <boost/current_function.hpp>
+
 #include <clocale>
 #include <sstream>
 
@@ -74,10 +77,8 @@ using lyx::support::isStrUnsignedInt;
 using lyx::support::token;
 
 using std::endl;
-using std::find;
 using std::string;
 using std::istringstream;
-using std::vector;
 
 
 extern string current_layout;
@@ -181,77 +182,6 @@ string const freefont2string()
 
 }
 
-bool LyXText::gotoNextInset(LCursor & cur,
-       vector<InsetBase_code> const & codes, string const & contents)
-{
-       BOOST_ASSERT(this == cur.text());
-       pit_type end = paragraphs().size();
-       pit_type pit = cur.pit();
-       pos_type pos = cur.pos();
-
-       InsetBase * inset;
-       do {
-               if (pos + 1 < pars_[pit].size()) {
-                       ++pos;
-               } else  {
-                       ++pit;
-                       pos = 0;
-               }
-
-       } while (pit != end &&
-                !(pars_[pit].isInset(pos) &&
-                  (inset = pars_[pit].getInset(pos)) != 0 &&
-                  find(codes.begin(), codes.end(), inset->lyxCode()) != codes.end() &&
-                  (contents.empty() ||
-                   static_cast<InsetCommand *>(pars_[pit].getInset(pos))->getContents()
-                   == contents)));
-
-       if (pit == end)
-               return false;
-
-       setCursor(cur, pit, pos, false);
-       return true;
-}
-
-
-void LyXText::gotoInset(LCursor & cur,
-       vector<InsetBase_code> const & codes, bool same_content)
-{
-       cur.clearSelection();
-
-       string contents;
-       if (same_content
-           && cur.pos() < cur.lastpos()
-           && cur.paragraph().isInset(cur.pos())) {
-               InsetBase const * inset = cur.paragraph().getInset(cur.pos());
-               if (find(codes.begin(), codes.end(), inset->lyxCode())
-                   != codes.end())
-                       contents = static_cast<InsetCommand const *>(inset)->getContents();
-       }
-
-       if (!gotoNextInset(cur, codes, contents)) {
-               if (cur.pos() || cur.pit() != 0) {
-                       CursorSlice tmp = cur.top();
-                       cur.pit() = 0;
-                       cur.pos() = 0;
-                       if (!gotoNextInset(cur, codes, contents)) {
-                               cur.top() = tmp;
-                               cur.message(_("No more insets"));
-                       }
-               } else {
-                       cur.message(_("No more insets"));
-               }
-       }
-       cur.resetAnchor();
-}
-
-
-void LyXText::gotoInset(LCursor & cur, InsetBase_code code, bool same_content)
-{
-       gotoInset(cur, vector<InsetBase_code>(1, code), same_content);
-}
-
-
 bool LyXText::cursorPrevious(LCursor & cur)
 {
        pos_type cpos = cur.pos();
@@ -305,12 +235,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;
@@ -325,6 +255,7 @@ void doInsertInset(LCursor & cur, LyXText * text,
 
        if (gotsel && pastesel)
                cur.bv().owner()->dispatch(FuncRequest(LFUN_PASTE));
+       return true;
 }
 
 
@@ -332,7 +263,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);
 }
 
 
@@ -370,6 +301,10 @@ void LyXText::dispatch(LCursor & cur, FuncRequest & cmd)
                Paragraph & par = cur.paragraph();
                bool start = !par.params().startOfAppendix();
 
+#ifdef WITH_WARNINGS
+#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
                for (pit_type tmp = 0, end = pars_.size(); tmp != end; ++tmp) {
                        if (pars_[tmp].params().startOfAppendix()) {
@@ -383,7 +318,7 @@ void LyXText::dispatch(LCursor & cur, FuncRequest & cmd)
                par.params().startOfAppendix(start);
 
                // we can set the refreshing parameters now
-               updateCounters();
+               updateCounters(cur.buffer());
                break;
        }
 
@@ -426,7 +361,7 @@ void LyXText::dispatch(LCursor & cur, FuncRequest & cmd)
                break;
 
        case LFUN_BEGINNINGBUF:
-               if (cur.size() == 1) {
+               if (cur.depth() == 1) {
                        if (!cur.mark())
                                cur.clearSelection();
                        cursorTop(cur);
@@ -437,7 +372,7 @@ void LyXText::dispatch(LCursor & cur, FuncRequest & cmd)
                break;
 
        case LFUN_BEGINNINGBUFSEL:
-               if (cur.size() == 1) {
+               if (cur.depth() == 1) {
                        if (!cur.selection())
                                cur.resetAnchor();
                        cursorTop(cur);
@@ -448,7 +383,7 @@ void LyXText::dispatch(LCursor & cur, FuncRequest & cmd)
                break;
 
        case LFUN_ENDBUF:
-               if (cur.size() == 1) {
+               if (cur.depth() == 1) {
                        if (!cur.mark())
                                cur.clearSelection();
                        cursorBottom(cur);
@@ -459,7 +394,7 @@ void LyXText::dispatch(LCursor & cur, FuncRequest & cmd)
                break;
 
        case LFUN_ENDBUFSEL:
-               if (cur.size() == 1) {
+               if (cur.depth() == 1) {
                        if (!cur.selection())
                                cur.resetAnchor();
                        cursorBottom(cur);
@@ -781,19 +716,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);
@@ -997,22 +919,6 @@ void LyXText::dispatch(LCursor & cur, FuncRequest & cmd)
                break;
        }
 
-       case LFUN_GOTOERROR:
-               gotoInset(cur, InsetBase::ERROR_CODE, false);
-               break;
-
-       case LFUN_GOTONOTE:
-               gotoInset(cur, InsetBase::NOTE_CODE, false);
-               break;
-
-       case LFUN_REFERENCE_GOTO: {
-               vector<InsetBase_code> tmp;
-               tmp.push_back(InsetBase::LABEL_CODE);
-               tmp.push_back(InsetBase::REF_CODE);
-               gotoInset(cur, tmp, true);
-               break;
-       }
-
        case LFUN_QUOTE: {
                lyx::cap::replaceSelection(cur);
                Paragraph & par = cur.paragraph();
@@ -1076,6 +982,7 @@ void LyXText::dispatch(LCursor & cur, FuncRequest & cmd)
                        cur.resetAnchor();
                        cursorEnd(cur);
                        cur.setSelection();
+                       bv->cursor() = cur;
                        bv->haveSelection(cur.selection());
                }
                break;
@@ -1083,6 +990,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;
@@ -1207,6 +1115,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)
@@ -1216,9 +1130,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;
        }
@@ -1266,13 +1183,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.
@@ -1280,6 +1193,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);
@@ -1633,6 +1565,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
@@ -1810,29 +1746,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:
@@ -1871,7 +1815,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:
@@ -1887,15 +1830,11 @@ bool LyXText::getStatus(LCursor & cur, FuncRequest const & cmd,
        case LFUN_GETLAYOUT:
        case LFUN_LAYOUT:
        case LFUN_PASTESELECTION:
-       case LFUN_GOTOERROR:
-       case LFUN_GOTONOTE:
-       case LFUN_REFERENCE_GOTO:
        case LFUN_DATE_INSERT:
        case LFUN_SELFINSERT:
        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: