]> git.lyx.org Git - lyx.git/blob - src/TextClassList.cpp
Fix dialog handling of Insert Plain Text
[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
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 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 TextClassList::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(&TextClass::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 TextClass const &
66 TextClassList::operator[](textclass_type textclass) const
67 {
68         if (textclass >= classlist_.size())
69                 return classlist_[0];
70         
71         //FIXME I don't believe the following line is actually necessary (rgh)
72         classlist_[textclass].load();
73         return classlist_[textclass];
74 }
75
76
77 // used when sorting the textclass list.
78 class less_textclass_avail_desc
79         : public std::binary_function<TextClass, TextClass, int>
80 {
81 public:
82         int operator()(TextClass const & tc1,
83                        TextClass const & tc2) const
84         {
85                 // Ordering criteria:
86                 //   1. Availability of text class
87                 //   2. Description (lexicographic)
88
89                 return (tc1.isTeXClassAvailable() && !tc2.isTeXClassAvailable()) ||
90                         (tc1.isTeXClassAvailable() == tc2.isTeXClassAvailable() &&
91                          tc1.description() < tc2.description());
92         }
93 };
94
95
96 // Reads LyX textclass definitions according to textclass config file
97 bool TextClassList::read()
98 {
99         Lexer lex(0, 0);
100         support::FileName const real_file = libFileSearch("", "textclass.lst");
101         LYXERR(Debug::TCLASS, "Reading textclasses from `" << real_file << '\'');
102
103         if (real_file.empty()) {
104                 lyxerr << "TextClassList::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 << "TextClassList::Read: "
118                         "lyxlex was not able to set file: "
119                        << real_file << endl;
120         }
121
122         if (!lex.isOK()) {
123                 lyxerr << "TextClassList::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");
134         while (lex.isOK() && !finished) {
135                 LYXERR(Debug::TCLASS, "\tline by line");
136                 switch (lex.lex()) {
137                 case Lexer::LEX_FEOF:
138                         finished = true;
139                         break;
140                 default:
141                         string const fname = lex.getString();
142                         LYXERR(Debug::TCLASS, "Fname: " << fname);
143                         if (lex.next()) {
144                                 string const clname = lex.getString();
145                                 LYXERR(Debug::TCLASS, "Clname: " << clname);
146                                 if (lex.next()) {
147                                         string const desc = lex.getString();
148                                         LYXERR(Debug::TCLASS, "Desc: " << desc);
149                                         if (lex.next()) {
150                                                 bool avail = lex.getBool();
151                                                 LYXERR(Debug::TCLASS, "Avail: " << avail);
152                                                 // This code is run when we have
153                                                 // fname, clname, desc, and avail
154                                                 TextClass 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");
165
166         // lyx will start with an empty classlist_, but only reconfigure is allowed
167         // in this case. This gives users a second chance to configure lyx if
168         // initial configuration fails. (c.f. bug 2829)
169         if (classlist_.empty())
170                 lyxerr << "TextClassList::Read: no textclasses found!"
171                        << endl;
172         else 
173                 // Ok everything loaded ok, now sort the list.
174                 sort(classlist_.begin(), classlist_.end(), less_textclass_avail_desc());
175         return true;
176 }
177
178
179 void TextClassList::reset(textclass_type const textclass) {
180         if (textclass >= classlist_.size())
181                 return;
182         TextClass const & tc = classlist_[textclass];
183         TextClass tmpl(tc.name(), tc.latexname(), tc.description(), 
184                        tc.isTeXClassAvailable());
185         classlist_[textclass] = tmpl;
186 }
187
188
189 std::pair<bool, textclass_type> const
190 TextClassList::addTextClass(std::string const & textclass, std::string const & path)
191 {
192         // only check for textclass.layout file, .cls can be anywhere in $TEXINPUTS
193         // NOTE: latex class name is defined in textclass.layout, which can be different from textclass
194         FileName const layout_file(addName(path, textclass + ".layout"));
195         if (layout_file.exists()) {
196                 LYXERR(Debug::TCLASS, "Adding class " << textclass << " from directory " << path);
197                 // Read .layout file and get description, real latex classname etc
198                 //
199                 // This is a C++ version of function processLayoutFile in configure.py,
200                 // which uses the following regex
201                 //     \Declare(LaTeX|DocBook)Class\s*(\[([^,]*)(,.*)*\])*\s*{(.*)}
202                 ifstream ifs(layout_file.toFilesystemEncoding().c_str());
203                 static regex const reg("^#\\s*\\\\Declare(LaTeX|DocBook)Class\\s*"
204                         "(?:\\[([^,]*)(?:,.*)*\\])*\\s*\\{(.*)\\}\\s*");
205                 string line;
206                 while (getline(ifs, line)) {
207                         // look for the \DeclareXXXClass line
208                         smatch sub;
209                         if (regex_match(line, sub, reg)) {
210                                 // returns: whole string, classtype (not used here), first option, description
211                                 BOOST_ASSERT(sub.size()==4);
212                                 // now, add the layout to textclass.
213                                 TextClass tmpl(textclass, sub.str(2)==""?textclass:sub.str(2),
214                                         sub.str(3) + " <" + path + ">", true);
215                                 if (lyxerr.debugging(Debug::TCLASS))
216                                         tmpl.load(path);
217                                 classlist_.push_back(tmpl);
218                                 return make_pair(true, classlist_.size() - 1);
219                         }
220                 }
221         }
222         // If .layout is not in local directory, or an invalid layout is found, return false
223         return make_pair(false, textclass_type(0));
224 }
225
226
227 // Global variable: textclass table.
228 TextClassList textclasslist;
229
230
231 textclass_type defaultTextclass()
232 {
233         // We want to return the article class. if `first' is
234         // true in the returned pair, then `second' is the textclass
235         // number; if it is false, second is 0. In both cases, second
236         // is what we want.
237         return textclasslist.numberOfClass("article").second;
238 }
239
240
241
242 // Reads the style files
243 bool LyXSetStyle()
244 {
245         LYXERR(Debug::TCLASS, "LyXSetStyle: parsing configuration...");
246
247         if (!textclasslist.read()) {
248                 LYXERR(Debug::TCLASS, "LyXSetStyle: an error occured "
249                         "during parsing.\n             Exiting.");
250                 return false;
251         }
252
253         LYXERR(Debug::TCLASS, "LyXSetStyle: configuration parsed.");
254         return true;
255 }
256
257
258 } // namespace lyx