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