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