]> git.lyx.org Git - lyx.git/blob - src/text.C
Various trivial bits 'n' bats about config.h, header blurb etc.
[lyx.git] / src / text.C
1 /**
2  * \file text.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Asger Alstrup
7  * \author Lars Gullik Bjønnes
8  * \author Jean-Marc Lasgouttes
9  * \author John Levon
10  * \author André Pönitz
11  * \author Dekel Tsur
12  * \author Jürgen Vigna
13  *
14  * Full author contact details are available in file CREDITS.
15  */
16
17 #include <config.h>
18
19 #include "lyxtext.h"
20
21 #include "buffer.h"
22 #include "BufferView.h"
23 #include "debug.h"
24 #include "encoding.h"
25 #include "funcrequest.h"
26 #include "gettext.h"
27 #include "language.h"
28 #include "lyxrc.h"
29 #include "lyxrow.h"
30 #include "lyxrow_funcs.h"
31 #include "paragraph_funcs.h"
32 #include "ParagraphParameters.h"
33 #include "rowpainter.h"
34 #include "text_funcs.h"
35 #include "undo_funcs.h"
36 #include "WordLangTuple.h"
37
38 #include "frontends/font_metrics.h"
39 #include "frontends/LyXView.h"
40
41 #include "insets/insettext.h"
42
43 #include "support/LAssert.h"
44 #include "support/lstrings.h"
45 #include "support/textutils.h"
46
47
48 using namespace lyx::support;
49
50 using std::max;
51 using std::endl;
52
53 using lyx::pos_type;
54 using lyx::word_location;
55
56 using namespace bv_funcs;
57
58 /// top, right, bottom pixel margin
59 extern int const PAPER_MARGIN = 20;
60 /// margin for changebar
61 extern int const CHANGEBAR_MARGIN = 10;
62 /// left margin
63 extern int const LEFT_MARGIN = PAPER_MARGIN + CHANGEBAR_MARGIN;
64
65
66
67 int bibitemMaxWidth(BufferView *, LyXFont const &);
68
69
70 BufferView * LyXText::bv()
71 {
72         Assert(bv_owner != 0);
73         return bv_owner;
74 }
75
76
77 BufferView * LyXText::bv() const
78 {
79         Assert(bv_owner != 0);
80         return bv_owner;
81 }
82
83
84 void LyXText::updateRowPositions()
85 {
86         ParagraphList::iterator pit = ownerParagraphs().begin();
87         ParagraphList::iterator end = ownerParagraphs().end();
88         for (height = 0; pit != end; ++pit) {
89                 RowList::iterator rit = pit->rows.begin();
90                 RowList::iterator rend = pit->rows.end();
91                 for ( ; rit != rend ; rit = ++rit) {
92                         rit->y(height);
93                         height += rit->height();
94                 }
95         }
96 }
97
98
99 int LyXText::workWidth() const
100 {
101         return inset_owner ? inset_owner->textWidth() : bv()->workWidth();
102 }
103
104
105 int LyXText::getRealCursorX() const
106 {
107         int x = cursor.x();
108         if (the_locking_inset && (the_locking_inset->getLyXText(bv())!= this))
109                 x = the_locking_inset->getLyXText(bv())->getRealCursorX();
110         return x;
111 }
112
113
114 #warning FIXME  This function seems to belong outside of LyxText.
115 unsigned char LyXText::transformChar(unsigned char c, Paragraph const & par,
116                                      pos_type pos) const
117 {
118         if (!Encodings::is_arabic(c))
119                 if (lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 && IsDigit(c))
120                         return c + (0xb0 - '0');
121                 else
122                         return c;
123
124         unsigned char const prev_char = pos > 0 ? par.getChar(pos - 1) : ' ';
125         unsigned char next_char = ' ';
126
127         pos_type const par_size = par.size();
128
129         for (pos_type i = pos + 1; i < par_size; ++i) {
130                 unsigned char const par_char = par.getChar(i);
131                 if (!Encodings::IsComposeChar_arabic(par_char)) {
132                         next_char = par_char;
133                         break;
134                 }
135         }
136
137         if (Encodings::is_arabic(next_char)) {
138                 if (Encodings::is_arabic(prev_char) &&
139                         !Encodings::is_arabic_special(prev_char))
140                         return Encodings::TransformChar(c, Encodings::FORM_MEDIAL);
141                 else
142                         return Encodings::TransformChar(c, Encodings::FORM_INITIAL);
143         } else {
144                 if (Encodings::is_arabic(prev_char) &&
145                         !Encodings::is_arabic_special(prev_char))
146                         return Encodings::TransformChar(c, Encodings::FORM_FINAL);
147                 else
148                         return Encodings::TransformChar(c, Encodings::FORM_ISOLATED);
149         }
150 }
151
152 // This is the comments that some of the warnings below refers to.
153 // There are some issues in this file and I don't think they are
154 // really related to the FIX_DOUBLE_SPACE patch. I'd rather think that
155 // this is a problem that has been here almost from day one and that a
156 // larger userbase with differenct access patters triggers the bad
157 // behaviour. (segfaults.) What I think happen is: In several places
158 // we store the paragraph in the current cursor and then moves the
159 // cursor. This movement of the cursor will delete paragraph at the
160 // old position if it is now empty. This will make the temporary
161 // pointer to the old cursor paragraph invalid and dangerous to use.
162 // And is some cases this will trigger a segfault. I have marked some
163 // of the cases where this happens with a warning, but I am sure there
164 // are others in this file and in text2.C. There is also a note in
165 // Delete() that you should read. In Delete I store the paragraph->id
166 // instead of a pointer to the paragraph. I am pretty sure this faulty
167 // use of temporary pointers to paragraphs that might have gotten
168 // invalidated (through a cursor movement) before they are used, are
169 // the cause of the strange crashes we get reported often.
170 //
171 // It is very tiresom to change this code, especially when it is as
172 // hard to read as it is. Help to fix all the cases where this is done
173 // would be greately appreciated.
174 //
175 // Lgb
176
177 int LyXText::singleWidth(ParagraphList::iterator pit, pos_type pos) const
178 {
179         if (pos >= pit->size())
180                 return 0;
181
182         char const c = pit->getChar(pos);
183         LyXFont const & font = getFont(pit, pos);
184         return singleWidth(pit, pos, c, font);
185 }
186
187
188 int LyXText::singleWidth(ParagraphList::iterator pit,
189                          pos_type pos, char c, LyXFont const & font) const
190 {
191         if (pos >= pit->size()) {
192                 lyxerr << "in singleWidth(), pos: " << pos << endl;
193                 Assert(false);
194                 return 0;
195         }
196
197
198         // The most common case is handled first (Asger)
199         if (IsPrintable(c)) {
200                 if (font.language()->RightToLeft()) {
201                         if ((lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 ||
202                              lyxrc.font_norm_type == LyXRC::ISO_10646_1)
203                             && font.language()->lang() == "arabic") {
204                                 if (Encodings::IsComposeChar_arabic(c))
205                                         return 0;
206                                 else
207                                         c = transformChar(c, *pit, pos);
208                         } else if (font.language()->lang() == "hebrew" &&
209                                  Encodings::IsComposeChar_hebrew(c))
210                                 return 0;
211                 }
212                 return font_metrics::width(c, font);
213         }
214
215         if (c == Paragraph::META_INSET) {
216                 InsetOld * tmpinset = pit->getInset(pos);
217                 Assert(tmpinset);
218                 if (tmpinset->lyxCode() == InsetOld::HFILL_CODE) {
219                         // Because of the representation as vertical lines
220                         return 3;
221                 }
222                 return tmpinset->width();
223         }
224
225         if (IsSeparatorChar(c))
226                 c = ' ';
227         return font_metrics::width(c, font);
228 }
229
230
231 lyx::pos_type LyXText::log2vis(lyx::pos_type pos) const
232 {
233         if (bidi_start == -1)
234                 return pos;
235         else
236                 return log2vis_list[pos - bidi_start];
237 }
238
239
240 lyx::pos_type LyXText::vis2log(lyx::pos_type pos) const
241 {
242         if (bidi_start == -1)
243                 return pos;
244         else
245                 return vis2log_list[pos - bidi_start];
246 }
247
248
249 lyx::pos_type LyXText::bidi_level(lyx::pos_type pos) const
250 {
251         if (bidi_start == -1)
252                 return 0;
253         else
254                 return bidi_levels[pos - bidi_start];
255 }
256
257
258 bool LyXText::bidi_InRange(lyx::pos_type pos) const
259 {
260         return bidi_start == -1 ||
261                 (bidi_start <= pos && pos <= bidi_end);
262 }
263
264
265 void LyXText::computeBidiTables(ParagraphList::iterator pit,
266    Buffer const & buf, RowList::iterator row) const
267 {
268         bidi_same_direction = true;
269         if (!lyxrc.rtl_support) {
270                 bidi_start = -1;
271                 return;
272         }
273
274         InsetOld * inset = pit->inInset();
275         if (inset && inset->owner() &&
276             inset->owner()->lyxCode() == InsetOld::ERT_CODE) {
277                 bidi_start = -1;
278                 return;
279         }
280
281         bidi_start = row->pos();
282         bidi_end = lastPos(*pit, row);
283
284         if (bidi_start > bidi_end) {
285                 bidi_start = -1;
286                 return;
287         }
288
289         if (bidi_end + 2 - bidi_start >
290             static_cast<pos_type>(log2vis_list.size())) {
291                 pos_type new_size =
292                         (bidi_end + 2 - bidi_start < 500) ?
293                         500 : 2 * (bidi_end + 2 - bidi_start);
294                 log2vis_list.resize(new_size);
295                 vis2log_list.resize(new_size);
296                 bidi_levels.resize(new_size);
297         }
298
299         vis2log_list[bidi_end + 1 - bidi_start] = -1;
300         log2vis_list[bidi_end + 1 - bidi_start] = -1;
301
302         pos_type stack[2];
303         bool const rtl_par =
304                 pit->isRightToLeftPar(buf.params);
305         int level = 0;
306         bool rtl = false;
307         bool rtl0 = false;
308         pos_type const body_pos = pit->beginningOfBody();
309
310         for (pos_type lpos = bidi_start; lpos <= bidi_end; ++lpos) {
311                 bool is_space = pit->isLineSeparator(lpos);
312                 pos_type const pos =
313                         (is_space && lpos + 1 <= bidi_end &&
314                          !pit->isLineSeparator(lpos + 1) &&
315                          !pit->isNewline(lpos + 1))
316                         ? lpos + 1 : lpos;
317                 LyXFont font = pit->getFontSettings(buf.params, pos);
318                 if (pos != lpos && 0 < lpos && rtl0 && font.isRightToLeft() &&
319                     font.number() == LyXFont::ON &&
320                     pit->getFontSettings(buf.params, lpos - 1).number()
321                     == LyXFont::ON) {
322                         font = pit->getFontSettings(buf.params, lpos);
323                         is_space = false;
324                 }
325
326
327                 bool new_rtl = font.isVisibleRightToLeft();
328                 bool new_rtl0 = font.isRightToLeft();
329                 int new_level;
330
331                 if (lpos == body_pos - 1
332                     && row->pos() < body_pos - 1
333                     && is_space) {
334                         new_level = (rtl_par) ? 1 : 0;
335                         new_rtl = new_rtl0 = rtl_par;
336                 } else if (new_rtl0)
337                         new_level = (new_rtl) ? 1 : 2;
338                 else
339                         new_level = (rtl_par) ? 2 : 0;
340
341                 if (is_space && new_level >= level) {
342                         new_level = level;
343                         new_rtl = rtl;
344                         new_rtl0 = rtl0;
345                 }
346
347                 int new_level2 = new_level;
348
349                 if (level == new_level && rtl0 != new_rtl0) {
350                         --new_level2;
351                         log2vis_list[lpos - bidi_start] = (rtl) ? 1 : -1;
352                 } else if (level < new_level) {
353                         log2vis_list[lpos - bidi_start] =  (rtl) ? -1 : 1;
354                         if (new_level > rtl_par)
355                                 bidi_same_direction = false;
356                 } else
357                         log2vis_list[lpos - bidi_start] = (new_rtl) ? -1 : 1;
358                 rtl = new_rtl;
359                 rtl0 = new_rtl0;
360                 bidi_levels[lpos - bidi_start] = new_level;
361
362                 while (level > new_level2) {
363                         pos_type old_lpos = stack[--level];
364                         int delta = lpos - old_lpos - 1;
365                         if (level % 2)
366                                 delta = -delta;
367                         log2vis_list[lpos - bidi_start] += delta;
368                         log2vis_list[old_lpos - bidi_start] += delta;
369                 }
370                 while (level < new_level)
371                         stack[level++] = lpos;
372         }
373
374         while (level > 0) {
375                 pos_type const old_lpos = stack[--level];
376                 int delta = bidi_end - old_lpos;
377                 if (level % 2)
378                         delta = -delta;
379                 log2vis_list[old_lpos - bidi_start] += delta;
380         }
381
382         pos_type vpos = bidi_start - 1;
383         for (pos_type lpos = bidi_start;
384              lpos <= bidi_end; ++lpos) {
385                 vpos += log2vis_list[lpos - bidi_start];
386                 vis2log_list[vpos - bidi_start] = lpos;
387                 log2vis_list[lpos - bidi_start] = vpos;
388         }
389 }
390
391
392 // This method requires a previous call to ComputeBidiTables()
393 bool LyXText::isBoundary(Buffer const & buf, Paragraph const & par,
394                          pos_type pos) const
395 {
396         if (!lyxrc.rtl_support || pos == 0)
397                 return false;
398
399         if (!bidi_InRange(pos - 1)) {
400                 /// This can happen if pos is the first char of a row.
401                 /// Returning false in this case is incorrect!
402                 return false;
403         }
404
405         bool const rtl = bidi_level(pos - 1) % 2;
406         bool const rtl2 = bidi_InRange(pos)
407                 ? bidi_level(pos) % 2
408                 : par.isRightToLeftPar(buf.params);
409         return rtl != rtl2;
410 }
411
412
413 bool LyXText::isBoundary(Buffer const & buf, Paragraph const & par,
414                          pos_type pos, LyXFont const & font) const
415 {
416         if (!lyxrc.rtl_support)
417                 return false;    // This is just for speedup
418
419         bool const rtl = font.isVisibleRightToLeft();
420         bool const rtl2 = bidi_InRange(pos)
421                 ? bidi_level(pos) % 2
422                 : par.isRightToLeftPar(buf.params);
423         return rtl != rtl2;
424 }
425
426
427 int LyXText::leftMargin(ParagraphList::iterator pit, Row const & row) const
428 {
429         LyXTextClass const & tclass =
430                 bv()->buffer()->params.getLyXTextClass();
431         LyXLayout_ptr const & layout = pit->layout();
432
433         string parindent = layout->parindent;
434
435         int x = LEFT_MARGIN;
436
437         x += font_metrics::signedWidth(tclass.leftmargin(), tclass.defaultfont());
438
439         // this is the way, LyX handles the LaTeX-Environments.
440         // I have had this idea very late, so it seems to be a
441         // later added hack and this is true
442         if (!pit->getDepth()) {
443                 if (pit->layout() == tclass.defaultLayout()) {
444                         // find the previous same level paragraph
445                         if (pit != ownerParagraphs().begin()) {
446                                 ParagraphList::iterator newpit =
447                                         depthHook(pit, ownerParagraphs(),
448                                                   pit->getDepth());
449                                 if (newpit == pit &&
450                                     newpit->layout()->nextnoindent)
451                                         parindent.erase();
452                         }
453                 }
454         } else {
455                 // find the next level paragraph
456
457                 ParagraphList::iterator newpar = outerHook(pit,
458                                                            ownerParagraphs());
459
460                 // make a corresponding row. Needed to call leftMargin()
461
462                 // check wether it is a sufficent paragraph
463                 if (newpar != ownerParagraphs().end() &&
464                     newpar->layout()->isEnvironment()) {
465                         x = leftMargin(newpar, Row(newpar->size()));
466                 }
467
468                 if (newpar != ownerParagraphs().end() &&
469                     pit->layout() == tclass.defaultLayout()) {
470                         if (newpar->params().noindent())
471                                 parindent.erase();
472                         else {
473                                 parindent = newpar->layout()->parindent;
474                         }
475
476                 }
477         }
478
479         LyXFont const labelfont = getLabelFont(pit);
480         switch (layout->margintype) {
481         case MARGIN_DYNAMIC:
482                 if (!layout->leftmargin.empty()) {
483                         x += font_metrics::signedWidth(layout->leftmargin,
484                                                   tclass.defaultfont());
485                 }
486                 if (!pit->getLabelstring().empty()) {
487                         x += font_metrics::signedWidth(layout->labelindent,
488                                                   labelfont);
489                         x += font_metrics::width(pit->getLabelstring(),
490                                             labelfont);
491                         x += font_metrics::width(layout->labelsep, labelfont);
492                 }
493                 break;
494         case MARGIN_MANUAL:
495                 x += font_metrics::signedWidth(layout->labelindent, labelfont);
496                 // The width of an empty par, even with manual label, should be 0
497                 if (!pit->empty() && row.pos() >= pit->beginningOfBody()) {
498                         if (!pit->getLabelWidthString().empty()) {
499                                 x += font_metrics::width(pit->getLabelWidthString(),
500                                                labelfont);
501                                 x += font_metrics::width(layout->labelsep, labelfont);
502                         }
503                 }
504                 break;
505         case MARGIN_STATIC:
506                 x += font_metrics::signedWidth(layout->leftmargin, tclass.defaultfont()) * 4
507                         / (pit->getDepth() + 4);
508                 break;
509         case MARGIN_FIRST_DYNAMIC:
510                 if (layout->labeltype == LABEL_MANUAL) {
511                         if (row.pos() >= pit->beginningOfBody()) {
512                                 x += font_metrics::signedWidth(layout->leftmargin,
513                                                           labelfont);
514                         } else {
515                                 x += font_metrics::signedWidth(layout->labelindent,
516                                                           labelfont);
517                         }
518                 } else if (row.pos()
519                            // Special case to fix problems with
520                            // theorems (JMarc)
521                            || (layout->labeltype == LABEL_STATIC
522                                && layout->latextype == LATEX_ENVIRONMENT
523                                && !isFirstInSequence(pit, ownerParagraphs()))) {
524                         x += font_metrics::signedWidth(layout->leftmargin,
525                                                   labelfont);
526                 } else if (layout->labeltype != LABEL_TOP_ENVIRONMENT
527                            && layout->labeltype != LABEL_BIBLIO
528                            && layout->labeltype !=
529                            LABEL_CENTERED_TOP_ENVIRONMENT) {
530                         x += font_metrics::signedWidth(layout->labelindent,
531                                                   labelfont);
532                         x += font_metrics::width(layout->labelsep, labelfont);
533                         x += font_metrics::width(pit->getLabelstring(),
534                                             labelfont);
535                 }
536                 break;
537
538         case MARGIN_RIGHT_ADDRESS_BOX:
539         {
540                 // ok, a terrible hack. The left margin depends on the widest
541                 // row in this paragraph. Do not care about footnotes, they
542                 // are *NOT* allowed in the LaTeX realisation of this layout.
543
544                 // find the first row of this paragraph
545                 RowList::iterator rit = pit->rows.begin();
546                 RowList::iterator end = pit->rows.end();
547                 int minfill = rit->fill();
548                 for ( ; rit != end; ++rit)
549                         if (rit->fill() < minfill)
550                                 minfill = rit->fill();
551
552                 x += font_metrics::signedWidth(layout->leftmargin,
553                         tclass.defaultfont());
554                 x += minfill;
555         }
556         break;
557         }
558
559         if (workWidth() > 0 && !pit->params().leftIndent().zero()) {
560                 LyXLength const len = pit->params().leftIndent();
561                 int const tw = inset_owner ?
562                         inset_owner->latexTextWidth(bv()) : workWidth();
563                 x += len.inPixels(tw);
564         }
565
566         LyXAlignment align;
567
568         if (pit->params().align() == LYX_ALIGN_LAYOUT)
569                 align = layout->align;
570         else
571                 align = pit->params().align();
572
573         // set the correct parindent
574         if (row.pos() == 0) {
575                 if ((layout->labeltype == LABEL_NO_LABEL
576                      || layout->labeltype == LABEL_TOP_ENVIRONMENT
577                      || layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT
578                      || (layout->labeltype == LABEL_STATIC
579                          && layout->latextype == LATEX_ENVIRONMENT
580                          && !isFirstInSequence(pit, ownerParagraphs())))
581                     && align == LYX_ALIGN_BLOCK
582                     && !pit->params().noindent()
583                         // in tabulars and ert paragraphs are never indented!
584                         && (!pit->inInset() || !pit->inInset()->owner() ||
585                                 (pit->inInset()->owner()->lyxCode() != InsetOld::TABULAR_CODE &&
586                                  pit->inInset()->owner()->lyxCode() != InsetOld::ERT_CODE))
587                     && (pit->layout() != tclass.defaultLayout() ||
588                         bv()->buffer()->params.paragraph_separation ==
589                         BufferParams::PARSEP_INDENT)) {
590                         x += font_metrics::signedWidth(parindent,
591                                                   tclass.defaultfont());
592                 } else if (layout->labeltype == LABEL_BIBLIO) {
593                         // ale970405 Right width for bibitems
594                         x += bibitemMaxWidth(bv(), tclass.defaultfont());
595                 }
596         }
597
598         return x;
599 }
600
601
602 int LyXText::rightMargin(ParagraphList::iterator pit,
603         Buffer const & buf, Row const &) const
604 {
605         LyXTextClass const & tclass = buf.params.getLyXTextClass();
606         LyXLayout_ptr const & layout = pit->layout();
607
608         return PAPER_MARGIN
609                 + font_metrics::signedWidth(tclass.rightmargin(),
610                                        tclass.defaultfont());
611                 + font_metrics::signedWidth(layout->rightmargin,
612                                        tclass.defaultfont())
613                 * 4 / (pit->getDepth() + 4);
614 }
615
616
617 int LyXText::labelEnd(ParagraphList::iterator pit, Row const & row) const
618 {
619         if (pit->layout()->margintype == MARGIN_MANUAL) {
620                 Row tmprow = row;
621                 tmprow.pos(pit->size());
622                 // return the beginning of the body
623                 return leftMargin(pit, tmprow);
624         }
625
626         // LabelEnd is only needed if the layout
627         // fills a flushleft label.
628         return 0;
629 }
630
631
632 namespace {
633
634 // this needs special handling - only newlines count as a break point
635 pos_type addressBreakPoint(pos_type i, Paragraph const & par)
636 {
637         for (; i < par.size(); ++i) {
638                 if (par.isNewline(i))
639                         return i;
640         }
641
642         return par.size();
643 }
644
645 };
646
647
648 pos_type LyXText::rowBreakPoint(ParagraphList::iterator pit,
649         Row const & row) const
650 {
651         // maximum pixel width of a row.
652         int width = workWidth()
653                 - rightMargin(pit, *bv()->buffer(), row);
654
655         // inset->textWidth() returns -1 via workWidth(),
656         // but why ?
657         if (width < 0)
658                 return pit->size();
659
660         LyXLayout_ptr const & layout = pit->layout();
661
662         if (layout->margintype == MARGIN_RIGHT_ADDRESS_BOX)
663                 return addressBreakPoint(row.pos(), *pit);
664
665         pos_type const pos = row.pos();
666         pos_type const body_pos = pit->beginningOfBody();
667         pos_type const last = pit->size();
668         pos_type point = last;
669
670         if (pos == last)
671                 return last;
672
673         // Now we iterate through until we reach the right margin
674         // or the end of the par, then choose the possible break
675         // nearest that.
676
677         int const left = leftMargin(pit, row);
678         int x = left;
679
680         // pixel width since last breakpoint
681         int chunkwidth = 0;
682
683         pos_type i = pos;
684
685         // We re-use the font resolution for the entire font span when possible
686         LyXFont font = getFont(pit, i);
687         lyx::pos_type endPosOfFontSpan = pit->getEndPosOfFontSpan(i);
688
689         for (; i < last; ++i) {
690                 if (pit->isNewline(i)) {
691                         point = i;
692                         break;
693                 }
694
695                 char const c = pit->getChar(i);
696                 if (i > endPosOfFontSpan) {
697                         font = getFont(pit, i);
698                         endPosOfFontSpan = pit->getEndPosOfFontSpan(i);
699                 }
700
701                 int thiswidth;
702
703                 // add the auto-hfill from label end to the body
704                 if (body_pos && i == body_pos) {
705                         thiswidth = font_metrics::width(layout->labelsep, getLabelFont(pit));
706                         if (pit->isLineSeparator(i - 1))
707                                 thiswidth -= singleWidth(pit, i - 1);
708                         int left_margin = labelEnd(pit, row);
709                         if (thiswidth + x < left_margin)
710                                 thiswidth = left_margin - x;
711                         thiswidth += singleWidth(pit, i, c, font);
712                 } else {
713                         thiswidth = singleWidth(pit, i, c, font);
714                 }
715
716                 x += thiswidth;
717                 chunkwidth += thiswidth;
718
719                 InsetOld * in = pit->isInset(i) ? pit->getInset(i) : 0;
720
721                 // break before a character that will fall off
722                 // the right of the row
723                 if (x >= width) {
724                         // if no break before, break here
725                         if (point == last || chunkwidth >= (width - left)) {
726                                 if (pos < i)
727                                         point = i - 1;
728                                 else
729                                         point = i;
730                         }
731                         break;
732                 }
733
734                 if (!in || in->isChar()) {
735                         // some insets are line separators too
736                         if (pit->isLineSeparator(i)) {
737                                 point = i;
738                                 chunkwidth = 0;
739                         }
740                         continue;
741                 }
742
743                 continue;
744         }
745
746         if (point == last && x >= width) {
747                 // didn't find one, break at the point we reached the edge
748                 point = i;
749         } else if (i == last && x < width) {
750                 // found one, but we fell off the end of the par, so prefer
751                 // that.
752                 point = last;
753         }
754
755         // manual labels cannot be broken in LaTeX. But we
756         // want to make our on-screen rendering of footnotes
757         // etc. still break
758         if (body_pos && point < body_pos)
759                 point = body_pos - 1;
760
761         return point;
762 }
763
764
765 // returns the minimum space a row needs on the screen in pixel
766 int LyXText::fill(ParagraphList::iterator pit,
767         RowList::iterator row, int paper_width) const
768 {
769         if (paper_width < 0)
770                 return 0;
771
772         int w;
773         // get the pure distance
774         pos_type const last = lastPos(*pit, row);
775
776         LyXLayout_ptr const & layout = pit->layout();
777
778         // special handling of the right address boxes
779         if (layout->margintype == MARGIN_RIGHT_ADDRESS_BOX) {
780                 int const tmpfill = row->fill();
781                 row->fill(0); // the minfill in MarginLeft()
782                 w = leftMargin(pit, *row);
783                 row->fill(tmpfill);
784         } else
785                 w = leftMargin(pit, *row);
786
787         pos_type const body_pos = pit->beginningOfBody();
788         pos_type i = row->pos();
789
790         if (! pit->empty() && i <= last) {
791                 // We re-use the font resolution for the entire span when possible
792                 LyXFont font = getFont(pit, i);
793                 lyx::pos_type endPosOfFontSpan = pit->getEndPosOfFontSpan(i);
794                 while (i <= last) {
795                         if (body_pos > 0 && i == body_pos) {
796                                 w += font_metrics::width(layout->labelsep, getLabelFont(pit));
797                                 if (pit->isLineSeparator(i - 1))
798                                         w -= singleWidth(pit, i - 1);
799                                 int left_margin = labelEnd(pit, *row);
800                                 if (w < left_margin)
801                                         w = left_margin;
802                         }
803                         char const c = pit->getChar(i);
804                         if (IsPrintable(c) && i > endPosOfFontSpan) {
805                                 // We need to get the next font
806                                 font = getFont(pit, i);
807                                 endPosOfFontSpan = pit->getEndPosOfFontSpan(i);
808                         }
809                         w += singleWidth(pit, i, c, font);
810                         ++i;
811                 }
812         }
813         if (body_pos > 0 && body_pos > last) {
814                 w += font_metrics::width(layout->labelsep, getLabelFont(pit));
815                 if (last >= 0 && pit->isLineSeparator(last))
816                         w -= singleWidth(pit, last);
817                 int const left_margin = labelEnd(pit, *row);
818                 if (w < left_margin)
819                         w = left_margin;
820         }
821
822         int const fill = paper_width - w - rightMargin(pit, *bv()->buffer(), *row);
823
824         // If this case happens, it means that our calculation
825         // of the widths of the chars when we do rowBreakPoint()
826         // went wrong for some reason. Typically in list bodies.
827         // Things just about hobble on anyway, though you'll end
828         // up with a "fill_separator" less than zero, which corresponds
829         // to inter-word spacing being too small. Hopefully this problem
830         // will die when the label hacks die.
831         if (lyxerr.debugging() && fill < 0) {
832                 lyxerr[Debug::GUI] << "Eek, fill() was < 0: " << fill
833                         << " w " << w << " paper_width " << paper_width
834                         << " right margin " << rightMargin(pit, *bv()->buffer(), *row) << endl;
835         }
836         return fill;
837 }
838
839
840 // returns the minimum space a manual label needs on the screen in pixel
841 int LyXText::labelFill(ParagraphList::iterator pit, Row const & row) const
842 {
843         pos_type last = pit->beginningOfBody();
844
845         Assert(last > 0);
846
847         // -1 because a label ends either with a space that is in the label,
848         // or with the beginning of a footnote that is outside the label.
849         --last;
850
851         // a separator at this end does not count
852         if (pit->isLineSeparator(last))
853                 --last;
854
855         int w = 0;
856         pos_type i = row.pos();
857         while (i <= last) {
858                 w += singleWidth(pit, i);
859                 ++i;
860         }
861
862         int fill = 0;
863         string const & labwidstr = pit->params().labelWidthString();
864         if (!labwidstr.empty()) {
865                 LyXFont const labfont = getLabelFont(pit);
866                 int const labwidth = font_metrics::width(labwidstr, labfont);
867                 fill = max(labwidth - w, 0);
868         }
869
870         return fill;
871 }
872
873
874 LColor::color LyXText::backgroundColor() const
875 {
876         if (inset_owner)
877                 return inset_owner->backgroundColor();
878         else
879                 return LColor::background;
880 }
881
882
883 void LyXText::setHeightOfRow(ParagraphList::iterator pit, RowList::iterator rit)
884 {
885         // get the maximum ascent and the maximum descent
886         double layoutasc = 0;
887         double layoutdesc = 0;
888         double tmptop = 0;
889
890         // ok, let us initialize the maxasc and maxdesc value.
891         // Only the fontsize count. The other properties
892         // are taken from the layoutfont. Nicer on the screen :)
893         LyXLayout_ptr const & layout = pit->layout();
894
895         // as max get the first character of this row then it can increase but not
896         // decrease the height. Just some point to start with so we don't have to
897         // do the assignment below too often.
898         LyXFont font = getFont(pit, rit->pos());
899         LyXFont::FONT_SIZE const tmpsize = font.size();
900         font = getLayoutFont(pit);
901         LyXFont::FONT_SIZE const size = font.size();
902         font.setSize(tmpsize);
903
904         LyXFont labelfont = getLabelFont(pit);
905
906         double spacing_val = 1.0;
907         if (!pit->params().spacing().isDefault())
908                 spacing_val = pit->params().spacing().getValue();
909         else
910                 spacing_val = bv()->buffer()->params.spacing.getValue();
911         //lyxerr << "spacing_val = " << spacing_val << endl;
912
913         int maxasc  = int(font_metrics::maxAscent(font) *
914                           layout->spacing.getValue() * spacing_val);
915         int maxdesc = int(font_metrics::maxDescent(font) *
916                           layout->spacing.getValue() * spacing_val);
917
918         pos_type const pos_end = lastPos(*pit, rit);
919         int labeladdon = 0;
920         int maxwidth = 0;
921
922         if (!pit->empty()) {
923                 // We re-use the font resolution for the entire font span when possible
924                 LyXFont font = getFont(pit, rit->pos());
925                 lyx::pos_type endPosOfFontSpan = pit->getEndPosOfFontSpan(rit->pos());
926
927                 // Optimisation
928                 Paragraph const & par = *pit;
929
930                 // Check if any insets are larger
931                 for (pos_type pos = rit->pos(); pos <= pos_end; ++pos) {
932                         // Manual inlined optimised version of common case of
933                         // "maxwidth += singleWidth(pit, pos);"
934                         char const c = par.getChar(pos);
935
936                         if (IsPrintable(c)) {
937                                 if (pos > endPosOfFontSpan) {
938                                         // We need to get the next font
939                                         font = getFont(pit, pos);
940                                         endPosOfFontSpan = par.getEndPosOfFontSpan(pos);
941                                 }
942                                 if (! font.language()->RightToLeft()) {
943                                         maxwidth += font_metrics::width(c, font);
944                                 } else {
945                                         // Fall-back to normal case
946                                         maxwidth += singleWidth(pit, pos, c, font);
947                                         // And flush font cache
948                                         endPosOfFontSpan = 0;
949                                 }
950                         } else {
951                                 // Special handling of insets - are any larger?
952                                 if (par.isInset(pos)) {
953                                         InsetOld const * tmpinset = par.getInset(pos);
954                                         if (tmpinset) {
955                                                 maxwidth += tmpinset->width();
956                                                 maxasc = max(maxasc, tmpinset->ascent());
957                                                 maxdesc = max(maxdesc, tmpinset->descent());
958                                         }
959                                 } else {
960                                         // Fall-back to normal case
961                                         maxwidth += singleWidth(pit, pos, c, font);
962                                         // And flush font cache
963                                         endPosOfFontSpan = 0;
964                                 }
965                         }
966                 }
967         }
968
969         // Check if any custom fonts are larger (Asger)
970         // This is not completely correct, but we can live with the small,
971         // cosmetic error for now.
972         LyXFont::FONT_SIZE maxsize =
973                 pit->highestFontInRange(rit->pos(), pos_end, size);
974         if (maxsize > font.size()) {
975                 font.setSize(maxsize);
976                 maxasc = max(maxasc, font_metrics::maxAscent(font));
977                 maxdesc = max(maxdesc, font_metrics::maxDescent(font));
978         }
979
980         // This is nicer with box insets:
981         ++maxasc;
982         ++maxdesc;
983
984         rit->ascent_of_text(maxasc);
985
986         // is it a top line?
987         if (!rit->pos()) {
988
989                 // some parksips VERY EASY IMPLEMENTATION
990                 if (bv()->buffer()->params.paragraph_separation ==
991                         BufferParams::PARSEP_SKIP)
992                 {
993                         if (layout->isParagraph()
994                                 && pit->getDepth() == 0
995                                 && pit != ownerParagraphs().begin())
996                         {
997                                 maxasc += bv()->buffer()->params.getDefSkip().inPixels(*bv());
998                         } else if (pit != ownerParagraphs().begin() &&
999                                    boost::prior(pit)->layout()->isParagraph() &&
1000                                    boost::prior(pit)->getDepth() == 0)
1001                         {
1002                                 // is it right to use defskip here too? (AS)
1003                                 maxasc += bv()->buffer()->params.getDefSkip().inPixels(*bv());
1004                         }
1005                 }
1006
1007                 // the top margin
1008                 if (pit == ownerParagraphs().begin() && !isInInset())
1009                         maxasc += PAPER_MARGIN;
1010
1011                 // add the vertical spaces, that the user added
1012                 maxasc += getLengthMarkerHeight(*bv(), pit->params().spaceTop());
1013
1014                 // do not forget the DTP-lines!
1015                 // there height depends on the font of the nearest character
1016                 if (pit->params().lineTop())
1017
1018                         maxasc += 2 * font_metrics::ascent('x', getFont(pit, 0));
1019                 // and now the pagebreaks
1020                 if (pit->params().pagebreakTop())
1021                         maxasc += 3 * defaultRowHeight();
1022
1023                 if (pit->params().startOfAppendix())
1024                         maxasc += 3 * defaultRowHeight();
1025
1026                 // This is special code for the chapter, since the label of this
1027                 // layout is printed in an extra row
1028                 if (layout->labeltype == LABEL_COUNTER_CHAPTER
1029                         && bv()->buffer()->params.secnumdepth >= 0)
1030                 {
1031                         float spacing_val = 1.0;
1032                         if (!pit->params().spacing().isDefault()) {
1033                                 spacing_val = pit->params().spacing().getValue();
1034                         } else {
1035                                 spacing_val = bv()->buffer()->params.spacing.getValue();
1036                         }
1037
1038                         labeladdon = int(font_metrics::maxDescent(labelfont) *
1039                                          layout->spacing.getValue() *
1040                                          spacing_val)
1041                                 + int(font_metrics::maxAscent(labelfont) *
1042                                       layout->spacing.getValue() *
1043                                       spacing_val);
1044                 }
1045
1046                 // special code for the top label
1047                 if ((layout->labeltype == LABEL_TOP_ENVIRONMENT
1048                      || layout->labeltype == LABEL_BIBLIO
1049                      || layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT)
1050                     && isFirstInSequence(pit, ownerParagraphs())
1051                     && !pit->getLabelstring().empty())
1052                 {
1053                         float spacing_val = 1.0;
1054                         if (!pit->params().spacing().isDefault()) {
1055                                 spacing_val = pit->params().spacing().getValue();
1056                         } else {
1057                                 spacing_val = bv()->buffer()->params.spacing.getValue();
1058                         }
1059
1060                         labeladdon = int(
1061                                 (font_metrics::maxAscent(labelfont) +
1062                                  font_metrics::maxDescent(labelfont)) *
1063                                   layout->spacing.getValue() *
1064                                   spacing_val
1065                                 + layout->topsep * defaultRowHeight()
1066                                 + layout->labelbottomsep * defaultRowHeight());
1067                 }
1068
1069                 // And now the layout spaces, for example before and after
1070                 // a section, or between the items of a itemize or enumerate
1071                 // environment.
1072
1073                 if (!pit->params().pagebreakTop()) {
1074                         ParagraphList::iterator prev =
1075                                 depthHook(pit, ownerParagraphs(),
1076                                           pit->getDepth());
1077                         if (prev != pit && prev->layout() == layout &&
1078                                 prev->getDepth() == pit->getDepth() &&
1079                                 prev->getLabelWidthString() == pit->getLabelWidthString())
1080                         {
1081                                 layoutasc = (layout->itemsep * defaultRowHeight());
1082                         } else if (rit != firstRow()) {
1083                                 tmptop = layout->topsep;
1084
1085                                 //if (boost::prior(pit)->getDepth() >= pit->getDepth())
1086                                 //      tmptop -= getPar(previousRow(rit))->layout()->bottomsep;
1087
1088                                 if (tmptop > 0)
1089                                         layoutasc = (tmptop * defaultRowHeight());
1090                         } else if (pit->params().lineTop()) {
1091                                 tmptop = layout->topsep;
1092
1093                                 if (tmptop > 0)
1094                                         layoutasc = (tmptop * defaultRowHeight());
1095                         }
1096
1097                         prev = outerHook(pit, ownerParagraphs());
1098                         if (prev != ownerParagraphs().end())  {
1099                                 maxasc += int(prev->layout()->parsep * defaultRowHeight());
1100                         } else if (pit != ownerParagraphs().begin()) {
1101                                 ParagraphList::iterator prior_pit = boost::prior(pit);
1102                                 if (prior_pit->getDepth() != 0 ||
1103                                     prior_pit->layout() == layout) {
1104                                         maxasc += int(layout->parsep * defaultRowHeight());
1105                                 }
1106                         }
1107                 }
1108         }
1109
1110         // is it a bottom line?
1111         if (boost::next(rit) == pit->rows.end()) {
1112                 // the bottom margin
1113                 ParagraphList::iterator nextpit = boost::next(pit);
1114                 if (nextpit == ownerParagraphs().end() && !isInInset())
1115                         maxdesc += PAPER_MARGIN;
1116
1117                 // add the vertical spaces, that the user added
1118                 maxdesc += getLengthMarkerHeight(*bv(), pit->params().spaceBottom());
1119
1120                 // do not forget the DTP-lines!
1121                 // there height depends on the font of the nearest character
1122                 if (pit->params().lineBottom())
1123                         maxdesc += 2 * font_metrics::ascent('x',
1124                                         getFont(pit, max(pos_type(0), pit->size() - 1)));
1125
1126                 // and now the pagebreaks
1127                 if (pit->params().pagebreakBottom())
1128                         maxdesc += 3 * defaultRowHeight();
1129
1130                 // and now the layout spaces, for example before and after
1131                 // a section, or between the items of a itemize or enumerate
1132                 // environment
1133                 if (!pit->params().pagebreakBottom()
1134                     && nextpit != ownerParagraphs().end()) {
1135                         ParagraphList::iterator comparepit = pit;
1136                         float usual = 0;
1137                         float unusual = 0;
1138
1139                         if (comparepit->getDepth() > nextpit->getDepth()) {
1140                                 usual = (comparepit->layout()->bottomsep * defaultRowHeight());
1141                                 comparepit = depthHook(comparepit, ownerParagraphs(), nextpit->getDepth());
1142                                 if (comparepit->layout()!= nextpit->layout()
1143                                         || nextpit->getLabelWidthString() !=
1144                                         comparepit->getLabelWidthString())
1145                                 {
1146                                         unusual = (comparepit->layout()->bottomsep * defaultRowHeight());
1147                                 }
1148                                 if (unusual > usual)
1149                                         layoutdesc = unusual;
1150                                 else
1151                                         layoutdesc = usual;
1152                         } else if (comparepit->getDepth() ==  nextpit->getDepth()) {
1153
1154                                 if (comparepit->layout() != nextpit->layout()
1155                                         || nextpit->getLabelWidthString() !=
1156                                         comparepit->getLabelWidthString())
1157                                         layoutdesc = int(comparepit->layout()->bottomsep * defaultRowHeight());
1158                         }
1159                 }
1160         }
1161
1162         // incalculate the layout spaces
1163         maxasc += int(layoutasc * 2 / (2 + pit->getDepth()));
1164         maxdesc += int(layoutdesc * 2 / (2 + pit->getDepth()));
1165
1166         rit->height(maxasc + maxdesc + labeladdon);
1167         rit->baseline(maxasc + labeladdon);
1168         rit->top_of_text(rit->baseline() - font_metrics::maxAscent(font));
1169
1170         double x = 0;
1171         rit->width(int(maxwidth + x));
1172         if (inset_owner) {
1173                 width = max(0, workWidth());
1174                 RowList::iterator rit = firstRow();
1175                 RowList::iterator end = endRow();
1176                 ParagraphList::iterator it = ownerParagraphs().begin();
1177                 while (rit != end) {
1178                         if (rit->width() > width)
1179                                 width = rit->width();
1180                         nextRow(it, rit);
1181                 }
1182         }
1183 }
1184
1185
1186 void LyXText::breakParagraph(ParagraphList & paragraphs, char keep_layout)
1187 {
1188         // allow only if at start or end, or all previous is new text
1189         if (cursor.pos() && cursor.pos() != cursor.par()->size()
1190                 && cursor.par()->isChangeEdited(0, cursor.pos()))
1191                 return;
1192
1193         LyXTextClass const & tclass =
1194                 bv()->buffer()->params.getLyXTextClass();
1195         LyXLayout_ptr const & layout = cursor.par()->layout();
1196
1197         // this is only allowed, if the current paragraph is not empty or caption
1198         // and if it has not the keepempty flag active
1199         if (cursor.par()->empty() && !cursor.par()->allowEmpty()
1200            && layout->labeltype != LABEL_SENSITIVE)
1201                 return;
1202
1203         recordUndo(bv(), Undo::ATOMIC, cursor.par());
1204
1205         // Always break behind a space
1206         //
1207         // It is better to erase the space (Dekel)
1208         if (cursor.pos() < cursor.par()->size()
1209              && cursor.par()->isLineSeparator(cursor.pos()))
1210            cursor.par()->erase(cursor.pos());
1211
1212         // break the paragraph
1213         if (keep_layout)
1214                 keep_layout = 2;
1215         else
1216                 keep_layout = layout->isEnvironment();
1217
1218         // we need to set this before we insert the paragraph. IMO the
1219         // breakParagraph call should return a bool if it inserts the
1220         // paragraph before or behind and we should react on that one
1221         // but we can fix this in 1.3.0 (Jug 20020509)
1222         bool const isempty = (cursor.par()->allowEmpty() && cursor.par()->empty());
1223         ::breakParagraph(bv()->buffer()->params, paragraphs, cursor.par(),
1224                          cursor.pos(), keep_layout);
1225
1226 #warning Trouble Point! (Lgb)
1227         // When ::breakParagraph is called from within an inset we must
1228         // ensure that the correct ParagraphList is used. Today that is not
1229         // the case and the Buffer::paragraphs is used. Not good. (Lgb)
1230         ParagraphList::iterator next_par = boost::next(cursor.par());
1231
1232         // well this is the caption hack since one caption is really enough
1233         if (layout->labeltype == LABEL_SENSITIVE) {
1234                 if (!cursor.pos())
1235                         // set to standard-layout
1236                         cursor.par()->applyLayout(tclass.defaultLayout());
1237                 else
1238                         // set to standard-layout
1239                         next_par->applyLayout(tclass.defaultLayout());
1240         }
1241
1242         // if the cursor is at the beginning of a row without prior newline,
1243         // move one row up!
1244         // This touches only the screen-update. Otherwise we would may have
1245         // an empty row on the screen
1246         if (cursor.pos() && cursorRow()->pos() == cursor.pos()
1247             && !cursor.par()->isNewline(cursor.pos() - 1))
1248         {
1249                 cursorLeft(bv());
1250         }
1251
1252         while (!next_par->empty() && next_par->isNewline(0))
1253                 next_par->erase(0);
1254
1255         updateCounters();
1256         redoParagraph(cursor.par());
1257         redoParagraph(next_par);
1258
1259         // This check is necessary. Otherwise the new empty paragraph will
1260         // be deleted automatically. And it is more friendly for the user!
1261         if (cursor.pos() || isempty)
1262                 setCursor(next_par, 0);
1263         else
1264                 setCursor(cursor.par(), 0);
1265 }
1266
1267
1268 // convenience function
1269 void LyXText::redoParagraph()
1270 {
1271         clearSelection();
1272         redoParagraph(cursor.par());
1273         setCursorIntern(cursor.par(), cursor.pos());
1274 }
1275
1276
1277 // insert a character, moves all the following breaks in the
1278 // same Paragraph one to the right and make a rebreak
1279 void LyXText::insertChar(char c)
1280 {
1281         recordUndo(bv(), Undo::INSERT, cursor.par());
1282
1283         // When the free-spacing option is set for the current layout,
1284         // disable the double-space checking
1285
1286         bool const freeSpacing = cursor.par()->layout()->free_spacing ||
1287                 cursor.par()->isFreeSpacing();
1288
1289         if (lyxrc.auto_number) {
1290                 static string const number_operators = "+-/*";
1291                 static string const number_unary_operators = "+-";
1292                 static string const number_seperators = ".,:";
1293
1294                 if (current_font.number() == LyXFont::ON) {
1295                         if (!IsDigit(c) && !contains(number_operators, c) &&
1296                             !(contains(number_seperators, c) &&
1297                               cursor.pos() >= 1 &&
1298                               cursor.pos() < cursor.par()->size() &&
1299                               getFont(cursor.par(), cursor.pos()).number() == LyXFont::ON &&
1300                               getFont(cursor.par(), cursor.pos() - 1).number() == LyXFont::ON)
1301                            )
1302                                 number(bv()); // Set current_font.number to OFF
1303                 } else if (IsDigit(c) &&
1304                            real_current_font.isVisibleRightToLeft()) {
1305                         number(bv()); // Set current_font.number to ON
1306
1307                         if (cursor.pos() > 0) {
1308                                 char const c = cursor.par()->getChar(cursor.pos() - 1);
1309                                 if (contains(number_unary_operators, c) &&
1310                                     (cursor.pos() == 1 ||
1311                                      cursor.par()->isSeparator(cursor.pos() - 2) ||
1312                                      cursor.par()->isNewline(cursor.pos() - 2))
1313                                   ) {
1314                                         setCharFont(
1315                                                     cursor.par(),
1316                                                     cursor.pos() - 1,
1317                                                     current_font);
1318                                 } else if (contains(number_seperators, c) &&
1319                                            cursor.pos() >= 2 &&
1320                                            getFont(
1321                                                    cursor.par(),
1322                                                    cursor.pos() - 2).number() == LyXFont::ON) {
1323                                         setCharFont(
1324                                                     cursor.par(),
1325                                                     cursor.pos() - 1,
1326                                                     current_font);
1327                                 }
1328                         }
1329                 }
1330         }
1331
1332
1333         // First check, if there will be two blanks together or a blank at
1334         // the beginning of a paragraph.
1335         // I decided to handle blanks like normal characters, the main
1336         // difference are the special checks when calculating the row.fill
1337         // (blank does not count at the end of a row) and the check here
1338
1339         // The bug is triggered when we type in a description environment:
1340         // The current_font is not changed when we go from label to main text
1341         // and it should (along with realtmpfont) when we type the space.
1342         // CHECK There is a bug here! (Asger)
1343
1344         // store the current font.  This is because of the use of cursor
1345         // movements. The moving cursor would refresh the current font
1346         LyXFont realtmpfont = real_current_font;
1347         LyXFont rawtmpfont = current_font;
1348
1349         if (!freeSpacing && IsLineSeparatorChar(c)) {
1350                 if ((cursor.pos() > 0
1351                      && cursor.par()->isLineSeparator(cursor.pos() - 1))
1352                     || (cursor.pos() > 0
1353                         && cursor.par()->isNewline(cursor.pos() - 1))
1354                     || (cursor.pos() == 0)) {
1355                         static bool sent_space_message = false;
1356                         if (!sent_space_message) {
1357                                 if (cursor.pos() == 0)
1358                                         bv()->owner()->message(_("You cannot insert a space at the beginning of a paragraph. Please read the Tutorial."));
1359                                 else
1360                                         bv()->owner()->message(_("You cannot type two spaces this way. Please read the Tutorial."));
1361                                 sent_space_message = true;
1362                         }
1363                         charInserted();
1364                         return;
1365                 }
1366         }
1367
1368         // Here case LyXText::InsertInset already inserted the character
1369         if (c != Paragraph::META_INSET)
1370                 cursor.par()->insertChar(cursor.pos(), c);
1371
1372         setCharFont(cursor.par(), cursor.pos(), rawtmpfont);
1373
1374         current_font = rawtmpfont;
1375         real_current_font = realtmpfont;
1376         redoParagraph(cursor.par());
1377         setCursor(cursor.par(), cursor.pos() + 1, false, cursor.boundary());
1378
1379         charInserted();
1380 }
1381
1382
1383 void LyXText::charInserted()
1384 {
1385         // Here we could call finishUndo for every 20 characters inserted.
1386         // This is from my experience how emacs does it. (Lgb)
1387         static unsigned int counter;
1388         if (counter < 20) {
1389                 ++counter;
1390         } else {
1391                 finishUndo();
1392                 counter = 0;
1393         }
1394 }
1395
1396
1397 void LyXText::prepareToPrint(ParagraphList::iterator pit,
1398            RowList::iterator const rit) const
1399 {
1400         double w = rit->fill();
1401         double fill_hfill = 0;
1402         double fill_label_hfill = 0;
1403         double fill_separator = 0;
1404         double x = 0;
1405
1406         bool const is_rtl =
1407                 pit->isRightToLeftPar(bv()->buffer()->params);
1408         if (is_rtl)
1409                 x = workWidth() > 0 ? rightMargin(pit, *bv()->buffer(), *rit) : 0;
1410         else
1411                 x = workWidth() > 0 ? leftMargin(pit, *rit) : 0;
1412
1413         // is there a manual margin with a manual label
1414         LyXLayout_ptr const & layout = pit->layout();
1415
1416         if (layout->margintype == MARGIN_MANUAL
1417             && layout->labeltype == LABEL_MANUAL) {
1418                 /// We might have real hfills in the label part
1419                 int nlh = numberOfLabelHfills(*pit, rit);
1420
1421                 // A manual label par (e.g. List) has an auto-hfill
1422                 // between the label text and the body of the
1423                 // paragraph too.
1424                 // But we don't want to do this auto hfill if the par
1425                 // is empty.
1426                 if (!pit->empty())
1427                         ++nlh;
1428
1429                 if (nlh && !pit->getLabelWidthString().empty()) {
1430                         fill_label_hfill = labelFill(pit, *rit) / double(nlh);
1431                 }
1432         }
1433
1434         // are there any hfills in the row?
1435         int const nh = numberOfHfills(*pit, rit);
1436
1437         if (nh) {
1438                 if (w > 0)
1439                         fill_hfill = w / nh;
1440         // we don't have to look at the alignment if it is ALIGN_LEFT and
1441         // if the row is already larger then the permitted width as then
1442         // we force the LEFT_ALIGN'edness!
1443         } else if (int(rit->width()) < workWidth()) {
1444                 // is it block, flushleft or flushright?
1445                 // set x how you need it
1446                 int align;
1447                 if (pit->params().align() == LYX_ALIGN_LAYOUT) {
1448                         align = layout->align;
1449                 } else {
1450                         align = pit->params().align();
1451                 }
1452                 InsetOld * inset = 0;
1453                 // ERT insets should always be LEFT ALIGNED on screen
1454                 inset = pit->inInset();
1455                 if (inset && inset->owner() &&
1456                         inset->owner()->lyxCode() == InsetOld::ERT_CODE)
1457                 {
1458                         align = LYX_ALIGN_LEFT;
1459                 }
1460
1461                 switch (align) {
1462             case LYX_ALIGN_BLOCK:
1463                 {
1464                         int const ns = numberOfSeparators(*pit, rit);
1465                         RowList::iterator next_row = boost::next(rit);
1466                         if (ns
1467                                 && next_row != pit->rows.end()
1468                                 && !pit->isNewline(next_row->pos() - 1)
1469                                 ) {
1470                                         fill_separator = w / ns;
1471                         } else if (is_rtl) {
1472                                 x += w;
1473                         }
1474                         break;
1475             }
1476             case LYX_ALIGN_RIGHT:
1477                         x += w;
1478                         break;
1479             case LYX_ALIGN_CENTER:
1480                         x += w / 2;
1481                         break;
1482                 }
1483         }
1484
1485         computeBidiTables(pit, *bv()->buffer(), rit);
1486         if (is_rtl) {
1487                 pos_type body_pos = pit->beginningOfBody();
1488                 pos_type last = lastPos(*pit, rit);
1489
1490                 if (body_pos > 0 &&
1491                                 (body_pos - 1 > last ||
1492                                  !pit->isLineSeparator(body_pos - 1))) {
1493                         x += font_metrics::width(layout->labelsep, getLabelFont(pit));
1494                         if (body_pos - 1 <= last)
1495                                 x += fill_label_hfill;
1496                 }
1497         }
1498
1499         rit->fill_hfill(fill_hfill);
1500         rit->fill_label_hfill(fill_label_hfill);
1501         rit->fill_separator(fill_separator);
1502         rit->x(x);
1503 }
1504
1505
1506 // important for the screen
1507
1508
1509 // the cursor set functions have a special mechanism. When they
1510 // realize, that you left an empty paragraph, they will delete it.
1511 // They also delete the corresponding row
1512
1513 void LyXText::cursorRightOneWord()
1514 {
1515         ::cursorRightOneWord(cursor, ownerParagraphs());
1516         setCursor(cursor.par(), cursor.pos());
1517 }
1518
1519
1520 // Skip initial whitespace at end of word and move cursor to *start*
1521 // of prior word, not to end of next prior word.
1522 void LyXText::cursorLeftOneWord()
1523 {
1524         LyXCursor tmpcursor = cursor;
1525         ::cursorLeftOneWord(tmpcursor, ownerParagraphs());
1526         setCursor(tmpcursor.par(), tmpcursor.pos());
1527 }
1528
1529
1530 void LyXText::selectWord(word_location loc)
1531 {
1532         LyXCursor from = cursor;
1533         LyXCursor to;
1534         ::getWord(from, to, loc, ownerParagraphs());
1535         if (cursor != from)
1536                 setCursor(from.par(), from.pos());
1537         if (to == from)
1538                 return;
1539         selection.cursor = cursor;
1540         setCursor(to.par(), to.pos());
1541         setSelection();
1542 }
1543
1544
1545 // Select the word currently under the cursor when no
1546 // selection is currently set
1547 bool LyXText::selectWordWhenUnderCursor(word_location loc)
1548 {
1549         if (!selection.set()) {
1550                 selectWord(loc);
1551                 return selection.set();
1552         }
1553         return false;
1554 }
1555
1556
1557 void LyXText::acceptChange()
1558 {
1559         if (!selection.set() && cursor.par()->size())
1560                 return;
1561
1562         if (selection.start.par() == selection.end.par()) {
1563                 LyXCursor & startc = selection.start;
1564                 LyXCursor & endc = selection.end;
1565                 recordUndo(bv(), Undo::INSERT, startc.par());
1566                 startc.par()->acceptChange(startc.pos(), endc.pos());
1567                 finishUndo();
1568                 clearSelection();
1569                 redoParagraph(startc.par());
1570                 setCursorIntern(startc.par(), 0);
1571         }
1572 #warning handle multi par selection
1573 }
1574
1575
1576 void LyXText::rejectChange()
1577 {
1578         if (!selection.set() && cursor.par()->size())
1579                 return;
1580
1581         if (selection.start.par() == selection.end.par()) {
1582                 LyXCursor & startc = selection.start;
1583                 LyXCursor & endc = selection.end;
1584                 recordUndo(bv(), Undo::INSERT, startc.par());
1585                 startc.par()->rejectChange(startc.pos(), endc.pos());
1586                 finishUndo();
1587                 clearSelection();
1588                 redoParagraph(startc.par());
1589                 setCursorIntern(startc.par(), 0);
1590         }
1591 #warning handle multi par selection
1592 }
1593
1594
1595 // This function is only used by the spellchecker for NextWord().
1596 // It doesn't handle LYX_ACCENTs and probably never will.
1597 WordLangTuple const
1598 LyXText::selectNextWordToSpellcheck(float & value)
1599 {
1600         if (the_locking_inset) {
1601                 WordLangTuple word = the_locking_inset->selectNextWordToSpellcheck(bv(), value);
1602                 if (!word.word().empty()) {
1603                         value += float(cursor.y());
1604                         value /= float(height);
1605                         return word;
1606                 }
1607                 // we have to go on checking so move cursor to the next char
1608                 if (cursor.pos() == cursor.par()->size()) {
1609                         if (boost::next(cursor.par()) == ownerParagraphs().end())
1610                                 return word;
1611                         cursor.par(boost::next(cursor.par()));
1612                         cursor.pos(0);
1613                 } else
1614                         cursor.pos(cursor.pos() + 1);
1615         }
1616         ParagraphList::iterator tmppit = cursor.par();
1617
1618         // If this is not the very first word, skip rest of
1619         // current word because we are probably in the middle
1620         // of a word if there is text here.
1621         if (cursor.pos() || cursor.par() != ownerParagraphs().begin()) {
1622                 while (cursor.pos() < cursor.par()->size()
1623                        && cursor.par()->isLetter(cursor.pos()))
1624                         cursor.pos(cursor.pos() + 1);
1625         }
1626
1627         // Now, skip until we have real text (will jump paragraphs)
1628         while (true) {
1629                 ParagraphList::iterator cpit = cursor.par();
1630                 pos_type const cpos(cursor.pos());
1631
1632                 if (cpos == cpit->size()) {
1633                         if (boost::next(cpit) != ownerParagraphs().end()) {
1634                                 cursor.par(boost::next(cpit));
1635                                 cursor.pos(0);
1636                                 continue;
1637                         }
1638                         break;
1639                 }
1640
1641                 bool const is_good_inset = cpit->isInset(cpos)
1642                         && cpit->getInset(cpos)->allowSpellcheck();
1643
1644                 if (!isDeletedText(*cpit, cpos)
1645                     && (is_good_inset || cpit->isLetter(cpos)))
1646                         break;
1647
1648                 cursor.pos(cpos + 1);
1649         }
1650
1651         // now check if we hit an inset so it has to be a inset containing text!
1652         if (cursor.pos() < cursor.par()->size() &&
1653             cursor.par()->isInset(cursor.pos())) {
1654                 // lock the inset!
1655                 FuncRequest cmd(bv(), LFUN_INSET_EDIT, "left");
1656                 cursor.par()->getInset(cursor.pos())->localDispatch(cmd);
1657                 // now call us again to do the above trick
1658                 // but obviously we have to start from down below ;)
1659                 return bv()->text->selectNextWordToSpellcheck(value);
1660         }
1661
1662         // Update the value if we changed paragraphs
1663         if (cursor.par() != tmppit) {
1664                 setCursor(cursor.par(), cursor.pos());
1665                 value = float(cursor.y())/float(height);
1666         }
1667
1668         // Start the selection from here
1669         selection.cursor = cursor;
1670
1671         string lang_code = getFont(cursor.par(), cursor.pos()).language()->code();
1672         // and find the end of the word (insets like optional hyphens
1673         // and ligature break are part of a word)
1674         while (cursor.pos() < cursor.par()->size()
1675                && cursor.par()->isLetter(cursor.pos())
1676                && !isDeletedText(*cursor.par(), cursor.pos()))
1677                 cursor.pos(cursor.pos() + 1);
1678
1679         // Finally, we copy the word to a string and return it
1680         string str;
1681         if (selection.cursor.pos() < cursor.pos()) {
1682                 pos_type i;
1683                 for (i = selection.cursor.pos(); i < cursor.pos(); ++i) {
1684                         if (!cursor.par()->isInset(i))
1685                                 str += cursor.par()->getChar(i);
1686                 }
1687         }
1688         return WordLangTuple(str, lang_code);
1689 }
1690
1691
1692 // This one is also only for the spellchecker
1693 void LyXText::selectSelectedWord()
1694 {
1695         if (the_locking_inset) {
1696                 the_locking_inset->selectSelectedWord(bv());
1697                 return;
1698         }
1699         // move cursor to the beginning
1700         setCursor(selection.cursor.par(), selection.cursor.pos());
1701
1702         // set the sel cursor
1703         selection.cursor = cursor;
1704
1705         // now find the end of the word
1706         while (cursor.pos() < cursor.par()->size()
1707                && cursor.par()->isLetter(cursor.pos()))
1708                 cursor.pos(cursor.pos() + 1);
1709
1710         setCursor(cursor.par(), cursor.pos());
1711
1712         // finally set the selection
1713         setSelection();
1714 }
1715
1716
1717 // Delete from cursor up to the end of the current or next word.
1718 void LyXText::deleteWordForward()
1719 {
1720         if (cursor.par()->empty())
1721                 cursorRight(bv());
1722         else {
1723                 LyXCursor tmpcursor = cursor;
1724                 selection.set(true); // to avoid deletion
1725                 cursorRightOneWord();
1726                 setCursor(tmpcursor, tmpcursor.par(), tmpcursor.pos());
1727                 selection.cursor = cursor;
1728                 cursor = tmpcursor;
1729                 setSelection();
1730
1731                 // Great, CutSelection() gets rid of multiple spaces.
1732                 cutSelection(true, false);
1733         }
1734 }
1735
1736
1737 // Delete from cursor to start of current or prior word.
1738 void LyXText::deleteWordBackward()
1739 {
1740         if (cursor.par()->empty())
1741                 cursorLeft(bv());
1742         else {
1743                 LyXCursor tmpcursor = cursor;
1744                 selection.set(true); // to avoid deletion
1745                 cursorLeftOneWord();
1746                 setCursor(tmpcursor, tmpcursor.par(), tmpcursor.pos());
1747                 selection.cursor = cursor;
1748                 cursor = tmpcursor;
1749                 setSelection();
1750                 cutSelection(true, false);
1751         }
1752 }
1753
1754
1755 // Kill to end of line.
1756 void LyXText::deleteLineForward()
1757 {
1758         if (cursor.par()->empty())
1759                 // Paragraph is empty, so we just go to the right
1760                 cursorRight(bv());
1761         else {
1762                 LyXCursor tmpcursor = cursor;
1763                 // We can't store the row over a regular setCursor
1764                 // so we set it to 0 and reset it afterwards.
1765                 selection.set(true); // to avoid deletion
1766                 cursorEnd();
1767                 setCursor(tmpcursor, tmpcursor.par(), tmpcursor.pos());
1768                 selection.cursor = cursor;
1769                 cursor = tmpcursor;
1770                 setSelection();
1771                 // What is this test for ??? (JMarc)
1772                 if (!selection.set()) {
1773                         deleteWordForward();
1774                 } else {
1775                         cutSelection(true, false);
1776                 }
1777         }
1778 }
1779
1780
1781 void LyXText::changeCase(LyXText::TextCase action)
1782 {
1783         LyXCursor from;
1784         LyXCursor to;
1785
1786         if (selection.set()) {
1787                 from = selection.start;
1788                 to = selection.end;
1789         } else {
1790                 from = cursor;
1791                 ::getWord(from, to, lyx::PARTIAL_WORD, ownerParagraphs());
1792                 setCursor(to.par(), to.pos() + 1);
1793         }
1794
1795         recordUndo(bv(), Undo::ATOMIC, from.par(), to.par());
1796
1797         pos_type pos = from.pos();
1798         ParagraphList::iterator pit = from.par();
1799
1800         while (pit != ownerParagraphs().end() &&
1801                (pos != to.pos() || pit != to.par())) {
1802                 if (pos == pit->size()) {
1803                         ++pit;
1804                         pos = 0;
1805                         continue;
1806                 }
1807                 unsigned char c = pit->getChar(pos);
1808                 if (!IsInsetChar(c)) {
1809                         switch (action) {
1810                         case text_lowercase:
1811                                 c = lowercase(c);
1812                                 break;
1813                         case text_capitalization:
1814                                 c = uppercase(c);
1815                                 action = text_lowercase;
1816                                 break;
1817                         case text_uppercase:
1818                                 c = uppercase(c);
1819                                 break;
1820                         }
1821                 }
1822 #warning changes
1823                 pit->setChar(pos, c);
1824                 ++pos;
1825         }
1826 }
1827
1828
1829 void LyXText::Delete()
1830 {
1831         // this is a very easy implementation
1832
1833         LyXCursor old_cursor = cursor;
1834         int const old_cur_par_id = old_cursor.par()->id();
1835         int const old_cur_par_prev_id =
1836                 (old_cursor.par() != ownerParagraphs().begin() ?
1837                  boost::prior(old_cursor.par())->id() : -1);
1838
1839         // just move to the right
1840         cursorRight(bv());
1841
1842         // CHECK Look at the comment here.
1843         // This check is not very good...
1844         // The cursorRightIntern calls DeleteEmptyParagrapgMechanism
1845         // and that can very well delete the par or par->previous in
1846         // old_cursor. Will a solution where we compare paragraph id's
1847         //work better?
1848         if ((cursor.par() != ownerParagraphs().begin() ? boost::prior(cursor.par())->id() : -1)
1849             == old_cur_par_prev_id
1850             && cursor.par()->id() != old_cur_par_id) {
1851                 // delete-empty-paragraph-mechanism has done it
1852                 return;
1853         }
1854
1855         // if you had success make a backspace
1856         if (old_cursor.par() != cursor.par() || old_cursor.pos() != cursor.pos()) {
1857                 LyXCursor tmpcursor = cursor;
1858                 // to make sure undo gets the right cursor position
1859                 cursor = old_cursor;
1860                 recordUndo(bv(), Undo::DELETE, cursor.par());
1861                 cursor = tmpcursor;
1862                 backspace();
1863         }
1864 }
1865
1866
1867 void LyXText::backspace()
1868 {
1869         // Get the font that is used to calculate the baselineskip
1870         pos_type lastpos = cursor.par()->size();
1871
1872         if (cursor.pos() == 0) {
1873                 // The cursor is at the beginning of a paragraph,
1874                 // so the the backspace will collapse two paragraphs into one.
1875
1876                 // but it's not allowed unless it's new
1877                 if (cursor.par()->isChangeEdited(0, cursor.par()->size()))
1878                         return;
1879
1880                 // we may paste some paragraphs
1881
1882                 // is it an empty paragraph?
1883
1884                 if (lastpos == 0
1885                      || (lastpos == 1 && cursor.par()->isSeparator(0))) {
1886                         // This is an empty paragraph and we delete it just
1887                         // by moving the cursor one step
1888                         // left and let the DeleteEmptyParagraphMechanism
1889                         // handle the actual deletion of the paragraph.
1890
1891                         if (cursor.par() != ownerParagraphs().begin()) {
1892                                 ParagraphList::iterator tmppit = boost::prior(cursor.par());
1893                                 if (cursor.par()->layout() == tmppit->layout()
1894                                     && cursor.par()->getAlign() == tmppit->getAlign()) {
1895                                         // Inherit bottom DTD from the paragraph below.
1896                                         // (the one we are deleting)
1897                                         tmppit->params().lineBottom(cursor.par()->params().lineBottom());
1898                                         tmppit->params().spaceBottom(cursor.par()->params().spaceBottom());
1899                                         tmppit->params().pagebreakBottom(cursor.par()->params().pagebreakBottom());
1900                                 }
1901
1902                                 cursorLeft(bv());
1903
1904                                 // the layout things can change the height of a row !
1905                                 redoParagraph();
1906                                 return;
1907                         }
1908                 }
1909
1910                 if (cursor.par() != ownerParagraphs().begin()) {
1911                         recordUndo(bv(), Undo::DELETE,
1912                                 boost::prior(cursor.par()),
1913                                 cursor.par());
1914                 }
1915
1916                 ParagraphList::iterator tmppit = cursor.par();
1917                 // We used to do cursorLeftIntern() here, but it is
1918                 // not a good idea since it triggers the auto-delete
1919                 // mechanism. So we do a cursorLeftIntern()-lite,
1920                 // without the dreaded mechanism. (JMarc)
1921                 if (cursor.par() != ownerParagraphs().begin()) {
1922                         // steps into the above paragraph.
1923                         setCursorIntern(boost::prior(cursor.par()),
1924                                         boost::prior(cursor.par())->size(),
1925                                         false);
1926                 }
1927
1928                 // Pasting is not allowed, if the paragraphs have different
1929                 // layout. I think it is a real bug of all other
1930                 // word processors to allow it. It confuses the user.
1931                 // Correction: Pasting is always allowed with standard-layout
1932                 LyXTextClass const & tclass =
1933                         bv()->buffer()->params.getLyXTextClass();
1934
1935                 if (cursor.par() != tmppit
1936                     && (cursor.par()->layout() == tmppit->layout()
1937                         || tmppit->layout() == tclass.defaultLayout())
1938                     && cursor.par()->getAlign() == tmppit->getAlign()) {
1939                         mergeParagraph(bv()->buffer()->params,
1940                                 bv()->buffer()->paragraphs, cursor.par());
1941
1942                         if (cursor.pos() && cursor.par()->isSeparator(cursor.pos() - 1))
1943                                 cursor.pos(cursor.pos() - 1);
1944
1945                         // the row may have changed, block, hfills etc.
1946                         updateCounters();
1947                         setCursor(cursor.par(), cursor.pos(), false);
1948                 }
1949         } else {
1950                 // this is the code for a normal backspace, not pasting
1951                 // any paragraphs
1952                 recordUndo(bv(), Undo::DELETE, cursor.par());
1953                 // We used to do cursorLeftIntern() here, but it is
1954                 // not a good idea since it triggers the auto-delete
1955                 // mechanism. So we do a cursorLeftIntern()-lite,
1956                 // without the dreaded mechanism. (JMarc)
1957                 setCursorIntern(cursor.par(), cursor.pos() - 1,
1958                                 false, cursor.boundary());
1959                 cursor.par()->erase(cursor.pos());
1960         }
1961
1962         lastpos = cursor.par()->size();
1963         if (cursor.pos() == lastpos)
1964                 setCurrentFont();
1965
1966         redoParagraph();
1967         setCursor(cursor.par(), cursor.pos(), false, !cursor.boundary());
1968 }
1969
1970
1971 RowList::iterator LyXText::cursorRow() const
1972 {
1973         return getRow(cursor.par(), cursor.pos());
1974 }
1975
1976
1977 RowList::iterator LyXText::getRow(LyXCursor const & cur) const
1978 {
1979         return getRow(cur.par(), cur.pos());
1980 }
1981
1982
1983 RowList::iterator
1984 LyXText::getRow(ParagraphList::iterator pit, pos_type pos) const
1985 {
1986         RowList::iterator rit = boost::prior(pit->rows.end());
1987         RowList::iterator const begin = pit->rows.begin();
1988
1989         while (rit != begin && rit->pos() > pos)
1990                 --rit;
1991
1992         return rit;
1993 }
1994
1995
1996 // returns pointer to some fancy row 'below' specified row
1997 RowList::iterator LyXText::cursorIRow() const
1998 {
1999         return getRow(cursor.par(), cursor.pos());
2000 }
2001
2002
2003 RowList::iterator LyXText::getRowNearY(int y,
2004         ParagraphList::iterator & pit) const
2005 {
2006         //lyxerr << "getRowNearY: y " << y << endl;
2007
2008         pit = boost::prior(ownerParagraphs().end());
2009
2010         RowList::iterator rit = lastRow();
2011         RowList::iterator rbegin = firstRow();
2012
2013         while (rit != rbegin && static_cast<int>(rit->y()) > y)
2014                 previousRow(pit, rit);
2015
2016         return rit;
2017 }
2018
2019
2020 int LyXText::getDepth() const
2021 {
2022         return cursor.par()->getDepth();
2023 }
2024
2025
2026 RowList::iterator LyXText::firstRow() const
2027 {
2028         return ownerParagraphs().front().rows.begin();
2029 }
2030
2031
2032 RowList::iterator LyXText::lastRow() const
2033 {
2034         return boost::prior(endRow());
2035 }
2036
2037
2038 RowList::iterator LyXText::endRow() const
2039 {
2040         return ownerParagraphs().back().rows.end();
2041 }
2042
2043
2044 void LyXText::nextRow(ParagraphList::iterator & pit,
2045         RowList::iterator & rit) const
2046 {
2047         ++rit;
2048         if (rit == pit->rows.end()) {
2049                 ++pit;
2050                 if (pit == ownerParagraphs().end())
2051                         --pit;
2052                 else
2053                         rit = pit->rows.begin();
2054         }
2055 }
2056
2057
2058 void LyXText::previousRow(ParagraphList::iterator & pit,
2059         RowList::iterator & rit) const
2060 {
2061         if (rit != pit->rows.begin())
2062                 --rit;
2063         else {
2064                 Assert(pit != ownerParagraphs().begin());
2065                 --pit;
2066                 rit = boost::prior(pit->rows.end());
2067         }
2068 }
2069
2070
2071 bool LyXText::noRows() const
2072 {
2073         return ownerParagraphs().begin()->rows.empty();
2074 }