]> git.lyx.org Git - lyx.git/blobdiff - src/BufferView.cpp
configure.py: Check path accessibility before call to os.path.isfile.
[lyx.git] / src / BufferView.cpp
index bc8599feefa9e3c74da694be759f5a43547614a1..daa05a60d939b033bea992e36c88eabd920a3eb7 100644 (file)
@@ -54,6 +54,7 @@
 #include "WordLangTuple.h"
 
 #include "insets/InsetBibtex.h"
+#include "insets/InsetCitation.h"
 #include "insets/InsetCommand.h" // ChangeRefs
 #include "insets/InsetExternal.h"
 #include "insets/InsetGraphics.h"
@@ -105,6 +106,7 @@ T * getInsetByCode(Cursor const & cur, InsetCode code)
        return 0;
 }
 
+
 /// Note that comparing contents can only be used for InsetCommand
 bool findNextInset(DocIterator & dit, vector<InsetCode> const & codes,
        docstring const & contents)
@@ -874,7 +876,7 @@ bool BufferView::scrollToCursor(DocIterator const & dit, bool recenter)
 
        if (tm.contains(bot_pit)) {
                ParagraphMetrics const & pm = tm.parMetrics(bot_pit);
-               LASSERT(!pm.rows().empty(), /**/);
+               LBUFERR(!pm.rows().empty());
                // FIXME: smooth scrolling doesn't work in mathed.
                CursorSlice const & cs = dit.innerTextSlice();
                int offset = coordOffset(dit).y_;
@@ -941,7 +943,15 @@ bool BufferView::scrollToCursor(DocIterator const & dit, bool recenter)
 }
 
 
-void BufferView::updateDocumentClass(DocumentClass const * const olddc)
+void BufferView::makeDocumentClass()
+{
+       DocumentClassConstPtr olddc = buffer_.params().documentClassPtr();
+       buffer_.params().makeDocumentClass();
+       updateDocumentClass(olddc);
+}
+
+
+void BufferView::updateDocumentClass(DocumentClassConstPtr olddc)
 {
        message(_("Converting document to new document class..."));
        
@@ -956,6 +966,7 @@ void BufferView::updateDocumentClass(DocumentClass const * const olddc)
        buffer_.errors("Class Switch");
 }
 
