]> git.lyx.org Git - lyx.git/blobdiff - src/frontends/qt4/GuiViewSource.cpp
Amend f441590c
[lyx.git] / src / frontends / qt4 / GuiViewSource.cpp
index 799bf405ddf25637870b373c1d575303b1868130..e528222dad5b6bcaff2e5997141549bca3756d1e 100644 (file)
@@ -26,6 +26,7 @@
 #include "support/debug.h"
 #include "support/lassert.h"
 #include "support/docstream.h"
+#include "support/docstring_list.h"
 #include "support/gettext.h"
 
 #include <boost/crc.hpp>
@@ -64,6 +65,11 @@ ViewSourceWidget::ViewSourceWidget()
                this, SLOT(setViewFormat(int)));
        connect(outputFormatCO, SIGNAL(activated(int)),
                this, SLOT(contentsChanged()));
+#ifdef DEVEL_VERSION
+       if (lyx::lyxerr.debugging(Debug::LATEX))
+               connect(viewSourceTV, SIGNAL(cursorPositionChanged()),
+                               this, SLOT(gotoCursor()));
+#endif
 
        // setting the update timer
        update_timer_->setSingleShot(true);
@@ -89,7 +95,7 @@ ViewSourceWidget::ViewSourceWidget()
 }
 
 
-auto_ptr<TexRow> ViewSourceWidget::getContent(BufferView const * view,
+void ViewSourceWidget::getContent(BufferView const * view,
                        Buffer::OutputWhat output, docstring & str, string const & format,
                        bool master)
 {
@@ -108,10 +114,10 @@ auto_ptr<TexRow> ViewSourceWidget::getContent(BufferView const * view,
        if (par_begin > par_end)
                swap(par_begin, par_end);
        odocstringstream ostr;
-       auto_ptr<TexRow> texrow = view->buffer().getSourceCode(ostr, format,
-                                                                   par_begin, par_end + 1, output, master);
-       str = ostr.str();
-       return texrow;
+       texrow_ = view->buffer().getSourceCode(ostr, format,
+                                                                               par_begin, par_end + 1, output, master);
+       //ensure that the last line can always be selected in its full width
+       str = ostr.str() + "\n";
 }
 
 
@@ -158,11 +164,13 @@ void ViewSourceWidget::updateView()
                                                short_delay : long_delay);
 }
 
+
 void ViewSourceWidget::updateViewNow()
 {
        update_timer_->start(0);
 }
 
+
 void ViewSourceWidget::realUpdateView()
 {
        if (!bv_) {
@@ -190,13 +198,28 @@ void ViewSourceWidget::realUpdateView()
                output = Buffer::OnlyBody;
 
        docstring content;
-       auto_ptr<TexRow> texrow = getContent(bv_, output, content, format,
-                                                                          masterPerspectiveCB->isChecked());
+       getContent(bv_, output, content, format, masterPerspectiveCB->isChecked());
        QString old = document_->toPlainText();
        QString qcontent = toqstr(content);
+#ifdef DEVEL_VERSION
+       // output tex<->row correspondences in the source panel if the "-dbg latex"
+       // option is given.
+       if (texrow_.get() && lyx::lyxerr.debugging(Debug::LATEX)) {
+               QStringList list = qcontent.split(QChar('\n'));
+               docstring_list dlist;
+               for (QStringList::const_iterator it = list.begin(); it != list.end(); ++it)
+                       dlist.push_back(from_utf8(fromqstr(*it)));
+               texrow_->prepend(dlist);
+               qcontent.clear();
+               for (docstring_list::iterator it = dlist.begin(); it != dlist.end(); ++it)
+                       qcontent += toqstr(*it) + '\n';
+       }
+#endif
+       // prevent gotoCursor()
+       viewSourceTV->blockSignals(true);
        bool const changed = setText(qcontent);
 
-       if (changed && !texrow.get()) {
+       if (changed && !texrow_.get()) {
                // position-to-row is unavailable
                // we jump to the first modification
                const QChar * oc = old.constData();
@@ -226,38 +249,12 @@ void ViewSourceWidget::realUpdateView()
                //c.movePosition(QTextCursor::Right, QTextCursor::KeepAnchor,1);
                viewSourceTV->setTextCursor(c);
 
-       } else if (texrow.get()) {
+       } else if (texrow_.get()) {
                // Use the available position-to-row conversion to highlight
                // the current selection in the source
-               //
-               // FIXME:
-               // * it is currently impossible to highlight the very last line
-               //   of a document, because TexRow gives the wrong data.
-               // * we currently only compute the top-level position, which
-               //   makes it impossible to highlight inside an inset. It is not
-               //   a limitation of TexRow,  but replacing bottom() with top()
-               //   works partially and causes segfaults with math. Solving
-               //   this could be seen as a solution to #4725.
-               // * even if we keep computing the top-level position, the data
-               //   given by TexRow is false if there is e.g. a float of a
-               //   footnote in the paragraph
-               CursorSlice beg = bv_->cursor().selectionBegin().bottom();
-               CursorSlice end = bv_->cursor().selectionEnd().bottom();
-               int const beg_par = beg.paragraph().id();
-               int const end_par = end.paragraph().id();
-               int const beg_pos = beg.pos();
-               int const end_pos = end.pos();
-               int const beg_row = texrow->getRowFromIdPos(beg_par, beg_pos);
-               int end_row, next_end_row;
-               if (beg_par != end_par || beg_pos != end_pos) {
-                       end_row = texrow->getRowFromIdPos(end_par, max(0, end_pos - 1));
-                       next_end_row = texrow->getRowFromIdPos(end_par, end_pos);
-               } else {
-                       end_row = beg_row;
-                       next_end_row = texrow->getRowFromIdPos(beg_par, beg_pos + 1);
-               }
-               if (end_row != next_end_row)
-                       end_row = next_end_row - 1;
+               std::pair<int,int> rows = texrow_->rowFromCursor(bv_->cursor());
+               int const beg_row = rows.first;
+               int const end_row = rows.second;
 
                QTextCursor c = QTextCursor(viewSourceTV->document());
 
@@ -303,10 +300,23 @@ void ViewSourceWidget::realUpdateView()
                c.clearSelection();
                viewSourceTV->setTextCursor(c);
                viewSourceTV->horizontalScrollBar()->setValue(h_scroll);
-       }
+       } // else if (texrow)
+       viewSourceTV->blockSignals(false);
 }
 
 
+// only used in DEVEL_MODE for debugging
+// need a proper LFUN if we want to implement it in release mode
+void ViewSourceWidget::gotoCursor()
+{
+       if (!bv_ || !texrow_.get())
+               return;
+       int row = viewSourceTV->textCursor().blockNumber() + 1;
+       const_cast<BufferView *>(bv_)->setCursorFromRow(row, *texrow_);
+}
+
+
+
 void ViewSourceWidget::updateDefaultFormat()
 {
        if (!bv_)