]> git.lyx.org Git - lyx.git/blob - src/texrow.C
The "I want this in now" patch.
[lyx.git] / src / texrow.C
1 /**
2  * \file texrow.C
3  * Copyright 1995-2002 the LyX Team
4  * Read the file COPYING
5  *
6  * \author Matthias Ettrich
7  */
8
9 #include <config.h>
10
11 #include "texrow.h"
12 #include "debug.h"
13
14 #include <algorithm>
15
16 using std::find_if;
17 using std::for_each;
18 using std::endl;
19
20 namespace {
21
22 /// function object returning true when row number is found
23 class same_rownumber {
24 public:
25         same_rownumber(int row) : row_(row) {}
26         bool operator()(TexRow::RowList::value_type const & vt) const {
27                 return vt.rownumber() == row_;
28         }
29
30 private:
31         int row_;
32 };
33
34
35 /// increment the pos value of the argument if the par id
36 /// is the same, and the pos parameter is larger
37 class increase_pos {
38 public:
39         increase_pos(int id, int pos)
40                 : id_(id), pos_(pos) {}
41
42         void operator()(TexRow::RowList::value_type & vt) const {
43                 if (vt.id() != id_ || vt.pos() >= pos_)
44                         return;
45                 vt.pos(vt.pos() + 1);
46
47                 lyxerr[Debug::INFO]
48                         << "TeXRow::increasePos: ideally this "
49                         "should never happen..." << endl;
50
51                 // FIXME: When verified to work this clause should be deleted.
52                 if (id_ == vt.id() && pos_ == vt.pos()) {
53                         lyxerr[Debug::INFO]
54                                 << "TexRow::increasePos: this should happen "
55                                 "maximum one time for each run of "
56                                 "increasePos!" << endl;
57                 }
58         }
59
60 private:
61         int id_;
62         int pos_;
63 };
64
65 } // namespace anon
66
67
68 void TexRow::reset()
69 {
70         rowlist.clear();
71         count = 0;
72         lastid = -1;
73         lastpos = -1;
74 }
75
76
77 void TexRow::start(int id, int pos)
78 {
79         lastid = id;
80         lastpos = pos;
81 }
82
83
84 void TexRow::newline()
85 {
86         int const id = lastid;
87         RowList::value_type tmp(id, lastpos, ++count);
88         rowlist.push_back(tmp);
89 }
90
91
92 bool TexRow::getIdFromRow(int row, int & id, int & pos) const
93 {
94         RowList::const_iterator cit =
95                 find_if(rowlist.begin(), rowlist.end(),
96                         same_rownumber(row));
97
98         if (cit != rowlist.end()) {
99                 id = cit->id();
100                 pos = cit->pos();
101                 return true;
102         }
103         id = -1;
104         pos = 0;
105         return false;
106 }
107
108
109 void TexRow::increasePos(int id, int pos)
110 {
111         for_each(rowlist.begin(), rowlist.end(), increase_pos(id, pos));
112 }
113
114
115 TexRow & TexRow::operator+=(TexRow const & tr)
116 {
117         rowlist.insert(rowlist.end(), tr.rowlist.begin(), tr.rowlist.end());
118         return *this;
119 }