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