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