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