]> git.lyx.org Git - lyx.git/blob - src/text2.C
fix "height grows when moving in tables" bug
[lyx.git] / src / text2.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 "LString.h"
15 #include "Lsstream.h"
16 #include "paragraph.h"
17 #include "funcrequest.h"
18 #include "frontends/LyXView.h"
19 #include "undo_funcs.h"
20 #include "buffer.h"
21 #include "buffer_funcs.h"
22 #include "bufferparams.h"
23 #include "errorlist.h"
24 #include "gettext.h"
25 #include "BufferView.h"
26 #include "CutAndPaste.h"
27 #include "frontends/Painter.h"
28 #include "frontends/font_metrics.h"
29 #include "debug.h"
30 #include "lyxrc.h"
31 #include "lyxrow.h"
32 #include "FloatList.h"
33 #include "language.h"
34 #include "ParagraphParameters.h"
35 #include "counters.h"
36 #include "lyxrow_funcs.h"
37 #include "metricsinfo.h"
38 #include "paragraph_funcs.h"
39
40 #include "insets/insetbibitem.h"
41 #include "insets/insetenv.h"
42 #include "insets/insetfloat.h"
43 #include "insets/insetwrap.h"
44
45 #include "support/LAssert.h"
46 #include "support/textutils.h"
47 #include "support/lstrings.h"
48
49 #include <boost/tuple/tuple.hpp>
50
51 #include <algorithm>
52
53 using namespace lyx::support;
54
55 using std::vector;
56 using std::copy;
57 using std::endl;
58 using std::find;
59 using std::pair;
60 using lyx::pos_type;
61
62
63 LyXText::LyXText(BufferView * bv)
64         : height(0), width(0), anchor_row_offset_(0),
65           inset_owner(0), the_locking_inset(0), bv_owner(bv)
66 {
67         anchor_row_ = rows().end();
68 }
69
70
71 LyXText::LyXText(BufferView * bv, InsetText * inset)
72         : height(0), width(0), anchor_row_offset_(0),
73           inset_owner(inset), the_locking_inset(0), bv_owner(bv)
74 {
75         anchor_row_ = rows().end();
76 }
77
78
79 void LyXText::init(BufferView * bview)
80 {
81         bv_owner = bview;
82
83         rowlist_.clear();
84         width = 0;
85         height = 0;
86
87         anchor_row_ = rows().end();
88         anchor_row_offset_ = 0;
89
90         current_font = getFont(ownerParagraphs().begin(), 0);
91
92         redoParagraphs(ownerParagraphs().begin(), ownerParagraphs().end());
93
94         setCursorIntern(rowlist_.begin()->par(), 0);
95         selection.cursor = cursor;
96
97         updateCounters();
98 }
99
100
101 // Gets the fully instantiated font at a given position in a paragraph
102 // Basically the same routine as Paragraph::getFont() in paragraph.C.
103 // The difference is that this one is used for displaying, and thus we
104 // are allowed to make cosmetic improvements. For instance make footnotes
105 // smaller. (Asger)
106 // If position is -1, we get the layout font of the paragraph.
107 // If position is -2, we get the font of the manual label of the paragraph.
108 LyXFont LyXText::getFont(ParagraphList::iterator pit, pos_type pos) const
109 {
110         Assert(pos >= 0);
111
112         LyXLayout_ptr const & layout = pit->layout();
113 #warning broken?
114         BufferParams const & params = bv()->buffer()->params;
115
116         // We specialize the 95% common case:
117         if (!pit->getDepth()) {
118                 if (layout->labeltype == LABEL_MANUAL
119                     && pos < pit->beginningOfBody()) {
120                         // 1% goes here
121                         LyXFont f = pit->getFontSettings(params, pos);
122                         if (pit->inInset())
123                                 pit->inInset()->getDrawFont(f);
124                         return f.realize(layout->reslabelfont);
125                 } else {
126                         LyXFont f = pit->getFontSettings(params, pos);
127                         if (pit->inInset())
128                                 pit->inInset()->getDrawFont(f);
129                         return f.realize(layout->resfont);
130                 }
131         }
132
133         // The uncommon case need not be optimized as much
134
135         LyXFont layoutfont;
136
137         if (pos < pit->beginningOfBody()) {
138                 // 1% goes here
139                 layoutfont = layout->labelfont;
140         } else {
141                 // 99% goes here
142                 layoutfont = layout->font;
143         }
144
145         LyXFont tmpfont = pit->getFontSettings(params, pos);
146         tmpfont.realize(layoutfont);
147
148         if (pit->inInset())
149                 pit->inInset()->getDrawFont(tmpfont);
150
151         // Realize with the fonts of lesser depth.
152         tmpfont.realize(outerFont(pit, ownerParagraphs()));
153         tmpfont.realize(defaultfont_);
154
155         return tmpfont;
156 }
157
158
159 LyXFont LyXText::getLayoutFont(ParagraphList::iterator pit) const
160 {
161         LyXLayout_ptr const & layout = pit->layout();
162
163         if (!pit->getDepth())
164                 return layout->resfont;
165
166         LyXFont font = layout->font;
167         // Realize with the fonts of lesser depth.
168         font.realize(outerFont(pit, ownerParagraphs()));
169         font.realize(defaultfont_);
170
171         return font;
172 }
173
174
175 LyXFont LyXText::getLabelFont(ParagraphList::iterator pit) const
176 {
177         LyXLayout_ptr const & layout = pit->layout();
178
179         if (!pit->getDepth())
180                 return layout->reslabelfont;
181
182         LyXFont font = layout->labelfont;
183         // Realize with the fonts of lesser depth.
184         font.realize(outerFont(pit, ownerParagraphs()));
185         font.realize(defaultfont_);
186
187         return font;
188 }
189
190
191 void LyXText::setCharFont(ParagraphList::iterator pit,
192                           pos_type pos, LyXFont const & fnt,
193                           bool toggleall)
194 {
195         BufferParams const & params = bv()->buffer()->params;
196         LyXFont font = getFont(pit, pos);
197         font.update(fnt, params.language, toggleall);
198         // Let the insets convert their font
199         if (pit->isInset(pos)) {
200                 InsetOld * inset = pit->getInset(pos);
201                 if (isEditableInset(inset)) {
202                         static_cast<UpdatableInset *>(inset)
203                                 ->setFont(bv(), fnt, toggleall, true);
204                 }
205         }
206
207         // Plug through to version below:
208         setCharFont(pit, pos, font);
209 }
210
211
212 void LyXText::setCharFont(
213         ParagraphList::iterator pit, pos_type pos, LyXFont const & fnt)
214 {
215         LyXFont font = fnt;
216         LyXLayout_ptr const & layout = pit->layout();
217
218         // Get concrete layout font to reduce against
219         LyXFont layoutfont;
220
221         if (pos < pit->beginningOfBody())
222                 layoutfont = layout->labelfont;
223         else
224                 layoutfont = layout->font;
225
226         // Realize against environment font information
227         if (pit->getDepth()) {
228                 ParagraphList::iterator tp = pit;
229                 while (!layoutfont.resolved() &&
230                        tp != ownerParagraphs().end() &&
231                        tp->getDepth()) {
232                         tp = outerHook(tp, ownerParagraphs());
233                         if (tp != ownerParagraphs().end())
234                                 layoutfont.realize(tp->layout()->font);
235                 }
236         }
237
238         layoutfont.realize(defaultfont_);
239
240         // Now, reduce font against full layout font
241         font.reduce(layoutfont);
242
243         pit->setFont(pos, font);
244 }
245
246
247 // removes the row and reset the touched counters
248 void LyXText::removeRow(RowList::iterator rit)
249 {
250         if (anchor_row_ == rit) {
251                 if (rit != rows().begin()) {
252                         anchor_row_ = boost::prior(rit);
253                         anchor_row_offset_ += anchor_row_->height();
254                 } else {
255                         anchor_row_ = boost::next(rit);
256                         anchor_row_offset_ -= rit->height();
257                 }
258         }
259
260         // the text becomes smaller
261         height -= rit->height();
262
263         rowlist_.erase(rit);
264 }
265
266
267 // remove all following rows of the paragraph of the specified row.
268 void LyXText::removeParagraph(RowList::iterator rit)
269 {
270         ParagraphList::iterator tmppit = rit->par();
271         ++rit;
272
273         while (rit != rows().end() && rit->par() == tmppit) {
274                 RowList::iterator tmprit = boost::next(rit);
275                 removeRow(rit);
276                 rit = tmprit;
277         }
278 }
279
280
281 void LyXText::insertParagraph(ParagraphList::iterator pit,
282                               RowList::iterator rit)
283 {
284         // insert a new row, starting at position 0
285         rit = rowlist_.insert(rit, Row(pit, 0));
286
287         // and now append the whole paragraph before the new row
288         appendParagraph(rit);
289 }
290
291
292 InsetOld * LyXText::getInset() const
293 {
294         ParagraphList::iterator pit = cursor.par();
295         pos_type const pos = cursor.pos();
296
297         if (pos < pit->size() && pit->isInset(pos)) {
298                 return pit->getInset(pos);
299         }
300         return 0;
301 }
302
303
304 void LyXText::toggleInset()
305 {
306         InsetOld * inset = getInset();
307         // is there an editable inset at cursor position?
308         if (!isEditableInset(inset)) {
309                 // No, try to see if we are inside a collapsable inset
310                 if (inset_owner && inset_owner->owner()
311                     && inset_owner->owner()->isOpen()) {
312                         bv()->unlockInset(inset_owner->owner());
313                         inset_owner->owner()->close(bv());
314                         bv()->getLyXText()->cursorRight(bv());
315                 }
316                 return;
317         }
318         //bv()->owner()->message(inset->editMessage());
319
320         // do we want to keep this?? (JMarc)
321         if (!isHighlyEditableInset(inset))
322                 recordUndo(bv(), Undo::ATOMIC);
323
324         if (inset->isOpen())
325                 inset->close(bv());
326         else
327                 inset->open(bv());
328
329         bv()->updateInset();
330 }
331
332
333 /* used in setlayout */
334 // Asger is not sure we want to do this...
335 void LyXText::makeFontEntriesLayoutSpecific(BufferParams const & params,
336                                             Paragraph & par)
337 {
338         LyXLayout_ptr const & layout = par.layout();
339         pos_type const psize = par.size();
340
341         LyXFont layoutfont;
342         for (pos_type pos = 0; pos < psize; ++pos) {
343                 if (pos < par.beginningOfBody())
344                         layoutfont = layout->labelfont;
345                 else
346                         layoutfont = layout->font;
347
348                 LyXFont tmpfont = par.getFontSettings(params, pos);
349                 tmpfont.reduce(layoutfont);
350                 par.setFont(pos, tmpfont);
351         }
352 }
353
354
355 ParagraphList::iterator
356 LyXText::setLayout(LyXCursor & cur, LyXCursor & sstart_cur,
357                    LyXCursor & send_cur,
358                    string const & layout)
359 {
360         ParagraphList::iterator endpit = boost::next(send_cur.par());
361         ParagraphList::iterator undoendpit = endpit;
362         ParagraphList::iterator pars_end = ownerParagraphs().end();
363
364         if (endpit != pars_end && endpit->getDepth()) {
365                 while (endpit != pars_end && endpit->getDepth()) {
366                         ++endpit;
367                         undoendpit = endpit;
368                 }
369         } else if (endpit != pars_end) {
370                 // because of parindents etc.
371                 ++endpit;
372         }
373
374         recordUndo(bv(), Undo::ATOMIC, sstart_cur.par(), boost::prior(undoendpit));
375
376         // ok we have a selection. This is always between sstart_cur
377         // and sel_end cursor
378         cur = sstart_cur;
379         ParagraphList::iterator pit = sstart_cur.par();
380         ParagraphList::iterator epit = boost::next(send_cur.par());
381
382         LyXLayout_ptr const & lyxlayout =
383                 bv()->buffer()->params.getLyXTextClass()[layout];
384
385         do {
386                 pit->applyLayout(lyxlayout);
387                 makeFontEntriesLayoutSpecific(bv()->buffer()->params, *pit);
388                 ParagraphList::iterator fppit = pit;
389                 fppit->params().spaceTop(lyxlayout->fill_top ?
390                                          VSpace(VSpace::VFILL)
391                                          : VSpace(VSpace::NONE));
392                 fppit->params().spaceBottom(lyxlayout->fill_bottom ?
393                                             VSpace(VSpace::VFILL)
394                                             : VSpace(VSpace::NONE));
395                 if (lyxlayout->margintype == MARGIN_MANUAL)
396                         pit->setLabelWidthString(lyxlayout->labelstring());
397                 cur.par(pit);
398                 ++pit;
399         } while (pit != epit);
400
401         return endpit;
402 }
403
404
405 // set layout over selection and make a total rebreak of those paragraphs
406 void LyXText::setLayout(string const & layout)
407 {
408         LyXCursor tmpcursor = cursor;  // store the current cursor
409
410         // if there is no selection just set the layout
411         // of the current paragraph
412         if (!selection.set()) {
413                 selection.start = cursor;  // dummy selection
414                 selection.end = cursor;
415         }
416
417         // special handling of new environment insets
418         BufferParams const & params = bv()->buffer()->params;
419         LyXLayout_ptr const & lyxlayout = params.getLyXTextClass()[layout];
420         if (lyxlayout->is_environment) {
421                 // move everything in a new environment inset
422                 lyxerr << "setting layout " << layout << endl;
423                 bv()->owner()->dispatch(FuncRequest(LFUN_HOME));
424                 bv()->owner()->dispatch(FuncRequest(LFUN_ENDSEL));
425                 bv()->owner()->dispatch(FuncRequest(LFUN_CUT));
426                 InsetOld * inset = new InsetEnvironment(params, layout);
427                 if (bv()->insertInset(inset)) {
428                         //inset->edit(bv());
429                         //bv()->owner()->dispatch(FuncRequest(LFUN_PASTE));
430                 }
431                 else
432                         delete inset;
433                 return;
434         }
435
436         ParagraphList::iterator endpit = setLayout(cursor, selection.start,
437                                                    selection.end, layout);
438         redoParagraphs(selection.start.par(), endpit);
439
440         // we have to reset the selection, because the
441         // geometry could have changed
442         setCursor(selection.start.par(), selection.start.pos(), false);
443         selection.cursor = cursor;
444         setCursor(selection.end.par(), selection.end.pos(), false);
445         updateCounters();
446         clearSelection();
447         setSelection();
448         setCursor(tmpcursor.par(), tmpcursor.pos(), true);
449 }
450
451
452 bool LyXText::changeDepth(bv_funcs::DEPTH_CHANGE type, bool test_only)
453 {
454         ParagraphList::iterator pit(cursor.par());
455         ParagraphList::iterator end(cursor.par());
456         ParagraphList::iterator start = pit;
457
458         if (selection.set()) {
459                 pit = selection.start.par();
460                 end = selection.end.par();
461                 start = pit;
462         }
463
464         ParagraphList::iterator pastend = boost::next(end);
465
466         if (!test_only)
467                 recordUndo(bv(), Undo::ATOMIC, start, end);
468
469         bool changed = false;
470
471         int prev_after_depth = 0;
472 #warning parlist ... could be nicer ?
473         if (start != ownerParagraphs().begin()) {
474                 prev_after_depth = boost::prior(start)->getMaxDepthAfter();
475         }
476
477         while (true) {
478                 int const depth = pit->params().depth();
479                 if (type == bv_funcs::INC_DEPTH) {
480                         if (depth < prev_after_depth
481                             && pit->layout()->labeltype != LABEL_BIBLIO) {
482                                 changed = true;
483                                 if (!test_only) {
484                                         pit->params().depth(depth + 1);
485                                 }
486
487                         }
488                 } else if (depth) {
489                         changed = true;
490                         if (!test_only)
491                                 pit->params().depth(depth - 1);
492                 }
493
494                 prev_after_depth = pit->getMaxDepthAfter();
495
496                 if (pit == end) {
497                         break;
498                 }
499
500                 ++pit;
501         }
502
503         if (test_only)
504                 return changed;
505
506
507         redoParagraphs(start, pastend);
508
509         // We need to actually move the text->cursor. I don't
510         // understand why ...
511         LyXCursor tmpcursor = cursor;
512
513         // we have to reset the visual selection because the
514         // geometry could have changed
515         if (selection.set()) {
516                 setCursor(selection.start.par(), selection.start.pos());
517                 selection.cursor = cursor;
518                 setCursor(selection.end.par(), selection.end.pos());
519         }
520
521         // this handles the counter labels, and also fixes up
522         // depth values for follow-on (child) paragraphs
523         updateCounters();
524
525         setSelection();
526         setCursor(tmpcursor.par(), tmpcursor.pos());
527
528         return changed;
529 }
530
531
532 // set font over selection and make a total rebreak of those paragraphs
533 void LyXText::setFont(LyXFont const & font, bool toggleall)
534 {
535         // if there is no selection just set the current_font
536         if (!selection.set()) {
537                 // Determine basis font
538                 LyXFont layoutfont;
539                 if (cursor.pos() < cursor.par()->beginningOfBody()) {
540                         layoutfont = getLabelFont(cursor.par());
541                 } else {
542                         layoutfont = getLayoutFont(cursor.par());
543                 }
544                 // Update current font
545                 real_current_font.update(font,
546                                          bv()->buffer()->params.language,
547                                          toggleall);
548
549                 // Reduce to implicit settings
550                 current_font = real_current_font;
551                 current_font.reduce(layoutfont);
552                 // And resolve it completely
553                 real_current_font.realize(layoutfont);
554
555                 return;
556         }
557
558         LyXCursor tmpcursor = cursor; // store the current cursor
559
560         // ok we have a selection. This is always between sel_start_cursor
561         // and sel_end cursor
562
563         recordUndo(bv(), Undo::ATOMIC, selection.start.par(), selection.end.par());
564         freezeUndo();
565         cursor = selection.start;
566         while (cursor.par() != selection.end.par() ||
567                cursor.pos() < selection.end.pos())
568         {
569                 if (cursor.pos() < cursor.par()->size()) {
570                         // an open footnote should behave like a closed one
571                         setCharFont(cursor.par(), cursor.pos(),
572                                     font, toggleall);
573                         cursor.pos(cursor.pos() + 1);
574                 } else {
575                         cursor.pos(0);
576                         cursor.par(boost::next(cursor.par()));
577                 }
578         }
579         unFreezeUndo();
580
581         redoParagraph(selection.start.par());
582
583         // we have to reset the selection, because the
584         // geometry could have changed, but we keep
585         // it for user convenience
586         setCursor(selection.start.par(), selection.start.pos());
587         selection.cursor = cursor;
588         setCursor(selection.end.par(), selection.end.pos());
589         setSelection();
590         setCursor(tmpcursor.par(), tmpcursor.pos(), true,
591                   tmpcursor.boundary());
592 }
593
594
595 void LyXText::redoHeightOfParagraph()
596 {
597         RowList::iterator tmprow = cursorRow();
598
599         setHeightOfRow(tmprow);
600
601         while (tmprow != rows().begin()
602                && boost::prior(tmprow)->par() == tmprow->par()) {
603                 --tmprow;
604                 setHeightOfRow(tmprow);
605         }
606
607         setCursor(cursor.par(), cursor.pos(), false, cursor.boundary());
608 }
609
610
611 RowList::iterator LyXText::firstRow(ParagraphList::iterator pit)
612 {
613         RowList::iterator rit;
614         for (rit = rows().begin(); rit != rows().end(); ++rit)
615                 if (rit->par() == pit)
616                         break;
617         return rit;
618 }
619
620
621 // rebreaks all paragraphs between the specified pars
622 // This function is needed after SetLayout and SetFont etc.
623 void LyXText::redoParagraphs(ParagraphList::iterator start,
624   ParagraphList::iterator end)
625 {
626         for ( ; start != end; ++start)
627                 redoParagraph(start);
628 }
629
630
631 void LyXText::redoParagraph(ParagraphList::iterator pit)
632 {
633         RowList::iterator rit = firstRow(pit);
634
635         // remove paragraph from rowlist
636         while (rit != rows().end() && rit->par() == pit) {
637                 RowList::iterator rit2 = rit++;
638                 removeRow(rit2);
639         }
640
641         // reinsert the paragraph
642         // insert a new row, starting at position 0
643         Row newrow(pit, 0);
644         rit = rowlist_.insert(rit, newrow);
645
646         // and now append the whole paragraph before the new row
647         pos_type const last = rit->par()->size();
648         bool done = false;
649
650         do {
651                 pos_type z = rowBreakPoint(*rit);
652
653                 RowList::iterator tmprow = rit;
654
655                 if (z < last) {
656                         ++z;
657                         Row newrow(rit->par(), z);
658                         rit = rowlist_.insert(boost::next(rit), newrow);
659                 } else {
660                         done = true;
661                 }
662
663                 // Set the dimensions of the row
664                 // fixed fill setting now by calling inset->update() in
665                 // SingleWidth when needed!
666                 tmprow->fill(fill(tmprow, workWidth()));
667                 setHeightOfRow(tmprow);
668
669         } while (!done);
670
671         setHeightOfRow(rows().begin());
672 }
673
674
675 void LyXText::fullRebreak()
676 {
677         redoParagraphs(ownerParagraphs().begin(), ownerParagraphs().end());
678         setCursorIntern(cursor.par(), cursor.pos());
679         selection.cursor = cursor;
680 }
681
682
683 void LyXText::metrics(MetricsInfo & mi, Dimension & dim)
684 {
685         //lyxerr << "LyXText::metrics: width: " << mi.base.textwidth << endl;
686         //Assert(mi.base.textwidth);
687
688         // rebuild row cache
689         rowlist_.clear();
690         width = 0;
691         height = 0;
692
693         anchor_row_ = rows().end();
694         anchor_row_offset_ = 0;
695
696         ParagraphList::iterator pit = ownerParagraphs().begin();
697         ParagraphList::iterator end = ownerParagraphs().end();
698
699         for (; pit != end; ++pit) {
700                 InsetList::iterator ii = pit->insetlist.begin();
701                 InsetList::iterator iend = pit->insetlist.end();
702                 for (; ii != iend; ++ii) {
703                         Dimension dim;
704                         MetricsInfo m = mi;
705 #warning FIXME: pos != 0
706                         m.base.font = getFont(pit, 0);
707                         ii->inset->metrics(m, dim);
708                 }
709
710 #if 1
711                 // insert a new row, starting at position 0
712                 Row newrow(pit, 0);
713                 RowList::iterator rit = rowlist_.insert(rowlist_.end(), newrow);
714
715                 // and now append the whole paragraph before the new row
716                 appendParagraph(rit);
717 #else
718                 redoParagraph(pit);
719 #endif
720         }
721
722         // compute height
723         //lyxerr << "height 0: " << height << endl;
724         //for (RowList::iterator rit = rows().begin(); rit != rows().end(); ++rit) {
725         //      height += rit->height();
726         //}
727         //lyxerr << "height 1: " << height << endl;
728
729         // final dimension
730         dim.asc = rows().begin()->ascent_of_text();
731         dim.des = height - dim.asc;
732         dim.wid = std::max(mi.base.textwidth, int(width));
733 }
734
735
736 void LyXText::partialRebreak()
737 {
738         if (rows().empty()) {
739                 init(bv());
740                 return;
741         }
742         breakAgain(rows().begin());
743 }
744
745
746 // important for the screen
747
748
749 // the cursor set functions have a special mechanism. When they
750 // realize, that you left an empty paragraph, they will delete it.
751 // They also delete the corresponding row
752
753 // need the selection cursor:
754 void LyXText::setSelection()
755 {
756         TextCursor::setSelection();
757 }
758
759
760
761 void LyXText::clearSelection()
762 {
763         TextCursor::clearSelection();
764
765         // reset this in the bv_owner!
766         if (bv_owner && bv_owner->text)
767                 bv_owner->text->xsel_cache.set(false);
768 }
769
770
771 void LyXText::cursorHome()
772 {
773         setCursor(cursor.par(), cursorRow()->pos());
774 }
775
776
777 void LyXText::cursorEnd()
778 {
779         if (cursor.par()->empty())
780                 return;
781
782         RowList::iterator rit = cursorRow();
783         RowList::iterator next_rit = boost::next(rit);
784         ParagraphList::iterator pit = rit->par();
785         pos_type last_pos = lastPos(*this, rit);
786
787         if (next_rit == rows().end() || next_rit->par() != pit) {
788                 ++last_pos;
789         } else {
790                 if (pit->empty() ||
791                     (pit->getChar(last_pos) != ' ' && !pit->isNewline(last_pos))) {
792                         ++last_pos;
793                 }
794         }
795
796         setCursor(pit, last_pos);
797 }
798
799
800 void LyXText::cursorTop()
801 {
802         setCursor(ownerParagraphs().begin(), 0);
803 }
804
805
806 void LyXText::cursorBottom()
807 {
808         ParagraphList::iterator lastpit =
809                 boost::prior(ownerParagraphs().end());
810         setCursor(lastpit, lastpit->size());
811 }
812
813
814 void LyXText::toggleFree(LyXFont const & font, bool toggleall)
815 {
816         // If the mask is completely neutral, tell user
817         if (font == LyXFont(LyXFont::ALL_IGNORE)) {
818                 // Could only happen with user style
819                 bv()->owner()->message(_("No font change defined. Use Character under the Layout menu to define font change."));
820                 return;
821         }
822
823         // Try implicit word selection
824         // If there is a change in the language the implicit word selection
825         // is disabled.
826         LyXCursor resetCursor = cursor;
827         bool implicitSelection = (font.language() == ignore_language
828                                   && font.number() == LyXFont::IGNORE)
829                 ? selectWordWhenUnderCursor(lyx::WHOLE_WORD_STRICT) : false;
830
831         // Set font
832         setFont(font, toggleall);
833
834         // Implicit selections are cleared afterwards
835         //and cursor is set to the original position.
836         if (implicitSelection) {
837                 clearSelection();
838                 cursor = resetCursor;
839                 setCursor(cursor.par(), cursor.pos());
840                 selection.cursor = cursor;
841         }
842 }
843
844
845 string LyXText::getStringToIndex()
846 {
847         // Try implicit word selection
848         // If there is a change in the language the implicit word selection
849         // is disabled.
850         LyXCursor const reset_cursor = cursor;
851         bool const implicitSelection =
852                 selectWordWhenUnderCursor(lyx::PREVIOUS_WORD);
853
854         string idxstring;
855         if (!selection.set())
856                 bv()->owner()->message(_("Nothing to index!"));
857         else if (selection.start.par() != selection.end.par())
858                 bv()->owner()->message(_("Cannot index more than one paragraph!"));
859         else
860                 idxstring = selectionAsString(bv()->buffer(), false);
861
862         // Reset cursors to their original position.
863         cursor = reset_cursor;
864         setCursor(cursor.par(), cursor.pos());
865         selection.cursor = cursor;
866
867         // Clear the implicit selection.
868         if (implicitSelection)
869                 clearSelection();
870
871         return idxstring;
872 }
873
874
875 // the DTP switches for paragraphs. LyX will store them in the first
876 // physicla paragraph. When a paragraph is broken, the top settings rest,
877 // the bottom settings are given to the new one. So I can make shure,
878 // they do not duplicate themself and you cannnot make dirty things with
879 // them!
880
881 void LyXText::setParagraph(bool line_top, bool line_bottom,
882                            bool pagebreak_top, bool pagebreak_bottom,
883                            VSpace const & space_top,
884                            VSpace const & space_bottom,
885                            Spacing const & spacing,
886                            LyXAlignment align,
887                            string const & labelwidthstring,
888                            bool noindent)
889 {
890         LyXCursor tmpcursor = cursor;
891         if (!selection.set()) {
892                 selection.start = cursor;
893                 selection.end = cursor;
894         }
895
896         // make sure that the depth behind the selection are restored, too
897         ParagraphList::iterator endpit = boost::next(selection.end.par());
898         ParagraphList::iterator undoendpit = endpit;
899         ParagraphList::iterator pars_end = ownerParagraphs().end();
900
901         if (endpit != pars_end && endpit->getDepth()) {
902                 while (endpit != pars_end && endpit->getDepth()) {
903                         ++endpit;
904                         undoendpit = endpit;
905                 }
906         } else if (endpit != pars_end) {
907                 // because of parindents etc.
908                 ++endpit;
909         }
910
911         recordUndo(bv(), Undo::ATOMIC, selection.start.par(),
912                 boost::prior(undoendpit));
913
914
915         ParagraphList::iterator tmppit = selection.end.par();
916
917         while (tmppit != boost::prior(selection.start.par())) {
918                 setCursor(tmppit, 0);
919
920                 ParagraphList::iterator pit = cursor.par();
921                 ParagraphParameters & params = pit->params();
922
923                 params.lineTop(line_top);
924                 params.lineBottom(line_bottom);
925                 params.pagebreakTop(pagebreak_top);
926                 params.pagebreakBottom(pagebreak_bottom);
927                 params.spaceTop(space_top);
928                 params.spaceBottom(space_bottom);
929                 params.spacing(spacing);
930                 // does the layout allow the new alignment?
931                 LyXLayout_ptr const & layout = pit->layout();
932
933                 if (align == LYX_ALIGN_LAYOUT)
934                         align = layout->align;
935                 if (align & layout->alignpossible) {
936                         if (align == layout->align)
937                                 params.align(LYX_ALIGN_LAYOUT);
938                         else
939                                 params.align(align);
940                 }
941                 pit->setLabelWidthString(labelwidthstring);
942                 params.noindent(noindent);
943                 tmppit = boost::prior(pit);
944         }
945
946         redoParagraphs(selection.start.par(), endpit);
947
948         clearSelection();
949         setCursor(selection.start.par(), selection.start.pos());
950         selection.cursor = cursor;
951         setCursor(selection.end.par(), selection.end.pos());
952         setSelection();
953         setCursor(tmpcursor.par(), tmpcursor.pos());
954         if (inset_owner)
955                 bv()->updateInset();
956 }
957
958
959 // set the counter of a paragraph. This includes the labels
960 void LyXText::setCounter(Buffer const * buf, ParagraphList::iterator pit)
961 {
962         LyXTextClass const & textclass = buf->params.getLyXTextClass();
963         LyXLayout_ptr const & layout = pit->layout();
964
965         if (pit != ownerParagraphs().begin()) {
966
967                 pit->params().appendix(boost::prior(pit)->params().appendix());
968                 if (!pit->params().appendix() &&
969                     pit->params().startOfAppendix()) {
970                         pit->params().appendix(true);
971                         textclass.counters().reset();
972                 }
973                 pit->enumdepth = boost::prior(pit)->enumdepth;
974                 pit->itemdepth = boost::prior(pit)->itemdepth;
975         } else {
976                 pit->params().appendix(pit->params().startOfAppendix());
977                 pit->enumdepth = 0;
978                 pit->itemdepth = 0;
979         }
980
981         /* Maybe we have to increment the enumeration depth.
982          * BUT, enumeration in a footnote is considered in isolation from its
983          *      surrounding paragraph so don't increment if this is the
984          *      first line of the footnote
985          * AND, bibliographies can't have their depth changed ie. they
986          *      are always of depth 0
987          */
988         if (pit != ownerParagraphs().begin()
989             && boost::prior(pit)->getDepth() < pit->getDepth()
990             && boost::prior(pit)->layout()->labeltype == LABEL_COUNTER_ENUMI
991             && pit->enumdepth < 3
992             && layout->labeltype != LABEL_BIBLIO) {
993                 pit->enumdepth++;
994         }
995
996         // Maybe we have to decrement the enumeration depth, see note above
997         if (pit != ownerParagraphs().begin()
998             && boost::prior(pit)->getDepth() > pit->getDepth()
999             && layout->labeltype != LABEL_BIBLIO) {
1000                 pit->enumdepth = depthHook(pit, ownerParagraphs(),
1001                                            pit->getDepth())->enumdepth;
1002         }
1003
1004         if (!pit->params().labelString().empty()) {
1005                 pit->params().labelString(string());
1006         }
1007
1008         if (layout->margintype == MARGIN_MANUAL) {
1009                 if (pit->params().labelWidthString().empty()) {
1010                         pit->setLabelWidthString(layout->labelstring());
1011                 }
1012         } else {
1013                 pit->setLabelWidthString(string());
1014         }
1015
1016         // is it a layout that has an automatic label?
1017         if (layout->labeltype >= LABEL_COUNTER_CHAPTER) {
1018                 int const i = layout->labeltype - LABEL_COUNTER_CHAPTER;
1019
1020                 ostringstream s;
1021
1022                 if (i >= 0 && i <= buf->params.secnumdepth) {
1023                         string numbertype;
1024                         string langtype;
1025
1026                         textclass.counters().step(layout->latexname());
1027
1028                         // Is there a label? Useful for Chapter layout
1029                         if (!pit->params().appendix()) {
1030                                 s << buf->B_(layout->labelstring());
1031                         } else {
1032                                 s << buf->B_(layout->labelstring_appendix());
1033                         }
1034
1035                         // Use of an integer is here less than elegant. For now.
1036                         int head = textclass.maxcounter() - LABEL_COUNTER_CHAPTER;
1037                         if (!pit->params().appendix()) {
1038                                 numbertype = "sectioning";
1039                         } else {
1040                                 numbertype = "appendix";
1041                                 if (pit->isRightToLeftPar(buf->params))
1042                                         langtype = "hebrew";
1043                                 else
1044                                         langtype = "latin";
1045                         }
1046
1047                         s << " "
1048                           << textclass.counters()
1049                                 .numberLabel(layout->latexname(),
1050                                              numbertype, langtype, head);
1051
1052                         pit->params().labelString(STRCONV(s.str()));
1053
1054                         // reset enum counters
1055                         textclass.counters().reset("enum");
1056                 } else if (layout->labeltype < LABEL_COUNTER_ENUMI) {
1057                         textclass.counters().reset("enum");
1058                 } else if (layout->labeltype == LABEL_COUNTER_ENUMI) {
1059                         // FIXME
1060                         // Yes I know this is a really, really! bad solution
1061                         // (Lgb)
1062                         string enumcounter("enum");
1063
1064                         switch (pit->enumdepth) {
1065                         case 2:
1066                                 enumcounter += 'i';
1067                         case 1:
1068                                 enumcounter += 'i';
1069                         case 0:
1070                                 enumcounter += 'i';
1071                                 break;
1072                         case 3:
1073                                 enumcounter += "iv";
1074                                 break;
1075                         default:
1076                                 // not a valid enumdepth...
1077                                 break;
1078                         }
1079
1080                         textclass.counters().step(enumcounter);
1081
1082                         s << textclass.counters()
1083                                 .numberLabel(enumcounter, "enumeration");
1084                         pit->params().labelString(STRCONV(s.str()));
1085                 }
1086         } else if (layout->labeltype == LABEL_BIBLIO) {// ale970302
1087                 textclass.counters().step("bibitem");
1088                 int number = textclass.counters().value("bibitem");
1089                 if (pit->bibitem()) {
1090                         pit->bibitem()->setCounter(number);
1091                         pit->params().labelString(layout->labelstring());
1092                 }
1093                 // In biblio should't be following counters but...
1094         } else {
1095                 string s = buf->B_(layout->labelstring());
1096
1097                 // the caption hack:
1098                 if (layout->labeltype == LABEL_SENSITIVE) {
1099                         ParagraphList::iterator end = ownerParagraphs().end();
1100                         ParagraphList::iterator tmppit = pit;
1101                         InsetOld * in = 0;
1102                         bool isOK = false;
1103                         while (tmppit != end && tmppit->inInset()
1104                                // the single '=' is intended below
1105                                && (in = tmppit->inInset()->owner()))
1106                         {
1107                                 if (in->lyxCode() == InsetOld::FLOAT_CODE ||
1108                                     in->lyxCode() == InsetOld::WRAP_CODE) {
1109                                         isOK = true;
1110                                         break;
1111                                 } else {
1112                                         tmppit = ownerParagraphs().begin();
1113                                         for ( ; tmppit != end; ++tmppit)
1114                                                 if (&*tmppit == in->parOwner())
1115                                                         break;
1116                                 }
1117                         }
1118
1119                         if (isOK) {
1120                                 string type;
1121
1122                                 if (in->lyxCode() == InsetOld::FLOAT_CODE)
1123                                         type = static_cast<InsetFloat*>(in)->params().type;
1124                                 else if (in->lyxCode() == InsetOld::WRAP_CODE)
1125                                         type = static_cast<InsetWrap*>(in)->params().type;
1126                                 else
1127                                         Assert(0);
1128
1129                                 Floating const & fl = textclass.floats().getType(type);
1130
1131                                 textclass.counters().step(fl.type());
1132
1133                                 // Doesn't work... yet.
1134                                 s = bformat(_("%1$s #:"), buf->B_(fl.name()));
1135                         } else {
1136                                 // par->SetLayout(0);
1137                                 // s = layout->labelstring;
1138                                 s = _("Senseless: ");
1139                         }
1140                 }
1141                 pit->params().labelString(s);
1142
1143                 // reset the enumeration counter. They are always reset
1144                 // when there is any other layout between
1145                 // Just fall-through between the cases so that all
1146                 // enum counters deeper than enumdepth is also reset.
1147                 switch (pit->enumdepth) {
1148                 case 0:
1149                         textclass.counters().reset("enumi");
1150                 case 1:
1151                         textclass.counters().reset("enumii");
1152                 case 2:
1153                         textclass.counters().reset("enumiii");
1154                 case 3:
1155                         textclass.counters().reset("enumiv");
1156                 }
1157         }
1158 }
1159
1160
1161 // Updates all counters. Paragraphs with changed label string will be rebroken
1162 void LyXText::updateCounters()
1163 {
1164         RowList::iterator rowit = rows().begin();
1165         ParagraphList::iterator pit = rowit->par();
1166
1167         // CHECK if this is really needed. (Lgb)
1168         bv()->buffer()->params.getLyXTextClass().counters().reset();
1169
1170         ParagraphList::iterator beg = ownerParagraphs().begin();
1171         ParagraphList::iterator end = ownerParagraphs().end();
1172         for (; pit != end; ++pit) {
1173                 while (rowit->par() != pit)
1174                         ++rowit;
1175
1176                 string const oldLabel = pit->params().labelString();
1177
1178                 size_t maxdepth = 0;
1179                 if (pit != beg)
1180                         maxdepth = boost::prior(pit)->getMaxDepthAfter();
1181
1182                 if (pit->params().depth() > maxdepth)
1183                         pit->params().depth(maxdepth);
1184
1185                 // setCounter can potentially change the labelString.
1186                 setCounter(bv()->buffer(), pit);
1187
1188                 string const & newLabel = pit->params().labelString();
1189
1190                 if (oldLabel.empty() && !newLabel.empty()) {
1191                         removeParagraph(rowit);
1192                         appendParagraph(rowit);
1193                 }
1194         }
1195 }
1196
1197
1198 void LyXText::insertInset(InsetOld * inset)
1199 {
1200         if (!cursor.par()->insetAllowed(inset->lyxCode()))
1201                 return;
1202         recordUndo(bv(), Undo::ATOMIC, cursor.par());
1203         freezeUndo();
1204         cursor.par()->insertInset(cursor.pos(), inset);
1205         // Just to rebreak and refresh correctly.
1206         // The character will not be inserted a second time
1207         insertChar(Paragraph::META_INSET);
1208         // If we enter a highly editable inset the cursor should be to before
1209         // the inset. This couldn't happen before as Undo was not handled inside
1210         // inset now after the Undo LyX tries to call inset->Edit(...) again
1211         // and cannot do this as the cursor is behind the inset and GetInset
1212         // does not return the inset!
1213         if (isHighlyEditableInset(inset)) {
1214                 cursorLeft(true);
1215         }
1216         unFreezeUndo();
1217 }
1218
1219
1220 void LyXText::cutSelection(bool doclear, bool realcut)
1221 {
1222         // Stuff what we got on the clipboard. Even if there is no selection.
1223
1224         // There is a problem with having the stuffing here in that the
1225         // larger the selection the slower LyX will get. This can be
1226         // solved by running the line below only when the selection has
1227         // finished. The solution used currently just works, to make it
1228         // faster we need to be more clever and probably also have more
1229         // calls to stuffClipboard. (Lgb)
1230         bv()->stuffClipboard(selectionAsString(bv()->buffer(), true));
1231
1232         // This doesn't make sense, if there is no selection
1233         if (!selection.set())
1234                 return;
1235
1236         // OK, we have a selection. This is always between selection.start
1237         // and selection.end
1238
1239         // make sure that the depth behind the selection are restored, too
1240         ParagraphList::iterator endpit = boost::next(selection.end.par());
1241         ParagraphList::iterator undoendpit = endpit;
1242         ParagraphList::iterator pars_end = ownerParagraphs().end();
1243
1244         if (endpit != pars_end && endpit->getDepth()) {
1245                 while (endpit != pars_end && endpit->getDepth()) {
1246                         ++endpit;
1247                         undoendpit = endpit;
1248                 }
1249         } else if (endpit != pars_end) {
1250                 // because of parindents etc.
1251                 ++endpit;
1252         }
1253
1254         recordUndo(bv(), Undo::DELETE, selection.start.par(),
1255                    boost::prior(undoendpit));
1256
1257
1258         endpit = selection.end.par();
1259         int endpos = selection.end.pos();
1260
1261         boost::tie(endpit, endpos) = realcut ?
1262                 CutAndPaste::cutSelection(bv()->buffer()->params,
1263                                           ownerParagraphs(),
1264                                           selection.start.par(), endpit,
1265                                           selection.start.pos(), endpos,
1266                                           bv()->buffer()->params.textclass,
1267                                           doclear)
1268                 : CutAndPaste::eraseSelection(bv()->buffer()->params,
1269                                               ownerParagraphs(),
1270                                               selection.start.par(), endpit,
1271                                               selection.start.pos(), endpos,
1272                                               doclear);
1273         // sometimes necessary
1274         if (doclear)
1275                 selection.start.par()->stripLeadingSpaces();
1276
1277         redoParagraphs(selection.start.par(), boost::next(endpit));
1278         // cutSelection can invalidate the cursor so we need to set
1279         // it anew. (Lgb)
1280         // we prefer the end for when tracking changes
1281         cursor.pos(endpos);
1282         cursor.par(endpit);
1283
1284         // need a valid cursor. (Lgb)
1285         clearSelection();
1286
1287         setCursor(cursor.par(), cursor.pos());
1288         selection.cursor = cursor;
1289         updateCounters();
1290 }
1291
1292
1293 void LyXText::copySelection()
1294 {
1295         // stuff the selection onto the X clipboard, from an explicit copy request
1296         bv()->stuffClipboard(selectionAsString(bv()->buffer(), true));
1297
1298         // this doesnt make sense, if there is no selection
1299         if (!selection.set())
1300                 return;
1301
1302         // ok we have a selection. This is always between selection.start
1303         // and sel_end cursor
1304
1305         // copy behind a space if there is one
1306         while (selection.start.par()->size() > selection.start.pos()
1307                && selection.start.par()->isLineSeparator(selection.start.pos())
1308                && (selection.start.par() != selection.end.par()
1309                    || selection.start.pos() < selection.end.pos()))
1310                 selection.start.pos(selection.start.pos() + 1);
1311
1312         CutAndPaste::copySelection(selection.start.par(),
1313                                    selection.end.par(),
1314                                    selection.start.pos(), selection.end.pos(),
1315                                    bv()->buffer()->params.textclass);
1316 }
1317
1318
1319 void LyXText::pasteSelection(size_t sel_index)
1320 {
1321         // this does not make sense, if there is nothing to paste
1322         if (!CutAndPaste::checkPastePossible())
1323                 return;
1324
1325         recordUndo(bv(), Undo::INSERT, cursor.par());
1326
1327         ParagraphList::iterator endpit;
1328         PitPosPair ppp;
1329
1330         ErrorList el;
1331
1332         boost::tie(ppp, endpit) =
1333                 CutAndPaste::pasteSelection(*bv()->buffer(),
1334                                             ownerParagraphs(),
1335                                             cursor.par(), cursor.pos(),
1336                                             bv()->buffer()->params.textclass,
1337                                             sel_index, el);
1338         bufferErrors(*bv()->buffer(), el);
1339         bv()->showErrorList(_("Paste"));
1340
1341         redoParagraphs(cursor.par(), endpit);
1342
1343         setCursor(cursor.par(), cursor.pos());
1344         clearSelection();
1345
1346         selection.cursor = cursor;
1347         setCursor(ppp.first, ppp.second);
1348         setSelection();
1349         updateCounters();
1350 }
1351
1352
1353 void LyXText::setSelectionRange(lyx::pos_type length)
1354 {
1355         if (!length)
1356                 return;
1357
1358         selection.cursor = cursor;
1359         while (length--)
1360                 cursorRight(bv());
1361         setSelection();
1362 }
1363
1364
1365 // simple replacing. The font of the first selected character is used
1366 void LyXText::replaceSelectionWithString(string const & str)
1367 {
1368         recordUndo(bv(), Undo::ATOMIC);
1369         freezeUndo();
1370
1371         if (!selection.set()) { // create a dummy selection
1372                 selection.end = cursor;
1373                 selection.start = cursor;
1374         }
1375
1376         // Get font setting before we cut
1377         pos_type pos = selection.end.pos();
1378         LyXFont const font = selection.start.par()
1379                 ->getFontSettings(bv()->buffer()->params,
1380                                   selection.start.pos());
1381
1382         // Insert the new string
1383         string::const_iterator cit = str.begin();
1384         string::const_iterator end = str.end();
1385         for (; cit != end; ++cit) {
1386                 selection.end.par()->insertChar(pos, (*cit), font);
1387                 ++pos;
1388         }
1389
1390         // Cut the selection
1391         cutSelection(true, false);
1392
1393         unFreezeUndo();
1394 }
1395
1396
1397 // needed to insert the selection
1398 void LyXText::insertStringAsLines(string const & str)
1399 {
1400         ParagraphList::iterator pit = cursor.par();
1401         pos_type pos = cursor.pos();
1402         ParagraphList::iterator endpit = boost::next(cursor.par());
1403
1404         recordUndo(bv(), Undo::ATOMIC);
1405
1406         // only to be sure, should not be neccessary
1407         clearSelection();
1408
1409         bv()->buffer()->insertStringAsLines(pit, pos, current_font, str);
1410
1411         redoParagraphs(cursor.par(), endpit);
1412         setCursor(cursor.par(), cursor.pos());
1413         selection.cursor = cursor;
1414         setCursor(pit, pos);
1415         setSelection();
1416 }
1417
1418
1419 // turns double-CR to single CR, others where converted into one
1420 // blank. Then InsertStringAsLines is called
1421 void LyXText::insertStringAsParagraphs(string const & str)
1422 {
1423         string linestr(str);
1424         bool newline_inserted = false;
1425         string::size_type const siz = linestr.length();
1426
1427         for (string::size_type i = 0; i < siz; ++i) {
1428                 if (linestr[i] == '\n') {
1429                         if (newline_inserted) {
1430                                 // we know that \r will be ignored by
1431                                 // InsertStringA. Of course, it is a dirty
1432                                 // trick, but it works...
1433                                 linestr[i - 1] = '\r';
1434                                 linestr[i] = '\n';
1435                         } else {
1436                                 linestr[i] = ' ';
1437                                 newline_inserted = true;
1438                         }
1439                 } else if (IsPrintable(linestr[i])) {
1440                         newline_inserted = false;
1441                 }
1442         }
1443         insertStringAsLines(linestr);
1444 }
1445
1446
1447 void LyXText::checkParagraph(ParagraphList::iterator pit, pos_type pos)
1448 {
1449         breakAgain(getRow(pit, pos));
1450         setCursorIntern(cursor.par(), cursor.pos(), false, cursor.boundary());
1451 }
1452
1453
1454 // returns false if inset wasn't found
1455 bool LyXText::updateInset(InsetOld * inset)
1456 {
1457         // first check the current paragraph
1458         int pos = cursor.par()->getPositionOfInset(inset);
1459         if (pos != -1) {
1460                 checkParagraph(cursor.par(), pos);
1461                 return true;
1462         }
1463
1464         // check every paragraph
1465         ParagraphList::iterator par = ownerParagraphs().begin();
1466         ParagraphList::iterator end = ownerParagraphs().end();
1467         for (; par != end; ++par) {
1468                 pos = par->getPositionOfInset(inset);
1469                 if (pos != -1) {
1470                         checkParagraph(par, pos);
1471                         return true;
1472                 }
1473         }
1474
1475         return false;
1476 }
1477
1478
1479 bool LyXText::setCursor(ParagraphList::iterator pit,
1480                         pos_type pos,
1481                         bool setfont, bool boundary)
1482 {
1483         LyXCursor old_cursor = cursor;
1484         setCursorIntern(pit, pos, setfont, boundary);
1485         return deleteEmptyParagraphMechanism(old_cursor);
1486 }
1487
1488
1489 void LyXText::setCursor(LyXCursor & cur, ParagraphList::iterator pit,
1490                         pos_type pos, bool boundary)
1491 {
1492         Assert(pit != ownerParagraphs().end());
1493
1494         cur.par(pit);
1495         cur.pos(pos);
1496         cur.boundary(boundary);
1497         if (rows().empty())
1498                 return;
1499
1500         // get the cursor y position in text
1501         int y = 0;
1502         RowList::iterator row = getRow(pit, pos, y);
1503         RowList::iterator beg = rows().begin();
1504
1505         RowList::iterator old_row = row;
1506         // if we are before the first char of this row and are still in the
1507         // same paragraph and there is a previous row then put the cursor on
1508         // the end of the previous row
1509         cur.iy(y + row->baseline());
1510         if (row != beg &&
1511             pos &&
1512             boost::prior(row)->par() == row->par() &&
1513             pos < pit->size() &&
1514             pit->getChar(pos) == Paragraph::META_INSET) {
1515                 InsetOld * ins = pit->getInset(pos);
1516                 if (ins && (ins->needFullRow() || ins->display())) {
1517                         --row;
1518                         y -= row->height();
1519                 }
1520         }
1521
1522         // y is now the beginning of the cursor row
1523         y += row->baseline();
1524         // y is now the cursor baseline
1525         cur.y(y);
1526
1527         pos_type last = lastPrintablePos(*this, old_row);
1528
1529         // None of these should happen, but we're scaredy-cats
1530         if (pos > pit->size()) {
1531                 lyxerr << "dont like 1 please report" << endl;
1532                 pos = 0;
1533                 cur.pos(0);
1534         } else if (pos > last + 1) {
1535                 lyxerr << "dont like 2 please report" << endl;
1536                 // This shouldn't happen.
1537                 pos = last + 1;
1538                 cur.pos(pos);
1539         } else if (pos < row->pos()) {
1540                 lyxerr << "dont like 3 please report" << endl;
1541                 pos = row->pos();
1542                 cur.pos(pos);
1543         }
1544
1545         // now get the cursors x position
1546         float x = getCursorX(row, pos, last, boundary);
1547         cur.x(int(x));
1548         cur.x_fix(cur.x());
1549         if (old_row != row) {
1550                 x = getCursorX(old_row, pos, last, boundary);
1551                 cur.ix(int(x));
1552         } else
1553                 cur.ix(cur.x());
1554 /* We take out this for the time being because 1) the redraw code is not
1555    prepared to this yet and 2) because some good policy has yet to be decided
1556    while editting: for instance how to act on rows being created/deleted
1557    because of DEPM.
1558 */
1559 #if 0
1560         //if the cursor is in a visible row, anchor to it
1561         int topy = top_y();
1562         if (topy < y && y < topy + bv()->workHeight())
1563                 anchor_row(row);
1564 #endif
1565 }
1566
1567
1568 float LyXText::getCursorX(RowList::iterator rit,
1569                           pos_type pos, pos_type last, bool boundary) const
1570 {
1571         pos_type cursor_vpos = 0;
1572         double x;
1573         double fill_separator;
1574         double fill_hfill;
1575         double fill_label_hfill;
1576         // This call HAS to be here because of the BidiTables!!!
1577         prepareToPrint(rit, x, fill_separator, fill_hfill,
1578                        fill_label_hfill);
1579
1580         ParagraphList::iterator rit_par = rit->par();
1581         pos_type const rit_pos = rit->pos();
1582
1583         if (last < rit_pos)
1584                 cursor_vpos = rit_pos;
1585         else if (pos > last && !boundary)
1586                 cursor_vpos = (rit_par->isRightToLeftPar(bv()->buffer()->params))
1587                         ? rit_pos : last + 1;
1588         else if (pos > rit_pos && (pos > last || boundary))
1589                 /// Place cursor after char at (logical) position pos - 1
1590                 cursor_vpos = (bidi_level(pos - 1) % 2 == 0)
1591                         ? log2vis(pos - 1) + 1 : log2vis(pos - 1);
1592         else
1593                 /// Place cursor before char at (logical) position pos
1594                 cursor_vpos = (bidi_level(pos) % 2 == 0)
1595                         ? log2vis(pos) : log2vis(pos) + 1;
1596
1597         pos_type body_pos = rit_par->beginningOfBody();
1598         if (body_pos > 0 &&
1599             (body_pos - 1 > last || !rit_par->isLineSeparator(body_pos - 1)))
1600                 body_pos = 0;
1601
1602         for (pos_type vpos = rit_pos; vpos < cursor_vpos; ++vpos) {
1603                 pos_type pos = vis2log(vpos);
1604                 if (body_pos > 0 && pos == body_pos - 1) {
1605                         x += fill_label_hfill +
1606                                 font_metrics::width(
1607                                         rit_par->layout()->labelsep, getLabelFont(rit_par));
1608                         if (rit_par->isLineSeparator(body_pos - 1))
1609                                 x -= singleWidth(rit_par, body_pos - 1);
1610                 }
1611
1612                 if (hfillExpansion(*this, rit, pos)) {
1613                         x += singleWidth(rit_par, pos);
1614                         if (pos >= body_pos)
1615                                 x += fill_hfill;
1616                         else
1617                                 x += fill_label_hfill;
1618                 } else if (rit_par->isSeparator(pos)) {
1619                         x += singleWidth(rit_par, pos);
1620                         if (pos >= body_pos)
1621                                 x += fill_separator;
1622                 } else
1623                         x += singleWidth(rit_par, pos);
1624         }
1625         return x;
1626 }
1627
1628
1629 void LyXText::setCursorIntern(ParagraphList::iterator pit,
1630                               pos_type pos, bool setfont, bool boundary)
1631 {
1632         UpdatableInset * it = pit->inInset();
1633         if (it) {
1634                 if (it != inset_owner) {
1635                         lyxerr[Debug::INSETS] << "InsetText   is " << it
1636                                               << endl
1637                                               << "inset_owner is "
1638                                               << inset_owner << endl;
1639 #ifdef WITH_WARNINGS
1640 #warning I believe this code is wrong. (Lgb)
1641 #warning Jürgen, have a look at this. (Lgb)
1642 #warning Hmmm, I guess you are right but we
1643 #warning should verify when this is needed
1644 #endif
1645                         // Jürgen, would you like to have a look?
1646                         // I guess we need to move the outer cursor
1647                         // and open and lock the inset (bla bla bla)
1648                         // stuff I don't know... so can you have a look?
1649                         // (Lgb)
1650                         // I moved the lyxerr stuff in here so we can see if
1651                         // this is actually really needed and where!
1652                         // (Jug)
1653                         // it->getLyXText(bv())->setCursorIntern(bv(), par, pos, setfont, boundary);
1654                         return;
1655                 }
1656         }
1657
1658         setCursor(cursor, pit, pos, boundary);
1659         if (setfont)
1660                 setCurrentFont();
1661 }
1662
1663
1664 void LyXText::setCurrentFont()
1665 {
1666         pos_type pos = cursor.pos();
1667         ParagraphList::iterator pit = cursor.par();
1668
1669         if (cursor.boundary() && pos > 0)
1670                 --pos;
1671
1672         if (pos > 0) {
1673                 if (pos == pit->size())
1674                         --pos;
1675                 else // potentional bug... BUG (Lgb)
1676                         if (pit->isSeparator(pos)) {
1677                                 if (pos > cursorRow()->pos() &&
1678                                     bidi_level(pos) % 2 ==
1679                                     bidi_level(pos - 1) % 2)
1680                                         --pos;
1681                                 else if (pos + 1 < pit->size())
1682                                         ++pos;
1683                         }
1684         }
1685
1686         current_font = pit->getFontSettings(bv()->buffer()->params, pos);
1687         real_current_font = getFont(pit, pos);
1688
1689         if (cursor.pos() == pit->size() &&
1690             isBoundary(bv()->buffer(), *pit, cursor.pos()) &&
1691             !cursor.boundary()) {
1692                 Language const * lang =
1693                         pit->getParLanguage(bv()->buffer()->params);
1694                 current_font.setLanguage(lang);
1695                 current_font.setNumber(LyXFont::OFF);
1696                 real_current_font.setLanguage(lang);
1697                 real_current_font.setNumber(LyXFont::OFF);
1698         }
1699 }
1700
1701
1702 // returns the column near the specified x-coordinate of the row
1703 // x is set to the real beginning of this column
1704 pos_type
1705 LyXText::getColumnNearX(RowList::iterator rit, int & x, bool & boundary) const
1706 {
1707         double tmpx = 0;
1708         double fill_separator;
1709         double fill_hfill;
1710         double fill_label_hfill;
1711
1712         prepareToPrint(rit, tmpx, fill_separator, fill_hfill, fill_label_hfill);
1713
1714         pos_type vc = rit->pos();
1715         pos_type last = lastPrintablePos(*this, rit);
1716         pos_type c = 0;
1717
1718         ParagraphList::iterator rit_par = rit->par();
1719         LyXLayout_ptr const & layout = rit->par()->layout();
1720
1721         bool left_side = false;
1722
1723         pos_type body_pos = rit_par->beginningOfBody();
1724         double last_tmpx = tmpx;
1725
1726         if (body_pos > 0 &&
1727             (body_pos - 1 > last ||
1728              !rit_par->isLineSeparator(body_pos - 1)))
1729                 body_pos = 0;
1730
1731         // check for empty row
1732         if (!rit_par->size()) {
1733                 x = int(tmpx);
1734                 return 0;
1735         }
1736
1737         while (vc <= last && tmpx <= x) {
1738                 c = vis2log(vc);
1739                 last_tmpx = tmpx;
1740                 if (body_pos > 0 && c == body_pos - 1) {
1741                         tmpx += fill_label_hfill +
1742                                 font_metrics::width(layout->labelsep, getLabelFont(rit_par));
1743                         if (rit_par->isLineSeparator(body_pos - 1))
1744                                 tmpx -= singleWidth(rit_par, body_pos - 1);
1745                 }
1746
1747                 if (hfillExpansion(*this, rit, c)) {
1748                         tmpx += singleWidth(rit_par, c);
1749                         if (c >= body_pos)
1750                                 tmpx += fill_hfill;
1751                         else
1752                                 tmpx += fill_label_hfill;
1753                 } else if (rit_par->isSeparator(c)) {
1754                         tmpx += singleWidth(rit_par, c);
1755                         if (c >= body_pos)
1756                                 tmpx += fill_separator;
1757                 } else {
1758                         tmpx += singleWidth(rit_par, c);
1759                 }
1760                 ++vc;
1761         }
1762
1763         if ((tmpx + last_tmpx) / 2 > x) {
1764                 tmpx = last_tmpx;
1765                 left_side = true;
1766         }
1767
1768         if (vc > last + 1)  // This shouldn't happen.
1769                 vc = last + 1;
1770
1771         boundary = false;
1772         // This (rtl_support test) is not needed, but gives
1773         // some speedup if rtl_support=false
1774         RowList::iterator next_rit = boost::next(rit);
1775
1776         bool const lastrow = lyxrc.rtl_support &&
1777                 (next_rit == rowlist_.end() ||
1778                  next_rit->par() != rit_par);
1779
1780         // If lastrow is false, we don't need to compute
1781         // the value of rtl.
1782         bool const rtl = (lastrow)
1783                 ? rit_par->isRightToLeftPar(bv()->buffer()->params)
1784                 : false;
1785         if (lastrow &&
1786                  ((rtl &&  left_side && vc == rit->pos() && x < tmpx - 5) ||
1787                    (!rtl && !left_side && vc == last + 1   && x > tmpx + 5)))
1788                 c = last + 1;
1789         else if (vc == rit->pos()) {
1790                 c = vis2log(vc);
1791                 if (bidi_level(c) % 2 == 1)
1792                         ++c;
1793         } else {
1794                 c = vis2log(vc - 1);
1795                 bool const rtl = (bidi_level(c) % 2 == 1);
1796                 if (left_side == rtl) {
1797                         ++c;
1798                         boundary = isBoundary(bv()->buffer(), *rit_par, c);
1799                 }
1800         }
1801
1802         if (rit->pos() <= last && c > last
1803             && rit_par->isNewline(last)) {
1804                 if (bidi_level(last) % 2 == 0)
1805                         tmpx -= singleWidth(rit_par, last);
1806                 else
1807                         tmpx += singleWidth(rit_par, last);
1808                 c = last;
1809         }
1810
1811         c -= rit->pos();
1812         x = int(tmpx);
1813         return c;
1814 }
1815
1816
1817 void LyXText::setCursorFromCoordinates(int x, int y)
1818 {
1819         //LyXCursor old_cursor = cursor;
1820         setCursorFromCoordinates(cursor, x, y);
1821         setCurrentFont();
1822 #warning DEPM disabled, otherwise crash when entering new table
1823         //deleteEmptyParagraphMechanism(old_cursor);
1824 }
1825
1826
1827 namespace {
1828
1829         /**
1830          * return true if the cursor given is at the end of a row,
1831          * and the next row is filled by an inset that spans an entire
1832          * row.
1833          */
1834         bool beforeFullRowInset(LyXText & lt, LyXCursor const & cur)
1835         {
1836                 RowList::iterator row = lt.getRow(cur);
1837                 if (boost::next(row) == lt.rows().end())
1838                         return false;
1839
1840                 Row const & next = *boost::next(row);
1841
1842                 if (next.pos() != cur.pos() || next.par() != cur.par())
1843                         return false;
1844
1845                 if (cur.pos() == cur.par()->size()
1846                     || !cur.par()->isInset(cur.pos()))
1847                         return false;
1848
1849                 InsetOld const * inset = cur.par()->getInset(cur.pos());
1850                 if (inset->needFullRow() || inset->display())
1851                         return true;
1852
1853                 return false;
1854         }
1855 }
1856
1857
1858 void LyXText::setCursorFromCoordinates(LyXCursor & cur, int x, int y)
1859 {
1860         // Get the row first.
1861
1862         RowList::iterator row = getRowNearY(y);
1863         bool bound = false;
1864         pos_type const column = getColumnNearX(row, x, bound);
1865         cur.par(row->par());
1866         cur.pos(row->pos() + column);
1867         cur.x(x);
1868         cur.y(y + row->baseline());
1869
1870         if (beforeFullRowInset(*this, cur)) {
1871                 pos_type const last = lastPrintablePos(*this, row);
1872                 RowList::iterator next_row = boost::next(row);
1873
1874                 float x = getCursorX(next_row, cur.pos(), last, bound);
1875                 cur.ix(int(x));
1876                 cur.iy(y + row->height() + next_row->baseline());
1877         } else {
1878                 cur.iy(cur.y());
1879                 cur.ix(cur.x());
1880         }
1881         cur.boundary(bound);
1882 }
1883
1884
1885 void LyXText::cursorLeft(bool internal)
1886 {
1887         if (cursor.pos() > 0) {
1888                 bool boundary = cursor.boundary();
1889                 setCursor(cursor.par(), cursor.pos() - 1, true, false);
1890                 if (!internal && !boundary &&
1891                     isBoundary(bv()->buffer(), *cursor.par(), cursor.pos() + 1))
1892                         setCursor(cursor.par(), cursor.pos() + 1, true, true);
1893         } else if (cursor.par() != ownerParagraphs().begin()) {
1894                 // steps into the paragraph above
1895                 ParagraphList::iterator pit = boost::prior(cursor.par());
1896                 setCursor(pit, pit->size());
1897         }
1898 }
1899
1900
1901 void LyXText::cursorRight(bool internal)
1902 {
1903         bool const at_end = (cursor.pos() == cursor.par()->size());
1904         bool const at_newline = !at_end &&
1905                 cursor.par()->isNewline(cursor.pos());
1906
1907         if (!internal && cursor.boundary() && !at_newline)
1908                 setCursor(cursor.par(), cursor.pos(), true, false);
1909         else if (!at_end) {
1910                 setCursor(cursor.par(), cursor.pos() + 1, true, false);
1911                 if (!internal &&
1912                     isBoundary(bv()->buffer(), *cursor.par(), cursor.pos()))
1913                         setCursor(cursor.par(), cursor.pos(), true, true);
1914         } else if (boost::next(cursor.par()) != ownerParagraphs().end())
1915                 setCursor(boost::next(cursor.par()), 0);
1916 }
1917
1918
1919 void LyXText::cursorUp(bool selecting)
1920 {
1921 #if 1
1922         int x = cursor.x_fix();
1923         int y = cursor.y() - cursorRow()->baseline() - 1;
1924         setCursorFromCoordinates(x, y);
1925         if (!selecting) {
1926                 int topy = top_y();
1927                 int y1 = cursor.iy() - topy;
1928                 int y2 = y1;
1929                 y -= topy;
1930                 InsetOld * inset_hit = checkInsetHit(x, y1);
1931                 if (inset_hit && isHighlyEditableInset(inset_hit)) {
1932                         inset_hit->localDispatch(
1933                                 FuncRequest(bv(), LFUN_INSET_EDIT, x, y - (y2 - y1), mouse_button::none));
1934                 }
1935         }
1936 #else
1937         setCursorFromCoordinates(bv(), cursor.x_fix(),
1938                                  cursor.y() - cursorRow()->baseline() - 1);
1939 #endif
1940 }
1941
1942
1943 void LyXText::cursorDown(bool selecting)
1944 {
1945 #if 1
1946         int x = cursor.x_fix();
1947         int y = cursor.y() - cursorRow()->baseline() + cursorRow()->height() + 1;
1948         setCursorFromCoordinates(x, y);
1949         if (!selecting && cursorRow() == cursorIRow()) {
1950                 int topy = top_y();
1951                 int y1 = cursor.iy() - topy;
1952                 int y2 = y1;
1953                 y -= topy;
1954                 InsetOld * inset_hit = checkInsetHit(x, y1);
1955                 if (inset_hit && isHighlyEditableInset(inset_hit)) {
1956                         FuncRequest cmd(bv(), LFUN_INSET_EDIT, x, y - (y2 - y1), mouse_button::none);
1957                         inset_hit->localDispatch(cmd);
1958                 }
1959         }
1960 #else
1961         setCursorFromCoordinates(bv(), cursor.x_fix(),
1962                                  cursor.y() - cursorRow()->baseline()
1963                                  + cursorRow()->height() + 1);
1964 #endif
1965 }
1966
1967
1968 void LyXText::cursorUpParagraph()
1969 {
1970         if (cursor.pos() > 0)
1971                 setCursor(cursor.par(), 0);
1972         else if (cursor.par() != ownerParagraphs().begin())
1973                 setCursor(boost::prior(cursor.par()), 0);
1974 }
1975
1976
1977 void LyXText::cursorDownParagraph()
1978 {
1979         ParagraphList::iterator par = cursor.par();
1980         ParagraphList::iterator next_par = boost::next(par);
1981
1982         if (next_par != ownerParagraphs().end())
1983                 setCursor(next_par, 0);
1984         else
1985                 setCursor(par, par->size());
1986 }
1987
1988
1989 // fix the cursor `cur' after a characters has been deleted at `where'
1990 // position. Called by deleteEmptyParagraphMechanism
1991 void LyXText::fixCursorAfterDelete(LyXCursor & cur,
1992                                    LyXCursor const & where)
1993 {
1994         // if cursor is not in the paragraph where the delete occured,
1995         // do nothing
1996         if (cur.par() != where.par())
1997                 return;
1998
1999         // if cursor position is after the place where the delete occured,
2000         // update it
2001         if (cur.pos() > where.pos())
2002                 cur.pos(cur.pos()-1);
2003
2004         // check also if we don't want to set the cursor on a spot behind the
2005         // pagragraph because we erased the last character.
2006         if (cur.pos() > cur.par()->size())
2007                 cur.pos(cur.par()->size());
2008
2009         // recompute row et al. for this cursor
2010         setCursor(cur, cur.par(), cur.pos(), cur.boundary());
2011 }
2012
2013
2014 bool LyXText::deleteEmptyParagraphMechanism(LyXCursor const & old_cursor)
2015 {
2016         // Would be wrong to delete anything if we have a selection.
2017         if (selection.set())
2018                 return false;
2019
2020         // We allow all kinds of "mumbo-jumbo" when freespacing.
2021         if (old_cursor.par()->layout()->free_spacing
2022             || old_cursor.par()->isFreeSpacing()) {
2023                 return false;
2024         }
2025
2026         /* Ok I'll put some comments here about what is missing.
2027            I have fixed BackSpace (and thus Delete) to not delete
2028            double-spaces automagically. I have also changed Cut,
2029            Copy and Paste to hopefully do some sensible things.
2030            There are still some small problems that can lead to
2031            double spaces stored in the document file or space at
2032            the beginning of paragraphs. This happens if you have
2033            the cursor betwenn to spaces and then save. Or if you
2034            cut and paste and the selection have a space at the
2035            beginning and then save right after the paste. I am
2036            sure none of these are very hard to fix, but I will
2037            put out 1.1.4pre2 with FIX_DOUBLE_SPACE defined so
2038            that I can get some feedback. (Lgb)
2039         */
2040
2041         // If old_cursor.pos() == 0 and old_cursor.pos()(1) == LineSeparator
2042         // delete the LineSeparator.
2043         // MISSING
2044
2045         // If old_cursor.pos() == 1 and old_cursor.pos()(0) == LineSeparator
2046         // delete the LineSeparator.
2047         // MISSING
2048
2049         // If the pos around the old_cursor were spaces, delete one of them.
2050         if (old_cursor.par() != cursor.par()
2051             || old_cursor.pos() != cursor.pos()) {
2052                 // Only if the cursor has really moved
2053
2054                 if (old_cursor.pos() > 0
2055                     && old_cursor.pos() < old_cursor.par()->size()
2056                     && old_cursor.par()->isLineSeparator(old_cursor.pos())
2057                     && old_cursor.par()->isLineSeparator(old_cursor.pos() - 1)) {
2058                         bool erased = old_cursor.par()->erase(old_cursor.pos() - 1);
2059                         redoParagraph(old_cursor.par());
2060
2061                         if (!erased)
2062                                 return false;
2063 #ifdef WITH_WARNINGS
2064 #warning This will not work anymore when we have multiple views of the same buffer
2065 // In this case, we will have to correct also the cursors held by
2066 // other bufferviews. It will probably be easier to do that in a more
2067 // automated way in LyXCursor code. (JMarc 26/09/2001)
2068 #endif
2069                         // correct all cursors held by the LyXText
2070                         fixCursorAfterDelete(cursor, old_cursor);
2071                         fixCursorAfterDelete(selection.cursor, old_cursor);
2072                         fixCursorAfterDelete(selection.start, old_cursor);
2073                         fixCursorAfterDelete(selection.end, old_cursor);
2074                         return false;
2075                 }
2076         }
2077
2078         // don't delete anything if this is the ONLY paragraph!
2079         if (ownerParagraphs().size() == 1)
2080                 return false;
2081
2082         // Do not delete empty paragraphs with keepempty set.
2083         if (old_cursor.par()->allowEmpty())
2084                 return false;
2085
2086         // only do our magic if we changed paragraph
2087         if (old_cursor.par() == cursor.par())
2088                 return false;
2089
2090         // record if we have deleted a paragraph
2091         // we can't possibly have deleted a paragraph before this point
2092         bool deleted = false;
2093
2094         if (old_cursor.par()->empty() ||
2095             (old_cursor.par()->size() == 1 &&
2096              old_cursor.par()->isLineSeparator(0))) {
2097                 // ok, we will delete anything
2098                 LyXCursor tmpcursor;
2099
2100                 deleted = true;
2101
2102                 bool selection_position_was_oldcursor_position = (
2103                         selection.cursor.par()  == old_cursor.par()
2104                         && selection.cursor.pos() == old_cursor.pos());
2105
2106                 if (getRow(old_cursor) != rows().begin()) {
2107                         RowList::iterator prevrow = boost::prior(getRow(old_cursor));
2108                         tmpcursor = cursor;
2109                         cursor = old_cursor; // that undo can restore the right cursor position
2110                         #warning FIXME. --end() iterator is usable here
2111                         ParagraphList::iterator endpit = boost::next(old_cursor.par());
2112                         while (endpit != ownerParagraphs().end() &&
2113                                endpit->getDepth()) {
2114                                 ++endpit;
2115                         }
2116
2117                         recordUndo(bv(), Undo::DELETE, old_cursor.par(),
2118                                 boost::prior(endpit));
2119                         cursor = tmpcursor;
2120
2121                         // delete old row
2122                         removeRow(getRow(old_cursor));
2123                         // delete old par
2124                         ownerParagraphs().erase(old_cursor.par());
2125
2126                         /* Breakagain the next par. Needed because of
2127                          * the parindent that can occur or dissappear.
2128                          * The next row can change its height, if
2129                          * there is another layout before */
2130                         RowList::iterator tmprit = boost::next(prevrow);
2131                         if (tmprit != rows().end()) {
2132                                 breakAgain(tmprit);
2133                                 updateCounters();
2134                         }
2135                         setHeightOfRow(prevrow);
2136                 } else {
2137                         RowList::iterator nextrow = boost::next(getRow(old_cursor));
2138
2139                         tmpcursor = cursor;
2140                         cursor = old_cursor; // that undo can restore the right cursor position
2141 #warning FIXME. --end() iterator is usable here
2142                         ParagraphList::iterator endpit = boost::next(old_cursor.par());
2143                         while (endpit != ownerParagraphs().end() &&
2144                                endpit->getDepth()) {
2145                                 ++endpit;
2146                         }
2147
2148                         recordUndo(bv(), Undo::DELETE, old_cursor.par(), boost::prior(endpit));
2149                         cursor = tmpcursor;
2150
2151                         // delete old row
2152                         removeRow(getRow(old_cursor));
2153                         // delete old par
2154                         ownerParagraphs().erase(old_cursor.par());
2155
2156                         /* Breakagain the next par. Needed because of
2157                            the parindent that can occur or dissappear.
2158                            The next row can change its height, if
2159                            there is another layout before */
2160                         if (nextrow != rows().end()) {
2161                                 breakAgain(nextrow);
2162                                 updateCounters();
2163                         }
2164                 }
2165
2166                 // correct cursor y
2167                 setCursorIntern(cursor.par(), cursor.pos());
2168
2169                 if (selection_position_was_oldcursor_position) {
2170                         // correct selection
2171                         selection.cursor = cursor;
2172                 }
2173         }
2174         if (!deleted) {
2175                 if (old_cursor.par()->stripLeadingSpaces()) {
2176                         redoParagraph(old_cursor.par());
2177                         // correct cursor y
2178                         setCursorIntern(cursor.par(), cursor.pos());
2179                         selection.cursor = cursor;
2180                 }
2181         }
2182         return deleted;
2183 }
2184
2185
2186 ParagraphList & LyXText::ownerParagraphs() const
2187 {
2188         if (inset_owner) {
2189                 return inset_owner->paragraphs;
2190         }
2191         return bv_owner->buffer()->paragraphs;
2192 }
2193
2194
2195 bool LyXText::isInInset() const
2196 {
2197         // Sub-level has non-null bv owner and
2198         // non-null inset owner.
2199         return inset_owner != 0 && bv_owner != 0;
2200 }
2201
2202
2203 int defaultRowHeight()
2204 {
2205         LyXFont const font(LyXFont::ALL_SANE);
2206         return int(font_metrics::maxAscent(font)
2207                  + font_metrics::maxDescent(font) * 1.5);
2208 }