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