NEDIT_require_macro_file("extensions.nm") # $backlights[win]["groupName"]["charRanges"] = "0-8,11-31,127" # $backlights[win]["groupName"]["colorValue"] = "#c8e8d8" $backlights[""] = "" delete $backlights[""] $backlights_is_assigned = 0 # ============================================================================== # backlight_set_from_range(rangestring): creates a string of ascii characters # for the specified range specification. Note that character zero is # ignored. # ============================================================================== define backlight_set_from_range { set[""] = "" delete set[""] rs = $1 while (search_string(rs, "^\\d+(-\\d+)?", 0, "regex") != -1) { r = replace_in_string(rs, "^(\\d+(-\\d+)?).*", "\\1", "regex") rs = replace_in_string(rs, "^(\\d+(-\\d+)?),?(.*)", "\\3", "regex") if (search_string(r, "^\\d+-\\d+", 0, "regex") != -1) { lo = replace_in_string(r, "^(\\d+).*", "\\1", "regex") hi = replace_in_string(r, "^\\d+-(\\d+).*", "\\1", "regex") } else { lo = r hi = r } for (i = lo; i <= hi; i++) set[tochar(i)] = 1 } res = "" for (s in set) res = res s return res } # ============================================================================== # backlight_range_from_set(setstring): creates a range specification # corresponding to the string of ascii characters. Note that character # zero is ignored. # ============================================================================== define backlight_range_from_set { set[""] = "" delete set[""] for (i = 0; i < length($1); i++) set[substring($1, i, i + 1)] = 1 last = -1 res = "" for (s in set) { i = toascii(s) if (last + 1 == i) { last = i continue } if (last >= 0) res = res "-" last last = i if (res != "") res = res "," res = res i } return res } # ============================================================================== # backlight_assign(resourcestr): creates an array suitable for assignment to # $backlights from the given resource string. # ============================================================================== define backlight_assign { res[""] = "" delete res[""] groups = split($1, ";") for (i = 0; i in groups; i++) { parts = split(groups[i], ":") elem["charRanges"] = parts[0] elem["colorValue"] = parts[1] res["group" i] = elem } return res } # ============================================================================== # backlight_make_string(array): creates a resource string from a suitable array. # ============================================================================== define backlight_make_string { s = "" for (i in $1) { r = $1[i]["charRanges"] c = $1[i]["colorValue"] if (length(r) && length(c)) s = s r ":" c ";" } if (s == "") s = ";" return substring(s, 0, -1) } # ============================================================================== # backlight_use_stored_group_names(olda, newa): checks newa's group names # against the ones stored in olda by matching charRanges entries: it # renames the newa's group names then returns it. # ============================================================================== define backlight_use_stored_group_names { olda = $1 newa = $2 oldl[""] = "" delete oldl[""] newl = oldl i = 0 for (og in olda) { for (ng in newa) { if (newa[ng]["charRanges"] == olda[og]["charRanges"]) if (ng != og && !(og in newa)) { newl[i] = ng oldl[i] = og i++ } } } for (i in newl) { ng = newl[i] og = oldl[i] newa[og] = newa[ng] delete newa[ng] } return newa } # ============================================================================== # backlight_display_build_lines(array): generates a string holding lines # suitable for a list_dialog() from the backlight array passed. # ============================================================================== define backlight_display_build_lines { array = $1 res = "" glen = 0 rlen = 0 clen = 0 # measure max lengths for (g in array) { glen = max(glen, length(g)) rlen = max(rlen, length(array[g]["charRanges"])) clen = max(clen, length(array[g]["colorValue"])) } # generate a space padding string len = max(glen, rlen, clen) sp = " " while (length(sp) < len) sp = sp sp # now generate the strings for (g in array) { gs = substring(g sp, 0, glen) rs = substring(array[g]["charRanges"] sp, 0, rlen) cs = substring(array[g]["colorValue"] sp, 0, clen) res = res gs " | " rs " | " cs "\n" } return res } # ============================================================================== # backlight_display_groups(title, array, button...): presents a list of groups # held in array, adding buttons as taken from the argument list. It # returns an array holding keys "button" (number of button pressed, if # any), "group", "charRanges" and "colorValue" holding parts of the # selected line. # ============================================================================== define backlight_display_groups { title = $1 array = $2 gline = backlight_display_build_lines(array) res["button"] = 0 if ($n_args <= 2) s = list_dialog(title, gline, "Cancel") else if ($n_args == 3) s = list_dialog(title, gline, $3, "Cancel") else if ($n_args == 4) s = list_dialog(title, gline, $3, $4, "Cancel") else if ($n_args == 5) s = list_dialog(title, gline, $3, $4, $5, "Cancel") else if ($n_args == 6) s = list_dialog(title, gline, $3, $4, $5, $6, "Cancel") else if ($n_args == 7) s = list_dialog(title, gline, $3, $4, $5, $6, $7, "Cancel") else if ($n_args == 8) s = list_dialog(title, gline, $3, $4, $5, $6, $7, $8, "Cancel") else if ($n_args == 9) s = list_dialog(title, gline, $3, $4, $5, $6, $7, $8, $9) else return res if (1 <= $list_dialog_button && $list_dialog_button <= min(7, $n_args - 2)) res["button"] = $list_dialog_button if (s != "") { parts = split(s, "|") res["group"] = replace_in_string(parts[0], "\\s+$", "", "regex") res["charRanges"] = replace_in_string(parts[1], "\\s+$", "", "regex") res["colorValue"] = replace_in_string(parts[2], "\\s+$", "", "regex") } return res } # ============================================================================== # backlight_get_backlights(): assembles the backlight groups into the array # $backlights. # ============================================================================== define backlight_get_backlights { set_backlight_string() win = $file_path $file_name if (win in $backlights) { group = $backlights[win] current = backlight_assign($backlight_string) $backlights[win] = backlight_use_stored_group_names(group, current) } else $backlights[win] = backlight_assign($backlight_string) } # ============================================================================== # backlight_display_backlights(): displays the current window backlights. # ============================================================================== define backlight_display_backlights { set_backlight_string() backlight = backlight_assign($backlight_string) backlight_display_groups("Backlighting for " $file_name, backlight) } # ============================================================================== # backlight_modify_range(oldr): walks the user through a number of dialogs, # modifying the range in different ways. Returns a new range, which may be # the same as the old range. # ============================================================================== define backlight_modify_range { keep_r = $1 set = backlight_set_from_range($1) dialog("DEBUG range->set - #chars in range "$1": "length(set)) r = keep_r ok = 1 done = 0 while (ok) { dialog("DEBUG range is "r) r = backlight_range_from_set(set) dialog("DEBUG set->range - #chars in range "r": "length(set)) ret = dialog("Current range is " r, \ "Done", \ "Enter new numeric\nrange", \ "Add text\ncharacters", \ "Remove text\ncharacters", \ "Add character\ncode(s)", \ "Remove character\ncode(s)", \ "Revert") ok = (1 < ret && ret < 7) done = ret == 1 if (ret == 2) # Enter new numeric range { str = string_dialog("Old range is '" r "'\nEnter new range:") if (str == "") { if (dialog("New range is empty\nKeep old range?", "Yes", "No") != 1) r = str } else { r = str str = replace_in_string(str, "\\s+", "", "regex", "copy") if (search_string(str, "^\\d+(-\\d+)(,d+(-\\d+)*$", 0, "regex") == 0) { set = backlight_set_from_range(str) } } } else if (ret == 3) # Add text characters { dialog("Not implemented") } else if (ret == 4) # Remove text characters { dialog("Not implemented") } else if (ret == 5) # Add character code(s) { dialog("Not implemented") } else if (ret == 6) # Remove character code(s) { dialog("Not implemented") } } return keep_r } # ============================================================================== # backlight_manip_backlights(): displays the array $backlights, assembling it # if necessary. # ============================================================================== define backlight_manip_backlights { backlight_get_backlights() groups = $backlights[$file_path $file_name] old = groups r["button"] = -1 changed = "" while (r["button"]) { if (changed == "") r = backlight_display_groups(changed "Backlighting for " $file_name, \ groups, \ "Rename Group", \ "New Group", \ "Delete Group", \ "Modify Range", \ "Modify Color") else r = backlight_display_groups(changed "Backlighting for " $file_name, \ groups, \ "Rename Group", \ "New Group", \ "Delete Group", \ "Modify Range", \ "Modify Color", \ "Apply", \ "Revert") if (r["button"] == 1 && ("group" in r)) # Rename Group { s = string_dialog("Rename group '" r["group"] "' to:") s = replace_in_string(s, "^\\s*", "", "regex") s = replace_in_string(s, "\\s*$", "", "regex") if (s == "" || search_string(s, "|", 0) != -1) { dialog("New group name " s " for group '" r["group"] "' " \ "invalid:\nmust not contain bars '|' and must not be empty") } else if (s in groups) { dialog("New group name " s " for group '" r["group"] "' " \ "invalid:\nthere is already a group with that name") } else { changed = "[**CHANGED**] " groups[s] = groups[r["group"]] delete groups[r["group"]] } } else if (r["button"] == 2 && ("group" in r)) # New Group { s = string_dialog("Add new group:") s = replace_in_string(s, "^\\s*", "", "regex") s = replace_in_string(s, "\\s*$", "", "regex") if (s == "" && search_string(s, "|", 0) != -1) { dialog("New group name " s " for group '" r["group"] "' " \ "invalid:\nmust not contain bars '|' and must not be empty") } else if (s in groups) { dialog("New group name " s " for group '" r["group"] "' " \ "invalid:\nthere is already a group with that name") } else { changed = "[**CHANGED**] " newa["charRanges"] = "" newa["colorValue"] = "" groups[s] = newa } } else if (r["button"] == 3 && ("group" in r)) # Delete Group { b = dialog("Deleting group '"r["group"]"'\nAre you sure?", "Yes", "No") if (b == 1) { delete groups[r["group"]] changed = "[**CHANGED**] " } } else if (r["button"] == 4 && ("group" in r)) # Modify Range { range = backlight_modify_range(r["charRanges"]) if (range != r["charRanges"]) { groups[r["group"]]["charRanges"] = range changed = "[**CHANGED**] " } } else if (r["button"] == 5 && ("group" in r)) # Modify Color { warn = "*** COLOR NAMES ARE NOT CHECKED ***\n" s = string_dialog(warn "New color for group '"r["group"]"':") s = replace_in_string(s, "^\\s*", "", "regex") s = replace_in_string(s, "\\s*$", "", "regex") groups[r["group"]]["colorValue"] = s changed = "[**CHANGED**] " } else if (r["button"] == 6) # Apply { old = groups s = backlight_make_string(groups) #dialog("Backlight string:\nold = "$backlight_string"\nnew = "s) set_backlight_string("") set_backlight_string(s) $backlights[$file_path $file_name] = groups changed = "" } else if (r["button"] == 7) # Revert { groups = old s = backlight_make_string(groups) #dialog("Backlight string:\nold = "$backlight_string"\nnew = "s) set_backlight_string("") set_backlight_string(s) $backlights[$file_path $file_name] = groups changed = "" } } }