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"
42 #include "support/Translator.h"
47 using namespace lyx::support;
53 typedef Translator<InsetInfo::info_type, string> NameTranslator;
55 NameTranslator const initTranslator()
57 NameTranslator translator(InsetInfo::UNKNOWN_INFO, "unknown");
59 translator.addPair(InsetInfo::SHORTCUTS_INFO, "shortcuts");
60 translator.addPair(InsetInfo::SHORTCUT_INFO, "shortcut");
61 translator.addPair(InsetInfo::LYXRC_INFO, "lyxrc");
62 translator.addPair(InsetInfo::PACKAGE_INFO, "package");
63 translator.addPair(InsetInfo::TEXTCLASS_INFO, "textclass");
64 translator.addPair(InsetInfo::MENU_INFO, "menu");
65 translator.addPair(InsetInfo::ICON_INFO, "icon");
66 translator.addPair(InsetInfo::BUFFER_INFO, "buffer");
67 translator.addPair(InsetInfo::LYX_INFO, "lyxinfo");
72 /// The translator between the information type enum and corresponding string.
73 NameTranslator const & nameTranslator()
75 static NameTranslator const translator = initTranslator();
81 /////////////////////////////////////////////////////////////////////////
85 /////////////////////////////////////////////////////////////////////////
89 InsetInfo::InsetInfo(Buffer * buf, string const & name)
90 : InsetCollapsable(buf), type_(UNKNOWN_INFO), name_()
92 setAutoBreakRows(true);
98 Inset * InsetInfo::editXY(Cursor & cur, int x, int y)
100 // do not allow the cursor to be set in this Inset
101 return Inset::editXY(cur, x, y);
105 string InsetInfo::infoType() const
107 return nameTranslator().find(type_);
111 docstring InsetInfo::layoutName() const
113 return from_ascii("Info:" + infoType());
117 docstring InsetInfo::toolTip(BufferView const &, int, int) const
119 return bformat(_("Information regarding %1$s '%2$s'"),
120 _(infoType()), from_utf8(name_));
124 void InsetInfo::read(Lexer & lex)
129 token = lex.getString();
130 if (token == "type") {
132 token = lex.getString();
133 type_ = nameTranslator().find(token);
134 } else if (token == "arg") {
136 name_ = lex.getString();
137 } else if (token == "\\end_inset")
140 if (token != "\\end_inset") {
141 lex.printError("Missing \\end_inset at this point");
142 throw ExceptionMessage(WarningException,
143 _("Missing \\end_inset at this point."),
150 void InsetInfo::write(ostream & os) const
152 os << "Info\ntype \"" << infoType()
153 << "\"\narg " << Lexer::quoteString(name_);
157 bool InsetInfo::validateModifyArgument(docstring const & arg) const
160 string const name = trim(split(to_utf8(arg), type, ' '));
162 switch (nameTranslator().find(type)) {
170 FuncRequest func = lyxaction.lookupFunc(name);
171 return func.action() != LFUN_UNKNOWN_ACTION;
176 lyxrc.write(oss, true, name);
177 return !oss.str().empty();
185 if (name == "name" || name == "path" || name == "class")
187 if (name == "vcs-revision" || name == "vcs-tree-revision" ||
188 name == "vcs-author" || name == "vcs-date" || name == "vcs-time")
189 return buffer().lyxvc().inUse();
193 return name == "version";
200 bool InsetInfo::showInsetDialog(BufferView * bv) const
202 bv->showDialog("info");
207 bool InsetInfo::getStatus(Cursor & cur, FuncRequest const & cmd,
208 FuncStatus & flag) const
210 switch (cmd.action()) {
211 case LFUN_INSET_SETTINGS:
212 return InsetCollapsable::getStatus(cur, cmd, flag);
214 case LFUN_INSET_DIALOG_UPDATE:
215 case LFUN_INSET_COPY_AS:
216 flag.setEnabled(true);
219 case LFUN_INSET_MODIFY:
220 if (validateModifyArgument(cmd.argument())) {
221 flag.setEnabled(true);
232 void InsetInfo::doDispatch(Cursor & cur, FuncRequest & cmd)
234 switch (cmd.action()) {
235 case LFUN_INSET_MODIFY:
237 setInfo(to_utf8(cmd.argument()));
240 case LFUN_INSET_COPY_AS: {
241 cap::clearSelection();
243 copy.pushBackward(*this);
247 copy.pit() = copy.lastpit();
248 copy.pos() = copy.lastpos();
250 cap::copySelection(copy);
255 InsetCollapsable::doDispatch(cur, cmd);
261 void InsetInfo::setInfo(string const & name)
267 name_ = trim(split(name, type, ' '));
268 type_ = nameTranslator().find(type);
273 void InsetInfo::error(string const & err)
275 setText(bformat(_(err), from_utf8(name_)),
276 Font(inherit_font, buffer().params().language), false);
280 void InsetInfo::setText(docstring const & str)
282 setText(str, Font(inherit_font, buffer().params().language), false);
286 void InsetInfo::updateInfo()
288 BufferParams const & bp = buffer().params();
292 error("Unknown Info: %1$s");
295 case SHORTCUTS_INFO: {
296 FuncRequest const func = lyxaction.lookupFunc(name_);
297 if (func.action() == LFUN_UNKNOWN_ACTION) {
298 error("Unknown action %1$s");
301 KeyMap::Bindings bindings = theTopLevelKeymap().findBindings(func);
302 if (bindings.empty()) {
303 // It is impropriate to use error() for undefined shortcut
304 setText(_("undefined"));
307 if (type_ == SHORTCUT_INFO)
308 setText(bindings.begin()->print(KeySequence::Portable));
310 setText(theTopLevelKeymap().printBindings(func, KeySequence::Portable));
316 setText(_("undefined"));
319 lyxrc.write(oss, true, name_);
320 string result = oss.str();
321 if (result.size() < 2) {
322 setText(_("undefined"));
325 string::size_type loc = result.rfind("\n", result.size() - 2);
326 loc = loc == string::npos ? 0 : loc + 1;
327 if (result.size() < loc + name_.size() + 1
328 || result.substr(loc + 1, name_.size()) != name_) {
329 setText(_("undefined"));
332 // remove leading comments and \\name and space
333 result = result.substr(loc + name_.size() + 2);
336 result = rtrim(result, "\n");
337 result = trim(result, "\"");
338 setText(from_utf8(result));
342 // check in packages.lst
343 setText(LaTeXFeatures::isAvailable(name_) ? _("yes") : _("no"));
346 case TEXTCLASS_INFO: {
347 // name_ is the class name
348 LayoutFileList const & list = LayoutFileList::get();
349 bool available = false;
350 if (list.haveClass(name_))
351 available = list[name_].isTeXClassAvailable();
352 setText(available ? _("yes") : _("no"));
356 docstring_list names;
357 FuncRequest const func = lyxaction.lookupFunc(name_);
358 if (func.action() == LFUN_UNKNOWN_ACTION) {
359 error("Unknown action %1$s");
362 // iterate through the menubackend to find it
364 error("Can't determine menu entry for action %1$s in batch mode");
367 if (!theApp()->searchMenu(func, names)) {
368 error("No menu entry for action %1$s");
371 // if found, return its path.
373 Paragraph & par = paragraphs().front();
374 Font const f(inherit_font, buffer().params().language);
376 //fu.fontInfo().setUnderbar(FONT_ON);
377 docstring_list::const_iterator beg = names.begin();
378 docstring_list::const_iterator end = names.end();
379 for (docstring_list::const_iterator it = beg ;
381 // do not insert > for the top level menu item
383 par.insertInset(par.size(), new InsetSpecialChar(InsetSpecialChar::MENU_SEPARATOR),
384 Change(Change::UNCHANGED));
385 //FIXME: add proper underlines here. This
386 // involves rewriting searchMenu used above to
387 // return a vector of menus. If we do not do
388 // that, we might as well use below
389 // Paragraph::insert on each string (JMarc)
390 for (size_type i = 0; i != it->length(); ++i)
391 par.insertChar(par.size(), (*it)[i],
392 f, Change(Change::UNCHANGED));
397 FuncRequest func = lyxaction.lookupFunc(name_);
398 docstring icon_name = frontend::Application::iconName(func, true);
399 //FIXME: We should use the icon directly instead of
400 // going through FileName. The code below won't work
401 // if the icon is embedded in the executable through
402 // the Qt resource system.
403 // This is only a negligible performance problem:
404 // If the installed icon differs from the resource icon the
405 // installed one is preferred anyway, and all icons that are
406 // embedded in the resources are installed as well.
407 FileName file(to_utf8(icon_name));
410 InsetGraphics * inset = new InsetGraphics(buffer_);
411 InsetGraphicsParams igp;
413 inset->setParams(igp);
415 paragraphs().front().insertInset(0, inset,
416 Change(Change::UNCHANGED));
420 if (name_ == "name") {
421 setText(from_utf8(buffer().fileName().onlyFileName()));
424 if (name_ == "path") {
425 setText(from_utf8(os::latex_path(buffer().filePath())));
428 if (name_ == "class") {
429 setText(from_utf8(bp.documentClass().name()));
433 // everything that follows is for version control.
434 // nothing that isn't version control should go below this line.
435 if (!buffer().lyxvc().inUse()) {
436 setText(_("No version control"));
439 LyXVC::RevisionInfo itype = LyXVC::Unknown;
440 if (name_ == "vcs-revision")
442 else if (name_ == "vcs-tree-revision")
444 else if (name_ == "vcs-author")
445 itype = LyXVC::Author;
446 else if (name_ == "vcs-time")
448 else if (name_ == "vcs-date")
450 string binfo = buffer().lyxvc().revisionInfo(itype);
452 setText(from_ascii(name_) + " unknown");
454 setText(from_utf8(binfo));
458 if (name_ == "version")
459 setText(from_ascii(lyx_version));
465 string InsetInfo::contextMenu(BufferView const &, int, int) const
467 //FIXME: We override the implementation of InsetCollapsable,
468 //because this inset is not a collapsable inset.
469 return contextMenuName();
473 string InsetInfo::contextMenuName() const
475 return "context-info";