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