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