]> git.lyx.org Git - lyx.git/blob - src/insets/InsetInfo.cpp
Do not use GUI language for shortcut info inset if no translation is available
[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  * \author Jürgen Spitzmüller
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11 #include <config.h>
12
13 #include "InsetInfo.h"
14 #include "LyX.h"
15 #include "Buffer.h"
16 #include "BufferParams.h"
17 #include "BufferView.h"
18 #include "CutAndPaste.h"
19 #include "Font.h"
20 #include "FuncRequest.h"
21 #include "FuncStatus.h"
22 #include "InsetGraphics.h"
23 #include "InsetSpecialChar.h"
24 #include "KeyMap.h"
25 #include "LaTeXFeatures.h"
26 #include "Language.h"
27 #include "LayoutFile.h"
28 #include "Length.h"
29 #include "LyXAction.h"
30 #include "LyXRC.h"
31 #include "LyXVC.h"
32 #include "Lexer.h"
33 #include "Paragraph.h"
34 #include "ParIterator.h"
35 #include "ParagraphParameters.h"
36 #include "version.h"
37
38 #include "frontends/Application.h"
39
40 #include "support/convert.h"
41 #include "support/debug.h"
42 #include "support/docstream.h"
43 #include "support/docstring_list.h"
44 #include "support/ExceptionMessage.h"
45 #include "support/FileName.h"
46 #include "support/filetools.h"
47 #include "support/gettext.h"
48 #include "support/Messages.h"
49 #include "support/lstrings.h"
50 #include "support/qstring_helpers.h"
51 #include "support/Translator.h"
52
53 #include <sstream>
54
55 #include <QtGui/QImage>
56 #include <QDate>
57 #include <QLocale>
58
59 using namespace std;
60 using namespace lyx::support;
61
62 namespace lyx {
63
64 namespace {
65
66 typedef Translator<InsetInfoParams::info_type, string> NameTranslator;
67
68 NameTranslator const initTranslator()
69 {
70         NameTranslator translator(InsetInfoParams::UNKNOWN_INFO, "unknown");
71
72         translator.addPair(InsetInfoParams::SHORTCUTS_INFO, "shortcuts");
73         translator.addPair(InsetInfoParams::SHORTCUT_INFO, "shortcut");
74         translator.addPair(InsetInfoParams::LYXRC_INFO, "lyxrc");
75         translator.addPair(InsetInfoParams::PACKAGE_INFO, "package");
76         translator.addPair(InsetInfoParams::TEXTCLASS_INFO, "textclass");
77         translator.addPair(InsetInfoParams::MENU_INFO, "menu");
78         translator.addPair(InsetInfoParams::L7N_INFO, "l7n");
79         translator.addPair(InsetInfoParams::ICON_INFO, "icon");
80         translator.addPair(InsetInfoParams::BUFFER_INFO, "buffer");
81         translator.addPair(InsetInfoParams::LYX_INFO, "lyxinfo");
82         translator.addPair(InsetInfoParams::VCS_INFO, "vcs");
83         translator.addPair(InsetInfoParams::DATE_INFO, "date");
84         translator.addPair(InsetInfoParams::MODDATE_INFO, "moddate");
85         translator.addPair(InsetInfoParams::FIXDATE_INFO, "fixdate");
86         translator.addPair(InsetInfoParams::TIME_INFO, "time");
87         translator.addPair(InsetInfoParams::MODTIME_INFO, "modtime");
88         translator.addPair(InsetInfoParams::FIXTIME_INFO, "fixtime");
89
90         return translator;
91 }
92
93 /// The translator between the information type enum and corresponding string.
94 NameTranslator const & nameTranslator()
95 {
96         static NameTranslator const translator = initTranslator();
97         return translator;
98 }
99
100
101 typedef Translator<InsetInfoParams::info_type, string> DefaultValueTranslator;
102
103 DefaultValueTranslator const initDVTranslator()
104 {
105         DefaultValueTranslator translator(InsetInfoParams::UNKNOWN_INFO, "");
106
107         translator.addPair(InsetInfoParams::SHORTCUTS_INFO, "info-insert");
108         translator.addPair(InsetInfoParams::SHORTCUT_INFO, "info-insert");
109         translator.addPair(InsetInfoParams::LYXRC_INFO, "user_name");
110         translator.addPair(InsetInfoParams::PACKAGE_INFO, "graphics");
111         translator.addPair(InsetInfoParams::TEXTCLASS_INFO, "article");
112         translator.addPair(InsetInfoParams::MENU_INFO, "info-insert");
113         translator.addPair(InsetInfoParams::L7N_INFO, "");
114         translator.addPair(InsetInfoParams::ICON_INFO, "info-insert");
115         translator.addPair(InsetInfoParams::BUFFER_INFO, "name-noext");
116         translator.addPair(InsetInfoParams::LYX_INFO, "version");
117         translator.addPair(InsetInfoParams::VCS_INFO, "revision");
118         translator.addPair(InsetInfoParams::DATE_INFO, "loclong");
119         translator.addPair(InsetInfoParams::MODDATE_INFO, "loclong");
120         translator.addPair(InsetInfoParams::FIXDATE_INFO, "loclong");
121         translator.addPair(InsetInfoParams::TIME_INFO, "long");
122         translator.addPair(InsetInfoParams::MODTIME_INFO, "long");
123         translator.addPair(InsetInfoParams::FIXTIME_INFO, "long");
124
125         return translator;
126 }
127
128 /// The translator between the information type enum and some sensible default value.
129 DefaultValueTranslator const & defaultValueTranslator()
130 {
131         static DefaultValueTranslator const translator = initDVTranslator();
132         return translator;
133 }
134
135 } // namespace
136
137
138 /////////////////////////////////////////////////////////////////////
139 //
140 // InsetInfoParams
141 //
142 ///////////////////////////////////////////////////////////////////////
143
144 InsetInfoParams infoparams;
145
146 namespace{
147 set<string> getTexFileList(string const & filename)
148 {
149         set<string> list;
150         FileName const file = libFileSearch(string(), filename);
151         if (file.empty())
152                 return list;
153
154         // FIXME Unicode.
155         vector<docstring> doclist =
156                 getVectorFromString(file.fileContents("UTF-8"), from_ascii("\n"));
157
158         // Normalise paths like /foo//bar ==> /foo/bar
159         for (auto doc : doclist) {
160                 doc = subst(doc, from_ascii("\r"), docstring());
161                 while (contains(doc, from_ascii("//")))
162                         doc = subst(doc, from_ascii("//"), from_ascii("/"));
163                 if (!doc.empty())
164                         list.insert(removeExtension(onlyFileName(to_utf8(doc))));
165         }
166
167         // remove duplicates
168         return list;
169 }
170
171 bool translateString(docstring const in, docstring & out, string const lcode)
172 {
173         out = translateIfPossible(in, lcode);
174         return in != out;
175 }
176 } // namespace anon
177
178
179 docstring InsetInfoParams::getDate(string const iname, QDate const date) const
180 {
181         QLocale loc;
182         if (lang)
183                 loc = QLocale(toqstr(lang->code()));
184         if (iname == "long")
185                 return qstring_to_ucs4(loc.toString(date, QLocale::LongFormat));
186         else if (iname == "short")
187                 return qstring_to_ucs4(loc.toString(date, QLocale::ShortFormat));
188         else if (iname == "ISO")
189                 return qstring_to_ucs4(date.toString(Qt::ISODate));
190         else if (iname == "loclong")
191                 return lang ? qstring_to_ucs4(loc.toString(date, toqstr(lang->dateFormat(0))))
192                             : _("No long date format (language unknown)!");
193         else if (iname == "locmedium")
194                 return lang ? qstring_to_ucs4(loc.toString(date, toqstr(lang->dateFormat(1))))
195                             : _("No medium date format (language unknown)!");
196         else if (iname == "locshort")
197                 return lang ? qstring_to_ucs4(loc.toString(date, toqstr(lang->dateFormat(2))))
198                                 : _("No short date format (language unknown)!");
199         else
200                 return qstring_to_ucs4(loc.toString(date, toqstr(iname)));
201 }
202
203
204 docstring InsetInfoParams::getTime(string const iname, QTime const time) const
205 {
206         QLocale loc;
207         if (lang)
208                 loc = QLocale(toqstr(lang->code()));
209         if (iname == "long")
210                 return qstring_to_ucs4(loc.toString(time, QLocale::LongFormat));
211         else if (iname == "short")
212                 return qstring_to_ucs4(loc.toString(time, QLocale::ShortFormat));
213         else if (iname == "ISO")
214                 return qstring_to_ucs4(time.toString(Qt::ISODate));
215         else
216                 return qstring_to_ucs4(loc.toString(time, toqstr(iname)));
217 }
218
219
220 vector<pair<string,docstring>> InsetInfoParams::getArguments(Buffer const * buf,
221                                                              string const & itype) const
222 {
223         vector<pair<string,docstring>> result;
224
225         switch (nameTranslator().find(itype)) {
226         case UNKNOWN_INFO:
227                 result.push_back(make_pair("invalid", _("Please select a valid type!")));
228                 break;
229
230         case SHORTCUT_INFO:
231         case SHORTCUTS_INFO:
232         case MENU_INFO:
233         case ICON_INFO: {
234                 result.push_back(make_pair("custom", _("Custom")));
235                 LyXAction::const_iterator fit = lyxaction.func_begin();
236                 LyXAction::const_iterator const fen = lyxaction.func_end();
237                 for (; fit != fen; ++fit) {
238                         string const lfun = fit->first;
239                         if (!lfun.empty())
240                                 result.push_back(make_pair(lfun, from_ascii(lfun)));
241                 }
242                 break;
243         }
244
245         case L7N_INFO:
246                 result.push_back(make_pair("custom", _("Custom")));
247                 break;
248
249         case LYXRC_INFO: {
250                 result.push_back(make_pair("custom", _("Custom")));
251                 set<string> rcs = lyxrc.getRCs();
252                 for (auto const & rc : rcs)
253                         result.push_back(make_pair(rc, from_ascii(rc)));
254                 break;
255         }
256
257         case PACKAGE_INFO:
258         case TEXTCLASS_INFO: {
259                 result.push_back(make_pair("custom", _("Custom")));
260                 string const filename = (itype == "package") ? "styFiles.lst"
261                                                             : "clsFiles.lst";
262                 set<string> flist = getTexFileList(filename);
263                 for (auto const & f : flist)
264                         result.push_back(make_pair(f, from_utf8(f)));
265                 break;
266         }
267
268         case BUFFER_INFO:
269                 result.push_back(make_pair("name", _("File name (with extension)")));
270                 result.push_back(make_pair("name-noext", _("File name (without extension)")));
271                 result.push_back(make_pair("path", _("File path")));
272                 result.push_back(make_pair("class", _("Used text class")));
273                 break;
274
275         case VCS_INFO: {
276                 if (!buf->lyxvc().inUse()) {
277                         result.push_back(make_pair("invalid", _("No version control!")));
278                         break;
279                 }
280                 result.push_back(make_pair("revision", _("Revision[[Version Control]]")));
281                 result.push_back(make_pair("tree-revision", _("Tree revision")));
282                 result.push_back(make_pair("author", _("Author")));
283                 result.push_back(make_pair("date", _("Date")));
284                 result.push_back(make_pair("time", _("Time[[of day]]")));
285                 break;
286         }
287
288         case LYX_INFO:
289                 result.push_back(make_pair("version", _("LyX version")));
290                 result.push_back(make_pair("layoutformat", _("LyX layout format")));
291                 break;
292
293         case FIXDATE_INFO:
294         case DATE_INFO:
295         case MODDATE_INFO: {
296                 string const dt = split(name, '@');
297                 QDate date;
298                 if (itype == "moddate")
299                         date = QDateTime::fromTime_t(buf->fileName().lastModified()).date();
300                 else if (itype == "fixdate" && !dt.empty()) {
301                         QDate const gdate = QDate::fromString(toqstr(dt), Qt::ISODate);
302                         date = (gdate.isValid()) ? gdate : QDate::currentDate();
303                 } else
304                         date = QDate::currentDate();
305                 result.push_back(make_pair("long",getDate("long", date)));
306                 result.push_back(make_pair("short", getDate("short", date)));
307                 result.push_back(make_pair("loclong", getDate("loclong", date)));
308                 result.push_back(make_pair("locmedium", getDate("locmedium", date)));
309                 result.push_back(make_pair("locshort", getDate("locshort", date)));
310                 result.push_back(make_pair("ISO", getDate("ISO", date)));
311                 result.push_back(make_pair("yyyy", getDate("yyyy", date)));
312                 result.push_back(make_pair("MMMM", getDate("MMMM", date)));
313                 result.push_back(make_pair("MMM", getDate("MMM", date)));
314                 result.push_back(make_pair("dddd", getDate("dddd", date)));
315                 result.push_back(make_pair("ddd", getDate("ddd", date)));
316                 result.push_back(make_pair("custom", _("Custom")));
317                 break;
318         }
319         case FIXTIME_INFO:
320         case TIME_INFO:
321         case MODTIME_INFO: {
322                 string const tt = split(name, '@');
323                 QTime time;
324                 if (itype == "modtime")
325                         time = QDateTime::fromTime_t(buf->fileName().lastModified()).time();
326                 else if (itype == "fixtime" && !tt.empty()) {
327                         QTime const gtime = QTime::fromString(toqstr(tt), Qt::ISODate);
328                         time = (gtime.isValid()) ? gtime : QTime::currentTime();
329                 } else
330                         time = QTime::currentTime();
331                 result.push_back(make_pair("long",getTime("long", time)));
332                 result.push_back(make_pair("short", getTime("short", time)));
333                 result.push_back(make_pair("ISO", getTime("ISO", time)));
334                 result.push_back(make_pair("custom", _("Custom")));
335                 break;
336         }
337         }
338
339         return result;
340 }
341
342
343 bool InsetInfoParams::validateArgument(Buffer const * buf, docstring const & arg,
344                                        bool const usedefaults) const
345 {
346         string type;
347         string name = trim(split(to_utf8(arg), type, ' '));
348         if (name.empty() && usedefaults)
349                 name = defaultValueTranslator().find(type);
350
351         switch (nameTranslator().find(type)) {
352         case UNKNOWN_INFO:
353                 return false;
354
355         case SHORTCUT_INFO:
356         case SHORTCUTS_INFO:
357         case MENU_INFO: {
358                 FuncRequest func = lyxaction.lookupFunc(name);
359                 return func.action() != LFUN_UNKNOWN_ACTION;
360         }
361
362         case L7N_INFO:
363                 return !name.empty();
364
365         case ICON_INFO: {
366                 FuncCode const action = lyxaction.lookupFunc(name).action();
367                 if (action == LFUN_UNKNOWN_ACTION) {
368                         string dir = "images";
369                         return !imageLibFileSearch(dir, name, "svgz,png").empty();
370                 }
371                 return true;
372         }
373
374         case LYXRC_INFO: {
375                 set<string> rcs = lyxrc.getRCs();
376                 return rcs.find(name) != rcs.end();
377         }
378
379         case PACKAGE_INFO:
380         case TEXTCLASS_INFO:
381                 return true;
382
383         case BUFFER_INFO:
384                 return (name == "name" || name == "name-noext"
385                         || name == "path" || name == "class");
386
387         case VCS_INFO:
388                 if (name == "revision" || name == "tree-revision"
389                     || name == "author" || name == "date" || name == "time")
390                         return buf->lyxvc().inUse();
391                 return false;
392
393         case LYX_INFO:
394                 return name == "version" || name == "layoutformat";
395
396         case FIXDATE_INFO: {
397                 string date;
398                 string piece;
399                 date = split(name, piece, '@');
400                 if (!date.empty() && !QDate::fromString(toqstr(date), Qt::ISODate).isValid())
401                         return false;
402                 if (!piece.empty())
403                         name = piece;
404         }
405         // fall through
406         case DATE_INFO:
407         case MODDATE_INFO: {
408                 if (name == "long" || name == "short" || name == "ISO")
409                         return true;
410                 else {
411                         QDate date = QDate::currentDate();
412                         return !date.toString(toqstr(name)).isEmpty();
413                 }
414         }
415         case FIXTIME_INFO: {
416                 string time;
417                 string piece;
418                 time = split(name, piece, '@');
419                 if (!time.empty() && !QTime::fromString(toqstr(time), Qt::ISODate).isValid())
420                         return false;
421                 if (!piece.empty())
422                         name = piece;
423         }
424         // fall through
425         case TIME_INFO:
426         case MODTIME_INFO: {
427                 if (name == "long" || name == "short" || name == "ISO")
428                         return true;
429                 else {
430                         QTime time = QTime::currentTime();
431                         return !time.toString(toqstr(name)).isEmpty();
432                 }
433         }
434         }
435
436         return false;
437 }
438
439
440
441
442 string InsetInfoParams::infoType() const
443 {
444         return nameTranslator().find(type);
445 }
446
447
448
449 /////////////////////////////////////////////////////////////////////////
450 //
451 // InsetInfo
452 //
453 /////////////////////////////////////////////////////////////////////////
454
455
456
457 InsetInfo::InsetInfo(Buffer * buf, string const & name)
458         : InsetCollapsible(buf), initialized_(false)
459 {
460         params_.type = InsetInfoParams::UNKNOWN_INFO;
461         params_.force_ltr = false;
462         setInfo(name);
463         status_ = Collapsed;
464 }
465
466
467 Inset * InsetInfo::editXY(Cursor & cur, int x, int y)
468 {
469         // do not allow the cursor to be set in this Inset
470         return Inset::editXY(cur, x, y);
471 }
472
473
474 docstring InsetInfo::layoutName() const
475 {
476         return from_ascii("Info:" + params_.infoType());
477 }
478
479
480 docstring InsetInfo::toolTip(BufferView const &, int, int) const
481 {
482         docstring result;
483         switch (nameTranslator().find(params_.infoType())) {
484         case InsetInfoParams::UNKNOWN_INFO:
485                 result = _("Invalid information inset");
486                 break;
487         case InsetInfoParams::SHORTCUT_INFO:
488                 result = bformat(_("The keybard shortcut for the function '%1$s'"),
489                                 from_utf8(params_.name));
490                 break;
491         case InsetInfoParams::SHORTCUTS_INFO:
492                 result = bformat(_("The keybard shortcuts for the function '%1$s'"),
493                                 from_utf8(params_.name));
494                 break;
495         case InsetInfoParams::MENU_INFO: 
496                 result = bformat(_("The menu location for the function '%1$s'"),
497                                 from_utf8(params_.name));
498                 break;
499         case InsetInfoParams::L7N_INFO: 
500                 result = bformat(_("The localization for the string '%1$s'"),
501                                 from_utf8(params_.name));
502                 break;
503         case InsetInfoParams::ICON_INFO:
504                 result = bformat(_("The toolbar icon for the function '%1$s'"),
505                                 from_utf8(params_.name));
506                 break;
507         case InsetInfoParams::LYXRC_INFO:
508                 result = bformat(_("The preference setting for the preference key '%1$s'"),
509                                 from_utf8(params_.name));
510                 break;
511         case InsetInfoParams::PACKAGE_INFO:
512                 result = bformat(_("Availability of the LaTeX package '%1$s'"),
513                                 from_utf8(params_.name));
514                 break;
515         case InsetInfoParams::TEXTCLASS_INFO:
516                 result = bformat(_("Availability of the LaTeX class '%1$s'"),
517                                 from_utf8(params_.name));
518                 break;
519         case InsetInfoParams::BUFFER_INFO:
520                 if (params_.name == "name")
521                         result = _("The name of this file (incl. extension)");
522                 else if (params_.name == "name-noext")
523                         result = _("The name of this file (without extension)");
524                 else if (params_.name == "path")
525                         result = _("The path where this file is saved");
526                 else if (params_.name == "class")
527                         result = _("The class this document uses");
528                 break;
529         case InsetInfoParams::VCS_INFO:
530                 if (params_.name == "revision")
531                         result = _("Version control revision");
532                 else if (params_.name == "tree-revision")
533                         result = _("Version control tree revision");
534                 else if (params_.name == "author")
535                          result = _("Version control author");
536                 else if (params_.name == "date")
537                         result = _("Version control date");
538                 else if (params_.name == "time")
539                         result = _("Version control time");
540                 break;
541         case InsetInfoParams::LYX_INFO:
542                 if (params_.name == "version")
543                         result = _("The current LyX version");
544                 else if (params_.name == "layoutformat")
545                         result = _("The current LyX layout format");
546                 break;
547         case InsetInfoParams::DATE_INFO:
548                 result = _("The current date");
549                 break;
550         case InsetInfoParams::MODDATE_INFO:
551                 result = _("The date of last save");
552                 break;
553         case InsetInfoParams::FIXDATE_INFO:
554                 result = _("A static date");
555                 break;
556         case InsetInfoParams::TIME_INFO:
557                 result = _("The current time");
558                 break;
559         case InsetInfoParams::MODTIME_INFO:
560                 result = _("The time of last save");
561                 break;
562         case InsetInfoParams::FIXTIME_INFO:
563                 result = _("A static time");
564                 break;
565         }
566
567         return result;
568 }
569
570
571 void InsetInfo::read(Lexer & lex)
572 {
573         string token;
574         while (lex.isOK()) {
575                 lex.next();
576                 token = lex.getString();
577                 if (token == "type") {
578                         lex.next();
579                         token = lex.getString();
580                         params_.type = nameTranslator().find(token);
581                 } else if (token == "arg") {
582                         lex.next(true);
583                         params_.name = lex.getString();
584                 } else if (token == "\\end_inset")
585                         break;
586         }
587         if (token != "\\end_inset") {
588                 lex.printError("Missing \\end_inset at this point");
589                 throw ExceptionMessage(WarningException,
590                         _("Missing \\end_inset at this point."),
591                         from_utf8(token));
592         }
593 }
594
595
596 void InsetInfo::write(ostream & os) const
597 {
598         os << "Info\ntype  \"" << params_.infoType()
599            << "\"\narg   " << Lexer::quoteString(params_.name);
600 }
601
602
603 bool InsetInfo::showInsetDialog(BufferView * bv) const
604 {
605         bv->showDialog("info");
606         return true;
607 }
608
609
610 bool InsetInfo::getStatus(Cursor & cur, FuncRequest const & cmd,
611                 FuncStatus & flag) const
612 {
613         switch (cmd.action()) {
614         case LFUN_INSET_SETTINGS:
615                 return InsetCollapsible::getStatus(cur, cmd, flag);
616
617         case LFUN_INSET_DIALOG_UPDATE:
618         case LFUN_INSET_COPY_AS:
619         case LFUN_INSET_DISSOLVE:
620                 flag.setEnabled(true);
621                 return true;
622
623         case LFUN_INSET_MODIFY:
624                 if (params_.validateArgument(&buffer(), cmd.argument())) {
625                         flag.setEnabled(true);
626                         string typestr;
627                         string name = trim(split(to_utf8(cmd.argument()), typestr, ' '));
628                         InsetInfoParams::info_type type = nameTranslator().find(typestr);
629                         string origname = params_.name;
630                         if (type == InsetInfoParams::FIXDATE_INFO
631                             || type == InsetInfoParams::FIXTIME_INFO)
632                                 split(params_.name, origname, '@');
633                         flag.setOnOff(type == params_.type && name == origname);
634                         return true;
635                 }
636                 //fall through
637
638         default:
639                 return false;
640         }
641 }
642
643
644 void InsetInfo::doDispatch(Cursor & cur, FuncRequest & cmd)
645 {
646         switch (cmd.action()) {
647         case LFUN_INSET_MODIFY:
648                 cur.recordUndo();
649                 setInfo(to_utf8(cmd.argument()));
650                 cur.forceBufferUpdate();
651                 initialized_ = false;
652                 break;
653
654         case LFUN_INSET_COPY_AS: {
655                 cap::clearSelection();
656                 Cursor copy(cur);
657                 copy.pushBackward(*this);
658                 copy.pit() = 0;
659                 copy.pos() = 0;
660                 copy.resetAnchor();
661                 copy.pit() = copy.lastpit();
662                 copy.pos() = copy.lastpos();
663                 copy.setSelection();
664                 cap::copySelection(copy);
665                 break;
666         }
667
668         default:
669                 InsetCollapsible::doDispatch(cur, cmd);
670                 break;
671         }
672 }
673
674
675 void InsetInfo::setInfo(string const & name)
676 {
677         if (name.empty())
678                 return;
679
680         string saved_date_specifier;
681         // Store old date specifier for potential re-use
682         if (!params_.name.empty())
683                 saved_date_specifier = split(params_.name, '@');
684         // info_type name
685         string type;
686         params_.name = trim(split(name, type, ' '));
687         params_.type = nameTranslator().find(type);
688         if (params_.name.empty())
689                 params_.name = defaultValueTranslator().find(params_.type);
690         if (params_.type == InsetInfoParams::FIXDATE_INFO) {
691                 string const date_specifier = split(params_.name, '@');
692                 // If an explicit new fix date is specified, use that
693                 // Otherwise, use the old one or, if there is none,
694                 // the current date
695                 if (date_specifier.empty()) {
696                         if (saved_date_specifier.empty())
697                                 params_.name += "@" + fromqstr(QDate::currentDate().toString(Qt::ISODate));
698                         else
699                                 params_.name += "@" + saved_date_specifier;
700                 }
701         }
702         else if (params_.type == InsetInfoParams::FIXTIME_INFO) {
703                 string const time_specifier = split(params_.name, '@');
704                 // If an explicit new fix time is specified, use that
705                 // Otherwise, use the old one or, if there is none,
706                 // the current time
707                 if (time_specifier.empty()) {
708                         if (saved_date_specifier.empty())
709                                 params_.name += "@" + fromqstr(QTime::currentTime().toString(Qt::ISODate));
710                         else
711                                 params_.name += "@" + saved_date_specifier;
712                 }
713         }
714 }
715
716
717 void InsetInfo::error(docstring const & err, Language const * lang)
718 {
719         docstring const res = translateIfPossible(err, lang->code());
720         bool const translated = res != err;
721         // If the string is not translated, we use default lang (English)
722         Font const f = translated ? Font(inherit_font, lang) : Font(inherit_font);
723         setText(bformat(res, from_utf8(params_.name)), f, false);
724 }
725
726
727 void InsetInfo::info(docstring const & err, Language const * lang)
728 {
729         docstring const res = translateIfPossible(err, lang->code());
730         bool const translated = res != err;
731         // If the string is not translated, we use default lang (English)
732         Font const f = translated ? Font(inherit_font, lang) : Font(inherit_font);
733         setText(translateIfPossible(err, lang->code()), f, false);
734 }
735
736
737 void InsetInfo::setText(docstring const & str, Language const * lang)
738 {
739         setText(str, Font(inherit_font, lang), false);
740 }
741
742
743 bool InsetInfo::forceLTR() const
744 {
745         return params_.force_ltr;
746 }
747
748
749 void InsetInfo::updateBuffer(ParIterator const & it, UpdateType utype) {
750         // If the Buffer is a clone, then we neither need nor want to do any
751         // of what follows. We want, rather, just to inherit how things were
752         // in the original Buffer. This is especially important for VCS.
753         // Otherwise, we could in principle have different settings here
754         // than in the Buffer we were exporting.
755         if (buffer().isClone())
756                 return;
757
758         BufferParams const & bp = buffer().params();
759         params_.lang = it.paragraph().getFontSettings(bp, it.pos()).language();
760         Language const * tryguilang = languages.getFromCode(Messages::guiLanguage());
761         // Some info insets use the language of the GUI (if available)
762         Language const * guilang = tryguilang ? tryguilang : params_.lang;
763
764         params_.force_ltr = !params_.lang->rightToLeft();
765         // This is just to get the string into the po files
766         docstring gui;
767         switch (params_.type) {
768         case InsetInfoParams::UNKNOWN_INFO:
769                 gui = _("Unknown Info!");
770                 info(from_ascii("Unknown Info!"), params_.lang);
771                 initialized_ = false;
772                 break;
773         case InsetInfoParams::SHORTCUT_INFO:
774         case InsetInfoParams::SHORTCUTS_INFO: {
775                 // shortcuts can change, so we need to re-do this each time
776                 FuncRequest const func = lyxaction.lookupFunc(params_.name);
777                 if (func.action() == LFUN_UNKNOWN_ACTION) {
778                         gui = _("Unknown action %1$s");
779                         error(from_ascii("Unknown action %1$s"), params_.lang);
780                         break;
781                 }
782                 KeyMap::Bindings bindings = theTopLevelKeymap().findBindings(func);
783                 if (bindings.empty()) {
784                         gui = _("undefined");
785                         info(from_ascii("undefined"), params_.lang);
786                         break;
787                 }
788                 docstring sequence;
789                 docstring seq_untranslated;
790                 if (params_.type == InsetInfoParams::SHORTCUT_INFO) {
791                         sequence = bindings.begin()->print(KeySequence::ForGui);
792                         seq_untranslated = bindings.begin()->print(KeySequence::ForGui, true);
793                 } else {
794                         sequence = theTopLevelKeymap().printBindings(func, KeySequence::ForGui);
795                         seq_untranslated = theTopLevelKeymap().printBindings(func, KeySequence::ForGui, true);
796                 }
797                 // QKeySequence returns special characters for keys on the mac
798                 // Since these are not included in many fonts, we
799                 // re-translate them to textual names (see #10641)
800                 odocstringstream ods;
801                 string const lcode = params_.lang->code();
802                 docstring trans;
803                 bool is_translated = sequence != seq_untranslated;
804                 for (size_t n = 0; n < sequence.size(); ++n) {
805                         char_type const c = sequence[n];
806                         switch(c) {
807                         case 0x21b5://Return
808                                 gui = _("Return[[Key]]");
809                                 is_translated = translateString(from_ascii("Return[[Key]]"), trans, lcode);
810                                 ods << trans;
811                                 break;
812                         case 0x21b9://Tab both directions (Win)
813                                 gui = _("Tab[[Key]]");
814                                 is_translated = translateString(from_ascii("Tab[[Key]]"), trans, lcode);
815                                 ods << trans;
816                                 break;
817                         case 0x21de://Qt::Key_PageUp
818                                 gui = _("PgUp");
819                                 is_translated = translateString(from_ascii("PgUp"), trans, lcode);
820                                 ods << trans;
821                                 break;
822                         case 0x21df://Qt::Key_PageDown
823                                 gui = _("PgDown");
824                                 is_translated = translateString(from_ascii("PgDown"), trans, lcode);
825                                 ods << trans;
826                                 break;
827                         case 0x21e4://Qt::Key_Backtab
828                                 gui = _("Backtab");
829                                 is_translated = translateString(from_ascii("Backtab"), trans, lcode);
830                                 ods << trans;
831                                 break;
832                         case 0x21e5://Qt::Key_Tab
833                                 gui = _("Tab");
834                                 is_translated = translateString(from_ascii("Tab"), trans, lcode);
835                                 ods << trans;
836                                 break;
837                         case 0x21e7://Shift
838                                 gui = _("Shift");
839                                 is_translated = translateString(from_ascii("Shift"), trans, lcode);
840                                 ods << trans;
841                                 break;
842                         case 0x21ea://Qt::Key_CapsLock
843                                 gui = _("CapsLock");
844                                 is_translated = translateString(from_ascii("CapsLock"), trans, lcode);
845                                 ods << trans;
846                                 break;
847                         case 0x2303://Control
848                                 gui = _("Control[[Key]]");
849                                 is_translated = translateString(from_ascii("Control[[Key]]"), trans, lcode);
850                                 ods << trans;
851                                 break;
852                         case 0x2318://CMD
853                                 gui = _("Command[[Key]]");
854                                 is_translated = translateString(from_ascii("Command[[Key]]"), trans, lcode);
855                                 ods << trans;
856                                 break;
857                         case 0x2324://Qt::Key_Enter
858                                 gui = _("Return[[Key]]");
859                                 is_translated = translateString(from_ascii("Return[[Key]]"), trans, lcode);
860                                 ods << trans;
861                                 break;
862                         case 0x2325://Option key
863                                 gui = _("Option[[Key]]");
864                                 is_translated = translateString(from_ascii("Option[[Key]]"), trans, lcode);
865                                 ods << trans;
866                                 break;
867                         case 0x2326://Qt::Key_Delete
868                                 gui = _("Delete[[Key]]");
869                                 is_translated = translateString(from_ascii("Delete[[Key]]"), trans, lcode);
870                                 ods << trans;
871                                 break;
872                         case 0x232b://Qt::Key_Backspace
873                                 gui = _("Fn+Del");
874                                 is_translated = translateString(from_ascii("Fn+Del"), trans, lcode);
875                                 ods << trans;
876                                 break;
877                         case 0x238b://Qt::Key_Escape
878                                 gui = _("Esc");
879                                 is_translated = translateString(from_ascii("Esc"), trans, lcode);
880                                 ods << trans;
881                                 break;
882                         default:
883                                 ods.put(c);
884                         }
885                 }
886                 setText(ods.str(), is_translated ? guilang : nullptr);
887                 params_.force_ltr = !is_translated || (!guilang->rightToLeft() && !params_.lang->rightToLeft());
888                 break;
889         }
890         case InsetInfoParams::LYXRC_INFO: {
891                 // this information could change, if the preferences are changed,
892                 // so we will recalculate each time through.
893                 ostringstream oss;
894                 if (params_.name.empty()) {
895                         gui = _("undefined");
896                         info(from_ascii("undefined"), params_.lang);
897                         break;
898                 }
899                 // FIXME this uses the serialization mechanism to get the info
900                 // we want, which i guess works but is a bit strange.
901                 lyxrc.write(oss, true, params_.name);
902                 string result = oss.str();
903                 if (result.size() < 2) {
904                         gui = _("undefined");
905                         info(from_ascii("undefined"), params_.lang);
906                         break;
907                 }
908                 string::size_type loc = result.rfind("\n", result.size() - 2);
909                 loc = loc == string::npos ? 0 : loc + 1;
910                 if (result.size() < loc + params_.name.size() + 1
911                           || result.substr(loc + 1, params_.name.size()) != params_.name) {
912                         gui = _("undefined");
913                         info(from_ascii("undefined"), params_.lang);
914                         break;
915                 }
916                 // remove leading comments and \\name and space
917                 result = result.substr(loc + params_.name.size() + 2);
918
919                 // remove \n and ""
920                 result = rtrim(result, "\n");
921                 result = trim(result, "\"");
922                 gui = _("not set");
923                 if (result.empty())
924                         result = "not set";
925                 setText(from_utf8(result), params_.lang);
926                 break;
927         }
928         case InsetInfoParams::PACKAGE_INFO:
929                 // only need to do this once.
930                 if (initialized_)
931                         break;
932                 // check in packages.lst
933                 if (LaTeXFeatures::isAvailable(params_.name)) {
934                         gui = _("yes");
935                         info(from_ascii("yes"), params_.lang);
936                 } else {
937                         gui = _("no");
938                         info(from_ascii("no"), params_.lang);
939                 }
940                 initialized_ = true;
941                 break;
942
943         case InsetInfoParams::TEXTCLASS_INFO: {
944                 // the TextClass can change
945                 LayoutFileList const & list = LayoutFileList::get();
946                 bool available = false;
947                 // params_.name is the class name
948                 if (list.haveClass(params_.name))
949                         available = list[params_.name].isTeXClassAvailable();
950                 if (available) {
951                         gui = _("yes");
952                         info(from_ascii("yes"), params_.lang);
953                 } else {
954                         gui = _("no");
955                         info(from_ascii("no"), params_.lang);
956                 }
957                 break;
958         }
959         case InsetInfoParams::MENU_INFO: {
960                 // only need to do this once.
961                 if (initialized_)
962                         break;
963                 docstring_list names;
964                 FuncRequest func = lyxaction.lookupFunc(params_.name);
965                 if (func.action() == LFUN_UNKNOWN_ACTION) {
966                         gui = _("Unknown action %1$s");
967                         error(from_ascii("Unknown action %1$s"), params_.lang);
968                         break;
969                 }
970                 if (func.action() == LFUN_BUFFER_VIEW || func.action() == LFUN_BUFFER_UPDATE)
971                         // The default output format is in the menu without argument,
972                         // so strip it here.
973                         if (func.argument() == from_ascii(buffer().params().getDefaultOutputFormat()))
974                                 func = FuncRequest(func.action());
975                 // iterate through the menubackend to find it
976                 if (!theApp()) {
977                         gui = _("Can't determine menu entry for action %1$s in batch mode");
978                         error(from_ascii("Can't determine menu entry for action %1$s in batch mode"), params_.lang);
979                         initialized_ = true;
980                         break;
981                 }
982                 // and we will not keep trying if we fail
983                 initialized_ = theApp()->hasBufferView();
984                 if (!theApp()->searchMenu(func, names)) {
985                         gui = _("No menu entry for action %1$s");
986                         error(from_ascii("No menu entry for action %1$s"), params_.lang);
987                         break;
988                 }
989                 // if found, return its path.
990                 clear();
991                 Paragraph & par = paragraphs().front();
992                 Font const f(inherit_font, guilang);
993                 params_.force_ltr = !guilang->rightToLeft();
994                 //Font fu = f;
995                 //fu.fontInfo().setUnderbar(FONT_ON);
996                 for (docstring const & name : names) {
997                         // do not insert > for the top level menu item
998                         if (&name != &names.front())
999                                 par.insertInset(par.size(), new InsetSpecialChar(InsetSpecialChar::MENU_SEPARATOR),
1000                                                 f, Change(Change::UNCHANGED));
1001                         //FIXME: add proper underlines here. This
1002                         // involves rewriting searchMenu used above to
1003                         // return a vector of menus. If we do not do
1004                         // that, we might as well use below
1005                         // Paragraph::insert on each string (JMarc)
1006                         for (char_type c : name)
1007                                 par.insertChar(par.size(), c, f, Change(Change::UNCHANGED));
1008                 }
1009                 break;
1010         }
1011         case InsetInfoParams::L7N_INFO: {
1012                 docstring locstring = _(params_.name);
1013                 // Remove trailing colons
1014                 locstring = rtrim(locstring, ":");
1015                 // Remove menu accelerators
1016                 if (contains(locstring, from_ascii("|"))) {
1017                         docstring nlocstring;
1018                         rsplit(locstring, nlocstring, '|');
1019                         locstring = nlocstring;
1020                 }
1021                 // Remove Qt accelerators, but keep literal ampersands
1022                 locstring = subst(locstring, from_ascii(" & "), from_ascii("</amp;>"));
1023                 locstring = subst(locstring, from_ascii("&"), docstring());
1024                 locstring = subst(locstring, from_ascii("</amp;>"), from_ascii(" & "));
1025                 setText(locstring, guilang);
1026                 params_.force_ltr = !guilang->rightToLeft() && !params_.lang->rightToLeft();
1027                 break;
1028         }
1029         case InsetInfoParams::ICON_INFO: {
1030                 // only need to do this once.
1031                 if (initialized_)
1032                         break;
1033                 // and we will not keep trying if we fail
1034                 initialized_ = true;
1035                 FuncRequest func = lyxaction.lookupFunc(params_.name);
1036                 docstring icon_name = frontend::Application::iconName(func, true);
1037                 // FIXME: We should use the icon directly instead of
1038                 // going through FileName. The code below won't work
1039                 // if the icon is embedded in the executable through
1040                 // the Qt resource system.
1041                 // This is only a negligible performance problem:
1042                 // If the installed icon differs from the resource icon the
1043                 // installed one is preferred anyway, and all icons that are
1044                 // embedded in the resources are installed as well.
1045                 FileName file(to_utf8(icon_name));
1046                 if (file.onlyFileNameWithoutExt() == "unknown") {
1047                         string dir = "images";
1048                         FileName file2(imageLibFileSearch(dir, params_.name, "svgz,png"));
1049                         if (!file2.empty())
1050                                 file = file2;
1051                 }
1052                 if (!file.exists())
1053                         break;
1054                 int percent_scale = 100;
1055                 if (use_gui) {
1056                         // Compute the scale factor for the icon such that its
1057                         // width on screen is equal to 1em in pixels.
1058                         // The scale factor is rounded to the integer nearest
1059                         // to the float value of the ratio 100*iconsize/imgsize.
1060                         int imgsize = QImage(toqstr(file.absFileName())).width();
1061                         if (imgsize > 0) {
1062                                 int iconsize = Length(1, Length::EM).inPixels(1);
1063                                 percent_scale = (100 * iconsize + imgsize / 2)/imgsize;
1064                         }
1065                 }
1066                 InsetGraphics * inset = new InsetGraphics(buffer_);
1067                 InsetGraphicsParams igp;
1068                 igp.filename = file;
1069                 igp.lyxscale = percent_scale;
1070                 igp.scale = string();
1071                 igp.width = Length(1, Length::EM);
1072                 inset->setParams(igp);
1073                 clear();
1074                 Font const f(inherit_font, params_.lang);
1075                 paragraphs().front().insertInset(0, inset, f,
1076                                                  Change(Change::UNCHANGED));
1077                 break;
1078         }
1079         case InsetInfoParams::BUFFER_INFO: {
1080                 // this could all change, so we will recalculate each time
1081                 if (params_.name == "name")
1082                         setText(from_utf8(buffer().fileName().onlyFileName()), params_.lang);
1083                 else if (params_.name == "name-noext")
1084                         setText(from_utf8(buffer().fileName().onlyFileNameWithoutExt()), params_.lang);
1085                 else if (params_.name == "path")
1086                         setText(from_utf8(os::latex_path(buffer().filePath())), params_.lang);
1087                 else if (params_.name == "class")
1088                         setText(from_utf8(bp.documentClass().name()), params_.lang);
1089                 break;
1090         }
1091         case InsetInfoParams::VCS_INFO: {
1092                 // this information could change, in principle, so we will 
1093                 // recalculate each time through
1094                 if (!buffer().lyxvc().inUse()) {
1095                         gui = _("No version control!");
1096                         info(from_ascii("No version control!"), params_.lang);
1097                         break;
1098                 }
1099                 LyXVC::RevisionInfo itype = LyXVC::Unknown;
1100                 if (params_.name == "revision")
1101                         itype = LyXVC::File;
1102                 else if (params_.name == "tree-revision")
1103                         itype = LyXVC::Tree;
1104                 else if (params_.name == "author")
1105                         itype = LyXVC::Author;
1106                 else if (params_.name == "time")
1107                         itype = LyXVC::Time;
1108                 else if (params_.name == "date")
1109                         itype = LyXVC::Date;
1110                 string binfo = buffer().lyxvc().revisionInfo(itype);
1111                 if (binfo.empty()) {
1112                         gui = _("%1$s[[vcs data]] unknown");
1113                         error(from_ascii("%1$s[[vcs data]] unknown"), params_.lang);
1114                 } else
1115                         setText(from_utf8(binfo), params_.lang);
1116                 break;
1117         }
1118         case InsetInfoParams::LYX_INFO:
1119                 // only need to do this once.
1120                 if (initialized_)
1121                         break;
1122                 if (params_.name == "version")
1123                         setText(from_ascii(lyx_version), params_.lang);
1124                 else if (params_.name == "layoutformat")
1125                         setText(convert<docstring>(LAYOUT_FORMAT), params_.lang);
1126                 initialized_ = true;
1127                 break;
1128         case InsetInfoParams::DATE_INFO:
1129         case InsetInfoParams::MODDATE_INFO:
1130         case InsetInfoParams::FIXDATE_INFO: {
1131                 string date_format = params_.name;
1132                 string const date_specifier = (params_.type == InsetInfoParams::FIXDATE_INFO
1133                                                && contains(params_.name, '@'))
1134                                 ? split(params_.name, date_format, '@') : string();
1135                 QDate date;
1136                 if (params_.type == InsetInfoParams::MODDATE_INFO)
1137                         date = QDateTime::fromTime_t(buffer().fileName().lastModified()).date();
1138                 else if (params_.type == InsetInfoParams::FIXDATE_INFO && !date_specifier.empty())
1139                         date = QDate::fromString(toqstr(date_specifier), Qt::ISODate);
1140                 else
1141                         date = QDate::currentDate();
1142                 setText(params_.getDate(date_format, date), params_.lang);
1143                 break;
1144         }
1145         case InsetInfoParams::TIME_INFO:
1146         case InsetInfoParams::MODTIME_INFO:
1147         case InsetInfoParams::FIXTIME_INFO: {
1148                 string time_format = params_.name;
1149                 string const time_specifier = (params_.type == InsetInfoParams::FIXTIME_INFO
1150                                                && contains(params_.name, '@'))
1151                                 ? split(params_.name, time_format, '@') : string();
1152                 QTime time;
1153                 if (params_.type == InsetInfoParams::MODTIME_INFO)
1154                         time = QDateTime::fromTime_t(buffer().fileName().lastModified()).time();
1155                 else if (params_.type == InsetInfoParams::FIXTIME_INFO && !time_specifier.empty())
1156                         time = QTime::fromString(toqstr(time_specifier), Qt::ISODate);
1157                 else
1158                         time = QTime::currentTime();
1159                 setText(params_.getTime(time_format, time), params_.lang);
1160                 break;
1161         }
1162         }
1163
1164         // Just to do something with that string
1165         LYXERR(Debug::INFO, "info inset text: " << gui);
1166         InsetCollapsible::updateBuffer(it, utype);
1167 }
1168
1169
1170 string InsetInfo::contextMenu(BufferView const &, int, int) const
1171 {
1172         //FIXME: We override the implementation of InsetCollapsible,
1173         //because this inset is not a collapsible inset.
1174         return contextMenuName();
1175 }
1176
1177
1178 string InsetInfo::contextMenuName() const
1179 {
1180         return "context-info";
1181 }
1182
1183
1184 } // namespace lyx