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