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