#include "support/filetools.h"
#include "support/lyxfunctional.h" //equal_1st_in_pair
#include "support/types.h"
+#include "support/lyxalgo.h" // lyx_count
#include <fstream>
-#include <algorithm>
extern BufferList bufferlist;
using std::ifstream;
using std::vector;
using std::find;
-using std::count;
using std::count_if;
bool BufferView::removeAutoInsets()
{
- LyXCursor tmpcursor = text->cursor;
- Paragraph * cur_par = tmpcursor.par();
- pos_type cur_pos = tmpcursor.pos();
-
+ Paragraph * cur_par = text->cursor.par();
+ Paragraph * cur_par_prev = cur_par ? cur_par->previous() : 0;
+ Paragraph * cur_par_next = cur_par ? cur_par->next() : 0;
+ pos_type cur_pos = text->cursor.pos();
+
bool found = false;
+ // Trap the deletion of the paragraph the cursor is in.
+ // Iterate until we find a paragraph that won't be immediately deleted.
+ // In reality this should mean we only execute the body of the while
+ // loop once at most. However for safety we iterate rather than just
+ // make this an if() conditional.
+ while ((cur_par_prev || cur_par_next)
+ && text->setCursor(this,
+ cur_par_prev ? cur_par_prev : cur_par_next,
+ 0)) {
+ // We just removed cur_par so have to fix the "cursor"
+ if (cur_par_prev) {
+ // '.' = cur_par
+ // a -> a.
+ // .
+ cur_par = cur_par_prev;
+ cur_pos = cur_par->size();
+ } else {
+ // . -> .a
+ // a
+ cur_par = cur_par_next;
+ cur_pos = 0;
+ }
+ cur_par_prev = cur_par->previous();
+ cur_par_next = cur_par->next();
+ }
+
ParIterator it = buffer()->par_iterator_begin();
ParIterator end = buffer()->par_iterator_end();
for (; it != end; ++it) {
Paragraph * par = *it;
+ Paragraph * par_prev = par ? par->previous() : 0;
bool removed = false;
- text->setCursor(this, par, 0);
-
+ if (text->setCursor(this, par, 0)
+ && cur_par == par_prev) {
+ // The previous setCursor line was deleted and that
+ // was the cur_par line. This can only happen if an
+ // error box was the sole item on cur_par.
+ // It is possible for cur_par_prev to be stray if
+ // the line it pointed to only had a error box on it
+ // so we have to set it to a known correct value.
+ // This is often the same value it already had.
+ cur_par_prev = par->previous();
+ if (cur_par_prev) {
+ // '|' = par, '.' = cur_par, 'E' = error box
+ // First step below may occur before while{}
+ // a |a a a a.
+ // E -> .E -> |.E -> . -> |b
+ // . b b |b
+ // b
+ cur_par = cur_par_prev;
+ cur_pos = cur_par_prev->size();
+ cur_par_prev = cur_par->previous();
+ // cur_par_next remains the same
+ } else if (cur_par_next) {
+ // First step below may occur before while{}
+ // .
+ // E -> |.E -> |. -> . -> .|a
+ // a a a |a
+ cur_par = cur_par_next;
+ cur_pos = 0;
+ // cur_par_prev remains unset
+ cur_par_next = cur_par->next();
+ } else {
+ // I can't find a way to trigger this
+ // so it should be unreachable code
+ // unless the buffer is corrupted.
+ lyxerr << "BufferView::removeAutoInsets() is bad\n";
+ }
+ }
+
Paragraph::inset_iterator pit = par->inset_iterator_begin();
Paragraph::inset_iterator pend = par->inset_iterator_end();
while (pit != pend) {
}
}
+ // It is possible that the last line is empty if it was cur_par and/or
+ // only had an error inset on it.
+ if (text->setCursor(this, text->ownerParagraph(), 0)
+ && 0 == cur_par_next) {
+ cur_par = cur_par_prev;
+ cur_pos = cur_par->size();
+ } else if (cur_pos > cur_par->size()) {
+ // Some C-Enter lines were removed by the setCursor call which
+ // then invalidated cur_pos. It could still be "wrong" because
+ // the cursor may appear to have jumped but since we collapsed
+ // some C-Enter lines this should be a reasonable compromise.
+ cur_pos = cur_par->size();
+ }
+
text->setCursorIntern(this, cur_par, cur_pos);
return found;
Paragraph * texrowpar = 0;
if (tmpid == -1) {
- texrowpar = text->firstParagraph();
+ texrowpar = text->ownerParagraph();
tmppos = 0;
} else {
texrowpar = buffer()->getParFromID(tmpid);
Paragraph * texrowpar;
if (tmpid == -1) {
- texrowpar = text->firstParagraph();
+ texrowpar = text->ownerParagraph();
tmppos = 0;
} else {
texrowpar = buffer()->getParFromID(tmpid);
{
// Check if the label 'from' appears more than once
vector<string> labels = buffer()->getLabelList();
- // count is broken on some systems, so use the HP version
- int res;
- count(labels.begin(), labels.end(), from, res);
- if (res > 1)
+
+ if (lyx::count(labels.begin(), labels.end(), from) > 1)
return false;
return ChangeInsets(Inset::REF_CODE, from, to);