]> git.lyx.org Git - lyx.git/blob - src/frontends/controllers/tex_helpers.C
* Throw out DialogBase.h
[lyx.git] / src / frontends / controllers / tex_helpers.C
1 /**
2  * \file tex_helpers.C
3  * Copyright 1995-2002 the LyX Team
4  * Read the file COPYING
5  *
6  * \author Herbert Voss <voss@lyx.org>
7  */
8
9 #include <config.h>
10
11 #ifdef __GNUG__
12 #pragma implementation
13 #endif
14
15 #include "tex_helpers.h"
16
17 #include "debug.h"
18 #include "gettext.h"
19
20 #include "support/filetools.h"
21 #include "support/lstrings.h"
22 #include "support/systemcall.h"
23 #include "support/path.h"
24 #include "support/lyxalgo.h"
25
26 #include <vector>
27 #include <fstream>
28 #include <algorithm>
29
30 using std::vector;
31 using std::endl;
32 using std::sort;
33 using std::unique;
34
35 extern string user_lyxdir; // home of *Files.lst
36
37 namespace {
38
39 vector<string> listWithoutPath(vector<string> & dbase)
40 {
41         vector<string>::iterator it = dbase.begin();
42         vector<string>::iterator end = dbase.end();
43         for (; it != end; ++it) 
44                 *it = OnlyFilename(*it);
45         return dbase;
46 }
47
48 }
49
50 // build filelists of all availabe bst/cls/sty-files. done through
51 // kpsewhich and an external script, saved in *Files.lst
52 void rescanTexStyles()
53 {
54         // Run rescan in user lyx directory
55         Path p(user_lyxdir);
56         Systemcall one;
57         one.startscript(Systemcall::Wait,
58                         LibFileSearch("scripts", "TeXFiles.sh"));
59         p.pop();
60 }
61
62
63 void texhash()
64 {
65         // Run texhash in user lyx directory
66         Path p(user_lyxdir);
67
68         //path to texhash through system
69         Systemcall one;
70         one.startscript(Systemcall::Wait,"texhash"); 
71         p.pop();
72 }
73
74 string const getTexFileList(string const & filename, bool withFullPath)
75 {
76         string const file = LibFileSearch("", filename);
77         if (file.empty())
78                 return string();
79  
80         vector<string> dbase =
81                 getVectorFromString(GetFileContents(file), "\n");
82
83         lyx::eliminate_duplicates(dbase); 
84         string const str_out = withFullPath ?
85                 getStringFromVector(dbase, "\n") :
86                 getStringFromVector(listWithoutPath(dbase), "\n");
87         return str_out;
88 }
89
90
91 string const getListOfOptions(string const & classname,
92                             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 = (type == "cls") ? "clsFiles.lst" : "styFiles.lst";
119         string const allClasses = GetFileContents(LibFileSearch(string(), lstfile));
120         int entries = 0;
121         string classfile = token(allClasses, '\n', entries);
122         int count = 0;
123         while ((!contains(classfile, file) || 
124                 (OnlyFilename(classfile) != file)) &&
125                 (++count < 1000)) {
126                 classfile = token(allClasses, '\n', ++entries);
127         }
128  
129         // now we have filename with full path
130         lyxerr << "with full path: " << classfile << endl;
131         return classfile;
132 }