From 2f4efb28d6d53865f08a01c7d4239ebd5fccd981 Mon Sep 17 00:00:00 2001 From: Paul Beckingham Date: Sun, 17 Apr 2011 00:48:05 -0400 Subject: [PATCH] Lua - Created new scripts/extensions directory. - Added README for extensions. - Started priority.lua extension. - Implemented api_task_get and api_task_set DOM accessors. - Removed more obsolete API. --- scripts/CMakeLists.txt | 2 +- scripts/extensions/README | 8 +++ scripts/extensions/priority.lua | 42 +++++++++++++ src/API.cpp | 104 +++++++++++--------------------- 4 files changed, 86 insertions(+), 70 deletions(-) create mode 100644 scripts/extensions/README create mode 100644 scripts/extensions/priority.lua diff --git a/scripts/CMakeLists.txt b/scripts/CMakeLists.txt index 36e98b416..ad1fc8614 100644 --- a/scripts/CMakeLists.txt +++ b/scripts/CMakeLists.txt @@ -1 +1 @@ -install (DIRECTORY bash fish vim zsh add-ons DESTINATION ${TASK_DOCDIR}/scripts) +install (DIRECTORY bash fish vim zsh add-ons extensions DESTINATION ${TASK_DOCDIR}/scripts) diff --git a/scripts/extensions/README b/scripts/extensions/README new file mode 100644 index 000000000..8f4bcac02 --- /dev/null +++ b/scripts/extensions/README @@ -0,0 +1,8 @@ +There are several types of extension: + +- Hook-based +- User Defined Attribute +- Command +- Format +- DOM + diff --git a/scripts/extensions/priority.lua b/scripts/extensions/priority.lua new file mode 100644 index 000000000..6a4120a6e --- /dev/null +++ b/scripts/extensions/priority.lua @@ -0,0 +1,42 @@ +-------------------------------------------------------------------------------- +-- taskwarrior - a command line task list manager. +-- +-- Copyright 2006 - 2011, Paul Beckingham, Federico Hernandez. +-- All rights reserved. +-- +-- This program is free software; you can redistribute it and/or modify it under +-- the terms of the GNU General Public License as published by the Free Software +-- Foundation; either version 2 of the License, or (at your option) any later +-- version. +-- +-- This program is distributed in the hope that it will be useful, but WITHOUT +-- ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +-- FOR A PARTICULAR PURPOSE. See the GNU General Public License for more +-- details. +-- +-- You should have received a copy of the GNU General Public License along with +-- this program; if not, write to the +-- +-- Free Software Foundation, Inc., +-- 51 Franklin Street, Fifth Floor, +-- Boston, MA +-- 02110-1301 +-- USA +-- +-------------------------------------------------------------------------------- + +-- Extension to implement 'priority' as a user-defined attribute. + +-- Required for all extensions. +function install () + return 'Priority', -- Name + 1.0, -- Version + 'uda', -- Type + 'Implements priority attribute', -- Description + 'Paul Beckingham', -- Author + 'paul@beckingham.net', -- Contact + '© 2011, Göteborg Bit Factory' -- Copyright +end + +-- TODO Required for UDA extensions. + diff --git a/src/API.cpp b/src/API.cpp index 10f9b0219..383ceea93 100644 --- a/src/API.cpp +++ b/src/API.cpp @@ -57,71 +57,6 @@ Task* the_task = NULL; #ifdef HAVE_LIBLUA -//////////////////////////////////////////////////////////////////////////////// -// Returns a string representing the taskwarrior version number, such as -// '1.9.0'. -static int api_task_version (lua_State* L) -{ - lua_pushstring (L, PACKAGE_VERSION); - return 1; -} - -//////////////////////////////////////////////////////////////////////////////// -// Returns a string representing the Lua version number, such as '5.1.4'. -// Lua 5.2.0 has a 'lua_version' call, but 5.1.4 is the target. -static int api_task_lua_version (lua_State* L) -{ - // Convert "Lua 5.1.4" -> "5.1.4" - std::string ver = LUA_RELEASE; - lua_pushstring (L, ver.substr (4, std::string::npos).c_str ()); - return 1; -} - -//////////////////////////////////////////////////////////////////////////////// -// Returns the type of OS that task is running on. -static int api_task_os (lua_State* L) -{ -#if defined (DARWIN) - lua_pushstring (L, "darwin"); -#elif defined (SOLARIS) - lua_pushstring (L, "solaris"); -#elif defined (CYGWIN) - lua_pushstring (L, "cygwin"); -#elif defined (OPENBSD) - lua_pushstring (L, "openbsd"); -#elif defined (HAIKU) - lua_pushstring (L, "haiku"); -#elif defined (FREEBSD) - lua_pushstring (L, "freebsd"); -#elif defined (LINUX) - lua_pushstring (L, "linux"); -#else - lua_pushstring (L, "unknown"); -#endif - - return 1; -} - -//////////////////////////////////////////////////////////////////////////////// -static int api_task_feature (lua_State* L) -{ - std::string name = luaL_checkstring (L, 1); - bool value = false; - - if (name == "readline") - { -#ifdef HAVE_READLINE - value = true; -#endif - } - - else if (name == "lua") - value = true; - - lua_pushboolean (L, value ? 1 : 0); - return 1; -} - //////////////////////////////////////////////////////////////////////////////// static int api_task_header_message (lua_State* L) { @@ -157,6 +92,39 @@ static int api_task_exit (lua_State* L) return 0; } +//////////////////////////////////////////////////////////////////////////////// +// DOM reads. +static int api_task_get (lua_State* L) +{ + std::string name = luaL_checkstring (L, 1); + try + { + lua_pushstring (L, context.dom.get (name).c_str ()); + } + catch (...) + { + // TODO Error! + lua_pushstring (L, ""); + } +} + +//////////////////////////////////////////////////////////////////////////////// +// DOM writes. +static int api_task_set (lua_State* L) +{ + std::string name = luaL_checkstring (L, 1); + std::string value = luaL_checkstring (L, 2); + + try + { + context.dom.set (name, value); + } + catch (...) + { + // TODO Error! + } +} + //////////////////////////////////////////////////////////////////////////////// API::API () : L (NULL) @@ -181,14 +149,12 @@ void API::initialize () luaL_openlibs (L); // TODO Error handling // Register all the API functions in Lua global space. - lua_pushcfunction (L, api_task_version); lua_setglobal (L, "task_version"); - lua_pushcfunction (L, api_task_lua_version); lua_setglobal (L, "task_lua_version"); - lua_pushcfunction (L, api_task_os); lua_setglobal (L, "task_os"); - lua_pushcfunction (L, api_task_feature); lua_setglobal (L, "task_feature"); lua_pushcfunction (L, api_task_header_message); lua_setglobal (L, "task_header_message"); lua_pushcfunction (L, api_task_footnote_message); lua_setglobal (L, "task_footnote_message"); lua_pushcfunction (L, api_task_debug_message); lua_setglobal (L, "task_debug_message"); lua_pushcfunction (L, api_task_exit); lua_setglobal (L, "task_exit"); + lua_pushcfunction (L, api_task_get); lua_setglobal (L, "task_get"); + lua_pushcfunction (L, api_task_set); lua_setglobal (L, "task_set"); } ////////////////////////////////////////////////////////////////////////////////