From 0472bb3509ba7c39928e6a20540fd02616598753 Mon Sep 17 00:00:00 2001 From: John Levon Date: Thu, 8 Aug 2002 22:03:30 +0000 Subject: [PATCH] some minor lyxaction cleanup git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@4911 a592a061-630c-0410-9148-cb99ea01b6c8 --- src/ChangeLog | 10 ++ src/LyXAction.C | 124 +++++------------ src/LyXAction.h | 126 ++++++++++-------- src/MenuBackend.C | 1 - src/ToolbarDefaults.C | 1 - src/frontends/ChangeLog | 4 + src/frontends/Toolbar.C | 3 - src/frontends/controllers/ChangeLog | 4 + .../controllers/ControlCommandBuffer.C | 2 - src/frontends/qt2/ChangeLog | 5 + src/frontends/qt2/Menubar_pimpl.C | 1 - src/frontends/qt2/Toolbar_pimpl.C | 8 +- src/frontends/xforms/ChangeLog | 5 + src/frontends/xforms/Menubar_pimpl.C | 1 - src/frontends/xforms/Toolbar_pimpl.C | 10 +- src/lyxfunc.C | 17 ++- src/lyxrc.C | 1 - src/toc.C | 2 - 18 files changed, 152 insertions(+), 173 deletions(-) diff --git a/src/ChangeLog b/src/ChangeLog index 7cf6b7b4d3..58005e0504 100644 --- a/src/ChangeLog +++ b/src/ChangeLog @@ -1,3 +1,13 @@ +2002-08-08 John Levon + + * LyXAction.h: + * LyXAction.C: + * MenuBackend.C: + * ToolbarDefaults.C: + * lyxfunc.C: + * lyxrc.C: + * toc.C: lyxaction cleanup + 2002-08-08 John Levon * BufferView2.C: small cleanup diff --git a/src/LyXAction.C b/src/LyXAction.C index 1163df8081..03f15b7b3f 100644 --- a/src/LyXAction.C +++ b/src/LyXAction.C @@ -1,12 +1,8 @@ -/* This file is part of - * ====================================================== - * - * LyX, The Document Processor - * - * Copyright 1995 Matthias Ettrich - * Copyright 1995-2001 The LyX Team. - * - * ====================================================== */ +/** + * \file LyXAction.C + * Copyright 1995-2002 the LyX Team + * Read the file COPYING + */ #include @@ -15,12 +11,17 @@ #endif #include "LyXAction.h" + #include "debug.h" #include "gettext.h" #include "support/lstrings.h" +#include + using std::ostream; using std::endl; +using std::pair; +using std::make_pair; /* NAMING RULES FOR USER-COMMANDS @@ -37,18 +38,19 @@ using std::endl; 8) The end of an object is called `end'. (May 19 1996, 12:04, RvdK) - */ +*/ -// These are globals. LyXAction lyxaction; -// Small helper function -inline -bool isPseudoAction(int a) +namespace { + +/// return true if the given action is a pseudo-action +inline bool isPseudoAction(int a) { return a > int(LFUN_LASTACTION); } +} void LyXAction::newFunc(kb_action action, string const & name, @@ -81,7 +83,7 @@ void LyXAction::init() unsigned int attrib; }; - ev_item items[] = { + ev_item const items[] = { { LFUN_ACUTE, "accent-acute", "", Noop }, { LFUN_BREVE, "accent-breve", "", Noop }, { LFUN_CARON, "accent-caron", "", Noop }, @@ -420,13 +422,9 @@ void LyXAction::init() { LFUN_NOACTION, "", "", Noop } }; - int i = 0; - while (items[i].action != LFUN_NOACTION) { - newFunc(items[i].action, - items[i].name, - _(items[i].helpText), - items[i].attrib); - ++i; + for (int i = 0; items[i].action != LFUN_NOACTION; ++i) { + newFunc(items[i].action, items[i].name, + _(items[i].helpText), items[i].attrib); } init = true; @@ -439,14 +437,11 @@ LyXAction::LyXAction() } -// Search for an existent pseudoaction, return LFUN_UNKNOWN_ACTION -// if it doesn't exist. int LyXAction::searchActionArg(kb_action action, string const & arg) const { arg_map::const_iterator pit = lyx_arg_map.find(action); if (pit == lyx_arg_map.end()) { - // the action does not have any pseudoactions lyxerr[Debug::ACTION] << "Action " << action << " does not have any pseudo actions." << endl; @@ -456,7 +451,6 @@ int LyXAction::searchActionArg(kb_action action, string const & arg) const arg_item::const_iterator aci = pit->second.find(arg); if (aci == pit->second.end()) { - // the action does not have any pseudoactions with this arg lyxerr[Debug::ACTION] << "Action " << action << "does not have any pseudoactions with arg " @@ -464,8 +458,7 @@ int LyXAction::searchActionArg(kb_action action, string const & arg) const return LFUN_UNKNOWN_ACTION; } - // pseudo action exist - lyxerr[Debug::ACTION] << "Pseudoaction exist[" + lyxerr[Debug::ACTION] << "Pseudoaction exists[" << action << '|' << arg << "] = " << aci->second << endl; @@ -473,8 +466,7 @@ int LyXAction::searchActionArg(kb_action action, string const & arg) const } -// Returns a pseudo-action given an action and its argument. -int LyXAction::getPseudoAction(kb_action action, string const & arg) const +int LyXAction::getPseudoAction(kb_action action, string const & arg) { int const psdaction = searchActionArg(action, arg); @@ -500,14 +492,10 @@ int LyXAction::getPseudoAction(kb_action action, string const & arg) const } -// Retrieves the real action and its argument. -// perhaps a pair should be returned? -kb_action LyXAction::retrieveActionArg(int pseudo, string & arg) const +pair LyXAction::retrieveActionArg(int pseudo) const { - arg.erase(); // clear it to be sure. - if (!isPseudoAction(pseudo)) - return static_cast(pseudo); + return make_pair(static_cast(pseudo), string()); pseudo_map::const_iterator pit = lyx_pseudo_map.find(pseudo); @@ -515,18 +503,17 @@ kb_action LyXAction::retrieveActionArg(int pseudo, string & arg) const lyxerr[Debug::ACTION] << "Found the pseudoaction: [" << pit->second.action << '|' << pit->second.argument << "]\n"; - arg = pit->second.argument; - return pit->second.action; + return make_pair(pit->second.action, pit->second.argument); } else { lyxerr << "Lyx Error: Unrecognized pseudo-action " << pseudo << endl; - return LFUN_UNKNOWN_ACTION; + return make_pair(LFUN_UNKNOWN_ACTION, string()); } } // Returns an action tag from a string. -int LyXAction::LookupFunc(string const & func) const +int LyXAction::LookupFunc(string const & func) { string const func2 = trim(func); if (func2.empty()) return LFUN_NOACTION; @@ -548,56 +535,12 @@ int LyXAction::LookupFunc(string const & func) const } -//#ifdef WITH_WARNINGS -//#warning Not working as it should. -//#endif -// I have no clue what is wrong with it... (Lgb) -int LyXAction::getApproxFunc(string const & func) const - // This func should perhaps also be able to return a list of all - // actions that has func as a prefix. That should actually be quite - // easy, just let it return a vector or something. -{ - int action = LookupFunc(func); - if (action == LFUN_UNKNOWN_ACTION) { - // func is not an action, but perhaps it is - // part of one...check if it is prefix if one of the - // actions. - // Checking for prefix is not so simple, but - // using a simple bounding function gives - // a similar result. [ale 19981103] - func_map::const_iterator fit = - lyx_func_map.lower_bound(func); - - if (fit != lyx_func_map.end()) { - action = fit->second; - } - } else { // Go get the next function - func_map::const_iterator fit = - lyx_func_map.upper_bound(func); - - if (fit != lyx_func_map.end()) { - action = fit->second; - } - } - - return action; -} - - -string const LyXAction::getApproxFuncName(string const & func) const -{ - int const f = getApproxFunc(func); - // This will return empty string if f isn't an action. - return getActionName(f); -} - - string const LyXAction::getActionName(int action) const { kb_action ac; string arg; - - ac = retrieveActionArg(action, arg); + boost::tie(ac, arg) = retrieveActionArg(action); + if (!arg.empty()) arg.insert(0, " "); @@ -612,15 +555,14 @@ string const LyXAction::getActionName(int action) const } -// Returns one line help associated with a (pseudo)action, i.e. appends -// the argument of the action if necessary string const LyXAction::helpText(int pseudoaction) const { - string help, arg; kb_action action; + string arg; + boost::tie(action, arg) = retrieveActionArg(pseudoaction); - action = retrieveActionArg(pseudoaction, arg); - + string help; + info_map::const_iterator ici = lyx_info_map.find(action); if (ici != lyx_info_map.end()) { if (lyxerr.debugging(Debug::ACTION)) { diff --git a/src/LyXAction.h b/src/LyXAction.h index 9b93c2744a..a2672a07eb 100644 --- a/src/LyXAction.h +++ b/src/LyXAction.h @@ -1,4 +1,10 @@ // -*- C++ -*- +/** + * \file LyXAction.h + * Copyright 1995-2002 the LyX Team + * Read the file COPYING + */ + #ifndef LYXACTION_H #define LYXACTION_H @@ -11,74 +17,67 @@ #include "funcrequest.h" #include -/** This class encapsulates LyX action and user command operations. +/** + * This class is a container for LyX actions. It also + * stores and managers "pseudo-actions". Pseudo-actions + * are not part of the kb_action enum, but are created + * dynamically, for encapsulating a real action and an + * argument. They are used for things like the menus. */ - class LyXAction : boost::noncopyable { private: - /// + /// information for an action struct func_info { - /// + /// the action name string name; - /// + /// the func_attrib values set unsigned int attrib; - /// + /// the help text for this action string helpText; }; public: - /// + /// type for map between a function name and its action typedef std::map func_map; - /// + /// type for map between an action and its info typedef std::map info_map; - /// + /// type for a map between a pseudo-action and its stored action/arg typedef std::map pseudo_map; - /// + /// map from argument to pseudo-action typedef std::map arg_item; - /// + /// map from an action to all its dependent pseudo-actions typedef std::map arg_map; - /// + /// possible "permissions" for an action enum func_attrib { - /// nothing special about this func - Noop = 0, - /// can be used in RO mode (perhaps this should change) - ReadOnly = 1, // , - /// Can be used when there is no document open - NoBuffer = 2, - //Interactive = 2, // Is interactive (requires a GUI) - /// - Argument = 4 // Requires argument - //MathOnly = 8, // Only math mode - //EtcEtc = ... // Or other attributes... + Noop = 0, //< nothing special about this func + ReadOnly = 1, //< can be used in RO mode (perhaps this should change) + NoBuffer = 2, //< Can be used when there is no document open + Argument = 4 //< Requires argument }; - /// LyXAction(); - /** Returns an pseudoaction from a string - If you include arguments in func_name, a new psedoaction will be - created if needed. */ - int LookupFunc(string const & func_name) const; - - /** Returns an action tag which name is the most similar to a string. - Don't include arguments, they would be ignored. */ - int getApproxFunc(string const & func) const; - - /** Returns an action name the most similar to a string. - Don't include arguments, they would be ignored. */ - string const getApproxFuncName(string const & func) const; + /** + * Returns an pseudoaction from a string + * If you include arguments in func_name, a new pseudoaction + * will be created if needed. + */ + int LookupFunc(string const & func_name); /// Returns a pseudo-action given an action and its argument. - int getPseudoAction(kb_action action, string const & arg) const; + int getPseudoAction(kb_action action, string const & arg); - /// Retrieves the real action and its argument. - kb_action retrieveActionArg(int i, string & arg) const; + /** + * Given a pseudo-action, return the real action and + * associated argument + */ + std::pair retrieveActionArg(int pseudo) const; /// Search for an existent pseudoaction, return -1 if it doesn't exist. int searchActionArg(kb_action action, string const & arg) const; - /// Return the name associated with command + /// Return the name (and argument) associated with the given (pseudo) action string const getActionName(int action) const; /// Return one line help text associated with (pseudo)action @@ -87,33 +86,50 @@ public: /// True if the command has `flag' set bool funcHasFlag(kb_action action, func_attrib flag) const; + /// iterator across all real actions typedef func_map::const_iterator const_func_iterator; + + /// return an iterator to the start of all real actions const_func_iterator func_begin() const; + + /// return an iterator to the end of all real actions const_func_iterator func_end() const; + private: - /// + /// populate the action container with our actions void init(); - /// + /// add the given action void newFunc(kb_action, string const & name, string const & helpText, unsigned int attrib); - /** This is a list of all the LyXFunc names with the - coresponding action number. It is usually only used by the - minibuffer or when assigning commands to keys during init. */ + /** + * This is a list of all the LyXFunc names with the + * coresponding action number. It is usually only used by the + * minibuffer or when assigning commands to keys during init. + */ func_map lyx_func_map; - /** This is a mapping from action number to an object holding - info about this action. f.ex. helptext, command name (string), - command attributes (ro) */ + /** + * This is a mapping from action number to an object holding + * info about this action. f.ex. helptext, command name (string), + * command attributes (ro) + */ info_map lyx_info_map; - /** A mapping from the automatically created pseudo action number - to the real action and its argument. */ - mutable pseudo_map lyx_pseudo_map; + /** + * A mapping from the automatically created pseudo action number + * to the real action and its argument. + */ + pseudo_map lyx_pseudo_map; - /** A (multi) mapping from the lyx action to all the generated - pseudofuncs and the arguments the action should use. */ - mutable arg_map lyx_arg_map; + /** + * A (multi) mapping from the lyx action to all the generated + * pseudofuncs and the arguments the action should use. + */ + arg_map lyx_arg_map; }; -#endif +/// singleton instance +extern LyXAction lyxaction; + +#endif // LYXACTION_H diff --git a/src/MenuBackend.C b/src/MenuBackend.C index f88905f28c..71ded3f708 100644 --- a/src/MenuBackend.C +++ b/src/MenuBackend.C @@ -34,7 +34,6 @@ #include "support/lyxfunctional.h" #include "support/lstrings.h" -extern LyXAction lyxaction; extern BufferList bufferlist; using std::endl; diff --git a/src/ToolbarDefaults.C b/src/ToolbarDefaults.C index 73d9e898b8..af42a4051a 100644 --- a/src/ToolbarDefaults.C +++ b/src/ToolbarDefaults.C @@ -24,7 +24,6 @@ using std::endl; -extern LyXAction lyxaction; ToolbarDefaults toolbardefaults; namespace { diff --git a/src/frontends/ChangeLog b/src/frontends/ChangeLog index b2167379d4..c78464eb2b 100644 --- a/src/frontends/ChangeLog +++ b/src/frontends/ChangeLog @@ -1,3 +1,7 @@ +2002-08-08 John Levon + + * Toolbar.C: + 2002-08-06 André Poentiz * Screen.C: Honor \show_banner lyxrc setting diff --git a/src/frontends/Toolbar.C b/src/frontends/Toolbar.C index ad2d95313a..c544bb3f91 100644 --- a/src/frontends/Toolbar.C +++ b/src/frontends/Toolbar.C @@ -20,9 +20,6 @@ using std::endl; -extern LyXAction lyxaction; - - Toolbar::Toolbar(LyXView * o, Dialogs & d, int x, int y, ToolbarDefaults const &tbd) : last_textclass_(-1) diff --git a/src/frontends/controllers/ChangeLog b/src/frontends/controllers/ChangeLog index 0e8f029cb8..384d656a38 100644 --- a/src/frontends/controllers/ChangeLog +++ b/src/frontends/controllers/ChangeLog @@ -1,3 +1,7 @@ +2002-08-08 John Levon + + * ControlCommandBuffer.C: LyXAction cleanup + 2002-08-07 John Levon * ControlSpellchecker.C: fix crash when spellchecker doesn't diff --git a/src/frontends/controllers/ControlCommandBuffer.C b/src/frontends/controllers/ControlCommandBuffer.C index 302c2721c3..f8fd58f9d5 100644 --- a/src/frontends/controllers/ControlCommandBuffer.C +++ b/src/frontends/controllers/ControlCommandBuffer.C @@ -24,8 +24,6 @@ using std::back_inserter; using std::transform; using std::endl; -extern LyXAction lyxaction; - namespace { struct prefix_p { diff --git a/src/frontends/qt2/ChangeLog b/src/frontends/qt2/ChangeLog index 80b58c6614..526a10ca45 100644 --- a/src/frontends/qt2/ChangeLog +++ b/src/frontends/qt2/ChangeLog @@ -1,3 +1,8 @@ +2002-08-08 John Levon + + * Toolbar_pimpl.C: + * Menubar_pimpl.C: lyxaction cleanup + 2002-08-08 John Levon * QGraphicsDialog.C: enable rotate diff --git a/src/frontends/qt2/Menubar_pimpl.C b/src/frontends/qt2/Menubar_pimpl.C index 6d9cfa47c7..afd9da20e9 100644 --- a/src/frontends/qt2/Menubar_pimpl.C +++ b/src/frontends/qt2/Menubar_pimpl.C @@ -63,7 +63,6 @@ string const getLabel(MenuItem const & mi) typedef vector::size_type size_type; extern boost::scoped_ptr toplevel_keymap; -extern LyXAction lyxaction; Menubar::Pimpl::Pimpl(LyXView * view, MenuBackend const & mbe) diff --git a/src/frontends/qt2/Toolbar_pimpl.C b/src/frontends/qt2/Toolbar_pimpl.C index deee5abc6c..ff340b90f2 100644 --- a/src/frontends/qt2/Toolbar_pimpl.C +++ b/src/frontends/qt2/Toolbar_pimpl.C @@ -36,17 +36,17 @@ using std::endl; -extern LyXAction lyxaction; - namespace { QPixmap getIconPixmap(int action) { + kb_action act; string arg; + boost::tie(act, arg) = lyxaction.retrieveActionArg(action); + + string const name = lyxaction.getActionName(act); string xpm_name; - const kb_action act = lyxaction.retrieveActionArg(action, arg); - string const name = lyxaction.getActionName(act); if (!arg.empty()) xpm_name = subst(name + ' ' + arg, ' ','_'); else diff --git a/src/frontends/xforms/ChangeLog b/src/frontends/xforms/ChangeLog index 0f140ca807..c902fd66f0 100644 --- a/src/frontends/xforms/ChangeLog +++ b/src/frontends/xforms/ChangeLog @@ -1,3 +1,8 @@ +2002-08-08 John Levon + + * Menubar_pimpl.C: + * Toolbar_pimpl.C: lyxaction cleanup + 2002-08-08 John Levon * forms/form_thesaurus.fd: allow Esc to close dialog diff --git a/src/frontends/xforms/Menubar_pimpl.C b/src/frontends/xforms/Menubar_pimpl.C index f820fb938d..9538925585 100644 --- a/src/frontends/xforms/Menubar_pimpl.C +++ b/src/frontends/xforms/Menubar_pimpl.C @@ -38,7 +38,6 @@ using std::for_each; typedef vector::size_type size_type; extern boost::scoped_ptr toplevel_keymap; -extern LyXAction lyxaction; namespace { diff --git a/src/frontends/xforms/Toolbar_pimpl.C b/src/frontends/xforms/Toolbar_pimpl.C index 2d69e24bb5..501531d7e2 100644 --- a/src/frontends/xforms/Toolbar_pimpl.C +++ b/src/frontends/xforms/Toolbar_pimpl.C @@ -36,10 +36,10 @@ #include "support/filetools.h" #include "support/lstrings.h" +#include + using std::endl; -extern LyXAction lyxaction; - // some constants const int standardspacing = 2; // the usual space between items const int sepspace = 6; // extra space @@ -251,10 +251,12 @@ extern "C" { void setPixmap(FL_OBJECT * obj, int action, int buttonwidth, int height) { - string arg; string xpm_name; - const kb_action act = lyxaction.retrieveActionArg(action, arg); + kb_action act; + string arg; + boost::tie(act, arg) = lyxaction.retrieveActionArg(action); + string const name = lyxaction.getActionName(act); if (!arg.empty()) xpm_name = subst(name + ' ' + arg, ' ','_'); diff --git a/src/lyxfunc.C b/src/lyxfunc.C index d301877e39..53b30fbb43 100644 --- a/src/lyxfunc.C +++ b/src/lyxfunc.C @@ -90,6 +90,8 @@ #include "support/path.h" #include "support/lyxfunctional.h" +#include + #include #include #include @@ -114,7 +116,6 @@ extern boost::scoped_ptr toplevel_keymap; extern void show_symbols_form(LyXFunc *); -extern LyXAction lyxaction; // (alkis) extern tex_accent_struct get_accent(kb_action action); @@ -275,9 +276,10 @@ void LyXFunc::processKeySym(LyXKeySymPtr keysym, FuncStatus LyXFunc::getStatus(int ac) const { - string argument; - kb_action action = lyxaction.retrieveActionArg(ac, argument); - return getStatus(FuncRequest(action, argument)); + kb_action action; + string arg; + boost::tie(action, arg) = lyxaction.retrieveActionArg(ac); + return getStatus(FuncRequest(action, arg)); } @@ -703,9 +705,10 @@ void LyXFunc::dispatch(string const & s, bool verbose) void LyXFunc::dispatch(int ac, bool verbose) { - string argument; - kb_action const action = lyxaction.retrieveActionArg(ac, argument); - dispatch(FuncRequest(action, argument), verbose); + kb_action action; + string arg; + boost::tie(action, arg) = lyxaction.retrieveActionArg(ac); + dispatch(FuncRequest(action, arg), verbose); } diff --git a/src/lyxrc.C b/src/lyxrc.C index eef41ae2f8..809f71ee64 100644 --- a/src/lyxrc.C +++ b/src/lyxrc.C @@ -40,7 +40,6 @@ using std::vector; class kb_keymap; -extern LyXAction lyxaction; extern boost::scoped_ptr toplevel_keymap; namespace { diff --git a/src/toc.C b/src/toc.C index e0398fb09d..8bca58c227 100644 --- a/src/toc.C +++ b/src/toc.C @@ -34,8 +34,6 @@ using std::max; using std::endl; using std::ostream; -extern LyXAction lyxaction; - namespace toc {