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"
18 #include "CutAndPaste.h"
20 #include "FuncRequest.h"
21 #include "FuncStatus.h"
22 #include "InsetGraphics.h"
23 #include "InsetSpecialChar.h"
25 #include "LaTeXFeatures.h"
27 #include "LayoutFile.h"
29 #include "LyXAction.h"
33 #include "Paragraph.h"
34 #include "ParIterator.h"
35 #include "ParagraphParameters.h"
38 #include "frontends/Application.h"
40 #include "support/convert.h"
41 #include "support/debug.h"
42 #include "support/docstream.h"
43 #include "support/docstring_list.h"
44 #include "support/ExceptionMessage.h"
45 #include "support/FileName.h"
46 #include "support/filetools.h"
47 #include "support/gettext.h"
48 #include "support/Messages.h"
49 #include "support/lstrings.h"
50 #include "support/qstring_helpers.h"
51 #include "support/Translator.h"
55 #include <QtGui/QImage>
60 using namespace lyx::support;
66 typedef Translator<InsetInfoParams::info_type, string> NameTranslator;
68 NameTranslator const initTranslator()
70 NameTranslator translator(InsetInfoParams::UNKNOWN_INFO, "unknown");
72 translator.addPair(InsetInfoParams::SHORTCUTS_INFO, "shortcuts");
73 translator.addPair(InsetInfoParams::SHORTCUT_INFO, "shortcut");
74 translator.addPair(InsetInfoParams::LYXRC_INFO, "lyxrc");
75 translator.addPair(InsetInfoParams::PACKAGE_INFO, "package");
76 translator.addPair(InsetInfoParams::TEXTCLASS_INFO, "textclass");
77 translator.addPair(InsetInfoParams::MENU_INFO, "menu");
78 translator.addPair(InsetInfoParams::L7N_INFO, "l7n");
79 translator.addPair(InsetInfoParams::ICON_INFO, "icon");
80 translator.addPair(InsetInfoParams::BUFFER_INFO, "buffer");
81 translator.addPair(InsetInfoParams::LYX_INFO, "lyxinfo");
82 translator.addPair(InsetInfoParams::VCS_INFO, "vcs");
83 translator.addPair(InsetInfoParams::DATE_INFO, "date");
84 translator.addPair(InsetInfoParams::MODDATE_INFO, "moddate");
85 translator.addPair(InsetInfoParams::FIXDATE_INFO, "fixdate");
86 translator.addPair(InsetInfoParams::TIME_INFO, "time");
87 translator.addPair(InsetInfoParams::MODTIME_INFO, "modtime");
88 translator.addPair(InsetInfoParams::FIXTIME_INFO, "fixtime");
93 /// The translator between the information type enum and corresponding string.
94 NameTranslator const & nameTranslator()
96 static NameTranslator const translator = initTranslator();
101 typedef Translator<InsetInfoParams::info_type, string> DefaultValueTranslator;
103 DefaultValueTranslator const initDVTranslator()
105 DefaultValueTranslator translator(InsetInfoParams::UNKNOWN_INFO, "");
107 translator.addPair(InsetInfoParams::SHORTCUTS_INFO, "info-insert");
108 translator.addPair(InsetInfoParams::SHORTCUT_INFO, "info-insert");
109 translator.addPair(InsetInfoParams::LYXRC_INFO, "user_name");
110 translator.addPair(InsetInfoParams::PACKAGE_INFO, "graphics");
111 translator.addPair(InsetInfoParams::TEXTCLASS_INFO, "article");
112 translator.addPair(InsetInfoParams::MENU_INFO, "info-insert");
113 translator.addPair(InsetInfoParams::L7N_INFO, "");
114 translator.addPair(InsetInfoParams::ICON_INFO, "info-insert");
115 translator.addPair(InsetInfoParams::BUFFER_INFO, "name-noext");
116 translator.addPair(InsetInfoParams::LYX_INFO, "version");
117 translator.addPair(InsetInfoParams::VCS_INFO, "revision");
118 translator.addPair(InsetInfoParams::DATE_INFO, "loclong");
119 translator.addPair(InsetInfoParams::MODDATE_INFO, "loclong");
120 translator.addPair(InsetInfoParams::FIXDATE_INFO, "loclong");
121 translator.addPair(InsetInfoParams::TIME_INFO, "long");
122 translator.addPair(InsetInfoParams::MODTIME_INFO, "long");
123 translator.addPair(InsetInfoParams::FIXTIME_INFO, "long");
128 /// The translator between the information type enum and some sensible default value.
129 DefaultValueTranslator const & defaultValueTranslator()
131 static DefaultValueTranslator const translator = initDVTranslator();
138 /////////////////////////////////////////////////////////////////////
142 ///////////////////////////////////////////////////////////////////////
144 InsetInfoParams infoparams;
147 set<string> getTexFileList(string const & filename)
150 FileName const file = libFileSearch(string(), filename);
155 vector<docstring> doclist =
156 getVectorFromString(file.fileContents("UTF-8"), from_ascii("\n"));
158 // Normalise paths like /foo//bar ==> /foo/bar
159 for (auto doc : doclist) {
160 doc = subst(doc, from_ascii("\r"), docstring());
161 while (contains(doc, from_ascii("//")))
162 doc = subst(doc, from_ascii("//"), from_ascii("/"));
164 list.insert(removeExtension(onlyFileName(to_utf8(doc))));
171 bool translateString(docstring const in, docstring & out, string const lcode)
173 out = translateIfPossible(in, lcode);
179 docstring InsetInfoParams::getDate(string const iname, QDate const date) const
183 loc = QLocale(toqstr(lang->code()));
185 return qstring_to_ucs4(loc.toString(date, QLocale::LongFormat));
186 else if (iname == "short")
187 return qstring_to_ucs4(loc.toString(date, QLocale::ShortFormat));
188 else if (iname == "ISO")
189 return qstring_to_ucs4(date.toString(Qt::ISODate));
190 else if (iname == "loclong")
191 return lang ? qstring_to_ucs4(loc.toString(date, toqstr(lang->dateFormat(0))))
192 : _("No long date format (language unknown)!");
193 else if (iname == "locmedium")
194 return lang ? qstring_to_ucs4(loc.toString(date, toqstr(lang->dateFormat(1))))
195 : _("No medium date format (language unknown)!");
196 else if (iname == "locshort")
197 return lang ? qstring_to_ucs4(loc.toString(date, toqstr(lang->dateFormat(2))))
198 : _("No short date format (language unknown)!");
200 return qstring_to_ucs4(loc.toString(date, toqstr(iname)));
204 docstring InsetInfoParams::getTime(string const iname, QTime const time) const
208 loc = QLocale(toqstr(lang->code()));
210 return qstring_to_ucs4(loc.toString(time, QLocale::LongFormat));
211 else if (iname == "short")
212 return qstring_to_ucs4(loc.toString(time, QLocale::ShortFormat));
213 else if (iname == "ISO")
214 return qstring_to_ucs4(time.toString(Qt::ISODate));
216 return qstring_to_ucs4(loc.toString(time, toqstr(iname)));
220 vector<pair<string,docstring>> InsetInfoParams::getArguments(Buffer const * buf,
221 string const & itype) const
223 vector<pair<string,docstring>> result;
225 switch (nameTranslator().find(itype)) {
227 result.push_back(make_pair("invalid", _("Please select a valid type!")));
234 result.push_back(make_pair("custom", _("Custom")));
235 LyXAction::const_iterator fit = lyxaction.func_begin();
236 LyXAction::const_iterator const fen = lyxaction.func_end();
237 for (; fit != fen; ++fit) {
238 string const lfun = fit->first;
240 result.push_back(make_pair(lfun, from_ascii(lfun)));
246 result.push_back(make_pair("custom", _("Custom")));
250 result.push_back(make_pair("custom", _("Custom")));
251 set<string> rcs = lyxrc.getRCs();
252 for (auto const & rc : rcs)
253 result.push_back(make_pair(rc, from_ascii(rc)));
258 case TEXTCLASS_INFO: {
259 result.push_back(make_pair("custom", _("Custom")));
260 string const filename = (itype == "package") ? "styFiles.lst"
262 set<string> flist = getTexFileList(filename);
263 for (auto const & f : flist)
264 result.push_back(make_pair(f, from_utf8(f)));
269 result.push_back(make_pair("name", _("File name (with extension)")));
270 result.push_back(make_pair("name-noext", _("File name (without extension)")));
271 result.push_back(make_pair("path", _("File path")));
272 result.push_back(make_pair("class", _("Used text class")));
276 if (!buf->lyxvc().inUse()) {
277 result.push_back(make_pair("invalid", _("No version control!")));
280 result.push_back(make_pair("revision", _("Revision[[Version Control]]")));
281 result.push_back(make_pair("tree-revision", _("Tree revision")));
282 result.push_back(make_pair("author", _("Author")));
283 result.push_back(make_pair("date", _("Date")));
284 result.push_back(make_pair("time", _("Time[[of day]]")));
289 result.push_back(make_pair("version", _("LyX version")));
290 result.push_back(make_pair("layoutformat", _("LyX layout format")));
296 string const dt = split(name, '@');
298 if (itype == "moddate")
299 date = QDateTime::fromTime_t(buf->fileName().lastModified()).date();
300 else if (itype == "fixdate" && !dt.empty()) {
301 QDate const gdate = QDate::fromString(toqstr(dt), Qt::ISODate);
302 date = (gdate.isValid()) ? gdate : QDate::currentDate();
304 date = QDate::currentDate();
305 result.push_back(make_pair("long",getDate("long", date)));
306 result.push_back(make_pair("short", getDate("short", date)));
307 result.push_back(make_pair("loclong", getDate("loclong", date)));
308 result.push_back(make_pair("locmedium", getDate("locmedium", date)));
309 result.push_back(make_pair("locshort", getDate("locshort", date)));
310 result.push_back(make_pair("ISO", getDate("ISO", date)));
311 result.push_back(make_pair("yyyy", getDate("yyyy", date)));
312 result.push_back(make_pair("MMMM", getDate("MMMM", date)));
313 result.push_back(make_pair("MMM", getDate("MMM", date)));
314 result.push_back(make_pair("dddd", getDate("dddd", date)));
315 result.push_back(make_pair("ddd", getDate("ddd", date)));
316 result.push_back(make_pair("custom", _("Custom")));
322 string const tt = split(name, '@');
324 if (itype == "modtime")
325 time = QDateTime::fromTime_t(buf->fileName().lastModified()).time();
326 else if (itype == "fixtime" && !tt.empty()) {
327 QTime const gtime = QTime::fromString(toqstr(tt), Qt::ISODate);
328 time = (gtime.isValid()) ? gtime : QTime::currentTime();
330 time = QTime::currentTime();
331 result.push_back(make_pair("long",getTime("long", time)));
332 result.push_back(make_pair("short", getTime("short", time)));
333 result.push_back(make_pair("ISO", getTime("ISO", time)));
334 result.push_back(make_pair("custom", _("Custom")));
343 bool InsetInfoParams::validateArgument(Buffer const * buf, docstring const & arg,
344 bool const usedefaults) const
347 string name = trim(split(to_utf8(arg), type, ' '));
348 if (name.empty() && usedefaults)
349 name = defaultValueTranslator().find(type);
351 switch (nameTranslator().find(type)) {
358 FuncRequest func = lyxaction.lookupFunc(name);
359 return func.action() != LFUN_UNKNOWN_ACTION;
363 return !name.empty();
366 FuncCode const action = lyxaction.lookupFunc(name).action();
367 if (action == LFUN_UNKNOWN_ACTION) {
368 string dir = "images";
369 return !imageLibFileSearch(dir, name, "svgz,png").empty();
375 set<string> rcs = lyxrc.getRCs();
376 return rcs.find(name) != rcs.end();
384 return (name == "name" || name == "name-noext"
385 || name == "path" || name == "class");
388 if (name == "revision" || name == "tree-revision"
389 || name == "author" || name == "date" || name == "time")
390 return buf->lyxvc().inUse();
394 return name == "version" || name == "layoutformat";
399 date = split(name, piece, '@');
400 if (!date.empty() && !QDate::fromString(toqstr(date), Qt::ISODate).isValid())
408 if (name == "long" || name == "short" || name == "ISO")
411 QDate date = QDate::currentDate();
412 return !date.toString(toqstr(name)).isEmpty();
418 time = split(name, piece, '@');
419 if (!time.empty() && !QTime::fromString(toqstr(time), Qt::ISODate).isValid())
427 if (name == "long" || name == "short" || name == "ISO")
430 QTime time = QTime::currentTime();
431 return !time.toString(toqstr(name)).isEmpty();
442 string InsetInfoParams::infoType() const
444 return nameTranslator().find(type);
449 /////////////////////////////////////////////////////////////////////////
453 /////////////////////////////////////////////////////////////////////////
457 InsetInfo::InsetInfo(Buffer * buf, string const & name)
458 : InsetCollapsible(buf), initialized_(false)
460 params_.type = InsetInfoParams::UNKNOWN_INFO;
461 params_.force_ltr = false;
467 Inset * InsetInfo::editXY(Cursor & cur, int x, int y)
469 // do not allow the cursor to be set in this Inset
470 return Inset::editXY(cur, x, y);
474 docstring InsetInfo::layoutName() const
476 return from_ascii("Info:" + params_.infoType());
480 docstring InsetInfo::toolTip(BufferView const &, int, int) const
483 switch (nameTranslator().find(params_.infoType())) {
484 case InsetInfoParams::UNKNOWN_INFO:
485 result = _("Invalid information inset");
487 case InsetInfoParams::SHORTCUT_INFO:
488 result = bformat(_("The keybard shortcut for the function '%1$s'"),
489 from_utf8(params_.name));
491 case InsetInfoParams::SHORTCUTS_INFO:
492 result = bformat(_("The keybard shortcuts for the function '%1$s'"),
493 from_utf8(params_.name));
495 case InsetInfoParams::MENU_INFO:
496 result = bformat(_("The menu location for the function '%1$s'"),
497 from_utf8(params_.name));
499 case InsetInfoParams::L7N_INFO:
500 result = bformat(_("The localization for the string '%1$s'"),
501 from_utf8(params_.name));
503 case InsetInfoParams::ICON_INFO:
504 result = bformat(_("The toolbar icon for the function '%1$s'"),
505 from_utf8(params_.name));
507 case InsetInfoParams::LYXRC_INFO:
508 result = bformat(_("The preference setting for the preference key '%1$s'"),
509 from_utf8(params_.name));
511 case InsetInfoParams::PACKAGE_INFO:
512 result = bformat(_("Availability of the LaTeX package '%1$s'"),
513 from_utf8(params_.name));
515 case InsetInfoParams::TEXTCLASS_INFO:
516 result = bformat(_("Availability of the LaTeX class '%1$s'"),
517 from_utf8(params_.name));
519 case InsetInfoParams::BUFFER_INFO:
520 if (params_.name == "name")
521 result = _("The name of this file (incl. extension)");
522 else if (params_.name == "name-noext")
523 result = _("The name of this file (without extension)");
524 else if (params_.name == "path")
525 result = _("The path where this file is saved");
526 else if (params_.name == "class")
527 result = _("The class this document uses");
529 case InsetInfoParams::VCS_INFO:
530 if (params_.name == "revision")
531 result = _("Version control revision");
532 else if (params_.name == "tree-revision")
533 result = _("Version control tree revision");
534 else if (params_.name == "author")
535 result = _("Version control author");
536 else if (params_.name == "date")
537 result = _("Version control date");
538 else if (params_.name == "time")
539 result = _("Version control time");
541 case InsetInfoParams::LYX_INFO:
542 if (params_.name == "version")
543 result = _("The current LyX version");
544 else if (params_.name == "layoutformat")
545 result = _("The current LyX layout format");
547 case InsetInfoParams::DATE_INFO:
548 result = _("The current date");
550 case InsetInfoParams::MODDATE_INFO:
551 result = _("The date of last save");
553 case InsetInfoParams::FIXDATE_INFO:
554 result = _("A static date");
556 case InsetInfoParams::TIME_INFO:
557 result = _("The current time");
559 case InsetInfoParams::MODTIME_INFO:
560 result = _("The time of last save");
562 case InsetInfoParams::FIXTIME_INFO:
563 result = _("A static time");
571 void InsetInfo::read(Lexer & lex)
576 token = lex.getString();
577 if (token == "type") {
579 token = lex.getString();
580 params_.type = nameTranslator().find(token);
581 } else if (token == "arg") {
583 params_.name = lex.getString();
584 } else if (token == "\\end_inset")
587 if (token != "\\end_inset") {
588 lex.printError("Missing \\end_inset at this point");
589 throw ExceptionMessage(WarningException,
590 _("Missing \\end_inset at this point."),
596 void InsetInfo::write(ostream & os) const
598 os << "Info\ntype \"" << params_.infoType()
599 << "\"\narg " << Lexer::quoteString(params_.name);
603 bool InsetInfo::showInsetDialog(BufferView * bv) const
605 bv->showDialog("info");
610 bool InsetInfo::getStatus(Cursor & cur, FuncRequest const & cmd,
611 FuncStatus & flag) const
613 switch (cmd.action()) {
614 case LFUN_INSET_SETTINGS:
615 return InsetCollapsible::getStatus(cur, cmd, flag);
617 case LFUN_INSET_DIALOG_UPDATE:
618 case LFUN_INSET_COPY_AS:
619 case LFUN_INSET_DISSOLVE:
620 flag.setEnabled(true);
623 case LFUN_INSET_MODIFY:
624 if (params_.validateArgument(&buffer(), cmd.argument())) {
625 flag.setEnabled(true);
627 string name = trim(split(to_utf8(cmd.argument()), typestr, ' '));
628 InsetInfoParams::info_type type = nameTranslator().find(typestr);
629 string origname = params_.name;
630 if (type == InsetInfoParams::FIXDATE_INFO
631 || type == InsetInfoParams::FIXTIME_INFO)
632 split(params_.name, origname, '@');
633 flag.setOnOff(type == params_.type && name == origname);
644 void InsetInfo::doDispatch(Cursor & cur, FuncRequest & cmd)
646 switch (cmd.action()) {
647 case LFUN_INSET_MODIFY:
649 setInfo(to_utf8(cmd.argument()));
650 cur.forceBufferUpdate();
651 initialized_ = false;
654 case LFUN_INSET_COPY_AS: {
655 cap::clearSelection();
657 copy.pushBackward(*this);
661 copy.pit() = copy.lastpit();
662 copy.pos() = copy.lastpos();
664 cap::copySelection(copy);
669 InsetCollapsible::doDispatch(cur, cmd);
675 void InsetInfo::setInfo(string const & name)
680 string saved_date_specifier;
681 // Store old date specifier for potential re-use
682 if (!params_.name.empty())
683 saved_date_specifier = split(params_.name, '@');
686 params_.name = trim(split(name, type, ' '));
687 params_.type = nameTranslator().find(type);
688 if (params_.name.empty())
689 params_.name = defaultValueTranslator().find(params_.type);
690 if (params_.type == InsetInfoParams::FIXDATE_INFO) {
691 string const date_specifier = split(params_.name, '@');
692 // If an explicit new fix date is specified, use that
693 // Otherwise, use the old one or, if there is none,
695 if (date_specifier.empty()) {
696 if (saved_date_specifier.empty())
697 params_.name += "@" + fromqstr(QDate::currentDate().toString(Qt::ISODate));
699 params_.name += "@" + saved_date_specifier;
702 else if (params_.type == InsetInfoParams::FIXTIME_INFO) {
703 string const time_specifier = split(params_.name, '@');
704 // If an explicit new fix time is specified, use that
705 // Otherwise, use the old one or, if there is none,
707 if (time_specifier.empty()) {
708 if (saved_date_specifier.empty())
709 params_.name += "@" + fromqstr(QTime::currentTime().toString(Qt::ISODate));
711 params_.name += "@" + saved_date_specifier;
717 void InsetInfo::error(docstring const & err, Language const * lang)
719 docstring const res = translateIfPossible(err, lang->code());
720 bool const translated = res != err;
721 // If the string is not translated, we use default lang (English)
722 Font const f = translated ? Font(inherit_font, lang) : Font(inherit_font);
723 setText(bformat(res, from_utf8(params_.name)), f, false);
727 void InsetInfo::info(docstring const & err, Language const * lang)
729 docstring const res = translateIfPossible(err, lang->code());
730 bool const translated = res != err;
731 // If the string is not translated, we use default lang (English)
732 Font const f = translated ? Font(inherit_font, lang) : Font(inherit_font);
733 setText(translateIfPossible(err, lang->code()), f, false);
737 void InsetInfo::setText(docstring const & str, Language const * lang)
739 setText(str, Font(inherit_font, lang), false);
743 bool InsetInfo::forceLTR() const
745 return params_.force_ltr;
749 void InsetInfo::updateBuffer(ParIterator const & it, UpdateType utype) {
750 // If the Buffer is a clone, then we neither need nor want to do any
751 // of what follows. We want, rather, just to inherit how things were
752 // in the original Buffer. This is especially important for VCS.
753 // Otherwise, we could in principle have different settings here
754 // than in the Buffer we were exporting.
755 if (buffer().isClone())
758 BufferParams const & bp = buffer().params();
759 params_.lang = it.paragraph().getFontSettings(bp, it.pos()).language();
760 Language const * tryguilang = languages.getFromCode(Messages::guiLanguage());
761 // Some info insets use the language of the GUI (if available)
762 Language const * guilang = tryguilang ? tryguilang : params_.lang;
764 params_.force_ltr = !params_.lang->rightToLeft();
765 // This is just to get the string into the po files
767 switch (params_.type) {
768 case InsetInfoParams::UNKNOWN_INFO:
769 gui = _("Unknown Info!");
770 info(from_ascii("Unknown Info!"), params_.lang);
771 initialized_ = false;
773 case InsetInfoParams::SHORTCUT_INFO:
774 case InsetInfoParams::SHORTCUTS_INFO: {
775 // shortcuts can change, so we need to re-do this each time
776 FuncRequest const func = lyxaction.lookupFunc(params_.name);
777 if (func.action() == LFUN_UNKNOWN_ACTION) {
778 gui = _("Unknown action %1$s");
779 error(from_ascii("Unknown action %1$s"), params_.lang);
782 KeyMap::Bindings bindings = theTopLevelKeymap().findBindings(func);
783 if (bindings.empty()) {
784 gui = _("undefined");
785 info(from_ascii("undefined"), params_.lang);
789 docstring seq_untranslated;
790 if (params_.type == InsetInfoParams::SHORTCUT_INFO) {
791 sequence = bindings.begin()->print(KeySequence::ForGui);
792 seq_untranslated = bindings.begin()->print(KeySequence::ForGui, true);
794 sequence = theTopLevelKeymap().printBindings(func, KeySequence::ForGui);
795 seq_untranslated = theTopLevelKeymap().printBindings(func, KeySequence::ForGui, true);
797 // QKeySequence returns special characters for keys on the mac
798 // Since these are not included in many fonts, we
799 // re-translate them to textual names (see #10641)
800 odocstringstream ods;
801 string const lcode = params_.lang->code();
803 bool is_translated = sequence != seq_untranslated;
804 for (size_t n = 0; n < sequence.size(); ++n) {
805 char_type const c = sequence[n];
808 gui = _("Return[[Key]]");
809 is_translated = translateString(from_ascii("Return[[Key]]"), trans, lcode);
812 case 0x21b9://Tab both directions (Win)
813 gui = _("Tab[[Key]]");
814 is_translated = translateString(from_ascii("Tab[[Key]]"), trans, lcode);
817 case 0x21de://Qt::Key_PageUp
819 is_translated = translateString(from_ascii("PgUp"), trans, lcode);
822 case 0x21df://Qt::Key_PageDown
824 is_translated = translateString(from_ascii("PgDown"), trans, lcode);
827 case 0x21e4://Qt::Key_Backtab
829 is_translated = translateString(from_ascii("Backtab"), trans, lcode);
832 case 0x21e5://Qt::Key_Tab
834 is_translated = translateString(from_ascii("Tab"), trans, lcode);
839 is_translated = translateString(from_ascii("Shift"), trans, lcode);
842 case 0x21ea://Qt::Key_CapsLock
844 is_translated = translateString(from_ascii("CapsLock"), trans, lcode);
847 case 0x2303://Control
848 gui = _("Control[[Key]]");
849 is_translated = translateString(from_ascii("Control[[Key]]"), trans, lcode);
853 gui = _("Command[[Key]]");
854 is_translated = translateString(from_ascii("Command[[Key]]"), trans, lcode);
857 case 0x2324://Qt::Key_Enter
858 gui = _("Return[[Key]]");
859 is_translated = translateString(from_ascii("Return[[Key]]"), trans, lcode);
862 case 0x2325://Option key
863 gui = _("Option[[Key]]");
864 is_translated = translateString(from_ascii("Option[[Key]]"), trans, lcode);
867 case 0x2326://Qt::Key_Delete
868 gui = _("Delete[[Key]]");
869 is_translated = translateString(from_ascii("Delete[[Key]]"), trans, lcode);
872 case 0x232b://Qt::Key_Backspace
874 is_translated = translateString(from_ascii("Fn+Del"), trans, lcode);
877 case 0x238b://Qt::Key_Escape
879 is_translated = translateString(from_ascii("Esc"), trans, lcode);
886 setText(ods.str(), is_translated ? guilang : nullptr);
887 params_.force_ltr = !is_translated || (!guilang->rightToLeft() && !params_.lang->rightToLeft());
890 case InsetInfoParams::LYXRC_INFO: {
891 // this information could change, if the preferences are changed,
892 // so we will recalculate each time through.
894 if (params_.name.empty()) {
895 gui = _("undefined");
896 info(from_ascii("undefined"), params_.lang);
899 // FIXME this uses the serialization mechanism to get the info
900 // we want, which i guess works but is a bit strange.
901 lyxrc.write(oss, true, params_.name);
902 string result = oss.str();
903 if (result.size() < 2) {
904 gui = _("undefined");
905 info(from_ascii("undefined"), params_.lang);
908 string::size_type loc = result.rfind("\n", result.size() - 2);
909 loc = loc == string::npos ? 0 : loc + 1;
910 if (result.size() < loc + params_.name.size() + 1
911 || result.substr(loc + 1, params_.name.size()) != params_.name) {
912 gui = _("undefined");
913 info(from_ascii("undefined"), params_.lang);
916 // remove leading comments and \\name and space
917 result = result.substr(loc + params_.name.size() + 2);
920 result = rtrim(result, "\n");
921 result = trim(result, "\"");
925 setText(from_utf8(result), params_.lang);
928 case InsetInfoParams::PACKAGE_INFO:
929 // only need to do this once.
932 // check in packages.lst
933 if (LaTeXFeatures::isAvailable(params_.name)) {
935 info(from_ascii("yes"), params_.lang);
938 info(from_ascii("no"), params_.lang);
943 case InsetInfoParams::TEXTCLASS_INFO: {
944 // the TextClass can change
945 LayoutFileList const & list = LayoutFileList::get();
946 bool available = false;
947 // params_.name is the class name
948 if (list.haveClass(params_.name))
949 available = list[params_.name].isTeXClassAvailable();
952 info(from_ascii("yes"), params_.lang);
955 info(from_ascii("no"), params_.lang);
959 case InsetInfoParams::MENU_INFO: {
960 // only need to do this once.
963 docstring_list names;
964 FuncRequest func = lyxaction.lookupFunc(params_.name);
965 if (func.action() == LFUN_UNKNOWN_ACTION) {
966 gui = _("Unknown action %1$s");
967 error(from_ascii("Unknown action %1$s"), params_.lang);
970 if (func.action() == LFUN_BUFFER_VIEW || func.action() == LFUN_BUFFER_UPDATE)
971 // The default output format is in the menu without argument,
973 if (func.argument() == from_ascii(buffer().params().getDefaultOutputFormat()))
974 func = FuncRequest(func.action());
975 // iterate through the menubackend to find it
977 gui = _("Can't determine menu entry for action %1$s in batch mode");
978 error(from_ascii("Can't determine menu entry for action %1$s in batch mode"), params_.lang);
982 // and we will not keep trying if we fail
983 initialized_ = theApp()->hasBufferView();
984 if (!theApp()->searchMenu(func, names)) {
985 gui = _("No menu entry for action %1$s");
986 error(from_ascii("No menu entry for action %1$s"), params_.lang);
989 // if found, return its path.
991 Paragraph & par = paragraphs().front();
992 Font const f(inherit_font, guilang);
993 params_.force_ltr = !guilang->rightToLeft();
995 //fu.fontInfo().setUnderbar(FONT_ON);
996 for (docstring const & name : names) {
997 // do not insert > for the top level menu item
998 if (&name != &names.front())
999 par.insertInset(par.size(), new InsetSpecialChar(InsetSpecialChar::MENU_SEPARATOR),
1000 f, Change(Change::UNCHANGED));
1001 //FIXME: add proper underlines here. This
1002 // involves rewriting searchMenu used above to
1003 // return a vector of menus. If we do not do
1004 // that, we might as well use below
1005 // Paragraph::insert on each string (JMarc)
1006 for (char_type c : name)
1007 par.insertChar(par.size(), c, f, Change(Change::UNCHANGED));
1011 case InsetInfoParams::L7N_INFO: {
1012 docstring locstring = _(params_.name);
1013 // Remove trailing colons
1014 locstring = rtrim(locstring, ":");
1015 // Remove menu accelerators
1016 if (contains(locstring, from_ascii("|"))) {
1017 docstring nlocstring;
1018 rsplit(locstring, nlocstring, '|');
1019 locstring = nlocstring;
1021 // Remove Qt accelerators, but keep literal ampersands
1022 locstring = subst(locstring, from_ascii(" & "), from_ascii("</amp;>"));
1023 locstring = subst(locstring, from_ascii("&"), docstring());
1024 locstring = subst(locstring, from_ascii("</amp;>"), from_ascii(" & "));
1025 setText(locstring, guilang);
1026 params_.force_ltr = !guilang->rightToLeft() && !params_.lang->rightToLeft();
1029 case InsetInfoParams::ICON_INFO: {
1030 // only need to do this once.
1033 // and we will not keep trying if we fail
1034 initialized_ = true;
1035 FuncRequest func = lyxaction.lookupFunc(params_.name);
1036 docstring icon_name = frontend::Application::iconName(func, true);
1037 // FIXME: We should use the icon directly instead of
1038 // going through FileName. The code below won't work
1039 // if the icon is embedded in the executable through
1040 // the Qt resource system.
1041 // This is only a negligible performance problem:
1042 // If the installed icon differs from the resource icon the
1043 // installed one is preferred anyway, and all icons that are
1044 // embedded in the resources are installed as well.
1045 FileName file(to_utf8(icon_name));
1046 if (file.onlyFileNameWithoutExt() == "unknown") {
1047 string dir = "images";
1048 FileName file2(imageLibFileSearch(dir, params_.name, "svgz,png"));
1054 int percent_scale = 100;
1056 // Compute the scale factor for the icon such that its
1057 // width on screen is equal to 1em in pixels.
1058 // The scale factor is rounded to the integer nearest
1059 // to the float value of the ratio 100*iconsize/imgsize.
1060 int imgsize = QImage(toqstr(file.absFileName())).width();
1062 int iconsize = Length(1, Length::EM).inPixels(1);
1063 percent_scale = (100 * iconsize + imgsize / 2)/imgsize;
1066 InsetGraphics * inset = new InsetGraphics(buffer_);
1067 InsetGraphicsParams igp;
1068 igp.filename = file;
1069 igp.lyxscale = percent_scale;
1070 igp.scale = string();
1071 igp.width = Length(1, Length::EM);
1072 inset->setParams(igp);
1074 Font const f(inherit_font, params_.lang);
1075 paragraphs().front().insertInset(0, inset, f,
1076 Change(Change::UNCHANGED));
1079 case InsetInfoParams::BUFFER_INFO: {
1080 // this could all change, so we will recalculate each time
1081 if (params_.name == "name")
1082 setText(from_utf8(buffer().fileName().onlyFileName()), params_.lang);
1083 else if (params_.name == "name-noext")
1084 setText(from_utf8(buffer().fileName().onlyFileNameWithoutExt()), params_.lang);
1085 else if (params_.name == "path")
1086 setText(from_utf8(os::latex_path(buffer().filePath())), params_.lang);
1087 else if (params_.name == "class")
1088 setText(from_utf8(bp.documentClass().name()), params_.lang);
1091 case InsetInfoParams::VCS_INFO: {
1092 // this information could change, in principle, so we will
1093 // recalculate each time through
1094 if (!buffer().lyxvc().inUse()) {
1095 gui = _("No version control!");
1096 info(from_ascii("No version control!"), params_.lang);
1099 LyXVC::RevisionInfo itype = LyXVC::Unknown;
1100 if (params_.name == "revision")
1101 itype = LyXVC::File;
1102 else if (params_.name == "tree-revision")
1103 itype = LyXVC::Tree;
1104 else if (params_.name == "author")
1105 itype = LyXVC::Author;
1106 else if (params_.name == "time")
1107 itype = LyXVC::Time;
1108 else if (params_.name == "date")
1109 itype = LyXVC::Date;
1110 string binfo = buffer().lyxvc().revisionInfo(itype);
1111 if (binfo.empty()) {
1112 gui = _("%1$s[[vcs data]] unknown");
1113 error(from_ascii("%1$s[[vcs data]] unknown"), params_.lang);
1115 setText(from_utf8(binfo), params_.lang);
1118 case InsetInfoParams::LYX_INFO:
1119 // only need to do this once.
1122 if (params_.name == "version")
1123 setText(from_ascii(lyx_version), params_.lang);
1124 else if (params_.name == "layoutformat")
1125 setText(convert<docstring>(LAYOUT_FORMAT), params_.lang);
1126 initialized_ = true;
1128 case InsetInfoParams::DATE_INFO:
1129 case InsetInfoParams::MODDATE_INFO:
1130 case InsetInfoParams::FIXDATE_INFO: {
1131 string date_format = params_.name;
1132 string const date_specifier = (params_.type == InsetInfoParams::FIXDATE_INFO
1133 && contains(params_.name, '@'))
1134 ? split(params_.name, date_format, '@') : string();
1136 if (params_.type == InsetInfoParams::MODDATE_INFO)
1137 date = QDateTime::fromTime_t(buffer().fileName().lastModified()).date();
1138 else if (params_.type == InsetInfoParams::FIXDATE_INFO && !date_specifier.empty())
1139 date = QDate::fromString(toqstr(date_specifier), Qt::ISODate);
1141 date = QDate::currentDate();
1142 setText(params_.getDate(date_format, date), params_.lang);
1145 case InsetInfoParams::TIME_INFO:
1146 case InsetInfoParams::MODTIME_INFO:
1147 case InsetInfoParams::FIXTIME_INFO: {
1148 string time_format = params_.name;
1149 string const time_specifier = (params_.type == InsetInfoParams::FIXTIME_INFO
1150 && contains(params_.name, '@'))
1151 ? split(params_.name, time_format, '@') : string();
1153 if (params_.type == InsetInfoParams::MODTIME_INFO)
1154 time = QDateTime::fromTime_t(buffer().fileName().lastModified()).time();
1155 else if (params_.type == InsetInfoParams::FIXTIME_INFO && !time_specifier.empty())
1156 time = QTime::fromString(toqstr(time_specifier), Qt::ISODate);
1158 time = QTime::currentTime();
1159 setText(params_.getTime(time_format, time), params_.lang);
1164 // Just to do something with that string
1165 LYXERR(Debug::INFO, "info inset text: " << gui);
1166 InsetCollapsible::updateBuffer(it, utype);
1170 string InsetInfo::contextMenu(BufferView const &, int, int) const
1172 //FIXME: We override the implementation of InsetCollapsible,
1173 //because this inset is not a collapsible inset.
1174 return contextMenuName();
1178 string InsetInfo::contextMenuName() const
1180 return "context-info";