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