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