]> git.lyx.org Git - lyx.git/blob - src/frontends/controllers/tex_helpers.C
Really dull and boring header shit
[lyx.git] / src / frontends / controllers / tex_helpers.C
1 /**
2  * \file tex_helpers.C
3  * Read the file COPYING
4  *
5  * \author Herbert Voss 
6  *
7  * Full author contact details are available in file CREDITS
8  */
9
10 #include <config.h>
11
12 #ifdef __GNUG__
13 #pragma implementation
14 #endif
15
16 #include "tex_helpers.h"
17
18 #include "debug.h"
19 #include "gettext.h"
20
21 #include "support/filetools.h"
22 #include "support/lstrings.h"
23 #include "support/systemcall.h"
24 #include "support/path.h"
25 #include "support/lyxalgo.h"
26
27 #include <vector>
28 #include <fstream>
29 #include <algorithm>
30
31 using std::vector;
32 using std::endl;
33 using std::sort;
34 using std::unique;
35
36 extern string user_lyxdir; // home of *Files.lst
37
38 namespace {
39
40 vector<string> listWithoutPath(vector<string> & dbase)
41 {
42         vector<string>::iterator it = dbase.begin();
43         vector<string>::iterator end = dbase.end();
44         for (; it != end; ++it) 
45                 *it = OnlyFilename(*it);
46         return dbase;
47 }
48
49 }
50
51 // build filelists of all availabe bst/cls/sty-files. done through
52 // kpsewhich and an external script, saved in *Files.lst
53 void rescanTexStyles()
54 {
55         // Run rescan in user lyx directory
56         Path p(user_lyxdir);
57         Systemcall one;
58         one.startscript(Systemcall::Wait,
59                         LibFileSearch("scripts", "TeXFiles.sh"));
60         p.pop();
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         p.pop();
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,
93                             string const & type)
94 {
95         string const filename = getTexFileFromList(classname,type);
96         string optionList = string();
97         std::ifstream is(filename.c_str());
98         while (is) {
99             string s;
100             is >> s;
101             if (contains(s,"DeclareOption")) {
102                 s = s.substr(s.find("DeclareOption"));
103                 s = split(s,'{');               // cut front
104                 s = token(s,'}',0);             // cut end
105                 optionList += (s + '\n');
106             }
107         }
108         return optionList;
109 }
110
111
112 string const getTexFileFromList(string const & file, 
113                             string const & type)
114 {
115         string const file_ = (type == "cls") ? file + ".cls" : file + ".sty";
116  
117         lyxerr << "Search for classfile " << file_ << endl;
118  
119         string const lstfile = (type == "cls") ? "clsFiles.lst" : "styFiles.lst";
120         string const allClasses = GetFileContents(LibFileSearch(string(), lstfile));
121         int entries = 0;
122         string classfile = token(allClasses, '\n', entries);
123         int count = 0;
124         while ((!contains(classfile, file) || 
125                 (OnlyFilename(classfile) != file)) &&
126                 (++count < 1000)) {
127                 classfile = token(allClasses, '\n', ++entries);
128         }
129  
130         // now we have filename with full path
131         lyxerr << "with full path: " << classfile << endl;
132         return classfile;
133 }