]> git.lyx.org Git - lyx.git/blob - src/frontends/controllers/tex_helpers.C
59677a859559bcb7f2c2df9e3775afa293502ba7
[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 }
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         p.pop();
62 }
63
64
65 void texhash()
66 {
67         // Run texhash in user lyx directory
68         Path p(user_lyxdir);
69
70         //path to texhash through system
71         Systemcall one;
72         one.startscript(Systemcall::Wait,"texhash"); 
73         p.pop();
74 }
75
76 string const getTexFileList(string const & filename, bool withFullPath)
77 {
78         string const file = LibFileSearch("", filename);
79         if (file.empty())
80                 return string();
81  
82         vector<string> dbase =
83                 getVectorFromString(GetFileContents(file), "\n");
84
85         lyx::eliminate_duplicates(dbase); 
86         string const str_out = withFullPath ?
87                 getStringFromVector(dbase, "\n") :
88                 getStringFromVector(listWithoutPath(dbase), "\n");
89         return str_out;
90 }
91
92
93 string const getListOfOptions(string const & classname,
94                             string const & type)
95 {
96         string const filename = getTexFileFromList(classname,type);
97         string optionList = string();
98         std::ifstream is(filename.c_str());
99         while (is) {
100             string s;
101             is >> s;
102             if (contains(s,"DeclareOption")) {
103                 s = s.substr(s.find("DeclareOption"));
104                 s = split(s,'{');               // cut front
105                 s = token(s,'}',0);             // cut end
106                 optionList += (s + '\n');
107             }
108         }
109         return optionList;
110 }
111
112
113 string const getTexFileFromList(string const & file, 
114                             string const & type)
115 {
116         string const file_ = (type == "cls") ? file + ".cls" : file + ".sty";
117  
118         lyxerr << "Search for classfile " << file_ << endl;
119  
120         string const lstfile = (type == "cls") ? "clsFiles.lst" : "styFiles.lst";
121         string const allClasses = GetFileContents(LibFileSearch(string(), 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         return classfile;
134 }