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