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