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