]> git.lyx.org Git - lyx.git/blob - src/text.C
Compile fix.
[lyx.git] / src / text.C
1 /**
2  * \file src/text.C
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 Jean-Marc Lasgouttes
9  * \author John Levon
10  * \author André Pönitz
11  * \author Dekel Tsur
12  * \author Jürgen Vigna
13  *
14  * Full author contact details are available in file CREDITS.
15  */
16
17 #include <config.h>
18
19 #include "lyxtext.h"
20
21 #include "author.h"
22 #include "buffer.h"
23 #include "buffer_funcs.h"
24 #include "bufferparams.h"
25 #include "BufferView.h"
26 #include "bufferview_funcs.h"
27 #include "cursor.h"
28 #include "pariterator.h"
29 #include "coordcache.h"
30 #include "CutAndPaste.h"
31 #include "debug.h"
32 #include "dispatchresult.h"
33 #include "encoding.h"
34 #include "errorlist.h"
35 #include "funcrequest.h"
36 #include "factory.h"
37 #include "FontIterator.h"
38 #include "gettext.h"
39 #include "language.h"
40 #include "LColor.h"
41 #include "lyxlength.h"
42 #include "lyxlex.h"
43 #include "lyxrc.h"
44 #include "lyxrow.h"
45 #include "metricsinfo.h"
46 #include "paragraph.h"
47 #include "paragraph_funcs.h"
48 #include "ParagraphParameters.h"
49 #include "rowpainter.h"
50 #include "undo.h"
51 #include "vspace.h"
52 #include "WordLangTuple.h"
53
54 #include "frontends/FontMetrics.h"
55 #include "frontends/Painter.h"
56 #include "frontends/Selection.h"
57
58 #include "insets/insettext.h"
59 #include "insets/insetbibitem.h"
60 #include "insets/insethfill.h"
61 #include "insets/insetlatexaccent.h"
62 #include "insets/insetline.h"
63 #include "insets/insetnewline.h"
64 #include "insets/insetpagebreak.h"
65 #include "insets/insetoptarg.h"
66 #include "insets/insetspace.h"
67 #include "insets/insetspecialchar.h"
68 #include "insets/insettabular.h"
69
70 #include "support/lstrings.h"
71 #include "support/textutils.h"
72 #include "support/convert.h"
73
74 #include <boost/current_function.hpp>
75
76 #include <sstream>
77
78 using std::auto_ptr;
79 using std::advance;
80 using std::distance;
81 using std::max;
82 using std::min;
83 using std::endl;
84 using std::string;
85
86 namespace lyx {
87
88 using support::bformat;
89 using support::contains;
90 using support::lowercase;
91 using support::split;
92 using support::uppercase;
93
94 using cap::cutSelection;
95 using cap::pasteParagraphList;
96
97 using frontend::FontMetrics;
98
99 namespace {
100
101 void readParToken(Buffer const & buf, Paragraph & par, LyXLex & lex,
102         string const & token, LyXFont & font, Change & change, ErrorList & errorList)
103 {
104         BufferParams const & bp = buf.params();
105
106         if (token[0] != '\\') {
107 #if 0
108                 string::const_iterator cit = token.begin();
109                 for (; cit != token.end(); ++cit)
110                         par.insertChar(par.size(), (*cit), font, change);
111 #else
112                 docstring dstr = lex.getDocString();
113                 docstring::const_iterator cit = dstr.begin();
114                 docstring::const_iterator cend = dstr.end();
115                 for (; cit != cend; ++cit)
116                         par.insertChar(par.size(), *cit, font, change);
117 #endif
118         } else if (token == "\\begin_layout") {
119                 lex.eatLine();
120                 string layoutname = lex.getString();
121
122                 font = LyXFont(LyXFont::ALL_INHERIT, bp.language);
123                 change = Change(Change::UNCHANGED);
124
125                 LyXTextClass const & tclass = bp.getLyXTextClass();
126
127                 if (layoutname.empty()) {
128                         layoutname = tclass.defaultLayoutName();
129                 }
130
131                 bool hasLayout = tclass.hasLayout(layoutname);
132
133                 if (!hasLayout) {
134                         errorList.push_back(ErrorItem(_("Unknown layout"),
135                         bformat(_("Layout '%1$s' does not exist in textclass '%2$s'\nTrying to use the default instead.\n"),
136                         from_utf8(layoutname), from_utf8(tclass.name())), par.id(), 0, par.size()));
137                         layoutname = tclass.defaultLayoutName();
138                 }
139
140                 par.layout(bp.getLyXTextClass()[layoutname]);
141
142                 // Test whether the layout is obsolete.
143                 LyXLayout_ptr const & layout = par.layout();
144                 if (!layout->obsoleted_by().empty())
145                         par.layout(bp.getLyXTextClass()[layout->obsoleted_by()]);
146
147                 par.params().read(lex);
148
149         } else if (token == "\\end_layout") {
150                 lyxerr << BOOST_CURRENT_FUNCTION
151                        << ": Solitary \\end_layout in line "
152                        << lex.getLineNo() << "\n"
153                        << "Missing \\begin_layout?.\n";
154         } else if (token == "\\end_inset") {
155                 lyxerr << BOOST_CURRENT_FUNCTION
156                        << ": Solitary \\end_inset in line "
157                        << lex.getLineNo() << "\n"
158                        << "Missing \\begin_inset?.\n";
159         } else if (token == "\\begin_inset") {
160                 InsetBase * inset = readInset(lex, buf);
161                 if (inset)
162                         par.insertInset(par.size(), inset, font, change);
163                 else {
164                         lex.eatLine();
165                         docstring line = lex.getDocString();
166                         errorList.push_back(ErrorItem(_("Unknown Inset"), line,
167                                             par.id(), 0, par.size()));
168                 }
169         } else if (token == "\\family") {
170                 lex.next();
171                 font.setLyXFamily(lex.getString());
172         } else if (token == "\\series") {
173                 lex.next();
174                 font.setLyXSeries(lex.getString());
175         } else if (token == "\\shape") {
176                 lex.next();
177                 font.setLyXShape(lex.getString());
178         } else if (token == "\\size") {
179                 lex.next();
180                 font.setLyXSize(lex.getString());
181         } else if (token == "\\lang") {
182                 lex.next();
183                 string const tok = lex.getString();
184                 Language const * lang = languages.getLanguage(tok);
185                 if (lang) {
186                         font.setLanguage(lang);
187                 } else {
188                         font.setLanguage(bp.language);
189                         lex.printError("Unknown language `$$Token'");
190                 }
191         } else if (token == "\\numeric") {
192                 lex.next();
193                 font.setNumber(font.setLyXMisc(lex.getString()));
194         } else if (token == "\\emph") {
195                 lex.next();
196                 font.setEmph(font.setLyXMisc(lex.getString()));
197         } else if (token == "\\bar") {
198                 lex.next();
199                 string const tok = lex.getString();
200
201                 if (tok == "under")
202                         font.setUnderbar(LyXFont::ON);
203                 else if (tok == "no")
204                         font.setUnderbar(LyXFont::OFF);
205                 else if (tok == "default")
206                         font.setUnderbar(LyXFont::INHERIT);
207                 else
208                         lex.printError("Unknown bar font flag "
209                                        "`$$Token'");
210         } else if (token == "\\noun") {
211                 lex.next();
212                 font.setNoun(font.setLyXMisc(lex.getString()));
213         } else if (token == "\\color") {
214                 lex.next();
215                 font.setLyXColor(lex.getString());
216         } else if (token == "\\InsetSpace" || token == "\\SpecialChar") {
217
218                 // Insets don't make sense in a free-spacing context! ---Kayvan
219                 if (par.isFreeSpacing()) {
220                         if (token == "\\InsetSpace")
221                                 par.insertChar(par.size(), ' ', font, change);
222                         else if (lex.isOK()) {
223                                 lex.next();
224                                 string const next_token = lex.getString();
225                                 if (next_token == "\\-")
226                                         par.insertChar(par.size(), '-', font, change);
227                                 else {
228                                         lex.printError("Token `$$Token' "
229                                                        "is in free space "
230                                                        "paragraph layout!");
231                                 }
232                         }
233                 } else {
234                         auto_ptr<InsetBase> inset;
235                         if (token == "\\SpecialChar" )
236                                 inset.reset(new InsetSpecialChar);
237                         else
238                                 inset.reset(new InsetSpace);
239                         inset->read(buf, lex);
240                         par.insertInset(par.size(), inset.release(),
241                                         font, change);
242                 }
243         } else if (token == "\\i") {
244                 auto_ptr<InsetBase> inset(new InsetLatexAccent);
245                 inset->read(buf, lex);
246                 par.insertInset(par.size(), inset.release(), font, change);
247         } else if (token == "\\backslash") {
248                 par.insertChar(par.size(), '\\', font, change);
249         } else if (token == "\\newline") {
250                 auto_ptr<InsetBase> inset(new InsetNewline);
251                 inset->read(buf, lex);
252                 par.insertInset(par.size(), inset.release(), font, change);
253         } else if (token == "\\LyXTable") {
254                 auto_ptr<InsetBase> inset(new InsetTabular(buf));
255                 inset->read(buf, lex);
256                 par.insertInset(par.size(), inset.release(), font, change);
257         } else if (token == "\\hfill") {
258                 par.insertInset(par.size(), new InsetHFill, font, change);
259         } else if (token == "\\lyxline") {
260                 par.insertInset(par.size(), new InsetLine, font, change);
261         } else if (token == "\\newpage") {
262                 par.insertInset(par.size(), new InsetPagebreak, font, change);
263         } else if (token == "\\clearpage") {
264                 par.insertInset(par.size(), new InsetClearPage, font, change);
265         } else if (token == "\\cleardoublepage") {
266                 par.insertInset(par.size(), new InsetClearDoublePage, font, change);
267         } else if (token == "\\change_unchanged") {
268                 change = Change(Change::UNCHANGED);
269         } else if (token == "\\change_inserted") {
270                 lex.eatLine();
271                 std::istringstream is(lex.getString());
272                 unsigned int aid;
273                 time_type ct;
274                 is >> aid >> ct;
275                 if (aid >= bp.author_map.size()) {
276                         errorList.push_back(ErrorItem(_("Change tracking error"),
277                                             bformat(_("Unknown author index for insertion: %1$d\n"), aid),
278                                             par.id(), 0, par.size()));
279                         change = Change(Change::UNCHANGED);
280                 } else
281                         change = Change(Change::INSERTED, bp.author_map[aid], ct);
282         } else if (token == "\\change_deleted") {
283                 lex.eatLine();
284                 std::istringstream is(lex.getString());
285                 unsigned int aid;
286                 time_type ct;
287                 is >> aid >> ct;
288                 if (aid >= bp.author_map.size()) {
289                         errorList.push_back(ErrorItem(_("Change tracking error"),
290                                             bformat(_("Unknown author index for deletion: %1$d\n"), aid),
291                                             par.id(), 0, par.size()));
292                         change = Change(Change::UNCHANGED);
293                 } else
294                         change = Change(Change::DELETED, bp.author_map[aid], ct);
295         } else {
296                 lex.eatLine();
297                 errorList.push_back(ErrorItem(_("Unknown token"),
298                         bformat(_("Unknown token: %1$s %2$s\n"), from_utf8(token),
299                         lex.getDocString()),
300                         par.id(), 0, par.size()));
301         }
302 }
303
304
305 void readParagraph(Buffer const & buf, Paragraph & par, LyXLex & lex,
306         ErrorList & errorList)
307 {
308         lex.nextToken();
309         string token = lex.getString();
310         LyXFont font;
311         Change change(Change::UNCHANGED);
312
313         while (lex.isOK()) {
314                 readParToken(buf, par, lex, token, font, change, errorList);
315
316                 lex.nextToken();
317                 token = lex.getString();
318
319                 if (token.empty())
320                         continue;
321
322                 if (token == "\\end_layout") {
323                         //Ok, paragraph finished
324                         break;
325                 }
326
327                 lyxerr[Debug::PARSER] << "Handling paragraph token: `"
328                                       << token << '\'' << endl;
329                 if (token == "\\begin_layout" || token == "\\end_document"
330                     || token == "\\end_inset" || token == "\\begin_deeper"
331                     || token == "\\end_deeper") {
332                         lex.pushToken(token);
333                         lyxerr << "Paragraph ended in line "
334                                << lex.getLineNo() << "\n"
335                                << "Missing \\end_layout.\n";
336                         break;
337                 }
338         }
339         // Final change goes to paragraph break:
340         par.setChange(par.size(), change);
341
342         // Initialize begin_of_body_ on load; redoParagraph maintains
343         par.setBeginOfBody();
344 }
345
346
347 } // namespace anon
348
349
350
351 double LyXText::spacing(Buffer const & buffer,
352                 Paragraph const & par) const
353 {
354         if (par.params().spacing().isDefault())
355                 return buffer.params().spacing().getValue();
356         return par.params().spacing().getValue();
357 }
358
359
360 int LyXText::singleWidth(Buffer const & buffer, Paragraph const & par,
361                 pos_type pos) const
362 {
363         return singleWidth(par, pos, par.getChar(pos),
364                 getFont(buffer, par, pos));
365 }
366
367
368 int LyXText::singleWidth(Paragraph const & par,
369                          pos_type pos, char_type c, LyXFont const & font) const
370 {
371         // The most common case is handled first (Asger)
372         if (isPrintable(c)) {
373                 Language const * language = font.language();
374                 if (language->rightToLeft()) {
375                         if ((lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 ||
376                              lyxrc.font_norm_type == LyXRC::ISO_10646_1)
377                             && language->lang() == "arabic") {
378                                 if (Encodings::isComposeChar_arabic(c))
379                                         return 0;
380                                 c = par.transformChar(c, pos);
381                         } else if (language->lang() == "hebrew" &&
382                                    Encodings::isComposeChar_hebrew(c))
383                                 return 0;
384                 }
385                 return theFontMetrics(font).width(c);
386         }
387
388         if (c == Paragraph::META_INSET)
389                 return par.getInset(pos)->width();
390
391         return theFontMetrics(font).width(c);
392 }
393
394
395 int LyXText::leftMargin(Buffer const & buffer, int max_width, pit_type pit) const
396 {
397         BOOST_ASSERT(pit >= 0);
398         BOOST_ASSERT(pit < int(pars_.size()));
399         return leftMargin(buffer, max_width, pit, pars_[pit].size());
400 }
401
402
403 int LyXText::leftMargin(Buffer const & buffer, int max_width,
404                 pit_type const pit, pos_type const pos) const
405 {
406         BOOST_ASSERT(pit >= 0);
407         BOOST_ASSERT(pit < int(pars_.size()));
408         Paragraph const & par = pars_[pit];
409         BOOST_ASSERT(pos >= 0);
410         BOOST_ASSERT(pos <= par.size());
411         //lyxerr << "LyXText::leftMargin: pit: " << pit << " pos: " << pos << endl;
412         LyXTextClass const & tclass = buffer.params().getLyXTextClass();
413         LyXLayout_ptr const & layout = par.layout();
414
415         string parindent = layout->parindent;
416
417         int l_margin = 0;
418
419         if (isMainText(buffer))
420                 l_margin += changebarMargin();
421
422         // FIXME UNICODE
423         docstring leftm = from_utf8(tclass.leftmargin());
424         l_margin += theFontMetrics(buffer.params().getFont()).signedWidth(leftm);
425
426         if (par.getDepth() != 0) {
427                 // find the next level paragraph
428                 pit_type newpar = outerHook(pit, pars_);
429                 if (newpar != pit_type(pars_.size())) {
430                         if (pars_[newpar].layout()->isEnvironment()) {
431                                 l_margin = leftMargin(buffer, max_width, newpar);
432                         }
433                         if (par.layout() == tclass.defaultLayout()) {
434                                 if (pars_[newpar].params().noindent())
435                                         parindent.erase();
436                                 else
437                                         parindent = pars_[newpar].layout()->parindent;
438                         }
439                 }
440         }
441
442         // This happens after sections in standard classes. The 1.3.x
443         // code compared depths too, but it does not seem necessary
444         // (JMarc)
445         if (par.layout() == tclass.defaultLayout()
446             && pit > 0 && pars_[pit - 1].layout()->nextnoindent)
447                 parindent.erase();
448
449         LyXFont const labelfont = getLabelFont(buffer, par);
450         FontMetrics const & labelfont_metrics = theFontMetrics(labelfont);
451
452         switch (layout->margintype) {
453         case MARGIN_DYNAMIC:
454                 if (!layout->leftmargin.empty()) {
455                         // FIXME UNICODE
456                         docstring leftm = from_utf8(layout->leftmargin);
457                         l_margin += theFontMetrics(buffer.params().getFont()).signedWidth(leftm);
458                 }
459                 if (!par.getLabelstring().empty()) {
460                         // FIXME UNICODE
461                         docstring labin = from_utf8(layout->labelindent);
462                         l_margin += labelfont_metrics.signedWidth(labin);
463                         docstring labstr = par.getLabelstring();
464                         l_margin += labelfont_metrics.width(labstr);
465                         docstring labsep = from_utf8(layout->labelsep);
466                         l_margin += labelfont_metrics.width(labsep);
467                 }
468                 break;
469
470         case MARGIN_MANUAL: {
471                 // FIXME UNICODE
472                 docstring labin = from_utf8(layout->labelindent);
473                 l_margin += labelfont_metrics.signedWidth(labin);
474                 // The width of an empty par, even with manual label, should be 0
475                 if (!par.empty() && pos >= par.beginOfBody()) {
476                         if (!par.getLabelWidthString().empty()) {
477                                 docstring labstr = par.getLabelWidthString();
478                                 l_margin += labelfont_metrics.width(labstr);
479                                 docstring labsep = from_utf8(layout->labelsep);
480                                 l_margin += labelfont_metrics.width(labsep);
481                         }
482                 }
483                 break;
484         }
485
486         case MARGIN_STATIC: {
487                 // FIXME UNICODE
488                 docstring leftm = from_utf8(layout->leftmargin);
489                 l_margin += theFontMetrics(buffer.params().getFont()).signedWidth(leftm)
490                         * 4     / (par.getDepth() + 4);
491                 break;
492         }
493
494         case MARGIN_FIRST_DYNAMIC:
495                 if (layout->labeltype == LABEL_MANUAL) {
496                         if (pos >= par.beginOfBody()) {
497                                 // FIXME UNICODE
498                                 l_margin += labelfont_metrics.signedWidth(
499                                         from_utf8(layout->leftmargin));
500                         } else {
501                                 // FIXME UNICODE
502                                 l_margin += labelfont_metrics.signedWidth(
503                                         from_utf8(layout->labelindent));
504                         }
505                 } else if (pos != 0
506                            // Special case to fix problems with
507                            // theorems (JMarc)
508                            || (layout->labeltype == LABEL_STATIC
509                                && layout->latextype == LATEX_ENVIRONMENT
510                                && !isFirstInSequence(pit, pars_))) {
511                         // FIXME UNICODE
512                         l_margin += labelfont_metrics.signedWidth(from_utf8(layout->leftmargin));
513                 } else if (layout->labeltype != LABEL_TOP_ENVIRONMENT
514                            && layout->labeltype != LABEL_BIBLIO
515                            && layout->labeltype !=
516                            LABEL_CENTERED_TOP_ENVIRONMENT) {
517                         l_margin += labelfont_metrics.signedWidth(from_utf8(layout->labelindent));
518                         l_margin += labelfont_metrics.width(from_utf8(layout->labelsep));
519                         l_margin += labelfont_metrics.width(par.getLabelstring());
520                 }
521                 break;
522
523         case MARGIN_RIGHT_ADDRESS_BOX: {
524 #if 0
525                 // ok, a terrible hack. The left margin depends on the widest
526                 // row in this paragraph.
527                 RowList::iterator rit = par.rows().begin();
528                 RowList::iterator end = par.rows().end();
529 #ifdef WITH_WARNINGS
530 #warning This is wrong.
531 #endif
532                 int minfill = max_width;
533                 for ( ; rit != end; ++rit)
534                         if (rit->fill() < minfill)
535                                 minfill = rit->fill();
536                 l_margin += theFontMetrics(params.getFont()).signedWidth(layout->leftmargin);
537                 l_margin += minfill;
538 #endif
539                 // also wrong, but much shorter.
540                 l_margin += max_width / 2;
541                 break;
542         }
543         }
544
545         if (!par.params().leftIndent().zero())
546                 l_margin += par.params().leftIndent().inPixels(max_width);
547
548         LyXAlignment align;
549
550         if (par.params().align() == LYX_ALIGN_LAYOUT)
551                 align = layout->align;
552         else
553                 align = par.params().align();
554
555         // set the correct parindent
556         if (pos == 0
557             && (layout->labeltype == LABEL_NO_LABEL
558                || layout->labeltype == LABEL_TOP_ENVIRONMENT
559                || layout->labeltype == LABEL_CENTERED_TOP_ENVIRONMENT
560                || (layout->labeltype == LABEL_STATIC
561                    && layout->latextype == LATEX_ENVIRONMENT
562                    && !isFirstInSequence(pit, pars_)))
563             && align == LYX_ALIGN_BLOCK
564             && !par.params().noindent()
565             // in some insets, paragraphs are never indented
566             && !(par.inInset() && par.inInset()->neverIndent(buffer))
567             // display style insets are always centered, omit indentation
568             && !(!par.empty()
569                     && par.isInset(pos)
570                     && par.getInset(pos)->display())
571             && (par.layout() != tclass.defaultLayout()
572                 || buffer.params().paragraph_separation ==
573                    BufferParams::PARSEP_INDENT))
574         {
575                 docstring din = from_utf8(parindent);
576                 l_margin += theFontMetrics(buffer.params().getFont()).signedWidth(din);
577         }
578
579         return l_margin;
580 }
581
582
583 LColor_color LyXText::backgroundColor() const
584 {
585         return LColor_color(LColor::color(background_color_));
586 }
587
588
589 void LyXText::breakParagraph(LCursor & cur, bool keep_layout)
590 {
591         BOOST_ASSERT(this == cur.text());
592
593         Paragraph & cpar = cur.paragraph();
594         pit_type cpit = cur.pit();
595
596         LyXTextClass const & tclass = cur.buffer().params().getLyXTextClass();
597         LyXLayout_ptr const & layout = cpar.layout();
598
599         // this is only allowed, if the current paragraph is not empty
600         // or caption and if it has not the keepempty flag active
601         if (cur.lastpos() == 0 && !cpar.allowEmpty() &&
602             layout->labeltype != LABEL_SENSITIVE)
603                 return;
604
605         // a layout change may affect also the following paragraph
606         recUndo(cur, cur.pit(), undoSpan(cur.pit()) - 1);
607
608         // Always break behind a space
609         // It is better to erase the space (Dekel)
610         if (cur.pos() != cur.lastpos() && cpar.isLineSeparator(cur.pos()))
611                 cpar.eraseChar(cur.pos(), cur.buffer().params().trackChanges);
612
613         // What should the layout for the new paragraph be?
614         int preserve_layout = 0;
615         if (keep_layout)
616                 preserve_layout = 2;
617         else
618                 preserve_layout = layout->isEnvironment();
619
620         // We need to remember this before we break the paragraph, because
621         // that invalidates the layout variable
622         bool sensitive = layout->labeltype == LABEL_SENSITIVE;
623
624         // we need to set this before we insert the paragraph.
625         bool const isempty = cpar.allowEmpty() && cpar.empty();
626
627         lyx::breakParagraph(cur.buffer().params(), paragraphs(), cpit,
628                          cur.pos(), preserve_layout);
629
630         // After this, neither paragraph contains any rows!
631
632         cpit = cur.pit();
633         pit_type next_par = cpit + 1;
634
635         // well this is the caption hack since one caption is really enough
636         if (sensitive) {
637                 if (cur.pos() == 0)
638                         // set to standard-layout
639                         pars_[cpit].applyLayout(tclass.defaultLayout());
640                 else
641                         // set to standard-layout
642                         pars_[next_par].applyLayout(tclass.defaultLayout());
643         }
644
645         while (!pars_[next_par].empty() && pars_[next_par].isNewline(0)) {
646                 if (!pars_[next_par].eraseChar(0, cur.buffer().params().trackChanges))
647                         break; // the character couldn't be deleted physically due to change tracking
648         }
649
650         ParIterator current_it(cur);
651         ParIterator last_it(cur);
652         ++last_it;
653         ++last_it;
654
655         updateLabels(cur.buffer(), current_it, last_it);
656
657         // A singlePar update is not enough in this case.
658         cur.updateFlags(Update::Force);
659
660         // This check is necessary. Otherwise the new empty paragraph will
661         // be deleted automatically. And it is more friendly for the user!
662         if (cur.pos() != 0 || isempty)
663                 setCursor(cur.top(), cur.pit() + 1, 0);
664         else
665                 setCursor(cur.top(), cur.pit(), 0);
666 }
667
668
669 // insert a character, moves all the following breaks in the
670 // same Paragraph one to the right and make a rebreak
671 void LyXText::insertChar(LCursor & cur, char_type c)
672 {
673         BOOST_ASSERT(this == cur.text());
674         BOOST_ASSERT(c != Paragraph::META_INSET);
675
676         recordUndo(cur, Undo::INSERT);
677
678         Buffer const & buffer = cur.buffer();
679         Paragraph & par = cur.paragraph();
680         // try to remove this
681         pit_type const pit = cur.pit();
682
683         bool const freeSpacing = par.layout()->free_spacing ||
684                 par.isFreeSpacing();
685
686         if (lyxrc.auto_number) {
687                 static docstring const number_operators = from_ascii("+-/*");
688                 static docstring const number_unary_operators = from_ascii("+-");
689                 static docstring const number_seperators = from_ascii(".,:");
690
691                 if (current_font.number() == LyXFont::ON) {
692                         if (!isDigit(c) && !contains(number_operators, c) &&
693                             !(contains(number_seperators, c) &&
694                               cur.pos() != 0 &&
695                               cur.pos() != cur.lastpos() &&
696                               getFont(buffer, par, cur.pos()).number() == LyXFont::ON &&
697                               getFont(buffer, par, cur.pos() - 1).number() == LyXFont::ON)
698                            )
699                                 number(cur); // Set current_font.number to OFF
700                 } else if (isDigit(c) &&
701                            real_current_font.isVisibleRightToLeft()) {
702                         number(cur); // Set current_font.number to ON
703
704                         if (cur.pos() != 0) {
705                                 char_type const c = par.getChar(cur.pos() - 1);
706                                 if (contains(number_unary_operators, c) &&
707                                     (cur.pos() == 1
708                                      || par.isSeparator(cur.pos() - 2)
709                                      || par.isNewline(cur.pos() - 2))
710                                   ) {
711                                         setCharFont(buffer, pit, cur.pos() - 1, current_font);
712                                 } else if (contains(number_seperators, c)
713                                      && cur.pos() >= 2
714                                      && getFont(buffer, par, cur.pos() - 2).number() == LyXFont::ON) {
715                                         setCharFont(buffer, pit, cur.pos() - 1, current_font);
716                                 }
717                         }
718                 }
719         }
720
721         // First check, if there will be two blanks together or a blank at
722         // the beginning of a paragraph.
723         // I decided to handle blanks like normal characters, the main
724         // difference are the special checks when calculating the row.fill
725         // (blank does not count at the end of a row) and the check here
726
727         // When the free-spacing option is set for the current layout,
728         // disable the double-space checking
729         if (!freeSpacing && isLineSeparatorChar(c)) {
730                 if (cur.pos() == 0) {
731                         static bool sent_space_message = false;
732                         if (!sent_space_message) {
733                                 cur.message(_("You cannot insert a space at the "
734                                                            "beginning of a paragraph. Please read the Tutorial."));
735                                 sent_space_message = true;
736                         }
737                         return;
738                 }
739                 BOOST_ASSERT(cur.pos() > 0);
740                 if ((par.isLineSeparator(cur.pos() - 1) || par.isNewline(cur.pos() - 1))
741                     && !par.isDeleted(cur.pos() - 1)) {
742                         static bool sent_space_message = false;
743                         if (!sent_space_message) {
744                                 cur.message(_("You cannot type two spaces this way. "
745                                                            "Please read the Tutorial."));
746                                 sent_space_message = true;
747                         }
748                         return;
749                 }
750         }
751
752         par.insertChar(cur.pos(), c, current_font, cur.buffer().params().trackChanges);
753
754 //              cur.updateFlags(Update::Force);
755         setCursor(cur.top(), cur.pit(), cur.pos() + 1);
756         charInserted();
757 }
758
759
760 void LyXText::charInserted()
761 {
762         // Here we call finishUndo for every 20 characters inserted.
763         // This is from my experience how emacs does it. (Lgb)
764         static unsigned int counter;
765         if (counter < 20) {
766                 ++counter;
767         } else {
768                 finishUndo();
769                 counter = 0;
770         }
771 }
772
773
774 // the cursor set functions have a special mechanism. When they
775 // realize, that you left an empty paragraph, they will delete it.
776
777 bool LyXText::cursorRightOneWord(LCursor & cur)
778 {
779         BOOST_ASSERT(this == cur.text());
780
781         LCursor old = cur;
782
783         if (old.pos() == old.lastpos() && old.pit() != old.lastpit()) {
784                 ++old.pit();
785                 old.pos() = 0;
786         } else {
787                 // Advance through word.
788                 while (old.pos() != old.lastpos() && old.paragraph().isLetter(old.pos()))
789                         ++old.pos();
790                 // Skip through trailing nonword stuff.
791                 while (old.pos() != old.lastpos() && !old.paragraph().isLetter(old.pos()))
792                         ++old.pos();
793         }
794         return setCursor(cur, old.pit(), old.pos());
795 }
796
797
798 bool LyXText::cursorLeftOneWord(LCursor & cur)
799 {
800         BOOST_ASSERT(this == cur.text());
801
802         LCursor old = cur;
803
804         if (old.pos() == 0 && old.pit() != 0) {
805                 --old.pit();
806                 old.pos() = old.lastpos();
807         } else {
808                 // Skip through initial nonword stuff.
809                 while (old.pos() != 0 && !old.paragraph().isLetter(old.pos() - 1))
810                         --old.pos();
811                 // Advance through word.
812                 while (old.pos() != 0 && old.paragraph().isLetter(old.pos() - 1))
813                         --old.pos();
814         }
815         return setCursor(cur, old.pit(), old.pos());
816 }
817
818
819 void LyXText::selectWord(LCursor & cur, word_location loc)
820 {
821         BOOST_ASSERT(this == cur.text());
822         CursorSlice from = cur.top();
823         CursorSlice to = cur.top();
824         getWord(from, to, loc);
825         if (cur.top() != from)
826                 setCursor(cur, from.pit(), from.pos());
827         if (to == from)
828                 return;
829         cur.resetAnchor();
830         setCursor(cur, to.pit(), to.pos());
831         cur.setSelection();
832         theSelection().haveSelection(cur.selection());
833 }
834
835
836 // Select the word currently under the cursor when no
837 // selection is currently set
838 bool LyXText::selectWordWhenUnderCursor(LCursor & cur, word_location loc)
839 {
840         BOOST_ASSERT(this == cur.text());
841         if (cur.selection())
842                 return false;
843         selectWord(cur, loc);
844         return cur.selection();
845 }
846
847
848 void LyXText::acceptChange(LCursor & cur)
849 {
850         BOOST_ASSERT(this == cur.text());
851
852         if (!cur.selection())
853                 return;
854
855         recordUndoSelection(cur, Undo::ATOMIC);
856
857         DocIterator beg = cur.selectionBegin();
858         DocIterator end = cur.selectionEnd();
859
860         // first, accept changes within each individual paragraph (do not consider end-of-par)
861         
862         for (pit_type pit = beg.pit(); pit <= end.pit(); ++pit) {
863                 // ignore empty paragraphs; otherwise, an assertion will fail for acceptChanges(0, 0) 
864                 if (pars_[pit].size() == 0)
865                         continue;
866
867                 // do not consider first paragraph if the cursor starts at pos size()
868                 if (pit == beg.pit() && beg.pos() == pars_[pit].size())
869                         continue;
870
871                 // do not consider last paragraph if the cursor ends at pos 0
872                 if (pit == end.pit() && end.pos() == 0) 
873                         break; // last iteration anyway
874
875                 pos_type left  = (pit == beg.pit() ? beg.pos() : 0);
876                 pos_type right = (pit == end.pit() ? end.pos() : pars_[pit].size());
877                 pars_[pit].acceptChanges(left, right);
878         }
879         
880         // next, accept imaginary end-of-par characters
881         // iterate from last to first paragraph such that we don't have to care for a changing 'end' 
882  
883         pos_type endpit = end.pit();    
884         for (pit_type pit = endpit; pit >= beg.pit(); --pit) {
885                 pos_type pos = pars_[pit].size();
886
887                 // last paragraph of the selection requires special handling
888                 if (pit == end.pit()) {
889                         // skip if the selection ends before the end-of-par
890                         if (end.pos() < pos)
891                                 continue;
892
893                         // skip if the selection ends with the end-of-par and this is not the
894                         // last paragraph of the document
895                         // note: the user must be able to accept the end-of-par of the last par!
896                         if (end.pos() == pos && pit != pars_.size() - 1)
897                                 continue;
898                 }
899
900
901                 if (!pars_[pit].isUnchanged(pos)) {
902                         if (pars_[pit].isInserted(pos)) {
903                                 pars_[pit].setChange(pos, Change(Change::UNCHANGED));
904                         } else { // isDeleted
905                                 if (pit == pars_.size()) {
906                                         // we cannot remove a par break at the end of the last paragraph
907                                         // instead, we mark it unchanged
908                                         pars_[pit].setChange(pos, Change(Change::UNCHANGED));
909                                 } else {
910                                         mergeParagraph(cur.buffer().params(), pars_, pit);
911                                         endpit--;
912                                 }
913                         }
914                 }
915         }
916
917         // finally, invoke the DEPM
918         // FIXME: the following code will be changed in the near future
919         setCursorIntern(cur, endpit, 0);
920         for (pit_type pit = endpit - 1; pit >= beg.pit(); --pit) {
921                 bool dummy;
922                 LCursor old = cur;
923                 setCursorIntern(cur, pit, 0);
924                 deleteEmptyParagraphMechanism(cur, old, dummy);
925         }
926
927         finishUndo();
928         cur.clearSelection();
929         setCursorIntern(cur, beg.pit(), beg.pos());
930         cur.updateFlags(Update::Force);
931         updateLabels(cur.buffer());
932 }
933
934
935 void LyXText::rejectChange(LCursor & cur)
936 {
937         // FIXME: change tracking (MG)
938
939         BOOST_ASSERT(this == cur.text());
940
941         if (!cur.selection() && cur.lastpos() != 0)
942                 return;
943
944         // FIXME: we must handle start = end = 0
945
946         recordUndoSelection(cur, Undo::INSERT);
947
948         DocIterator it = cur.selectionBegin();
949         DocIterator et = cur.selectionEnd();
950         pit_type pit = it.pit();
951         for (; pit <= et.pit(); ++pit) {
952                 pos_type left  = (pit == it.pit() ? it.pos() : 0);
953                 pos_type right = (pit == et.pit() ? et.pos() : pars_[pit].size());
954
955                 // handle imaginary end-of-par character first
956                 if (right == pars_[pit].size() && !pars_[pit].isUnchanged(right)) {
957                         if (pars_[pit].isDeleted(right)) {
958                                 pars_[pit].setChange(right, Change(Change::UNCHANGED));
959                         } else {
960                                 // if (pit + 1 < et.pit()) {
961                                 //      setCursorIntern(cur, pit + 1, 0);
962                                 //      backspacePos0(cur);
963                                 // }
964                         }
965                 }
966                 
967                 pars_[pit].rejectChanges(left, right);
968         }
969         finishUndo();
970         cur.clearSelection();
971         setCursorIntern(cur, it.pit(), it.pos());
972         cur.updateFlags(Update::Force);
973 }
974
975
976 // Delete from cursor up to the end of the current or next word.
977 void LyXText::deleteWordForward(LCursor & cur)
978 {
979         BOOST_ASSERT(this == cur.text());
980         if (cur.lastpos() == 0)
981                 cursorRight(cur);
982         else {
983                 cur.resetAnchor();
984                 cur.selection() = true;
985                 cursorRightOneWord(cur);
986                 cur.setSelection();
987                 cutSelection(cur, true, false);
988         }
989 }
990
991
992 // Delete from cursor to start of current or prior word.
993 void LyXText::deleteWordBackward(LCursor & cur)
994 {
995         BOOST_ASSERT(this == cur.text());
996         if (cur.lastpos() == 0)
997                 cursorLeft(cur);
998         else {
999                 cur.resetAnchor();
1000                 cur.selection() = true;
1001                 cursorLeftOneWord(cur);
1002                 cur.setSelection();
1003                 cutSelection(cur, true, false);
1004         }
1005 }
1006
1007
1008 // Kill to end of line.
1009 void LyXText::deleteLineForward(LCursor & cur)
1010 {
1011         BOOST_ASSERT(this == cur.text());
1012         if (cur.lastpos() == 0) {
1013                 // Paragraph is empty, so we just go to the right
1014                 cursorRight(cur);
1015         } else {
1016                 cur.resetAnchor();
1017                 cur.selection() = true; // to avoid deletion
1018                 cursorEnd(cur);
1019                 cur.setSelection();
1020                 // What is this test for ??? (JMarc)
1021                 if (!cur.selection())
1022                         deleteWordForward(cur);
1023                 else
1024                         cutSelection(cur, true, false);
1025         }
1026 }
1027
1028
1029 void LyXText::changeCase(LCursor & cur, LyXText::TextCase action)
1030 {
1031         BOOST_ASSERT(this == cur.text());
1032         CursorSlice from;
1033         CursorSlice to;
1034
1035         if (cur.selection()) {
1036                 from = cur.selBegin();
1037                 to = cur.selEnd();
1038         } else {
1039                 from = cur.top();
1040                 getWord(from, to, PARTIAL_WORD);
1041                 cursorRightOneWord(cur);
1042         }
1043
1044         recordUndoSelection(cur);
1045
1046         pos_type pos = from.pos();
1047         int par = from.pit();
1048
1049         while (par != int(pars_.size()) && (pos != to.pos() || par != to.pit())) {
1050                 pit_type pit = par;
1051                 if (pos == pars_[pit].size()) {
1052                         ++par;
1053                         pos = 0;
1054                         continue;
1055                 }
1056                 char_type c = pars_[pit].getChar(pos);
1057                 if (c != Paragraph::META_INSET) {
1058                         switch (action) {
1059                         case text_lowercase:
1060                                 c = lowercase(c);
1061                                 break;
1062                         case text_capitalization:
1063                                 c = uppercase(c);
1064                                 action = text_lowercase;
1065                                 break;
1066                         case text_uppercase:
1067                                 c = uppercase(c);
1068                                 break;
1069                         }
1070                 }
1071
1072                 // FIXME: change tracking (MG)
1073                 // sorry but we are no longer allowed to set a single character directly
1074                 // we have to rewrite this method in terms of erase&insert operations
1075                 //pars_[pit].setChar(pos, c);
1076                 ++pos;
1077         }
1078 }
1079
1080
1081 bool LyXText::erase(LCursor & cur)
1082 {
1083         BOOST_ASSERT(this == cur.text());
1084         bool needsUpdate = false;
1085         Paragraph & par = cur.paragraph();
1086
1087         if (cur.pos() != cur.lastpos()) {
1088                 // this is the code for a normal delete, not pasting
1089                 // any paragraphs
1090                 recordUndo(cur, Undo::DELETE);
1091                 if(!par.eraseChar(cur.pos(), cur.buffer().params().trackChanges)) {
1092                         // the character has been logically deleted only => skip it
1093                         cur.forwardPosNoDescend();
1094                 }
1095                 needsUpdate = true;
1096         } else {
1097                 if (cur.pit() == cur.lastpit())
1098                         return dissolveInset(cur);
1099
1100                 if (!par.isMergedOnEndOfParDeletion(cur.buffer().params().trackChanges)) {
1101                         par.setChange(cur.pos(), Change(Change::DELETED));
1102                         cur.forwardPos();
1103                         needsUpdate = true;
1104                 } else {
1105                         setCursorIntern(cur, cur.pit() + 1, 0);
1106                         needsUpdate = backspacePos0(cur);
1107                 }
1108         }
1109
1110         if (needsUpdate) {
1111                 // Make sure the cursor is correct. Is this really needed?
1112                 // No, not really... at least not here!
1113                 cur.text()->setCursor(cur.top(), cur.pit(), cur.pos());
1114         }
1115         
1116         return needsUpdate;
1117 }
1118
1119
1120 bool LyXText::backspacePos0(LCursor & cur)
1121 {
1122         BOOST_ASSERT(this == cur.text());
1123         if (cur.pit() == 0)
1124                 return false;
1125
1126         bool needsUpdate = false;
1127
1128         BufferParams const & bufparams = cur.buffer().params();
1129         LyXTextClass const & tclass = bufparams.getLyXTextClass();
1130         ParagraphList & plist = cur.text()->paragraphs();
1131         Paragraph const & par = cur.paragraph();
1132         LCursor prevcur = cur;
1133         --prevcur.pit();
1134         prevcur.pos() = prevcur.lastpos();
1135         Paragraph const & prevpar = prevcur.paragraph();
1136
1137         // is it an empty paragraph?
1138         if (cur.lastpos() == 0 
1139             || (cur.lastpos() == 1 && par.isSeparator(0))) {
1140                 recordUndo(cur, Undo::ATOMIC, prevcur.pit(), cur.pit());
1141                 plist.erase(boost::next(plist.begin(), cur.pit()));
1142                 needsUpdate = true;
1143         }
1144         // is previous par empty?
1145         else if (prevcur.lastpos() == 0 
1146                  || (prevcur.lastpos() == 1 && prevpar.isSeparator(0))) {
1147                 recordUndo(cur, Undo::ATOMIC, prevcur.pit(), cur.pit());
1148                 plist.erase(boost::next(plist.begin(), prevcur.pit()));
1149                 needsUpdate = true;
1150         }
1151         // Pasting is not allowed, if the paragraphs have different
1152         // layouts. I think it is a real bug of all other
1153         // word processors to allow it. It confuses the user.
1154         // Correction: Pasting is always allowed with standard-layout
1155         else if (par.layout() == prevpar.layout()
1156                  || par.layout() == tclass.defaultLayout()) {
1157                 recordUndo(cur, Undo::ATOMIC, prevcur.pit());
1158                 mergeParagraph(bufparams, plist, prevcur.pit());
1159                 needsUpdate = true;
1160         }
1161
1162         if (needsUpdate) {
1163                 updateLabels(cur.buffer());
1164                 setCursorIntern(cur, prevcur.pit(), prevcur.pos());
1165         }
1166
1167         return needsUpdate;
1168 }
1169
1170
1171 bool LyXText::backspace(LCursor & cur)
1172 {
1173         BOOST_ASSERT(this == cur.text());
1174         bool needsUpdate = false;
1175         if (cur.pos() == 0) {
1176                 if (cur.pit() == 0)
1177                         return dissolveInset(cur);
1178
1179                 Paragraph & prev_par = pars_[cur.pit() - 1];
1180
1181                 if (!prev_par.isMergedOnEndOfParDeletion(cur.buffer().params().trackChanges)) {
1182                         prev_par.setChange(prev_par.size(), Change(Change::DELETED));
1183                         setCursorIntern(cur, cur.pit() - 1, prev_par.size());
1184                         return true;
1185                 }
1186                 // The cursor is at the beginning of a paragraph, so
1187                 // the backspace will collapse two paragraphs into one.
1188                 needsUpdate = backspacePos0(cur);
1189
1190         } else {
1191                 // this is the code for a normal backspace, not pasting
1192                 // any paragraphs
1193                 recordUndo(cur, Undo::DELETE);
1194                 // We used to do cursorLeftIntern() here, but it is
1195                 // not a good idea since it triggers the auto-delete
1196                 // mechanism. So we do a cursorLeftIntern()-lite,
1197                 // without the dreaded mechanism. (JMarc)
1198                 setCursorIntern(cur, cur.pit(), cur.pos() - 1,
1199                                 false, cur.boundary());
1200                 cur.paragraph().eraseChar(cur.pos(), cur.buffer().params().trackChanges);
1201         }
1202
1203         if (cur.pos() == cur.lastpos())
1204                 setCurrentFont(cur);
1205
1206         // A singlePar update is not enough in this case.
1207 //              cur.updateFlags(Update::Force);
1208         setCursor(cur.top(), cur.pit(), cur.pos());
1209
1210         return needsUpdate;
1211 }
1212
1213
1214 bool LyXText::dissolveInset(LCursor & cur) {
1215         BOOST_ASSERT(this == cur.text());
1216
1217         if (isMainText(*cur.bv().buffer()) || cur.inset().nargs() != 1)
1218                 return false;
1219
1220         recordUndoInset(cur);
1221         cur.selHandle(false);
1222         // save position
1223         pos_type spos = cur.pos();
1224         pit_type spit = cur.pit();
1225         ParagraphList plist;
1226         if (cur.lastpit() != 0 || cur.lastpos() != 0)
1227                 plist = paragraphs();
1228         cur.popLeft();
1229         // store cursor offset
1230         if (spit == 0)
1231                 spos += cur.pos();
1232         spit += cur.pit();
1233         Buffer & b = cur.buffer();
1234         cur.paragraph().eraseChar(cur.pos(), b.params().trackChanges);
1235         if (!plist.empty()) {
1236                 // ERT paragraphs have the Language latex_language.
1237                 // This is invalid outside of ERT, so we need to
1238                 // change it to the buffer language.
1239                 ParagraphList::iterator it = plist.begin();
1240                 ParagraphList::iterator it_end = plist.end();
1241                 for (; it != it_end; it++) {
1242                         it->changeLanguage(b.params(), latex_language,
1243                                         b.getLanguage());
1244                 }
1245
1246                 pasteParagraphList(cur, plist, b.params().textclass,
1247                                    b.errorList("Paste"));
1248                 // restore position
1249                 cur.pit() = std::min(cur.lastpit(), spit);
1250                 cur.pos() = std::min(cur.lastpos(), spos);
1251         }
1252         cur.clearSelection();
1253         cur.resetAnchor();
1254         return true;
1255 }
1256
1257
1258 // only used for inset right now. should also be used for main text
1259 void LyXText::draw(PainterInfo & pi, int x, int y) const
1260 {
1261         paintTextInset(*this, pi, x, y);
1262 }
1263
1264
1265 // only used for inset right now. should also be used for main text
1266 void LyXText::drawSelection(PainterInfo & pi, int x, int) const
1267 {
1268         LCursor & cur = pi.base.bv->cursor();
1269         if (!cur.selection())
1270                 return;
1271         if (!ptr_cmp(cur.text(), this))
1272                 return;
1273
1274         if (lyxerr.debugging(Debug::DEBUG)) {
1275                 lyxerr[Debug::DEBUG]
1276                         << BOOST_CURRENT_FUNCTION
1277                         << "draw selection at " << x
1278                         << endl;
1279         }
1280
1281         DocIterator beg = cur.selectionBegin();
1282         DocIterator end = cur.selectionEnd();
1283
1284         BufferView & bv = *pi.base.bv;
1285         Buffer const & buffer = *bv.buffer();
1286
1287         // the selection doesn't touch the visible screen
1288         if (bv_funcs::status(&bv, beg) == bv_funcs::CUR_BELOW
1289             || bv_funcs::status(&bv, end) == bv_funcs::CUR_ABOVE)
1290                 return;
1291
1292         Paragraph const & par1 = pars_[beg.pit()];
1293         Paragraph const & par2 = pars_[end.pit()];
1294         TextMetrics const & tm = bv.textMetrics(this);
1295         ParagraphMetrics const & pm1 = tm.parMetrics(beg.pit());
1296         ParagraphMetrics const & pm2 = tm.parMetrics(end.pit());
1297
1298         bool const above = (bv_funcs::status(&bv, beg)
1299                             == bv_funcs::CUR_ABOVE);
1300         bool const below = (bv_funcs::status(&bv, end)
1301                             == bv_funcs::CUR_BELOW);
1302         int y1,y2,x1,x2;
1303         if (above) {
1304                 y1 = 0;
1305                 y2 = 0;
1306                 x1 = 0;
1307                 x2 = tm.width();
1308         } else {
1309                 Row const & row1 = pm1.getRow(beg.pos(), beg.boundary());
1310                 y1 = bv_funcs::getPos(bv, beg, beg.boundary()).y_ - row1.ascent();
1311                 y2 = y1 + row1.height();
1312                 int const startx = cursorX(bv, beg.top(), beg.boundary());
1313                 if (!isRTL(buffer, par1)) {
1314                         x1 = startx;
1315                         x2 = 0 + tm.width();
1316                 }
1317                 else {
1318                         x1 = 0;
1319                         x2 = startx;
1320                 }
1321         }
1322
1323         int Y1,Y2,X1,X2;
1324         if (below) {
1325                 Y1 = bv.workHeight();
1326                 Y2 = bv.workHeight();
1327                 X1 = 0;
1328                 X2 = tm.width();
1329         } else {
1330                 Row const & row2 = pm2.getRow(end.pos(), end.boundary());
1331                 Y1 = bv_funcs::getPos(bv, end, end.boundary()).y_ - row2.ascent();
1332                 Y2 = Y1 + row2.height();
1333                 int const endx = cursorX(bv, end.top(), end.boundary());
1334                 if (!isRTL(buffer, par2)) {
1335                         X1 = 0;
1336                         X2 = endx;
1337                 }
1338                 else {
1339                         X1 = endx;
1340                         X2 = 0 + tm.width();
1341                 }
1342         }
1343
1344         if (!above && !below && &pm1.getRow(beg.pos(), beg.boundary())
1345             == &pm2.getRow(end.pos(), end.boundary()))
1346         {
1347                 // paint only one rectangle
1348                 int const b( !isRTL(*bv.buffer(), par1) ? x + x1 : x + X1 );
1349                 int const w( !isRTL(*bv.buffer(), par1) ? X2 - x1 : x2 - X1 );
1350                 pi.pain.fillRectangle(b, y1, w, y2 - y1, LColor::selection);
1351                 return;
1352         }
1353
1354         if (lyxerr.debugging(Debug::DEBUG)) {
1355                 lyxerr[Debug::DEBUG] << " y1: " << y1 << " y2: " << y2
1356                            << "X1:" << X1 << " x2: " << X2 << " wid: " << tm.width()
1357                         << endl;
1358         }
1359
1360         // paint upper rectangle
1361         pi.pain.fillRectangle(x + x1, y1, x2 - x1, y2 - y1,
1362                                       LColor::selection);
1363         // paint bottom rectangle
1364         pi.pain.fillRectangle(x + X1, Y1, X2 - X1, Y2 - Y1,
1365                                       LColor::selection);
1366         // paint center rectangle
1367         pi.pain.fillRectangle(x, y2, tm.width(),
1368                               Y1 - y2, LColor::selection);
1369 }
1370
1371
1372 bool LyXText::isLastRow(pit_type pit, Row const & row) const
1373 {
1374         return row.endpos() >= pars_[pit].size()
1375                 && pit + 1 == pit_type(paragraphs().size());
1376 }
1377
1378
1379 bool LyXText::isFirstRow(pit_type pit, Row const & row) const
1380 {
1381         return row.pos() == 0 && pit == 0;
1382 }
1383
1384
1385 void LyXText::getWord(CursorSlice & from, CursorSlice & to,
1386         word_location const loc)
1387 {
1388         Paragraph const & from_par = pars_[from.pit()];
1389         switch (loc) {
1390         case WHOLE_WORD_STRICT:
1391                 if (from.pos() == 0 || from.pos() == from_par.size()
1392                     || !from_par.isLetter(from.pos())
1393                     || !from_par.isLetter(from.pos() - 1)) {
1394                         to = from;
1395                         return;
1396                 }
1397                 // no break here, we go to the next
1398
1399         case WHOLE_WORD:
1400                 // If we are already at the beginning of a word, do nothing
1401                 if (!from.pos() || !from_par.isLetter(from.pos() - 1))
1402                         break;
1403                 // no break here, we go to the next
1404
1405         case PREVIOUS_WORD:
1406                 // always move the cursor to the beginning of previous word
1407                 while (from.pos() && from_par.isLetter(from.pos() - 1))
1408                         --from.pos();
1409                 break;
1410         case NEXT_WORD:
1411                 lyxerr << "LyXText::getWord: NEXT_WORD not implemented yet"
1412                        << endl;
1413                 break;
1414         case PARTIAL_WORD:
1415                 // no need to move the 'from' cursor
1416                 break;
1417         }
1418         to = from;
1419         Paragraph & to_par = pars_[to.pit()];
1420         while (to.pos() < to_par.size() && to_par.isLetter(to.pos()))
1421                 ++to.pos();
1422 }
1423
1424
1425 void LyXText::write(Buffer const & buf, std::ostream & os) const
1426 {
1427         ParagraphList::const_iterator pit = paragraphs().begin();
1428         ParagraphList::const_iterator end = paragraphs().end();
1429         depth_type dth = 0;
1430         for (; pit != end; ++pit)
1431                 pit->write(buf, os, buf.params(), dth);
1432 }
1433
1434
1435 bool LyXText::read(Buffer const & buf, LyXLex & lex, ErrorList & errorList)
1436 {
1437         depth_type depth = 0;
1438
1439         while (lex.isOK()) {
1440                 lex.nextToken();
1441                 string const token = lex.getString();
1442
1443                 if (token.empty())
1444                         continue;
1445
1446                 if (token == "\\end_inset")
1447                         break;
1448
1449                 if (token == "\\end_body")
1450                         continue;
1451
1452                 if (token == "\\begin_body")
1453                         continue;
1454
1455                 if (token == "\\end_document")
1456                         return false;
1457
1458                 if (token == "\\begin_layout") {
1459                         lex.pushToken(token);
1460
1461                         Paragraph par;
1462                         par.params().depth(depth);
1463                         par.setFont(0, LyXFont(LyXFont::ALL_INHERIT, buf.params().language));
1464                         pars_.push_back(par);
1465
1466                         // FIXME: goddamn InsetTabular makes us pass a Buffer
1467                         // not BufferParams
1468                         lyx::readParagraph(buf, pars_.back(), lex, errorList);
1469
1470                 } else if (token == "\\begin_deeper") {
1471                         ++depth;
1472                 } else if (token == "\\end_deeper") {
1473                         if (!depth) {
1474                                 lex.printError("\\end_deeper: " "depth is already null");
1475                         } else {
1476                                 --depth;
1477                         }
1478                 } else {
1479                         lyxerr << "Handling unknown body token: `"
1480                                << token << '\'' << endl;
1481                 }
1482         }
1483         return true;
1484 }
1485
1486 int LyXText::cursorX(BufferView const & bv, CursorSlice const & sl,
1487                 bool boundary) const
1488 {
1489         TextMetrics const & tm = bv.textMetrics(sl.text());
1490         pit_type const pit = sl.pit();
1491         Paragraph const & par = pars_[pit];
1492         ParagraphMetrics const & pm = tm.parMetrics(pit);
1493         if (pm.rows().empty())
1494                 return 0;
1495
1496         pos_type ppos = sl.pos();
1497         // Correct position in front of big insets
1498         bool const boundary_correction = ppos != 0 && boundary;
1499         if (boundary_correction)
1500                 --ppos;
1501
1502         Row const & row = pm.getRow(sl.pos(), boundary);
1503
1504         pos_type cursor_vpos = 0;
1505
1506         Buffer const & buffer = *bv.buffer();
1507         RowMetrics const m = tm.computeRowMetrics(pit, row);
1508         double x = m.x;
1509
1510         pos_type const row_pos  = row.pos();
1511         pos_type const end      = row.endpos();
1512
1513         if (end <= row_pos)
1514                 cursor_vpos = row_pos;
1515         else if (ppos >= end)
1516                 cursor_vpos = isRTL(buffer, par) ? row_pos : end;
1517         else if (ppos > row_pos && ppos >= end)
1518                 // Place cursor after char at (logical) position pos - 1
1519                 cursor_vpos = (bidi.level(ppos - 1) % 2 == 0)
1520                         ? bidi.log2vis(ppos - 1) + 1 : bidi.log2vis(ppos - 1);
1521         else
1522                 // Place cursor before char at (logical) position ppos
1523                 cursor_vpos = (bidi.level(ppos) % 2 == 0)
1524                         ? bidi.log2vis(ppos) : bidi.log2vis(ppos) + 1;
1525
1526         pos_type body_pos = par.beginOfBody();
1527         if (body_pos > 0 &&
1528             (body_pos > end || !par.isLineSeparator(body_pos - 1)))
1529                 body_pos = 0;
1530
1531         // Use font span to speed things up, see below
1532         FontSpan font_span;
1533         LyXFont font;
1534         FontMetrics const & labelfm = theFontMetrics(
1535                 getLabelFont(buffer, par));
1536
1537         for (pos_type vpos = row_pos; vpos < cursor_vpos; ++vpos) {
1538                 pos_type pos = bidi.vis2log(vpos);
1539                 if (body_pos > 0 && pos == body_pos - 1) {
1540                         // FIXME UNICODE
1541                         docstring const lsep = from_utf8(par.layout()->labelsep);
1542                         x += m.label_hfill + labelfm.width(lsep);
1543                         if (par.isLineSeparator(body_pos - 1))
1544                                 x -= singleWidth(buffer, par, body_pos - 1);
1545                 }
1546
1547                 // Use font span to speed things up, see above
1548                 if (pos < font_span.first || pos > font_span.last) {
1549                         font_span = par.fontSpan(pos);
1550                         font = getFont(buffer, par, pos);
1551                 }
1552
1553                 x += singleWidth(par, pos, par.getChar(pos), font);
1554
1555                 if (par.hfillExpansion(row, pos))
1556                         x += (pos >= body_pos) ? m.hfill : m.label_hfill;
1557                 else if (par.isSeparator(pos) && pos >= body_pos)
1558                         x += m.separator;
1559         }
1560
1561         // see correction above
1562         if (boundary_correction)
1563                 if (getFont(buffer, par, ppos).isVisibleRightToLeft())
1564                         x -= singleWidth(buffer, par, ppos);
1565                 else
1566                         x += singleWidth(buffer, par, ppos);
1567
1568         // Make sure inside an inset we always count from the left
1569         // edge (bidi!) -- MV
1570         if (sl.pos() < par.size()) {
1571                 font = getFont(buffer, par, sl.pos());
1572                 if (!boundary && font.isVisibleRightToLeft()
1573                   && par.isInset(sl.pos()))
1574                         x -= par.getInset(sl.pos())->width();
1575         }
1576         return int(x);
1577 }
1578
1579
1580 int LyXText::cursorY(BufferView const & bv, CursorSlice const & sl, bool boundary) const
1581 {
1582         //lyxerr << "LyXText::cursorY: boundary: " << boundary << std::endl;
1583         ParagraphMetrics const & pm = bv.parMetrics(this, sl.pit());
1584         if (pm.rows().empty())
1585                 return 0;
1586
1587         int h = 0;
1588         h -= bv.parMetrics(this, 0).rows()[0].ascent();
1589         for (pit_type pit = 0; pit < sl.pit(); ++pit) {
1590                 h += bv.parMetrics(this, pit).height();
1591         }
1592         int pos = sl.pos();
1593         if (pos && boundary)
1594                 --pos;
1595         size_t const rend = pm.pos2row(pos);
1596         for (size_t rit = 0; rit != rend; ++rit)
1597                 h += pm.rows()[rit].height();
1598         h += pm.rows()[rend].ascent();
1599         return h;
1600 }
1601
1602
1603 // Returns the current font and depth as a message.
1604 docstring LyXText::currentState(LCursor & cur)
1605 {
1606         BOOST_ASSERT(this == cur.text());
1607         Buffer & buf = cur.buffer();
1608         Paragraph const & par = cur.paragraph();
1609         odocstringstream os;
1610
1611         if (buf.params().trackChanges)
1612                 os << _("[Change Tracking] ");
1613
1614         Change change = par.lookupChange(cur.pos());
1615
1616         if (change.type != Change::UNCHANGED) {
1617                 Author const & a = buf.params().authors().get(change.author);
1618                 os << _("Change: ") << a.name();
1619                 if (!a.email().empty())
1620                         os << " (" << a.email() << ")";
1621                 // FIXME ctime is english, we should translate that
1622                 os << _(" at ") << ctime(&change.changetime);
1623                 os << " : ";
1624         }
1625
1626         // I think we should only show changes from the default
1627         // font. (Asger)
1628         // No, from the document font (MV)
1629         LyXFont font = real_current_font;
1630         font.reduce(buf.params().getFont());
1631
1632         os << bformat(_("Font: %1$s"), font.stateText(&buf.params()));
1633
1634         // The paragraph depth
1635         int depth = cur.paragraph().getDepth();
1636         if (depth > 0)
1637                 os << bformat(_(", Depth: %1$d"), depth);
1638
1639         // The paragraph spacing, but only if different from
1640         // buffer spacing.
1641         Spacing const & spacing = par.params().spacing();
1642         if (!spacing.isDefault()) {
1643                 os << _(", Spacing: ");
1644                 switch (spacing.getSpace()) {
1645                 case Spacing::Single:
1646                         os << _("Single");
1647                         break;
1648                 case Spacing::Onehalf:
1649                         os << _("OneHalf");
1650                         break;
1651                 case Spacing::Double:
1652                         os << _("Double");
1653                         break;
1654                 case Spacing::Other:
1655                         os << _("Other (") << from_ascii(spacing.getValueAsString()) << ')';
1656                         break;
1657                 case Spacing::Default:
1658                         // should never happen, do nothing
1659                         break;
1660                 }
1661         }
1662
1663 #ifdef DEVEL_VERSION
1664         os << _(", Inset: ") << &cur.inset();
1665         os << _(", Paragraph: ") << cur.pit();
1666         os << _(", Id: ") << par.id();
1667         os << _(", Position: ") << cur.pos();
1668         os << _(", Boundary: ") << cur.boundary();
1669 //      Row & row = cur.textRow();
1670 //      os << bformat(_(", Row b:%1$d e:%2$d"), row.pos(), row.endpos());
1671 #endif
1672         return os.str();
1673 }
1674
1675
1676 docstring LyXText::getPossibleLabel(LCursor & cur) const
1677 {
1678         pit_type pit = cur.pit();
1679
1680         LyXLayout_ptr layout = pars_[pit].layout();
1681
1682         if (layout->latextype == LATEX_PARAGRAPH && pit != 0) {
1683                 LyXLayout_ptr const & layout2 = pars_[pit - 1].layout();
1684                 if (layout2->latextype != LATEX_PARAGRAPH) {
1685                         --pit;
1686                         layout = layout2;
1687                 }
1688         }
1689
1690         docstring name = from_ascii(layout->latexname());
1691
1692         // for captions, we want the abbreviation of the float type
1693         if (layout->labeltype == LABEL_SENSITIVE) {
1694                 // Search for the first float or wrap inset in the iterator
1695                 for (int i = cur.depth(); --i >= 0; ) {
1696                         InsetBase * const in = &cur[i].inset();
1697                         if (in->lyxCode() == InsetBase::FLOAT_CODE
1698                             || in->lyxCode() == InsetBase::WRAP_CODE) {
1699                                 name = in->getInsetName();
1700                                 break;
1701                         }
1702                 }
1703         }
1704
1705         docstring text = name.substr(0, 3);
1706         if (name == "theorem")
1707                 text = from_ascii("thm"); // Create a correct prefix for prettyref
1708
1709         text += ':';
1710         if (layout->latextype == LATEX_PARAGRAPH || lyxrc.label_init_length < 0)
1711                 text.erase();
1712
1713         docstring par_text = pars_[pit].asString(cur.buffer(), false);
1714         for (int i = 0; i < lyxrc.label_init_length; ++i) {
1715                 if (par_text.empty())
1716                         break;
1717                 docstring head;
1718                 par_text = split(par_text, head, ' ');
1719                 // Is it legal to use spaces in labels ?
1720                 if (i > 0)
1721                         text += '-';
1722                 text += head;
1723         }
1724
1725         return text;
1726 }
1727
1728
1729 void LyXText::setCursorFromCoordinates(LCursor & cur, int const x, int const y)
1730 {
1731         BOOST_ASSERT(this == cur.text());
1732         pit_type pit = getPitNearY(cur.bv(), y);
1733
1734         TextMetrics const & tm = cur.bv().textMetrics(this);
1735         ParagraphMetrics const & pm = tm.parMetrics(pit);
1736
1737         int yy = cur.bv().coordCache().get(this, pit).y_ - pm.ascent();
1738         lyxerr[Debug::DEBUG]
1739                 << BOOST_CURRENT_FUNCTION
1740                 << ": x: " << x
1741                 << " y: " << y
1742                 << " pit: " << pit
1743                 << " yy: " << yy << endl;
1744
1745         int r = 0;
1746         BOOST_ASSERT(pm.rows().size());
1747         for (; r < int(pm.rows().size()) - 1; ++r) {
1748                 Row const & row = pm.rows()[r];
1749                 if (int(yy + row.height()) > y)
1750                         break;
1751                 yy += row.height();
1752         }
1753
1754         Row const & row = pm.rows()[r];
1755
1756         lyxerr[Debug::DEBUG]
1757                 << BOOST_CURRENT_FUNCTION
1758                 << ": row " << r
1759                 << " from pos: " << row.pos()
1760                 << endl;
1761
1762         bool bound = false;
1763         int xx = x;
1764         pos_type const pos = row.pos() 
1765                 + tm.getColumnNearX(pit, row, xx, bound);
1766
1767         lyxerr[Debug::DEBUG]
1768                 << BOOST_CURRENT_FUNCTION
1769                 << ": setting cursor pit: " << pit
1770                 << " pos: " << pos
1771                 << endl;
1772
1773         setCursor(cur, pit, pos, true, bound);
1774         // remember new position.
1775         cur.setTargetX();
1776 }
1777
1778
1779 void LyXText::charsTranspose(LCursor & cur)
1780 {
1781         BOOST_ASSERT(this == cur.text());
1782
1783         pos_type pos = cur.pos();
1784
1785         // If cursor is at beginning or end of paragraph, do nothing.
1786         if (pos == cur.lastpos() || pos == 0)
1787                 return;
1788
1789         Paragraph & par = cur.paragraph();
1790
1791         // Get the positions of the characters to be transposed. 
1792         pos_type pos1 = pos - 1;
1793         pos_type pos2 = pos;
1794
1795         // In change tracking mode, ignore deleted characters.
1796         while (pos2 < cur.lastpos() && par.isDeleted(pos2))
1797                 ++pos2;
1798         if (pos2 == cur.lastpos())
1799                 return;
1800
1801         while (pos1 >= 0 && par.isDeleted(pos1))
1802                 --pos1;
1803         if (pos1 < 0)
1804                 return;
1805
1806         // Don't do anything if one of the "characters" is not regular text.
1807         if (par.isInset(pos1) || par.isInset(pos2))
1808                 return;
1809
1810         // Store the characters to be transposed (including font information).
1811         char_type char1 = par.getChar(pos1);
1812         LyXFont const font1 =
1813                 par.getFontSettings(cur.buffer().params(), pos1);
1814         
1815         char_type char2 = par.getChar(pos2);
1816         LyXFont const font2 =
1817                 par.getFontSettings(cur.buffer().params(), pos2);
1818
1819         // And finally, we are ready to perform the transposition.
1820         // Track the changes if Change Tracking is enabled.
1821         bool const trackChanges = cur.buffer().params().trackChanges;
1822
1823         recordUndo(cur);
1824
1825         par.eraseChar(pos2, trackChanges);
1826         par.eraseChar(pos1, trackChanges);
1827         par.insertChar(pos1, char2, font2, trackChanges);
1828         par.insertChar(pos2, char1, font1, trackChanges);
1829
1830         // After the transposition, move cursor to after the transposition.
1831         setCursor(cur, cur.pit(), pos2);
1832         cur.forwardPos();
1833 }
1834
1835
1836 } // namespace lyx