]> git.lyx.org Git - lyx.git/blob - src/text2.C
Martin's fullRow patch
[lyx.git] / src / text2.C
1 /**
2  * \file text2.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Asger Alstrup
7  * \author Lars Gullik Bjønnes
8  * \author Alfredo Braunstein
9  * \author Jean-Marc Lasgouttes
10  * \author Angus Leeming
11  * \author John Levon
12  * \author André Pönitz
13  * \author Allan Rae
14  * \author Dekel Tsur
15  * \author Jürgen Vigna
16  *
17  * Full author contact details are available in file CREDITS.
18  */
19
20 #include <config.h>
21
22 #include "lyxtext.h"
23 #include "LString.h"
24 #include "Lsstream.h"
25 #include "paragraph.h"
26 #include "funcrequest.h"
27 #include "frontends/LyXView.h"
28 #include "undo_funcs.h"
29 #include "buffer.h"
30 #include "buffer_funcs.h"
31 #include "bufferparams.h"
32 #include "errorlist.h"
33 #include "gettext.h"
34 #include "BufferView.h"
35 #include "CutAndPaste.h"
36 #include "frontends/Painter.h"
37 #include "frontends/font_metrics.h"
38 #include "debug.h"
39 #include "lyxrc.h"
40 #include "FloatList.h"
41 #include "language.h"
42 #include "ParagraphParameters.h"
43 #include "counters.h"
44 #include "lyxrow_funcs.h"
45 #include "metricsinfo.h"
46 #include "paragraph_funcs.h"
47
48 #include "insets/insetbibitem.h"
49 #include "insets/insetenv.h"
50 #include "insets/insetfloat.h"
51 #include "insets/insetwrap.h"
52
53 #include "support/LAssert.h"
54 #include "support/textutils.h"
55 #include "support/lstrings.h"
56
57 #include <boost/tuple/tuple.hpp>
58
59 #include <algorithm>
60
61 using namespace lyx::support;
62
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();
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         if (cursor.par()->empty())
675                 return;
676
677         RowList::iterator rit = cursorRow();
678         RowList::iterator next_rit = boost::next(rit);
679         RowList::iterator end = boost::next(rit);
680         ParagraphList::iterator pit = cursor.par();
681         pos_type last_pos = lastPos(*pit, rit);
682
683         if (next_rit == end) {
684                 ++last_pos;
685         } else {
686                 if (pit->empty() ||
687                     (pit->getChar(last_pos) != ' ' && !pit->isNewline(last_pos))) {
688                         ++last_pos;
689                 }
690         }
691
692         setCursor(pit, last_pos);
693 }
694
695
696 void LyXText::cursorTop()
697 {
698         setCursor(ownerParagraphs().begin(), 0);
699 }
700
701
702 void LyXText::cursorBottom()
703 {
704         ParagraphList::iterator lastpit =
705                 boost::prior(ownerParagraphs().end());
706         setCursor(lastpit, lastpit->size());
707 }
708
709
710 void LyXText::toggleFree(LyXFont const & font, bool toggleall)
711 {
712         // If the mask is completely neutral, tell user
713         if (font == LyXFont(LyXFont::ALL_IGNORE)) {
714                 // Could only happen with user style
715                 bv()->owner()->message(_("No font change defined. Use Character under the Layout menu to define font change."));
716                 return;
717         }
718
719         // Try implicit word selection
720         // If there is a change in the language the implicit word selection
721         // is disabled.
722         LyXCursor resetCursor = cursor;
723         bool implicitSelection = (font.language() == ignore_language
724                                   && font.number() == LyXFont::IGNORE)
725                 ? selectWordWhenUnderCursor(lyx::WHOLE_WORD_STRICT) : false;
726
727         // Set font
728         setFont(font, toggleall);
729
730         // Implicit selections are cleared afterwards
731         //and cursor is set to the original position.
732         if (implicitSelection) {
733                 clearSelection();
734                 cursor = resetCursor;
735                 setCursor(cursor.par(), cursor.pos());
736                 selection.cursor = cursor;
737         }
738 }
739
740
741 string LyXText::getStringToIndex()
742 {
743         // Try implicit word selection
744         // If there is a change in the language the implicit word selection
745         // is disabled.
746         LyXCursor const reset_cursor = cursor;
747         bool const implicitSelection =
748                 selectWordWhenUnderCursor(lyx::PREVIOUS_WORD);
749
750         string idxstring;
751         if (!selection.set())
752                 bv()->owner()->message(_("Nothing to index!"));
753         else if (selection.start.par() != selection.end.par())
754                 bv()->owner()->message(_("Cannot index more than one paragraph!"));
755         else
756                 idxstring = selectionAsString(bv()->buffer(), false);
757
758         // Reset cursors to their original position.
759         cursor = reset_cursor;
760         setCursor(cursor.par(), cursor.pos());
761         selection.cursor = cursor;
762
763         // Clear the implicit selection.
764         if (implicitSelection)
765                 clearSelection();
766
767         return idxstring;
768 }
769
770
771 // the DTP switches for paragraphs. LyX will store them in the first
772 // physical paragraph. When a paragraph is broken, the top settings rest,
773 // the bottom settings are given to the new one. So I can make sure,
774 // they do not duplicate themself and you cannnot make dirty things with
775 // them!
776
777 void LyXText::setParagraph(bool line_top, bool line_bottom,
778                            bool pagebreak_top, bool pagebreak_bottom,
779                            VSpace const & space_top,
780                            VSpace const & space_bottom,
781                            Spacing const & spacing,
782                            LyXAlignment align,
783                            string const & labelwidthstring,
784                            bool noindent)
785 {
786         LyXCursor tmpcursor = cursor;
787         if (!selection.set()) {
788                 selection.start = cursor;
789                 selection.end = cursor;
790         }
791
792         // make sure that the depth behind the selection are restored, too
793         ParagraphList::iterator endpit = boost::next(selection.end.par());
794         ParagraphList::iterator undoendpit = endpit;
795         ParagraphList::iterator pars_end = ownerParagraphs().end();
796
797         if (endpit != pars_end && endpit->getDepth()) {
798                 while (endpit != pars_end && endpit->getDepth()) {
799                         ++endpit;
800                         undoendpit = endpit;
801                 }
802         } else if (endpit != pars_end) {
803                 // because of parindents etc.
804                 ++endpit;
805         }
806
807         recordUndo(bv(), Undo::ATOMIC, selection.start.par(),
808                 boost::prior(undoendpit));
809
810
811         ParagraphList::iterator tmppit = selection.end.par();
812
813         while (tmppit != boost::prior(selection.start.par())) {
814                 setCursor(tmppit, 0);
815
816                 ParagraphList::iterator pit = cursor.par();
817                 ParagraphParameters & params = pit->params();
818
819                 params.lineTop(line_top);
820                 params.lineBottom(line_bottom);
821                 params.pagebreakTop(pagebreak_top);
822                 params.pagebreakBottom(pagebreak_bottom);
823                 params.spaceTop(space_top);
824                 params.spaceBottom(space_bottom);
825                 params.spacing(spacing);
826                 // does the layout allow the new alignment?
827                 LyXLayout_ptr const & layout = pit->layout();
828
829                 if (align == LYX_ALIGN_LAYOUT)
830                         align = layout->align;
831                 if (align & layout->alignpossible) {
832                         if (align == layout->align)
833                                 params.align(LYX_ALIGN_LAYOUT);
834                         else
835                                 params.align(align);
836                 }
837                 pit->setLabelWidthString(labelwidthstring);
838                 params.noindent(noindent);
839                 tmppit = boost::prior(pit);
840         }
841
842         redoParagraphs(selection.start.par(), endpit);
843
844         clearSelection();
845         setCursor(selection.start.par(), selection.start.pos());
846         selection.cursor = cursor;
847         setCursor(selection.end.par(), selection.end.pos());
848         setSelection();
849         setCursor(tmpcursor.par(), tmpcursor.pos());
850         if (inset_owner)
851                 bv()->updateInset();
852 }
853
854
855 // set the counter of a paragraph. This includes the labels
856 void LyXText::setCounter(Buffer const * buf, ParagraphList::iterator pit)
857 {
858         LyXTextClass const & textclass = buf->params.getLyXTextClass();
859         LyXLayout_ptr const & layout = pit->layout();
860
861         if (pit != ownerParagraphs().begin()) {
862
863                 pit->params().appendix(boost::prior(pit)->params().appendix());
864                 if (!pit->params().appendix() &&
865                     pit->params().startOfAppendix()) {
866                         pit->params().appendix(true);
867                         textclass.counters().reset();
868                 }
869                 pit->enumdepth = boost::prior(pit)->enumdepth;
870                 pit->itemdepth = boost::prior(pit)->itemdepth;
871         } else {
872                 pit->params().appendix(pit->params().startOfAppendix());
873                 pit->enumdepth = 0;
874                 pit->itemdepth = 0;
875         }
876
877         // Maybe we have to increment the enumeration depth.
878         // BUT, enumeration in a footnote is considered in isolation from its
879         //      surrounding paragraph so don't increment if this is the
880         //      first line of the footnote
881         // AND, bibliographies can't have their depth changed ie. they
882         //      are always of depth 0
883         if (pit != ownerParagraphs().begin()
884             && boost::prior(pit)->getDepth() < pit->getDepth()
885             && boost::prior(pit)->layout()->labeltype == LABEL_COUNTER_ENUMI
886             && pit->enumdepth < 3
887             && layout->labeltype != LABEL_BIBLIO) {
888                 pit->enumdepth++;
889         }
890
891         // Maybe we have to decrement the enumeration depth, see note above
892         if (pit != ownerParagraphs().begin()
893             && boost::prior(pit)->getDepth() > pit->getDepth()
894             && layout->labeltype != LABEL_BIBLIO) {
895                 pit->enumdepth = depthHook(pit, ownerParagraphs(),
896                                            pit->getDepth())->enumdepth;
897         }
898
899         if (!pit->params().labelString().empty()) {
900                 pit->params().labelString(string());
901         }
902
903         if (layout->margintype == MARGIN_MANUAL) {
904                 if (pit->params().labelWidthString().empty())
905                         pit->setLabelWidthString(layout->labelstring());
906         } else {
907                 pit->setLabelWidthString(string());
908         }
909
910         // is it a layout that has an automatic label?
911         if (layout->labeltype >= LABEL_COUNTER_CHAPTER) {
912                 int const i = layout->labeltype - LABEL_COUNTER_CHAPTER;
913
914                 ostringstream s;
915
916                 if (i >= 0 && i <= buf->params.secnumdepth) {
917                         string numbertype;
918                         string langtype;
919
920                         textclass.counters().step(layout->latexname());
921
922                         // Is there a label? Useful for Chapter layout
923                         if (!pit->params().appendix()) {
924                                 s << buf->B_(layout->labelstring());
925                         } else {
926                                 s << buf->B_(layout->labelstring_appendix());
927                         }
928
929                         // Use of an integer is here less than elegant. For now.
930                         int head = textclass.maxcounter() - LABEL_COUNTER_CHAPTER;
931                         if (!pit->params().appendix()) {
932                                 numbertype = "sectioning";
933                         } else {
934                                 numbertype = "appendix";
935                                 if (pit->isRightToLeftPar(buf->params))
936                                         langtype = "hebrew";
937                                 else
938                                         langtype = "latin";
939                         }
940
941                         s << " "
942                           << textclass.counters()
943                                 .numberLabel(layout->latexname(),
944                                              numbertype, langtype, head);
945
946                         pit->params().labelString(STRCONV(s.str()));
947
948                         // reset enum counters
949                         textclass.counters().reset("enum");
950                 } else if (layout->labeltype < LABEL_COUNTER_ENUMI) {
951                         textclass.counters().reset("enum");
952                 } else if (layout->labeltype == LABEL_COUNTER_ENUMI) {
953                         // FIXME
954                         // Yes I know this is a really, really! bad solution
955                         // (Lgb)
956                         string enumcounter("enum");
957
958                         switch (pit->enumdepth) {
959                         case 2:
960                                 enumcounter += 'i';
961                         case 1:
962                                 enumcounter += 'i';
963                         case 0:
964                                 enumcounter += 'i';
965                                 break;
966                         case 3:
967                                 enumcounter += "iv";
968                                 break;
969                         default:
970                                 // not a valid enumdepth...
971                                 break;
972                         }
973
974                         textclass.counters().step(enumcounter);
975
976                         s << textclass.counters()
977                                 .numberLabel(enumcounter, "enumeration");
978                         pit->params().labelString(STRCONV(s.str()));
979                 }
980         } else if (layout->labeltype == LABEL_BIBLIO) {// ale970302
981                 textclass.counters().step("bibitem");
982                 int number = textclass.counters().value("bibitem");
983                 if (pit->bibitem()) {
984                         pit->bibitem()->setCounter(number);
985                         pit->params().labelString(layout->labelstring());
986                 }
987                 // In biblio should't be following counters but...
988         } else {
989                 string s = buf->B_(layout->labelstring());
990
991                 // the caption hack:
992                 if (layout->labeltype == LABEL_SENSITIVE) {
993                         ParagraphList::iterator end = ownerParagraphs().end();
994                         ParagraphList::iterator tmppit = pit;
995                         InsetOld * in = 0;
996                         bool isOK = false;
997                         while (tmppit != end && tmppit->inInset()
998                                // the single '=' is intended below
999                                && (in = tmppit->inInset()->owner()))
1000                         {
1001                                 if (in->lyxCode() == InsetOld::FLOAT_CODE ||
1002                                     in->lyxCode() == InsetOld::WRAP_CODE) {
1003                                         isOK = true;
1004                                         break;
1005                                 } else {
1006                                         tmppit = ownerParagraphs().begin();
1007                                         for ( ; tmppit != end; ++tmppit)
1008                                                 if (&*tmppit == in->parOwner())
1009                                                         break;
1010                                 }
1011                         }
1012
1013                         if (isOK) {
1014                                 string type;
1015
1016                                 if (in->lyxCode() == InsetOld::FLOAT_CODE)
1017                                         type = static_cast<InsetFloat*>(in)->params().type;
1018                                 else if (in->lyxCode() == InsetOld::WRAP_CODE)
1019                                         type = static_cast<InsetWrap*>(in)->params().type;
1020                                 else
1021                                         Assert(0);
1022
1023                                 Floating const & fl = textclass.floats().getType(type);
1024
1025                                 textclass.counters().step(fl.type());
1026
1027                                 // Doesn't work... yet.
1028                                 s = bformat(_("%1$s #:"), buf->B_(fl.name()));
1029                         } else {
1030                                 // par->SetLayout(0);
1031                                 // s = layout->labelstring;
1032                                 s = _("Senseless: ");
1033                         }
1034                 }
1035                 pit->params().labelString(s);
1036
1037                 // reset the enumeration counter. They are always reset
1038                 // when there is any other layout between
1039                 // Just fall-through between the cases so that all
1040                 // enum counters deeper than enumdepth is also reset.
1041                 switch (pit->enumdepth) {
1042                 case 0:
1043                         textclass.counters().reset("enumi");
1044                 case 1:
1045                         textclass.counters().reset("enumii");
1046                 case 2:
1047                         textclass.counters().reset("enumiii");
1048                 case 3:
1049                         textclass.counters().reset("enumiv");
1050                 }
1051         }
1052 }
1053
1054
1055 // Updates all counters. Paragraphs with changed label string will be rebroken
1056 void LyXText::updateCounters()
1057 {
1058         // start over
1059         bv()->buffer()->params.getLyXTextClass().counters().reset();
1060
1061         ParagraphList::iterator beg = ownerParagraphs().begin();
1062         ParagraphList::iterator end = ownerParagraphs().end();
1063         for (ParagraphList::iterator pit = beg; pit != end; ++pit) {
1064                 string const oldLabel = pit->params().labelString();
1065
1066                 size_t maxdepth = 0;
1067                 if (pit != beg)
1068                         maxdepth = boost::prior(pit)->getMaxDepthAfter();
1069
1070                 if (pit->params().depth() > maxdepth)
1071                         pit->params().depth(maxdepth);
1072
1073                 // setCounter can potentially change the labelString.
1074                 setCounter(bv()->buffer(), pit);
1075
1076                 string const & newLabel = pit->params().labelString();
1077
1078                 if (oldLabel != newLabel)
1079                         redoParagraph(pit);
1080         }
1081 }
1082
1083
1084 void LyXText::insertInset(InsetOld * inset)
1085 {
1086         if (!cursor.par()->insetAllowed(inset->lyxCode()))
1087                 return;
1088         recordUndo(bv(), Undo::ATOMIC, cursor.par());
1089         freezeUndo();
1090         cursor.par()->insertInset(cursor.pos(), inset);
1091         // Just to rebreak and refresh correctly.
1092         // The character will not be inserted a second time
1093         insertChar(Paragraph::META_INSET);
1094         // If we enter a highly editable inset the cursor should be before
1095         // the inset. After an Undo LyX tries to call inset->edit(...) 
1096         // and fails if the cursor is behind the inset and getInset
1097         // does not return the inset!
1098         if (isHighlyEditableInset(inset))
1099                 cursorLeft(true);
1100         unFreezeUndo();
1101 }
1102
1103
1104 void LyXText::cutSelection(bool doclear, bool realcut)
1105 {
1106         // Stuff what we got on the clipboard. Even if there is no selection.
1107
1108         // There is a problem with having the stuffing here in that the
1109         // larger the selection the slower LyX will get. This can be
1110         // solved by running the line below only when the selection has
1111         // finished. The solution used currently just works, to make it
1112         // faster we need to be more clever and probably also have more
1113         // calls to stuffClipboard. (Lgb)
1114         bv()->stuffClipboard(selectionAsString(bv()->buffer(), true));
1115
1116         // This doesn't make sense, if there is no selection
1117         if (!selection.set())
1118                 return;
1119
1120         // OK, we have a selection. This is always between selection.start
1121         // and selection.end
1122
1123         // make sure that the depth behind the selection are restored, too
1124         ParagraphList::iterator endpit = boost::next(selection.end.par());
1125         ParagraphList::iterator undoendpit = endpit;
1126         ParagraphList::iterator pars_end = ownerParagraphs().end();
1127
1128         if (endpit != pars_end && endpit->getDepth()) {
1129                 while (endpit != pars_end && endpit->getDepth()) {
1130                         ++endpit;
1131                         undoendpit = endpit;
1132                 }
1133         } else if (endpit != pars_end) {
1134                 // because of parindents etc.
1135                 ++endpit;
1136         }
1137
1138         recordUndo(bv(), Undo::DELETE, selection.start.par(),
1139                    boost::prior(undoendpit));
1140
1141         endpit = selection.end.par();
1142         int endpos = selection.end.pos();
1143
1144         boost::tie(endpit, endpos) = realcut ?
1145                 CutAndPaste::cutSelection(bv()->buffer()->params,
1146                                           ownerParagraphs(),
1147                                           selection.start.par(), endpit,
1148                                           selection.start.pos(), endpos,
1149                                           bv()->buffer()->params.textclass,
1150                                           doclear)
1151                 : CutAndPaste::eraseSelection(bv()->buffer()->params,
1152                                               ownerParagraphs(),
1153                                               selection.start.par(), endpit,
1154                                               selection.start.pos(), endpos,
1155                                               doclear);
1156         // sometimes necessary
1157         if (doclear)
1158                 selection.start.par()->stripLeadingSpaces();
1159
1160         redoParagraphs(selection.start.par(), boost::next(endpit));
1161         // cutSelection can invalidate the cursor so we need to set
1162         // it anew. (Lgb)
1163         // we prefer the end for when tracking changes
1164         cursor.pos(endpos);
1165         cursor.par(endpit);
1166
1167         // need a valid cursor. (Lgb)
1168         clearSelection();
1169
1170         setCursor(cursor.par(), cursor.pos());
1171         selection.cursor = cursor;
1172         updateCounters();
1173 }
1174
1175
1176 void LyXText::copySelection()
1177 {
1178         // stuff the selection onto the X clipboard, from an explicit copy request
1179         bv()->stuffClipboard(selectionAsString(bv()->buffer(), true));
1180
1181         // this doesnt make sense, if there is no selection
1182         if (!selection.set())
1183                 return;
1184
1185         // ok we have a selection. This is always between selection.start
1186         // and sel_end cursor
1187
1188         // copy behind a space if there is one
1189         while (selection.start.par()->size() > selection.start.pos()
1190                && selection.start.par()->isLineSeparator(selection.start.pos())
1191                && (selection.start.par() != selection.end.par()
1192                    || selection.start.pos() < selection.end.pos()))
1193                 selection.start.pos(selection.start.pos() + 1);
1194
1195         CutAndPaste::copySelection(selection.start.par(),
1196                                    selection.end.par(),
1197                                    selection.start.pos(), selection.end.pos(),
1198                                    bv()->buffer()->params.textclass);
1199 }
1200
1201
1202 void LyXText::pasteSelection(size_t sel_index)
1203 {
1204         // this does not make sense, if there is nothing to paste
1205         if (!CutAndPaste::checkPastePossible())
1206                 return;
1207
1208         recordUndo(bv(), Undo::INSERT, cursor.par());
1209
1210         ParagraphList::iterator endpit;
1211         PitPosPair ppp;
1212
1213         ErrorList el;
1214
1215         boost::tie(ppp, endpit) =
1216                 CutAndPaste::pasteSelection(*bv()->buffer(),
1217                                             ownerParagraphs(),
1218                                             cursor.par(), cursor.pos(),
1219                                             bv()->buffer()->params.textclass,
1220                                             sel_index, el);
1221         bufferErrors(*bv()->buffer(), el);
1222         bv()->showErrorList(_("Paste"));
1223
1224         redoParagraphs(cursor.par(), endpit);
1225
1226         setCursor(cursor.par(), cursor.pos());
1227         clearSelection();
1228
1229         selection.cursor = cursor;
1230         setCursor(ppp.first, ppp.second);
1231         setSelection();
1232         updateCounters();
1233 }
1234
1235
1236 void LyXText::setSelectionRange(lyx::pos_type length)
1237 {
1238         if (!length)
1239                 return;
1240
1241         selection.cursor = cursor;
1242         while (length--)
1243                 cursorRight(bv());
1244         setSelection();
1245 }
1246
1247
1248 // simple replacing. The font of the first selected character is used
1249 void LyXText::replaceSelectionWithString(string const & str)
1250 {
1251         recordUndo(bv(), Undo::ATOMIC);
1252         freezeUndo();
1253
1254         if (!selection.set()) { // create a dummy selection
1255                 selection.end = cursor;
1256                 selection.start = cursor;
1257         }
1258
1259         // Get font setting before we cut
1260         pos_type pos = selection.end.pos();
1261         LyXFont const font = selection.start.par()
1262                 ->getFontSettings(bv()->buffer()->params,
1263                                   selection.start.pos());
1264
1265         // Insert the new string
1266         string::const_iterator cit = str.begin();
1267         string::const_iterator end = str.end();
1268         for (; cit != end; ++cit) {
1269                 selection.end.par()->insertChar(pos, (*cit), font);
1270                 ++pos;
1271         }
1272
1273         // Cut the selection
1274         cutSelection(true, false);
1275
1276         unFreezeUndo();
1277 }
1278
1279
1280 // needed to insert the selection
1281 void LyXText::insertStringAsLines(string const & str)
1282 {
1283         ParagraphList::iterator pit = cursor.par();
1284         pos_type pos = cursor.pos();
1285         ParagraphList::iterator endpit = boost::next(cursor.par());
1286
1287         recordUndo(bv(), Undo::ATOMIC);
1288
1289         // only to be sure, should not be neccessary
1290         clearSelection();
1291
1292         bv()->buffer()->insertStringAsLines(pit, pos, current_font, str);
1293
1294         redoParagraphs(cursor.par(), endpit);
1295         setCursor(cursor.par(), cursor.pos());
1296         selection.cursor = cursor;
1297         setCursor(pit, pos);
1298         setSelection();
1299 }
1300
1301
1302 // turns double-CR to single CR, others where converted into one
1303 // blank. Then InsertStringAsLines is called
1304 void LyXText::insertStringAsParagraphs(string const & str)
1305 {
1306         string linestr(str);
1307         bool newline_inserted = false;
1308         string::size_type const siz = linestr.length();
1309
1310         for (string::size_type i = 0; i < siz; ++i) {
1311                 if (linestr[i] == '\n') {
1312                         if (newline_inserted) {
1313                                 // we know that \r will be ignored by
1314                                 // InsertStringA. Of course, it is a dirty
1315                                 // trick, but it works...
1316                                 linestr[i - 1] = '\r';
1317                                 linestr[i] = '\n';
1318                         } else {
1319                                 linestr[i] = ' ';
1320                                 newline_inserted = true;
1321                         }
1322                 } else if (IsPrintable(linestr[i])) {
1323                         newline_inserted = false;
1324                 }
1325         }
1326         insertStringAsLines(linestr);
1327 }
1328
1329
1330 bool LyXText::setCursor(ParagraphList::iterator pit,
1331                         pos_type pos,
1332                         bool setfont, bool boundary)
1333 {
1334         LyXCursor old_cursor = cursor;
1335         setCursorIntern(pit, pos, setfont, boundary);
1336         return deleteEmptyParagraphMechanism(old_cursor);
1337 }
1338
1339
1340 void LyXText::redoCursor()
1341 {
1342 #warning maybe the same for selections?
1343         setCursor(cursor, cursor.par(), cursor.pos(), cursor.boundary());
1344 }
1345
1346
1347 void LyXText::setCursor(LyXCursor & cur, ParagraphList::iterator pit,
1348                         pos_type pos, bool boundary)
1349 {
1350         Assert(pit != ownerParagraphs().end());
1351
1352         cur.par(pit);
1353         cur.pos(pos);
1354         cur.boundary(boundary);
1355         if (noRows())
1356                 return;
1357
1358         // get the cursor y position in text
1359
1360         RowList::iterator row = getRow(pit, pos);
1361         int y = row->y();
1362         
1363         RowList::iterator old_row = row;
1364         // if we are before the first char of this row and are still in the
1365         // same paragraph and there is a previous row then put the cursor on
1366         // the end of the previous row
1367         cur.iy(y + row->baseline());
1368
1369         // y is now the beginning of the cursor row
1370         y += row->baseline();
1371         // y is now the cursor baseline
1372         cur.y(y);
1373
1374         pos_type last = lastPrintablePos(*pit, old_row);
1375
1376         // None of these should happen, but we're scaredy-cats
1377         if (pos > pit->size()) {
1378                 lyxerr << "dont like 1, pos: " << pos << " size: " << pit->size() << endl;
1379                 pos = 0;
1380                 cur.pos(0);
1381         } else if (pos > last + 1) {
1382                 lyxerr << "dont like 2 please report" << endl;
1383                 // This shouldn't happen.
1384                 pos = last + 1;
1385                 cur.pos(pos);
1386         } else if (pos < row->pos()) {
1387                 lyxerr << "dont like 3 please report" << endl;
1388                 pos = row->pos();
1389                 cur.pos(pos);
1390         }
1391
1392         // now get the cursors x position
1393         float x = getCursorX(pit, row, pos, last, boundary);
1394         cur.x(int(x));
1395         cur.x_fix(cur.x());
1396         if (old_row != row) {
1397                 x = getCursorX(pit, old_row, pos, last, boundary);
1398                 cur.ix(int(x));
1399         } else
1400                 cur.ix(cur.x());
1401 /* We take out this for the time being because 1) the redraw code is not
1402    prepared to this yet and 2) because some good policy has yet to be decided
1403    while editting: for instance how to act on rows being created/deleted
1404    because of DEPM.
1405 */
1406 #if 0
1407         //if the cursor is in a visible row, anchor to it
1408         int topy = top_y();
1409         if (topy < y && y < topy + bv()->workHeight())
1410                 anchor_row(row);
1411 #endif
1412 }
1413
1414
1415 float LyXText::getCursorX(ParagraphList::iterator pit, RowList::iterator rit,
1416                           pos_type pos, pos_type last, bool boundary) const
1417 {
1418         pos_type cursor_vpos    = 0;
1419         double x                = rit->x();
1420         double fill_separator   = rit->fill_separator();
1421         double fill_hfill       = rit->fill_hfill();
1422         double fill_label_hfill = rit->fill_label_hfill();
1423         pos_type const rit_pos  = rit->pos();
1424
1425         if (last < rit_pos)
1426                 cursor_vpos = rit_pos;
1427         else if (pos > last && !boundary)
1428                 cursor_vpos = (pit->isRightToLeftPar(bv()->buffer()->params))
1429                         ? rit_pos : last + 1;
1430         else if (pos > rit_pos && (pos > last || boundary))
1431                 // Place cursor after char at (logical) position pos - 1
1432                 cursor_vpos = (bidi_level(pos - 1) % 2 == 0)
1433                         ? log2vis(pos - 1) + 1 : log2vis(pos - 1);
1434         else
1435                 // Place cursor before char at (logical) position pos
1436                 cursor_vpos = (bidi_level(pos) % 2 == 0)
1437                         ? log2vis(pos) : log2vis(pos) + 1;
1438
1439         pos_type body_pos = pit->beginningOfBody();
1440         if (body_pos > 0 &&
1441             (body_pos - 1 > last || !pit->isLineSeparator(body_pos - 1)))
1442                 body_pos = 0;
1443
1444         for (pos_type vpos = rit_pos; vpos < cursor_vpos; ++vpos) {
1445                 pos_type pos = vis2log(vpos);
1446                 if (body_pos > 0 && pos == body_pos - 1) {
1447                         x += fill_label_hfill +
1448                                 font_metrics::width(
1449                                         pit->layout()->labelsep, getLabelFont(pit));
1450                         if (pit->isLineSeparator(body_pos - 1))
1451                                 x -= singleWidth(pit, body_pos - 1);
1452                 }
1453
1454                 if (hfillExpansion(*pit, rit, pos)) {
1455                         x += singleWidth(pit, pos);
1456                         if (pos >= body_pos)
1457                                 x += fill_hfill;
1458                         else
1459                                 x += fill_label_hfill;
1460                 } else if (pit->isSeparator(pos)) {
1461                         x += singleWidth(pit, pos);
1462                         if (pos >= body_pos)
1463                                 x += fill_separator;
1464                 } else
1465                         x += singleWidth(pit, pos);
1466         }
1467         return x;
1468 }
1469
1470
1471 void LyXText::setCursorIntern(ParagraphList::iterator pit,
1472                               pos_type pos, bool setfont, bool boundary)
1473 {
1474         setCursor(cursor, pit, pos, boundary);
1475         if (setfont)
1476                 setCurrentFont();
1477 }
1478
1479
1480 void LyXText::setCurrentFont()
1481 {
1482         pos_type pos = cursor.pos();
1483         ParagraphList::iterator pit = cursor.par();
1484
1485         if (cursor.boundary() && pos > 0)
1486                 --pos;
1487
1488         if (pos > 0) {
1489                 if (pos == pit->size())
1490                         --pos;
1491                 else // potentional bug... BUG (Lgb)
1492                         if (pit->isSeparator(pos)) {
1493                                 if (pos > cursorRow()->pos() &&
1494                                     bidi_level(pos) % 2 ==
1495                                     bidi_level(pos - 1) % 2)
1496                                         --pos;
1497                                 else if (pos + 1 < pit->size())
1498                                         ++pos;
1499                         }
1500         }
1501
1502         current_font = pit->getFontSettings(bv()->buffer()->params, pos);
1503         real_current_font = getFont(pit, pos);
1504
1505         if (cursor.pos() == pit->size() &&
1506             isBoundary(bv()->buffer(), *pit, cursor.pos()) &&
1507             !cursor.boundary()) {
1508                 Language const * lang =
1509                         pit->getParLanguage(bv()->buffer()->params);
1510                 current_font.setLanguage(lang);
1511                 current_font.setNumber(LyXFont::OFF);
1512                 real_current_font.setLanguage(lang);
1513                 real_current_font.setNumber(LyXFont::OFF);
1514         }
1515 }
1516
1517
1518 // returns the column near the specified x-coordinate of the row
1519 // x is set to the real beginning of this column
1520 pos_type LyXText::getColumnNearX(ParagraphList::iterator pit,
1521         RowList::iterator rit, int & x, bool & boundary) const
1522 {
1523         double tmpx             = rit->x();
1524         double fill_separator   = rit->fill_separator();
1525         double fill_hfill       = rit->fill_hfill();
1526         double fill_label_hfill = rit->fill_label_hfill();
1527
1528         pos_type vc = rit->pos();
1529         pos_type last = lastPrintablePos(*pit, rit);
1530         pos_type c = 0;
1531         LyXLayout_ptr const & layout = pit->layout();
1532
1533         bool left_side = false;
1534
1535         pos_type body_pos = pit->beginningOfBody();
1536         double last_tmpx = tmpx;
1537
1538         if (body_pos > 0 &&
1539             (body_pos - 1 > last ||
1540              !pit->isLineSeparator(body_pos - 1)))
1541                 body_pos = 0;
1542
1543         // check for empty row
1544         if (!pit->size()) {
1545                 x = int(tmpx);
1546                 return 0;
1547         }
1548
1549         while (vc <= last && tmpx <= x) {
1550                 c = vis2log(vc);
1551                 last_tmpx = tmpx;
1552                 if (body_pos > 0 && c == body_pos - 1) {
1553                         tmpx += fill_label_hfill +
1554                                 font_metrics::width(layout->labelsep, getLabelFont(pit));
1555                         if (pit->isLineSeparator(body_pos - 1))
1556                                 tmpx -= singleWidth(pit, body_pos - 1);
1557                 }
1558
1559                 if (hfillExpansion(*pit, rit, c)) {
1560                         tmpx += singleWidth(pit, c);
1561                         if (c >= body_pos)
1562                                 tmpx += fill_hfill;
1563                         else
1564                                 tmpx += fill_label_hfill;
1565                 } else if (pit->isSeparator(c)) {
1566                         tmpx += singleWidth(pit, c);
1567                         if (c >= body_pos)
1568                                 tmpx += fill_separator;
1569                 } else {
1570                         tmpx += singleWidth(pit, c);
1571                 }
1572                 ++vc;
1573         }
1574
1575         if ((tmpx + last_tmpx) / 2 > x) {
1576                 tmpx = last_tmpx;
1577                 left_side = true;
1578         }
1579
1580         if (vc > last + 1)  // This shouldn't happen.
1581                 vc = last + 1;
1582
1583         boundary = false;
1584         // This (rtl_support test) is not needed, but gives
1585         // some speedup if rtl_support == false
1586         bool const lastrow = lyxrc.rtl_support
1587                         && boost::next(rit) == pit->rows.end();
1588
1589         // If lastrow is false, we don't need to compute
1590         // the value of rtl.
1591         bool const rtl = (lastrow)
1592                 ? pit->isRightToLeftPar(bv()->buffer()->params)
1593                 : false;
1594         if (lastrow &&
1595                  ((rtl  &&  left_side && vc == rit->pos() && x < tmpx - 5) ||
1596                   (!rtl && !left_side && vc == last + 1   && x > tmpx + 5)))
1597                 c = last + 1;
1598         else if (vc == rit->pos()) {
1599                 c = vis2log(vc);
1600                 if (bidi_level(c) % 2 == 1)
1601                         ++c;
1602         } else {
1603                 c = vis2log(vc - 1);
1604                 bool const rtl = (bidi_level(c) % 2 == 1);
1605                 if (left_side == rtl) {
1606                         ++c;
1607                         boundary = isBoundary(bv()->buffer(), *pit, c);
1608                 }
1609         }
1610
1611         if (rit->pos() <= last && c > last && pit->isNewline(last)) {
1612                 if (bidi_level(last) % 2 == 0)
1613                         tmpx -= singleWidth(pit, last);
1614                 else
1615                         tmpx += singleWidth(pit, last);
1616                 c = last;
1617         }
1618
1619         c -= rit->pos();
1620         x = int(tmpx);
1621         return c;
1622 }
1623
1624
1625 void LyXText::setCursorFromCoordinates(int x, int y)
1626 {
1627         LyXCursor old_cursor = cursor;
1628         setCursorFromCoordinates(cursor, x, y);
1629         setCurrentFont();
1630         deleteEmptyParagraphMechanism(old_cursor);
1631 }
1632
1633
1634 void LyXText::setCursorFromCoordinates(LyXCursor & cur, int x, int y)
1635 {
1636         // Get the row first.
1637         ParagraphList::iterator pit;
1638         RowList::iterator rit = getRowNearY(y, pit);
1639         y = rit->y();
1640
1641         bool bound = false;
1642         pos_type const column = getColumnNearX(pit, rit, x, bound);
1643         cur.par(pit);
1644         cur.pos(rit->pos() + column);
1645         cur.x(x);
1646         cur.y(y + rit->baseline());
1647
1648         cur.iy(cur.y());
1649         cur.ix(cur.x());
1650
1651         cur.boundary(bound);
1652 }
1653
1654
1655 void LyXText::cursorLeft(bool internal)
1656 {
1657         if (cursor.pos() > 0) {
1658                 bool boundary = cursor.boundary();
1659                 setCursor(cursor.par(), cursor.pos() - 1, true, false);
1660                 if (!internal && !boundary &&
1661                     isBoundary(bv()->buffer(), *cursor.par(), cursor.pos() + 1))
1662                         setCursor(cursor.par(), cursor.pos() + 1, true, true);
1663         } else if (cursor.par() != ownerParagraphs().begin()) {
1664                 // steps into the paragraph above
1665                 ParagraphList::iterator pit = boost::prior(cursor.par());
1666                 setCursor(pit, pit->size());
1667         }
1668 }
1669
1670
1671 void LyXText::cursorRight(bool internal)
1672 {
1673         bool const at_end = (cursor.pos() == cursor.par()->size());
1674         bool const at_newline = !at_end &&
1675                 cursor.par()->isNewline(cursor.pos());
1676
1677         if (!internal && cursor.boundary() && !at_newline)
1678                 setCursor(cursor.par(), cursor.pos(), true, false);
1679         else if (!at_end) {
1680                 setCursor(cursor.par(), cursor.pos() + 1, true, false);
1681                 if (!internal &&
1682                     isBoundary(bv()->buffer(), *cursor.par(), cursor.pos()))
1683                         setCursor(cursor.par(), cursor.pos(), true, true);
1684         } else if (boost::next(cursor.par()) != ownerParagraphs().end())
1685                 setCursor(boost::next(cursor.par()), 0);
1686 }
1687
1688
1689 void LyXText::cursorUp(bool selecting)
1690 {
1691 #if 1
1692         int x = cursor.x_fix();
1693         int y = cursor.y() - cursorRow()->baseline() - 1;
1694         setCursorFromCoordinates(x, y);
1695         if (!selecting) {
1696                 int topy = top_y();
1697                 int y1 = cursor.iy() - topy;
1698                 int y2 = y1;
1699                 y -= topy;
1700                 InsetOld * inset_hit = checkInsetHit(x, y1);
1701                 if (inset_hit && isHighlyEditableInset(inset_hit)) {
1702                         inset_hit->localDispatch(
1703                                 FuncRequest(bv(), LFUN_INSET_EDIT, x, y - (y2 - y1), mouse_button::none));
1704                 }
1705         }
1706 #else
1707         lyxerr << "cursorUp: y " << cursor.y() << " bl: " <<
1708                 cursorRow()->baseline() << endl;
1709         setCursorFromCoordinates(cursor.x_fix(),
1710                 cursor.y() - cursorRow()->baseline() - 1);
1711 #endif
1712 }
1713
1714
1715 void LyXText::cursorDown(bool selecting)
1716 {
1717 #if 1
1718         int x = cursor.x_fix();
1719         int y = cursor.y() - cursorRow()->baseline() + cursorRow()->height() + 1;
1720         setCursorFromCoordinates(x, y);
1721         if (!selecting && cursorRow() == cursorIRow()) {
1722                 int topy = top_y();
1723                 int y1 = cursor.iy() - topy;
1724                 int y2 = y1;
1725                 y -= topy;
1726                 InsetOld * inset_hit = checkInsetHit(x, y1);
1727                 if (inset_hit && isHighlyEditableInset(inset_hit)) {
1728                         FuncRequest cmd(bv(), LFUN_INSET_EDIT, x, y - (y2 - y1), mouse_button::none);
1729                         inset_hit->localDispatch(cmd);
1730                 }
1731         }
1732 #else
1733         setCursorFromCoordinates(cursor.x_fix(),
1734                  cursor.y() - cursorRow()->baseline() + cursorRow()->height() + 1);
1735 #endif
1736 }
1737
1738
1739 void LyXText::cursorUpParagraph()
1740 {
1741         if (cursor.pos() > 0)
1742                 setCursor(cursor.par(), 0);
1743         else if (cursor.par() != ownerParagraphs().begin())
1744                 setCursor(boost::prior(cursor.par()), 0);
1745 }
1746
1747
1748 void LyXText::cursorDownParagraph()
1749 {
1750         ParagraphList::iterator par = cursor.par();
1751         ParagraphList::iterator next_par = boost::next(par);
1752
1753         if (next_par != ownerParagraphs().end())
1754                 setCursor(next_par, 0);
1755         else
1756                 setCursor(par, par->size());
1757 }
1758
1759
1760 // fix the cursor `cur' after a characters has been deleted at `where'
1761 // position. Called by deleteEmptyParagraphMechanism
1762 void LyXText::fixCursorAfterDelete(LyXCursor & cur, LyXCursor const & where)
1763 {
1764         // if cursor is not in the paragraph where the delete occured,
1765         // do nothing
1766         if (cur.par() != where.par())
1767                 return;
1768
1769         // if cursor position is after the place where the delete occured,
1770         // update it
1771         if (cur.pos() > where.pos())
1772                 cur.pos(cur.pos()-1);
1773
1774         // check also if we don't want to set the cursor on a spot behind the
1775         // pagragraph because we erased the last character.
1776         if (cur.pos() > cur.par()->size())
1777                 cur.pos(cur.par()->size());
1778
1779         // recompute row et al. for this cursor
1780         setCursor(cur, cur.par(), cur.pos(), cur.boundary());
1781 }
1782
1783
1784 bool LyXText::deleteEmptyParagraphMechanism(LyXCursor const & old_cursor)
1785 {
1786         // Would be wrong to delete anything if we have a selection.
1787         if (selection.set())
1788                 return false;
1789
1790         // We allow all kinds of "mumbo-jumbo" when freespacing.
1791         if (old_cursor.par()->isFreeSpacing())
1792                 return false;
1793
1794         /* Ok I'll put some comments here about what is missing.
1795            I have fixed BackSpace (and thus Delete) to not delete
1796            double-spaces automagically. I have also changed Cut,
1797            Copy and Paste to hopefully do some sensible things.
1798            There are still some small problems that can lead to
1799            double spaces stored in the document file or space at
1800            the beginning of paragraphs. This happens if you have
1801            the cursor between to spaces and then save. Or if you
1802            cut and paste and the selection have a space at the
1803            beginning and then save right after the paste. I am
1804            sure none of these are very hard to fix, but I will
1805            put out 1.1.4pre2 with FIX_DOUBLE_SPACE defined so
1806            that I can get some feedback. (Lgb)
1807         */
1808
1809         // If old_cursor.pos() == 0 and old_cursor.pos()(1) == LineSeparator
1810         // delete the LineSeparator.
1811         // MISSING
1812
1813         // If old_cursor.pos() == 1 and old_cursor.pos()(0) == LineSeparator
1814         // delete the LineSeparator.
1815         // MISSING
1816
1817         // If the pos around the old_cursor were spaces, delete one of them.
1818         if (old_cursor.par() != cursor.par()
1819             || old_cursor.pos() != cursor.pos()) {
1820
1821                 // Only if the cursor has really moved
1822                 if (old_cursor.pos() > 0
1823                     && old_cursor.pos() < old_cursor.par()->size()
1824                     && old_cursor.par()->isLineSeparator(old_cursor.pos())
1825                     && old_cursor.par()->isLineSeparator(old_cursor.pos() - 1)) {
1826                         bool erased = old_cursor.par()->erase(old_cursor.pos() - 1);
1827                         redoParagraph(old_cursor.par());
1828
1829                         if (!erased)
1830                                 return false;
1831 #ifdef WITH_WARNINGS
1832 #warning This will not work anymore when we have multiple views of the same buffer
1833 // In this case, we will have to correct also the cursors held by
1834 // other bufferviews. It will probably be easier to do that in a more
1835 // automated way in LyXCursor code. (JMarc 26/09/2001)
1836 #endif
1837                         // correct all cursors held by the LyXText
1838                         fixCursorAfterDelete(cursor, old_cursor);
1839                         fixCursorAfterDelete(selection.cursor, old_cursor);
1840                         fixCursorAfterDelete(selection.start, old_cursor);
1841                         fixCursorAfterDelete(selection.end, old_cursor);
1842                         return false;
1843                 }
1844         }
1845
1846         // don't delete anything if this is the ONLY paragraph!
1847         if (ownerParagraphs().size() == 1)
1848                 return false;
1849
1850         // Do not delete empty paragraphs with keepempty set.
1851         if (old_cursor.par()->allowEmpty())
1852                 return false;
1853
1854         // only do our magic if we changed paragraph
1855         if (old_cursor.par() == cursor.par())
1856                 return false;
1857
1858         // record if we have deleted a paragraph
1859         // we can't possibly have deleted a paragraph before this point
1860         bool deleted = false;
1861
1862         if (old_cursor.par()->empty() ||
1863             (old_cursor.par()->size() == 1 &&
1864              old_cursor.par()->isLineSeparator(0))) {
1865                 // ok, we will delete something
1866                 LyXCursor tmpcursor;
1867
1868                 deleted = true;
1869
1870                 bool selection_position_was_oldcursor_position = (
1871                         selection.cursor.par()  == old_cursor.par()
1872                         && selection.cursor.pos() == old_cursor.pos());
1873
1874                 tmpcursor = cursor;
1875                 cursor = old_cursor; // that undo can restore the right cursor position
1876
1877                 ParagraphList::iterator endpit = boost::next(old_cursor.par());
1878                 while (endpit != ownerParagraphs().end() && endpit->getDepth())
1879                         ++endpit;
1880         
1881                 recordUndo(bv(), Undo::DELETE, old_cursor.par(), boost::prior(endpit));
1882                 cursor = tmpcursor;
1883
1884                 // delete old par
1885                 ownerParagraphs().erase(old_cursor.par());
1886                 redoParagraph();
1887
1888                 // correct cursor y
1889                 setCursorIntern(cursor.par(), cursor.pos());
1890
1891                 if (selection_position_was_oldcursor_position) {
1892                         // correct selection
1893                         selection.cursor = cursor;
1894                 }
1895         }
1896         if (!deleted) {
1897                 if (old_cursor.par()->stripLeadingSpaces()) {
1898                         redoParagraph(old_cursor.par());
1899                         // correct cursor y
1900                         setCursorIntern(cursor.par(), cursor.pos());
1901                         selection.cursor = cursor;
1902                 }
1903         }
1904         return deleted;
1905 }
1906
1907
1908 ParagraphList & LyXText::ownerParagraphs() const
1909 {
1910         return paragraphs_;
1911 }
1912
1913
1914 bool LyXText::isInInset() const
1915 {
1916         // Sub-level has non-null bv owner and non-null inset owner.
1917         return inset_owner != 0;
1918 }
1919
1920
1921 int defaultRowHeight()
1922 {
1923         LyXFont const font(LyXFont::ALL_SANE);
1924         return int(font_metrics::maxAscent(font)
1925                  + font_metrics::maxDescent(font) * 1.5);
1926 }