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