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