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