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