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