]> git.lyx.org Git - lyx.git/blob - src/paragraph.C
Use 'assign' as the name for the operation that opens/closes branch
[lyx.git] / src / paragraph.C
1 /**
2  * \file paragraph.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 Angus Leeming
10  * \author John Levon
11  * \author André Pönitz
12  * \author Dekel Tsur
13  * \author Jürgen Vigna
14  *
15  * Full author contact details are available in file CREDITS.
16  */
17
18 #include <config.h>
19
20 #include "paragraph.h"
21 #include "paragraph_pimpl.h"
22
23 #include "buffer.h"
24 #include "bufferparams.h"
25 #include "counters.h"
26 #include "encoding.h"
27 #include "debug.h"
28 #include "gettext.h"
29 #include "language.h"
30 #include "lyxfont.h"
31 #include "lyxrc.h"
32 #include "lyxrow.h"
33 #include "outputparams.h"
34 #include "paragraph_funcs.h"
35 #include "ParagraphList_fwd.h"
36 #include "sgml.h"
37 #include "texrow.h"
38 #include "vspace.h"
39
40 #include "insets/insetbibitem.h"
41 #include "insets/insetoptarg.h"
42
43 #include "support/lstrings.h"
44 #include "support/std_sstream.h"
45 #include "support/textutils.h"
46 #include "support/tostr.h"
47
48 #include <boost/tuple/tuple.hpp>
49 #include <boost/bind.hpp>
50
51 #include <list>
52 #include <stack>
53
54 using lyx::pos_type;
55
56 using lyx::support::contains;
57 using lyx::support::subst;
58
59 using std::distance;
60 using std::endl;
61 using std::list;
62 using std::stack;
63 using std::string;
64 using std::ostream;
65 using std::ostringstream;
66
67
68 ParagraphList::ParagraphList()
69 {}
70
71
72 Paragraph::Paragraph()
73         : y(0), height(0), begin_of_body_(0),
74           pimpl_(new Paragraph::Pimpl(this))
75 {
76         //lyxerr << "sizeof Paragraph::Pimpl: " << sizeof(Paragraph::Pimpl) << endl;
77         itemdepth = 0;
78         params().clear();
79 }
80
81
82 Paragraph::Paragraph(Paragraph const & par)
83         :       itemdepth(par.itemdepth), insetlist(par.insetlist),
84                 rows(par.rows), y(par.y), height(par.height),
85                 width(par.width), layout_(par.layout_),
86                 text_(par.text_), begin_of_body_(par.begin_of_body_),
87           pimpl_(new Paragraph::Pimpl(*par.pimpl_, this))
88 {
89         //lyxerr << "Paragraph::Paragraph(Paragraph const&)" << endl;
90         InsetList::iterator it = insetlist.begin();
91         InsetList::iterator end = insetlist.end();
92         for (; it != end; ++it)
93                 it->inset = it->inset->clone().release();
94 }
95
96
97 Paragraph & Paragraph::operator=(Paragraph const & par)
98 {
99         // needed as we will destroy the pimpl_ before copying it
100         if (&par != this) {
101                 itemdepth = par.itemdepth;
102
103                 insetlist = par.insetlist;
104                 InsetList::iterator it = insetlist.begin();
105                 InsetList::iterator end = insetlist.end();
106                 for (; it != end; ++it)
107                         it->inset = it->inset->clone().release();
108
109                 rows = par.rows;
110                 y = par.y;
111                 height = par.height;
112                 width = par.width;
113                 layout_ = par.layout();
114                 text_ = par.text_;
115                 begin_of_body_ = par.begin_of_body_;
116
117                 delete pimpl_;
118                 pimpl_ = new Pimpl(*par.pimpl_, this);
119         }
120         return *this;
121 }
122
123
124 Paragraph::~Paragraph()
125 {
126         delete pimpl_;
127         //
128         //lyxerr << "Paragraph::paragraph_id = "
129         //       << Paragraph::paragraph_id << endl;
130 }
131
132
133 void Paragraph::write(Buffer const & buf, ostream & os,
134                           BufferParams const & bparams,
135                           depth_type & dth) const
136 {
137         // The beginning or end of a deeper (i.e. nested) area?
138         if (dth != params().depth()) {
139                 if (params().depth() > dth) {
140                         while (params().depth() > dth) {
141                                 os << "\n\\begin_deeper ";
142                                 ++dth;
143                         }
144                 } else {
145                         while (params().depth() < dth) {
146                                 os << "\n\\end_deeper ";
147                                 --dth;
148                         }
149                 }
150         }
151
152         // First write the layout
153         os << "\n\\begin_layout " << layout()->name() << '\n';
154
155         params().write(os);
156
157         LyXFont font1(LyXFont::ALL_INHERIT, bparams.language);
158
159         Change running_change = Change(Change::UNCHANGED);
160         lyx::time_type const curtime(lyx::current_time());
161
162         int column = 0;
163         for (pos_type i = 0; i < size(); ++i) {
164                 if (!i) {
165                         os << '\n';
166                         column = 0;
167                 }
168
169                 Change change = pimpl_->lookupChangeFull(i);
170                 Changes::lyxMarkChange(os, column, curtime, running_change, change);
171                 running_change = change;
172
173                 // Write font changes
174                 LyXFont font2 = getFontSettings(bparams, i);
175                 if (font2 != font1) {
176                         font2.lyxWriteChanges(font1, os);
177                         column = 0;
178                         font1 = font2;
179                 }
180
181                 value_type const c = getChar(i);
182                 switch (c) {
183                 case META_INSET:
184                 {
185                         InsetBase const * inset = getInset(i);
186                         if (inset)
187                                 if (inset->directWrite()) {
188                                         // international char, let it write
189                                         // code directly so it's shorter in
190                                         // the file
191                                         inset->write(buf, os);
192                                 } else {
193                                         os << "\n\\begin_inset ";
194                                         inset->write(buf, os);
195                                         os << "\n\\end_inset \n\n";
196                                         column = 0;
197                                 }
198                 }
199                 break;
200                 case '\\':
201                         os << "\n\\backslash \n";
202                         column = 0;
203                         break;
204                 case '.':
205                         if (i + 1 < size() && getChar(i + 1) == ' ') {
206                                 os << ".\n";
207                                 column = 0;
208                         } else
209                                 os << '.';
210                         break;
211                 default:
212                         if ((column > 70 && c == ' ')
213                             || column > 79) {
214                                 os << '\n';
215                                 column = 0;
216                         }
217                         // this check is to amend a bug. LyX sometimes
218                         // inserts '\0' this could cause problems.
219                         if (c != '\0')
220                                 os << c;
221                         else
222                                 lyxerr << "ERROR (Paragraph::writeFile):"
223                                         " NULL char in structure." << endl;
224                         ++column;
225                         break;
226                 }
227         }
228
229         // to make reading work properly
230         if (!size()) {
231                 running_change = pimpl_->lookupChange(0);
232                 Changes::lyxMarkChange(os, column, curtime,
233                         Change(Change::UNCHANGED), running_change);
234         }
235         Changes::lyxMarkChange(os, column, curtime,
236                 running_change, Change(Change::UNCHANGED));
237
238         os << "\n\\end_layout\n";
239 }
240
241
242 void Paragraph::validate(LaTeXFeatures & features) const
243 {
244         pimpl_->validate(features, *layout());
245 }
246
247
248 void Paragraph::eraseIntern(lyx::pos_type pos)
249 {
250         pimpl_->eraseIntern(pos);
251 }
252
253
254 bool Paragraph::erase(pos_type pos)
255 {
256         return pimpl_->erase(pos);
257 }
258
259
260 int Paragraph::erase(pos_type start, pos_type end)
261 {
262         return pimpl_->erase(start, end);
263 }
264
265
266 void Paragraph::insert(pos_type start, string const & str,
267                        LyXFont const & font)
268 {
269         int size = str.size();
270         for (int i = 0 ; i < size ; ++i)
271                 insertChar(start + i, str[i], font);
272 }
273
274
275 bool Paragraph::checkInsertChar(LyXFont &)
276 {
277         return true;
278 }
279
280
281 void Paragraph::insertChar(pos_type pos, Paragraph::value_type c)
282 {
283         insertChar(pos, c, LyXFont(LyXFont::ALL_INHERIT));
284 }
285
286
287 void Paragraph::insertChar(pos_type pos, Paragraph::value_type c,
288                            LyXFont const & font, Change change)
289 {
290         pimpl_->insertChar(pos, c, font, change);
291 }
292
293
294 void Paragraph::insertInset(pos_type pos, InsetBase * inset)
295 {
296         insertInset(pos, inset, LyXFont(LyXFont::ALL_INHERIT));
297 }
298
299
300 void Paragraph::insertInset(pos_type pos, InsetBase * inset,
301         LyXFont const & font, Change change)
302 {
303         pimpl_->insertInset(pos, inset, font, change);
304 }
305
306
307 bool Paragraph::insetAllowed(InsetOld_code code)
308 {
309         //lyxerr << "Paragraph::InsertInsetAllowed" << endl;
310         if (pimpl_->inset_owner)
311                 return pimpl_->inset_owner->insetAllowed(code);
312         return true;
313 }
314
315
316 InsetBase * Paragraph::getInset(pos_type pos)
317 {
318         BOOST_ASSERT(pos < size());
319         return insetlist.get(pos);
320 }
321
322
323 InsetBase const * Paragraph::getInset(pos_type pos) const
324 {
325         BOOST_ASSERT(pos < size());
326         return insetlist.get(pos);
327 }
328
329
330 // Gets uninstantiated font setting at position.
331 LyXFont const Paragraph::getFontSettings(BufferParams const & bparams,
332                                          pos_type pos) const
333 {
334         if (pos > size()) {
335                 lyxerr << " pos: " << pos << " size: " << size() << endl;
336                 BOOST_ASSERT(pos <= size());
337         }
338
339         Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
340         Pimpl::FontList::const_iterator end = pimpl_->fontlist.end();
341         for (; cit != end; ++cit)
342                 if (cit->pos() >= pos)
343                         break;
344
345         if (cit != end)
346                 return cit->font();
347
348         if (pos == size() && !empty())
349                 return getFontSettings(bparams, pos - 1);
350
351         return LyXFont(LyXFont::ALL_INHERIT, getParLanguage(bparams));
352 }
353
354
355 lyx::pos_type Paragraph::getEndPosOfFontSpan(lyx::pos_type pos) const
356 {
357         BOOST_ASSERT(pos <= size());
358
359         Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
360         Pimpl::FontList::const_iterator end = pimpl_->fontlist.end();
361         for (; cit != end; ++cit)
362                 if (cit->pos() >= pos)
363                         return cit->pos();
364
365         // This should not happen, but if so, we take no chances.
366         //lyxerr << "Paragraph::getEndPosOfFontSpan: This should not happen!"
367         //      << endl;
368         return pos;
369 }
370
371
372 // Gets uninstantiated font setting at position 0
373 LyXFont const Paragraph::getFirstFontSettings() const
374 {
375         if (!empty() && !pimpl_->fontlist.empty())
376                 return pimpl_->fontlist[0].font();
377
378         return LyXFont(LyXFont::ALL_INHERIT);
379 }
380
381
382 // Gets the fully instantiated font at a given position in a paragraph
383 // This is basically the same function as LyXText::GetFont() in text2.C.
384 // The difference is that this one is used for generating the LaTeX file,
385 // and thus cosmetic "improvements" are disallowed: This has to deliver
386 // the true picture of the buffer. (Asger)
387 LyXFont const Paragraph::getFont(BufferParams const & bparams, pos_type pos,
388                                  LyXFont const & outerfont) const
389 {
390         BOOST_ASSERT(pos >= 0);
391
392         LyXLayout_ptr const & lout = layout();
393
394         pos_type const body_pos = beginOfBody();
395
396         LyXFont layoutfont;
397         if (pos < body_pos)
398                 layoutfont = lout->labelfont;
399         else
400                 layoutfont = lout->font;
401
402         LyXFont font = getFontSettings(bparams, pos);
403         font.realize(layoutfont);
404         font.realize(outerfont);
405         font.realize(bparams.getLyXTextClass().defaultfont());
406
407         return font;
408 }
409
410
411 LyXFont const Paragraph::getLabelFont(BufferParams const & bparams,
412                                       LyXFont const & outerfont) const
413 {
414         LyXFont tmpfont = layout()->labelfont;
415         tmpfont.setLanguage(getParLanguage(bparams));
416         tmpfont.realize(outerfont);
417         tmpfont.realize(bparams.getLyXTextClass().defaultfont());
418         return tmpfont;
419 }
420
421
422 LyXFont const Paragraph::getLayoutFont(BufferParams const & bparams,
423                                        LyXFont const & outerfont) const
424 {
425         LyXFont tmpfont = layout()->font;
426         tmpfont.setLanguage(getParLanguage(bparams));
427         tmpfont.realize(outerfont);
428         tmpfont.realize(bparams.getLyXTextClass().defaultfont());
429         return tmpfont;
430 }
431
432
433 /// Returns the height of the highest font in range
434 LyXFont_size
435 Paragraph::highestFontInRange(pos_type startpos, pos_type endpos,
436                               LyXFont_size def_size) const
437 {
438         if (pimpl_->fontlist.empty())
439                 return def_size;
440
441         Pimpl::FontList::const_iterator end_it = pimpl_->fontlist.begin();
442         Pimpl::FontList::const_iterator const end = pimpl_->fontlist.end();
443         for (; end_it != end; ++end_it) {
444                 if (end_it->pos() >= endpos)
445                         break;
446         }
447
448         if (end_it != end)
449                 ++end_it;
450
451         Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
452         for (; cit != end; ++cit) {
453                 if (cit->pos() >= startpos)
454                         break;
455         }
456
457         LyXFont::FONT_SIZE maxsize = LyXFont::SIZE_TINY;
458         for (; cit != end_it; ++cit) {
459                 LyXFont::FONT_SIZE size = cit->font().size();
460                 if (size == LyXFont::INHERIT_SIZE)
461                         size = def_size;
462                 if (size > maxsize && size <= LyXFont::SIZE_HUGER)
463                         maxsize = size;
464         }
465         return maxsize;
466 }
467
468
469 Paragraph::value_type
470 Paragraph::getUChar(BufferParams const & bparams, pos_type pos) const
471 {
472         value_type c = getChar(pos);
473         if (!lyxrc.rtl_support)
474                 return c;
475
476         value_type uc = c;
477         switch (c) {
478         case '(':
479                 uc = ')';
480                 break;
481         case ')':
482                 uc = '(';
483                 break;
484         case '[':
485                 uc = ']';
486                 break;
487         case ']':
488                 uc = '[';
489                 break;
490         case '{':
491                 uc = '}';
492                 break;
493         case '}':
494                 uc = '{';
495                 break;
496         case '<':
497                 uc = '>';
498                 break;
499         case '>':
500                 uc = '<';
501                 break;
502         }
503         if (uc != c && getFontSettings(bparams, pos).isRightToLeft())
504                 return uc;
505         else
506                 return c;
507 }
508
509
510 void Paragraph::setFont(pos_type pos, LyXFont const & font)
511 {
512         BOOST_ASSERT(pos <= size());
513
514         // First, reduce font against layout/label font
515         // Update: The setCharFont() routine in text2.C already
516         // reduces font, so we don't need to do that here. (Asger)
517         // No need to simplify this because it will disappear
518         // in a new kernel. (Asger)
519         // Next search font table
520
521         Pimpl::FontList::iterator beg = pimpl_->fontlist.begin();
522         Pimpl::FontList::iterator it = beg;
523         Pimpl::FontList::iterator endit = pimpl_->fontlist.end();
524         for (; it != endit; ++it) {
525                 if (it->pos() >= pos)
526                         break;
527         }
528         unsigned int i = distance(beg, it);
529         bool notfound = (it == endit);
530
531         if (!notfound && pimpl_->fontlist[i].font() == font)
532                 return;
533
534         bool begin = pos == 0 || notfound ||
535                 (i > 0 && pimpl_->fontlist[i - 1].pos() == pos - 1);
536         // Is position pos is a beginning of a font block?
537         bool end = !notfound && pimpl_->fontlist[i].pos() == pos;
538         // Is position pos is the end of a font block?
539         if (begin && end) { // A single char block
540                 if (i + 1 < pimpl_->fontlist.size() &&
541                     pimpl_->fontlist[i + 1].font() == font) {
542                         // Merge the singleton block with the next block
543                         pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i);
544                         if (i > 0 && pimpl_->fontlist[i - 1].font() == font)
545                                 pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i - 1);
546                 } else if (i > 0 && pimpl_->fontlist[i - 1].font() == font) {
547                         // Merge the singleton block with the previous block
548                         pimpl_->fontlist[i - 1].pos(pos);
549                         pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i);
550                 } else
551                         pimpl_->fontlist[i].font(font);
552         } else if (begin) {
553                 if (i > 0 && pimpl_->fontlist[i - 1].font() == font)
554                         pimpl_->fontlist[i - 1].pos(pos);
555                 else
556                         pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i,
557                                         Pimpl::FontTable(pos, font));
558         } else if (end) {
559                 pimpl_->fontlist[i].pos(pos - 1);
560                 if (!(i + 1 < pimpl_->fontlist.size() &&
561                       pimpl_->fontlist[i + 1].font() == font))
562                         pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i + 1,
563                                         Pimpl::FontTable(pos, font));
564         } else { // The general case. The block is splitted into 3 blocks
565                 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i,
566                                 Pimpl::FontTable(pos - 1, pimpl_->fontlist[i].font()));
567                 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i + 1,
568                                 Pimpl::FontTable(pos, font));
569         }
570 }
571
572
573 void Paragraph::makeSameLayout(Paragraph const & par)
574 {
575         layout(par.layout());
576         // move to pimpl?
577         params() = par.params();
578 }
579
580
581 int Paragraph::stripLeadingSpaces()
582 {
583         if (isFreeSpacing())
584                 return 0;
585
586         int i = 0;
587         while (!empty() && (isNewline(0) || isLineSeparator(0))) {
588                 pimpl_->eraseIntern(0);
589                 ++i;
590         }
591
592         return i;
593 }
594
595
596 bool Paragraph::hasSameLayout(Paragraph const & par) const
597 {
598         return par.layout() == layout() && params().sameLayout(par.params());
599 }
600
601
602 Paragraph::depth_type Paragraph::getDepth() const
603 {
604         return params().depth();
605 }
606
607
608 Paragraph::depth_type Paragraph::getMaxDepthAfter() const
609 {
610         if (layout()->isEnvironment())
611                 return params().depth() + 1;
612         else
613                 return params().depth();
614 }
615
616
617 char Paragraph::getAlign() const
618 {
619         return params().align();
620 }
621
622
623 string const & Paragraph::getLabelstring() const
624 {
625         return params().labelString();
626 }
627
628
629 // the next two functions are for the manual labels
630 string const Paragraph::getLabelWidthString() const
631 {
632         if (!params().labelWidthString().empty())
633                 return params().labelWidthString();
634         else
635                 return _("Senseless with this layout!");
636 }
637
638
639 void Paragraph::setLabelWidthString(string const & s)
640 {
641         params().labelWidthString(s);
642 }
643
644
645 void Paragraph::applyLayout(LyXLayout_ptr const & new_layout)
646 {
647         layout(new_layout);
648         params().labelWidthString(string());
649         params().align(LYX_ALIGN_LAYOUT);
650         params().spacing(Spacing(Spacing::Default));
651 }
652
653
654 pos_type Paragraph::beginOfBody() const
655 {
656         return begin_of_body_;
657 }
658
659
660 void Paragraph::setBeginOfBody()
661 {
662         if (layout()->labeltype != LABEL_MANUAL) {
663                 begin_of_body_ = 0;
664                 return;
665         }
666
667         // Unroll the first two cycles of the loop
668         // and remember the previous character to
669         // remove unnecessary getChar() calls
670         pos_type i = 0;
671         pos_type end = size();
672         if (i < end && !isNewline(i)) {
673                 ++i;
674                 char previous_char = 0;
675                 char temp = 0;
676                 if (i < end) {
677                         previous_char = text_[i];
678                         if (!isNewline(i)) {
679                                 ++i;
680                                 while (i < end && previous_char != ' ') {
681                                         temp = text_[i];
682                                         if (isNewline(i))
683                                                 break;
684                                         ++i;
685                                         previous_char = temp;
686                                 }
687                         }
688                 }
689         }
690
691         begin_of_body_ = i;
692 }
693
694
695 // returns -1 if inset not found
696 int Paragraph::getPositionOfInset(InsetBase const * inset) const
697 {
698         // Find the entry.
699         InsetList::const_iterator it = insetlist.begin();
700         InsetList::const_iterator end = insetlist.end();
701         for (; it != end; ++it)
702                 if (it->inset == inset)
703                         return it->pos;
704         return -1;
705 }
706
707
708 InsetBibitem * Paragraph::bibitem() const
709 {
710         if (!insetlist.empty()) {
711                 InsetBase * inset = insetlist.begin()->inset;
712                 if (inset->lyxCode() == InsetBase::BIBTEX_CODE)
713                         return static_cast<InsetBibitem *>(inset);
714         }
715         return 0;
716 }
717
718
719 namespace {
720
721 // paragraphs inside floats need different alignment tags to avoid
722 // unwanted space
723
724 bool noTrivlistCentering(UpdatableInset const * inset)
725 {
726         if (inset) {
727                 InsetBase::Code const code = inset->lyxCode();
728                 return code == InsetBase::FLOAT_CODE ||
729                         code == InsetBase::WRAP_CODE;
730         }
731         return false;
732 }
733
734
735 string correction(string const & orig)
736 {
737         if (orig == "flushleft")
738                 return "raggedright";
739         if (orig == "flushright")
740                 return "raggedleft";
741         if (orig == "center")
742                 return "centering";
743         return orig;
744 }
745
746
747 string const corrected_env(string const & suffix, string const & env,
748                            UpdatableInset const * inset)
749 {
750         string output = suffix + "{";
751         if (noTrivlistCentering(inset))
752                 output += correction(env);
753         else
754                 output += env;
755         return output + "}";
756 }
757
758 } // namespace anon
759
760
761 // This could go to ParagraphParameters if we want to
762 int Paragraph::startTeXParParams(BufferParams const & bparams,
763                                  ostream & os, bool moving_arg) const
764 {
765         int column = 0;
766
767         if (params().noindent()) {
768                 os << "\\noindent ";
769                 column += 10;
770         }
771
772         switch (params().align()) {
773         case LYX_ALIGN_NONE:
774         case LYX_ALIGN_BLOCK:
775         case LYX_ALIGN_LAYOUT:
776         case LYX_ALIGN_SPECIAL:
777                 break;
778         case LYX_ALIGN_LEFT:
779         case LYX_ALIGN_RIGHT:
780         case LYX_ALIGN_CENTER:
781                 if (moving_arg) {
782                         os << "\\protect";
783                         column = 8;
784                 }
785                 break;
786         }
787
788         switch (params().align()) {
789         case LYX_ALIGN_NONE:
790         case LYX_ALIGN_BLOCK:
791         case LYX_ALIGN_LAYOUT:
792         case LYX_ALIGN_SPECIAL:
793                 break;
794         case LYX_ALIGN_LEFT: {
795                 string output;
796                 UpdatableInset const * const inset = pimpl_->inset_owner;
797                 if (getParLanguage(bparams)->babel() != "hebrew")
798                         output = corrected_env("\\begin", "flushleft", inset);
799                 else
800                         output = corrected_env("\\begin", "flushright", inset);
801                 os << output;
802                 column += output.size();
803                 break;
804         } case LYX_ALIGN_RIGHT: {
805                 string output;
806                 UpdatableInset const * const inset = pimpl_->inset_owner;
807                 if (getParLanguage(bparams)->babel() != "hebrew")
808                         output = corrected_env("\\begin", "flushright", inset);
809                 else
810                         output = corrected_env("\\begin", "flushleft", inset);
811                 os << output;
812                 column += output.size();
813                 break;
814         } case LYX_ALIGN_CENTER: {
815                 string output;
816                 output = corrected_env("\\begin", "center", pimpl_->inset_owner);
817                 os << output;
818                 column += output.size();
819                 break;
820         }
821         }
822
823         return column;
824 }
825
826
827 // This could go to ParagraphParameters if we want to
828 int Paragraph::endTeXParParams(BufferParams const & bparams,
829                                ostream & os, bool moving_arg) const
830 {
831         int column = 0;
832
833         switch (params().align()) {
834         case LYX_ALIGN_NONE:
835         case LYX_ALIGN_BLOCK:
836         case LYX_ALIGN_LAYOUT:
837         case LYX_ALIGN_SPECIAL:
838                 break;
839         case LYX_ALIGN_LEFT:
840         case LYX_ALIGN_RIGHT:
841         case LYX_ALIGN_CENTER:
842                 if (moving_arg) {
843                         os << "\\protect";
844                         column = 8;
845                 }
846                 break;
847         }
848
849         switch (params().align()) {
850         case LYX_ALIGN_NONE:
851         case LYX_ALIGN_BLOCK:
852         case LYX_ALIGN_LAYOUT:
853         case LYX_ALIGN_SPECIAL:
854                 break;
855         case LYX_ALIGN_LEFT: {
856                 string output;
857                 UpdatableInset const * const inset = pimpl_->inset_owner;
858                 if (getParLanguage(bparams)->babel() != "hebrew")
859                         output = corrected_env("\\par\\end", "flushleft", inset);
860                 else
861                         output = corrected_env("\\par\\end", "flushright", inset);
862                 os << output;
863                 column += output.size();
864                 break;
865         } case LYX_ALIGN_RIGHT: {
866                 string output;
867                 UpdatableInset const * const inset = pimpl_->inset_owner;
868                 if (getParLanguage(bparams)->babel() != "hebrew")
869                         output = corrected_env("\\par\\end", "flushright", inset);
870                 else
871                         output = corrected_env("\\par\\end", "flushleft", inset);
872                 os << output;
873                 column += output.size();
874                 break;
875         } case LYX_ALIGN_CENTER: {
876                 string output;
877                 output = corrected_env("\\par\\end", "center", pimpl_->inset_owner);
878                 os << output;
879                 column += output.size();
880                 break;
881         }
882         }
883
884         return column;
885 }
886
887
888 // This one spits out the text of the paragraph
889 bool Paragraph::simpleTeXOnePar(Buffer const & buf,
890                                 BufferParams const & bparams,
891                                 LyXFont const & outerfont,
892                                 ostream & os, TexRow & texrow,
893                                 OutputParams const & runparams)
894 {
895         lyxerr[Debug::LATEX] << "SimpleTeXOnePar...     " << this << endl;
896
897         bool return_value = false;
898
899         LyXLayout_ptr style;
900
901         // well we have to check if we are in an inset with unlimited
902         // length (all in one row) if that is true then we don't allow
903         // any special options in the paragraph and also we don't allow
904         // any environment other then "Standard" to be valid!
905         bool asdefault =
906                 (inInset() && inInset()->forceDefaultParagraphs(inInset()));
907
908         if (asdefault) {
909                 style = bparams.getLyXTextClass().defaultLayout();
910         } else {
911                 style = layout();
912         }
913
914         LyXFont basefont;
915
916         // Maybe we have to create a optional argument.
917         pos_type body_pos = beginOfBody();
918         unsigned int column = 0;
919
920         if (body_pos > 0) {
921                 os << '[';
922                 ++column;
923                 basefont = getLabelFont(bparams, outerfont);
924         } else {
925                 basefont = getLayoutFont(bparams, outerfont);
926         }
927
928         bool moving_arg = runparams.moving_arg;
929         moving_arg |= style->needprotect;
930
931         // Which font is currently active?
932         LyXFont running_font(basefont);
933         // Do we have an open font change?
934         bool open_font = false;
935
936         Change::Type running_change = Change::UNCHANGED;
937
938         texrow.start(id(), 0);
939
940         // if the paragraph is empty, the loop will not be entered at all
941         if (empty()) {
942                 if (style->isCommand()) {
943                         os << '{';
944                         ++column;
945                 }
946                 if (!asdefault)
947                         column += startTeXParParams(bparams, os, moving_arg);
948         }
949
950         for (pos_type i = 0; i < size(); ++i) {
951                 ++column;
952                 // First char in paragraph or after label?
953                 if (i == body_pos) {
954                         if (body_pos > 0) {
955                                 if (open_font) {
956                                         column += running_font.latexWriteEndChanges(os, basefont, basefont);
957                                         open_font = false;
958                                 }
959                                 basefont = getLayoutFont(bparams, outerfont);
960                                 running_font = basefont;
961                                 os << ']';
962                                 ++column;
963                         }
964                         if (style->isCommand()) {
965                                 os << '{';
966                                 ++column;
967                         }
968
969                         if (!asdefault)
970                                 column += startTeXParParams(bparams, os,
971                                                             moving_arg);
972                 }
973
974                 value_type c = getChar(i);
975
976                 // Fully instantiated font
977                 LyXFont font = getFont(bparams, i, outerfont);
978
979                 LyXFont const last_font = running_font;
980
981                 // Spaces at end of font change are simulated to be
982                 // outside font change, i.e. we write "\textXX{text} "
983                 // rather than "\textXX{text }". (Asger)
984                 if (open_font && c == ' ' && i <= size() - 2) {
985                         LyXFont const & next_font = getFont(bparams, i + 1, outerfont);
986                         if (next_font != running_font && next_font != font) {
987                                 font = next_font;
988                         }
989                 }
990
991                 // We end font definition before blanks
992                 if (open_font &&
993                     (font != running_font ||
994                      font.language() != running_font.language()))
995                 {
996                         column += running_font.latexWriteEndChanges(os,
997                                                                     basefont,
998                                                                     (i == body_pos-1) ? basefont : font);
999                         running_font = basefont;
1000                         open_font = false;
1001                 }
1002
1003                 // Blanks are printed before start of fontswitch
1004                 if (c == ' ') {
1005                         // Do not print the separation of the optional argument
1006                         if (i != body_pos - 1) {
1007                                 pimpl_->simpleTeXBlanks(os, texrow, i,
1008                                                        column, font, *style);
1009                         }
1010                 }
1011
1012                 // Do we need to change font?
1013                 if ((font != running_font ||
1014                      font.language() != running_font.language()) &&
1015                         i != body_pos - 1)
1016                 {
1017                         column += font.latexWriteStartChanges(os, basefont,
1018                                                               last_font);
1019                         running_font = font;
1020                         open_font = true;
1021                 }
1022
1023                 Change::Type change = pimpl_->lookupChange(i);
1024
1025                 column += Changes::latexMarkChange(os, running_change, change);
1026                 running_change = change;
1027
1028                 OutputParams rp = runparams;
1029                 rp.moving_arg = moving_arg;
1030                 rp.free_spacing = style->free_spacing;
1031                 pimpl_->simpleTeXSpecialChars(buf, bparams,
1032                                               os, texrow, runparams,
1033                                               font, running_font,
1034                                               basefont, outerfont, open_font,
1035                                               running_change,
1036                                               *style, i, column, c);
1037         }
1038
1039         column += Changes::latexMarkChange(os,
1040                         running_change, Change::UNCHANGED);
1041
1042         // If we have an open font definition, we have to close it
1043         if (open_font) {
1044 #ifdef FIXED_LANGUAGE_END_DETECTION
1045                 if (next_) {
1046                         running_font
1047                                 .latexWriteEndChanges(os, basefont,
1048                                                       next_->getFont(bparams,
1049                                                       0, outerfont));
1050                 } else {
1051                         running_font.latexWriteEndChanges(os, basefont,
1052                                                           basefont);
1053                 }
1054 #else
1055 #ifdef WITH_WARNINGS
1056 //#warning For now we ALWAYS have to close the foreign font settings if they are
1057 //#warning there as we start another \selectlanguage with the next paragraph if
1058 //#warning we are in need of this. This should be fixed sometime (Jug)
1059 #endif
1060                 running_font.latexWriteEndChanges(os, basefont,  basefont);
1061 #endif
1062         }
1063
1064         // Needed if there is an optional argument but no contents.
1065         if (body_pos > 0 && body_pos == size()) {
1066                 os << "]~";
1067                 return_value = false;
1068         }
1069
1070         if (!asdefault) {
1071                 column += endTeXParParams(bparams, os, moving_arg);
1072         }
1073
1074         lyxerr[Debug::LATEX] << "SimpleTeXOnePar...done " << this << endl;
1075         return return_value;
1076 }
1077
1078
1079 namespace {
1080
1081 // checks, if newcol chars should be put into this line
1082 // writes newline, if necessary.
1083 void sgmlLineBreak(ostream & os, string::size_type & colcount,
1084                           string::size_type newcol)
1085 {
1086         colcount += newcol;
1087         if (colcount > lyxrc.ascii_linelen) {
1088                 os << "\n";
1089                 colcount = newcol; // assume write after this call
1090         }
1091 }
1092
1093 enum PAR_TAG {
1094         PAR_NONE=0,
1095         TT = 1,
1096         SF = 2,
1097         BF = 4,
1098         IT = 8,
1099         SL = 16,
1100         EM = 32
1101 };
1102
1103
1104 string tag_name(PAR_TAG const & pt) {
1105         switch (pt) {
1106         case PAR_NONE: return "!-- --";
1107         case TT: return "tt";
1108         case SF: return "sf";
1109         case BF: return "bf";
1110         case IT: return "it";
1111         case SL: return "sl";
1112         case EM: return "em";
1113         }
1114         return "";
1115 }
1116
1117
1118 inline
1119 void operator|=(PAR_TAG & p1, PAR_TAG const & p2)
1120 {
1121         p1 = static_cast<PAR_TAG>(p1 | p2);
1122 }
1123
1124
1125 inline
1126 void reset(PAR_TAG & p1, PAR_TAG const & p2)
1127 {
1128         p1 = static_cast<PAR_TAG>(p1 & ~p2);
1129 }
1130
1131 } // anon
1132
1133
1134 // Handle internal paragraph parsing -- layout already processed.
1135 void Paragraph::simpleLinuxDocOnePar(Buffer const & buf,
1136                                      ostream & os,
1137                                      LyXFont const & outerfont,
1138                                      OutputParams const & runparams,
1139                                      lyx::depth_type /*depth*/) const
1140 {
1141         LyXLayout_ptr const & style = layout();
1142
1143         string::size_type char_line_count = 5;     // Heuristic choice ;-)
1144
1145         // gets paragraph main font
1146         LyXFont font_old;
1147         bool desc_on;
1148         if (style->labeltype == LABEL_MANUAL) {
1149                 font_old = style->labelfont;
1150                 desc_on = true;
1151         } else {
1152                 font_old = style->font;
1153                 desc_on = false;
1154         }
1155
1156         LyXFont::FONT_FAMILY family_type = LyXFont::ROMAN_FAMILY;
1157         LyXFont::FONT_SERIES series_type = LyXFont::MEDIUM_SERIES;
1158         LyXFont::FONT_SHAPE  shape_type  = LyXFont::UP_SHAPE;
1159         bool is_em = false;
1160
1161         stack<PAR_TAG> tag_state;
1162         // parsing main loop
1163         for (pos_type i = 0; i < size(); ++i) {
1164
1165                 PAR_TAG tag_close = PAR_NONE;
1166                 list < PAR_TAG > tag_open;
1167
1168                 LyXFont const font = getFont(buf.params(), i, outerfont);
1169
1170                 if (font_old.family() != font.family()) {
1171                         switch (family_type) {
1172                         case LyXFont::SANS_FAMILY:
1173                                 tag_close |= SF;
1174                                 break;
1175                         case LyXFont::TYPEWRITER_FAMILY:
1176                                 tag_close |= TT;
1177                                 break;
1178                         default:
1179                                 break;
1180                         }
1181
1182                         family_type = font.family();
1183
1184                         switch (family_type) {
1185                         case LyXFont::SANS_FAMILY:
1186                                 tag_open.push_back(SF);
1187                                 break;
1188                         case LyXFont::TYPEWRITER_FAMILY:
1189                                 tag_open.push_back(TT);
1190                                 break;
1191                         default:
1192                                 break;
1193                         }
1194                 }
1195
1196                 if (font_old.series() != font.series()) {
1197                         switch (series_type) {
1198                         case LyXFont::BOLD_SERIES:
1199                                 tag_close |= BF;
1200                                 break;
1201                         default:
1202                                 break;
1203                         }
1204
1205                         series_type = font.series();
1206
1207                         switch (series_type) {
1208                         case LyXFont::BOLD_SERIES:
1209                                 tag_open.push_back(BF);
1210                                 break;
1211                         default:
1212                                 break;
1213                         }
1214
1215                 }
1216
1217                 if (font_old.shape() != font.shape()) {
1218                         switch (shape_type) {
1219                         case LyXFont::ITALIC_SHAPE:
1220                                 tag_close |= IT;
1221                                 break;
1222                         case LyXFont::SLANTED_SHAPE:
1223                                 tag_close |= SL;
1224                                 break;
1225                         default:
1226                                 break;
1227                         }
1228
1229                         shape_type = font.shape();
1230
1231                         switch (shape_type) {
1232                         case LyXFont::ITALIC_SHAPE:
1233                                 tag_open.push_back(IT);
1234                                 break;
1235                         case LyXFont::SLANTED_SHAPE:
1236                                 tag_open.push_back(SL);
1237                                 break;
1238                         default:
1239                                 break;
1240                         }
1241                 }
1242                 // handle <em> tag
1243                 if (font_old.emph() != font.emph()) {
1244                         if (font.emph() == LyXFont::ON) {
1245                                 tag_open.push_back(EM);
1246                                 is_em = true;
1247                         }
1248                         else if (is_em) {
1249                                 tag_close |= EM;
1250                                 is_em = false;
1251                         }
1252                 }
1253
1254                 list < PAR_TAG > temp;
1255                 while (!tag_state.empty() && tag_close) {
1256                         PAR_TAG k =  tag_state.top();
1257                         tag_state.pop();
1258                         os << "</" << tag_name(k) << '>';
1259                         if (tag_close & k)
1260                                 reset(tag_close,k);
1261                         else
1262                                 temp.push_back(k);
1263                 }
1264
1265                 for(list< PAR_TAG >::const_iterator j = temp.begin();
1266                     j != temp.end(); ++j) {
1267                         tag_state.push(*j);
1268                         os << '<' << tag_name(*j) << '>';
1269                 }
1270
1271                 for(list< PAR_TAG >::const_iterator j = tag_open.begin();
1272                     j != tag_open.end(); ++j) {
1273                         tag_state.push(*j);
1274                         os << '<' << tag_name(*j) << '>';
1275                 }
1276
1277                 char c = getChar(i);
1278
1279
1280                 if (c == Paragraph::META_INSET) {
1281                         getInset(i)->linuxdoc(buf, os, runparams);
1282                         font_old = font;
1283                         continue;
1284                 }
1285
1286                 if (style->latexparam() == "CDATA") {
1287                         // "TeX"-Mode on == > SGML-Mode on.
1288                         if (c != '\0')
1289                                 os << c;
1290                         ++char_line_count;
1291                 } else {
1292                         bool ws;
1293                         string str;
1294                         boost::tie(ws, str) = sgml::escapeChar(c);
1295                         if (ws && !isFreeSpacing()) {
1296                                 // in freespacing mode, spaces are
1297                                 // non-breaking characters
1298                                 if (desc_on) { // if char is ' ' then...
1299                                         ++char_line_count;
1300                                         sgmlLineBreak(os, char_line_count, 6);
1301                                         os << "</tag>";
1302                                         desc_on = false;
1303                                 } else  {
1304                                         sgmlLineBreak(os, char_line_count, 1);
1305                                         os << c;
1306                                 }
1307                         } else {
1308                                 os << str;
1309                                 char_line_count += str.length();
1310                         }
1311                 }
1312                 font_old = font;
1313         }
1314
1315         while (!tag_state.empty()) {
1316                 os << "</" << tag_name(tag_state.top()) << '>';
1317                 tag_state.pop();
1318         }
1319
1320         // resets description flag correctly
1321         if (desc_on) {
1322                 // <tag> not closed...
1323                 sgmlLineBreak(os, char_line_count, 6);
1324                 os << "</tag>";
1325         }
1326 }
1327
1328
1329 void Paragraph::simpleDocBookOnePar(Buffer const & buf,
1330                                     ostream & os,
1331                                     LyXFont const & outerfont,
1332                                     OutputParams const & runparams,
1333                                     lyx::depth_type depth,
1334                                     bool labelid) const
1335 {
1336         bool emph_flag = false;
1337
1338         LyXLayout_ptr const & style = layout();
1339         LyXLayout_ptr const & defaultstyle =
1340                 buf.params().getLyXTextClass().defaultLayout();
1341
1342         LyXFont font_old =
1343                 style->labeltype == LABEL_MANUAL ? style->labelfont : style->font;
1344
1345         int char_line_count = depth;
1346         bool label_closed = true;
1347         bool para_closed = true;
1348
1349         if (style->latextype == LATEX_ITEM_ENVIRONMENT) {
1350                 string ls = "";
1351                 Counters & counters = buf.params().getLyXTextClass().counters();
1352                 if (!style->free_spacing)
1353                         os << string(depth,' ');
1354                 if (!style->labeltag().empty()) {
1355                         os << "<" << style->labeltag() << ">\n";
1356                         label_closed = false;
1357                 } else {
1358                         if (!defaultstyle->latexparam().empty()) {
1359                                 counters.step("para");
1360                                 ls = tostr(counters.value("para"));
1361                                 ls = " id=\""
1362                                         + subst(defaultstyle->latexparam(), "#", ls) + '"';
1363                         }
1364                         os << "<" << style->itemtag() << ">\n"
1365                            << string(depth, ' ') << "<"
1366                            << defaultstyle->latexname() << ls << ">\n";
1367                         para_closed = false;
1368                 }
1369         }
1370
1371         // parsing main loop
1372         for (pos_type i = 0; i < size(); ++i) {
1373                 LyXFont font = getFont(buf.params(), i, outerfont);
1374
1375                 // handle <emphasis> tag
1376                 if (font_old.emph() != font.emph()) {
1377                         if (font.emph() == LyXFont::ON) {
1378                                 if (style->latexparam() == "CDATA")
1379                                         os << "]]>";
1380                                 os << "<emphasis>";
1381                                 if (style->latexparam() == "CDATA")
1382                                         os << "<![CDATA[";
1383                                 emph_flag = true;
1384                         } else if (i) {
1385                                 if (style->latexparam() == "CDATA")
1386                                         os << "]]>";
1387                                 os << "</emphasis>";
1388                                 if (style->latexparam() == "CDATA")
1389                                         os << "<![CDATA[";
1390                                 emph_flag = false;
1391                         }
1392                 }
1393
1394                 if (isInset(i)) {
1395                         InsetBase const * inset = getInset(i);
1396                         // don't print the inset in position 0 if desc_on == 3 (label)
1397                         //if (i || desc_on != 3) {
1398                         if (!labelid) {
1399                                 if (style->latexparam() == "CDATA")
1400                                         os << "]]>";
1401                                 inset->docbook(buf, os, runparams);
1402                                 if (style->latexparam() == "CDATA")
1403                                         os << "<![CDATA[";
1404                         }
1405                 } else {
1406                         char c = getChar(i);
1407                         bool ws;
1408                         string str;
1409                         boost::tie(ws, str) = sgml::escapeChar(c);
1410
1411                         if (style->pass_thru) {
1412                                 os << c;
1413                         } else if (isFreeSpacing() || c != ' ') {
1414                                         os << str;
1415                         } else if (!style->labeltag().empty() && !label_closed) {
1416                                 ++char_line_count;
1417                                 os << "\n</" << style->labeltag() << "><"
1418                                    << style->itemtag() << "><"
1419                                    << defaultstyle->latexname() << ">";
1420                                 label_closed = true;
1421                                 para_closed = false;
1422                         } else {
1423                                 os << ' ';
1424                         }
1425                 }
1426                 font_old = font;
1427         }
1428
1429         if (emph_flag) {
1430                 if (style->latexparam() == "CDATA")
1431                         os << "]]>";
1432                 os << "</emphasis>";
1433                 if (style->latexparam() == "CDATA")
1434                         os << "<![CDATA[";
1435         }
1436
1437         // resets description flag correctly
1438         if (!label_closed) {
1439                 // <term> not closed...
1440                 os << "</" << style->labeltag() << ">\n<"
1441                    << style->itemtag() << "><"
1442                    << defaultstyle->latexname() << ">&nbsp;";
1443         }
1444         if (!para_closed) {
1445                 os << "\n" << string(depth, ' ') << "</"
1446                    << defaultstyle->latexname() << ">\n";
1447         }
1448         if (style->free_spacing)
1449                 os << '\n';
1450 }
1451
1452
1453 namespace {
1454
1455 /// return true if the char is a meta-character for an inset
1456 inline
1457 bool IsInsetChar(char c)
1458 {
1459         return (c == Paragraph::META_INSET);
1460 }
1461
1462 } // namespace anon
1463
1464
1465
1466 bool Paragraph::isHfill(pos_type pos) const
1467 {
1468         return
1469                 isInset(pos) && getInset(pos)->lyxCode() == InsetBase::HFILL_CODE;
1470 }
1471
1472
1473 bool Paragraph::isNewline(pos_type pos) const
1474 {
1475         return
1476                 isInset(pos) && getInset(pos)->lyxCode() == InsetBase::NEWLINE_CODE;
1477 }
1478
1479
1480 bool Paragraph::isSeparator(pos_type pos) const
1481 {
1482         return IsSeparatorChar(getChar(pos));
1483 }
1484
1485
1486 bool Paragraph::isLineSeparator(pos_type pos) const
1487 {
1488         value_type const c = getChar(pos);
1489         return IsLineSeparatorChar(c)
1490                 || (IsInsetChar(c) && getInset(pos) &&
1491                 getInset(pos)->isLineSeparator());
1492 }
1493
1494
1495 bool Paragraph::isKomma(pos_type pos) const
1496 {
1497         return IsKommaChar(getChar(pos));
1498 }
1499
1500
1501 /// Used by the spellchecker
1502 bool Paragraph::isLetter(pos_type pos) const
1503 {
1504         value_type const c = getChar(pos);
1505         if (IsLetterChar(c))
1506                 return true;
1507         if (isInset(pos))
1508                 return getInset(pos)->isLetter();
1509         // We want to pass the ' and escape chars to ispell
1510         string const extra = lyxrc.isp_esc_chars + '\'';
1511         return contains(extra, c);
1512 }
1513
1514
1515 bool Paragraph::isWord(pos_type pos) const
1516 {
1517         unsigned char const c = getChar(pos);
1518         return !(IsSeparatorChar(c)
1519                   || IsKommaChar(c)
1520                   || IsInsetChar(c));
1521 }
1522
1523
1524 Language const *
1525 Paragraph::getParLanguage(BufferParams const & bparams) const
1526 {
1527         if (!empty())
1528                 return getFirstFontSettings().language();
1529 #ifdef WITH_WARNINGS
1530 #warning FIXME we should check the prev par as well (Lgb)
1531 #endif
1532         return bparams.language;
1533 }
1534
1535
1536 bool Paragraph::isRightToLeftPar(BufferParams const & bparams) const
1537 {
1538         return lyxrc.rtl_support
1539                 && getParLanguage(bparams)->RightToLeft()
1540                 && !(inInset() && inInset()->lyxCode() == InsetBase::ERT_CODE);
1541 }
1542
1543
1544 void Paragraph::changeLanguage(BufferParams const & bparams,
1545                                Language const * from, Language const * to)
1546 {
1547         for (pos_type i = 0; i < size(); ++i) {
1548                 LyXFont font = getFontSettings(bparams, i);
1549                 if (font.language() == from) {
1550                         font.setLanguage(to);
1551                         setFont(i, font);
1552                 }
1553         }
1554 }
1555
1556
1557 bool Paragraph::isMultiLingual(BufferParams const & bparams) const
1558 {
1559         Language const * doc_language = bparams.language;
1560         Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
1561         Pimpl::FontList::const_iterator end = pimpl_->fontlist.end();
1562
1563         for (; cit != end; ++cit)
1564                 if (cit->font().language() != ignore_language &&
1565                     cit->font().language() != latex_language &&
1566                     cit->font().language() != doc_language)
1567                         return true;
1568         return false;
1569 }
1570
1571
1572 // Convert the paragraph to a string.
1573 // Used for building the table of contents
1574 string const Paragraph::asString(Buffer const & buffer,
1575                                  bool label) const
1576 {
1577         OutputParams runparams;
1578         return asString(buffer, runparams, label);
1579 }
1580
1581
1582 string const Paragraph::asString(Buffer const & buffer,
1583                                  OutputParams const & runparams,
1584                                  bool label) const
1585 {
1586 #if 0
1587         string s;
1588         if (label && !params().labelString().empty())
1589                 s += params().labelString() + ' ';
1590
1591         for (pos_type i = 0; i < size(); ++i) {
1592                 value_type c = getChar(i);
1593                 if (IsPrintable(c))
1594                         s += c;
1595                 else if (c == META_INSET &&
1596                          getInset(i)->lyxCode() == InsetBase::MATH_CODE) {
1597                         ostringstream os;
1598                         getInset(i)->plaintext(buffer, os, runparams);
1599                         s += subst(STRCONV(os.str()),'\n',' ');
1600                 }
1601         }
1602
1603         return s;
1604 #else
1605         // This should really be done by the caller and not here.
1606         string ret = asString(buffer, runparams, 0, size(), label);
1607         return subst(ret, '\n', ' ');
1608 #endif
1609 }
1610
1611
1612 string const Paragraph::asString(Buffer const & buffer,
1613                                  pos_type beg, pos_type end, bool label) const
1614 {
1615
1616         OutputParams const runparams;
1617         return asString(buffer, runparams, beg, end, label);
1618 }
1619
1620
1621 string const Paragraph::asString(Buffer const & buffer,
1622                                  OutputParams const & runparams,
1623                                  pos_type beg, pos_type end, bool label) const
1624 {
1625         ostringstream os;
1626
1627         if (beg == 0 && label && !params().labelString().empty())
1628                 os << params().labelString() << ' ';
1629
1630         for (pos_type i = beg; i < end; ++i) {
1631                 value_type const c = getUChar(buffer.params(), i);
1632                 if (IsPrintable(c))
1633                         os << c;
1634                 else if (c == META_INSET)
1635                         getInset(i)->plaintext(buffer, os, runparams);
1636         }
1637
1638         return os.str();
1639 }
1640
1641
1642 void Paragraph::setInsetOwner(UpdatableInset * inset)
1643 {
1644         pimpl_->inset_owner = inset;
1645 }
1646
1647
1648 void Paragraph::setContentsFromPar(Paragraph const & par)
1649 {
1650         pimpl_->setContentsFromPar(par);
1651 }
1652
1653
1654 void Paragraph::trackChanges(Change::Type type)
1655 {
1656         pimpl_->trackChanges(type);
1657 }
1658
1659
1660 void Paragraph::untrackChanges()
1661 {
1662         pimpl_->untrackChanges();
1663 }
1664
1665
1666 void Paragraph::cleanChanges()
1667 {
1668         pimpl_->cleanChanges();
1669 }
1670
1671
1672 Change::Type Paragraph::lookupChange(lyx::pos_type pos) const
1673 {
1674         BOOST_ASSERT(!size() || pos < size());
1675         return pimpl_->lookupChange(pos);
1676 }
1677
1678
1679 Change const Paragraph::lookupChangeFull(lyx::pos_type pos) const
1680 {
1681         BOOST_ASSERT(!size() || pos < size());
1682         return pimpl_->lookupChangeFull(pos);
1683 }
1684
1685
1686 bool Paragraph::isChanged(pos_type start, pos_type end) const
1687 {
1688         return pimpl_->isChanged(start, end);
1689 }
1690
1691
1692 bool Paragraph::isChangeEdited(pos_type start, pos_type end) const
1693 {
1694         return pimpl_->isChangeEdited(start, end);
1695 }
1696
1697
1698 void Paragraph::setChange(lyx::pos_type pos, Change::Type type)
1699 {
1700         pimpl_->setChange(pos, type);
1701 }
1702
1703
1704 void Paragraph::markErased()
1705 {
1706         pimpl_->markErased();
1707 }
1708
1709
1710 void Paragraph::acceptChange(pos_type start, pos_type end)
1711 {
1712         return pimpl_->acceptChange(start, end);
1713 }
1714
1715
1716 void Paragraph::rejectChange(pos_type start, pos_type end)
1717 {
1718         return pimpl_->rejectChange(start, end);
1719 }
1720
1721
1722 Paragraph::value_type Paragraph::getChar(pos_type pos) const
1723 {
1724         // This is in the critical path!
1725         pos_type const siz = text_.size();
1726
1727         BOOST_ASSERT(0 <= pos);
1728         BOOST_ASSERT(pos <= siz);
1729
1730         if (pos == siz) {
1731                 lyxerr << "getChar() on pos " << pos << " in par id "
1732                        << id() << " of size " << siz
1733                        << "  is a bit silly !" << endl;
1734                 BOOST_ASSERT(false);
1735         }
1736
1737         return text_[pos];
1738 }
1739
1740
1741 int Paragraph::id() const
1742 {
1743         return pimpl_->id_;
1744 }
1745
1746
1747 LyXLayout_ptr const & Paragraph::layout() const
1748 {
1749 /*
1750         InsetBase * inset = inInset();
1751         if (inset && inset->lyxCode() == InsetBase::ENVIRONMENT_CODE)
1752                 return static_cast<InsetEnvironment*>(inset)->layout();
1753 */
1754         return layout_;
1755 }
1756
1757
1758 void Paragraph::layout(LyXLayout_ptr const & new_layout)
1759 {
1760         layout_ = new_layout;
1761 }
1762
1763
1764 UpdatableInset * Paragraph::inInset() const
1765 {
1766         return pimpl_->inset_owner;
1767 }
1768
1769
1770 void Paragraph::clearContents()
1771 {
1772         text_.clear();
1773 }
1774
1775
1776 void Paragraph::setChar(pos_type pos, value_type c)
1777 {
1778         text_[pos] = c;
1779 }
1780
1781
1782 ParagraphParameters & Paragraph::params()
1783 {
1784         return pimpl_->params;
1785 }
1786
1787
1788 ParagraphParameters const & Paragraph::params() const
1789 {
1790         return pimpl_->params;
1791 }
1792
1793
1794 bool Paragraph::isFreeSpacing() const
1795 {
1796         if (layout()->free_spacing)
1797                 return true;
1798
1799         // for now we just need this, later should we need this in some
1800         // other way we can always add a function to InsetBase too.
1801         if (pimpl_->inset_owner)
1802                 return pimpl_->inset_owner->lyxCode() == InsetBase::ERT_CODE;
1803         return false;
1804 }
1805
1806
1807 bool Paragraph::allowEmpty() const
1808 {
1809         if (layout()->keepempty)
1810                 return true;
1811         if (pimpl_->inset_owner)
1812                 return pimpl_->inset_owner->lyxCode() == InsetBase::ERT_CODE;
1813         return false;
1814 }
1815
1816
1817 RowList::iterator Paragraph::getRow(pos_type pos)
1818 {
1819         RowList::iterator rit = rows.end();
1820         RowList::iterator const begin = rows.begin();
1821
1822         for (--rit; rit != begin && rit->pos() > pos; --rit)
1823                 ;
1824
1825         return rit;
1826 }
1827
1828
1829 RowList::const_iterator Paragraph::getRow(pos_type pos) const
1830 {
1831         RowList::const_iterator rit = rows.end();
1832         RowList::const_iterator const begin = rows.begin();
1833
1834         for (--rit; rit != begin && rit->pos() > pos; --rit)
1835                 ;
1836
1837         return rit;
1838 }
1839
1840
1841 size_t Paragraph::row(pos_type pos) const
1842 {
1843         RowList::const_iterator rit = rows.end();
1844         RowList::const_iterator const begin = rows.begin();
1845
1846         for (--rit; rit != begin && rit->pos() > pos; --rit)
1847                 ;
1848
1849         return rit - begin;
1850 }
1851
1852
1853 unsigned char Paragraph::transformChar(unsigned char c, pos_type pos) const
1854 {
1855         if (!Encodings::is_arabic(c))
1856                 if (lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 && IsDigit(c))
1857                         return c + (0xb0 - '0');
1858                 else
1859                         return c;
1860
1861         unsigned char const prev_char = pos > 0 ? getChar(pos - 1) : ' ';
1862         unsigned char next_char = ' ';
1863
1864         for (pos_type i = pos + 1, end = size(); i < end; ++i) {
1865                 unsigned char const par_char = getChar(i);
1866                 if (!Encodings::IsComposeChar_arabic(par_char)) {
1867                         next_char = par_char;
1868                         break;
1869                 }
1870         }
1871
1872         if (Encodings::is_arabic(next_char)) {
1873                 if (Encodings::is_arabic(prev_char) &&
1874                         !Encodings::is_arabic_special(prev_char))
1875                         return Encodings::TransformChar(c, Encodings::FORM_MEDIAL);
1876                 else
1877                         return Encodings::TransformChar(c, Encodings::FORM_INITIAL);
1878         } else {
1879                 if (Encodings::is_arabic(prev_char) &&
1880                         !Encodings::is_arabic_special(prev_char))
1881                         return Encodings::TransformChar(c, Encodings::FORM_FINAL);
1882                 else
1883                         return Encodings::TransformChar(c, Encodings::FORM_ISOLATED);
1884         }
1885 }