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