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