+
 /** Return the change status at cursor position, taking in account the
  * status at each level of the document iterator (a table in a deleted
  * footnote is deleted).
@@ -1175,17 +1186,6 @@ bool BufferView::getStatus(FuncRequest const & cmd, FuncStatus & flag)
                break;
        }
 
-       // FIXME We do not really want this here, but at present we need to
-       // handle their dispatch here, for reasons explained there, so we'll
-       // handle this here, too, for consistency.
-       case LFUN_BRANCH_ACTIVATE:
-       case LFUN_BRANCH_DEACTIVATE: {
-               BranchList const & branchList = buffer().params().branchlist();
-               docstring const branchName = cmd.argument();
-               flag.setEnabled(!branchName.empty() && branchList.find(branchName));
-               break;
-       }
-
        default:
                return false;
        }
@@ -1209,14 +1209,7 @@ void BufferView::editInset(string const & name, Inset * inset)
 
 void BufferView::dispatch(FuncRequest const & cmd, DispatchResult & dr)
 {
-       //lyxerr << [ cmd = " << cmd << "]" << endl;
-
-       // Make sure that the cached BufferView is correct.
-       LYXERR(Debug::ACTION, " action[" << cmd.action() << ']'
-               << " arg[" << to_utf8(cmd.argument()) << ']'
-               << " x[" << cmd.x() << ']'
-               << " y[" << cmd.y() << ']'
-               << " button[" << cmd.button() << ']');
+       LYXERR(Debug::ACTION, "BufferView::dispatch: cmd: " << cmd);
 
        string const argument = to_utf8(cmd.argument());
        Cursor & cur = d->cursor_;
@@ -1234,7 +1227,7 @@ void BufferView::dispatch(FuncRequest const & cmd, DispatchResult & dr)
        switch (act) {
 
        case LFUN_BUFFER_PARAMS_APPLY: {
-               DocumentClass const * const oldClass = buffer_.params().documentClassPtr();
+               DocumentClassConstPtr olddc = buffer_.params().documentClassPtr();
                cur.recordUndoFullDocument();
                istringstream ss(to_utf8(cmd.argument()));
                Lexer lex;
@@ -1245,7 +1238,7 @@ void BufferView::dispatch(FuncRequest const & cmd, DispatchResult & dr)
                                                << unknown_tokens << " unknown token"
                                                << (unknown_tokens == 1 ? "" : "s"));
                }
-               updateDocumentClass(oldClass);
+               updateDocumentClass(olddc);
                        
                // We are most certainly here because of a change in the document
                // It is then better to make sure that all dialogs are in sync with
@@ -1256,12 +1249,9 @@ void BufferView::dispatch(FuncRequest const & cmd, DispatchResult & dr)
        }
                
        case LFUN_LAYOUT_MODULES_CLEAR: {
-               DocumentClass const * const oldClass =
-                       buffer_.params().documentClassPtr();
                cur.recordUndoFullDocument();
                buffer_.params().clearLayoutModules();
-               buffer_.params().makeDocumentClass();
-               updateDocumentClass(oldClass);
+               makeDocumentClass();
                dr.screenUpdate(Update::Force);
                dr.forceBufferUpdate();
                break;
@@ -1269,17 +1259,15 @@ void BufferView::dispatch(FuncRequest const & cmd, DispatchResult & dr)
 
        case LFUN_LAYOUT_MODULE_ADD: {
                BufferParams const & params = buffer_.params();
-               if (!params.moduleCanBeAdded(argument)) {
+               if (!params.layoutModuleCanBeAdded(argument)) {
                        LYXERR0("Module `" << argument << 
                                "' cannot be added due to failed requirements or "
                                "conflicts with installed modules.");
                        break;
                }
-               DocumentClass const * const oldClass = params.documentClassPtr();
                cur.recordUndoFullDocument();
                buffer_.params().addLayoutModule(argument);
-               buffer_.params().makeDocumentClass();
-               updateDocumentClass(oldClass);
+               makeDocumentClass();
                dr.screenUpdate(Update::Force);
                dr.forceBufferUpdate();
                break;
@@ -1306,12 +1294,9 @@ void BufferView::dispatch(FuncRequest const & cmd, DispatchResult & dr)
                        break;
 
                // Save the old, possibly modular, layout for use in conversion.
-               DocumentClass const * const oldDocClass =
-                       buffer_.params().documentClassPtr();
                cur.recordUndoFullDocument();
                buffer_.params().setBaseClass(argument);
-               buffer_.params().makeDocumentClass();
-               updateDocumentClass(oldDocClass);
+               makeDocumentClass();
                dr.screenUpdate(Update::Force);
                dr.forceBufferUpdate();
                break;
@@ -1332,12 +1317,10 @@ void BufferView::dispatch(FuncRequest const & cmd, DispatchResult & dr)
        }
 
        case LFUN_LAYOUT_RELOAD: {
-               DocumentClass const * const oldClass = buffer_.params().documentClassPtr();
                LayoutFileIndex bc = buffer_.params().baseClassID();
                LayoutFileList::get().reset(bc);
                buffer_.params().setBaseClass(bc);
-               buffer_.params().makeDocumentClass();
-               updateDocumentClass(oldClass);
+               makeDocumentClass();
                dr.screenUpdate(Update::Force);
                dr.forceBufferUpdate();
                break;
@@ -1546,7 +1529,7 @@ void BufferView::dispatch(FuncRequest const & cmd, DispatchResult & dr)
                if (lyxfind(this, req))
                        dr.screenUpdate(Update::Force | Update::FitCursor);
                else
-                       message(_("String not found!"));
+                       message(_("String not found."));
                d->search_request_cache_ = req;
                break;
        }
@@ -1810,7 +1793,7 @@ void BufferView::dispatch(FuncRequest const & cmd, DispatchResult & dr)
                        cur.forwardInset();
                cur.beginUndoGroup();
                while(cur && iterations < max_iter) {
-                       Inset * ins = cur.nextInset();
+                       Inset * const ins = cur.nextInset();
                        if (!ins)
                                break;
                        docstring insname = ins->layoutName();
@@ -1826,7 +1809,9 @@ void BufferView::dispatch(FuncRequest const & cmd, DispatchResult & dr)
                                        break;
                                insname = insname.substr(0, i);
                        }
-                       cur.forwardInset();
+                       // if we did not delete the inset, skip it
+                       if (!cur.nextInset() || cur.nextInset() == ins)
+                               cur.forwardInset();
                }
                cur.endUndoGroup();
                cur = savecur;
@@ -1909,6 +1894,23 @@ void BufferView::dispatch(FuncRequest const & cmd, DispatchResult & dr)
                        arg = token(argument, '|', 0);
                        opt1 = token(argument, '|', 1);
                }
+
+               // if our cursor is directly in front of or behind a citation inset,
+               // we will instead add the new key to it.
+               Inset * inset = cur.nextInset();
+               if (!inset || inset->lyxCode() != CITE_CODE)
+                       inset = cur.prevInset();
+               if (inset && inset->lyxCode() == CITE_CODE) {
+                       InsetCitation * icite = static_cast<InsetCitation *>(inset);
+                       if (icite->addKey(arg)) {
+                               dr.forceBufferUpdate();
+                               dr.screenUpdate(Update::FitCursor | Update::SinglePar);
+                               if (!opt1.empty())
+                                       LYXERR0("Discarding optional argument to citation-insert.");
+                       }
+                       dispatched = true;
+                       break;
+               }
                InsetCommandParams icp(CITE_CODE);
                icp["key"] = from_utf8(arg);
                if (!opt1.empty())
@@ -1970,41 +1972,6 @@ void BufferView::dispatch(FuncRequest const & cmd, DispatchResult & dr)
                break;
        }
 
-       // FIXME We do not really want this here, but it has to be at present
-       // because we need a cursor for the recordUndoFullDocument call. What
-       // we would really like is a recordUndoBufferParams call that did not
-       // need a cursor, but we do not have that yet.
-       // So, if this does get fixed, this code can be moved back to Buffer.cpp,
-       // and the corresponding code in getStatus() should be moved back, too.
-       case LFUN_BRANCH_ACTIVATE:
-       case LFUN_BRANCH_DEACTIVATE: {
-               BranchList & branch_list = buffer().params().branchlist();
-               docstring const branch_name = cmd.argument();
-               // the case without a branch name is handled elsewhere
-               if (branch_name.empty()) {
-                       dispatched = false;
-                       break;
-               }
-               Branch * branch = branch_list.find(branch_name);
-               if (!branch) {
-                       LYXERR0("Branch " << branch_name << " does not exist.");
-                       dr.setError(true);
-                       docstring const msg =
-                               bformat(_("Branch \"%1$s\" does not exist."), branch_name);
-                       dr.setMessage(msg);
-                       break;
-               }
-               bool activate = cmd.action() == LFUN_BRANCH_ACTIVATE;
-               if (branch->isSelected() != activate) {
-                       branch->setSelected(activate);
-                       cur.recordUndoFullDocument();
-                       dr.setError(false);
-                       dr.screenUpdate(Update::Force);
-                       dr.forceBufferUpdate();
-               }
-               break;
-       }
-
        default:
                // OK, so try the Buffer itself...
                buffer_.dispatch(cmd, dr);
@@ -2144,7 +2111,7 @@ void BufferView::clearLastInset(Inset * inset) const
 {
        if (d->last_inset_ != inset) {
                LYXERR0("Wrong last_inset!");
-               LASSERT(false, /**/);
+               LATTEST(false);
        }
        d->last_inset_ = 0;
 }
