]> git.lyx.org Git - lyx.git/blob - src/text.C
fix bug 1254 (handling of blanks in change tracking mode)
[lyx.git] / src / text.C
1 /**
2  * \file src/text.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 Jean-Marc Lasgouttes
9  * \author John Levon
10  * \author André Pönitz
11  * \author Dekel Tsur
12  * \author Jürgen Vigna
13  *
14  * Full author contact details are available in file CREDITS.
15  */
16
17 #include <config.h>
18
19 #include "lyxtext.h"
20
21 #include "author.h"
22 #include "buffer.h"
23 #include "buffer_funcs.h"
24 #include "bufferparams.h"
25 #include "BufferView.h"
26 #include "cursor.h"
27 #include "coordcache.h"
28 #include "CutAndPaste.h"
29 #include "debug.h"
30 #include "dispatchresult.h"
31 #include "encoding.h"
32 #include "errorlist.h"
33 #include "funcrequest.h"
34 #include "factory.h"
35 #include "FontIterator.h"
36 #include "gettext.h"
37 #include "language.h"
38 #include "LColor.h"
39 #include "lyxlength.h"
40 #include "lyxlex.h"
41 #include "lyxrc.h"
42 #include "lyxrow.h"
43 #include "lyxrow_funcs.h"
44 #include "metricsinfo.h"
45 #include "paragraph.h"
46 #include "paragraph_funcs.h"
47 #include "ParagraphParameters.h"
48 #include "rowpainter.h"
49 #include "undo.h"
50 #include "vspace.h"
51 #include "WordLangTuple.h"
52
53 #include "frontends/font_metrics.h"
54 #include "frontends/LyXView.h"
55 #include "frontends/Painter.h"
56
57 #include "insets/insettext.h"
58 #include "insets/insetbibitem.h"
59 #include "insets/insethfill.h"
60 #include "insets/insetlatexaccent.h"
61 #include "insets/insetline.h"
62 #include "insets/insetnewline.h"
63 #include "insets/insetpagebreak.h"
64 #include "insets/insetoptarg.h"
65 #include "insets/insetspace.h"
66 #include "insets/insetspecialchar.h"
67 #include "insets/insettabular.h"
68
69 #include "support/lstrings.h"
70 #include "support/textutils.h"
71 #include "support/convert.h"
72
73 #include <sstream>
74
75 using lyx::pit_type;
76 using lyx::pos_type;
77 using lyx::word_location;
78
79 using lyx::support::bformat;
80 using lyx::support::contains;
81 using lyx::support::lowercase;
82 using lyx::support::split;
83 using lyx::support::uppercase;
84
85 using lyx::cap::cutSelection;
86
87 using std::auto_ptr;
88 using std::advance;
89 using std::distance;
90 using std::max;
91 using std::min;
92 using std::endl;
93 using std::string;
94
95
96 namespace {
97
98 int numberOfSeparators(Paragraph const & par, Row const & row)
99 {
100         pos_type const first = max(row.pos(), par.beginOfBody());
101         pos_type const last = row.endpos() - 1;
102         int n = 0;
103         for (pos_type p = first; p < last; ++p) {
104                 if (par.isSeparator(p))
105                         ++n;
106         }
107         return n;
108 }
109
110
111 int numberOfLabelHfills(Paragraph const & par, Row const & row)
112 {
113         pos_type last = row.endpos() - 1;
114         pos_type first = row.pos();
115
116         // hfill *DO* count at the beginning of paragraphs!
117         if (first) {
118                 while (first < last && par.isHfill(first))
119                         ++first;
120         }
121
122         last = min(last, par.beginOfBody());
123         int n = 0;
124         for (pos_type p = first; p < last; ++p) {
125                 if (par.isHfill(p))
126                         ++n;
127         }
128         return n;
129 }
130
131
132 int numberOfHfills(Paragraph const & par, Row const & row)
133 {
134         pos_type const last = row.endpos() - 1;
135         pos_type first = row.pos();
136
137         // hfill *DO* count at the beginning of paragraphs!
138         if (first) {
139                 while (first < last && par.isHfill(first))
140                         ++first;
141         }
142
143         first = max(first, par.beginOfBody());
144
145         int n = 0;
146         for (pos_type p = first; p < last; ++p) {
147                 if (par.isHfill(p))
148                         ++n;
149         }
150         return n;
151 }
152
153
154 void readParToken(Buffer const & buf, Paragraph & par, LyXLex & lex,
155         string const & token, LyXFont & font)
156 {
157         static Change change;
158
159         BufferParams const & bp = buf.params();
160
161         if (token[0] != '\\') {
162                 string::const_iterator cit = token.begin();
163                 for (; cit != token.end(); ++cit)
164                         par.insertChar(par.size(), (*cit), font, change);
165         } else if (token == "\\begin_layout") {
166                 lex.eatLine();
167                 string layoutname = lex.getString();
168
169                 font = LyXFont(LyXFont::ALL_INHERIT, bp.language);
170                 change = Change();
171
172                 LyXTextClass const & tclass = bp.getLyXTextClass();
173
174                 if (layoutname.empty()) {
175                         layoutname = tclass.defaultLayoutName();
176                 }
177
178                 bool hasLayout = tclass.hasLayout(layoutname);
179
180                 if (!hasLayout) {
181                         buf.error(ErrorItem(_("Unknown layout"),
182                         bformat(_("Layout '%1$s' does not exist in textclass '%2$s'\nTrying to use the default instead.\n"),
183                                 layoutname, tclass.name()), par.id(), 0, par.size()));
184                         layoutname = tclass.defaultLayoutName();
185                 }
186
187                 par.layout(bp.getLyXTextClass()[layoutname]);
188
189                 // Test whether the layout is obsolete.
190                 LyXLayout_ptr const & layout = par.layout();
191                 if (!layout->obsoleted_by().empty())
192                         par.layout(bp.getLyXTextClass()[layout->obsoleted_by()]);
193
194                 par.params().read(lex);
195
196         } else if (token == "\\end_layout") {
197                 lyxerr << BOOST_CURRENT_FUNCTION
198                        << ": Solitary \\end_layout in line "
199                        << lex.getLineNo() << "\n"
200                        << "Missing \\begin_layout?.\n";
201         } else if (token == "\\end_inset") {
202                 lyxerr << BOOST_CURRENT_FUNCTION
203                        << ": Solitary \\end_inset in line "
204                        << lex.getLineNo() << "\n"
205                        << "Missing \\begin_inset?.\n";
206         } else if (token == "\\begin_inset") {
207                 InsetBase * inset = readInset(lex, buf);
208                 if (inset)
209                         par.insertInset(par.size(), inset, font, change);
210                 else {
211                         lex.eatLine();
212                         string line = lex.getString();
213                         buf.error(ErrorItem(_("Unknown Inset"), line,
214                                             par.id(), 0, par.size()));
215                 }
216         } else if (token == "\\family") {
217                 lex.next();
218                 font.setLyXFamily(lex.getString());
219         } else if (token == "\\series") {
220                 lex.next();
221                 font.setLyXSeries(lex.getString());
222         } else if (token == "\\shape") {
223                 lex.next();
224                 font.setLyXShape(lex.getString());
225         } else if (token == "\\size") {
226                 lex.next();
227                 font.setLyXSize(lex.getString());
228         } else if (token == "\\lang") {
229                 lex.next();
230                 string const tok = lex.getString();
231                 Language const * lang = languages.getLanguage(tok);
232                 if (lang) {
233                         font.setLanguage(lang);
234                 } else {
235                         font.setLanguage(bp.language);
236                         lex.printError("Unknown language `$$Token'");
237                 }
238         } else if (token == "\\numeric") {
239                 lex.next();
240                 font.setNumber(font.setLyXMisc(lex.getString()));
241         } else if (token == "\\emph") {
242                 lex.next();
243                 font.setEmph(font.setLyXMisc(lex.getString()));
244         } else if (token == "\\bar") {
245                 lex.next();
246                 string const tok = lex.getString();
247
248                 if (tok == "under")
249                         font.setUnderbar(LyXFont::ON);
250                 else if (tok == "no")
251                         font.setUnderbar(LyXFont::OFF);
252                 else if (tok == "default")
253                         font.setUnderbar(LyXFont::INHERIT);
254                 else
255                         lex.printError("Unknown bar font flag "
256                                        "`$$Token'");
257         } else if (token == "\\noun") {
258                 lex.next();
259                 font.setNoun(font.setLyXMisc(lex.getString()));
260         } else if (token == "\\color") {
261                 lex.next();
262                 font.setLyXColor(lex.getString());
263         } else if (token == "\\InsetSpace" || token == "\\SpecialChar") {
264
265                 // Insets don't make sense in a free-spacing context! ---Kayvan
266                 if (par.isFreeSpacing()) {
267                         if (token == "\\InsetSpace")
268                                 par.insertChar(par.size(), ' ', font, change);
269                         else if (lex.isOK()) {
270                                 lex.next();
271                                 string const next_token = lex.getString();
272                                 if (next_token == "\\-")
273                                         par.insertChar(par.size(), '-', font, change);
274                                 else {
275                                         lex.printError("Token `$$Token' "
276                                                        "is in free space "
277                                                        "paragraph layout!");
278                                 }
279                         }
280                 } else {
281                         auto_ptr<InsetBase> inset;
282                         if (token == "\\SpecialChar" )
283                                 inset.reset(new InsetSpecialChar);
284                         else
285                                 inset.reset(new InsetSpace);
286                         inset->read(buf, lex);
287                         par.insertInset(par.size(), inset.release(),
288                                         font, change);
289                 }
290         } else if (token == "\\i") {
291                 auto_ptr<InsetBase> inset(new InsetLatexAccent);
292                 inset->read(buf, lex);
293                 par.insertInset(par.size(), inset.release(), font, change);
294         } else if (token == "\\backslash") {
295                 par.insertChar(par.size(), '\\', font, change);
296         } else if (token == "\\newline") {
297                 auto_ptr<InsetBase> inset(new InsetNewline);
298                 inset->read(buf, lex);
299                 par.insertInset(par.size(), inset.release(), font, change);
300         } else if (token == "\\LyXTable") {
301                 auto_ptr<InsetBase> inset(new InsetTabular(buf));
302                 inset->read(buf, lex);
303                 par.insertInset(par.size(), inset.release(), font, change);
304         } else if (token == "\\bibitem") {
305                 InsetCommandParams p("bibitem", "dummy");
306                 auto_ptr<InsetBibitem> inset(new InsetBibitem(p));
307                 inset->read(buf, lex);
308                 par.insertInset(par.size(), inset.release(), font, change);
309         } else if (token == "\\hfill") {
310                 par.insertInset(par.size(), new InsetHFill, font, change);
311         } else if (token == "\\lyxline") {
312                 par.insertInset(par.size(), new InsetLine, font, change);
313         } else if (token == "\\newpage") {
314                 par.insertInset(par.size(), new InsetPagebreak, font, change);
315         } else if (token == "\\change_unchanged") {
316                 // Hack ! Needed for empty paragraphs :/
317                 // FIXME: is it still ??
318                 if (!par.size())
319                         par.cleanChanges();
320                 change = Change(Change::UNCHANGED);
321         } else if (token == "\\change_inserted") {
322                 lex.eatLine();
323                 std::istringstream is(lex.getString());
324                 int aid;
325                 lyx::time_type ct;
326                 is >> aid >> ct;
327                 change = Change(Change::INSERTED, bp.author_map[aid], ct);
328         } else if (token == "\\change_deleted") {
329                 lex.eatLine();
330                 std::istringstream is(lex.getString());
331                 int aid;
332                 lyx::time_type ct;
333                 is >> aid >> ct;
334                 change = Change(Change::DELETED, bp.author_map[aid], ct);
335         } else {
336                 lex.eatLine();
337                 buf.error(ErrorItem(_("Unknown token"),
338                         bformat(_("Unknown token: %1$s %2$s\n"), token, lex.getString()),
339                         par.id(), 0, par.size()));
340         }
341 }
342
343
344 void readParagraph(Buffer const & buf, Paragraph & par, LyXLex & lex)
345 {
346         lex.nextToken();
347         string token = lex.getString();
348         LyXFont font;
349
350         while (lex.isOK()) {
351
352                 readParToken(buf, par, lex, token, font);
353
354                 lex.nextToken();
355                 token = lex.getString();
356
357                 if (token.empty())
358                         continue;
359
360                 if (token == "\\end_layout") {
361                         //Ok, paragraph finished
362                         break;
363                 }
364
365                 lyxerr[Debug::PARSER] << "Handling paragraph token: `"
366                                       << token << '\'' << endl;
367                 if (token == "\\begin_layout" || token == "\\end_document"
368                     || token == "\\end_inset" || token == "\\begin_deeper"
369                     || token == "\\end_deeper") {
370                         lex.pushToken(token);
371                         lyxerr << "Paragraph ended in line "
372                                << lex.getLineNo() << "\n"
373                                << "Missing \\end_layout.\n";
374                         break;
375                 }
376         }
377         // Initialize begin_of_body_ on load; redoParagraph maintains
378         par.setBeginOfBody();
379 }
380
381
382 } // namespace anon
383
384
385
386 BufferView * LyXText::bv() const
387 {
388         BOOST_ASSERT(bv_owner != 0);
389         return bv_owner;
390 }
391
392
393 double LyXText::spacing(Paragraph const & par) const
394 {
395         if (par.params().spacing().isDefault())
396                 return bv()->buffer()->params().spacing().getValue();
397         return par.params().spacing().getValue();
398 }
399
400
401 int LyXText::width() const
402 {
403         return dim_.wid;
404 }
405
406
407 int LyXText::height() const
408 {
409         return dim_.height();
410 }
411
412
413 int LyXText::singleWidth(Paragraph const & par, pos_type pos) const
414 {
415         return singleWidth(par, pos, par.getChar(pos), getFont(par, pos));
416 }
417
418
419 int LyXText::singleWidth(Paragraph const & par,
420                          pos_type pos, char c, LyXFont const & font) const
421 {
422         // The most common case is handled first (Asger)
423         if (IsPrintable(c)) {
424                 Language const * language = font.language();
425                 if (language->RightToLeft()) {
426                         if ((lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 ||
427                              lyxrc.font_norm_type == LyXRC::ISO_10646_1)
428                             && language->lang() == "arabic") {
429                                 if (Encodings::IsComposeChar_arabic(c))
430                                         return 0;
431                                 c = par.transformChar(c, pos);
432                         } else if (language->lang() == "hebrew" &&
433                                    Encodings::IsComposeChar_hebrew(c))
434                                 return 0;
435                 }
436                 return font_metrics::width(c, font);
437         }
438
439         if (c == Paragraph::META_INSET)
440                 return par.getInset(pos)->width();
441
442         return font_metrics::width(c, font);
443 }
444
445
446 int LyXText::leftMargin(pit_type pit) const
447 {
448         BOOST_ASSERT(pit >= 0);
449         BOOST_ASSERT(pit < int(pars_.size()));
450         return leftMargin(pit, pars_[pit].size());
451 }
452
453
454 int LyXText::leftMargin(pit_type const pit, pos_type const pos) const
455 {
456         BOOST_ASSERT(pit >= 0);
457         BOOST_ASSERT(pit < int(pars_.size()));
458         Paragraph const & par = pars_[pit];
459         BOOST_ASSERT(pos >= 0);
460         BOOST_ASSERT(pos <= par.size());
461         //lyxerr << "LyXText::leftMargin: pit: " << pit << " pos: " << pos << endl;
462         LyXTextClass const & tclass =
463                 bv()->buffer()->params().getLyXTextClass();
464         LyXLayout_ptr const & layout = par.layout();
465
466         string parindent = layout->parindent;
467
468         int l_margin = 0;
469
470         if (isMainText())
471                 l_margin += changebarMargin();
472
473         l_margin += font_metrics::signedWidth(tclass.leftmargin(), tclass.defaultfont());
474
475         if (par.getDepth() != 0) {
476         // find the next level paragraph
477         pit_type newpar = outerHook(pit, pars_);
478                 if (newpar != pit_type(pars_.size())) {
479                         if (pars_[newpar].layout()->isEnvironment()) {
480                                 l_margin = leftMargin(newpar);
481                         }
482                         if (par.layout() == tclass.defaultLayout()) {
483                                 if (pars_[newpar].params().noindent())
484                                         parindent.erase();
485                                 else
486                                         parindent = pars_[newpar].layout()->parindent;
487                         }
488                 }
489         }
490
491         LyXFont const labelfont = getLabelFont(par);
492         switch (layout->margintype) {
493         case MARGIN_DYNAMIC:
494                 if (!layout->leftmargin.empty())
495                         l_margin += font_metrics::signedWidth(layout->leftmargin,
496                                                   tclass.defaultfont());
497                 if (!par.getLabelstring().empty()) {
498                         l_margin += font_metrics::signedWidth(layout->labelindent,
499                                                   labelfont);
500                         l_margin += font_metrics::width(par.getLabelstring(),
501                                             labelfont);
502                         l_margin += font_metrics::width(layout->labelsep, labelfont);
503                 }
504                 break;
505
506         case MARGIN_MANUAL:
507                 l_margin += font_metrics::signedWidth(layout->labelindent, labelfont);
508                 // The width of an empty par, even with manual label, should be 0
509                 if (!par.empty() && pos >= par.beginOfBody()) {
510                         if (!par.getLabelWidthString().empty()) {
511                                 l_margin += font_metrics::width(par.getLabelWidthString(),
512                                                labelfont);
513                                 l_margin += font_metrics::width(layout->labelsep, labelfont);
514                         }
515                 }
516                 break;
517
518         case MARGIN_STATIC:
519                 l_margin += font_metrics::signedWidth(layout->leftmargin, tclass.defaultfont()) * 4
520                         / (par.getDepth() + 4);
521                 break;
522
523         case MARGIN_FIRST_DYNAMIC:
524                 if (layout->labeltype == LABEL_MANUAL) {
525                         if (pos >= par.beginOfBody()) {
526                                 l_margin += font_metrics::signedWidth(layout->leftmargin,
527                                                           labelfont);
528                         } else {
529                                 l_margin += font_metrics::signedWidth(layout->labelindent,
530                                                           labelfont);
531                         }
532                 } else if (pos != 0
533                            // Special case to fix problems with
534                            // theorems (JMarc)
535                            || (layout->labeltype == LABEL_STATIC
536                                && layout->latextype == LATEX_ENVIRONMENT
537                                && !isFirstInSequence(pit, pars_))) {
538                         l_margin += font_metrics::signedWidth(layout->leftmargin,
539                                                   labelfont);
540                 } else if (layout->labeltype != LABEL_TOP_ENVIRONMENT
541                            && layout->labeltype != LABEL_BIBLIO
542                            && layout->labeltype !=
543                            LABEL_CENTERED_TOP_ENVIRONMENT) {
544                         l_margin += font_metrics::signedWidth(layout->labelindent,
545                                                   labelfont);
546                         l_margin += font_metrics::width(layout->labelsep, labelfont);
547                         l_margin += font_metrics::width(par.getLabelstring(),
548                                             labelfont);
549                 }
550                 break;
551
552         case MARGIN_RIGHT_ADDRESS_BOX: {
553 #if 0
554                 // ok, a terrible hack. The left margin depends on the widest
555                 // row in this paragraph.
556                 RowList::iterator rit = par.rows().begin();
557                 RowList::iterator end = par.rows().end();
558 #ifdef WITH_WARNINGS
559 #warning This is wrong.
560 #endif
561                 int minfill = maxwidth_;
562                 for ( ; rit != end; ++rit)
563                         if (rit->fill() < minfill)
564                                 minfill = rit->fill();
565                 l_margin += font_metrics::signedWidth(layout->leftmargin,
566                         tclass.defaultfont());
567                 l_margin += minfill;
568 #endif
569                 // also wrong, but much shorter.
570                 l_margin += maxwidth_ / 2;
571                 break;
572         }
573         }
574
575         if (!par.params().leftIndent().zero())
576                 l_margin += par.params().leftIndent().inPixels(maxwidth_);
577
578         LyXAlignment align;
579
580         if (par.params().align() == LYX_ALIGN_LAYOUT)
581                 align = layout->align;
582         else
583                 align = par.params().align();
584
585         // set the correct parindent
586         if (pos == 0
587             && (layout->labeltype == LABEL_NO_LABEL
588                || layout->labeltype == LABEL_TOP_ENVIRONMENT
589                || layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT
590                || (layout->labeltype == LABEL_STATIC
591                    && layout->latextype == LATEX_ENVIRONMENT
592                    && !isFirstInSequence(pit, pars_)))
593             && align == LYX_ALIGN_BLOCK
594             && !par.params().noindent()
595             // display style insets are always centered, omit indentation
596             && !(!par.empty()
597                     && par.isInset(pos)
598                     && par.getInset(pos)->display())
599             // in charstyles, tabulars and ert paragraphs are never indented!
600             && ((par.ownerCode() != InsetBase::TEXT_CODE || isMainText())
601                     && par.ownerCode() != InsetBase::ERT_CODE
602                     && par.ownerCode() != InsetBase::CHARSTYLE_CODE)
603             && (par.layout() != tclass.defaultLayout()
604                 || bv()->buffer()->params().paragraph_separation ==
605                    BufferParams::PARSEP_INDENT))
606         {
607                 l_margin += font_metrics::signedWidth(parindent, tclass.defaultfont());
608         }
609
610         return l_margin;
611 }
612
613
614 int LyXText::rightMargin(Paragraph const & par) const
615 {
616         // We do not want rightmargins on inner texts.
617         if (bv()->text() != this)
618                 return 0;
619
620         LyXTextClass const & tclass = bv()->buffer()->params().getLyXTextClass();
621         int const r_margin =
622                 ::rightMargin()
623                 + font_metrics::signedWidth(tclass.rightmargin(),
624                                             tclass.defaultfont())
625                 + font_metrics::signedWidth(par.layout()->rightmargin,
626                                             tclass.defaultfont())
627                 * 4 / (par.getDepth() + 4);
628
629         return r_margin;
630 }
631
632
633 int LyXText::labelEnd(pit_type const pit) const
634 {
635         // labelEnd is only needed if the layout fills a flushleft label.
636         if (pars_[pit].layout()->margintype != MARGIN_MANUAL)
637                 return 0;
638         // return the beginning of the body
639         return leftMargin(pit);
640 }
641
642
643 namespace {
644
645 // this needs special handling - only newlines count as a break point
646 pos_type addressBreakPoint(pos_type i, Paragraph const & par)
647 {
648         pos_type const end = par.size();
649
650         for (; i < end; ++i)
651                 if (par.isNewline(i))
652                         return i + 1;
653
654         return end;
655 }
656
657 };
658
659
660 void LyXText::rowBreakPoint(pit_type const pit, Row & row) const
661 {
662         Paragraph const & par = pars_[pit];
663         pos_type const end = par.size();
664         pos_type const pos = row.pos();
665         if (pos == end) {
666                 row.endpos(end);
667                 return;
668         }
669
670         // maximum pixel width of a row
671         int width = maxwidth_ - rightMargin(par); // - leftMargin(pit, row);
672         if (width < 0) {
673                 row.endpos(end);
674                 return;
675         }
676
677         LyXLayout_ptr const & layout = par.layout();
678
679         if (layout->margintype == MARGIN_RIGHT_ADDRESS_BOX) {
680                 row.endpos(addressBreakPoint(pos, par));
681                 return;
682         }
683
684         pos_type const body_pos = par.beginOfBody();
685
686
687         // Now we iterate through until we reach the right margin
688         // or the end of the par, then choose the possible break
689         // nearest that.
690
691         int const left = leftMargin(pit, pos);
692         int x = left;
693
694         // pixel width since last breakpoint
695         int chunkwidth = 0;
696
697         FontIterator fi = FontIterator(*this, par, pos);
698         pos_type point = end;
699         pos_type i = pos;
700         for ( ; i < end; ++i, ++fi) {
701                 char const c = par.getChar(i);
702                 int thiswidth = singleWidth(par, i, c, *fi);
703
704                 // add the auto-hfill from label end to the body
705                 if (body_pos && i == body_pos) {
706                         int add = font_metrics::width(layout->labelsep, getLabelFont(par));
707                         if (par.isLineSeparator(i - 1))
708                                 add -= singleWidth(par, i - 1);
709
710                         add = std::max(add, labelEnd(pit) - x);
711                         thiswidth += add;
712                 }
713
714                 x += thiswidth;
715                 chunkwidth += thiswidth;
716
717                 // break before a character that will fall off
718                 // the right of the row
719                 if (x >= width) {
720                         // if no break before, break here
721                         if (point == end || chunkwidth >= width - left) {
722                                 if (i > pos)
723                                         point = i;
724                                 else
725                                         point = i + 1;
726
727                         }
728                         // exit on last registered breakpoint:
729                         break;
730                 }
731
732                 if (par.isNewline(i)) {
733                         point = i + 1;
734                         break;
735                 }
736                 // Break before...
737                 if (i + 1 < end) {
738                         if (par.isInset(i + 1) && par.getInset(i + 1)->display()) {
739                                 point = i + 1;
740                                 break;
741                         }
742                         // ...and after.
743                         if (par.isInset(i) && par.getInset(i)->display()) {
744                                 point = i + 1;
745                                 break;
746                         }
747                 }
748
749                 if (!par.isInset(i) || par.getInset(i)->isChar()) {
750                         // some insets are line separators too
751                         if (par.isLineSeparator(i)) {
752                                 // register breakpoint:
753                                 point = i + 1;
754                                 chunkwidth = 0;
755                         }
756                 }
757         }
758
759         // maybe found one, but the par is short enough.
760         if (i == end && x < width)
761                 point = end;
762
763         // manual labels cannot be broken in LaTeX. But we
764         // want to make our on-screen rendering of footnotes
765         // etc. still break
766         if (body_pos && point < body_pos)
767                 point = body_pos;
768
769         row.endpos(point);
770 }
771
772
773 void LyXText::setRowWidth(pit_type const pit, Row & row) const
774 {
775         // get the pure distance
776         pos_type const end = row.endpos();
777
778         Paragraph const & par = pars_[pit];
779         string const & labelsep = par.layout()->labelsep;
780         int w = leftMargin(pit, row.pos());
781
782         pos_type const body_pos = par.beginOfBody();
783         pos_type i = row.pos();
784
785         if (i < end) {
786                 FontIterator fi = FontIterator(*this, par, i);
787                 for ( ; i < end; ++i, ++fi) {
788                         if (body_pos > 0 && i == body_pos) {
789                                 w += font_metrics::width(labelsep, getLabelFont(par));
790                                 if (par.isLineSeparator(i - 1))
791                                         w -= singleWidth(par, i - 1);
792                                 w = max(w, labelEnd(pit));
793                         }
794                         char const c = par.getChar(i);
795                         w += singleWidth(par, i, c, *fi);
796                 }
797         }
798
799         if (body_pos > 0 && body_pos >= end) {
800                 w += font_metrics::width(labelsep, getLabelFont(par));
801                 if (end > 0 && par.isLineSeparator(end - 1))
802                         w -= singleWidth(par, end - 1);
803                 w = max(w, labelEnd(pit));
804         }
805
806         row.width(w + rightMargin(par));
807 }
808
809
810 // returns the minimum space a manual label needs on the screen in pixel
811 int LyXText::labelFill(Paragraph const & par, Row const & row) const
812 {
813         pos_type last = par.beginOfBody();
814
815         BOOST_ASSERT(last > 0);
816
817         // -1 because a label ends with a space that is in the label
818         --last;
819
820         // a separator at this end does not count
821         if (par.isLineSeparator(last))
822                 --last;
823
824         int w = 0;
825         for (pos_type i = row.pos(); i <= last; ++i)
826                 w += singleWidth(par, i);
827
828         string const & label = par.params().labelWidthString();
829         if (label.empty())
830                 return 0;
831
832         return max(0, font_metrics::width(label, getLabelFont(par)) - w);
833 }
834
835
836 LColor_color LyXText::backgroundColor() const
837 {
838         return LColor_color(LColor::color(background_color_));
839 }
840
841
842 void LyXText::setHeightOfRow(pit_type const pit, Row & row)
843 {
844         Paragraph const & par = pars_[pit];
845         // get the maximum ascent and the maximum descent
846         double layoutasc = 0;
847         double layoutdesc = 0;
848         double const dh = defaultRowHeight();
849
850         // ok, let us initialize the maxasc and maxdesc value.
851         // Only the fontsize count. The other properties
852         // are taken from the layoutfont. Nicer on the screen :)
853         LyXLayout_ptr const & layout = par.layout();
854
855         // as max get the first character of this row then it can
856         // increase but not decrease the height. Just some point to
857         // start with so we don't have to do the assignment below too
858         // often.
859         LyXFont font = getFont(par, row.pos());
860         LyXFont::FONT_SIZE const tmpsize = font.size();
861         font = getLayoutFont(pit);
862         LyXFont::FONT_SIZE const size = font.size();
863         font.setSize(tmpsize);
864
865         LyXFont labelfont = getLabelFont(par);
866
867         // these are minimum values
868         double const spacing_val = layout->spacing.getValue() * spacing(par);
869         //lyxerr << "spacing_val = " << spacing_val << endl;
870         int maxasc  = int(font_metrics::maxAscent(font)  * spacing_val);
871         int maxdesc = int(font_metrics::maxDescent(font) * spacing_val);
872
873         // insets may be taller
874         InsetList::const_iterator ii = par.insetlist.begin();
875         InsetList::const_iterator iend = par.insetlist.end();
876         for ( ; ii != iend; ++ii) {
877                 if (ii->pos >= row.pos() && ii->pos < row.endpos()) {
878                         maxasc  = max(maxasc,  ii->inset->ascent());
879                         maxdesc = max(maxdesc, ii->inset->descent());
880                 }
881         }
882
883         // Check if any custom fonts are larger (Asger)
884         // This is not completely correct, but we can live with the small,
885         // cosmetic error for now.
886         int labeladdon = 0;
887         pos_type const pos_end = row.endpos();
888
889         LyXFont::FONT_SIZE maxsize =
890                 par.highestFontInRange(row.pos(), pos_end, size);
891         if (maxsize > font.size()) {
892                 font.setSize(maxsize);
893                 maxasc  = max(maxasc,  font_metrics::maxAscent(font));
894                 maxdesc = max(maxdesc, font_metrics::maxDescent(font));
895         }
896
897         // This is nicer with box insets:
898         ++maxasc;
899         ++maxdesc;
900
901         row.ascent(maxasc);
902
903         // is it a top line?
904         if (row.pos() == 0) {
905                 BufferParams const & bufparams = bv()->buffer()->params();
906                 // some parksips VERY EASY IMPLEMENTATION
907                 if (bv()->buffer()->params().paragraph_separation
908                     == BufferParams::PARSEP_SKIP
909                         && pit != 0
910                         && ((layout->isParagraph() && par.getDepth() == 0)
911                             || (pars_[pit - 1].layout()->isParagraph()
912                                 && pars_[pit - 1].getDepth() == 0)))
913                 {
914                                 maxasc += bufparams.getDefSkip().inPixels(*bv());
915                 }
916
917                 if (par.params().startOfAppendix())
918                         maxasc += int(3 * dh);
919
920                 // This is special code for the chapter, since the label of this
921                 // layout is printed in an extra row
922                 if (layout->counter == "chapter"
923                     && !par.params().labelString().empty()) {
924                         labeladdon = int(font_metrics::maxHeight(labelfont)
925                                      * layout->spacing.getValue()
926                                      * spacing(par));
927                 }
928
929                 // special code for the top label
930                 if ((layout->labeltype == LABEL_TOP_ENVIRONMENT
931                      || layout->labeltype == LABEL_BIBLIO
932                      || layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT)
933                     && isFirstInSequence(pit, paragraphs())
934                     && !par.getLabelstring().empty())
935                 {
936                         labeladdon = int(
937                                   font_metrics::maxHeight(labelfont)
938                                         * layout->spacing.getValue()
939                                         * spacing(par)
940                                 + (layout->topsep + layout->labelbottomsep) * dh);
941                 }
942
943                 // Add the layout spaces, for example before and after
944                 // a section, or between the items of a itemize or enumerate
945                 // environment.
946
947                 pit_type prev = depthHook(pit, pars_, par.getDepth());
948                 if (prev != pit
949                     && pars_[prev].layout() == layout
950                     && pars_[prev].getDepth() == par.getDepth()
951                     && pars_[prev].getLabelWidthString() == par.getLabelWidthString())
952                 {
953                         layoutasc = layout->itemsep * dh;
954                 } else if (pit != 0 || row.pos() != 0) {
955                         if (layout->topsep > 0)
956                                 layoutasc = layout->topsep * dh;
957                 }
958
959                 prev = outerHook(pit, pars_);
960                 if (prev != pit_type(pars_.size())) {
961                         maxasc += int(pars_[prev].layout()->parsep * dh);
962                 } else if (pit != 0) {
963                         if (pars_[pit - 1].getDepth() != 0 ||
964                                         pars_[pit - 1].layout() == layout) {
965                                 maxasc += int(layout->parsep * dh);
966                         }
967                 }
968         }
969
970         // is it a bottom line?
971         if (row.endpos() >= par.size()) {
972                 // add the layout spaces, for example before and after
973                 // a section, or between the items of a itemize or enumerate
974                 // environment
975                 pit_type nextpit = pit + 1;
976                 if (nextpit != pit_type(pars_.size())) {
977                         pit_type cpit = pit;
978                         double usual = 0;
979                         double unusual = 0;
980
981                         if (pars_[cpit].getDepth() > pars_[nextpit].getDepth()) {
982                                 usual = pars_[cpit].layout()->bottomsep * dh;
983                                 cpit = depthHook(cpit, paragraphs(), pars_[nextpit].getDepth());
984                                 if (pars_[cpit].layout() != pars_[nextpit].layout()
985                                         || pars_[nextpit].getLabelWidthString() != pars_[cpit].getLabelWidthString())
986                                 {
987                                         unusual = pars_[cpit].layout()->bottomsep * dh;
988                                 }
989                                 layoutdesc = max(unusual, usual);
990                         } else if (pars_[cpit].getDepth() == pars_[nextpit].getDepth()) {
991                                 if (pars_[cpit].layout() != pars_[nextpit].layout()
992                                         || pars_[nextpit].getLabelWidthString() != pars_[cpit].getLabelWidthString())
993                                         layoutdesc = int(pars_[cpit].layout()->bottomsep * dh);
994                         }
995                 }
996         }
997
998         // incalculate the layout spaces
999         maxasc  += int(layoutasc  * 2 / (2 + pars_[pit].getDepth()));
1000         maxdesc += int(layoutdesc * 2 / (2 + pars_[pit].getDepth()));
1001
1002         // Top and bottom margin of the document (only at top-level)
1003         if (bv_owner->text() == this) {
1004                 if (pit == 0 && row.pos() == 0)
1005                         maxasc += 20;
1006                 if (pit + 1 == pars_.size() && row.endpos() == par.size())
1007                         maxdesc += 20;
1008         }
1009
1010         row.ascent(maxasc + labeladdon);
1011         row.descent(maxdesc);
1012 }
1013
1014
1015 namespace {
1016
1017 }
1018
1019 void LyXText::breakParagraph(LCursor & cur, bool keep_layout)
1020 {
1021         BOOST_ASSERT(this == cur.text());
1022         // allow only if at start or end, or all previous is new text
1023         Paragraph & cpar = cur.paragraph();
1024         pit_type cpit = cur.pit();
1025
1026         if (cur.pos() != 0 && cur.pos() != cur.lastpos()
1027             && cpar.isChangeEdited(0, cur.pos()))
1028                 return;
1029
1030         LyXTextClass const & tclass = cur.buffer().params().getLyXTextClass();
1031         LyXLayout_ptr const & layout = cpar.layout();
1032
1033         // this is only allowed, if the current paragraph is not empty
1034         // or caption and if it has not the keepempty flag active
1035         if (cur.lastpos() == 0 && !cpar.allowEmpty()
1036            && layout->labeltype != LABEL_SENSITIVE)
1037                 return;
1038
1039         // a layout change may affect also the following paragraph
1040         recUndo(cur.pit(), undoSpan(cur.pit()) - 1);
1041
1042         // Always break behind a space
1043         // It is better to erase the space (Dekel)
1044         if (cur.pos() != cur.lastpos() && cpar.isLineSeparator(cur.pos()))
1045                 cpar.erase(cur.pos());
1046
1047         // How should the layout for the new paragraph be?
1048         int preserve_layout = 0;
1049         if (keep_layout)
1050                 preserve_layout = 2;
1051         else
1052                 preserve_layout = layout->isEnvironment();
1053
1054         // We need to remember this before we break the paragraph, because
1055         // that invalidates the layout variable
1056         bool sensitive = layout->labeltype == LABEL_SENSITIVE;
1057
1058         // we need to set this before we insert the paragraph.
1059         bool const isempty = cpar.allowEmpty() && cpar.empty();
1060
1061         ::breakParagraph(cur.buffer().params(), paragraphs(), cpit,
1062                          cur.pos(), preserve_layout);
1063
1064         // After this, neither paragraph contains any rows!
1065
1066         cpit = cur.pit();
1067         pit_type next_par = cpit + 1;
1068
1069         // well this is the caption hack since one caption is really enough
1070         if (sensitive) {
1071                 if (cur.pos() == 0)
1072                         // set to standard-layout
1073                         pars_[cpit].applyLayout(tclass.defaultLayout());
1074                 else
1075                         // set to standard-layout
1076                         pars_[next_par].applyLayout(tclass.defaultLayout());
1077         }
1078
1079         while (!pars_[next_par].empty() && pars_[next_par].isNewline(0))
1080                 pars_[next_par].erase(0);
1081
1082         updateCounters(cur.buffer());
1083
1084         // This check is necessary. Otherwise the new empty paragraph will
1085         // be deleted automatically. And it is more friendly for the user!
1086         if (cur.pos() != 0 || isempty)
1087                 setCursor(cur, cur.pit() + 1, 0);
1088         else
1089                 setCursor(cur, cur.pit(), 0);
1090 }
1091
1092
1093 // insert a character, moves all the following breaks in the
1094 // same Paragraph one to the right and make a rebreak
1095 void LyXText::insertChar(LCursor & cur, char c)
1096 {
1097         BOOST_ASSERT(this == cur.text());
1098         BOOST_ASSERT(c != Paragraph::META_INSET);
1099
1100         recordUndo(cur, Undo::INSERT);
1101
1102         Paragraph & par = cur.paragraph();
1103         // try to remove this
1104         pit_type const pit = cur.pit();
1105
1106         bool const freeSpacing = par.layout()->free_spacing ||
1107                 par.isFreeSpacing();
1108
1109         if (lyxrc.auto_number) {
1110                 static string const number_operators = "+-/*";
1111                 static string const number_unary_operators = "+-";
1112                 static string const number_seperators = ".,:";
1113
1114                 if (current_font.number() == LyXFont::ON) {
1115                         if (!IsDigit(c) && !contains(number_operators, c) &&
1116                             !(contains(number_seperators, c) &&
1117                               cur.pos() != 0 &&
1118                               cur.pos() != cur.lastpos() &&
1119                               getFont(par, cur.pos()).number() == LyXFont::ON &&
1120                               getFont(par, cur.pos() - 1).number() == LyXFont::ON)
1121                            )
1122                                 number(cur); // Set current_font.number to OFF
1123                 } else if (IsDigit(c) &&
1124                            real_current_font.isVisibleRightToLeft()) {
1125                         number(cur); // Set current_font.number to ON
1126
1127                         if (cur.pos() != 0) {
1128                                 char const c = par.getChar(cur.pos() - 1);
1129                                 if (contains(number_unary_operators, c) &&
1130                                     (cur.pos() == 1
1131                                      || par.isSeparator(cur.pos() - 2)
1132                                      || par.isNewline(cur.pos() - 2))
1133                                   ) {
1134                                         setCharFont(pit, cur.pos() - 1, current_font);
1135                                 } else if (contains(number_seperators, c)
1136                                      && cur.pos() >= 2
1137                                      && getFont(par, cur.pos() - 2).number() == LyXFont::ON) {
1138                                         setCharFont(pit, cur.pos() - 1, current_font);
1139                                 }
1140                         }
1141                 }
1142         }
1143
1144         // First check, if there will be two blanks together or a blank at
1145         // the beginning of a paragraph.
1146         // I decided to handle blanks like normal characters, the main
1147         // difference are the special checks when calculating the row.fill
1148         // (blank does not count at the end of a row) and the check here
1149
1150         // The bug is triggered when we type in a description environment:
1151         // The current_font is not changed when we go from label to main text
1152         // and it should (along with realtmpfont) when we type the space.
1153         // CHECK There is a bug here! (Asger)
1154
1155         // store the current font.  This is because of the use of cursor
1156         // movements. The moving cursor would refresh the current font
1157         LyXFont realtmpfont = real_current_font;
1158         LyXFont rawtmpfont = current_font;
1159
1160         // When the free-spacing option is set for the current layout,
1161         // disable the double-space checking
1162         if (!freeSpacing && IsLineSeparatorChar(c)) {
1163                 if (cur.pos() == 0) {
1164                         static bool sent_space_message = false;
1165                         if (!sent_space_message) {
1166                                 cur.message(_("You cannot insert a space at the "
1167                                         "beginning of a paragraph. Please read the Tutorial."));
1168                                 sent_space_message = true;
1169                         }
1170                         return;
1171                 }
1172                 BOOST_ASSERT(cur.pos() > 0);
1173                 if ((par.isLineSeparator(cur.pos() - 1)
1174                     || par.isNewline(cur.pos() - 1))
1175                     && par.lookupChange(cur.pos() - 1) != Change::DELETED) {
1176                         static bool sent_space_message = false;
1177                         if (!sent_space_message) {
1178                                 cur.message(_("You cannot type two spaces this way. "
1179                                         "Please read the Tutorial."));
1180                                 sent_space_message = true;
1181                         }
1182                         return;
1183                 }
1184         }
1185
1186         par.insertChar(cur.pos(), c, rawtmpfont);
1187
1188         current_font = rawtmpfont;
1189         real_current_font = realtmpfont;
1190         //setCursor(cur, cur.pit(), cur.pos() + 1, false, cur.boundary());
1191         setCursor(cur, cur.pit(), cur.pos() + 1, false, true);
1192         charInserted();
1193 }
1194
1195
1196 void LyXText::charInserted()
1197 {
1198         // Here we call finishUndo for every 20 characters inserted.
1199         // This is from my experience how emacs does it. (Lgb)
1200         static unsigned int counter;
1201         if (counter < 20) {
1202                 ++counter;
1203         } else {
1204                 finishUndo();
1205                 counter = 0;
1206         }
1207 }
1208
1209
1210 RowMetrics
1211 LyXText::computeRowMetrics(pit_type const pit, Row const & row) const
1212 {
1213         RowMetrics result;
1214         Paragraph const & par = pars_[pit];
1215
1216         double w = dim_.wid - row.width();
1217
1218         bool const is_rtl = isRTL(par);
1219         if (is_rtl)
1220                 result.x = rightMargin(par);
1221         else
1222                 result.x = leftMargin(pit, row.pos());
1223
1224         // is there a manual margin with a manual label
1225         LyXLayout_ptr const & layout = par.layout();
1226
1227         if (layout->margintype == MARGIN_MANUAL
1228             && layout->labeltype == LABEL_MANUAL) {
1229                 /// We might have real hfills in the label part
1230                 int nlh = numberOfLabelHfills(par, row);
1231
1232                 // A manual label par (e.g. List) has an auto-hfill
1233                 // between the label text and the body of the
1234                 // paragraph too.
1235                 // But we don't want to do this auto hfill if the par
1236                 // is empty.
1237                 if (!par.empty())
1238                         ++nlh;
1239
1240                 if (nlh && !par.getLabelWidthString().empty())
1241                         result.label_hfill = labelFill(par, row) / double(nlh);
1242         }
1243
1244         // are there any hfills in the row?
1245         int const nh = numberOfHfills(par, row);
1246
1247         if (nh) {
1248                 if (w > 0)
1249                         result.hfill = w / nh;
1250         // we don't have to look at the alignment if it is ALIGN_LEFT and
1251         // if the row is already larger then the permitted width as then
1252         // we force the LEFT_ALIGN'edness!
1253         } else if (int(row.width()) < maxwidth_) {
1254                 // is it block, flushleft or flushright?
1255                 // set x how you need it
1256                 int align;
1257                 if (par.params().align() == LYX_ALIGN_LAYOUT)
1258                         align = layout->align;
1259                 else
1260                         align = par.params().align();
1261
1262                 // Display-style insets should always be on a centred row
1263                 // The test on par.size() is to catch zero-size pars, which
1264                 // would trigger the assert in Paragraph::getInset().
1265                 //inset = par.size() ? par.getInset(row.pos()) : 0;
1266                 if (!par.empty()
1267                     && par.isInset(row.pos())
1268                     && par.getInset(row.pos())->display())
1269                 {
1270                         align = LYX_ALIGN_CENTER;
1271                 }
1272
1273                 switch (align) {
1274                 case LYX_ALIGN_BLOCK: {
1275                         int const ns = numberOfSeparators(par, row);
1276                         bool disp_inset = false;
1277                         if (row.endpos() < par.size()) {
1278                                 InsetBase const * in = par.getInset(row.endpos());
1279                                 if (in)
1280                                         disp_inset = in->display();
1281                         }
1282                         // If we have separators, this is not the last row of a
1283                         // par, does not end in newline, and is not row above a
1284                         // display inset... then stretch it
1285                         if (ns
1286                             && row.endpos() < par.size()
1287                             && !par.isNewline(row.endpos() - 1)
1288                             && !disp_inset
1289                                 ) {
1290                                 result.separator = w / ns;
1291                         } else if (is_rtl) {
1292                                 result.x += w;
1293                         }
1294                         break;
1295                 }
1296                 case LYX_ALIGN_RIGHT:
1297                         result.x += w;
1298                         break;
1299                 case LYX_ALIGN_CENTER:
1300                         result.x += w / 2;
1301                         break;
1302                 }
1303         }
1304
1305         bidi.computeTables(par, *bv()->buffer(), row);
1306         if (is_rtl) {
1307                 pos_type body_pos = par.beginOfBody();
1308                 pos_type end = row.endpos();
1309
1310                 if (body_pos > 0
1311                     && (body_pos > end || !par.isLineSeparator(body_pos - 1)))
1312                 {
1313                         result.x += font_metrics::width(layout->labelsep, getLabelFont(par));
1314                         if (body_pos <= end)
1315                                 result.x += result.label_hfill;
1316                 }
1317         }
1318
1319         return result;
1320 }
1321
1322
1323 // the cursor set functions have a special mechanism. When they
1324 // realize, that you left an empty paragraph, they will delete it.
1325
1326 bool LyXText::cursorRightOneWord(LCursor & cur)
1327 {
1328         BOOST_ASSERT(this == cur.text());
1329
1330         LCursor old = cur;
1331
1332         if (old.pos() == old.lastpos() && old.pit() != old.lastpit()) {
1333                 ++old.pit();
1334                 old.pos() = 0;
1335         } else {
1336                 // Skip through initial nonword stuff.
1337                 // Treat floats and insets as words.
1338                 while (old.pos() != old.lastpos() && !old.paragraph().isLetter(old.pos()))
1339                         ++old.pos();
1340                 // Advance through word.
1341                 while (old.pos() != old.lastpos() && old.paragraph().isLetter(old.pos()))
1342                         ++old.pos();
1343         }
1344         return setCursor(cur, old.pit(), old.pos());
1345 }
1346
1347
1348 bool LyXText::cursorLeftOneWord(LCursor & cur)
1349 {
1350         BOOST_ASSERT(this == cur.text());
1351
1352         LCursor old = cur;
1353
1354         if (old.pos() == 0 && old.pit() != 0) {
1355                 --old.pit();
1356                 old.pos() = old.lastpos();
1357         } else {
1358                 // Skip through initial nonword stuff.
1359                 // Treat floats and insets as words.
1360                 while (old.pos() != 0 && !old.paragraph().isLetter(old.pos() - 1))
1361                         --old.pos();
1362                 // Advance through word.
1363                 while (old.pos() != 0 && old.paragraph().isLetter(old.pos() - 1))
1364                         --old.pos();
1365         }
1366         return setCursor(cur, old.pit(), old.pos());
1367 }
1368
1369
1370 void LyXText::selectWord(LCursor & cur, word_location loc)
1371 {
1372         BOOST_ASSERT(this == cur.text());
1373         CursorSlice from = cur.top();
1374         CursorSlice to = cur.top();
1375         getWord(from, to, loc);
1376         if (cur.top() != from)
1377                 setCursor(cur, from.pit(), from.pos());
1378         if (to == from)
1379                 return;
1380         cur.resetAnchor();
1381         setCursor(cur, to.pit(), to.pos());
1382         cur.setSelection();
1383 }
1384
1385
1386 // Select the word currently under the cursor when no
1387 // selection is currently set
1388 bool LyXText::selectWordWhenUnderCursor(LCursor & cur, word_location loc)
1389 {
1390         BOOST_ASSERT(this == cur.text());
1391         if (cur.selection())
1392                 return false;
1393         selectWord(cur, loc);
1394         return cur.selection();
1395 }
1396
1397
1398 void LyXText::acceptChange(LCursor & cur)
1399 {
1400         BOOST_ASSERT(this == cur.text());
1401         if (!cur.selection() && cur.lastpos() != 0)
1402                 return;
1403
1404         CursorSlice const & startc = cur.selBegin();
1405         CursorSlice const & endc = cur.selEnd();
1406         if (startc.pit() == endc.pit()) {
1407                 recordUndoSelection(cur, Undo::INSERT);
1408                 pars_[startc.pit()].acceptChange(startc.pos(), endc.pos());
1409                 finishUndo();
1410                 cur.clearSelection();
1411                 setCursorIntern(cur, startc.pit(), 0);
1412         }
1413 #ifdef WITH_WARNINGS
1414 #warning handle multi par selection
1415 #endif
1416 }
1417
1418
1419 void LyXText::rejectChange(LCursor & cur)
1420 {
1421         BOOST_ASSERT(this == cur.text());
1422         if (!cur.selection() && cur.lastpos() != 0)
1423                 return;
1424
1425         CursorSlice const & startc = cur.selBegin();
1426         CursorSlice const & endc = cur.selEnd();
1427         if (startc.pit() == endc.pit()) {
1428                 recordUndoSelection(cur, Undo::INSERT);
1429                 pars_[startc.pit()].rejectChange(startc.pos(), endc.pos());
1430                 finishUndo();
1431                 cur.clearSelection();
1432                 setCursorIntern(cur, startc.pit(), 0);
1433         }
1434 #ifdef WITH_WARNINGS
1435 #warning handle multi par selection
1436 #endif
1437 }
1438
1439
1440 // Delete from cursor up to the end of the current or next word.
1441 void LyXText::deleteWordForward(LCursor & cur)
1442 {
1443         BOOST_ASSERT(this == cur.text());
1444         if (cur.lastpos() == 0)
1445                 cursorRight(cur);
1446         else {
1447                 cur.resetAnchor();
1448                 cur.selection() = true;
1449                 cursorRightOneWord(cur);
1450                 cur.setSelection();
1451                 cutSelection(cur, true, false);
1452         }
1453 }
1454
1455
1456 // Delete from cursor to start of current or prior word.
1457 void LyXText::deleteWordBackward(LCursor & cur)
1458 {
1459         BOOST_ASSERT(this == cur.text());
1460         if (cur.lastpos() == 0)
1461                 cursorLeft(cur);
1462         else {
1463                 cur.resetAnchor();
1464                 cur.selection() = true;
1465                 cursorLeftOneWord(cur);
1466                 cur.setSelection();
1467                 cutSelection(cur, true, false);
1468         }
1469 }
1470
1471
1472 // Kill to end of line.
1473 void LyXText::deleteLineForward(LCursor & cur)
1474 {
1475         BOOST_ASSERT(this == cur.text());
1476         if (cur.lastpos() == 0) {
1477                 // Paragraph is empty, so we just go to the right
1478                 cursorRight(cur);
1479         } else {
1480                 cur.resetAnchor();
1481                 cur.selection() = true; // to avoid deletion
1482                 cursorEnd(cur);
1483                 cur.setSelection();
1484                 // What is this test for ??? (JMarc)
1485                 if (!cur.selection())
1486                         deleteWordForward(cur);
1487                 else
1488                         cutSelection(cur, true, false);
1489         }
1490 }
1491
1492
1493 void LyXText::changeCase(LCursor & cur, LyXText::TextCase action)
1494 {
1495         BOOST_ASSERT(this == cur.text());
1496         CursorSlice from;
1497         CursorSlice to;
1498
1499         if (cur.selection()) {
1500                 from = cur.selBegin();
1501                 to = cur.selEnd();
1502         } else {
1503                 from = cur.top();
1504                 getWord(from, to, lyx::PARTIAL_WORD);
1505                 setCursor(cur, to.pit(), to.pos() + 1);
1506         }
1507
1508         recordUndoSelection(cur);
1509
1510         pos_type pos = from.pos();
1511         int par = from.pit();
1512
1513         while (par != int(pars_.size()) && (pos != to.pos() || par != to.pit())) {
1514                 pit_type pit = par;
1515                 if (pos == pars_[pit].size()) {
1516                         ++par;
1517                         pos = 0;
1518                         continue;
1519                 }
1520                 unsigned char c = pars_[pit].getChar(pos);
1521                 if (c != Paragraph::META_INSET) {
1522                         switch (action) {
1523                         case text_lowercase:
1524                                 c = lowercase(c);
1525                                 break;
1526                         case text_capitalization:
1527                                 c = uppercase(c);
1528                                 action = text_lowercase;
1529                                 break;
1530                         case text_uppercase:
1531                                 c = uppercase(c);
1532                                 break;
1533                         }
1534                 }
1535 #ifdef WITH_WARNINGS
1536 #warning changes
1537 #endif
1538                 pars_[pit].setChar(pos, c);
1539                 ++pos;
1540         }
1541 }
1542
1543
1544 void LyXText::Delete(LCursor & cur)
1545 {
1546         BOOST_ASSERT(this == cur.text());
1547
1548         if (cur.pos() != cur.lastpos()) {
1549                 recordUndo(cur, Undo::DELETE, cur.pit());
1550                 setCursorIntern(cur, cur.pit(), cur.pos() + 1, false, cur.boundary());
1551                 backspace(cur);
1552         } else if (cur.pit() != cur.lastpit()) {
1553                 LCursor scur = cur;
1554
1555                 setCursorIntern(cur, cur.pit()+1, 0, false, false);
1556                 if (pars_[cur.pit()].layout() == pars_[scur.pit()].layout()) {
1557                         recordUndo(scur, Undo::DELETE, scur.pit());
1558                         backspace(cur);
1559                 } else {
1560                         setCursorIntern(scur, scur.pit(), scur.pos(), false, scur.boundary());
1561                 }
1562         }
1563 }
1564
1565
1566 void LyXText::backspace(LCursor & cur)
1567 {
1568         BOOST_ASSERT(this == cur.text());
1569         if (cur.pos() == 0) {
1570                 // The cursor is at the beginning of a paragraph, so
1571                 // the the backspace will collapse two paragraphs into
1572                 // one.
1573
1574                 // but it's not allowed unless it's new
1575                 Paragraph & par = cur.paragraph();
1576                 if (par.isChangeEdited(0, par.size()))
1577                         return;
1578
1579                 // we may paste some paragraphs
1580
1581                 // is it an empty paragraph?
1582                 pos_type lastpos = cur.lastpos();
1583                 if (lastpos == 0 || (lastpos == 1 && par.isSeparator(0))) {
1584                         // This is an empty paragraph and we delete it just
1585                         // by moving the cursor one step
1586                         // left and let the DeleteEmptyParagraphMechanism
1587                         // handle the actual deletion of the paragraph.
1588
1589                         if (cur.pit() != 0) {
1590                                 // For KeepEmpty layouts we need to get
1591                                 // rid of the keepEmpty setting first.
1592                                 // And the only way to do this is to
1593                                 // reset the layout to something
1594                                 // else: f.ex. the default layout.
1595                                 if (par.allowEmpty()) {
1596                                         Buffer & buf = cur.buffer();
1597                                         BufferParams const & bparams = buf.params();
1598                                         par.layout(bparams.getLyXTextClass().defaultLayout());
1599                                 }
1600                                 
1601                                 cursorLeft(cur);
1602                                 return;
1603                         }
1604                 }
1605
1606                 if (cur.pit() != 0)
1607                         recordUndo(cur, Undo::DELETE, cur.pit() - 1);
1608
1609                 pit_type tmppit = cur.pit();
1610                 // We used to do cursorLeftIntern() here, but it is
1611                 // not a good idea since it triggers the auto-delete
1612                 // mechanism. So we do a cursorLeftIntern()-lite,
1613                 // without the dreaded mechanism. (JMarc)
1614                 if (cur.pit() != 0) {
1615                         // steps into the above paragraph.
1616                         setCursorIntern(cur, cur.pit() - 1,
1617                                         pars_[cur.pit() - 1].size(),
1618                                         false);
1619                 }
1620
1621                 // Pasting is not allowed, if the paragraphs have different
1622                 // layout. I think it is a real bug of all other
1623                 // word processors to allow it. It confuses the user.
1624                 // Correction: Pasting is always allowed with standard-layout
1625                 // Correction (Jug 20050717): Remove check about alignment!
1626                 Buffer & buf = cur.buffer();
1627                 BufferParams const & bufparams = buf.params();
1628                 LyXTextClass const & tclass = bufparams.getLyXTextClass();
1629                 pit_type const cpit = cur.pit();
1630
1631                 if (cpit != tmppit
1632                     && (pars_[cpit].layout() == pars_[tmppit].layout()
1633                         || pars_[tmppit].layout() == tclass.defaultLayout()))
1634                 {
1635                         mergeParagraph(bufparams, pars_, cpit);
1636
1637                         if (cur.pos() != 0 && pars_[cpit].isSeparator(cur.pos() - 1))
1638                                 --cur.pos();
1639
1640                         // the counters may have changed
1641                         updateCounters(cur.buffer());
1642                         setCursor(cur, cur.pit(), cur.pos(), false);
1643                 }
1644         } else {
1645                 // this is the code for a normal backspace, not pasting
1646                 // any paragraphs
1647                 recordUndo(cur, Undo::DELETE);
1648                 // We used to do cursorLeftIntern() here, but it is
1649                 // not a good idea since it triggers the auto-delete
1650                 // mechanism. So we do a cursorLeftIntern()-lite,
1651                 // without the dreaded mechanism. (JMarc)
1652                 setCursorIntern(cur, cur.pit(), cur.pos() - 1,
1653                                 false, cur.boundary());
1654                 cur.paragraph().erase(cur.pos());
1655         }
1656
1657         if (cur.pos() == cur.lastpos())
1658                 setCurrentFont(cur);
1659
1660         setCursor(cur, cur.pit(), cur.pos(), false, cur.boundary());
1661 }
1662
1663
1664 Row const & LyXText::firstRow() const
1665 {
1666         return *paragraphs().front().rows().begin();
1667 }
1668
1669
1670 bool LyXText::redoParagraph(pit_type const pit)
1671 {
1672         // remove rows of paragraph, keep track of height changes
1673         Paragraph & par = pars_[pit];
1674
1675         // Add bibitem insets if necessary
1676         if (par.layout()->labeltype == LABEL_BIBLIO) {
1677                 bool hasbibitem(false);
1678                 if (!par.insetlist.empty()
1679                         // Insist on it being in pos 0
1680                         && par.getChar(0) == Paragraph::META_INSET) {
1681                         InsetBase * inset = par.insetlist.begin()->inset;
1682                         if (inset->lyxCode() == InsetBase::BIBITEM_CODE)
1683                                 hasbibitem = true;
1684                 }
1685                 if (!hasbibitem) {
1686                         InsetBibitem * inset(new
1687                                 InsetBibitem(InsetCommandParams("bibitem")));
1688                         par.insertInset(0, static_cast<InsetBase *>(inset));
1689                         bv()->cursor().posRight();
1690                 }
1691         }
1692
1693         // redo insets
1694         InsetList::iterator ii = par.insetlist.begin();
1695         InsetList::iterator iend = par.insetlist.end();
1696         for (; ii != iend; ++ii) {
1697                 Dimension dim;
1698                 int const w = maxwidth_ - leftMargin(pit) - rightMargin(par);
1699                 MetricsInfo mi(bv(), getFont(par, ii->pos), w);
1700                 ii->inset->metrics(mi, dim);
1701         }
1702
1703         // rebreak the paragraph
1704         par.rows().clear();
1705         Dimension dim;
1706
1707         par.setBeginOfBody();
1708         pos_type z = 0;
1709         do {
1710                 Row row(z);
1711                 rowBreakPoint(pit, row);
1712                 setRowWidth(pit, row);
1713                 setHeightOfRow(pit, row);
1714                 par.rows().push_back(row);
1715                 dim.wid = std::max(dim.wid, row.width());
1716                 dim.des += row.height();
1717                 z = row.endpos();
1718         } while (z < par.size());
1719
1720         dim.asc += par.rows()[0].ascent();
1721         dim.des -= par.rows()[0].ascent();
1722
1723         bool const same = dim == par.dim();
1724
1725         par.dim() = dim;
1726         //lyxerr << "redoParagraph: " << par.rows().size() << " rows\n";
1727
1728         return !same;
1729 }
1730
1731
1732 void LyXText::metrics(MetricsInfo & mi, Dimension & dim)
1733 {
1734         //BOOST_ASSERT(mi.base.textwidth);
1735         if (mi.base.textwidth)
1736                 maxwidth_ = mi.base.textwidth;
1737         //lyxerr << "LyXText::metrics: width: " << mi.base.textwidth
1738         //      << " maxWidth: " << maxwidth_ << "\nfont: " << mi.base.font << endl;
1739         // save the caller's font locally:
1740         font_ = mi.base.font;
1741
1742         unsigned int h = 0;
1743         unsigned int w = 0;
1744         for (pit_type pit = 0, n = paragraphs().size(); pit != n; ++pit) {
1745                 redoParagraph(pit);
1746                 Paragraph & par = paragraphs()[pit];
1747                 h += par.height();
1748                 if (w < par.width())
1749                         w = par.width();
1750         }
1751
1752         dim.wid = w;
1753         dim.asc = pars_[0].ascent();
1754         dim.des = h - dim.asc;
1755
1756         dim_ = dim;
1757 }
1758
1759
1760 // only used for inset right now. should also be used for main text
1761 void LyXText::draw(PainterInfo & pi, int x, int y) const
1762 {
1763         paintTextInset(*this, pi, x, y);
1764 }
1765
1766
1767 #if 0
1768 // only used for inset right now. should also be used for main text
1769 void LyXText::drawSelection(PainterInfo & pi, int x , int) const
1770 {
1771         LCursor & cur = pi.base.bv->cursor();
1772         if (!cur.selection())
1773                 return;
1774         if (!ptr_cmp(cur.text(), this))
1775                 return;
1776
1777         lyxerr << "draw selection at " << x << endl;
1778
1779         // is there a better way of getting these two iterators?
1780         DocIterator beg = cur;
1781         DocIterator end = cur;
1782
1783         beg.top() = cur.selBegin();
1784         end.top() = cur.selEnd();
1785
1786         // the selection doesn't touch the visible screen
1787         if (bv_funcs::status(pi.base.bv, beg) == bv_funcs::CUR_BELOW
1788             || bv_funcs::status(pi.base.bv, end) == bv_funcs::CUR_ABOVE)
1789                 return;
1790
1791         Paragraph const & par1 = pars_[beg.pit()];
1792         Paragraph const & par2 = pars_[end.pit()];
1793
1794         Row const & row1 = par1.getRow(beg.pos(), beg.boundary());
1795         Row const & row2 = par2.getRow(end.pos(), end.boundary());
1796
1797         int y1,x1,x2;
1798         if (bv_funcs::status(pi.base.bv, beg) == bv_funcs::CUR_ABOVE) {
1799                 y1 = 0;
1800                 x1 = 0;
1801                 x2 = 0;
1802         } else {
1803                 y1 = bv_funcs::getPos(beg).y_ - row1.ascent();
1804                 int const startx = cursorX(beg.top(), begin.boundary());
1805                 x1 = isRTL(par1) ? startx : 0;
1806                 x2 = isRTL(par1) ? 0 + dim_.wid : startx;
1807         }
1808
1809         int y2,X1,X2;
1810         if (bv_funcs::status(pi.base.bv, end) == bv_funcs::CUR_BELOW) {
1811                 y2 = pi.base.bv->workHeight();
1812                 X1 = 0;
1813                 X2 = 0;
1814         } else {
1815                 y2 = bv_funcs::getPos(end).y_ + row2.descent();
1816                 int const endx = cursorX(end.top(), end.boundary());
1817                 X1 = isRTL(par2) ? 0 : endx;
1818                 X2 = isRTL(par2) ? endx : 0 + dim_.wid;
1819         }
1820
1821         lyxerr << " y1: " << y1 << " y2: " << y2
1822                 << " xo: " << xo_ << " wid: " << dim_.wid
1823                 << endl;
1824
1825         // paint big rectangle in one go
1826         pi.pain.fillRectangle(x, y1, dim_.wid, y2 - y1, LColor::selection);
1827
1828         // reset background at begin of first selected line
1829         pi.pain.fillRectangle(x + x1, y1, x2 - x1, row1.height(),
1830                 LColor::background);
1831
1832         // reset background at end of last selected line
1833         pi.pain.fillRectangle(x + X1, y2  - row2.height(),
1834                 X2 - X1, row2.height(), LColor::background);
1835 }
1836
1837 #else
1838
1839 void LyXText::drawSelection(PainterInfo & pi, int x, int) const
1840 {
1841         LCursor & cur = pi.base.bv->cursor();
1842         if (!cur.selection())
1843                 return;
1844         if (!ptr_cmp(cur.text(), this))
1845                 return;
1846
1847         lyxerr[Debug::DEBUG]
1848                 << BOOST_CURRENT_FUNCTION
1849                 << "draw selection at " << x
1850                 << endl;
1851
1852         // is there a better way of getting these two iterators?
1853         DocIterator beg = cur;
1854         DocIterator end = cur;
1855
1856         beg.top() = cur.selBegin();
1857         end.top() = cur.selEnd();
1858
1859         // the selection doesn't touch the visible screen
1860         if (bv_funcs::status(pi.base.bv, beg) == bv_funcs::CUR_BELOW
1861             || bv_funcs::status(pi.base.bv, end) == bv_funcs::CUR_ABOVE)
1862                 return;
1863
1864         Paragraph const & par1 = pars_[beg.pit()];
1865         Paragraph const & par2 = pars_[end.pit()];
1866
1867         bool const above = (bv_funcs::status(pi.base.bv, beg)
1868                             == bv_funcs::CUR_ABOVE);
1869         bool const below = (bv_funcs::status(pi.base.bv, end)
1870                             == bv_funcs::CUR_BELOW);
1871         int y1,y2,x1,x2;
1872         if (above) {
1873                 y1 = 0;
1874                 y2 = 0;
1875                 x1 = 0;
1876                 x2 = dim_.wid;
1877         } else {
1878                 Row const & row1 = par1.getRow(beg.pos(), beg.boundary());
1879                 y1 = bv_funcs::getPos(beg, beg.boundary()).y_ - row1.ascent();
1880                 y2 = y1 + row1.height();
1881                 int const startx = cursorX(beg.top(), beg.boundary());
1882                 x1 = !isRTL(par1) ? startx : 0;
1883                 x2 = !isRTL(par1) ? 0 + dim_.wid : startx;
1884         }
1885
1886         int Y1,Y2,X1,X2;
1887         if (below) {
1888                 Y1 = pi.base.bv->workHeight();
1889                 Y2 = pi.base.bv->workHeight();
1890                 X1 = 0;
1891                 X2 = dim_.wid;
1892         } else {
1893                 Row const & row2 = par2.getRow(end.pos(), end.boundary());
1894                 Y1 = bv_funcs::getPos(end, end.boundary()).y_ - row2.ascent();
1895                 Y2 = Y1 + row2.height();
1896                 int const endx = cursorX(end.top(), end.boundary());
1897                 X1 = !isRTL(par2) ? 0 : endx;
1898                 X2 = !isRTL(par2) ? endx : 0 + dim_.wid;
1899         }
1900
1901         if (!above && !below && &par1.getRow(beg.pos(), end.boundary())
1902             == &par2.getRow(end.pos(), end.boundary()))
1903         {
1904                 // paint only one rectangle
1905                 pi.pain.fillRectangle(x + x1, y1, X2 - x1, y2 - y1,
1906                                       LColor::selection);
1907                 return;
1908         }
1909
1910         lyxerr[Debug::DEBUG] << " y1: " << y1 << " y2: " << y2
1911                << "X1:" << X1 << " x2: " << X2 << " wid: " << dim_.wid
1912                 << endl;
1913
1914         // paint upper rectangle
1915         pi.pain.fillRectangle(x + x1, y1, x2 - x1, y2 - y1,
1916                                       LColor::selection);
1917         // paint bottom rectangle
1918         pi.pain.fillRectangle(x + X1, Y1, X2 - X1, Y2 - Y1,
1919                                       LColor::selection);
1920         // paint center rectangle
1921         pi.pain.fillRectangle(x, y2, dim_.wid,
1922                               Y1 - y2, LColor::selection);
1923 }
1924 #endif
1925
1926 bool LyXText::isLastRow(pit_type pit, Row const & row) const
1927 {
1928         return row.endpos() >= pars_[pit].size()
1929                 && pit + 1 == pit_type(paragraphs().size());
1930 }
1931
1932
1933 bool LyXText::isFirstRow(pit_type pit, Row const & row) const
1934 {
1935         return row.pos() == 0 && pit == 0;
1936 }
1937
1938
1939 void LyXText::getWord(CursorSlice & from, CursorSlice & to,
1940         word_location const loc)
1941 {
1942         Paragraph const & from_par = pars_[from.pit()];
1943         switch (loc) {
1944         case lyx::WHOLE_WORD_STRICT:
1945                 if (from.pos() == 0 || from.pos() == from_par.size()
1946                     || !from_par.isLetter(from.pos())
1947                     || !from_par.isLetter(from.pos() - 1)) {
1948                         to = from;
1949                         return;
1950                 }
1951                 // no break here, we go to the next
1952
1953         case lyx::WHOLE_WORD:
1954                 // If we are already at the beginning of a word, do nothing
1955                 if (!from.pos() || !from_par.isLetter(from.pos() - 1))
1956                         break;
1957                 // no break here, we go to the next
1958
1959         case lyx::PREVIOUS_WORD:
1960                 // always move the cursor to the beginning of previous word
1961                 while (from.pos() && from_par.isLetter(from.pos() - 1))
1962                         --from.pos();
1963                 break;
1964         case lyx::NEXT_WORD:
1965                 lyxerr << "LyXText::getWord: NEXT_WORD not implemented yet"
1966                        << endl;
1967                 break;
1968         case lyx::PARTIAL_WORD:
1969                 // no need to move the 'from' cursor
1970                 break;
1971         }
1972         to = from;
1973         Paragraph & to_par = pars_[to.pit()];
1974         while (to.pos() < to_par.size() && to_par.isLetter(to.pos()))
1975                 ++to.pos();
1976 }
1977
1978
1979 void LyXText::write(Buffer const & buf, std::ostream & os) const
1980 {
1981         ParagraphList::const_iterator pit = paragraphs().begin();
1982         ParagraphList::const_iterator end = paragraphs().end();
1983         Paragraph::depth_type dth = 0;
1984         for (; pit != end; ++pit)
1985                 pit->write(buf, os, buf.params(), dth);
1986 }
1987
1988
1989 bool LyXText::read(Buffer const & buf, LyXLex & lex)
1990 {
1991         static Change current_change;
1992
1993         Paragraph::depth_type depth = 0;
1994
1995         while (lex.isOK()) {
1996                 lex.nextToken();
1997                 string const token = lex.getString();
1998
1999                 if (token.empty())
2000                         continue;
2001
2002                 if (token == "\\end_inset") {
2003                         break;
2004                 }
2005
2006                 if (token == "\\end_body") {
2007                         continue;
2008                 }
2009
2010                 if (token == "\\begin_body") {
2011                         continue;
2012                 }
2013
2014                 if (token == "\\end_document") {
2015                         return false;
2016                 }
2017
2018                 if (token == "\\begin_layout") {
2019                         lex.pushToken(token);
2020
2021                         Paragraph par;
2022                         par.params().depth(depth);
2023                         if (buf.params().tracking_changes)
2024                                 par.trackChanges();
2025                         par.setFont(0, LyXFont(LyXFont::ALL_INHERIT, buf.params().language));
2026                         pars_.push_back(par);
2027
2028                         // FIXME: goddamn InsetTabular makes us pass a Buffer
2029                         // not BufferParams
2030                         ::readParagraph(buf, pars_.back(), lex);
2031
2032                 } else if (token == "\\begin_deeper") {
2033                         ++depth;
2034                 } else if (token == "\\end_deeper") {
2035                         if (!depth) {
2036                                 lex.printError("\\end_deeper: " "depth is already null");
2037                         } else {
2038                                 --depth;
2039                         }
2040                 } else {
2041                         lyxerr << "Handling unknown body token: `"
2042                                << token << '\'' << endl;
2043                 }
2044         }
2045         return true;
2046 }
2047
2048
2049 int LyXText::ascent() const
2050 {
2051         return dim_.asc;
2052 }
2053
2054
2055 int LyXText::descent() const
2056 {
2057         return dim_.des;
2058 }
2059
2060
2061 int LyXText::cursorX(CursorSlice const & sl, bool boundary) const
2062 {
2063         pit_type const pit = sl.pit();
2064         Paragraph const & par = pars_[pit];
2065         if (par.rows().empty())
2066                 return 0;
2067
2068         pos_type ppos = sl.pos();
2069         // Correct position in front of big insets
2070         bool const boundary_correction = ppos != 0 && boundary;
2071         if (boundary_correction)
2072                 --ppos;
2073
2074         Row const & row = par.getRow(sl.pos(), boundary);
2075
2076         pos_type cursor_vpos = 0;
2077
2078         RowMetrics const m = computeRowMetrics(pit, row);
2079         double x = m.x;
2080
2081         pos_type const row_pos  = row.pos();
2082         pos_type const end      = row.endpos();
2083
2084         if (end <= row_pos)
2085                 cursor_vpos = row_pos;
2086         else if (ppos >= end)
2087                 cursor_vpos = isRTL(par) ? row_pos : end;
2088         else if (ppos > row_pos && ppos >= end)
2089                 // Place cursor after char at (logical) position pos - 1
2090                 cursor_vpos = (bidi.level(ppos - 1) % 2 == 0)
2091                         ? bidi.log2vis(ppos - 1) + 1 : bidi.log2vis(ppos - 1);
2092         else
2093                 // Place cursor before char at (logical) position ppos
2094                 cursor_vpos = (bidi.level(ppos) % 2 == 0)
2095                         ? bidi.log2vis(ppos) : bidi.log2vis(ppos) + 1;
2096
2097         pos_type body_pos = par.beginOfBody();
2098         if (body_pos > 0 &&
2099             (body_pos > end || !par.isLineSeparator(body_pos - 1)))
2100                 body_pos = 0;
2101
2102         // Use font span to speed things up, see below
2103         FontSpan font_span;
2104         LyXFont font;
2105
2106         for (pos_type vpos = row_pos; vpos < cursor_vpos; ++vpos) {
2107                 pos_type pos = bidi.vis2log(vpos);
2108                 if (body_pos > 0 && pos == body_pos - 1) {
2109                         x += m.label_hfill
2110                                 + font_metrics::width(par.layout()->labelsep,
2111                                                       getLabelFont(par));
2112                         if (par.isLineSeparator(body_pos - 1))
2113                                 x -= singleWidth(par, body_pos - 1);
2114                 }
2115
2116                 // Use font span to speed things up, see above
2117                 if (pos < font_span.first || pos > font_span.last) {
2118                         font_span = par.fontSpan(pos);
2119                         font = getFont(par, pos);
2120                 }
2121
2122                 x += singleWidth(par, pos, par.getChar(pos), font);
2123
2124                 if (hfillExpansion(par, row, pos))
2125                         x += (pos >= body_pos) ? m.hfill : m.label_hfill;
2126                 else if (par.isSeparator(pos) && pos >= body_pos)
2127                         x += m.separator;
2128         }
2129         
2130         // see correction above
2131         if (boundary_correction)
2132                 x += singleWidth(par, ppos);
2133
2134         return int(x);
2135 }
2136
2137
2138 int LyXText::cursorY(CursorSlice const & sl, bool boundary) const
2139 {
2140         //lyxerr << "LyXText::cursorY: boundary: " << boundary << std::endl;
2141         Paragraph const & par = getPar(sl.pit());
2142         int h = 0;
2143         h -= pars_[0].rows()[0].ascent();
2144         for (pit_type pit = 0; pit < sl.pit(); ++pit)
2145                 h += pars_[pit].height();
2146         int pos = sl.pos();
2147         if (pos && boundary)
2148                 --pos;
2149         size_t const rend = par.pos2row(pos);
2150         for (size_t rit = 0; rit != rend; ++rit)
2151                 h += par.rows()[rit].height();
2152         h += par.rows()[rend].ascent();
2153         return h;
2154 }
2155
2156
2157 // Returns the current font and depth as a message.
2158 string LyXText::currentState(LCursor & cur)
2159 {
2160         BOOST_ASSERT(this == cur.text());
2161         Buffer & buf = cur.buffer();
2162         Paragraph const & par = cur.paragraph();
2163         std::ostringstream os;
2164
2165         bool const show_change = buf.params().tracking_changes
2166                 && cur.pos() != cur.lastpos()
2167                 && par.lookupChange(cur.pos()) != Change::UNCHANGED;
2168
2169         if (show_change) {
2170                 Change change = par.lookupChangeFull(cur.pos());
2171                 Author const & a = buf.params().authors().get(change.author);
2172                 os << _("Change: ") << a.name();
2173                 if (!a.email().empty())
2174                         os << " (" << a.email() << ")";
2175                 if (change.changetime)
2176                         os << _(" at ") << ctime(&change.changetime);
2177                 os << " : ";
2178         }
2179
2180         // I think we should only show changes from the default
2181         // font. (Asger)
2182         LyXFont font = real_current_font;
2183         font.reduce(buf.params().getLyXTextClass().defaultfont());
2184
2185         // avoid _(...) re-entrance problem
2186         string const s = font.stateText(&buf.params());
2187         os << bformat(_("Font: %1$s"), s);
2188
2189         // os << bformat(_("Font: %1$s"), font.stateText(&buf.params));
2190
2191         // The paragraph depth
2192         int depth = cur.paragraph().getDepth();
2193         if (depth > 0)
2194                 os << bformat(_(", Depth: %1$d"), depth);
2195
2196         // The paragraph spacing, but only if different from
2197         // buffer spacing.
2198         Spacing const & spacing = par.params().spacing();
2199         if (!spacing.isDefault()) {
2200                 os << _(", Spacing: ");
2201                 switch (spacing.getSpace()) {
2202                 case Spacing::Single:
2203                         os << _("Single");
2204                         break;
2205                 case Spacing::Onehalf:
2206                         os << _("OneHalf");
2207                         break;
2208                 case Spacing::Double:
2209                         os << _("Double");
2210                         break;
2211                 case Spacing::Other:
2212                         os << _("Other (") << spacing.getValueAsString() << ')';
2213                         break;
2214                 case Spacing::Default:
2215                         // should never happen, do nothing
2216                         break;
2217                 }
2218         }
2219
2220 #ifdef DEVEL_VERSION
2221         os << _(", Inset: ") << &cur.inset();
2222         os << _(", Paragraph: ") << cur.pit();
2223         os << _(", Id: ") << par.id();
2224         os << _(", Position: ") << cur.pos();
2225         os << _(", Boundary: ") << cur.boundary();
2226 //      Row & row = cur.textRow();
2227 //      os << bformat(_(", Row b:%1$d e:%2$d"), row.pos(), row.endpos());
2228 #endif
2229         return os.str();
2230 }
2231
2232
2233 string LyXText::getPossibleLabel(LCursor & cur) const
2234 {
2235         pit_type pit = cur.pit();
2236
2237         LyXLayout_ptr layout = pars_[pit].layout();
2238
2239         if (layout->latextype == LATEX_PARAGRAPH && pit != 0) {
2240                 LyXLayout_ptr const & layout2 = pars_[pit - 1].layout();
2241                 if (layout2->latextype != LATEX_PARAGRAPH) {
2242                         --pit;
2243                         layout = layout2;
2244                 }
2245         }
2246
2247         string text = layout->latexname().substr(0, 3);
2248         if (layout->latexname() == "theorem")
2249                 text = "thm"; // Create a correct prefix for prettyref
2250
2251         text += ':';
2252         if (layout->latextype == LATEX_PARAGRAPH || lyxrc.label_init_length < 0)
2253                 text.erase();
2254
2255         string par_text = pars_[pit].asString(cur.buffer(), false);
2256         for (int i = 0; i < lyxrc.label_init_length; ++i) {
2257                 if (par_text.empty())
2258                         break;
2259                 string head;
2260                 par_text = split(par_text, head, ' ');
2261                 // Is it legal to use spaces in labels ?
2262                 if (i > 0)
2263                         text += '-';
2264                 text += head;
2265         }
2266
2267         return text;
2268 }
2269
2270
2271 //pos_type LyXText::x2pos(pit_type pit, int row, int x) const
2272 //{
2273 //      int lastx = 0;
2274 //      int currx = 0;
2275 //      Paragraph const & par = pars_[pit];
2276 //      Row const & r = par.rows()[row];
2277 //      int pos = r.pos();
2278 //      for (; currx < x && pos < r.endpos(); ++pos) {
2279 //              lastx = currx;
2280 //              currx += singleWidth(par, pos);
2281 //      }
2282 //      if (abs(lastx - x) < abs(currx - x) && pos != r.pos())
2283 //              --pos;
2284 //      return pos;
2285 //}
2286
2287
2288 pos_type LyXText::x2pos(pit_type pit, int row, int x) const
2289 {
2290         BOOST_ASSERT(row < int(pars_[pit].rows().size()));
2291         bool bound = false;
2292         Row const & r = pars_[pit].rows()[row];
2293         return r.pos() + getColumnNearX(pit, r, x, bound);
2294 }
2295
2296
2297 //int LyXText::pos2x(pit_type pit, pos_type pos) const
2298 //{
2299 //      Paragraph const & par = pars_[pit];
2300 //      Row const & r = par.rows()[row];
2301 //      int x = 0;
2302 //      pos -= r.pos();
2303 //}
2304
2305
2306 // x,y are screen coordinates
2307 // sets cursor only within this LyXText
2308 void LyXText::setCursorFromCoordinates(LCursor & cur, int const x, int const y)
2309 {
2310         pit_type pit = getPitNearY(y);
2311         int yy = theCoords.get(this, pit).y_ - pars_[pit].ascent();
2312         lyxerr[Debug::DEBUG]
2313                 << BOOST_CURRENT_FUNCTION
2314                 << ": x: " << x
2315                 << " y: " << y
2316                 << " pit: " << pit
2317                 << " yy: " << yy << endl;
2318
2319         Paragraph const & par = pars_[pit];
2320         int r = 0;
2321         BOOST_ASSERT(par.rows().size());
2322         for (; r < int(par.rows().size()) - 1; ++r) {
2323                 Row const & row = par.rows()[r];
2324                 if (int(yy + row.height()) > y)
2325                         break;
2326                 yy += row.height();
2327         }
2328
2329         Row const & row = par.rows()[r];
2330
2331         lyxerr[Debug::DEBUG]
2332                 << BOOST_CURRENT_FUNCTION
2333                 << ": row " << r
2334                 << " from pos: " << row.pos()
2335                 << endl;
2336
2337         bool bound = false;
2338         int xx = x;
2339         pos_type const pos = row.pos() + getColumnNearX(pit, row, xx, bound);
2340
2341         lyxerr[Debug::DEBUG]
2342                 << BOOST_CURRENT_FUNCTION
2343                 << ": setting cursor pit: " << pit
2344                 << " pos: " << pos
2345                 << endl;
2346         
2347         setCursor(cur, pit, pos, true, bound);
2348 }