]> git.lyx.org Git - lyx.git/blob - src/lyxtextclasslist.C
fix a compiler warning regarding unused variable
[lyx.git] / src / lyxtextclasslist.C
1 /**
2  * \file lyxtextclasslist.C
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 "lyxtextclasslist.h"
15 #include "lyxtextclass.h"
16 #include "debug.h"
17 #include "lyxlex.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::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 LyXTextClassList::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(&LyXTextClass::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 LyXTextClass const &
66 LyXTextClassList::operator[](textclass_type textclass) const
67 {
68         classlist_[textclass].load();
69         if (textclass < classlist_.size())
70                 return classlist_[textclass];
71         else
72                 return classlist_[0];
73 }
74
75
76 // used when sorting the textclass list.
77 class less_textclass_avail_desc
78         : public std::binary_function<LyXTextClass, LyXTextClass, int>
79 {
80 public:
81         int operator()(LyXTextClass const & tc1,
82                        LyXTextClass const & tc2) const
83         {
84                 // Ordering criteria:
85                 //   1. Availability of text class
86                 //   2. Description (lexicographic)
87
88                 return (tc1.isTeXClassAvailable() && !tc2.isTeXClassAvailable()) ||
89                         (tc1.isTeXClassAvailable() == tc2.isTeXClassAvailable() &&
90                          tc1.description() < tc2.description());
91         }
92 };
93
94
95 // Reads LyX textclass definitions according to textclass config file
96 bool LyXTextClassList::read()
97 {
98         LyXLex lex(0, 0);
99         support::FileName const real_file = libFileSearch("", "textclass.lst");
100         lyxerr[Debug::TCLASS] << "Reading textclasses from `"
101                               << real_file << '\'' << endl;
102
103         if (real_file.empty()) {
104                 lyxerr << "LyXTextClassList::Read: unable to find "
105                           "textclass file  `"
106                        << to_utf8(makeDisplayPath(real_file.absFilename(), 1000))
107                        << "'. Exiting." << endl;
108                 return false;
109                 // This causes LyX to end... Not a desirable behaviour. Lgb
110                 // What do you propose? That the user gets a file dialog
111                 // and is allowed to hunt for the file? (Asger)
112                 // more that we have a layout for minimal.cls statically
113                 // compiled in... (Lgb)
114         }
115
116         if (!lex.setFile(real_file)) {
117                 lyxerr << "LyXTextClassList::Read: "
118                         "lyxlex was not able to set file: "
119                        << real_file << endl;
120         }
121
122         if (!lex.isOK()) {
123                 lyxerr << "LyXTextClassList::Read: unable to open "
124                           "textclass file  `"
125                        << to_utf8(makeDisplayPath(real_file.absFilename(), 1000))
126                        << "'\nCheck your installation. LyX can't continue."
127                        << endl;
128                 return false;
129         }
130
131         bool finished = false;
132         // Parse config-file
133         lyxerr[Debug::TCLASS] << "Starting parsing of textclass.lst" << endl;
134         while (lex.isOK() && !finished) {
135                 lyxerr[Debug::TCLASS] << "\tline by line" << endl;
136                 switch (lex.lex()) {
137                 case LyXLex::LEX_FEOF:
138                         finished = true;
139                         break;
140                 default:
141                         string const fname = lex.getString();
142                         lyxerr[Debug::TCLASS] << "Fname: " << fname << endl;
143                         if (lex.next()) {
144                                 string const clname = lex.getString();
145                                 lyxerr[Debug::TCLASS] << "Clname: " << clname << endl;
146                                 if (lex.next()) {
147                                         string const desc = lex.getString();
148                                         lyxerr[Debug::TCLASS] << "Desc: " << desc << endl;
149                                         if (lex.next()) {
150                                                 bool avail = lex.getBool();
151                                                 lyxerr[Debug::TCLASS] << "Avail: " << avail << endl;
152                                                 // This code is run when we have
153                                                 // fname, clname, desc, and avail
154                                                 LyXTextClass tmpl(fname, clname, desc, avail);
155                                                 if (lyxerr.debugging(Debug::TCLASS)) {
156                                                         tmpl.load();
157                                                 }
158                                                 classlist_.push_back(tmpl);
159                                         }
160                                 }
161                         }
162                 }
163         }
164         lyxerr[Debug::TCLASS] << "End of parsing of textclass.lst" << endl;
165
166         if (classlist_.empty()) {
167                 lyxerr << "LyXTextClassList::Read: no textclasses found!"
168                        << endl;
169                 return false;
170         }
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 std::pair<bool, textclass_type> const
178 LyXTextClassList::addTextClass(std::string const & textclass, std::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(path + '/' + textclass + ".layout");
183         if (fs::exists(layout_file.toFilesystemEncoding())) {
184                 lyxerr[Debug::TCLASS] << "Adding class " << textclass << " from directory " << path << endl;
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, add the layout to textclass.
201                                 LyXTextClass 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                                 classlist_.push_back(tmpl);
206                                 return make_pair(true, classlist_.size() - 1);
207                         }
208                 }
209         }
210         // If .layout is not in local directory, or an invalid layout is found, return false
211         return make_pair(false, textclass_type(0));
212 }
213         
214
215 // Global variable: textclass table.
216 LyXTextClassList textclasslist;
217
218
219 // Reads the style files
220 bool LyXSetStyle()
221 {
222         lyxerr[Debug::TCLASS] << "LyXSetStyle: parsing configuration..." << endl;
223
224         if (!textclasslist.read()) {
225                 lyxerr[Debug::TCLASS] << "LyXSetStyle: an error occured "
226                         "during parsing.\n             Exiting." << endl;
227                 return false;
228         }
229
230         lyxerr[Debug::TCLASS] << "LyXSetStyle: configuration parsed." << endl;
231         return true;
232 }
233
234
235 } // namespace lyx