NEDIT_require_macro_file("extensions.nm") $REGEX_FILE = $NEDIT_HOME_DIR "/datafiles/REGEXes" $REGEX_stored = $empty_array # ============================================================================== # REGEX_open_regexes_file(): opens the regex repository file for editing. (The # file is in $NEDIT_HOME/datafiles/REGEXes.) # ============================================================================== define REGEX_open_regexes_file { open($REGEX_FILE) focus_window($REGEX_FILE) set_wrap_text("none") } # ============================================================================== # REGEX_load_regexes(): loads a file of useful regex expressions. These are # stored as \t pairs, one per line, in the file. # ============================================================================== define REGEX_load_regexes { REGEX_loaded = $empty_array s = read_file($REGEX_FILE) if (s == "") { dialog("REGEX: File\n " $REGEX_FILE "\ncould not be read", "Cancel") return 0 } lines = split(s, "\n") errors = "" spaces = replace_in_string(lines[], ".", " ", "regex") nwidth = length(spaces) good = 0 for (i = 0; i < lines[]; i++) { line = lines[i] if (line == "") continue pos = search_string(line, "\t", 0) if (pos < 1) { label = spaces "" (i + 1) label = substring(label, length(label) - nwidth, nwidth) ": " errors = errors label line "\n" continue } good++ key = substring(line, 0, pos) val = substring(line, pos + 1) REGEX_loaded[key] = val } if (errors != "") { label = "REGEX: The following lines in\n " $REGEX_FILE "\nare invalid" s = list_dialog(label, errors, "Go there", "Cancel") if ($list_dialog_button == 1) { back = $file_path $file_name open($REGEX_FILE) focus_window($REGEX_FILE) if (s != "") { i = replace_in_string(s, " *(\\d+):.*", "\\1", "regex") if (i != "") goto_line_number(i) } } } old_REGEX = $REGEX_stored - REGEX_loaded if (old_REGEX[] > 0) { lines = "" for (key in old_REGEX) { line = key "\t" old_REGEX[key] "\n" lines = lines line } list_dialog("REGEX: The following regexes were not loaded with the file", \ lines, "Keep", "Discard") if ($list_dialog_button == 1) $REGEX_stored = REGEX_loaded + old_REGEX else $REGEX_stored = REGEX_loaded } else { $REGEX_stored = REGEX_loaded } return good > 0 } # ============================================================================== # REGEX_save_regexes(): saves the array of useful regex expressions. These are # stored as \t pairs, one per line, in the file. # ============================================================================== define REGEX_save_regexes { lines = "" for (key in $REGEX_stored) { line = key "\t" $REGEX_stored[key] "\n" lines = lines line } if (!write_file(lines, $REGEX_FILE)) { r = dialog("REGEX: Could not write the stored useful regexes to file\n" \ $REGEX_FILE, "Copy to clipboard", "Cancel") if (r == 1) string_to_clipboard(lines) return 0 } return 1 } # ============================================================================== # REGEX_add(): prompts for a new regex to store, with its name. # ============================================================================== define REGEX_add { re = "" s = "" if ($REGEX_stored[] == 0) msg = "No stored regexes" else if ($REGEX_stored[] == 1) msg = "1 stored regex" else msg = $REGEX_stored[] " stored regexes" err = msg while (re == "") { # pick up other input sources altSrc = 2 alt = $empty_array but = $empty_array # selection sel = get_selection("any") if (sel != "") { s = sel if (length(s) > 100) s = substring(s, 0, 100) "..." s = replace_in_string(s, "\n", "\\n", "copy") err = err "\nSelection:\n " s "\n" alt[altSrc] = sel but[altSrc] = "From selection" altSrc++ } # clipboard clip = clipboard_to_string() if (clip != "") { s = clip if (length(s) > 100) s = substring(s, 0, 100) "..." s = replace_in_string(s, "\n", "\\n", "copy") err = err "\nClipboard:\n " s "\n" alt[altSrc] = clip but[altSrc] = "From clipboard" altSrc++ } but[altSrc] = "Cancel" err = "REGEX: " err "\nEnter regular expression string" if (altSrc == 2) s = string_dialog(err, "OK", but[2]) else if (altSrc == 3) s = string_dialog(err, "OK", but[2], but[3]) else if (altSrc == 4) s = string_dialog(err, "OK", but[2], but[3], but[4]) else { REGEX_add_undefinedNumberOfButtons() } sbutton = $string_dialog_button if (sbutton == 0 || sbutton == altSrc) return # "Cancel" or dialog closed alt[1] = s # result for the "OK" button re = alt[sbutton] err = msg "\nREGEX: no regex given" } # now we have a regex in re - ask for a name for it bad = 1 while (bad) { s = string_dialog("REGEX: Enter name for regex\n\n " re, "OK", "Cancel") sbutton = $string_dialog_button s = replace_in_string(s, "(?n\\s+)", " ", "regex", "copy") if (s == "" || sbutton != 1) { r = dialog("REGEX: No name for regex string\n\n " re "\n\nreceived", \ "Try again", "Cancel") if (r != 1) return } else if (s in $REGEX_stored) { r = dialog("REGEX: Name\n\n " s "\n\nfor regex string\n\n " re \ "\n\nalready used to identify regex\n\n " $REGEX_stored[s], \ "Overwrite Regex", "Change Name", "Cancel") if (r == 1) bad = 0 else if (r != 2) return } else bad = 0 } $REGEX_stored[s] = re REGEX_save_regexes() } # ============================================================================== # old version # ============================================================================== # define REGEX_add # { # re = "" # s = "" # # r = dialog("REGEX: No stored regexes", \ # "Create", "Copy from clipboard", "Cancel") # if (r == 1) # { # s = string_dialog("REGEX: Enter regular expression string", \ # "OK", "Cancel") # if ($string_dialog_button == 1) # re = s # } # if (r == 2) # re = clipboard_to_string() # if (re == "") # { # dialog("REGEX: No regex string received", "Cancel") # return # } # bad = 1 # while (bad) # { # s = string_dialog("REGEX: Enter name for regex\n\n " re, "OK", "Cancel") # if (s == "" || $string_dialog_button != 1) # { # r = dialog("REGEX: No name for regex string\n\n " re "\n\nreceived", \ # "Try again", "Cancel") # if (r != 1) # return # } # else if (s in $REGEX_stored) # { # r = dialog("REGEX: Name\n\n " s "\n\nfor regex string\n\n " re \ # "\n\nalready used to identify regex\n\n " $REGEX_stored[s], \ # "Overwrite Regex", "Change Name", "Cancel") # if (r == 1) # bad = 0 # else if (r != 2) # return # } # else # bad = 0 # } # # $REGEX_stored[s] = re # REGEX_save_regexes() # } # ============================================================================== # REGEX_delete(name): prompts the user whether he wants to delete the regex with # the given key. Returns an error string, which is empty if deletion was # chosen. # ============================================================================== define REGEX_delete { name = $1 if (name in $REGEX_stored) { but = dialog("REGEX: Deleting regex '" name "':\n\n " \ $REGEX_stored[name], \ "Delete", "Cancel") if (but == 1) { delete $REGEX_stored[name] REGEX_save_regexes() return "" } return "Deletion cancelled" } return "No regex named " name " found" } # ============================================================================== # REGEX_fetch(): prompts the user with the list of regex names; the buttons # indicate possible actions. # ============================================================================== define REGEX_fetch { if ($REGEX_stored[] == 0) { if (!REGEX_load_regexes()) { REGEX_add() if ($REGEX_stored[] == 0) return } } doFindInBuf = "Find It" doToClipboard = "To Clipboard" doToFile = "To File (Quoted)" doDelete = "Delete" doAddNew = "Add New" doChangeName = "Change Name" doChangeRegex = "Change Regex" i = 0 but[++i] = doFindInBuf but[++i] = doToClipboard but[++i] = doToFile but[++i] = doDelete but[++i] = doAddNew but[++i] = doChangeName but[++i] = doChangeRegex errNotImplemented = "*** NOT IMPLEMENTED! ***" errNoRegexSelected = "*** No regex selected ***" # now we should have something bad = 1 error = "" while (bad) { # ask for a regex lines = "" for (key in $REGEX_stored) { line = key "\n" lines = lines line } if (error != "") error = error "\n" key = list_dialog("REGEX: " error "Select a regex to use", lines, \ but[1], but[2], but[3], but[4], but[5], but[6], but[7], \ "Cancel") r = $list_dialog_button if (!(r in but)) { return } theRegex = "" if (key != "" && (key in $REGEX_stored)) theRegex = $REGEX_stored[key] else error = but[r] ": " errNoRegexSelected if (but[r] == doFindInBuf) { if (theRegex != "") { find(theRegex, "regex") return } } else if (but[r] == doToClipboard) { if (theRegex != "") { string_to_clipboard(theRegex) return } } else if (but[r] == doToFile) { if (theRegex != "") { insert_string(regex_to_quoted_string(theRegex)) return } } else if (but[r] == doDelete) { error = REGEX_delete(key) } else if (but[r] == doAddNew) { error = "" REGEX_add() } else { error = but[r] ": " errNotImplemented } } }