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