]> git.lyx.org Git - lyx.git/blob - src/lyxtextclasslist.C
fix keys to be unique
[lyx.git] / src / lyxtextclasslist.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 "lyxtextclasslist.h"
19 #include "lyxtextclass.h"
20 #include "debug.h"
21 #include "lyxlex.h"
22 #include "gettext.h"
23 #include "frontends/Alert.h"
24
25 #include "support/lyxfunctional.h"
26 #include "support/LAssert.h"
27 #include "support/filetools.h"
28
29 #include <utility>
30
31 #ifndef CXX_GLOBAL_CSTD
32 using std::exit;
33 #endif
34
35 using lyx::textclass_type;
36 using std::pair;
37 using std::make_pair;
38 using std::endl;
39 using std::find_if;
40 using std::sort;
41
42
43 // Gets textclass number from name
44 pair<bool, textclass_type> const
45 LyXTextClassList::NumberOfClass(string const & textclass) const
46 {
47         ClassList::const_iterator cit =
48                 find_if(classlist_.begin(), classlist_.end(),
49                         lyx::compare_memfun(&LyXTextClass::name, textclass));
50         return cit != classlist_.end() ?
51                 make_pair(true, textclass_type(cit - classlist_.begin())) :
52                 make_pair(false, textclass_type(0));
53 }
54
55
56 // Gets a textclass structure from number
57 LyXTextClass const &
58 LyXTextClassList::operator[](textclass_type textclass) const
59 {
60         classlist_[textclass].load();
61         if (textclass < classlist_.size())
62                 return classlist_[textclass];
63         else
64                 return classlist_[0];
65 }
66
67
68 // used when sorting the textclass list.
69 class less_textclass_desc {
70 public:
71         int operator()(LyXTextClass const & tc1, LyXTextClass const & tc2) {
72                 return tc1.description() < tc2.description();
73         }
74 };
75
76
77 // Reads LyX textclass definitions according to textclass config file
78 bool LyXTextClassList::Read ()
79 {
80         LyXLex lex(0, 0);
81         string real_file = LibFileSearch("", "textclass.lst");
82         lyxerr[Debug::TCLASS] << "Reading textclasses from `"
83                               << real_file << "'" << endl;
84
85         if (real_file.empty()) {
86                 lyxerr << "LyXTextClassList::Read: unable to find "
87                         "textclass file  `" << MakeDisplayPath(real_file, 1000)
88                        << "'. Exiting." << endl;
89
90                 Alert::alert(_("LyX wasn't able to find its layout descriptions!"),
91                            _("Check that the file \"textclass.lst\""),
92                            _("is installed correctly. Sorry, has to exit :-("));
93                 return false;
94                 // This causes LyX to end... Not a desirable behaviour. Lgb
95                 // What do you propose? That the user gets a file dialog
96                 // and is allowed to hunt for the file? (Asger)
97                 // more that we have a layout for minimal.cls statically
98                 // compiled in... (Lgb)
99         }
100
101         if (!lex.setFile(real_file)) {
102                 lyxerr << "LyXTextClassList::Read: "
103                         "lyxlex was not able to set file: "
104                        << real_file << endl;
105         }
106
107         if (!lex.isOK()) {
108                 lyxerr << "LyXTextClassList::Read: unable to open "
109                         "textclass file  `" << MakeDisplayPath(real_file, 1000)
110                        << "'\nCheck your installation. LyX can't continue."
111                        << endl;
112                 return false;
113         }
114
115         bool finished = false;
116         // Parse config-file
117         lyxerr[Debug::TCLASS] << "Starting parsing of textclass.lst" << endl;
118         while (lex.isOK() && !finished) {
119                 lyxerr[Debug::TCLASS] << "\tline by line" << endl;
120                 switch (lex.lex()) {
121                 case LyXLex::LEX_FEOF:
122                         finished = true;
123                         break;
124                 default:
125                         string const fname = lex.getString();
126                         lyxerr[Debug::TCLASS] << "Fname: " << fname << endl;
127                         if (lex.next()) {
128                                 string const clname = lex.getString();
129                                 lyxerr[Debug::TCLASS] << "Clname: " << clname << endl;
130                                 if (lex.next()) {
131                                         string const desc = lex.getString();
132                                         lyxerr[Debug::TCLASS] << "Desc: " << desc << endl;
133                                         // This code is run when we have
134                                         // fname, clname and desc
135                                         LyXTextClass tmpl(fname, clname, desc);
136                                         if (lyxerr.debugging(Debug::TCLASS)) {
137                                                 tmpl.load();
138                                         }
139                                         classlist_.push_back(tmpl);
140                                 }
141                         }
142                 }
143         }
144         lyxerr[Debug::TCLASS] << "End of parsing of textclass.lst" << endl;
145
146         if (classlist_.empty()) {
147                 lyxerr << "LyXTextClassList::Read: no textclasses found!"
148                        << endl;
149                 Alert::alert(_("LyX wasn't able to find any layout description!"),
150                            _("Check the contents of the file \"textclass.lst\""),
151                            _("Sorry, has to exit :-("));
152                 return false;
153         }
154         // Ok everything loaded ok, now sort the list.
155         sort(classlist_.begin(), classlist_.end(), less_textclass_desc());
156         return true;
157 }
158
159
160 // Global variable: textclass table.
161 LyXTextClassList textclasslist;
162
163 // Reads the style files
164 void LyXSetStyle()
165 {
166         lyxerr[Debug::TCLASS] << "LyXSetStyle: parsing configuration...\n";
167
168         if (!textclasslist.Read()) {
169                 lyxerr[Debug::TCLASS] << "LyXSetStyle: an error occured "
170                         "during parsing.\n             Exiting." << endl;
171                 exit(1);
172         }
173
174         lyxerr[Debug::TCLASS] << "LyXSetStyle: configuration parsed." << endl;
175 }