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