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