]> git.lyx.org Git - lyx.git/blob - src/lyxtextclasslist.C
99c2d94a3acf32b2ae8af998a49ff46cdc9270b0
[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
23 using lyx::textclass_type;
24
25 using lyx::support::LibFileSearch;
26 using lyx::support::MakeDisplayPath;
27
28 using boost::bind;
29
30 #ifndef CXX_GLOBAL_CSTD
31 using std::exit;
32 #endif
33
34 using std::endl;
35 using std::equal_to;
36 using std::find_if;
37 using std::make_pair;
38 using std::sort;
39 using std::string;
40 using std::pair;
41
42
43 // Gets textclass number from name
44 pair<bool, textclass_type> const
45 LyXTextClassList::NumberOfClass(string const & textclass) const
46 {
47         ClassList::const_iterator cit =
48                 find_if(classlist_.begin(), classlist_.end(),
49                         bind(equal_to<string>(),
50                              bind(&LyXTextClass::name, _1),
51                              textclass));
52
53         return cit != classlist_.end() ?
54                 make_pair(true, textclass_type(cit - classlist_.begin())) :
55                 make_pair(false, textclass_type(0));
56 }
57
58
59 // Gets a textclass structure from number
60 LyXTextClass const &
61 LyXTextClassList::operator[](textclass_type textclass) const
62 {
63         classlist_[textclass].load();
64         if (textclass < classlist_.size())
65                 return classlist_[textclass];
66         else
67                 return classlist_[0];
68 }
69
70
71 // used when sorting the textclass list.
72 class less_textclass_avail_desc
73         : public std::binary_function<LyXTextClass, LyXTextClass, int>
74 {
75 public:
76         int operator()(LyXTextClass const & tc1,
77                        LyXTextClass const & tc2) const
78         {
79                 // Ordering criteria:
80                 //   1. Availability of text class
81                 //   2. Description (lexicographic)
82
83                 return (tc1.isTeXClassAvailable() && !tc2.isTeXClassAvailable()) ||
84                         (tc1.isTeXClassAvailable() == tc2.isTeXClassAvailable() &&
85                          tc1.description() < tc2.description());
86         }
87 };
88
89
90 // Reads LyX textclass definitions according to textclass config file
91 bool LyXTextClassList::Read()
92 {
93         LyXLex lex(0, 0);
94         string real_file = LibFileSearch("", "textclass.lst");
95         lyxerr[Debug::TCLASS] << "Reading textclasses from `"
96                               << real_file << '\'' << endl;
97
98         if (real_file.empty()) {
99                 lyxerr << "LyXTextClassList::Read: unable to find "
100                         "textclass file  `" << MakeDisplayPath(real_file, 1000)
101                        << "'. Exiting." << endl;
102                 return false;
103                 // This causes LyX to end... Not a desirable behaviour. Lgb
104                 // What do you propose? That the user gets a file dialog
105                 // and is allowed to hunt for the file? (Asger)
106                 // more that we have a layout for minimal.cls statically
107                 // compiled in... (Lgb)
108         }
109
110         if (!lex.setFile(real_file)) {
111                 lyxerr << "LyXTextClassList::Read: "
112                         "lyxlex was not able to set file: "
113                        << real_file << endl;
114         }
115
116         if (!lex.isOK()) {
117                 lyxerr << "LyXTextClassList::Read: unable to open "
118                         "textclass file  `" << MakeDisplayPath(real_file, 1000)
119                        << "'\nCheck your installation. LyX can't continue."
120                        << endl;
121                 return false;
122         }
123
124         bool finished = false;
125         // Parse config-file
126         lyxerr[Debug::TCLASS] << "Starting parsing of textclass.lst" << endl;
127         while (lex.isOK() && !finished) {
128                 lyxerr[Debug::TCLASS] << "\tline by line" << endl;
129                 switch (lex.lex()) {
130                 case LyXLex::LEX_FEOF:
131                         finished = true;
132                         break;
133                 default:
134                         string const fname = lex.getString();
135                         lyxerr[Debug::TCLASS] << "Fname: " << fname << endl;
136                         if (lex.next()) {
137                                 string const clname = lex.getString();
138                                 lyxerr[Debug::TCLASS] << "Clname: " << clname << endl;
139                                 if (lex.next()) {
140                                         string const desc = lex.getString();
141                                         lyxerr[Debug::TCLASS] << "Desc: " << desc << endl;
142                                         if (lex.next()) {
143                                                 bool avail = lex.getBool();
144                                                 lyxerr[Debug::TCLASS] << "Avail: " << avail << endl;
145                                                 // This code is run when we have
146                                                 // fname, clname, desc, and avail
147                                                 LyXTextClass tmpl(fname, clname, desc, avail);
148                                                 if (lyxerr.debugging(Debug::TCLASS)) {
149                                                         tmpl.load();
150                                                 }
151                                                 classlist_.push_back(tmpl);
152                                         }
153                                 }
154                         }
155                 }
156         }
157         lyxerr[Debug::TCLASS] << "End of parsing of textclass.lst" << endl;
158
159         if (classlist_.empty()) {
160                 lyxerr << "LyXTextClassList::Read: no textclasses found!"
161                        << endl;
162                 return false;
163         }
164         // Ok everything loaded ok, now sort the list.
165         sort(classlist_.begin(), classlist_.end(), less_textclass_avail_desc());
166         return true;
167 }
168
169
170 // Global variable: textclass table.
171 LyXTextClassList textclasslist;
172
173
174 // Reads the style files
175 void LyXSetStyle()
176 {
177         lyxerr[Debug::TCLASS] << "LyXSetStyle: parsing configuration..." << endl;
178
179         if (!textclasslist.Read()) {
180                 lyxerr[Debug::TCLASS] << "LyXSetStyle: an error occured "
181                         "during parsing.\n             Exiting." << endl;
182                 exit(1);
183         }
184
185         lyxerr[Debug::TCLASS] << "LyXSetStyle: configuration parsed." << endl;
186 }