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"
31 #include "frontends/Application.h"
33 #include "support/debug.h"
34 #include "support/docstream.h"
35 #include "support/docstring_list.h"
36 #include "support/ExceptionMessage.h"
37 #include "support/FileName.h"
38 #include "support/filetools.h"
39 #include "support/gettext.h"
40 #include "support/lstrings.h"
45 using namespace lyx::support;
51 typedef Translator<InsetInfo::info_type, string> NameTranslator;
53 NameTranslator const initTranslator()
55 NameTranslator translator(InsetInfo::UNKNOWN_INFO, "unknown");
57 translator.addPair(InsetInfo::SHORTCUTS_INFO, "shortcuts");
58 translator.addPair(InsetInfo::SHORTCUT_INFO, "shortcut");
59 translator.addPair(InsetInfo::LYXRC_INFO, "lyxrc");
60 translator.addPair(InsetInfo::PACKAGE_INFO, "package");
61 translator.addPair(InsetInfo::TEXTCLASS_INFO, "textclass");
62 translator.addPair(InsetInfo::MENU_INFO, "menu");
63 translator.addPair(InsetInfo::ICON_INFO, "icon");
64 translator.addPair(InsetInfo::BUFFER_INFO, "buffer");
65 translator.addPair(InsetInfo::LYX_INFO, "lyxinfo");
70 /// The translator between the information type enum and corresponding string.
71 NameTranslator const & nameTranslator()
73 static NameTranslator const translator = initTranslator();
79 /////////////////////////////////////////////////////////////////////////
83 /////////////////////////////////////////////////////////////////////////
87 InsetInfo::InsetInfo(Buffer * buf, string const & name)
88 : InsetCollapsable(buf), type_(UNKNOWN_INFO), name_()
90 setAutoBreakRows(true);
96 Inset * InsetInfo::editXY(Cursor & cur, int x, int y)
98 // do not allow the cursor to be set in this Inset
99 return Inset::editXY(cur, x, y);
103 string InsetInfo::infoType() const
105 return nameTranslator().find(type_);
109 docstring InsetInfo::name() const
111 return from_ascii("Info:" + infoType());
115 docstring InsetInfo::toolTip(BufferView const &, int, int) const
117 return bformat(_("Information regarding %1$s '%2$s'"),
118 _(infoType()), from_utf8(name_));
122 void InsetInfo::read(Lexer & lex)
127 token = lex.getString();
128 if (token == "type") {
130 token = lex.getString();
131 type_ = nameTranslator().find(token);
132 } else if (token == "arg") {
134 name_ = lex.getString();
135 } else if (token == "\\end_inset")
138 if (token != "\\end_inset") {
139 lex.printError("Missing \\end_inset at this point");
140 throw ExceptionMessage(WarningException,
141 _("Missing \\end_inset at this point."),
148 void InsetInfo::write(ostream & os) const
150 os << "Info\ntype \"" << infoType()
151 << "\"\narg " << Lexer::quoteString(name_);
155 bool InsetInfo::validateModifyArgument(docstring const & arg) const
158 string const name = trim(split(to_utf8(arg), type, ' '));
160 switch (nameTranslator().find(type)) {
168 FuncRequest func = lyxaction.lookupFunc(name);
169 return func.action() != LFUN_UNKNOWN_ACTION;
174 lyxrc.write(oss, true, name);
175 return !oss.str().empty();
183 if (name == "name" || name == "path" || name == "class")
185 if (name == "vcs-revision" || name == "vcs-tree-revision" ||
186 name == "vcs-author" || name == "vcs-date" || name == "vcs-time")
187 return buffer().lyxvc().inUse();
191 return name == "version";
198 bool InsetInfo::showInsetDialog(BufferView * bv) const
200 bv->showDialog("info");
205 bool InsetInfo::getStatus(Cursor & cur, FuncRequest const & cmd,
206 FuncStatus & flag) const
208 switch (cmd.action()) {
209 case LFUN_INSET_SETTINGS:
210 return InsetCollapsable::getStatus(cur, cmd, flag);
212 case LFUN_INSET_DIALOG_UPDATE:
213 case LFUN_INSET_COPY_AS:
214 flag.setEnabled(true);
217 case LFUN_INSET_MODIFY:
218 if (validateModifyArgument(cmd.argument())) {
219 flag.setEnabled(true);
230 void InsetInfo::doDispatch(Cursor & cur, FuncRequest & cmd)
232 switch (cmd.action()) {
233 case LFUN_INSET_MODIFY:
234 setInfo(to_utf8(cmd.argument()));
237 case LFUN_INSET_COPY_AS: {
238 cap::clearSelection();
240 copy.pushBackward(*this);
244 copy.pit() = copy.lastpit();
245 copy.pos() = copy.lastpos();
247 cap::copySelection(copy);
252 InsetCollapsable::doDispatch(cur, cmd);
258 void InsetInfo::setInfo(string const & name)
264 name_ = trim(split(name, type, ' '));
265 type_ = nameTranslator().find(type);
270 void InsetInfo::error(string const & err)
272 setText(bformat(_(err), from_utf8(name_)),
273 Font(inherit_font, buffer().params().language), false);
277 void InsetInfo::setText(docstring const & str)
279 setText(str, Font(inherit_font, buffer().params().language), false);
283 void InsetInfo::updateInfo()
285 BufferParams const & bp = buffer().params();
289 error("Unknown Info: %1$s");
292 case SHORTCUTS_INFO: {
293 FuncRequest const func = lyxaction.lookupFunc(name_);
294 if (func.action() == LFUN_UNKNOWN_ACTION) {
295 error("Unknown action %1$s");
298 KeyMap::Bindings bindings = theTopLevelKeymap().findBindings(func);
299 if (bindings.empty()) {
300 // It is impropriate to use error() for undefined shortcut
301 setText(_("undefined"));
304 if (type_ == SHORTCUT_INFO)
305 setText(bindings.begin()->print(KeySequence::Portable));
307 setText(theTopLevelKeymap().printBindings(func, KeySequence::Portable));
313 setText(_("undefined"));
316 lyxrc.write(oss, true, name_);
317 string result = oss.str();
318 if (result.size() < 2) {
319 setText(_("undefined"));
322 string::size_type loc = result.rfind("\n", result.size() - 2);
323 loc = loc == string::npos ? 0 : loc + 1;
324 if (result.size() < loc + name_.size() + 1
325 || result.substr(loc + 1, name_.size()) != name_) {
326 setText(_("undefined"));
329 // remove leading comments and \\name and space
330 result = result.substr(loc + name_.size() + 2);
333 result = rtrim(result, "\n");
334 result = trim(result, "\"");
335 setText(from_utf8(result));
339 // check in packages.lst
340 setText(LaTeXFeatures::isAvailable(name_) ? _("yes") : _("no"));
343 case TEXTCLASS_INFO: {
344 // name_ is the class name
345 LayoutFileList const & list = LayoutFileList::get();
346 bool available = false;
347 if (list.haveClass(name_))
348 available = list[name_].isTeXClassAvailable();
349 setText(available ? _("yes") : _("no"));
353 docstring_list names;
354 FuncRequest const func = lyxaction.lookupFunc(name_);
355 if (func.action() == LFUN_UNKNOWN_ACTION) {
356 error("Unknown action %1$s");
359 // iterate through the menubackend to find it
360 if (!theApp()->searchMenu(func, names)) {
361 error("No menu entry for action %1$s");
364 // if found, return its path.
366 Paragraph & par = paragraphs().front();
367 Font const f(inherit_font, buffer().params().language);
369 //fu.fontInfo().setUnderbar(FONT_ON);
370 docstring_list::const_iterator beg = names.begin();
371 docstring_list::const_iterator end = names.end();
372 for (docstring_list::const_iterator it = beg ;
374 // do not insert > for the top level menu item
376 par.insertInset(par.size(), new InsetSpecialChar(InsetSpecialChar::MENU_SEPARATOR),
377 Change(Change::UNCHANGED));
378 //FIXME: add proper underlines here. This
379 // involves rewriting searchMenu used above to
380 // return a vector of menus. If we do not do
381 // that, we might as well use below
382 // Paragraph::insert on each string (JMarc)
383 for (size_type i = 0; i != it->length(); ++i)
384 par.insertChar(par.size(), (*it)[i],
385 f, Change(Change::UNCHANGED));
390 FuncRequest func = lyxaction.lookupFunc(name_);
391 docstring icon_name = theApp()->iconName(func, true);
392 //FIXME: We should use the icon directly instead of
393 // going through FileName. The code below won't work
394 // if the icon is embedded in the executable through
395 // the Qt resource system.
396 FileName file(to_utf8(icon_name));
399 InsetGraphics * inset = new InsetGraphics(buffer_);
400 InsetGraphicsParams igp;
402 inset->setParams(igp);
404 paragraphs().front().insertInset(0, inset,
405 Change(Change::UNCHANGED));
409 if (name_ == "name") {
410 setText(from_utf8(buffer().fileName().onlyFileName()));
413 if (name_ == "path") {
414 setText(from_utf8(buffer().filePath()));
417 if (name_ == "class") {
418 setText(from_utf8(bp.documentClass().name()));
422 // everything that follows is for version control.
423 // nothing that isn't version control should go below this line.
424 if (!buffer().lyxvc().inUse()) {
425 setText(_("No version control"));
428 LyXVC::RevisionInfo itype = LyXVC::Unknown;
429 if (name_ == "vcs-revision")
431 else if (name_ == "vcs-tree-revision")
433 else if (name_ == "vcs-author")
434 itype = LyXVC::Author;
435 else if (name_ == "vcs-time")
437 else if (name_ == "vcs-date")
439 string binfo = buffer().lyxvc().revisionInfo(itype);
441 setText(bformat(_("[[%1$s unknown]]"), from_utf8(name_)));
443 setText(from_utf8(binfo));
447 if (name_ == "version")
448 setText(from_ascii(PACKAGE_VERSION));
454 docstring InsetInfo::contextMenu(BufferView const &, int, int) const
456 return from_ascii("context-info");