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