]> git.lyx.org Git - lyx.git/blob - src/TextClassList.cpp
After a hiatus, I'm returning to the rewrite of InsetCommandParams, the purpose of...
[lyx.git] / src / TextClassList.cpp
1 /**
2  * \file TextClassList.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Lars Gullik Bjønnes
7  * \author John Levon
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "TextClassList.h"
15 #include "TextClass.h"
16 #include "Lexer.h"
17
18 #include "support/debug.h"
19 #include "support/FileName.h"
20 #include "support/filetools.h"
21
22 #include <boost/bind.hpp>
23 #include <boost/regex.hpp>
24
25 #include <fstream>
26
27 using namespace std;
28 using namespace lyx::support;
29
30 namespace lyx {
31
32 using boost::bind;
33 using boost::regex;
34 using boost::smatch;
35
36 // Gets textclass number from name
37 pair<bool, TextClassIndex> const
38 TextClassList::numberOfClass(string const & textclass) const
39 {
40         ClassList::const_iterator cit =
41                 find_if(classlist_.begin(), classlist_.end(),
42                         bind(equal_to<string>(),
43                              bind(&TextClass::name, _1),
44                              textclass));
45
46         return cit != classlist_.end() ?
47                 make_pair(true, TextClassIndex(cit - classlist_.begin())) :
48                 make_pair(false, TextClassIndex(0));
49 }
50
51
52 // Gets a textclass structure from number
53 TextClass const &
54 TextClassList::operator[](TextClassIndex textclass) const
55 {
56         if (textclass >= classlist_.size())
57                 return classlist_[0];
58         
59         //FIXME I don't believe the following line is actually necessary (rgh)
60         classlist_[textclass].load();
61         return classlist_[textclass];
62 }
63
64 TextClass & TextClassList::at(TextClassIndex textclass)
65 {
66         if (textclass >= classlist_.size())
67                 return classlist_[0];
68         
69         //FIXME I don't believe the following line is actually necessary (rgh)
70         classlist_[textclass].load();
71         return classlist_[textclass];
72 }
73
74
75 // used when sorting the textclass list.
76 class less_textclass_avail_desc
77         : public binary_function<TextClass, TextClass, int>
78 {
79 public:
80         int operator()(TextClass const & tc1,
81                        TextClass const & tc2) const
82         {
83                 // Ordering criteria:
84                 //   1. Availability of text class
85                 //   2. Description (lexicographic)
86
87                 return (tc1.isTeXClassAvailable() && !tc2.isTeXClassAvailable()) ||
88                         (tc1.isTeXClassAvailable() == tc2.isTeXClassAvailable() &&
89                          tc1.description() < tc2.description());
90         }
91 };
92
93
94 // Reads LyX textclass definitions according to textclass config file
95 bool TextClassList::read()
96 {
97         Lexer lex(0, 0);
98         FileName const real_file = libFileSearch("", "textclass.lst");
99         LYXERR(Debug::TCLASS, "Reading textclasses from `" << real_file << '\'');
100
101         if (real_file.empty()) {
102                 lyxerr << "TextClassList::Read: unable to find "
103                           "textclass file  `"
104                        << to_utf8(makeDisplayPath(real_file.absFilename(), 1000))
105                        << "'. Exiting." << endl;
106                 return false;
107                 // This causes LyX to end... Not a desirable behaviour. Lgb
108                 // What do you propose? That the user gets a file dialog
109                 // and is allowed to hunt for the file? (Asger)
110                 // more that we have a layout for minimal.cls statically
111                 // compiled in... (Lgb)
112         }
113
114         if (!lex.setFile(real_file)) {
115                 lyxerr << "TextClassList::Read: "
116                         "lyxlex was not able to set file: "
117                        << real_file << endl;
118         }
119
120         if (!lex.isOK()) {
121                 lyxerr << "TextClassList::Read: unable to open "
122                           "textclass file  `"
123                        << to_utf8(makeDisplayPath(real_file.absFilename(), 1000))
124                        << "'\nCheck your installation. LyX can't continue."
125                        << endl;
126                 return false;
127         }
128
129         bool finished = false;
130         // Parse config-file
131         LYXERR(Debug::TCLASS, "Starting parsing of textclass.lst");
132         while (lex.isOK() && !finished) {
133                 LYXERR(Debug::TCLASS, "\tline by line");
134                 switch (lex.lex()) {
135                 case Lexer::LEX_FEOF:
136                         finished = true;
137                         break;
138                 default:
139                         string const fname = lex.getString();
140                         LYXERR(Debug::TCLASS, "Fname: " << fname);
141                         if (lex.next()) {
142                                 string const clname = lex.getString();
143                                 LYXERR(Debug::TCLASS, "Clname: " << clname);
144                                 if (lex.next()) {
145                                         string const desc = lex.getString();
146                                         LYXERR(Debug::TCLASS, "Desc: " << desc);
147                                         if (lex.next()) {
148                                                 bool avail = lex.getBool();
149                                                 LYXERR(Debug::TCLASS, "Avail: " << avail);
150                                                 // This code is run when we have
151                                                 // fname, clname, desc, and avail
152                                                 TextClass tmpl(fname, clname, desc, avail);
153                                                 if (lyxerr.debugging(Debug::TCLASS)) {
154                                                         tmpl.load();
155                                                 }
156                                                 classlist_.push_back(tmpl);
157                                         }
158                                 }
159                         }
160                 }
161         }
162         LYXERR(Debug::TCLASS, "End of parsing of textclass.lst");
163
164         // lyx will start with an empty classlist_, but only reconfigure is allowed
165         // in this case. This gives users a second chance to configure lyx if
166         // initial configuration fails. (c.f. bug 2829)
167         if (classlist_.empty())
168                 lyxerr << "TextClassList::Read: no textclasses found!"
169                        << endl;
170         else 
171                 // Ok everything loaded ok, now sort the list.
172                 sort(classlist_.begin(), classlist_.end(), less_textclass_avail_desc());
173         return true;
174 }
175
176
177 void TextClassList::reset(TextClassIndex const & textclass)
178 {
179         if (textclass >= classlist_.size())
180                 return;
181         TextClass const & tc = classlist_[textclass];
182         TextClass tmpl(tc.name(), tc.latexname(), tc.description(), 
183                        tc.isTeXClassAvailable());
184         classlist_[textclass] = tmpl;
185 }
186
187
188 pair<bool, TextClassIndex> const
189 TextClassList::addTextClass(string const & textclass, string const & path)
190 {
191         // only check for textclass.layout file, .cls can be anywhere in $TEXINPUTS
192         // NOTE: latex class name is defined in textclass.layout, which can be different from textclass
193         FileName const layout_file(addName(path, textclass + ".layout"));
194         if (layout_file.exists()) {
195                 LYXERR(Debug::TCLASS, "Adding class " << textclass << " from directory " << path);
196                 // Read .layout file and get description, real latex classname etc
197                 //
198                 // This is a C++ version of function processLayoutFile in configure.py,
199                 // which uses the following regex
200                 //     \Declare(LaTeX|DocBook)Class\s*(\[([^,]*)(,.*)*\])*\s*{(.*)}
201                 ifstream ifs(layout_file.toFilesystemEncoding().c_str());
202                 static regex const reg("^#\\s*\\\\Declare(LaTeX|DocBook)Class\\s*"
203                         "(?:\\[([^,]*)(?:,.*)*\\])*\\s*\\{(.*)\\}\\s*");
204                 string line;
205                 while (getline(ifs, line)) {
206                         // look for the \DeclareXXXClass line
207                         smatch sub;
208                         if (regex_match(line, sub, reg)) {
209                                 // returns: whole string, classtype (not used here), first option, description
210                                 BOOST_ASSERT(sub.size()==4);
211                                 // now, create a TextClass with description containing path information
212                                 TextClass tmpl(textclass, sub.str(2)==""?textclass:sub.str(2),
213                                         sub.str(3) + " <" + path + ">", true);
214                                 if (lyxerr.debugging(Debug::TCLASS))
215                                         tmpl.load(path);
216                                 // Do not add this local TextClass to classlist_ if it has
217                                 // already been loaded by, for example, a master buffer.
218                                 pair<bool, lyx::TextClassIndex> pp =
219                                         textclasslist.numberOfClass(textclass);
220                                 // only layouts from the same directory are considered to be identical.
221                                 if (pp.first && classlist_[pp.second].description() == tmpl.description())
222                                         return pp;
223                                 classlist_.push_back(tmpl);
224                                 return make_pair(true, classlist_.size() - 1);
225                         }
226                 }
227         }
228         // If .layout is not in local directory, or an invalid layout is found, return false
229         return make_pair(false, TextClassIndex(0));
230 }
231
232
233 // Global variable: textclass table.
234 TextClassList textclasslist;
235
236
237 TextClassIndex defaultTextclass()
238 {
239         // We want to return the article class. if `first' is
240         // true in the returned pair, then `second' is the textclass
241         // number; if it is false, second is 0. In both cases, second
242         // is what we want.
243         return textclasslist.numberOfClass("article").second;
244 }
245
246
247
248 // Reads the style files
249 bool LyXSetStyle()
250 {
251         LYXERR(Debug::TCLASS, "LyXSetStyle: parsing configuration...");
252
253         if (!textclasslist.read()) {
254                 LYXERR(Debug::TCLASS, "LyXSetStyle: an error occured "
255                         "during parsing.\n             Exiting.");
256                 return false;
257         }
258
259         LYXERR(Debug::TCLASS, "LyXSetStyle: configuration parsed.");
260         return true;
261 }
262
263
264 } // namespace lyx