]> git.lyx.org Git - lyx.git/blob - src/lyxtextclasslist.C
* Replace all use of 'slashify_path' with 'internal_path'.
[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 struct less_textclass_avail_desc
73         : public std::binary_function<LyXTextClass, LyXTextClass, int>
74 {
75         int operator()(LyXTextClass const & tc1,
76                        LyXTextClass const & tc2) const
77         {
78                 // Ordering criteria:
79                 //   1. Availability of text class
80                 //   2. Description (lexicographic)
81
82                 return (tc1.isTeXClassAvailable() && !tc2.isTeXClassAvailable()) ||
83                         (tc1.isTeXClassAvailable() == tc2.isTeXClassAvailable() &&
84                          tc1.description() < tc2.description());
85         }
86 };
87
88
89 // Reads LyX textclass definitions according to textclass config file
90 bool LyXTextClassList::Read()
91 {
92         LyXLex lex(0, 0);
93         string real_file = LibFileSearch("", "textclass.lst");
94         lyxerr[Debug::TCLASS] << "Reading textclasses from `"
95                               << real_file << '\'' << endl;
96
97         if (real_file.empty()) {
98                 lyxerr << "LyXTextClassList::Read: unable to find "
99                         "textclass file  `" << MakeDisplayPath(real_file, 1000)
100                        << "'. Exiting." << endl;
101                 return false;
102                 // This causes LyX to end... Not a desirable behaviour. Lgb
103                 // What do you propose? That the user gets a file dialog
104                 // and is allowed to hunt for the file? (Asger)
105                 // more that we have a layout for minimal.cls statically
106                 // compiled in... (Lgb)
107         }
108
109         if (!lex.setFile(real_file)) {
110                 lyxerr << "LyXTextClassList::Read: "
111                         "lyxlex was not able to set file: "
112                        << real_file << endl;
113         }
114
115         if (!lex.isOK()) {
116                 lyxerr << "LyXTextClassList::Read: unable to open "
117                         "textclass file  `" << MakeDisplayPath(real_file, 1000)
118                        << "'\nCheck your installation. LyX can't continue."
119                        << endl;
120                 return false;
121         }
122
123         bool finished = false;
124         // Parse config-file
125         lyxerr[Debug::TCLASS] << "Starting parsing of textclass.lst" << endl;
126         while (lex.isOK() && !finished) {
127                 lyxerr[Debug::TCLASS] << "\tline by line" << endl;
128                 switch (lex.lex()) {
129                 case LyXLex::LEX_FEOF:
130                         finished = true;
131                         break;
132                 default:
133                         string const fname = lex.getString();
134                         lyxerr[Debug::TCLASS] << "Fname: " << fname << endl;
135                         if (lex.next()) {
136                                 string const clname = lex.getString();
137                                 lyxerr[Debug::TCLASS] << "Clname: " << clname << endl;
138                                 if (lex.next()) {
139                                         string const desc = lex.getString();
140                                         lyxerr[Debug::TCLASS] << "Desc: " << desc << endl;
141                                         if (lex.next()) {
142                                                 bool avail = lex.getBool();
143                                                 lyxerr[Debug::TCLASS] << "Avail: " << avail << endl;
144                                                 // This code is run when we have
145                                                 // fname, clname, desc, and avail
146                                                 LyXTextClass tmpl(fname, clname, desc, avail);
147                                                 if (lyxerr.debugging(Debug::TCLASS)) {
148                                                         tmpl.load();
149                                                 }
150                                                 classlist_.push_back(tmpl);
151                                         }
152                                 }
153                         }
154                 }
155         }
156         lyxerr[Debug::TCLASS] << "End of parsing of textclass.lst" << endl;
157
158         if (classlist_.empty()) {
159                 lyxerr << "LyXTextClassList::Read: no textclasses found!"
160                        << endl;
161                 return false;
162         }
163         // Ok everything loaded ok, now sort the list.
164         sort(classlist_.begin(), classlist_.end(), less_textclass_avail_desc());
165         return true;
166 }
167
168
169 // Global variable: textclass table.
170 LyXTextClassList textclasslist;
171
172
173 // Reads the style files
174 void LyXSetStyle()
175 {
176         lyxerr[Debug::TCLASS] << "LyXSetStyle: parsing configuration..." << endl;
177
178         if (!textclasslist.Read()) {
179                 lyxerr[Debug::TCLASS] << "LyXSetStyle: an error occured "
180                         "during parsing.\n             Exiting." << endl;
181                 exit(1);
182         }
183
184         lyxerr[Debug::TCLASS] << "LyXSetStyle: configuration parsed." << endl;
185 }