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