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