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