]> git.lyx.org Git - lyx.git/blobdiff - src/frontends/controllers/ControlErrorList.C
fix crash due to invalidated iterator
[lyx.git] / src / frontends / controllers / ControlErrorList.C
index 18d871a7ea5e2eedf53d1df4a6349e3bda0cf7d8..690e52a763522fd5a1c8b5f9c61cca9005606179 100644 (file)
  *
  * \author Alfredo Braunstein
  *
- * Full author contact details are available in file CREDITS
+ * Full author contact details are available in file CREDITS.
  */
 
 #include <config.h>
 
 #include "ControlErrorList.h"
-#include "support/lstrings.h" // tostr
-#include "LaTeX.h"
 #include "buffer.h"
 #include "BufferView.h"
-#include "lyxtext.h"
 #include "debug.h"
-
+#include "lyxtext.h"
+#include "paragraph.h"
+#include "pariterator.h"
 
 using std::endl;
+using std::string;
 
-
-ControlErrorList::ErrorItem::ErrorItem(string const & error,
-                                      string const & description,
-                                      int par_id, int pos_start, int pos_end)
-       : error(error), description(description), par_id(par_id),
-         pos_start(pos_start),  pos_end(pos_end)
-{}
-
+namespace lyx {
+namespace frontend {
 
 ControlErrorList::ControlErrorList(Dialog & d)
-       : Dialog::Controller(d), current_(0)
+       : Dialog::Controller(d)
 {}
 
 
 void ControlErrorList::clearParams()
-{
-       logfilename_.erase();
-       clearErrors();
-}
-
-
-std::vector<ControlErrorList::ErrorItem> const &
-ControlErrorList::ErrorList() const
-{
-       return ErrorList_;
-}
+{}
 
 
-int ControlErrorList::currentItem() const
+ErrorList const & ControlErrorList::errorList() const
 {
-       return current_;
+       return errorlist_;
 }
 
 
-bool ControlErrorList::initialiseParams(string const &)
+bool ControlErrorList::initialiseParams(string const & name)
 {
-       logfilename_ = kernel().buffer()->getLogName().second;
-       clearErrors();
-       fillErrors();
-       current_ = 0;
+       errorlist_ = kernel().bufferview()->getErrorList();
+       name_ = name;
        return true;
 }
 
 
-void ControlErrorList::clearErrors()
-{
-       ErrorList_.clear();
-       current_ = 0;
-}
-
-
-void ControlErrorList::fillErrors()
-{
-       LaTeX latex("", logfilename_, "");
-       TeXErrors terr;
-       latex.scanLogFile(terr);
-
-       Buffer * const buf = kernel().buffer();
-
-       TeXErrors::Errors::const_iterator cit = terr.begin();
-       TeXErrors::Errors::const_iterator end = terr.end();
-
-       for (; cit != end; ++cit) {
-               int par_id = -1;
-               int posstart = -1;
-               int const errorrow = cit->error_in_line;
-               buf->texrow.getIdFromRow(errorrow, par_id, posstart);
-               int posend = -1;
-               buf->texrow.getIdFromRow(errorrow + 1, par_id, posend);
-               ErrorList_.push_back(ErrorItem(cit->error_desc,
-                                              cit->error_text,
-                                              par_id, posstart, posend));
-       }
-}
-
-
-string const & ControlErrorList::docName()
+string const & ControlErrorList::name()
 {
-       return kernel().buffer()->fileName();
+       return name_;
 }
 
 
 void ControlErrorList::goTo(int item)
 {
-       BufferView * const bv = kernel().bufferview();
-       Buffer * const buf = kernel().buffer();
-
-       current_ = item;
-
-       ControlErrorList::ErrorItem const & err = ErrorList_[item];
-
+       ErrorItem const & err = errorlist_[item];
 
        if (err.par_id == -1)
                return;
 
-       ParagraphList::iterator pit = buf->getParFromID(err.par_id);
+       Buffer & buf = kernel().buffer();
+       ParIterator pit = buf.getParFromID(err.par_id);
 
-       if (pit == bv->text->ownerParagraphs().end()) {
-               lyxerr << "par id not found" << endl;
+       if (pit == buf.par_iterator_end()) {
+               lyxerr << "par id " << err.par_id << " not found" << endl;
                return;
        }
 
-       int range = err.pos_end - err.pos_start;
-
-       if (err.pos_end > pit->size() || range <= 0)
-               range = pit->size() - err.pos_start;
-
        // Now make the selection.
-       bv->insetUnlock();
-       bv->toggleSelection();
-       bv->text->clearSelection();
-       bv->text->setCursor(pit, err.pos_start);
-       bv->text->setSelectionRange(range);
-       bv->toggleSelection(false);
-       bv->fitCursor();
-       bv->update(bv->text, BufferView::SELECT);
+       // This should be implemented using an LFUN. (Angus)
+       // if pos_end is 0, this means it is end-of-paragraph
+       pos_type const end = err.pos_end ? std::min(err.pos_end, pit->size())
+                                        : pit->size();
+       pos_type const start = std::min(err.pos_start, end);
+       pos_type const range = end - start;
+       DocIterator const dit = makeDocIterator(pit, start);
+       kernel().bufferview()->putSelectionAt(dit, range, false);
+       // If we used an LFUN, we would not need that
+       kernel().bufferview()->update();
 }
+
+} // namespace frontend
+} // namespace lyx