]> git.lyx.org Git - lyx.git/blob - src/Text.cpp
Routines for calculating numerical labels for BibTeX citations.
[lyx.git] / src / Text.cpp
1 /**
2  * \file src/Text.cpp
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 Dov Feldstern
9  * \author Jean-Marc Lasgouttes
10  * \author John Levon
11  * \author André Pönitz
12  * \author Stefan Schimanski
13  * \author Dekel Tsur
14  * \author Jürgen Vigna
15  *
16  * Full author contact details are available in file CREDITS.
17  */
18
19 #include <config.h>
20
21 #include "Text.h"
22
23 #include "Author.h"
24 #include "Buffer.h"
25 #include "buffer_funcs.h"
26 #include "BufferParams.h"
27 #include "BufferView.h"
28 #include "Changes.h"
29 #include "CompletionList.h"
30 #include "Cursor.h"
31 #include "CutAndPaste.h"
32 #include "DispatchResult.h"
33 #include "Encoding.h"
34 #include "ErrorList.h"
35 #include "FuncRequest.h"
36 #include "factory.h"
37 #include "InsetList.h"
38 #include "Language.h"
39 #include "Layout.h"
40 #include "Length.h"
41 #include "Lexer.h"
42 #include "lyxfind.h"
43 #include "LyXRC.h"
44 #include "Paragraph.h"
45 #include "ParagraphParameters.h"
46 #include "ParIterator.h"
47 #include "TextClass.h"
48 #include "TextMetrics.h"
49 #include "VSpace.h"
50 #include "WordLangTuple.h"
51 #include "WordList.h"
52
53 #include "insets/InsetText.h"
54 #include "insets/InsetBibitem.h"
55 #include "insets/InsetCaption.h"
56 #include "insets/InsetLine.h"
57 #include "insets/InsetNewline.h"
58 #include "insets/InsetNewpage.h"
59 #include "insets/InsetOptArg.h"
60 #include "insets/InsetSpace.h"
61 #include "insets/InsetSpecialChar.h"
62 #include "insets/InsetTabular.h"
63
64 #include "support/debug.h"
65 #include "support/docstream.h"
66 #include "support/gettext.h"
67 #include "support/lassert.h"
68 #include "support/lstrings.h"
69 #include "support/textutils.h"
70
71 #include <boost/next_prior.hpp>
72
73 #include <sstream>
74
75 using namespace std;
76 using namespace lyx::support;
77
78 namespace lyx {
79
80 using cap::cutSelection;
81 using cap::pasteParagraphList;
82
83 static bool moveItem(Paragraph & fromPar, pos_type fromPos,
84         Paragraph & toPar, pos_type toPos, BufferParams const & params)
85 {
86         // Note: moveItem() does not honour change tracking!
87         // Therefore, it should only be used for breaking and merging paragraphs
88
89         // We need a copy here because the character at fromPos is going to be erased.
90         Font const tmpFont = fromPar.getFontSettings(params, fromPos);
91         Change const tmpChange = fromPar.lookupChange(fromPos);
92
93         if (Inset * tmpInset = fromPar.getInset(fromPos)) {
94                 fromPar.releaseInset(fromPos);
95                 // The inset is not in fromPar any more.
96                 if (!toPar.insertInset(toPos, tmpInset, tmpFont, tmpChange)) {
97                         delete tmpInset;
98                         return false;
99                 }
100                 return true;
101         }
102
103         char_type const tmpChar = fromPar.getChar(fromPos);
104         fromPar.eraseChar(fromPos, false);
105         toPar.insertChar(toPos, tmpChar, tmpFont, tmpChange);
106         return true;
107 }
108
109
110 void breakParagraphConservative(BufferParams const & bparams,
111         ParagraphList & pars, pit_type par_offset, pos_type pos)
112 {
113         // create a new paragraph
114         Paragraph & tmp = *pars.insert(boost::next(pars.begin(), par_offset + 1),
115                                        Paragraph());
116         Paragraph & par = pars[par_offset];
117
118         tmp.setInsetOwner(&par.inInset());
119         tmp.makeSameLayout(par);
120
121         LASSERT(pos <= par.size(), /**/);
122
123         if (pos < par.size()) {
124                 // move everything behind the break position to the new paragraph
125                 pos_type pos_end = par.size() - 1;
126
127                 for (pos_type i = pos, j = 0; i <= pos_end; ++i) {
128                         if (moveItem(par, pos, tmp, j, bparams)) {
129                                 ++j;
130                         }
131                 }
132                 // Move over the end-of-par change information
133                 tmp.setChange(tmp.size(), par.lookupChange(par.size()));
134                 par.setChange(par.size(), Change(bparams.trackChanges ?
135                                            Change::INSERTED : Change::UNCHANGED));
136         }
137 }
138
139
140 void mergeParagraph(BufferParams const & bparams,
141         ParagraphList & pars, pit_type par_offset)
142 {
143         Paragraph & next = pars[par_offset + 1];
144         Paragraph & par = pars[par_offset];
145
146         pos_type pos_end = next.size() - 1;
147         pos_type pos_insert = par.size();
148
149         // the imaginary end-of-paragraph character (at par.size()) has to be
150         // marked as unmodified. Otherwise, its change is adopted by the first
151         // character of the next paragraph.
152         if (par.isChanged(par.size())) {
153                 LYXERR(Debug::CHANGES,
154                    "merging par with inserted/deleted end-of-par character");
155                 par.setChange(par.size(), Change(Change::UNCHANGED));
156         }
157
158         Change change = next.lookupChange(next.size());
159
160         // move the content of the second paragraph to the end of the first one
161         for (pos_type i = 0, j = pos_insert; i <= pos_end; ++i) {
162                 if (moveItem(next, 0, par, j, bparams)) {
163                         ++j;
164                 }
165         }
166
167         // move the change of the end-of-paragraph character
168         par.setChange(par.size(), change);
169
170         pars.erase(boost::next(pars.begin(), par_offset + 1));
171 }
172
173
174 Text::Text(InsetText * owner, bool use_default_layout)
175         : owner_(owner), autoBreakRows_(false), undo_counter_(0)
176 {
177         pars_.push_back(Paragraph());
178         Paragraph & par = pars_.back();
179         par.setInsetOwner(owner);
180         DocumentClass const & dc = owner->buffer().params().documentClass();
181         if (use_default_layout)
182                 par.setDefaultLayout(dc);
183         else
184                 par.setPlainLayout(dc);
185 }
186
187
188 Text::Text(InsetText * owner, Text const & text)
189         : owner_(owner), autoBreakRows_(text.autoBreakRows_), undo_counter_(0)
190 {
191         pars_ = text.pars_;
192         ParagraphList::iterator const end = pars_.end();
193         ParagraphList::iterator it = pars_.begin();
194         for (; it != end; ++it)
195                 it->setInsetOwner(owner);
196 }
197
198
199 pit_type Text::depthHook(pit_type pit, depth_type depth) const
200 {
201         pit_type newpit = pit;
202
203         if (newpit != 0)
204                 --newpit;
205
206         while (newpit != 0 && pars_[newpit].getDepth() > depth)
207                 --newpit;
208
209         if (pars_[newpit].getDepth() > depth)
210                 return pit;
211
212         return newpit;
213 }
214
215
216 pit_type Text::outerHook(pit_type par_offset) const
217 {
218         Paragraph const & par = pars_[par_offset];
219
220         if (par.getDepth() == 0)
221                 return pars_.size();
222         return depthHook(par_offset, depth_type(par.getDepth() - 1));
223 }
224
225
226 bool Text::isFirstInSequence(pit_type par_offset) const
227 {
228         Paragraph const & par = pars_[par_offset];
229
230         pit_type dhook_offset = depthHook(par_offset, par.getDepth());
231
232         if (dhook_offset == par_offset)
233                 return true;
234
235         Paragraph const & dhook = pars_[dhook_offset];
236
237         return dhook.layout() != par.layout()
238                 || dhook.getDepth() != par.getDepth();
239 }
240
241
242 Font const Text::outerFont(pit_type par_offset) const
243 {
244         depth_type par_depth = pars_[par_offset].getDepth();
245         FontInfo tmpfont = inherit_font;
246
247         // Resolve against environment font information
248         while (par_offset != pit_type(pars_.size())
249                && par_depth
250                && !tmpfont.resolved()) {
251                 par_offset = outerHook(par_offset);
252                 if (par_offset != pit_type(pars_.size())) {
253                         tmpfont.realize(pars_[par_offset].layout().font);
254                         par_depth = pars_[par_offset].getDepth();
255                 }
256         }
257
258         return Font(tmpfont);
259 }
260
261
262 void acceptChanges(ParagraphList & pars, BufferParams const & bparams)
263 {
264         pit_type pars_size = static_cast<pit_type>(pars.size());
265
266         // first, accept changes within each individual paragraph
267         // (do not consider end-of-par)
268         for (pit_type pit = 0; pit < pars_size; ++pit) {
269                 if (!pars[pit].empty())   // prevent assertion failure
270                         pars[pit].acceptChanges(0, pars[pit].size());
271         }
272
273         // next, accept imaginary end-of-par characters
274         for (pit_type pit = 0; pit < pars_size; ++pit) {
275                 pos_type pos = pars[pit].size();
276
277                 if (pars[pit].isInserted(pos)) {
278                         pars[pit].setChange(pos, Change(Change::UNCHANGED));
279                 } else if (pars[pit].isDeleted(pos)) {
280                         if (pit == pars_size - 1) {
281                                 // we cannot remove a par break at the end of the last
282                                 // paragraph; instead, we mark it unchanged
283                                 pars[pit].setChange(pos, Change(Change::UNCHANGED));
284                         } else {
285                                 mergeParagraph(bparams, pars, pit);
286                                 --pit;
287                                 --pars_size;
288                         }
289                 }
290         }
291 }
292
293 InsetText const & Text::inset() const
294 {
295         return *owner_;
296 }
297
298
299 void Text::readParToken(Paragraph & par, Lexer & lex,
300         string const & token, Font & font, Change & change, ErrorList & errorList)
301 {
302         Buffer * buf = const_cast<Buffer *>(&owner_->buffer());
303         BufferParams const & bp = buf->params();
304
305         if (token[0] != '\\') {
306                 docstring dstr = lex.getDocString();
307                 par.appendString(dstr, font, change);
308
309         } else if (token == "\\begin_layout") {
310                 lex.eatLine();
311                 docstring layoutname = lex.getDocString();
312
313                 font = Font(inherit_font, bp.language);
314                 change = Change(Change::UNCHANGED);
315
316                 DocumentClass const & tclass = bp.documentClass();
317
318                 if (layoutname.empty())
319                         layoutname = tclass.defaultLayoutName();
320
321                 if (owner_->forcePlainLayout()) {
322                         // in this case only the empty layout is allowed
323                         layoutname = tclass.plainLayoutName();
324                 } else if (par.usePlainLayout()) {
325                         // in this case, default layout maps to empty layout 
326                         if (layoutname == tclass.defaultLayoutName())
327                                 layoutname = tclass.plainLayoutName();
328                 } else { 
329                         // otherwise, the empty layout maps to the default
330                         if (layoutname == tclass.plainLayoutName())
331                                 layoutname = tclass.defaultLayoutName();
332                 }
333
334                 // When we apply an unknown layout to a document, we add this layout to the textclass
335                 // of this document. For example, when you apply class article to a beamer document,
336                 // all unknown layouts such as frame will be added to document class article so that
337                 // these layouts can keep their original names.
338                 tclass.addLayoutIfNeeded(layoutname);
339
340                 par.setLayout(bp.documentClass()[layoutname]);
341
342                 // Test whether the layout is obsolete.
343                 Layout const & layout = par.layout();
344                 if (!layout.obsoleted_by().empty())
345                         par.setLayout(bp.documentClass()[layout.obsoleted_by()]);
346
347                 par.params().read(lex);
348
349         } else if (token == "\\end_layout") {
350                 LYXERR0("Solitary \\end_layout in line " << lex.lineNumber() << "\n"
351                        << "Missing \\begin_layout ?");
352         } else if (token == "\\end_inset") {
353                 LYXERR0("Solitary \\end_inset in line " << lex.lineNumber() << "\n"
354                        << "Missing \\begin_inset ?");
355         } else if (token == "\\begin_inset") {
356                 Inset * inset = readInset(lex, buf);
357                 if (inset)
358                         par.insertInset(par.size(), inset, font, change);
359                 else {
360                         lex.eatLine();
361                         docstring line = lex.getDocString();
362                         errorList.push_back(ErrorItem(_("Unknown Inset"), line,
363                                             par.id(), 0, par.size()));
364                 }
365         } else if (token == "\\family") {
366                 lex.next();
367                 setLyXFamily(lex.getString(), font.fontInfo());
368         } else if (token == "\\series") {
369                 lex.next();
370                 setLyXSeries(lex.getString(), font.fontInfo());
371         } else if (token == "\\shape") {
372                 lex.next();
373                 setLyXShape(lex.getString(), font.fontInfo());
374         } else if (token == "\\size") {
375                 lex.next();
376                 setLyXSize(lex.getString(), font.fontInfo());
377         } else if (token == "\\lang") {
378                 lex.next();
379                 string const tok = lex.getString();
380                 Language const * lang = languages.getLanguage(tok);
381                 if (lang) {
382                         font.setLanguage(lang);
383                 } else {
384                         font.setLanguage(bp.language);
385                         lex.printError("Unknown language `$$Token'");
386                 }
387         } else if (token == "\\numeric") {
388                 lex.next();
389                 font.fontInfo().setNumber(setLyXMisc(lex.getString()));
390         } else if (token == "\\emph") {
391                 lex.next();
392                 font.fontInfo().setEmph(setLyXMisc(lex.getString()));
393         } else if (token == "\\bar") {
394                 lex.next();
395                 string const tok = lex.getString();
396
397                 if (tok == "under")
398                         font.fontInfo().setUnderbar(FONT_ON);
399                 else if (tok == "no")
400                         font.fontInfo().setUnderbar(FONT_OFF);
401                 else if (tok == "default")
402                         font.fontInfo().setUnderbar(FONT_INHERIT);
403                 else
404                         lex.printError("Unknown bar font flag "
405                                        "`$$Token'");
406         } else if (token == "\\strikeout") {
407                 lex.next();
408                 font.fontInfo().setStrikeout(setLyXMisc(lex.getString()));
409         } else if (token == "\\uuline") {
410                 lex.next();
411                 font.fontInfo().setUuline(setLyXMisc(lex.getString()));
412         } else if (token == "\\uwave") {
413                 lex.next();
414                 font.fontInfo().setUwave(setLyXMisc(lex.getString()));
415         } else if (token == "\\noun") {
416                 lex.next();
417                 font.fontInfo().setNoun(setLyXMisc(lex.getString()));
418         } else if (token == "\\color") {
419                 lex.next();
420                 setLyXColor(lex.getString(), font.fontInfo());
421         } else if (token == "\\SpecialChar") {
422                 auto_ptr<Inset> inset;
423                 inset.reset(new InsetSpecialChar);
424                 inset->read(lex);
425                 inset->setBuffer(*buf);
426                 par.insertInset(par.size(), inset.release(), font, change);
427         } else if (token == "\\backslash") {
428                 par.appendChar('\\', font, change);
429         } else if (token == "\\LyXTable") {
430                 auto_ptr<Inset> inset(new InsetTabular(buf));
431                 inset->read(lex);
432                 par.insertInset(par.size(), inset.release(), font, change);
433         } else if (token == "\\lyxline") {
434                 auto_ptr<Inset> inset;
435                 inset.reset(new InsetLine);
436                 inset->setBuffer(*buf);
437                 par.insertInset(par.size(), inset.release(), font, change);
438         } else if (token == "\\change_unchanged") {
439                 change = Change(Change::UNCHANGED);
440         } else if (token == "\\change_inserted" || token == "\\change_deleted") {
441                 lex.eatLine();
442                 istringstream is(lex.getString());
443                 unsigned int aid;
444                 time_t ct;
445                 is >> aid >> ct;
446                 map<unsigned int, int> const & am = bp.author_map;
447                 if (am.find(aid) == am.end()) {
448                         errorList.push_back(ErrorItem(_("Change tracking error"),
449                                             bformat(_("Unknown author index for change: %1$d\n"), aid),
450                                             par.id(), 0, par.size()));
451                         change = Change(Change::UNCHANGED);
452                 } else {
453                         if (token == "\\change_inserted")
454                                 change = Change(Change::INSERTED, am.find(aid)->second, ct);
455                         else
456                                 change = Change(Change::DELETED, am.find(aid)->second, ct);
457                 }
458         } else {
459                 lex.eatLine();
460                 errorList.push_back(ErrorItem(_("Unknown token"),
461                         bformat(_("Unknown token: %1$s %2$s\n"), from_utf8(token),
462                         lex.getDocString()),
463                         par.id(), 0, par.size()));
464         }
465 }
466
467
468 void Text::readParagraph(Paragraph & par, Lexer & lex,
469         ErrorList & errorList)
470 {
471         lex.nextToken();
472         string token = lex.getString();
473         Font font;
474         Change change(Change::UNCHANGED);
475
476         while (lex.isOK()) {
477                 readParToken(par, lex, token, font, change, errorList);
478
479                 lex.nextToken();
480                 token = lex.getString();
481
482                 if (token.empty())
483                         continue;
484
485                 if (token == "\\end_layout") {
486                         //Ok, paragraph finished
487                         break;
488                 }
489
490                 LYXERR(Debug::PARSER, "Handling paragraph token: `" << token << '\'');
491                 if (token == "\\begin_layout" || token == "\\end_document"
492                     || token == "\\end_inset" || token == "\\begin_deeper"
493                     || token == "\\end_deeper") {
494                         lex.pushToken(token);
495                         lyxerr << "Paragraph ended in line "
496                                << lex.lineNumber() << "\n"
497                                << "Missing \\end_layout.\n";
498                         break;
499                 }
500         }
501         // Final change goes to paragraph break:
502         par.setChange(par.size(), change);
503
504         // Initialize begin_of_body_ on load; redoParagraph maintains
505         par.setBeginOfBody();
506 }
507
508
509 class TextCompletionList : public CompletionList
510 {
511 public:
512         ///
513         TextCompletionList(Cursor const & cur, WordList const * list)
514                 : buffer_(cur.buffer()), pos_(0), list_(list)
515         {}
516         ///
517         virtual ~TextCompletionList() {}
518         
519         ///
520         virtual bool sorted() const { return true; }
521         ///
522         virtual size_t size() const
523         {
524                 return list_->size();
525         }
526         ///
527         virtual docstring const & data(size_t idx) const
528         {
529                 return list_->word(idx);
530         }
531         
532 private:
533         ///
534         Buffer const * buffer_;
535         ///
536         size_t pos_;
537         ///
538         WordList const * list_;
539 };
540
541
542 bool Text::empty() const
543 {
544         return pars_.empty() || (pars_.size() == 1 && pars_[0].empty()
545                 // FIXME: Should we consider the labeled type as empty too? 
546                 && pars_[0].layout().labeltype == LABEL_NO_LABEL);
547 }
548
549
550 double Text::spacing(Paragraph const & par) const
551 {
552         if (par.params().spacing().isDefault())
553                 return owner_->buffer().params().spacing().getValue();
554         return par.params().spacing().getValue();
555 }
556
557
558 /**
559  * This breaks a paragraph at the specified position.
560  * The new paragraph will:
561  * - Decrease depth by one (or change layout to default layout) when
562  *    keep_layout == false  
563  * - keep current depth and layout when keep_layout == true
564  */
565 static void breakParagraph(Text & text, pit_type par_offset, pos_type pos, 
566                     bool keep_layout)
567 {
568         BufferParams const & bparams = text.inset().buffer().params();
569         ParagraphList & pars = text.paragraphs();
570         // create a new paragraph, and insert into the list
571         ParagraphList::iterator tmp =
572                 pars.insert(boost::next(pars.begin(), par_offset + 1),
573                             Paragraph());
574
575         Paragraph & par = pars[par_offset];
576
577         // remember to set the inset_owner
578         tmp->setInsetOwner(&par.inInset());
579         // without doing that we get a crash when typing <Return> at the
580         // end of a paragraph
581         tmp->setPlainOrDefaultLayout(bparams.documentClass());
582
583         // layout stays the same with latex-environments
584         if (keep_layout) {
585                 tmp->setLayout(par.layout());
586                 tmp->setLabelWidthString(par.params().labelWidthString());
587                 tmp->params().depth(par.params().depth());
588         } else if (par.params().depth() > 0) {
589                 Paragraph const & hook = pars[text.outerHook(par_offset)];
590                 tmp->setLayout(hook.layout());
591                 // not sure the line below is useful
592                 tmp->setLabelWidthString(par.params().labelWidthString());
593                 tmp->params().depth(hook.params().depth());
594         }
595
596         bool const isempty = (par.allowEmpty() && par.empty());
597
598         if (!isempty && (par.size() > pos || par.empty())) {
599                 tmp->setLayout(par.layout());
600                 tmp->params().align(par.params().align());
601                 tmp->setLabelWidthString(par.params().labelWidthString());
602
603                 tmp->params().depth(par.params().depth());
604                 tmp->params().noindent(par.params().noindent());
605
606                 // move everything behind the break position
607                 // to the new paragraph
608
609                 /* Note: if !keepempty, empty() == true, then we reach
610                  * here with size() == 0. So pos_end becomes - 1. This
611                  * doesn't cause problems because both loops below
612                  * enforce pos <= pos_end and 0 <= pos
613                  */
614                 pos_type pos_end = par.size() - 1;
615
616                 for (pos_type i = pos, j = 0; i <= pos_end; ++i) {
617                         if (moveItem(par, pos, *tmp, j, bparams)) {
618                                 ++j;
619                         }
620                 }
621         }
622
623         // Move over the end-of-par change information
624         tmp->setChange(tmp->size(), par.lookupChange(par.size()));
625         par.setChange(par.size(), Change(bparams.trackChanges ?
626                                            Change::INSERTED : Change::UNCHANGED));
627
628         if (pos) {
629                 // Make sure that we keep the language when
630                 // breaking paragraph.
631                 if (tmp->empty()) {
632                         Font changed = tmp->getFirstFontSettings(bparams);
633                         Font const & old = par.getFontSettings(bparams, par.size());
634                         changed.setLanguage(old.language());
635                         tmp->setFont(0, changed);
636                 }
637
638                 return;
639         }
640
641         if (!isempty) {
642                 bool const soa = par.params().startOfAppendix();
643                 par.params().clear();
644                 // do not lose start of appendix marker (bug 4212)
645                 par.params().startOfAppendix(soa);
646                 par.setPlainOrDefaultLayout(bparams.documentClass());
647         }
648
649         // layout stays the same with latex-environments
650         if (keep_layout) {
651                 par.setLayout(tmp->layout());
652                 par.setLabelWidthString(tmp->params().labelWidthString());
653                 par.params().depth(tmp->params().depth());
654         }
655 }
656
657
658 void Text::breakParagraph(Cursor & cur, bool inverse_logic)
659 {
660         LASSERT(this == cur.text(), /**/);
661
662         Paragraph & cpar = cur.paragraph();
663         pit_type cpit = cur.pit();
664
665         DocumentClass const & tclass = cur.buffer()->params().documentClass();
666         Layout const & layout = cpar.layout();
667
668         // this is only allowed, if the current paragraph is not empty
669         // or caption and if it has not the keepempty flag active
670         if (cur.lastpos() == 0 && !cpar.allowEmpty() &&
671             layout.labeltype != LABEL_SENSITIVE)
672                 return;
673
674         // a layout change may affect also the following paragraph
675         recUndo(cur, cur.pit(), undoSpan(cur.pit()) - 1);
676
677         // Always break behind a space
678         // It is better to erase the space (Dekel)
679         if (cur.pos() != cur.lastpos() && cpar.isLineSeparator(cur.pos()))
680                 cpar.eraseChar(cur.pos(), cur.buffer()->params().trackChanges);
681
682         // What should the layout for the new paragraph be?
683         bool keep_layout = inverse_logic ? 
684                 !layout.isEnvironment() 
685                 : layout.isEnvironment();
686
687         // We need to remember this before we break the paragraph, because
688         // that invalidates the layout variable
689         bool sensitive = layout.labeltype == LABEL_SENSITIVE;
690
691         // we need to set this before we insert the paragraph.
692         bool const isempty = cpar.allowEmpty() && cpar.empty();
693
694         lyx::breakParagraph(*this, cpit, cur.pos(), keep_layout);
695
696         // After this, neither paragraph contains any rows!
697
698         cpit = cur.pit();
699         pit_type next_par = cpit + 1;
700
701         // well this is the caption hack since one caption is really enough
702         if (sensitive) {
703                 if (cur.pos() == 0)
704                         // set to standard-layout
705                 //FIXME Check if this should be plainLayout() in some cases
706                         pars_[cpit].applyLayout(tclass.defaultLayout());
707                 else
708                         // set to standard-layout
709                         //FIXME Check if this should be plainLayout() in some cases
710                         pars_[next_par].applyLayout(tclass.defaultLayout());
711         }
712
713         while (!pars_[next_par].empty() && pars_[next_par].isNewline(0)) {
714                 if (!pars_[next_par].eraseChar(0, cur.buffer()->params().trackChanges))
715                         break; // the character couldn't be deleted physically due to change tracking
716         }
717
718         cur.buffer()->updateLabels();
719
720         // A singlePar update is not enough in this case.
721         cur.updateFlags(Update::Force);
722
723         // This check is necessary. Otherwise the new empty paragraph will
724         // be deleted automatically. And it is more friendly for the user!
725         if (cur.pos() != 0 || isempty)
726                 setCursor(cur, cur.pit() + 1, 0);
727         else
728                 setCursor(cur, cur.pit(), 0);
729 }
730
731
732 // needed to insert the selection
733 void Text::insertStringAsLines(DocIterator const & dit, docstring const & str,
734                 Font const & font)
735 {
736         BufferParams const & bparams = owner_->buffer().params();
737         pit_type pit = dit.pit();
738         pos_type pos = dit.pos();
739
740         // insert the string, don't insert doublespace
741         bool space_inserted = true;
742         for (docstring::const_iterator cit = str.begin();
743             cit != str.end(); ++cit) {
744                 Paragraph & par = pars_[pit];
745                 if (*cit == '\n') {
746                         if (autoBreakRows_ && (!par.empty() || par.allowEmpty())) {
747                                 lyx::breakParagraph(*this, pit, pos,
748                                         par.layout().isEnvironment());
749                                 ++pit;
750                                 pos = 0;
751                                 space_inserted = true;
752                         } else {
753                                 continue;
754                         }
755                         // do not insert consecutive spaces if !free_spacing
756                 } else if ((*cit == ' ' || *cit == '\t') &&
757                            space_inserted && !par.isFreeSpacing()) {
758                         continue;
759                 } else if (*cit == '\t') {
760                         if (!par.isFreeSpacing()) {
761                                 // tabs are like spaces here
762                                 par.insertChar(pos, ' ', font, bparams.trackChanges);
763                                 ++pos;
764                                 space_inserted = true;
765                         } else {
766                                 par.insertChar(pos, *cit, font, bparams.trackChanges);
767                                 ++pos;
768                                 space_inserted = true;
769                         }
770                 } else if (!isPrintable(*cit)) {
771                         // Ignore unprintables
772                         continue;
773                 } else {
774                         // just insert the character
775                         par.insertChar(pos, *cit, font, bparams.trackChanges);
776                         ++pos;
777                         space_inserted = (*cit == ' ');
778                 }
779         }
780 }
781
782
783 // turn double CR to single CR, others are converted into one
784 // blank. Then insertStringAsLines is called
785 void Text::insertStringAsParagraphs(DocIterator const & dit, docstring const & str,
786                 Font const & font)
787 {
788         docstring linestr = str;
789         bool newline_inserted = false;
790
791         for (string::size_type i = 0, siz = linestr.size(); i < siz; ++i) {
792                 if (linestr[i] == '\n') {
793                         if (newline_inserted) {
794                                 // we know that \r will be ignored by
795                                 // insertStringAsLines. Of course, it is a dirty
796                                 // trick, but it works...
797                                 linestr[i - 1] = '\r';
798                                 linestr[i] = '\n';
799                         } else {
800                                 linestr[i] = ' ';
801                                 newline_inserted = true;
802                         }
803                 } else if (isPrintable(linestr[i])) {
804                         newline_inserted = false;
805                 }
806         }
807         insertStringAsLines(dit, linestr, font);
808 }
809
810
811 // insert a character, moves all the following breaks in the
812 // same Paragraph one to the right and make a rebreak
813 void Text::insertChar(Cursor & cur, char_type c)
814 {
815         LASSERT(this == cur.text(), /**/);
816
817         cur.recordUndo(INSERT_UNDO);
818
819         TextMetrics const & tm = cur.bv().textMetrics(this);
820         Buffer const & buffer = *cur.buffer();
821         Paragraph & par = cur.paragraph();
822         // try to remove this
823         pit_type const pit = cur.pit();
824
825         bool const freeSpacing = par.layout().free_spacing ||
826                 par.isFreeSpacing();
827
828         if (lyxrc.auto_number) {
829                 static docstring const number_operators = from_ascii("+-/*");
830                 static docstring const number_unary_operators = from_ascii("+-");
831                 static docstring const number_seperators = from_ascii(".,:");
832
833                 if (cur.current_font.fontInfo().number() == FONT_ON) {
834                         if (!isDigit(c) && !contains(number_operators, c) &&
835                             !(contains(number_seperators, c) &&
836                               cur.pos() != 0 &&
837                               cur.pos() != cur.lastpos() &&
838                               tm.displayFont(pit, cur.pos()).fontInfo().number() == FONT_ON &&
839                               tm.displayFont(pit, cur.pos() - 1).fontInfo().number() == FONT_ON)
840                            )
841                                 number(cur); // Set current_font.number to OFF
842                 } else if (isDigit(c) &&
843                            cur.real_current_font.isVisibleRightToLeft()) {
844                         number(cur); // Set current_font.number to ON
845
846                         if (cur.pos() != 0) {
847                                 char_type const c = par.getChar(cur.pos() - 1);
848                                 if (contains(number_unary_operators, c) &&
849                                     (cur.pos() == 1
850                                      || par.isSeparator(cur.pos() - 2)
851                                      || par.isNewline(cur.pos() - 2))
852                                   ) {
853                                         setCharFont(pit, cur.pos() - 1, cur.current_font,
854                                                 tm.font_);
855                                 } else if (contains(number_seperators, c)
856                                      && cur.pos() >= 2
857                                      && tm.displayFont(pit, cur.pos() - 2).fontInfo().number() == FONT_ON) {
858                                         setCharFont(pit, cur.pos() - 1, cur.current_font,
859                                                 tm.font_);
860                                 }
861                         }
862                 }
863         }
864
865         // In Bidi text, we want spaces to be treated in a special way: spaces
866         // which are between words in different languages should get the 
867         // paragraph's language; otherwise, spaces should keep the language 
868         // they were originally typed in. This is only in effect while typing;
869         // after the text is already typed in, the user can always go back and
870         // explicitly set the language of a space as desired. But 99.9% of the
871         // time, what we're doing here is what the user actually meant.
872         // 
873         // The following cases are the ones in which the language of the space
874         // should be changed to match that of the containing paragraph. In the
875         // depictions, lowercase is LTR, uppercase is RTL, underscore (_) 
876         // represents a space, pipe (|) represents the cursor position (so the
877         // character before it is the one just typed in). The different cases
878         // are depicted logically (not visually), from left to right:
879         // 
880         // 1. A_a|
881         // 2. a_A|
882         //
883         // Theoretically, there are other situations that we should, perhaps, deal
884         // with (e.g.: a|_A, A|_a). In practice, though, there really isn't any 
885         // point (to understand why, just try to create this situation...).
886
887         if ((cur.pos() >= 2) && (par.isLineSeparator(cur.pos() - 1))) {
888                 // get font in front and behind the space in question. But do NOT 
889                 // use getFont(cur.pos()) because the character c is not inserted yet
890                 Font const pre_space_font  = tm.displayFont(cur.pit(), cur.pos() - 2);
891                 Font const & post_space_font = cur.real_current_font;
892                 bool pre_space_rtl  = pre_space_font.isVisibleRightToLeft();
893                 bool post_space_rtl = post_space_font.isVisibleRightToLeft();
894                 
895                 if (pre_space_rtl != post_space_rtl) {
896                         // Set the space's language to match the language of the 
897                         // adjacent character whose direction is the paragraph's
898                         // direction; don't touch other properties of the font
899                         Language const * lang = 
900                                 (pre_space_rtl == par.isRTL(buffer.params())) ?
901                                 pre_space_font.language() : post_space_font.language();
902
903                         Font space_font = tm.displayFont(cur.pit(), cur.pos() - 1);
904                         space_font.setLanguage(lang);
905                         par.setFont(cur.pos() - 1, space_font);
906                 }
907         }
908         
909         // Next check, if there will be two blanks together or a blank at
910         // the beginning of a paragraph.
911         // I decided to handle blanks like normal characters, the main
912         // difference are the special checks when calculating the row.fill
913         // (blank does not count at the end of a row) and the check here
914
915         // When the free-spacing option is set for the current layout,
916         // disable the double-space checking
917         if (!freeSpacing && isLineSeparatorChar(c)) {
918                 if (cur.pos() == 0) {
919                         static bool sent_space_message = false;
920                         if (!sent_space_message) {
921                                 cur.message(_("You cannot insert a space at the "
922                                                            "beginning of a paragraph. Please read the Tutorial."));
923                                 sent_space_message = true;
924                         }
925                         return;
926                 }
927                 LASSERT(cur.pos() > 0, /**/);
928                 if ((par.isLineSeparator(cur.pos() - 1) || par.isNewline(cur.pos() - 1))
929                     && !par.isDeleted(cur.pos() - 1)) {
930                         static bool sent_space_message = false;
931                         if (!sent_space_message) {
932                                 cur.message(_("You cannot type two spaces this way. "
933                                                            "Please read the Tutorial."));
934                                 sent_space_message = true;
935                         }
936                         return;
937                 }
938         }
939
940         par.insertChar(cur.pos(), c, cur.current_font,
941                 cur.buffer()->params().trackChanges);
942         cur.checkBufferStructure();
943
944 //              cur.updateFlags(Update::Force);
945         bool boundary = cur.boundary()
946                 || tm.isRTLBoundary(cur.pit(), cur.pos() + 1);
947         setCursor(cur, cur.pit(), cur.pos() + 1, false, boundary);
948         charInserted(cur);
949 }
950
951
952 void Text::charInserted(Cursor & cur)
953 {
954         Paragraph & par = cur.paragraph();
955
956         // Here we call finishUndo for every 20 characters inserted.
957         // This is from my experience how emacs does it. (Lgb)
958         if (undo_counter_ < 20) {
959                 ++undo_counter_;
960         } else {
961                 cur.finishUndo();
962                 undo_counter_ = 0;
963         }
964
965         // register word if a non-letter was entered
966         if (cur.pos() > 1
967             && !par.isWordSeparator(cur.pos() - 2)
968             && par.isWordSeparator(cur.pos() - 1)) {
969                 // get the word in front of cursor
970                 LASSERT(this == cur.text(), /**/);
971                 cur.paragraph().updateWords();
972         }
973 }
974
975
976 // the cursor set functions have a special mechanism. When they
977 // realize, that you left an empty paragraph, they will delete it.
978
979 bool Text::cursorForwardOneWord(Cursor & cur)
980 {
981         LASSERT(this == cur.text(), /**/);
982
983         pos_type const lastpos = cur.lastpos();
984         pit_type pit = cur.pit();
985         pos_type pos = cur.pos();
986         Paragraph const & par = cur.paragraph();
987
988         // Paragraph boundary is a word boundary
989         if (pos == lastpos) {
990                 if (pit != cur.lastpit())
991                         return setCursor(cur, pit + 1, 0);
992                 else
993                         return false;
994         }
995
996         if (lyxrc.mac_like_word_movement) {
997                 // Skip through trailing punctuation and spaces.
998                 while (pos != lastpos && (par.isChar(pos) || par.isSpace(pos)))
999                         ++pos;
1000
1001                 // Skip over either a non-char inset or a full word
1002                 if (pos != lastpos && par.isWordSeparator(pos))
1003                         ++pos;
1004                 else while (pos != lastpos && !par.isWordSeparator(pos))
1005                              ++pos;
1006         } else {
1007                 LASSERT(pos < lastpos, /**/); // see above
1008                 if (!par.isWordSeparator(pos))
1009                         while (pos != lastpos && !par.isWordSeparator(pos))
1010                                 ++pos;
1011                 else if (par.isChar(pos))
1012                         while (pos != lastpos && par.isChar(pos))
1013                                 ++pos;
1014                 else if (!par.isSpace(pos)) // non-char inset
1015                         ++pos;
1016
1017                 // Skip over white space
1018                 while (pos != lastpos && par.isSpace(pos))
1019                              ++pos;             
1020         }
1021
1022         return setCursor(cur, pit, pos);
1023 }
1024
1025
1026 bool Text::cursorBackwardOneWord(Cursor & cur)
1027 {
1028         LASSERT(this == cur.text(), /**/);
1029
1030         pit_type pit = cur.pit();
1031         pos_type pos = cur.pos();
1032         Paragraph & par = cur.paragraph();
1033
1034         // Paragraph boundary is a word boundary
1035         if (pos == 0 && pit != 0)
1036                 return setCursor(cur, pit - 1, getPar(pit - 1).size());
1037
1038         if (lyxrc.mac_like_word_movement) {
1039                 // Skip through punctuation and spaces.
1040                 while (pos != 0 && (par.isChar(pos - 1) || par.isSpace(pos - 1)))
1041                         --pos;
1042
1043                 // Skip over either a non-char inset or a full word
1044                 if (pos != 0 && par.isWordSeparator(pos - 1) && !par.isChar(pos - 1))
1045                         --pos;
1046                 else while (pos != 0 && !par.isWordSeparator(pos - 1))
1047                              --pos;
1048         } else {
1049                 // Skip over white space
1050                 while (pos != 0 && par.isSpace(pos - 1))
1051                              --pos;
1052
1053                 if (pos != 0 && !par.isWordSeparator(pos - 1))
1054                         while (pos != 0 && !par.isWordSeparator(pos - 1))
1055                                 --pos;
1056                 else if (pos != 0 && par.isChar(pos - 1))
1057                         while (pos != 0 && par.isChar(pos - 1))
1058                                 --pos;
1059                 else if (pos != 0 && !par.isSpace(pos - 1)) // non-char inset
1060                         --pos;
1061         }
1062
1063         return setCursor(cur, pit, pos);
1064 }
1065
1066
1067 bool Text::cursorVisLeftOneWord(Cursor & cur)
1068 {
1069         LASSERT(this == cur.text(), /**/);
1070
1071         pos_type left_pos, right_pos;
1072         bool left_is_letter, right_is_letter;
1073
1074         Cursor temp_cur = cur;
1075
1076         // always try to move at least once...
1077         while (temp_cur.posVisLeft(true /* skip_inset */)) {
1078
1079                 // collect some information about current cursor position
1080                 temp_cur.getSurroundingPos(left_pos, right_pos);
1081                 left_is_letter = 
1082                         (left_pos > -1 ? !temp_cur.paragraph().isWordSeparator(left_pos) : false);
1083                 right_is_letter = 
1084                         (right_pos > -1 ? !temp_cur.paragraph().isWordSeparator(right_pos) : false);
1085
1086                 // if we're not at a letter/non-letter boundary, continue moving
1087                 if (left_is_letter == right_is_letter)
1088                         continue;
1089
1090                 // we should stop when we have an LTR word on our right or an RTL word
1091                 // on our left
1092                 if ((left_is_letter && temp_cur.paragraph().getFontSettings(
1093                                 temp_cur.buffer()->params(), left_pos).isRightToLeft())
1094                         || (right_is_letter && !temp_cur.paragraph().getFontSettings(
1095                                 temp_cur.buffer()->params(), right_pos).isRightToLeft()))
1096                         break;
1097         }
1098
1099         return setCursor(cur, temp_cur.pit(), temp_cur.pos(), 
1100                                          true, temp_cur.boundary());
1101 }
1102
1103
1104 bool Text::cursorVisRightOneWord(Cursor & cur)
1105 {
1106         LASSERT(this == cur.text(), /**/);
1107
1108         pos_type left_pos, right_pos;
1109         bool left_is_letter, right_is_letter;
1110
1111         Cursor temp_cur = cur;
1112
1113         // always try to move at least once...
1114         while (temp_cur.posVisRight(true /* skip_inset */)) {
1115
1116                 // collect some information about current cursor position
1117                 temp_cur.getSurroundingPos(left_pos, right_pos);
1118                 left_is_letter = 
1119                         (left_pos > -1 ? !temp_cur.paragraph().isWordSeparator(left_pos) : false);
1120                 right_is_letter = 
1121                         (right_pos > -1 ? !temp_cur.paragraph().isWordSeparator(right_pos) : false);
1122
1123                 // if we're not at a letter/non-letter boundary, continue moving
1124                 if (left_is_letter == right_is_letter)
1125                         continue;
1126
1127                 // we should stop when we have an LTR word on our right or an RTL word
1128                 // on our left
1129                 if ((left_is_letter && temp_cur.paragraph().getFontSettings(
1130                                 temp_cur.buffer()->params(), 
1131                                 left_pos).isRightToLeft())
1132                         || (right_is_letter && !temp_cur.paragraph().getFontSettings(
1133                                 temp_cur.buffer()->params(), 
1134                                 right_pos).isRightToLeft()))
1135                         break;
1136         }
1137
1138         return setCursor(cur, temp_cur.pit(), temp_cur.pos(), 
1139                                          true, temp_cur.boundary());
1140 }
1141
1142
1143 void Text::selectWord(Cursor & cur, word_location loc)
1144 {
1145         LASSERT(this == cur.text(), /**/);
1146         CursorSlice from = cur.top();
1147         CursorSlice to = cur.top();
1148         getWord(from, to, loc);
1149         if (cur.top() != from)
1150                 setCursor(cur, from.pit(), from.pos());
1151         if (to == from)
1152                 return;
1153         if (!cur.selection())
1154                 cur.resetAnchor();
1155         setCursor(cur, to.pit(), to.pos());
1156         cur.setSelection();
1157         cur.setWordSelection(true);
1158 }
1159
1160
1161 void Text::selectAll(Cursor & cur)
1162 {
1163         LASSERT(this == cur.text(), /**/);
1164         if (cur.lastpos() == 0 && cur.lastpit() == 0)
1165                 return;
1166         // If the cursor is at the beginning, make sure the cursor ends there
1167         if (cur.pit() == 0 && cur.pos() == 0) {
1168                 setCursor(cur, cur.lastpit(), getPar(cur.lastpit()).size());
1169                 cur.resetAnchor();
1170                 setCursor(cur, 0, 0);           
1171         } else {
1172                 setCursor(cur, 0, 0);
1173                 cur.resetAnchor();
1174                 setCursor(cur, cur.lastpit(), getPar(cur.lastpit()).size());
1175         }
1176         cur.setSelection();
1177 }
1178
1179
1180 // Select the word currently under the cursor when no
1181 // selection is currently set
1182 bool Text::selectWordWhenUnderCursor(Cursor & cur, word_location loc)
1183 {
1184         LASSERT(this == cur.text(), /**/);
1185         if (cur.selection())
1186                 return false;
1187         selectWord(cur, loc);
1188         return cur.selection();
1189 }
1190
1191
1192 void Text::acceptOrRejectChanges(Cursor & cur, ChangeOp op)
1193 {
1194         LASSERT(this == cur.text(), /**/);
1195
1196         if (!cur.selection()) {
1197                 bool const changed = cur.paragraph().isChanged(cur.pos());
1198                 if (!(changed && findNextChange(&cur.bv())))
1199                         return;
1200         }
1201
1202         cur.recordUndoSelection();
1203
1204         pit_type begPit = cur.selectionBegin().pit();
1205         pit_type endPit = cur.selectionEnd().pit();
1206
1207         pos_type begPos = cur.selectionBegin().pos();
1208         pos_type endPos = cur.selectionEnd().pos();
1209
1210         // keep selection info, because endPos becomes invalid after the first loop
1211         bool endsBeforeEndOfPar = (endPos < pars_[endPit].size());
1212
1213         // first, accept/reject changes within each individual paragraph (do not consider end-of-par)
1214
1215         for (pit_type pit = begPit; pit <= endPit; ++pit) {
1216                 pos_type parSize = pars_[pit].size();
1217
1218                 // ignore empty paragraphs; otherwise, an assertion will fail for
1219                 // acceptChanges(bparams, 0, 0) or rejectChanges(bparams, 0, 0)
1220                 if (parSize == 0)
1221                         continue;
1222
1223                 // do not consider first paragraph if the cursor starts at pos size()
1224                 if (pit == begPit && begPos == parSize)
1225                         continue;
1226
1227                 // do not consider last paragraph if the cursor ends at pos 0
1228                 if (pit == endPit && endPos == 0)
1229                         break; // last iteration anyway
1230
1231                 pos_type left  = (pit == begPit ? begPos : 0);
1232                 pos_type right = (pit == endPit ? endPos : parSize);
1233
1234                 if (op == ACCEPT) {
1235                         pars_[pit].acceptChanges(left, right);
1236                 } else {
1237                         pars_[pit].rejectChanges(left, right);
1238                 }
1239         }
1240
1241         // next, accept/reject imaginary end-of-par characters
1242
1243         for (pit_type pit = begPit; pit <= endPit; ++pit) {
1244                 pos_type pos = pars_[pit].size();
1245
1246                 // skip if the selection ends before the end-of-par
1247                 if (pit == endPit && endsBeforeEndOfPar)
1248                         break; // last iteration anyway
1249
1250                 // skip if this is not the last paragraph of the document
1251                 // note: the user should be able to accept/reject the par break of the last par!
1252                 if (pit == endPit && pit + 1 != int(pars_.size()))
1253                         break; // last iteration anway
1254
1255                 if (op == ACCEPT) {
1256                         if (pars_[pit].isInserted(pos)) {
1257                                 pars_[pit].setChange(pos, Change(Change::UNCHANGED));
1258                         } else if (pars_[pit].isDeleted(pos)) {
1259                                 if (pit + 1 == int(pars_.size())) {
1260                                         // we cannot remove a par break at the end of the last paragraph;
1261                                         // instead, we mark it unchanged
1262                                         pars_[pit].setChange(pos, Change(Change::UNCHANGED));
1263                                 } else {
1264                                         mergeParagraph(cur.buffer()->params(), pars_, pit);
1265                                         --endPit;
1266                                         --pit;
1267                                 }
1268                         }
1269                 } else {
1270                         if (pars_[pit].isDeleted(pos)) {
1271                                 pars_[pit].setChange(pos, Change(Change::UNCHANGED));
1272                         } else if (pars_[pit].isInserted(pos)) {
1273                                 if (pit + 1 == int(pars_.size())) {
1274                                         // we mark the par break at the end of the last paragraph unchanged
1275                                         pars_[pit].setChange(pos, Change(Change::UNCHANGED));
1276                                 } else {
1277                                         mergeParagraph(cur.buffer()->params(), pars_, pit);
1278                                         --endPit;
1279                                         --pit;
1280                                 }
1281                         }
1282                 }
1283         }
1284
1285         // finally, invoke the DEPM
1286
1287         deleteEmptyParagraphMechanism(begPit, endPit, cur.buffer()->params().trackChanges);
1288
1289         //
1290
1291         cur.finishUndo();
1292         cur.clearSelection();
1293         setCursorIntern(cur, begPit, begPos);
1294         cur.updateFlags(Update::Force);
1295         cur.buffer()->updateLabels();
1296 }
1297
1298
1299 void Text::acceptChanges()
1300 {
1301         BufferParams const & bparams = owner_->buffer().params();
1302         lyx::acceptChanges(pars_, bparams);
1303         deleteEmptyParagraphMechanism(0, pars_.size() - 1, bparams.trackChanges);
1304 }
1305
1306
1307 void Text::rejectChanges()
1308 {
1309         BufferParams const & bparams = owner_->buffer().params();
1310         pit_type pars_size = static_cast<pit_type>(pars_.size());
1311
1312         // first, reject changes within each individual paragraph
1313         // (do not consider end-of-par)
1314         for (pit_type pit = 0; pit < pars_size; ++pit) {
1315                 if (!pars_[pit].empty())   // prevent assertion failure
1316                         pars_[pit].rejectChanges(0, pars_[pit].size());
1317         }
1318
1319         // next, reject imaginary end-of-par characters
1320         for (pit_type pit = 0; pit < pars_size; ++pit) {
1321                 pos_type pos = pars_[pit].size();
1322
1323                 if (pars_[pit].isDeleted(pos)) {
1324                         pars_[pit].setChange(pos, Change(Change::UNCHANGED));
1325                 } else if (pars_[pit].isInserted(pos)) {
1326                         if (pit == pars_size - 1) {
1327                                 // we mark the par break at the end of the last
1328                                 // paragraph unchanged
1329                                 pars_[pit].setChange(pos, Change(Change::UNCHANGED));
1330                         } else {
1331                                 mergeParagraph(bparams, pars_, pit);
1332                                 --pit;
1333                                 --pars_size;
1334                         }
1335                 }
1336         }
1337
1338         // finally, invoke the DEPM
1339         deleteEmptyParagraphMechanism(0, pars_size - 1, bparams.trackChanges);
1340 }
1341
1342
1343 void Text::deleteWordForward(Cursor & cur)
1344 {
1345         LASSERT(this == cur.text(), /**/);
1346         if (cur.lastpos() == 0)
1347                 cursorForward(cur);
1348         else {
1349                 cur.resetAnchor();
1350                 cur.setSelection(true);
1351                 cursorForwardOneWord(cur);
1352                 cur.setSelection();
1353                 cutSelection(cur, true, false);
1354                 cur.checkBufferStructure();
1355         }
1356 }
1357
1358
1359 void Text::deleteWordBackward(Cursor & cur)
1360 {
1361         LASSERT(this == cur.text(), /**/);
1362         if (cur.lastpos() == 0)
1363                 cursorBackward(cur);
1364         else {
1365                 cur.resetAnchor();
1366                 cur.setSelection(true);
1367                 cursorBackwardOneWord(cur);
1368                 cur.setSelection();
1369                 cutSelection(cur, true, false);
1370                 cur.checkBufferStructure();
1371         }
1372 }
1373
1374
1375 // Kill to end of line.
1376 void Text::changeCase(Cursor & cur, TextCase action)
1377 {
1378         LASSERT(this == cur.text(), /**/);
1379         CursorSlice from;
1380         CursorSlice to;
1381
1382         bool gotsel = false;
1383         if (cur.selection()) {
1384                 from = cur.selBegin();
1385                 to = cur.selEnd();
1386                 gotsel = true;
1387         } else {
1388                 from = cur.top();
1389                 getWord(from, to, PARTIAL_WORD);
1390                 cursorForwardOneWord(cur);
1391         }
1392
1393         cur.recordUndoSelection();
1394
1395         pit_type begPit = from.pit();
1396         pit_type endPit = to.pit();
1397
1398         pos_type begPos = from.pos();
1399         pos_type endPos = to.pos();
1400
1401         pos_type right = 0; // needed after the for loop
1402
1403         for (pit_type pit = begPit; pit <= endPit; ++pit) {
1404                 Paragraph & par = pars_[pit];
1405                 pos_type const pos = (pit == begPit ? begPos : 0);
1406                 right = (pit == endPit ? endPos : par.size());
1407                 par.changeCase(cur.buffer()->params(), pos, right, action);
1408         }
1409
1410         // the selection may have changed due to logically-only deleted chars
1411         if (gotsel) {
1412                 setCursor(cur, begPit, begPos);
1413                 cur.resetAnchor();
1414                 setCursor(cur, endPit, right);
1415                 cur.setSelection();
1416         } else
1417                 setCursor(cur, endPit, right);
1418
1419         cur.checkBufferStructure();
1420 }
1421
1422
1423 bool Text::handleBibitems(Cursor & cur)
1424 {
1425         if (cur.paragraph().layout().labeltype != LABEL_BIBLIO)
1426                 return false;
1427
1428         if (cur.pos() != 0)
1429                 return false;
1430
1431         BufferParams const & bufparams = cur.buffer()->params();
1432         Paragraph const & par = cur.paragraph();
1433         Cursor prevcur = cur;
1434         if (cur.pit() > 0) {
1435                 --prevcur.pit();
1436                 prevcur.pos() = prevcur.lastpos();
1437         }
1438         Paragraph const & prevpar = prevcur.paragraph();
1439
1440         // if a bibitem is deleted, merge with previous paragraph
1441         // if this is a bibliography item as well
1442         if (cur.pit() > 0 && par.layout() == prevpar.layout()) {
1443                 cur.recordUndo(ATOMIC_UNDO, prevcur.pit());
1444                 mergeParagraph(bufparams, cur.text()->paragraphs(),
1445                                                         prevcur.pit());
1446                 cur.buffer()->updateLabels();
1447                 setCursorIntern(cur, prevcur.pit(), prevcur.pos());
1448                 cur.updateFlags(Update::Force);
1449                 return true;
1450         } 
1451
1452         // otherwise reset to default
1453         cur.paragraph().setPlainOrDefaultLayout(bufparams.documentClass());
1454         return true;
1455 }
1456
1457
1458 bool Text::erase(Cursor & cur)
1459 {
1460         LASSERT(this == cur.text(), return false);
1461         bool needsUpdate = false;
1462         Paragraph & par = cur.paragraph();
1463
1464         if (cur.pos() != cur.lastpos()) {
1465                 // this is the code for a normal delete, not pasting
1466                 // any paragraphs
1467                 cur.recordUndo(DELETE_UNDO);
1468                 bool const was_inset = cur.paragraph().isInset(cur.pos());
1469                 if(!par.eraseChar(cur.pos(), cur.buffer()->params().trackChanges))
1470                         // the character has been logically deleted only => skip it
1471                         cur.top().forwardPos();
1472
1473                 if (was_inset)
1474                         cur.buffer()->updateLabels();
1475                 else
1476                         cur.checkBufferStructure();
1477                 needsUpdate = true;
1478         } else {
1479                 if (cur.pit() == cur.lastpit())
1480                         return dissolveInset(cur);
1481
1482                 if (!par.isMergedOnEndOfParDeletion(cur.buffer()->params().trackChanges)) {
1483                         par.setChange(cur.pos(), Change(Change::DELETED));
1484                         cur.forwardPos();
1485                         needsUpdate = true;
1486                 } else {
1487                         setCursorIntern(cur, cur.pit() + 1, 0);
1488                         needsUpdate = backspacePos0(cur);
1489                 }
1490         }
1491
1492         needsUpdate |= handleBibitems(cur);
1493
1494         if (needsUpdate) {
1495                 // Make sure the cursor is correct. Is this really needed?
1496                 // No, not really... at least not here!
1497                 cur.text()->setCursor(cur.top(), cur.pit(), cur.pos());
1498                 cur.checkBufferStructure();
1499         }
1500
1501         return needsUpdate;
1502 }
1503
1504
1505 bool Text::backspacePos0(Cursor & cur)
1506 {
1507         LASSERT(this == cur.text(), /**/);
1508         if (cur.pit() == 0)
1509                 return false;
1510
1511         bool needsUpdate = false;
1512
1513         BufferParams const & bufparams = cur.buffer()->params();
1514         DocumentClass const & tclass = bufparams.documentClass();
1515         ParagraphList & plist = cur.text()->paragraphs();
1516         Paragraph const & par = cur.paragraph();
1517         Cursor prevcur = cur;
1518         --prevcur.pit();
1519         prevcur.pos() = prevcur.lastpos();
1520         Paragraph const & prevpar = prevcur.paragraph();
1521
1522         // is it an empty paragraph?
1523         if (cur.lastpos() == 0
1524             || (cur.lastpos() == 1 && par.isSeparator(0))) {
1525                 cur.recordUndo(ATOMIC_UNDO, prevcur.pit(), cur.pit());
1526                 plist.erase(boost::next(plist.begin(), cur.pit()));
1527                 needsUpdate = true;
1528         }
1529         // is previous par empty?
1530         else if (prevcur.lastpos() == 0
1531                  || (prevcur.lastpos() == 1 && prevpar.isSeparator(0))) {
1532                 cur.recordUndo(ATOMIC_UNDO, prevcur.pit(), cur.pit());
1533                 plist.erase(boost::next(plist.begin(), prevcur.pit()));
1534                 needsUpdate = true;
1535         }
1536         // Pasting is not allowed, if the paragraphs have different
1537         // layouts. I think it is a real bug of all other
1538         // word processors to allow it. It confuses the user.
1539         // Correction: Pasting is always allowed with standard-layout
1540         // or the empty layout.
1541         else if (par.layout() == prevpar.layout()
1542                  || tclass.isDefaultLayout(par.layout())
1543                  || tclass.isPlainLayout(par.layout())) {
1544                 cur.recordUndo(ATOMIC_UNDO, prevcur.pit());
1545                 mergeParagraph(bufparams, plist, prevcur.pit());
1546                 needsUpdate = true;
1547         }
1548
1549         if (needsUpdate) {
1550                 cur.buffer()->updateLabels();
1551                 setCursorIntern(cur, prevcur.pit(), prevcur.pos());
1552         }
1553
1554         return needsUpdate;
1555 }
1556
1557
1558 bool Text::backspace(Cursor & cur)
1559 {
1560         LASSERT(this == cur.text(), /**/);
1561         bool needsUpdate = false;
1562         if (cur.pos() == 0) {
1563                 if (cur.pit() == 0)
1564                         return dissolveInset(cur);
1565
1566                 Paragraph & prev_par = pars_[cur.pit() - 1];
1567
1568                 if (!prev_par.isMergedOnEndOfParDeletion(cur.buffer()->params().trackChanges)) {
1569                         prev_par.setChange(prev_par.size(), Change(Change::DELETED));
1570                         setCursorIntern(cur, cur.pit() - 1, prev_par.size());
1571                         return true;
1572                 }
1573                 // The cursor is at the beginning of a paragraph, so
1574                 // the backspace will collapse two paragraphs into one.
1575                 needsUpdate = backspacePos0(cur);
1576
1577         } else {
1578                 // this is the code for a normal backspace, not pasting
1579                 // any paragraphs
1580                 cur.recordUndo(DELETE_UNDO);
1581                 // We used to do cursorBackwardIntern() here, but it is
1582                 // not a good idea since it triggers the auto-delete
1583                 // mechanism. So we do a cursorBackwardIntern()-lite,
1584                 // without the dreaded mechanism. (JMarc)
1585                 setCursorIntern(cur, cur.pit(), cur.pos() - 1,
1586                                 false, cur.boundary());
1587                 bool const was_inset = cur.paragraph().isInset(cur.pos());
1588                 cur.paragraph().eraseChar(cur.pos(), cur.buffer()->params().trackChanges);
1589                 if (was_inset)
1590                         cur.buffer()->updateLabels();
1591                 else
1592                         cur.checkBufferStructure();
1593         }
1594
1595         if (cur.pos() == cur.lastpos())
1596                 cur.setCurrentFont();
1597
1598         needsUpdate |= handleBibitems(cur);
1599
1600         // A singlePar update is not enough in this case.
1601 //              cur.updateFlags(Update::Force);
1602         setCursor(cur.top(), cur.pit(), cur.pos());
1603
1604         return needsUpdate;
1605 }
1606
1607
1608 bool Text::dissolveInset(Cursor & cur)
1609 {
1610         LASSERT(this == cur.text(), return false);
1611
1612         if (isMainText() || cur.inset().nargs() != 1)
1613                 return false;
1614
1615         cur.recordUndoInset();
1616         cur.setMark(false);
1617         cur.selHandle(false);
1618         // save position
1619         pos_type spos = cur.pos();
1620         pit_type spit = cur.pit();
1621         ParagraphList plist;
1622         if (cur.lastpit() != 0 || cur.lastpos() != 0)
1623                 plist = paragraphs();
1624         cur.popBackward();
1625         // store cursor offset
1626         if (spit == 0)
1627                 spos += cur.pos();
1628         spit += cur.pit();
1629         Buffer & b = *cur.buffer();
1630         cur.paragraph().eraseChar(cur.pos(), b.params().trackChanges);
1631         if (!plist.empty()) {
1632                 // ERT paragraphs have the Language latex_language.
1633                 // This is invalid outside of ERT, so we need to
1634                 // change it to the buffer language.
1635                 ParagraphList::iterator it = plist.begin();
1636                 ParagraphList::iterator it_end = plist.end();
1637                 for (; it != it_end; it++)
1638                         it->changeLanguage(b.params(), latex_language, b.language());
1639
1640                 pasteParagraphList(cur, plist, b.params().documentClassPtr(),
1641                                    b.errorList("Paste"));
1642                 // restore position
1643                 cur.pit() = min(cur.lastpit(), spit);
1644                 cur.pos() = min(cur.lastpos(), spos);
1645         } else
1646                 // this is the least that needs to be done (bug 6003)
1647                 // in the above case, pasteParagraphList handles this
1648                 cur.buffer()->updateLabels();
1649
1650         // Ensure the current language is set correctly (bug 6292)
1651         cur.text()->setCursor(cur, cur.pit(), cur.pos());
1652         cur.clearSelection();
1653         cur.resetAnchor();
1654         return true;
1655 }
1656
1657
1658 void Text::getWord(CursorSlice & from, CursorSlice & to,
1659         word_location const loc) const
1660 {
1661         to = from;
1662         pars_[to.pit()].locateWord(from.pos(), to.pos(), loc);
1663 }
1664
1665
1666 void Text::write(ostream & os) const
1667 {
1668         Buffer const & buf = owner_->buffer();
1669         ParagraphList::const_iterator pit = paragraphs().begin();
1670         ParagraphList::const_iterator end = paragraphs().end();
1671         depth_type dth = 0;
1672         for (; pit != end; ++pit)
1673                 pit->write(os, buf.params(), dth);
1674
1675         // Close begin_deeper
1676         for(; dth > 0; --dth)
1677                 os << "\n\\end_deeper";
1678 }
1679
1680
1681 bool Text::read(Lexer & lex, 
1682                 ErrorList & errorList, InsetText * insetPtr)
1683 {
1684         Buffer const & buf = owner_->buffer();
1685         depth_type depth = 0;
1686         bool res = true;
1687
1688         while (lex.isOK()) {
1689                 lex.nextToken();
1690                 string const token = lex.getString();
1691
1692                 if (token.empty())
1693                         continue;
1694
1695                 if (token == "\\end_inset")
1696                         break;
1697
1698                 if (token == "\\end_body")
1699                         continue;
1700
1701                 if (token == "\\begin_body")
1702                         continue;
1703
1704                 if (token == "\\end_document") {
1705                         res = false;
1706                         break;
1707                 }
1708
1709                 if (token == "\\begin_layout") {
1710                         lex.pushToken(token);
1711
1712                         Paragraph par;
1713                         par.setInsetOwner(insetPtr);
1714                         par.params().depth(depth);
1715                         par.setFont(0, Font(inherit_font, buf.params().language));
1716                         pars_.push_back(par);
1717                         readParagraph(pars_.back(), lex, errorList);
1718
1719                         // register the words in the global word list
1720                         pars_.back().updateWords();
1721                 } else if (token == "\\begin_deeper") {
1722                         ++depth;
1723                 } else if (token == "\\end_deeper") {
1724                         if (!depth)
1725                                 lex.printError("\\end_deeper: " "depth is already null");
1726                         else
1727                                 --depth;
1728                 } else {
1729                         LYXERR0("Handling unknown body token: `" << token << '\'');
1730                 }
1731         }
1732
1733         // avoid a crash on weird documents (bug 4859)
1734         if (pars_.empty()) {
1735                 Paragraph par;
1736                 par.setInsetOwner(insetPtr);
1737                 par.params().depth(depth);
1738                 par.setFont(0, Font(inherit_font, 
1739                                     buf.params().language));
1740                 par.setPlainOrDefaultLayout(buf.params().documentClass());
1741                 pars_.push_back(par);
1742         }
1743         
1744         return res;
1745 }
1746
1747 // Returns the current font and depth as a message.
1748 docstring Text::currentState(Cursor const & cur) const
1749 {
1750         LASSERT(this == cur.text(), /**/);
1751         Buffer & buf = *cur.buffer();
1752         Paragraph const & par = cur.paragraph();
1753         odocstringstream os;
1754
1755         if (buf.params().trackChanges)
1756                 os << _("[Change Tracking] ");
1757
1758         Change change = par.lookupChange(cur.pos());
1759
1760         if (change.changed()) {
1761                 Author const & a = buf.params().authors().get(change.author);
1762                 os << _("Change: ") << a.name();
1763                 if (!a.email().empty())
1764                         os << " (" << a.email() << ")";
1765                 // FIXME ctime is english, we should translate that
1766                 os << _(" at ") << ctime(&change.changetime);
1767                 os << " : ";
1768         }
1769
1770         // I think we should only show changes from the default
1771         // font. (Asger)
1772         // No, from the document font (MV)
1773         Font font = cur.real_current_font;
1774         font.fontInfo().reduce(buf.params().getFont().fontInfo());
1775
1776         os << bformat(_("Font: %1$s"), font.stateText(&buf.params()));
1777
1778         // The paragraph depth
1779         int depth = cur.paragraph().getDepth();
1780         if (depth > 0)
1781                 os << bformat(_(", Depth: %1$d"), depth);
1782
1783         // The paragraph spacing, but only if different from
1784         // buffer spacing.
1785         Spacing const & spacing = par.params().spacing();
1786         if (!spacing.isDefault()) {
1787                 os << _(", Spacing: ");
1788                 switch (spacing.getSpace()) {
1789                 case Spacing::Single:
1790                         os << _("Single");
1791                         break;
1792                 case Spacing::Onehalf:
1793                         os << _("OneHalf");
1794                         break;
1795                 case Spacing::Double:
1796                         os << _("Double");
1797                         break;
1798                 case Spacing::Other:
1799                         os << _("Other (") << from_ascii(spacing.getValueAsString()) << ')';
1800                         break;
1801                 case Spacing::Default:
1802                         // should never happen, do nothing
1803                         break;
1804                 }
1805         }
1806
1807 #ifdef DEVEL_VERSION
1808         os << _(", Inset: ") << &cur.inset();
1809         os << _(", Paragraph: ") << cur.pit();
1810         os << _(", Id: ") << par.id();
1811         os << _(", Position: ") << cur.pos();
1812         // FIXME: Why is the check for par.size() needed?
1813         // We are called with cur.pos() == par.size() quite often.
1814         if (!par.empty() && cur.pos() < par.size()) {
1815                 // Force output of code point, not character
1816                 size_t const c = par.getChar(cur.pos());
1817                 os << _(", Char: 0x") << hex << c;
1818         }
1819         os << _(", Boundary: ") << cur.boundary();
1820 //      Row & row = cur.textRow();
1821 //      os << bformat(_(", Row b:%1$d e:%2$d"), row.pos(), row.endpos());
1822 #endif
1823         return os.str();
1824 }
1825
1826
1827 docstring Text::getPossibleLabel(Cursor const & cur) const
1828 {
1829         pit_type pit = cur.pit();
1830
1831         Layout const * layout = &(pars_[pit].layout());
1832
1833         docstring text;
1834         docstring par_text = pars_[pit].asString();
1835         string piece;
1836         // the return string of math matrices might contain linebreaks
1837         par_text = subst(par_text, '\n', '-');
1838         for (int i = 0; i < lyxrc.label_init_length; ++i) {
1839                 if (par_text.empty())
1840                         break;
1841                 docstring head;
1842                 par_text = split(par_text, head, ' ');
1843                 // Is it legal to use spaces in labels ?
1844                 if (i > 0)
1845                         text += '-';
1846                 text += head;
1847         }
1848
1849         // No need for a prefix if the user said so.
1850         if (lyxrc.label_init_length <= 0)
1851                 return text;
1852
1853         // Will contain the label type.
1854         docstring name;
1855
1856         // For section, subsection, etc...
1857         if (layout->latextype == LATEX_PARAGRAPH && pit != 0) {
1858                 Layout const * layout2 = &(pars_[pit - 1].layout());
1859                 if (layout2->latextype != LATEX_PARAGRAPH) {
1860                         --pit;
1861                         layout = layout2;
1862                 }
1863         }
1864         if (layout->latextype != LATEX_PARAGRAPH)
1865                 name = from_ascii(layout->latexname());
1866
1867         // for captions, we just take the caption type
1868         Inset * caption_inset = cur.innerInsetOfType(CAPTION_CODE);
1869         if (caption_inset)
1870                 name = from_ascii(static_cast<InsetCaption *>(caption_inset)->type());
1871
1872         // If none of the above worked, we'll see if we're inside various
1873         // types of insets and take our abbreviation from them.
1874         if (name.empty()) {
1875                 InsetCode const codes[] = {
1876                         FLOAT_CODE,
1877                         WRAP_CODE,
1878                         FOOT_CODE
1879                 };
1880                 for (unsigned int i = 0; i < (sizeof codes / sizeof codes[0]); ++i) {
1881                         Inset * float_inset = cur.innerInsetOfType(codes[i]);
1882                         if (float_inset) {
1883                                 name = float_inset->name();
1884                                 break;
1885                         }
1886                 }
1887         }
1888
1889         // Create a correct prefix for prettyref
1890         if (name == "theorem")
1891                 name = from_ascii("thm");
1892         else if (name == "Foot")
1893                 name = from_ascii("fn");
1894         else if (name == "listing")
1895                 name = from_ascii("lst");
1896
1897         if (!name.empty())
1898                 text = name.substr(0, 3) + ':' + text;
1899
1900         return text;
1901 }
1902
1903
1904 docstring Text::asString(int options) const
1905 {
1906         return asString(0, pars_.size(), options);
1907 }
1908
1909
1910 docstring Text::asString(pit_type beg, pit_type end, int options) const
1911 {
1912         size_t i = size_t(beg);
1913         docstring str = pars_[i].asString(options);
1914         for (++i; i != size_t(end); ++i) {
1915                 str += '\n';
1916                 str += pars_[i].asString(options);
1917         }
1918         return str;
1919 }
1920
1921
1922
1923 void Text::charsTranspose(Cursor & cur)
1924 {
1925         LASSERT(this == cur.text(), /**/);
1926
1927         pos_type pos = cur.pos();
1928
1929         // If cursor is at beginning or end of paragraph, do nothing.
1930         if (pos == cur.lastpos() || pos == 0)
1931                 return;
1932
1933         Paragraph & par = cur.paragraph();
1934
1935         // Get the positions of the characters to be transposed.
1936         pos_type pos1 = pos - 1;
1937         pos_type pos2 = pos;
1938
1939         // In change tracking mode, ignore deleted characters.
1940         while (pos2 < cur.lastpos() && par.isDeleted(pos2))
1941                 ++pos2;
1942         if (pos2 == cur.lastpos())
1943                 return;
1944
1945         while (pos1 >= 0 && par.isDeleted(pos1))
1946                 --pos1;
1947         if (pos1 < 0)
1948                 return;
1949
1950         // Don't do anything if one of the "characters" is not regular text.
1951         if (par.isInset(pos1) || par.isInset(pos2))
1952                 return;
1953
1954         // Store the characters to be transposed (including font information).
1955         char_type const char1 = par.getChar(pos1);
1956         Font const font1 =
1957                 par.getFontSettings(cur.buffer()->params(), pos1);
1958
1959         char_type const char2 = par.getChar(pos2);
1960         Font const font2 =
1961                 par.getFontSettings(cur.buffer()->params(), pos2);
1962
1963         // And finally, we are ready to perform the transposition.
1964         // Track the changes if Change Tracking is enabled.
1965         bool const trackChanges = cur.buffer()->params().trackChanges;
1966
1967         cur.recordUndo();
1968
1969         par.eraseChar(pos2, trackChanges);
1970         par.eraseChar(pos1, trackChanges);
1971         par.insertChar(pos1, char2, font2, trackChanges);
1972         par.insertChar(pos2, char1, font1, trackChanges);
1973
1974         cur.checkBufferStructure();
1975
1976         // After the transposition, move cursor to after the transposition.
1977         setCursor(cur, cur.pit(), pos2);
1978         cur.forwardPos();
1979 }
1980
1981
1982 DocIterator Text::macrocontextPosition() const
1983 {
1984         return macrocontext_position_;
1985 }
1986
1987
1988 void Text::setMacrocontextPosition(DocIterator const & pos)
1989 {
1990         macrocontext_position_ = pos;
1991 }
1992
1993
1994 docstring Text::previousWord(CursorSlice const & sl) const
1995 {
1996         CursorSlice from = sl;
1997         CursorSlice to = sl;
1998         getWord(from, to, PREVIOUS_WORD);
1999         if (sl == from || to == from)
2000                 return docstring();
2001         
2002         Paragraph const & par = sl.paragraph();
2003         return par.asString(from.pos(), to.pos());
2004 }
2005
2006
2007 bool Text::completionSupported(Cursor const & cur) const
2008 {
2009         Paragraph const & par = cur.paragraph();
2010         return cur.pos() > 0
2011                 && (cur.pos() >= par.size() || par.isWordSeparator(cur.pos()))
2012                 && !par.isWordSeparator(cur.pos() - 1);
2013 }
2014
2015
2016 CompletionList const * Text::createCompletionList(Cursor const & cur) const
2017 {
2018         WordList const * list = theWordList(*cur.getFont().language());
2019         return new TextCompletionList(cur, list);
2020 }
2021
2022
2023 bool Text::insertCompletion(Cursor & cur, docstring const & s, bool /*finished*/)
2024 {       
2025         LASSERT(cur.bv().cursor() == cur, /**/);
2026         cur.insert(s);
2027         cur.bv().cursor() = cur;
2028         if (!(cur.disp_.update() & Update::Force))
2029                 cur.updateFlags(cur.disp_.update() | Update::SinglePar);
2030         return true;
2031 }
2032         
2033         
2034 docstring Text::completionPrefix(Cursor const & cur) const
2035 {
2036         return previousWord(cur.top());
2037 }
2038
2039 } // namespace lyx