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::ICON_INFO, "icon");
79 translator.addPair(InsetInfoParams::BUFFER_INFO, "buffer");
80 translator.addPair(InsetInfoParams::LYX_INFO, "lyxinfo");
81 translator.addPair(InsetInfoParams::VCS_INFO, "vcs");
82 translator.addPair(InsetInfoParams::DATE_INFO, "date");
83 translator.addPair(InsetInfoParams::MODDATE_INFO, "moddate");
84 translator.addPair(InsetInfoParams::FIXDATE_INFO, "fixdate");
85 translator.addPair(InsetInfoParams::TIME_INFO, "time");
86 translator.addPair(InsetInfoParams::MODTIME_INFO, "modtime");
87 translator.addPair(InsetInfoParams::FIXTIME_INFO, "fixtime");
92 /// The translator between the information type enum and corresponding string.
93 NameTranslator const & nameTranslator()
95 static NameTranslator const translator = initTranslator();
100 typedef Translator<InsetInfoParams::info_type, string> DefaultValueTranslator;
102 DefaultValueTranslator const initDVTranslator()
104 DefaultValueTranslator translator(InsetInfoParams::UNKNOWN_INFO, "");
106 translator.addPair(InsetInfoParams::SHORTCUTS_INFO, "info-insert");
107 translator.addPair(InsetInfoParams::SHORTCUT_INFO, "info-insert");
108 translator.addPair(InsetInfoParams::LYXRC_INFO, "user_name");
109 translator.addPair(InsetInfoParams::PACKAGE_INFO, "graphics");
110 translator.addPair(InsetInfoParams::TEXTCLASS_INFO, "article");
111 translator.addPair(InsetInfoParams::MENU_INFO, "info-insert");
112 translator.addPair(InsetInfoParams::ICON_INFO, "info-insert");
113 translator.addPair(InsetInfoParams::BUFFER_INFO, "name-noext");
114 translator.addPair(InsetInfoParams::LYX_INFO, "version");
115 translator.addPair(InsetInfoParams::VCS_INFO, "revision");
116 translator.addPair(InsetInfoParams::DATE_INFO, "loclong");
117 translator.addPair(InsetInfoParams::MODDATE_INFO, "loclong");
118 translator.addPair(InsetInfoParams::FIXDATE_INFO, "loclong");
119 translator.addPair(InsetInfoParams::TIME_INFO, "long");
120 translator.addPair(InsetInfoParams::MODTIME_INFO, "long");
121 translator.addPair(InsetInfoParams::FIXTIME_INFO, "long");
126 /// The translator between the information type enum and some sensible default value.
127 DefaultValueTranslator const & defaultValueTranslator()
129 static DefaultValueTranslator const translator = initDVTranslator();
136 /////////////////////////////////////////////////////////////////////
140 ///////////////////////////////////////////////////////////////////////
142 InsetInfoParams infoparams;
145 set<string> getTexFileList(string const & filename)
148 FileName const file = libFileSearch(string(), filename);
153 vector<docstring> doclist =
154 getVectorFromString(file.fileContents("UTF-8"), from_ascii("\n"));
156 // Normalise paths like /foo//bar ==> /foo/bar
157 for (auto doc : doclist) {
158 subst(doc, from_ascii("\r"), docstring());
159 while (contains(doc, from_ascii("//")))
160 subst(doc, from_ascii("//"), from_ascii("/"));
162 list.insert(removeExtension(onlyFileName(to_utf8(doc))));
171 docstring InsetInfoParams::getDate(string const iname, QDate const date) const
175 loc = QLocale(toqstr(lang->code()));
177 return qstring_to_ucs4(loc.toString(date, QLocale::LongFormat));
178 else if (iname == "short")
179 return qstring_to_ucs4(loc.toString(date, QLocale::ShortFormat));
180 else if (iname == "ISO")
181 return qstring_to_ucs4(date.toString(Qt::ISODate));
182 else if (iname == "loclong")
183 return qstring_to_ucs4(loc.toString(date, toqstr(lang->dateFormat(0))));
184 else if (iname == "locmedium")
185 return qstring_to_ucs4(loc.toString(date, toqstr(lang->dateFormat(1))));
186 else if (iname == "locshort")
187 return qstring_to_ucs4(loc.toString(date, toqstr(lang->dateFormat(2))));
189 return qstring_to_ucs4(loc.toString(date, toqstr(iname)));
193 docstring InsetInfoParams::getTime(string const iname, QTime const time) const
197 loc = QLocale(toqstr(lang->code()));
199 return qstring_to_ucs4(loc.toString(time, QLocale::LongFormat));
200 else if (iname == "short")
201 return qstring_to_ucs4(loc.toString(time, QLocale::ShortFormat));
202 else if (iname == "ISO")
203 return qstring_to_ucs4(time.toString(Qt::ISODate));
205 return qstring_to_ucs4(loc.toString(time, toqstr(iname)));
209 vector<pair<string,docstring>> InsetInfoParams::getArguments(Buffer const * buf,
210 string const & itype) const
212 vector<pair<string,docstring>> result;
214 switch (nameTranslator().find(itype)) {
216 result.push_back(make_pair("invalid", _("Please select a valid type!")));
223 result.push_back(make_pair("custom", _("Custom")));
224 LyXAction::const_iterator fit = lyxaction.func_begin();
225 LyXAction::const_iterator const fen = lyxaction.func_end();
226 for (; fit != fen; ++fit) {
227 string const lfun = fit->first;
229 result.push_back(make_pair(lfun, from_ascii(lfun)));
235 result.push_back(make_pair("custom", _("Custom")));
236 set<string> rcs = lyxrc.getRCs();
237 for (auto const & rc : rcs)
238 result.push_back(make_pair(rc, from_ascii(rc)));
243 case TEXTCLASS_INFO: {
244 result.push_back(make_pair("custom", _("Custom")));
245 string const filename = (itype == "package") ? "styFiles.lst"
247 set<string> flist = getTexFileList(filename);
248 for (auto const & f : flist)
249 result.push_back(make_pair(f, from_utf8(f)));
254 result.push_back(make_pair("name", _("File name (with extension)")));
255 result.push_back(make_pair("name-noext", _("File name (without extension)")));
256 result.push_back(make_pair("path", _("File path")));
257 result.push_back(make_pair("class", _("Used text class")));
261 if (!buf->lyxvc().inUse()) {
262 result.push_back(make_pair("invalid", _("No version control!")));
265 result.push_back(make_pair("revision", _("Revision[[Version Control]]")));
266 result.push_back(make_pair("tree-revision", _("Tree revision")));
267 result.push_back(make_pair("author", _("Author")));
268 result.push_back(make_pair("date", _("Date")));
269 result.push_back(make_pair("time", _("Time[[of day]]")));
274 result.push_back(make_pair("version", _("LyX version")));
280 string const dt = split(name, '@');
282 if (itype == "moddate")
283 date = QDateTime::fromTime_t(buf->fileName().lastModified()).date();
284 else if (itype == "fixdate" && !dt.empty()) {
285 QDate const gdate = QDate::fromString(toqstr(dt), Qt::ISODate);
286 date = (gdate.isValid()) ? gdate : QDate::currentDate();
288 date = QDate::currentDate();
289 result.push_back(make_pair("long",getDate("long", date)));
290 result.push_back(make_pair("short", getDate("short", date)));
291 result.push_back(make_pair("loclong", getDate("loclong", date)));
292 result.push_back(make_pair("locmedium", getDate("locmedium", date)));
293 result.push_back(make_pair("locshort", getDate("locshort", date)));
294 result.push_back(make_pair("ISO", getDate("ISO", date)));
295 result.push_back(make_pair("yyyy", getDate("yyyy", date)));
296 result.push_back(make_pair("MMMM", getDate("MMMM", date)));
297 result.push_back(make_pair("MMM", getDate("MMM", date)));
298 result.push_back(make_pair("dddd", getDate("dddd", date)));
299 result.push_back(make_pair("ddd", getDate("ddd", date)));
300 result.push_back(make_pair("custom", _("Custom")));
306 string const tt = split(name, '@');
308 if (itype == "modtime")
309 time = QDateTime::fromTime_t(buf->fileName().lastModified()).time();
310 else if (itype == "fixtime" && !tt.empty()) {
311 QTime const gtime = QTime::fromString(toqstr(tt), Qt::ISODate);
312 time = (gtime.isValid()) ? gtime : QTime::currentTime();
314 time = QTime::currentTime();
315 result.push_back(make_pair("long",getTime("long", time)));
316 result.push_back(make_pair("short", getTime("short", time)));
317 result.push_back(make_pair("ISO", getTime("ISO", time)));
318 result.push_back(make_pair("custom", _("Custom")));
327 string InsetInfoParams::infoType() const
329 return nameTranslator().find(type);
334 /////////////////////////////////////////////////////////////////////////
338 /////////////////////////////////////////////////////////////////////////
342 InsetInfo::InsetInfo(Buffer * buf, string const & name)
343 : InsetCollapsible(buf), initialized_(false)
345 params_.type = InsetInfoParams::UNKNOWN_INFO;
346 params_.force_ltr = false;
352 Inset * InsetInfo::editXY(Cursor & cur, int x, int y)
354 // do not allow the cursor to be set in this Inset
355 return Inset::editXY(cur, x, y);
359 docstring InsetInfo::layoutName() const
361 return from_ascii("Info:" + params_.infoType());
365 docstring InsetInfo::toolTip(BufferView const &, int, int) const
368 switch (nameTranslator().find(params_.infoType())) {
369 case InsetInfoParams::UNKNOWN_INFO:
370 result = _("Invalid information inset");
372 case InsetInfoParams::SHORTCUT_INFO:
373 result = bformat(_("The keybard shortcut for the function '%1$s'"),
374 from_utf8(params_.name));
376 case InsetInfoParams::SHORTCUTS_INFO:
377 result = bformat(_("The keybard shortcuts for the function '%1$s'"),
378 from_utf8(params_.name));
380 case InsetInfoParams::MENU_INFO:
381 result = bformat(_("The menu location for the function '%1$s'"),
382 from_utf8(params_.name));
384 case InsetInfoParams::ICON_INFO:
385 result = bformat(_("The toolbar icon for the function '%1$s'"),
386 from_utf8(params_.name));
388 case InsetInfoParams::LYXRC_INFO:
389 result = bformat(_("The preference setting for the preference key '%1$s'"),
390 from_utf8(params_.name));
392 case InsetInfoParams::PACKAGE_INFO:
393 result = bformat(_("Availability of the LaTeX package '%1$s'"),
394 from_utf8(params_.name));
396 case InsetInfoParams::TEXTCLASS_INFO:
397 result = bformat(_("Availability of the LaTeX class '%1$s'"),
398 from_utf8(params_.name));
400 case InsetInfoParams::BUFFER_INFO:
401 if (params_.name == "name")
402 result = _("The name of this file (incl. extension)");
403 else if (params_.name == "name-noext")
404 result = _("The name of this file (without extension)");
405 else if (params_.name == "path")
406 result = _("The path where this file is saved");
407 else if (params_.name == "class")
408 result = _("The class this document uses");
410 case InsetInfoParams::VCS_INFO:
411 if (params_.name == "revision")
412 result = _("Version control revision");
413 else if (params_.name == "tree-revision")
414 result = _("Version control tree revision");
415 else if (params_.name == "author")
416 result = _("Version control author");
417 else if (params_.name == "date")
418 result = _("Version control date");
419 else if (params_.name == "time")
420 result = _("Version control time");
422 case InsetInfoParams::LYX_INFO:
423 result = _("The current LyX version");
425 case InsetInfoParams::DATE_INFO:
426 result = _("The current date");
428 case InsetInfoParams::MODDATE_INFO:
429 result = _("The date of last save");
431 case InsetInfoParams::FIXDATE_INFO:
432 result = _("A static date");
434 case InsetInfoParams::TIME_INFO:
435 result = _("The current time");
437 case InsetInfoParams::MODTIME_INFO:
438 result = _("The time of last save");
440 case InsetInfoParams::FIXTIME_INFO:
441 result = _("A static time");
449 void InsetInfo::read(Lexer & lex)
454 token = lex.getString();
455 if (token == "type") {
457 token = lex.getString();
458 params_.type = nameTranslator().find(token);
459 } else if (token == "arg") {
461 params_.name = lex.getString();
462 } else if (token == "\\end_inset")
465 if (token != "\\end_inset") {
466 lex.printError("Missing \\end_inset at this point");
467 throw ExceptionMessage(WarningException,
468 _("Missing \\end_inset at this point."),
474 void InsetInfo::write(ostream & os) const
476 os << "Info\ntype \"" << params_.infoType()
477 << "\"\narg " << Lexer::quoteString(params_.name);
481 bool InsetInfo::validateModifyArgument(docstring const & arg) const
484 string name = trim(split(to_utf8(arg), type, ' '));
486 switch (nameTranslator().find(type)) {
487 case InsetInfoParams::UNKNOWN_INFO:
490 case InsetInfoParams::SHORTCUT_INFO:
491 case InsetInfoParams::SHORTCUTS_INFO:
492 case InsetInfoParams::MENU_INFO: {
493 FuncRequest func = lyxaction.lookupFunc(name);
494 return func.action() != LFUN_UNKNOWN_ACTION;
497 case InsetInfoParams::ICON_INFO: {
498 FuncCode const action = lyxaction.lookupFunc(name).action();
499 if (action == LFUN_UNKNOWN_ACTION) {
500 string dir = "images";
501 return !imageLibFileSearch(dir, name, "svgz,png").empty();
506 case InsetInfoParams::LYXRC_INFO: {
507 set<string> rcs = lyxrc.getRCs();
508 return rcs.find(name) != rcs.end();
511 case InsetInfoParams::PACKAGE_INFO:
512 case InsetInfoParams::TEXTCLASS_INFO:
515 case InsetInfoParams::BUFFER_INFO:
516 return (name == "name" || name == "name-noext"
517 || name == "path" || name == "class");
519 case InsetInfoParams::VCS_INFO:
520 if (name == "revision" || name == "tree-revision"
521 || name == "author" || name == "date" || name == "time")
522 return buffer().lyxvc().inUse();
525 case InsetInfoParams::LYX_INFO:
526 return name == "version";
528 case InsetInfoParams::FIXDATE_INFO: {
531 date = split(name, piece, '@');
532 if (!date.empty() && !QDate::fromString(toqstr(date), Qt::ISODate).isValid())
538 case InsetInfoParams::DATE_INFO:
539 case InsetInfoParams::MODDATE_INFO: {
540 if (name == "long" || name == "short" || name == "ISO")
543 QDate date = QDate::currentDate();
544 return !date.toString(toqstr(name)).isEmpty();
547 case InsetInfoParams::FIXTIME_INFO: {
550 time = split(name, piece, '@');
551 if (!time.empty() && !QTime::fromString(toqstr(time), Qt::ISODate).isValid())
557 case InsetInfoParams::TIME_INFO:
558 case InsetInfoParams::MODTIME_INFO: {
559 if (name == "long" || name == "short" || name == "ISO")
562 QTime time = QTime::currentTime();
563 return !time.toString(toqstr(name)).isEmpty();
572 bool InsetInfo::showInsetDialog(BufferView * bv) const
574 bv->showDialog("info");
579 bool InsetInfo::getStatus(Cursor & cur, FuncRequest const & cmd,
580 FuncStatus & flag) const
582 switch (cmd.action()) {
583 case LFUN_INSET_SETTINGS:
584 return InsetCollapsible::getStatus(cur, cmd, flag);
586 case LFUN_INSET_DIALOG_UPDATE:
587 case LFUN_INSET_COPY_AS:
588 case LFUN_INSET_DISSOLVE:
589 flag.setEnabled(true);
592 case LFUN_INSET_MODIFY:
593 if (validateModifyArgument(cmd.argument())) {
594 flag.setEnabled(true);
596 string name = trim(split(to_utf8(cmd.argument()), typestr, ' '));
597 InsetInfoParams::info_type type = nameTranslator().find(typestr);
598 string origname = params_.name;
599 if (type == InsetInfoParams::FIXDATE_INFO
600 || type == InsetInfoParams::FIXTIME_INFO)
601 split(params_.name, origname, '@');
602 flag.setOnOff(type == params_.type && name == origname);
613 void InsetInfo::doDispatch(Cursor & cur, FuncRequest & cmd)
615 switch (cmd.action()) {
616 case LFUN_INSET_MODIFY:
618 setInfo(to_utf8(cmd.argument()));
619 cur.forceBufferUpdate();
620 initialized_ = false;
623 case LFUN_INSET_COPY_AS: {
624 cap::clearSelection();
626 copy.pushBackward(*this);
630 copy.pit() = copy.lastpit();
631 copy.pos() = copy.lastpos();
633 cap::copySelection(copy);
638 InsetCollapsible::doDispatch(cur, cmd);
644 void InsetInfo::setInfo(string const & name)
649 string saved_date_specifier;
650 // Store old date specifier for potential re-use
651 if (!params_.name.empty())
652 saved_date_specifier = split(params_.name, '@');
655 params_.name = trim(split(name, type, ' '));
656 params_.type = nameTranslator().find(type);
657 if (params_.name.empty())
658 params_.name = defaultValueTranslator().find(params_.type);
659 if (params_.type == InsetInfoParams::FIXDATE_INFO) {
660 string const date_specifier = split(params_.name, '@');
661 // If an explicit new fix date is specified, use that
662 // Otherwise, use the old one or, if there is none,
664 if (date_specifier.empty()) {
665 if (saved_date_specifier.empty())
666 params_.name += "@" + fromqstr(QDate::currentDate().toString(Qt::ISODate));
668 params_.name += "@" + saved_date_specifier;
671 else if (params_.type == InsetInfoParams::FIXTIME_INFO) {
672 string const time_specifier = split(params_.name, '@');
673 // If an explicit new fix time is specified, use that
674 // Otherwise, use the old one or, if there is none,
676 if (time_specifier.empty()) {
677 if (saved_date_specifier.empty())
678 params_.name += "@" + fromqstr(QTime::currentTime().toString(Qt::ISODate));
680 params_.name += "@" + saved_date_specifier;
686 void InsetInfo::error(docstring const & err, Language const * lang)
688 setText(bformat(translateIfPossible(err, lang->code()), from_utf8(params_.name)),
689 Font(inherit_font, lang), false);
693 void InsetInfo::info(docstring const & err, Language const * lang)
695 setText(translateIfPossible(err, lang->code()),
696 Font(inherit_font, lang), false);
700 void InsetInfo::setText(docstring const & str, Language const * lang)
702 setText(str, Font(inherit_font, lang), false);
706 bool InsetInfo::forceLTR() const
708 return params_.force_ltr;
712 void InsetInfo::updateBuffer(ParIterator const & it, UpdateType utype) {
713 // If the Buffer is a clone, then we neither need nor want to do any
714 // of what follows. We want, rather, just to inherit how things were
715 // in the original Buffer. This is especially important for VCS.
716 // Otherwise, we could in principle have different settings here
717 // than in the Buffer we were exporting.
718 if (buffer().isClone())
721 BufferParams const & bp = buffer().params();
722 params_.lang = it.paragraph().getFontSettings(bp, it.pos()).language();
723 Language const * tryguilang = languages.getFromCode(Messages::guiLanguage());
724 // Some info insets use the language of the GUI (if available)
725 Language const * guilang = tryguilang ? tryguilang : params_.lang;
727 params_.force_ltr = !params_.lang->rightToLeft();
728 // This is just to get the string into the po files
730 switch (params_.type) {
731 case InsetInfoParams::UNKNOWN_INFO:
732 gui = _("Unknown Info!");
733 info(from_ascii("Unknown Info!"), params_.lang);
734 initialized_ = false;
736 case InsetInfoParams::SHORTCUT_INFO:
737 case InsetInfoParams::SHORTCUTS_INFO: {
738 // shortcuts can change, so we need to re-do this each time
739 FuncRequest const func = lyxaction.lookupFunc(params_.name);
740 if (func.action() == LFUN_UNKNOWN_ACTION) {
741 gui = _("Unknown action %1$s");
742 error(from_ascii("Unknown action %1$s"), params_.lang);
745 KeyMap::Bindings bindings = theTopLevelKeymap().findBindings(func);
746 if (bindings.empty()) {
747 gui = _("undefined");
748 info(from_ascii("undefined"), params_.lang);
752 if (params_.type == InsetInfoParams::SHORTCUT_INFO)
753 sequence = bindings.begin()->print(KeySequence::ForGui);
755 sequence = theTopLevelKeymap().printBindings(func, KeySequence::ForGui);
756 // QKeySequence returns special characters for keys on the mac
757 // Since these are not included in many fonts, we
758 // re-translate them to textual names (see #10641)
759 odocstringstream ods;
760 string const lcode = params_.lang->code();
761 for (size_t n = 0; n < sequence.size(); ++n) {
762 char_type const c = sequence[n];
765 gui = _("Return[[Key]]");
766 ods << translateIfPossible(from_ascii("Return[[Key]]"), lcode);
768 case 0x21b9://Tab both directions (Win)
769 gui = _("Tab[[Key]]");
770 ods << translateIfPossible(from_ascii("Tab[[Key]]"), lcode);
772 case 0x21de://Qt::Key_PageUp
774 ods << translateIfPossible(from_ascii("PgUp"), lcode);
776 case 0x21df://Qt::Key_PageDown
778 ods << translateIfPossible(from_ascii("PgDown"), lcode);
780 case 0x21e4://Qt::Key_Backtab
782 ods << translateIfPossible(from_ascii("Backtab"), lcode);
784 case 0x21e5://Qt::Key_Tab
786 ods << translateIfPossible(from_ascii("Tab"), lcode);
790 ods << translateIfPossible(from_ascii("Shift"), lcode);
792 case 0x21ea://Qt::Key_CapsLock
794 ods << translateIfPossible(from_ascii("CapsLock"), lcode);
796 case 0x2303://Control
797 gui = _("Control[[Key]]");
798 ods << translateIfPossible(from_ascii("Control[[Key]]"), lcode);
801 gui = _("Command[[Key]]");
802 ods << translateIfPossible(from_ascii("Command[[Key]]"), lcode);
804 case 0x2324://Qt::Key_Enter
805 gui = _("Return[[Key]]");
806 ods << translateIfPossible(from_ascii("Return[[Key]]"), lcode);
808 case 0x2325://Option key
809 gui = _("Option[[Key]]");
810 ods << translateIfPossible(from_ascii("Option[[Key]]"), lcode);
812 case 0x2326://Qt::Key_Delete
813 gui = _("Delete[[Key]]");
814 ods << translateIfPossible(from_ascii("Delete[[Key]]"), lcode);
816 case 0x232b://Qt::Key_Backspace
818 ods << translateIfPossible(from_ascii("Fn+Delete"), lcode);
820 case 0x238b://Qt::Key_Escape
822 ods << translateIfPossible(from_ascii("Esc"), lcode);
828 setText(ods.str(), guilang);
829 params_.force_ltr = !guilang->rightToLeft() && !params_.lang->rightToLeft();
832 case InsetInfoParams::LYXRC_INFO: {
833 // this information could change, if the preferences are changed,
834 // so we will recalculate each time through.
836 if (params_.name.empty()) {
837 gui = _("undefined");
838 info(from_ascii("undefined"), params_.lang);
841 // FIXME this uses the serialization mechanism to get the info
842 // we want, which i guess works but is a bit strange.
843 lyxrc.write(oss, true, params_.name);
844 string result = oss.str();
845 if (result.size() < 2) {
846 gui = _("undefined");
847 info(from_ascii("undefined"), params_.lang);
850 string::size_type loc = result.rfind("\n", result.size() - 2);
851 loc = loc == string::npos ? 0 : loc + 1;
852 if (result.size() < loc + params_.name.size() + 1
853 || result.substr(loc + 1, params_.name.size()) != params_.name) {
854 gui = _("undefined");
855 info(from_ascii("undefined"), params_.lang);
858 // remove leading comments and \\name and space
859 result = result.substr(loc + params_.name.size() + 2);
862 result = rtrim(result, "\n");
863 result = trim(result, "\"");
867 setText(from_utf8(result), params_.lang);
870 case InsetInfoParams::PACKAGE_INFO:
871 // only need to do this once.
874 // check in packages.lst
875 if (LaTeXFeatures::isAvailable(params_.name)) {
877 info(from_ascii("yes"), params_.lang);
880 info(from_ascii("no"), params_.lang);
885 case InsetInfoParams::TEXTCLASS_INFO: {
886 // the TextClass can change
887 LayoutFileList const & list = LayoutFileList::get();
888 bool available = false;
889 // params_.name is the class name
890 if (list.haveClass(params_.name))
891 available = list[params_.name].isTeXClassAvailable();
894 info(from_ascii("yes"), params_.lang);
897 info(from_ascii("no"), params_.lang);
901 case InsetInfoParams::MENU_INFO: {
902 // only need to do this once.
905 docstring_list names;
906 FuncRequest func = lyxaction.lookupFunc(params_.name);
907 if (func.action() == LFUN_UNKNOWN_ACTION) {
908 gui = _("Unknown action %1$s");
909 error(from_ascii("Unknown action %1$s"), params_.lang);
912 if (func.action() == LFUN_BUFFER_VIEW || func.action() == LFUN_BUFFER_UPDATE)
913 // The default output format is in the menu without argument,
915 if (func.argument() == from_ascii(buffer().params().getDefaultOutputFormat()))
916 func = FuncRequest(func.action());
917 // iterate through the menubackend to find it
919 gui = _("Can't determine menu entry for action %1$s in batch mode");
920 error(from_ascii("Can't determine menu entry for action %1$s in batch mode"), params_.lang);
924 // and we will not keep trying if we fail
925 initialized_ = theApp()->hasBufferView();
926 if (!theApp()->searchMenu(func, names)) {
927 gui = _("No menu entry for action %1$s");
928 error(from_ascii("No menu entry for action %1$s"), params_.lang);
931 // if found, return its path.
933 Paragraph & par = paragraphs().front();
934 Font const f(inherit_font, guilang);
935 params_.force_ltr = !guilang->rightToLeft();
937 //fu.fontInfo().setUnderbar(FONT_ON);
938 for (docstring const & name : names) {
939 // do not insert > for the top level menu item
940 if (&name != &names.front())
941 par.insertInset(par.size(), new InsetSpecialChar(InsetSpecialChar::MENU_SEPARATOR),
942 f, Change(Change::UNCHANGED));
943 //FIXME: add proper underlines here. This
944 // involves rewriting searchMenu used above to
945 // return a vector of menus. If we do not do
946 // that, we might as well use below
947 // Paragraph::insert on each string (JMarc)
948 for (char_type c : name)
949 par.insertChar(par.size(), c, f, Change(Change::UNCHANGED));
953 case InsetInfoParams::ICON_INFO: {
954 // only need to do this once.
957 // and we will not keep trying if we fail
959 FuncRequest func = lyxaction.lookupFunc(params_.name);
960 docstring icon_name = frontend::Application::iconName(func, true);
961 // FIXME: We should use the icon directly instead of
962 // going through FileName. The code below won't work
963 // if the icon is embedded in the executable through
964 // the Qt resource system.
965 // This is only a negligible performance problem:
966 // If the installed icon differs from the resource icon the
967 // installed one is preferred anyway, and all icons that are
968 // embedded in the resources are installed as well.
969 FileName file(to_utf8(icon_name));
970 if (file.onlyFileNameWithoutExt() == "unknown") {
971 string dir = "images";
972 FileName file2(imageLibFileSearch(dir, params_.name, "svgz,png"));
978 int percent_scale = 100;
980 // Compute the scale factor for the icon such that its
981 // width on screen is equal to 1em in pixels.
982 // The scale factor is rounded to the integer nearest
983 // to the float value of the ratio 100*iconsize/imgsize.
984 int imgsize = QImage(toqstr(file.absFileName())).width();
986 int iconsize = Length(1, Length::EM).inPixels(1);
987 percent_scale = (100 * iconsize + imgsize / 2)/imgsize;
990 InsetGraphics * inset = new InsetGraphics(buffer_);
991 InsetGraphicsParams igp;
993 igp.lyxscale = percent_scale;
994 igp.scale = string();
995 igp.width = Length(1, Length::EM);
996 inset->setParams(igp);
998 Font const f(inherit_font, params_.lang);
999 paragraphs().front().insertInset(0, inset, f,
1000 Change(Change::UNCHANGED));
1003 case InsetInfoParams::BUFFER_INFO: {
1004 // this could all change, so we will recalculate each time
1005 if (params_.name == "name")
1006 setText(from_utf8(buffer().fileName().onlyFileName()), params_.lang);
1007 else if (params_.name == "name-noext")
1008 setText(from_utf8(buffer().fileName().onlyFileNameWithoutExt()), params_.lang);
1009 else if (params_.name == "path")
1010 setText(from_utf8(os::latex_path(buffer().filePath())), params_.lang);
1011 else if (params_.name == "class")
1012 setText(from_utf8(bp.documentClass().name()), params_.lang);
1015 case InsetInfoParams::VCS_INFO: {
1016 // this information could change, in principle, so we will
1017 // recalculate each time through
1018 if (!buffer().lyxvc().inUse()) {
1019 gui = _("No version control!");
1020 info(from_ascii("No version control!"), params_.lang);
1023 LyXVC::RevisionInfo itype = LyXVC::Unknown;
1024 if (params_.name == "revision")
1025 itype = LyXVC::File;
1026 else if (params_.name == "tree-revision")
1027 itype = LyXVC::Tree;
1028 else if (params_.name == "author")
1029 itype = LyXVC::Author;
1030 else if (params_.name == "time")
1031 itype = LyXVC::Time;
1032 else if (params_.name == "date")
1033 itype = LyXVC::Date;
1034 string binfo = buffer().lyxvc().revisionInfo(itype);
1035 if (binfo.empty()) {
1036 gui = _("%1$s[[vcs data]] unknown");
1037 error(from_ascii("%1$s[[vcs data]] unknown"), params_.lang);
1039 setText(from_utf8(binfo), params_.lang);
1042 case InsetInfoParams::LYX_INFO:
1043 // only need to do this once.
1046 if (params_.name == "version")
1047 setText(from_ascii(lyx_version), params_.lang);
1048 initialized_ = true;
1050 case InsetInfoParams::DATE_INFO:
1051 case InsetInfoParams::MODDATE_INFO:
1052 case InsetInfoParams::FIXDATE_INFO: {
1053 string date_format = params_.name;
1054 string const date_specifier = (params_.type == InsetInfoParams::FIXDATE_INFO
1055 && contains(params_.name, '@'))
1056 ? split(params_.name, date_format, '@') : string();
1058 if (params_.type == InsetInfoParams::MODDATE_INFO)
1059 date = QDateTime::fromTime_t(buffer().fileName().lastModified()).date();
1060 else if (params_.type == InsetInfoParams::FIXDATE_INFO && !date_specifier.empty())
1061 date = QDate::fromString(toqstr(date_specifier), Qt::ISODate);
1063 date = QDate::currentDate();
1064 setText(params_.getDate(date_format, date), params_.lang);
1067 case InsetInfoParams::TIME_INFO:
1068 case InsetInfoParams::MODTIME_INFO:
1069 case InsetInfoParams::FIXTIME_INFO: {
1070 string time_format = params_.name;
1071 string const time_specifier = (params_.type == InsetInfoParams::FIXTIME_INFO
1072 && contains(params_.name, '@'))
1073 ? split(params_.name, time_format, '@') : string();
1075 if (params_.type == InsetInfoParams::MODTIME_INFO)
1076 time = QDateTime::fromTime_t(buffer().fileName().lastModified()).time();
1077 else if (params_.type == InsetInfoParams::FIXTIME_INFO && !time_specifier.empty())
1078 time = QTime::fromString(toqstr(time_specifier), Qt::ISODate);
1080 time = QTime::currentTime();
1081 setText(params_.getTime(time_format, time), params_.lang);
1086 // Just to do something with that string
1087 LYXERR(Debug::INFO, "info inset text: " << gui);
1088 InsetCollapsible::updateBuffer(it, utype);
1092 string InsetInfo::contextMenu(BufferView const &, int, int) const
1094 //FIXME: We override the implementation of InsetCollapsible,
1095 //because this inset is not a collapsible inset.
1096 return contextMenuName();
1100 string InsetInfo::contextMenuName() const
1102 return "context-info";