]> git.lyx.org Git - lyx.git/blob - src/layout.C
7c73af5362cba619e95bd507049e079cc1cb375b
[lyx.git] / src / layout.C
1 /* This file is part of
2  * ======================================================
3  * 
4  *           LyX, The Document Processor
5  *       
6  *          Copyright 1995 Matthias Ettrich
7  *          Copyright 1995-1999 The LyX Team.
8  *
9  * ======================================================*/
10
11 /* Change log:
12  * 
13  *  14/11/1995,   Pascal AndrĂ© <andre@via.ecp.fr>
14  *  Modified for external style definition. 
15  * 
16  *  15/11/1995,   Alejandro Aguilar Sierra <asierra@servidor.unam.mx>
17  *  Modified to use binary search and a small pseudo lexical analyzer.
18  *  
19  *  29/03/1996,  Dirk Niggeman
20  *  Created classes LyXTextClass & LyXLayout.
21  * 
22  *  30/03/1996,  asierra
23  *  Created class LyxLex and improved the lexical analyzer. 
24  */
25
26 #include <config.h>
27
28 #ifdef __GNUG__
29 #pragma implementation
30 #endif
31
32 #include "definitions.h"
33 #include <cstdlib>
34 #include "layout.h"
35 #include "lyxlex.h"
36 #include "support/filetools.h"
37 #include "lyx_gui_misc.h"
38 #include "debug.h"
39 #include "gettext.h"
40
41 /* Global variable: textclass table */
42 LyXTextClassList lyxstyle;
43
44 // Reads the style files
45 void LyXSetStyle()
46 {
47         lyxerr.debug() << "LyXSetStyle: parsing configuration..." << endl;
48         
49         if (!lyxstyle.Read()) {
50                 lyxerr << "LyXSetStyle: an error occured during parsing.\n"
51                        << "             Exiting." << endl;
52                 exit(1);
53         }
54
55         lyxerr.debug() << "LyXSetStyle: configuration parsed." << endl;
56 }
57
58
59 //  The order of the LayoutTags enum is no more important. [asierra300396]
60 /* tags indexes */
61 enum _LayoutTags {
62         LT_ALIGN, LT_ALIGNPOSSIBLE, 
63         LT_BLOCK, LT_MARGIN,
64         LT_BOTTOMSEP, LT_CENTER, LT_CENTERED_TOP_ENVIRONMENT, LT_COLUMNS,
65         LT_COPYSTYLE, LT_OBSOLETEDBY,
66         LT_COMMAND, LT_COUNTER_CHAPTER, LT_COUNTER_ENUMI, LT_COUNTER_ENUMII,
67         LT_COUNTER_ENUMIII, LT_COUNTER_ENUMIV, LT_COUNTER_PARAGRAPH,
68         LT_COUNTER_SECTION, LT_COUNTER_SUBPARAGRAPH, LT_COUNTER_SUBSECTION,
69         LT_COUNTER_SUBSUBSECTION, LT_DEFAULTFONT, LT_DYNAMIC, LT_EMPTY,
70         LT_END, LT_ENVIRONMENT, LT_ENVIRONMENT_DEFAULT, 
71         LT_FANCYHDR, LT_FILL_BOTTOM, LT_FILL_TOP, LT_FIRST_COUNTER,
72         LT_FIRST_DYNAMIC, LT_FONT, LT_FREE_SPACING, LT_HEADINGS, LT_INPUT,
73         LT_ITEM_ENVIRONMENT, LT_ITEMSEP, LT_KEEPEMPTY,
74         LT_LABEL_BOTTOMSEP, LT_LABELFONT, LT_TEXTFONT,
75         LT_LABELINDENT, LT_LABELSEP, LT_LABELSTRING,
76         LT_LABELSTRING_APPENDIX, LT_LABELTYPE,
77         LT_LATEXNAME, LT_LATEXPARAM, LT_LATEXTYPE, LT_LAYOUT, LT_LEFT,
78         LT_LEFTMARGIN,
79         LT_LIST_ENVIRONMENT , LT_MANUAL, LT_MAXCOUNTER, 
80         LT_NEED_PROTECT, LT_NEWLINE,
81         LT_NEXTNOINDENT, LT_NO_LABEL, LT_NOSTYLE,
82         LT_PAGESTYLE, LT_PARAGRAPH,
83         LT_PARINDENT, LT_PARSEP, LT_PARSKIP, LT_PLAIN, LT_PREAMBLE, 
84         LT_PROVIDESAMSMATH, LT_PROVIDESMAKEIDX, LT_PROVIDESURL, LT_RIGHT,
85         LT_RIGHT_ADDRESS_BOX, LT_RIGHTMARGIN, LT_SENSITIVE, LT_SIDES,
86         LT_SPACING, LT_SPACING_SINGLE, LT_SPACING_ONEHALF,
87         LT_SPACING_DOUBLE, LT_OTHER,  LT_CLASSOPTIONS, LT_FONTSIZE,
88         LT_STATIC, LT_STYLE, LT_TOP_ENVIRONMENT, LT_TOPSEP, LT_BIBLIO,
89         LT_INTITLE, LT_SECNUMDEPTH, LT_TOCDEPTH,
90         LT_OUTPUTTYPE, LT_OTLATEX, LT_OTLINUXDOC, LT_OTDOCBOOK, LT_OTLITERATE
91 };
92
93
94 // This table is sorted alphabetically [asierra 30March96]
95 static keyword_item layoutTags[] = {
96         { "align",                      LT_ALIGN },
97         { "alignpossible",              LT_ALIGNPOSSIBLE },
98         { "bibliography",               LT_BIBLIO },
99         { "block",                      LT_BLOCK },
100         { "bottomsep",                  LT_BOTTOMSEP },
101         { "center",                     LT_CENTER },
102         { "centered_top_environment",   LT_CENTERED_TOP_ENVIRONMENT },
103         { "classoptions",               LT_CLASSOPTIONS },
104         { "columns",                    LT_COLUMNS },
105         { "command",                    LT_COMMAND },
106         { "copystyle",                  LT_COPYSTYLE },
107         { "counter_chapter",            LT_COUNTER_CHAPTER },
108         { "counter_enumi",              LT_COUNTER_ENUMI },
109         { "counter_enumii",             LT_COUNTER_ENUMII },
110         { "counter_enumiii",            LT_COUNTER_ENUMIII },
111         { "counter_enumiv",             LT_COUNTER_ENUMIV },
112         { "counter_paragraph",          LT_COUNTER_PARAGRAPH },
113         { "counter_section",            LT_COUNTER_SECTION },
114         { "counter_subparagraph",       LT_COUNTER_SUBPARAGRAPH },
115         { "counter_subsection",         LT_COUNTER_SUBSECTION },
116         { "counter_subsubsection",      LT_COUNTER_SUBSUBSECTION },
117         { "defaultfont",                LT_DEFAULTFONT },
118         { "docbook",                    LT_OTDOCBOOK },
119         { "double",                     LT_SPACING_DOUBLE },
120         { "dynamic",                    LT_DYNAMIC },
121         { "empty",                      LT_EMPTY },
122         { "end",                        LT_END },
123         { "environment",                LT_ENVIRONMENT },
124         { "environment_default",        LT_ENVIRONMENT_DEFAULT },
125         { "fancyhdr",                   LT_FANCYHDR },
126         { "fill_bottom",                LT_FILL_BOTTOM },
127         { "fill_top",                   LT_FILL_TOP },
128         { "first_counter",              LT_FIRST_COUNTER },
129         { "first_dynamic",              LT_FIRST_DYNAMIC },
130         { "font",                       LT_FONT },
131         { "fontsize",                   LT_FONTSIZE },
132         { "freespacing",                LT_FREE_SPACING },
133         { "headings",                   LT_HEADINGS },
134         { "input",                      LT_INPUT },
135         { "intitle",                    LT_INTITLE },
136         { "item_environment",           LT_ITEM_ENVIRONMENT },
137         { "itemsep",                    LT_ITEMSEP },
138         { "keepempty",                  LT_KEEPEMPTY },
139         { "labelbottomsep",             LT_LABEL_BOTTOMSEP },
140         { "labelfont",                  LT_LABELFONT },
141         { "labelindent",                LT_LABELINDENT },
142         { "labelsep",                   LT_LABELSEP },
143         { "labelstring",                LT_LABELSTRING },
144         { "labelstringappendix",                LT_LABELSTRING_APPENDIX },
145         { "labeltype",                  LT_LABELTYPE },
146         { "latex",                      LT_OTLATEX },
147         { "latexname",                  LT_LATEXNAME },
148         { "latexparam",                 LT_LATEXPARAM },    //arrae970411
149         { "latextype",                  LT_LATEXTYPE },
150         { "layout",                     LT_LAYOUT },
151         { "left",                       LT_LEFT },
152         { "leftmargin",                 LT_LEFTMARGIN },
153         { "linuxdoc",                   LT_OTLINUXDOC },
154         { "list_environment",           LT_LIST_ENVIRONMENT },
155         { "literate",                   LT_OTLITERATE },
156         { "manual",                     LT_MANUAL },
157         { "margin",                     LT_MARGIN },
158         { "maxcounter",                 LT_MAXCOUNTER },
159         { "needprotect",                LT_NEED_PROTECT },
160         { "newline",                    LT_NEWLINE },
161         { "nextnoindent",               LT_NEXTNOINDENT },
162         { "no_label",                   LT_NO_LABEL },
163         { "nostyle",                    LT_NOSTYLE },
164         { "obsoletedby",                LT_OBSOLETEDBY },
165         { "onehalf",                    LT_SPACING_ONEHALF },
166         { "other",                      LT_OTHER },
167         { "outputtype",                 LT_OUTPUTTYPE },
168         { "pagestyle",                  LT_PAGESTYLE },
169         { "paragraph",                  LT_PARAGRAPH },
170         { "parindent",                  LT_PARINDENT },
171         { "parsep",                     LT_PARSEP },
172         { "parskip",                    LT_PARSKIP },
173         { "plain",                      LT_PLAIN },
174         { "preamble",                   LT_PREAMBLE },
175         { "providesamsmath",            LT_PROVIDESAMSMATH },
176         { "providesmakeidx",            LT_PROVIDESMAKEIDX },
177         { "providesurl",                LT_PROVIDESURL },
178         { "right",                      LT_RIGHT },
179         { "right_address_box",          LT_RIGHT_ADDRESS_BOX },
180         { "rightmargin",                LT_RIGHTMARGIN },
181         { "secnumdepth",                LT_SECNUMDEPTH },
182         { "sensitive",                  LT_SENSITIVE },
183         { "sides",                      LT_SIDES },
184         { "single",                     LT_SPACING_SINGLE },
185         { "spacing",                    LT_SPACING },
186         { "static",                     LT_STATIC },
187         { "style",                      LT_STYLE },
188         { "textfont",                   LT_TEXTFONT },
189         { "tocdepth",                   LT_TOCDEPTH },
190         { "top_environment",            LT_TOP_ENVIRONMENT },
191         { "topsep",                     LT_TOPSEP }
192 };
193
194
195 /* ******************************************************************* */
196
197 // Constructor for layout
198 LyXLayout::LyXLayout ()
199 {
200         margintype = MARGIN_STATIC;
201         latextype = LATEX_PARAGRAPH;
202         intitle = false;
203         needprotect = false;
204         keepempty = false;
205         font = LyXFont(LyXFont::ALL_INHERIT);
206         labelfont = LyXFont(LyXFont::ALL_INHERIT);
207         resfont = LyXFont(LyXFont::ALL_SANE);
208         reslabelfont = LyXFont(LyXFont::ALL_SANE);
209         nextnoindent = false;
210         parskip = 0.0;
211         itemsep = 0;
212         topsep = 0.0;
213         bottomsep = 0.0;
214         labelbottomsep = 0.0;
215         parsep = 0;
216         align = LYX_ALIGN_BLOCK;
217         alignpossible = LYX_ALIGN_BLOCK;
218         labeltype = LABEL_NO_LABEL;
219         // Should or should not. That is the question.
220         // spacing.set(Spacing::OneHalf);
221         fill_top = false;
222         fill_bottom = false;
223         newline_allowed = true;
224         free_spacing = false;
225 }
226
227
228 LyXLayout::~LyXLayout ()
229 {
230 }
231
232
233 void LyXLayout::Copy (LyXLayout const &l)
234 {
235         name = l.name;
236         obsoleted_by = l.obsoleted_by;
237         margintype = l.margintype;
238         latextype = l.latextype;
239         intitle = l.intitle;
240         needprotect = l.needprotect;
241         keepempty = l.keepempty;
242         latexname = l.latexname;
243         latexparam = l.latexparam;   //arrae970411
244         preamble = l.preamble;
245         font = l.font;
246         labelfont = l.labelfont;
247         resfont = l.resfont;
248         reslabelfont = l.reslabelfont;
249         nextnoindent = l.nextnoindent;
250         leftmargin = l.leftmargin;
251         rightmargin = l.rightmargin;
252         labelsep = l.labelsep;
253         labelindent = l.labelindent;
254         parindent = l.parindent;
255         parskip = l.parskip;
256         itemsep = l.itemsep;
257         topsep = l.topsep;
258         bottomsep = l.bottomsep;
259         labelbottomsep = l.labelbottomsep;
260         parsep = l.parsep;
261         align = l.align;
262         alignpossible = l.alignpossible;
263         labeltype = l.labeltype;
264         spacing = l.spacing;
265         labelstring = l.labelstring;
266         labelstring_appendix = l.labelstring_appendix;
267         fill_top = l.fill_top;
268         fill_bottom = l.fill_bottom;
269         newline_allowed = l.newline_allowed;
270         free_spacing = l.free_spacing;
271 }
272
273
274 /* Reads a layout definition from file */
275 bool LyXLayout::Read (LyXLex & lexrc, LyXLayoutList * list)
276 {
277         bool error = false;
278         bool finished = false;
279         
280         /* parse style section */
281         while (!finished && lexrc.IsOK() && !error) {
282                 switch(lexrc.lex()) {
283
284                 case -2:
285                         break;
286
287                 case -1:                /* parse error */
288                         lexrc.printError("Unknown tag `$$Token'");
289                         error = true;
290                         break;
291
292                 case LT_END:            /* end of structure */
293                         finished = true;
294                         break;
295
296                 case LT_COPYSTYLE:     // initialize with a known style
297                         if (lexrc.next()) {
298                                 LyXLayout * layout = list->GetLayout(lexrc.GetString());
299                                 if (layout) {
300                                         string tmpname = name;
301                                         Copy(*layout);
302                                         name = tmpname;
303                                 } else {
304                                         lexrc.printError("Cannot copy unknown "
305                                                  "style `$$Token'");
306                                 }
307                         }
308                         break;
309
310                 case LT_OBSOLETEDBY:     // replace with a known style
311                         if (lexrc.next()) {
312                                 LyXLayout * layout = list->GetLayout(lexrc.GetString());
313                                 if (layout) {
314                                         string tmpname = name;
315                                         Copy(*layout);
316                                         name = tmpname;
317                                         if (obsoleted_by.empty())
318                                                 obsoleted_by = lexrc.GetString();
319                                 } else {
320                                         lexrc.printError("Cannot replace with" 
321                                                          " unknown style "
322                                                          "`$$Token'");
323                                 }
324                         }
325                         break;
326
327                 case LT_MARGIN:         /* margin style definition */
328                        
329                         switch(lexrc.lex()) {
330                         case LT_STATIC:
331                                 margintype = MARGIN_STATIC;
332                                 break;
333                         case LT_MANUAL:
334                                 margintype = MARGIN_MANUAL;
335                                 break;
336                         case LT_DYNAMIC:
337                                 margintype = MARGIN_DYNAMIC;
338                                 break;
339                         case LT_FIRST_DYNAMIC:
340                                 margintype = MARGIN_FIRST_DYNAMIC;
341                                 break;
342                         case LT_RIGHT_ADDRESS_BOX:
343                                 margintype = MARGIN_RIGHT_ADDRESS_BOX;
344                                 break;
345                         default:
346                                 lexrc.printError("Unknown margin type `$$Token'");
347                                 break;
348                         }
349                         break;
350
351                 case LT_LATEXTYPE:      /* latex style definition */
352                         switch (lexrc.lex()) {
353                         case LT_PARAGRAPH:
354                                 latextype=LATEX_PARAGRAPH;
355                                 break;
356                         case LT_COMMAND:
357                                 latextype=LATEX_COMMAND;
358                                 break;
359                         case LT_ENVIRONMENT:
360                                 latextype=LATEX_ENVIRONMENT;
361                                 break;
362                         case LT_ITEM_ENVIRONMENT:
363                                 latextype=LATEX_ITEM_ENVIRONMENT;
364                                 break;
365                         case LT_LIST_ENVIRONMENT:
366                                 latextype=LATEX_LIST_ENVIRONMENT;
367                                 break;
368                         default:
369                                 lexrc.printError("Unknown latextype `$$Token'");
370                                 break;
371                         }
372                         break;
373
374                 case LT_INTITLE:
375                         intitle = lexrc.next() && lexrc.GetInteger();
376                         break;
377                         
378                 case LT_NEED_PROTECT:
379                         needprotect = lexrc.next() && lexrc.GetInteger();
380                         break;
381                         
382                 case LT_KEEPEMPTY:
383                         keepempty = lexrc.next() && lexrc.GetInteger();
384                         break;
385
386                 case LT_FONT:
387                         font.lyxRead(lexrc);
388                         labelfont=font;
389                         break;
390
391                 case LT_TEXTFONT:
392                         font.lyxRead(lexrc);
393                         break;
394
395                 case LT_LABELFONT:
396                         labelfont.lyxRead(lexrc);
397                         break;
398
399                 case LT_NEXTNOINDENT:   /* indent next paragraph ? */
400                         if (lexrc.next() && lexrc.GetInteger())
401                                 nextnoindent = true;
402                         else
403                                 nextnoindent = false;
404                         break;
405
406                 case LT_LATEXNAME:      /* latex name */
407                         if (lexrc.next())
408                                 latexname = lexrc.GetString();
409                         break;
410                         
411                 //arrae970411
412                 case LT_LATEXPARAM:     /* latex parameter */
413                         if (lexrc.next())
414                                 latexparam = lexrc.GetString();
415                         break;
416
417                 case LT_PREAMBLE:
418                         preamble = lexrc.getLongString("EndPreamble");
419                         break;
420
421                 case LT_LABELTYPE:      /* label type */
422                         switch (lexrc.lex()) {
423                         case LT_NO_LABEL:
424                                 labeltype = LABEL_NO_LABEL;
425                                 break;
426                         case LT_MANUAL:
427                                 labeltype = LABEL_MANUAL;
428                                 break;
429                         case LT_TOP_ENVIRONMENT:
430                                 labeltype = LABEL_TOP_ENVIRONMENT;
431                                 break;
432                         case LT_CENTERED_TOP_ENVIRONMENT:
433                                 labeltype = LABEL_CENTERED_TOP_ENVIRONMENT;
434                                 break;
435                         case LT_STATIC:
436                                 labeltype = LABEL_STATIC;
437                                 break;
438                         case LT_SENSITIVE:
439                                 labeltype = LABEL_SENSITIVE;
440                                 break;
441                         case LT_COUNTER_CHAPTER:
442                                 labeltype = LABEL_COUNTER_CHAPTER;
443                                 break;
444                         case LT_COUNTER_SECTION:
445                                 labeltype = LABEL_COUNTER_SECTION;
446                                 break;
447                         case LT_COUNTER_SUBSECTION:
448                                 labeltype = LABEL_COUNTER_SUBSECTION;
449                                 break;
450                         case LT_COUNTER_SUBSUBSECTION:
451                                 labeltype = LABEL_COUNTER_SUBSUBSECTION;
452                                 break;
453                         case LT_COUNTER_PARAGRAPH:
454                                 labeltype = LABEL_COUNTER_PARAGRAPH;
455                                 break;
456                         case LT_COUNTER_SUBPARAGRAPH:
457                                 labeltype = LABEL_COUNTER_SUBPARAGRAPH;
458                                 break;
459                         case LT_COUNTER_ENUMI:
460                                 labeltype = LABEL_COUNTER_ENUMI;
461                                 break;
462                         case LT_COUNTER_ENUMII:
463                                 labeltype = LABEL_COUNTER_ENUMII;
464                                 break;
465                         case LT_COUNTER_ENUMIII:
466                                 labeltype = LABEL_COUNTER_ENUMIII;
467                                 break;
468                         case LT_COUNTER_ENUMIV:
469                                 labeltype = LABEL_COUNTER_ENUMIV;
470                                 break;
471                         case LT_BIBLIO:
472                                 labeltype = LABEL_BIBLIO;
473                                 break;
474                         default:
475                                 lexrc.printError("Unknown labeltype `$$Token'");
476                         }
477                         break;
478
479                 case LT_LEFTMARGIN:     /* left margin type */
480                         if (lexrc.next())
481                                 leftmargin = lexrc.GetString();
482                         break;                  
483
484                 case LT_RIGHTMARGIN:    /* right margin type */
485                         if (lexrc.next())
486                                 rightmargin = lexrc.GetString();
487                         break;
488
489                 case LT_LABELINDENT:    /* label indenting flag */
490                         if (lexrc.next())
491                                 labelindent = lexrc.GetString();
492                         break;
493
494                 case LT_PARINDENT:      /* paragraph indent. flag */
495                         if (lexrc.next())
496                                 parindent = lexrc.GetString();
497                         break;
498
499                 case LT_PARSKIP:        /* paragraph skip size */
500                         if (lexrc.next())
501                                 parskip = lexrc.GetFloat();
502                         break;
503
504                 case LT_ITEMSEP:        /* item separation size */
505                         if (lexrc.next())
506                                 itemsep = lexrc.GetFloat();
507                         break;
508
509                 case LT_TOPSEP:         /* top separation size */
510                         if (lexrc.next())
511                                 topsep = lexrc.GetFloat();
512                         break;
513
514                 case LT_BOTTOMSEP:      /* bottom separation size */
515                         if (lexrc.next())
516                                 bottomsep = lexrc.GetFloat();
517                         break;
518
519                 case LT_LABEL_BOTTOMSEP:/* label bottom separation size */
520                         if (lexrc.next())
521                                 labelbottomsep = lexrc.GetFloat();
522                         break;
523
524                 case LT_LABELSEP:       /* label separator */
525                         if (lexrc.next()) {
526                                 labelsep = subst(lexrc.GetString(), 'x', ' ');
527                         }
528                         break;
529
530                 case LT_PARSEP:         /* par. separation size */
531                         if (lexrc.next())
532                                 parsep = lexrc.GetFloat();
533                         break;
534
535                 case LT_FILL_TOP:       /* fill top flag */
536                         if (lexrc.next())
537                                 fill_top = lexrc.GetInteger();
538                         break;
539
540                 case LT_FILL_BOTTOM:    /* fill bottom flag */
541                         if (lexrc.next())
542                                 fill_bottom = lexrc.GetInteger();
543                         break;
544
545                 case LT_NEWLINE:        /* newlines allowed ? */
546                         if (lexrc.next())
547                                 newline_allowed = lexrc.GetInteger();
548                         break;
549
550                 case LT_ALIGN:          /* paragraph align */
551                         switch (lexrc.lex()) {
552                         case LT_BLOCK:
553                                 align = LYX_ALIGN_BLOCK;
554                                 break;
555                         case LT_LEFT:
556                                 align = LYX_ALIGN_LEFT;
557                                 break;
558                         case LT_RIGHT:
559                                 align = LYX_ALIGN_RIGHT;
560                                 break;
561                         case LT_CENTER:
562                                 align = LYX_ALIGN_CENTER;
563                                 break;
564                         case LT_LAYOUT:
565                                 align = LYX_ALIGN_LAYOUT;
566                                 break;
567                         default:
568                                 lexrc.printError("Unknown alignment `$$Token'");
569                         }
570                         break;
571
572                 case LT_ALIGNPOSSIBLE:  /* paragraph allowed align */
573                 {       alignpossible = 0;
574                       
575                 int lineno = lexrc.GetLineNo();
576                 do {
577                         switch (lexrc.lex()) {
578                         case LT_BLOCK:
579                                 alignpossible |= LYX_ALIGN_BLOCK;
580                                 break;
581                         case LT_LEFT:
582                                 alignpossible |= LYX_ALIGN_LEFT;
583                                 break;
584                         case LT_RIGHT:
585                                 alignpossible |= LYX_ALIGN_RIGHT;
586                                 break;
587                         case LT_CENTER:
588                                 alignpossible |= LYX_ALIGN_CENTER;
589                                 break;
590                         case LT_LAYOUT:
591                                 alignpossible |= LYX_ALIGN_LAYOUT;
592                                 break;
593                         default:
594                                 lexrc.printError("Unknown alignment `$$Token'");
595
596                         }
597                 } while (lineno==lexrc.GetLineNo());
598                 break;
599                 }
600
601                 case LT_LABELSTRING:    /* label string definition */
602                         if (lexrc.next())
603                                 labelstring = lexrc.GetString();
604                         break;
605
606                 case LT_LABELSTRING_APPENDIX:   /* label string appendix definition */
607                         if (lexrc.next())
608                                 labelstring_appendix = lexrc.GetString();
609                         break;
610
611                 case LT_FREE_SPACING:   /* Allow for free spacing. */
612                         if (lexrc.next())
613                                 free_spacing = lexrc.GetInteger();
614                         break;
615
616                 case LT_SPACING: // setspace.sty
617                         switch(lexrc.lex()) {
618                         case LT_SPACING_SINGLE:
619                                 spacing.set(Spacing::Single);
620                                 //spacing_value = 1.0;
621                                 break;
622                         case LT_SPACING_ONEHALF:
623                                 spacing.set(Spacing::Onehalf);
624                                 //spacing_value = 1.25;
625                                 break;
626                         case LT_SPACING_DOUBLE:
627                                 spacing.set(Spacing::Double);
628                                 //spacing_value = 1.667;
629                                 break;
630                         case LT_OTHER:
631                                 lexrc.next();
632                                 spacing.set(Spacing::Other, lexrc.GetFloat());
633                                 break;
634                         default:
635                                 lexrc.printError("Unknown spacing `$$Token'");
636                         }
637                         break;
638                 default:                /* context error */
639                         lexrc.printError("Tag `$$Token' is not "
640                                          "allowed in layout");
641                         error = true;
642                         break;
643                 }
644         }
645
646         return error;
647 }
648
649 /* ******************************************************************* */
650
651 LyXLayoutList::LyXLayoutList()
652 {
653         l = 0;
654         eol = 0;
655         num_layouts = 0;
656 }
657
658
659 LyXLayoutList::~LyXLayoutList()
660 {
661         //don't do anything. the layouts will be extracted by ToAr.
662         //destruction is done by Clean in emergencies
663 }
664
665
666 int LyXLayoutList::GetNum ()
667 {
668         return num_layouts;
669 }
670
671
672 void LyXLayoutList::Add (LyXLayout *lay)
673 {
674         LyXLayoutL * tmp = new LyXLayoutL;
675         tmp->layout = lay;
676         tmp->next = 0;
677         if (!eol) l = tmp; 
678         else eol->next = tmp;
679         eol = tmp;
680         num_layouts++;
681 }
682
683
684 bool LyXLayoutList::Delete (string const &name)
685 {
686         LyXLayoutL * layoutl = l;
687         while(layoutl) {
688                 if (layoutl->layout && layoutl->layout->name == name) {
689                         delete layoutl->layout;
690                         layoutl->layout = 0; // not sure it is necessary
691                         num_layouts--;
692                         return true;
693                 }
694                 layoutl = layoutl->next;
695         }
696         return false;
697 }
698
699
700 LyXLayout * LyXLayoutList::GetLayout (string const &name)
701 {
702         LyXLayoutL * layoutl = l;
703         while(layoutl) {
704                 if (layoutl->layout && layoutl->layout->name == name) 
705                         return layoutl->layout;
706                 layoutl = layoutl->next;
707         }
708         return 0;
709 }
710
711
712 LyXLayout * LyXLayoutList::ToAr ()
713 {
714         LyXLayoutL * lp, * op;
715         int idx = 0;
716         LyXLayout* ar = new LyXLayout [num_layouts];
717         lp = l;
718         while (lp) {
719                 if (lp->layout) {
720                         ar[idx].Copy (*lp->layout);
721                         idx++;
722                         delete lp->layout;
723                 }
724                 op = lp;
725                 lp = lp->next;
726                 delete op;
727         }
728         return ar;
729 }
730
731
732 //wipe up any dead layouts
733 void LyXLayoutList::Clean ()
734 {
735         LyXLayoutL * lp, * op;
736         lp = l;
737         while (lp) {
738                 delete lp->layout;
739                 op = lp;
740                 lp = lp->next;
741                 delete op;
742         }
743 }
744
745 /* ******************************************************************* */
746
747 LyXTextClass::LyXTextClass(string const &fn, string const &cln,
748                            string const &desc)
749 {
750         name = fn;
751         latexname = cln;
752         description = desc;
753         output_type = LATEX;
754         style = 0;
755         columns = 1;
756         sides = 1;
757         secnumdepth = 3;
758         tocdepth = 3;
759         pagestyle = "default";
760         maxcounter = LABEL_COUNTER_CHAPTER;
761         defaultfont = LyXFont(LyXFont::ALL_SANE);
762         number_of_defined_layouts = 0;
763         opt_fontsize = "10|11|12";
764         opt_pagestyle = "empty|plain|headings|fancy";
765         provides_amsmath = false;
766         provides_makeidx = false;
767         provides_url = false;
768         loaded = false;
769 }
770
771
772 // This is not a proper copy.
773 // It just references the style rather than copying it!
774 void LyXTextClass::Copy (LyXTextClass const &l)
775 {
776         name = l.name;
777         latexname = l.latexname;
778         description = l.description;
779         output_type = l.output_type;
780         preamble = l.preamble;
781         options = l.options;
782         if (style) delete style;
783         style = l.style; //just aliases NO COPY
784         number_of_defined_layouts = l.number_of_defined_layouts;
785         columns = l.columns;
786         sides = l.sides;
787         secnumdepth = l.secnumdepth;
788         tocdepth = l.tocdepth;
789         pagestyle = l.pagestyle;
790         maxcounter = l.maxcounter;
791         defaultfont = l.defaultfont;
792         opt_fontsize = l.opt_fontsize;
793         opt_pagestyle = l.opt_pagestyle;
794         provides_amsmath = l.provides_amsmath;
795         provides_makeidx = l.provides_makeidx;
796         provides_url = l.provides_url;
797         loaded = l.loaded;
798
799         leftmargin = l.leftmargin;
800         rightmargin = l.rightmargin;
801           
802 }
803
804
805 LyXTextClass::~LyXTextClass()
806 {
807         //we can't delete the style here because otherwise 
808         //our list classes wouldn't work
809 }
810
811
812 /* Reads a textclass structure from file */
813 int LyXTextClass::Read (string const &filename, LyXLayoutList *list)
814 {
815         if (!list)
816                 lyxerr[Debug::TCLASS] << "Reading textclass "
817                                       << MakeDisplayPath(filename)
818                                       << endl;
819         else 
820                 lyxerr[Debug::TCLASS] << "Reading input file "
821                                       << MakeDisplayPath(filename) << endl;
822
823         LyXLex lexrc(layoutTags, sizeof(layoutTags)/sizeof(keyword_item));
824         bool error = false;
825
826         lexrc.setFile(filename);
827         if (!lexrc.IsOK()) return -2; 
828
829         LyXLayoutList * l;
830         LyXLayout * tmpl;
831
832         if (list) 
833                 l = list;
834         else 
835                 l = new LyXLayoutList;
836
837         /* parsing */
838         while (lexrc.IsOK() && !error) {
839                 switch(lexrc.lex()) {
840                 case -2:
841                         break;
842
843                 case -1:                                 
844                         lexrc.printError("Unknown tag `$$Token'");
845                         error = true;
846                         break;
847
848                 case LT_OUTPUTTYPE:   // output type definition
849                         switch(lexrc.lex()) {
850                         case LT_OTLATEX:
851                                 output_type=LATEX;
852                                 break;
853                         case LT_OTLINUXDOC:
854                                 output_type=LINUXDOC;
855                                 break;
856                         case LT_OTDOCBOOK:
857                                 output_type=DOCBOOK;
858                                 break;
859                         case LT_OTLITERATE:
860                                 output_type=LITERATE;
861                                 break;
862                         default:
863                                 lexrc.printError("Unknown output type `$$Token'");
864                                 break;
865                         }
866                         break;
867                         
868                 case LT_INPUT: // Include file
869                         if (lexrc.next()) {
870                                 string tmp = LibFileSearch("layouts",
871                                                             lexrc.GetString(), 
872                                                             "layout");
873                                 
874                                 if (Read(tmp, l)) {
875                                         lexrc.printError("Error reading input"
876                                                          "file: "+tmp);
877                                         error = true;
878                                 }
879                         }
880                         break;
881
882                 case LT_STYLE:
883                         if (lexrc.next()) {
884                                 bool is_new = false;
885
886                                 string name = subst(lexrc.GetString(), '_', ' ');
887                                 tmpl = l->GetLayout(name);
888                                 if (!tmpl) {
889                                         is_new = true;
890                                         tmpl = new LyXLayout;
891                                         tmpl->name = name;
892                                 }
893
894                                 lyxerr[Debug::TCLASS] << "  Reading style "
895                                                       << tmpl->name
896                                                       << endl;
897
898                                 if (!tmpl->Read(lexrc, l)) {
899                                         // Resolve fonts
900                                         tmpl->resfont = tmpl->font;
901                                         tmpl->resfont.realize(defaultfont);
902                                         tmpl->reslabelfont = tmpl->labelfont;
903                                         tmpl->reslabelfont.realize(defaultfont);
904                                         if (is_new) {
905                                                 l->Add (tmpl);
906                                                 // NB! we don't delete because 
907                                                 // we just pass it in.... 
908                                         }
909                                 } else {
910                                         lexrc.printError(
911                                                        "Error parsing style `"
912                                                        +tmpl->name+'\'');
913                                         error = true;
914                                         if (is_new) {
915                                                 delete tmpl;  
916                                                 //we delete dead ones here
917                                         }
918                                 }
919                         }
920                         else {
921                                 lexrc.printError("No name given for style: `$$Token'.");
922                                 error = true;
923                         }
924                         break;
925
926                 case LT_NOSTYLE:
927                         if (lexrc.next()) {
928                                 string style = lexrc.GetString();
929                                 if (!l->Delete(subst(style, '_', ' ')))
930                                         lexrc.printError("Cannot delete style `$$Token'");
931                         }
932                         break;
933
934                 case LT_COLUMNS:
935                         if (lexrc.next())
936                                 columns = lexrc.GetInteger();
937                         break;
938                         
939                 case LT_SIDES:
940                         if (lexrc.next())
941                                 sides = lexrc.GetInteger();
942                         break;
943                         
944                 case LT_PAGESTYLE:
945                         lexrc.next();
946                         pagestyle = strip(lexrc.GetString());
947                         break;
948                         
949                 case LT_DEFAULTFONT:
950                         defaultfont.lyxRead(lexrc);
951                         if (!defaultfont.resolved()) {
952                                 lexrc.printError("Warning: defaultfont should "
953                                                  "be fully instantiated!");
954                                 defaultfont.realize(LyXFont::ALL_SANE);
955                         }
956                         break;
957
958                 case LT_MAXCOUNTER:
959                         switch (lexrc.lex()) {
960                         case LT_COUNTER_CHAPTER:
961                                 maxcounter = LABEL_COUNTER_CHAPTER;
962                                 break;
963                         case LT_COUNTER_SECTION:
964                                 maxcounter = LABEL_COUNTER_SECTION;
965                                 break;
966                         case LT_COUNTER_SUBSECTION:
967                                 maxcounter = LABEL_COUNTER_SUBSECTION;
968                                 break;
969                         case LT_COUNTER_SUBSUBSECTION:
970                                 maxcounter = LABEL_COUNTER_SUBSUBSECTION;
971                                 break;
972                         case LT_COUNTER_PARAGRAPH:
973                                 maxcounter = LABEL_COUNTER_PARAGRAPH;
974                                 break;
975                         case LT_COUNTER_SUBPARAGRAPH:
976                                 maxcounter = LABEL_COUNTER_SUBPARAGRAPH;
977                                 break;
978                         case LT_COUNTER_ENUMI:
979                                 maxcounter = LABEL_COUNTER_ENUMI;
980                                 break;
981                         case LT_COUNTER_ENUMII:
982                                 maxcounter = LABEL_COUNTER_ENUMII;
983                                 break;
984                         case LT_COUNTER_ENUMIII:
985                                 maxcounter = LABEL_COUNTER_ENUMIII;
986                                 break;
987                         case LT_COUNTER_ENUMIV:
988                                 maxcounter = LABEL_COUNTER_ENUMIV;
989                                 break;
990                         }
991                         break;
992
993                 case LT_SECNUMDEPTH:
994                         lexrc.next();
995                         secnumdepth = lexrc.GetInteger();
996                         break;
997
998                 case LT_TOCDEPTH:
999                         lexrc.next();
1000                         tocdepth = lexrc.GetInteger();
1001                         break;
1002
1003          // First step to support options 
1004                 case LT_CLASSOPTIONS:
1005                 {
1006                         bool getout = true;
1007                         while (getout && lexrc.IsOK()) { 
1008                                 switch (lexrc.lex()) {
1009                                 case LT_FONTSIZE:
1010                                         lexrc.next();
1011                                         opt_fontsize = strip(lexrc.GetString());
1012                                         break;
1013                                 case LT_PAGESTYLE:
1014                                         lexrc.next();
1015                                         opt_pagestyle = strip(lexrc.GetString()); 
1016                                         break;
1017                                 case LT_OTHER:
1018                                         lexrc.next();
1019                                         options = lexrc.GetString();
1020                                         break;
1021                                 case LT_END: getout = false; break;
1022                                 default:
1023                                         lexrc.printError("Out of context tag `$$Token'");
1024                                         break;
1025                                 }
1026                         }
1027                         break;
1028                 }
1029
1030                 case LT_PREAMBLE:
1031                         preamble = lexrc.getLongString("EndPreamble");
1032                         break;
1033
1034                 case LT_PROVIDESAMSMATH:
1035                         if (lexrc.next())
1036                                 provides_amsmath = lexrc.GetInteger();
1037                         break;
1038
1039                 case LT_PROVIDESMAKEIDX:
1040                         if (lexrc.next())
1041                                 provides_makeidx = lexrc.GetInteger();
1042                         break;
1043
1044                 case LT_PROVIDESURL:
1045                         if (lexrc.next())
1046                                 provides_url = lexrc.GetInteger();
1047                         break;
1048
1049                 case LT_LEFTMARGIN:     /* left margin type */
1050                         if (lexrc.next())
1051                                 leftmargin = lexrc.GetString();
1052                         break;                  
1053
1054                 case LT_RIGHTMARGIN:    /* right margin type */
1055                         if (lexrc.next())
1056                                 rightmargin = lexrc.GetString();
1057                         break;
1058
1059                 default:
1060                         lexrc.printError("Out of context tag `$$Token'");
1061                         break;
1062                 }
1063         }       
1064
1065         if (!list) { // we are at top level here.
1066                 if (error) {
1067                         number_of_defined_layouts = 0;
1068                         l->Clean(); //wipe any we may have found
1069                         delete l;
1070                 }
1071                 else {
1072                         style = l->ToAr();
1073                         number_of_defined_layouts = l->GetNum();
1074                         delete l;
1075                 }
1076                 lyxerr[Debug::TCLASS] << "Finished reading textclass " 
1077                                       << MakeDisplayPath(filename)
1078                                       << endl;
1079         }
1080         else
1081                 lyxerr[Debug::TCLASS] << "Finished reading input file " 
1082                                       << MakeDisplayPath(filename)
1083                                       << endl;
1084
1085         return error;
1086 }
1087
1088
1089 // Load textclass info if not loaded yet
1090 void LyXTextClass::load()
1091 {
1092         if (loaded)
1093                 return;
1094
1095         // Read style-file
1096         string real_file = LibFileSearch("layouts", name, "layout");
1097
1098         if (Read(real_file)) {
1099                 lyxerr << "Error reading `"
1100                        << MakeDisplayPath(real_file)
1101                        << "'\n(Check `" << name
1102                        << "')\nCheck your installation and "
1103                         "try Options/Reconfigure..." << endl;
1104         }
1105         loaded = true;
1106 }
1107
1108 /* ******************************************************************* */
1109
1110 LyXTextClassList::LyXTextClassList()
1111 {
1112         l = 0;
1113         ar = 0;
1114         num_textclass = 0;
1115 }
1116
1117
1118 LyXTextClassList::~LyXTextClassList()
1119 {
1120         // The textclass list is in ar.
1121         if (ar) {
1122                 delete [] ar;
1123         }
1124 }
1125
1126
1127 // Gets textclass number from name
1128 signed char LyXTextClassList::NumberOfClass(string const &textclass) 
1129 {
1130         int i = 0;
1131    
1132         while (i < num_textclass && textclass != ar[i].name)
1133                 i++;
1134    
1135         if (i >= num_textclass)
1136                 i = -1;
1137
1138         return i;
1139 }
1140
1141
1142 // Gets layout structure from style number and textclass number
1143 LyXLayout *LyXTextClassList::Style(char textclass, char layout) 
1144 {
1145         ar[textclass].load();
1146
1147         if (layout < ar[textclass].number_of_defined_layouts)
1148                 return &ar[textclass].style[layout];
1149         else {
1150                 return &ar[textclass].style[0];
1151         };
1152 }
1153
1154
1155 // Gets layout number from name and textclass number
1156 char LyXTextClassList::NumberOfLayout(char textclass, string const &name) 
1157 {
1158         ar[textclass].load();
1159
1160         int i = 0;
1161         while (i < ar[textclass].number_of_defined_layouts 
1162                && name != ar[textclass].style[i].name)
1163                 i++;
1164
1165         if (i >= ar[textclass].number_of_defined_layouts) {
1166                 if (name == "dummy")
1167                         i = LYX_DUMMY_LAYOUT;
1168                 else
1169                         // so that we can detect if the layout doesn't exist.
1170                         i = -1; // not found
1171         } 
1172         return i;
1173 }
1174
1175
1176 // Gets a layout (style) name from layout number and textclass number
1177 string LyXTextClassList::NameOfLayout(char textclass, char layout) 
1178 {
1179         ar[textclass].load();
1180
1181         if (layout < ar[textclass].number_of_defined_layouts)
1182                 return ar[textclass].style[layout].name;
1183         else if (layout == LYX_DUMMY_LAYOUT)
1184                 return "dummy";
1185         else
1186                 return "@@end@@";
1187 }
1188
1189
1190 // Gets a textclass name from number
1191 string LyXTextClassList::NameOfClass(char number) 
1192 {
1193         if (num_textclass == 0) { 
1194                 if (number == 0) return "dummy";
1195                 else return "@@end@@";
1196         }
1197         if (number < num_textclass)
1198                 return ar[number].name;
1199         else
1200                 return "@@end@@";
1201 }
1202
1203 // Gets a textclass latexname from number
1204 string LyXTextClassList::LatexnameOfClass(char number) 
1205 {
1206         ar[number].load();
1207
1208         if (num_textclass == 0) { 
1209                 if (number == 0) return "dummy";
1210                 else return "@@end@@";
1211         }
1212         if (number < num_textclass)
1213                 return ar[number].latexname;
1214         else
1215                 return "@@end@@";
1216 }
1217
1218 // Gets a textclass description from number
1219 string LyXTextClassList::DescOfClass(char number) 
1220 {
1221         if (num_textclass == 0) { 
1222                 if (number == 0) return "dummy";
1223                 else return "@@end@@";
1224         }
1225         if (number < num_textclass)
1226                 return ar[number].description;
1227         else
1228                 return "@@end@@";
1229 }
1230
1231
1232 // Gets a textclass structure from number
1233 LyXTextClass * LyXTextClassList::TextClass(char textclass) 
1234 {
1235         ar[textclass].load();
1236         if (textclass < num_textclass)
1237                 return &ar[textclass];
1238         else
1239                 return &ar[0];
1240 }
1241
1242
1243 void LyXTextClassList::Add (LyXTextClass *t)
1244 {
1245         LyXTextClassL ** h = &l;
1246         string const desc = t->description;
1247         while (*h && compare_no_case((*h)->textclass->description, desc) < 0)
1248                 h = &((*h)->next);
1249         LyXTextClassL * tmp = new LyXTextClassL;
1250         tmp->textclass = t;
1251         tmp->next = *h;
1252         *h = tmp;
1253         num_textclass++;
1254 }
1255
1256
1257 void LyXTextClassList::ToAr ()
1258 {
1259         LyXTextClassL * lp, *op;
1260         int idx = 0;
1261         ar = new LyXTextClass [num_textclass];
1262         lp = l;
1263         while (lp) {
1264                 ar[idx].Copy (*lp->textclass);
1265                 idx++;
1266                 delete lp->textclass; // note we don't delete layouts
1267                                       // here at all 
1268                 op = lp;
1269                 lp = lp->next;
1270                 delete op;
1271         }
1272 }
1273
1274
1275 // Reads LyX textclass definitions according to textclass config file
1276 bool LyXTextClassList::Read ()
1277 {
1278         LyXLex lex(0, 0);
1279         string real_file = LibFileSearch("", "textclass.lst");
1280         lyxerr[Debug::TCLASS] << "Reading textclasses from "
1281                               << real_file << endl;
1282
1283         if (real_file.empty()) {
1284                 lyxerr << "LyXTextClassList::Read: unable to find "
1285                         "textclass file  `" << MakeDisplayPath(real_file, 1000)
1286                        << "'. Exiting." << endl;
1287
1288                 WriteAlert(_("LyX wasn't able to find its layout descriptions!"),
1289                            _("Check that the file \"textclass.lst\""),
1290                            _("is installed correctly. Sorry, has to exit :-("));
1291                 return false;
1292                 // This causes LyX to end... Not a desirable behaviour. Lgb
1293                 // What do you propose? That the user gets a file dialog
1294                 // and is allowed to hunt for the file? (Asger)
1295         }
1296
1297         lex.setFile(real_file);
1298         
1299         if (!lex.IsOK()) {
1300                 lyxerr << "LyXTextClassList::Read: unable to open "
1301                         "textclass file  `" << MakeDisplayPath(real_file, 1000)
1302                        << "\'\nCheck your installation. LyX can't continue."
1303                        << endl;
1304                 return false;
1305         }
1306         bool finished = false;
1307         string fname, clname, desc;
1308         LyXTextClass * tmpl;
1309
1310         // Parse config-file
1311         while (lex.IsOK() && !finished) {
1312                 switch (lex.lex()) {
1313                 case LyXLex::LEX_FEOF:
1314                         finished = true;
1315                         break;
1316                 default:
1317                         fname = lex.GetString();
1318                         lyxerr[Debug::TCLASS] << "Fname: " << fname << endl;
1319                         if (lex.next()) {
1320                                 clname = lex.GetString();
1321                                 lyxerr[Debug::TCLASS]
1322                                         << "Clname: " << clname << endl;
1323                                 if (lex.next()) {
1324                                               desc = lex.GetString();
1325                                               lyxerr[Debug::TCLASS]
1326                                                       << "Desc: " << desc << endl;
1327                                               // This code is run when we have
1328                                               // fname, clname and desc
1329                                               tmpl =new LyXTextClass(fname,
1330                                                                      clname,
1331                                                                      desc);
1332                                               Add (tmpl);
1333                                               if (lyxerr.
1334                                                   debugging(Debug::TCLASS)) {
1335                                                     tmpl->load();
1336                                               }
1337                                 }
1338                         }
1339                 }
1340         }
1341         
1342         if (num_textclass == 0) {
1343                 lyxerr << "LyXTextClassList::Read: no textclass found!" << endl;
1344                 WriteAlert(_("LyX wasn't able to find any layout description!"),
1345                            _("Check the contents of  the file \"textclass.lst\""),
1346                            _("Sorry, has to exit :-("));
1347                 return false;
1348         }
1349         else { 
1350                 ToAr();
1351                 return true;
1352         }
1353 }
1354
1355 // Load textclass
1356 /* Returns false if this fails */
1357 bool LyXTextClassList::Load (char const number)
1358 {
1359         bool result = 1;
1360         
1361         if (number < num_textclass) {
1362                 ar[number].load();
1363                 if (!ar[number].number_of_defined_layouts) {
1364                         result = 0;
1365                 }
1366         } else {
1367                 result = 0;
1368         }
1369         return result;
1370 }