]> git.lyx.org Git - lyx.git/blob - src/format.C
Small clean-up.
[lyx.git] / src / format.C
1 /**
2  * \file format.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Dekel Tsur
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "format.h"
14 #include "buffer.h"
15 #include "bufferparams.h"
16 #include "lyxrc.h"
17 #include "debug.h"
18 #include "gettext.h"
19
20 #include "frontends/Alert.h" //to be removed?
21
22 #include "support/filetools.h"
23 #include "support/path.h"
24 #include "support/systemcall.h"
25 #include "support/lyxfunctional.h"
26
27 using lyx::support::bformat;
28 using lyx::support::compare_ascii_no_case;
29 using lyx::support::contains;
30 using lyx::support::OnlyFilename;
31 using lyx::support::OnlyPath;
32 using lyx::support::Path;
33 using lyx::support::QuoteName;
34 using lyx::support::subst;
35 using lyx::support::Systemcall;
36
37
38 namespace {
39
40 string const token_from("$$i");
41 string const token_path("$$p");
42
43 } //namespace anon
44
45 bool operator<(Format const & a, Format const & b)
46 {
47         // use the compare_ascii_no_case instead of compare_no_case,
48         // because in turkish, 'i' is not the lowercase version of 'I',
49         // and thus turkish locale breaks parsing of tags.
50
51         return compare_ascii_no_case(a.prettyname(), b.prettyname()) < 0;
52 }
53
54 Format::Format(string const & n, string const & e, string const & p,
55        string const & s, string const & v): name_(n),
56                                             extension_(e),
57                                             prettyname_(p),
58                                             shortcut_(s),
59                                             viewer_(v)
60 {}
61
62
63 bool Format::dummy() const
64 {
65         return extension().empty();
66 }
67
68
69 bool Format::isChildFormat() const
70 {
71         if (name_.empty())
72                 return false;
73         return isdigit(name_[name_.length() - 1]);
74 }
75
76
77 string const Format::parentFormat() const
78 {
79         return name_.substr(0, name_.length() - 1);
80 }
81
82
83 // This method should return a reference, and throw an exception
84 // if the format named name cannot be found (Lgb)
85 Format const * Formats::getFormat(string const & name) const
86 {
87         FormatList::const_iterator cit =
88                 find_if(formatlist.begin(), formatlist.end(),
89                         lyx::compare_memfun(&Format::name, name));
90         if (cit != formatlist.end())
91                 return &(*cit);
92         else
93                 return 0;
94 }
95
96
97 int Formats::getNumber(string const & name) const
98 {
99         FormatList::const_iterator cit =
100                 find_if(formatlist.begin(), formatlist.end(),
101                         lyx::compare_memfun(&Format::name, name));
102         if (cit != formatlist.end())
103                 return cit - formatlist.begin();
104         else
105                 return -1;
106 }
107
108
109 void Formats::add(string const & name)
110 {
111         if (!getFormat(name))
112                 add(name, name, name, string());
113 }
114
115
116 void Formats::add(string const & name, string const & extension,
117                   string const & prettyname, string const & shortcut)
118 {
119         FormatList::iterator it =
120                 find_if(formatlist.begin(), formatlist.end(),
121                         lyx::compare_memfun(&Format::name, name));
122         if (it == formatlist.end())
123                 formatlist.push_back(Format(name, extension, prettyname,
124                                             shortcut, ""));
125         else {
126                 string viewer = it->viewer();
127                 *it = Format(name, extension, prettyname, shortcut, viewer);
128         }
129 }
130
131
132 void Formats::erase(string const & name)
133 {
134         FormatList::iterator it =
135                 find_if(formatlist.begin(), formatlist.end(),
136                         lyx::compare_memfun(&Format::name, name));
137         if (it != formatlist.end())
138                 formatlist.erase(it);
139 }
140
141
142 void Formats::sort()
143 {
144         std::sort(formatlist.begin(), formatlist.end());
145 }
146
147
148 void Formats::setViewer(string const & name, string const & command)
149 {
150         add(name);
151         FormatList::iterator it =
152                 find_if(formatlist.begin(), formatlist.end(),
153                         lyx::compare_memfun(&Format::name, name));
154         if (it != formatlist.end())
155                 it->setViewer(command);
156 }
157
158
159 bool Formats::view(Buffer const & buffer, string const & filename,
160                    string const & format_name) const
161 {
162         if (filename.empty())
163                 return false;
164
165         Format const * format = getFormat(format_name);
166         if (format && format->viewer().empty() &&
167             format->isChildFormat())
168                 format = getFormat(format->parentFormat());
169         if (!format || format->viewer().empty()) {
170 // I believe this is the wrong place to show alerts, it should be done by
171 // the caller (this should be "utility" code
172                 Alert::error(_("Cannot view file"),
173                         bformat(_("No information for viewing %1$s"),
174                                 prettyName(format_name)));
175                 return false;
176         }
177
178         string command = format->viewer();
179
180         if (format_name == "dvi" &&
181             !lyxrc.view_dvi_paper_option.empty()) {
182                 command += ' ' + lyxrc.view_dvi_paper_option;
183                 string paper_size = buffer.params().paperSizeName();
184                 if (paper_size == "letter")
185                         paper_size = "us";
186                 command += ' ' + paper_size;
187                 if (buffer.params().orientation == ORIENTATION_LANDSCAPE)
188                         command += 'r';
189         }
190
191         if (!contains(command, token_from))
192                 command += ' ' + token_from;
193
194         command = subst(command, token_from,
195                         QuoteName(OnlyFilename(filename)));
196         command = subst(command, token_path, QuoteName(OnlyPath(filename)));
197
198         lyxerr[Debug::FILES] << "Executing command: " << command << std::endl;
199         buffer.message(_("Executing command: ") + command);
200
201         Path p(OnlyPath(filename));
202         Systemcall one;
203         int const res = one.startscript(Systemcall::DontWait, command);
204
205         if (res) {
206                 Alert::error(_("Cannot view file"),
207                              bformat(_("An error occurred whilst running %1$s"),
208                                command.substr(0, 50)));
209                 return false;
210         }
211         return true;
212 }
213
214
215 string const Formats::prettyName(string const & name) const
216 {
217         Format const * format = getFormat(name);
218         if (format)
219                 return format->prettyname();
220         else
221                 return name;
222 }
223
224
225 string const Formats::extension(string const & name) const
226 {
227         Format const * format = getFormat(name);
228         if (format)
229                 return format->extension();
230         else
231                 return name;
232 }
233
234
235
236
237 Formats formats;
238
239 Formats system_formats;