qemacs-commit
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Qemacs-commit] qemacs variables.c variables.h Makefile


From: Charlie Gordon
Subject: [Qemacs-commit] qemacs variables.c variables.h Makefile
Date: Sun, 30 Aug 2015 12:03:47 +0000

CVSROOT:        /sources/qemacs
Module name:    qemacs
Changes by:     Charlie Gordon <chqrlie>        15/08/30 12:03:47

Modified files:
        .              : variables.c variables.h Makefile 

Log message:
        variables: add value setting handler support
        - Allow handler to return error codes VAR_INVALID or VAR_READONLY
        - Display explicit error messages
        - Add variables.h to generic dependency list in Makefile

CVSWeb URLs:
http://cvs.savannah.gnu.org/viewcvs/qemacs/variables.c?cvsroot=qemacs&r1=1.18&r2=1.19
http://cvs.savannah.gnu.org/viewcvs/qemacs/variables.h?cvsroot=qemacs&r1=1.6&r2=1.7
http://cvs.savannah.gnu.org/viewcvs/qemacs/Makefile?cvsroot=qemacs&r1=1.80&r2=1.81

Patches:
Index: variables.c
===================================================================
RCS file: /sources/qemacs/qemacs/variables.c,v
retrieving revision 1.18
retrieving revision 1.19
diff -u -b -r1.18 -r1.19
--- variables.c 13 Aug 2015 23:27:00 -0000      1.18
+++ variables.c 30 Aug 2015 12:03:46 -0000      1.19
@@ -93,18 +93,6 @@
     //G_VAR( "perl-mode-extensions", perl_mode.extensions, VAR_STRING, VAR_RW )
 };
 
