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