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