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