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