]> git.lyx.org Git - lyx.git/blobdiff - src/MenuBackend.C
Add code to check shortcuts in menu files
[lyx.git] / src / MenuBackend.C
index a35d9f40d1067e59d7de9aea1c83256e53264077..0289bf8928732951f51c5c18dcc30cd69b18901e 100644 (file)
 #endif
 
 #include <config.h>
+#include <memory>
+#include "support/LAssert.h"
 #include "MenuBackend.h"
 #include "lyxlex.h"
 #include "LyXAction.h"
 #include "debug.h"
+#include "gettext.h"
+#include "lastfiles.h"
+#include "bufferlist.h"
+#include "converter.h"
+#include "exporter.h"
+#include "support/filetools.h"
+#include "support/lyxfunctional.h"
 
 extern LyXAction lyxaction;
+extern LastFiles * lastfiles; 
+extern BufferList bufferlist;
 
 using std::endl;
+using std::vector;
+using std::pair;
+using std::find_if;
+using std::sort;
 
 // This is the global menu definition
 MenuBackend menubackend;
 
 
-MenuItem::MenuItem(Kind kind, string const & label, string const & command) 
-       : kind_(kind), label_(label) 
+MenuItem::MenuItem(Kind kind, string const & label, 
+                  string const & command, bool optional) 
+       : kind_(kind), label_(label), optional_(optional)
 {
        switch(kind) {
        case Separator:
        case Documents:
        case Lastfiles:
+       case Toc:
+       case References:
+       case ViewFormats:
+       case UpdateFormats:
+       case ExportFormats:
                break;
        case Command:
                action_ = lyxaction.LookupFunc(command);
@@ -42,6 +63,9 @@ MenuItem::MenuItem(Kind kind, string const & label, string const & command)
                        lyxerr << "MenuItem(): LyX command `"
                               << command << "' does not exist." << endl; 
                }
+               if (optional_)
+                       lyxerr[Debug::GUI] << "Optional item " 
+                                          << command << endl; 
                break;
        case Submenu:
                submenu_ = command;
@@ -57,25 +81,37 @@ Menu & Menu::add(MenuItem const & i)
 }
 
 
