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