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