]> git.lyx.org Git - lyx.git/blob - src/paragraph_pimpl.C
Use UTF8 for LaTeX export.
[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 using lyx::docstring;
33 using lyx::odocstream;
34 using lyx::pos_type;
35
36 using std::endl;
37 using std::upper_bound;
38 using std::lower_bound;
39 using std::string;
40
41
42 // Initialization of the counter for the paragraph id's,
43 unsigned int Paragraph::Pimpl::paragraph_id = 0;
44
45 namespace {
46
47 struct special_phrase {
48         string phrase;
49         docstring macro;
50         bool builtin;
51 };
52
53 special_phrase const special_phrases[] = {
54         { "LyX", lyx::from_ascii("\\LyX{}"), false },
55         { "TeX", lyx::from_ascii("\\TeX{}"), true },
56         { "LaTeX2e", lyx::from_ascii("\\LaTeXe{}"), true },
57         { "LaTeX", lyx::from_ascii("\\LaTeX{}"), true },
58 };
59
60 size_t const phrases_nr = sizeof(special_phrases)/sizeof(special_phrase);
61
62 } // namespace anon
63
64
65 Paragraph::Pimpl::Pimpl(Paragraph * owner)
66         : owner_(owner)
67 {
68         inset_owner = 0;
69         id_ = paragraph_id++;
70 }
71
72
73 Paragraph::Pimpl::Pimpl(Pimpl const & p, Paragraph * owner)
74         : params(p.params), owner_(owner)
75 {
76         inset_owner = p.inset_owner;
77         fontlist = p.fontlist;
78         id_ = paragraph_id++;
79
80         if (p.tracking())
81                 changes_.reset(new Changes(*p.changes_.get()));
82 }
83
84
85 void Paragraph::Pimpl::setContentsFromPar(Paragraph const & par)
86 {
87         owner_->text_ = par.text_;
88         if (par.pimpl_->tracking()) {
89                 changes_.reset(new Changes(*(par.pimpl_->changes_.get())));
90         }
91 }
92
93
94 bool Paragraph::Pimpl::isChanged(pos_type start, pos_type end) const
95 {
96         // FIXME: change tracking (MG)
97         if (!tracking())
98                 return false;
99
100         return changes_->isChange(start, end);
101 }
102
103
104 void Paragraph::Pimpl::setChange(Change const & change)
105 {
106         // FIXME: change tracking (MG)
107         // changes_.set(change, 0, size());
108
109         if (change.type == Change::UNCHANGED) { // only for UNCHANGED ???
110                 for (pos_type i = 0; i < size(); ++i) {
111                         if (owner_->isInset(i)) {
112                                 // FIXME: change tracking (MG)
113                                 // owner_->getInset(i)->setChange(change);
114                         }
115                 }
116         }
117 }
118
119
120 void Paragraph::Pimpl::setChange(pos_type pos, Change const & change)
121 {
122         if (!tracking())
123                 return;
124
125         changes_->set(change, pos);
126 }
127
128
129 Change const Paragraph::Pimpl::lookupChange(pos_type pos) const
130 {
131         if (!tracking())
132                 return Change(Change::UNCHANGED);
133
134         return changes_->lookup(pos);
135 }
136
137
138 void Paragraph::Pimpl::acceptChange(pos_type start, pos_type end)
139 {
140         if (!tracking())
141                 return;
142
143         if (!size()) {
144                 changes_.reset(new Changes(Change::UNCHANGED));
145                 return;
146         }
147
148         lyxerr[Debug::CHANGES] << "acceptchange" << endl;
149         pos_type i = start;
150
151         for (; i < end; ++i) {
152                 switch (lookupChange(i).type) {
153                         case Change::UNCHANGED:
154                                 break;
155
156                         case Change::INSERTED:
157                                 changes_->set(Change::UNCHANGED, i);
158                                 break;
159
160                         case Change::DELETED:
161                                 // Suppress access to nonexistent
162                                 // "end-of-paragraph char":
163                                 if (i < size()) {
164                                         eraseIntern(i);
165                                         changes_->erase(i);
166                                         --end;
167                                         --i;
168                                 }
169                                 break;
170                 }
171         }
172
173         lyxerr[Debug::CHANGES] << "endacceptchange" << endl;
174         changes_->reset(Change::UNCHANGED);
175 }
176
177
178 void Paragraph::Pimpl::rejectChange(pos_type start, pos_type end)
179 {
180         if (!tracking())
181                 return;
182
183         if (!size()) {
184                 changes_.reset(new Changes(Change::UNCHANGED));
185                 return;
186         }
187
188         pos_type i = start;
189
190         for (; i < end; ++i) {
191                 switch (lookupChange(i).type) {
192                         case Change::UNCHANGED:
193                                 break;
194
195                         case Change::INSERTED:
196                                 if (i < size()) {
197                                         eraseIntern(i);
198                                         changes_->erase(i);
199                                         --end;
200                                         --i;
201                                 }
202                                 break;
203
204                         case Change::DELETED:
205                                 changes_->set(Change::UNCHANGED, i);
206                                 // No real char at position size():
207                                 if (i < size() && owner_->isInset(i))
208                                         owner_->getInset(i)->markErased(false);
209                                 break;
210                 }
211         }
212         changes_->reset(Change::UNCHANGED);
213 }
214
215
216 Paragraph::value_type Paragraph::Pimpl::getChar(pos_type pos) const
217 {
218         return owner_->getChar(pos);
219 }
220
221
222 void Paragraph::Pimpl::insertChar(pos_type pos, value_type c, Change const & change)
223 {
224         BOOST_ASSERT(pos <= size());
225
226         if (tracking()) {
227                 changes_->record(change, pos);
228         }
229
230         // This is actually very common when parsing buffers (and
231         // maybe inserting ascii text)
232         if (pos == size()) {
233                 // when appending characters, no need to update tables
234                 owner_->text_.push_back(c);
235                 return;
236         }
237
238         owner_->text_.insert(owner_->text_.begin() + pos, c);
239
240         // Update the font table.
241         FontTable search_font(pos, LyXFont());
242         for (FontList::iterator it = lower_bound(fontlist.begin(),
243                                                       fontlist.end(),
244                                                       search_font, matchFT());
245              it != fontlist.end(); ++it)
246         {
247                 it->pos(it->pos() + 1);
248         }
249
250         // Update the insets
251         owner_->insetlist.increasePosAfterPos(pos);
252 }
253
254
255 void Paragraph::Pimpl::insertInset(pos_type pos,
256                                    InsetBase * inset, Change const & change)
257 {
258         BOOST_ASSERT(inset);
259         BOOST_ASSERT(pos <= size());
260
261         insertChar(pos, META_INSET, change);
262         BOOST_ASSERT(owner_->text_[pos] == META_INSET);
263
264         // Add a new entry in the insetlist.
265         owner_->insetlist.insert(inset, pos);
266 }
267
268
269 void Paragraph::Pimpl::eraseIntern(pos_type pos)
270 {
271         // if it is an inset, delete the inset entry
272         if (owner_->text_[pos] == Paragraph::META_INSET) {
273                 owner_->insetlist.erase(pos);
274         }
275
276         owner_->text_.erase(owner_->text_.begin() + pos);
277
278         // Erase entries in the tables.
279         FontTable search_font(pos, LyXFont());
280
281         FontList::iterator it =
282                 lower_bound(fontlist.begin(),
283                             fontlist.end(),
284                             search_font, matchFT());
285         if (it != fontlist.end() && it->pos() == pos &&
286             (pos == 0 ||
287              (it != fontlist.begin()
288               && boost::prior(it)->pos() == pos - 1))) {
289                 // If it is a multi-character font
290                 // entry, we just make it smaller
291                 // (see update below), otherwise we
292                 // should delete it.
293                 unsigned int const i = it - fontlist.begin();
294                 fontlist.erase(fontlist.begin() + i);
295                 it = fontlist.begin() + i;
296                 if (i > 0 && i < fontlist.size() &&
297                     fontlist[i - 1].font() == fontlist[i].font()) {
298                         fontlist.erase(fontlist.begin() + i - 1);
299                         it = fontlist.begin() + i - 1;
300                 }
301         }
302
303         // Update all other entries.
304         FontList::iterator fend = fontlist.end();
305         for (; it != fend; ++it)
306                 it->pos(it->pos() - 1);
307
308         // Update the insetlist.
309         owner_->insetlist.decreasePosAfterPos(pos);
310 }
311
312
313 bool Paragraph::Pimpl::erase(pos_type pos)
314 {
315         BOOST_ASSERT(pos <= size());
316
317         if (tracking()) {
318                 Change::Type changetype(changes_->lookup(pos).type);
319
320                 // only allow the actual removal if it was /new/ text
321                 if (changetype != Change::INSERTED) {
322                         changes_->record(Change(Change::DELETED), pos);
323                         if (pos < size() && owner_->isInset(pos))
324                                 owner_->getInset(pos)->markErased(true);
325                         return false;
326                 }
327         }
328
329         // Don't physically access nonexistent end-of-paragraph char
330         if (pos < size()) {
331                 eraseIntern(pos);
332                 return true;
333         }
334
335         return false;
336 }
337
338
339 int Paragraph::Pimpl::erase(pos_type start, pos_type end)
340 {
341         pos_type i = start;
342         for (pos_type count = end - start; count; --count) {
343                 if (!erase(i))
344                         ++i;
345         }
346         return end - i;
347 }
348
349
350 void Paragraph::Pimpl::simpleTeXBlanks(odocstream & os, TexRow & texrow,
351                                        pos_type const i,
352                                        unsigned int & column,
353                                        LyXFont const & font,
354                                        LyXLayout const & style)
355 {
356         if (style.pass_thru)
357                 return;
358
359         if (column > lyxrc.ascii_linelen
360             && i
361             && getChar(i - 1) != ' '
362             && (i < size() - 1)
363             // same in FreeSpacing mode
364             && !owner_->isFreeSpacing()
365             // In typewriter mode, we want to avoid
366             // ! . ? : at the end of a line
367             && !(font.family() == LyXFont::TYPEWRITER_FAMILY
368                  && (getChar(i - 1) == '.'
369                      || getChar(i - 1) == '?'
370                      || getChar(i - 1) == ':'
371                      || getChar(i - 1) == '!'))) {
372                 os << '\n';
373                 texrow.newline();
374                 texrow.start(owner_->id(), i + 1);
375                 column = 0;
376         } else if (style.free_spacing) {
377                 os << '~';
378         } else {
379                 os << ' ';
380         }
381 }
382
383
384 bool Paragraph::Pimpl::isTextAt(string const & str, pos_type pos) const
385 {
386         pos_type const len = str.length();
387
388         // is the paragraph large enough?
389         if (pos + len > size())
390                 return false;
391
392         // does the wanted text start at point?
393         for (string::size_type i = 0; i < str.length(); ++i) {
394                 if (str[i] != owner_->text_[pos + i])
395                         return false;
396         }
397
398         // is there a font change in middle of the word?
399         FontList::const_iterator cit = fontlist.begin();
400         FontList::const_iterator end = fontlist.end();
401         for (; cit != end; ++cit) {
402                 if (cit->pos() >= pos)
403                         break;
404         }
405         if (cit != end && pos + len - 1 > cit->pos())
406                 return false;
407
408         return true;
409 }
410
411
412 void Paragraph::Pimpl::simpleTeXSpecialChars(Buffer const & buf,
413                                              BufferParams const & bparams,
414                                              odocstream & os,
415                                              TexRow & texrow,
416                                              OutputParams const & runparams,
417                                              LyXFont & font,
418                                              LyXFont & running_font,
419                                              LyXFont & basefont,
420                                              LyXFont const & outerfont,
421                                              bool & open_font,
422                                              Change::Type & running_change,
423                                              LyXLayout const & style,
424                                              pos_type & i,
425                                              unsigned int & column,
426                                              value_type const c)
427 {
428         if (style.pass_thru) {
429                 if (c != Paragraph::META_INSET) {
430                         if (c != '\0')
431                                 os.put(c);
432                 } else
433                         owner_->getInset(i)->plaintext(buf, os, runparams);
434                 return;
435         }
436
437         // Two major modes:  LaTeX or plain
438         // Handle here those cases common to both modes
439         // and then split to handle the two modes separately.
440         switch (c) {
441         case Paragraph::META_INSET: {
442                 InsetBase * inset = owner_->getInset(i);
443
444                 // FIXME: remove this check
445                 if (!inset)
446                         break;
447
448                 // FIXME: move this to InsetNewline::latex
449                 if (inset->lyxCode() == InsetBase::NEWLINE_CODE) {
450                         // newlines are handled differently here than
451                         // the default in simpleTeXSpecialChars().
452                         if (!style.newline_allowed) {
453                                 os << '\n';
454                         } else {
455                                 if (open_font) {
456                                         column += running_font.latexWriteEndChanges(os, basefont, basefont);
457                                         open_font = false;
458                                 }
459                                 basefont = owner_->getLayoutFont(bparams, outerfont);
460                                 running_font = basefont;
461
462                                 if (font.family() == LyXFont::TYPEWRITER_FAMILY)
463                                         os << '~';
464
465                                 if (runparams.moving_arg)
466                                         os << "\\protect ";
467
468                                 os << "\\\\\n";
469                         }
470                         texrow.newline();
471                         texrow.start(owner_->id(), i + 1);
472                         column = 0;
473                         break;
474                 }
475
476                 // output change tracking marks only if desired,
477                 // if dvipost is installed,
478                 // and with dvi/ps (other formats don't work)
479                 LaTeXFeatures features(buf, bparams, runparams);
480                 bool const output = bparams.outputChanges
481                         && runparams.flavor == OutputParams::LATEX
482                         && features.isAvailable("dvipost");
483
484                 if (inset->canTrackChanges()) {
485                         column += Changes::latexMarkChange(os, running_change,
486                                 Change::UNCHANGED, output);
487                         running_change = Change::UNCHANGED;
488                 }
489
490                 bool close = false;
491                 odocstream::pos_type const len = os.tellp();
492
493                 if ((inset->lyxCode() == InsetBase::GRAPHICS_CODE
494                      || inset->lyxCode() == InsetBase::MATH_CODE
495                      || inset->lyxCode() == InsetBase::URL_CODE)
496                     && running_font.isRightToLeft()) {
497                         os << "\\L{";
498                         close = true;
499                 }
500
501 #ifdef WITH_WARNINGS
502 #warning Bug: we can have an empty font change here!
503 // if there has just been a font change, we are going to close it
504 // right now, which means stupid latex code like \textsf{}. AFAIK,
505 // this does not harm dvi output. A minor bug, thus (JMarc)
506 #endif
507                 // some insets cannot be inside a font change command
508                 if (open_font && inset->noFontChange()) {
509                         column +=running_font.
510                                 latexWriteEndChanges(os,
511                                                      basefont,
512                                                      basefont);
513                         open_font = false;
514                         basefont = owner_->getLayoutFont(bparams, outerfont);
515                         running_font = basefont;
516                 }
517
518                 int tmp = inset->latex(buf, os, runparams);
519
520                 if (close)
521                         os << '}';
522
523                 if (tmp) {
524                         for (int j = 0; j < tmp; ++j) {
525                                 texrow.newline();
526                         }
527                         texrow.start(owner_->id(), i + 1);
528                         column = 0;
529                 } else {
530                         column += os.tellp() - len;
531                 }
532         }
533         break;
534
535         default:
536                 // And now for the special cases within each mode
537
538                 switch (c) {
539                 case '\\':
540                         os << "\\textbackslash{}";
541                         column += 15;
542                         break;
543
544                 case '±': case '²': case '³':
545                 case '×': case '÷': case '¹':
546                 case '¬': case 'µ':
547                         if ((bparams.inputenc == "latin1" ||
548                              bparams.inputenc == "latin9") ||
549                             (bparams.inputenc == "auto" &&
550                              (font.language()->encoding()->latexName()
551                               == "latin1" ||
552                               font.language()->encoding()->latexName()
553                               == "latin9"))) {
554                                 os << "\\ensuremath{"
555                                    << c
556                                    << '}';
557                                 column += 13;
558                         } else {
559                                 os << c;
560                         }
561                         break;
562
563                 case '|': case '<': case '>':
564                         // In T1 encoding, these characters exist
565                         if (lyxrc.fontenc == "T1") {
566                                 os << c;
567                                 //... but we should avoid ligatures
568                                 if ((c == '>' || c == '<')
569                                     && i <= size() - 2
570                                     && getChar(i + 1) == c) {
571                                         //os << "\\textcompwordmark{}";
572                                         // Jean-Marc, have a look at
573                                         // this. I think this works
574                                         // equally well:
575                                         os << "\\,{}";
576                                         // Lgb
577                                         column += 19;
578                                 }
579                                 break;
580                         }
581                         // Typewriter font also has them
582                         if (font.family() == LyXFont::TYPEWRITER_FAMILY) {
583                                 os << c;
584                                 break;
585                         }
586                         // Otherwise, we use what LaTeX
587                         // provides us.
588                         switch (c) {
589                         case '<':
590                                 os << "\\textless{}";
591                                 column += 10;
592                                 break;
593                         case '>':
594                                 os << "\\textgreater{}";
595                                 column += 13;
596                                 break;
597                         case '|':
598                                 os << "\\textbar{}";
599                                 column += 9;
600                                 break;
601                         }
602                         break;
603
604                 case '-': // "--" in Typewriter mode -> "-{}-"
605                         if (i <= size() - 2
606                             && getChar(i + 1) == '-'
607                             && font.family() == LyXFont::TYPEWRITER_FAMILY) {
608                                 os << "-{}";
609                                 column += 2;
610                         } else {
611                                 os << '-';
612                         }
613                         break;
614
615                 case '\"':
616                         os << "\\char`\\\"{}";
617                         column += 9;
618                         break;
619
620                 case '£':
621                         if (bparams.inputenc == "default") {
622                                 os << "\\pounds{}";
623                                 column += 8;
624                         } else {
625                                 os << c;
626                         }
627                         break;
628
629                 case '$': case '&':
630                 case '%': case '#': case '{':
631                 case '}': case '_':
632                         os << '\\' << c;
633                         column += 1;
634                         break;
635
636                 case '~':
637                         os << "\\textasciitilde{}";
638                         column += 16;
639                         break;
640
641                 case '^':
642                         os << "\\textasciicircum{}";
643                         column += 17;
644                         break;
645
646                 case '*': case '[':
647                         // avoid being mistaken for optional arguments
648                         os << '{' << c << '}';
649                         column += 2;
650                         break;
651
652                 case ' ':
653                         // Blanks are printed before font switching.
654                         // Sure? I am not! (try nice-latex)
655                         // I am sure it's correct. LyX might be smarter
656                         // in the future, but for now, nothing wrong is
657                         // written. (Asger)
658                         break;
659
660                 default:
661
662                         // I assume this is hack treating typewriter as verbatim
663                         if (font.family() == LyXFont::TYPEWRITER_FAMILY) {
664                                 if (c != '\0') {
665                                         os << c;
666                                 }
667                                 break;
668                         }
669
670                         // LyX, LaTeX etc.
671
672                         // FIXME: if we have "LaTeX" with a font
673                         // change in the middle (before the 'T', then
674                         // the "TeX" part is still special cased.
675                         // Really we should only operate this on
676                         // "words" for some definition of word
677
678                         size_t pnr = 0;
679
680                         for (; pnr < phrases_nr; ++pnr) {
681                                 if (isTextAt(special_phrases[pnr].phrase, i)) {
682                                         os << special_phrases[pnr].macro;
683                                         i += special_phrases[pnr].phrase.length() - 1;
684                                         column += special_phrases[pnr].macro.length() - 1;
685                                         break;
686                                 }
687                         }
688
689                         if (pnr == phrases_nr && c != '\0') {
690                                 os << c;
691                         }
692                         break;
693                 }
694         }
695 }
696
697
698 void Paragraph::Pimpl::validate(LaTeXFeatures & features,
699                                 LyXLayout const & layout) const
700 {
701         BufferParams const & bparams = features.bufferParams();
702
703         // check the params.
704         if (!params.spacing().isDefault())
705                 features.require("setspace");
706
707         // then the layouts
708         features.useLayout(layout.name());
709
710         // then the fonts
711         Language const * doc_language = bparams.language;
712
713         FontList::const_iterator fcit = fontlist.begin();
714         FontList::const_iterator fend = fontlist.end();
715         for (; fcit != fend; ++fcit) {
716                 if (fcit->font().noun() == LyXFont::ON) {
717                         lyxerr[Debug::LATEX] << "font.noun: "
718                                              << fcit->font().noun()
719                                              << endl;
720                         features.require("noun");
721                         lyxerr[Debug::LATEX] << "Noun enabled. Font: "
722                                              << fcit->font().stateText(0)
723                                              << endl;
724                 }
725                 switch (fcit->font().color()) {
726                 case LColor::none:
727                 case LColor::inherit:
728                 case LColor::ignore:
729                         // probably we should put here all interface colors used for
730                         // font displaying! For now I just add this ones I know of (Jug)
731                 case LColor::latex:
732                 case LColor::note:
733                         break;
734                 default:
735                         features.require("color");
736                         lyxerr[Debug::LATEX] << "Color enabled. Font: "
737                                              << fcit->font().stateText(0)
738                                              << endl;
739                 }
740
741                 Language const * language = fcit->font().language();
742                 if (language->babel() != doc_language->babel() &&
743                     language != ignore_language &&
744                     language != latex_language)
745                 {
746                         features.useLanguage(language);
747                         lyxerr[Debug::LATEX] << "Found language "
748                                              << language->babel() << endl;
749                 }
750         }
751
752         if (!params.leftIndent().zero())
753                 features.require("ParagraphLeftIndent");
754
755         // then the insets
756         InsetList::const_iterator icit = owner_->insetlist.begin();
757         InsetList::const_iterator iend = owner_->insetlist.end();
758         for (; icit != iend; ++icit) {
759                 if (icit->inset) {
760                         icit->inset->validate(features);
761                         if (layout.needprotect &&
762                             icit->inset->lyxCode() == InsetBase::FOOT_CODE)
763                                 features.require("NeedLyXFootnoteCode");
764                 }
765         }
766
767         // then the contents
768         for (pos_type i = 0; i < size() ; ++i) {
769                 for (size_t pnr = 0; pnr < phrases_nr; ++pnr) {
770                         if (!special_phrases[pnr].builtin
771                             && isTextAt(special_phrases[pnr].phrase, i)) {
772                                 features.require(special_phrases[pnr].phrase);
773                                 break;
774                         }
775                 }
776         }
777 }