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