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/debug.h"
35 #include "support/docstream.h"
36 #include "support/docstring_list.h"
37 #include "support/ExceptionMessage.h"
38 #include "support/FileName.h"
39 #include "support/filetools.h"
40 #include "support/gettext.h"
41 #include "support/lstrings.h"
46 using namespace lyx::support;
52 typedef Translator<InsetInfo::info_type, string> NameTranslator;
54 NameTranslator const initTranslator()
56 NameTranslator translator(InsetInfo::UNKNOWN_INFO, "unknown");
58 translator.addPair(InsetInfo::SHORTCUTS_INFO, "shortcuts");
59 translator.addPair(InsetInfo::SHORTCUT_INFO, "shortcut");
60 translator.addPair(InsetInfo::LYXRC_INFO, "lyxrc");
61 translator.addPair(InsetInfo::PACKAGE_INFO, "package");
62 translator.addPair(InsetInfo::TEXTCLASS_INFO, "textclass");
63 translator.addPair(InsetInfo::MENU_INFO, "menu");
64 translator.addPair(InsetInfo::ICON_INFO, "icon");
65 translator.addPair(InsetInfo::BUFFER_INFO, "buffer");
66 translator.addPair(InsetInfo::LYX_INFO, "lyxinfo");
71 /// The translator between the information type enum and corresponding string.
72 NameTranslator const & nameTranslator()
74 static NameTranslator const translator = initTranslator();
80 /////////////////////////////////////////////////////////////////////////
84 /////////////////////////////////////////////////////////////////////////
88 InsetInfo::InsetInfo(Buffer * buf, string const & name)
89 : InsetCollapsable(buf), type_(UNKNOWN_INFO), name_()
91 setAutoBreakRows(true);
97 Inset * InsetInfo::editXY(Cursor & cur, int x, int y)
99 // do not allow the cursor to be set in this Inset
100 return Inset::editXY(cur, x, y);
104 string InsetInfo::infoType() const
106 return nameTranslator().find(type_);
110 docstring InsetInfo::name() const
112 return from_ascii("Info:" + infoType());
116 docstring InsetInfo::toolTip(BufferView const &, int, int) const
118 return bformat(_("Information regarding %1$s '%2$s'"),
119 _(infoType()), from_utf8(name_));
123 void InsetInfo::read(Lexer & lex)
128 token = lex.getString();
129 if (token == "type") {
131 token = lex.getString();
132 type_ = nameTranslator().find(token);
133 } else if (token == "arg") {
135 name_ = lex.getString();
136 } else if (token == "\\end_inset")
139 if (token != "\\end_inset") {
140 lex.printError("Missing \\end_inset at this point");
141 throw ExceptionMessage(WarningException,
142 _("Missing \\end_inset at this point."),
149 void InsetInfo::write(ostream & os) const
151 os << "Info\ntype \"" << infoType()
152 << "\"\narg " << Lexer::quoteString(name_);
156 bool InsetInfo::validateModifyArgument(docstring const & arg) const
159 string const name = trim(split(to_utf8(arg), type, ' '));
161 switch (nameTranslator().find(type)) {
169 FuncRequest func = lyxaction.lookupFunc(name);
170 return func.action() != LFUN_UNKNOWN_ACTION;
175 lyxrc.write(oss, true, name);
176 return !oss.str().empty();
184 if (name == "name" || name == "path" || name == "class")
186 if (name == "vcs-revision" || name == "vcs-tree-revision" ||
187 name == "vcs-author" || name == "vcs-date" || name == "vcs-time")
188 return buffer().lyxvc().inUse();
192 return name == "version";
199 bool InsetInfo::showInsetDialog(BufferView * bv) const
201 bv->showDialog("info");
206 bool InsetInfo::getStatus(Cursor & cur, FuncRequest const & cmd,
207 FuncStatus & flag) const
209 switch (cmd.action()) {
210 case LFUN_INSET_SETTINGS:
211 return InsetCollapsable::getStatus(cur, cmd, flag);
213 case LFUN_INSET_DIALOG_UPDATE:
214 case LFUN_INSET_COPY_AS:
215 flag.setEnabled(true);
218 case LFUN_INSET_MODIFY:
219 if (validateModifyArgument(cmd.argument())) {
220 flag.setEnabled(true);
231 void InsetInfo::doDispatch(Cursor & cur, FuncRequest & cmd)
233 switch (cmd.action()) {
234 case LFUN_INSET_MODIFY:
236 setInfo(to_utf8(cmd.argument()));
239 case LFUN_INSET_COPY_AS: {
240 cap::clearSelection();
242 copy.pushBackward(*this);
246 copy.pit() = copy.lastpit();
247 copy.pos() = copy.lastpos();
249 cap::copySelection(copy);
254 InsetCollapsable::doDispatch(cur, cmd);
260 void InsetInfo::setInfo(string const & name)
266 name_ = trim(split(name, type, ' '));
267 type_ = nameTranslator().find(type);
272 void InsetInfo::error(string const & err)
274 setText(bformat(_(err), from_utf8(name_)),
275 Font(inherit_font, buffer().params().language), false);
279 void InsetInfo::setText(docstring const & str)
281 setText(str, Font(inherit_font, buffer().params().language), false);
285 void InsetInfo::updateInfo()
287 BufferParams const & bp = buffer().params();
291 error("Unknown Info: %1$s");
294 case SHORTCUTS_INFO: {
295 FuncRequest const func = lyxaction.lookupFunc(name_);
296 if (func.action() == LFUN_UNKNOWN_ACTION) {
297 error("Unknown action %1$s");
300 KeyMap::Bindings bindings = theTopLevelKeymap().findBindings(func);
301 if (bindings.empty()) {
302 // It is impropriate to use error() for undefined shortcut
303 setText(_("undefined"));
306 if (type_ == SHORTCUT_INFO)
307 setText(bindings.begin()->print(KeySequence::Portable));
309 setText(theTopLevelKeymap().printBindings(func, KeySequence::Portable));
315 setText(_("undefined"));
318 lyxrc.write(oss, true, name_);
319 string result = oss.str();
320 if (result.size() < 2) {
321 setText(_("undefined"));
324 string::size_type loc = result.rfind("\n", result.size() - 2);
325 loc = loc == string::npos ? 0 : loc + 1;
326 if (result.size() < loc + name_.size() + 1
327 || result.substr(loc + 1, name_.size()) != name_) {
328 setText(_("undefined"));
331 // remove leading comments and \\name and space
332 result = result.substr(loc + name_.size() + 2);
335 result = rtrim(result, "\n");
336 result = trim(result, "\"");
337 setText(from_utf8(result));
341 // check in packages.lst
342 setText(LaTeXFeatures::isAvailable(name_) ? _("yes") : _("no"));
345 case TEXTCLASS_INFO: {
346 // name_ is the class name
347 LayoutFileList const & list = LayoutFileList::get();
348 bool available = false;
349 if (list.haveClass(name_))
350 available = list[name_].isTeXClassAvailable();
351 setText(available ? _("yes") : _("no"));
355 docstring_list names;
356 FuncRequest const func = lyxaction.lookupFunc(name_);
357 if (func.action() == LFUN_UNKNOWN_ACTION) {
358 error("Unknown action %1$s");
361 // iterate through the menubackend to find it
363 error("Can't determine menu entry for action %1$s in batch mode");
366 if (!theApp()->searchMenu(func, names)) {
367 error("No menu entry for action %1$s");
370 // if found, return its path.
372 Paragraph & par = paragraphs().front();
373 Font const f(inherit_font, buffer().params().language);
375 //fu.fontInfo().setUnderbar(FONT_ON);
376 docstring_list::const_iterator beg = names.begin();
377 docstring_list::const_iterator end = names.end();
378 for (docstring_list::const_iterator it = beg ;
380 // do not insert > for the top level menu item
382 par.insertInset(par.size(), new InsetSpecialChar(InsetSpecialChar::MENU_SEPARATOR),
383 Change(Change::UNCHANGED));
384 //FIXME: add proper underlines here. This
385 // involves rewriting searchMenu used above to
386 // return a vector of menus. If we do not do
387 // that, we might as well use below
388 // Paragraph::insert on each string (JMarc)
389 for (size_type i = 0; i != it->length(); ++i)
390 par.insertChar(par.size(), (*it)[i],
391 f, Change(Change::UNCHANGED));
396 FuncRequest func = lyxaction.lookupFunc(name_);
397 docstring icon_name = frontend::Application::iconName(func, true);
398 //FIXME: We should use the icon directly instead of
399 // going through FileName. The code below won't work
400 // if the icon is embedded in the executable through
401 // the Qt resource system.
402 // This is only a negligible performance problem:
403 // If the installed icon differs from the resource icon the
404 // installed one is preferred anyway, and all icons that are
405 // embedded in the resources are installed as well.
406 FileName file(to_utf8(icon_name));
409 InsetGraphics * inset = new InsetGraphics(buffer_);
410 InsetGraphicsParams igp;
412 inset->setParams(igp);
414 paragraphs().front().insertInset(0, inset,
415 Change(Change::UNCHANGED));
419 if (name_ == "name") {
420 setText(from_utf8(buffer().fileName().onlyFileName()));
423 if (name_ == "path") {
424 setText(from_utf8(buffer().filePath()));
427 if (name_ == "class") {
428 setText(from_utf8(bp.documentClass().name()));
432 // everything that follows is for version control.
433 // nothing that isn't version control should go below this line.
434 if (!buffer().lyxvc().inUse()) {
435 setText(_("No version control"));
438 LyXVC::RevisionInfo itype = LyXVC::Unknown;
439 if (name_ == "vcs-revision")
441 else if (name_ == "vcs-tree-revision")
443 else if (name_ == "vcs-author")
444 itype = LyXVC::Author;
445 else if (name_ == "vcs-time")
447 else if (name_ == "vcs-date")
449 string binfo = buffer().lyxvc().revisionInfo(itype);
451 setText(bformat(_("%1$s unknown"), from_ascii(name_)));
453 setText(from_utf8(binfo));
457 if (name_ == "version")
458 setText(from_ascii(lyx_version));
464 docstring InsetInfo::contextMenu(BufferView const &, int, int) const
466 //FIXME: We override the implementation of InsetCollapsable,
467 //because this inset is not a collapsable inset.
468 return contextMenuName();
472 docstring InsetInfo::contextMenuName() const
474 return from_ascii("context-info");