]> git.lyx.org Git - lyx.git/blob - src/lyxtextclass.C
fix typo that put too many include paths for most people
[lyx.git] / src / lyxtextclass.C
1 /* This file is part of
2  * ======================================================
3  *
4  *           LyX, The Document Processor
5  *
6  *          Copyright 1995 Matthias Ettrich
7  *          Copyright 1995-2001 The LyX Team.
8  *
9  * ======================================================
10  */
11
12 #include <config.h>
13
14 #ifdef __GNUG__
15 #pragma implementation
16 #endif
17
18 #include "lyxtextclass.h"
19 #include "debug.h"
20 #include "lyxlex.h"
21
22 #include "support/lstrings.h"
23 #include "support/LAssert.h"
24 #include "support/lyxfunctional.h"
25 #include "support/filetools.h"
26
27 #include <algorithm>
28
29 using std::endl;
30 using std::find_if;
31 using std::remove_if;
32 using std::ostream;
33
34
35 /* ******************************************************************* */
36
37 LyXTextClass::LyXTextClass(string const & fn, string const & cln,
38                            string const & desc)
39         : name_(fn), latexname_(cln), description_(desc)
40 {
41         outputType_ = LATEX;
42         columns_ = 1;
43         sides_ = OneSide;
44         secnumdepth_ = 3;
45         tocdepth_ = 3;
46         pagestyle_ = "default";
47         maxcounter_ = LABEL_COUNTER_CHAPTER;
48         defaultfont_ = LyXFont(LyXFont::ALL_SANE);
49         opt_fontsize_ = "10|11|12";
50         opt_pagestyle_ = "empty|plain|headings|fancy";
51         provides_ = nothing;
52         loaded = false;
53 }
54
55
56 bool LyXTextClass::do_readStyle(LyXLex & lexrc, LyXLayout & lay)
57 {
58         lyxerr[Debug::TCLASS] << "Reading style " << lay.name() << endl;
59         if (!lay.Read(lexrc, *this)) {
60                 // Resolve fonts
61                 lay.resfont = lay.font;
62 #ifndef INHERIT_LANGUAGE
63                 lay.resfont.realize(defaultfont());
64                 lay.reslabelfont = lay.labelfont;
65                 lay.reslabelfont.realize(defaultfont());
66 #else
67                 lay.resfont.realize(defaultfont(), default_language);
68                 lay.reslabelfont = lay.labelfont;
69                 lay.reslabelfont.realize(defaultfont(), default_language);
70 #endif
71                 return false; // no errors
72         }
73         lyxerr << "Error parsing style `" << lay.name() << "'" << endl;
74         return true;
75 }
76
77
78 enum TextClassTags {
79         TC_OUTPUTTYPE = 1,
80         TC_INPUT,
81         TC_STYLE,
82         TC_DEFAULTSTYLE,
83         TC_NOSTYLE,
84         TC_COLUMNS,
85         TC_SIDES,
86         TC_PAGESTYLE,
87         TC_DEFAULTFONT,
88         TC_MAXCOUNTER,
89         TC_SECNUMDEPTH,
90         TC_TOCDEPTH,
91         TC_CLASSOPTIONS,
92         TC_PREAMBLE,
93         TC_PROVIDESAMSMATH,
94         TC_PROVIDESMAKEIDX,
95         TC_PROVIDESURL,
96         TC_LEFTMARGIN,
97         TC_RIGHTMARGIN
98 };
99
100
101 // Reads a textclass structure from file.
102 bool LyXTextClass::Read(string const & filename, bool merge)
103 {
104         keyword_item textClassTags[] = {
105                 { "classoptions",    TC_CLASSOPTIONS },
106                 { "columns",         TC_COLUMNS },
107                 { "defaultfont",     TC_DEFAULTFONT },
108                 { "defaultstyle",    TC_DEFAULTSTYLE },
109                 { "input",           TC_INPUT },
110                 { "leftmargin",      TC_LEFTMARGIN },
111                 { "maxcounter",      TC_MAXCOUNTER },
112                 { "nostyle",         TC_NOSTYLE },
113                 { "outputtype",      TC_OUTPUTTYPE },
114                 { "pagestyle",       TC_PAGESTYLE },
115                 { "preamble",        TC_PREAMBLE },
116                 { "providesamsmath", TC_PROVIDESAMSMATH },
117                 { "providesmakeidx", TC_PROVIDESMAKEIDX },
118                 { "providesurl",     TC_PROVIDESURL },
119                 { "rightmargin",     TC_RIGHTMARGIN },
120                 { "secnumdepth",     TC_SECNUMDEPTH },
121                 { "sides",           TC_SIDES },
122                 { "style",           TC_STYLE },
123                 { "tocdepth",        TC_TOCDEPTH }
124         };
125
126         if (!merge)
127                 lyxerr[Debug::TCLASS] << "Reading textclass "
128                                       << MakeDisplayPath(filename)
129                                       << endl;
130         else
131                 lyxerr[Debug::TCLASS] << "Reading input file "
132                                      << MakeDisplayPath(filename)
133                                      << endl;
134
135         LyXLex lexrc(textClassTags, TC_RIGHTMARGIN);
136         bool error = false;
137
138         lexrc.setFile(filename);
139         if (!lexrc.isOK()) error = true;
140
141         // parsing
142         while (lexrc.isOK() && !error) {
143                 int le = lexrc.lex();
144                 switch (le) {
145                 case LyXLex::LEX_FEOF:
146                         continue;
147
148                 case LyXLex::LEX_UNDEF:
149                         lexrc.printError("Unknown TextClass tag `$$Token'");
150                         error = true;
151                         continue;
152                 default: break;
153                 }
154                 switch (static_cast<TextClassTags>(le)) {
155                 case TC_OUTPUTTYPE:   // output type definition
156                         readOutputType(lexrc);
157                         break;
158
159                 case TC_INPUT: // Include file
160                         if (lexrc.next()) {
161                                 string tmp = LibFileSearch("layouts",
162                                                             lexrc.getString(),
163                                                             "layout");
164
165                                 if (Read(tmp, true)) {
166                                         lexrc.printError("Error reading input"
167                                                          "file: "+tmp);
168                                         error = true;
169                                 }
170                         }
171                         break;
172
173                 case TC_DEFAULTSTYLE:
174                         if (lexrc.next()) {
175                                 string const name = subst(lexrc.getString(),
176                                                           '_', ' ');
177                                 defaultlayout_ = name;
178                         }
179                         break;
180
181                 case TC_STYLE:
182                         if (lexrc.next()) {
183                                 string const name = subst(lexrc.getString(),
184                                                     '_', ' ');
185                                 if (hasLayout(name)) {
186                                         LyXLayout & lay =
187                                                 const_cast<LyXLayout &>(operator[](name));
188                                         error = do_readStyle(lexrc, lay);
189                                 } else {
190                                         LyXLayout lay;
191                                         lay.setName(name);
192                                         if (!(error = do_readStyle(lexrc, lay)))
193                                                 layoutlist.push_back(lay);
194                                         if (defaultlayout_.empty()) {
195                                                 // We do not have a default
196                                                 // layout yet, so we choose
197                                                 // the first layout we
198                                                 // encounter.
199                                                 defaultlayout_ = name;
200                                         }
201                                 }
202                         }
203                         else {
204                                 lexrc.printError("No name given for style: `$$Token'.");
205                                 error = true;
206                         }
207                         break;
208
209                 case TC_NOSTYLE:
210                         if (lexrc.next()) {
211                                 string const style = subst(lexrc.getString(),
212                                                      '_', ' ');
213                                 if (!delete_layout(style))
214                                         lyxerr << "Cannot delete style `" << style << "'" << endl;
215 //                                      lexrc.printError("Cannot delete style"
216 //                                                       " `$$Token'");
217                         }
218                         break;
219
220                 case TC_COLUMNS:
221                         if (lexrc.next())
222                                 columns_ = lexrc.getInteger();
223                         break;
224
225                 case TC_SIDES:
226                         if (lexrc.next()) {
227                                 switch (lexrc.getInteger()) {
228                                 case 1: sides_ = OneSide; break;
229                                 case 2: sides_ = TwoSides; break;
230                                 default:
231                                         lyxerr << "Impossible number of page"
232                                                 " sides, setting to one."
233                                                << endl;
234                                         sides_ = OneSide;
235                                         break;
236                                 }
237                         }
238                         break;
239
240                 case TC_PAGESTYLE:
241                         lexrc.next();
242                         pagestyle_ = strip(lexrc.getString());
243                         break;
244
245                 case TC_DEFAULTFONT:
246                         defaultfont_.lyxRead(lexrc);
247                         if (!defaultfont_.resolved()) {
248                                 lexrc.printError("Warning: defaultfont should "
249                                                  "be fully instantiated!");
250 #ifndef INHERIT_LANGUAGE
251                                 defaultfont_.realize(LyXFont(LyXFont::ALL_SANE));
252 #else
253                                 defaultfont_.realize(LyXFont(LyXFont::ALL_SANE),
254                                                      default_language);
255 #endif
256                         }
257                         break;
258
259                 case TC_MAXCOUNTER:
260                         readMaxCounter(lexrc);
261                         break;
262
263                 case TC_SECNUMDEPTH:
264                         lexrc.next();
265                         secnumdepth_ = lexrc.getInteger();
266                         break;
267
268                 case TC_TOCDEPTH:
269                         lexrc.next();
270                         tocdepth_ = lexrc.getInteger();
271                         break;
272
273                         // First step to support options
274                 case TC_CLASSOPTIONS:
275                         readClassOptions(lexrc);
276                         break;
277
278                 case TC_PREAMBLE:
279                         preamble_ = lexrc.getLongString("EndPreamble");
280                         break;
281
282                 case TC_PROVIDESAMSMATH:
283                         if (lexrc.next() && lexrc.getInteger())
284                                 provides_ |= amsmath;
285                         break;
286
287                 case TC_PROVIDESMAKEIDX:
288                         if (lexrc.next() && lexrc.getInteger())
289                                 provides_ |= makeidx;
290                         break;
291
292                 case TC_PROVIDESURL:
293                         if (lexrc.next() && lexrc.getInteger())
294                                 provides_ = url;
295                         break;
296
297                 case TC_LEFTMARGIN:     // left margin type
298                         if (lexrc.next())
299                                 leftmargin_ = lexrc.getString();
300                         break;
301
302                 case TC_RIGHTMARGIN:    // right margin type
303                         if (lexrc.next())
304                                 rightmargin_ = lexrc.getString();
305                         break;
306                 }
307         }
308
309         if (!merge) { // we are at top level here.
310                 lyxerr[Debug::TCLASS] << "Finished reading textclass "
311                                       << MakeDisplayPath(filename)
312                                       << endl;
313                 if (defaultlayout_.empty()) {
314                         lyxerr << "Error: Textclass '" << name_
315                                << "' is missing a defaultstyle." << endl;
316                         error = true;
317                 }
318         } else
319                 lyxerr[Debug::TCLASS] << "Finished reading input file "
320                                       << MakeDisplayPath(filename)
321                                       << endl;
322
323         return error;
324 }
325
326
327 void LyXTextClass::readOutputType(LyXLex & lexrc)
328 {
329         keyword_item outputTypeTags[] = {
330                 { "docbook", DOCBOOK },
331                 { "latex", LATEX },
332                 { "linuxdoc", LINUXDOC },
333                 { "literate", LITERATE }
334         };
335
336         pushpophelper pph(lexrc, outputTypeTags, LITERATE);
337
338         int le = lexrc.lex();
339         switch (le) {
340         case LyXLex::LEX_UNDEF:
341                 lexrc.printError("Unknown output type `$$Token'");
342                 return;
343         case LATEX:
344         case LINUXDOC:
345         case DOCBOOK:
346         case LITERATE:
347                 outputType_ = static_cast<OutputType>(le);
348                 break;
349         default:
350                 lyxerr << "Unhandled value " << le
351                        << " in LyXTextClass::readOutputType." << endl;
352
353                 break;
354         }
355 }
356
357
358 enum MaxCounterTags {
359         MC_COUNTER_CHAPTER = 1,
360         MC_COUNTER_SECTION,
361         MC_COUNTER_SUBSECTION,
362         MC_COUNTER_SUBSUBSECTION,
363         MC_COUNTER_PARAGRAPH,
364         MC_COUNTER_SUBPARAGRAPH,
365         MC_COUNTER_ENUMI,
366         MC_COUNTER_ENUMII,
367         MC_COUNTER_ENUMIII,
368         MC_COUNTER_ENUMIV
369 };
370
371
372 void LyXTextClass::readMaxCounter(LyXLex & lexrc)
373 {
374         keyword_item maxCounterTags[] = {
375                 {"counter_chapter", MC_COUNTER_CHAPTER },
376                 {"counter_enumi", MC_COUNTER_ENUMI },
377                 {"counter_enumii", MC_COUNTER_ENUMII },
378                 {"counter_enumiii", MC_COUNTER_ENUMIII },
379                 {"counter_enumiv", MC_COUNTER_ENUMIV },
380                 {"counter_paragraph", MC_COUNTER_PARAGRAPH },
381                 {"counter_section", MC_COUNTER_SECTION },
382                 {"counter_subparagraph", MC_COUNTER_SUBPARAGRAPH },
383                 {"counter_subsection", MC_COUNTER_SUBSECTION },
384                 {"counter_subsubsection", MC_COUNTER_SUBSUBSECTION }
385         };
386
387         pushpophelper pph(lexrc, maxCounterTags, MC_COUNTER_ENUMIV);
388         int le = lexrc.lex();
389         switch (le) {
390         case LyXLex::LEX_UNDEF:
391                 lexrc.printError("Unknown MaxCounter tag `$$Token'");
392                 return;
393         default: break;
394         }
395         switch (static_cast<MaxCounterTags>(le)) {
396         case MC_COUNTER_CHAPTER:
397                 maxcounter_ = LABEL_COUNTER_CHAPTER;
398                 break;
399         case MC_COUNTER_SECTION:
400                 maxcounter_ = LABEL_COUNTER_SECTION;
401                 break;
402         case MC_COUNTER_SUBSECTION:
403                 maxcounter_ = LABEL_COUNTER_SUBSECTION;
404                 break;
405         case MC_COUNTER_SUBSUBSECTION:
406                 maxcounter_ = LABEL_COUNTER_SUBSUBSECTION;
407                 break;
408         case MC_COUNTER_PARAGRAPH:
409                 maxcounter_ = LABEL_COUNTER_PARAGRAPH;
410                 break;
411         case MC_COUNTER_SUBPARAGRAPH:
412                 maxcounter_ = LABEL_COUNTER_SUBPARAGRAPH;
413                 break;
414         case MC_COUNTER_ENUMI:
415                 maxcounter_ = LABEL_COUNTER_ENUMI;
416                 break;
417         case MC_COUNTER_ENUMII:
418                 maxcounter_ = LABEL_COUNTER_ENUMII;
419                 break;
420         case MC_COUNTER_ENUMIII:
421                 maxcounter_ = LABEL_COUNTER_ENUMIII;
422                 break;
423         case MC_COUNTER_ENUMIV:
424                 maxcounter_ = LABEL_COUNTER_ENUMIV;
425                 break;
426         }
427 }
428
429
430 enum ClassOptionsTags {
431         CO_FONTSIZE = 1,
432         CO_PAGESTYLE,
433         CO_OTHER,
434         CO_END
435 };
436
437
438 void LyXTextClass::readClassOptions(LyXLex & lexrc)
439 {
440         keyword_item classOptionsTags[] = {
441                 {"end", CO_END },
442                 {"fontsize", CO_FONTSIZE },
443                 {"other", CO_OTHER },
444                 {"pagestyle", CO_PAGESTYLE }
445         };
446
447         lexrc.pushTable(classOptionsTags, CO_END);
448         bool getout = false;
449         while (!getout && lexrc.isOK()) {
450                 int le = lexrc.lex();
451                 switch (le) {
452                 case LyXLex::LEX_UNDEF:
453                         lexrc.printError("Unknown ClassOption tag `$$Token'");
454                         continue;
455                 default: break;
456                 }
457                 switch (static_cast<ClassOptionsTags>(le)) {
458                 case CO_FONTSIZE:
459                         lexrc.next();
460                         opt_fontsize_ = strip(lexrc.getString());
461                         break;
462                 case CO_PAGESTYLE:
463                         lexrc.next();
464                         opt_pagestyle_ = strip(lexrc.getString());
465                         break;
466                 case CO_OTHER:
467                         lexrc.next();
468                         options_ = lexrc.getString();
469                         break;
470                 case CO_END:
471                         getout = true;
472                         break;
473                 }
474         }
475         lexrc.popTable();
476 }
477
478
479 LyXFont const & LyXTextClass::defaultfont() const
480 {
481         return defaultfont_;
482 }
483
484
485 string const & LyXTextClass::leftmargin() const
486 {
487         return leftmargin_;
488 }
489
490
491 string const & LyXTextClass::rightmargin() const
492 {
493         return rightmargin_;
494 }
495
496
497 bool LyXTextClass::hasLayout(string const & n) const
498 {
499         string const name = (n.empty() ? defaultLayoutName() : n);
500
501         return find_if(layoutlist.begin(), layoutlist.end(),
502                        lyx::compare_memfun(&LyXLayout::name, name))
503                 != layoutlist.end();
504 }
505
506
507 LyXLayout const & LyXTextClass::operator[](string const & n) const
508 {
509         lyx::Assert(!n.empty());
510
511         if (n.empty())
512                 lyxerr << "Operator[] called with empty n" << endl;
513
514         string const name = (n.empty() ? defaultLayoutName() : n);
515
516         static string lastLayoutName;
517         static LayoutList::difference_type lastLayoutIndex;
518
519         if (name == lastLayoutName)
520                 return layoutlist[lastLayoutIndex];
521
522
523         LayoutList::const_iterator cit =
524                 find_if(layoutlist.begin(),
525                         layoutlist.end(),
526                         lyx::compare_memfun(&LyXLayout::name, name));
527
528         if (cit == layoutlist.end()) {
529                 lyxerr << "We failed to find the layout '" << name
530                        << "' in the layout list. You MUST investigate!"
531                        << endl;
532
533                 // we require the name to exist
534                 lyx::Assert(false);
535         }
536
537         lastLayoutName = name;
538         lastLayoutIndex = std::distance(layoutlist.begin(), cit);
539
540         return *cit;
541 }
542
543
544 bool LyXTextClass::delete_layout(string const & name)
545 {
546         if (name == defaultLayoutName())
547                 return false;
548
549         LayoutList::iterator it =
550                 remove_if(layoutlist.begin(), layoutlist.end(),
551                           lyx::compare_memfun(&LyXLayout::name, name));
552         LayoutList::iterator end = layoutlist.end();
553         bool const ret = (it != end);
554         layoutlist.erase(it, end);
555         return ret;
556 }
557
558
559 // Load textclass info if not loaded yet
560 bool LyXTextClass::load() const
561 {
562         if (loaded)
563                 return true;
564
565         // Read style-file
566         string const real_file = LibFileSearch("layouts", name_, "layout");
567
568         if (const_cast<LyXTextClass*>(this)->Read(real_file)) {
569                 lyxerr << "Error reading `"
570                        << MakeDisplayPath(real_file)
571                        << "'\n(Check `" << name_
572                        << "')\nCheck your installation and "
573                         "try Options/Reconfigure..." << endl;
574                 loaded = false;
575         }
576         loaded = true;
577         return loaded;
578
579 }
580
581
582 string const LyXTextClass::defaultLayoutName() const
583 {
584         // This really should come from the actual layout... (Lgb)
585         return defaultlayout_;
586 }
587
588
589 LyXLayout const & LyXTextClass::defaultLayout() const
590 {
591         return operator[](defaultLayoutName());
592 }
593
594
595 string const & LyXTextClass::name() const
596 {
597         return name_;
598 }
599
600
601 string const & LyXTextClass::latexname() const
602 {
603         const_cast<LyXTextClass*>(this)->load();
604         return latexname_;
605 }
606
607
608 string const & LyXTextClass::description() const
609 {
610         return description_;
611 }
612
613
614 string const & LyXTextClass::opt_fontsize() const
615 {
616         return opt_fontsize_;
617 }
618
619
620 string const & LyXTextClass::opt_pagestyle() const
621 {
622         return opt_pagestyle_;
623 }
624
625
626 string const & LyXTextClass::options() const
627 {
628         return options_;
629 }
630
631
632 string const & LyXTextClass::pagestyle() const
633 {
634         return pagestyle_;
635 }
636
637
638 string const & LyXTextClass::preamble() const
639 {
640         return preamble_;
641 }
642
643
644 LyXTextClass::PageSides LyXTextClass::sides() const
645 {
646         return sides_;
647 }
648
649
650 int LyXTextClass::secnumdepth() const
651 {
652         return secnumdepth_;
653 }
654
655
656 int LyXTextClass::tocdepth() const
657 {
658         return tocdepth_;
659 }
660
661
662 OutputType LyXTextClass::outputType() const
663 {
664         return outputType_;
665 }
666
667
668 bool LyXTextClass::provides(LyXTextClass::Provides p) const
669 {
670         return provides_ & p;
671 }
672
673
674 unsigned int LyXTextClass::columns() const
675 {
676         return columns_;
677 }
678
679
680 int LyXTextClass::maxcounter() const
681 {
682         return maxcounter_;
683 }
684
685
686 int LyXTextClass::size() const
687 {
688         return layoutlist.size();
689 }
690
691
692 ostream & operator<<(ostream & os, LyXTextClass::PageSides p)
693 {
694         switch (p) {
695         case LyXTextClass::OneSide:
696                 os << "1";
697                 break;
698         case LyXTextClass::TwoSides:
699                 os << "2";
700                 break;
701         }
702         return os;
703 }