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