]> git.lyx.org Git - lyx.git/blob - src/lyxtextclass.C
frontends fixes from Juergen S
[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         lyx::Assert(!n.empty());
509         
510         if (n.empty())
511                 lyxerr << "Operator[] called with empty n" << endl;
512         
513         string const name = (n.empty() ? defaultLayoutName() : n);
514         
515         LayoutList::const_iterator cit =
516                 find_if(layoutlist.begin(),
517                         layoutlist.end(),
518                         lyx::compare_memfun(&LyXLayout::name, name));
519
520         if (cit == layoutlist.end()) {
521                 lyxerr << "We failed to find the layout '" << name
522                        << "' in the layout list. You MUST investigate!"
523                        << endl;
524                 
525                 // we require the name to exist
526                 lyx::Assert(false);
527         }
528
529         return *cit;
530 }
531
532
533 LyXLayout & LyXTextClass::operator[](string const & n)
534 {
535         lyx::Assert(!n.empty());
536
537         if (n.empty())
538                 lyxerr << "Operator[] called with empty n" << endl;
539
540         string const name = (n.empty() ? defaultLayoutName() : n);
541         
542         LayoutList::iterator it =
543                 find_if(layoutlist.begin(),
544                         layoutlist.end(),
545                         lyx::compare_memfun(&LyXLayout::name, name));
546
547         if (it == layoutlist.end()) {
548                 lyxerr << "We failed to find the layout '" << name
549                        << "' in the layout list. You MUST investigate!"
550                        << endl;
551                 
552                 // we require the name to exist
553                 lyx::Assert(false);
554         }
555         
556         return *it;
557 }
558
559
560 bool LyXTextClass::delete_layout(string const & name)
561 {
562         if (name == defaultLayoutName())
563                 return false;
564         
565         LayoutList::iterator it =
566                 remove_if(layoutlist.begin(), layoutlist.end(),
567                           lyx::compare_memfun(&LyXLayout::name, name));
568         LayoutList::iterator end = layoutlist.end();
569         bool const ret = (it != end);
570         layoutlist.erase(it, end);
571         return ret;
572 }
573
574
575 // Load textclass info if not loaded yet
576 bool LyXTextClass::load() const
577 {
578         if (loaded)
579                 return true;
580
581         // Read style-file
582         string const real_file = LibFileSearch("layouts", name_, "layout");
583
584         if (const_cast<LyXTextClass*>(this)->Read(real_file)) {
585                 lyxerr << "Error reading `"
586                        << MakeDisplayPath(real_file)
587                        << "'\n(Check `" << name_
588                        << "')\nCheck your installation and "
589                         "try Options/Reconfigure..." << endl;
590                 loaded = false;
591         }
592         loaded = true;
593         return loaded;
594         
595 }
596
597
598 string const LyXTextClass::defaultLayoutName() const
599 {
600         // This really should come from the actual layout... (Lgb)
601         return defaultlayout_;
602 }
603
604
605 LyXLayout const & LyXTextClass::defaultLayout() const
606 {
607         return operator[](defaultLayoutName());
608 }
609
610
611 LyXLayout & LyXTextClass::defaultLayout()
612 {
613         return operator[](defaultLayoutName());
614 }
615
616
617 string const & LyXTextClass::name() const
618 {
619         return name_;
620 }
621
622
623 string const & LyXTextClass::latexname() const
624 {
625         const_cast<LyXTextClass*>(this)->load();
626         return latexname_;
627 }
628
629
630 string const & LyXTextClass::description() const
631 {
632         return description_;
633 }
634
635
636 string const & LyXTextClass::opt_fontsize() const
637 {
638         return opt_fontsize_;
639 }
640
641
642 string const & LyXTextClass::opt_pagestyle() const
643 {
644         return opt_pagestyle_;
645 }
646
647
648 string const & LyXTextClass::options() const
649 {
650         return options_;
651 }
652
653
654 string const & LyXTextClass::pagestyle() const
655 {
656         return pagestyle_;
657 }
658
659
660 string const & LyXTextClass::preamble() const
661 {
662         return preamble_;
663 }
664
665
666 LyXTextClass::PageSides LyXTextClass::sides() const
667 {
668         return sides_;
669 }
670
671
672 int LyXTextClass::secnumdepth() const
673 {
674         return secnumdepth_;
675 }
676
677
678 int LyXTextClass::tocdepth() const
679 {
680         return tocdepth_;
681 }
682
683
684 OutputType LyXTextClass::outputType() const
685 {
686         return outputType_;
687 }
688
689
690 bool LyXTextClass::provides(LyXTextClass::Provides p) const
691 {
692         return provides_ & p;
693 }
694         
695
696 unsigned int LyXTextClass::columns() const
697 {
698         return columns_;
699 }
700
701
702 int LyXTextClass::maxcounter() const
703 {
704         return maxcounter_;
705 }
706
707
708 int LyXTextClass::size() const
709 {
710         return layoutlist.size();
711 }
712
713
714 ostream & operator<<(ostream & os, LyXTextClass::PageSides p)
715 {
716         switch (p) {
717         case LyXTextClass::OneSide:
718                 os << "1";
719                 break;
720         case LyXTextClass::TwoSides:
721                 os << "2";
722                 break;
723         }
724         return os;
725 }