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