]> git.lyx.org Git - lyx.git/blob - src/insets/InsetInfo.cpp
correct the gettext context for the format of the displayed error message for unknown...
[lyx.git] / src / insets / InsetInfo.cpp
1 /**
2  * \file InsetInfo.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Bo Peng
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10 #include <config.h>
11
12 #include "InsetInfo.h"
13
14 #include "Buffer.h"
15 #include "BufferParams.h"
16 #include "BufferView.h"
17 #include "CutAndPaste.h"
18 #include "FuncRequest.h"
19 #include "FuncStatus.h"
20 #include "InsetGraphics.h"
21 #include "InsetSpecialChar.h"
22 #include "KeyMap.h"
23 #include "LaTeXFeatures.h"
24 #include "LayoutFile.h"
25 #include "LyXAction.h"
26 #include "LyXRC.h"
27 #include "LyXVC.h"
28 #include "Lexer.h"
29 #include "ParagraphParameters.h"
30
31 #include "frontends/Application.h"
32
33 #include "support/debug.h"
34 #include "support/docstream.h"
35 #include "support/docstring_list.h"
36 #include "support/ExceptionMessage.h"
37 #include "support/FileName.h"
38 #include "support/filetools.h"
39 #include "support/gettext.h"
40 #include "support/lstrings.h"
41
42 #include <sstream>
43
44 using namespace std;
45 using namespace lyx::support;
46
47 namespace lyx {
48
49 namespace {
50
51 typedef Translator<InsetInfo::info_type, string> NameTranslator;
52
53 NameTranslator const initTranslator()
54 {       
55         NameTranslator translator(InsetInfo::UNKNOWN_INFO, "unknown");
56
57         translator.addPair(InsetInfo::SHORTCUTS_INFO, "shortcuts");
58         translator.addPair(InsetInfo::SHORTCUT_INFO, "shortcut");
59         translator.addPair(InsetInfo::LYXRC_INFO, "lyxrc");
60         translator.addPair(InsetInfo::PACKAGE_INFO, "package");
61         translator.addPair(InsetInfo::TEXTCLASS_INFO, "textclass");
62         translator.addPair(InsetInfo::MENU_INFO, "menu");
63         translator.addPair(InsetInfo::ICON_INFO, "icon");
64         translator.addPair(InsetInfo::BUFFER_INFO, "buffer");
65         translator.addPair(InsetInfo::LYX_INFO, "lyxinfo");
66
67         return translator;
68 }
69
70 /// The translator between the information type enum and corresponding string.
71 NameTranslator const & nameTranslator()
72 {
73         static NameTranslator const translator = initTranslator();
74         return translator;
75 }
76
77 } // namespace anon
78
79 /////////////////////////////////////////////////////////////////////////
80 //
81 // InsetInfo
82 //
83 /////////////////////////////////////////////////////////////////////////
84
85
86         
87 InsetInfo::InsetInfo(Buffer * buf, string const & name) 
88         : InsetCollapsable(buf), type_(UNKNOWN_INFO), name_()
89 {
90         setAutoBreakRows(true);
91         setInfo(name);
92         status_ = Collapsed;
93 }
94
95
96 Inset * InsetInfo::editXY(Cursor & cur, int x, int y)
97 {
98         // do not allow the cursor to be set in this Inset
99         return Inset::editXY(cur, x, y);
100 }
101
102
103 string InsetInfo::infoType() const
104 {
105         return nameTranslator().find(type_);
106 }
107
108
109 docstring InsetInfo::name() const 
110 {
111         return from_ascii("Info:" + infoType());
112 }
113
114
115 docstring InsetInfo::toolTip(BufferView const &, int, int) const
116 {
117         return bformat(_("Information regarding %1$s '%2$s'"),
118                         _(infoType()), from_utf8(name_));
119 }
120
121
122 void InsetInfo::read(Lexer & lex)
123 {
124         string token;
125         while (lex.isOK()) {
126                 lex.next();
127                 token = lex.getString();
128                 if (token == "type") {
129                         lex.next();
130                         token = lex.getString();
131                         type_ = nameTranslator().find(token);
132                 } else if (token == "arg") {
133                         lex.next(true);
134                         name_ = lex.getString();
135                 } else if (token == "\\end_inset")
136                         break;
137         }       
138         if (token != "\\end_inset") {
139                 lex.printError("Missing \\end_inset at this point");
140                 throw ExceptionMessage(WarningException,
141                         _("Missing \\end_inset at this point."),
142                         from_utf8(token));
143         }
144         updateInfo();
145 }
146
147
148 void InsetInfo::write(ostream & os) const
149 {
150         os << "Info\ntype  \"" << infoType() 
151            << "\"\narg   " << Lexer::quoteString(name_);
152 }
153
154
155 bool InsetInfo::validateModifyArgument(docstring const & arg) const
156 {
157         string type;
158         string const name = trim(split(to_utf8(arg), type, ' '));
159
160         switch (nameTranslator().find(type)) {
161         case UNKNOWN_INFO:
162                 return false;
163
164         case SHORTCUT_INFO:
165         case SHORTCUTS_INFO:
166         case MENU_INFO:
167         case ICON_INFO: {
168                 FuncRequest func = lyxaction.lookupFunc(name);
169                 return func.action() != LFUN_UNKNOWN_ACTION;
170         }
171
172         case LYXRC_INFO: {
173                 ostringstream oss;
174                 lyxrc.write(oss, true, name);
175                 return !oss.str().empty();
176         }
177
178         case PACKAGE_INFO:
179         case TEXTCLASS_INFO:
180                 return true;
181
182         case BUFFER_INFO:
183                 if (name == "name" || name == "path" || name == "class")
184                         return true;
185                 if (name == "vcs-revision" || name == "vcs-tree-revision" ||
186                        name == "vcs-author" || name == "vcs-date" || name == "vcs-time")
187                         return buffer().lyxvc().inUse();
188                 return false;
189
190         case LYX_INFO:
191                 return name == "version";
192         }
193
194         return false;
195 }
196
197
198 bool InsetInfo::showInsetDialog(BufferView * bv) const
199 {
200         bv->showDialog("info");
201         return true;
202 }
203
204
205 bool InsetInfo::getStatus(Cursor & cur, FuncRequest const & cmd,
206                 FuncStatus & flag) const
207 {
208         switch (cmd.action()) {
209         case LFUN_INSET_SETTINGS:
210                 return InsetCollapsable::getStatus(cur, cmd, flag);
211                 
212         case LFUN_INSET_DIALOG_UPDATE:
213         case LFUN_INSET_COPY_AS:
214                 flag.setEnabled(true);
215                 return true;
216                 
217         case LFUN_INSET_MODIFY:
218                 if (validateModifyArgument(cmd.argument())) {
219                         flag.setEnabled(true);
220                         return true;
221                 }
222                 //fall through
223                 
224         default:
225                 return false;
226         }
227 }
228
229
230 void InsetInfo::doDispatch(Cursor & cur, FuncRequest & cmd)
231 {
232         switch (cmd.action()) {
233         case LFUN_INSET_MODIFY:
234                 setInfo(to_utf8(cmd.argument()));
235                 break;
236
237         case LFUN_INSET_COPY_AS: {
238                 cap::clearSelection();
239                 Cursor copy(cur);
240                 copy.pushBackward(*this);
241                 copy.pit() = 0;
242                 copy.pos() = 0;
243                 copy.resetAnchor();
244                 copy.pit() = copy.lastpit();
245                 copy.pos() = copy.lastpos();
246                 copy.setSelection();
247                 cap::copySelection(copy);
248                 break;
249         }
250
251         default:
252                 InsetCollapsable::doDispatch(cur, cmd);
253                 break;
254         }
255 }
256
257
258 void InsetInfo::setInfo(string const & name)
259 {
260         if (name.empty())
261                 return;
262         // info_type name
263         string type;
264         name_ = trim(split(name, type, ' '));
265         type_ = nameTranslator().find(type);
266         updateInfo();
267 }
268
269
270 void InsetInfo::error(string const & err)
271 {
272         setText(bformat(_(err), from_utf8(name_)), 
273                 Font(inherit_font, buffer().params().language), false);
274 }
275
276
277 void InsetInfo::setText(docstring const & str)
278 {
279         setText(str, Font(inherit_font, buffer().params().language), false);
280 }
281
282
283 void InsetInfo::updateInfo()
284 {
285         BufferParams const & bp = buffer().params();    
286
287         switch (type_) {
288         case UNKNOWN_INFO:
289                 error("Unknown Info: %1$s");
290                 break;
291         case SHORTCUT_INFO:
292         case SHORTCUTS_INFO: {
293                 FuncRequest const func = lyxaction.lookupFunc(name_);
294                 if (func.action() == LFUN_UNKNOWN_ACTION) {
295                         error("Unknown action %1$s");
296                         break;
297                 }
298                 KeyMap::Bindings bindings = theTopLevelKeymap().findBindings(func);
299                 if (bindings.empty()) {
300                         // It is impropriate to use error() for undefined shortcut
301                         setText(_("undefined"));
302                         break;
303                 }
304                 if (type_ == SHORTCUT_INFO)
305                         setText(bindings.begin()->print(KeySequence::Portable));
306                 else
307                         setText(theTopLevelKeymap().printBindings(func, KeySequence::Portable));
308                 break;
309         }
310         case LYXRC_INFO: {
311                 ostringstream oss;
312                 if (name_.empty()) {
313                         setText(_("undefined"));
314                         break;
315                 }
316                 lyxrc.write(oss, true, name_);
317                 string result = oss.str();
318                 if (result.size() < 2) {
319                         setText(_("undefined"));
320                         break;
321                 }
322                 string::size_type loc = result.rfind("\n", result.size() - 2);
323                 loc = loc == string::npos ? 0 : loc + 1;
324                 if (result.size() < loc + name_.size() + 1
325                           || result.substr(loc + 1, name_.size()) != name_) {
326                         setText(_("undefined"));
327                         break;
328                 }
329                 // remove leading comments and \\name and space
330                 result = result.substr(loc + name_.size() + 2);
331                 
332                 // remove \n and ""
333                 result = rtrim(result, "\n");
334                 result = trim(result, "\"");
335                 setText(from_utf8(result));
336                 break;
337         }
338         case PACKAGE_INFO:
339                 // check in packages.lst
340                 setText(LaTeXFeatures::isAvailable(name_) ? _("yes") : _("no"));
341                 break;
342
343         case TEXTCLASS_INFO: {
344                 // name_ is the class name
345                 LayoutFileList const & list = LayoutFileList::get();
346                 bool available = false;
347                 if (list.haveClass(name_))
348                         available = list[name_].isTeXClassAvailable();
349                 setText(available ? _("yes") : _("no"));
350                 break;
351         }
352         case MENU_INFO: {
353                 docstring_list names;
354                 FuncRequest const func = lyxaction.lookupFunc(name_);
355                 if (func.action() == LFUN_UNKNOWN_ACTION) {
356                         error("Unknown action %1$s");
357                         break;
358                 }
359                 // iterate through the menubackend to find it
360                 if (!theApp()->searchMenu(func, names)) {
361                         error("No menu entry for action %1$s");
362                         break;
363                 }
364                 // if found, return its path.
365                 clear();
366                 Paragraph & par = paragraphs().front();
367                 Font const f(inherit_font, buffer().params().language);
368                 //Font fu = f;
369                 //fu.fontInfo().setUnderbar(FONT_ON);
370                 docstring_list::const_iterator beg = names.begin();
371                 docstring_list::const_iterator end = names.end();
372                 for (docstring_list::const_iterator it = beg ; 
373                      it != end ; ++it) {
374                         // do not insert > for the top level menu item
375                         if (it != beg)
376                                 par.insertInset(par.size(), new InsetSpecialChar(InsetSpecialChar::MENU_SEPARATOR),
377                                                 Change(Change::UNCHANGED));
378                         //FIXME: add proper underlines here. This
379                         // involves rewriting searchMenu used above to
380                         // return a vector of menus. If we do not do
381                         // that, we might as well use below
382                         // Paragraph::insert on each string (JMarc)
383                         for (size_type i = 0; i != it->length(); ++i)
384                                 par.insertChar(par.size(), (*it)[i], 
385                                                f, Change(Change::UNCHANGED));
386                 }
387                 break;
388         }
389         case ICON_INFO: {
390                 FuncRequest func = lyxaction.lookupFunc(name_);
391                 docstring icon_name = theApp()->iconName(func, true);
392                 //FIXME: We should use the icon directly instead of
393                 // going through FileName. The code below won't work
394                 // if the icon is embedded in the executable through
395                 // the Qt resource system.
396                 FileName file(to_utf8(icon_name));
397                 if (!file.exists())
398                         break;
399                 InsetGraphics * inset = new InsetGraphics(buffer_);
400                 InsetGraphicsParams igp;
401                 igp.filename = file;
402                 inset->setParams(igp);
403                 clear();
404                 paragraphs().front().insertInset(0, inset, 
405                                                  Change(Change::UNCHANGED));
406                 break;
407         }
408         case BUFFER_INFO: {
409                 if (name_ == "name") {
410                         setText(from_utf8(buffer().fileName().onlyFileName()));
411                         break;
412                 }
413                 if (name_ == "path") {
414                         setText(from_utf8(buffer().filePath()));
415                         break;
416                 }
417                 if (name_ == "class") {
418                         setText(from_utf8(bp.documentClass().name()));
419                         break;
420                 }
421                 
422                 // everything that follows is for version control.
423                 // nothing that isn't version control should go below this line.
424                 if (!buffer().lyxvc().inUse()) {
425                         setText(_("No version control"));
426                         break;
427                 }
428                 LyXVC::RevisionInfo itype = LyXVC::Unknown;
429                 if (name_ == "vcs-revision")
430                         itype = LyXVC::File;
431                 else if (name_ == "vcs-tree-revision")
432                         itype = LyXVC::Tree;
433                 else if (name_ == "vcs-author")
434                         itype = LyXVC::Author;
435                 else if (name_ == "vcs-time")
436                         itype = LyXVC::Time;
437                 else if (name_ == "vcs-date")
438                         itype = LyXVC::Date;
439                 string binfo = buffer().lyxvc().revisionInfo(itype);
440                 if (binfo.empty()) {
441                         docstring fmt = _("%1$s unknown[[InsetInfo]]");
442                         setText(bformat(fmt, from_utf8(name_)));
443                 } else
444                         setText(from_utf8(binfo));
445                 break;
446         }
447         case LYX_INFO:
448                 if (name_ == "version")
449                         setText(from_ascii(PACKAGE_VERSION));
450                 break;
451         }
452 }
453
454
455 docstring InsetInfo::contextMenu(BufferView const &, int, int) const
456 {
457         return from_ascii("context-info");
458 }
459
460
461 } // namespace lyx