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