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/convert.h"
42 #include "support/debug.h"
43 #include "support/docstream.h"
44 #include "support/docstring_list.h"
45 #include "support/ExceptionMessage.h"
46 #include "support/FileName.h"
47 #include "support/filetools.h"
48 #include "support/gettext.h"
49 #include "support/Length.h"
50 #include "support/Messages.h"
51 #include "support/lstrings.h"
52 #include "support/qstring_helpers.h"
53 #include "support/Translator.h"
57 #include <QtGui/QImage>
62 using namespace lyx::support;
68 typedef Translator<InsetInfoParams::info_type, string> NameTranslator;
70 NameTranslator const initTranslator()
72 NameTranslator translator(InsetInfoParams::UNKNOWN_INFO, "unknown");
74 translator.addPair(InsetInfoParams::SHORTCUTS_INFO, "shortcuts");
75 translator.addPair(InsetInfoParams::SHORTCUT_INFO, "shortcut");
76 translator.addPair(InsetInfoParams::LYXRC_INFO, "lyxrc");
77 translator.addPair(InsetInfoParams::PACKAGE_INFO, "package");
78 translator.addPair(InsetInfoParams::TEXTCLASS_INFO, "textclass");
79 translator.addPair(InsetInfoParams::MENU_INFO, "menu");
80 translator.addPair(InsetInfoParams::L7N_INFO, "l7n");
81 translator.addPair(InsetInfoParams::ICON_INFO, "icon");
82 translator.addPair(InsetInfoParams::BUFFER_INFO, "buffer");
83 translator.addPair(InsetInfoParams::LYX_INFO, "lyxinfo");
84 translator.addPair(InsetInfoParams::VCS_INFO, "vcs");
85 translator.addPair(InsetInfoParams::DATE_INFO, "date");
86 translator.addPair(InsetInfoParams::MODDATE_INFO, "moddate");
87 translator.addPair(InsetInfoParams::FIXDATE_INFO, "fixdate");
88 translator.addPair(InsetInfoParams::TIME_INFO, "time");
89 translator.addPair(InsetInfoParams::MODTIME_INFO, "modtime");
90 translator.addPair(InsetInfoParams::FIXTIME_INFO, "fixtime");
95 /// The translator between the information type enum and corresponding string.
96 NameTranslator const & nameTranslator()
98 static NameTranslator const translator = initTranslator();
103 typedef Translator<InsetInfoParams::info_type, string> DefaultValueTranslator;
105 DefaultValueTranslator const initDVTranslator()
107 DefaultValueTranslator translator(InsetInfoParams::UNKNOWN_INFO, "");
109 translator.addPair(InsetInfoParams::SHORTCUTS_INFO, "info-insert");
110 translator.addPair(InsetInfoParams::SHORTCUT_INFO, "info-insert");
111 translator.addPair(InsetInfoParams::LYXRC_INFO, "user_name");
112 translator.addPair(InsetInfoParams::PACKAGE_INFO, "graphics");
113 translator.addPair(InsetInfoParams::TEXTCLASS_INFO, "article");
114 translator.addPair(InsetInfoParams::MENU_INFO, "info-insert");
115 translator.addPair(InsetInfoParams::L7N_INFO, "");
116 translator.addPair(InsetInfoParams::ICON_INFO, "info-insert");
117 translator.addPair(InsetInfoParams::BUFFER_INFO, "name-noext");
118 translator.addPair(InsetInfoParams::LYX_INFO, "version");
119 translator.addPair(InsetInfoParams::VCS_INFO, "revision");
120 translator.addPair(InsetInfoParams::DATE_INFO, "loclong");
121 translator.addPair(InsetInfoParams::MODDATE_INFO, "loclong");
122 translator.addPair(InsetInfoParams::FIXDATE_INFO, "loclong");
123 translator.addPair(InsetInfoParams::TIME_INFO, "long");
124 translator.addPair(InsetInfoParams::MODTIME_INFO, "long");
125 translator.addPair(InsetInfoParams::FIXTIME_INFO, "long");
130 /// The translator between the information type enum and some sensible default value.
131 DefaultValueTranslator const & defaultValueTranslator()
133 static DefaultValueTranslator const translator = initDVTranslator();
140 /////////////////////////////////////////////////////////////////////
144 ///////////////////////////////////////////////////////////////////////
146 InsetInfoParams infoparams;
149 set<string> getTexFileList(string const & filename)
152 FileName const file = libFileSearch(string(), filename);
157 vector<docstring> doclist =
158 getVectorFromString(file.fileContents("UTF-8"), from_ascii("\n"));
160 // Normalise paths like /foo//bar ==> /foo/bar
161 for (auto doc : doclist) {
162 doc = subst(doc, from_ascii("\r"), docstring());
163 while (contains(doc, from_ascii("//")))
164 doc = subst(doc, from_ascii("//"), from_ascii("/"));
166 list.insert(removeExtension(onlyFileName(to_utf8(doc))));
173 bool translateString(docstring const & in, docstring & out, string const & lcode)
175 out = translateIfPossible(in, lcode);
181 docstring InsetInfoParams::getDate(string const & iname, QDate const date) const
185 loc = QLocale(toqstr(lang->code()));
187 return qstring_to_ucs4(loc.toString(date, QLocale::LongFormat));
188 else if (iname == "short")
189 return qstring_to_ucs4(loc.toString(date, QLocale::ShortFormat));
190 else if (iname == "ISO")
191 return qstring_to_ucs4(date.toString(Qt::ISODate));
192 else if (iname == "loclong")
193 return lang ? qstring_to_ucs4(loc.toString(date, toqstr(lang->dateFormat(0))))
194 : _("No long date format (language unknown)!");
195 else if (iname == "locmedium")
196 return lang ? qstring_to_ucs4(loc.toString(date, toqstr(lang->dateFormat(1))))
197 : _("No medium date format (language unknown)!");
198 else if (iname == "locshort")
199 return lang ? qstring_to_ucs4(loc.toString(date, toqstr(lang->dateFormat(2))))
200 : _("No short date format (language unknown)!");
202 return qstring_to_ucs4(loc.toString(date, toqstr(iname)));
206 docstring InsetInfoParams::getTime(string const & iname, QTime const time) const
210 loc = QLocale(toqstr(lang->code()));
212 return qstring_to_ucs4(loc.toString(time, QLocale::LongFormat));
213 else if (iname == "short")
214 return qstring_to_ucs4(loc.toString(time, QLocale::ShortFormat));
215 else if (iname == "ISO")
216 return qstring_to_ucs4(time.toString(Qt::ISODate));
218 return qstring_to_ucs4(loc.toString(time, toqstr(iname)));
222 vector<pair<string,docstring>> InsetInfoParams::getArguments(Buffer const * buf,
223 string const & itype) const
225 vector<pair<string,docstring>> result;
227 switch (nameTranslator().find(itype)) {
229 result.push_back(make_pair("invalid", _("Please select a valid type!")));
236 result.push_back(make_pair("custom", _("Custom")));
237 for (auto const & name_code : lyxaction) {
238 string const lfun = name_code.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("revision-abbrev", _("Abbreviated revision[[Version Control]]")));
282 result.push_back(make_pair("tree-revision", _("Tree revision")));
283 result.push_back(make_pair("author", _("Author")));
284 result.push_back(make_pair("date", _("Date")));
285 result.push_back(make_pair("time", _("Time[[of day]]")));
290 result.push_back(make_pair("version", _("LyX version")));
291 result.push_back(make_pair("layoutformat", _("LyX layout format")));
297 string const dt = split(name, '@');
299 if (itype == "moddate")
300 date = QDateTime::fromTime_t(buf->fileName().lastModified()).date();
301 else if (itype == "fixdate" && !dt.empty()) {
302 QDate const gdate = QDate::fromString(toqstr(dt), Qt::ISODate);
303 date = (gdate.isValid()) ? gdate : QDate::currentDate();
305 date = QDate::currentDate();
306 result.push_back(make_pair("long",getDate("long", date)));
307 result.push_back(make_pair("short", getDate("short", date)));
308 result.push_back(make_pair("loclong", getDate("loclong", date)));
309 result.push_back(make_pair("locmedium", getDate("locmedium", date)));
310 result.push_back(make_pair("locshort", getDate("locshort", date)));
311 result.push_back(make_pair("ISO", getDate("ISO", date)));
312 result.push_back(make_pair("yyyy", getDate("yyyy", date)));
313 result.push_back(make_pair("MMMM", getDate("MMMM", date)));
314 result.push_back(make_pair("MMM", getDate("MMM", date)));
315 result.push_back(make_pair("dddd", getDate("dddd", date)));
316 result.push_back(make_pair("ddd", getDate("ddd", date)));
317 result.push_back(make_pair("custom", _("Custom")));
323 string const tt = split(name, '@');
325 if (itype == "modtime")
326 time = QDateTime::fromTime_t(buf->fileName().lastModified()).time();
327 else if (itype == "fixtime" && !tt.empty()) {
328 QTime const gtime = QTime::fromString(toqstr(tt), Qt::ISODate);
329 time = (gtime.isValid()) ? gtime : QTime::currentTime();
331 time = QTime::currentTime();
332 result.push_back(make_pair("long",getTime("long", time)));
333 result.push_back(make_pair("short", getTime("short", time)));
334 result.push_back(make_pair("ISO", getTime("ISO", time)));
335 result.push_back(make_pair("custom", _("Custom")));
344 bool InsetInfoParams::validateArgument(Buffer const * buf, docstring const & arg,
345 bool const usedefaults) const
348 string name = trim(split(to_utf8(arg), type, ' '));
349 if (name.empty() && usedefaults)
350 name = defaultValueTranslator().find(type);
352 switch (nameTranslator().find(type)) {
359 FuncRequest func = lyxaction.lookupFunc(name);
360 return func.action() != LFUN_UNKNOWN_ACTION;
364 return !name.empty();
367 FuncCode const action = lyxaction.lookupFunc(name).action();
368 if (action == LFUN_UNKNOWN_ACTION) {
369 string dir = "images";
370 return !imageLibFileSearch(dir, name, "svgz,png").empty();
376 set<string> rcs = lyxrc.getRCs();
377 return rcs.find(name) != rcs.end();
385 return (name == "name" || name == "name-noext"
386 || name == "path" || name == "class");
389 if (name == "revision" || name == "revision-abbrev" || name == "tree-revision"
390 || name == "author" || name == "date" || name == "time")
391 return buf->lyxvc().inUse();
395 return name == "version" || name == "layoutformat";
400 date = split(name, piece, '@');
401 if (!date.empty() && !QDate::fromString(toqstr(date), Qt::ISODate).isValid())
409 if (name == "long" || name == "short" || name == "ISO")
412 QDate date = QDate::currentDate();
413 return !date.toString(toqstr(name)).isEmpty();
419 time = split(name, piece, '@');
420 if (!time.empty() && !QTime::fromString(toqstr(time), Qt::ISODate).isValid())
428 if (name == "long" || name == "short" || name == "ISO")
431 QTime time = QTime::currentTime();
432 return !time.toString(toqstr(name)).isEmpty();
443 string InsetInfoParams::infoType() const
445 return nameTranslator().find(type);
450 /////////////////////////////////////////////////////////////////////////
454 /////////////////////////////////////////////////////////////////////////
458 InsetInfo::InsetInfo(Buffer * buf, string const & info)
459 : InsetCollapsible(buf), initialized_(false)
461 params_.type = InsetInfoParams::UNKNOWN_INFO;
462 params_.force_ltr = false;
468 Inset * InsetInfo::editXY(Cursor & cur, int x, int y)
470 // do not allow the cursor to be set in this Inset
471 return Inset::editXY(cur, x, y);
475 docstring InsetInfo::layoutName() const
477 return from_ascii("Info:" + params_.infoType());
481 docstring InsetInfo::toolTip(BufferView const &, int, int) const
484 switch (nameTranslator().find(params_.infoType())) {
485 case InsetInfoParams::UNKNOWN_INFO:
486 result = _("Invalid information inset");
488 case InsetInfoParams::SHORTCUT_INFO:
489 result = bformat(_("The keybard shortcut for the function '%1$s'"),
490 from_utf8(params_.name));
492 case InsetInfoParams::SHORTCUTS_INFO:
493 result = bformat(_("The keybard shortcuts for the function '%1$s'"),
494 from_utf8(params_.name));
496 case InsetInfoParams::MENU_INFO:
497 result = bformat(_("The menu location for the function '%1$s'"),
498 from_utf8(params_.name));
500 case InsetInfoParams::L7N_INFO:
501 result = bformat(_("The localization for the string '%1$s'"),
502 from_utf8(params_.name));
504 case InsetInfoParams::ICON_INFO:
505 result = bformat(_("The toolbar icon for the function '%1$s'"),
506 from_utf8(params_.name));
508 case InsetInfoParams::LYXRC_INFO:
509 result = bformat(_("The preference setting for the preference key '%1$s'"),
510 from_utf8(params_.name));
512 case InsetInfoParams::PACKAGE_INFO:
513 result = bformat(_("Availability of the LaTeX package '%1$s'"),
514 from_utf8(params_.name));
516 case InsetInfoParams::TEXTCLASS_INFO:
517 result = bformat(_("Availability of the LaTeX class '%1$s'"),
518 from_utf8(params_.name));
520 case InsetInfoParams::BUFFER_INFO:
521 if (params_.name == "name")
522 result = _("The name of this file (incl. extension)");
523 else if (params_.name == "name-noext")
524 result = _("The name of this file (without extension)");
525 else if (params_.name == "path")
526 result = _("The path where this file is saved");
527 else if (params_.name == "class")
528 result = _("The class this document uses");
530 case InsetInfoParams::VCS_INFO:
531 if (params_.name == "revision")
532 result = _("Version control revision");
533 else if (params_.name == "revision-abbrev")
534 result = _("Version control abbreviated revision");
535 else if (params_.name == "tree-revision")
536 result = _("Version control tree revision");
537 else if (params_.name == "author")
538 result = _("Version control author");
539 else if (params_.name == "date")
540 result = _("Version control date");
541 else if (params_.name == "time")
542 result = _("Version control time");
544 case InsetInfoParams::LYX_INFO:
545 if (params_.name == "version")
546 result = _("The current LyX version");
547 else if (params_.name == "layoutformat")
548 result = _("The current LyX layout format");
550 case InsetInfoParams::DATE_INFO:
551 result = _("The current date");
553 case InsetInfoParams::MODDATE_INFO:
554 result = _("The date of last save");
556 case InsetInfoParams::FIXDATE_INFO:
557 result = _("A static date");
559 case InsetInfoParams::TIME_INFO:
560 result = _("The current time");
562 case InsetInfoParams::MODTIME_INFO:
563 result = _("The time of last save");
565 case InsetInfoParams::FIXTIME_INFO:
566 result = _("A static time");
574 void InsetInfo::read(Lexer & lex)
579 token = lex.getString();
580 if (token == "type") {
582 token = lex.getString();
583 params_.type = nameTranslator().find(token);
584 } else if (token == "arg") {
586 params_.name = lex.getString();
587 } else if (token == "\\end_inset")
590 if (token != "\\end_inset") {
591 lex.printError("Missing \\end_inset at this point");
592 throw ExceptionMessage(WarningException,
593 _("Missing \\end_inset at this point."),
599 void InsetInfo::write(ostream & os) const
601 os << "Info\ntype \"" << params_.infoType()
602 << "\"\narg " << Lexer::quoteString(params_.name);
606 bool InsetInfo::showInsetDialog(BufferView * bv) const
608 bv->showDialog("info");
613 bool InsetInfo::getStatus(Cursor & cur, FuncRequest const & cmd,
614 FuncStatus & flag) const
616 switch (cmd.action()) {
617 case LFUN_INSET_SETTINGS:
618 return InsetCollapsible::getStatus(cur, cmd, flag);
620 case LFUN_INSET_DIALOG_UPDATE:
621 case LFUN_INSET_COPY_AS:
622 case LFUN_INSET_DISSOLVE:
623 flag.setEnabled(true);
626 case LFUN_INSET_MODIFY:
627 if (nameTranslator().find(cmd.getArg(0)) == InsetInfoParams::UNKNOWN_INFO)
628 return Inset::getStatus(cur, cmd, flag);
629 if (params_.validateArgument(&buffer(), cmd.argument())) {
630 flag.setEnabled(true);
632 string name = trim(split(to_utf8(cmd.argument()), typestr, ' '));
633 InsetInfoParams::info_type type = nameTranslator().find(typestr);
634 string origname = params_.name;
635 if (type == InsetInfoParams::FIXDATE_INFO
636 || type == InsetInfoParams::FIXTIME_INFO)
637 split(params_.name, origname, '@');
638 flag.setOnOff(type == params_.type && name == origname);
649 void InsetInfo::doDispatch(Cursor & cur, FuncRequest & cmd)
651 switch (cmd.action()) {
652 case LFUN_INSET_MODIFY:
653 if (nameTranslator().find(cmd.getArg(0)) == InsetInfoParams::UNKNOWN_INFO) {
658 setInfo(to_utf8(cmd.argument()));
659 cur.forceBufferUpdate();
660 initialized_ = false;
663 case LFUN_INSET_COPY_AS: {
664 cap::clearSelection();
666 copy.pushBackward(*this);
670 copy.pit() = copy.lastpit();
671 copy.pos() = copy.lastpos();
673 cap::copySelection(copy);
678 InsetCollapsible::doDispatch(cur, cmd);
684 void InsetInfo::setInfo(string const & info)
689 string saved_date_specifier;
690 // Store old date specifier for potential re-use
691 if (!params_.name.empty())
692 saved_date_specifier = split(params_.name, '@');
695 params_.name = trim(split(info, type, ' '));
696 params_.type = nameTranslator().find(type);
697 if (params_.name.empty())
698 params_.name = defaultValueTranslator().find(params_.type);
699 if (params_.type == InsetInfoParams::FIXDATE_INFO) {
700 string const date_specifier = split(params_.name, '@');
701 // If an explicit new fix date is specified, use that
702 // Otherwise, use the old one or, if there is none,
704 if (date_specifier.empty()) {
705 if (saved_date_specifier.empty())
706 params_.name += "@" + fromqstr(QDate::currentDate().toString(Qt::ISODate));
708 params_.name += "@" + saved_date_specifier;
711 else if (params_.type == InsetInfoParams::FIXTIME_INFO) {
712 string const time_specifier = split(params_.name, '@');
713 // If an explicit new fix time is specified, use that
714 // Otherwise, use the old one or, if there is none,
716 if (time_specifier.empty()) {
717 if (saved_date_specifier.empty())
718 params_.name += "@" + fromqstr(QTime::currentTime().toString(Qt::ISODate));
720 params_.name += "@" + saved_date_specifier;
726 void InsetInfo::error(docstring const & err, Language const * lang)
728 docstring const res = translateIfPossible(err, lang->code());
729 bool const translated = res != err;
730 // If the string is not translated, we use default lang (English)
731 Font const f = translated ? Font(inherit_font, lang) : Font(inherit_font);
732 setText(bformat(res, from_utf8(params_.name)), f, false);
736 void InsetInfo::info(docstring const & err, Language const * lang)
738 docstring const res = translateIfPossible(err, lang->code());
739 bool const translated = res != err;
740 // If the string is not translated, we use default lang (English)
741 Font const f = translated ? Font(inherit_font, lang) : Font(inherit_font);
742 setText(translateIfPossible(err, lang->code()), f, false);
746 void InsetInfo::setText(docstring const & str, Language const * lang)
748 setText(str, Font(inherit_font, lang), false);
752 bool InsetInfo::forceLTR(OutputParams const &) const
754 return params_.force_ltr;
758 bool InsetInfo::forceLocalFontSwitch() const
760 return params_.type == InsetInfoParams::MENU_INFO
761 || params_.type == InsetInfoParams::SHORTCUTS_INFO
762 || params_.type == InsetInfoParams::SHORTCUT_INFO
763 || params_.type == InsetInfoParams::L7N_INFO;
767 void InsetInfo::updateBuffer(ParIterator const & it, UpdateType utype, bool const deleted) {
768 // If the Buffer is a clone, then we neither need nor want to do any
769 // of what follows. We want, rather, just to inherit how things were
770 // in the original Buffer. This is especially important for VCS.
771 // Otherwise, we could in principle have different settings here
772 // than in the Buffer we were exporting.
773 if (buffer().isClone())
776 BufferParams const & bp = buffer().params();
777 params_.lang = it.paragraph().getFontSettings(bp, it.pos()).language();
778 Language const * tryguilang = languages.getFromCode(Messages::guiLanguage());
779 // Some info insets use the language of the GUI (if available)
780 Language const * guilang = tryguilang ? tryguilang : params_.lang;
782 params_.force_ltr = !params_.lang->rightToLeft();
783 // This is just to get the string into the po files
785 switch (params_.type) {
786 case InsetInfoParams::UNKNOWN_INFO:
787 gui = _("Unknown Info!");
788 info(from_ascii("Unknown Info!"), params_.lang);
789 initialized_ = false;
791 case InsetInfoParams::SHORTCUT_INFO:
792 case InsetInfoParams::SHORTCUTS_INFO: {
793 // shortcuts can change, so we need to re-do this each time
794 FuncRequest const func = lyxaction.lookupFunc(params_.name);
795 if (func.action() == LFUN_UNKNOWN_ACTION) {
796 gui = _("Unknown action %1$s");
797 error(from_ascii("Unknown action %1$s"), params_.lang);
800 KeyMap::Bindings bindings = theTopLevelKeymap().findBindings(func);
801 if (bindings.empty()) {
802 gui = _("undefined");
803 info(from_ascii("undefined"), params_.lang);
807 docstring seq_untranslated;
808 if (params_.type == InsetInfoParams::SHORTCUT_INFO) {
809 sequence = bindings.begin()->print(KeySequence::ForGui);
810 seq_untranslated = bindings.begin()->print(KeySequence::ForGui, true);
812 sequence = theTopLevelKeymap().printBindings(func, KeySequence::ForGui);
813 seq_untranslated = theTopLevelKeymap().printBindings(func, KeySequence::ForGui, true);
815 // QKeySequence returns special characters for keys on the mac
816 // Since these are not included in many fonts, we
817 // re-translate them to textual names (see #10641)
818 odocstringstream ods;
819 string const lcode = params_.lang->code();
821 bool is_translated = sequence != seq_untranslated;
822 for (char_type const c : sequence) {
825 gui = _("Return[[Key]]");
826 is_translated = translateString(from_ascii("Return[[Key]]"), trans, lcode);
829 case 0x21b9://Tab both directions (Win)
830 gui = _("Tab[[Key]]");
831 is_translated = translateString(from_ascii("Tab[[Key]]"), trans, lcode);
834 case 0x21de://Qt::Key_PageUp
836 is_translated = translateString(from_ascii("PgUp"), trans, lcode);
839 case 0x21df://Qt::Key_PageDown
841 is_translated = translateString(from_ascii("PgDown"), trans, lcode);
844 case 0x21e4://Qt::Key_Backtab
846 is_translated = translateString(from_ascii("Backtab"), trans, lcode);
849 case 0x21e5://Qt::Key_Tab
851 is_translated = translateString(from_ascii("Tab"), trans, lcode);
856 is_translated = translateString(from_ascii("Shift"), trans, lcode);
859 case 0x21ea://Qt::Key_CapsLock
861 is_translated = translateString(from_ascii("CapsLock"), trans, lcode);
864 case 0x2303://Control
865 gui = _("Control[[Key]]");
866 is_translated = translateString(from_ascii("Control[[Key]]"), trans, lcode);
870 gui = _("Command[[Key]]");
871 is_translated = translateString(from_ascii("Command[[Key]]"), trans, lcode);
874 case 0x2324://Qt::Key_Enter
875 gui = _("Return[[Key]]");
876 is_translated = translateString(from_ascii("Return[[Key]]"), trans, lcode);
879 case 0x2325://Option key
880 gui = _("Option[[Key]]");
881 is_translated = translateString(from_ascii("Option[[Key]]"), trans, lcode);
884 case 0x2326://Qt::Key_Delete
885 gui = _("Delete[[Key]]");
886 is_translated = translateString(from_ascii("Delete[[Key]]"), trans, lcode);
889 case 0x232b://Qt::Key_Backspace
891 is_translated = translateString(from_ascii("Fn+Del"), trans, lcode);
894 case 0x238b://Qt::Key_Escape
896 is_translated = translateString(from_ascii("Esc"), trans, lcode);
903 setText(ods.str(), is_translated ? guilang : nullptr);
904 params_.force_ltr = !is_translated || (!guilang->rightToLeft() && !params_.lang->rightToLeft());
907 case InsetInfoParams::LYXRC_INFO: {
908 // this information could change, if the preferences are changed,
909 // so we will recalculate each time through.
911 if (params_.name.empty()) {
912 gui = _("undefined");
913 info(from_ascii("undefined"), params_.lang);
916 // FIXME this uses the serialization mechanism to get the info
917 // we want, which i guess works but is a bit strange.
918 lyxrc.write(oss, true, params_.name);
919 string result = oss.str();
920 if (result.size() < 2) {
921 gui = _("undefined");
922 info(from_ascii("undefined"), params_.lang);
925 string::size_type loc = result.rfind("\n", result.size() - 2);
926 loc = loc == string::npos ? 0 : loc + 1;
927 if (result.size() < loc + params_.name.size() + 1
928 || result.substr(loc + 1, params_.name.size()) != params_.name) {
929 gui = _("undefined");
930 info(from_ascii("undefined"), params_.lang);
933 // remove leading comments and \\name and space
934 result = result.substr(loc + params_.name.size() + 2);
937 result = rtrim(result, "\n");
938 result = trim(result, "\"");
942 setText(from_utf8(result), params_.lang);
945 case InsetInfoParams::PACKAGE_INFO:
946 // only need to do this once.
949 // check in packages.lst
950 if (LaTeXFeatures::isAvailable(params_.name)) {
952 info(from_ascii("yes"), params_.lang);
955 info(from_ascii("no"), params_.lang);
960 case InsetInfoParams::TEXTCLASS_INFO: {
961 // the TextClass can change
962 LayoutFileList const & list = LayoutFileList::get();
963 bool available = false;
964 // params_.name is the class name
965 if (list.haveClass(params_.name))
966 available = list[params_.name].isTeXClassAvailable();
969 info(from_ascii("yes"), params_.lang);
972 info(from_ascii("no"), params_.lang);
976 case InsetInfoParams::MENU_INFO: {
977 // only need to do this once.
980 docstring_list names;
981 FuncRequest func = lyxaction.lookupFunc(params_.name);
982 if (func.action() == LFUN_UNKNOWN_ACTION) {
983 gui = _("Unknown action %1$s");
984 error(from_ascii("Unknown action %1$s"), params_.lang);
987 if (func.action() == LFUN_BUFFER_VIEW || func.action() == LFUN_BUFFER_UPDATE)
988 // The default output format is in the menu without argument,
990 if (func.argument() == from_ascii(buffer().params().getDefaultOutputFormat()))
991 func = FuncRequest(func.action());
992 // iterate through the menubackend to find it
994 gui = _("Can't determine menu entry for action %1$s in batch mode");
995 error(from_ascii("Can't determine menu entry for action %1$s in batch mode"), params_.lang);
999 // and we will not keep trying if we fail
1000 initialized_ = theApp()->hasBufferView();
1001 if (!theApp()->searchMenu(func, names)) {
1002 gui = _("No menu entry for action %1$s");
1003 error(from_ascii("No menu entry for action %1$s"), params_.lang);
1006 // if found, return its path.
1008 Paragraph & par = paragraphs().front();
1009 Font const f(inherit_font, guilang);
1010 params_.force_ltr = !guilang->rightToLeft();
1012 //fu.fontInfo().setUnderbar(FONT_ON);
1013 for (docstring const & name : names) {
1014 // do not insert > for the top level menu item
1015 if (&name != &names.front())
1016 par.insertInset(par.size(), new InsetSpecialChar(InsetSpecialChar::MENU_SEPARATOR),
1017 f, Change(Change::UNCHANGED));
1018 //FIXME: add proper underlines here. This
1019 // involves rewriting searchMenu used above to
1020 // return a vector of menus. If we do not do
1021 // that, we might as well use below
1022 // Paragraph::insert on each string (JMarc)
1023 for (char_type c : name)
1024 par.insertChar(par.size(), c, f, Change(Change::UNCHANGED));
1028 case InsetInfoParams::L7N_INFO: {
1029 docstring locstring = _(params_.name);
1030 // Remove trailing colons
1031 locstring = rtrim(locstring, ":");
1032 // Remove menu accelerators
1033 if (contains(locstring, from_ascii("|"))) {
1034 docstring nlocstring;
1035 rsplit(locstring, nlocstring, '|');
1036 locstring = nlocstring;
1038 // Remove Qt accelerators, but keep literal ampersands
1039 locstring = subst(locstring, from_ascii(" & "), from_ascii("</amp;>"));
1040 locstring = subst(locstring, from_ascii("&"), docstring());
1041 locstring = subst(locstring, from_ascii("</amp;>"), from_ascii(" & "));
1042 setText(locstring, guilang);
1043 params_.force_ltr = !guilang->rightToLeft() && !params_.lang->rightToLeft();
1046 case InsetInfoParams::ICON_INFO: {
1047 // only need to do this once.
1050 // and we will not keep trying if we fail
1051 initialized_ = true;
1052 FuncRequest func = lyxaction.lookupFunc(params_.name);
1053 docstring icon_name = frontend::Application::iconName(func, true);
1054 FileName file(to_utf8(icon_name));
1055 if (file.onlyFileNameWithoutExt() == "unknown") {
1056 string dir = "images";
1057 FileName file2(imageLibFileSearch(dir, params_.name, "svgz,png"));
1063 int percent_scale = 100;
1065 // Compute the scale factor for the icon such that its
1066 // width on screen is equal to 1em in pixels.
1067 // The scale factor is rounded to the integer nearest
1068 // to the float value of the ratio 100*iconsize/imgsize.
1069 int imgsize = QImage(toqstr(file.absFileName())).width();
1071 int iconsize = Length(1, Length::EM).inPixels(1);
1072 percent_scale = (100 * iconsize + imgsize / 2)/imgsize;
1075 InsetGraphics * inset = new InsetGraphics(buffer_);
1076 InsetGraphicsParams igp;
1077 igp.filename = file;
1078 igp.lyxscale = percent_scale;
1079 igp.scale = string();
1080 igp.width = Length(1, Length::EM);
1081 inset->setParams(igp);
1083 Font const f(inherit_font, params_.lang);
1084 paragraphs().front().insertInset(0, inset, f,
1085 Change(Change::UNCHANGED));
1088 case InsetInfoParams::BUFFER_INFO: {
1089 // this could all change, so we will recalculate each time
1090 if (params_.name == "name")
1091 setText(from_utf8(buffer().fileName().onlyFileName()), params_.lang);
1092 else if (params_.name == "name-noext")
1093 setText(from_utf8(buffer().fileName().onlyFileNameWithoutExt()), params_.lang);
1094 else if (params_.name == "path")
1095 setText(from_utf8(os::latex_path(buffer().filePath())), params_.lang);
1096 else if (params_.name == "class")
1097 setText(from_utf8(bp.documentClass().name()), params_.lang);
1100 case InsetInfoParams::VCS_INFO: {
1101 // this information could change, in principle, so we will
1102 // recalculate each time through
1103 if (!buffer().lyxvc().inUse()) {
1104 gui = _("No version control!");
1105 info(from_ascii("No version control!"), params_.lang);
1108 LyXVC::RevisionInfo itype = LyXVC::Unknown;
1109 if (params_.name == "revision")
1110 itype = LyXVC::File;
1111 else if (params_.name == "revision-abbrev")
1112 itype = LyXVC::FileAbbrev;
1113 else if (params_.name == "tree-revision")
1114 itype = LyXVC::Tree;
1115 else if (params_.name == "author")
1116 itype = LyXVC::Author;
1117 else if (params_.name == "time")
1118 itype = LyXVC::Time;
1119 else if (params_.name == "date")
1120 itype = LyXVC::Date;
1121 string binfo = buffer().lyxvc().revisionInfo(itype);
1122 if (binfo.empty()) {
1123 gui = _("%1$s[[vcs data]] unknown");
1124 error(from_ascii("%1$s[[vcs data]] unknown"), params_.lang);
1126 setText(from_utf8(binfo), params_.lang);
1129 case InsetInfoParams::LYX_INFO:
1130 // only need to do this once.
1133 if (params_.name == "version")
1134 setText(from_ascii(lyx_version), params_.lang);
1135 else if (params_.name == "layoutformat")
1136 setText(convert<docstring>(LAYOUT_FORMAT), params_.lang);
1137 initialized_ = true;
1139 case InsetInfoParams::DATE_INFO:
1140 case InsetInfoParams::MODDATE_INFO:
1141 case InsetInfoParams::FIXDATE_INFO: {
1142 string date_format = params_.name;
1143 string const date_specifier = (params_.type == InsetInfoParams::FIXDATE_INFO
1144 && contains(params_.name, '@'))
1145 ? split(params_.name, date_format, '@') : string();
1147 if (params_.type == InsetInfoParams::MODDATE_INFO)
1148 date = QDateTime::fromTime_t(buffer().fileName().lastModified()).date();
1149 else if (params_.type == InsetInfoParams::FIXDATE_INFO && !date_specifier.empty())
1150 date = QDate::fromString(toqstr(date_specifier), Qt::ISODate);
1152 date = QDate::currentDate();
1153 setText(params_.getDate(date_format, date), params_.lang);
1156 case InsetInfoParams::TIME_INFO:
1157 case InsetInfoParams::MODTIME_INFO:
1158 case InsetInfoParams::FIXTIME_INFO: {
1159 string time_format = params_.name;
1160 string const time_specifier = (params_.type == InsetInfoParams::FIXTIME_INFO
1161 && contains(params_.name, '@'))
1162 ? split(params_.name, time_format, '@') : string();
1164 if (params_.type == InsetInfoParams::MODTIME_INFO)
1165 time = QDateTime::fromTime_t(buffer().fileName().lastModified()).time();
1166 else if (params_.type == InsetInfoParams::FIXTIME_INFO && !time_specifier.empty())
1167 time = QTime::fromString(toqstr(time_specifier), Qt::ISODate);
1169 time = QTime::currentTime();
1170 setText(params_.getTime(time_format, time), params_.lang);
1175 // Just to do something with that string
1176 LYXERR(Debug::INFO, "info inset text: " << gui);
1177 InsetCollapsible::updateBuffer(it, utype, deleted);
1181 string InsetInfo::contextMenu(BufferView const &, int, int) const
1183 //FIXME: We override the implementation of InsetCollapsible,
1184 //because this inset is not a collapsible inset.
1185 return contextMenuName();
1189 string InsetInfo::contextMenuName() const
1191 return "context-info";