]> git.lyx.org Git - lyx.git/blob - src/paragraph.C
4bb88f2b824be88429f5616c7777add205cba1c6
[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 #ifdef __GNUG__
14 #pragma implementation
15 #endif
16
17 #include "paragraph.h"
18 #include "paragraph_pimpl.h"
19 #include "lyxrc.h"
20 #include "layout.h"
21 #include "language.h"
22 #include "tex-strings.h"
23 #include "buffer.h"
24 #include "bufferparams.h"
25 #include "debug.h"
26 #include "LaTeXFeatures.h"
27 #include "texrow.h"
28 #include "BufferView.h"
29 #include "encoding.h"
30 #include "ParameterStruct.h"
31 #include "gettext.h"
32 #include "lyxtextclasslist.h"
33
34 #include "insets/insetinclude.h"
35 #include "insets/insetbib.h"
36 #include "insets/insettext.h"
37
38 #include "support/filetools.h"
39 #include "support/lstrings.h"
40 #include "support/lyxmanip.h"
41 #include "support/FileInfo.h"
42 #include "support/LAssert.h"
43 #include "support/textutils.h"
44
45 #include <algorithm>
46 #include <fstream>
47 #include <csignal>
48
49 using std::ostream;
50 using std::endl;
51 using std::fstream;
52 using std::ios;
53 using std::lower_bound;
54 using std::upper_bound;
55 using std::reverse;
56
57 using lyx::pos_type;
58
59 int tex_code_break_column = 72;  // needs non-zero initialization. set later.
60 // this is a bad idea, but how can Paragraph find its buffer to get
61 // parameters? (JMarc)
62
63 extern string bibitemWidest(Buffer const *);
64
65 // this is a minibuffer
66
67 namespace {
68
69 char minibuffer_char;
70 LyXFont minibuffer_font;
71 Inset * minibuffer_inset;
72
73 } // namespace anon
74
75
76 extern BufferView * current_view;
77
78
79 Paragraph::Paragraph()
80         : pimpl_(new Paragraph::Pimpl(this))
81 {
82         for (int i = 0; i < 10; ++i)
83                 setCounter(i, 0);
84         next_ = 0;
85         previous_ = 0;
86         enumdepth = 0;
87         itemdepth = 0;
88         bibkey = 0; // ale970302
89         params().clear();
90 }
91
92
93 // This constructor inserts the new paragraph in a list.
94 Paragraph::Paragraph(Paragraph * par)
95         : pimpl_(new Paragraph::Pimpl(this))
96 {
97         for (int i = 0; i < 10; ++i)
98                 setCounter(i, 0);
99         enumdepth = 0;
100         itemdepth = 0;
101
102         // double linked list begin
103         next_ = par->next_;
104         if (next_)
105                 next_->previous_ = this;
106         previous_ = par;
107         previous_->next_ = this;
108         // end
109
110         bibkey = 0; // ale970302
111         params().clear();
112 }
113
114
115 Paragraph::Paragraph(Paragraph const & lp, bool same_ids)
116         : pimpl_(new Paragraph::Pimpl(*lp.pimpl_, this, same_ids))
117 {
118         for (int i = 0; i < 10; ++i)
119                 setCounter(i, 0);
120         enumdepth = 0;
121         itemdepth = 0;
122         next_     = 0;
123         previous_ = 0;
124
125         // this is because of the dummy layout of the paragraphs that
126         // follow footnotes
127         layout_ = lp.layout();
128
129         // ale970302
130         if (lp.bibkey) {
131                 bibkey = static_cast<InsetBibKey *>
132                         (lp.bibkey->clone(*current_view->buffer()));
133         } else {
134                 bibkey = 0;
135         }
136
137         // copy everything behind the break-position to the new paragraph
138         insetlist = lp.insetlist;
139         for (InsetList::iterator it = insetlist.begin();
140              it != insetlist.end(); ++it)
141         {
142                 it->inset = it->inset->clone(*current_view->buffer(), same_ids);
143                 // tell the new inset who is the boss now
144                 it->inset->parOwner(this);
145         }
146 }
147
148
149 // the destructor removes the new paragraph from the list
150 Paragraph::~Paragraph()
151 {
152         if (previous_)
153                 previous_->next_ = next_;
154         if (next_)
155                 next_->previous_ = previous_;
156
157         for (InsetList::iterator it = insetlist.begin();
158              it != insetlist.end(); ++it) {
159                 delete it->inset;
160         }
161
162         // ale970302
163         delete bibkey;
164
165         delete pimpl_;
166         //
167         //lyxerr << "Paragraph::paragraph_id = "
168         //       << Paragraph::paragraph_id << endl;
169 }
170
171
172 void Paragraph::writeFile(Buffer const * buf, ostream & os,
173                           BufferParams const & bparams,
174                           depth_type dth) const
175 {
176         // The beginning or end of a deeper (i.e. nested) area?
177         if (dth != params().depth()) {
178                 if (params().depth() > dth) {
179                         while (params().depth() > dth) {
180                                 os << "\n\\begin_deeper ";
181                                 ++dth;
182                         }
183                 } else {
184                         while (params().depth() < dth) {
185                                 os << "\n\\end_deeper ";
186                                 --dth;
187                         }
188                 }
189         }
190
191         // First write the layout
192         string lay = layout();
193         if (lay.empty()) {
194                 lay = textclasslist[bparams.textclass].defaultLayoutName();
195         }
196
197         os << "\n\\layout " << layout() << "\n";
198
199         // Maybe some vertical spaces.
200         if (params().spaceTop().kind() != VSpace::NONE)
201                 os << "\\added_space_top "
202                    << params().spaceTop().asLyXCommand() << " ";
203         if (params().spaceBottom().kind() != VSpace::NONE)
204                 os << "\\added_space_bottom "
205                    << params().spaceBottom().asLyXCommand() << " ";
206
207         // Maybe the paragraph has special spacing
208         params().spacing().writeFile(os, true);
209
210         // The labelwidth string used in lists.
211         if (!params().labelWidthString().empty())
212                 os << "\\labelwidthstring "
213                    << params().labelWidthString() << '\n';
214
215         // Lines above or below?
216         if (params().lineTop())
217                 os << "\\line_top ";
218         if (params().lineBottom())
219                 os << "\\line_bottom ";
220
221         // Pagebreaks above or below?
222         if (params().pagebreakTop())
223                 os << "\\pagebreak_top ";
224         if (params().pagebreakBottom())
225                 os << "\\pagebreak_bottom ";
226
227         // Start of appendix?
228         if (params().startOfAppendix())
229                 os << "\\start_of_appendix ";
230
231         // Noindent?
232         if (params().noindent())
233                 os << "\\noindent ";
234
235         // Do we have a manual left indent?
236         if (!params().leftIndent().zero())
237                 os << "\\leftindent " << params().leftIndent().asString() << " ";
238
239         // Alignment?
240         if (params().align() != LYX_ALIGN_LAYOUT) {
241                 int h = 0;
242                 switch (params().align()) {
243                 case LYX_ALIGN_LEFT: h = 1; break;
244                 case LYX_ALIGN_RIGHT: h = 2; break;
245                 case LYX_ALIGN_CENTER: h = 3; break;
246                 default: h = 0; break;
247                 }
248                 os << "\\align " << string_align[h] << " ";
249         }
250
251         // bibitem  ale970302
252         if (bibkey)
253                 bibkey->write(buf, os);
254
255         LyXFont font1(LyXFont::ALL_INHERIT, bparams.language);
256
257         int column = 0;
258         for (pos_type i = 0; i < size(); ++i) {
259                 if (!i) {
260                         os << "\n";
261                         column = 0;
262                 }
263
264                 // Write font changes
265                 LyXFont font2 = getFontSettings(bparams, i);
266                 if (font2 != font1) {
267 #ifndef INHERIT_LANGUAGE
268                         font2.lyxWriteChanges(font1, os);
269 #else
270                         font2.lyxWriteChanges(font1, bparams.language, os);
271 #endif
272                         column = 0;
273                         font1 = font2;
274                 }
275
276                 value_type const c = getChar(i);
277                 switch (c) {
278                 case META_INSET:
279                 {
280                         Inset const * inset = getInset(i);
281                         if (inset)
282                                 if (inset->directWrite()) {
283                                         // international char, let it write
284                                         // code directly so it's shorter in
285                                         // the file
286                                         inset->write(buf, os);
287                                 } else {
288                                         os << "\n\\begin_inset ";
289                                         inset->write(buf, os);
290                                         os << "\n\\end_inset \n\n";
291                                         column = 0;
292                                 }
293                 }
294                 break;
295                 case META_NEWLINE:
296                         os << "\n\\newline \n";
297                         column = 0;
298                         break;
299                 case META_HFILL:
300                         os << "\n\\hfill \n";
301                         column = 0;
302                         break;
303                 case '\\':
304                         os << "\n\\backslash \n";
305                         column = 0;
306                         break;
307                 case '.':
308                         if (i + 1 < size() && getChar(i + 1) == ' ') {
309                                 os << ".\n";
310                                 column = 0;
311                         } else
312                                 os << ".";
313                         break;
314                 default:
315                         if ((column > 70 && c == ' ')
316                             || column > 79) {
317                                 os << "\n";
318                                 column = 0;
319                         }
320                         // this check is to amend a bug. LyX sometimes
321                         // inserts '\0' this could cause problems.
322                         if (c != '\0')
323                                 os << c;
324                         else
325                                 lyxerr << "ERROR (Paragraph::writeFile):"
326                                         " NULL char in structure." << endl;
327                         ++column;
328                         break;
329                 }
330         }
331
332         // now write the next paragraph
333         if (next_)
334                 next_->writeFile(buf, os, bparams, dth);
335 }
336
337
338 void Paragraph::validate(LaTeXFeatures & features) const
339 {
340         BufferParams const & bparams = features.bufferParams();
341
342         // check the params.
343         if (params().lineTop() || params().lineBottom())
344                 features.require("lyxline");
345         if (!params().spacing().isDefault())
346                 features.require("setspace");
347
348         // then the layouts
349         features.useLayout(layout());
350
351         // then the fonts
352         Language const * doc_language = bparams.language;
353
354         Pimpl::FontList::const_iterator fcit = pimpl_->fontlist.begin();
355         Pimpl::FontList::const_iterator fend = pimpl_->fontlist.end();
356         for (; fcit != fend; ++fcit) {
357                 if (fcit->font().noun() == LyXFont::ON) {
358                         lyxerr[Debug::LATEX] << "font.noun: "
359                                              << fcit->font().noun()
360                                              << endl;
361                         features.require("noun");
362                         lyxerr[Debug::LATEX] << "Noun enabled. Font: "
363                                              << fcit->font().stateText(0)
364                                              << endl;
365                 }
366                 switch (fcit->font().color()) {
367                 case LColor::none:
368                 case LColor::inherit:
369                 case LColor::ignore:
370                         // probably we should put here all interface colors used for
371                         // font displaying! For now I just add this ones I know of (Jug)
372                 case LColor::latex:
373                 case LColor::note:
374                         break;
375                 default:
376                         features.require("color");
377                         lyxerr[Debug::LATEX] << "Color enabled. Font: "
378                                              << fcit->font().stateText(0)
379                                              << endl;
380                 }
381
382                 Language const * language = fcit->font().language();
383                 if (language->babel() != doc_language->babel() &&
384                     language != ignore_language &&
385 #ifdef INHERIT_LANGUAGE
386                     language != inherit_language &&
387 #endif
388                     language != latex_language)
389                 {
390                         features.useLanguage(language);
391                         lyxerr[Debug::LATEX] << "Found language "
392                                              << language->babel() << endl;
393                 }
394         }
395
396         if (!params().leftIndent().zero())
397                 features.require("ParagraphLeftIndent");
398
399         // then the insets
400         LyXLayout const & lout = textclasslist[bparams.textclass][layout()];
401
402         InsetList::const_iterator icit = insetlist.begin();
403         InsetList::const_iterator iend = insetlist.end();
404         for (; icit != iend; ++icit) {
405                 if (icit->inset) {
406                         icit->inset->validate(features);
407                         if (lout.needprotect &&
408                             icit->inset->lyxCode() == Inset::FOOT_CODE)
409                                 features.require("NeedLyXFootnoteCode");
410                 }
411         }
412 }
413
414
415 // First few functions needed for cut and paste and paragraph breaking.
416 void Paragraph::copyIntoMinibuffer(Buffer const & buffer, pos_type pos) const
417 {
418         BufferParams bparams = buffer.params;
419
420         minibuffer_char = getChar(pos);
421         minibuffer_font = getFontSettings(bparams, pos);
422         minibuffer_inset = 0;
423         if (minibuffer_char == Paragraph::META_INSET) {
424                 if (getInset(pos)) {
425                         minibuffer_inset = getInset(pos)->clone(buffer);
426                 } else {
427                         minibuffer_inset = 0;
428                         minibuffer_char = ' ';
429                         // This reflects what GetInset() does (ARRae)
430                 }
431         }
432 }
433
434
435 void Paragraph::cutIntoMinibuffer(BufferParams const & bparams, pos_type pos)
436 {
437         minibuffer_char = getChar(pos);
438         minibuffer_font = getFontSettings(bparams, pos);
439         minibuffer_inset = 0;
440         if (minibuffer_char == Paragraph::META_INSET) {
441                 if (getInset(pos)) {
442                         minibuffer_inset = getInset(pos);
443                         // This is a little hack since I want exactly
444                         // the inset, not just a clone. Otherwise
445                         // the inset would be deleted when calling Erase(pos)
446                         // find the entry
447                         InsetList::iterator it = insetlist.begin();
448                         InsetList::iterator end = insetlist.end();
449                         for (; it != end; ++it) {
450                                 if (it->pos == pos)
451                                         break;
452                         }
453
454                         if (it != end && it->pos == pos)
455                                 it->inset = 0;
456                         // the inset is not in a paragraph anymore
457                         minibuffer_inset->parOwner(0);
458                 } else {
459                         minibuffer_inset = 0;
460                         minibuffer_char = ' ';
461                         // This reflects what GetInset() does (ARRae)
462                 }
463
464         }
465
466         // Erase(pos); now the caller is responsible for that.
467 }
468
469
470 bool Paragraph::insertFromMinibuffer(pos_type pos)
471 {
472         if (minibuffer_char == Paragraph::META_INSET) {
473                 if (!insetAllowed(minibuffer_inset->lyxCode())) {
474                         return false;
475                 }
476                 insertInset(pos, minibuffer_inset, minibuffer_font);
477         } else {
478                 LyXFont f = minibuffer_font;
479                 if (!checkInsertChar(f)) {
480                         return false;
481                 }
482                 insertChar(pos, minibuffer_char, f);
483         }
484         return true;
485 }
486
487 // end of minibuffer
488
489
490 void Paragraph::erase(pos_type pos)
491 {
492         pimpl_->erase(pos);
493 }
494
495
496 bool Paragraph::checkInsertChar(LyXFont & font)
497 {
498         if (pimpl_->inset_owner)
499                 return pimpl_->inset_owner->checkInsertChar(font);
500         return true;
501 }
502
503
504 void Paragraph::insertChar(pos_type pos, Paragraph::value_type c)
505 {
506         LyXFont const f(LyXFont::ALL_INHERIT);
507         insertChar(pos, c, f);
508 }
509
510
511 void Paragraph::insertChar(pos_type pos, Paragraph::value_type c,
512                            LyXFont const & font)
513 {
514         pimpl_->insertChar(pos, c, font);
515 }
516
517
518 void Paragraph::insertInset(pos_type pos, Inset * inset)
519 {
520         LyXFont const f(LyXFont::ALL_INHERIT);
521         insertInset(pos, inset, f);
522 }
523
524
525 void Paragraph::insertInset(pos_type pos, Inset * inset, LyXFont const & font)
526 {
527         pimpl_->insertInset(pos, inset, font);
528 }
529
530
531 bool Paragraph::insetAllowed(Inset::Code code)
532 {
533         //lyxerr << "Paragraph::InsertInsetAllowed" << endl;
534
535         if (pimpl_->inset_owner)
536                 return pimpl_->inset_owner->insetAllowed(code);
537         return true;
538 }
539
540
541 Inset * Paragraph::getInset(pos_type pos)
542 {
543         lyx::Assert(pos < size());
544
545         // Find the inset.
546         InsetList::iterator it = insetlist.begin();
547         InsetList::iterator end = insetlist.end();
548         for (; it != end; ++it) {
549                 if (it->pos == pos)
550                         break;
551         }
552
553         if (it != end && it->pos == pos)
554                 return it->inset;
555
556         lyxerr << "ERROR (Paragraph::getInset): "
557                << "Inset does not exist: " << pos << endl;
558         //::raise(SIGSTOP);
559
560         // text[pos] = ' '; // WHY!!! does this set the pos to ' '????
561         // Did this commenting out introduce a bug? So far I have not
562         // see any, please enlighten me. (Lgb)
563         // My guess is that since the inset does not exist, we might
564         // as well replace it with a space to prevent craches. (Asger)
565         return 0;
566 }
567
568
569 Inset const * Paragraph::getInset(pos_type pos) const
570 {
571         lyx::Assert(pos < size());
572
573         // Find the inset.
574         InsetList::const_iterator cit = insetlist.begin();
575         InsetList::const_iterator end = insetlist.end();
576         for (; cit != end; ++cit) {
577                 if (cit->pos == pos)
578                         break;
579         }
580
581         if (cit != end && cit->pos == pos)
582                 return cit->inset;
583
584         lyxerr << "ERROR (Paragraph::getInset): "
585                << "Inset does not exist: " << pos << endl;
586         //::raise(SIGSTOP);
587         //text[pos] = ' '; // WHY!!! does this set the pos to ' '????
588         // Did this commenting out introduce a bug? So far I have not
589         // see any, please enlighten me. (Lgb)
590         // My guess is that since the inset does not exist, we might
591         // as well replace it with a space to prevent craches. (Asger)
592         return 0;
593 }
594
595
596 // Gets uninstantiated font setting at position.
597 LyXFont const Paragraph::getFontSettings(BufferParams const & bparams,
598                                          pos_type pos) const
599 {
600         lyx::Assert(pos <= size());
601
602         Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
603         Pimpl::FontList::const_iterator end = pimpl_->fontlist.end();
604         for (; cit != end; ++cit) {
605                 if (cit->pos() >= pos)
606                         break;
607         }
608
609         LyXFont retfont;
610         if (cit != end) {
611                 retfont = cit->font();
612         } else if (pos == size() && size()) {
613                 retfont = getFontSettings(bparams, pos - 1);
614         } else
615                 retfont = LyXFont(LyXFont::ALL_INHERIT, getParLanguage(bparams));
616 #ifdef INHERIT_LANGUAGE
617         if (retfont.language() == inherit_language)
618                 retfont.setLanguage(bparams.language);
619 #endif
620
621         return retfont;
622 }
623
624
625 // Gets uninstantiated font setting at position 0
626 LyXFont const Paragraph::getFirstFontSettings() const
627 {
628         if (size() > 0 && !pimpl_->fontlist.empty())
629                 return pimpl_->fontlist[0].font();
630
631         return LyXFont(LyXFont::ALL_INHERIT);
632 }
633
634
635 // Gets the fully instantiated font at a given position in a paragraph
636 // This is basically the same function as LyXText::GetFont() in text2.C.
637 // The difference is that this one is used for generating the LaTeX file,
638 // and thus cosmetic "improvements" are disallowed: This has to deliver
639 // the true picture of the buffer. (Asger)
640 // If position is -1, we get the layout font of the paragraph.
641 // If position is -2, we get the font of the manual label of the paragraph.
642 LyXFont const Paragraph::getFont(BufferParams const & bparams,
643                                  pos_type pos) const
644 {
645         lyx::Assert(pos >= 0);
646
647         LyXLayout const & lout =
648                 textclasslist[bparams.textclass][layout()];
649         pos_type main_body = 0;
650         if (lout.labeltype == LABEL_MANUAL)
651                 main_body = beginningOfMainBody();
652
653         LyXFont layoutfont;
654         if (pos < main_body)
655                 layoutfont = lout.labelfont;
656         else
657                 layoutfont = lout.font;
658
659         LyXFont tmpfont = getFontSettings(bparams, pos);
660 #ifndef INHERIT_LANGUAGE
661         tmpfont.realize(layoutfont);
662 #else
663         tmpfont.realize(layoutfont, bparams.language);
664 #endif
665
666         return pimpl_->realizeFont(tmpfont, bparams);
667 }
668
669
670 LyXFont const Paragraph::getLabelFont(BufferParams const & bparams) const
671 {
672         LyXLayout const & lout =
673                 textclasslist[bparams.textclass][layout()];
674
675         LyXFont tmpfont = lout.labelfont;
676         tmpfont.setLanguage(getParLanguage(bparams));
677
678         return pimpl_->realizeFont(tmpfont, bparams);
679 }
680
681
682 LyXFont const Paragraph::getLayoutFont(BufferParams const & bparams) const
683 {
684         LyXLayout const & lout =
685                 textclasslist[bparams.textclass][layout()];
686
687         LyXFont tmpfont = lout.font;
688         tmpfont.setLanguage(getParLanguage(bparams));
689
690         return pimpl_->realizeFont(tmpfont, bparams);
691 }
692
693
694 /// Returns the height of the highest font in range
695 LyXFont::FONT_SIZE
696 Paragraph::highestFontInRange(pos_type startpos, pos_type endpos,
697                               LyXFont::FONT_SIZE const def_size) const
698 {
699         if (pimpl_->fontlist.empty())
700                 return def_size;
701
702         Pimpl::FontList::const_iterator end_it = pimpl_->fontlist.begin();
703         Pimpl::FontList::const_iterator end = pimpl_->fontlist.end();
704         for (; end_it != end; ++end_it) {
705                 if (end_it->pos() >= endpos)
706                         break;
707         }
708
709         if (end_it != end)
710                 ++end_it;
711
712         Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
713         for (; cit != end; ++cit) {
714                 if (cit->pos() >= startpos)
715                         break;
716         }
717
718         LyXFont::FONT_SIZE maxsize = LyXFont::SIZE_TINY;
719         for (; cit != end_it; ++cit) {
720                 LyXFont::FONT_SIZE size = cit->font().size();
721                 if (size == LyXFont::INHERIT_SIZE)
722                         size = def_size;
723                 if (size > maxsize && size <= LyXFont::SIZE_HUGER)
724                         maxsize = size;
725         }
726         return maxsize;
727 }
728
729
730 Paragraph::value_type
731 Paragraph::getUChar(BufferParams const & bparams, pos_type pos) const
732 {
733         value_type c = getChar(pos);
734         if (!lyxrc.rtl_support)
735                 return c;
736
737         value_type uc = c;
738         switch (c) {
739         case '(':
740                 uc = ')';
741                 break;
742         case ')':
743                 uc = '(';
744                 break;
745         case '[':
746                 uc = ']';
747                 break;
748         case ']':
749                 uc = '[';
750                 break;
751         case '{':
752                 uc = '}';
753                 break;
754         case '}':
755                 uc = '{';
756                 break;
757         case '<':
758                 uc = '>';
759                 break;
760         case '>':
761                 uc = '<';
762                 break;
763         }
764         if (uc != c && getFontSettings(bparams, pos).isRightToLeft())
765                 return uc;
766         else
767                 return c;
768 }
769
770
771 void Paragraph::setFont(pos_type pos, LyXFont const & font)
772 {
773         lyx::Assert(pos <= size());
774
775         // First, reduce font against layout/label font
776         // Update: The SetCharFont() routine in text2.C already
777         // reduces font, so we don't need to do that here. (Asger)
778         // No need to simplify this because it will disappear
779         // in a new kernel. (Asger)
780         // Next search font table
781
782         Pimpl::FontList::iterator beg = pimpl_->fontlist.begin();
783         Pimpl::FontList::iterator it = beg;
784         Pimpl::FontList::iterator endit = pimpl_->fontlist.end();
785         for (; it != endit; ++it) {
786                 if (it->pos() >= pos)
787                         break;
788         }
789         unsigned int i = std::distance(beg, it);
790         bool notfound = (it == endit);
791
792         if (!notfound && pimpl_->fontlist[i].font() == font)
793                 return;
794
795         bool begin = pos == 0 || notfound ||
796                 (i > 0 && pimpl_->fontlist[i - 1].pos() == pos - 1);
797         // Is position pos is a beginning of a font block?
798         bool end = !notfound && pimpl_->fontlist[i].pos() == pos;
799         // Is position pos is the end of a font block?
800         if (begin && end) { // A single char block
801                 if (i + 1 < pimpl_->fontlist.size() &&
802                     pimpl_->fontlist[i + 1].font() == font) {
803                         // Merge the singleton block with the next block
804                         pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i);
805                         if (i > 0 && pimpl_->fontlist[i - 1].font() == font)
806                                 pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i-1);
807                 } else if (i > 0 && pimpl_->fontlist[i - 1].font() == font) {
808                         // Merge the singleton block with the previous block
809                         pimpl_->fontlist[i - 1].pos(pos);
810                         pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i);
811                 } else
812                         pimpl_->fontlist[i].font(font);
813         } else if (begin) {
814                 if (i > 0 && pimpl_->fontlist[i - 1].font() == font)
815                         pimpl_->fontlist[i - 1].pos(pos);
816                 else
817                         pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i,
818                                         Pimpl::FontTable(pos, font));
819         } else if (end) {
820                 pimpl_->fontlist[i].pos(pos - 1);
821                 if (!(i + 1 < pimpl_->fontlist.size() &&
822                       pimpl_->fontlist[i + 1].font() == font))
823                         pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i + 1,
824                                         Pimpl::FontTable(pos, font));
825         } else { // The general case. The block is splitted into 3 blocks
826                 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i,
827                                 Pimpl::FontTable(pos - 1, pimpl_->fontlist[i].font()));
828                 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i + 1,
829                                 Pimpl::FontTable(pos, font));
830         }
831 }
832
833
834
835 void Paragraph::next(Paragraph * p)
836 {
837         next_ = p;
838 }
839
840
841 // This function is able to hide closed footnotes.
842 Paragraph * Paragraph::next()
843 {
844         return next_;
845 }
846
847
848 Paragraph const * Paragraph::next() const
849 {
850         return next_;
851 }
852
853
854 void Paragraph::previous(Paragraph * p)
855 {
856         previous_ = p;
857 }
858
859
860 // This function is able to hide closed footnotes.
861 Paragraph * Paragraph::previous()
862 {
863         return previous_;
864 }
865
866
867 // This function is able to hide closed footnotes.
868 Paragraph const * Paragraph::previous() const
869 {
870         return previous_;
871 }
872
873
874 void Paragraph::breakParagraph(BufferParams const & bparams,
875                                   pos_type pos,
876                                   int flag)
877 {
878         // create a new paragraph
879         Paragraph * tmp = new Paragraph(this);
880         tmp->layout(textclasslist[bparams.textclass].defaultLayoutName());
881
882         // remember to set the inset_owner
883         tmp->setInsetOwner(inInset());
884
885         // this is an idea for a more userfriendly layout handling, I will
886         // see what the users say
887
888         // layout stays the same with latex-environments
889         if (flag) {
890                 tmp->layout(layout());
891                 tmp->setLabelWidthString(params().labelWidthString());
892         }
893
894         bool isempty = (textclasslist[bparams.textclass][layout()].keepempty &&
895                         !size());
896         if (!isempty && (size() > pos || !size() || flag == 2)) {
897                 tmp->layout(layout());
898                 tmp->params().align(params().align());
899                 tmp->setLabelWidthString(params().labelWidthString());
900
901                 tmp->params().lineBottom(params().lineBottom());
902                 params().lineBottom(false);
903                 tmp->params().pagebreakBottom(params().pagebreakBottom());
904                 params().pagebreakBottom(false);
905                 tmp->params().spaceBottom(params().spaceBottom());
906                 params().spaceBottom(VSpace(VSpace::NONE));
907
908                 tmp->params().depth(params().depth());
909                 tmp->params().noindent(params().noindent());
910
911                 // copy everything behind the break-position
912                 // to the new paragraph
913                 pos_type pos_end = size() - 1;
914                 pos_type i = pos;
915                 pos_type j = pos;
916                 for (; i <= pos_end; ++i) {
917                         cutIntoMinibuffer(bparams, i);
918                         if (tmp->insertFromMinibuffer(j - pos))
919                                 ++j;
920                 }
921                 for (i = pos_end; i >= pos; --i) {
922                         erase(i);
923                 }
924         }
925
926         // just an idea of me
927         if (!isempty && !pos) {
928                 tmp->params().lineTop(params().lineTop());
929                 tmp->params().pagebreakTop(params().pagebreakTop());
930                 tmp->params().spaceTop(params().spaceTop());
931                 tmp->bibkey = bibkey;
932
933                 bibkey = 0;
934                 params().clear();
935                 layout(textclasslist[bparams.textclass].defaultLayoutName());
936
937                 // layout stays the same with latex-environments
938                 if (flag) {
939                         layout(tmp->layout());
940                         setLabelWidthString(tmp->params().labelWidthString());
941                         params().depth(tmp->params().depth());
942                 }
943         }
944 }
945
946
947 void Paragraph::makeSameLayout(Paragraph const * par)
948 {
949         layout(par->layout());
950         // move to pimpl?
951         params() = par->params();
952 }
953
954
955 int Paragraph::stripLeadingSpaces(lyx::textclass_type tclass)
956 {
957         if (textclasslist[tclass][layout()].free_spacing ||
958             isFreeSpacing()) {
959                 return 0;
960         }
961
962         int i = 0;
963         while (size() && (isNewline(0) || isLineSeparator(0))) {
964                 erase(0);
965                 ++i;
966         }
967
968         return i;
969 }
970
971
972 bool Paragraph::hasSameLayout(Paragraph const * par) const
973 {
974         return
975                 par->layout() == layout() &&
976                 params().sameLayout(par->params());
977 }
978
979
980 void Paragraph::breakParagraphConservative(BufferParams const & bparams,
981                                            pos_type pos)
982 {
983         // create a new paragraph
984         Paragraph * tmp = new Paragraph(this);
985         tmp->makeSameLayout(this);
986
987         // When can pos > Last()?
988         // I guess pos == Last() is possible.
989         if (size() > pos) {
990                 // copy everything behind the break-position to the new
991                 // paragraph
992                 pos_type pos_end = size() - 1;
993
994                 //pos_type i = pos;
995                 //pos_type j = pos;
996                 for (pos_type i = pos, j = pos; i <= pos_end; ++i) {
997                         cutIntoMinibuffer(bparams, i);
998                         if (tmp->insertFromMinibuffer(j - pos))
999                                 ++j;
1000                 }
1001
1002                 for (pos_type k = pos_end; k >= pos; --k) {
1003                         erase(k);
1004                 }
1005         }
1006 }
1007
1008
1009 // Be carefull, this does not make any check at all.
1010 // This method has wrong name, it combined this par with the next par.
1011 // In that sense it is the reverse of break paragraph. (Lgb)
1012 void Paragraph::pasteParagraph(BufferParams const & bparams)
1013 {
1014         // copy the next paragraph to this one
1015         Paragraph * the_next = next();
1016
1017         // first the DTP-stuff
1018         params().lineBottom(the_next->params().lineBottom());
1019         params().spaceBottom(the_next->params().spaceBottom());
1020         params().pagebreakBottom(the_next->params().pagebreakBottom());
1021
1022         pos_type pos_end = the_next->pimpl_->size() - 1;
1023         pos_type pos_insert = size();
1024
1025         // ok, now copy the paragraph
1026         for (pos_type i = 0, j = 0; i <= pos_end; ++i) {
1027                 the_next->cutIntoMinibuffer(bparams, i);
1028                 if (insertFromMinibuffer(pos_insert + j))
1029                         ++j;
1030         }
1031
1032         // delete the next paragraph
1033         Paragraph * ppar = the_next->previous_;
1034         Paragraph * npar = the_next->next_;
1035         delete the_next;
1036         ppar->next(npar);
1037 }
1038
1039
1040 int Paragraph::getEndLabel(BufferParams const & bparams) const
1041 {
1042         Paragraph const * par = this;
1043         depth_type par_depth = getDepth();
1044         while (par) {
1045                 string const & layout = par->layout();
1046                 int const endlabeltype =
1047                         textclasslist[bparams.textclass][layout].endlabeltype;
1048                 if (endlabeltype != END_LABEL_NO_LABEL) {
1049                         if (!next_)
1050                                 return endlabeltype;
1051
1052                         depth_type const next_depth = next_->getDepth();
1053                         if (par_depth > next_depth ||
1054                             (par_depth == next_depth
1055                              && layout != next_->layout()))
1056                                 return endlabeltype;
1057                         break;
1058                 }
1059                 if (par_depth == 0)
1060                         break;
1061                 par = par->outerHook();
1062                 if (par)
1063                         par_depth = par->getDepth();
1064         }
1065         return END_LABEL_NO_LABEL;
1066 }
1067
1068
1069 Paragraph::depth_type Paragraph::getDepth() const
1070 {
1071         return params().depth();
1072 }
1073
1074
1075 Paragraph::depth_type Paragraph::getMaxDepthAfter(Buffer const * buffer) const
1076 {
1077         bool const isenv = textclasslist[buffer->params.textclass][layout()].isEnvironment();
1078
1079         if (isenv)
1080                 return params().depth() + 1;
1081         else
1082                 return params().depth();
1083
1084 }
1085
1086 char Paragraph::getAlign() const
1087 {
1088         return params().align();
1089 }
1090
1091
1092 string const & Paragraph::getLabelstring() const
1093 {
1094         return params().labelString();
1095 }
1096
1097
1098 int Paragraph::getFirstCounter(int i) const
1099 {
1100         return pimpl_->counter_[i];
1101 }
1102
1103
1104 // the next two functions are for the manual labels
1105 string const Paragraph::getLabelWidthString() const
1106 {
1107         if (!params().labelWidthString().empty())
1108                 return params().labelWidthString();
1109         else
1110                 return _("Senseless with this layout!");
1111 }
1112
1113
1114 void Paragraph::setLabelWidthString(string const & s)
1115 {
1116         params().labelWidthString(s);
1117 }
1118
1119
1120 void Paragraph::applyLayout(string const & new_layout)
1121 {
1122         layout(new_layout);
1123         params().labelWidthString(string());
1124         params().align(LYX_ALIGN_LAYOUT);
1125         params().spaceTop(VSpace(VSpace::NONE));
1126         params().spaceBottom(VSpace(VSpace::NONE));
1127         params().spacing(Spacing(Spacing::Default));
1128 }
1129
1130
1131 // if the layout of a paragraph contains a manual label, the beginning of the
1132 // main body is the beginning of the second word. This is what the par-
1133 // function returns. If the layout does not contain a label, the main
1134 // body always starts with position 0. This differentiation is necessary,
1135 // because there cannot be a newline or a blank <= the beginning of the
1136 // main body in TeX.
1137
1138 int Paragraph::beginningOfMainBody() const
1139 {
1140         // Unroll the first two cycles of the loop
1141         // and remember the previous character to
1142         // remove unnecessary GetChar() calls
1143         pos_type i = 0;
1144         if (i < size() && getChar(i) != Paragraph::META_NEWLINE) {
1145                 ++i;
1146                 char previous_char = 0;
1147                 char temp = 0;
1148                 if (i < size()
1149                     && (previous_char = getChar(i)) != Paragraph::META_NEWLINE) {
1150                         // Yes, this  ^ is supposed to be "= " not "=="
1151                         ++i;
1152                         while (i < size()
1153                                && previous_char != ' '
1154                                && (temp = getChar(i)) != Paragraph::META_NEWLINE) {
1155                                 ++i;
1156                                 previous_char = temp;
1157                         }
1158                 }
1159         }
1160
1161         return i;
1162 }
1163
1164
1165 Paragraph * Paragraph::depthHook(depth_type depth)
1166 {
1167         Paragraph * newpar = this;
1168
1169         do {
1170                 newpar = newpar->previous();
1171         } while (newpar && newpar->getDepth() > depth);
1172
1173         if (!newpar) {
1174                 if (previous() || getDepth())
1175                         lyxerr << "ERROR (Paragraph::DepthHook): "
1176                                 "no hook." << endl;
1177                 newpar = this;
1178         }
1179
1180         return newpar;
1181 }
1182
1183
1184 Paragraph const * Paragraph::depthHook(depth_type depth) const
1185 {
1186         Paragraph const * newpar = this;
1187
1188         do {
1189                 newpar = newpar->previous();
1190         } while (newpar && newpar->getDepth() > depth);
1191
1192         if (!newpar) {
1193                 if (previous() || getDepth())
1194                         lyxerr << "ERROR (Paragraph::DepthHook): "
1195                                 "no hook." << endl;
1196                 newpar = this;
1197         }
1198
1199         return newpar;
1200 }
1201
1202 Paragraph * Paragraph::outerHook()
1203 {
1204         if (!getDepth())
1205                 return 0;
1206         return depthHook(depth_type(getDepth() - 1));
1207 }
1208
1209 Paragraph const * Paragraph::outerHook() const
1210 {
1211         if (!getDepth())
1212                 return 0;
1213         return depthHook(depth_type(getDepth() - 1));
1214 }
1215
1216
1217 Paragraph::inset_iterator
1218 Paragraph::InsetIterator(pos_type pos)
1219 {
1220         InsetList::iterator it = insetlist.begin();
1221         InsetList::iterator end = insetlist.end();
1222         for (; it != end; ++it) {
1223                 if (it->pos >= pos)
1224                         break;
1225         }
1226
1227         return inset_iterator(it);
1228 }
1229
1230
1231 // returns -1 if inset not found
1232 int Paragraph::getPositionOfInset(Inset const * inset) const
1233 {
1234         // Find the entry.
1235         InsetList::const_iterator cit = insetlist.begin();
1236         InsetList::const_iterator end = insetlist.end();
1237         for (; cit != end; ++cit) {
1238                 if (cit->inset == inset) {
1239                         return cit->pos;
1240                 }
1241         }
1242         if (inset == bibkey)
1243                 return 0;
1244
1245         return -1;
1246 }
1247
1248
1249 Paragraph * Paragraph::TeXOnePar(Buffer const * buf,
1250                                  BufferParams const & bparams,
1251                                  ostream & os, TexRow & texrow,
1252                                  bool moving_arg)
1253 {
1254         lyxerr[Debug::LATEX] << "TeXOnePar...     " << this << endl;
1255         Inset const * in = inInset();
1256         bool further_blank_line = false;
1257         LyXLayout style;
1258
1259         // well we have to check if we are in an inset with unlimited
1260         // lenght (all in one row) if that is true then we don't allow
1261         // any special options in the paragraph and also we don't allow
1262         // any environment other then "Standard" to be valid!
1263         if ((in == 0) || !in->forceDefaultParagraphs(in)) {
1264                 style = textclasslist[bparams.textclass][layout()];
1265
1266                 if (params().startOfAppendix()) {
1267                         os << "\\appendix\n";
1268                         texrow.newline();
1269                 }
1270
1271                 if (!params().spacing().isDefault()
1272                         && (!previous() || !previous()->hasSameLayout(this))) {
1273                         os << params().spacing().writeEnvirBegin() << "\n";
1274                         texrow.newline();
1275                 }
1276
1277                 if (tex_code_break_column && style.isCommand()) {
1278                         os << '\n';
1279                         texrow.newline();
1280                 }
1281
1282                 if (params().pagebreakTop()) {
1283                         os << "\\newpage";
1284                         further_blank_line = true;
1285                 }
1286                 if (params().spaceTop().kind() != VSpace::NONE) {
1287                         os << params().spaceTop().asLatexCommand(bparams);
1288                         further_blank_line = true;
1289                 }
1290
1291                 if (params().lineTop()) {
1292                         os << "\\lyxline{\\" << getFont(bparams, 0).latexSize() << '}'
1293                            << "\\vspace{-1\\parskip}";
1294                         further_blank_line = true;
1295                 }
1296
1297                 if (further_blank_line) {
1298                         os << '\n';
1299                         texrow.newline();
1300                 }
1301         } else {
1302                 style = textclasslist[bparams.textclass].defaultLayout();
1303         }
1304
1305         Language const * language = getParLanguage(bparams);
1306         Language const * doc_language = bparams.language;
1307         Language const * previous_language = previous()
1308                 ? previous()->getParLanguage(bparams) : doc_language;
1309
1310         if (language->babel() != previous_language->babel()
1311             // check if we already put language command in TeXEnvironment()
1312             && !(style.isEnvironment()
1313                  && (!previous() || previous()->layout() != layout() ||
1314                          previous()->params().depth() != params().depth())))
1315         {
1316                 if (!lyxrc.language_command_end.empty() &&
1317                     previous_language->babel() != doc_language->babel())
1318                 {
1319                         os << subst(lyxrc.language_command_end, "$$lang",
1320                                     previous_language->babel())
1321                            << endl;
1322                         texrow.newline();
1323                 }
1324
1325                 if (lyxrc.language_command_end.empty() ||
1326                     language->babel() != doc_language->babel())
1327                 {
1328                         os << subst(lyxrc.language_command_begin, "$$lang",
1329                                     language->babel())
1330                            << endl;
1331                         texrow.newline();
1332                 }
1333         }
1334
1335         if (bparams.inputenc == "auto" &&
1336             language->encoding() != previous_language->encoding()) {
1337                 os << "\\inputencoding{"
1338                    << language->encoding()->LatexName()
1339                    << "}" << endl;
1340                 texrow.newline();
1341         }
1342
1343         switch (style.latextype) {
1344         case LATEX_COMMAND:
1345                 os << '\\'
1346                    << style.latexname()
1347                    << style.latexparam();
1348                 break;
1349         case LATEX_ITEM_ENVIRONMENT:
1350                 if (bibkey) {
1351                         bibkey->latex(buf, os, false, false);
1352                 } else
1353                         os << "\\item ";
1354                 break;
1355         case LATEX_LIST_ENVIRONMENT:
1356                 os << "\\item ";
1357                 break;
1358         default:
1359                 break;
1360         }
1361
1362         bool need_par = simpleTeXOnePar(buf, bparams, os, texrow, moving_arg);
1363
1364         // Make sure that \\par is done with the font of the last
1365         // character if this has another size as the default.
1366         // This is necessary because LaTeX (and LyX on the screen)
1367         // calculates the space between the baselines according
1368         // to this font. (Matthias)
1369         //
1370         // Is this really needed ? (Dekel)
1371         // We do not need to use to change the font for the last paragraph
1372         // or for a command.
1373         LyXFont const font =
1374                 (size() == 0
1375                  ? getLayoutFont(bparams) : getFont(bparams, size() - 1));
1376
1377         bool is_command = style.isCommand();
1378
1379         if (style.resfont.size() != font.size() && next_ && !is_command) {
1380                 if (!need_par)
1381                         os << "{";
1382                 os << "\\" << font.latexSize() << " \\par}";
1383         } else if (need_par) {
1384                 os << "\\par}";
1385         } else if (is_command)
1386                 os << "}";
1387
1388         switch (style.latextype) {
1389         case LATEX_ITEM_ENVIRONMENT:
1390         case LATEX_LIST_ENVIRONMENT:
1391                 if (next_ && (params().depth() < next_->params().depth())) {
1392                         os << '\n';
1393                         texrow.newline();
1394                 }
1395                 break;
1396         case LATEX_ENVIRONMENT:
1397                 // if its the last paragraph of the current environment
1398                 // skip it otherwise fall through
1399                 if (next_
1400                     && (next_->layout() != layout()
1401                         || next_->params().depth() != params().depth()))
1402                         break;
1403                 // fall through possible
1404         default:
1405                 // we don't need it for the last paragraph!!!
1406                 if (next_) {
1407                         os << '\n';
1408                         texrow.newline();
1409                 }
1410         }
1411
1412         if ((in == 0) || !in->forceDefaultParagraphs(in)) {
1413                 further_blank_line = false;
1414                 if (params().lineBottom()) {
1415                         os << "\\lyxline{\\" << font.latexSize() << '}';
1416                         further_blank_line = true;
1417                 }
1418
1419                 if (params().spaceBottom().kind() != VSpace::NONE) {
1420                         os << params().spaceBottom().asLatexCommand(bparams);
1421                         further_blank_line = true;
1422                 }
1423
1424                 if (params().pagebreakBottom()) {
1425                         os << "\\newpage";
1426                         further_blank_line = true;
1427                 }
1428
1429                 if (further_blank_line) {
1430                         os << '\n';
1431                         texrow.newline();
1432                 }
1433
1434                 if (!params().spacing().isDefault()
1435                         && (!next_ || !next_->hasSameLayout(this))) {
1436                         os << params().spacing().writeEnvirEnd() << "\n";
1437                         texrow.newline();
1438                 }
1439         }
1440
1441         // we don't need it for the last paragraph!!!
1442         if (next_) {
1443                 os << '\n';
1444                 texrow.newline();
1445         } else {
1446                 // Since \selectlanguage write the language to the aux file,
1447                 // we need to reset the language at the end of footnote or
1448                 // float.
1449
1450                 if (language->babel() != doc_language->babel()) {
1451                         if (lyxrc.language_command_end.empty())
1452                                 os << subst(lyxrc.language_command_begin,
1453                                             "$$lang",
1454                                             doc_language->babel())
1455                                    << endl;
1456                         else
1457                                 os << subst(lyxrc.language_command_end,
1458                                             "$$lang",
1459                                             language->babel())
1460                                    << endl;
1461                         texrow.newline();
1462                 }
1463         }
1464
1465         lyxerr[Debug::LATEX] << "TeXOnePar...done " << next_ << endl;
1466         return next_;
1467 }
1468
1469 // This could go to ParagraphParameters if we want to
1470 int Paragraph::startTeXParParams(BufferParams const & bparams,
1471                                  ostream & os) const
1472 {
1473         int column = 0;
1474
1475         if (params().noindent()) {
1476                 os << "\\noindent ";
1477                 column += 10;
1478         }
1479
1480         switch (params().align()) {
1481         case LYX_ALIGN_NONE:
1482         case LYX_ALIGN_BLOCK:
1483         case LYX_ALIGN_LAYOUT:
1484         case LYX_ALIGN_SPECIAL:
1485                 break;
1486         case LYX_ALIGN_LEFT:
1487                 if (getParLanguage(bparams)->babel() != "hebrew") {
1488                         os << "\\begin{flushleft}";
1489                         column += 17;
1490                 } else {
1491                         os << "\\begin{flushright}";
1492                         column += 18;
1493                 }
1494                 break;
1495         case LYX_ALIGN_RIGHT:
1496                 if (getParLanguage(bparams)->babel() != "hebrew") {
1497                         os << "\\begin{flushright}";
1498                         column += 18;
1499                 } else {
1500                         os << "\\begin{flushleft}";
1501                         column += 17;
1502                 }
1503                 break;
1504         case LYX_ALIGN_CENTER:
1505                 os << "\\begin{center}";
1506                 column += 14;
1507                 break;
1508         }
1509
1510         return column;
1511 }
1512
1513 // This could go to ParagraphParameters if we want to
1514 int Paragraph::endTeXParParams(BufferParams const & bparams,
1515                                ostream & os) const
1516 {
1517         int column = 0;
1518
1519         switch (params().align()) {
1520         case LYX_ALIGN_NONE:
1521         case LYX_ALIGN_BLOCK:
1522         case LYX_ALIGN_LAYOUT:
1523         case LYX_ALIGN_SPECIAL:
1524                 break;
1525         case LYX_ALIGN_LEFT:
1526                 if (getParLanguage(bparams)->babel() != "hebrew") {
1527                         os << "\\end{flushleft}";
1528                         column = 15;
1529                 } else {
1530                         os << "\\end{flushright}";
1531                         column = 16;
1532                 }
1533                 break;
1534         case LYX_ALIGN_RIGHT:
1535                 if (getParLanguage(bparams)->babel() != "hebrew") {
1536                         os << "\\end{flushright}";
1537                         column+= 16;
1538                 } else {
1539                         os << "\\end{flushleft}";
1540                         column = 15;
1541                 }
1542                 break;
1543         case LYX_ALIGN_CENTER:
1544                 os << "\\end{center}";
1545                 column = 12;
1546                 break;
1547         }
1548         return column;
1549 }
1550
1551
1552 // This one spits out the text of the paragraph
1553 bool Paragraph::simpleTeXOnePar(Buffer const * buf,
1554                                 BufferParams const & bparams,
1555                                 ostream & os, TexRow & texrow,
1556                                 bool moving_arg)
1557 {
1558         lyxerr[Debug::LATEX] << "SimpleTeXOnePar...     " << this << endl;
1559
1560         bool return_value = false;
1561
1562         LyXLayout style;
1563
1564         // well we have to check if we are in an inset with unlimited
1565         // lenght (all in one row) if that is true then we don't allow
1566         // any special options in the paragraph and also we don't allow
1567         // any environment other then "Standard" to be valid!
1568         bool asdefault =
1569                 (inInset() && inInset()->forceDefaultParagraphs(inInset()));
1570
1571         if (asdefault) {
1572                 style = textclasslist[bparams.textclass].defaultLayout();
1573         } else {
1574                 style = textclasslist[bparams.textclass][layout()];
1575         }
1576
1577         LyXFont basefont;
1578
1579         // Maybe we have to create a optional argument.
1580         pos_type main_body;
1581         if (style.labeltype != LABEL_MANUAL)
1582                 main_body = 0;
1583         else
1584                 main_body = beginningOfMainBody();
1585
1586         int column = 0;
1587
1588         if (main_body > 0) {
1589                 os << '[';
1590                 ++column;
1591                 basefont = getLabelFont(bparams);
1592         } else {
1593                 basefont = getLayoutFont(bparams);
1594         }
1595
1596         moving_arg |= style.needprotect;
1597
1598         // Which font is currently active?
1599         LyXFont running_font(basefont);
1600         // Do we have an open font change?
1601         bool open_font = false;
1602
1603         texrow.start(this, 0);
1604
1605         // if the paragraph is empty, the loop will not be entered at all
1606         if (!size()) {
1607                 if (style.isCommand()) {
1608                         os << '{';
1609                         ++column;
1610                 }
1611                 if (!asdefault)
1612                         column += startTeXParParams(bparams, os);
1613
1614         }
1615
1616         for (pos_type i = 0; i < size(); ++i) {
1617                 ++column;
1618                 // First char in paragraph or after label?
1619                 if (i == main_body) {
1620                         if (main_body > 0) {
1621                                 if (open_font) {
1622                                         column += running_font.latexWriteEndChanges(os, basefont, basefont);
1623                                         open_font = false;
1624                                 }
1625                                 basefont = getLayoutFont(bparams);
1626                                 running_font = basefont;
1627                                 os << ']';
1628                                 ++column;
1629                         }
1630                         if (style.isCommand()) {
1631                                 os << '{';
1632                                 ++column;
1633                         }
1634
1635                         if (!asdefault)
1636                                 column += startTeXParParams(bparams, os);
1637                 }
1638
1639                 value_type c = getChar(i);
1640
1641                 // Fully instantiated font
1642                 LyXFont font = getFont(bparams, i);
1643
1644                 LyXFont const last_font = running_font;
1645
1646                 // Spaces at end of font change are simulated to be
1647                 // outside font change, i.e. we write "\textXX{text} "
1648                 // rather than "\textXX{text }". (Asger)
1649                 if (open_font && c == ' ' && i <= size() - 2) {
1650                         LyXFont const & next_font = getFont(bparams, i + 1);
1651                         if (next_font != running_font
1652                             && next_font != font) {
1653                                 font = next_font;
1654                         }
1655                 }
1656
1657                 // We end font definition before blanks
1658                 if (open_font &&
1659                     (font != running_font ||
1660                      font.language() != running_font.language()))
1661                 {
1662                         column += running_font.latexWriteEndChanges(os,
1663                                                                     basefont,
1664                                                                     (i == main_body-1) ? basefont : font);
1665                         running_font = basefont;
1666                         open_font = false;
1667                 }
1668
1669                 // Blanks are printed before start of fontswitch
1670                 if (c == ' ') {
1671                         // Do not print the separation of the optional argument
1672                         if (i != main_body - 1) {
1673                                 pimpl_->simpleTeXBlanks(os, texrow, i,
1674                                                        column, font, style);
1675                         }
1676                 }
1677
1678                 // Do we need to change font?
1679                 if ((font != running_font ||
1680                      font.language() != running_font.language()) &&
1681                         i != main_body - 1)
1682                 {
1683                         column += font.latexWriteStartChanges(os, basefont,
1684                                                               last_font);
1685                         running_font = font;
1686                         open_font = true;
1687                 }
1688
1689                 if (c == Paragraph::META_NEWLINE) {
1690                         // newlines are handled differently here than
1691                         // the default in SimpleTeXSpecialChars().
1692                         if (!style.newline_allowed) {
1693                                 os << '\n';
1694                         } else {
1695                                 if (open_font) {
1696                                         column += running_font.latexWriteEndChanges(os, basefont, basefont);
1697                                         open_font = false;
1698                                 }
1699                                 basefont = getLayoutFont(bparams);
1700                                 running_font = basefont;
1701                                 if (font.family() ==
1702                                     LyXFont::TYPEWRITER_FAMILY) {
1703                                         os << "~";
1704                                 }
1705                                 if (moving_arg)
1706                                         os << "\\protect ";
1707
1708                                 os << "\\\\\n";
1709                         }
1710                         texrow.newline();
1711                         texrow.start(this, i + 1);
1712                         column = 0;
1713                 } else {
1714                         pimpl_->simpleTeXSpecialChars(buf, bparams,
1715                                                       os, texrow, moving_arg,
1716                                                       font, running_font,
1717                                                       basefont, open_font,
1718                                                       style, i, column, c);
1719                 }
1720         }
1721
1722         // If we have an open font definition, we have to close it
1723         if (open_font) {
1724 #ifdef FIXED_LANGUAGE_END_DETECTION
1725                 if (next_) {
1726                         running_font
1727                                 .latexWriteEndChanges(os, basefont,
1728                                                       next_->getFont(bparams,
1729                                                       0));
1730                 } else {
1731                         running_font.latexWriteEndChanges(os, basefont,
1732                                                           basefont);
1733                 }
1734 #else
1735 #ifdef WITH_WARNINGS
1736 //#warning For now we ALWAYS have to close the foreign font settings if they are
1737 //#warning there as we start another \selectlanguage with the next paragraph if
1738 //#warning we are in need of this. This should be fixed sometime (Jug)
1739 #endif
1740                 running_font.latexWriteEndChanges(os, basefont,  basefont);
1741 #endif
1742         }
1743
1744         // Needed if there is an optional argument but no contents.
1745         if (main_body > 0 && main_body == size()) {
1746                 os << "]~";
1747                 return_value = false;
1748         }
1749
1750         if (!asdefault) {
1751                 column += endTeXParParams(bparams, os);
1752         }
1753
1754         lyxerr[Debug::LATEX] << "SimpleTeXOnePar...done " << this << endl;
1755         return return_value;
1756 }
1757
1758
1759 bool Paragraph::sgmlConvertChar(char c, string & sgml_string)
1760 {
1761         bool retval = false;
1762         switch (c) {
1763         case Paragraph::META_HFILL:
1764                 sgml_string.erase();
1765                 break;
1766         case Paragraph::META_NEWLINE:
1767                 sgml_string = '\n';
1768                 break;
1769         case '&':
1770                 sgml_string = "&amp;";
1771                 break;
1772         case '<':
1773                 sgml_string = "&lt;";
1774                 break;
1775         case '>':
1776                 sgml_string = "&gt;";
1777                 break;
1778         case '$':
1779                 sgml_string = "&dollar;";
1780                 break;
1781         case '#':
1782                 sgml_string = "&num;";
1783                 break;
1784         case '%':
1785                 sgml_string = "&percnt;";
1786                 break;
1787         case '[':
1788                 sgml_string = "&lsqb;";
1789                 break;
1790         case ']':
1791                 sgml_string = "&rsqb;";
1792                 break;
1793         case '{':
1794                 sgml_string = "&lcub;";
1795                 break;
1796         case '}':
1797                 sgml_string = "&rcub;";
1798                 break;
1799         case '~':
1800                 sgml_string = "&tilde;";
1801                 break;
1802         case '"':
1803                 sgml_string = "&quot;";
1804                 break;
1805         case '\\':
1806                 sgml_string = "&bsol;";
1807                 break;
1808         case ' ':
1809                 retval = true;
1810                 sgml_string = ' ';
1811                 break;
1812         case '\0': // Ignore :-)
1813                 sgml_string.erase();
1814                 break;
1815         default:
1816                 sgml_string = c;
1817                 break;
1818         }
1819         return retval;
1820 }
1821
1822
1823 Paragraph * Paragraph::TeXEnvironment(Buffer const * buf,
1824                                             BufferParams const & bparams,
1825                                             ostream & os, TexRow & texrow)
1826 {
1827         lyxerr[Debug::LATEX] << "TeXEnvironment...     " << this << endl;
1828
1829         LyXLayout const & style =
1830                 textclasslist[bparams.textclass][layout()];
1831
1832         Language const * language = getParLanguage(bparams);
1833         Language const * doc_language = bparams.language;
1834         Language const * previous_language = previous_
1835                 ? previous_->getParLanguage(bparams) : doc_language;
1836         if (language->babel() != previous_language->babel()) {
1837
1838                 if (!lyxrc.language_command_end.empty() &&
1839                     previous_language->babel() != doc_language->babel()) {
1840                         os << subst(lyxrc.language_command_end, "$$lang",
1841                                     previous_language->babel())
1842                            << endl;
1843                         texrow.newline();
1844                 }
1845
1846                 if (lyxrc.language_command_end.empty() ||
1847                     language->babel() != doc_language->babel()) {
1848                         os << subst(lyxrc.language_command_begin, "$$lang",
1849                                     language->babel())
1850                            << endl;
1851                         texrow.newline();
1852                 }
1853         }
1854
1855         bool leftindent_open = false;
1856         if (!params().leftIndent().zero()) {
1857                 os << "\\begin{LyXParagraphLeftIndent}{" <<
1858                         params().leftIndent().asLatexString() << "}\n";
1859                 texrow.newline();
1860                 leftindent_open = true;
1861         }
1862
1863         if (style.isEnvironment()) {
1864                 if (style.latextype == LATEX_LIST_ENVIRONMENT) {
1865                         os << "\\begin{" << style.latexname() << "}{"
1866                            << params().labelWidthString() << "}\n";
1867                 } else if (style.labeltype == LABEL_BIBLIO) {
1868                         // ale970405
1869                         os << "\\begin{" << style.latexname() << "}{"
1870                            <<  bibitemWidest(buf)
1871                            << "}\n";
1872                 } else if (style.latextype == LATEX_ITEM_ENVIRONMENT) {
1873                         os << "\\begin{" << style.latexname() << '}'
1874                            << style.latexparam() << '\n';
1875                 } else
1876                         os << "\\begin{" << style.latexname() << '}'
1877                            << style.latexparam() << '\n';
1878                 texrow.newline();
1879         }
1880         Paragraph * par = this;
1881         do {
1882                 par = par->TeXOnePar(buf, bparams, os, texrow, false);
1883
1884                 if (par && par->params().depth() > params().depth()) {
1885                         if (textclasslist[bparams.textclass][par->layout()].isParagraph()
1886                             // Thinko!
1887                             // How to handle this? (Lgb)
1888                             //&& !suffixIs(os, "\n\n")
1889                                 ) {
1890                                 // There should be at least one '\n' already
1891                                 // but we need there to be two for Standard
1892                                 // paragraphs that are depth-increment'ed to be
1893                                 // output correctly.  However, tables can
1894                                 // also be paragraphs so don't adjust them.
1895                                 // ARRae
1896                                 // Thinkee:
1897                                 // Will it ever harm to have one '\n' too
1898                                 // many? i.e. that we sometimes will have
1899                                 // three in a row. (Lgb)
1900                                 os << '\n';
1901                                 texrow.newline();
1902                         }
1903                         par = par->pimpl_->TeXDeeper(buf, bparams, os, texrow);
1904                 }
1905         } while (par
1906                  && par->layout() == layout()
1907                  && par->params().depth() == params().depth()
1908                  && par->params().leftIndent() == params().leftIndent());
1909
1910         if (style.isEnvironment()) {
1911                 os << "\\end{" << style.latexname() << "}\n";
1912                 texrow.newline();
1913         }
1914
1915         if (leftindent_open) {
1916                 os << "\\end{LyXParagraphLeftIndent}\n";
1917                 texrow.newline();
1918         }
1919
1920         lyxerr[Debug::LATEX] << "TeXEnvironment...done " << par << endl;
1921         return par;  // ale970302
1922 }
1923
1924
1925 bool Paragraph::isHfill(pos_type pos) const
1926 {
1927         return IsHfillChar(getChar(pos));
1928 }
1929
1930
1931 bool Paragraph::isInset(pos_type pos) const
1932 {
1933         return IsInsetChar(getChar(pos));
1934 }
1935
1936
1937 bool Paragraph::isNewline(pos_type pos) const
1938 {
1939         return pos >= 0 && IsNewlineChar(getChar(pos));
1940 }
1941
1942
1943 bool Paragraph::isSeparator(pos_type pos) const
1944 {
1945         return IsSeparatorChar(getChar(pos));
1946 }
1947
1948
1949 bool Paragraph::isLineSeparator(pos_type pos) const
1950 {
1951         value_type const c = getChar(pos);
1952         return IsLineSeparatorChar(c)
1953                 || (IsInsetChar(c) && getInset(pos) &&
1954                 getInset(pos)->isLineSeparator());
1955 }
1956
1957
1958 bool Paragraph::isKomma(pos_type pos) const
1959 {
1960         return IsKommaChar(getChar(pos));
1961 }
1962
1963
1964 /// Used by the spellchecker
1965 bool Paragraph::isLetter(pos_type pos) const
1966 {
1967         value_type const c = getChar(pos);
1968         if (IsLetterChar(c))
1969                 return true;
1970         if (isInset(pos))
1971                 return getInset(pos)->isLetter();
1972         // We want to pass the ' and escape chars to ispell
1973         string const extra = lyxrc.isp_esc_chars + '\'';
1974         return contains(extra, c);
1975 }
1976
1977
1978 bool Paragraph::isWord(pos_type pos) const
1979 {
1980         return IsWordChar(getChar(pos)) ;
1981 }
1982
1983
1984 Language const *
1985 Paragraph::getParLanguage(BufferParams const & bparams) const
1986 {
1987         if (size() > 0) {
1988 #ifndef INHERIT_LANGUAGE
1989                 return getFirstFontSettings().language();
1990 #else
1991                 Language const * lang = getFirstFontSettings().language();
1992 #ifdef WITH_WARNINGS
1993 #warning We should make this somewhat better, any ideas? (Jug)
1994 #endif
1995                 if (lang == inherit_language || lang == ignore_language)
1996                         lang = bparams.language;
1997                 return lang;
1998 #endif
1999         } else if (previous_)
2000                 return previous_->getParLanguage(bparams);
2001         else
2002                 return bparams.language;
2003 }
2004
2005
2006 bool Paragraph::isRightToLeftPar(BufferParams const & bparams) const
2007 {
2008         return lyxrc.rtl_support
2009                 && getParLanguage(bparams)->RightToLeft()
2010                 && !(inInset() && inInset()->owner() &&
2011                      inInset()->owner()->lyxCode() == Inset::ERT_CODE);
2012 }
2013
2014
2015 void Paragraph::changeLanguage(BufferParams const & bparams,
2016                                   Language const * from, Language const * to)
2017 {
2018         for (pos_type i = 0; i < size(); ++i) {
2019                 LyXFont font = getFontSettings(bparams, i);
2020                 if (font.language() == from) {
2021                         font.setLanguage(to);
2022                         setFont(i, font);
2023                 }
2024         }
2025 }
2026
2027
2028 bool Paragraph::isMultiLingual(BufferParams const & bparams)
2029 {
2030         Language const * doc_language = bparams.language;
2031         Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
2032         Pimpl::FontList::const_iterator end = pimpl_->fontlist.end();
2033
2034         for (; cit != end; ++cit)
2035                 if (cit->font().language() != ignore_language &&
2036                     cit->font().language() != latex_language &&
2037 #ifdef INHERIT_LANGUAGE
2038                         cit->font().language() != inherit_language &&
2039 #endif
2040                         cit->font().language() != doc_language)
2041                         return true;
2042         return false;
2043 }
2044
2045
2046 // Convert the paragraph to a string.
2047 // Used for building the table of contents
2048 string const Paragraph::asString(Buffer const * buffer, bool label)
2049 {
2050         BufferParams const & bparams = buffer->params;
2051         string s;
2052         if (label && !params().labelString().empty())
2053                 s += params().labelString() + ' ';
2054         string::size_type const len = s.size();
2055
2056         for (pos_type i = 0; i < size(); ++i) {
2057                 value_type c = getChar(i);
2058                 if (IsPrintable(c))
2059                         s += c;
2060                 else if (c == META_INSET &&
2061                          getInset(i)->lyxCode() == Inset::MATH_CODE) {
2062                         ostringstream ost;
2063                         getInset(i)->ascii(buffer, ost);
2064                         s += subst(ost.str().c_str(),'\n',' ');
2065                 }
2066         }
2067
2068         if (isRightToLeftPar(bparams))
2069                 reverse(s.begin() + len,s.end());
2070
2071         return s;
2072 }
2073
2074
2075 string const Paragraph::asString(Buffer const * buffer,
2076                                  pos_type beg, pos_type end, bool label)
2077 {
2078         ostringstream ost;
2079
2080         if (beg == 0 && label && !params().labelString().empty())
2081                 ost << params().labelString() << ' ';
2082
2083         for (pos_type i = beg; i < end; ++i) {
2084                 value_type const c = getUChar(buffer->params, i);
2085                 if (IsPrintable(c))
2086                         ost << c;
2087                 else if (c == META_NEWLINE)
2088                         ost << '\n';
2089                 else if (c == META_HFILL)
2090                         ost << '\t';
2091                 else if (c == META_INSET) {
2092                         getInset(i)->ascii(buffer, ost);
2093                 }
2094         }
2095
2096         return ost.str().c_str();
2097 }
2098
2099
2100 void Paragraph::setInsetOwner(Inset * i)
2101 {
2102         pimpl_->inset_owner = i;
2103         InsetList::const_iterator cit = insetlist.begin();
2104         InsetList::const_iterator end = insetlist.end();
2105         for (; cit != end; ++cit) {
2106                 if (cit->inset)
2107                         cit->inset->setOwner(i);
2108         }
2109 }
2110
2111
2112 void Paragraph::deleteInsetsLyXText(BufferView * bv)
2113 {
2114         // then the insets
2115         InsetList::const_iterator cit = insetlist.begin();
2116         InsetList::const_iterator end = insetlist.end();
2117         for (; cit != end; ++cit) {
2118                 if (cit->inset && cit->inset->isTextInset()) {
2119                         static_cast<UpdatableInset *>
2120                                 (cit->inset)->deleteLyXText(bv, true);
2121                 }
2122         }
2123 }
2124
2125
2126 void Paragraph::resizeInsetsLyXText(BufferView * bv)
2127 {
2128         // then the insets
2129         InsetList::const_iterator cit = insetlist.begin();
2130         InsetList::const_iterator end = insetlist.end();
2131         for (; cit != end; ++cit) {
2132                 if (cit->inset) {
2133                         if (cit->inset->isTextInset()) {
2134                                 static_cast<UpdatableInset *>
2135                                         (cit->inset)->resizeLyXText(bv, true);
2136                         }
2137                 }
2138         }
2139 }
2140
2141
2142 void Paragraph::setContentsFromPar(Paragraph * par)
2143 {
2144         pimpl_->setContentsFromPar(par);
2145 }
2146
2147
2148 lyx::pos_type Paragraph::size() const
2149 {
2150         return pimpl_->size();
2151 }
2152
2153
2154 Paragraph::value_type Paragraph::getChar(pos_type pos) const
2155 {
2156         return pimpl_->getChar(pos);
2157 }
2158
2159
2160 int Paragraph::id() const
2161 {
2162         return pimpl_->id_;
2163 }
2164
2165
2166 void  Paragraph::id(int id_arg)
2167 {
2168         pimpl_->id_ = id_arg;
2169 }
2170
2171
2172 string const & Paragraph::layout() const
2173 {
2174         return layout_;
2175 }
2176
2177
2178 void Paragraph::layout(string const & new_layout)
2179 {
2180         lyx::Assert(!new_layout.empty());
2181
2182         layout_ = new_layout;
2183 }
2184
2185
2186 bool Paragraph::isFirstInSequence() const
2187 {
2188         Paragraph const * dhook = depthHook(getDepth());
2189         return (dhook == this
2190                 || dhook->layout() != layout()
2191                 || dhook->getDepth() != getDepth());
2192 }
2193
2194
2195 Inset * Paragraph::inInset() const
2196 {
2197         return pimpl_->inset_owner;
2198 }
2199
2200
2201 void Paragraph::clearContents()
2202 {
2203         pimpl_->clear();
2204 }
2205
2206
2207 void Paragraph::setCounter(int i, int v)
2208 {
2209         pimpl_->counter_[i] = v;
2210 }
2211
2212
2213 int Paragraph::getCounter(int i) const
2214 {
2215         return pimpl_->counter_[i];
2216 }
2217
2218
2219 void Paragraph::incCounter(int i)
2220 {
2221         pimpl_->counter_[i]++;
2222 }
2223
2224
2225 void Paragraph::setChar(pos_type pos, value_type c)
2226 {
2227         pimpl_->setChar(pos, c);
2228 }
2229
2230
2231 Paragraph::inset_iterator::inset_iterator(Paragraph::InsetList::iterator const & iter)
2232  : it(iter)
2233 {}
2234
2235
2236 Paragraph::inset_iterator Paragraph::inset_iterator_begin()
2237 {
2238         return inset_iterator(insetlist.begin());
2239 }
2240
2241
2242 Paragraph::inset_iterator Paragraph::inset_iterator_end()
2243 {
2244         return inset_iterator(insetlist.end());
2245 }
2246
2247
2248 ParagraphParameters & Paragraph::params()
2249 {
2250         return pimpl_->params;
2251 }
2252
2253
2254 ParagraphParameters const & Paragraph::params() const
2255 {
2256         return pimpl_->params;
2257 }
2258
2259
2260 Paragraph * Paragraph::getParFromID(int id) const
2261 {
2262         return pimpl_->getParFromID(id);
2263 }
2264
2265
2266 bool Paragraph::isFreeSpacing() const
2267 {
2268         // for now we just need this, later should we need this in some
2269         // other way we can always add a function to Inset::() too.
2270         if (pimpl_->inset_owner && pimpl_->inset_owner->owner())
2271                 return (pimpl_->inset_owner->owner()->lyxCode() == Inset::ERT_CODE);
2272         return false;
2273 }