]> git.lyx.org Git - lyx.git/blob - src/texrow.C
small parser tweaks
[lyx.git] / src / texrow.C
1 /* This file is part of
2  * ======================================================
3  *
4  *           LyX, The Document Processor
5  *
6  *          Copyright 1995 Matthias Ettrich
7  *          Copyright 1995-2001 The LyX Team.
8  *
9  * ====================================================== */
10
11 #include <config.h>
12
13 #include <algorithm>
14
15 #ifdef __GNUG__
16 #pragma implementation
17 #endif
18
19 #include "texrow.h"
20 #include "paragraph.h"
21 #include "debug.h"
22
23 using std::find_if;
24 using std::endl;
25
26 // Delete linked list
27 void TexRow::reset()
28 {
29         rowlist.clear();
30         count = 0;
31         lastpar = 0;
32         lastpos = -1;
33 }
34
35
36 // Defines paragraph and position for the beginning of this row
37 void TexRow::start(Paragraph * par, int pos)
38 {
39         lastpar = par;
40         lastpos = pos;
41 }
42
43
44 // Insert node when line is completed
45 void TexRow::newline()
46 {
47         RowItem tmp;
48         tmp.pos(lastpos);
49         if (lastpar)
50                 tmp.id(lastpar->id());
51         else
52                 tmp.id(-1);
53         tmp.rownumber(++count);
54         rowlist.push_back(tmp);
55 }
56
57
58 class same_rownumber {
59 public:
60         same_rownumber(TexRow::RowList::value_type const & v):vt(v) {}
61         bool operator()(TexRow::RowList::value_type const & vt1) const {
62                 return vt.rownumber() == vt1.rownumber();
63         }
64 private:
65         TexRow::RowList::value_type const & vt;
66 };
67
68
69
70 bool TexRow::getIdFromRow(int row, int & id, int & pos) const
71 {
72         RowList::value_type vt;
73         vt.rownumber(row);
74         RowList::const_iterator cit =
75                 find_if(rowlist.begin(), rowlist.end(), same_rownumber(vt));
76
77         if (cit != rowlist.end()) {
78                 id = cit->id();
79                 pos = cit->pos();
80                 return true;
81         }
82         id = -1;
83         pos = 0;
84         return false;
85 }
86
87
88 // should perhaps have a better name...
89 // Increase the pos of all rows with the
90 // same id (and where the pos is larger)
91 // to avoid putting errorinsets at the
92 // same pos.
93 void TexRow::increasePos(int id, int pos) const
94 {
95         RowList::iterator kit = rowlist.begin();
96         RowList::iterator end = rowlist.end();
97         for (; kit != end; ++kit) {
98                 if (id == kit->id()
99                     && pos < kit->pos()) {
100                         kit->pos(kit->pos() + 1);
101                         lyxerr[Debug::INFO]
102                                 << "TeXRow::increasePos: ideally this "
103                                 "should never happen..." << endl;
104                 }
105                 // When verified to work this clause should be deleted.
106                 if (id == kit->id()
107                     && pos == kit->pos()) {
108                         lyxerr[Debug::INFO]
109                                 << "TexRow::increasePos: this should happen "
110                                 "maximum one time for each run of "
111                                 "increasePos!" << endl;
112                 }
113         }
114 }
115
116
117 TexRow & TexRow::operator+= (TexRow const & tr)
118 {
119         rowlist.insert(rowlist.end(), tr.rowlist.begin(), tr.rowlist.end());
120         return *this;
121 }