]> git.lyx.org Git - lyx.git/blob - src/paragraph.C
fix "height grows when moving in tables" bug
[lyx.git] / src / paragraph.C
1 /* This file is part of
2  * ======================================================
3  *
4  *           LyX, The Document Processor
5  *
6  *           Copyright 1995 Matthias Ettrich
7  *           Copyright 1995-2001 The LyX Team.
8  *
9  * ====================================================== */
10
11 #include <config.h>
12
13 #include "paragraph.h"
14 #include "paragraph_pimpl.h"
15
16 #include "buffer.h"
17 #include "bufferparams.h"
18 #include "BufferView.h"
19 #include "changes.h"
20 #include "encoding.h"
21 #include "debug.h"
22 #include "gettext.h"
23 #include "language.h"
24 #include "latexrunparams.h"
25 #include "layout.h"
26 #include "lyxrc.h"
27 #include "paragraph_funcs.h"
28 #include "ParameterStruct.h"
29 #include "texrow.h"
30
31 #include "Lsstream.h"
32
33 #include "insets/insetbibitem.h"
34 #include "insets/insetoptarg.h"
35 #include "insets/insetenv.h"
36
37 #include "support/filetools.h"
38 #include "support/lstrings.h"
39 #include "support/lyxmanip.h"
40 #include "support/FileInfo.h"
41 #include "support/LAssert.h"
42 #include "support/textutils.h"
43
44 #include <algorithm>
45 #include <fstream>
46 #include <csignal>
47 #include <ctime>
48
49 using namespace lyx::support;
50
51 using std::ostream;
52 using std::endl;
53 using std::fstream;
54 using std::ios;
55 using std::lower_bound;
56 using std::upper_bound;
57
58 using lyx::pos_type;
59
60
61 Paragraph::Paragraph()
62         : pimpl_(new Paragraph::Pimpl(this))
63 {
64         enumdepth = 0;
65         itemdepth = 0;
66         params().clear();
67 }
68
69
70 Paragraph::Paragraph(Paragraph const & lp)
71         : pimpl_(new Paragraph::Pimpl(*lp.pimpl_, this))
72 {
73         enumdepth = 0;
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                 // tell the new inset who is the boss now
87                 it->inset->parOwner(this);
88         }
89 }
90
91
92 void Paragraph::operator=(Paragraph const & lp)
93 {
94         // needed as we will destroy the pimpl_ before copying it
95         if (&lp != this)
96                 return;
97
98         lyxerr << "Paragraph::operator=()" << endl;
99
100         delete pimpl_;
101         pimpl_ = new Pimpl(*lp.pimpl_, this);
102
103         enumdepth = lp.enumdepth;
104         itemdepth = lp.itemdepth;
105         // this is because of the dummy layout of the paragraphs that
106         // follow footnotes
107         layout_ = lp.layout();
108
109         // copy everything behind the break-position to the new paragraph
110         insetlist = lp.insetlist;
111         InsetList::iterator it = insetlist.begin();
112         InsetList::iterator end = insetlist.end();
113         for (; it != end; ++it) {
114                 it->inset = static_cast<InsetOld*>(it->inset->clone().release());
115                 // tell the new inset who is the boss now
116                 it->inset->parOwner(this);
117         }
118 }
119
120 // the destructor removes the new paragraph from the list
121 Paragraph::~Paragraph()
122 {
123         delete pimpl_;
124         //
125         //lyxerr << "Paragraph::paragraph_id = "
126         //       << Paragraph::paragraph_id << endl;
127 }
128
129
130 void Paragraph::write(Buffer const * buf, ostream & os,
131                           BufferParams const & bparams,
132                           depth_type & dth) const
133 {
134         // The beginning or end of a deeper (i.e. nested) area?
135         if (dth != params().depth()) {
136                 if (params().depth() > dth) {
137                         while (params().depth() > dth) {
138                                 os << "\n\\begin_deeper ";
139                                 ++dth;
140                         }
141                 } else {
142                         while (params().depth() < dth) {
143                                 os << "\n\\end_deeper ";
144                                 --dth;
145                         }
146                 }
147         }
148
149         // First write the layout
150         os << "\n\\begin_layout " << layout()->name() << '\n';
151
152         params().write(os);
153
154         LyXFont font1(LyXFont::ALL_INHERIT, bparams.language);
155
156         Change running_change = Change(Change::UNCHANGED);
157         lyx::time_type const curtime(lyx::current_time());
158
159         int column = 0;
160         for (pos_type i = 0; i < size(); ++i) {
161                 if (!i) {
162                         os << '\n';
163                         column = 0;
164                 }
165
166                 Change change = pimpl_->lookupChangeFull(i);
167                 Changes::lyxMarkChange(os, column, curtime, running_change, change);
168                 running_change = change;
169
170                 // Write font changes
171                 LyXFont font2 = getFontSettings(bparams, i);
172                 if (font2 != font1) {
173                         font2.lyxWriteChanges(font1, os);
174                         column = 0;
175                         font1 = font2;
176                 }
177
178                 value_type const c = getChar(i);
179                 switch (c) {
180                 case META_INSET:
181                 {
182                         InsetOld const * inset = getInset(i);
183                         if (inset)
184                                 if (inset->directWrite()) {
185                                         // international char, let it write
186                                         // code directly so it's shorter in
187                                         // the file
188                                         inset->write(buf, os);
189                                 } else {
190                                         os << "\n\\begin_inset ";
191                                         inset->write(buf, os);
192                                         os << "\n\\end_inset \n\n";
193                                         column = 0;
194                                 }
195                 }
196                 break;
197                 case '\\':
198                         os << "\n\\backslash \n";
199                         column = 0;
200                         break;
201                 case '.':
202                         if (i + 1 < size() && getChar(i + 1) == ' ') {
203                                 os << ".\n";
204                                 column = 0;
205                         } else
206                                 os << '.';
207                         break;
208                 default:
209                         if ((column > 70 && c == ' ')
210                             || column > 79) {
211                                 os << '\n';
212                                 column = 0;
213                         }
214                         // this check is to amend a bug. LyX sometimes
215                         // inserts '\0' this could cause problems.
216                         if (c != '\0')
217                                 os << c;
218                         else
219                                 lyxerr << "ERROR (Paragraph::writeFile):"
220                                         " NULL char in structure." << endl;
221                         ++column;
222                         break;
223                 }
224         }
225
226         // to make reading work properly
227         if (!size()) {
228                 running_change = pimpl_->lookupChange(0);
229                 Changes::lyxMarkChange(os, column, curtime,
230                         Change(Change::UNCHANGED), running_change);
231         }
232         Changes::lyxMarkChange(os, column, curtime,
233                 running_change, Change(Change::UNCHANGED));
234
235         os << "\n\\end_layout\n";
236 }
237
238
239 void Paragraph::validate(LaTeXFeatures & features) const
240 {
241         pimpl_->validate(features, *layout());
242 }
243
244
245 void Paragraph::eraseIntern(lyx::pos_type pos)
246 {
247         pimpl_->eraseIntern(pos);
248 }
249
250
251 bool Paragraph::erase(pos_type pos)
252 {
253         return pimpl_->erase(pos);
254 }
255
256
257 int Paragraph::erase(pos_type start, pos_type end)
258 {
259         return pimpl_->erase(start, end);
260 }
261
262
263 bool Paragraph::checkInsertChar(LyXFont & font)
264 {
265         if (pimpl_->inset_owner)
266                 return pimpl_->inset_owner->checkInsertChar(font);
267         return true;
268 }
269
270
271 void Paragraph::insertChar(pos_type pos, Paragraph::value_type c)
272 {
273         insertChar(pos, c, LyXFont(LyXFont::ALL_INHERIT));
274 }
275
276
277 void Paragraph::insertChar(pos_type pos, Paragraph::value_type c,
278                            LyXFont const & font, Change change)
279 {
280         pimpl_->insertChar(pos, c, font, change);
281 }
282
283
284 void Paragraph::insertInset(pos_type pos, InsetOld * inset)
285 {
286         insertInset(pos, inset, LyXFont(LyXFont::ALL_INHERIT));
287 }
288
289
290 void Paragraph::insertInset(pos_type pos, InsetOld * inset, LyXFont const & font, Change change)
291 {
292         pimpl_->insertInset(pos, inset, font, change);
293 }
294
295
296 bool Paragraph::insetAllowed(InsetOld::Code code)
297 {
298         //lyxerr << "Paragraph::InsertInsetAllowed" << endl;
299         if (pimpl_->inset_owner)
300                 return pimpl_->inset_owner->insetAllowed(code);
301         return true;
302 }
303
304
305 InsetOld * Paragraph::getInset(pos_type pos)
306 {
307         Assert(pos < size());
308         return insetlist.get(pos);
309 }
310
311
312 InsetOld const * Paragraph::getInset(pos_type pos) const
313 {
314         Assert(pos < size());
315         return insetlist.get(pos);
316 }
317
318
319 // Gets uninstantiated font setting at position.
320 LyXFont const Paragraph::getFontSettings(BufferParams const & bparams,
321                                          pos_type pos) const
322 {
323         Assert(pos <= size());
324
325         Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
326         Pimpl::FontList::const_iterator end = pimpl_->fontlist.end();
327         for (; cit != end; ++cit)
328                 if (cit->pos() >= pos)
329                         break;
330
331         if (cit != end)
332                 return cit->font();
333
334         if (pos == size() && !empty())
335                 return getFontSettings(bparams, pos - 1);
336
337         return LyXFont(LyXFont::ALL_INHERIT, getParLanguage(bparams));
338 }
339
340
341 lyx::pos_type Paragraph::getEndPosOfFontSpan(lyx::pos_type pos) const
342 {
343         Assert(pos <= size());
344
345         Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
346         Pimpl::FontList::const_iterator end = pimpl_->fontlist.end();
347         for (; cit != end; ++cit)
348                 if (cit->pos() >= pos)
349                         return cit->pos();
350
351         // This should not happen, but if so, we take no chances.
352         lyxerr << "Pararaph::getEndPosOfFontSpan: This should not happen!"
353                << endl;
354         return pos;
355 }
356
357
358 // Gets uninstantiated font setting at position 0
359 LyXFont const Paragraph::getFirstFontSettings() const
360 {
361         if (!empty() && !pimpl_->fontlist.empty())
362                 return pimpl_->fontlist[0].font();
363
364         return LyXFont(LyXFont::ALL_INHERIT);
365 }
366
367
368 // Gets the fully instantiated font at a given position in a paragraph
369 // This is basically the same function as LyXText::GetFont() in text2.C.
370 // The difference is that this one is used for generating the LaTeX file,
371 // and thus cosmetic "improvements" are disallowed: This has to deliver
372 // the true picture of the buffer. (Asger)
373 LyXFont const Paragraph::getFont(BufferParams const & bparams, pos_type pos,
374                                  LyXFont const & outerfont) const
375 {
376         Assert(pos >= 0);
377
378         LyXLayout_ptr const & lout = layout();
379
380         pos_type const body_pos = beginningOfBody();
381
382         LyXFont layoutfont;
383         if (pos < body_pos)
384                 layoutfont = lout->labelfont;
385         else
386                 layoutfont = lout->font;
387
388         LyXFont tmpfont = getFontSettings(bparams, pos);
389         tmpfont.realize(layoutfont);
390         tmpfont.realize(outerfont);
391         tmpfont.realize(bparams.getLyXTextClass().defaultfont());
392
393         return tmpfont;
394 }
395
396
397 LyXFont const Paragraph::getLabelFont(BufferParams const & bparams,
398                                       LyXFont const & outerfont) const
399 {
400         LyXFont tmpfont = layout()->labelfont;
401         tmpfont.setLanguage(getParLanguage(bparams));
402         tmpfont.realize(outerfont);
403         tmpfont.realize(bparams.getLyXTextClass().defaultfont());
404         return tmpfont;
405 }
406
407
408 LyXFont const Paragraph::getLayoutFont(BufferParams const & bparams,
409                                        LyXFont const & outerfont) const
410 {
411         LyXFont tmpfont = layout()->font;
412         tmpfont.setLanguage(getParLanguage(bparams));
413         tmpfont.realize(outerfont);
414         tmpfont.realize(bparams.getLyXTextClass().defaultfont());
415         return tmpfont;
416 }
417
418
419 /// Returns the height of the highest font in range
420 LyXFont::FONT_SIZE
421 Paragraph::highestFontInRange(pos_type startpos, pos_type endpos,
422                               LyXFont::FONT_SIZE const def_size) const
423 {
424         if (pimpl_->fontlist.empty())
425                 return def_size;
426
427         Pimpl::FontList::const_iterator end_it = pimpl_->fontlist.begin();
428         Pimpl::FontList::const_iterator end = pimpl_->fontlist.end();
429         for (; end_it != end; ++end_it) {
430                 if (end_it->pos() >= endpos)
431                         break;
432         }
433
434         if (end_it != end)
435                 ++end_it;
436
437         Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
438         for (; cit != end; ++cit) {
439                 if (cit->pos() >= startpos)
440                         break;
441         }
442
443         LyXFont::FONT_SIZE maxsize = LyXFont::SIZE_TINY;
444         for (; cit != end_it; ++cit) {
445                 LyXFont::FONT_SIZE size = cit->font().size();
446                 if (size == LyXFont::INHERIT_SIZE)
447                         size = def_size;
448                 if (size > maxsize && size <= LyXFont::SIZE_HUGER)
449                         maxsize = size;
450         }
451         return maxsize;
452 }
453
454
455 Paragraph::value_type
456 Paragraph::getUChar(BufferParams const & bparams, pos_type pos) const
457 {
458         value_type c = getChar(pos);
459         if (!lyxrc.rtl_support)
460                 return c;
461
462         value_type uc = c;
463         switch (c) {
464         case '(':
465                 uc = ')';
466                 break;
467         case ')':
468                 uc = '(';
469                 break;
470         case '[':
471                 uc = ']';
472                 break;
473         case ']':
474                 uc = '[';
475                 break;
476         case '{':
477                 uc = '}';
478                 break;
479         case '}':
480                 uc = '{';
481                 break;
482         case '<':
483                 uc = '>';
484                 break;
485         case '>':
486                 uc = '<';
487                 break;
488         }
489         if (uc != c && getFontSettings(bparams, pos).isRightToLeft())
490                 return uc;
491         else
492                 return c;
493 }
494
495
496 void Paragraph::setFont(pos_type pos, LyXFont const & font)
497 {
498         Assert(pos <= size());
499
500         // First, reduce font against layout/label font
501         // Update: The SetCharFont() routine in text2.C already
502         // reduces font, so we don't need to do that here. (Asger)
503         // No need to simplify this because it will disappear
504         // in a new kernel. (Asger)
505         // Next search font table
506
507         Pimpl::FontList::iterator beg = pimpl_->fontlist.begin();
508         Pimpl::FontList::iterator it = beg;
509         Pimpl::FontList::iterator endit = pimpl_->fontlist.end();
510         for (; it != endit; ++it) {
511                 if (it->pos() >= pos)
512                         break;
513         }
514         unsigned int i = std::distance(beg, it);
515         bool notfound = (it == endit);
516
517         if (!notfound && pimpl_->fontlist[i].font() == font)
518                 return;
519
520         bool begin = pos == 0 || notfound ||
521                 (i > 0 && pimpl_->fontlist[i - 1].pos() == pos - 1);
522         // Is position pos is a beginning of a font block?
523         bool end = !notfound && pimpl_->fontlist[i].pos() == pos;
524         // Is position pos is the end of a font block?
525         if (begin && end) { // A single char block
526                 if (i + 1 < pimpl_->fontlist.size() &&
527                     pimpl_->fontlist[i + 1].font() == font) {
528                         // Merge the singleton block with the next block
529                         pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i);
530                         if (i > 0 && pimpl_->fontlist[i - 1].font() == font)
531                                 pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i - 1);
532                 } else if (i > 0 && pimpl_->fontlist[i - 1].font() == font) {
533                         // Merge the singleton block with the previous block
534                         pimpl_->fontlist[i - 1].pos(pos);
535                         pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i);
536                 } else
537                         pimpl_->fontlist[i].font(font);
538         } else if (begin) {
539                 if (i > 0 && pimpl_->fontlist[i - 1].font() == font)
540                         pimpl_->fontlist[i - 1].pos(pos);
541                 else
542                         pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i,
543                                         Pimpl::FontTable(pos, font));
544         } else if (end) {
545                 pimpl_->fontlist[i].pos(pos - 1);
546                 if (!(i + 1 < pimpl_->fontlist.size() &&
547                       pimpl_->fontlist[i + 1].font() == font))
548                         pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i + 1,
549                                         Pimpl::FontTable(pos, font));
550         } else { // The general case. The block is splitted into 3 blocks
551                 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i,
552                                 Pimpl::FontTable(pos - 1, pimpl_->fontlist[i].font()));
553                 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i + 1,
554                                 Pimpl::FontTable(pos, font));
555         }
556 }
557
558
559 void Paragraph::makeSameLayout(Paragraph const & par)
560 {
561         layout(par.layout());
562         // move to pimpl?
563         params() = par.params();
564 }
565
566
567 int Paragraph::stripLeadingSpaces()
568 {
569         if (isFreeSpacing())
570                 return 0;
571
572         int i = 0;
573         while (!empty() && (isNewline(0) || isLineSeparator(0))) {
574                 pimpl_->eraseIntern(0);
575                 ++i;
576         }
577
578         return i;
579 }
580
581
582 bool Paragraph::hasSameLayout(Paragraph const & par) const
583 {
584         return
585                 par.layout() == layout() &&
586                 params().sameLayout(par.params());
587 }
588
589
590 Paragraph::depth_type Paragraph::getDepth() const
591 {
592         return params().depth();
593 }
594
595
596 Paragraph::depth_type Paragraph::getMaxDepthAfter() const
597 {
598         if (layout()->isEnvironment())
599                 return params().depth() + 1;
600         else
601                 return params().depth();
602 }
603
604
605 char Paragraph::getAlign() const
606 {
607         return params().align();
608 }
609
610
611 string const & Paragraph::getLabelstring() const
612 {
613         return params().labelString();
614 }
615
616
617 // the next two functions are for the manual labels
618 string const Paragraph::getLabelWidthString() const
619 {
620         if (!params().labelWidthString().empty())
621                 return params().labelWidthString();
622         else
623                 return _("Senseless with this layout!");
624 }
625
626
627 void Paragraph::setLabelWidthString(string const & s)
628 {
629         params().labelWidthString(s);
630 }
631
632
633 void Paragraph::applyLayout(LyXLayout_ptr const & new_layout)
634 {
635         layout(new_layout);
636         params().labelWidthString(string());
637         params().align(LYX_ALIGN_LAYOUT);
638         params().spaceTop(VSpace(VSpace::NONE));
639         params().spaceBottom(VSpace(VSpace::NONE));
640         params().spacing(Spacing(Spacing::Default));
641 }
642
643
644 int Paragraph::beginningOfBody() const
645 {
646         if (layout()->labeltype != LABEL_MANUAL)
647                 return 0;
648
649         // Unroll the first two cycles of the loop
650         // and remember the previous character to
651         // remove unnecessary GetChar() calls
652         pos_type i = 0;
653         if (i < size() && !isNewline(i)) {
654                 ++i;
655                 char previous_char = 0;
656                 char temp = 0;
657                 if (i < size()) {
658                         previous_char = getChar(i);
659                         if (!isNewline(i)) {
660                                 ++i;
661                                 while (i < size() && previous_char != ' ') {
662                                         temp = getChar(i);
663                                         if (isNewline(i))
664                                                 break;
665
666                                         ++i;
667                                         previous_char = temp;
668                                 }
669                         }
670                 }
671         }
672
673         return i;
674 }
675
676
677 // returns -1 if inset not found
678 int Paragraph::getPositionOfInset(InsetOld const * inset) const
679 {
680         // Find the entry.
681         InsetList::const_iterator it = insetlist.begin();
682         InsetList::const_iterator end = insetlist.end();
683         for (; it != end; ++it)
684                 if (it->inset == inset)
685                         return it->pos;
686         return -1;
687 }
688
689
690 InsetBibitem * Paragraph::bibitem() const
691 {
692         InsetList::const_iterator it = insetlist.begin();
693         if (it != insetlist.end() && it->inset->lyxCode() == InsetOld::BIBTEX_CODE)
694                 return static_cast<InsetBibitem *>(it->inset);
695         return 0;
696 }
697
698
699
700 // This could go to ParagraphParameters if we want to
701 int Paragraph::startTeXParParams(BufferParams const & bparams,
702                                  ostream & os, bool moving_arg) const
703 {
704         int column = 0;
705
706         if (params().noindent()) {
707                 os << "\\noindent ";
708                 column += 10;
709         }
710
711         switch (params().align()) {
712         case LYX_ALIGN_NONE:
713         case LYX_ALIGN_BLOCK:
714         case LYX_ALIGN_LAYOUT:
715         case LYX_ALIGN_SPECIAL:
716                 break;
717         case LYX_ALIGN_LEFT:
718         case LYX_ALIGN_RIGHT:
719         case LYX_ALIGN_CENTER:
720                 if (moving_arg) {
721                         os << "\\protect";
722                         column = 8;
723                 }
724                 break;
725         }
726
727         switch (params().align()) {
728         case LYX_ALIGN_NONE:
729         case LYX_ALIGN_BLOCK:
730         case LYX_ALIGN_LAYOUT:
731         case LYX_ALIGN_SPECIAL:
732                 break;
733         case LYX_ALIGN_LEFT:
734                 if (getParLanguage(bparams)->babel() != "hebrew") {
735                         os << "\\begin{flushleft}";
736                         column += 17;
737                 } else {
738                         os << "\\begin{flushright}";
739                         column += 18;
740                 }
741                 break;
742         case LYX_ALIGN_RIGHT:
743                 if (getParLanguage(bparams)->babel() != "hebrew") {
744                         os << "\\begin{flushright}";
745                         column += 18;
746                 } else {
747                         os << "\\begin{flushleft}";
748                         column += 17;
749                 }
750                 break;
751         case LYX_ALIGN_CENTER:
752                 os << "\\begin{center}";
753                 column += 14;
754                 break;
755         }
756
757         return column;
758 }
759
760
761 // This could go to ParagraphParameters if we want to
762 int Paragraph::endTeXParParams(BufferParams const & bparams,
763                                ostream & os, bool moving_arg) const
764 {
765         int column = 0;
766
767         switch (params().align()) {
768         case LYX_ALIGN_NONE:
769         case LYX_ALIGN_BLOCK:
770         case LYX_ALIGN_LAYOUT:
771         case LYX_ALIGN_SPECIAL:
772                 break;
773         case LYX_ALIGN_LEFT:
774         case LYX_ALIGN_RIGHT:
775         case LYX_ALIGN_CENTER:
776                 if (moving_arg) {
777                         os << "\\protect";
778                         column = 8;
779                 }
780                 break;
781         }
782
783         switch (params().align()) {
784         case LYX_ALIGN_NONE:
785         case LYX_ALIGN_BLOCK:
786         case LYX_ALIGN_LAYOUT:
787         case LYX_ALIGN_SPECIAL:
788                 break;
789         case LYX_ALIGN_LEFT:
790                 if (getParLanguage(bparams)->babel() != "hebrew") {
791                         os << "\\end{flushleft}";
792                         column = 15;
793                 } else {
794                         os << "\\end{flushright}";
795                         column = 16;
796                 }
797                 break;
798         case LYX_ALIGN_RIGHT:
799                 if (getParLanguage(bparams)->babel() != "hebrew") {
800                         os << "\\end{flushright}";
801                         column+= 16;
802                 } else {
803                         os << "\\end{flushleft}";
804                         column = 15;
805                 }
806                 break;
807         case LYX_ALIGN_CENTER:
808                 os << "\\end{center}";
809                 column = 12;
810                 break;
811         }
812         return column;
813 }
814
815
816 // This one spits out the text of the paragraph
817 bool Paragraph::simpleTeXOnePar(Buffer const * buf,
818                                 BufferParams const & bparams,
819                                 LyXFont const & outerfont,
820                                 ostream & os, TexRow & texrow,
821                                 LatexRunParams const & runparams)
822 {
823         lyxerr[Debug::LATEX] << "SimpleTeXOnePar...     " << this << endl;
824
825         bool return_value = false;
826
827         LyXLayout_ptr style;
828
829         // well we have to check if we are in an inset with unlimited
830         // lenght (all in one row) if that is true then we don't allow
831         // any special options in the paragraph and also we don't allow
832         // any environment other then "Standard" to be valid!
833         bool asdefault =
834                 (inInset() && inInset()->forceDefaultParagraphs(inInset()));
835
836         if (asdefault) {
837                 style = bparams.getLyXTextClass().defaultLayout();
838         } else {
839                 style = layout();
840         }
841
842         LyXFont basefont;
843
844         // Maybe we have to create a optional argument.
845         pos_type body_pos;
846
847         // FIXME: can we actually skip this check and just call
848         // beginningOfBody() ??
849         if (style->labeltype != LABEL_MANUAL) {
850                 body_pos = 0;
851         } else {
852                 body_pos = beginningOfBody();
853         }
854
855         unsigned int column = 0;
856
857         if (body_pos > 0) {
858                 os << '[';
859                 ++column;
860                 basefont = getLabelFont(bparams, outerfont);
861         } else {
862                 basefont = getLayoutFont(bparams, outerfont);
863         }
864
865         bool moving_arg = runparams.moving_arg;
866         moving_arg |= style->needprotect;
867
868         // Which font is currently active?
869         LyXFont running_font(basefont);
870         // Do we have an open font change?
871         bool open_font = false;
872
873         Change::Type running_change = Change::UNCHANGED;
874
875         texrow.start(id(), 0);
876
877         // if the paragraph is empty, the loop will not be entered at all
878         if (empty()) {
879                 if (style->isCommand()) {
880                         os << '{';
881                         ++column;
882                 }
883                 if (!asdefault)
884                         column += startTeXParParams(bparams, os, moving_arg);
885
886         }
887
888         for (pos_type i = 0; i < size(); ++i) {
889                 ++column;
890                 // First char in paragraph or after label?
891                 if (i == body_pos) {
892                         if (body_pos > 0) {
893                                 if (open_font) {
894                                         column += running_font.latexWriteEndChanges(os, basefont, basefont);
895                                         open_font = false;
896                                 }
897                                 basefont = getLayoutFont(bparams, outerfont);
898                                 running_font = basefont;
899                                 os << ']';
900                                 ++column;
901                         }
902                         if (style->isCommand()) {
903                                 os << '{';
904                                 ++column;
905                         }
906
907                         if (!asdefault)
908                                 column += startTeXParParams(bparams, os,
909                                                             moving_arg);
910                 }
911
912                 value_type c = getChar(i);
913
914                 // Fully instantiated font
915                 LyXFont font = getFont(bparams, i, outerfont);
916
917                 LyXFont const last_font = running_font;
918
919                 // Spaces at end of font change are simulated to be
920                 // outside font change, i.e. we write "\textXX{text} "
921                 // rather than "\textXX{text }". (Asger)
922                 if (open_font && c == ' ' && i <= size() - 2) {
923                         LyXFont const & next_font = getFont(bparams, i + 1, outerfont);
924                         if (next_font != running_font
925                             && next_font != font) {
926                                 font = next_font;
927                         }
928                 }
929
930                 // We end font definition before blanks
931                 if (open_font &&
932                     (font != running_font ||
933                      font.language() != running_font.language()))
934                 {
935                         column += running_font.latexWriteEndChanges(os,
936                                                                     basefont,
937                                                                     (i == body_pos-1) ? basefont : font);
938                         running_font = basefont;
939                         open_font = false;
940                 }
941
942                 // Blanks are printed before start of fontswitch
943                 if (c == ' ') {
944                         // Do not print the separation of the optional argument
945                         if (i != body_pos - 1) {
946                                 pimpl_->simpleTeXBlanks(os, texrow, i,
947                                                        column, font, *style);
948                         }
949                 }
950
951                 // Do we need to change font?
952                 if ((font != running_font ||
953                      font.language() != running_font.language()) &&
954                         i != body_pos - 1)
955                 {
956                         column += font.latexWriteStartChanges(os, basefont,
957                                                               last_font);
958                         running_font = font;
959                         open_font = true;
960                 }
961
962                 Change::Type change = pimpl_->lookupChange(i);
963
964                 column += Changes::latexMarkChange(os, running_change, change);
965                 running_change = change;
966
967                 LatexRunParams rp = runparams;
968                 rp.moving_arg = moving_arg;
969                 rp.free_spacing = style->free_spacing;
970                 pimpl_->simpleTeXSpecialChars(buf, bparams,
971                                               os, texrow, runparams,
972                                               font, running_font,
973                                               basefont, outerfont, open_font,
974                                               running_change,
975                                               *style, i, column, c);
976         }
977
978         column += Changes::latexMarkChange(os,
979                         running_change, Change::UNCHANGED);
980
981         // If we have an open font definition, we have to close it
982         if (open_font) {
983 #ifdef FIXED_LANGUAGE_END_DETECTION
984                 if (next_) {
985                         running_font
986                                 .latexWriteEndChanges(os, basefont,
987                                                       next_->getFont(bparams,
988                                                       0, outerfont));
989                 } else {
990                         running_font.latexWriteEndChanges(os, basefont,
991                                                           basefont);
992                 }
993 #else
994 #ifdef WITH_WARNINGS
995 //#warning For now we ALWAYS have to close the foreign font settings if they are
996 //#warning there as we start another \selectlanguage with the next paragraph if
997 //#warning we are in need of this. This should be fixed sometime (Jug)
998 #endif
999                 running_font.latexWriteEndChanges(os, basefont,  basefont);
1000 #endif
1001         }
1002
1003         // Needed if there is an optional argument but no contents.
1004         if (body_pos > 0 && body_pos == size()) {
1005                 os << "]~";
1006                 return_value = false;
1007         }
1008
1009         if (!asdefault) {
1010                 column += endTeXParParams(bparams, os, moving_arg);
1011         }
1012
1013         lyxerr[Debug::LATEX] << "SimpleTeXOnePar...done " << this << endl;
1014         return return_value;
1015 }
1016
1017
1018
1019
1020 bool Paragraph::isHfill(pos_type pos) const
1021 {
1022         return IsInsetChar(getChar(pos))
1023                && getInset(pos)->lyxCode() == InsetOld::HFILL_CODE;
1024 }
1025
1026
1027 bool Paragraph::isInset(pos_type pos) const
1028 {
1029         return IsInsetChar(getChar(pos));
1030 }
1031
1032
1033 bool Paragraph::isNewline(pos_type pos) const
1034 {
1035         return IsInsetChar(getChar(pos))
1036                && getInset(pos)->lyxCode() == InsetOld::NEWLINE_CODE;
1037 }
1038
1039
1040 bool Paragraph::isSeparator(pos_type pos) const
1041 {
1042         return IsSeparatorChar(getChar(pos));
1043 }
1044
1045
1046 bool Paragraph::isLineSeparator(pos_type pos) const
1047 {
1048         value_type const c = getChar(pos);
1049         return IsLineSeparatorChar(c)
1050                 || (IsInsetChar(c) && getInset(pos) &&
1051                 getInset(pos)->isLineSeparator());
1052 }
1053
1054
1055 bool Paragraph::isKomma(pos_type pos) const
1056 {
1057         return IsKommaChar(getChar(pos));
1058 }
1059
1060
1061 /// Used by the spellchecker
1062 bool Paragraph::isLetter(pos_type pos) const
1063 {
1064         value_type const c = getChar(pos);
1065         if (IsLetterChar(c))
1066                 return true;
1067         if (isInset(pos))
1068                 return getInset(pos)->isLetter();
1069         // We want to pass the ' and escape chars to ispell
1070         string const extra = lyxrc.isp_esc_chars + '\'';
1071         return contains(extra, c);
1072 }
1073
1074
1075 bool Paragraph::isWord(pos_type pos) const
1076 {
1077         return IsWordChar(getChar(pos)) ;
1078 }
1079
1080
1081 Language const *
1082 Paragraph::getParLanguage(BufferParams const & bparams) const
1083 {
1084         if (!empty())
1085                 return getFirstFontSettings().language();
1086 #warning FIXME we should check the prev par as well (Lgb)
1087         return bparams.language;
1088 }
1089
1090
1091 bool Paragraph::isRightToLeftPar(BufferParams const & bparams) const
1092 {
1093         return lyxrc.rtl_support
1094                 && getParLanguage(bparams)->RightToLeft()
1095                 && !(inInset() && inInset()->owner() &&
1096                      inInset()->owner()->lyxCode() == InsetOld::ERT_CODE);
1097 }
1098
1099
1100 void Paragraph::changeLanguage(BufferParams const & bparams,
1101                                Language const * from, Language const * to)
1102 {
1103         for (pos_type i = 0; i < size(); ++i) {
1104                 LyXFont font = getFontSettings(bparams, i);
1105                 if (font.language() == from) {
1106                         font.setLanguage(to);
1107                         setFont(i, font);
1108                 }
1109         }
1110 }
1111
1112
1113 bool Paragraph::isMultiLingual(BufferParams const & bparams)
1114 {
1115         Language const * doc_language = bparams.language;
1116         Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
1117         Pimpl::FontList::const_iterator end = pimpl_->fontlist.end();
1118
1119         for (; cit != end; ++cit)
1120                 if (cit->font().language() != ignore_language &&
1121                     cit->font().language() != latex_language &&
1122                     cit->font().language() != doc_language)
1123                         return true;
1124         return false;
1125 }
1126
1127
1128 // Convert the paragraph to a string.
1129 // Used for building the table of contents
1130 string const Paragraph::asString(Buffer const * buffer, bool label) const
1131 {
1132 #if 0
1133         string s;
1134         if (label && !params().labelString().empty())
1135                 s += params().labelString() + ' ';
1136
1137         for (pos_type i = 0; i < size(); ++i) {
1138                 value_type c = getChar(i);
1139                 if (IsPrintable(c))
1140                         s += c;
1141                 else if (c == META_INSET &&
1142                          getInset(i)->lyxCode() == InsetOld::MATH_CODE) {
1143                         ostringstream ost;
1144                         getInset(i)->ascii(buffer, ost);
1145                         s += subst(STRCONV(ost.str()),'\n',' ');
1146                 }
1147         }
1148
1149         return s;
1150 #else
1151         // This should really be done by the caller and not here.
1152         string ret(asString(buffer, 0, size(), label));
1153         return subst(ret, '\n', ' ');
1154 #endif
1155 }
1156
1157
1158 string const Paragraph::asString(Buffer const * buffer,
1159                                  pos_type beg, pos_type end, bool label) const
1160 {
1161         ostringstream os;
1162
1163         if (beg == 0 && label && !params().labelString().empty())
1164                 os << params().labelString() << ' ';
1165
1166         for (pos_type i = beg; i < end; ++i) {
1167                 value_type const c = getUChar(buffer->params, i);
1168                 if (IsPrintable(c))
1169                         os << c;
1170                 else if (c == META_INSET)
1171                         getInset(i)->ascii(buffer, os);
1172         }
1173
1174         return STRCONV(os.str());
1175 }
1176
1177
1178 void Paragraph::setInsetOwner(UpdatableInset * inset)
1179 {
1180         pimpl_->inset_owner = inset;
1181         InsetList::iterator it = insetlist.begin();
1182         InsetList::iterator end = insetlist.end();
1183         for (; it != end; ++it)
1184                 if (it->inset)
1185                         it->inset->setOwner(inset);
1186 }
1187
1188
1189 void Paragraph::deleteInsetsLyXText(BufferView * bv)
1190 {
1191         insetlist.deleteInsetsLyXText(bv);
1192 }
1193
1194
1195 void Paragraph::resizeInsetsLyXText(BufferView * bv)
1196 {
1197         insetlist.resizeInsetsLyXText(bv);
1198 }
1199
1200
1201 void Paragraph::setContentsFromPar(Paragraph const & par)
1202 {
1203         pimpl_->setContentsFromPar(par);
1204 }
1205
1206
1207 void Paragraph::trackChanges(Change::Type type)
1208 {
1209         pimpl_->trackChanges(type);
1210 }
1211
1212
1213 void Paragraph::untrackChanges()
1214 {
1215         pimpl_->untrackChanges();
1216 }
1217
1218
1219 void Paragraph::cleanChanges()
1220 {
1221         pimpl_->cleanChanges();
1222 }
1223
1224
1225 Change::Type Paragraph::lookupChange(lyx::pos_type pos) const
1226 {
1227         Assert(!size() || pos < size());
1228         return pimpl_->lookupChange(pos);
1229 }
1230
1231
1232 Change const Paragraph::lookupChangeFull(lyx::pos_type pos) const
1233 {
1234         Assert(!size() || pos < size());
1235         return pimpl_->lookupChangeFull(pos);
1236 }
1237
1238
1239 bool Paragraph::isChanged(pos_type start, pos_type end) const
1240 {
1241         return pimpl_->isChanged(start, end);
1242 }
1243
1244
1245 bool Paragraph::isChangeEdited(pos_type start, pos_type end) const
1246 {
1247         return pimpl_->isChangeEdited(start, end);
1248 }
1249
1250
1251 void Paragraph::setChange(lyx::pos_type pos, Change::Type type)
1252 {
1253         pimpl_->setChange(pos, type);
1254
1255 }
1256
1257
1258 void Paragraph::markErased()
1259 {
1260         pimpl_->markErased();
1261 }
1262
1263
1264 void Paragraph::acceptChange(pos_type start, pos_type end)
1265 {
1266         return pimpl_->acceptChange(start, end);
1267 }
1268
1269
1270 void Paragraph::rejectChange(pos_type start, pos_type end)
1271 {
1272         return pimpl_->rejectChange(start, end);
1273 }
1274
1275
1276 lyx::pos_type Paragraph::size() const
1277 {
1278         return pimpl_->size();
1279 }
1280
1281
1282 bool Paragraph::empty() const
1283 {
1284         return pimpl_->empty();
1285 }
1286
1287
1288 Paragraph::value_type Paragraph::getChar(pos_type pos) const
1289 {
1290         return pimpl_->getChar(pos);
1291 }
1292
1293
1294 int Paragraph::id() const
1295 {
1296         return pimpl_->id_;
1297 }
1298
1299
1300 void Paragraph::id(int i)
1301 {
1302         pimpl_->id_ = i;
1303 }
1304
1305
1306 LyXLayout_ptr const & Paragraph::layout() const
1307 {
1308 /*
1309         InsetOld * inset = inInset();
1310         if (inset && inset->lyxCode() == InsetOld::ENVIRONMENT_CODE)
1311                 return static_cast<InsetEnvironment*>(inset)->layout();
1312 */
1313         return layout_;
1314 }
1315
1316
1317 void Paragraph::layout(LyXLayout_ptr const & new_layout)
1318 {
1319         layout_ = new_layout;
1320 }
1321
1322
1323 UpdatableInset * Paragraph::inInset() const
1324 {
1325         return pimpl_->inset_owner;
1326 }
1327
1328
1329 void Paragraph::clearContents()
1330 {
1331         pimpl_->clear();
1332 }
1333
1334 void Paragraph::setChar(pos_type pos, value_type c)
1335 {
1336         pimpl_->setChar(pos, c);
1337 }
1338
1339
1340 ParagraphParameters & Paragraph::params()
1341 {
1342         return pimpl_->params;
1343 }
1344
1345
1346 ParagraphParameters const & Paragraph::params() const
1347 {
1348         return pimpl_->params;
1349 }
1350
1351
1352 bool Paragraph::isFreeSpacing() const
1353 {
1354         if (layout()->free_spacing)
1355                 return true;
1356
1357         // for now we just need this, later should we need this in some
1358         // other way we can always add a function to InsetOld::() too.
1359         if (pimpl_->inset_owner && pimpl_->inset_owner->owner())
1360                 return pimpl_->inset_owner->owner()->lyxCode() == InsetOld::ERT_CODE;
1361         return false;
1362 }
1363
1364
1365 bool Paragraph::allowEmpty() const
1366 {
1367         if (layout()->keepempty)
1368                 return true;
1369         if (pimpl_->inset_owner && pimpl_->inset_owner->owner())
1370                 return pimpl_->inset_owner->owner()->lyxCode() == InsetOld::ERT_CODE;
1371         return false;
1372 }