]> git.lyx.org Git - lyx.git/blob - src/paragraph_pimpl.C
* change LFUN file-insert-ascii to file-insert-plaintext
[lyx.git] / src / paragraph_pimpl.C
1 /**
2  * \file paragraph_pimpl.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Lars Gullik Bjønnes
7  * \author Jean-Marc Lasgouttes
8  * \author John Levon
9  * \author André Pönitz
10  *
11  * Full author contact details are available in file CREDITS.
12  */
13
14 #include <config.h>
15
16 #include "paragraph_pimpl.h"
17 #include "paragraph.h"
18
19 #include "bufferparams.h"
20 #include "debug.h"
21 #include "encoding.h"
22 #include "language.h"
23 #include "LaTeXFeatures.h"
24 #include "LColor.h"
25 #include "lyxlength.h"
26 #include "lyxrc.h"
27 #include "outputparams.h"
28 #include "texrow.h"
29
30 #include <boost/next_prior.hpp>
31
32
33 namespace lyx {
34
35 using std::endl;
36 using std::upper_bound;
37 using std::lower_bound;
38 using std::string;
39
40
41 // Initialization of the counter for the paragraph id's,
42 unsigned int Paragraph::Pimpl::paragraph_id = 0;
43
44 namespace {
45
46 struct special_phrase {
47         string phrase;
48         docstring macro;
49         bool builtin;
50 };
51
52 special_phrase const special_phrases[] = {
53         { "LyX", from_ascii("\\LyX{}"), false },
54         { "TeX", from_ascii("\\TeX{}"), true },
55         { "LaTeX2e", from_ascii("\\LaTeXe{}"), true },
56         { "LaTeX", from_ascii("\\LaTeX{}"), true },
57 };
58
59 size_t const phrases_nr = sizeof(special_phrases)/sizeof(special_phrase);
60
61
62 bool isEncoding(BufferParams const & bparams, LyXFont const & font,
63                 string const & encoding)
64 {
65         // We do ignore bparams.inputenc == "default" here because characters
66         // in this encoding could be treated by TeX as something different,
67         // e.g. if they are inside a CJK environment. See also
68         // http://bugzilla.lyx.org/show_bug.cgi?id=3043.
69         return (bparams.inputenc == encoding
70                 || (bparams.inputenc == "auto"
71                     && font.language()->encoding()->latexName() == encoding));
72 }
73
74 } // namespace anon
75
76
77 Paragraph::Pimpl::Pimpl(Paragraph * owner)
78         : owner_(owner)
79 {
80         inset_owner = 0;
81         id_ = paragraph_id++;
82 }
83
84
85 Paragraph::Pimpl::Pimpl(Pimpl const & p, Paragraph * owner)
86         : params(p.params), changes_(p.changes_), owner_(owner)
87 {
88         inset_owner = p.inset_owner;
89         fontlist = p.fontlist;
90         id_ = paragraph_id++;
91 }
92
93
94 bool Paragraph::Pimpl::isChanged(pos_type start, pos_type end) const
95 {
96         BOOST_ASSERT(start >= 0 && start <= size());
97         BOOST_ASSERT(end > start && end <= size() + 1);
98
99         return changes_.isChanged(start, end);
100 }
101
102
103 bool Paragraph::Pimpl::isMergedOnEndOfParDeletion(bool trackChanges) const {
104         // keep the logic here in sync with the logic of eraseChars()
105
106         if (!trackChanges) {
107                 return true;
108         }
109
110         Change change = changes_.lookup(size());
111
112         return change.type == Change::INSERTED && change.author == 0;
113 }
114
115
116 void Paragraph::Pimpl::setChange(Change const & change)
117 {
118         // beware of the imaginary end-of-par character!
119         changes_.set(change, 0, size() + 1);
120
121         /*
122          * Propagate the change recursively - but not in case of DELETED!
123          *
124          * Imagine that your co-author makes changes in an existing inset. He
125          * sends your document to you and you come to the conclusion that the
126          * inset should go completely. If you erase it, LyX must not delete all
127          * text within the inset. Otherwise, the change tracked insertions of
128          * your co-author get lost and there is no way to restore them later.
129          *
130          * Conclusion: An inset's content should remain untouched if you delete it
131          */
132
133         if (change.type != Change::DELETED) {
134                 for (pos_type pos = 0; pos < size(); ++pos) {
135                         if (owner_->isInset(pos)) {
136                                 owner_->getInset(pos)->setChange(change);
137                         }
138                 }
139         }
140 }
141
142
143 void Paragraph::Pimpl::setChange(pos_type pos, Change const & change)
144 {
145         BOOST_ASSERT(pos >= 0 && pos <= size());
146
147         changes_.set(change, pos);
148
149         // see comment in setChange(Change const &) above
150
151         if (change.type != Change::DELETED &&
152             pos < size() && owner_->isInset(pos)) {
153                 owner_->getInset(pos)->setChange(change);
154         }
155 }
156
157
158 Change const & Paragraph::Pimpl::lookupChange(pos_type pos) const
159 {
160         BOOST_ASSERT(pos >= 0 && pos <= size());
161
162         return changes_.lookup(pos);
163 }
164
165
166 void Paragraph::Pimpl::acceptChanges(pos_type start, pos_type end)
167 {
168         BOOST_ASSERT(start >= 0 && start <= size());
169         BOOST_ASSERT(end > start && end <= size() + 1);
170
171         for (pos_type pos = start; pos < end; ++pos) {
172                 switch (lookupChange(pos).type) {
173                         case Change::UNCHANGED:
174                                 // accept changes in nested inset
175                                 if (pos < size() && owner_->isInset(pos)) {
176                                         owner_->getInset(pos)->acceptChanges();
177                                 }
178
179                                 break;
180
181                         case Change::INSERTED:
182                                 changes_.set(Change(Change::UNCHANGED), pos);
183                                 // also accept changes in nested inset
184                                 if (pos < size() && owner_->isInset(pos)) {
185                                         owner_->getInset(pos)->acceptChanges();
186                                 }
187                                 break;
188
189                         case Change::DELETED:
190                                 // Suppress access to non-existent
191                                 // "end-of-paragraph char"
192                                 if (pos < size()) {
193                                         eraseChar(pos, false);
194                                         --end;
195                                         --pos;
196                                 }
197                                 break;
198                 }
199
200         }
201 }
202
203
204 void Paragraph::Pimpl::rejectChanges(pos_type start, pos_type end)
205 {
206         BOOST_ASSERT(start >= 0 && start <= size());
207         BOOST_ASSERT(end > start && end <= size() + 1);
208
209         for (pos_type pos = start; pos < end; ++pos) {
210                 switch (lookupChange(pos).type) {
211                         case Change::UNCHANGED:
212                                 // reject changes in nested inset
213                                 if (pos < size() && owner_->isInset(pos)) {
214                                         owner_->getInset(pos)->rejectChanges();
215                                 }
216                                 break;
217
218                         case Change::INSERTED:
219                                 // Suppress access to non-existent
220                                 // "end-of-paragraph char"
221                                 if (pos < size()) {
222                                         eraseChar(pos, false);
223                                         --end;
224                                         --pos;
225                                 }
226                                 break;
227
228                         case Change::DELETED:
229                                 changes_.set(Change(Change::UNCHANGED), pos);
230
231                                 // Do NOT reject changes within a deleted inset!
232                                 // There may be insertions of a co-author inside of it!
233
234                                 break;
235                 }
236         }
237 }
238
239
240 Paragraph::value_type Paragraph::Pimpl::getChar(pos_type pos) const
241 {
242         BOOST_ASSERT(pos >= 0 && pos <= size());
243
244         return owner_->getChar(pos);
245 }
246
247
248 void Paragraph::Pimpl::insertChar(pos_type pos, value_type c, Change const & change)
249 {
250         BOOST_ASSERT(pos >= 0 && pos <= size());
251
252         // track change
253         changes_.insert(change, pos);
254
255         // This is actually very common when parsing buffers (and
256         // maybe inserting ascii text)
257         if (pos == size()) {
258                 // when appending characters, no need to update tables
259                 owner_->text_.push_back(c);
260                 return;
261         }
262
263         owner_->text_.insert(owner_->text_.begin() + pos, c);
264
265         // Update the font table.
266         FontTable search_font(pos, LyXFont());
267         for (FontList::iterator it 
268               = lower_bound(fontlist.begin(), fontlist.end(), search_font, matchFT());
269              it != fontlist.end(); ++it)
270         {
271                 it->pos(it->pos() + 1);
272         }
273
274         // Update the insets
275         owner_->insetlist.increasePosAfterPos(pos);
276 }
277
278
279 void Paragraph::Pimpl::insertInset(pos_type pos, InsetBase * inset,
280                                    Change const & change)
281 {
282         BOOST_ASSERT(inset);
283         BOOST_ASSERT(pos >= 0 && pos <= size());
284
285         insertChar(pos, META_INSET, change);
286         BOOST_ASSERT(owner_->text_[pos] == META_INSET);
287
288         // Add a new entry in the insetlist.
289         owner_->insetlist.insert(inset, pos);
290 }
291
292
293 bool Paragraph::Pimpl::eraseChar(pos_type pos, bool trackChanges)
294 {
295         BOOST_ASSERT(pos >= 0 && pos <= size());
296
297         // keep the logic here in sync with the logic of isMergedOnEndOfParDeletion()
298
299         if (trackChanges) {
300                 Change change = changes_.lookup(pos);
301
302                 // set the character to DELETED if 
303                 //  a) it was previously unchanged or
304                 //  b) it was inserted by a co-author
305
306                 if (change.type == Change::UNCHANGED ||
307                     (change.type == Change::INSERTED && change.author != 0)) {
308                         setChange(pos, Change(Change::DELETED));
309                         return false;
310                 }
311
312                 if (change.type == Change::DELETED)
313                         return false;
314         }
315
316         // Don't physically access the imaginary end-of-paragraph character.
317         // eraseChar() can only mark it as DELETED. A physical deletion of
318         // end-of-par must be handled externally.
319         if (pos == size()) {
320                 return false;
321         }
322
323         // track change
324         changes_.erase(pos);
325
326         // if it is an inset, delete the inset entry
327         if (owner_->text_[pos] == Paragraph::META_INSET) {
328                 owner_->insetlist.erase(pos);
329         }
330
331         owner_->text_.erase(owner_->text_.begin() + pos);
332
333         // Erase entries in the tables.
334         FontTable search_font(pos, LyXFont());
335
336         FontList::iterator it =
337                 lower_bound(fontlist.begin(),
338                             fontlist.end(),
339                             search_font, matchFT());
340         if (it != fontlist.end() && it->pos() == pos &&
341             (pos == 0 ||
342              (it != fontlist.begin()
343               && boost::prior(it)->pos() == pos - 1))) {
344                 // If it is a multi-character font
345                 // entry, we just make it smaller
346                 // (see update below), otherwise we
347                 // should delete it.
348                 unsigned int const i = it - fontlist.begin();
349                 fontlist.erase(fontlist.begin() + i);
350                 it = fontlist.begin() + i;
351                 if (i > 0 && i < fontlist.size() &&
352                     fontlist[i - 1].font() == fontlist[i].font()) {
353                         fontlist.erase(fontlist.begin() + i - 1);
354                         it = fontlist.begin() + i - 1;
355                 }
356         }
357
358         // Update all other entries
359         FontList::iterator fend = fontlist.end();
360         for (; it != fend; ++it)
361                 it->pos(it->pos() - 1);
362
363         // Update the insetlist
364         owner_->insetlist.decreasePosAfterPos(pos);
365
366         return true;
367 }
368
369
370 int Paragraph::Pimpl::eraseChars(pos_type start, pos_type end, bool trackChanges)
371 {
372         BOOST_ASSERT(start >= 0 && start <= size());
373         BOOST_ASSERT(end >= start && end <= size() + 1);
374
375         pos_type i = start;
376         for (pos_type count = end - start; count; --count) {
377                 if (!eraseChar(i, trackChanges))
378                         ++i;
379         }
380         return end - i;
381 }
382
383
384 void Paragraph::Pimpl::simpleTeXBlanks(odocstream & os, TexRow & texrow,
385                                        pos_type const i,
386                                        unsigned int & column,
387                                        LyXFont const & font,
388                                        LyXLayout const & style)
389 {
390         if (style.pass_thru)
391                 return;
392
393         if (column > lyxrc.plaintext_linelen
394             && i
395             && getChar(i - 1) != ' '
396             && (i < size() - 1)
397             // same in FreeSpacing mode
398             && !owner_->isFreeSpacing()
399             // In typewriter mode, we want to avoid
400             // ! . ? : at the end of a line
401             && !(font.family() == LyXFont::TYPEWRITER_FAMILY
402                  && (getChar(i - 1) == '.'
403                      || getChar(i - 1) == '?'
404                      || getChar(i - 1) == ':'
405                      || getChar(i - 1) == '!'))) {
406                 os << '\n';
407                 texrow.newline();
408                 texrow.start(owner_->id(), i + 1);
409                 column = 0;
410         } else if (style.free_spacing) {
411                 os << '~';
412         } else {
413                 os << ' ';
414         }
415 }
416
417
418 bool Paragraph::Pimpl::isTextAt(string const & str, pos_type pos) const
419 {
420         pos_type const len = str.length();
421
422         // is the paragraph large enough?
423         if (pos + len > size())
424                 return false;
425
426         // does the wanted text start at point?
427         for (string::size_type i = 0; i < str.length(); ++i) {
428                 if (str[i] != owner_->text_[pos + i])
429                         return false;
430         }
431
432         // is there a font change in middle of the word?
433         FontList::const_iterator cit = fontlist.begin();
434         FontList::const_iterator end = fontlist.end();
435         for (; cit != end; ++cit) {
436                 if (cit->pos() >= pos)
437                         break;
438         }
439         if (cit != end && pos + len - 1 > cit->pos())
440                 return false;
441
442         return true;
443 }
444
445
446 void Paragraph::Pimpl::simpleTeXSpecialChars(Buffer const & buf,
447                                              BufferParams const & bparams,
448                                              odocstream & os,
449                                              TexRow & texrow,
450                                              OutputParams const & runparams,
451                                              LyXFont & font,
452                                              LyXFont & running_font,
453                                              LyXFont & basefont,
454                                              LyXFont const & outerfont,
455                                              bool & open_font,
456                                              Change::Type & running_change,
457                                              LyXLayout const & style,
458                                              pos_type & i,
459                                              unsigned int & column,
460                                              value_type const c)
461 {
462         if (style.pass_thru) {
463                 if (c != Paragraph::META_INSET) {
464                         if (c != '\0')
465                                 os.put(c);
466                 } else
467                         owner_->getInset(i)->plaintext(buf, os, runparams);
468                 return;
469         }
470
471         // Two major modes:  LaTeX or plain
472         // Handle here those cases common to both modes
473         // and then split to handle the two modes separately.
474         switch (c) {
475         case Paragraph::META_INSET: {
476                 InsetBase * inset = owner_->getInset(i);
477
478                 // FIXME: remove this check
479                 if (!inset)
480                         break;
481
482                 // FIXME: move this to InsetNewline::latex
483                 if (inset->lyxCode() == InsetBase::NEWLINE_CODE) {
484                         // newlines are handled differently here than
485                         // the default in simpleTeXSpecialChars().
486                         if (!style.newline_allowed) {
487                                 os << '\n';
488                         } else {
489                                 if (open_font) {
490                                         column += running_font.latexWriteEndChanges(
491                                                 os, basefont, basefont, bparams);
492                                         open_font = false;
493                                 }
494                                 basefont = owner_->getLayoutFont(bparams, outerfont);
495                                 running_font = basefont;
496
497                                 if (font.family() == LyXFont::TYPEWRITER_FAMILY)
498                                         os << '~';
499
500                                 if (runparams.moving_arg)
501                                         os << "\\protect ";
502
503                                 os << "\\\\\n";
504                         }
505                         texrow.newline();
506                         texrow.start(owner_->id(), i + 1);
507                         column = 0;
508                         break;
509                 }
510
511                 // output change tracking marks only if desired,
512                 // if dvipost is installed,
513                 // and with dvi/ps (other formats don't work)
514                 LaTeXFeatures features(buf, bparams, runparams);
515                 bool const output = bparams.outputChanges
516                         && runparams.flavor == OutputParams::LATEX
517                         && features.isAvailable("dvipost");
518
519                 if (inset->canTrackChanges()) {
520                         column += Changes::latexMarkChange(os, running_change,
521                                 Change::UNCHANGED, output);
522                         running_change = Change::UNCHANGED;
523                 }
524
525                 bool close = false;
526                 odocstream::pos_type const len = os.tellp();
527
528                 if ((inset->lyxCode() == InsetBase::GRAPHICS_CODE
529                      || inset->lyxCode() == InsetBase::MATH_CODE
530                      || inset->lyxCode() == InsetBase::URL_CODE)
531                     && running_font.isRightToLeft()) {
532                         os << "\\L{";
533                         close = true;
534                 }
535
536 #ifdef WITH_WARNINGS
537 #warning Bug: we can have an empty font change here!
538 // if there has just been a font change, we are going to close it
539 // right now, which means stupid latex code like \textsf{}. AFAIK,
540 // this does not harm dvi output. A minor bug, thus (JMarc)
541 #endif
542                 // some insets cannot be inside a font change command
543                 if (open_font && inset->noFontChange()) {
544                         column += running_font.latexWriteEndChanges(
545                                         os, basefont, basefont, bparams);
546                         open_font = false;
547                         basefont = owner_->getLayoutFont(bparams, outerfont);
548                         running_font = basefont;
549                 }
550
551                 int tmp = inset->latex(buf, os, runparams);
552
553                 if (close)
554                         os << '}';
555
556                 if (tmp) {
557                         for (int j = 0; j < tmp; ++j) {
558                                 texrow.newline();
559                         }
560                         texrow.start(owner_->id(), i + 1);
561                         column = 0;
562                 } else {
563                         column += os.tellp() - len;
564                 }
565         }
566         break;
567
568         default:
569                 // And now for the special cases within each mode
570
571                 switch (c) {
572                 case '\\':
573                         os << "\\textbackslash{}";
574                         column += 15;
575                         break;
576
577                 // The following characters could be written literally in latin1, but they
578                 // would be wrongly converted on systems where char is signed, so we give
579                 // the code points.
580                 // This also makes us independant from the encoding of this source file.
581                 case 0xb1:    // ± PLUS-MINUS SIGN
582                 case 0xb2:    // ² SUPERSCRIPT TWO
583                 case 0xb3:    // ³ SUPERSCRIPT THREE
584                 case 0xd7:    // × MULTIPLICATION SIGN
585                 case 0xf7:    // ÷ DIVISION SIGN
586                 case 0xb9:    // ¹ SUPERSCRIPT ONE
587                 case 0xac:    // ¬ NOT SIGN
588                 case 0xb5:    // µ MICRO SIGN
589                         if (isEncoding(bparams, font, "latin1")
590                             || isEncoding(bparams, font, "latin9")) {
591                                 os << "\\ensuremath{";
592                                 os.put(c);
593                                 os << '}';
594                                 column += 13;
595                         } else {
596                                 os.put(c);
597                         }
598                         break;
599
600                 case '|': case '<': case '>':
601                         // In T1 encoding, these characters exist
602                         if (lyxrc.fontenc == "T1") {
603                                 os.put(c);
604                                 //... but we should avoid ligatures
605                                 if ((c == '>' || c == '<')
606                                     && i <= size() - 2
607                                     && getChar(i + 1) == c) {
608                                         //os << "\\textcompwordmark{}";
609                                         // Jean-Marc, have a look at
610                                         // this. I think this works
611                                         // equally well:
612                                         os << "\\,{}";
613                                         // Lgb
614                                         column += 19;
615                                 }
616                                 break;
617                         }
618                         // Typewriter font also has them
619                         if (font.family() == LyXFont::TYPEWRITER_FAMILY) {
620                                 os.put(c);
621                                 break;
622                         }
623                         // Otherwise, we use what LaTeX
624                         // provides us.
625                         switch (c) {
626                         case '<':
627                                 os << "\\textless{}";
628                                 column += 10;
629                                 break;
630                         case '>':
631                                 os << "\\textgreater{}";
632                                 column += 13;
633                                 break;
634                         case '|':
635                                 os << "\\textbar{}";
636                                 column += 9;
637                                 break;
638                         }
639                         break;
640
641                 case '-': // "--" in Typewriter mode -> "-{}-"
642                         if (i <= size() - 2
643                             && getChar(i + 1) == '-'
644                             && font.family() == LyXFont::TYPEWRITER_FAMILY) {
645                                 os << "-{}";
646                                 column += 2;
647                         } else {
648                                 os << '-';
649                         }
650                         break;
651
652                 case '\"':
653                         os << "\\char`\\\"{}";
654                         column += 9;
655                         break;
656
657                 case 0xa3:    // £ POUND SIGN
658                         if (bparams.inputenc == "default") {
659                                 os << "\\pounds{}";
660                                 column += 8;
661                         } else {
662                                 os.put(c);
663                         }
664                         break;
665
666                 case 0x20ac:    // EURO SIGN
667                         if (isEncoding(bparams, font, "latin9")
668                             || isEncoding(bparams, font, "cp1251")
669                             || isEncoding(bparams, font, "utf8")
670                             || isEncoding(bparams, font, "latin10")
671                             || isEncoding(bparams, font, "cp858")) {
672                                 os.put(c);
673                         } else {
674                                 os << "\\texteuro{}";
675                                 column += 10;
676                         }
677                         break;
678
679                 // These characters are covered by latin1, but not
680                 // by latin9 (a.o.). We have to support them because
681                 // we switched the default of latin1-languages to latin9
682                 case 0xa4:    // CURRENCY SYMBOL
683                 case 0xa6:    // BROKEN BAR
684                 case 0xa8:    // DIAERESIS
685                 case 0xb4:    // ACUTE ACCENT
686                 case 0xb8:    // CEDILLA
687                 case 0xbd:    // 1/2 FRACTION
688                 case 0xbc:    // 1/4 FRACTION
689                 case 0xbe:    // 3/4 FRACTION
690                         if (isEncoding(bparams, font, "latin1")
691                             || isEncoding(bparams, font, "latin5")
692                             || isEncoding(bparams, font, "utf8")) {
693                                 os.put(c);
694                                 break;
695                         } else {
696                                 switch (c) {
697                                 case 0xa4:
698                                         os << "\\textcurrency{}";
699                                         column += 15;
700                                         break;
701                                 case 0xa6:
702                                         os << "\\textbrokenbar{}";
703                                         column += 16;
704                                         break;
705                                 case 0xa8:
706                                         os << "\\textasciidieresis{}";
707                                         column += 20;
708                                         break;
709                                 case 0xb4:
710                                         os << "\\textasciiacute{}";
711                                         column += 17;
712                                         break;
713                                 case 0xb8: // from latin1.def:
714                                         os << "\\c\\ ";
715                                         column += 3;
716                                         break;
717                                 case 0xbd:
718                                         os << "\\textonehalf{}";
719                                         column += 14;
720                                         break;
721                                 case 0xbc:
722                                         os << "\\textonequarter{}";
723                                         column += 17;
724                                         break;
725                                 case 0xbe:
726                                         os << "\\textthreequarters{}";
727                                         column += 20;
728                                         break;
729                                 }
730                                 break;
731                         }
732
733                 case '$': case '&':
734                 case '%': case '#': case '{':
735                 case '}': case '_':
736                         os << '\\';
737                         os.put(c);
738                         column += 1;
739                         break;
740
741                 case '~':
742                         os << "\\textasciitilde{}";
743                         column += 16;
744                         break;
745
746                 case '^':
747                         os << "\\textasciicircum{}";
748                         column += 17;
749                         break;
750
751                 case '*': case '[':
752                         // avoid being mistaken for optional arguments
753                         os << '{';
754                         os.put(c);
755                         os << '}';
756                         column += 2;
757                         break;
758
759                 case ' ':
760                         // Blanks are printed before font switching.
761                         // Sure? I am not! (try nice-latex)
762                         // I am sure it's correct. LyX might be smarter
763                         // in the future, but for now, nothing wrong is
764                         // written. (Asger)
765                         break;
766
767                 default:
768
769                         // I assume this is hack treating typewriter as verbatim
770                         if (font.family() == LyXFont::TYPEWRITER_FAMILY) {
771                                 if (c != '\0') {
772                                         os.put(c);
773                                 }
774                                 break;
775                         }
776
777                         // LyX, LaTeX etc.
778
779                         // FIXME: if we have "LaTeX" with a font
780                         // change in the middle (before the 'T', then
781                         // the "TeX" part is still special cased.
782                         // Really we should only operate this on
783                         // "words" for some definition of word
784
785                         size_t pnr = 0;
786
787                         for (; pnr < phrases_nr; ++pnr) {
788                                 if (isTextAt(special_phrases[pnr].phrase, i)) {
789                                         os << special_phrases[pnr].macro;
790                                         i += special_phrases[pnr].phrase.length() - 1;
791                                         column += special_phrases[pnr].macro.length() - 1;
792                                         break;
793                                 }
794                         }
795
796                         if (pnr == phrases_nr && c != '\0') {
797                                 os.put(c);
798                         }
799                         break;
800                 }
801         }
802 }
803
804
805 void Paragraph::Pimpl::validate(LaTeXFeatures & features,
806                                 LyXLayout const & layout) const
807 {
808         BufferParams const & bparams = features.bufferParams();
809
810         // check the params.
811         if (!params.spacing().isDefault())
812                 features.require("setspace");
813
814         // then the layouts
815         features.useLayout(layout.name());
816
817         // then the fonts
818         Language const * doc_language = bparams.language;
819
820         FontList::const_iterator fcit = fontlist.begin();
821         FontList::const_iterator fend = fontlist.end();
822         for (; fcit != fend; ++fcit) {
823                 if (fcit->font().noun() == LyXFont::ON) {
824                         lyxerr[Debug::LATEX] << "font.noun: "
825                                              << fcit->font().noun()
826                                              << endl;
827                         features.require("noun");
828                         lyxerr[Debug::LATEX] << "Noun enabled. Font: "
829                                              << to_utf8(fcit->font().stateText(0))
830                                              << endl;
831                 }
832                 switch (fcit->font().color()) {
833                 case LColor::none:
834                 case LColor::inherit:
835                 case LColor::ignore:
836                         // probably we should put here all interface colors used for
837                         // font displaying! For now I just add this ones I know of (Jug)
838                 case LColor::latex:
839                 case LColor::note:
840                         break;
841                 default:
842                         features.require("color");
843                         lyxerr[Debug::LATEX] << "Color enabled. Font: "
844                                              << to_utf8(fcit->font().stateText(0))
845                                              << endl;
846                 }
847
848                 Language const * language = fcit->font().language();
849                 if (language->babel() != doc_language->babel() &&
850                     language != ignore_language &&
851                     language != latex_language)
852                 {
853                         features.useLanguage(language);
854                         lyxerr[Debug::LATEX] << "Found language "
855                                              << language->babel() << endl;
856                 }
857         }
858
859         if (!params.leftIndent().zero())
860                 features.require("ParagraphLeftIndent");
861
862         // then the insets
863         InsetList::const_iterator icit = owner_->insetlist.begin();
864         InsetList::const_iterator iend = owner_->insetlist.end();
865         for (; icit != iend; ++icit) {
866                 if (icit->inset) {
867                         icit->inset->validate(features);
868                         if (layout.needprotect &&
869                             icit->inset->lyxCode() == InsetBase::FOOT_CODE)
870                                 features.require("NeedLyXFootnoteCode");
871                 }
872         }
873
874         // then the contents
875         for (pos_type i = 0; i < size() ; ++i) {
876                 for (size_t pnr = 0; pnr < phrases_nr; ++pnr) {
877                         if (!special_phrases[pnr].builtin
878                             && isTextAt(special_phrases[pnr].phrase, i)) {
879                                 features.require(special_phrases[pnr].phrase);
880                                 break;
881                         }
882                 }
883                 // these glyphs require the textcomp package
884                 if (getChar(i) == 0x20ac || getChar(i) == 0xa4
885                     || getChar(i) == 0xa6 || getChar(i) == 0xa8
886                     || getChar(i) == 0xb4 || getChar(i) == 0xbd
887                     || getChar(i) == 0xbc || getChar(i) == 0xbe)
888                         features.require("textcomp");
889         }
890 }
891
892
893 } // namespace lyx