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