]> git.lyx.org Git - lyx.git/blob - src/format.C
Alfredo's second patch
[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 #include "format.h"
13 #include "lyxrc.h"
14 #include "debug.h"
15 #include "lyx_cb.h" // for ShowMessage() ... to be removed?
16 #include "gettext.h"
17 #include "LString.h"
18 #include "support/BoostFormat.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
28
29 namespace {
30
31 string const token_from("$$i");
32 string const token_path("$$p");
33
34 } //namespace anon
35
36 bool operator<(Format const & a, Format const & b)
37 {
38         // use the compare_ascii_no_case instead of compare_no_case,
39         // because in turkish, 'i' is not the lowercase version of 'I',
40         // and thus turkish locale breaks parsing of tags.
41
42         return compare_ascii_no_case(a.prettyname(), b.prettyname()) < 0;
43 }
44
45 Format::Format(string const & n, string const & e, string const & p,
46        string const & s, string const & v): name_(n),
47                                             extension_(e),
48                                             prettyname_(p),
49                                             shortcut_(s),
50                                             viewer_(v)
51 {}
52
53
54 bool Format::dummy() const
55 {
56         return extension().empty();
57 }
58
59
60 bool Format::isChildFormat() const
61 {
62         if (name_.empty())
63                 return false;
64         return isdigit(name_[name_.length() - 1]);
65 }
66
67
68 string const Format::parentFormat() const
69 {
70         return name_.substr(0, name_.length() - 1);
71 }
72
73
74 // This method should return a reference, and throw an exception
75 // if the format named name cannot be found (Lgb)
76 Format const * Formats::getFormat(string const & name) const
77 {
78         FormatList::const_iterator cit =
79                 find_if(formatlist.begin(), formatlist.end(),
80                         lyx::compare_memfun(&Format::name, name));
81         if (cit != formatlist.end())
82                 return &(*cit);
83         else
84                 return 0;
85 }
86
87
88 int Formats::getNumber(string const & name) const
89 {
90         FormatList::const_iterator cit =
91                 find_if(formatlist.begin(), formatlist.end(),
92                         lyx::compare_memfun(&Format::name, name));
93         if (cit != formatlist.end())
94                 return cit - formatlist.begin();
95         else
96                 return -1;
97 }
98
99
100 void Formats::add(string const & name)
101 {
102         if (!getFormat(name))
103                 add(name, name, name, string());
104 }
105
106
107 void Formats::add(string const & name, string const & extension,
108                   string const & prettyname, string const & shortcut)
109 {
110         FormatList::iterator it =
111                 find_if(formatlist.begin(), formatlist.end(),
112                         lyx::compare_memfun(&Format::name, name));
113         if (it == formatlist.end())
114                 formatlist.push_back(Format(name, extension, prettyname,
115                                             shortcut, ""));
116         else {
117                 string viewer = it->viewer();
118                 *it = Format(name, extension, prettyname, shortcut, viewer);
119         }
120 }
121
122
123 void Formats::erase(string const & name)
124 {
125         FormatList::iterator it =
126                 find_if(formatlist.begin(), formatlist.end(),
127                         lyx::compare_memfun(&Format::name, name));
128         if (it != formatlist.end())
129                 formatlist.erase(it);
130 }
131
132
133 void Formats::sort()
134 {
135         std::sort(formatlist.begin(), formatlist.end());
136 }
137
138
139 void Formats::setViewer(string const & name, string const & command)
140 {
141         add(name);
142         FormatList::iterator it =
143                 find_if(formatlist.begin(), formatlist.end(),
144                         lyx::compare_memfun(&Format::name, name));
145         if (it != formatlist.end())
146                 it->setViewer(command);
147 }
148
149
150 bool Formats::view(Buffer const * buffer, string const & filename,
151                    string const & format_name) const
152 {
153         if (filename.empty())
154                 return false;
155
156         Format const * format = getFormat(format_name);
157         if (format && format->viewer().empty() &&
158             format->isChildFormat())
159                 format = getFormat(format->parentFormat());
160         if (!format || format->viewer().empty()) {
161 // I believe this is the wrong place to show alerts, it should be done by
162 // the caller (this should be "utility" code
163 #if USE_BOOST_FORMAT
164                 Alert::error(_("Cannot view file"),
165                              boost::io::str(boost::format(_("No information for viewing %1$s"))
166                            % prettyName(format_name)));
167 #else
168                 Alert::error(_("Cannot view file"),
169                              _("No information for viewing ")
170                              + prettyName(format_name));
171 #endif
172                            return false;
173         }
174
175         string command = format->viewer();
176
177         if (format_name == "dvi" &&
178             !lyxrc.view_dvi_paper_option.empty()) {
179                 command += ' ' + lyxrc.view_dvi_paper_option;
180                 string paper_size = papersize(buffer);
181                 if (paper_size == "letter")
182                         paper_size = "us";
183                 command += ' ' + paper_size;
184                 if (buffer->params.orientation
185                     == BufferParams::ORIENTATION_LANDSCAPE)
186                         command += 'r';
187         }
188
189         if (!contains(command, token_from))
190                 command += ' ' + token_from;
191
192         command = subst(command, token_from,
193                         QuoteName(OnlyFilename(filename)));
194         command = subst(command, token_path, QuoteName(OnlyPath(filename)));
195
196         lyxerr[Debug::FILES] << "Executing command: " << command << std::endl;
197         ShowMessage(buffer, _("Executing command:"), command);
198
199         Path p(OnlyPath(filename));
200         Systemcall one;
201         int const res = one.startscript(Systemcall::DontWait, command);
202
203         if (res) {
204 #if USE_BOOST_FORMAT
205                 Alert::error(_("Cannot view file"),
206                              boost::io::str(boost::format(_("An error occurred whilst running %1$s"))
207                            % command.substr(0, 50)));
208 #else
209                 Alert::error(_("Cannot view file"),
210                              _("An error occurred whilst running ")
211                              + command.substr(0, 50));
212 #endif
213                 return false;
214         }
215         return true;
216 }
217
218
219 string const Formats::prettyName(string const & name) const
220 {
221         Format const * format = getFormat(name);
222         if (format)
223                 return format->prettyname();
224         else
225                 return name;
226 }
227
228
229 string const Formats::extension(string const & name) const
230 {
231         Format const * format = getFormat(name);
232         if (format)
233                 return format->extension();
234         else
235                 return name;
236 }
237
238
239 string const papersize(Buffer const * buffer)
240 {
241         char real_papersize = buffer->params.papersize;
242         if (real_papersize == BufferParams::PAPER_DEFAULT)
243                 real_papersize = lyxrc.default_papersize;
244
245         switch (real_papersize) {
246         case BufferParams::PAPER_A3PAPER:
247                 return "a3";
248         case BufferParams::PAPER_A4PAPER:
249                 return "a4";
250         case BufferParams::PAPER_A5PAPER:
251                 return "a5";
252         case BufferParams::PAPER_B5PAPER:
253                 return "b5";
254         case BufferParams::PAPER_EXECUTIVEPAPER:
255                 return "foolscap";
256         case BufferParams::PAPER_LEGALPAPER:
257                 return "legal";
258         case BufferParams::PAPER_USLETTER:
259         default:
260                 return "letter";
261         }
262 }
263
264
265 Formats formats;
266
267 Formats system_formats;