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