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