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 flag.setEnabled(true);
591 case LFUN_INSET_MODIFY:
592 if (validateModifyArgument(cmd.argument())) {
593 flag.setEnabled(true);
595 string name = trim(split(to_utf8(cmd.argument()), typestr, ' '));
596 InsetInfoParams::info_type type = nameTranslator().find(typestr);
597 string origname = params_.name;
598 if (type == InsetInfoParams::FIXDATE_INFO
599 || type == InsetInfoParams::FIXTIME_INFO)
600 split(params_.name, origname, '@');
601 flag.setOnOff(type == params_.type && name == origname);
612 void InsetInfo::doDispatch(Cursor & cur, FuncRequest & cmd)
614 switch (cmd.action()) {
615 case LFUN_INSET_MODIFY:
617 setInfo(to_utf8(cmd.argument()));
618 cur.forceBufferUpdate();
619 initialized_ = false;
622 case LFUN_INSET_COPY_AS: {
623 cap::clearSelection();
625 copy.pushBackward(*this);
629 copy.pit() = copy.lastpit();
630 copy.pos() = copy.lastpos();
632 cap::copySelection(copy);
637 InsetCollapsible::doDispatch(cur, cmd);
643 void InsetInfo::setInfo(string const & name)
648 string saved_date_specifier;
649 // Store old date specifier for potential re-use
650 if (!params_.name.empty())
651 saved_date_specifier = split(params_.name, '@');
654 params_.name = trim(split(name, type, ' '));
655 params_.type = nameTranslator().find(type);
656 if (params_.name.empty())
657 params_.name = defaultValueTranslator().find(params_.type);
658 if (params_.type == InsetInfoParams::FIXDATE_INFO) {
659 string const date_specifier = split(params_.name, '@');
660 // If an explicit new fix date is specified, use that
661 // Otherwise, use the old one or, if there is none,
663 if (date_specifier.empty()) {
664 if (saved_date_specifier.empty())
665 params_.name += "@" + fromqstr(QDate::currentDate().toString(Qt::ISODate));
667 params_.name += "@" + saved_date_specifier;
670 else if (params_.type == InsetInfoParams::FIXTIME_INFO) {
671 string const time_specifier = split(params_.name, '@');
672 // If an explicit new fix time is specified, use that
673 // Otherwise, use the old one or, if there is none,
675 if (time_specifier.empty()) {
676 if (saved_date_specifier.empty())
677 params_.name += "@" + fromqstr(QTime::currentTime().toString(Qt::ISODate));
679 params_.name += "@" + saved_date_specifier;
685 void InsetInfo::error(docstring const & err, Language const * lang)
687 setText(bformat(translateIfPossible(err, lang->code()), from_utf8(params_.name)),
688 Font(inherit_font, lang), false);
692 void InsetInfo::info(docstring const & err, Language const * lang)
694 setText(translateIfPossible(err, lang->code()),
695 Font(inherit_font, lang), false);
699 void InsetInfo::setText(docstring const & str, Language const * lang)
701 setText(str, Font(inherit_font, lang), false);
705 bool InsetInfo::forceLTR() const
707 return params_.force_ltr;
711 void InsetInfo::updateBuffer(ParIterator const & it, UpdateType utype) {
712 // If the Buffer is a clone, then we neither need nor want to do any
713 // of what follows. We want, rather, just to inherit how things were
714 // in the original Buffer. This is especially important for VCS.
715 // Otherwise, we could in principle have different settings here
716 // than in the Buffer we were exporting.
717 if (buffer().isClone())
720 BufferParams const & bp = buffer().params();
721 params_.lang = it.paragraph().getFontSettings(bp, it.pos()).language();
722 Language const * tryguilang = languages.getFromCode(Messages::guiLanguage());
723 // Some info insets use the language of the GUI (if available)
724 Language const * guilang = tryguilang ? tryguilang : params_.lang;
726 params_.force_ltr = !params_.lang->rightToLeft();
727 // This is just to get the string into the po files
729 switch (params_.type) {
730 case InsetInfoParams::UNKNOWN_INFO:
731 gui = _("Unknown Info!");
732 info(from_ascii("Unknown Info!"), params_.lang);
733 initialized_ = false;
735 case InsetInfoParams::SHORTCUT_INFO:
736 case InsetInfoParams::SHORTCUTS_INFO: {
737 // shortcuts can change, so we need to re-do this each time
738 FuncRequest const func = lyxaction.lookupFunc(params_.name);
739 if (func.action() == LFUN_UNKNOWN_ACTION) {
740 gui = _("Unknown action %1$s");
741 error(from_ascii("Unknown action %1$s"), params_.lang);
744 KeyMap::Bindings bindings = theTopLevelKeymap().findBindings(func);
745 if (bindings.empty()) {
746 gui = _("undefined");
747 info(from_ascii("undefined"), params_.lang);
750 if (params_.type == InsetInfoParams::SHORTCUT_INFO)
751 setText(bindings.begin()->print(KeySequence::ForGui), guilang);
753 setText(theTopLevelKeymap().printBindings(func, KeySequence::ForGui), guilang);
754 params_.force_ltr = !guilang->rightToLeft() && !params_.lang->rightToLeft();
757 case InsetInfoParams::LYXRC_INFO: {
758 // this information could change, if the preferences are changed,
759 // so we will recalculate each time through.
761 if (params_.name.empty()) {
762 gui = _("undefined");
763 info(from_ascii("undefined"), params_.lang);
766 // FIXME this uses the serialization mechanism to get the info
767 // we want, which i guess works but is a bit strange.
768 lyxrc.write(oss, true, params_.name);
769 string result = oss.str();
770 if (result.size() < 2) {
771 gui = _("undefined");
772 info(from_ascii("undefined"), params_.lang);
775 string::size_type loc = result.rfind("\n", result.size() - 2);
776 loc = loc == string::npos ? 0 : loc + 1;
777 if (result.size() < loc + params_.name.size() + 1
778 || result.substr(loc + 1, params_.name.size()) != params_.name) {
779 gui = _("undefined");
780 info(from_ascii("undefined"), params_.lang);
783 // remove leading comments and \\name and space
784 result = result.substr(loc + params_.name.size() + 2);
787 result = rtrim(result, "\n");
788 result = trim(result, "\"");
792 setText(from_utf8(result), params_.lang);
795 case InsetInfoParams::PACKAGE_INFO:
796 // only need to do this once.
799 // check in packages.lst
800 if (LaTeXFeatures::isAvailable(params_.name)) {
802 info(from_ascii("yes"), params_.lang);
805 info(from_ascii("no"), params_.lang);
810 case InsetInfoParams::TEXTCLASS_INFO: {
811 // the TextClass can change
812 LayoutFileList const & list = LayoutFileList::get();
813 bool available = false;
814 // params_.name is the class name
815 if (list.haveClass(params_.name))
816 available = list[params_.name].isTeXClassAvailable();
819 info(from_ascii("yes"), params_.lang);
822 info(from_ascii("no"), params_.lang);
826 case InsetInfoParams::MENU_INFO: {
827 // only need to do this once.
830 docstring_list names;
831 FuncRequest const func = lyxaction.lookupFunc(params_.name);
832 if (func.action() == LFUN_UNKNOWN_ACTION) {
833 gui = _("Unknown action %1$s");
834 error(from_ascii("Unknown action %1$s"), params_.lang);
837 // iterate through the menubackend to find it
839 gui = _("Can't determine menu entry for action %1$s in batch mode");
840 error(from_ascii("Can't determine menu entry for action %1$s in batch mode"), params_.lang);
844 // and we will not keep trying if we fail
845 initialized_ = theApp()->hasBufferView();
846 if (!theApp()->searchMenu(func, names)) {
847 gui = _("No menu entry for action %1$s");
848 error(from_ascii("No menu entry for action %1$s"), params_.lang);
851 // if found, return its path.
853 Paragraph & par = paragraphs().front();
854 Font const f(inherit_font, guilang);
855 params_.force_ltr = !guilang->rightToLeft();
857 //fu.fontInfo().setUnderbar(FONT_ON);
858 for (docstring const & name : names) {
859 // do not insert > for the top level menu item
860 if (&name != &names.front())
861 par.insertInset(par.size(), new InsetSpecialChar(InsetSpecialChar::MENU_SEPARATOR),
862 f, Change(Change::UNCHANGED));
863 //FIXME: add proper underlines here. This
864 // involves rewriting searchMenu used above to
865 // return a vector of menus. If we do not do
866 // that, we might as well use below
867 // Paragraph::insert on each string (JMarc)
868 for (char_type c : name)
869 par.insertChar(par.size(), c, f, Change(Change::UNCHANGED));
873 case InsetInfoParams::ICON_INFO: {
874 // only need to do this once.
877 // and we will not keep trying if we fail
879 FuncRequest func = lyxaction.lookupFunc(params_.name);
880 docstring icon_name = frontend::Application::iconName(func, true);
881 // FIXME: We should use the icon directly instead of
882 // going through FileName. The code below won't work
883 // if the icon is embedded in the executable through
884 // the Qt resource system.
885 // This is only a negligible performance problem:
886 // If the installed icon differs from the resource icon the
887 // installed one is preferred anyway, and all icons that are
888 // embedded in the resources are installed as well.
889 FileName file(to_utf8(icon_name));
890 if (file.onlyFileNameWithoutExt() == "unknown") {
891 string dir = "images";
892 FileName file2(imageLibFileSearch(dir, params_.name, "svgz,png"));
898 int percent_scale = 100;
900 // Compute the scale factor for the icon such that its
901 // width on screen is equal to 1em in pixels.
902 // The scale factor is rounded to the integer nearest
903 // to the float value of the ratio 100*iconsize/imgsize.
904 int imgsize = QImage(toqstr(file.absFileName())).width();
906 int iconsize = Length(1, Length::EM).inPixels(1);
907 percent_scale = (100 * iconsize + imgsize / 2)/imgsize;
910 InsetGraphics * inset = new InsetGraphics(buffer_);
911 InsetGraphicsParams igp;
913 igp.lyxscale = percent_scale;
914 igp.scale = string();
915 igp.width = Length(1, Length::EM);
916 inset->setParams(igp);
918 Font const f(inherit_font, params_.lang);
919 paragraphs().front().insertInset(0, inset, f,
920 Change(Change::UNCHANGED));
923 case InsetInfoParams::BUFFER_INFO: {
924 // this could all change, so we will recalculate each time
925 if (params_.name == "name")
926 setText(from_utf8(buffer().fileName().onlyFileName()), params_.lang);
927 else if (params_.name == "name-noext")
928 setText(from_utf8(buffer().fileName().onlyFileNameWithoutExt()), params_.lang);
929 else if (params_.name == "path")
930 setText(from_utf8(os::latex_path(buffer().filePath())), params_.lang);
931 else if (params_.name == "class")
932 setText(from_utf8(bp.documentClass().name()), params_.lang);
935 case InsetInfoParams::VCS_INFO: {
936 // this information could change, in principle, so we will
937 // recalculate each time through
938 if (!buffer().lyxvc().inUse()) {
939 gui = _("No version control!");
940 info(from_ascii("No version control!"), params_.lang);
943 LyXVC::RevisionInfo itype = LyXVC::Unknown;
944 if (params_.name == "revision")
946 else if (params_.name == "tree-revision")
948 else if (params_.name == "author")
949 itype = LyXVC::Author;
950 else if (params_.name == "time")
952 else if (params_.name == "date")
954 string binfo = buffer().lyxvc().revisionInfo(itype);
956 gui = _("%1$s[[vcs data]] unknown");
957 error(from_ascii("%1$s[[vcs data]] unknown"), params_.lang);
959 setText(from_utf8(binfo), params_.lang);
962 case InsetInfoParams::LYX_INFO:
963 // only need to do this once.
966 if (params_.name == "version")
967 setText(from_ascii(lyx_version), params_.lang);
970 case InsetInfoParams::DATE_INFO:
971 case InsetInfoParams::MODDATE_INFO:
972 case InsetInfoParams::FIXDATE_INFO: {
973 string date_format = params_.name;
974 string const date_specifier = (params_.type == InsetInfoParams::FIXDATE_INFO
975 && contains(params_.name, '@'))
976 ? split(params_.name, date_format, '@') : string();
978 if (params_.type == InsetInfoParams::MODDATE_INFO)
979 date = QDateTime::fromTime_t(buffer().fileName().lastModified()).date();
980 else if (params_.type == InsetInfoParams::FIXDATE_INFO && !date_specifier.empty())
981 date = QDate::fromString(toqstr(date_specifier), Qt::ISODate);
983 date = QDate::currentDate();
984 setText(params_.getDate(date_format, date), params_.lang);
987 case InsetInfoParams::TIME_INFO:
988 case InsetInfoParams::MODTIME_INFO:
989 case InsetInfoParams::FIXTIME_INFO: {
990 string time_format = params_.name;
991 string const time_specifier = (params_.type == InsetInfoParams::FIXTIME_INFO
992 && contains(params_.name, '@'))
993 ? split(params_.name, time_format, '@') : string();
995 if (params_.type == InsetInfoParams::MODTIME_INFO)
996 time = QDateTime::fromTime_t(buffer().fileName().lastModified()).time();
997 else if (params_.type == InsetInfoParams::FIXTIME_INFO && !time_specifier.empty())
998 time = QTime::fromString(toqstr(time_specifier), Qt::ISODate);
1000 time = QTime::currentTime();
1001 setText(params_.getTime(time_format, time), params_.lang);
1006 // Just to do something with that string
1007 LYXERR(Debug::INFO, "info inset text: " << gui);
1008 InsetCollapsible::updateBuffer(it, utype);
1012 string InsetInfo::contextMenu(BufferView const &, int, int) const
1014 //FIXME: We override the implementation of InsetCollapsible,
1015 //because this inset is not a collapsible inset.
1016 return contextMenuName();
1020 string InsetInfo::contextMenuName() const
1022 return "context-info";