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 "buffer_funcs.h"
21 #include "frontends/Alert.h" //to be removed?
23 #include "support/lstrings.h"
24 #include "support/filetools.h"
25 #include "support/path.h"
26 #include "support/systemcall.h"
27 #include "support/lyxfunctional.h"
29 using namespace lyx::support;
34 string const token_from("$$i");
35 string const token_path("$$p");
39 bool operator<(Format const & a, Format const & b)
41 // use the compare_ascii_no_case instead of compare_no_case,
42 // because in turkish, 'i' is not the lowercase version of 'I',
43 // and thus turkish locale breaks parsing of tags.
45 return compare_ascii_no_case(a.prettyname(), b.prettyname()) < 0;
48 Format::Format(string const & n, string const & e, string const & p,
49 string const & s, string const & v): name_(n),
57 bool Format::dummy() const
59 return extension().empty();
63 bool Format::isChildFormat() const
67 return isdigit(name_[name_.length() - 1]);
71 string const Format::parentFormat() const
73 return name_.substr(0, name_.length() - 1);
77 // This method should return a reference, and throw an exception
78 // if the format named name cannot be found (Lgb)
79 Format const * Formats::getFormat(string const & name) const
81 FormatList::const_iterator cit =
82 find_if(formatlist.begin(), formatlist.end(),
83 lyx::compare_memfun(&Format::name, name));
84 if (cit != formatlist.end())
91 int Formats::getNumber(string const & name) const
93 FormatList::const_iterator cit =
94 find_if(formatlist.begin(), formatlist.end(),
95 lyx::compare_memfun(&Format::name, name));
96 if (cit != formatlist.end())
97 return cit - formatlist.begin();
103 void Formats::add(string const & name)
105 if (!getFormat(name))
106 add(name, name, name, string());
110 void Formats::add(string const & name, string const & extension,
111 string const & prettyname, string const & shortcut)
113 FormatList::iterator it =
114 find_if(formatlist.begin(), formatlist.end(),
115 lyx::compare_memfun(&Format::name, name));
116 if (it == formatlist.end())
117 formatlist.push_back(Format(name, extension, prettyname,
120 string viewer = it->viewer();
121 *it = Format(name, extension, prettyname, shortcut, viewer);
126 void Formats::erase(string const & name)
128 FormatList::iterator it =
129 find_if(formatlist.begin(), formatlist.end(),
130 lyx::compare_memfun(&Format::name, name));
131 if (it != formatlist.end())
132 formatlist.erase(it);
138 std::sort(formatlist.begin(), formatlist.end());
142 void Formats::setViewer(string const & name, string const & command)
145 FormatList::iterator it =
146 find_if(formatlist.begin(), formatlist.end(),
147 lyx::compare_memfun(&Format::name, name));
148 if (it != formatlist.end())
149 it->setViewer(command);
153 bool Formats::view(Buffer const * buffer, string const & filename,
154 string const & format_name) const
156 if (filename.empty())
159 Format const * format = getFormat(format_name);
160 if (format && format->viewer().empty() &&
161 format->isChildFormat())
162 format = getFormat(format->parentFormat());
163 if (!format || format->viewer().empty()) {
164 // I believe this is the wrong place to show alerts, it should be done by
165 // the caller (this should be "utility" code
166 Alert::error(_("Cannot view file"),
167 bformat(_("No information for viewing %1$s"),
168 prettyName(format_name)));
172 string command = format->viewer();
174 if (format_name == "dvi" &&
175 !lyxrc.view_dvi_paper_option.empty()) {
176 command += ' ' + lyxrc.view_dvi_paper_option;
177 string paper_size = buffer->params.paperSizeName();
178 if (paper_size == "letter")
180 command += ' ' + paper_size;
181 if (buffer->params.orientation
182 == ORIENTATION_LANDSCAPE)
186 if (!contains(command, token_from))
187 command += ' ' + token_from;
189 command = subst(command, token_from,
190 QuoteName(OnlyFilename(filename)));
191 command = subst(command, token_path, QuoteName(OnlyPath(filename)));
193 lyxerr[Debug::FILES] << "Executing command: " << command << std::endl;
194 buffer->message(_("Executing command: ") + command);
196 Path p(OnlyPath(filename));
198 int const res = one.startscript(Systemcall::DontWait, command);
201 Alert::error(_("Cannot view file"),
202 bformat(_("An error occurred whilst running %1$s"),
203 command.substr(0, 50)));
210 string const Formats::prettyName(string const & name) const
212 Format const * format = getFormat(name);
214 return format->prettyname();
220 string const Formats::extension(string const & name) const
222 Format const * format = getFormat(name);
224 return format->extension();
234 Formats system_formats;