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