]> git.lyx.org Git - lyx.git/blob - src/lyxtextclasslist.C
Extracted from r14281
[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 namespace fs = boost::filesystem;
26
27 using lyx::textclass_type;
28
29 using lyx::support::libFileSearch;
30 using lyx::support::makeDisplayPath;
31
32 using boost::bind;
33 using boost::regex;
34 using boost::smatch;
35
36 #ifndef CXX_GLOBAL_CSTD
37 using std::exit;
38 #endif
39
40 using std::endl;
41 using std::equal_to;
42 using std::find_if;
43 using std::make_pair;
44 using std::sort;
45 using std::string;
46 using std::pair;
47 using std::ifstream;
48
49
50 // Gets textclass number from name
51 pair<bool, textclass_type> const
52 LyXTextClassList::numberOfClass(string const & textclass) const
53 {
54         ClassList::const_iterator cit =
55                 find_if(classlist_.begin(), classlist_.end(),
56                         bind(equal_to<string>(),
57                              bind(&LyXTextClass::name, _1),
58                              textclass));
59
60         return cit != classlist_.end() ?
61                 make_pair(true, textclass_type(cit - classlist_.begin())) :
62                 make_pair(false, textclass_type(0));
63 }
64
65
66 // Gets a textclass structure from number
67 LyXTextClass const &
68 LyXTextClassList::operator[](textclass_type textclass) const
69 {
70         classlist_[textclass].load();
71         if (textclass < classlist_.size())
72                 return classlist_[textclass];
73         else
74                 return classlist_[0];
75 }
76
77
78 // used when sorting the textclass list.
79 class less_textclass_avail_desc
80         : public std::binary_function<LyXTextClass, LyXTextClass, int>
81 {
82 public:
83         int operator()(LyXTextClass const & tc1,
84                        LyXTextClass 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 LyXTextClassList::read()
99 {
100         LyXLex lex(0, 0);
101         string real_file = libFileSearch("", "textclass.lst");
102         lyxerr[Debug::TCLASS] << "Reading textclasses from `"
103                               << real_file << '\'' << endl;
104
105         if (real_file.empty()) {
106                 lyxerr << "LyXTextClassList::Read: unable to find "
107                         "textclass file  `" << makeDisplayPath(real_file, 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 << "LyXTextClassList::Read: "
119                         "lyxlex was not able to set file: "
120                        << real_file << endl;
121         }
122
123         if (!lex.isOK()) {
124                 lyxerr << "LyXTextClassList::Read: unable to open "
125                         "textclass file  `" << makeDisplayPath(real_file, 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, lyx::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         string layout_file = path + "/" + textclass + ".layout";
183         if (fs::exists(layout_file)) {
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|LinuxDoc)Class\s*(\[([^,]*)(,.*)*\])*\s*{(.*)}
190                 ifstream ifs(layout_file.c_str());
191                 static regex const reg("^#\\s*\\\\Declare(LaTeX|DocBook|LinuxDoc)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 }