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