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