-void Menu::read(LyXLex & lex)
+Menu & Menu::read(LyXLex & lex)
 {
        enum Menutags {
                md_item = 1,
                md_documents,
                md_endmenu,
+               md_exportformats,
                md_lastfiles,
-               md_submenu,
+               md_optitem,
+               md_references,
                md_separator,
+               md_submenu,
+               md_toc,
+               md_updateformats,
+               md_viewformats,
                md_last
        };
 
-       struct keyword_item menutags[md_last-1] = {
+       struct keyword_item menutags[md_last - 1] = {
                { "documents", md_documents },
                { "end", md_endmenu },
+               { "exportformats", md_exportformats },
                { "item", md_item },
                { "lastfiles", md_lastfiles },
+               { "optitem", md_optitem }, 
+               { "references", md_references },
                { "separator", md_separator },
-               { "submenu", md_submenu }
+               { "submenu", md_submenu },
+               { "toc", md_toc },
+               { "updateformats", md_updateformats },
+               { "viewformats", md_viewformats }
        };
 
        lex.pushTable(menutags, md_last - 1);
@@ -83,37 +119,69 @@ void Menu::read(LyXLex & lex)
                lex.printTable(lyxerr);
 
        bool quit = false;
+       bool optional = false;
 
        while (lex.IsOK() && !quit) {
                switch(lex.lex()) {
+               case md_optitem:
+                       optional = true;
+                       // fallback to md_item
                case md_item: {
                        lex.next();
-                       string name = lex.GetString();
+                       string name = _(lex.GetString());
                        lex.next();
-                       string command = lex.GetString();
-                       add(MenuItem(MenuItem::Command, name, command));
+                       string const command = lex.GetString();
+                       add(MenuItem(MenuItem::Command, name, 
+                                    command, optional));
+                       optional = false;
                        break;
                }
+
                case md_separator:
                        add(MenuItem(MenuItem::Separator));
                        break;
+
                case md_lastfiles:
                        add(MenuItem(MenuItem::Lastfiles));
                        break;
+
                case md_documents:
                        add(MenuItem(MenuItem::Documents));
                        break;
+
+               case md_toc:
+                       add(MenuItem(MenuItem::Toc));
+                       break;
+
+               case md_references:
+                       add(MenuItem(MenuItem::References));
+                       break;
+
+               case md_viewformats:
+                       add(MenuItem(MenuItem::ViewFormats));
+                       break;
+
+               case md_updateformats:
+                       add(MenuItem(MenuItem::UpdateFormats));
+                       break;
+
+               case md_exportformats:
+                       add(MenuItem(MenuItem::ExportFormats));
+                       break;
+
                case md_submenu: {
                        lex.next();
-                       string mlabel = lex.GetString();
+                       string mlabel = _(lex.GetString());
                        lex.next();
                        string mname = lex.GetString();
                        add(MenuItem(MenuItem::Submenu, mlabel, mname));
                        break;
                }
+
                case md_endmenu:
                        quit = true;
                        break;
+
                default:
                        lex.printError("menubar::read: "
                                       "Unknown menu tag: `$$Token'");
@@ -121,6 +189,137 @@ void Menu::read(LyXLex & lex)
                }
        }
        lex.popTable();
+       return *this;
+}
+
+struct compare_formatpair {
+       bool operator()(FormatPair const & a, FormatPair const & b) {
+               return a.format->prettyname < b.format->prettyname; 
+       }
+};
+
+void Menu::checkShortcuts() const
+{
+       // This is a quadratic algorithm, but we do not care because
+       // it is used for debugging only.
+       for (const_iterator it1 = begin(); it1 != end(); ++it1) {
+               string shortcut = it1->shortcut();
+               if (shortcut.empty())
+                       continue;
+               if (!contains(it1->label(), shortcut))
+                       lyxerr << "Menu warning: menu entry \""
+                              << it1->label()
+                              << "\" does not contain shortcut `"
+                              << shortcut << '\'' << endl;
+               for (const_iterator it2 = begin(); it2 != it1 ; ++it2) {
+                       if (!compare_no_case(it2->shortcut(), shortcut)) {
+                               lyxerr << "Menu warning: menu entries "
+                                      << '"' << it1->fulllabel()
+                                      << "\" and \"" << it2->fulllabel()
+                                      << "\" share the same shortcut."
+                                      << endl;
+                       }
+               }
+       }
+}
+
+void Menu::expand(Menu & tomenu, Buffer * buf) const
+{
+       for (const_iterator cit = begin();
+            cit != end() ; ++cit) {
+               switch ((*cit).kind()) {
+               case MenuItem::Lastfiles: {
+                       int ii = 1;
+                       for (LastFiles::const_iterator lfit = lastfiles->begin();
+                            lfit != lastfiles->end() && ii < 10;
+                            ++lfit, ++ii) {
+                               string label = tostr(ii) + ". "
+                                       + MakeDisplayPath((*lfit), 30)
+                                       + '|' + tostr(ii);
+                               int action = lyxaction.
+                                       getPseudoAction(LFUN_FILE_OPEN,
+                                                       (*lfit));
+                               tomenu.add(MenuItem(MenuItem::Command,
+                                                   label, action));
+                       }
+               }
+               break;
+               
+               case MenuItem::Documents: {
+                       vector<string> names = bufferlist.getFileNames();
+                       
+                       if (names.empty()) {
+                               tomenu.add(MenuItem(MenuItem::Command,
+                                                   _("No Documents Open!"),
+                                                   LFUN_NOACTION));
+                               break;
+                       }
+
+                       for (vector<string>::const_iterator docit = names.begin();
+                            docit != names.end() ; ++docit) {
+                               int action =
+                                       lyxaction.getPseudoAction(LFUN_SWITCHBUFFER, *docit);
+                               string label = MakeDisplayPath(*docit, 30);
+                               tomenu.add(MenuItem(MenuItem::Command,
+                                                   label, action));
+                       }
+               }
+               break;
+
+               case MenuItem::ViewFormats:
+               case MenuItem::UpdateFormats:
+               case MenuItem::ExportFormats: {
+                       vector<FormatPair> names;
+                       kb_action action;
+                       if ((*cit).kind() == MenuItem::ViewFormats) {
+                               names = Exporter::GetViewableFormats(buf);
+                               action = LFUN_PREVIEW;
+                       } else if ((*cit).kind() == MenuItem::UpdateFormats) {
+                               names = Exporter::GetViewableFormats(buf);
+                               action = LFUN_UPDATE;
+                       } else {
+                               names = Exporter::GetExportableFormats(buf);
+                               action = LFUN_EXPORT;
+                       }
+                       sort(names.begin(), names.end(), compare_formatpair());
+
+                       for (vector<FormatPair>::const_iterator fit = names.begin();
+                            fit != names.end() ; ++fit) {
+                               string fmt = (*fit).format->name;
+                               string label = (*fit).format->prettyname;
+                               bool same_before = 
+                                       fit != names.begin() &&
+                                       (*fit).format == (*(fit-1)).format;
+                               bool same_after = 
+                                       fit+1 != names.end() &&
+                                       (*fit).format == (*(fit+1)).format;
+                               if ((*fit).from &&
+                                   (same_before || same_after)) {
+                                       fmt += ":" + (*fit).from->name;
+                                       string head;
+                                       split((*fit).command, head, ' ');
+                                       label += _(" (using ") + head + ")";
+                                       if (!(*fit).format->shortcut.empty() &&
+                                           !same_before)
+                                               label += "|" + (*fit).format->shortcut;
+                               } else if (!(*fit).format->shortcut.empty())
+                                       label += "|" + (*fit).format->shortcut;
+                               int action2 = lyxaction.getPseudoAction(action, fmt);
+                               tomenu.add(MenuItem(MenuItem::Command,
+                                                   label, action2));
+                       }
+               }
+               break;
+
+                       
+               default:
+                       tomenu.add(*cit);
+               }
+       }
+
+       // Check whether the shortcuts are unique
+       if (lyxerr.debugging(Debug::GUI))
+               checkShortcuts();
 }
 
 
@@ -149,23 +348,29 @@ void MenuBackend::read(LyXLex & lex)
                lex.printTable(lyxerr);
 
        bool quit = false;
+       bool menubar = false;
 
        while (lex.IsOK() && !quit) {
                switch(lex.lex()) {
+               case md_menubar: 
+                       menubar = true;
+                       // fallback to md_menu
                case md_menu: {
                        lex.next();
                        string name = lex.GetString();
-                       Menu menu(name, false);
-                       menu.read(lex);
-                       add(menu);
-                       break;
-               }
-               case md_menubar: {
-                       lex.next();
-                       string name = lex.GetString();
-                       Menu menubar(name, true);
-                       menubar.read(lex);
-                       add(menubar);
+                       if (hasMenu(name)) {
+                               if (getMenu(name).menubar() == menubar) {
+                                       getMenu(name).read(lex);
+                               } else {
+                                       lex.printError("Cannot append to menu `$$Token' unless it is of the same type");
+                                       return;
+                               }
+                       } else {                                
+                               Menu menu(name, menubar);
+                               menu.read(lex);
+                               add(menu);
+                       }
+                       menubar = false;
                        break;
                }
                case md_endmenuset:
@@ -190,10 +395,10 @@ void MenuBackend::defaults()
 
        Menu file("file");
        file
-               .add(MenuItem(MenuItem::Command, "New...|N", "buffer-new"))
-               .add(MenuItem(MenuItem::Command, "Open...|O", "buffer-open"))
-               .add(MenuItem(MenuItem::Submenu, "Import|I", "import"))
-               .add(MenuItem(MenuItem::Command, "Quit|Q", "lyx-quit"))
+               .add(MenuItem(MenuItem::Command, _("New...|N"), "buffer-new"))
+               .add(MenuItem(MenuItem::Command, _("Open...|O"), "buffer-open"))
+               .add(MenuItem(MenuItem::Submenu, _("Import|I"), "import"))
+               .add(MenuItem(MenuItem::Command, _("Quit|Q"), "lyx-quit"))
                .add(MenuItem(MenuItem::Separator))
                .add(MenuItem(MenuItem::Lastfiles));
        add(file);
@@ -201,17 +406,17 @@ void MenuBackend::defaults()
        Menu import("import");
        import
                .add(MenuItem(MenuItem::Command,
-                             "LaTeX...|L", "buffer-import latex"))
+                             _("LaTeX...|L"), "buffer-import latex"))
                .add(MenuItem(MenuItem::Command,
-                             "LinuxDoc...|L", "buffer-import linuxdoc"));
+                             _("LinuxDoc...|L"), "buffer-import linuxdoc"));
        add(import);
  
        Menu edit("edit");
        edit
