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