]> git.lyx.org Git - lyx.git/blob - src/frontends/controllers/tex_helpers.C
The package reworking.
[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
17 #include "support/filetools.h"
18 #include "support/lstrings.h"
19 #include "support/lyxalgo.h"
20 #include "support/package.h"
21 #include "support/path.h"
22 #include "support/systemcall.h"
23
24 #include <boost/cregex.hpp>
25 #include <fstream>
26
27 using std::string;
28 using std::endl;
29
30 namespace lyx {
31
32 using support::contains;
33 using support::GetFileContents;
34 using support::getVectorFromString;
35 using support::LibFileSearch;
36 using support::OnlyFilename;
37 using support::package;
38 using support::Path;
39 using support::split;
40 using support::Systemcall;
41 using support::token;
42
43 namespace frontend {
44
45 // build filelists of all availabe bst/cls/sty-files. done through
46 // kpsewhich and an external script, saved in *Files.lst
47 void rescanTexStyles()
48 {
49         // Run rescan in user lyx directory
50         Path p(package().user_support());
51         Systemcall one;
52         one.startscript(Systemcall::Wait,
53                         LibFileSearch("scripts", "TeXFiles.sh"));
54 }
55
56
57 void texhash()
58 {
59         // Run texhash in user lyx directory
60         Path p(package().user_support());
61
62         //path to texhash through system
63         Systemcall one;
64         one.startscript(Systemcall::Wait,"texhash");
65 }
66
67
68 void getTexFileList(string const & filename, std::vector<string> & list)
69 {
70         list.clear();
71         string const file = LibFileSearch("", filename);
72         if (file.empty())
73                 return;
74
75         list = getVectorFromString(GetFileContents(file), "\n");
76
77         // Normalise paths like /foo//bar ==> /foo/bar
78         boost::RegEx regex("/{2,}");
79         std::vector<string>::iterator it  = list.begin();
80         std::vector<string>::iterator end = list.end();
81         for (; it != end; ++it) {
82                 *it = regex.Merge((*it), "/");
83         }
84
85         eliminate_duplicates(list);
86 }
87
88
89 string const getListOfOptions(string const & classname, string const & type)
90 {
91         string const filename = getTexFileFromList(classname,type);
92         string optionList = string();
93         std::ifstream is(filename.c_str());
94         while (is) {
95                 string s;
96                 is >> s;
97                 if (contains(s,"DeclareOption")) {
98                         s = s.substr(s.find("DeclareOption"));
99                         s = split(s,'{');               // cut front
100                         s = token(s,'}',0);             // cut end
101                         optionList += (s + '\n');
102                 }
103         }
104         return optionList;
105 }
106
107
108 string const getTexFileFromList(string const & file,
109                             string const & type)
110 {
111         string const file_ = (type == "cls") ? file + ".cls" : file + ".sty";
112
113         lyxerr << "Search for classfile " << file_ << endl;
114
115         string const lstfile =
116                 ((type == "cls") ? "clsFiles.lst" : "styFiles.lst");
117         string const allClasses = GetFileContents(LibFileSearch(string(),
118                                                                 lstfile));
119         int entries = 0;
120         string classfile = token(allClasses, '\n', entries);
121         int count = 0;
122         while ((!contains(classfile, file) ||
123                 (OnlyFilename(classfile) != file)) &&
124                 (++count < 1000)) {
125                 classfile = token(allClasses, '\n', ++entries);
126         }
127
128         // now we have filename with full path
129         lyxerr << "with full path: " << classfile << endl;
130
131         return classfile;
132 }
133
134 } // namespace frontend
135 } // namespace lyx