NEDIT_require_macro_file("extensions.nm") NEDIT_require_macro_file("RSextensions.nm") # ============================================================================== # goThereGrep_bgIsDark(): return true if the background color is dark. (Only # works if syntax highlighting is on.) # ============================================================================== define goThereGrep_bgIsDark { style = get_style_at_pos(0) if ("back_rgb" in style) { rgb = style["back_rgb"] r = hex_to_int(substring(rgb, 1, 3)) g = hex_to_int(substring(rgb, 3, 5)) b = hex_to_int(substring(rgb, 5, 7)) return (r < 192 && g < 192 && b < 220 && (r + g + b) < (3 * 128)) } # presume it is light return 0 } # ============================================================================== # goThereGrep(): attempt to move to a line in a file specified by the current # line in "grep -n" format, ie ::content. It colors # the current line (and subsequent/previous lines referencing the same # destination), then attempts to open the file mentioned and go to the # line number. If this works, and if the content part of the current line # (combined with the content parts of subsequent/previous lines with the # same reference) does not match the destination, a calltip is produced # for the newly opened/raised file document, with the reference content. # (This is particularly useful for GNU compiler output: the messages are # in "grep -n" format, so this can open a faulty source file and display # the error message where the fault was found.) # ============================================================================== define goThereGrep { $gothere[""] = 0 if ("grep -n" in $gothere) { kill_calltip($gothere["grep -n"]) delete $gothere["grep -n"] } l = 0 f = "" m = "" if ($selection_start != -1) deselect_all() cursor = $cursor begin = start_of_line_pos() end = end_of_line_pos() s = get_range(begin, end) # look for "grep -n" format, ie :: pos = search_string(s, ":\\s*[0-9]+:", 0, "regex") endpos = $search_end # extract pieces of the line if (pos != -1) { l = substring(s, pos + 1, endpos - 1) # line number f = substring(s, 0, pos) # file name m = substring(s, endpos) if (search_string(f, "[:\\s]", 0, "regex") != -1) { # file name contains spaces or colons: # choose whether to take only last part ef = replace_in_string(f, ".*[:\\s]([^:\\s]+)\\s*$", "\\1", "regex") f = list_dialog("Filename portion contains spaces or colons\n" \ "Which do you mean?", ef "\n" f) } } # look for contiguous lines with the same file:line: prefix if (pos != -1) { top = begin bottom = end + 1 matchfront = substring(s, 0, endpos) # search back line-by-line for (eol = begin - 1; eol >= 0; eol = start_of_line_pos(eol) - 1) { sol = start_of_line_pos(eol) if (sol != search(matchfront, sol, "case")) break # does not match our prefix top = sol txtpos = $search_end m = get_range(txtpos, eol + 1) m } # search forward line-by-line for (sol = bottom; sol < $text_length; sol = end_of_line_pos(sol) + 1) { if (sol != search(matchfront, sol, "case")) break # does not match our prefix txtpos = $search_end eol = end_of_line_pos(sol) m = m "\n" get_range(txtpos, eol) bottom = eol + 1 } m = break_lines_over(150, m) if (goThereGrep_bgIsDark()) { contigColor = "#00407f" selectColor = "#00609F" } else { contigColor = "#80c0df" selectColor = "#a0e0FF" } # color the selection found for contiguous lines Y = rangeset_fetch("selected contiguous lines", "color", contigColor) rangeset_clear(Y) rangeset_add(Y, top, bottom) # color the selection found for the current line Z = rangeset_fetch("selected line", "color", selectColor) rangeset_clear(Z) rangeset_add(Z, begin, end) } f = NEDIT_remove_dotdot(f) if (l != 0 && f != "") { if (substring(f, 0, 1) != "/") # not an absolute path? { # try to find one which works f_ok = "" flast = "" path = $file_path if (path == "") { path = replace_in_string(shell_command("pwd", ""), "\n", "") path = path "/" } ftest = NEDIT_reduce_filename(path f) fcontent = read_file(ftest) if ($read_status) f_ok = f_ok ftest "\n" flast = ftest ftest = NEDIT_reduce_filename($NEDIT_START_DIR "/" f) if (flast != ftest) { fcontent = read_file(ftest) if ($read_status) f_ok = f_ok ftest "\n" f_ok = substring(f_ok, 0, -1) if (search_string(f_ok, "\n", 0) != -1) f = list_dialog("Alternatives are", f_ok) else if (f_ok != "") f = f_ok } else f = flast } } if (l != 0 && f != "") { fcontent = read_file(f) if (!$read_status) { # see if we don't have a file of that name in the current directory or # below basefile = replace_in_string(f, "^.*/", "", "regex", "copy") list = shell_command("find " $file_path " -name " basefile " -print", "") # filter results for the correct file name filefilter = "^(?!/.*(?<=/)" quote_literal_as_regex(basefile) ").*\n" list = replace_in_string(list, filefilter, "", "regex", "copy") if ($shell_cmd_status == 0 && list != "") { dialog_msg = "Could not find file:\n " file dialog_msg = dialog_msg "\nSelect one of the following (if correct):" f = list_dialog(dialog_msg, list, "OK", "Cancel") if (f != "" && $list_dialog_button == 1) fcontent = read_file(f) else return } } if ($read_status) { open(f) focus_window(f) raise_window(f) goto_line_number(l) if (m != "" && (m "\n") != get_selection()) $gothere["grep -n"] = calltip(m, -1, "tipText") } else { dialog("Could not open file for reading:\n" f) } } else { dialog("No file name found") } }