3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
7 * \author Jürgen Spitzmüller
9 * Full author contact details are available in file CREDITS.
13 #include "InsetInfo.h"
16 #include "BufferParams.h"
17 #include "BufferView.h"
20 #include "CutAndPaste.h"
22 #include "FuncRequest.h"
23 #include "FuncStatus.h"
24 #include "InsetGraphics.h"
25 #include "InsetSpecialChar.h"
27 #include "LaTeXFeatures.h"
29 #include "LayoutFile.h"
30 #include "LyXAction.h"
34 #include "Paragraph.h"
35 #include "ParIterator.h"
36 #include "ParagraphParameters.h"
39 #include "frontends/Application.h"
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"
58 #include <QtGui/QImage>
63 using namespace lyx::support;
69 typedef Translator<InsetInfoParams::info_type, string> NameTranslator;
71 NameTranslator const initTranslator()
73 NameTranslator translator(InsetInfoParams::UNKNOWN_INFO, "unknown");
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");
96 /// The translator between the information type enum and corresponding string.
97 NameTranslator const & nameTranslator()
99 static NameTranslator const translator = initTranslator();
104 typedef Translator<InsetInfoParams::info_type, string> DefaultValueTranslator;
106 DefaultValueTranslator const initDVTranslator()
108 DefaultValueTranslator translator(InsetInfoParams::UNKNOWN_INFO, "");
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");
131 /// The translator between the information type enum and some sensible default value.
132 DefaultValueTranslator const & defaultValueTranslator()
134 static DefaultValueTranslator const translator = initDVTranslator();
141 /////////////////////////////////////////////////////////////////////
145 ///////////////////////////////////////////////////////////////////////
147 InsetInfoParams infoparams;
150 set<string> getTexFileList(string const & filename)
153 FileName const file = libFileSearch(string(), filename);
158 vector<docstring> doclist =
159 getVectorFromString(file.fileContents("UTF-8"), from_ascii("\n"));
161 // Normalise paths like /foo//bar ==> /foo/bar
162 // No "auto const &" because doc is modified later
163 // coverity[auto_causes_copy]
164 for (auto doc : doclist) {
165 doc = subst(doc, from_ascii("\r"), docstring());
166 while (contains(doc, from_ascii("//")))
167 doc = subst(doc, from_ascii("//"), from_ascii("/"));
169 list.insert(removeExtension(onlyFileName(to_utf8(doc))));
176 bool translateString(docstring const & in, docstring & out, string const & lcode)
178 out = translateIfPossible(in, lcode);
184 docstring InsetInfoParams::getDate(string const & iname, QDate const date) const
188 loc = QLocale(toqstr(lang->code()));
190 return qstring_to_ucs4(loc.toString(date, QLocale::LongFormat));
191 else if (iname == "short")
192 return qstring_to_ucs4(loc.toString(date, QLocale::ShortFormat));
193 else if (iname == "ISO")
194 return qstring_to_ucs4(date.toString(Qt::ISODate));
195 else if (iname == "loclong")
196 return lang ? qstring_to_ucs4(loc.toString(date, toqstr(lang->dateFormat(0))))
197 : _("No long date format (language unknown)!");
198 else if (iname == "locmedium")
199 return lang ? qstring_to_ucs4(loc.toString(date, toqstr(lang->dateFormat(1))))
200 : _("No medium date format (language unknown)!");
201 else if (iname == "locshort")
202 return lang ? qstring_to_ucs4(loc.toString(date, toqstr(lang->dateFormat(2))))
203 : _("No short date format (language unknown)!");
205 return qstring_to_ucs4(loc.toString(date, toqstr(iname)));
209 docstring InsetInfoParams::getTime(string const & iname, QTime const time) const
213 loc = QLocale(toqstr(lang->code()));
215 return qstring_to_ucs4(loc.toString(time, QLocale::LongFormat));
216 else if (iname == "short")
217 return qstring_to_ucs4(loc.toString(time, QLocale::ShortFormat));
218 else if (iname == "ISO")
219 return qstring_to_ucs4(time.toString(Qt::ISODate));
221 return qstring_to_ucs4(loc.toString(time, toqstr(iname)));
225 vector<pair<string,docstring>> InsetInfoParams::getArguments(Buffer const * buf,
226 string const & itype) const
228 vector<pair<string,docstring>> result;
230 switch (nameTranslator().find(itype)) {
232 result.push_back(make_pair("invalid", _("Please select a valid type!")));
239 result.push_back(make_pair("custom", _("Custom")));
240 for (auto const & name_code : lyxaction) {
241 string const lfun = name_code.first;
243 result.push_back(make_pair(lfun, from_ascii(lfun)));
249 result.push_back(make_pair("custom", _("Custom")));
253 result.push_back(make_pair("custom", _("Custom")));
254 set<string> rcs = lyxrc.getRCs();
255 for (auto const & rc : rcs)
256 result.push_back(make_pair(rc, from_ascii(rc)));
261 case TEXTCLASS_INFO: {
262 result.push_back(make_pair("custom", _("Custom")));
263 string const filename = (itype == "package") ? "styFiles.lst"
265 set<string> flist = getTexFileList(filename);
266 for (auto const & f : flist)
267 result.push_back(make_pair(f, from_utf8(f)));
272 result.push_back(make_pair("name", _("File name (with extension)")));
273 result.push_back(make_pair("name-noext", _("File name (without extension)")));
274 result.push_back(make_pair("path", _("File path")));
275 result.push_back(make_pair("class", _("Used text class")));
279 if (!buf->lyxvc().inUse()) {
280 result.push_back(make_pair("invalid", _("No version control!")));
283 result.push_back(make_pair("revision", _("Revision[[Version Control]]")));
284 result.push_back(make_pair("revision-abbrev", _("Abbreviated revision[[Version Control]]")));
285 result.push_back(make_pair("tree-revision", _("Tree revision")));
286 result.push_back(make_pair("author", _("Author")));
287 result.push_back(make_pair("date", _("Date")));
288 result.push_back(make_pair("time", _("Time[[of day]]")));
293 result.push_back(make_pair("version", _("LyX version")));
294 result.push_back(make_pair("layoutformat", _("LyX layout format")));
300 string const dt = split(name, '@');
302 if (itype == "moddate")
303 #if (QT_VERSION >= QT_VERSION_CHECK(5, 8, 0))
304 date = QDateTime::fromSecsSinceEpoch(buf->fileName().lastModified()).date();
306 date = QDateTime::fromTime_t(buf->fileName().lastModified()).date();
308 else if (itype == "fixdate" && !dt.empty()) {
309 QDate const gdate = QDate::fromString(toqstr(dt), Qt::ISODate);
310 date = (gdate.isValid()) ? gdate : QDate::currentDate();
312 date = QDate::currentDate();
313 result.push_back(make_pair("long",getDate("long", date)));
314 result.push_back(make_pair("short", getDate("short", date)));
315 result.push_back(make_pair("loclong", getDate("loclong", date)));
316 result.push_back(make_pair("locmedium", getDate("locmedium", date)));
317 result.push_back(make_pair("locshort", getDate("locshort", date)));
318 result.push_back(make_pair("ISO", getDate("ISO", date)));
319 result.push_back(make_pair("yyyy", getDate("yyyy", date)));
320 result.push_back(make_pair("MMMM", getDate("MMMM", date)));
321 result.push_back(make_pair("MMM", getDate("MMM", date)));
322 result.push_back(make_pair("dddd", getDate("dddd", date)));
323 result.push_back(make_pair("ddd", getDate("ddd", date)));
324 result.push_back(make_pair("custom", _("Custom")));
330 string const tt = split(name, '@');
332 if (itype == "modtime")
333 #if (QT_VERSION >= QT_VERSION_CHECK(5, 8, 0))
334 time = QDateTime::fromSecsSinceEpoch(buf->fileName().lastModified()).time();
336 time = QDateTime::fromTime_t(buf->fileName().lastModified()).time();
338 else if (itype == "fixtime" && !tt.empty()) {
339 QTime const gtime = QTime::fromString(toqstr(tt), Qt::ISODate);
340 time = (gtime.isValid()) ? gtime : QTime::currentTime();
342 time = QTime::currentTime();
343 result.push_back(make_pair("long",getTime("long", time)));
344 result.push_back(make_pair("short", getTime("short", time)));
345 result.push_back(make_pair("ISO", getTime("ISO", time)));
346 result.push_back(make_pair("custom", _("Custom")));
355 bool InsetInfoParams::validateArgument(Buffer const * buf, docstring const & arg,
356 bool const usedefaults) const
359 string name = trim(split(to_utf8(arg), type, ' '));
360 if (name.empty() && usedefaults)
361 name = defaultValueTranslator().find(type);
363 switch (nameTranslator().find(type)) {
370 FuncRequest func = lyxaction.lookupFunc(name);
371 return func.action() != LFUN_UNKNOWN_ACTION;
375 return !name.empty();
378 FuncCode const action = lyxaction.lookupFunc(name).action();
379 if (action == LFUN_UNKNOWN_ACTION) {
380 string dir = "images";
381 return !imageLibFileSearch(dir, name, "svgz,png").empty();
387 set<string> rcs = lyxrc.getRCs();
388 return rcs.find(name) != rcs.end();
396 return (name == "name" || name == "name-noext"
397 || name == "path" || name == "class");
400 if (name == "revision" || name == "revision-abbrev" || name == "tree-revision"
401 || name == "author" || name == "date" || name == "time")
402 return buf->lyxvc().inUse();
406 return name == "version" || name == "layoutformat";
411 date = split(name, piece, '@');
412 if (!date.empty() && !QDate::fromString(toqstr(date), Qt::ISODate).isValid())
420 if (name == "long" || name == "short" || name == "ISO")
423 QDate date = QDate::currentDate();
424 return !date.toString(toqstr(name)).isEmpty();
430 time = split(name, piece, '@');
431 if (!time.empty() && !QTime::fromString(toqstr(time), Qt::ISODate).isValid())
439 if (name == "long" || name == "short" || name == "ISO")
442 QTime time = QTime::currentTime();
443 return !time.toString(toqstr(name)).isEmpty();
454 string InsetInfoParams::infoType() const
456 return nameTranslator().find(type);
461 /////////////////////////////////////////////////////////////////////////
465 /////////////////////////////////////////////////////////////////////////
469 InsetInfo::InsetInfo(Buffer * buf, string const & info)
470 : InsetCollapsible(buf), initialized_(false), dirty_(true)
472 params_.type = InsetInfoParams::UNKNOWN_INFO;
473 params_.force_ltr = false;
479 Inset * InsetInfo::editXY(Cursor & cur, int x, int y)
481 // do not allow the cursor to be set in this Inset
482 return Inset::editXY(cur, x, y);
486 docstring InsetInfo::layoutName() const
488 return from_ascii("Info:" + params_.infoType());
492 docstring InsetInfo::toolTip(BufferView const &, int, int) const
495 switch (nameTranslator().find(params_.infoType())) {
496 case InsetInfoParams::UNKNOWN_INFO:
497 result = _("Invalid information inset");
499 case InsetInfoParams::SHORTCUT_INFO:
500 result = bformat(_("The keybard shortcut for the function '%1$s'"),
501 from_utf8(params_.name));
503 case InsetInfoParams::SHORTCUTS_INFO:
504 result = bformat(_("The keybard shortcuts for the function '%1$s'"),
505 from_utf8(params_.name));
507 case InsetInfoParams::MENU_INFO:
508 result = bformat(_("The menu location for the function '%1$s'"),
509 from_utf8(params_.name));
511 case InsetInfoParams::L7N_INFO:
512 result = bformat(_("The localization for the string '%1$s'"),
513 from_utf8(params_.name));
515 case InsetInfoParams::ICON_INFO:
516 result = bformat(_("The toolbar icon for the function '%1$s'"),
517 from_utf8(params_.name));
519 case InsetInfoParams::LYXRC_INFO:
520 result = bformat(_("The preference setting for the preference key '%1$s'"),
521 from_utf8(params_.name));
523 case InsetInfoParams::PACKAGE_INFO:
524 result = bformat(_("Availability of the LaTeX package '%1$s'"),
525 from_utf8(params_.name));
527 case InsetInfoParams::TEXTCLASS_INFO:
528 result = bformat(_("Availability of the LaTeX class '%1$s'"),
529 from_utf8(params_.name));
531 case InsetInfoParams::BUFFER_INFO:
532 if (params_.name == "name")
533 result = _("The name of this file (incl. extension)");
534 else if (params_.name == "name-noext")
535 result = _("The name of this file (without extension)");
536 else if (params_.name == "path")
537 result = _("The path where this file is saved");
538 else if (params_.name == "class")
539 result = _("The class this document uses");
541 case InsetInfoParams::VCS_INFO:
542 if (params_.name == "revision")
543 result = _("Version control revision");
544 else if (params_.name == "revision-abbrev")
545 result = _("Version control abbreviated revision");
546 else if (params_.name == "tree-revision")
547 result = _("Version control tree revision");
548 else if (params_.name == "author")
549 result = _("Version control author");
550 else if (params_.name == "date")
551 result = _("Version control date");
552 else if (params_.name == "time")
553 result = _("Version control time");
555 case InsetInfoParams::LYX_INFO:
556 if (params_.name == "version")
557 result = _("The current LyX version");
558 else if (params_.name == "layoutformat")
559 result = _("The current LyX layout format");
561 case InsetInfoParams::DATE_INFO:
562 result = _("The current date");
564 case InsetInfoParams::MODDATE_INFO:
565 result = _("The date of last save");
567 case InsetInfoParams::FIXDATE_INFO:
568 result = _("A static date");
570 case InsetInfoParams::TIME_INFO:
571 result = _("The current time");
573 case InsetInfoParams::MODTIME_INFO:
574 result = _("The time of last save");
576 case InsetInfoParams::FIXTIME_INFO:
577 result = _("A static time");
585 void InsetInfo::read(Lexer & lex)
590 token = lex.getString();
591 if (token == "type") {
593 token = lex.getString();
594 params_.type = nameTranslator().find(token);
595 } else if (token == "arg") {
597 params_.name = lex.getString();
598 } else if (token == "\\end_inset")
601 if (token != "\\end_inset") {
602 lex.printError("Missing \\end_inset at this point");
603 throw ExceptionMessage(WarningException,
604 _("Missing \\end_inset at this point."),
610 void InsetInfo::write(ostream & os) const
612 os << "Info\ntype \"" << params_.infoType()
613 << "\"\narg " << Lexer::quoteString(params_.name);
617 bool InsetInfo::showInsetDialog(BufferView * bv) const
619 bv->showDialog("info");
624 bool InsetInfo::getStatus(Cursor & cur, FuncRequest const & cmd,
625 FuncStatus & flag) const
627 switch (cmd.action()) {
628 case LFUN_INSET_SETTINGS:
629 return InsetCollapsible::getStatus(cur, cmd, flag);
631 case LFUN_INSET_DIALOG_UPDATE:
632 case LFUN_INSET_COPY_AS:
633 case LFUN_INSET_DISSOLVE:
634 flag.setEnabled(true);
637 case LFUN_INSET_MODIFY:
638 if (nameTranslator().find(cmd.getArg(0)) == InsetInfoParams::UNKNOWN_INFO)
639 return Inset::getStatus(cur, cmd, flag);
640 if (params_.validateArgument(&buffer(), cmd.argument())) {
641 flag.setEnabled(true);
643 string name = trim(split(to_utf8(cmd.argument()), typestr, ' '));
644 InsetInfoParams::info_type type = nameTranslator().find(typestr);
645 string origname = params_.name;
646 if (type == InsetInfoParams::FIXDATE_INFO
647 || type == InsetInfoParams::FIXTIME_INFO)
648 split(params_.name, origname, '@');
649 flag.setOnOff(type == params_.type && name == origname);
660 void InsetInfo::doDispatch(Cursor & cur, FuncRequest & cmd)
662 switch (cmd.action()) {
663 case LFUN_INSET_MODIFY:
664 if (nameTranslator().find(cmd.getArg(0)) == InsetInfoParams::UNKNOWN_INFO) {
669 setInfo(to_utf8(cmd.argument()));
670 cur.forceBufferUpdate();
671 initialized_ = false;
674 case LFUN_INSET_COPY_AS: {
675 cap::clearSelection();
677 copy.pushBackward(*this);
681 copy.pit() = copy.lastpit();
682 copy.pos() = copy.lastpos();
684 cap::copySelection(copy);
689 InsetCollapsible::doDispatch(cur, cmd);
695 void InsetInfo::setInfo(string const & info)
700 string saved_date_specifier;
701 // Store old date specifier for potential re-use
702 if (!params_.name.empty())
703 saved_date_specifier = split(params_.name, '@');
706 params_.name = trim(split(info, type, ' '));
707 params_.type = nameTranslator().find(type);
708 if (params_.name.empty())
709 params_.name = defaultValueTranslator().find(params_.type);
710 if (params_.type == InsetInfoParams::FIXDATE_INFO) {
711 string const date_specifier = split(params_.name, '@');
712 // If an explicit new fix date is specified, use that
713 // Otherwise, use the old one or, if there is none,
715 if (date_specifier.empty()) {
716 if (saved_date_specifier.empty())
717 params_.name += "@" + fromqstr(QDate::currentDate().toString(Qt::ISODate));
719 params_.name += "@" + saved_date_specifier;
722 else if (params_.type == InsetInfoParams::FIXTIME_INFO) {
723 string const time_specifier = split(params_.name, '@');
724 // If an explicit new fix time is specified, use that
725 // Otherwise, use the old one or, if there is none,
727 if (time_specifier.empty()) {
728 if (saved_date_specifier.empty())
729 params_.name += "@" + fromqstr(QTime::currentTime().toString(Qt::ISODate));
731 params_.name += "@" + saved_date_specifier;
737 void InsetInfo::error(docstring const & err, Language const * lang)
739 docstring const res = translateIfPossible(err, lang->code());
740 bool const translated = res != err;
741 // If the string is not translated, we use default lang (English)
742 Font const f = translated ? Font(inherit_font, lang) : Font(inherit_font);
743 setText(bformat(res, from_utf8(params_.name)), f, false);
747 void InsetInfo::info(docstring const & err, Language const * lang)
749 docstring const res = translateIfPossible(err, lang->code());
750 bool const translated = res != err;
751 // If the string is not translated, we use default lang (English)
752 Font const f = translated ? Font(inherit_font, lang) : Font(inherit_font);
753 setText(translateIfPossible(err, lang->code()), f, false);
757 void InsetInfo::setText(docstring const & str, Language const * lang)
759 setText(str, Font(inherit_font, lang), false);
763 bool InsetInfo::forceLTR(OutputParams const &) const
765 return params_.force_ltr;
769 bool InsetInfo::forceLocalFontSwitch() const
771 return params_.type == InsetInfoParams::MENU_INFO
772 || params_.type == InsetInfoParams::SHORTCUTS_INFO
773 || params_.type == InsetInfoParams::SHORTCUT_INFO
774 || params_.type == InsetInfoParams::L7N_INFO;
778 void InsetInfo::metrics(MetricsInfo & mi, Dimension & dim) const
780 const_cast<InsetInfo *>(this)->build();
781 InsetCollapsible::metrics(mi, dim);
786 void InsetInfo::draw(PainterInfo & pi, int x, int y) const
790 Changer chg = changeVar(lyxrc.mark_foreign_language, false);
791 InsetCollapsible::draw(pi, x, y);
795 void InsetInfo::updateBuffer(ParIterator const & it, UpdateType utype, bool const deleted)
798 // If the Buffer is a clone, then we neither need nor want to do any
799 // of what follows. We want, rather, just to inherit how things were
800 // in the original Buffer. This is especially important for VCS.
801 // Otherwise, we could in principle have different settings here
802 // than in the Buffer we were exporting.
803 if (buffer().isClone())
805 BufferParams const & bp = buffer().params();
806 params_.lang = it.paragraph().getFontSettings(bp, it.pos()).language();
807 InsetCollapsible::updateBuffer(it, utype, deleted);
811 void InsetInfo::build()
813 // If the Buffer is a clone, then we neither need nor want to do any
814 // of what follows. We want, rather, just to inherit how things were
815 // in the original Buffer. This is especially important for VCS.
816 // Otherwise, we could in principle have different settings here
817 // than in the Buffer we were exporting.
818 if (buffer().isClone())
821 Language const * tryguilang = languages.getFromCode(Messages::guiLanguage());
822 // Some info insets use the language of the GUI (if available)
823 Language const * guilang = tryguilang ? tryguilang : params_.lang;
825 params_.force_ltr = !params_.lang->rightToLeft();
826 // This is just to get the string into the po files
828 switch (params_.type) {
829 case InsetInfoParams::UNKNOWN_INFO:
830 gui = _("Unknown Info!");
831 info(from_ascii("Unknown Info!"), params_.lang);
832 initialized_ = false;
834 case InsetInfoParams::SHORTCUT_INFO:
835 case InsetInfoParams::SHORTCUTS_INFO: {
836 // shortcuts can change, so we need to re-do this each time
837 FuncRequest const func = lyxaction.lookupFunc(params_.name);
838 if (func.action() == LFUN_UNKNOWN_ACTION) {
839 gui = _("Unknown action %1$s");
840 error(from_ascii("Unknown action %1$s"), params_.lang);
843 KeyMap::Bindings bindings = theTopLevelKeymap().findBindings(func);
844 if (bindings.empty()) {
845 gui = _("undefined");
846 info(from_ascii("undefined"), params_.lang);
850 docstring seq_untranslated;
851 if (params_.type == InsetInfoParams::SHORTCUT_INFO) {
852 sequence = bindings.begin()->print(KeySequence::ForGui);
853 seq_untranslated = bindings.begin()->print(KeySequence::ForGui, true);
855 sequence = theTopLevelKeymap().printBindings(func, KeySequence::ForGui);
856 seq_untranslated = theTopLevelKeymap().printBindings(func, KeySequence::ForGui, true);
858 // QKeySequence returns special characters for keys on the mac
859 // Since these are not included in many fonts, we
860 // re-translate them to textual names (see #10641)
861 odocstringstream ods;
862 string const lcode = params_.lang->code();
864 bool is_translated = sequence != seq_untranslated;
865 for (char_type const c : sequence) {
868 gui = _("Return[[Key]]");
869 is_translated = translateString(from_ascii("Return[[Key]]"), trans, lcode);
872 case 0x21b9://Tab both directions (Win)
873 gui = _("Tab[[Key]]");
874 is_translated = translateString(from_ascii("Tab[[Key]]"), trans, lcode);
877 case 0x21de://Qt::Key_PageUp
879 is_translated = translateString(from_ascii("PgUp"), trans, lcode);
882 case 0x21df://Qt::Key_PageDown
884 is_translated = translateString(from_ascii("PgDown"), trans, lcode);
887 case 0x21e4://Qt::Key_Backtab
889 is_translated = translateString(from_ascii("Backtab"), trans, lcode);
892 case 0x21e5://Qt::Key_Tab
894 is_translated = translateString(from_ascii("Tab"), trans, lcode);
899 is_translated = translateString(from_ascii("Shift"), trans, lcode);
902 case 0x21ea://Qt::Key_CapsLock
904 is_translated = translateString(from_ascii("CapsLock"), trans, lcode);
907 case 0x2303://Control
908 gui = _("Control[[Key]]");
909 is_translated = translateString(from_ascii("Control[[Key]]"), trans, lcode);
913 gui = _("Command[[Key]]");
914 is_translated = translateString(from_ascii("Command[[Key]]"), trans, lcode);
917 case 0x2324://Qt::Key_Enter
918 gui = _("Return[[Key]]");
919 is_translated = translateString(from_ascii("Return[[Key]]"), trans, lcode);
922 case 0x2325://Option key
923 gui = _("Option[[Key]]");
924 is_translated = translateString(from_ascii("Option[[Key]]"), trans, lcode);
927 case 0x2326://Qt::Key_Delete
928 gui = _("Delete[[Key]]");
929 is_translated = translateString(from_ascii("Delete[[Key]]"), trans, lcode);
932 case 0x232b://Qt::Key_Backspace
934 is_translated = translateString(from_ascii("Fn+Del"), trans, lcode);
937 case 0x238b://Qt::Key_Escape
939 is_translated = translateString(from_ascii("Esc"), trans, lcode);
946 setText(ods.str(), is_translated ? guilang : nullptr);
947 params_.force_ltr = !is_translated || (!guilang->rightToLeft() && !params_.lang->rightToLeft());
950 case InsetInfoParams::LYXRC_INFO: {
951 // this information could change, if the preferences are changed,
952 // so we will recalculate each time through.
954 if (params_.name.empty()) {
955 gui = _("undefined");
956 info(from_ascii("undefined"), params_.lang);
959 // FIXME this uses the serialization mechanism to get the info
960 // we want, which i guess works but is a bit strange.
961 lyxrc.write(oss, true, params_.name);
962 string result = oss.str();
963 if (result.size() < 2) {
964 gui = _("undefined");
965 info(from_ascii("undefined"), params_.lang);
968 string::size_type loc = result.rfind("\n", result.size() - 2);
969 loc = loc == string::npos ? 0 : loc + 1;
970 if (result.size() < loc + params_.name.size() + 1
971 || result.substr(loc + 1, params_.name.size()) != params_.name) {
972 gui = _("undefined");
973 info(from_ascii("undefined"), params_.lang);
976 // remove leading comments and \\name and space
977 result = result.substr(loc + params_.name.size() + 2);
980 result = rtrim(result, "\n");
981 result = trim(result, "\"");
985 setText(from_utf8(result), params_.lang);
988 case InsetInfoParams::PACKAGE_INFO:
989 // only need to do this once.
992 // check in packages.lst
994 // we also allow version check with version separated by blank
995 if (contains(params_.name, ' ')) {
997 string const version = split(params_.name, name, ' ');
998 int const y = convert<int>(version.substr(0,4));
999 int const m = convert<int>(version.substr(4,2));
1000 int const d = convert<int>(version.substr(6,2));
1001 available = LaTeXFeatures::isAvailableAtLeastFrom(name, y, m, d);
1003 available = LaTeXFeatures::isAvailable(params_.name);
1007 info(from_ascii("yes"), params_.lang);
1010 info(from_ascii("no"), params_.lang);
1012 initialized_ = true;
1015 case InsetInfoParams::TEXTCLASS_INFO: {
1016 // the TextClass can change
1017 LayoutFileList const & list = LayoutFileList::get();
1018 bool available = false;
1019 // params_.name is the class name
1020 if (list.haveClass(params_.name))
1021 available = list[params_.name].isTeXClassAvailable();
1024 info(from_ascii("yes"), params_.lang);
1027 info(from_ascii("no"), params_.lang);
1031 case InsetInfoParams::MENU_INFO: {
1032 // only need to do this once.
1035 docstring_list names;
1036 FuncRequest func = lyxaction.lookupFunc(params_.name);
1037 if (func.action() == LFUN_UNKNOWN_ACTION) {
1038 gui = _("Unknown action %1$s");
1039 error(from_ascii("Unknown action %1$s"), params_.lang);
1042 if (func.action() == LFUN_BUFFER_VIEW || func.action() == LFUN_BUFFER_UPDATE)
1043 // The default output format is in the menu without argument,
1044 // so strip it here.
1045 if (func.argument() == from_ascii(buffer().params().getDefaultOutputFormat()))
1046 func = FuncRequest(func.action());
1047 // iterate through the menubackend to find it
1049 gui = _("Can't determine menu entry for action %1$s in batch mode");
1050 error(from_ascii("Can't determine menu entry for action %1$s in batch mode"), params_.lang);
1051 initialized_ = true;
1054 // and we will not keep trying if we fail
1055 initialized_ = theApp()->hasBufferView();
1056 if (!theApp()->searchMenu(func, names)) {
1057 gui = _("No menu entry for action %1$s");
1058 error(from_ascii("No menu entry for action %1$s"), params_.lang);
1061 // if found, return its path.
1063 Paragraph & par = paragraphs().front();
1064 Font const f(inherit_font, guilang);
1065 params_.force_ltr = !guilang->rightToLeft();
1067 //fu.fontInfo().setUnderbar(FONT_ON);
1068 for (docstring const & name : names) {
1069 // do not insert > for the top level menu item
1070 if (&name != &names.front())
1071 par.insertInset(par.size(), new InsetSpecialChar(InsetSpecialChar::MENU_SEPARATOR),
1072 f, Change(Change::UNCHANGED));
1073 //FIXME: add proper underlines here. This
1074 // involves rewriting searchMenu used above to
1075 // return a vector of menus. If we do not do
1076 // that, we might as well use below
1077 // Paragraph::insert on each string (JMarc)
1078 for (char_type c : name)
1079 par.insertChar(par.size(), c, f, Change(Change::UNCHANGED));
1083 case InsetInfoParams::L7N_INFO: {
1084 docstring locstring = _(params_.name);
1085 // Remove trailing colons
1086 locstring = rtrim(locstring, ":");
1087 // Remove menu accelerators
1088 if (contains(locstring, from_ascii("|"))) {
1089 docstring nlocstring;
1090 rsplit(locstring, nlocstring, '|');
1091 locstring = nlocstring;
1093 // Remove Qt accelerators, but keep literal ampersands
1094 locstring = subst(locstring, from_ascii(" & "), from_ascii("</amp;>"));
1095 locstring = subst(locstring, from_ascii("&"), docstring());
1096 locstring = subst(locstring, from_ascii("</amp;>"), from_ascii(" & "));
1097 setText(locstring, guilang);
1098 params_.force_ltr = !guilang->rightToLeft() && !params_.lang->rightToLeft();
1101 case InsetInfoParams::ICON_INFO: {
1102 // only need to do this once.
1105 // and we will not keep trying if we fail
1106 initialized_ = true;
1107 FuncRequest func = lyxaction.lookupFunc(params_.name);
1108 docstring icon_name = frontend::Application::iconName(func, true);
1109 FileName file(to_utf8(icon_name));
1110 if (file.onlyFileNameWithoutExt() == "unknown") {
1111 string dir = "images";
1112 FileName file2(imageLibFileSearch(dir, params_.name, "svgz,png"));
1118 int percent_scale = 100;
1120 // Compute the scale factor for the icon such that its
1121 // width on screen is equal to 1em in pixels.
1122 // The scale factor is rounded to the integer nearest
1123 // to the float value of the ratio 100*iconsize/imgsize.
1124 int imgsize = QImage(toqstr(file.absFileName())).width();
1126 int iconsize = Length(1, Length::EM).inPixels(1);
1127 percent_scale = (100 * iconsize + imgsize / 2)/imgsize;
1130 InsetGraphics * inset = new InsetGraphics(buffer_);
1131 InsetGraphicsParams igp;
1132 igp.filename = file;
1133 igp.lyxscale = percent_scale;
1134 igp.scale = string();
1135 igp.width = Length(1, Length::EM);
1136 if (contains(file.absoluteFilePath(), from_ascii("math"))
1137 || contains(file.absoluteFilePath(), from_ascii("ert-insert"))
1138 || suffixIs(file.onlyPath().absoluteFilePath(), from_ascii("ipa")))
1139 igp.darkModeSensitive = true;
1140 inset->setParams(igp);
1142 Font const f(inherit_font, params_.lang);
1143 paragraphs().front().insertInset(0, inset, f,
1144 Change(Change::UNCHANGED));
1147 case InsetInfoParams::BUFFER_INFO: {
1148 // this could all change, so we will recalculate each time
1149 if (params_.name == "name")
1150 setText(from_utf8(buffer().fileName().onlyFileName()), params_.lang);
1151 else if (params_.name == "name-noext")
1152 setText(from_utf8(buffer().fileName().onlyFileNameWithoutExt()), params_.lang);
1153 else if (params_.name == "path")
1154 setText(from_utf8(os::latex_path(buffer().filePath())), params_.lang);
1155 else if (params_.name == "class")
1156 setText(from_utf8(buffer().params().documentClass().name()), params_.lang);
1159 case InsetInfoParams::VCS_INFO: {
1160 // this information could change, in principle, so we will
1161 // recalculate each time through
1162 if (!buffer().lyxvc().inUse()) {
1163 gui = _("No version control!");
1164 info(from_ascii("No version control!"), params_.lang);
1167 LyXVC::RevisionInfo itype = LyXVC::Unknown;
1168 if (params_.name == "revision")
1169 itype = LyXVC::File;
1170 else if (params_.name == "revision-abbrev")
1171 itype = LyXVC::FileAbbrev;
1172 else if (params_.name == "tree-revision")
1173 itype = LyXVC::Tree;
1174 else if (params_.name == "author")
1175 itype = LyXVC::Author;
1176 else if (params_.name == "time")
1177 itype = LyXVC::Time;
1178 else if (params_.name == "date")
1179 itype = LyXVC::Date;
1180 string binfo = buffer().lyxvc().revisionInfo(itype);
1181 if (binfo.empty()) {
1182 gui = _("%1$s[[vcs data]] unknown");
1183 error(from_ascii("%1$s[[vcs data]] unknown"), params_.lang);
1185 setText(from_utf8(binfo), params_.lang);
1188 case InsetInfoParams::LYX_INFO:
1189 // only need to do this once.
1192 if (params_.name == "version")
1193 setText(from_ascii(lyx_version), params_.lang);
1194 else if (params_.name == "layoutformat")
1195 setText(convert<docstring>(LAYOUT_FORMAT), params_.lang);
1196 initialized_ = true;
1198 case InsetInfoParams::DATE_INFO:
1199 case InsetInfoParams::MODDATE_INFO:
1200 case InsetInfoParams::FIXDATE_INFO: {
1201 string date_format = params_.name;
1202 string const date_specifier = (params_.type == InsetInfoParams::FIXDATE_INFO
1203 && contains(params_.name, '@'))
1204 ? split(params_.name, date_format, '@') : string();
1206 if (params_.type == InsetInfoParams::MODDATE_INFO)
1207 #if (QT_VERSION >= QT_VERSION_CHECK(5, 8, 0))
1208 date = QDateTime::fromSecsSinceEpoch(buffer().fileName().lastModified()).date();
1210 date = QDateTime::fromTime_t(buffer().fileName().lastModified()).date();
1212 else if (params_.type == InsetInfoParams::FIXDATE_INFO && !date_specifier.empty())
1213 date = QDate::fromString(toqstr(date_specifier), Qt::ISODate);
1215 date = QDate::currentDate();
1216 setText(params_.getDate(date_format, date), params_.lang);
1219 case InsetInfoParams::TIME_INFO:
1220 case InsetInfoParams::MODTIME_INFO:
1221 case InsetInfoParams::FIXTIME_INFO: {
1222 string time_format = params_.name;
1223 string const time_specifier = (params_.type == InsetInfoParams::FIXTIME_INFO
1224 && contains(params_.name, '@'))
1225 ? split(params_.name, time_format, '@') : string();
1227 if (params_.type == InsetInfoParams::MODTIME_INFO)
1228 #if (QT_VERSION >= QT_VERSION_CHECK(5, 8, 0))
1229 time = QDateTime::fromSecsSinceEpoch(buffer().fileName().lastModified()).time();
1231 time = QDateTime::fromTime_t(buffer().fileName().lastModified()).time();
1233 else if (params_.type == InsetInfoParams::FIXTIME_INFO && !time_specifier.empty())
1234 time = QTime::fromString(toqstr(time_specifier), Qt::ISODate);
1236 time = QTime::currentTime();
1237 setText(params_.getTime(time_format, time), params_.lang);
1242 // indicate that metrics are not usable anymore
1244 // Just to do something with that string
1245 LYXERR(Debug::INFO, "info inset text: " << gui);
1249 void InsetInfo::validate(LaTeXFeatures & features) const
1251 const_cast<InsetInfo *>(this)->build();
1252 InsetCollapsible::validate(features);
1256 string InsetInfo::contextMenu(BufferView const &, int, int) const
1258 //FIXME: We override the implementation of InsetCollapsible,
1259 //because this inset is not a collapsible inset.
1260 return contextMenuName();
1264 string InsetInfo::contextMenuName() const
1266 return "context-info";