-               .add(MenuItem(MenuItem::Command, "Cut", "cut"))
-               .add(MenuItem(MenuItem::Command, "Copy", "copy"))
-               .add(MenuItem(MenuItem::Command, "Paste", "paste"))
-               .add(MenuItem(MenuItem::Command, "Emphasize", "font-emph"));
+               .add(MenuItem(MenuItem::Command, _("Cut"), "cut"))
+               .add(MenuItem(MenuItem::Command, _("Copy"), "copy"))
+               .add(MenuItem(MenuItem::Command, _("Paste"), "paste"))
+               .add(MenuItem(MenuItem::Command, _("Emphasize"), "font-emph"));
        add(edit);
 
        Menu documents("documents");
@@ -220,14 +425,14 @@ void MenuBackend::defaults()
 
        Menu main("main", true);
        main
-               .add(MenuItem(MenuItem::Submenu, "File|F", "file"))
-               .add(MenuItem(MenuItem::Submenu, "Edit|E", "edit"))
-               .add(MenuItem(MenuItem::Submenu, 
-                             "Documents|D", "documents"));
+               .add(MenuItem(MenuItem::Submenu, _("File|F"), "file"))
+               .add(MenuItem(MenuItem::Submenu, _("Edit|E"), "edit"))
+               .add(MenuItem(MenuItem::Submenu,
+                             _("Documents|D"), "documents"));
        add(main);
 
        Menu main_nobuffer("main_nobuffer", true);
