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