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