]> git.lyx.org Git - lyx.git/blob - src/paragraph.C
speedup latex export a lot
[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/textutils.h"
45 #include "support/tostr.h"
46
47 #include <boost/tuple/tuple.hpp>
48 #include <boost/bind.hpp>
49
50 #include <list>
51 #include <stack>
52 #include <sstream>
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         return !pimpl_->inset_owner || pimpl_->inset_owner->insetAllowed(code);
310 }
311
312
313 InsetBase * Paragraph::getInset(pos_type pos)
314 {
315         BOOST_ASSERT(pos < size());
316         return insetlist.get(pos);
317 }
318
319
320 InsetBase const * Paragraph::getInset(pos_type pos) const
321 {
322         BOOST_ASSERT(pos < size());
323         return insetlist.get(pos);
324 }
325
326
327 // Gets uninstantiated font setting at position.
328 LyXFont const Paragraph::getFontSettings(BufferParams const & bparams,
329                                          pos_type pos) const
330 {
331         if (pos > size()) {
332                 lyxerr << " pos: " << pos << " size: " << size() << endl;
333                 BOOST_ASSERT(pos <= size());
334         }
335
336         Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
337         Pimpl::FontList::const_iterator end = pimpl_->fontlist.end();
338         for (; cit != end; ++cit)
339                 if (cit->pos() >= pos)
340                         break;
341
342         if (cit != end)
343                 return cit->font();
344
345         if (pos == size() && !empty())
346                 return getFontSettings(bparams, pos - 1);
347
348         return LyXFont(LyXFont::ALL_INHERIT, getParLanguage(bparams));
349 }
350
351
352 lyx::pos_type Paragraph::getEndPosOfFontSpan(lyx::pos_type pos) const
353 {
354         BOOST_ASSERT(pos <= size());
355
356         Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
357         Pimpl::FontList::const_iterator end = pimpl_->fontlist.end();
358         for (; cit != end; ++cit)
359                 if (cit->pos() >= pos)
360                         return cit->pos();
361
362         // This should not happen, but if so, we take no chances.
363         //lyxerr << "Paragraph::getEndPosOfFontSpan: This should not happen!"
364         //      << endl;
365         return pos;
366 }
367
368
369 // Gets uninstantiated font setting at position 0
370 LyXFont const Paragraph::getFirstFontSettings() const
371 {
372         if (!empty() && !pimpl_->fontlist.empty())
373                 return pimpl_->fontlist[0].font();
374
375         return LyXFont(LyXFont::ALL_INHERIT);
376 }
377
378
379 // Gets the fully instantiated font at a given position in a paragraph
380 // This is basically the same function as LyXText::GetFont() in text2.C.
381 // The difference is that this one is used for generating the LaTeX file,
382 // and thus cosmetic "improvements" are disallowed: This has to deliver
383 // the true picture of the buffer. (Asger)
384 LyXFont const Paragraph::getFont(BufferParams const & bparams, pos_type pos,
385                                  LyXFont const & outerfont) const
386 {
387         BOOST_ASSERT(pos >= 0);
388
389         LyXLayout_ptr const & lout = layout();
390
391         pos_type const body_pos = beginOfBody();
392
393         LyXFont layoutfont;
394         if (pos < body_pos)
395                 layoutfont = lout->labelfont;
396         else
397                 layoutfont = lout->font;
398
399         LyXFont font = getFontSettings(bparams, pos);
400         font.realize(layoutfont);
401         font.realize(outerfont);
402         font.realize(bparams.getLyXTextClass().defaultfont());
403
404         return font;
405 }
406
407
408 LyXFont const Paragraph::getLabelFont(BufferParams const & bparams,
409                                       LyXFont const & outerfont) const
410 {
411         LyXFont tmpfont = layout()->labelfont;
412         tmpfont.setLanguage(getParLanguage(bparams));
413         tmpfont.realize(outerfont);
414         tmpfont.realize(bparams.getLyXTextClass().defaultfont());
415         return tmpfont;
416 }
417
418
419 LyXFont const Paragraph::getLayoutFont(BufferParams const & bparams,
420                                        LyXFont const & outerfont) const
421 {
422         LyXFont tmpfont = layout()->font;
423         tmpfont.setLanguage(getParLanguage(bparams));
424         tmpfont.realize(outerfont);
425         tmpfont.realize(bparams.getLyXTextClass().defaultfont());
426         return tmpfont;
427 }
428
429
430 /// Returns the height of the highest font in range
431 LyXFont_size
432 Paragraph::highestFontInRange(pos_type startpos, pos_type endpos,
433                               LyXFont_size def_size) const
434 {
435         if (pimpl_->fontlist.empty())
436                 return def_size;
437
438         Pimpl::FontList::const_iterator end_it = pimpl_->fontlist.begin();
439         Pimpl::FontList::const_iterator const end = pimpl_->fontlist.end();
440         for (; end_it != end; ++end_it) {
441                 if (end_it->pos() >= endpos)
442                         break;
443         }
444
445         if (end_it != end)
446                 ++end_it;
447
448         Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
449         for (; cit != end; ++cit) {
450                 if (cit->pos() >= startpos)
451                         break;
452         }
453
454         LyXFont::FONT_SIZE maxsize = LyXFont::SIZE_TINY;
455         for (; cit != end_it; ++cit) {
456                 LyXFont::FONT_SIZE size = cit->font().size();
457                 if (size == LyXFont::INHERIT_SIZE)
458                         size = def_size;
459                 if (size > maxsize && size <= LyXFont::SIZE_HUGER)
460                         maxsize = size;
461         }
462         return maxsize;
463 }
464
465
466 Paragraph::value_type
467 Paragraph::getUChar(BufferParams const & bparams, pos_type pos) const
468 {
469         value_type c = getChar(pos);
470         if (!lyxrc.rtl_support)
471                 return c;
472
473         value_type uc = c;
474         switch (c) {
475         case '(':
476                 uc = ')';
477                 break;
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         }
500         if (uc != c && getFontSettings(bparams, pos).isRightToLeft())
501                 return uc;
502         else
503                 return c;
504 }
505
506
507 void Paragraph::setFont(pos_type pos, LyXFont const & font)
508 {
509         BOOST_ASSERT(pos <= size());
510
511         // First, reduce font against layout/label font
512         // Update: The setCharFont() routine in text2.C already
513         // reduces font, so we don't need to do that here. (Asger)
514         // No need to simplify this because it will disappear
515         // in a new kernel. (Asger)
516         // Next search font table
517
518         Pimpl::FontList::iterator beg = pimpl_->fontlist.begin();
519         Pimpl::FontList::iterator it = beg;
520         Pimpl::FontList::iterator endit = pimpl_->fontlist.end();
521         for (; it != endit; ++it) {
522                 if (it->pos() >= pos)
523                         break;
524         }
525         unsigned int i = distance(beg, it);
526         bool notfound = (it == endit);
527
528         if (!notfound && pimpl_->fontlist[i].font() == font)
529                 return;
530
531         bool begin = pos == 0 || notfound ||
532                 (i > 0 && pimpl_->fontlist[i - 1].pos() == pos - 1);
533         // Is position pos is a beginning of a font block?
534         bool end = !notfound && pimpl_->fontlist[i].pos() == pos;
535         // Is position pos is the end of a font block?
536         if (begin && end) { // A single char block
537                 if (i + 1 < pimpl_->fontlist.size() &&
538                     pimpl_->fontlist[i + 1].font() == font) {
539                         // Merge the singleton block with the next block
540                         pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i);
541                         if (i > 0 && pimpl_->fontlist[i - 1].font() == font)
542                                 pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i - 1);
543                 } else if (i > 0 && pimpl_->fontlist[i - 1].font() == font) {
544                         // Merge the singleton block with the previous block
545                         pimpl_->fontlist[i - 1].pos(pos);
546                         pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i);
547                 } else
548                         pimpl_->fontlist[i].font(font);
549         } else if (begin) {
550                 if (i > 0 && pimpl_->fontlist[i - 1].font() == font)
551                         pimpl_->fontlist[i - 1].pos(pos);
552                 else
553                         pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i,
554                                         Pimpl::FontTable(pos, font));
555         } else if (end) {
556                 pimpl_->fontlist[i].pos(pos - 1);
557                 if (!(i + 1 < pimpl_->fontlist.size() &&
558                       pimpl_->fontlist[i + 1].font() == font))
559                         pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i + 1,
560                                         Pimpl::FontTable(pos, font));
561         } else { // The general case. The block is splitted into 3 blocks
562                 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i,
563                                 Pimpl::FontTable(pos - 1, pimpl_->fontlist[i].font()));
564                 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i + 1,
565                                 Pimpl::FontTable(pos, font));
566         }
567 }
568
569
570 void Paragraph::makeSameLayout(Paragraph const & par)
571 {
572         layout(par.layout());
573         // move to pimpl?
574         params() = par.params();
575 }
576
577
578 int Paragraph::stripLeadingSpaces()
579 {
580         if (isFreeSpacing())
581                 return 0;
582
583         int i = 0;
584         while (!empty() && (isNewline(0) || isLineSeparator(0))) {
585                 pimpl_->eraseIntern(0);
586                 ++i;
587         }
588
589         return i;
590 }
591
592
593 bool Paragraph::hasSameLayout(Paragraph const & par) const
594 {
595         return par.layout() == layout() && params().sameLayout(par.params());
596 }
597
598
599 Paragraph::depth_type Paragraph::getDepth() const
600 {
601         return params().depth();
602 }
603
604
605 Paragraph::depth_type Paragraph::getMaxDepthAfter() const
606 {
607         if (layout()->isEnvironment())
608                 return params().depth() + 1;
609         else
610                 return params().depth();
611 }
612
613
614 char Paragraph::getAlign() const
615 {
616         return params().align();
617 }
618
619
620 string const & Paragraph::getLabelstring() const
621 {
622         return params().labelString();
623 }
624
625
626 // the next two functions are for the manual labels
627 string const Paragraph::getLabelWidthString() const
628 {
629         if (!params().labelWidthString().empty())
630                 return params().labelWidthString();
631         else
632                 return _("Senseless with this layout!");
633 }
634
635
636 void Paragraph::setLabelWidthString(string const & s)
637 {
638         params().labelWidthString(s);
639 }
640
641
642 void Paragraph::applyLayout(LyXLayout_ptr const & new_layout)
643 {
644         layout(new_layout);
645         params().labelWidthString(string());
646         params().align(LYX_ALIGN_LAYOUT);
647         params().spacing(Spacing(Spacing::Default));
648 }
649
650
651 pos_type Paragraph::beginOfBody() const
652 {
653         return begin_of_body_;
654 }
655
656
657 void Paragraph::setBeginOfBody()
658 {
659         if (layout()->labeltype != LABEL_MANUAL) {
660                 begin_of_body_ = 0;
661                 return;
662         }
663
664         // Unroll the first two cycles of the loop
665         // and remember the previous character to
666         // remove unnecessary getChar() calls
667         pos_type i = 0;
668         pos_type end = size();
669         if (i < end && !isNewline(i)) {
670                 ++i;
671                 char previous_char = 0;
672                 char temp = 0;
673                 if (i < end) {
674                         previous_char = text_[i];
675                         if (!isNewline(i)) {
676                                 ++i;
677                                 while (i < end && previous_char != ' ') {
678                                         temp = text_[i];
679                                         if (isNewline(i))
680                                                 break;
681                                         ++i;
682                                         previous_char = temp;
683                                 }
684                         }
685                 }
686         }
687
688         begin_of_body_ = i;
689 }
690
691
692 // returns -1 if inset not found
693 int Paragraph::getPositionOfInset(InsetBase const * inset) const
694 {
695         // Find the entry.
696         InsetList::const_iterator it = insetlist.begin();
697         InsetList::const_iterator end = insetlist.end();
698         for (; it != end; ++it)
699                 if (it->inset == inset)
700                         return it->pos;
701         return -1;
702 }
703
704
705 InsetBibitem * Paragraph::bibitem() const
706 {
707         if (!insetlist.empty()) {
708                 InsetBase * inset = insetlist.begin()->inset;
709                 if (inset->lyxCode() == InsetBase::BIBTEX_CODE)
710                         return static_cast<InsetBibitem *>(inset);
711         }
712         return 0;
713 }
714
715
716 bool Paragraph::forceDefaultParagraphs() const
717 {
718         return inInset() && inInset()->forceDefaultParagraphs(inInset());
719 }
720
721
722 bool Paragraph::autoBreakRows() const
723 {
724         return inInset() && static_cast<InsetText *>(inInset())->getAutoBreakRows();
725 }
726
727
728 namespace {
729
730 // paragraphs inside floats need different alignment tags to avoid
731 // unwanted space
732
733 bool noTrivlistCentering(InsetBase::Code code)
734 {
735         return code == InsetBase::FLOAT_CODE || code == InsetBase::WRAP_CODE;
736 }
737
738
739 string correction(string const & orig)
740 {
741         if (orig == "flushleft")
742                 return "raggedright";
743         if (orig == "flushright")
744                 return "raggedleft";
745         if (orig == "center")
746                 return "centering";
747         return orig;
748 }
749
750
751 string const corrected_env(string const & suffix, string const & env,
752         InsetBase::Code code)
753 {
754         string output = suffix + "{";
755         if (noTrivlistCentering(code))
756                 output += correction(env);
757         else
758                 output += env;
759         return output + "}";
760 }
761
762 } // namespace anon
763
764
765 // This could go to ParagraphParameters if we want to
766 int Paragraph::startTeXParParams(BufferParams const & bparams,
767                                  ostream & os, bool moving_arg) const
768 {
769         int column = 0;
770
771         if (params().noindent()) {
772                 os << "\\noindent ";
773                 column += 10;
774         }
775
776         switch (params().align()) {
777         case LYX_ALIGN_NONE:
778         case LYX_ALIGN_BLOCK:
779         case LYX_ALIGN_LAYOUT:
780         case LYX_ALIGN_SPECIAL:
781                 break;
782         case LYX_ALIGN_LEFT:
783         case LYX_ALIGN_RIGHT:
784         case LYX_ALIGN_CENTER:
785                 if (moving_arg) {
786                         os << "\\protect";
787                         column += 8;
788                 }
789                 break;
790         }
791
792         switch (params().align()) {
793         case LYX_ALIGN_NONE:
794         case LYX_ALIGN_BLOCK:
795         case LYX_ALIGN_LAYOUT:
796         case LYX_ALIGN_SPECIAL:
797                 break;
798         case LYX_ALIGN_LEFT: {
799                 string output;
800                 if (getParLanguage(bparams)->babel() != "hebrew")
801                         output = corrected_env("\\begin", "flushleft", ownerCode());
802                 else
803                         output = corrected_env("\\begin", "flushright", ownerCode());
804                 os << output;
805                 column += output.size();
806                 break;
807         } case LYX_ALIGN_RIGHT: {
808                 string output;
809                 if (getParLanguage(bparams)->babel() != "hebrew")
810                         output = corrected_env("\\begin", "flushright", ownerCode());
811                 else
812                         output = corrected_env("\\begin", "flushleft", ownerCode());
813                 os << output;
814                 column += output.size();
815                 break;
816         } case LYX_ALIGN_CENTER: {
817                 string output;
818                 output = corrected_env("\\begin", "center", ownerCode());
819                 os << output;
820                 column += output.size();
821                 break;
822         }
823         }
824
825         return column;
826 }
827
828
829 // This could go to ParagraphParameters if we want to
830 int Paragraph::endTeXParParams(BufferParams const & bparams,
831                                ostream & os, bool moving_arg) const
832 {
833         int column = 0;
834
835         switch (params().align()) {
836         case LYX_ALIGN_NONE:
837         case LYX_ALIGN_BLOCK:
838         case LYX_ALIGN_LAYOUT:
839         case LYX_ALIGN_SPECIAL:
840                 break;
841         case LYX_ALIGN_LEFT:
842         case LYX_ALIGN_RIGHT:
843         case LYX_ALIGN_CENTER:
844                 if (moving_arg) {
845                         os << "\\protect";
846                         column = 8;
847                 }
848                 break;
849         }
850
851         switch (params().align()) {
852         case LYX_ALIGN_NONE:
853         case LYX_ALIGN_BLOCK:
854         case LYX_ALIGN_LAYOUT:
855         case LYX_ALIGN_SPECIAL:
856                 break;
857         case LYX_ALIGN_LEFT: {
858                 string output;
859                 if (getParLanguage(bparams)->babel() != "hebrew")
860                         output = corrected_env("\\par\\end", "flushleft", ownerCode());
861                 else
862                         output = corrected_env("\\par\\end", "flushright", ownerCode());
863                 os << output;
864                 column += output.size();
865                 break;
866         } case LYX_ALIGN_RIGHT: {
867                 string output;
868                 if (getParLanguage(bparams)->babel() != "hebrew")
869                         output = corrected_env("\\par\\end", "flushright", ownerCode());
870                 else
871                         output = corrected_env("\\par\\end", "flushleft", ownerCode());
872                 os << output;
873                 column += output.size();
874                 break;
875         } case LYX_ALIGN_CENTER: {
876                 string output;
877                 output = corrected_env("\\par\\end", "center", ownerCode());
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) const
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 = forceDefaultParagraphs();
906
907         if (asdefault) {
908                 style = bparams.getLyXTextClass().defaultLayout();
909         } else {
910                 style = layout();
911         }
912
913         LyXFont basefont;
914
915         // Maybe we have to create a optional argument.
916         pos_type body_pos = beginOfBody();
917         unsigned int column = 0;
918
919         if (body_pos > 0) {
920                 os << '[';
921                 ++column;
922                 basefont = getLabelFont(bparams, outerfont);
923         } else {
924                 basefont = getLayoutFont(bparams, outerfont);
925         }
926
927         bool const moving_arg = runparams.moving_arg | style->needprotect;
928
929         // Which font is currently active?
930         LyXFont running_font(basefont);
931         // Do we have an open font change?
932         bool open_font = false;
933
934         Change::Type running_change = Change::UNCHANGED;
935
936         texrow.start(id(), 0);
937
938         // if the paragraph is empty, the loop will not be entered at all
939         if (empty()) {
940                 if (style->isCommand()) {
941                         os << '{';
942                         ++column;
943                 }
944                 if (!asdefault)
945                         column += startTeXParParams(bparams, os, moving_arg);
946         }
947
948         for (pos_type i = 0; i < size(); ++i) {
949                 ++column;
950                 // First char in paragraph or after label?
951                 if (i == body_pos) {
952                         if (body_pos > 0) {
953                                 if (open_font) {
954                                         column += running_font.latexWriteEndChanges(os, basefont, basefont);
955                                         open_font = false;
956                                 }
957                                 basefont = getLayoutFont(bparams, outerfont);
958                                 running_font = basefont;
959                                 os << ']';
960                                 ++column;
961                         }
962                         if (style->isCommand()) {
963                                 os << '{';
964                                 ++column;
965                         }
966
967                         if (!asdefault)
968                                 column += startTeXParParams(bparams, os,
969                                                             moving_arg);
970                 }
971
972                 value_type c = getChar(i);
973
974                 // Fully instantiated font
975                 LyXFont font = getFont(bparams, i, outerfont);
976
977                 LyXFont const last_font = running_font;
978
979                 // Spaces at end of font change are simulated to be
980                 // outside font change, i.e. we write "\textXX{text} "
981                 // rather than "\textXX{text }". (Asger)
982                 if (open_font && c == ' ' && i <= size() - 2) {
983                         LyXFont const & next_font = getFont(bparams, i + 1, outerfont);
984                         if (next_font != running_font && next_font != font) {
985                                 font = next_font;
986                         }
987                 }
988
989                 // We end font definition before blanks
990                 if (open_font &&
991                     (font != running_font ||
992                      font.language() != running_font.language()))
993                 {
994                         column += running_font.latexWriteEndChanges(os,
995                                                                     basefont,
996                                                                     (i == body_pos-1) ? basefont : font);
997                         running_font = basefont;
998                         open_font = false;
999                 }
1000
1001                 // Blanks are printed before start of fontswitch
1002                 if (c == ' ') {
1003                         // Do not print the separation of the optional argument
1004                         if (i != body_pos - 1) {
1005                                 pimpl_->simpleTeXBlanks(os, texrow, i,
1006                                                        column, font, *style);
1007                         }
1008                 }
1009
1010                 // Do we need to change font?
1011                 if ((font != running_font ||
1012                      font.language() != running_font.language()) &&
1013                         i != body_pos - 1)
1014                 {
1015                         column += font.latexWriteStartChanges(os, basefont,
1016                                                               last_font);
1017                         running_font = font;
1018                         open_font = true;
1019                 }
1020
1021                 Change::Type change = pimpl_->lookupChange(i);
1022
1023                 column += Changes::latexMarkChange(os, running_change, change);
1024                 running_change = change;
1025
1026                 OutputParams rp = runparams;
1027                 rp.moving_arg = moving_arg;
1028                 rp.free_spacing = style->free_spacing;
1029                 rp.lang = font.language()->babel();
1030                 rp.intitle = style->intitle;
1031                 pimpl_->simpleTeXSpecialChars(buf, bparams,
1032                                               os, texrow, rp,
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 string Paragraph::getDocbookId() const
1330 {
1331         for (pos_type i = 0; i < size(); ++i) {
1332                 if (isInset(i)) {
1333                         InsetBase const * inset = getInset(i);
1334                         InsetBase::Code lyx_code = inset->lyxCode();
1335                         if (lyx_code == InsetBase::LABEL_CODE) {
1336                                 return static_cast<InsetCommand const *>(inset)->getContents();
1337                         }
1338                 }
1339
1340         }
1341         return string();
1342 }
1343
1344
1345 void Paragraph::simpleDocBookOnePar(Buffer const & buf,
1346                                     ostream & os,
1347                                     LyXFont const & outerfont,
1348                                     OutputParams const & runparams,
1349                                     lyx::depth_type depth) const
1350 {
1351         bool emph_flag = false;
1352
1353         LyXLayout_ptr const & style = layout();
1354         LyXLayout_ptr const & defaultstyle =
1355                 buf.params().getLyXTextClass().defaultLayout();
1356
1357         LyXFont font_old =
1358                 style->labeltype == LABEL_MANUAL ? style->labelfont : style->font;
1359
1360         int char_line_count = depth;
1361         bool label_closed = true;
1362         bool para_closed = true;
1363
1364         if (style->latextype == LATEX_ITEM_ENVIRONMENT) {
1365                 string ls = "";
1366                 Counters & counters = buf.params().getLyXTextClass().counters();
1367                 if (!style->free_spacing)
1368                         os << string(depth,' ');
1369                 if (!style->labeltag().empty()) {
1370                         os << "<" << style->labeltag() << ">\n";
1371                         label_closed = false;
1372                 } else {
1373                         if (!defaultstyle->latexparam().empty()) {
1374                                 counters.step("para");
1375                                 ls = tostr(counters.value("para"));
1376                                 ls = " id=\""
1377                                         + subst(defaultstyle->latexparam(), "#", ls) + '"';
1378                         }
1379                         os << "<" << style->itemtag() << ">\n"
1380                            << string(depth, ' ') << "<"
1381                            << defaultstyle->latexname() << ls << ">\n";
1382                         para_closed = false;
1383                 }
1384         }
1385
1386         // parsing main loop
1387         for (pos_type i = 0; i < size(); ++i) {
1388                 LyXFont font = getFont(buf.params(), i, outerfont);
1389
1390                 // handle <emphasis> tag
1391                 if (font_old.emph() != font.emph()) {
1392                         if (font.emph() == LyXFont::ON) {
1393                                 if (style->latexparam() == "CDATA")
1394                                         os << "]]>";
1395                                 os << "<emphasis>";
1396                                 if (style->latexparam() == "CDATA")
1397                                         os << "<![CDATA[";
1398                                 emph_flag = true;
1399                         } else if (i) {
1400                                 if (style->latexparam() == "CDATA")
1401                                         os << "]]>";
1402                                 os << "</emphasis>";
1403                                 if (style->latexparam() == "CDATA")
1404                                         os << "<![CDATA[";
1405                                 emph_flag = false;
1406                         }
1407                 }
1408
1409                 if (isInset(i)) {
1410                         InsetBase const * inset = getInset(i);
1411                         if (style->latexparam() == "CDATA")
1412                                 os << "]]>";
1413                         inset->docbook(buf, os, runparams);
1414                         if (style->latexparam() == "CDATA")
1415                                 os << "<![CDATA[";
1416                 } else {
1417                         char c = getChar(i);
1418                         bool ws;
1419                         string str;
1420                         boost::tie(ws, str) = sgml::escapeChar(c);
1421
1422                         if (style->pass_thru) {
1423                                 os << c;
1424                         } else if (isFreeSpacing() || c != ' ') {
1425                                         os << str;
1426                         } else if (!style->labeltag().empty() && !label_closed) {
1427                                 ++char_line_count;
1428                                 os << "\n</" << style->labeltag() << "><"
1429                                    << style->itemtag() << "><"
1430                                    << defaultstyle->latexname() << ">";
1431                                 label_closed = true;
1432                                 para_closed = false;
1433                         } else {
1434                                 os << ' ';
1435                         }
1436                 }
1437                 font_old = font;
1438         }
1439
1440         if (emph_flag) {
1441                 if (style->latexparam() == "CDATA")
1442                         os << "]]>";
1443                 os << "</emphasis>";
1444                 if (style->latexparam() == "CDATA")
1445                         os << "<![CDATA[";
1446         }
1447
1448         // resets description flag correctly
1449         if (!label_closed) {
1450                 // <term> not closed...
1451                 os << "</" << style->labeltag() << ">\n<"
1452                    << style->itemtag() << "><"
1453                    << defaultstyle->latexname() << ">&nbsp;";
1454         }
1455         if (!para_closed) {
1456                 os << "\n" << string(depth, ' ') << "</"
1457                    << defaultstyle->latexname() << ">\n";
1458         }
1459         if (style->free_spacing)
1460                 os << '\n';
1461 }
1462
1463
1464 namespace {
1465
1466 /// return true if the char is a meta-character for an inset
1467 inline
1468 bool IsInsetChar(char c)
1469 {
1470         return (c == Paragraph::META_INSET);
1471 }
1472
1473 } // namespace anon
1474
1475
1476
1477 bool Paragraph::isHfill(pos_type pos) const
1478 {
1479         return
1480                 isInset(pos) && getInset(pos)->lyxCode() == InsetBase::HFILL_CODE;
1481 }
1482
1483
1484 bool Paragraph::isNewline(pos_type pos) const
1485 {
1486         return
1487                 isInset(pos) && getInset(pos)->lyxCode() == InsetBase::NEWLINE_CODE;
1488 }
1489
1490
1491 bool Paragraph::isSeparator(pos_type pos) const
1492 {
1493         return IsSeparatorChar(getChar(pos));
1494 }
1495
1496
1497 bool Paragraph::isLineSeparator(pos_type pos) const
1498 {
1499         value_type const c = getChar(pos);
1500         return IsLineSeparatorChar(c)
1501                 || (IsInsetChar(c) && getInset(pos) &&
1502                 getInset(pos)->isLineSeparator());
1503 }
1504
1505
1506 bool Paragraph::isKomma(pos_type pos) const
1507 {
1508         return IsKommaChar(getChar(pos));
1509 }
1510
1511
1512 /// Used by the spellchecker
1513 bool Paragraph::isLetter(pos_type pos) const
1514 {
1515         value_type const c = getChar(pos);
1516         if (IsLetterChar(c))
1517                 return true;
1518         if (isInset(pos))
1519                 return getInset(pos)->isLetter();
1520         // We want to pass the ' and escape chars to ispell
1521         string const extra = lyxrc.isp_esc_chars + '\'';
1522         return contains(extra, c);
1523 }
1524
1525
1526 bool Paragraph::isWord(pos_type pos) const
1527 {
1528         if (isInset(pos))
1529                 return getInset(pos)->isLetter();
1530         value_type const c = getChar(pos);
1531         return !(IsSeparatorChar(c)
1532                   || IsKommaChar(c)
1533                   || IsInsetChar(c));
1534 }
1535
1536
1537 Language const *
1538 Paragraph::getParLanguage(BufferParams const & bparams) const
1539 {
1540         if (!empty())
1541                 return getFirstFontSettings().language();
1542 #ifdef WITH_WARNINGS
1543 #warning FIXME we should check the prev par as well (Lgb)
1544 #endif
1545         return bparams.language;
1546 }
1547
1548
1549 bool Paragraph::isRightToLeftPar(BufferParams const & bparams) const
1550 {
1551         return lyxrc.rtl_support
1552                 && getParLanguage(bparams)->RightToLeft()
1553                 && ownerCode() != InsetBase::ERT_CODE;
1554 }
1555
1556
1557 void Paragraph::changeLanguage(BufferParams const & bparams,
1558                                Language const * from, Language const * to)
1559 {
1560         for (pos_type i = 0; i < size(); ++i) {
1561                 LyXFont font = getFontSettings(bparams, i);
1562                 if (font.language() == from) {
1563                         font.setLanguage(to);
1564                         setFont(i, font);
1565                 }
1566         }
1567 }
1568
1569
1570 bool Paragraph::isMultiLingual(BufferParams const & bparams) const
1571 {
1572         Language const * doc_language = bparams.language;
1573         Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
1574         Pimpl::FontList::const_iterator end = pimpl_->fontlist.end();
1575
1576         for (; cit != end; ++cit)
1577                 if (cit->font().language() != ignore_language &&
1578                     cit->font().language() != latex_language &&
1579                     cit->font().language() != doc_language)
1580                         return true;
1581         return false;
1582 }
1583
1584
1585 // Convert the paragraph to a string.
1586 // Used for building the table of contents
1587 string const Paragraph::asString(Buffer const & buffer, bool label) const
1588 {
1589         OutputParams runparams;
1590         return asString(buffer, runparams, label);
1591 }
1592
1593
1594 string const Paragraph::asString(Buffer const & buffer,
1595                                  OutputParams const & runparams,
1596                                  bool label) const
1597 {
1598 #if 0
1599         string s;
1600         if (label && !params().labelString().empty())
1601                 s += params().labelString() + ' ';
1602
1603         for (pos_type i = 0; i < size(); ++i) {
1604                 value_type c = getChar(i);
1605                 if (IsPrintable(c))
1606                         s += c;
1607                 else if (c == META_INSET &&
1608                          getInset(i)->lyxCode() == InsetBase::MATH_CODE) {
1609                         ostringstream os;
1610                         getInset(i)->plaintext(buffer, os, runparams);
1611                         s += subst(STRCONV(os.str()),'\n',' ');
1612                 }
1613         }
1614
1615         return s;
1616 #else
1617         // This should really be done by the caller and not here.
1618         string ret = asString(buffer, runparams, 0, size(), label);
1619         return subst(ret, '\n', ' ');
1620 #endif
1621 }
1622
1623
1624 string const Paragraph::asString(Buffer const & buffer,
1625                                  pos_type beg, pos_type end, bool label) const
1626 {
1627
1628         OutputParams const runparams;
1629         return asString(buffer, runparams, beg, end, label);
1630 }
1631
1632
1633 string const Paragraph::asString(Buffer const & buffer,
1634                                  OutputParams const & runparams,
1635                                  pos_type beg, pos_type end, bool label) const
1636 {
1637         ostringstream os;
1638
1639         if (beg == 0 && label && !params().labelString().empty())
1640                 os << params().labelString() << ' ';
1641
1642         for (pos_type i = beg; i < end; ++i) {
1643                 value_type const c = getUChar(buffer.params(), i);
1644                 if (IsPrintable(c))
1645                         os << c;
1646                 else if (c == META_INSET)
1647                         getInset(i)->plaintext(buffer, os, runparams);
1648         }
1649
1650         return os.str();
1651 }
1652
1653
1654 void Paragraph::setInsetOwner(UpdatableInset * inset)
1655 {
1656         pimpl_->inset_owner = inset;
1657 }
1658
1659
1660 void Paragraph::setContentsFromPar(Paragraph const & par)
1661 {
1662         pimpl_->setContentsFromPar(par);
1663 }
1664
1665
1666 void Paragraph::trackChanges(Change::Type type)
1667 {
1668         pimpl_->trackChanges(type);
1669 }
1670
1671
1672 void Paragraph::untrackChanges()
1673 {
1674         pimpl_->untrackChanges();
1675 }
1676
1677
1678 void Paragraph::cleanChanges()
1679 {
1680         pimpl_->cleanChanges();
1681 }
1682
1683
1684 Change::Type Paragraph::lookupChange(lyx::pos_type pos) const
1685 {
1686         BOOST_ASSERT(empty() || pos < size());
1687         return pimpl_->lookupChange(pos);
1688 }
1689
1690
1691 Change const Paragraph::lookupChangeFull(lyx::pos_type pos) const
1692 {
1693         BOOST_ASSERT(empty() || pos < size());
1694         return pimpl_->lookupChangeFull(pos);
1695 }
1696
1697
1698 bool Paragraph::isChanged(pos_type start, pos_type end) const
1699 {
1700         return pimpl_->isChanged(start, end);
1701 }
1702
1703
1704 bool Paragraph::isChangeEdited(pos_type start, pos_type end) const
1705 {
1706         return pimpl_->isChangeEdited(start, end);
1707 }
1708
1709
1710 void Paragraph::setChange(lyx::pos_type pos, Change::Type type)
1711 {
1712         pimpl_->setChange(pos, type);
1713 }
1714
1715
1716 void Paragraph::markErased()
1717 {
1718         pimpl_->markErased();
1719 }
1720
1721
1722 void Paragraph::acceptChange(pos_type start, pos_type end)
1723 {
1724         return pimpl_->acceptChange(start, end);
1725 }
1726
1727
1728 void Paragraph::rejectChange(pos_type start, pos_type end)
1729 {
1730         return pimpl_->rejectChange(start, end);
1731 }
1732
1733
1734 Paragraph::value_type Paragraph::getChar(pos_type pos) const
1735 {
1736         // This is in the critical path!
1737         pos_type const siz = text_.size();
1738
1739         BOOST_ASSERT(0 <= pos);
1740         BOOST_ASSERT(pos <= siz);
1741
1742         if (pos == siz) {
1743                 lyxerr << "getChar() on pos " << pos << " in par id "
1744                        << id() << " of size " << siz
1745                        << "  is a bit silly !" << endl;
1746                 BOOST_ASSERT(false);
1747         }
1748
1749         return text_[pos];
1750 }
1751
1752
1753 int Paragraph::id() const
1754 {
1755         return pimpl_->id_;
1756 }
1757
1758
1759 LyXLayout_ptr const & Paragraph::layout() const
1760 {
1761         return layout_;
1762 }
1763
1764
1765 void Paragraph::layout(LyXLayout_ptr const & new_layout)
1766 {
1767         layout_ = new_layout;
1768 }
1769
1770
1771 UpdatableInset * Paragraph::inInset() const
1772 {
1773         return pimpl_->inset_owner;
1774 }
1775
1776
1777 InsetBase::Code Paragraph::ownerCode() const
1778 {
1779         return pimpl_->inset_owner
1780                 ? pimpl_->inset_owner->lyxCode() : InsetBase::NO_CODE;
1781 }
1782
1783
1784 void Paragraph::clearContents()
1785 {
1786         text_.clear();
1787 }
1788
1789
1790 void Paragraph::setChar(pos_type pos, value_type c)
1791 {
1792         text_[pos] = c;
1793 }
1794
1795
1796 ParagraphParameters & Paragraph::params()
1797 {
1798         return pimpl_->params;
1799 }
1800
1801
1802 ParagraphParameters const & Paragraph::params() const
1803 {
1804         return pimpl_->params;
1805 }
1806
1807
1808 bool Paragraph::isFreeSpacing() const
1809 {
1810         if (layout()->free_spacing)
1811                 return true;
1812
1813         // for now we just need this, later should we need this in some
1814         // other way we can always add a function to InsetBase too.
1815         return ownerCode() == InsetBase::ERT_CODE;
1816 }
1817
1818
1819 bool Paragraph::allowEmpty() const
1820 {
1821         if (layout()->keepempty)
1822                 return true;
1823         return ownerCode() == InsetBase::ERT_CODE;
1824 }
1825
1826
1827 RowList::iterator Paragraph::getRow(pos_type pos)
1828 {
1829         RowList::iterator rit = rows.end();
1830         RowList::iterator const begin = rows.begin();
1831
1832         for (--rit; rit != begin && rit->pos() > pos; --rit)
1833                 ;
1834
1835         return rit;
1836 }
1837
1838
1839 RowList::const_iterator Paragraph::getRow(pos_type pos) const
1840 {
1841         RowList::const_iterator rit = rows.end();
1842         RowList::const_iterator const begin = rows.begin();
1843
1844         for (--rit; rit != begin && rit->pos() > pos; --rit)
1845                 ;
1846
1847         return rit;
1848 }
1849
1850
1851 size_t Paragraph::row(pos_type pos) const
1852 {
1853         RowList::const_iterator rit = rows.end();
1854         RowList::const_iterator const begin = rows.begin();
1855
1856         for (--rit; rit != begin && rit->pos() > pos; --rit)
1857                 ;
1858
1859         return rit - begin;
1860 }
1861
1862
1863 unsigned char Paragraph::transformChar(unsigned char c, pos_type pos) const
1864 {
1865         if (!Encodings::is_arabic(c))
1866                 if (lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 && IsDigit(c))
1867                         return c + (0xb0 - '0');
1868                 else
1869                         return c;
1870
1871         unsigned char const prev_char = pos > 0 ? getChar(pos - 1) : ' ';
1872         unsigned char next_char = ' ';
1873
1874         for (pos_type i = pos + 1, end = size(); i < end; ++i) {
1875                 unsigned char const par_char = getChar(i);
1876                 if (!Encodings::IsComposeChar_arabic(par_char)) {
1877                         next_char = par_char;
1878                         break;
1879                 }
1880         }
1881
1882         if (Encodings::is_arabic(next_char)) {
1883                 if (Encodings::is_arabic(prev_char) &&
1884                         !Encodings::is_arabic_special(prev_char))
1885                         return Encodings::TransformChar(c, Encodings::FORM_MEDIAL);
1886                 else
1887                         return Encodings::TransformChar(c, Encodings::FORM_INITIAL);
1888         } else {
1889                 if (Encodings::is_arabic(prev_char) &&
1890                         !Encodings::is_arabic_special(prev_char))
1891                         return Encodings::TransformChar(c, Encodings::FORM_FINAL);
1892                 else
1893                         return Encodings::TransformChar(c, Encodings::FORM_ISOLATED);
1894         }
1895 }