aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMartin Peach <mrpeach@users.sourceforge.net>2011-10-27 20:21:51 +0000
committerIOhannes m zmölnig <zmoelnig@iem.at>2015-10-14 14:28:31 +0200
commit61f31586cbc4b9c358533e81847ad852fa0e1195 (patch)
tree0a10b3199e084d2d465953eb27dcd8a17cc03495
parent439fbb614935165fd40277d0e1153e1e8954652d (diff)
Added support for the "Open" item in the right-click menu of pdlua and pdluax objects.
The script for a .pd_lua or .pd_luax object will be opened in an editor via ::pd_menucommands::menuopenfile, as proposed by hcs. svn path=/trunk/externals/loaders/pdlua/; revision=15667
-rw-r--r--src/pd.lua21
-rw-r--r--src/pdlua.c36
2 files changed, 51 insertions, 6 deletions
diff --git a/src/pd.lua b/src/pd.lua
index 341894f..36ba28d 100644
--- a/src/pd.lua
+++ b/src/pd.lua
@@ -76,6 +76,13 @@ pd._clockdispatch = function (c)
end
end
+--whoami method dispatcher
+pd._whoami = function (object)
+ if nil ~= pd._objects[object] then
+ return pd._objects[object]:whoami()
+ end
+end
+
-- prototypical OO system
pd.Prototype = { }
function pd.Prototype:new(o)
@@ -207,6 +214,11 @@ function pd.Class:register(name)
self._class = pd._register(name) -- register new class
pd._classes[name] = self -- record registration
self._name = name
+ if name == "pdlua" then
+ self._scriptname = "pd.lua"
+ else
+ self._scriptname = name .. ".pd_lua"
+ end -- mrpeach 20111027
return self -- return new
end
end
@@ -273,9 +285,11 @@ end
function pd.Class:error(msg)
pd._error(self._object, msg)
end
+function pd.Class:whoami()
+ return self._scriptname or self._name
+end
-
-local lua = pd.Class:new():register("pdlua") -- global controls
+local lua = pd.Class:new():register("pdlua") -- global controls (the [pdlua] object only)
function lua:initialize(sel, atoms)
self.inlets = 1
self.outlets = 0 -- FIXME: might be nice to have errors go here?
@@ -286,10 +300,11 @@ function lua:in_1_load(atoms) -- execute a script
end
-local luax = pd.Class:new():register("pdluax") -- classless lua externals
+local luax = pd.Class:new():register("pdluax") -- classless lua externals (like [pdluax foo])
function luax:initialize(sel, atoms) -- motivation: pd-list 2007-09-23
local f = self:dofile(atoms[1] .. ".pd_luax")
if nil ~= f then
+ self._scriptname = atoms[1] .. ".pd_luax" -- mrpeach 20111027
local atomstail = { } -- munge for better lua<->luax compatibility
for i,_ in ipairs(atoms) do
if i > 1 then
diff --git a/src/pdlua.c b/src/pdlua.c
index 33df6fb..f814727 100644
--- a/src/pdlua.c
+++ b/src/pdlua.c
@@ -390,7 +390,32 @@ static void pdlua_free( t_pdlua *o /**< The object to destruct. */)
return;
}
-/** Lua class registration. */
+/** a handler for the open item in the right-click menu (mrpeach 20111025) */
+/** Here we find the lua code for the object and open it in an editor */
+static void pdlua_menu_open(t_pdlua *o)
+{
+ const char *name;
+
+#ifdef PDLUA_DEBUG
+ post("pdlua_menu_open");
+#endif // PDLUA_DEBUG
+ /** Get the scriptname of the object */
+ lua_getglobal(L, "pd");
+ lua_getfield(L, -1, "_whoami");
+ lua_pushlightuserdata(L, o);
+ if (lua_pcall(L, 1, 1, 0))
+ {
+ error("lua: error in whoami:\n%s", lua_tostring(L, -1));
+ return;
+ }
+ name = luaL_checkstring(L, -1);
+#ifdef PDLUA_DEBUG
+ post("pdlua_menu_open: L is %p, name is %s", L, name);
+#endif // PDLUA_DEBUG
+ if (name) sys_vgui("::pd_menucommands::menu_openfile {%s}\n", name);
+}
+
+/** Lua class registration. This is equivalent to the "setup" method for an ordinary Pd class */
static int pdlua_class_new(lua_State *L)
/**< Lua interpreter state.
* \par Inputs:
@@ -402,12 +427,17 @@ static int pdlua_class_new(lua_State *L)
const char *name;
t_class *c;
+ name = luaL_checkstring(L, 1);
#ifdef PDLUA_DEBUG
- post("pdlua_class_new:");
+ post("pdlua_class_new: L is %p, name is %s", L, name);
#endif // PDLUA_DEBUG
- name = luaL_checkstring(L, 1);
c = class_new(gensym((char *) name), (t_newmethod) pdlua_new,
(t_method) pdlua_free, sizeof(t_pdlua), CLASS_NOINLET, A_GIMME, 0);
+
+/* a class with a "menu-open" method will have the "Open" item highlighted in the right-click menu */
+ class_addmethod(c, (t_method)pdlua_menu_open, gensym("menu-open"), A_NULL);/* (mrpeach 20111025) */
+/**/
+
lua_pushlightuserdata(L, c);
return 1;
}