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