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