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