]> git.lyx.org Git - lyx.git/blob - src/frontends/controllers/tex_helpers.C
Give feedback if TeXFiles.py fails
[lyx.git] / src / frontends / controllers / tex_helpers.C
1 /**
2  * \file tex_helpers.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Herbert Voß
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "tex_helpers.h"
14
15 #include "debug.h"
16 #include "gettext.h"
17
18 #include "frontends/Alert.h"
19
20 #include "support/filetools.h"
21 #include "support/lstrings.h"
22 #include "support/lyxalgo.h"
23 #include "support/os.h"
24 #include "support/package.h"
25 #include "support/path.h"
26 #include "support/systemcall.h"
27
28 #include <boost/cregex.hpp>
29 #include <fstream>
30
31 using std::string;
32 using std::endl;
33
34 namespace lyx {
35
36 using support::bformat;
37 using support::contains;
38 using support::getExtension;
39 using support::getFileContents;
40 using support::getVectorFromString;
41 using support::libFileSearch;
42 using support::onlyFilename;
43 using support::package;
44 using support::Path;
45 using support::quoteName;
46 using support::split;
47 using support::Systemcall;
48 using support::token;
49
50 namespace frontend {
51
52 void rescanTexStyles()
53 {
54         // Run rescan in user lyx directory
55         Path p(package().user_support());
56         string const command = libFileSearch("scripts", "TeXFiles.py");
57         Systemcall one;
58         int const status = one.startscript(Systemcall::Wait,
59                         lyx::support::os::python() + ' ' +
60                         quoteName(command));
61         if (status == 0)
62                 return;
63         Alert::error(_("Could not update TeX information"),
64                      bformat(_("The script `%s' failed."), command));
65 }
66
67
68 void texhash()
69 {
70         // Run texhash in user lyx directory
71         Path p(package().user_support());
72
73         //path to texhash through system
74         Systemcall one;
75         one.startscript(Systemcall::Wait,"texhash");
76 }
77
78
79 void getTexFileList(string const & filename, std::vector<string> & list)
80 {
81         list.clear();
82         string const file = libFileSearch("", filename);
83         if (file.empty())
84                 return;
85
86         list = getVectorFromString(getFileContents(file), "\n");
87
88         // Normalise paths like /foo//bar ==> /foo/bar
89         boost::RegEx regex("/{2,}");
90         std::vector<string>::iterator it  = list.begin();
91         std::vector<string>::iterator end = list.end();
92         for (; it != end; ++it) {
93                 *it = regex.Merge((*it), "/");
94         }
95
96         // remove empty items and duplicates
97         list.erase(std::remove(list.begin(), list.end(), ""), list.end());
98         eliminate_duplicates(list);
99 }
100
101
102 string const getListOfOptions(string const & classname, string const & type)
103 {
104         string const filename = getTexFileFromList(classname,type);
105         string optionList = string();
106         std::ifstream is(filename.c_str());
107         while (is) {
108                 string s;
109                 is >> s;
110                 if (contains(s,"DeclareOption")) {
111                         s = s.substr(s.find("DeclareOption"));
112                         s = split(s,'{');               // cut front
113                         s = token(s,'}',0);             // cut end
114                         optionList += (s + '\n');
115                 }
116         }
117         return optionList;
118 }
119
120
121 string const getTexFileFromList(string const & file,
122                             string const & type)
123 {
124         string file_ = file;
125         // do we need to add the suffix?
126         if (!(getExtension(file) == type))
127                 file_ += '.' + type;
128
129         lyxerr << "Searching for file " << file_ << endl;
130
131         string lstfile;
132         if (type == "cls")
133                 lstfile = "clsFiles.lst";
134         else if (type == "sty")
135                 lstfile = "styFiles.lst";
136         else if (type == "bst")
137                 lstfile = "bstFiles.lst";
138         else if (type == "bib")
139                 lstfile = "bibFiles.lst";
140         string const allClasses = getFileContents(libFileSearch(string(),
141                                                                 lstfile));
142         int entries = 0;
143         string classfile = token(allClasses, '\n', entries);
144         int count = 0;
145         while ((!contains(classfile, file) ||
146                 (onlyFilename(classfile) != file)) &&
147                 (++count < 1000)) {
148                 classfile = token(allClasses, '\n', ++entries);
149         }
150
151         // now we have filename with full path
152         lyxerr << "with full path: " << classfile << endl;
153
154         return classfile;
155 }
156
157 } // namespace frontend
158 } // namespace lyx