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))));
173 docstring InsetInfoParams::getDate(string const iname, QDate const date) const
177 loc = QLocale(toqstr(lang->code()));
179 return qstring_to_ucs4(loc.toString(date, QLocale::LongFormat));
180 else if (iname == "short")
181 return qstring_to_ucs4(loc.toString(date, QLocale::ShortFormat));
182 else if (iname == "ISO")
183 return qstring_to_ucs4(date.toString(Qt::ISODate));
184 else if (iname == "loclong")
185 return lang ? qstring_to_ucs4(loc.toString(date, toqstr(lang->dateFormat(0))))
186 : _("No long date format (language unknown)!");
187 else if (iname == "locmedium")
188 return lang ? qstring_to_ucs4(loc.toString(date, toqstr(lang->dateFormat(1))))
189 : _("No medium date format (language unknown)!");
190 else if (iname == "locshort")
191 return lang ? qstring_to_ucs4(loc.toString(date, toqstr(lang->dateFormat(2))))
192 : _("No short date format (language unknown)!");
194 return qstring_to_ucs4(loc.toString(date, toqstr(iname)));
198 docstring InsetInfoParams::getTime(string const iname, QTime const time) const
202 loc = QLocale(toqstr(lang->code()));
204 return qstring_to_ucs4(loc.toString(time, QLocale::LongFormat));
205 else if (iname == "short")
206 return qstring_to_ucs4(loc.toString(time, QLocale::ShortFormat));
207 else if (iname == "ISO")
208 return qstring_to_ucs4(time.toString(Qt::ISODate));
210 return qstring_to_ucs4(loc.toString(time, toqstr(iname)));
214 vector<pair<string,docstring>> InsetInfoParams::getArguments(Buffer const * buf,
215 string const & itype) const
217 vector<pair<string,docstring>> result;
219 switch (nameTranslator().find(itype)) {
221 result.push_back(make_pair("invalid", _("Please select a valid type!")));
228 result.push_back(make_pair("custom", _("Custom")));
229 LyXAction::const_iterator fit = lyxaction.func_begin();
230 LyXAction::const_iterator const fen = lyxaction.func_end();
231 for (; fit != fen; ++fit) {
232 string const lfun = fit->first;
234 result.push_back(make_pair(lfun, from_ascii(lfun)));
240 result.push_back(make_pair("custom", _("Custom")));
244 result.push_back(make_pair("custom", _("Custom")));
245 set<string> rcs = lyxrc.getRCs();
246 for (auto const & rc : rcs)
247 result.push_back(make_pair(rc, from_ascii(rc)));
252 case TEXTCLASS_INFO: {
253 result.push_back(make_pair("custom", _("Custom")));
254 string const filename = (itype == "package") ? "styFiles.lst"
256 set<string> flist = getTexFileList(filename);
257 for (auto const & f : flist)
258 result.push_back(make_pair(f, from_utf8(f)));
263 result.push_back(make_pair("name", _("File name (with extension)")));
264 result.push_back(make_pair("name-noext", _("File name (without extension)")));
265 result.push_back(make_pair("path", _("File path")));
266 result.push_back(make_pair("class", _("Used text class")));
270 if (!buf->lyxvc().inUse()) {
271 result.push_back(make_pair("invalid", _("No version control!")));
274 result.push_back(make_pair("revision", _("Revision[[Version Control]]")));
275 result.push_back(make_pair("tree-revision", _("Tree revision")));
276 result.push_back(make_pair("author", _("Author")));
277 result.push_back(make_pair("date", _("Date")));
278 result.push_back(make_pair("time", _("Time[[of day]]")));
283 result.push_back(make_pair("version", _("LyX version")));
284 result.push_back(make_pair("layoutformat", _("LyX layout format")));
290 string const dt = split(name, '@');
292 if (itype == "moddate")
293 date = QDateTime::fromTime_t(buf->fileName().lastModified()).date();
294 else if (itype == "fixdate" && !dt.empty()) {
295 QDate const gdate = QDate::fromString(toqstr(dt), Qt::ISODate);
296 date = (gdate.isValid()) ? gdate : QDate::currentDate();
298 date = QDate::currentDate();
299 result.push_back(make_pair("long",getDate("long", date)));
300 result.push_back(make_pair("short", getDate("short", date)));
301 result.push_back(make_pair("loclong", getDate("loclong", date)));
302 result.push_back(make_pair("locmedium", getDate("locmedium", date)));
303 result.push_back(make_pair("locshort", getDate("locshort", date)));
304 result.push_back(make_pair("ISO", getDate("ISO", date)));
305 result.push_back(make_pair("yyyy", getDate("yyyy", date)));
306 result.push_back(make_pair("MMMM", getDate("MMMM", date)));
307 result.push_back(make_pair("MMM", getDate("MMM", date)));
308 result.push_back(make_pair("dddd", getDate("dddd", date)));
309 result.push_back(make_pair("ddd", getDate("ddd", date)));
310 result.push_back(make_pair("custom", _("Custom")));
316 string const tt = split(name, '@');
318 if (itype == "modtime")
319 time = QDateTime::fromTime_t(buf->fileName().lastModified()).time();
320 else if (itype == "fixtime" && !tt.empty()) {
321 QTime const gtime = QTime::fromString(toqstr(tt), Qt::ISODate);
322 time = (gtime.isValid()) ? gtime : QTime::currentTime();
324 time = QTime::currentTime();
325 result.push_back(make_pair("long",getTime("long", time)));
326 result.push_back(make_pair("short", getTime("short", time)));
327 result.push_back(make_pair("ISO", getTime("ISO", time)));
328 result.push_back(make_pair("custom", _("Custom")));
337 bool InsetInfoParams::validateArgument(Buffer const * buf, docstring const & arg,
338 bool const usedefaults) const
341 string name = trim(split(to_utf8(arg), type, ' '));
342 if (name.empty() && usedefaults)
343 name = defaultValueTranslator().find(type);
345 switch (nameTranslator().find(type)) {
352 FuncRequest func = lyxaction.lookupFunc(name);
353 return func.action() != LFUN_UNKNOWN_ACTION;
357 return !name.empty();
360 FuncCode const action = lyxaction.lookupFunc(name).action();
361 if (action == LFUN_UNKNOWN_ACTION) {
362 string dir = "images";
363 return !imageLibFileSearch(dir, name, "svgz,png").empty();
369 set<string> rcs = lyxrc.getRCs();
370 return rcs.find(name) != rcs.end();
378 return (name == "name" || name == "name-noext"
379 || name == "path" || name == "class");
382 if (name == "revision" || name == "tree-revision"
383 || name == "author" || name == "date" || name == "time")
384 return buf->lyxvc().inUse();
388 return name == "version" || name == "layoutformat";
393 date = split(name, piece, '@');
394 if (!date.empty() && !QDate::fromString(toqstr(date), Qt::ISODate).isValid())
402 if (name == "long" || name == "short" || name == "ISO")
405 QDate date = QDate::currentDate();
406 return !date.toString(toqstr(name)).isEmpty();
412 time = split(name, piece, '@');
413 if (!time.empty() && !QTime::fromString(toqstr(time), Qt::ISODate).isValid())
421 if (name == "long" || name == "short" || name == "ISO")
424 QTime time = QTime::currentTime();
425 return !time.toString(toqstr(name)).isEmpty();
436 string InsetInfoParams::infoType() const
438 return nameTranslator().find(type);
443 /////////////////////////////////////////////////////////////////////////
447 /////////////////////////////////////////////////////////////////////////
451 InsetInfo::InsetInfo(Buffer * buf, string const & name)
452 : InsetCollapsible(buf), initialized_(false)
454 params_.type = InsetInfoParams::UNKNOWN_INFO;
455 params_.force_ltr = false;
461 Inset * InsetInfo::editXY(Cursor & cur, int x, int y)
463 // do not allow the cursor to be set in this Inset
464 return Inset::editXY(cur, x, y);
468 docstring InsetInfo::layoutName() const
470 return from_ascii("Info:" + params_.infoType());
474 docstring InsetInfo::toolTip(BufferView const &, int, int) const
477 switch (nameTranslator().find(params_.infoType())) {
478 case InsetInfoParams::UNKNOWN_INFO:
479 result = _("Invalid information inset");
481 case InsetInfoParams::SHORTCUT_INFO:
482 result = bformat(_("The keybard shortcut for the function '%1$s'"),
483 from_utf8(params_.name));
485 case InsetInfoParams::SHORTCUTS_INFO:
486 result = bformat(_("The keybard shortcuts for the function '%1$s'"),
487 from_utf8(params_.name));
489 case InsetInfoParams::MENU_INFO:
490 result = bformat(_("The menu location for the function '%1$s'"),
491 from_utf8(params_.name));
493 case InsetInfoParams::L7N_INFO:
494 result = bformat(_("The localization for the string '%1$s'"),
495 from_utf8(params_.name));
497 case InsetInfoParams::ICON_INFO:
498 result = bformat(_("The toolbar icon for the function '%1$s'"),
499 from_utf8(params_.name));
501 case InsetInfoParams::LYXRC_INFO:
502 result = bformat(_("The preference setting for the preference key '%1$s'"),
503 from_utf8(params_.name));
505 case InsetInfoParams::PACKAGE_INFO:
506 result = bformat(_("Availability of the LaTeX package '%1$s'"),
507 from_utf8(params_.name));
509 case InsetInfoParams::TEXTCLASS_INFO:
510 result = bformat(_("Availability of the LaTeX class '%1$s'"),
511 from_utf8(params_.name));
513 case InsetInfoParams::BUFFER_INFO:
514 if (params_.name == "name")
515 result = _("The name of this file (incl. extension)");
516 else if (params_.name == "name-noext")
517 result = _("The name of this file (without extension)");
518 else if (params_.name == "path")
519 result = _("The path where this file is saved");
520 else if (params_.name == "class")
521 result = _("The class this document uses");
523 case InsetInfoParams::VCS_INFO:
524 if (params_.name == "revision")
525 result = _("Version control revision");
526 else if (params_.name == "tree-revision")
527 result = _("Version control tree revision");
528 else if (params_.name == "author")
529 result = _("Version control author");
530 else if (params_.name == "date")
531 result = _("Version control date");
532 else if (params_.name == "time")
533 result = _("Version control time");
535 case InsetInfoParams::LYX_INFO:
536 if (params_.name == "version")
537 result = _("The current LyX version");
538 else if (params_.name == "layoutformat")
539 result = _("The current LyX layout format");
541 case InsetInfoParams::DATE_INFO:
542 result = _("The current date");
544 case InsetInfoParams::MODDATE_INFO:
545 result = _("The date of last save");
547 case InsetInfoParams::FIXDATE_INFO:
548 result = _("A static date");
550 case InsetInfoParams::TIME_INFO:
551 result = _("The current time");
553 case InsetInfoParams::MODTIME_INFO:
554 result = _("The time of last save");
556 case InsetInfoParams::FIXTIME_INFO:
557 result = _("A static time");
565 void InsetInfo::read(Lexer & lex)
570 token = lex.getString();
571 if (token == "type") {
573 token = lex.getString();
574 params_.type = nameTranslator().find(token);
575 } else if (token == "arg") {
577 params_.name = lex.getString();
578 } else if (token == "\\end_inset")
581 if (token != "\\end_inset") {
582 lex.printError("Missing \\end_inset at this point");
583 throw ExceptionMessage(WarningException,
584 _("Missing \\end_inset at this point."),
590 void InsetInfo::write(ostream & os) const
592 os << "Info\ntype \"" << params_.infoType()
593 << "\"\narg " << Lexer::quoteString(params_.name);
597 bool InsetInfo::showInsetDialog(BufferView * bv) const
599 bv->showDialog("info");
604 bool InsetInfo::getStatus(Cursor & cur, FuncRequest const & cmd,
605 FuncStatus & flag) const
607 switch (cmd.action()) {
608 case LFUN_INSET_SETTINGS:
609 return InsetCollapsible::getStatus(cur, cmd, flag);
611 case LFUN_INSET_DIALOG_UPDATE:
612 case LFUN_INSET_COPY_AS:
613 case LFUN_INSET_DISSOLVE:
614 flag.setEnabled(true);
617 case LFUN_INSET_MODIFY:
618 if (params_.validateArgument(&buffer(), cmd.argument())) {
619 flag.setEnabled(true);
621 string name = trim(split(to_utf8(cmd.argument()), typestr, ' '));
622 InsetInfoParams::info_type type = nameTranslator().find(typestr);
623 string origname = params_.name;
624 if (type == InsetInfoParams::FIXDATE_INFO
625 || type == InsetInfoParams::FIXTIME_INFO)
626 split(params_.name, origname, '@');
627 flag.setOnOff(type == params_.type && name == origname);
638 void InsetInfo::doDispatch(Cursor & cur, FuncRequest & cmd)
640 switch (cmd.action()) {
641 case LFUN_INSET_MODIFY:
643 setInfo(to_utf8(cmd.argument()));
644 cur.forceBufferUpdate();
645 initialized_ = false;
648 case LFUN_INSET_COPY_AS: {
649 cap::clearSelection();
651 copy.pushBackward(*this);
655 copy.pit() = copy.lastpit();
656 copy.pos() = copy.lastpos();
658 cap::copySelection(copy);
663 InsetCollapsible::doDispatch(cur, cmd);
669 void InsetInfo::setInfo(string const & name)
674 string saved_date_specifier;
675 // Store old date specifier for potential re-use
676 if (!params_.name.empty())
677 saved_date_specifier = split(params_.name, '@');
680 params_.name = trim(split(name, type, ' '));
681 params_.type = nameTranslator().find(type);
682 if (params_.name.empty())
683 params_.name = defaultValueTranslator().find(params_.type);
684 if (params_.type == InsetInfoParams::FIXDATE_INFO) {
685 string const date_specifier = split(params_.name, '@');
686 // If an explicit new fix date is specified, use that
687 // Otherwise, use the old one or, if there is none,
689 if (date_specifier.empty()) {
690 if (saved_date_specifier.empty())
691 params_.name += "@" + fromqstr(QDate::currentDate().toString(Qt::ISODate));
693 params_.name += "@" + saved_date_specifier;
696 else if (params_.type == InsetInfoParams::FIXTIME_INFO) {
697 string const time_specifier = split(params_.name, '@');
698 // If an explicit new fix time is specified, use that
699 // Otherwise, use the old one or, if there is none,
701 if (time_specifier.empty()) {
702 if (saved_date_specifier.empty())
703 params_.name += "@" + fromqstr(QTime::currentTime().toString(Qt::ISODate));
705 params_.name += "@" + saved_date_specifier;
711 void InsetInfo::error(docstring const & err, Language const * lang)
713 setText(bformat(translateIfPossible(err, lang->code()), from_utf8(params_.name)),
714 Font(inherit_font, lang), false);
718 void InsetInfo::info(docstring const & err, Language const * lang)
720 setText(translateIfPossible(err, lang->code()),
721 Font(inherit_font, lang), false);
725 void InsetInfo::setText(docstring const & str, Language const * lang)
727 setText(str, Font(inherit_font, lang), false);
731 bool InsetInfo::forceLTR() const
733 return params_.force_ltr;
737 void InsetInfo::updateBuffer(ParIterator const & it, UpdateType utype) {
738 // If the Buffer is a clone, then we neither need nor want to do any
739 // of what follows. We want, rather, just to inherit how things were
740 // in the original Buffer. This is especially important for VCS.
741 // Otherwise, we could in principle have different settings here
742 // than in the Buffer we were exporting.
743 if (buffer().isClone())
746 BufferParams const & bp = buffer().params();
747 params_.lang = it.paragraph().getFontSettings(bp, it.pos()).language();
748 Language const * tryguilang = languages.getFromCode(Messages::guiLanguage());
749 // Some info insets use the language of the GUI (if available)
750 Language const * guilang = tryguilang ? tryguilang : params_.lang;
752 params_.force_ltr = !params_.lang->rightToLeft();
753 // This is just to get the string into the po files
755 switch (params_.type) {
756 case InsetInfoParams::UNKNOWN_INFO:
757 gui = _("Unknown Info!");
758 info(from_ascii("Unknown Info!"), params_.lang);
759 initialized_ = false;
761 case InsetInfoParams::SHORTCUT_INFO:
762 case InsetInfoParams::SHORTCUTS_INFO: {
763 // shortcuts can change, so we need to re-do this each time
764 FuncRequest const func = lyxaction.lookupFunc(params_.name);
765 if (func.action() == LFUN_UNKNOWN_ACTION) {
766 gui = _("Unknown action %1$s");
767 error(from_ascii("Unknown action %1$s"), params_.lang);
770 KeyMap::Bindings bindings = theTopLevelKeymap().findBindings(func);
771 if (bindings.empty()) {
772 gui = _("undefined");
773 info(from_ascii("undefined"), params_.lang);
777 if (params_.type == InsetInfoParams::SHORTCUT_INFO)
778 sequence = bindings.begin()->print(KeySequence::ForGui);
780 sequence = theTopLevelKeymap().printBindings(func, KeySequence::ForGui);
781 // QKeySequence returns special characters for keys on the mac
782 // Since these are not included in many fonts, we
783 // re-translate them to textual names (see #10641)
784 odocstringstream ods;
785 string const lcode = params_.lang->code();
786 for (size_t n = 0; n < sequence.size(); ++n) {
787 char_type const c = sequence[n];
790 gui = _("Return[[Key]]");
791 ods << translateIfPossible(from_ascii("Return[[Key]]"), lcode);
793 case 0x21b9://Tab both directions (Win)
794 gui = _("Tab[[Key]]");
795 ods << translateIfPossible(from_ascii("Tab[[Key]]"), lcode);
797 case 0x21de://Qt::Key_PageUp
799 ods << translateIfPossible(from_ascii("PgUp"), lcode);
801 case 0x21df://Qt::Key_PageDown
803 ods << translateIfPossible(from_ascii("PgDown"), lcode);
805 case 0x21e4://Qt::Key_Backtab
807 ods << translateIfPossible(from_ascii("Backtab"), lcode);
809 case 0x21e5://Qt::Key_Tab
811 ods << translateIfPossible(from_ascii("Tab"), lcode);
815 ods << translateIfPossible(from_ascii("Shift"), lcode);
817 case 0x21ea://Qt::Key_CapsLock
819 ods << translateIfPossible(from_ascii("CapsLock"), lcode);
821 case 0x2303://Control
822 gui = _("Control[[Key]]");
823 ods << translateIfPossible(from_ascii("Control[[Key]]"), lcode);
826 gui = _("Command[[Key]]");
827 ods << translateIfPossible(from_ascii("Command[[Key]]"), lcode);
829 case 0x2324://Qt::Key_Enter
830 gui = _("Return[[Key]]");
831 ods << translateIfPossible(from_ascii("Return[[Key]]"), lcode);
833 case 0x2325://Option key
834 gui = _("Option[[Key]]");
835 ods << translateIfPossible(from_ascii("Option[[Key]]"), lcode);
837 case 0x2326://Qt::Key_Delete
838 gui = _("Delete[[Key]]");
839 ods << translateIfPossible(from_ascii("Delete[[Key]]"), lcode);
841 case 0x232b://Qt::Key_Backspace
843 ods << translateIfPossible(from_ascii("Fn+Delete"), lcode);
845 case 0x238b://Qt::Key_Escape
847 ods << translateIfPossible(from_ascii("Esc"), lcode);
853 setText(ods.str(), guilang);
854 params_.force_ltr = !guilang->rightToLeft() && !params_.lang->rightToLeft();
857 case InsetInfoParams::LYXRC_INFO: {
858 // this information could change, if the preferences are changed,
859 // so we will recalculate each time through.
861 if (params_.name.empty()) {
862 gui = _("undefined");
863 info(from_ascii("undefined"), params_.lang);
866 // FIXME this uses the serialization mechanism to get the info
867 // we want, which i guess works but is a bit strange.
868 lyxrc.write(oss, true, params_.name);
869 string result = oss.str();
870 if (result.size() < 2) {
871 gui = _("undefined");
872 info(from_ascii("undefined"), params_.lang);
875 string::size_type loc = result.rfind("\n", result.size() - 2);
876 loc = loc == string::npos ? 0 : loc + 1;
877 if (result.size() < loc + params_.name.size() + 1
878 || result.substr(loc + 1, params_.name.size()) != params_.name) {
879 gui = _("undefined");
880 info(from_ascii("undefined"), params_.lang);
883 // remove leading comments and \\name and space
884 result = result.substr(loc + params_.name.size() + 2);
887 result = rtrim(result, "\n");
888 result = trim(result, "\"");
892 setText(from_utf8(result), params_.lang);
895 case InsetInfoParams::PACKAGE_INFO:
896 // only need to do this once.
899 // check in packages.lst
900 if (LaTeXFeatures::isAvailable(params_.name)) {
902 info(from_ascii("yes"), params_.lang);
905 info(from_ascii("no"), params_.lang);
910 case InsetInfoParams::TEXTCLASS_INFO: {
911 // the TextClass can change
912 LayoutFileList const & list = LayoutFileList::get();
913 bool available = false;
914 // params_.name is the class name
915 if (list.haveClass(params_.name))
916 available = list[params_.name].isTeXClassAvailable();
919 info(from_ascii("yes"), params_.lang);
922 info(from_ascii("no"), params_.lang);
926 case InsetInfoParams::MENU_INFO: {
927 // only need to do this once.
930 docstring_list names;
931 FuncRequest func = lyxaction.lookupFunc(params_.name);
932 if (func.action() == LFUN_UNKNOWN_ACTION) {
933 gui = _("Unknown action %1$s");
934 error(from_ascii("Unknown action %1$s"), params_.lang);
937 if (func.action() == LFUN_BUFFER_VIEW || func.action() == LFUN_BUFFER_UPDATE)
938 // The default output format is in the menu without argument,
940 if (func.argument() == from_ascii(buffer().params().getDefaultOutputFormat()))
941 func = FuncRequest(func.action());
942 // iterate through the menubackend to find it
944 gui = _("Can't determine menu entry for action %1$s in batch mode");
945 error(from_ascii("Can't determine menu entry for action %1$s in batch mode"), params_.lang);
949 // and we will not keep trying if we fail
950 initialized_ = theApp()->hasBufferView();
951 if (!theApp()->searchMenu(func, names)) {
952 gui = _("No menu entry for action %1$s");
953 error(from_ascii("No menu entry for action %1$s"), params_.lang);
956 // if found, return its path.
958 Paragraph & par = paragraphs().front();
959 Font const f(inherit_font, guilang);
960 params_.force_ltr = !guilang->rightToLeft();
962 //fu.fontInfo().setUnderbar(FONT_ON);
963 for (docstring const & name : names) {
964 // do not insert > for the top level menu item
965 if (&name != &names.front())
966 par.insertInset(par.size(), new InsetSpecialChar(InsetSpecialChar::MENU_SEPARATOR),
967 f, Change(Change::UNCHANGED));
968 //FIXME: add proper underlines here. This
969 // involves rewriting searchMenu used above to
970 // return a vector of menus. If we do not do
971 // that, we might as well use below
972 // Paragraph::insert on each string (JMarc)
973 for (char_type c : name)
974 par.insertChar(par.size(), c, f, Change(Change::UNCHANGED));
978 case InsetInfoParams::L7N_INFO: {
979 docstring locstring = _(params_.name);
980 // Remove trailing colons
981 locstring = rtrim(locstring, ":");
982 // Remove menu accelerators
983 if (contains(locstring, from_ascii("|"))) {
984 docstring nlocstring;
985 rsplit(locstring, nlocstring, '|');
986 locstring = nlocstring;
988 // Remove Qt accelerators, but keep literal ampersands
989 locstring = subst(locstring, from_ascii(" & "), from_ascii("</amp;>"));
990 locstring = subst(locstring, from_ascii("&"), docstring());
991 locstring = subst(locstring, from_ascii("</amp;>"), from_ascii(" & "));
992 setText(locstring, guilang);
993 params_.force_ltr = !guilang->rightToLeft() && !params_.lang->rightToLeft();
996 case InsetInfoParams::ICON_INFO: {
997 // only need to do this once.
1000 // and we will not keep trying if we fail
1001 initialized_ = true;
1002 FuncRequest func = lyxaction.lookupFunc(params_.name);
1003 docstring icon_name = frontend::Application::iconName(func, true);
1004 // FIXME: We should use the icon directly instead of
1005 // going through FileName. The code below won't work
1006 // if the icon is embedded in the executable through
1007 // the Qt resource system.
1008 // This is only a negligible performance problem:
1009 // If the installed icon differs from the resource icon the
1010 // installed one is preferred anyway, and all icons that are
1011 // embedded in the resources are installed as well.
1012 FileName file(to_utf8(icon_name));
1013 if (file.onlyFileNameWithoutExt() == "unknown") {
1014 string dir = "images";
1015 FileName file2(imageLibFileSearch(dir, params_.name, "svgz,png"));
1021 int percent_scale = 100;
1023 // Compute the scale factor for the icon such that its
1024 // width on screen is equal to 1em in pixels.
1025 // The scale factor is rounded to the integer nearest
1026 // to the float value of the ratio 100*iconsize/imgsize.
1027 int imgsize = QImage(toqstr(file.absFileName())).width();
1029 int iconsize = Length(1, Length::EM).inPixels(1);
1030 percent_scale = (100 * iconsize + imgsize / 2)/imgsize;
1033 InsetGraphics * inset = new InsetGraphics(buffer_);
1034 InsetGraphicsParams igp;
1035 igp.filename = file;
1036 igp.lyxscale = percent_scale;
1037 igp.scale = string();
1038 igp.width = Length(1, Length::EM);
1039 inset->setParams(igp);
1041 Font const f(inherit_font, params_.lang);
1042 paragraphs().front().insertInset(0, inset, f,
1043 Change(Change::UNCHANGED));
1046 case InsetInfoParams::BUFFER_INFO: {
1047 // this could all change, so we will recalculate each time
1048 if (params_.name == "name")
1049 setText(from_utf8(buffer().fileName().onlyFileName()), params_.lang);
1050 else if (params_.name == "name-noext")
1051 setText(from_utf8(buffer().fileName().onlyFileNameWithoutExt()), params_.lang);
1052 else if (params_.name == "path")
1053 setText(from_utf8(os::latex_path(buffer().filePath())), params_.lang);
1054 else if (params_.name == "class")
1055 setText(from_utf8(bp.documentClass().name()), params_.lang);
1058 case InsetInfoParams::VCS_INFO: {
1059 // this information could change, in principle, so we will
1060 // recalculate each time through
1061 if (!buffer().lyxvc().inUse()) {
1062 gui = _("No version control!");
1063 info(from_ascii("No version control!"), params_.lang);
1066 LyXVC::RevisionInfo itype = LyXVC::Unknown;
1067 if (params_.name == "revision")
1068 itype = LyXVC::File;
1069 else if (params_.name == "tree-revision")
1070 itype = LyXVC::Tree;
1071 else if (params_.name == "author")
1072 itype = LyXVC::Author;
1073 else if (params_.name == "time")
1074 itype = LyXVC::Time;
1075 else if (params_.name == "date")
1076 itype = LyXVC::Date;
1077 string binfo = buffer().lyxvc().revisionInfo(itype);
1078 if (binfo.empty()) {
1079 gui = _("%1$s[[vcs data]] unknown");
1080 error(from_ascii("%1$s[[vcs data]] unknown"), params_.lang);
1082 setText(from_utf8(binfo), params_.lang);
1085 case InsetInfoParams::LYX_INFO:
1086 // only need to do this once.
1089 if (params_.name == "version")
1090 setText(from_ascii(lyx_version), params_.lang);
1091 else if (params_.name == "layoutformat")
1092 setText(convert<docstring>(LAYOUT_FORMAT), params_.lang);
1093 initialized_ = true;
1095 case InsetInfoParams::DATE_INFO:
1096 case InsetInfoParams::MODDATE_INFO:
1097 case InsetInfoParams::FIXDATE_INFO: {
1098 string date_format = params_.name;
1099 string const date_specifier = (params_.type == InsetInfoParams::FIXDATE_INFO
1100 && contains(params_.name, '@'))
1101 ? split(params_.name, date_format, '@') : string();
1103 if (params_.type == InsetInfoParams::MODDATE_INFO)
1104 date = QDateTime::fromTime_t(buffer().fileName().lastModified()).date();
1105 else if (params_.type == InsetInfoParams::FIXDATE_INFO && !date_specifier.empty())
1106 date = QDate::fromString(toqstr(date_specifier), Qt::ISODate);
1108 date = QDate::currentDate();
1109 setText(params_.getDate(date_format, date), params_.lang);
1112 case InsetInfoParams::TIME_INFO:
1113 case InsetInfoParams::MODTIME_INFO:
1114 case InsetInfoParams::FIXTIME_INFO: {
1115 string time_format = params_.name;
1116 string const time_specifier = (params_.type == InsetInfoParams::FIXTIME_INFO
1117 && contains(params_.name, '@'))
1118 ? split(params_.name, time_format, '@') : string();
1120 if (params_.type == InsetInfoParams::MODTIME_INFO)
1121 time = QDateTime::fromTime_t(buffer().fileName().lastModified()).time();
1122 else if (params_.type == InsetInfoParams::FIXTIME_INFO && !time_specifier.empty())
1123 time = QTime::fromString(toqstr(time_specifier), Qt::ISODate);
1125 time = QTime::currentTime();
1126 setText(params_.getTime(time_format, time), params_.lang);
1131 // Just to do something with that string
1132 LYXERR(Debug::INFO, "info inset text: " << gui);
1133 InsetCollapsible::updateBuffer(it, utype);
1137 string InsetInfo::contextMenu(BufferView const &, int, int) const
1139 //FIXME: We override the implementation of InsetCollapsible,
1140 //because this inset is not a collapsible inset.
1141 return contextMenuName();
1145 string InsetInfo::contextMenuName() const
1147 return "context-info";