]> git.lyx.org Git - lyx.git/blob - src/frontends/controllers/tex_helpers.C
Change _() to return a docstring. Fixup callers with the help of lyx::to_utf8.
[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         // FIXME UNICODE
64         Alert::error(lyx::to_utf8(_("Could not update TeX information")),
65                      bformat(lyx::to_utf8(_("The script `%s' failed.")), command));
66 }
67
68
69 void texhash()
70 {
71         // Run texhash in user lyx directory
72         Path p(package().user_support());
73
74         //path to texhash through system
75         Systemcall one;
76         one.startscript(Systemcall::Wait,"texhash");
77 }
78
79
80 void getTexFileList(string const & filename, std::vector<string> & list)
81 {
82         list.clear();
83         string const file = libFileSearch("", filename);
84         if (file.empty())
85                 return;
86
87         list = getVectorFromString(getFileContents(file), "\n");
88
89         // Normalise paths like /foo//bar ==> /foo/bar
90         boost::RegEx regex("/{2,}");
91         std::vector<string>::iterator it  = list.begin();
92         std::vector<string>::iterator end = list.end();
93         for (; it != end; ++it) {
94                 *it = regex.Merge((*it), "/");
95         }
96
97         // remove empty items and duplicates
98         list.erase(std::remove(list.begin(), list.end(), ""), list.end());
99         eliminate_duplicates(list);
100 }
101
102
103 string const getListOfOptions(string const & classname, string const & type)
104 {
105         string const filename = getTexFileFromList(classname,type);
106         string optionList = string();
107         std::ifstream is(filename.c_str());
108         while (is) {
109                 string s;
110                 is >> s;
111                 if (contains(s,"DeclareOption")) {
112                         s = s.substr(s.find("DeclareOption"));
113                         s = split(s,'{');               // cut front
114                         s = token(s,'}',0);             // cut end
115                         optionList += (s + '\n');
116                 }
117         }
118         return optionList;
119 }
120
121
122 string const getTexFileFromList(string const & file,
123                             string const & type)
124 {
125         string file_ = file;
126         // do we need to add the suffix?
127         if (!(getExtension(file) == type))
128                 file_ += '.' + type;
129
130         lyxerr << "Searching for file " << file_ << endl;
131
132         string lstfile;
133         if (type == "cls")
134                 lstfile = "clsFiles.lst";
135         else if (type == "sty")
136                 lstfile = "styFiles.lst";
137         else if (type == "bst")
138                 lstfile = "bstFiles.lst";
139         else if (type == "bib")
140                 lstfile = "bibFiles.lst";
141         string const allClasses = getFileContents(libFileSearch(string(),
142                                                                 lstfile));
143         int entries = 0;
144         string classfile = token(allClasses, '\n', entries);
145         int count = 0;
146         while ((!contains(classfile, file) ||
147                 (onlyFilename(classfile) != file)) &&
148                 (++count < 1000)) {
149                 classfile = token(allClasses, '\n', ++entries);
150         }
151
152         // now we have filename with full path
153         lyxerr << "with full path: " << classfile << endl;
154
155         return classfile;
156 }
157
158 } // namespace frontend
159 } // namespace lyx