NEDIT_require_macro_file("array_utils.nm") # ============================================================================== # SaveAllFiles(): writes all modified documents to their files. # ============================================================================== define SaveAllFiles { current = $file_path $file_name filename = focus_window("last") while (filename != "") { save_it = $modified if ($modified) { if (search_string($file_name, "^Untitled(?:_\\d+)?$", 0, "regex") == 0) { if (read_file($file_name) == "" && $read_status == 0) { # this file doesn't exist and should not (?) be saved save_it = 0 } } } if (save_it) { raise_window() save() } filename = focus_window("next") } focus_window(current) raise_window() } # ============================================================================== # SaveAllFilesConfirm(): lists all modified files (excluding Untitled's) in # a list box, asking for confirmation about writing out. # ============================================================================== define SaveAllFilesConfirm { current = $file_path $file_name filename = focus_window("last") list = "" sep = "" while (filename != "") { save_it = $modified if ($modified) { if (search_string($file_name, "^Untitled(?:_\\d+)?$", 0, "regex") == 0) { if (read_file($file_name) == "" && $read_status == 0) { # this file doesn't exist and should not (?) be saved save_it = 0 } } } if (save_it) { list = list sep $file_path $file_name sep = "\n" } filename = focus_window("next") } focus_window(current) raise_window() if (list == "") return # right: now we save what? sellist = list_multisel_dialog("Select files to save or not to save", list, \ "Save except these", "Save only these", \ "Cancel") btn = $list_dialog_button if (btn < 1 || 2 < btn) return a = split(sellist, "\n") for (i = 0; i in a; ++i) selected[a[i]] = i a = split(list, "\n") for (i = 0; i in a; ++i) allfiles[a[i]] = i files = $empty_array if (btn == 1) # all except these files = allfiles - selected else files = selected # now cycle through files list = "" sep = "" for (file in files) { if (focus_window(file) == file) { save() list = list sep file sep = "\n" } } if (list != "") list_dialog("The following "files[]" files were saved", list, "Dismiss") else dialog("No files were saved", "Dismiss") focus_window(current) raise_window() } # ============================================================================== # TidyFile(): if the current file requires tidying (removing CRs, TABs or SPACEs # from line ends), this is done. Returns true if tidied. # ============================================================================== define TidyFile { tidied = 0 if (!$read_only) { for (pos = search("[ \t\r]+$", 0, "regex"); \ pos >= 0; \ pos = search("[ \t\r]+$", pos + 1, "regex")) { replace_range(pos, $search_end, "") tidied = 1 } } return tidied } # ============================================================================== # SaveCurrFileAsFormat(fmt, tidy): if the current file is modified or requires # tidying (removing CRs, TABs or SPACEs from line ends, when tidy is # true), saves it, after perhaps tidying it first. If the target format # fmt is not that of the file, the disk version is then read and the # appropriate change made to the line endings in that string. # ============================================================================== define SaveCurrFileAsFormat { dest["unix"] = "\n" dest["dos"] = "\r\n" dest["macintosh"] = "\r" fmt = $1 oldfmt = $file_format change_format = oldfmt != fmt valid = (fmt in dest) error = "" if (!valid) error = "INVALID NEW FORMAT '" fmt "'" if ($read_only) { error = "READ-ONLY " error } if (change_format && valid && !$read_only) { content = replace_in_string(get_range(0, $text_length), \ "\n", dest[fmt], "case", "copy") write_file(content, $file_path $file_name) revert_to_saved() return oldfmt " -> " fmt } else if ($modified && !$read_only) { save() return "saved " error } return error } # ============================================================================== # SaveAllFilesAsFormat(fmt, tidy): writes all modified documents to their files # using the format fmt. It tidy is true, trailing spaces and tabs are # removed. fmt must be one of "unix", "macintosh" or "dos". # ============================================================================== define SaveAllFilesAsFormat { fmt = $1 tidy = $2 res = $empty_array current = $file_path $file_name filename = focus_window("last") maxname = filename while (filename != "") { done = "" if (tidy && TidyFile()) done = done "tidied " done = done SaveCurrFileAsFormat(fmt) res[filename] = done filename = focus_window("next") if (length(filename) > length(maxname)) maxname = filename } focus_window(current) raise_window() maxname = replace_in_string(maxname, ".", " ", "regex") " -> " list = "" for (filename in res) { if (res[filename] == "") res[filename] = "untouched" list = list filename \ substring(maxname, length(filename)) \ res[filename] "\n" } list_dialog("Handled the following files", list) }