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