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