NEDIT_require_macro_file("DEBUG.nm") # ============================================================================== # MakeMacroCallTipsDbg.nm # # You need the variable $NEDIT_MACRO_DIR set up to run this macro; it is set # up by NEDIT_LOADED.nm macros, if you use them; otherwise, set it here. # ============================================================================== # $NEDIT_MACRO_DIR = "/home/username/.nedit/macros" # ------------------------------------------------------------------------------ # MakeMacroCallTipsDbg(macro_dir): scans the directory identified by macro_dir # (defaulting to the value of $NEDIT_MACRO_DIR) for files matching *.nm. # Each of these files is scanned for macro function definitions (of form # "define functionName") and any comments preceding these. The function # names and comments are assembled into a calltip-file formatted string, # which is returned. # # NB You need to write the returned string to a file or insert it into a # document to keep your results. # # This is the debugging version of MakeMacroCallTips.nm. # ------------------------------------------------------------------------------ define MakeMacroCallTipsDbg { bar = "----------------------------------------" \ "----------------------------------------" top = "ŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻ" \ "ŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻ" if ($n_args > 0) macro_dir = $1 else macro_dir = $NEDIT_MACRO_DIR filesStr = shell_command("ls " macro_dir "/*.nm", "") if ($shell_cmd_status != 0) { dialog("Failed to execute shell command\n\n" \ " ls " macro_dir "/*.nm\n\n" \ "Output: " filesStr) return "" } files = split(filesStr, "\n") Define = "^\\s*define (\\w+)" Comment = "^\\s*#(.*)" NonComment = "^\\s*\\S.*" BlankLine = "^\\s*$" calltips = "* language *\nNEdit Macro\n\n" DEBUG_clear_all() DEBUG_filepos = DEBUG_mark_to() file_fns = "" file_fns_head = "" file_globals = "" shortfile = "" for (i = 0; i < files[]; i++) { filename = files[i] if (filename == "") continue file = read_file(files[i]) pos = 0 beg = 0 end = 0 # last file's function list DEBUG_undo_to(DEBUG_filepos) if (file_fns != "") { calltips = calltips shortfile "\n" \ file_fns_head file_fns file_globals "\n" DEBUG("***File resume: " shortfile "\n" file_fns_head file_fns "\n") } DEBUG("***Scanning file: " filename "\n") DEBUG_filepos = DEBUG_mark_to() DEBUG("\n") DEBUG_funcpos = DEBUG_mark_to() # get this file's basename and empty out the function list shortfile = replace_in_string(filename, ".*/", "", "regex","copy") file_fns = "" file_fns_head = "Functions in file " shortfile ":\n" top "\n" file_globals = "" # start with a comment line for the file calltips = calltips "* comment *\n" \ shortfile " " \ substring(bar, 0, -(length(shortfile) + 1)) \ "\n\n" blank = 0 fn_comment = "" # loop over lines for (pos = search_string(file, "\n", 0); \ pos != -1; \ pos = search_string(file, "\n", end, "regex"), beg = end) { end = $search_end DEBUG_undo_to(DEBUG_funcpos) DEBUG("Line:\n" DEBUG_s_nl(substring(file, beg, end))) if (beg == search_string(file, Comment, beg, "regex")) { # line is a comment # forget previous comments if required if (blank) { if (fn_comment != "") DEBUG("---Dropped comments:\n" fn_comment) fn_comment = "" blank = 0 } c = replace_in_string(substring(file, beg, pos), \ Comment, "\\1", "regex") # ignore comment bars (either all -'s and ='s) if (search_string(c, "^\\s*(-+|=+)\\s*$", 0, "regex") != 0) { # replace single leading space c = replace_in_string(c, "^ (?=\\S)", "", "regex", "copy") # "fake" blank lines if (search_string(c, BlankLine, 0, "regex") == 0) c = "\b" fn_comment = fn_comment c "\n" DEBUG("\n+++Comment lines:\n" fn_comment) } } else if (beg == search_string(file, Define, beg, "regex")) { # line is a define function fn = replace_in_string(substring(file, beg, pos), \ Define, "\\1", "regex") if (fn_comment == "") fn_comment = fn "(???)\n" # assemble calltip for this function ct = fn "\n" fn_comment \ "\b\n(requires macro file " shortfile ")\n\n" # remove repeated single "\b"s ct = replace_in_string(ct, "(\n\b(?=\n)){2,}", "\n\b", "regex", "copy") # try to pick up function name from the comment for file function list if (search_string(fn_comment, \ "(?n" fn "\\s*\\([^)]*\\))", 0, "regex") >= 0) { proto = replace_in_string(fn_comment, \ "(?n.*?(" fn "\\s*\\([^)]*\\)).*)", \ "\\1", "regex", "copy") proto = replace_in_string(proto, "[\\n \t]+", " ", "regex", "copy") file_fns = file_fns proto "\n" } fn_comment = "" DEBUG("===Calltip entry:\n" ct) calltips = calltips ct } else # every other line breaks a comment block { blank = 1 # but save it if it contains a "Globals" comment # (first line must contain a single word "Globals") # remove punctuation, reduce spaces glob = replace_in_string(fn_comment, "\\W+", " ", "regex", "copy") # cut off lines following the first glob = replace_in_string(glob, "(?n\n.*)", "", "regex", "copy") # trim spaces from start/end of line, convert to upper case glob = replace_in_string(glob, \ "^\\s*((?:(?!\\s+$).)+)\\s*$", "\\U\\1", \ "regex", "copy") if (glob == "GLOBALS") { e_glob = search_string(fn_comment, "\n", 0) glob = substring(fn_comment, e_glob + 1) if (glob != "") { if (file_globals == "") file_globals = top "\nGlobals:\n" file_globals = file_globals "\b\n" glob } fn_comment = "" } } } } DEBUG_undo_to(DEBUG_filepos) # print the last file's function list if (file_fns != "") { calltips = calltips shortfile "\n" file_fns_head file_fns file_globals DEBUG("***File resume: " shortfile "\n" file_fns_head file_fns "\n") } return calltips }