When a script grows a bit in size and has more than a few parameters, it becomes essential for usability that those parameters are listed in an ergonomic order on the script settings menu.
CHDK always lists @param a first, then b and z last on the script menu. Moving parameters up and down the list in a finished script to give it a finishing touch (or when using it as basis for a new script) is tedious as it requires manual search & replace of all involved single character variables. This is a near-certain way for introducing a bug or two.
So, I went ahead and patched CHDK a bit so that the script settings menu shows parameters in the order they are listed in the script instead of sorting them in alphabetical order. So, if a user wants to move his favorite parameters to the top, all that needs to be done is to move the @param lines around a bit.
Obviously this affects current scripts, but I don't consider this a break of backwards compatibility since the scripts and parameters work exactly as they did before, they just appear in a different order on the menu AND ONLY IF the script author didn't list them in alphabetical order for one reason or another. Actually my MDFB is one of the affected scripts...when I first wrote it I thought the parameters would move around on the list at will but I later noticed they didn't and just gave up.
These diffs are against allbest trunk-436. I haven't taken a look at the juciphox tree to see if this affects Lua, which I believe doesn't suffer from the single letter variable disease of uBasic.
Modified files and functions are:
core/script.c: script_scan(), process_param()
include/script.h
core/gui.c: gui_update_script_submenu()
--- core/script.c-trunk436 2008-07-06 20:41:08.000000000 +0300
+++ core/script.c 2008-07-06 21:00:16.000000000 +0300
@@ -72,6 +72,7 @@
char script_title[36];
char script_params[SCRIPT_NUM_PARAMS][28];
+int script_param_order[SCRIPT_NUM_PARAMS];
char script_console_buf[SCRIPT_CONSOLE_NUM_LINES][SCRIPT_CONSOLE_LINE_LENGTH+1];
static int script_console_lines=0;
//-------------------------------------------------------------------
@@ -88,7 +89,7 @@
}
//-------------------------------------------------------------------
-static void process_param(const char *param) {
+static int process_param(const char *param) {
register const char *ptr = param;
register int n, i=0;
@@ -102,7 +103,9 @@
++i;
}
script_params[n][i]=0;
- } // ??? else produce error message
+ n++;
+ } else n=0; // ??? else produce error message
+ return n; // n=1 if '@param a' was processed, n=2 for 'b' ... n=26 for 'z'. n=0 if failed.
}
//-------------------------------------------------------------------
@@ -121,7 +124,7 @@
//-------------------------------------------------------------------
static void script_scan(const char *fn, int update_vars) {
register const char *ptr = state_ubasic_script;
- register int i;
+ register int i,j=0,n;
char *c;
c=strrchr(fn, '/');
@@ -129,6 +132,7 @@
script_title[sizeof(script_title)-1]=0;
for (i=0; i<SCRIPT_NUM_PARAMS; ++i) {
script_params[i][0]=0;
+ script_param_order[i]=0;
}
while (ptr[0]) {
@@ -139,7 +143,11 @@
process_title(ptr);
} else if (strncmp("@param", ptr, 6)==0) {
ptr+=6;
- process_param(ptr);
+ n=process_param(ptr); // n=1 if '@param a' was processed, n=2 for 'b' ... n=26 for 'z'. n=0 if failed.
+ if (n>0 && n<SCRIPT_NUM_PARAMS) {
+ script_param_order[j]=n;
+ j++;
+ }
} else if (update_vars && strncmp("@default", ptr, 8)==0) {
ptr+=8;
process_default(ptr);
--- include/script.h-trunk436 2008-07-06 20:41:22.000000000 +0300
+++ include/script.h 2008-07-06 20:43:40.000000000 +0300
@@ -10,6 +10,7 @@
extern char script_title[36];
extern char script_params[SCRIPT_NUM_PARAMS][28];
+extern int script_param_order[SCRIPT_NUM_PARAMS];
//-------------------------------------------------------------------
extern void script_load(const char *fn);
--- core/gui.c-trunk436 2008-07-06 20:41:00.000000000 +0300
+++ core/gui.c 2008-07-06 20:45:04.000000000 +0300
@@ -1341,10 +1341,10 @@
script_submenu_items[p]=script_submenu_items_top[i];
}
for (i=0; i<SCRIPT_NUM_PARAMS; ++i) {
- if (script_params[i][0]) {
- script_submenu_items[p].text=(int)script_params[i];
+ if (script_param_order[i]) {
+ script_submenu_items[p].text=(int)script_params[script_param_order[i]-1];
script_submenu_items[p].type=MENUITEM_INT;
- script_submenu_items[p].value=&conf.ubasic_vars[i];
+ script_submenu_items[p].value=&conf.ubasic_vars[script_param_order[i]-1];
++p;
}
}