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