[patch 08/11] timer_add()/timer_remove() macros

Bert Wesarg wesarg at informatik.uni-halle.de
Sat Jan 27 16:15:37 CET 2007


These macros are used to dispaly the calltip only after a timeout of 1.5 sec

---

 source/macro.c |  134 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 132 insertions(+), 2 deletions(-)

diff --quilt old/source/macro.c new/source/macro.c
--- old/source/macro.c
+++ new/source/macro.c
@@ -416,10 +416,14 @@ static int callFuncMS(WindowInfo *window
         int nArgs, DataValue *result, char **errMsg);
 static int callFuncWithArgsMS(WindowInfo *window, DataValue *argList,
         int nArgs, DataValue *result, char **errMsg);
 static int defineFuncMS(WindowInfo *window, DataValue *argList,
         int nArgs, DataValue *result, char **errMsg);
+static int timerAddMS(WindowInfo *window, DataValue *argList,
+        int nArgs, DataValue *result, char **errMsg);
+static int timerRemoveMS(WindowInfo *window, DataValue *argList,
+        int nArgs, DataValue *result, char **errMsg);
 
 static int dictinsertMS(WindowInfo *window, DataValue *argList, int nArgs,
         DataValue *result, char **errMsg);
 static int dictcompleteMS(WindowInfo *window, DataValue *argList, int nArgs,
         DataValue *result, char **errMsg);
@@ -451,11 +455,12 @@ static BuiltInSubr MacroSubrs[] = {lengt
         getStyleByNameMS, getStyleAtPosMS, filenameDialogMS,
         typeofMS,
         dictinsertMS, dictcompleteMS, dictsaveMS,
         dictappendMS, dictiselementMS,
         callFuncMS, callFuncWithArgsMS,
-        defineFuncMS
+        defineFuncMS,
+        timerAddMS, timerRemoveMS
     };
 #define N_MACRO_SUBRS (sizeof MacroSubrs/sizeof *MacroSubrs)
 static const char *MacroSubrNames[N_MACRO_SUBRS] = {"length", "get_range", "t_print",
         "dialog", "string_dialog", "replace_range", "replace_selection",
         "set_cursor_pos", "get_character", "min", "max", "search",
@@ -476,11 +481,12 @@ static const char *MacroSubrNames[N_MACR
         "get_style_by_name", "get_style_at_pos", "filename_dialog",
         "typeof",
         "dict_insert", "dict_complete", "dict_save",
         "dict_append", "dict_is_element",
         "call_func", "call_func_with_args",
-        "define_func"
+        "define_func",
+        "timer_add", "timer_remove"
     };
 static BuiltInSubr SpecialVars[] = {cursorMV, lineMV, columnMV,
         fileNameMV, filePathMV, lengthMV, selectionStartMV, selectionEndMV,
         selectionLeftMV, selectionRightMV, wrapMarginMV, tabDistMV,
         emTabDistMV, useTabsMV, languageModeMV, modifiedMV,
@@ -3822,10 +3828,134 @@ static int defineFuncMS(WindowInfo *wind
     }
 
     return True;
 }
 
+struct timer_cb_data {
+    WindowInfo *window;
+    int global;
+    char *name;
+};
+
+static void macroTimerProc(XtPointer clientData, XtIntervalId *id)
+{
+    struct timer_cb_data *data = clientData;
+    WindowInfo *win;
+
+    /* check if window is still there, but don't trust the pointer */
+    for (win = WindowList; win; win = win->next)
+        if (win == data->window)
+            break;
+    if (win != data->window)
+        goto out;
+
+    /* don't call macro if its not a global timer and current top-window is
+       different from timer_add() window */
+    if (!data->global && !IsTopDocument(data->window))
+        goto out;
+
+    MacroApplyHook(data->window, data->name, 0, NULL);
+
+out:
+    XtFree((char *)data);
+}
+
+static int timerAddMS(WindowInfo *window, DataValue *argList,
+        int nArgs, DataValue *result, char **errMsg)
+{
+    int timeout;
+    XtIntervalId id;
+    char stringStorage[2][TYPE_INT_STR_SIZE(int)];
+    char *name = NULL;
+    int is_global = 0;
+    char *global;
+    struct timer_cb_data *data;
+
+    if (nArgs > 3) {
+        *errMsg = "subroutine %s called with too many arguments";
+        return False;
+    }
+    if (nArgs < 2) {
+        *errMsg = "subroutine %s called with too few arguments";
+        return False;
+    }
+    if (!readStringArg(argList[0], &name, stringStorage[0], errMsg)) {
+        return False;
+    }
+    if (!readIntArg(argList[1], &timeout, errMsg)) {
+        return False;
+    }
+    if (nArgs >= 3) {
+        if(!readStringArg(argList[2], &global, stringStorage[1], errMsg)) {
+            return False;
+        }
+        if (!strcmp(global, "global")) {
+            is_global = 1;
+        } else {
+            *errMsg = "unknown argument for subroutine %s";
+            return False;
+        }
+    }
+
+    if (timeout < 100) {
+        *errMsg = "timeout too small";
+        return False;
+    }
+
+    data = (struct timer_cb_data *)XtMalloc(
+        sizeof(*data) + ((strlen(name) + 1) * sizeof(char)));
+    if (!data) {
+        *errMsg = "internal error";
+        return False;
+    }
+
+    data->window = window;
+    data->global = is_global;
+    data->name = (char *)data + sizeof(*data);
+    strcpy(data->name, name);
+
+    id = XtAppAddTimeOut(XtWidgetToApplicationContext(window->shell), timeout,
+            macroTimerProc, data);
+
+    if (id != (XtIntervalId)(int)id) {
+        XtRemoveTimeOut(id);
+        XtFree((char *)data);
+        *errMsg = "internal error: can't convert from XtIntervalId to int";
+        return False;
+    }
+
+    result->tag = INT_TAG;
+    result->val.n = id;
+
+    return True;
+}
+
+static int timerRemoveMS(WindowInfo *window, DataValue *argList,
+        int nArgs, DataValue *result, char **errMsg)
+{
+    int timerid;
+    XtIntervalId id;
+
+    if (nArgs > 1) {
+        *errMsg = "subroutine %s called with too many arguments";
+        return False;
+    }
+    if (nArgs < 1) {
+        *errMsg = "subroutine %s called with too few arguments";
+        return False;
+    }
+    if (!readIntArg(argList[0], &timerid, errMsg)) {
+        return False;
+    }
+
+    id = timerid;
+
+    XtRemoveTimeOut(id);
+
+    return True;
+}
+
 /* T Balinski */
 static int listDialogMS(WindowInfo *window, DataValue *argList, int nArgs,
       DataValue *result, char **errMsg)
 {
     macroCmdInfo *cmdData;

-- 



More information about the Develop mailing list