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::MakeDisplayPath;
31 using lyx::support::OnlyFilename;
32 using lyx::support::OnlyPath;
33 using lyx::support::Path;
34 using lyx::support::QuoteName;
35 using lyx::support::subst;
36 using lyx::support::Systemcall;
41 extern LyXServerSocket * lyxsocket;
45 string const token_from("$$i");
46 string const token_path("$$p");
47 string const token_socket("$$a");
50 class FormatNamesEqual : public std::unary_function<Format, bool> {
52 FormatNamesEqual(string const & name)
54 bool operator()(Format const & f) const
56 return f.name() == name_;
64 bool operator<(Format const & a, Format const & b)
66 // use the compare_ascii_no_case instead of compare_no_case,
67 // because in turkish, 'i' is not the lowercase version of 'I',
68 // and thus turkish locale breaks parsing of tags.
70 return compare_ascii_no_case(a.prettyname(), b.prettyname()) < 0;
73 Format::Format(string const & n, string const & e, string const & p,
74 string const & s, string const & v, string const & ed)
75 : name_(n), extension_(e), prettyname_(p), shortcut_(s), viewer_(v), editor_(ed)
79 bool Format::dummy() const
81 return extension().empty();
85 bool Format::isChildFormat() const
89 return isdigit(name_[name_.length() - 1]);
93 string const Format::parentFormat() const
95 return name_.substr(0, name_.length() - 1);
99 // This method should return a reference, and throw an exception
100 // if the format named name cannot be found (Lgb)
101 Format const * Formats::getFormat(string const & name) const
103 FormatList::const_iterator cit =
104 find_if(formatlist.begin(), formatlist.end(),
105 FormatNamesEqual(name));
106 if (cit != formatlist.end())
113 int Formats::getNumber(string const & name) const
115 FormatList::const_iterator cit =
116 find_if(formatlist.begin(), formatlist.end(),
117 FormatNamesEqual(name));
118 if (cit != formatlist.end())
119 return distance(formatlist.begin(), cit);
125 void Formats::add(string const & name)
127 if (!getFormat(name))
128 add(name, name, name, string(), string(), string());
132 void Formats::add(string const & name, string const & extension,
133 string const & prettyname, string const & shortcut,
134 string const & viewer, string const & editor)
136 FormatList::iterator it =
137 find_if(formatlist.begin(), formatlist.end(),
138 FormatNamesEqual(name));
139 if (it == formatlist.end())
140 formatlist.push_back(Format(name, extension, prettyname,
141 shortcut, viewer, editor));
143 *it = Format(name, extension, prettyname, shortcut, viewer, editor);
147 void Formats::erase(string const & name)
149 FormatList::iterator it =
150 find_if(formatlist.begin(), formatlist.end(),
151 FormatNamesEqual(name));
152 if (it != formatlist.end())
153 formatlist.erase(it);
159 std::sort(formatlist.begin(), formatlist.end());
163 void Formats::setViewer(string const & name, string const & command)
166 FormatList::iterator it =
167 find_if(formatlist.begin(), formatlist.end(),
168 FormatNamesEqual(name));
169 if (it != formatlist.end())
170 it->setViewer(command);
174 bool Formats::view(Buffer const & buffer, string const & filename,
175 string const & format_name) const
177 if (filename.empty())
180 Format const * format = getFormat(format_name);
181 if (format && format->viewer().empty() &&
182 format->isChildFormat())
183 format = getFormat(format->parentFormat());
184 if (!format || format->viewer().empty()) {
185 // I believe this is the wrong place to show alerts, it should be done by
186 // the caller (this should be "utility" code)
187 Alert::error(_("Cannot view file"),
188 bformat(_("No information for viewing %1$s"),
189 prettyName(format_name)));
193 string command = format->viewer();
195 if (format_name == "dvi" &&
196 !lyxrc.view_dvi_paper_option.empty()) {
197 command += ' ' + lyxrc.view_dvi_paper_option;
198 string paper_size = buffer.params().paperSizeName();
199 if (paper_size == "letter")
201 command += ' ' + paper_size;
202 if (buffer.params().orientation == ORIENTATION_LANDSCAPE)
206 if (!contains(command, token_from))
207 command += ' ' + token_from;
209 command = subst(command, token_from,
210 QuoteName(OnlyFilename(filename)));
211 command = subst(command, token_path, QuoteName(OnlyPath(filename)));
212 command = subst(command, token_socket, QuoteName(lyxsocket->address()));
213 lyxerr[Debug::FILES] << "Executing command: " << command << std::endl;
214 buffer.message(_("Executing command: ") + command);
216 Path p(OnlyPath(filename));
218 int const res = one.startscript(Systemcall::DontWait, command);
221 Alert::error(_("Cannot view file"),
222 bformat(_("An error occurred whilst running %1$s"),
223 MakeDisplayPath(command, 50)));
230 bool Formats::edit(Buffer const & buffer, string const & filename,
231 string const & format_name) const
233 if (filename.empty())
236 Format const * format = getFormat(format_name);
237 if (format && format->editor().empty() &&
238 format->isChildFormat())
239 format = getFormat(format->parentFormat());
240 if (!format || format->editor().empty()) {
241 // I believe this is the wrong place to show alerts, it should be done by
242 // the caller (this should be "utility" code)
243 Alert::error(_("Cannot edit file"),
244 bformat(_("No information for editing %1$s"),
245 prettyName(format_name)));
249 string command = format->editor();
251 if (!contains(command, token_from))
252 command += ' ' + token_from;
254 command = subst(command, token_from,
255 QuoteName(OnlyFilename(filename)));
256 command = subst(command, token_path, QuoteName(OnlyPath(filename)));
257 command = subst(command, token_socket, QuoteName(lyxsocket->address()));
258 lyxerr[Debug::FILES] << "Executing command: " << command << std::endl;
259 buffer.message(_("Executing command: ") + command);
261 Path p(OnlyPath(filename));
263 int const res = one.startscript(Systemcall::DontWait, command);
266 Alert::error(_("Cannot edit file"),
267 bformat(_("An error occurred whilst running %1$s"),
268 MakeDisplayPath(command, 50)));
275 string const Formats::prettyName(string const & name) const
277 Format const * format = getFormat(name);
279 return format->prettyname();
285 string const Formats::extension(string const & name) const
287 Format const * format = getFormat(name);
289 return format->extension();
299 Formats system_formats;