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