]> git.lyx.org Git - lyx.git/blob - src/dociterator.C
Continue to improve GtkLengthEntry
[lyx.git] / src / dociterator.C
1 /**
2  * \file dociterator.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author André Pönitz
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11
12 #include <config.h>
13
14 #include "dociterator.h"
15
16 #include "debug.h"
17 #include "lyxtext.h"
18 #include "lyxrow.h"
19 #include "paragraph.h"
20
21 #include "mathed/math_data.h"
22 #include "mathed/math_inset.h"
23 #include "insets/insettabular.h"
24
25 #include <boost/assert.hpp>
26 #include <boost/current_function.hpp>
27
28 using std::endl;
29
30
31 // We could be able to get rid of this if only every BufferView were
32 // associated to a buffer on construction.
33 DocIterator::DocIterator()
34         : boundary_(false), inset_(0)
35 {}
36
37
38 DocIterator::DocIterator(InsetBase & inset)
39         : boundary_(false), inset_(&inset)
40 {}
41
42
43 DocIterator doc_iterator_begin(InsetBase & inset)
44 {
45         DocIterator dit(inset);
46         dit.forwardPos();
47         return dit;
48 }
49
50
51 DocIterator doc_iterator_end(InsetBase & inset)
52 {
53         return DocIterator(inset);
54 }
55
56
57 InsetBase * DocIterator::nextInset()
58 {
59         BOOST_ASSERT(!empty());
60         if (pos() == lastpos())
61                 return 0;
62         if (pos() > lastpos()) {
63                 lyxerr << "Should not happen, but it does. " << endl;
64                 return 0;
65         }
66         if (inMathed())
67                 return nextAtom().nucleus();
68         return paragraph().isInset(pos()) ? paragraph().getInset(pos()) : 0;
69 }
70
71
72 InsetBase * DocIterator::prevInset()
73 {
74         BOOST_ASSERT(!empty());
75         if (pos() == 0)
76                 return 0;
77         if (inMathed())
78                 return prevAtom().nucleus();
79         return paragraph().isInset(pos() - 1) ? paragraph().getInset(pos() - 1) : 0;
80 }
81
82
83 InsetBase const * DocIterator::prevInset() const
84 {
85         BOOST_ASSERT(!empty());
86         if (pos() == 0)
87                 return 0;
88         if (inMathed())
89                 return prevAtom().nucleus();
90         return paragraph().isInset(pos() - 1) ? paragraph().getInset(pos() - 1) : 0;
91 }
92
93
94 InsetBase * DocIterator::realInset() const
95 {
96         BOOST_ASSERT(inTexted());
97         // if we are in a tabular, we need the cell
98         if (inset().lyxCode() == InsetBase::TABULAR_CODE) {
99                 InsetTabular & tabular = static_cast<InsetTabular&>(inset());
100                 return tabular.cell(idx()).get();
101         }
102         return &inset();
103 }
104
105
106 MathAtom const & DocIterator::prevAtom() const
107 {
108         BOOST_ASSERT(!empty());
109         BOOST_ASSERT(pos() > 0);
110         return cell()[pos() - 1];
111 }
112
113
114 MathAtom & DocIterator::prevAtom()
115 {
116         BOOST_ASSERT(!empty());
117         BOOST_ASSERT(pos() > 0);
118         return cell()[pos() - 1];
119 }
120
121
122 MathAtom const & DocIterator::nextAtom() const
123 {
124         BOOST_ASSERT(!empty());
125         //lyxerr << "lastpos: " << lastpos() << " next atom:\n" << *this << endl;
126         BOOST_ASSERT(pos() < lastpos());
127         return cell()[pos()];
128 }
129
130
131 MathAtom & DocIterator::nextAtom()
132 {
133         BOOST_ASSERT(!empty());
134         //lyxerr << "lastpos: " << lastpos() << " next atom:\n" << *this << endl;
135         BOOST_ASSERT(pos() < lastpos());
136         return cell()[pos()];
137 }
138
139
140 LyXText * DocIterator::text()
141 {
142         BOOST_ASSERT(!empty());
143         return top().text();
144 }
145
146 LyXText const * DocIterator::text() const
147 {
148         BOOST_ASSERT(!empty());
149         return top().text();
150 }
151
152
153 Paragraph & DocIterator::paragraph()
154 {
155         if (!inTexted())
156                 lyxerr << *this << endl;
157         BOOST_ASSERT(inTexted());
158         return top().paragraph();
159 }
160
161
162 Paragraph const & DocIterator::paragraph() const
163 {
164         BOOST_ASSERT(inTexted());
165         return top().paragraph();
166 }
167
168
169 Row & DocIterator::textRow()
170 {
171         BOOST_ASSERT(!paragraph().rows().empty());
172         return paragraph().getRow(pos(), boundary_);
173 }
174
175
176 Row const & DocIterator::textRow() const
177 {
178         BOOST_ASSERT(!paragraph().rows().empty());
179         return paragraph().getRow(pos(), boundary_);
180 }
181
182
183 DocIterator::pit_type DocIterator::lastpit() const
184 {
185         return inMathed() ? 0 : text()->paragraphs().size() - 1;
186 }
187
188
189 DocIterator::pos_type DocIterator::lastpos() const
190 {
191         return inMathed() ? cell().size() : paragraph().size();
192 }
193
194
195 DocIterator::idx_type DocIterator::lastidx() const
196 {
197         return top().lastidx();
198 }
199
200
201 size_t DocIterator::nargs() const
202 {
203         // assume 1x1 grid for main text
204         return top().nargs();
205 }
206
207
208 size_t DocIterator::ncols() const
209 {
210         // assume 1x1 grid for main text
211         return top().ncols();
212 }
213
214
215 size_t DocIterator::nrows() const
216 {
217         // assume 1x1 grid for main text
218         return top().nrows();
219 }
220
221
222 DocIterator::row_type DocIterator::row() const
223 {
224         return top().row();
225 }
226
227
228 DocIterator::col_type DocIterator::col() const
229 {
230         return top().col();
231 }
232
233
234 MathArray const & DocIterator::cell() const
235 {
236         BOOST_ASSERT(inMathed());
237         return top().cell();
238 }
239
240
241 MathArray & DocIterator::cell()
242 {
243         BOOST_ASSERT(inMathed());
244         return top().cell();
245 }
246
247
248 bool DocIterator::inMathed() const
249 {
250         return !empty() && inset().inMathed();
251 }
252
253
254 bool DocIterator::inTexted() const
255 {
256         return !empty() && !inset().inMathed();
257 }
258
259
260 LyXText * DocIterator::innerText()
261 {
262         BOOST_ASSERT(!empty());
263         // Go up until first non-0 text is hit
264         // (innermost text is 0 in mathed)
265         for (int i = depth() - 1; i >= 0; --i)
266                 if (slices_[i].text())
267                         return slices_[i].text();
268         return 0;
269 }
270
271 LyXText const * DocIterator::innerText() const
272 {
273         BOOST_ASSERT(!empty());
274         // go up until first non-0 text is hit
275         // (innermost text is 0 in mathed)
276         for (int i = depth() - 1; i >= 0; --i)
277                 if (slices_[i].text())
278                         return slices_[i].text();
279         return 0;
280 }
281
282
283 InsetBase * DocIterator::innerInsetOfType(int code) const
284 {
285         for (int i = depth() - 1; i >= 0; --i)
286                 if (slices_[i].inset_->lyxCode() == code)
287                         return slices_[i].inset_;
288         return 0;
289 }
290
291
292 void DocIterator::forwardPos(bool ignorecollapsed)
293 {
294         //this dog bites his tail
295         if (empty()) {
296                 push_back(CursorSlice(*inset_));
297                 return;
298         }
299
300         // jump over collapsables if they are collapsed
301         // FIXME: the check for asMathInset() shouldn't be necessary
302         // but math insets do not return a sensible editable() state yet.
303         if (ignorecollapsed && nextInset() && (!nextInset()->asMathInset() 
304             && nextInset()->editable() != InsetBase::HIGHLY_EDITABLE)) {
305                 ++top().pos();
306                 return;
307         }
308
309         CursorSlice & tip = top();
310         //lyxerr << "XXX\n" << *this << endl;
311
312         // this is used twice and shows up in the profiler!
313         pos_type const lastp = lastpos();
314
315         // move into an inset to the right if possible
316         InsetBase * n = 0;
317
318         if (tip.pos() != lastp) {
319                 // this is impossible for pos() == size()
320                 if (inMathed()) {
321                         n = (tip.cell().begin() + tip.pos())->nucleus();
322                 } else {
323                         if (paragraph().isInset(tip.pos()))
324                                 n = paragraph().getInset(tip.pos());
325                 }
326         }
327
328         if (n && n->isActive()) {
329                 //lyxerr << "... descend" << endl;
330                 push_back(CursorSlice(*n));
331                 return;
332         }
333
334         // otherwise move on one position if possible
335         if (tip.pos() < lastp) {
336                 //lyxerr << "... next pos" << endl;
337                 ++tip.pos();
338                 return;
339         }
340         //lyxerr << "... no next pos" << endl;
341
342         // otherwise move on one paragraph if possible
343         if (tip.pit() < lastpit()) {
344                 //lyxerr << "... next par" << endl;
345                 ++tip.pit();
346                 tip.pos() = 0;
347                 return;
348         }
349         //lyxerr << "... no next pit" << endl;
350
351         // otherwise try to move on one cell if possible
352         if (tip.idx() < lastidx()) {
353                 //lyxerr << "... next idx" << endl;
354                 ++tip.idx();
355                 tip.pit() = 0;
356                 tip.pos() = 0;
357                 return;
358         }
359         //lyxerr << "... no next idx" << endl;
360
361         // otherwise leave inset and jump over inset as a whole
362         pop_back();
363         // 'top' is invalid now...
364         if (!empty())
365                 ++top().pos();
366 }
367
368
369 void DocIterator::forwardPosNoDescend()
370 {
371         CursorSlice & tip = top();
372         pos_type const lastp = lastpos();
373
374         //  move on one position if possible
375         if (tip.pos() < lastp) {
376                 //lyxerr << "... next pos" << endl;
377                 ++tip.pos();
378                 return;
379         }
380         //lyxerr << "... no next pos" << endl;
381
382         // otherwise move on one paragraph if possible
383         if (tip.pit() < lastpit()) {
384                 //lyxerr << "... next par" << endl;
385                 ++tip.pit();
386                 tip.pos() = 0;
387                 return;
388         }
389         //lyxerr << "... no next pit" << endl;
390
391         // otherwise try to move on one cell if possible
392         if (tip.idx() < lastidx()) {
393                 //lyxerr << "... next idx" << endl;
394                 ++tip.idx();
395                 tip.pit() = 0;
396                 tip.pos() = 0;
397                 return;
398         }
399         //lyxerr << "... no next idx" << endl;
400
401         // otherwise we can't move on
402 }
403
404
405 void DocIterator::forwardPar()
406 {
407         forwardPos();
408
409 #if 0
410         DocIterator cmp(*this);
411 #endif
412
413         while (!empty() && (!inTexted() || pos() != 0)) {
414                 if (inTexted()) {
415                         pos_type const lastp = lastpos();
416                         Paragraph const & par = paragraph();
417                         pos_type & pos = top().pos();
418                         while (pos < lastp && !par.isInset(pos))
419                                 ++pos;
420                 }
421                 forwardPos();
422         }
423
424 #if 0
425         while (!cmp.empty() && (!cmp.inTexted() || cmp.pos() != 0))
426                 cmp.forwardPos();
427         BOOST_ASSERT(cmp == *this);
428 #endif
429 }
430
431
432 void DocIterator::forwardChar()
433 {
434         forwardPos();
435         while (!empty() && pos() == lastpos())
436                 forwardPos();
437 }
438
439
440 void DocIterator::forwardInset()
441 {
442         forwardPos();
443         while (!empty() && (pos() == lastpos() || nextInset() == 0))
444                 forwardPos();
445 }
446
447
448 void DocIterator::backwardChar()
449 {
450         backwardPos();
451         while (!empty() && pos() == lastpos())
452                 backwardPos();
453 }
454
455
456 void DocIterator::backwardPos()
457 {
458         //this dog bites his tail
459         if (empty()) {
460                 push_back(CursorSlice(*inset_));
461                 top().idx() = lastidx();
462                 top().pit() = lastpit();
463                 top().pos() = lastpos();
464                 return;
465         }
466
467         CursorSlice & tip = top();
468
469         if (tip.pos() != 0) {
470                 --tip.pos();
471         } else if (tip.pit() != 0) {
472                 --tip.pit();
473                 tip.pos() = lastpos();
474                 return;
475         } else if (tip.idx() != 0) {
476                 --tip.idx();
477                 tip.pit() = lastpit();
478                 tip.pos() = lastpos();
479                 return;
480         } else {
481                 pop_back();
482                 return;
483         }
484
485         // move into an inset to the left if possible
486         InsetBase * n = 0;
487
488         if (inMathed()) {
489                 n = (tip.cell().begin() + tip.pos())->nucleus();
490         } else {
491                 if (paragraph().isInset(tip.pos()))
492                         n = paragraph().getInset(tip.pos());
493         }
494
495         if (n && n->isActive()) {
496                 push_back(CursorSlice(*n));
497                 top().idx() = lastidx();
498                 top().pit() = lastpit();
499                 top().pos() = lastpos();
500         }
501 }
502
503
504 bool DocIterator::hasPart(DocIterator const & it) const
505 {
506         // it can't be a part if it is larger
507         if (it.depth() > depth())
508                 return false;
509
510         // as inset adresses are the 'last' level
511         return &it.top().inset() == &slices_[it.depth() - 1].inset();
512 }
513
514
515 void DocIterator::updateInsets(InsetBase * inset) 
516 {
517         // this function re-creates the cache of inset pointers.
518         // code taken in part from StableDocIterator::asDocIterator.
519         //lyxerr << "converting:\n" << *this << endl;
520         DocIterator dit = DocIterator(*inset);
521         size_t const n = slices_.size();
522         for (size_t i = 0 ; i < n; ++i) {
523                 BOOST_ASSERT(inset);
524                 dit.push_back(slices_[i]);
525                 dit.top().inset_ = inset;
526                 if (i + 1 != n)
527                         inset = dit.nextInset();
528         }
529         //lyxerr << "converted:\n" << *this << endl;
530         operator=(dit);
531 }
532
533
534 std::ostream & operator<<(std::ostream & os, DocIterator const & dit)
535 {
536         for (size_t i = 0, n = dit.depth(); i != n; ++i)
537                 os << " " << dit[i] << "\n";
538         return os;
539 }
540
541
542
543 ///////////////////////////////////////////////////////
544
545 StableDocIterator::StableDocIterator(DocIterator const & dit)
546 {
547         data_ = dit.internalData();
548         for (size_t i = 0, n = data_.size(); i != n; ++i)
549                 data_[i].inset_ = 0;
550 }
551
552
553 DocIterator StableDocIterator::asDocIterator(InsetBase * inset) const
554 {
555         // this function re-creates the cache of inset pointers
556         //lyxerr << "converting:\n" << *this << endl;
557         DocIterator dit = DocIterator(*inset);
558         for (size_t i = 0, n = data_.size(); i != n; ++i) {
559                 if (inset == 0) {
560                         // FIXME
561                         lyxerr << BOOST_CURRENT_FUNCTION
562                                << " Should not happen, but does e.g. after C-n C-l C-z S-C-z"
563                                 << '\n' << "dit: " << dit << '\n'
564                                 << " lastpos: " << dit.lastpos() << endl;
565                         //break;
566                         BOOST_ASSERT(false);
567                 }
568                 dit.push_back(data_[i]);
569                 dit.top().inset_ = inset;
570                 if (i + 1 != n)
571                         inset = dit.nextInset();
572         }
573         //lyxerr << "convert:\n" << *this << " to:\n" << dit << endl;
574         return dit;
575 }
576
577
578 std::ostream & operator<<(std::ostream & os, StableDocIterator const & dit)
579 {
580         for (size_t i = 0, n = dit.data_.size(); i != n; ++i)
581                 os << " " << dit.data_[i] << "\n";
582         return os;
583 }
584
585
586 bool operator==(StableDocIterator const & dit1, StableDocIterator const & dit2)
587 {
588         return dit1.data_ == dit2.data_;
589 }
590