@@ -2201,8 +2168,8 @@ void BufferView::mouseEventDispatch(FuncRequest const & cmd0)
 
        // Notify left insets
        if (cur != old) {
-               old.fixIfBroken();
-               bool badcursor = notifyCursorLeavesOrEnters(old, cur);
+               bool badcursor = old.fixIfBroken() | cur.fixIfBroken();
+               badcursor |= notifyCursorLeavesOrEnters(old, cur);
                if (badcursor)
                        cursor().fixIfBroken();
        }
@@ -2376,7 +2343,7 @@ TextMetrics const & BufferView::textMetrics(Text const * t) const
 
 TextMetrics & BufferView::textMetrics(Text const * t)
 {
-       LASSERT(t, /**/);
+       LBUFERR(t);
        TextMetricsCache::iterator tmc_it  = d->text_metrics_.find(t);
        if (tmc_it == d->text_metrics_.end()) {
                tmc_it = d->text_metrics_.insert(
@@ -2442,7 +2409,7 @@ bool BufferView::checkDepm(Cursor & cur, Cursor & old)
 
 bool BufferView::mouseSetCursor(Cursor & cur, bool select)
 {
-       LASSERT(&cur.bv() == this, /**/);
+       LASSERT(&cur.bv() == this, return false);
 
        if (!select)
                // this event will clear selection so we save selection for
@@ -2450,6 +2417,8 @@ bool BufferView::mouseSetCursor(Cursor & cur, bool select)
                cap::saveSelection(cursor());
 
        d->cursor_.macroModeClose();
+       // If a macro has been finalized, the cursor might have been broken
+       cur.fixIfBroken();
 
        // Has the cursor just left the inset?
        bool const leftinset = (&d->cursor_.inset() != &cur.inset());
@@ -2681,7 +2650,7 @@ void BufferView::updateMetrics()
 
 void BufferView::insertLyXFile(FileName const & fname)
 {
-       LASSERT(d->cursor_.inTexted(), /**/);
+       LASSERT(d->cursor_.inTexted(), return);
 
        // Get absolute path of file and add ".lyx"
        // to the filename if necessary
@@ -2764,7 +2733,8 @@ Point BufferView::coordOffset(DocIterator const & dit) const
        CursorSlice const & sl = dit[0];
        TextMetrics const & tm = textMetrics(sl.text());
        ParagraphMetrics const & pm = tm.parMetrics(sl.pit());
-       LASSERT(!pm.rows().empty(), /**/);
+
+       LBUFERR(!pm.rows().empty());
        y -= pm.rows()[0].ascent();
 #if 1
        // FIXME: document this mess