Bo Peng wrote: > On 6/20/07, Bo Peng <[EMAIL PROTECTED]> > wrote: >> > This is probably 1.5.x stuff at minimum, but I'd appreciate comments >> > very much nevertheless... >> >> Looks good to me right now... > > I am not familiar with textrow() stuff, I guess Abdel can comment on > that. One thing I notice that your patch does not work for tables. > > The viewsource part is fine to me. > > Cheers, > Bo
Updated patches: - The texrow part, should be safe as is an equivalent transformation (list -> vector) plus a new function getRowFromIdPos (difference with previous patch: the missing include you corrected + a bug fixed (the row index in getIdFromRow goes from 1..rowlist.size(), not 0..rowlist.size-1)) - The gui part, small and purely ui (but depends on the first) plus a small adjustment to buffer (the texrow was out of sync because of the small comments added to the source). PS: you get a grey selection on the source code? here it is blue. Do you know how to how to change the color with only qt 4.1 libs? PS2: I'm ok if one or both of them get applied after 1.5.0 is out. A/
Index: Buffer.cpp =================================================================== --- Buffer.cpp (revision 18837) +++ Buffer.cpp (working copy) @@ -1746,8 +1746,11 @@ // No side effect of file copying and image conversion runparams.dryrun = true; + texrow().reset(); if (full_source) { os << "% " << _("Preview source code") << "\n\n"; + texrow().newline(); + texrow().newline(); if (isLatex()) writeLaTeXSource(os, filePath(), runparams, true, true); else { @@ -1766,9 +1769,10 @@ convert<docstring>(par_begin), convert<docstring>(par_end - 1)) << "\n\n"; + texrow().newline(); + texrow().newline(); // output paragraphs if (isLatex()) { - texrow().reset(); latexParagraphs(*this, paragraphs(), os, texrow(), runparams); } else { // DocBook Index: frontends/controllers/ControlViewSource.h =================================================================== --- frontends/controllers/ControlViewSource.h (revision 18837) +++ frontends/controllers/ControlViewSource.h (working copy) @@ -45,6 +45,9 @@ \param fullSource get full source code */ docstring const updateContent(bool fullSource); + /** get the cursor position in the source code + */ + int getRow() const; }; } // namespace frontend Index: frontends/controllers/ControlViewSource.cpp =================================================================== --- frontends/controllers/ControlViewSource.cpp (revision 18837) +++ frontends/controllers/ControlViewSource.cpp (working copy) @@ -18,6 +18,7 @@ #include "BufferView.h" #include "Buffer.h" #include "Cursor.h" +#include "TexRow.h" #include <sstream> using std::string; @@ -58,6 +59,15 @@ } +int ControlViewSource::getRow() const +{ + BufferView const * view = kernel().bufferview(); + return view->buffer()->texrow(). + getRowFromIdPos(view->cursor().bottom().paragraph().id(), + view->cursor().bottom().pos()); +} + + void ControlViewSource::clearParams() { } Index: frontends/qt4/QViewSource.cpp =================================================================== --- frontends/qt4/QViewSource.cpp (revision 18837) +++ frontends/qt4/QViewSource.cpp (working copy) @@ -61,6 +61,11 @@ if (autoUpdateCB->isChecked()) form_->update(viewFullSourceCB->isChecked()); + int row = form_->getRow(); + QTextCursor c = QTextCursor(viewSourceTV->document()); + c.movePosition(QTextCursor::NextBlock, QTextCursor::MoveAnchor, row); + c.select(QTextCursor::BlockUnderCursor); + viewSourceTV->setTextCursor(c); QWidget::update(); }
Index: TexRow.h =================================================================== --- TexRow.h (revision 18837) +++ TexRow.h (working copy) @@ -14,7 +14,7 @@ #ifndef TEXROW_H #define TEXROW_H -#include <list> +#include <vector> namespace lyx { @@ -24,10 +24,8 @@ class TexRow { public: /// - TexRow() : count(0), lastid(-1), lastpos(-1) {} + TexRow() : lastid(-1), lastpos(-1) {} - TexRow & operator+= (TexRow const &); - /// Clears structure void reset(); @@ -49,14 +47,22 @@ */ bool getIdFromRow(int row, int & id, int & pos) const; + /** + * getIdFromRow - find row containing a given id and pos + * @param id of the paragraph + * @param pos a given position in that paragraph + * @return the row number within the rowlist + */ + int getRowFromIdPos(int id, int pos) const; + /// Returns the number of rows contained - int rows() const { return count; } + int rows() const { return rowlist.size(); } /// an individual id/pos <=> row mapping class RowItem { public: - RowItem(int id, int pos, int row) - : id_(id), pos_(pos), rownumber_(row) + RowItem(int id, int pos) + : id_(id), pos_(pos) {} /// paragraph id @@ -74,21 +80,14 @@ return pos_; } - /// row number - int rownumber() const { - return rownumber_; - } private: RowItem(); int id_; int pos_; - int rownumber_; }; /// - typedef std::list<RowItem> RowList; + typedef std::vector<RowItem> RowList; private: - /// number of lines - unsigned int count; /// container of id/pos <=> row mapping RowList rowlist; /// Last paragraph Index: TexRow.cpp =================================================================== --- TexRow.cpp (revision 18837) +++ TexRow.cpp (working copy) @@ -20,30 +20,10 @@ namespace lyx { -using std::find_if; - -namespace { - -/// function object returning true when row number is found -class same_rownumber { -public: - same_rownumber(int row) : row_(row) {} - bool operator()(TexRow::RowList::value_type const & vt) const { - return vt.rownumber() == row_; - } - -private: - int row_; -}; - -} // namespace anon - - void TexRow::reset() { rowlist.clear(); - count = 0; lastid = -1; lastpos = -1; } @@ -59,32 +39,41 @@ void TexRow::newline() { int const id = lastid; - RowList::value_type tmp(id, lastpos, ++count); + RowList::value_type tmp(id, lastpos); rowlist.push_back(tmp); } bool TexRow::getIdFromRow(int row, int & id, int & pos) const { - RowList::const_iterator cit = - find_if(rowlist.begin(), rowlist.end(), - same_rownumber(row)); - - if (cit != rowlist.end()) { - id = cit->id(); - pos = cit->pos(); - return true; + if (row <= 0 || row > rowlist.size()) { + id = -1; + pos = 0; + return false; } - id = -1; - pos = 0; - return false; + + id = rowlist[row - 1].id(); + pos = rowlist[row - 1].pos(); + return true; } -TexRow & TexRow::operator+=(TexRow const & tr) +int TexRow::getRowFromIdPos(int id, int pos) const { - rowlist.insert(rowlist.end(), tr.rowlist.begin(), tr.rowlist.end()); - return *this; + int bestrow = 0; + bool foundid = false; + + // this loop finds the last *nonempty* row whith the same id + // and position <= pos + for (unsigned r = 0, n = rowlist.size(); r != n; ++r) { + if (rowlist[r].id() == id && rowlist[r].pos() <= pos) { + foundid = true; + if (rowlist[bestrow].id() != id || rowlist[r].pos() > rowlist[bestrow].pos()) + bestrow = r; + } else if (foundid) + break; + } + return bestrow; }