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