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