]> git.lyx.org Git - lyx.git/blob - src/ModuleList.cpp
Fix dialog handling of Insert Plain Text
[lyx.git] / src / ModuleList.cpp
1 // -*- C++ -*-
2 /**
3  * \file ModuleList.cpp
4  * This file is part of LyX, the document processor.
5  * Licence details can be found in the file COPYING.
6  *
7  * \author Richard Heck
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "ModuleList.h"
15
16 #include "debug.h"
17 #include "Lexer.h"
18
19 #include "support/filetools.h"
20 #include "support/lstrings.h"
21
22 #include <algorithm>
23         
24 using std::endl;
25 using std::map;
26 using std::string;
27 using std::vector;
28
29 namespace lyx {
30
31 using support::FileName;
32 using support::libFileSearch;
33 using support::makeDisplayPath;
34
35 //global variable: module list
36 ModuleList moduleList;
37
38
39 // used when sorting the module list.
40 class ModuleSorter
41 {
42 public:
43         int operator()(LyXModule const & lm1, LyXModule const & lm2) const
44         {
45                 return lm1.name < lm2.name;
46         }
47 };
48
49
50 //Much of this is borrowed from TextClassList::read()
51 bool ModuleList::load()
52 {
53         support::FileName const real_file = libFileSearch("", "lyxmodules.lst");
54         LYXERR(Debug::TCLASS, "Reading modules from `" << real_file << '\'');
55
56         if (real_file.empty()) {
57                 lyxerr << "ModuleList::load(): unable to find "
58                                 "modules file  `"
59                                 << to_utf8(makeDisplayPath(real_file.absFilename(), 1000))
60                                 << "'.\nNo modules will be available." << endl;
61                 return false;
62         }
63
64         Lexer lex(0, 0);
65         if (!lex.setFile(real_file)) {
66                 lyxerr << "ModuleList::load():"
67                                 "lyxlex was not able to set file: "
68                                 << real_file << ".\nNo modules will be available." << endl;
69                 return false;
70         }
71
72         if (!lex.isOK()) {
73                 lyxerr << "ModuleList::load():" <<
74                                 "unable to open modules file  `"
75                                 << to_utf8(makeDisplayPath(real_file.absFilename(), 1000))
76                                 << "'\nNo modules will be available."
77                                 << endl;
78                 return false;
79         }
80
81         bool finished = false;
82         // Parse modules files
83         LYXERR(Debug::TCLASS, "Starting parsing of lyxmodules.lst");
84         while (lex.isOK() && !finished) {
85                 LYXERR(Debug::TCLASS, "\tline by line");
86                 switch (lex.lex()) {
87                 case Lexer::LEX_FEOF:
88                         finished = true;
89                         break;
90                 default:
91                         string const modName = lex.getString();
92                         LYXERR(Debug::TCLASS, "Module name: " << modName);
93                         if (!lex.next())
94                                 break;
95                         string const fname = lex.getString();
96                         LYXERR(Debug::TCLASS, "Filename: " << fname);
97                         if (!lex.next())
98                                 break;
99                         string const desc = lex.getString();
100                         LYXERR(Debug::TCLASS, "Description: " << desc);
101                         //FIXME Add packages
102                         if (!lex.next())
103                                 break;
104                         string packages = lex.getString();
105                         LYXERR(Debug::TCLASS, "Packages: " << packages);
106                         vector<string> pkgs;
107                         while (!packages.empty()) {
108                                 string p;
109                                 packages = support::split(packages, p, ',');
110                                 pkgs.push_back(p);
111                         }
112                         // This code is run when we have
113                         // modName, fname, desc, and pkgs
114                         addLayoutModule(modName, fname, desc, pkgs);
115                 } // end switch
116         } //end while
117         
118         LYXERR(Debug::TCLASS, "End of parsing of lyxmodules.lst");
119
120         if (!moduleList.empty())
121                 std::sort(moduleList.begin(), moduleList.end(), ModuleSorter());
122         return true;
123 }
124
125
126 void ModuleList::addLayoutModule(string const & moduleName, 
127         string const & filename, string const & description,
128         vector<string> const & pkgs)
129 {
130         LyXModule lm;
131         lm.name = moduleName;
132         lm.filename = filename;
133         lm.description = description;
134         lm.packageList = pkgs;
135         modlist_.push_back(lm);
136 }
137
138
139 LyXModuleList::const_iterator ModuleList::begin() const
140 {
141         return modlist_.begin();
142 }
143
144
145 LyXModuleList::iterator ModuleList::begin()
146 {
147         return modlist_.begin();
148 }
149
150
151 LyXModuleList::const_iterator ModuleList::end() const
152 {
153         return modlist_.end();
154 }
155
156
157 LyXModuleList::iterator ModuleList::end()
158 {
159         return modlist_.end();
160 }
161
162
163 LyXModule * ModuleList::operator[](string const & str)
164 {
165         LyXModuleList::iterator it = modlist_.begin();
166         for (; it != modlist_.end(); ++it)
167                 if (it->name == str) {
168                         LyXModule & mod = *it;
169                         return &mod;
170                 }
171         return 0;
172 }
173
174 } // namespace lyx