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