3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
8 * Full author contact details are available in file CREDITS.
15 #include "bufferparams.h"
19 #include "lyxsocket.h"
21 #include "frontends/Alert.h" //to be removed?
23 #include "support/filetools.h"
24 #include "support/path.h"
25 #include "support/systemcall.h"
27 using lyx::support::bformat;
28 using lyx::support::compare_ascii_no_case;
29 using lyx::support::contains;
30 using lyx::support::libScriptSearch;
31 using lyx::support::makeDisplayPath;
32 using lyx::support::onlyFilename;
33 using lyx::support::onlyPath;
34 using lyx::support::Path;
35 using lyx::support::quoteName;
36 using lyx::support::subst;
37 using lyx::support::Systemcall;
42 extern LyXServerSocket * lyxsocket;
46 string const token_from("$$i");
47 string const token_path("$$p");
48 string const token_socket("$$a");
51 class FormatNamesEqual : public std::unary_function<Format, bool> {
53 FormatNamesEqual(string const & name)
55 bool operator()(Format const & f) const
57 return f.name() == name_;
64 class FormatExtensionsEqual : public std::unary_function<Format, bool> {
66 FormatExtensionsEqual(string const & extension)
67 : extension_(extension) {}
68 bool operator()(Format const & f) const
70 return f.extension() == extension_;
78 bool operator<(Format const & a, Format const & b)
80 // use the compare_ascii_no_case instead of compare_no_case,
81 // because in turkish, 'i' is not the lowercase version of 'I',
82 // and thus turkish locale breaks parsing of tags.
84 return compare_ascii_no_case(a.prettyname(), b.prettyname()) < 0;
87 Format::Format(string const & n, string const & e, string const & p,
88 string const & s, string const & v, string const & ed)
89 : name_(n), extension_(e), prettyname_(p), shortcut_(s), viewer_(v), editor_(ed)
93 bool Format::dummy() const
95 return extension().empty();
99 bool Format::isChildFormat() const
103 return isdigit(name_[name_.length() - 1]);
107 string const Format::parentFormat() const
109 return name_.substr(0, name_.length() - 1);
113 // This method should return a reference, and throw an exception
114 // if the format named name cannot be found (Lgb)
115 Format const * Formats::getFormat(string const & name) const
117 FormatList::const_iterator cit =
118 find_if(formatlist.begin(), formatlist.end(),
119 FormatNamesEqual(name));
120 if (cit != formatlist.end())
127 string Formats::getFormatFromFile(string const & filename) const
129 if (filename.empty())
132 string const format = lyx::support::getFormatFromContents(filename);
136 // try to find a format from the file extension.
137 string const ext(lyx::support::getExtension(filename));
139 // this is ambigous if two formats have the same extension,
140 // but better than nothing
141 Formats::const_iterator cit =
142 find_if(formatlist.begin(), formatlist.end(),
143 FormatExtensionsEqual(ext));
144 if (cit != formats.end()) {
145 lyxerr[Debug::GRAPHICS]
146 << "\twill guess format from file extension: "
147 << ext << " -> " << cit->name() << std::endl;
155 int Formats::getNumber(string const & name) const
157 FormatList::const_iterator cit =
158 find_if(formatlist.begin(), formatlist.end(),
159 FormatNamesEqual(name));
160 if (cit != formatlist.end())
161 return distance(formatlist.begin(), cit);
167 void Formats::add(string const & name)
169 if (!getFormat(name))
170 add(name, name, name, string(), string(), string());
174 void Formats::add(string const & name, string const & extension,
175 string const & prettyname, string const & shortcut,
176 string const & viewer, string const & editor)
178 FormatList::iterator it =
179 find_if(formatlist.begin(), formatlist.end(),
180 FormatNamesEqual(name));
181 if (it == formatlist.end())
182 formatlist.push_back(Format(name, extension, prettyname,
183 shortcut, viewer, editor));
185 *it = Format(name, extension, prettyname, shortcut, viewer, editor);
189 void Formats::erase(string const & name)
191 FormatList::iterator it =
192 find_if(formatlist.begin(), formatlist.end(),
193 FormatNamesEqual(name));
194 if (it != formatlist.end())
195 formatlist.erase(it);
201 std::sort(formatlist.begin(), formatlist.end());
205 void Formats::setViewer(string const & name, string const & command)
208 FormatList::iterator it =
209 find_if(formatlist.begin(), formatlist.end(),
210 FormatNamesEqual(name));
211 if (it != formatlist.end())
212 it->setViewer(command);
216 bool Formats::view(Buffer const & buffer, string const & filename,
217 string const & format_name) const
219 if (filename.empty())
222 Format const * format = getFormat(format_name);
223 if (format && format->viewer().empty() &&
224 format->isChildFormat())
225 format = getFormat(format->parentFormat());
226 if (!format || format->viewer().empty()) {
227 // I believe this is the wrong place to show alerts, it should be done by
228 // the caller (this should be "utility" code)
229 Alert::error(_("Cannot view file"),
230 bformat(_("No information for viewing %1$s"),
231 prettyName(format_name)));
235 string command = libScriptSearch(format->viewer());
237 if (format_name == "dvi" &&
238 !lyxrc.view_dvi_paper_option.empty()) {
239 command += ' ' + lyxrc.view_dvi_paper_option;
240 string paper_size = buffer.params().paperSizeName();
241 if (paper_size == "letter")
243 command += ' ' + paper_size;
244 if (buffer.params().orientation == ORIENTATION_LANDSCAPE)
248 if (!contains(command, token_from))
249 command += ' ' + token_from;
251 command = subst(command, token_from,
252 quoteName(onlyFilename(filename)));
253 command = subst(command, token_path, quoteName(onlyPath(filename)));
254 command = subst(command, token_socket, quoteName(lyxsocket->address()));
255 lyxerr[Debug::FILES] << "Executing command: " << command << std::endl;
256 buffer.message(_("Executing command: ") + command);
258 Path p(onlyPath(filename));
260 int const res = one.startscript(Systemcall::DontWait, command);
263 Alert::error(_("Cannot view file"),
264 bformat(_("An error occurred whilst running %1$s"),
265 makeDisplayPath(command, 50)));
272 bool Formats::edit(Buffer const & buffer, string const & filename,
273 string const & format_name) const
275 if (filename.empty())
278 Format const * format = getFormat(format_name);
279 if (format && format->editor().empty() &&
280 format->isChildFormat())
281 format = getFormat(format->parentFormat());
282 if (!format || format->editor().empty()) {
283 // I believe this is the wrong place to show alerts, it should be done by
284 // the caller (this should be "utility" code)
285 Alert::error(_("Cannot edit file"),
286 bformat(_("No information for editing %1$s"),
287 prettyName(format_name)));
291 string command = format->editor();
293 if (!contains(command, token_from))
294 command += ' ' + token_from;
296 command = subst(command, token_from,
297 quoteName(onlyFilename(filename)));
298 command = subst(command, token_path, quoteName(onlyPath(filename)));
299 command = subst(command, token_socket, quoteName(lyxsocket->address()));
300 lyxerr[Debug::FILES] << "Executing command: " << command << std::endl;
301 buffer.message(_("Executing command: ") + command);
303 Path p(onlyPath(filename));
305 int const res = one.startscript(Systemcall::DontWait, command);
308 Alert::error(_("Cannot edit file"),
309 bformat(_("An error occurred whilst running %1$s"),
310 makeDisplayPath(command, 50)));
317 string const Formats::prettyName(string const & name) const
319 Format const * format = getFormat(name);
321 return format->prettyname();
327 string const Formats::extension(string const & name) const
329 Format const * format = getFormat(name);
331 return format->extension();
341 Formats system_formats;