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