-       main_nobuffer.add(MenuItem(MenuItem::Submenu, "File|F", "file"));
+       main_nobuffer.add(MenuItem(MenuItem::Submenu, _("File|F"), "file"));
        add(main_nobuffer);
 
        if (lyxerr.debugging(Debug::GUI)) {
@@ -248,20 +453,24 @@ void MenuBackend::add(Menu const & menu)
 
 bool MenuBackend::hasMenu(string const & name) const
 {
-       for (const_iterator cit = begin(); cit != end(); ++cit) {
-               if ((*cit).name() == name)
-                       return true;
-       }
-       return false;
+       return find_if(begin(), end(),
+                      compare_memfun(&Menu::name, name)) != end();
 }
 
 
 Menu const & MenuBackend::getMenu(string const & name) const
 {
-       for (const_iterator cit = begin(); cit != end(); ++cit) {
-               if ((*cit).name() == name)
-                       return (*cit);
-       }
-       Assert(false); // we actually require the name to exist.
-       return menulist_.front();
+       const_iterator cit = find_if(begin(), end(),
+                                    compare_memfun(&Menu::name, name));
+       Assert(cit != end());
+       return (*cit);
+}
+
+
+Menu & MenuBackend::getMenu(string const & name)
+{
+       MenuList::iterator it = find_if(menulist_.begin(), menulist_.end(),
+                                       compare_memfun(&Menu::name, name));
+       Assert(it != menulist_.end());
+       return (*it);
 }