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