Skip to content

Commit

Permalink
Added filter-prepare function to utils.filterpicker
Browse files Browse the repository at this point in the history
  • Loading branch information
nickgammon committed Jul 12, 2011
1 parent 863e926 commit e901b4e
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 0 deletions.
23 changes: 23 additions & 0 deletions dialogs/FunctionListDlg.cpp
Expand Up @@ -27,6 +27,7 @@ CFunctionListDlg::CFunctionListDlg(CWnd* pParent /*=NULL*/)
m_bNoSort = false;
m_L = NULL;
m_iFilterIndex = LUA_NOREF;
m_iFilterPrepIndex = LUA_NOREF;
}


Expand Down Expand Up @@ -72,6 +73,28 @@ BOOL CFunctionListDlg::ReloadList ()

string sFilter (m_strFilter);

// call filter "prep" function (eg. do FTS3 database lookup based on the wanted filter)

if (m_L && m_iFilterPrepIndex != LUA_NOREF)
{

lua_rawgeti (m_L, LUA_REGISTRYINDEX, m_iFilterPrepIndex);

// Lua filter: function f (filter) ... end

// what they have currently typed
lua_pushlstring (m_L, sFilter.c_str (), sFilter.size ());

// call the function: arg1: filter field, arg2: key, arg3: value
if (lua_pcall (m_L, 1, 0, 0)) // call with 3 args and 0 results
{
LuaError (m_L); // note that this clears the stack, so we won't call it again
lua_settop (m_L, 0); // clear stack, just in case LuaError changes behaviour
} // end of error

} // end of Lua filter prep function available


// filter based on a partial match on what is in the filter box
// (eg. "chat" would find all chat functions)

Expand Down
1 change: 1 addition & 0 deletions dialogs/FunctionListDlg.h
Expand Up @@ -40,6 +40,7 @@ class CFunctionListDlg : public CDialog
// Lua state for filter function
lua_State * m_L;
int m_iFilterIndex;
int m_iFilterPrepIndex;

// Overrides
// ClassWizard generated virtual function overrides
Expand Down
20 changes: 20 additions & 0 deletions scripting/lua_utils.cpp
Expand Up @@ -1787,6 +1787,7 @@ CFunctionListDlg dlg;
} // end of looping through table


// filter function
if (lua_gettop (L) > 4)
{
if (!lua_isnil (L, 5))
Expand All @@ -1804,6 +1805,24 @@ CFunctionListDlg dlg;
}
}

// filter prep function
if (lua_gettop (L) > 5)
{
if (!lua_isnil (L, 6))
{
luaL_checktype (L, 6, LUA_TFUNCTION);
lua_pushvalue (L, 6); // function is now on top of stack

dlg.m_L = L;

// we can't leave the function on the stack, that gets cleared from time to time
// while the dialog box is running - so we store it in the registry and get the
// unique index back
dlg.m_iFilterPrepIndex = luaL_ref (L, LUA_REGISTRYINDEX);

}
}

if (dlg.DoModal () != IDOK)
lua_pushnil (L);
else
Expand All @@ -1821,6 +1840,7 @@ CFunctionListDlg dlg;

// free up registry entry (is OK with LUA_NOREF)
luaL_unref(L, LUA_REGISTRYINDEX, dlg.m_iFilterIndex);
luaL_unref(L, LUA_REGISTRYINDEX, dlg.m_iFilterPrepIndex);

return 1; // 1 result

Expand Down

0 comments on commit e901b4e

Please sign in to comment.