rationalize settings into a central registry.
[apps/madmutt.git] / lib-lua / madmutt.c
index 6872096..4bb0095 100644 (file)
@@ -26,6 +26,8 @@
 
 #include "../mutt.h"
 
+/* {{{ madmutt functions */
+
 static int madmutt_pwd(lua_State *L)
 {
     char path[_POSIX_PATH_MAX];
@@ -60,45 +62,62 @@ static int madmutt_assign(lua_State *L)
 {
     const char *idx = luaL_checkstring(L, 2);
     const char *val = luaL_checkstring(L, 3);
-    char buf[STRING];
+    int tk;
+
+    switch ((tk = lua_which_token(idx, -1))) {
+        char buf[STRING];
 
-    switch (lua_which_token(idx, -1)) {
       default:
-        luaL_error(L, "bad subscript to madmutt: %s", val);
+        luaL_error(L, "read-only or inexistant property '%s'", idx, tk);
         return 0;
 
       case LTK_DOTLOCK:
       case LTK_SENDMAIL:
       case LTK_SHELL:
         _mutt_expand_path(buf, sizeof(buf), val, 0);
-        val = buf;
-        break;
+        mlua_regsets(tk, buf);
+        return 0;
     }
+}
 
-    lua_getmetatable(L, 1);
-    lua_pushstring(L, idx);
-    lua_pushstring(L, val);
-    lua_rawset(L, -3);
+static int madmutt_get(lua_State *L)
+{
+    const char *idx = luaL_checkstring(L, 2);
+    int tk;
+
+    switch ((tk = lua_which_token(idx, -1))) {
+      default:
+        lua_getmetatable(L, 1);
+        lua_replace(L, 1);
+        lua_rawget(L, 1);
+        return 1;
+
+      case LTK_DOTLOCK:
+      case LTK_SENDMAIL:
+      case LTK_SHELL:
+        lua_pushstring(L, mlua_reggets(tk));
+        break;
+    }
 
-    return 0;
+    return 1;
 }
 
 static const struct luaL_Reg madmutt_module_funcs[] = {
-    {"pwd",         madmutt_pwd},
+    { "pwd",         madmutt_pwd },
     /*
      ** .pp
      ** \fIThis is a read-only system property and, at runtime,
      ** specifies the current working directory of the madmutt
      ** binary.\fP
      */
-    {"folder_path", madmutt_folder_path},
+    { "folder_path", madmutt_folder_path },
     /*
      ** .pp
      ** \fIThis is a read-only system property and, at runtime,
      ** specifies the full path or URI of the folder currently
      ** open (if any).\fP
      */
-    {"folder_name", madmutt_folder_name},
+    { "folder_name", madmutt_folder_name },
     /*
      ** .pp
      ** \fIThis is a read-only system property and, at runtime,
@@ -118,10 +137,15 @@ static const struct luaL_Reg madmutt_module_funcs[] = {
      ** $$$folder nor a ``/'' were found in the name.
      */
 
-    {"__newindex",  madmutt_assign},
-    {NULL, NULL}
+    { "__newindex",  madmutt_assign },
+    { "__index",     madmutt_get },
+    { NULL, NULL }
 };
 
+/* }}} */
+
+/* {{{ read-only properties */
+
 static const struct {
     const char *key;
     const char *value;
@@ -166,22 +190,12 @@ static const struct {
      ** header chaching's database backend.\fP
      */
 #endif
-
-    {"dotlock",     BINDIR "/mutt_dotlock"},
-    /*
-     ** .pp
-     ** Contains the path of the \fTmadmutt_dotlock(1)\fP binary to be used by
-     ** Madmutt.
-     */
-    {"sendmail",    SENDMAIL " -oem -oi"},
-    /*
-     ** .pp
-     ** Specifies the program and arguments used to deliver mail sent by Madmutt.
-     ** Madmutt expects that the specified program interprets additional
-     ** arguments as recipient addresses.
-     */
 };
 
+/* }}} */
+
+/* {{{ madmutt magic properties */
+
 static void madmutt_init_shell(char *buf, ssize_t len)
 {
     struct passwd *pw = getpwuid(getuid());
@@ -197,8 +211,22 @@ static void madmutt_init_shell(char *buf, ssize_t len)
 static const struct {
     const char *key;
     void (*fun)(char *buf, ssize_t len);
+    const char *val;
 } madmutt_module_vars2[] = {
-    { "shell",          madmutt_init_shell },
+    { "dotlock",     NULL, BINDIR "/mutt_dotlock" },
+    /*
+     ** .pp
+     ** Contains the path of the \fTmadmutt_dotlock(1)\fP binary to be used by
+     ** Madmutt.
+     */
+    { "sendmail",    NULL, SENDMAIL " -oem -oi" },
+    /*
+     ** .pp
+     ** Specifies the program and arguments used to deliver mail sent by Madmutt.
+     ** Madmutt expects that the specified program interprets additional
+     ** arguments as recipient addresses.
+     */
+    { "shell",       madmutt_init_shell, NULL },
     /*
      ** .pp
      ** Command to use when spawning a subshell.  By default, the user's login
@@ -206,6 +234,8 @@ static const struct {
      */
 };
 
+/* }}} */
+
 int luaopen_madmutt(lua_State *L)
 {
     int i;
@@ -220,19 +250,19 @@ int luaopen_madmutt(lua_State *L)
         lua_setfield(L, -2, madmutt_module_vars[i].key);
     }
 
+    lua_setmetatable(L, -2);
+
     for (i = 0; i < countof(madmutt_module_vars2); i++) {
-        char buf[STRING];
-        (madmutt_module_vars2[i].fun)(buf, sizeof(buf));
-        lua_pushstring(L, buf);
+        if (madmutt_module_vars2[i].fun) {
+            char buf[STRING];
+            (madmutt_module_vars2[i].fun)(buf, sizeof(buf));
+            lua_pushstring(L, buf);
+        } else {
+            lua_pushstring(L, madmutt_module_vars2[i].val);
+        }
         lua_setfield(L, -2, madmutt_module_vars2[i].key);
     }
 
-    lua_pushstring(L, "__index");
-    lua_pushvalue(L, -2);
-    lua_settable(L, -3);
-
-    lua_setmetatable(L, -2);
     lua_setglobal(L, "madmutt");
-
     return 1;
 }