-void qe_register_variables(VarDef *vars, int count)
-{
-    QEmacsState *qs = &qe_state;
-    VarDef *vp;
-
-    for (vp = vars; vp < vars + count - 1; vp++) {
-        vp->next = vp + 1;
-    }
-    vp->next = qs->first_variable;
-    qs->first_variable = vars;
-}
-
 VarDef *qe_find_variable(const char *name)
 {
     QEmacsState *qs = &qe_state;
@@ -215,12 +203,48 @@
 u8 end[8];
 #endif
 
-QVarType qe_set_variable(EditState *s, const char *name,
+static QVarType qe_generic_set_variable(EditState *s, VarDef *vp, void *ptr,
                          const char *value, int num)
 {
     char buf[32];
-    void *ptr;
     char **pstr;
+
+    switch (vp->type) {
+    case VAR_STRING:
+        if (!value) {
+            snprintf(buf, sizeof(buf), "%d", num);
+            value = buf;
+        }
+        pstr = (char **)ptr;
+        if ((u8 *)*pstr > end)
+            qe_free(pstr);
+        *pstr = qe_strdup(value);
+        break;
+    case VAR_CHARS:
+        if (!value) {
+            snprintf(buf, sizeof(buf), "%d", num);
+            value = buf;
+        }
+        pstrcpy(ptr, vp->size, value);
+        break;
+    case VAR_NUMBER:
+        if (!value) {
+            /* XXX: should have default, min and max values */
+            *(int*)ptr = num;
+        } else {
+            return VAR_INVALID;
+        }
+        break;
+    default:
+        return VAR_UNKNOWN;
+    }
+    return vp->type;
+}
+
+QVarType qe_set_variable(EditState *s, const char *name,
+                         const char *value, int num)
+{
+    void *ptr;
     VarDef *vp;
 
     vp = qe_find_variable(name);
@@ -241,8 +265,7 @@
         return vp->type;
     } else
     if (vp->rw == VAR_RO) {
-        put_status(s, "Variable %s is read-only", name);
-        return VAR_UNKNOWN;
+        return VAR_READONLY;
     } else {
         switch (vp->domain) {
         case VAR_SELF:
@@ -266,35 +289,13 @@
         default:
             return VAR_UNKNOWN;
         }
-
-        switch (vp->type) {
-        case VAR_STRING:
-            if (!value) {
-                snprintf(buf, sizeof(buf), "%d", num);
-                value = buf;
-            }
-            pstr = (char **)ptr;
-            if ((u8 *)*pstr > end)
-                qe_free(pstr);
-            *pstr = qe_strdup(value);
-            break;
-        case VAR_CHARS:
-            if (!value) {
-                snprintf(buf, sizeof(buf), "%d", num);
-                value = buf;
-            }
-            pstrcpy(ptr, vp->size, value);
-            break;
-        case VAR_NUMBER:
-            if (!value)
-                *(int*)ptr = num;
-            else
-                *(int*)ptr = strtol(value, NULL, 0);
-            break;
-        default:
-            return VAR_UNKNOWN;
+        if (vp->type == VAR_NUMBER && value) {
+            char *p;
+            num = strtol(value, &p, 0);
+            if (!*p)
+                value = NULL;
         }
-        return vp->type;
+        return vp->set_value(s, vp, ptr, value, num);
     }
 }
 
@@ -310,8 +311,34 @@
 
 void do_set_variable(EditState *s, const char *name, const char *value)
 {
-    qe_set_variable(s, name, value, 0);
+    switch (qe_set_variable(s, name, value, 0)) {
+    case VAR_UNKNOWN:
+        put_status(s, "Variable %s is invalid", name);
+        break;
+    case VAR_READONLY:
+        put_status(s, "Variable %s is read-only", name);
+        break;
+    case VAR_INVALID:
+        put_status(s, "Invalid value for variable %s: %s", name, value);
+        break;
+    default:
     do_show_variable(s, name);
+        break;
+    }
+}
+
+void qe_register_variables(VarDef *vars, int count)
+{
+    QEmacsState *qs = &qe_state;
+    VarDef *vp;
+
+    for (vp = vars; vp < vars + count - 1; vp++) {
+        if (!vp->set_value)
+            vp->set_value = qe_generic_set_variable;
+        vp->next = vp + 1;
+    }
+    vp->next = qs->first_variable;
+    qs->first_variable = vars;
 }
 
 /* should register this as help function */

Index: variables.h
===================================================================
RCS file: /sources/qemacs/qemacs/variables.h,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -b -r1.6 -r1.7
--- variables.h 27 Aug 2015 23:03:41 -0000      1.6
+++ variables.h 30 Aug 2015 12:03:47 -0000      1.7
@@ -23,6 +23,8 @@
     VAR_NUMBER,
     VAR_STRING,
     VAR_CHARS,
+    VAR_READONLY,
+    VAR_INVALID,
 } QVarType;
 
 enum QVarAccess {
@@ -42,7 +44,8 @@
 
 extern const char * const var_domain[];
 
-typedef struct VarDef {
+typedef struct VarDef VarDef;
+struct VarDef {
     const char *name;
     enum QVarDomain domain : 4;
     enum QVarType type : 4;
@@ -56,25 +59,34 @@
         char *str;
         int num;
     } value;
-    struct VarDef *next;
-} VarDef;
+    QVarType (*set_value)(EditState *s, VarDef *vp, void *ptr,
+                          const char *str, int num);
+    VarDef *next;
+};
 
-#define U_VAR(name, type) \
-    { (name), VAR_SELF, type, VAR_RW, 0, { .num = 0 }, NULL },
-#define G_VAR(name,var,type,rw) \
-    { (name), VAR_GLOBAL, type, rw, 0, { .ptr = (void*)&(var) }, NULL },
-#define S_VAR(name,fld,type,rw) \
+#define U_VAR_F(name, type, fun) \
+    { (name), VAR_SELF, type, VAR_RW, 0, { .num = 0 }, fun, NULL },
+#define G_VAR_F(name, var, type, rw, fun) \
+    { (name), VAR_GLOBAL, type, rw, 0, { .ptr = (void*)&(var) }, fun, NULL },
+#define S_VAR_F(name, fld, type, rw, fun) \
     { (name), VAR_STATE, type, rw, sizeof(((QEmacsState*)0)->fld), \
-      { .offset = offsetof(QEmacsState, fld) }, NULL },
-#define B_VAR(name,fld,type,rw) \
+      { .offset = offsetof(QEmacsState, fld) }, fun, NULL },
+#define B_VAR_F(name, fld, type, rw, fun) \
     { (name), VAR_BUFFER, type, rw, sizeof(((EditBuffer*)0)->fld), \
-      { .offset = offsetof(EditBuffer, fld) }, NULL },
-#define W_VAR(name,fld,type,rw) \
+      { .offset = offsetof(EditBuffer, fld) }, fun, NULL },
+#define W_VAR_F(name, fld, type, rw, fun) \
     { (name), VAR_WINDOW, type, rw, sizeof(((EditState*)0)->fld), \
-      { .offset = offsetof(EditState, fld) }, NULL },
-#define M_VAR(name,fld,type,rw) \
+      { .offset = offsetof(EditState, fld) }, fun, NULL },
+#define M_VAR_F(name, fld, type, rw, fun) \
     { (name), VAR_MODE, type, rw, sizeof(((ModeDef*)0)->fld), \
-      { .offset = offsetof(ModeDef, fld) }, NULL },
+      { .offset = offsetof(ModeDef, fld) }, fun, NULL },
+
+#define U_VAR(name, type)        U_VAR_F(name, type, NULL)
+#define G_VAR(name,var,type,rw)  G_VAR_F(name,var,type,rw, NULL)
+#define S_VAR(name,fld,type,rw)  S_VAR_F(name,fld,type,rw, NULL)
+#define B_VAR(name,fld,type,rw)  B_VAR_F(name,fld,type,rw, NULL)
+#define W_VAR(name,fld,type,rw)  W_VAR_F(name,fld,type,rw, NULL)
+#define M_VAR(name,fld,type,rw)  M_VAR_F(name,fld,type,rw, NULL)
 
 void qe_register_variables(VarDef *vars, int count);
 VarDef *qe_find_variable(const char *name);

Index: Makefile
===================================================================
RCS file: /sources/qemacs/qemacs/Makefile,v
retrieving revision 1.80
retrieving revision 1.81
diff -u -b -r1.80 -r1.81
--- Makefile    19 Aug 2015 14:30:11 -0000      1.80
+++ Makefile    30 Aug 2015 12:03:47 -0000      1.81
@@ -169,7 +169,7 @@
 SRCS:= $(OBJS:.o=.c)
 TSRCS:= $(TOBJS:.o=.c)
 
-DEPENDS:= qe.h config.h cutils.h display.h qestyles.h config.mak
+DEPENDS:= qe.h config.h cutils.h display.h qestyles.h variables.h config.mak
 DEPENDS:= $(addprefix $(DEPTH)/, $(DEPENDS))
 
 OBJS_DIR:= $(DEPTH)/.objs



reply via email to

[Prev in Thread] Current Thread [Next in Thread]