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