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