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 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("/"));
167 list.insert(removeExtension(onlyFileName(to_utf8(doc))));
174 bool translateString(docstring const & in, docstring & out, string const & lcode)
176 out = translateIfPossible(in, lcode);
182 docstring InsetInfoParams::getDate(string const & iname, QDate const date) const
186 loc = QLocale(toqstr(lang->code()));
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)!");
203 return qstring_to_ucs4(loc.toString(date, toqstr(iname)));
207 docstring InsetInfoParams::getTime(string const & iname, QTime const time) const
211 loc = QLocale(toqstr(lang->code()));
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));
219 return qstring_to_ucs4(loc.toString(time, toqstr(iname)));
223 vector<pair<string,docstring>> InsetInfoParams::getArguments(Buffer const * buf,
224 string const & itype) const
226 vector<pair<string,docstring>> result;
228 switch (nameTranslator().find(itype)) {
230 result.push_back(make_pair("invalid", _("Please select a valid type!")));
237 result.push_back(make_pair("custom", _("Custom")));
238 for (auto const & name_code : lyxaction) {
239 string const lfun = name_code.first;
241 result.push_back(make_pair(lfun, from_ascii(lfun)));
247 result.push_back(make_pair("custom", _("Custom")));
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)));
259 case TEXTCLASS_INFO: {
260 result.push_back(make_pair("custom", _("Custom")));
261 string const filename = (itype == "package") ? "styFiles.lst"
263 set<string> flist = getTexFileList(filename);
264 for (auto const & f : flist)
265 result.push_back(make_pair(f, from_utf8(f)));
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")));
277 if (!buf->lyxvc().inUse()) {
278 result.push_back(make_pair("invalid", _("No version control!")));
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]]")));
291 result.push_back(make_pair("version", _("LyX version")));
292 result.push_back(make_pair("layoutformat", _("LyX layout format")));
298 string const dt = split(name, '@');
300 if (itype == "moddate")
301 #if (QT_VERSION >= QT_VERSION_CHECK(5, 8, 0))
302 date = QDateTime::fromSecsSinceEpoch(buf->fileName().lastModified()).date();
304 date = QDateTime::fromTime_t(buf->fileName().lastModified()).date();
306 else if (itype == "fixdate" && !dt.empty()) {
307 QDate const gdate = QDate::fromString(toqstr(dt), Qt::ISODate);
308 date = (gdate.isValid()) ? gdate : QDate::currentDate();
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")));
328 string const tt = split(name, '@');
330 if (itype == "modtime")
331 #if (QT_VERSION >= QT_VERSION_CHECK(5, 8, 0))
332 time = QDateTime::fromSecsSinceEpoch(buf->fileName().lastModified()).time();
334 time = QDateTime::fromTime_t(buf->fileName().lastModified()).time();
336 else if (itype == "fixtime" && !tt.empty()) {
337 QTime const gtime = QTime::fromString(toqstr(tt), Qt::ISODate);
338 time = (gtime.isValid()) ? gtime : QTime::currentTime();
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")));
353 bool InsetInfoParams::validateArgument(Buffer const * buf, docstring const & arg,
354 bool const usedefaults) const
357 string name = trim(split(to_utf8(arg), type, ' '));
358 if (name.empty() && usedefaults)
359 name = defaultValueTranslator().find(type);
361 switch (nameTranslator().find(type)) {
368 FuncRequest func = lyxaction.lookupFunc(name);
369 return func.action() != LFUN_UNKNOWN_ACTION;
373 return !name.empty();
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();
385 set<string> rcs = lyxrc.getRCs();
386 return rcs.find(name) != rcs.end();
394 return (name == "name" || name == "name-noext"
395 || name == "path" || name == "class");
398 if (name == "revision" || name == "revision-abbrev" || name == "tree-revision"
399 || name == "author" || name == "date" || name == "time")
400 return buf->lyxvc().inUse();
404 return name == "version" || name == "layoutformat";
409 date = split(name, piece, '@');
410 if (!date.empty() && !QDate::fromString(toqstr(date), Qt::ISODate).isValid())
418 if (name == "long" || name == "short" || name == "ISO")
421 QDate date = QDate::currentDate();
422 return !date.toString(toqstr(name)).isEmpty();
428 time = split(name, piece, '@');
429 if (!time.empty() && !QTime::fromString(toqstr(time), Qt::ISODate).isValid())
437 if (name == "long" || name == "short" || name == "ISO")
440 QTime time = QTime::currentTime();
441 return !time.toString(toqstr(name)).isEmpty();
452 string InsetInfoParams::infoType() const
454 return nameTranslator().find(type);
459 /////////////////////////////////////////////////////////////////////////
463 /////////////////////////////////////////////////////////////////////////
467 InsetInfo::InsetInfo(Buffer * buf, string const & info)
468 : InsetCollapsible(buf), initialized_(false)
470 params_.type = InsetInfoParams::UNKNOWN_INFO;
471 params_.force_ltr = false;
477 Inset * InsetInfo::editXY(Cursor & cur, int x, int y)
479 // do not allow the cursor to be set in this Inset
480 return Inset::editXY(cur, x, y);
484 docstring InsetInfo::layoutName() const
486 return from_ascii("Info:" + params_.infoType());
490 docstring InsetInfo::toolTip(BufferView const &, int, int) const
493 switch (nameTranslator().find(params_.infoType())) {
494 case InsetInfoParams::UNKNOWN_INFO:
495 result = _("Invalid information inset");
497 case InsetInfoParams::SHORTCUT_INFO:
498 result = bformat(_("The keybard shortcut for the function '%1$s'"),
499 from_utf8(params_.name));
501 case InsetInfoParams::SHORTCUTS_INFO:
502 result = bformat(_("The keybard shortcuts for the function '%1$s'"),
503 from_utf8(params_.name));
505 case InsetInfoParams::MENU_INFO:
506 result = bformat(_("The menu location for the function '%1$s'"),
507 from_utf8(params_.name));
509 case InsetInfoParams::L7N_INFO:
510 result = bformat(_("The localization for the string '%1$s'"),
511 from_utf8(params_.name));
513 case InsetInfoParams::ICON_INFO:
514 result = bformat(_("The toolbar icon for the function '%1$s'"),
515 from_utf8(params_.name));
517 case InsetInfoParams::LYXRC_INFO:
518 result = bformat(_("The preference setting for the preference key '%1$s'"),
519 from_utf8(params_.name));
521 case InsetInfoParams::PACKAGE_INFO:
522 result = bformat(_("Availability of the LaTeX package '%1$s'"),
523 from_utf8(params_.name));
525 case InsetInfoParams::TEXTCLASS_INFO:
526 result = bformat(_("Availability of the LaTeX class '%1$s'"),
527 from_utf8(params_.name));
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");
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");
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");
559 case InsetInfoParams::DATE_INFO:
560 result = _("The current date");
562 case InsetInfoParams::MODDATE_INFO:
563 result = _("The date of last save");
565 case InsetInfoParams::FIXDATE_INFO:
566 result = _("A static date");
568 case InsetInfoParams::TIME_INFO:
569 result = _("The current time");
571 case InsetInfoParams::MODTIME_INFO:
572 result = _("The time of last save");
574 case InsetInfoParams::FIXTIME_INFO:
575 result = _("A static time");
583 void InsetInfo::read(Lexer & lex)
588 token = lex.getString();
589 if (token == "type") {
591 token = lex.getString();
592 params_.type = nameTranslator().find(token);
593 } else if (token == "arg") {
595 params_.name = lex.getString();
596 } else if (token == "\\end_inset")
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."),
608 void InsetInfo::write(ostream & os) const
610 os << "Info\ntype \"" << params_.infoType()
611 << "\"\narg " << Lexer::quoteString(params_.name);
615 bool InsetInfo::showInsetDialog(BufferView * bv) const
617 bv->showDialog("info");
622 bool InsetInfo::getStatus(Cursor & cur, FuncRequest const & cmd,
623 FuncStatus & flag) const
625 switch (cmd.action()) {
626 case LFUN_INSET_SETTINGS:
627 return InsetCollapsible::getStatus(cur, cmd, flag);
629 case LFUN_INSET_DIALOG_UPDATE:
630 case LFUN_INSET_COPY_AS:
631 case LFUN_INSET_DISSOLVE:
632 flag.setEnabled(true);
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);
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);
658 void InsetInfo::doDispatch(Cursor & cur, FuncRequest & cmd)
660 switch (cmd.action()) {
661 case LFUN_INSET_MODIFY:
662 if (nameTranslator().find(cmd.getArg(0)) == InsetInfoParams::UNKNOWN_INFO) {
667 setInfo(to_utf8(cmd.argument()));
668 cur.forceBufferUpdate();
669 initialized_ = false;
672 case LFUN_INSET_COPY_AS: {
673 cap::clearSelection();
675 copy.pushBackward(*this);
679 copy.pit() = copy.lastpit();
680 copy.pos() = copy.lastpos();
682 cap::copySelection(copy);
687 InsetCollapsible::doDispatch(cur, cmd);
693 void InsetInfo::setInfo(string const & info)
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, '@');
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,
713 if (date_specifier.empty()) {
714 if (saved_date_specifier.empty())
715 params_.name += "@" + fromqstr(QDate::currentDate().toString(Qt::ISODate));
717 params_.name += "@" + saved_date_specifier;
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,
725 if (time_specifier.empty()) {
726 if (saved_date_specifier.empty())
727 params_.name += "@" + fromqstr(QTime::currentTime().toString(Qt::ISODate));
729 params_.name += "@" + saved_date_specifier;
735 void InsetInfo::error(docstring const & err, Language const * lang)
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);
745 void InsetInfo::info(docstring const & err, Language const * lang)
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);
755 void InsetInfo::setText(docstring const & str, Language const * lang)
757 setText(str, Font(inherit_font, lang), false);
761 bool InsetInfo::forceLTR(OutputParams const &) const
763 return params_.force_ltr;
767 bool InsetInfo::forceLocalFontSwitch() const
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;
776 void InsetInfo::metrics(MetricsInfo & mi, Dimension & dim) const
778 const_cast<InsetInfo *>(this)->build();
779 InsetCollapsible::metrics(mi, dim);
783 void InsetInfo::draw(PainterInfo & pi, int x, int y) const
785 Changer chg = changeVar(lyxrc.mark_foreign_language, false);
786 InsetCollapsible::draw(pi, x, y);
789 void InsetInfo::updateBuffer(ParIterator const & it, UpdateType utype, bool const deleted)
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())
799 BufferParams const & bp = buffer().params();
800 params_.lang = it.paragraph().getFontSettings(bp, it.pos()).language();
801 InsetCollapsible::updateBuffer(it, utype, deleted);
805 void InsetInfo::build()
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())
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;
819 params_.force_ltr = !params_.lang->rightToLeft();
820 // This is just to get the string into the po files
822 switch (params_.type) {
823 case InsetInfoParams::UNKNOWN_INFO:
824 gui = _("Unknown Info!");
825 info(from_ascii("Unknown Info!"), params_.lang);
826 initialized_ = false;
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);
837 KeyMap::Bindings bindings = theTopLevelKeymap().findBindings(func);
838 if (bindings.empty()) {
839 gui = _("undefined");
840 info(from_ascii("undefined"), params_.lang);
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);
849 sequence = theTopLevelKeymap().printBindings(func, KeySequence::ForGui);
850 seq_untranslated = theTopLevelKeymap().printBindings(func, KeySequence::ForGui, true);
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();
858 bool is_translated = sequence != seq_untranslated;
859 for (char_type const c : sequence) {
862 gui = _("Return[[Key]]");
863 is_translated = translateString(from_ascii("Return[[Key]]"), trans, lcode);
866 case 0x21b9://Tab both directions (Win)
867 gui = _("Tab[[Key]]");
868 is_translated = translateString(from_ascii("Tab[[Key]]"), trans, lcode);
871 case 0x21de://Qt::Key_PageUp
873 is_translated = translateString(from_ascii("PgUp"), trans, lcode);
876 case 0x21df://Qt::Key_PageDown
878 is_translated = translateString(from_ascii("PgDown"), trans, lcode);
881 case 0x21e4://Qt::Key_Backtab
883 is_translated = translateString(from_ascii("Backtab"), trans, lcode);
886 case 0x21e5://Qt::Key_Tab
888 is_translated = translateString(from_ascii("Tab"), trans, lcode);
893 is_translated = translateString(from_ascii("Shift"), trans, lcode);
896 case 0x21ea://Qt::Key_CapsLock
898 is_translated = translateString(from_ascii("CapsLock"), trans, lcode);
901 case 0x2303://Control
902 gui = _("Control[[Key]]");
903 is_translated = translateString(from_ascii("Control[[Key]]"), trans, lcode);
907 gui = _("Command[[Key]]");
908 is_translated = translateString(from_ascii("Command[[Key]]"), trans, lcode);
911 case 0x2324://Qt::Key_Enter
912 gui = _("Return[[Key]]");
913 is_translated = translateString(from_ascii("Return[[Key]]"), trans, lcode);
916 case 0x2325://Option key
917 gui = _("Option[[Key]]");
918 is_translated = translateString(from_ascii("Option[[Key]]"), trans, lcode);
921 case 0x2326://Qt::Key_Delete
922 gui = _("Delete[[Key]]");
923 is_translated = translateString(from_ascii("Delete[[Key]]"), trans, lcode);
926 case 0x232b://Qt::Key_Backspace
928 is_translated = translateString(from_ascii("Fn+Del"), trans, lcode);
931 case 0x238b://Qt::Key_Escape
933 is_translated = translateString(from_ascii("Esc"), trans, lcode);
940 setText(ods.str(), is_translated ? guilang : nullptr);
941 params_.force_ltr = !is_translated || (!guilang->rightToLeft() && !params_.lang->rightToLeft());
944 case InsetInfoParams::LYXRC_INFO: {
945 // this information could change, if the preferences are changed,
946 // so we will recalculate each time through.
948 if (params_.name.empty()) {
949 gui = _("undefined");
950 info(from_ascii("undefined"), params_.lang);
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);
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);
970 // remove leading comments and \\name and space
971 result = result.substr(loc + params_.name.size() + 2);
974 result = rtrim(result, "\n");
975 result = trim(result, "\"");
979 setText(from_utf8(result), params_.lang);
982 case InsetInfoParams::PACKAGE_INFO:
983 // only need to do this once.
986 // check in packages.lst
987 if (LaTeXFeatures::isAvailable(params_.name)) {
989 info(from_ascii("yes"), params_.lang);
992 info(from_ascii("no"), params_.lang);
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();
1006 info(from_ascii("yes"), params_.lang);
1009 info(from_ascii("no"), params_.lang);
1013 case InsetInfoParams::MENU_INFO: {
1014 // only need to do this once.
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);
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
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;
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);
1043 // if found, return its path.
1045 Paragraph & par = paragraphs().front();
1046 Font const f(inherit_font, guilang);
1047 params_.force_ltr = !guilang->rightToLeft();
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));
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;
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();
1083 case InsetInfoParams::ICON_INFO: {
1084 // only need to do this once.
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"));
1100 int percent_scale = 100;
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();
1108 int iconsize = Length(1, Length::EM).inPixels(1);
1109 percent_scale = (100 * iconsize + imgsize / 2)/imgsize;
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);
1124 Font const f(inherit_font, params_.lang);
1125 paragraphs().front().insertInset(0, inset, f,
1126 Change(Change::UNCHANGED));
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);
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);
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);
1167 setText(from_utf8(binfo), params_.lang);
1170 case InsetInfoParams::LYX_INFO:
1171 // only need to do this once.
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;
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();
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();
1192 date = QDateTime::fromTime_t(buffer().fileName().lastModified()).date();
1194 else if (params_.type == InsetInfoParams::FIXDATE_INFO && !date_specifier.empty())
1195 date = QDate::fromString(toqstr(date_specifier), Qt::ISODate);
1197 date = QDate::currentDate();
1198 setText(params_.getDate(date_format, date), params_.lang);
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();
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();
1213 time = QDateTime::fromTime_t(buffer().fileName().lastModified()).time();
1215 else if (params_.type == InsetInfoParams::FIXTIME_INFO && !time_specifier.empty())
1216 time = QTime::fromString(toqstr(time_specifier), Qt::ISODate);
1218 time = QTime::currentTime();
1219 setText(params_.getTime(time_format, time), params_.lang);
1224 // Just to do something with that string
1225 LYXERR(Debug::INFO, "info inset text: " << gui);
1229 void InsetInfo::validate(LaTeXFeatures & features) const
1231 const_cast<InsetInfo *>(this)->build();
1232 InsetCollapsible::validate(features);
1236 string InsetInfo::contextMenu(BufferView const &, int, int) const
1238 //FIXME: We override the implementation of InsetCollapsible,
1239 //because this inset is not a collapsible inset.
1240 return contextMenuName();
1244 string InsetInfo::contextMenuName() const
1246 return "context-info";