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
17 #include "lyx_cb.h" // for ShowMessage() ... to be removed?
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"
32 string const token_from("$$i");
33 string const token_path("$$p");
37 bool operator<(Format const & a, Format const & b)
39 // use the compare_ascii_no_case instead of compare_no_case,
40 // because in turkish, 'i' is not the lowercase version of 'I',
41 // and thus turkish locale breaks parsing of tags.
43 return compare_ascii_no_case(a.prettyname(), b.prettyname()) < 0;
46 Format::Format(string const & n, string const & e, string const & p,
47 string const & s, string const & v): name_(n),
55 bool Format::dummy() const
57 return extension().empty();
61 bool Format::isChildFormat() const
65 return isdigit(name_[name_.length() - 1]);
69 string const Format::parentFormat() const
71 return name_.substr(0, name_.length() - 1);
75 // This method should return a reference, and throw an exception
76 // if the format named name cannot be found (Lgb)
77 Format const * Formats::getFormat(string const & name) const
79 FormatList::const_iterator cit =
80 find_if(formatlist.begin(), formatlist.end(),
81 lyx::compare_memfun(&Format::name, name));
82 if (cit != formatlist.end())
89 int Formats::getNumber(string const & name) const
91 FormatList::const_iterator cit =
92 find_if(formatlist.begin(), formatlist.end(),
93 lyx::compare_memfun(&Format::name, name));
94 if (cit != formatlist.end())
95 return cit - formatlist.begin();
101 void Formats::add(string const & name)
103 if (!getFormat(name))
104 add(name, name, name, string());
108 void Formats::add(string const & name, string const & extension,
109 string const & prettyname, string const & shortcut)
111 FormatList::iterator it =
112 find_if(formatlist.begin(), formatlist.end(),
113 lyx::compare_memfun(&Format::name, name));
114 if (it == formatlist.end())
115 formatlist.push_back(Format(name, extension, prettyname,
118 string viewer = it->viewer();
119 *it = Format(name, extension, prettyname, shortcut, viewer);
124 void Formats::erase(string const & name)
126 FormatList::iterator it =
127 find_if(formatlist.begin(), formatlist.end(),
128 lyx::compare_memfun(&Format::name, name));
129 if (it != formatlist.end())
130 formatlist.erase(it);
136 std::sort(formatlist.begin(), formatlist.end());
140 void Formats::setViewer(string const & name, string const & command)
143 FormatList::iterator it =
144 find_if(formatlist.begin(), formatlist.end(),
145 lyx::compare_memfun(&Format::name, name));
146 if (it != formatlist.end())
147 it->setViewer(command);
151 bool Formats::view(Buffer const * buffer, string const & filename,
152 string const & format_name) const
154 if (filename.empty())
157 Format const * format = getFormat(format_name);
158 if (format && format->viewer().empty() &&
159 format->isChildFormat())
160 format = getFormat(format->parentFormat());
161 if (!format || format->viewer().empty()) {
162 // I believe this is the wrong place to show alerts, it should be done by
163 // the caller (this should be "utility" code
164 Alert::error(_("Cannot view file"),
165 bformat(_("No information for viewing %1$s"),
166 prettyName(format_name)));
170 string command = format->viewer();
172 if (format_name == "dvi" &&
173 !lyxrc.view_dvi_paper_option.empty()) {
174 command += ' ' + lyxrc.view_dvi_paper_option;
175 string paper_size = papersize(buffer);
176 if (paper_size == "letter")
178 command += ' ' + paper_size;
179 if (buffer->params.orientation
180 == BufferParams::ORIENTATION_LANDSCAPE)
184 if (!contains(command, token_from))
185 command += ' ' + token_from;
187 command = subst(command, token_from,
188 QuoteName(OnlyFilename(filename)));
189 command = subst(command, token_path, QuoteName(OnlyPath(filename)));
191 lyxerr[Debug::FILES] << "Executing command: " << command << std::endl;
192 ShowMessage(buffer, _("Executing command:"), command);
194 Path p(OnlyPath(filename));
196 int const res = one.startscript(Systemcall::DontWait, command);
199 Alert::error(_("Cannot view file"),
200 bformat(_("An error occurred whilst running %1$s"),
201 command.substr(0, 50)));
208 string const Formats::prettyName(string const & name) const
210 Format const * format = getFormat(name);
212 return format->prettyname();
218 string const Formats::extension(string const & name) const
220 Format const * format = getFormat(name);
222 return format->extension();
228 string const papersize(Buffer const * buffer)
230 char real_papersize = buffer->params.papersize;
231 if (real_papersize == BufferParams::PAPER_DEFAULT)
232 real_papersize = lyxrc.default_papersize;
234 switch (real_papersize) {
235 case BufferParams::PAPER_A3PAPER:
237 case BufferParams::PAPER_A4PAPER:
239 case BufferParams::PAPER_A5PAPER:
241 case BufferParams::PAPER_B5PAPER:
243 case BufferParams::PAPER_EXECUTIVEPAPER:
245 case BufferParams::PAPER_LEGALPAPER:
247 case BufferParams::PAPER_USLETTER:
256 Formats system_formats;