3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
8 * Full author contact details are available in file CREDITS.
12 #include "InsetInfo.h"
15 #include "BufferParams.h"
16 #include "BufferView.h"
17 #include "CutAndPaste.h"
18 #include "FuncRequest.h"
19 #include "FuncStatus.h"
20 #include "InsetGraphics.h"
21 #include "InsetSpecialChar.h"
23 #include "LaTeXFeatures.h"
24 #include "LayoutFile.h"
25 #include "LyXAction.h"
29 #include "ParagraphParameters.h"
32 #include "frontends/Application.h"
34 #include "support/convert.h"
35 #include "support/debug.h"
36 #include "support/docstream.h"
37 #include "support/docstring_list.h"
38 #include "support/ExceptionMessage.h"
39 #include "support/FileName.h"
40 #include "support/filetools.h"
41 #include "support/gettext.h"
42 #include "support/lstrings.h"
43 #include "support/Translator.h"
48 using namespace lyx::support;
54 typedef Translator<InsetInfo::info_type, string> NameTranslator;
56 NameTranslator const initTranslator()
58 NameTranslator translator(InsetInfo::UNKNOWN_INFO, "unknown");
60 translator.addPair(InsetInfo::SHORTCUTS_INFO, "shortcuts");
61 translator.addPair(InsetInfo::SHORTCUT_INFO, "shortcut");
62 translator.addPair(InsetInfo::LYXRC_INFO, "lyxrc");
63 translator.addPair(InsetInfo::PACKAGE_INFO, "package");
64 translator.addPair(InsetInfo::TEXTCLASS_INFO, "textclass");
65 translator.addPair(InsetInfo::MENU_INFO, "menu");
66 translator.addPair(InsetInfo::ICON_INFO, "icon");
67 translator.addPair(InsetInfo::BUFFER_INFO, "buffer");
68 translator.addPair(InsetInfo::LYX_INFO, "lyxinfo");
73 /// The translator between the information type enum and corresponding string.
74 NameTranslator const & nameTranslator()
76 static NameTranslator const translator = initTranslator();
82 /////////////////////////////////////////////////////////////////////////
86 /////////////////////////////////////////////////////////////////////////
90 InsetInfo::InsetInfo(Buffer * buf, string const & name)
91 : InsetCollapsable(buf), type_(UNKNOWN_INFO), name_()
93 setAutoBreakRows(true);
99 Inset * InsetInfo::editXY(Cursor & cur, int x, int y)
101 // do not allow the cursor to be set in this Inset
102 return Inset::editXY(cur, x, y);
106 string InsetInfo::infoType() const
108 return nameTranslator().find(type_);
112 docstring InsetInfo::layoutName() const
114 return from_ascii("Info:" + infoType());
118 docstring InsetInfo::toolTip(BufferView const &, int, int) const
120 return bformat(_("Information regarding %1$s '%2$s'"),
121 _(infoType()), from_utf8(name_));
125 void InsetInfo::read(Lexer & lex)
130 token = lex.getString();
131 if (token == "type") {
133 token = lex.getString();
134 type_ = nameTranslator().find(token);
135 } else if (token == "arg") {
137 name_ = lex.getString();
138 } else if (token == "\\end_inset")
141 if (token != "\\end_inset") {
142 lex.printError("Missing \\end_inset at this point");
143 throw ExceptionMessage(WarningException,
144 _("Missing \\end_inset at this point."),
151 void InsetInfo::write(ostream & os) const
153 os << "Info\ntype \"" << infoType()
154 << "\"\narg " << Lexer::quoteString(name_);
158 bool InsetInfo::validateModifyArgument(docstring const & arg) const
161 string const name = trim(split(to_utf8(arg), type, ' '));
163 switch (nameTranslator().find(type)) {
171 FuncRequest func = lyxaction.lookupFunc(name);
172 return func.action() != LFUN_UNKNOWN_ACTION;
177 lyxrc.write(oss, true, name);
178 return !oss.str().empty();
186 if (name == "name" || name == "path" || name == "class")
188 if (name == "vcs-revision" || name == "vcs-tree-revision" ||
189 name == "vcs-author" || name == "vcs-date" || name == "vcs-time")
190 return buffer().lyxvc().inUse();
194 return name == "version";
201 bool InsetInfo::showInsetDialog(BufferView * bv) const
203 bv->showDialog("info");
208 bool InsetInfo::getStatus(Cursor & cur, FuncRequest const & cmd,
209 FuncStatus & flag) const
211 switch (cmd.action()) {
212 case LFUN_INSET_SETTINGS:
213 return InsetCollapsable::getStatus(cur, cmd, flag);
215 case LFUN_INSET_DIALOG_UPDATE:
216 case LFUN_INSET_COPY_AS:
217 flag.setEnabled(true);
220 case LFUN_INSET_MODIFY:
221 if (validateModifyArgument(cmd.argument())) {
222 flag.setEnabled(true);
233 void InsetInfo::doDispatch(Cursor & cur, FuncRequest & cmd)
235 switch (cmd.action()) {
236 case LFUN_INSET_MODIFY:
238 setInfo(to_utf8(cmd.argument()));
241 case LFUN_INSET_COPY_AS: {
242 cap::clearSelection();
244 copy.pushBackward(*this);
248 copy.pit() = copy.lastpit();
249 copy.pos() = copy.lastpos();
251 cap::copySelection(copy);
256 InsetCollapsable::doDispatch(cur, cmd);
262 void InsetInfo::setInfo(string const & name)
268 name_ = trim(split(name, type, ' '));
269 type_ = nameTranslator().find(type);
274 void InsetInfo::error(string const & err)
276 setText(bformat(_(err), from_utf8(name_)),
277 Font(inherit_font, buffer().params().language), false);
281 void InsetInfo::setText(docstring const & str)
283 setText(str, Font(inherit_font, buffer().params().language), false);
287 void InsetInfo::updateInfo()
289 BufferParams const & bp = buffer().params();
293 error("Unknown Info: %1$s");
296 case SHORTCUTS_INFO: {
297 FuncRequest const func = lyxaction.lookupFunc(name_);
298 if (func.action() == LFUN_UNKNOWN_ACTION) {
299 error("Unknown action %1$s");
302 KeyMap::Bindings bindings = theTopLevelKeymap().findBindings(func);
303 if (bindings.empty()) {
304 // It is impropriate to use error() for undefined shortcut
305 setText(_("undefined"));
308 if (type_ == SHORTCUT_INFO)
309 setText(bindings.begin()->print(KeySequence::Portable));
311 setText(theTopLevelKeymap().printBindings(func, KeySequence::Portable));
317 setText(_("undefined"));
320 lyxrc.write(oss, true, name_);
321 string result = oss.str();
322 if (result.size() < 2) {
323 setText(_("undefined"));
326 string::size_type loc = result.rfind("\n", result.size() - 2);
327 loc = loc == string::npos ? 0 : loc + 1;
328 if (result.size() < loc + name_.size() + 1
329 || result.substr(loc + 1, name_.size()) != name_) {
330 setText(_("undefined"));
333 // remove leading comments and \\name and space
334 result = result.substr(loc + name_.size() + 2);
337 result = rtrim(result, "\n");
338 result = trim(result, "\"");
339 setText(from_utf8(result));
343 // check in packages.lst
344 setText(LaTeXFeatures::isAvailable(name_) ? _("yes") : _("no"));
347 case TEXTCLASS_INFO: {
348 // name_ is the class name
349 LayoutFileList const & list = LayoutFileList::get();
350 bool available = false;
351 if (list.haveClass(name_))
352 available = list[name_].isTeXClassAvailable();
353 setText(available ? _("yes") : _("no"));
357 docstring_list names;
358 FuncRequest const func = lyxaction.lookupFunc(name_);
359 if (func.action() == LFUN_UNKNOWN_ACTION) {
360 error("Unknown action %1$s");
363 // iterate through the menubackend to find it
365 error("Can't determine menu entry for action %1$s in batch mode");
368 if (!theApp()->searchMenu(func, names)) {
369 error("No menu entry for action %1$s");
372 // if found, return its path.
374 Paragraph & par = paragraphs().front();
375 Font const f(inherit_font, buffer().params().language);
377 //fu.fontInfo().setUnderbar(FONT_ON);
378 docstring_list::const_iterator beg = names.begin();
379 docstring_list::const_iterator end = names.end();
380 for (docstring_list::const_iterator it = beg ;
382 // do not insert > for the top level menu item
384 par.insertInset(par.size(), new InsetSpecialChar(InsetSpecialChar::MENU_SEPARATOR),
385 f, Change(Change::UNCHANGED));
386 //FIXME: add proper underlines here. This
387 // involves rewriting searchMenu used above to
388 // return a vector of menus. If we do not do
389 // that, we might as well use below
390 // Paragraph::insert on each string (JMarc)
391 for (size_type i = 0; i != it->length(); ++i)
392 par.insertChar(par.size(), (*it)[i],
393 f, Change(Change::UNCHANGED));
398 FuncRequest func = lyxaction.lookupFunc(name_);
399 docstring icon_name = frontend::Application::iconName(func, true);
400 // FIXME: We should use the icon directly instead of
401 // going through FileName. The code below won't work
402 // if the icon is embedded in the executable through
403 // the Qt resource system.
404 // This is only a negligible performance problem:
405 // If the installed icon differs from the resource icon the
406 // installed one is preferred anyway, and all icons that are
407 // embedded in the resources are installed as well.
408 FileName file(to_utf8(icon_name));
411 InsetGraphics * inset = new InsetGraphics(buffer_);
412 InsetGraphicsParams igp;
414 igp.lyxscale = iconScaleFactor(file);
415 igp.scale = convert<string>(igp.lyxscale);
416 inset->setParams(igp);
418 Font const f(inherit_font, buffer().params().language);
419 paragraphs().front().insertInset(0, inset, f,
420 Change(Change::UNCHANGED));
424 if (name_ == "name") {
425 setText(from_utf8(buffer().fileName().onlyFileName()));
428 if (name_ == "path") {
429 setText(from_utf8(os::latex_path(buffer().filePath())));
432 if (name_ == "class") {
433 setText(from_utf8(bp.documentClass().name()));
437 // everything that follows is for version control.
438 // nothing that isn't version control should go below this line.
439 if (!buffer().lyxvc().inUse()) {
440 setText(_("No version control"));
443 LyXVC::RevisionInfo itype = LyXVC::Unknown;
444 if (name_ == "vcs-revision")
446 else if (name_ == "vcs-tree-revision")
448 else if (name_ == "vcs-author")
449 itype = LyXVC::Author;
450 else if (name_ == "vcs-time")
452 else if (name_ == "vcs-date")
454 string binfo = buffer().lyxvc().revisionInfo(itype);
456 setText(from_ascii(name_) + " unknown");
458 setText(from_utf8(binfo));
462 if (name_ == "version")
463 setText(from_ascii(lyx_version));
469 string InsetInfo::contextMenu(BufferView const &, int, int) const
471 //FIXME: We override the implementation of InsetCollapsable,
472 //because this inset is not a collapsable inset.
473 return contextMenuName();
477 string InsetInfo::contextMenuName() const
479 return "context-info";