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