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