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