]> git.lyx.org Git - lyx.git/blob - src/text.C
small general cleanup
[lyx.git] / src / text.C
1 /* This file is part of
2  * ======================================================
3  *
4  *           LyX, The Document Processor
5  *
6  *           Copyright 1995 Matthias Ettrich
7  *           Copyright 1995-2001 The LyX Team.
8  *
9  * ====================================================== */
10
11 #include <config.h>
12
13 #include "lyxtext.h"
14 #include "lyxrow.h"
15 #include "paragraph.h"
16 #include "gettext.h"
17 #include "bufferparams.h"
18 #include "buffer.h"
19 #include "debug.h"
20 #include "intl.h"
21 #include "lyxrc.h"
22 #include "encoding.h"
23 #include "frontends/LyXView.h"
24 #include "frontends/Painter.h"
25 #include "frontends/font_metrics.h"
26 #include "frontends/screen.h"
27 #include "frontends/WorkArea.h"
28 #include "bufferview_funcs.h"
29 #include "BufferView.h"
30 #include "language.h"
31 #include "ParagraphParameters.h"
32 #include "undo_funcs.h"
33 #include "text_funcs.h"
34 #include "WordLangTuple.h"
35 #include "paragraph_funcs.h"
36 #include "rowpainter.h"
37 #include "lyxrow_funcs.h"
38 #include "metricsinfo.h"
39
40 #include "insets/insettext.h"
41
42 #include "support/textutils.h"
43 #include "support/LAssert.h"
44 #include "support/lstrings.h"
45
46 #include <algorithm>
47
48 using namespace lyx::support;
49
50 using std::max;
51 using std::min;
52 using std::endl;
53 using std::pair;
54
55 using lyx::pos_type;
56 using lyx::word_location;
57
58 using namespace bv_funcs;
59
60 /// top, right, bottom pixel margin
61 extern int const PAPER_MARGIN = 20;
62 /// margin for changebar
63 extern int const CHANGEBAR_MARGIN = 10;
64 /// left margin
65 extern int const LEFT_MARGIN = PAPER_MARGIN + CHANGEBAR_MARGIN;
66
67 extern 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         RowList::iterator rit = rows().begin();
87         RowList::iterator rend = rows().end();
88         for (int y = 0; rit != rend ; ++rit) {
89                 rit->y(y);
90                 y += rit->height();
91         }
92 }
93
94
95 int LyXText::top_y() const
96 {
97         if (anchor_row_ == rowlist_.end())
98                 return 0;
99
100         return anchor_row_->y() + anchor_row_offset_;
101 }
102
103
104 void LyXText::top_y(int newy)
105 {
106         if (rows().empty())
107                 return;
108
109         if (isInInset()) {
110                 anchor_row_ = rows().begin();
111                 anchor_row_offset_ = newy;
112                 return;
113         }
114
115         lyxerr[Debug::GUI] << "setting top y = " << newy << endl;
116
117         int y = newy;
118         RowList::iterator rit = getRowNearY(y);
119
120         if (rit == anchor_row_ && anchor_row_offset_ == newy - y) {
121                 lyxerr[Debug::GUI] << "top_y to same value, skipping update" << endl;
122                 return;
123         }
124
125         anchor_row_ = rit;
126         anchor_row_offset_ = newy - y;
127         lyxerr[Debug::GUI] << "changing reference to row: " << &*anchor_row_
128                << " offset: " << anchor_row_offset_ << endl;
129         postPaint();
130 }
131
132
133 void LyXText::anchor_row(RowList::iterator rit)
134 {
135         int old_y = top_y();
136         anchor_row_offset_ = 0;
137         anchor_row_ = rit;
138         anchor_row_offset_ = old_y - top_y();
139         lyxerr[Debug::GUI] << "anchor_row(): changing reference to row: "
140                            << &*anchor_row_ << " offset: "
141                            << anchor_row_offset_ << endl;
142 }
143
144
145 int LyXText::workWidth() const
146 {
147         return inset_owner ? inset_owner->textWidth() : bv()->workWidth();
148 }
149
150
151 int LyXText::workWidth(Inset const * inset) const
152 {
153         ParagraphList::iterator par = std::find(ownerParagraphs().begin(),
154                                                 ownerParagraphs().end(),
155                                                 *inset->parOwner());
156         if (par == ownerParagraphs().end()) {
157                 lyxerr[Debug::GUI] << "LyXText::workWidth: unexpected\n";
158                 return -1;
159         }
160
161         pos_type pos = par->getPositionOfInset(inset);
162         Assert(pos != -1);
163
164         LyXLayout_ptr const & layout = par->layout();
165
166         if (layout->margintype != MARGIN_RIGHT_ADDRESS_BOX) {
167                 // Optimization here: in most cases, the real row is
168                 // not needed, but only the par/pos values. So we just
169                 // construct a dummy row for leftMargin. (JMarc)
170                 Row dummyrow;
171                 dummyrow.par(par);
172                 dummyrow.pos(pos);
173                 return workWidth() - leftMargin(dummyrow);
174         } else {
175                 int dummy_y;
176                 RowList::iterator row = getRow(par, pos, dummy_y);
177                 RowList::iterator frow = row;
178                 RowList::iterator beg = rowlist_.begin();
179
180                 while (frow != beg && frow->par() == boost::prior(frow)->par())
181                         --frow;
182
183                 // FIXME: I don't understand this code - jbl
184
185                 unsigned int maxw = 0;
186                 while (!isParEnd(*this, frow)) {
187                         if ((frow != row) && (maxw < frow->width()))
188                                 maxw = frow->width();
189                         ++frow;
190                 }
191                 if (maxw)
192                         return maxw;
193
194         }
195         return workWidth();
196 }
197
198
199 int LyXText::getRealCursorX() const
200 {
201         int x = cursor.x();
202         if (the_locking_inset && (the_locking_inset->getLyXText(bv())!= this))
203                 x = the_locking_inset->getLyXText(bv())->getRealCursorX();
204         return x;
205 }
206
207
208 #warning FIXME  This function seems to belong outside of LyxText.
209 unsigned char LyXText::transformChar(unsigned char c, Paragraph const & par,
210                                      pos_type pos) const
211 {
212         if (!Encodings::is_arabic(c))
213                 if (lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 && IsDigit(c))
214                         return c + (0xb0 - '0');
215                 else
216                         return c;
217
218         unsigned char const prev_char = pos > 0 ? par.getChar(pos - 1) : ' ';
219         unsigned char next_char = ' ';
220
221         pos_type const par_size = par.size();
222
223         for (pos_type i = pos + 1; i < par_size; ++i) {
224                 unsigned char const par_char = par.getChar(i);
225                 if (!Encodings::IsComposeChar_arabic(par_char)) {
226                         next_char = par_char;
227                         break;
228                 }
229         }
230
231         if (Encodings::is_arabic(next_char)) {
232                 if (Encodings::is_arabic(prev_char) &&
233                         !Encodings::is_arabic_special(prev_char))
234                         return Encodings::TransformChar(c, Encodings::FORM_MEDIAL);
235                 else
236                         return Encodings::TransformChar(c, Encodings::FORM_INITIAL);
237         } else {
238                 if (Encodings::is_arabic(prev_char) &&
239                         !Encodings::is_arabic_special(prev_char))
240                         return Encodings::TransformChar(c, Encodings::FORM_FINAL);
241                 else
242                         return Encodings::TransformChar(c, Encodings::FORM_ISOLATED);
243         }
244 }
245
246 // This is the comments that some of the warnings below refers to.
247 // There are some issues in this file and I don't think they are
248 // really related to the FIX_DOUBLE_SPACE patch. I'd rather think that
249 // this is a problem that has been here almost from day one and that a
250 // larger userbase with differenct access patters triggers the bad
251 // behaviour. (segfaults.) What I think happen is: In several places
252 // we store the paragraph in the current cursor and then moves the
253 // cursor. This movement of the cursor will delete paragraph at the
254 // old position if it is now empty. This will make the temporary
255 // pointer to the old cursor paragraph invalid and dangerous to use.
256 // And is some cases this will trigger a segfault. I have marked some
257 // of the cases where this happens with a warning, but I am sure there
258 // are others in this file and in text2.C. There is also a note in
259 // Delete() that you should read. In Delete I store the paragraph->id
260 // instead of a pointer to the paragraph. I am pretty sure this faulty
261 // use of temporary pointers to paragraphs that might have gotten
262 // invalidated (through a cursor movement) before they are used, are
263 // the cause of the strange crashes we get reported often.
264 //
265 // It is very tiresom to change this code, especially when it is as
266 // hard to read as it is. Help to fix all the cases where this is done
267 // would be greately appreciated.
268 //
269 // Lgb
270
271 int LyXText::singleWidth(ParagraphList::iterator pit, pos_type pos) const
272 {
273         if (pos >= pit->size())
274                 return 0;
275
276         char const c = pit->getChar(pos);
277         return singleWidth(pit, pos, c);
278 }
279
280
281 int LyXText::singleWidth(ParagraphList::iterator pit,
282                          pos_type pos, char c) const
283 {
284         if (pos >= pit->size())
285                 return 0;
286
287         LyXFont const font = getFont(bv()->buffer(), pit, pos);
288
289         // The most common case is handled first (Asger)
290         if (IsPrintable(c)) {
291                 if (font.language()->RightToLeft()) {
292                         if (font.language()->lang() == "arabic" &&
293                             (lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 ||
294                              lyxrc.font_norm_type == LyXRC::ISO_10646_1)) {
295                                 if (Encodings::IsComposeChar_arabic(c))
296                                         return 0;
297                                 else
298                                         c = transformChar(c, *pit, pos);
299                         } else if (font.language()->lang() == "hebrew" &&
300                                  Encodings::IsComposeChar_hebrew(c))
301                                 return 0;
302                 }
303                 return font_metrics::width(c, font);
304
305         }
306
307         if (c == Paragraph::META_INSET) {
308                 Inset * tmpinset = pit->getInset(pos);
309                 if (tmpinset) {
310                         if (tmpinset->lyxCode() == Inset::HFILL_CODE) {
311                                 // Because of the representation as vertical lines
312                                 return 3;
313                         }
314 #if 1
315 #warning inset->update FIXME
316                         // this IS needed otherwise on initialitation we don't get the fill
317                         // of the row right (ONLY on initialization if we read a file!)
318                         // should be changed! (Jug 20011204)
319                         //tmpinset->update(bv());
320                         Dimension dim;
321                         MetricsInfo mi(bv(), font, workWidth());
322                         tmpinset->metrics(mi, dim);
323                         return dim.wid;
324 #else 
325                         return tmpinset->width();
326 #endif
327                 }
328                 return 0;
329         }
330
331         if (IsSeparatorChar(c))
332                 c = ' ';
333         return font_metrics::width(c, font);
334 }
335
336
337 lyx::pos_type LyXText::log2vis(lyx::pos_type pos) const
338 {
339         if (bidi_start == -1)
340                 return pos;
341         else
342                 return log2vis_list[pos - bidi_start];
343 }
344
345
346 lyx::pos_type LyXText::vis2log(lyx::pos_type pos) const
347 {
348         if (bidi_start == -1)
349                 return pos;
350         else
351                 return vis2log_list[pos - bidi_start];
352 }
353
354
355 lyx::pos_type LyXText::bidi_level(lyx::pos_type pos) const
356 {
357         if (bidi_start == -1)
358                 return 0;
359         else
360                 return bidi_levels[pos - bidi_start];
361 }
362
363
364 bool LyXText::bidi_InRange(lyx::pos_type pos) const
365 {
366         return bidi_start == -1 ||
367                 (bidi_start <= pos && pos <= bidi_end);
368 }
369
370
371 void LyXText::computeBidiTables(Buffer const * buf,
372                                 RowList::iterator row) const
373 {
374         bidi_same_direction = true;
375         if (!lyxrc.rtl_support) {
376                 bidi_start = -1;
377                 return;
378         }
379
380         ParagraphList::iterator row_par = row->par();
381
382         Inset * inset = row_par->inInset();
383         if (inset && inset->owner() &&
384             inset->owner()->lyxCode() == Inset::ERT_CODE) {
385                 bidi_start = -1;
386                 return;
387         }
388
389         bidi_start = row->pos();
390         bidi_end = lastPrintablePos(*this, row);
391
392         if (bidi_start > bidi_end) {
393                 bidi_start = -1;
394                 return;
395         }
396
397         if (bidi_end + 2 - bidi_start >
398             static_cast<pos_type>(log2vis_list.size())) {
399                 pos_type new_size =
400                         (bidi_end + 2 - bidi_start < 500) ?
401                         500 : 2 * (bidi_end + 2 - bidi_start);
402                 log2vis_list.resize(new_size);
403                 vis2log_list.resize(new_size);
404                 bidi_levels.resize(new_size);
405         }
406
407         vis2log_list[bidi_end + 1 - bidi_start] = -1;
408         log2vis_list[bidi_end + 1 - bidi_start] = -1;
409
410         pos_type stack[2];
411         bool const rtl_par =
412                 row_par->isRightToLeftPar(buf->params);
413         int level = 0;
414         bool rtl = false;
415         bool rtl0 = false;
416         pos_type const body_pos = row_par->beginningOfBody();
417
418         for (pos_type lpos = bidi_start; lpos <= bidi_end; ++lpos) {
419                 bool is_space = row_par->isLineSeparator(lpos);
420                 pos_type const pos =
421                         (is_space && lpos + 1 <= bidi_end &&
422                          !row_par->isLineSeparator(lpos + 1) &&
423                          !row_par->isNewline(lpos + 1))
424                         ? lpos + 1 : lpos;
425                 LyXFont font = row_par->getFontSettings(buf->params, pos);
426                 if (pos != lpos && 0 < lpos && rtl0 && font.isRightToLeft() &&
427                     font.number() == LyXFont::ON &&
428                     row_par->getFontSettings(buf->params, lpos - 1).number()
429                     == LyXFont::ON) {
430                         font = row_par->getFontSettings(buf->params, lpos);
431                         is_space = false;
432                 }
433
434
435                 bool new_rtl = font.isVisibleRightToLeft();
436                 bool new_rtl0 = font.isRightToLeft();
437                 int new_level;
438
439                 if (lpos == body_pos - 1
440                     && row->pos() < body_pos - 1
441                     && is_space) {
442                         new_level = (rtl_par) ? 1 : 0;
443                         new_rtl = new_rtl0 = rtl_par;
444                 } else if (new_rtl0)
445                         new_level = (new_rtl) ? 1 : 2;
446                 else
447                         new_level = (rtl_par) ? 2 : 0;
448
449                 if (is_space && new_level >= level) {
450                         new_level = level;
451                         new_rtl = rtl;
452                         new_rtl0 = rtl0;
453                 }
454
455                 int new_level2 = new_level;
456
457                 if (level == new_level && rtl0 != new_rtl0) {
458                         --new_level2;
459                         log2vis_list[lpos - bidi_start] = (rtl) ? 1 : -1;
460                 } else if (level < new_level) {
461                         log2vis_list[lpos - bidi_start] =  (rtl) ? -1 : 1;
462                         if (new_level > rtl_par)
463                                 bidi_same_direction = false;
464                 } else
465                         log2vis_list[lpos - bidi_start] = (new_rtl) ? -1 : 1;
466                 rtl = new_rtl;
467                 rtl0 = new_rtl0;
468                 bidi_levels[lpos - bidi_start] = new_level;
469
470                 while (level > new_level2) {
471                         pos_type old_lpos = stack[--level];
472                         int delta = lpos - old_lpos - 1;
473                         if (level % 2)
474                                 delta = -delta;
475                         log2vis_list[lpos - bidi_start] += delta;
476                         log2vis_list[old_lpos - bidi_start] += delta;
477                 }
478                 while (level < new_level)
479                         stack[level++] = lpos;
480         }
481
482         while (level > 0) {
483                 pos_type const old_lpos = stack[--level];
484                 int delta = bidi_end - old_lpos;
485                 if (level % 2)
486                         delta = -delta;
487                 log2vis_list[old_lpos - bidi_start] += delta;
488         }
489
490         pos_type vpos = bidi_start - 1;
491         for (pos_type lpos = bidi_start;
492              lpos <= bidi_end; ++lpos) {
493                 vpos += log2vis_list[lpos - bidi_start];
494                 vis2log_list[vpos - bidi_start] = lpos;
495                 log2vis_list[lpos - bidi_start] = vpos;
496         }
497 }
498
499
500 // This method requires a previous call to ComputeBidiTables()
501 bool LyXText::isBoundary(Buffer const * buf, Paragraph const & par,
502                          pos_type pos) const
503 {
504         if (!lyxrc.rtl_support || pos == 0)
505                 return false;
506
507         if (!bidi_InRange(pos - 1)) {
508                 /// This can happen if pos is the first char of a row.
509                 /// Returning false in this case is incorrect!
510                 return false;
511         }
512
513         bool const rtl = bidi_level(pos - 1) % 2;
514         bool const rtl2 = bidi_InRange(pos)
515                 ? bidi_level(pos) % 2
516                 : par.isRightToLeftPar(buf->params);
517         return rtl != rtl2;
518 }
519
520
521 bool LyXText::isBoundary(Buffer const * buf, Paragraph const & par,
522                          pos_type pos, LyXFont const & font) const
523 {
524         if (!lyxrc.rtl_support)
525                 return false;    // This is just for speedup
526
527         bool const rtl = font.isVisibleRightToLeft();
528         bool const rtl2 = bidi_InRange(pos)
529                 ? bidi_level(pos) % 2
530                 : par.isRightToLeftPar(buf->params);
531         return rtl != rtl2;
532 }
533
534
535 int LyXText::leftMargin(Row const & row) const
536 {
537         Inset * ins;
538
539         if (row.pos() < row.par()->size())
540                 if ((row.par()->getChar(row.pos()) == Paragraph::META_INSET) &&
541                     (ins = row.par()->getInset(row.pos())) &&
542                     (ins->needFullRow() || ins->display()))
543                         return LEFT_MARGIN;
544
545         LyXTextClass const & tclass =
546                 bv()->buffer()->params.getLyXTextClass();
547         LyXLayout_ptr const & layout = row.par()->layout();
548
549         string parindent = layout->parindent;
550
551         int x = LEFT_MARGIN;
552
553         x += font_metrics::signedWidth(tclass.leftmargin(), tclass.defaultfont());
554
555         // this is the way, LyX handles the LaTeX-Environments.
556         // I have had this idea very late, so it seems to be a
557         // later added hack and this is true
558         if (!row.par()->getDepth()) {
559                 if (row.par()->layout() == tclass.defaultLayout()) {
560                         // find the previous same level paragraph
561                         if (row.par() != ownerParagraphs().begin()) {
562                                 ParagraphList::iterator newpit =
563                                         depthHook(row.par(), ownerParagraphs(),
564                                                   row.par()->getDepth());
565                                 if (newpit == row.par() &&
566                                     newpit->layout()->nextnoindent)
567                                         parindent.erase();
568                         }
569                 }
570         } else {
571                 // find the next level paragraph
572
573                 ParagraphList::iterator newpar = outerHook(row.par(),
574                                                            ownerParagraphs());
575
576                 // make a corresponding row. Needed to call leftMargin()
577
578                 // check wether it is a sufficent paragraph
579                 if (newpar != ownerParagraphs().end() &&
580                     newpar->layout()->isEnvironment()) {
581                         Row dummyrow;
582                         dummyrow.par(newpar);
583                         dummyrow.pos(newpar->size());
584                         x = leftMargin(dummyrow);
585                 }
586
587                 if (newpar != ownerParagraphs().end() &&
588                     row.par()->layout() == tclass.defaultLayout()) {
589                         if (newpar->params().noindent())
590                                 parindent.erase();
591                         else {
592                                 parindent = newpar->layout()->parindent;
593                         }
594
595                 }
596         }
597
598         LyXFont const labelfont = getLabelFont(bv()->buffer(), row.par());
599         switch (layout->margintype) {
600         case MARGIN_DYNAMIC:
601                 if (!layout->leftmargin.empty()) {
602                         x += font_metrics::signedWidth(layout->leftmargin,
603                                                   tclass.defaultfont());
604                 }
605                 if (!row.par()->getLabelstring().empty()) {
606                         x += font_metrics::signedWidth(layout->labelindent,
607                                                   labelfont);
608                         x += font_metrics::width(row.par()->getLabelstring(),
609                                             labelfont);
610                         x += font_metrics::width(layout->labelsep, labelfont);
611                 }
612                 break;
613         case MARGIN_MANUAL:
614                 x += font_metrics::signedWidth(layout->labelindent, labelfont);
615                 // The width of an empty par, even with manual label, should be 0
616                 if (!row.par()->empty() && row.pos() >= row.par()->beginningOfBody()) {
617                         if (!row.par()->getLabelWidthString().empty()) {
618                                 x += font_metrics::width(row.par()->getLabelWidthString(),
619                                                labelfont);
620                                 x += font_metrics::width(layout->labelsep, labelfont);
621                         }
622                 }
623                 break;
624         case MARGIN_STATIC:
625                 x += font_metrics::signedWidth(layout->leftmargin, tclass.defaultfont()) * 4
626                         / (row.par()->getDepth() + 4);
627                 break;
628         case MARGIN_FIRST_DYNAMIC:
629                 if (layout->labeltype == LABEL_MANUAL) {
630                         if (row.pos() >= row.par()->beginningOfBody()) {
631                                 x += font_metrics::signedWidth(layout->leftmargin,
632                                                           labelfont);
633                         } else {
634                                 x += font_metrics::signedWidth(layout->labelindent,
635                                                           labelfont);
636                         }
637                 } else if (row.pos()
638                            // Special case to fix problems with
639                            // theorems (JMarc)
640                            || (layout->labeltype == LABEL_STATIC
641                                && layout->latextype == LATEX_ENVIRONMENT
642                                && !isFirstInSequence(row.par(), ownerParagraphs()))) {
643                         x += font_metrics::signedWidth(layout->leftmargin,
644                                                   labelfont);
645                 } else if (layout->labeltype != LABEL_TOP_ENVIRONMENT
646                            && layout->labeltype != LABEL_BIBLIO
647                            && layout->labeltype !=
648                            LABEL_CENTERED_TOP_ENVIRONMENT) {
649                         x += font_metrics::signedWidth(layout->labelindent,
650                                                   labelfont);
651                         x += font_metrics::width(layout->labelsep, labelfont);
652                         x += font_metrics::width(row.par()->getLabelstring(),
653                                             labelfont);
654                 }
655                 break;
656
657         case MARGIN_RIGHT_ADDRESS_BOX:
658         {
659                 // ok, a terrible hack. The left margin depends on the widest
660                 // row in this paragraph. Do not care about footnotes, they
661                 // are *NOT* allowed in the LaTeX realisation of this layout.
662
663                 // find the first row of this paragraph
664                 RowList::iterator tmprit = rowlist_.begin();
665                 while (tmprit != rowlist_.end()
666                        && tmprit->par() != row.par())
667                         ++tmprit;
668
669                 int minfill = tmprit->fill();
670                 while (boost::next(tmprit) != rowlist_.end() &&
671                        boost::next(tmprit)->par() == row.par()) {
672                         ++tmprit;
673                         if (tmprit->fill() < minfill)
674                                 minfill = tmprit->fill();
675                 }
676
677                 x += font_metrics::signedWidth(layout->leftmargin,
678                         tclass.defaultfont());
679                 x += minfill;
680         }
681         break;
682         }
683
684         if ((workWidth() > 0) &&
685                 !row.par()->params().leftIndent().zero())
686         {
687                 LyXLength const len = row.par()->params().leftIndent();
688                 int const tw = inset_owner ?
689                         inset_owner->latexTextWidth(bv()) : workWidth();
690                 x += len.inPixels(tw);
691         }
692
693         LyXAlignment align;
694
695         if (row.par()->params().align() == LYX_ALIGN_LAYOUT)
696                 align = layout->align;
697         else
698                 align = row.par()->params().align();
699
700         // set the correct parindent
701         if (row.pos() == 0) {
702                 if ((layout->labeltype == LABEL_NO_LABEL
703                      || layout->labeltype == LABEL_TOP_ENVIRONMENT
704                      || layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT
705                      || (layout->labeltype == LABEL_STATIC
706                          && layout->latextype == LATEX_ENVIRONMENT
707                          && !isFirstInSequence(row.par(), ownerParagraphs())))
708                     && align == LYX_ALIGN_BLOCK
709                     && !row.par()->params().noindent()
710                         // in tabulars and ert paragraphs are never indented!
711                         && (!row.par()->inInset() || !row.par()->inInset()->owner() ||
712                                 (row.par()->inInset()->owner()->lyxCode() != Inset::TABULAR_CODE &&
713                                  row.par()->inInset()->owner()->lyxCode() != Inset::ERT_CODE))
714                     && (row.par()->layout() != tclass.defaultLayout() ||
715                         bv()->buffer()->params.paragraph_separation ==
716                         BufferParams::PARSEP_INDENT)) {
717                         x += font_metrics::signedWidth(parindent,
718                                                   tclass.defaultfont());
719                 } else if (layout->labeltype == LABEL_BIBLIO) {
720                         // ale970405 Right width for bibitems
721                         x += bibitemMaxWidth(bv(), tclass.defaultfont());
722                 }
723         }
724
725         return x;
726 }
727
728
729 int LyXText::rightMargin(Buffer const & buf, Row const & row) const
730 {
731         Inset * ins;
732
733         if (row.pos() < row.par()->size())
734                 if ((row.par()->getChar(row.pos()) == Paragraph::META_INSET) &&
735                     (ins = row.par()->getInset(row.pos())) &&
736                     (ins->needFullRow() || ins->display()))
737                         return PAPER_MARGIN;
738
739         LyXTextClass const & tclass = buf.params.getLyXTextClass();
740         LyXLayout_ptr const & layout = row.par()->layout();
741
742         int x = PAPER_MARGIN
743                 + font_metrics::signedWidth(tclass.rightmargin(),
744                                        tclass.defaultfont());
745
746         x += font_metrics::signedWidth(layout->rightmargin,
747                                        tclass.defaultfont())
748                 * 4 / (row.par()->getDepth() + 4);
749         return x;
750 }
751
752
753 int LyXText::labelEnd(Row const & row) const
754 {
755         if (row.par()->layout()->margintype == MARGIN_MANUAL) {
756                 Row tmprow = row;
757                 tmprow.pos(row.par()->size());
758                 // return the beginning of the body
759                 return leftMargin(tmprow);
760         }
761
762         // LabelEnd is only needed if the layout
763         // fills a flushleft label.
764         return 0;
765 }
766
767
768 namespace {
769
770 // this needs special handling - only newlines count as a break point
771 pos_type addressBreakPoint(pos_type i, Paragraph const & par)
772 {
773         for (; i < par.size(); ++i) {
774                 if (par.isNewline(i))
775                         return i;
776         }
777
778         return par.size();
779 }
780
781 };
782
783
784 pos_type LyXText::rowBreakPoint(Row const & row) const
785 {
786         ParagraphList::iterator pit = row.par();
787
788         // maximum pixel width of a row.
789         int width = workWidth() - rightMargin(*bv()->buffer(), row);
790
791         // inset->textWidth() returns -1 via workWidth(),
792         // but why ?
793         if (width < 0)
794                 return pit->size();
795
796         LyXLayout_ptr const & layout = pit->layout();
797
798         if (layout->margintype == MARGIN_RIGHT_ADDRESS_BOX)
799                 return addressBreakPoint(row.pos(), *pit);
800
801         pos_type const pos = row.pos();
802         pos_type const body_pos = pit->beginningOfBody();
803         pos_type const last = pit->size();
804         pos_type point = last;
805
806         if (pos == last)
807                 return last;
808
809         // Now we iterate through until we reach the right margin
810         // or the end of the par, then choose the possible break
811         // nearest that.
812
813         int const left = leftMargin(row);
814         int x = left;
815
816         // pixel width since last breakpoint
817         int chunkwidth = 0;
818         bool fullrow = false;
819
820         pos_type i = pos;
821         for (; i < last; ++i) {
822                 if (pit->isNewline(i)) {
823                         point = i;
824                         break;
825                 }
826
827                 char const c = pit->getChar(i);
828
829                 int thiswidth;
830
831                 // add the auto-hfill from label end to the body
832                 if (body_pos && i == body_pos) {
833                         thiswidth = font_metrics::width(layout->labelsep,
834                                 getLabelFont(bv()->buffer(), pit));
835                         if (pit->isLineSeparator(i - 1))
836                                 thiswidth -= singleWidth(pit, i - 1);
837                         int left_margin = labelEnd(row);
838                         if (thiswidth + x < left_margin)
839                                 thiswidth = left_margin - x;
840                         thiswidth += singleWidth(pit, i, c);
841                 } else {
842                         thiswidth = singleWidth(pit, i, c);
843                 }
844
845                 x += thiswidth;
846                 chunkwidth += thiswidth;
847
848                 Inset * in = pit->isInset(i) ? pit->getInset(i) : 0;
849                 fullrow = (in && (in->display() || in->needFullRow()));
850
851                 // break before a character that will fall off
852                 // the right of the row
853                 if (x >= width) {
854                         // if no break before or we are at an inset
855                         // that will take up a row, break here
856                         if (point == last || fullrow || chunkwidth >= (width - left)) {
857                                 if (pos < i)
858                                         point = i - 1;
859                                 else
860                                         point = i;
861                         }
862                         break;
863                 }
864
865                 if (!in || in->isChar()) {
866                         // some insets are line separators too
867                         if (pit->isLineSeparator(i)) {
868                                 point = i;
869                                 chunkwidth = 0;
870                         }
871                         continue;
872                 }
873
874                 if (!fullrow)
875                         continue;
876
877                 // full row insets start at a new row
878                 if (i == pos) {
879                         if (pos < last - 1) {
880                                 point = i;
881                                 if (pit->isLineSeparator(i + 1))
882                                         ++point;
883                         } else {
884                                 // to avoid extra rows
885                                 point = last;
886                         }
887                 } else {
888                         point = i - 1;
889                 }
890
891                 return point;
892         }
893
894         if (point == last && x >= width) {
895                 // didn't find one, break at the point we reached the edge
896                 point = i;
897         } else if (i == last && x < width) {
898                 // found one, but we fell off the end of the par, so prefer
899                 // that.
900                 point = last;
901         }
902
903         // manual labels cannot be broken in LaTeX. But we
904         // want to make our on-screen rendering of footnotes
905         // etc. still break
906         if (!fullrow && body_pos && point < body_pos)
907                 point = body_pos - 1;
908
909         return point;
910 }
911
912
913 // returns the minimum space a row needs on the screen in pixel
914 int LyXText::fill(RowList::iterator row, int paper_width) const
915 {
916         if (paper_width < 0)
917                 return 0;
918
919         int w;
920         // get the pure distance
921         pos_type const last = lastPrintablePos(*this, row);
922
923         ParagraphList::iterator pit = row->par();
924         LyXLayout_ptr const & layout = pit->layout();
925
926         // special handling of the right address boxes
927         if (layout->margintype == MARGIN_RIGHT_ADDRESS_BOX) {
928                 int const tmpfill = row->fill();
929                 row->fill(0); // the minfill in MarginLeft()
930                 w = leftMargin(*row);
931                 row->fill(tmpfill);
932         } else
933                 w = leftMargin(*row);
934
935         pos_type const body_pos = pit->beginningOfBody();
936         pos_type i = row->pos();
937
938         while (i <= last) {
939                 if (body_pos > 0 && i == body_pos) {
940                         w += font_metrics::width(layout->labelsep, getLabelFont(bv()->buffer(), pit));
941                         if (pit->isLineSeparator(i - 1))
942                                 w -= singleWidth(pit, i - 1);
943                         int left_margin = labelEnd(*row);
944                         if (w < left_margin)
945                                 w = left_margin;
946                 }
947                 w += singleWidth(pit, i);
948                 ++i;
949         }
950         if (body_pos > 0 && body_pos > last) {
951                 w += font_metrics::width(layout->labelsep, getLabelFont(bv()->buffer(), pit));
952                 if (last >= 0 && pit->isLineSeparator(last))
953                         w -= singleWidth(pit, last);
954                 int const left_margin = labelEnd(*row);
955                 if (w < left_margin)
956                         w = left_margin;
957         }
958
959         int const fill = paper_width - w - rightMargin(*bv()->buffer(), *row);
960
961         // If this case happens, it means that our calculation
962         // of the widths of the chars when we do rowBreakPoint()
963         // went wrong for some reason. Typically in list bodies.
964         // Things just about hobble on anyway, though you'll end
965         // up with a "fill_separator" less than zero, which corresponds
966         // to inter-word spacing being too small. Hopefully this problem
967         // will die when the label hacks die.
968         if (lyxerr.debugging() && fill < 0) {
969                 lyxerr[Debug::GUI] << "Eek, fill() was < 0: " << fill
970                         << " w " << w << " paper_width " << paper_width
971                         << " right margin " << rightMargin(*bv()->buffer(), *row) << endl;
972         }
973
974         return fill;
975 }
976
977
978 // returns the minimum space a manual label needs on the screen in pixel
979 int LyXText::labelFill(Row const & row) const
980 {
981         ParagraphList::iterator pit = row.par();
982
983         pos_type last = pit->beginningOfBody();
984
985         Assert(last > 0);
986
987         // -1 because a label ends either with a space that is in the label,
988         // or with the beginning of a footnote that is outside the label.
989         --last;
990
991         // a separator at this end does not count
992         if (pit->isLineSeparator(last))
993                 --last;
994
995         int w = 0;
996         pos_type i = row.pos();
997         while (i <= last) {
998                 w += singleWidth(pit, i);
999                 ++i;
1000         }
1001
1002         int fill = 0;
1003         string const & labwidstr = pit->params().labelWidthString();
1004         if (!labwidstr.empty()) {
1005                 LyXFont const labfont = getLabelFont(bv()->buffer(), pit);
1006                 int const labwidth = font_metrics::width(labwidstr, labfont);
1007                 fill = max(labwidth - w, 0);
1008         }
1009
1010         return fill;
1011 }
1012
1013
1014 LColor::color LyXText::backgroundColor() const
1015 {
1016         if (inset_owner)
1017                 return inset_owner->backgroundColor();
1018         else
1019                 return LColor::background;
1020 }
1021
1022
1023 void LyXText::setHeightOfRow(RowList::iterator rit)
1024 {
1025         Assert(rit != rows().end());
1026
1027         // get the maximum ascent and the maximum descent
1028         float layoutasc = 0;
1029         float layoutdesc = 0;
1030         float tmptop = 0;
1031         LyXFont tmpfont;
1032         Inset * tmpinset = 0;
1033
1034         // ok, let us initialize the maxasc and maxdesc value.
1035         // This depends in LaTeX of the font of the last character
1036         // in the paragraph. The hack below is necessary because
1037         // of the possibility of open footnotes
1038
1039         // Correction: only the fontsize count. The other properties
1040         //  are taken from the layoutfont. Nicer on the screen :)
1041         ParagraphList::iterator pit = rit->par();
1042
1043         LyXLayout_ptr const & layout = pit->layout();
1044
1045         // as max get the first character of this row then it can increase but not
1046         // decrease the height. Just some point to start with so we don't have to
1047         // do the assignment below too often.
1048         LyXFont font = getFont(bv()->buffer(), pit, rit->pos());
1049         LyXFont::FONT_SIZE const tmpsize = font.size();
1050         font = getLayoutFont(bv()->buffer(), pit);
1051         LyXFont::FONT_SIZE const size = font.size();
1052         font.setSize(tmpsize);
1053
1054         LyXFont labelfont = getLabelFont(bv()->buffer(), pit);
1055
1056         float spacing_val = 1.0;
1057         if (!pit->params().spacing().isDefault()) {
1058                 spacing_val = pit->params().spacing().getValue();
1059         } else {
1060                 spacing_val = bv()->buffer()->params.spacing.getValue();
1061         }
1062         //lyxerr << "spacing_val = " << spacing_val << endl;
1063
1064         int maxasc = int(font_metrics::maxAscent(font) *
1065                          layout->spacing.getValue() *
1066                          spacing_val);
1067         int maxdesc = int(font_metrics::maxDescent(font) *
1068                           layout->spacing.getValue() *
1069                           spacing_val);
1070
1071         pos_type const pos_end = lastPos(*this, rit);
1072         int labeladdon = 0;
1073         int maxwidth = 0;
1074
1075         if (!pit->empty()) {
1076                 // Check if any insets are larger
1077                 for (pos_type pos = rit->pos(); pos <= pos_end; ++pos) {
1078                         if (pit->isInset(pos)) {
1079                                 tmpfont = getFont(bv()->buffer(), pit, pos);
1080                                 tmpinset = pit->getInset(pos);
1081                                 if (tmpinset) {
1082 #if 0 // this is needed for deep update on initialitation
1083 #warning inset->update FIXME
1084                                         //tmpinset->update(bv());
1085                                         Dimension dim;
1086                                         MetricsInfo mi(bv(), tmpfont, workWidth());
1087                                         tmpinset->metrics(mi, dim);
1088                                         maxwidth += dim.wid;
1089                                         maxasc = max(maxasc, dim.asc);
1090                                         maxdesc = max(maxdesc, dim.des);
1091 #else
1092                                         maxwidth += tmpinset->width();
1093                                         maxasc = max(maxasc, tmpinset->ascent());
1094                                         maxdesc = max(maxdesc, tmpinset->descent());
1095 #endif
1096                                 }
1097                         } else {
1098                                 maxwidth += singleWidth(pit, pos);
1099                         }
1100                 }
1101         }
1102
1103         // Check if any custom fonts are larger (Asger)
1104         // This is not completely correct, but we can live with the small,
1105         // cosmetic error for now.
1106         LyXFont::FONT_SIZE maxsize =
1107                 pit->highestFontInRange(rit->pos(), pos_end, size);
1108         if (maxsize > font.size()) {
1109                 font.setSize(maxsize);
1110                 maxasc = max(maxasc, font_metrics::maxAscent(font));
1111                 maxdesc = max(maxdesc, font_metrics::maxDescent(font));
1112         }
1113
1114         // This is nicer with box insets:
1115         ++maxasc;
1116         ++maxdesc;
1117
1118         rit->ascent_of_text(maxasc);
1119
1120         // is it a top line?
1121         if (!rit->pos()) {
1122
1123                 // some parksips VERY EASY IMPLEMENTATION
1124                 if (bv()->buffer()->params.paragraph_separation ==
1125                         BufferParams::PARSEP_SKIP)
1126                 {
1127                         if (layout->isParagraph()
1128                                 && pit->getDepth() == 0
1129                                 && pit != ownerParagraphs().begin())
1130                         {
1131                                 maxasc += bv()->buffer()->params.getDefSkip().inPixels(*bv());
1132                         } else if (pit != ownerParagraphs().begin() &&
1133                                    boost::prior(pit)->layout()->isParagraph() &&
1134                                    boost::prior(pit)->getDepth() == 0)
1135                         {
1136                                 // is it right to use defskip here too? (AS)
1137                                 maxasc += bv()->buffer()->params.getDefSkip().inPixels(*bv());
1138                         }
1139                 }
1140
1141                 // the top margin
1142                 if (pit == ownerParagraphs().begin() && !isInInset())
1143                         maxasc += PAPER_MARGIN;
1144
1145                 // add the vertical spaces, that the user added
1146                 maxasc += getLengthMarkerHeight(*bv(), pit->params().spaceTop());
1147
1148                 // do not forget the DTP-lines!
1149                 // there height depends on the font of the nearest character
1150                 if (pit->params().lineTop())
1151
1152                         maxasc += 2 * font_metrics::ascent('x', getFont(bv()->buffer(),
1153                                         pit, 0));
1154                 // and now the pagebreaks
1155                 if (pit->params().pagebreakTop())
1156                         maxasc += 3 * defaultRowHeight();
1157
1158                 if (pit->params().startOfAppendix())
1159                         maxasc += 3 * defaultRowHeight();
1160
1161                 // This is special code for the chapter, since the label of this
1162                 // layout is printed in an extra row
1163                 if (layout->labeltype == LABEL_COUNTER_CHAPTER
1164                         && bv()->buffer()->params.secnumdepth >= 0)
1165                 {
1166                         float spacing_val = 1.0;
1167                         if (!pit->params().spacing().isDefault()) {
1168                                 spacing_val = pit->params().spacing().getValue();
1169                         } else {
1170                                 spacing_val = bv()->buffer()->params.spacing.getValue();
1171                         }
1172
1173                         labeladdon = int(font_metrics::maxDescent(labelfont) *
1174                                          layout->spacing.getValue() *
1175                                          spacing_val)
1176                                 + int(font_metrics::maxAscent(labelfont) *
1177                                       layout->spacing.getValue() *
1178                                       spacing_val);
1179                 }
1180
1181                 // special code for the top label
1182                 if ((layout->labeltype == LABEL_TOP_ENVIRONMENT
1183                      || layout->labeltype == LABEL_BIBLIO
1184                      || layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT)
1185                     && isFirstInSequence(pit, ownerParagraphs())
1186                     && !pit->getLabelstring().empty())
1187                 {
1188                         float spacing_val = 1.0;
1189                         if (!pit->params().spacing().isDefault()) {
1190                                 spacing_val = pit->params().spacing().getValue();
1191                         } else {
1192                                 spacing_val = bv()->buffer()->params.spacing.getValue();
1193                         }
1194
1195                         labeladdon = int(
1196                                 (font_metrics::maxAscent(labelfont) *
1197                                  layout->spacing.getValue() *
1198                                  spacing_val)
1199                                 +(font_metrics::maxDescent(labelfont) *
1200                                   layout->spacing.getValue() *
1201                                   spacing_val)
1202                                 + layout->topsep * defaultRowHeight()
1203                                 + layout->labelbottomsep * defaultRowHeight());
1204                 }
1205
1206                 // And now the layout spaces, for example before and after
1207                 // a section, or between the items of a itemize or enumerate
1208                 // environment.
1209
1210                 if (!pit->params().pagebreakTop()) {
1211                         ParagraphList::iterator prev =
1212                                 depthHook(pit, ownerParagraphs(),
1213                                           pit->getDepth());
1214                         if (prev != pit && prev->layout() == layout &&
1215                                 prev->getDepth() == pit->getDepth() &&
1216                                 prev->getLabelWidthString() == pit->getLabelWidthString())
1217                         {
1218                                 layoutasc = (layout->itemsep * defaultRowHeight());
1219                         } else if (rit != rows().begin()) {
1220                                 tmptop = layout->topsep;
1221
1222                                 if (boost::prior(pit)->getDepth() >= pit->getDepth())
1223                                         tmptop -= boost::prior(rit)->par()->layout()->bottomsep;
1224
1225                                 if (tmptop > 0)
1226                                         layoutasc = (tmptop * defaultRowHeight());
1227                         } else if (pit->params().lineTop()) {
1228                                 tmptop = layout->topsep;
1229
1230                                 if (tmptop > 0)
1231                                         layoutasc = (tmptop * defaultRowHeight());
1232                         }
1233
1234                         prev = outerHook(pit, ownerParagraphs());
1235                         if (prev != ownerParagraphs().end())  {
1236                                 maxasc += int(prev->layout()->parsep * defaultRowHeight());
1237                         } else if (pit != ownerParagraphs().begin()) {
1238                                 ParagraphList::iterator prior_pit = boost::prior(pit);
1239                                 if (prior_pit->getDepth() != 0 ||
1240                                     prior_pit->layout() == layout) {
1241                                         maxasc += int(layout->parsep * defaultRowHeight());
1242                                 }
1243                         }
1244                 }
1245         }
1246
1247         // is it a bottom line?
1248         RowList::iterator next_rit = boost::next(rit);
1249         if (next_rit == rows().end() || next_rit->par() != pit) {
1250                 // the bottom margin
1251                 ParagraphList::iterator nextpit = boost::next(pit);
1252                 if (nextpit == ownerParagraphs().end() &&
1253                     !isInInset())
1254                         maxdesc += PAPER_MARGIN;
1255
1256                 // add the vertical spaces, that the user added
1257                 maxdesc += getLengthMarkerHeight(*bv(), pit->params().spaceBottom());
1258
1259                 // do not forget the DTP-lines!
1260                 // there height depends on the font of the nearest character
1261                 if (pit->params().lineBottom())
1262                         maxdesc += 2 * font_metrics::ascent('x',
1263                                                        getFont(bv()->buffer(),
1264                                                                pit,
1265                                                                max(pos_type(0), pit->size() - 1)));
1266
1267                 // and now the pagebreaks
1268                 if (pit->params().pagebreakBottom())
1269                         maxdesc += 3 * defaultRowHeight();
1270
1271                 // and now the layout spaces, for example before and after
1272                 // a section, or between the items of a itemize or enumerate
1273                 // environment
1274                 if (!pit->params().pagebreakBottom()
1275                     && nextpit != ownerParagraphs().end()) {
1276                         ParagraphList::iterator comparepit = pit;
1277                         float usual = 0;
1278                         float unusual = 0;
1279
1280                         if (comparepit->getDepth() > nextpit->getDepth()) {
1281                                 usual = (comparepit->layout()->bottomsep * defaultRowHeight());
1282                                 comparepit = depthHook(comparepit, ownerParagraphs(), nextpit->getDepth());
1283                                 if (comparepit->layout()!= nextpit->layout()
1284                                         || nextpit->getLabelWidthString() !=
1285                                         comparepit->getLabelWidthString())
1286                                 {
1287                                         unusual = (comparepit->layout()->bottomsep * defaultRowHeight());
1288                                 }
1289                                 if (unusual > usual)
1290                                         layoutdesc = unusual;
1291                                 else
1292                                         layoutdesc = usual;
1293                         } else if (comparepit->getDepth() ==  nextpit->getDepth()) {
1294
1295                                 if (comparepit->layout() != nextpit->layout()
1296                                         || nextpit->getLabelWidthString() !=
1297                                         comparepit->getLabelWidthString())
1298                                         layoutdesc = int(comparepit->layout()->bottomsep * defaultRowHeight());
1299                         }
1300                 }
1301         }
1302
1303         // incalculate the layout spaces
1304         maxasc += int(layoutasc * 2 / (2 + pit->getDepth()));
1305         maxdesc += int(layoutdesc * 2 / (2 + pit->getDepth()));
1306
1307         // calculate the new height of the text
1308         height -= rit->height();
1309
1310         rit->height(maxasc + maxdesc + labeladdon);
1311         rit->baseline(maxasc + labeladdon);
1312
1313         height += rit->height();
1314
1315         rit->top_of_text(rit->baseline() - font_metrics::maxAscent(font));
1316
1317         int x = 0;
1318         if (layout->margintype != MARGIN_RIGHT_ADDRESS_BOX) {
1319                 int dummy;
1320                 // this IS needed
1321                 rit->width(maxwidth);
1322                 prepareToPrint(rit, x, dummy, dummy, dummy, false);
1323         }
1324         rit->width(int(maxwidth + x));
1325         if (inset_owner) {
1326                 width = max(0, workWidth());
1327                 RowList::iterator it = rows().begin();
1328                 RowList::iterator end = rows().end();
1329                 for (; it != end; ++it) {
1330                         if (it->width() > width)
1331                                 width = it->width();
1332                 }
1333         }
1334 }
1335
1336
1337 // Appends the implicit specified paragraph before the specified row,
1338 // start at the implicit given position
1339 void LyXText::appendParagraph(RowList::iterator rowit)
1340 {
1341         Assert(rowit != rowlist_.end());
1342
1343         pos_type const last = rowit->par()->size();
1344         bool done = false;
1345
1346         do {
1347                 pos_type z = rowBreakPoint(*rowit);
1348
1349                 RowList::iterator tmprow = rowit;
1350
1351                 if (z < last) {
1352                         ++z;
1353                         Row newrow(rowit->par(), z);
1354                         rowit = rowlist_.insert(boost::next(rowit), newrow);
1355                 } else {
1356                         done = true;
1357                 }
1358
1359                 // Set the dimensions of the row
1360                 // fixed fill setting now by calling inset->update() in
1361                 // SingleWidth when needed!
1362                 tmprow->fill(fill(tmprow, workWidth()));
1363                 setHeightOfRow(tmprow);
1364
1365         } while (!done);
1366 }
1367
1368
1369 void LyXText::breakAgain(RowList::iterator rit)
1370 {
1371         Assert(rit != rows().end());
1372
1373         bool not_ready = true;
1374
1375         do  {
1376                 pos_type z = rowBreakPoint(*rit);
1377                 RowList::iterator tmprit = rit;
1378                 RowList::iterator end = rows().end();
1379
1380                 if (z < rit->par()->size()) {
1381                         RowList::iterator next_rit = boost::next(rit);
1382
1383                         if (next_rit == end ||
1384                             (next_rit != end &&
1385                              next_rit->par() != rit->par())) {
1386                                 // insert a new row
1387                                 ++z;
1388                                 Row newrow(rit->par(), z);
1389                                 rit = rowlist_.insert(next_rit, newrow);
1390                         } else  {
1391                                 ++rit;
1392                                 ++z;
1393                                 if (rit->pos() == z)
1394                                         not_ready = false; // the rest will not change
1395                                 else {
1396                                         rit->pos(z);
1397                                 }
1398                         }
1399                 } else {
1400                         // if there are some rows too much, delete them
1401                         // only if you broke the whole paragraph!
1402                         RowList::iterator tmprit2 = rit;
1403                         while (boost::next(tmprit2) != end
1404                                && boost::next(tmprit2)->par() == rit->par()) {
1405                                 ++tmprit2;
1406                         }
1407                         while (tmprit2 != rit) {
1408                                 --tmprit2;
1409                                 removeRow(boost::next(tmprit2));
1410                         }
1411                         not_ready = false;
1412                 }
1413
1414                 // set the dimensions of the row
1415                 tmprit->fill(fill(tmprit, workWidth()));
1416                 setHeightOfRow(tmprit);
1417         } while (not_ready);
1418 }
1419
1420
1421 // this is just a little changed version of break again
1422 void LyXText::breakAgainOneRow(RowList::iterator rit)
1423 {
1424         Assert(rit != rows().end());
1425
1426         pos_type z = rowBreakPoint(*rit);
1427         RowList::iterator tmprit = rit;
1428         RowList::iterator end = rows().end();
1429
1430         if (z < rit->par()->size()) {
1431                 RowList::iterator next_rit = boost::next(rit);
1432
1433                 if (next_rit == end ||
1434                     (next_rit != end &&
1435                      next_rit->par() != rit->par())) {
1436                         // insert a new row
1437                         ++z;
1438                         Row newrow(rit->par(), z);
1439                         rit = rowlist_.insert(next_rit, newrow);
1440                 } else  {
1441                         ++rit;
1442                         ++z;
1443                         if (rit->pos() != z)
1444                                 rit->pos(z);
1445                 }
1446         } else {
1447                 // if there are some rows too much, delete them
1448                 // only if you broke the whole paragraph!
1449                 RowList::iterator tmprit2 = rit;
1450                 while (boost::next(tmprit2) != end
1451                        && boost::next(tmprit2)->par() == rit->par()) {
1452                         ++tmprit2;
1453                 }
1454                 while (tmprit2 != rit) {
1455                         --tmprit2;
1456                         removeRow(boost::next(tmprit2));
1457                 }
1458         }
1459
1460         // set the dimensions of the row
1461         tmprit->fill(fill(tmprit, workWidth()));
1462         setHeightOfRow(tmprit);
1463 }
1464
1465
1466 void LyXText::breakParagraph(ParagraphList & paragraphs, char keep_layout)
1467 {
1468         // allow only if at start or end, or all previous is new text
1469         if (cursor.pos() && cursor.pos() != cursor.par()->size()
1470                 && cursor.par()->isChangeEdited(0, cursor.pos()))
1471                 return;
1472
1473         LyXTextClass const & tclass =
1474                 bv()->buffer()->params.getLyXTextClass();
1475         LyXLayout_ptr const & layout = cursor.par()->layout();
1476
1477         // this is only allowed, if the current paragraph is not empty or caption
1478         // and if it has not the keepempty flag active
1479         if (cursor.par()->empty() && !cursor.par()->allowEmpty()
1480            && layout->labeltype != LABEL_SENSITIVE)
1481                 return;
1482
1483         setUndo(bv(), Undo::FINISH, cursor.par());
1484
1485         // Always break behind a space
1486         //
1487         // It is better to erase the space (Dekel)
1488         if (cursor.pos() < cursor.par()->size()
1489              && cursor.par()->isLineSeparator(cursor.pos()))
1490            cursor.par()->erase(cursor.pos());
1491         // cursor.pos(cursor.pos() + 1);
1492
1493         // break the paragraph
1494         if (keep_layout)
1495                 keep_layout = 2;
1496         else
1497                 keep_layout = layout->isEnvironment();
1498
1499         // we need to set this before we insert the paragraph. IMO the
1500         // breakParagraph call should return a bool if it inserts the
1501         // paragraph before or behind and we should react on that one
1502         // but we can fix this in 1.3.0 (Jug 20020509)
1503         bool const isempty = (cursor.par()->allowEmpty() && cursor.par()->empty());
1504         ::breakParagraph(bv()->buffer()->params, paragraphs, cursor.par(), 
1505                          cursor.pos(), keep_layout);
1506
1507         // well this is the caption hack since one caption is really enough
1508         if (layout->labeltype == LABEL_SENSITIVE) {
1509                 if (!cursor.pos())
1510                         // set to standard-layout
1511                         cursor.par()->applyLayout(tclass.defaultLayout());
1512                 else
1513                         // set to standard-layout
1514                         boost::next(cursor.par())->applyLayout(tclass.defaultLayout());
1515         }
1516
1517         // if the cursor is at the beginning of a row without prior newline,
1518         // move one row up!
1519         // This touches only the screen-update. Otherwise we would may have
1520         // an empty row on the screen
1521         if (cursor.pos() && cursorRow()->pos() == cursor.pos()
1522             && !cursorRow()->par()->isNewline(cursor.pos() - 1))
1523         {
1524                 cursorLeft(bv());
1525         }
1526
1527         postPaint();
1528
1529         removeParagraph(cursorRow());
1530
1531         // set the dimensions of the cursor row
1532         cursorRow()->fill(fill(cursorRow(), workWidth()));
1533
1534         setHeightOfRow(cursorRow());
1535
1536 #warning Trouble Point! (Lgb)
1537         // When ::breakParagraph is called from within an inset we must
1538         // ensure that the correct ParagraphList is used. Today that is not
1539         // the case and the Buffer::paragraphs is used. Not good. (Lgb)
1540         ParagraphList::iterator next_par = boost::next(cursor.par());
1541
1542         while (!next_par->empty() && next_par->isNewline(0))
1543                 next_par->erase(0);
1544
1545         insertParagraph(next_par, boost::next(cursorRow()));
1546         updateCounters();
1547
1548         // This check is necessary. Otherwise the new empty paragraph will
1549         // be deleted automatically. And it is more friendly for the user!
1550         if (cursor.pos() || isempty)
1551                 setCursor(next_par, 0);
1552         else
1553                 setCursor(cursor.par(), 0);
1554
1555         if (boost::next(cursorRow()) != rows().end())
1556                 breakAgain(boost::next(cursorRow()));
1557
1558         need_break_row = rows().end();
1559 }
1560
1561
1562 // Just a macro to make some thing easier.
1563 void LyXText::redoParagraph()
1564 {
1565         clearSelection();
1566         redoParagraphs(cursor, boost::next(cursor.par()));
1567         setCursorIntern(cursor.par(), cursor.pos());
1568 }
1569
1570
1571 // insert a character, moves all the following breaks in the
1572 // same Paragraph one to the right and make a rebreak
1573 void LyXText::insertChar(char c)
1574 {
1575         setUndo(bv(), Undo::INSERT, cursor.par());
1576
1577         // When the free-spacing option is set for the current layout,
1578         // disable the double-space checking
1579
1580         bool const freeSpacing = cursorRow()->par()->layout()->free_spacing ||
1581                 cursorRow()->par()->isFreeSpacing();
1582
1583         if (lyxrc.auto_number) {
1584                 static string const number_operators = "+-/*";
1585                 static string const number_unary_operators = "+-";
1586                 static string const number_seperators = ".,:";
1587
1588                 if (current_font.number() == LyXFont::ON) {
1589                         if (!IsDigit(c) && !contains(number_operators, c) &&
1590                             !(contains(number_seperators, c) &&
1591                               cursor.pos() >= 1 &&
1592                               cursor.pos() < cursor.par()->size() &&
1593                               getFont(bv()->buffer(),
1594                                       cursor.par(),
1595                                       cursor.pos()).number() == LyXFont::ON &&
1596                               getFont(bv()->buffer(),
1597                                       cursor.par(),
1598                                       cursor.pos() - 1).number() == LyXFont::ON)
1599                            )
1600                                 number(bv()); // Set current_font.number to OFF
1601                 } else if (IsDigit(c) &&
1602                            real_current_font.isVisibleRightToLeft()) {
1603                         number(bv()); // Set current_font.number to ON
1604
1605                         if (cursor.pos() > 0) {
1606                                 char const c = cursor.par()->getChar(cursor.pos() - 1);
1607                                 if (contains(number_unary_operators, c) &&
1608                                     (cursor.pos() == 1 ||
1609                                      cursor.par()->isSeparator(cursor.pos() - 2) ||
1610                                      cursor.par()->isNewline(cursor.pos() - 2))
1611                                   ) {
1612                                         setCharFont(bv()->buffer(),
1613                                                     cursor.par(),
1614                                                     cursor.pos() - 1,
1615                                                     current_font);
1616                                 } else if (contains(number_seperators, c) &&
1617                                            cursor.pos() >= 2 &&
1618                                            getFont(bv()->buffer(),
1619                                                    cursor.par(),
1620                                                    cursor.pos() - 2).number() == LyXFont::ON) {
1621                                         setCharFont(bv()->buffer(),
1622                                                     cursor.par(),
1623                                                     cursor.pos() - 1,
1624                                                     current_font);
1625                                 }
1626                         }
1627                 }
1628         }
1629
1630
1631         // First check, if there will be two blanks together or a blank at
1632         // the beginning of a paragraph.
1633         // I decided to handle blanks like normal characters, the main
1634         // difference are the special checks when calculating the row.fill
1635         // (blank does not count at the end of a row) and the check here
1636
1637         // The bug is triggered when we type in a description environment:
1638         // The current_font is not changed when we go from label to main text
1639         // and it should (along with realtmpfont) when we type the space.
1640         // CHECK There is a bug here! (Asger)
1641
1642         LyXFont realtmpfont = real_current_font;
1643         LyXFont rawtmpfont = current_font;
1644         // store the current font.  This is because of the use of cursor
1645         // movements. The moving cursor would refresh the current font
1646
1647         // Get the font that is used to calculate the baselineskip
1648         pos_type const lastpos = cursor.par()->size();
1649         LyXFont rawparfont =
1650                 cursor.par()->getFontSettings(bv()->buffer()->params,
1651                                               lastpos - 1);
1652
1653         bool jumped_over_space = false;
1654
1655         if (!freeSpacing && IsLineSeparatorChar(c)) {
1656                 if ((cursor.pos() > 0
1657                      && cursor.par()->isLineSeparator(cursor.pos() - 1))
1658                     || (cursor.pos() > 0
1659                         && cursor.par()->isNewline(cursor.pos() - 1))
1660                     || (cursor.pos() == 0)) {
1661                         static bool sent_space_message = false;
1662                         if (!sent_space_message) {
1663                                 if (cursor.pos() == 0)
1664                                         bv()->owner()->message(_("You cannot insert a space at the beginning of a paragraph. Please read the Tutorial."));
1665                                 else
1666                                         bv()->owner()->message(_("You cannot type two spaces this way. Please read the Tutorial."));
1667                                 sent_space_message = true;
1668                         }
1669                         charInserted();
1670                         return;
1671                 }
1672         }
1673
1674         // the display inset stuff
1675         if (cursorRow()->pos() < cursorRow()->par()->size()
1676             && cursorRow()->par()->isInset(cursorRow()->pos())) {
1677                 Inset * inset = cursorRow()->par()->getInset(cursorRow()->pos());
1678                 if (inset && (inset->display() || inset->needFullRow())) {
1679                         // force a new break
1680                         cursorRow()->fill(-1); // to force a new break
1681                 }
1682         }
1683
1684         // get the cursor row fist
1685         RowList::iterator row = cursorRow();
1686         if (c != Paragraph::META_INSET) {
1687                 // Here case LyXText::InsertInset  already inserted the character
1688                 cursor.par()->insertChar(cursor.pos(), c);
1689         }
1690         setCharFont(bv()->buffer(), cursor.par(), cursor.pos(), rawtmpfont);
1691
1692         if (!jumped_over_space) {
1693                 // refresh the positions
1694                 RowList::iterator tmprow = row;
1695                 while (boost::next(tmprow) != rows().end() &&
1696                        boost::next(tmprow)->par() == row->par()) {
1697                         ++tmprow;
1698                         tmprow->pos(tmprow->pos() + 1);
1699                 }
1700         }
1701
1702         // Is there a break one row above
1703         if (row != rows().begin() &&
1704             boost::prior(row)->par() == row->par()
1705             && (cursor.par()->isLineSeparator(cursor.pos())
1706                 || cursor.par()->isNewline(cursor.pos())
1707                 || ((cursor.pos() + 1 < cursor.par()->size()) &&
1708                     cursor.par()->isInset(cursor.pos() + 1))
1709                 || cursorRow()->fill() == -1))
1710         {
1711                 pos_type z = rowBreakPoint(*boost::prior(row));
1712
1713                 if (z >= row->pos()) {
1714                         row->pos(z + 1);
1715
1716                         // set the dimensions of the row above
1717                         boost::prior(row)->fill(fill(
1718                                                    boost::prior(row),
1719                                                    workWidth()));
1720
1721                         setHeightOfRow(boost::prior(row));
1722
1723                         postPaint();
1724
1725                         breakAgainOneRow(row);
1726
1727                         current_font = rawtmpfont;
1728                         real_current_font = realtmpfont;
1729                         setCursor(cursor.par(), cursor.pos() + 1,
1730                                   false, cursor.boundary());
1731                         // cursor MUST be in row now.
1732
1733                         RowList::iterator next_row = boost::next(row);
1734                         if (next_row != rows().end() &&
1735                             next_row->par() == row->par())
1736                                 need_break_row = next_row;
1737                         else
1738                                 need_break_row = rows().end();
1739
1740                         // check, wether the last characters font has changed.
1741                         if (cursor.pos() && cursor.pos() == cursor.par()->size()
1742                             && rawparfont != rawtmpfont)
1743                                 redoHeightOfParagraph();
1744
1745                         charInserted();
1746                         return;
1747                 }
1748         }
1749
1750         // recalculate the fill of the row
1751         if (row->fill() >= 0) {
1752                 // needed because a newline will set fill to -1. Otherwise
1753                 // we would not get a rebreak!
1754                 row->fill(fill(row, workWidth()));
1755         }
1756
1757         if (c == Paragraph::META_INSET || row->fill() < 0) {
1758                 postPaint();
1759                 breakAgainOneRow(row);
1760
1761                 RowList::iterator next_row = boost::next(row);
1762
1763                 // will the cursor be in another row now?
1764                 if (lastPos(*this, row) <= cursor.pos() + 1 &&
1765                     next_row != rows().end()) {
1766                         if (next_row != rows().end() &&
1767                             next_row->par() == row->par()) {
1768                                 // this should always be true
1769                                 ++row;
1770                         }
1771
1772                         breakAgainOneRow(row);
1773                 }
1774                 current_font = rawtmpfont;
1775                 real_current_font = realtmpfont;
1776
1777                 setCursor(cursor.par(), cursor.pos() + 1, false,
1778                           cursor.boundary());
1779                 if (isBoundary(bv()->buffer(), *cursor.par(), cursor.pos())
1780                     != cursor.boundary())
1781                         setCursor(cursor.par(), cursor.pos(), false,
1782                           !cursor.boundary());
1783
1784                 next_row = boost::next(row);
1785
1786                 if (next_row != rows().end() &&
1787                     next_row->par() == row->par())
1788                         need_break_row = next_row;
1789                 else
1790                         need_break_row = rows().end();
1791         } else {
1792                 // FIXME: similar code is duplicated all over - make resetHeightOfRow
1793                 setHeightOfRow(row);
1794                 postPaint();
1795
1796                 current_font = rawtmpfont;
1797                 real_current_font = realtmpfont;
1798                 setCursor(cursor.par(), cursor.pos() + 1, false,
1799                           cursor.boundary());
1800         }
1801
1802         // check, wether the last characters font has changed.
1803         if (cursor.pos() && cursor.pos() == cursor.par()->size()
1804             && rawparfont != rawtmpfont) {
1805                 redoHeightOfParagraph();
1806         }
1807
1808         charInserted();
1809 }
1810
1811
1812 void LyXText::charInserted()
1813 {
1814         // Here we could call FinishUndo for every 20 characters inserted.
1815         // This is from my experience how emacs does it. (Lgb)
1816         static unsigned int counter;
1817         if (counter < 20) {
1818                 ++counter;
1819         } else {
1820                 finishUndo();
1821                 counter = 0;
1822         }
1823 }
1824
1825
1826 void LyXText::prepareToPrint(RowList::iterator rit, int & x,
1827                              int & fill_separator,
1828                              int & fill_hfill,
1829                              int & fill_label_hfill,
1830                              bool bidi) const
1831 {
1832         int w = rit->fill();
1833         fill_hfill = 0;
1834         fill_label_hfill = 0;
1835         fill_separator = 0;
1836         fill_label_hfill = 0;
1837
1838         ParagraphList::iterator pit = rit->par();
1839
1840         bool const is_rtl =
1841                 pit->isRightToLeftPar(bv()->buffer()->params);
1842         if (is_rtl)
1843                 x = workWidth() > 0 ? rightMargin(*bv()->buffer(), *rit) : 0;
1844         else
1845                 x = workWidth() > 0 ? leftMargin(*rit) : 0;
1846
1847         // is there a manual margin with a manual label
1848         LyXLayout_ptr const & layout = pit->layout();
1849
1850         if (layout->margintype == MARGIN_MANUAL
1851             && layout->labeltype == LABEL_MANUAL) {
1852                 /// We might have real hfills in the label part
1853                 int nlh = numberOfLabelHfills(*this, rit);
1854
1855                 // A manual label par (e.g. List) has an auto-hfill
1856                 // between the label text and the body of the
1857                 // paragraph too.
1858                 // But we don't want to do this auto hfill if the par
1859                 // is empty.
1860                 if (!pit->empty())
1861                         ++nlh;
1862
1863                 if (nlh && !pit->getLabelWidthString().empty()) {
1864                         fill_label_hfill = int(labelFill(*rit) / nlh);
1865                 }
1866         }
1867
1868         // are there any hfills in the row?
1869         int const nh = numberOfHfills(*this, rit);
1870
1871         if (nh) {
1872                 if (w > 0)
1873                         fill_hfill = w / nh;
1874         // we don't have to look at the alignment if it is ALIGN_LEFT and
1875         // if the row is already larger then the permitted width as then
1876         // we force the LEFT_ALIGN'edness!
1877         } else if (int(rit->width()) < workWidth()) {
1878                 // is it block, flushleft or flushright?
1879                 // set x how you need it
1880                 int align;
1881                 if (pit->params().align() == LYX_ALIGN_LAYOUT) {
1882                         align = layout->align;
1883                 } else {
1884                         align = pit->params().align();
1885                 }
1886
1887                 // center displayed insets
1888                 Inset * inset = 0;
1889                 if (rit->pos() < pit->size()
1890                     && pit->isInset(rit->pos())
1891                     && (inset = pit->getInset(rit->pos()))
1892                     && (inset->display())) // || (inset->scroll() < 0)))
1893                     align = (inset->lyxCode() == Inset::MATHMACRO_CODE)
1894                         ? LYX_ALIGN_BLOCK : LYX_ALIGN_CENTER;
1895                 // ERT insets should always be LEFT ALIGNED on screen
1896                 inset = pit->inInset();
1897                 if (inset && inset->owner() &&
1898                         inset->owner()->lyxCode() == Inset::ERT_CODE)
1899                 {
1900                         align = LYX_ALIGN_LEFT;
1901                 }
1902
1903                 switch (align) {
1904             case LYX_ALIGN_BLOCK:
1905             {
1906                         int const ns = numberOfSeparators(*this, rit);
1907                         RowList::iterator next_row = boost::next(rit);
1908                         ParagraphList::iterator next_pit = next_row->par();
1909
1910                         if (ns && next_row != rowlist_.end() &&
1911                             next_pit == pit &&
1912                             !(next_pit->isNewline(next_row->pos() - 1))
1913                             && !(next_pit->isInset(next_row->pos()) &&
1914                                  next_pit->getInset(next_row->pos()) &&
1915                                  next_pit->getInset(next_row->pos())->display())
1916                                 ) {
1917                                 fill_separator = w / ns;
1918                         } else if (is_rtl) {
1919                                 x += w;
1920                         }
1921                         break;
1922             }
1923             case LYX_ALIGN_RIGHT:
1924                         x += w;
1925                         break;
1926             case LYX_ALIGN_CENTER:
1927                         x += w / 2;
1928                         break;
1929                 }
1930         }
1931         if (!bidi)
1932                 return;
1933
1934         computeBidiTables(bv()->buffer(), rit);
1935         if (is_rtl) {
1936                 pos_type body_pos = pit->beginningOfBody();
1937                 pos_type last = lastPos(*this, rit);
1938
1939                 if (body_pos > 0 &&
1940                     (body_pos - 1 > last ||
1941                      !pit->isLineSeparator(body_pos - 1))) {
1942                         x += font_metrics::width(layout->labelsep,
1943                                             getLabelFont(bv()->buffer(),
1944                                                          pit));
1945                         if (body_pos - 1 <= last)
1946                                 x += fill_label_hfill;
1947                 }
1948         }
1949 }
1950
1951
1952 // important for the screen
1953
1954
1955 // the cursor set functions have a special mechanism. When they
1956 // realize, that you left an empty paragraph, they will delete it.
1957 // They also delete the corresponding row
1958
1959 void LyXText::cursorRightOneWord()
1960 {
1961         ::cursorRightOneWord(cursor, ownerParagraphs());
1962         setCursor(cursor.par(), cursor.pos());
1963 }
1964
1965
1966 // Skip initial whitespace at end of word and move cursor to *start*
1967 // of prior word, not to end of next prior word.
1968 void LyXText::cursorLeftOneWord()
1969 {
1970         LyXCursor tmpcursor = cursor;
1971         ::cursorLeftOneWord(tmpcursor, ownerParagraphs());
1972         setCursor(tmpcursor.par(), tmpcursor.pos());
1973 }
1974
1975
1976 void LyXText::selectWord(word_location loc)
1977 {
1978         LyXCursor from = cursor;
1979         LyXCursor to;
1980         ::getWord(from, to, loc, ownerParagraphs());
1981         if (cursor != from)
1982                 setCursor(from.par(), from.pos());
1983         if (to == from)
1984                 return;
1985         selection.cursor = cursor;
1986         setCursor(to.par(), to.pos());
1987         setSelection();
1988 }
1989
1990
1991 // Select the word currently under the cursor when no
1992 // selection is currently set
1993 bool LyXText::selectWordWhenUnderCursor(word_location loc)
1994 {
1995         if (!selection.set()) {
1996                 selectWord(loc);
1997                 return selection.set();
1998         }
1999         return false;
2000 }
2001
2002
2003 void LyXText::acceptChange()
2004 {
2005         if (!selection.set() && cursor.par()->size())
2006                 return;
2007
2008         if (selection.start.par() == selection.end.par()) {
2009                 LyXCursor & startc = selection.start;
2010                 LyXCursor & endc = selection.end;
2011                 setUndo(bv(), Undo::INSERT, startc.par());
2012                 startc.par()->acceptChange(startc.pos(), endc.pos());
2013                 finishUndo();
2014                 clearSelection();
2015                 redoParagraphs(startc, boost::next(startc.par()));
2016                 setCursorIntern(startc.par(), 0);
2017         }
2018 #warning handle multi par selection
2019 }
2020
2021
2022 void LyXText::rejectChange()
2023 {
2024         if (!selection.set() && cursor.par()->size())
2025                 return;
2026
2027         if (selection.start.par() == selection.end.par()) {
2028                 LyXCursor & startc = selection.start;
2029                 LyXCursor & endc = selection.end;
2030                 setUndo(bv(), Undo::INSERT, startc.par());
2031                 startc.par()->rejectChange(startc.pos(), endc.pos());
2032                 finishUndo();
2033                 clearSelection();
2034                 redoParagraphs(startc, boost::next(startc.par()));
2035                 setCursorIntern(startc.par(), 0);
2036         }
2037 #warning handle multi par selection
2038 }
2039
2040
2041 // This function is only used by the spellchecker for NextWord().
2042 // It doesn't handle LYX_ACCENTs and probably never will.
2043 WordLangTuple const
2044 LyXText::selectNextWordToSpellcheck(float & value)
2045 {
2046         if (the_locking_inset) {
2047                 WordLangTuple word = the_locking_inset->selectNextWordToSpellcheck(bv(), value);
2048                 if (!word.word().empty()) {
2049                         value += float(cursor.y());
2050                         value /= float(height);
2051                         return word;
2052                 }
2053                 // we have to go on checking so move cursor to the next char
2054                 if (cursor.pos() == cursor.par()->size()) {
2055                         if (boost::next(cursor.par()) == ownerParagraphs().end())
2056                                 return word;
2057                         cursor.par(boost::next(cursor.par()));
2058                         cursor.pos(0);
2059                 } else
2060                         cursor.pos(cursor.pos() + 1);
2061         }
2062         ParagraphList::iterator tmppit = cursor.par();
2063
2064         // If this is not the very first word, skip rest of
2065         // current word because we are probably in the middle
2066         // of a word if there is text here.
2067         if (cursor.pos() || cursor.par() != ownerParagraphs().begin()) {
2068                 while (cursor.pos() < cursor.par()->size()
2069                        && cursor.par()->isLetter(cursor.pos()))
2070                         cursor.pos(cursor.pos() + 1);
2071         }
2072
2073         // Now, skip until we have real text (will jump paragraphs)
2074         while (true) {
2075                 ParagraphList::iterator cpit = cursor.par();
2076                 pos_type const cpos(cursor.pos());
2077
2078                 if (cpos == cpit->size()) {
2079                         if (boost::next(cpit) != ownerParagraphs().end()) {
2080                                 cursor.par(boost::next(cpit));
2081                                 cursor.pos(0);
2082                                 continue;
2083                         }
2084                         break;
2085                 }
2086
2087                 bool const is_good_inset = cpit->isInset(cpos)
2088                         && cpit->getInset(cpos)->allowSpellcheck();
2089
2090                 if (!isDeletedText(*cpit, cpos)
2091                     && (is_good_inset || cpit->isLetter(cpos)))
2092                         break;
2093
2094                 cursor.pos(cpos + 1);
2095         }
2096
2097         // now check if we hit an inset so it has to be a inset containing text!
2098         if (cursor.pos() < cursor.par()->size() &&
2099             cursor.par()->isInset(cursor.pos())) {
2100                 // lock the inset!
2101                 FuncRequest cmd(bv(), LFUN_INSET_EDIT, "left");
2102                 cursor.par()->getInset(cursor.pos())->localDispatch(cmd);
2103                 // now call us again to do the above trick
2104                 // but obviously we have to start from down below ;)
2105                 return bv()->text->selectNextWordToSpellcheck(value);
2106         }
2107
2108         // Update the value if we changed paragraphs
2109         if (cursor.par() != tmppit) {
2110                 setCursor(cursor.par(), cursor.pos());
2111                 value = float(cursor.y())/float(height);
2112         }
2113
2114         // Start the selection from here
2115         selection.cursor = cursor;
2116
2117         string lang_code(
2118                 getFont(bv()->buffer(), cursor.par(), cursor.pos())
2119                         .language()->code());
2120         // and find the end of the word (insets like optional hyphens
2121         // and ligature break are part of a word)
2122         while (cursor.pos() < cursor.par()->size()
2123                && cursor.par()->isLetter(cursor.pos())
2124                && !isDeletedText(*cursor.par(), cursor.pos()))
2125                 cursor.pos(cursor.pos() + 1);
2126
2127         // Finally, we copy the word to a string and return it
2128         string str;
2129         if (selection.cursor.pos() < cursor.pos()) {
2130                 pos_type i;
2131                 for (i = selection.cursor.pos(); i < cursor.pos(); ++i) {
2132                         if (!cursor.par()->isInset(i))
2133                                 str += cursor.par()->getChar(i);
2134                 }
2135         }
2136         return WordLangTuple(str, lang_code);
2137 }
2138
2139
2140 // This one is also only for the spellchecker
2141 void LyXText::selectSelectedWord()
2142 {
2143         if (the_locking_inset) {
2144                 the_locking_inset->selectSelectedWord(bv());
2145                 return;
2146         }
2147         // move cursor to the beginning
2148         setCursor(selection.cursor.par(), selection.cursor.pos());
2149
2150         // set the sel cursor
2151         selection.cursor = cursor;
2152
2153         // now find the end of the word
2154         while (cursor.pos() < cursor.par()->size()
2155                && (cursor.par()->isLetter(cursor.pos())))
2156                 cursor.pos(cursor.pos() + 1);
2157
2158         setCursor(cursor.par(), cursor.pos());
2159
2160         // finally set the selection
2161         setSelection();
2162 }
2163
2164
2165 // Delete from cursor up to the end of the current or next word.
2166 void LyXText::deleteWordForward()
2167 {
2168         if (cursor.par()->empty())
2169                 cursorRight(bv());
2170         else {
2171                 LyXCursor tmpcursor = cursor;
2172                 selection.set(true); // to avoid deletion
2173                 cursorRightOneWord();
2174                 setCursor(tmpcursor, tmpcursor.par(), tmpcursor.pos());
2175                 selection.cursor = cursor;
2176                 cursor = tmpcursor;
2177                 setSelection();
2178
2179                 // Great, CutSelection() gets rid of multiple spaces.
2180                 cutSelection(true, false);
2181         }
2182 }
2183
2184
2185 // Delete from cursor to start of current or prior word.
2186 void LyXText::deleteWordBackward()
2187 {
2188         if (cursor.par()->empty())
2189                 cursorLeft(bv());
2190         else {
2191                 LyXCursor tmpcursor = cursor;
2192                 selection.set(true); // to avoid deletion
2193                 cursorLeftOneWord();
2194                 setCursor(tmpcursor, tmpcursor.par(), tmpcursor.pos());
2195                 selection.cursor = cursor;
2196                 cursor = tmpcursor;
2197                 setSelection();
2198                 cutSelection(true, false);
2199         }
2200 }
2201
2202
2203 // Kill to end of line.
2204 void LyXText::deleteLineForward()
2205 {
2206         if (cursor.par()->empty())
2207                 // Paragraph is empty, so we just go to the right
2208                 cursorRight(bv());
2209         else {
2210                 LyXCursor tmpcursor = cursor;
2211                 // We can't store the row over a regular setCursor
2212                 // so we set it to 0 and reset it afterwards.
2213                 selection.set(true); // to avoid deletion
2214                 cursorEnd();
2215                 setCursor(tmpcursor, tmpcursor.par(), tmpcursor.pos());
2216                 selection.cursor = cursor;
2217                 cursor = tmpcursor;
2218                 setSelection();
2219                 // What is this test for ??? (JMarc)
2220                 if (!selection.set()) {
2221                         deleteWordForward();
2222                 } else {
2223                         cutSelection(true, false);
2224                 }
2225         }
2226 }
2227
2228
2229 void LyXText::changeCase(LyXText::TextCase action)
2230 {
2231         LyXCursor from;
2232         LyXCursor to;
2233
2234         if (selection.set()) {
2235                 from = selection.start;
2236                 to = selection.end;
2237         } else {
2238                 from = cursor;
2239                 ::getWord(from, to, lyx::PARTIAL_WORD, ownerParagraphs());
2240                 setCursor(to.par(), to.pos() + 1);
2241         }
2242
2243         setUndo(bv(), Undo::FINISH, from.par(), to.par());
2244
2245         pos_type pos = from.pos();
2246         ParagraphList::iterator pit = from.par();
2247
2248         while (pit != ownerParagraphs().end() &&
2249                (pos != to.pos() || pit != to.par())) {
2250                 if (pos == pit->size()) {
2251                         ++pit;
2252                         pos = 0;
2253                         continue;
2254                 }
2255                 unsigned char c = pit->getChar(pos);
2256                 if (!IsInsetChar(c)) {
2257                         switch (action) {
2258                         case text_lowercase:
2259                                 c = lowercase(c);
2260                                 break;
2261                         case text_capitalization:
2262                                 c = uppercase(c);
2263                                 action = text_lowercase;
2264                                 break;
2265                         case text_uppercase:
2266                                 c = uppercase(c);
2267                                 break;
2268                         }
2269                 }
2270 #warning changes
2271                 pit->setChar(pos, c);
2272                 checkParagraph(pit, pos);
2273
2274                 ++pos;
2275         }
2276
2277         if (getRow(to) != getRow(from))
2278                 postPaint();
2279 }
2280
2281
2282 void LyXText::Delete()
2283 {
2284         // this is a very easy implementation
2285
2286         LyXCursor old_cursor = cursor;
2287         int const old_cur_par_id = old_cursor.par()->id();
2288         int const old_cur_par_prev_id =
2289                 (old_cursor.par() != ownerParagraphs().begin() ?
2290                  boost::prior(old_cursor.par())->id() : -1);
2291
2292         // just move to the right
2293         cursorRight(bv());
2294
2295         // CHECK Look at the comment here.
2296         // This check is not very good...
2297         // The cursorRightIntern calls DeleteEmptyParagrapgMechanism
2298         // and that can very well delete the par or par->previous in
2299         // old_cursor. Will a solution where we compare paragraph id's
2300         //work better?
2301         if ((cursor.par() != ownerParagraphs().begin() ? boost::prior(cursor.par())->id() : -1)
2302             == old_cur_par_prev_id
2303             && cursor.par()->id() != old_cur_par_id) {
2304                 // delete-empty-paragraph-mechanism has done it
2305                 return;
2306         }
2307
2308         // if you had success make a backspace
2309         if (old_cursor.par() != cursor.par() || old_cursor.pos() != cursor.pos()) {
2310                 LyXCursor tmpcursor = cursor;
2311                 // to make sure undo gets the right cursor position
2312                 cursor = old_cursor;
2313                 setUndo(bv(), Undo::DELETE, cursor.par());
2314                 cursor = tmpcursor;
2315                 backspace();
2316         }
2317 }
2318
2319
2320 void LyXText::backspace()
2321 {
2322         // Get the font that is used to calculate the baselineskip
2323         pos_type lastpos = cursor.par()->size();
2324         LyXFont rawparfont =
2325                 cursor.par()->getFontSettings(bv()->buffer()->params,
2326                                               lastpos - 1);
2327
2328         if (cursor.pos() == 0) {
2329                 // The cursor is at the beginning of a paragraph,
2330                 // so the the backspace will collapse two paragraphs into one.
2331
2332                 // but it's not allowed unless it's new
2333                 if (cursor.par()->isChangeEdited(0, cursor.par()->size()))
2334                         return;
2335
2336                 // we may paste some paragraphs
2337
2338                 // is it an empty paragraph?
2339
2340                 if ((lastpos == 0
2341                      || (lastpos == 1 && cursor.par()->isSeparator(0)))) {
2342                         // This is an empty paragraph and we delete it just by moving the cursor one step
2343                         // left and let the DeleteEmptyParagraphMechanism handle the actual deletion
2344                         // of the paragraph.
2345
2346                         if (cursor.par() != ownerParagraphs().begin()) {
2347                                 ParagraphList::iterator tmppit = boost::prior(cursor.par());
2348                                 if (cursor.par()->layout() == tmppit->layout()
2349                                     && cursor.par()->getAlign() == tmppit->getAlign()) {
2350                                         // Inherit bottom DTD from the paragraph below.
2351                                         // (the one we are deleting)
2352                                         tmppit->params().lineBottom(cursor.par()->params().lineBottom());
2353                                         tmppit->params().spaceBottom(cursor.par()->params().spaceBottom());
2354                                         tmppit->params().pagebreakBottom(cursor.par()->params().pagebreakBottom());
2355                                 }
2356
2357                                 cursorLeft(bv());
2358
2359                                 // the layout things can change the height of a row !
2360                                 int const tmpheight = cursorRow()->height();
2361                                 setHeightOfRow(cursorRow());
2362                                 if (cursorRow()->height() != tmpheight)
2363                                         postPaint();
2364                                 return;
2365                         }
2366                 }
2367
2368                 if (cursor.par() != ownerParagraphs().begin()) {
2369                         setUndo(bv(), Undo::DELETE,
2370                                 boost::prior(cursor.par()),
2371                                 cursor.par());
2372                 }
2373
2374                 ParagraphList::iterator tmppit = cursor.par();
2375                 RowList::iterator tmprow = cursorRow();
2376
2377                 // We used to do cursorLeftIntern() here, but it is
2378                 // not a good idea since it triggers the auto-delete
2379                 // mechanism. So we do a cursorLeftIntern()-lite,
2380                 // without the dreaded mechanism. (JMarc)
2381                 if (cursor.par() != ownerParagraphs().begin()) {
2382                         // steps into the above paragraph.
2383                         setCursorIntern(boost::prior(cursor.par()),
2384                                         boost::prior(cursor.par())->size(),
2385                                         false);
2386                 }
2387
2388                 // Pasting is not allowed, if the paragraphs have different
2389                 // layout. I think it is a real bug of all other
2390                 // word processors to allow it. It confuses the user.
2391                 // Even so with a footnote paragraph and a non-footnote
2392                 // paragraph. I will not allow pasting in this case,
2393                 // because the user would be confused if the footnote behaves
2394                 // different wether it is open or closed.
2395
2396                 //      Correction: Pasting is always allowed with standard-layout
2397                 LyXTextClass const & tclass =
2398                         bv()->buffer()->params.getLyXTextClass();
2399
2400                 if (cursor.par() != tmppit
2401                     && (cursor.par()->layout() == tmppit->layout()
2402                         || tmppit->layout() == tclass.defaultLayout())
2403                     && cursor.par()->getAlign() == tmppit->getAlign()) {
2404                         removeParagraph(tmprow);
2405                         removeRow(tmprow);
2406                         mergeParagraph(bv()->buffer()->params, bv()->buffer()->paragraphs, cursor.par());
2407
2408                         if (!cursor.pos() || !cursor.par()->isSeparator(cursor.pos() - 1))
2409                                 ; //cursor.par()->insertChar(cursor.pos(), ' ');
2410                         // strangely enough it seems that commenting out the line above removes
2411                         // most or all of the segfaults. I will however also try to move the
2412                         // two Remove... lines in front of the PasteParagraph too.
2413                         else
2414                                 if (cursor.pos())
2415                                         cursor.pos(cursor.pos() - 1);
2416
2417                         postPaint();
2418
2419                         // remove the lost paragraph
2420                         // This one is not safe, since the paragraph that the tmprow and the
2421                         // following rows belong to has been deleted by the PasteParagraph
2422                         // above. The question is... could this be moved in front of the
2423                         // PasteParagraph?
2424                         //RemoveParagraph(tmprow);
2425                         //RemoveRow(tmprow);
2426
2427                         // This rebuilds the rows.
2428                         appendParagraph(cursorRow());
2429                         updateCounters();
2430
2431                         // the row may have changed, block, hfills etc.
2432                         setCursor(cursor.par(), cursor.pos(), false);
2433                 }
2434         } else {
2435                 // this is the code for a normal backspace, not pasting
2436                 // any paragraphs
2437                 setUndo(bv(), Undo::DELETE, cursor.par());
2438                 // We used to do cursorLeftIntern() here, but it is
2439                 // not a good idea since it triggers the auto-delete
2440                 // mechanism. So we do a cursorLeftIntern()-lite,
2441                 // without the dreaded mechanism. (JMarc)
2442                 setCursorIntern(cursor.par(), cursor.pos()- 1,
2443                                 false, cursor.boundary());
2444
2445                 if (cursor.par()->isInset(cursor.pos())) {
2446                         // force complete redo when erasing display insets
2447                         // this is a cruel method but safe..... Matthias
2448                         if (cursor.par()->getInset(cursor.pos())->display() ||
2449                             cursor.par()->getInset(cursor.pos())->needFullRow()) {
2450                                 cursor.par()->erase(cursor.pos());
2451                                 redoParagraph();
2452                                 return;
2453                         }
2454                 }
2455
2456                 RowList::iterator row = cursorRow();
2457                 int y = cursor.y() - row->baseline();
2458                 pos_type z;
2459                 // remember that a space at the end of a row doesnt count
2460                 // when calculating the fill
2461                 if (cursor.pos() < lastPos(*this, row) ||
2462                     !cursor.par()->isLineSeparator(cursor.pos())) {
2463                         row->fill(row->fill() + singleWidth(
2464                                                             cursor.par(),
2465                                                             cursor.pos()));
2466                 }
2467
2468                 // some special code when deleting a newline. This is similar
2469                 // to the behavior when pasting paragraphs
2470                 if (cursor.pos() && cursor.par()->isNewline(cursor.pos())) {
2471                         cursor.par()->erase(cursor.pos());
2472                         // refresh the positions
2473                         RowList::iterator tmprow = row;
2474                         while (boost::next(tmprow) != rows().end() &&
2475                                boost::next(tmprow)->par() == row->par()) {
2476                                 ++tmprow;
2477                                 tmprow->pos(tmprow->pos() - 1);
2478                         }
2479                         if (cursor.par()->isLineSeparator(cursor.pos() - 1))
2480                                 cursor.pos(cursor.pos() - 1);
2481
2482                         if (cursor.pos() < cursor.par()->size()
2483                             && !cursor.par()->isSeparator(cursor.pos())) {
2484                                 cursor.par()->insertChar(cursor.pos(), ' ');
2485                                 setCharFont(bv()->buffer(), cursor.par(),
2486                                             cursor.pos(), current_font);
2487                                 // refresh the positions
2488                                 tmprow = row;
2489                                 while (boost::next(tmprow) != rows().end() &&
2490                                        boost::next(tmprow)->par() == row->par()) {
2491                                         ++tmprow;
2492                                         tmprow->pos(tmprow->pos() + 1);
2493                                 }
2494                         }
2495                 } else {
2496                         cursor.par()->erase(cursor.pos());
2497
2498                         // refresh the positions
2499                         RowList::iterator tmprow = row;
2500                         while (boost::next(tmprow) != rows().end() &&
2501                                boost::next(tmprow)->par() == row->par()) {
2502                                 ++tmprow;
2503                                 tmprow->pos(tmprow->pos() - 1);
2504                         }
2505
2506                         // delete newlines at the beginning of paragraphs
2507                         while (!cursor.par()->empty() &&
2508                                cursor.pos() < cursor.par()->size() &&
2509                                cursor.par()->isNewline(cursor.pos()) &&
2510                                cursor.pos() == cursor.par()->beginningOfBody()) {
2511                                 cursor.par()->erase(cursor.pos());
2512                                 // refresh the positions
2513                                 tmprow = row;
2514                                 while (boost::next(tmprow) != rows().end() &&
2515                                        boost::next(tmprow)->par() == row->par()) {
2516                                         ++tmprow;
2517                                         tmprow->pos(tmprow->pos() - 1);
2518                                 }
2519                         }
2520                 }
2521
2522                 // is there a break one row above
2523                 if (row != rows().begin() && boost::prior(row)->par() == row->par()) {
2524                         z = rowBreakPoint(*boost::prior(row));
2525                         if (z >= row->pos()) {
2526                                 row->pos(z + 1);
2527
2528                                 RowList::iterator tmprow = boost::prior(row);
2529
2530                                 // maybe the current row is now empty
2531                                 if (row->pos() >= row->par()->size()) {
2532                                         // remove it
2533                                         removeRow(row);
2534                                         need_break_row = rows().end();
2535                                 } else {
2536                                         breakAgainOneRow(row);
2537                                         if (boost::next(row) != rows().end() &&
2538                                             boost::next(row)->par() == row->par())
2539                                                 need_break_row = boost::next(row);
2540                                         else
2541                                                 need_break_row = rows().end();
2542                                 }
2543
2544                                 // set the dimensions of the row above
2545                                 y -= tmprow->height();
2546                                 tmprow->fill(fill(tmprow, workWidth()));
2547                                 setHeightOfRow(tmprow);
2548                                 postPaint();
2549
2550                                 setCursor(cursor.par(), cursor.pos(),
2551                                           false, cursor.boundary());
2552                                 //current_font = rawtmpfont;
2553                                 //real_current_font = realtmpfont;
2554                                 // check, whether the last character's font has changed.
2555                                 if (rawparfont !=
2556                                     cursor.par()->getFontSettings(bv()->buffer()->params,
2557                                                                   cursor.par()->size() - 1))
2558                                         redoHeightOfParagraph();
2559                                 return;
2560                         }
2561                 }
2562
2563                 // break the cursor row again
2564                 if (boost::next(row) != rows().end() &&
2565                     boost::next(row)->par() == row->par() &&
2566                     (lastPos(*this, row) == row->par()->size() - 1 ||
2567                      rowBreakPoint(*row) != lastPos(*this, row))) {
2568
2569                         // it can happen that a paragraph loses one row
2570                         // without a real breakup. This is when a word
2571                         // is to long to be broken. Well, I don t care this
2572                         // hack ;-)
2573                         if (lastPos(*this, row) == row->par()->size() - 1)
2574                                 removeRow(boost::next(row));
2575
2576                         postPaint();
2577
2578                         breakAgainOneRow(row);
2579                         // will the cursor be in another row now?
2580                         if (boost::next(row) != rows().end() &&
2581                             boost::next(row)->par() == row->par() &&
2582                             lastPos(*this, row) <= cursor.pos()) {
2583                                 ++row;
2584                                 breakAgainOneRow(row);
2585                         }
2586
2587                         setCursor(cursor.par(), cursor.pos(), false, cursor.boundary());
2588
2589                         if (boost::next(row) != rows().end() &&
2590                             boost::next(row)->par() == row->par())
2591                                 need_break_row = boost::next(row);
2592                         else
2593                                 need_break_row = rows().end();
2594                 } else  {
2595                         // set the dimensions of the row
2596                         row->fill(fill(row, workWidth()));
2597                         setHeightOfRow(row);
2598                         postPaint();
2599                         setCursor(cursor.par(), cursor.pos(), false, cursor.boundary());
2600                 }
2601         }
2602
2603         // current_font = rawtmpfont;
2604         // real_current_font = realtmpfont;
2605
2606         if (isBoundary(bv()->buffer(), *cursor.par(), cursor.pos())
2607             != cursor.boundary())
2608                 setCursor(cursor.par(), cursor.pos(), false,
2609                           !cursor.boundary());
2610
2611         lastpos = cursor.par()->size();
2612         if (cursor.pos() == lastpos)
2613                 setCurrentFont();
2614
2615         // check, whether the last characters font has changed.
2616         if (rawparfont !=
2617             cursor.par()->getFontSettings(bv()->buffer()->params, lastpos - 1)) {
2618                 redoHeightOfParagraph();
2619         }
2620 }
2621
2622
2623 RowList::iterator LyXText::cursorRow() const
2624 {
2625         return getRow(cursor.par(), cursor.pos());
2626 }
2627
2628
2629 RowList::iterator LyXText::getRow(LyXCursor const & cur) const
2630 {
2631         return getRow(cur.par(), cur.pos());
2632 }
2633
2634
2635 RowList::iterator
2636 LyXText::getRow(ParagraphList::iterator pit, pos_type pos) const
2637 {
2638         if (rows().empty())
2639                 return rowlist_.end();
2640
2641         // find the first row of the specified paragraph
2642         RowList::iterator rit = rowlist_.begin();
2643         RowList::iterator end = rowlist_.end();
2644         while (boost::next(rit) != end && rit->par() != pit) {
2645                 ++rit;
2646         }
2647
2648         // now find the wanted row
2649         while (rit->pos() < pos
2650                && boost::next(rit) != end
2651                && boost::next(rit)->par() == pit
2652                && boost::next(rit)->pos() <= pos) {
2653                 ++rit;
2654         }
2655
2656         return rit;
2657 }
2658
2659
2660 // returns pointer to a specified row
2661 RowList::iterator
2662 LyXText::getRow(ParagraphList::iterator pit, pos_type pos, int & y) const
2663 {
2664         y = 0;
2665
2666         if (rows().empty())
2667                 return rowlist_.end();
2668
2669         // find the first row of the specified paragraph
2670         RowList::iterator rit = rowlist_.begin();
2671         RowList::iterator end = rowlist_.end();
2672         while (boost::next(rit) != end && rit->par() != pit) {
2673                 y += rit->height();
2674                 ++rit;
2675         }
2676
2677         // now find the wanted row
2678         while (rit->pos() < pos
2679                && boost::next(rit) != end
2680                && boost::next(rit)->par() == pit
2681                && boost::next(rit)->pos() <= pos) {
2682                 y += rit->height();
2683                 ++rit;
2684         }
2685
2686         return rit;
2687 }
2688
2689
2690 // returns pointer to some fancy row 'below' specified row
2691 RowList::iterator LyXText::cursorIRow() const
2692 {
2693         int y = 0;
2694         return getRow(cursor.par(), cursor.pos(), y);
2695 }
2696
2697
2698 RowList::iterator LyXText::getRowNearY(int & y) const
2699 {
2700         RowList::iterator rit = anchor_row_;
2701         RowList::iterator const beg = rows().begin();
2702         RowList::iterator const end = rows().end();
2703
2704         if (rows().empty()) {
2705                 y = 0;
2706                 return end;
2707         }
2708         if (rit == end)
2709                 rit = beg;
2710
2711         int tmpy = rit->y();
2712
2713         if (tmpy <= y) {
2714                 while (rit != end && tmpy <= y) {
2715                         tmpy += rit->height();
2716                         ++rit;
2717                 }
2718                 if (rit != beg) {
2719                         --rit;
2720                         tmpy -= rit->height();
2721                 }
2722         } else {
2723                 while (rit != beg && tmpy > y) {
2724                         --rit;
2725                         tmpy -= rit->height();
2726                 }
2727         }
2728         if (tmpy < 0 || rit == end) {
2729                 tmpy = 0;
2730                 rit = beg;
2731         }
2732
2733         // return the rel y
2734         y = tmpy;
2735
2736         return rit;
2737 }
2738
2739
2740 int LyXText::getDepth() const
2741 {
2742         return cursor.par()->getDepth();
2743 }