% -*- mode:SLang; mode:fold; -*- % % file: fsearch.sl v1.0 % Author: Marko Mahnič, % % File grepping utility % % Uses the built-in function search_file to search text in a single file. % % Uses filefinder.sl to search for files % Uses some additional macros from mmutil.sl: two_windows, get_char % Grep Mode %{{{ static variable FileTag = "File"; static variable Line_Mark; static define update_grep_hook () { Line_Mark = create_line_mark (color_number ("menu_selection")); } static variable File_Delimiter_Mark; $1 = "GrepResults"; !if (keymap_p ($1)) make_keymap ($1); definekey ("grep_display_line", " ", $1); definekey ("grep_display_line_onewindow", "\r", $1); static define grep_mode () { variable kmap = "GrepResults"; set_mode(kmap, 2); use_keymap(kmap); } static define grep_goto_selected_line () { variable line = 0, file = "", pat; variable SearchFileTag = "^" + FileTag; bol (); push_spot(); % if the line begins with the line number, read it if (re_looking_at ("[ 0-9]*:")) { push_mark (); () = fsearch (":"); line = integer (bufsubstr ()); } else { % no line number --> the line itself will be a search pattren line = -1; skip_chars (" \t"); push_mark (); eol (); pat = bufsubstr(); } eol (); % search backward for the line containing the filename !if (re_bsearch (SearchFileTag)) { pop_spot(); return; } % extract the filename and read in the file fsearch (" "); go_right (1); push_mark (); eol (); file = bufsubstr(); pop_spot(); otherwindow(); find_file (file); % jump to the specified line if (line > 0) goto_line (line); else { % if the line number was not read previously, find the line with fsearch eol(); !if (fsearch (pat)) { bob (); () = fsearch (pat); } bol(); } } define grep_display_line_onewindow () { grep_goto_selected_line (); call ("one_window"); } define grep_display_line () { grep_goto_selected_line (); pop2buf ("*grep*"); } define grep_view_results () { Line_Mark = NULL; File_Delimiter_Mark = NULL; two_windows (SCREEN_HEIGHT / 2); otherwindow(); pop2buf ("*grep*"); % 20000228: Mark lines with filenames variable count = 0; variable SearchFileTag = "^" + FileTag; push_spot (); bob (); while (re_fsearch (SearchFileTag)) { eol (); count++; } bob (); if (count > 0) { File_Delimiter_Mark = Mark_Type[count]; count = 0; while (re_fsearch (SearchFileTag)) { File_Delimiter_Mark[count] = create_line_mark (color_number ("error")); eol (); count++; } } pop_spot (); grep_mode(); set_buffer_hook ("update_hook", &update_grep_hook); set_buffer_modified_flag (0); set_readonly (1); } %}}} %% 01 Regular %% 02 Case sensitive %% 04 Subdirectories %% 08 Whole words (used with grep.exe) static variable grep_opt_Regexp = 1; static variable grep_opt_Case = 2; static variable grep_opt_Subdir = 4; static variable grep_opt_Word = 8; static variable grep_options = grep_opt_Regexp | grep_opt_Subdir; static variable grep_textspec = ""; static variable grep_filespec = "*.*"; define grep_options_2_str () { variable optstr = ""; !if ((grep_options & grep_opt_Regexp) == 0) optstr = strcat (optstr, "Regular "); !if ((grep_options & grep_opt_Case) == 0) optstr = strcat (optstr, "Case "); !if ((grep_options & grep_opt_Subdir) == 0) optstr = strcat (optstr, "Subdirs "); !if ((grep_options & grep_opt_Word) == 0) optstr = strcat (optstr, "Word "); return (optstr); } static define grep_read_options () { variable ch = '\x00'; grep_textspec = read_mini ("Search regexp:", grep_textspec, Null_String); grep_filespec = read_mini ("In files:", grep_filespec, Null_String); while (not (ch == '\r')) { ch = get_char ("Options [RCSW]: " + grep_options_2_str(), "\rrcsw"); switch (ch) {case 'r': grep_options = grep_options xor grep_opt_Regexp; } {case 'c': grep_options = grep_options xor grep_opt_Case; } {case 's': grep_options = grep_options xor grep_opt_Subdir; } {case 'w': grep_options = grep_options xor grep_opt_Word; } } } #iffalse %{{{ Borland GREP and RGREP support define grep_options_2_opt () { %% Borland GREP %% variable optstr = "-n+o-c-v-r-i+d-w-"; %% !if ((grep_options & grep_opt_Regexp) == 0) optstr = strcat (optstr, "r+"); %% !if ((grep_options & grep_opt_Case) == 0) optstr = strcat (optstr, "i-"); %% !if ((grep_options & grep_opt_Subdir) == 0) optstr = strcat (optstr, "d+"); %% !if ((grep_options & grep_opt_Word) == 0) optstr = strcat (optstr, "w+"); %% (JED) RGREP %% RGREP with additional parameter L. When L is used the output looks like: %% File D:\Dir\file.sl %% 123: define create_file() %% 456: define delete_file() %% variable optstr = "-L -n"; !if ((grep_options & grep_opt_Regexp) == 0) optstr = strcat (optstr, ""); if ((grep_options & grep_opt_Case) == 0) optstr = strcat (optstr, " -i"); !if ((grep_options & grep_opt_Subdir) == 0) optstr = strcat (optstr, " -r"); !if ((grep_options & grep_opt_Word) == 0) optstr = strcat (optstr, ""); return (optstr); } define rgrep_grep_text () { variable cmd; variable dir, file, flags, name, buf; buf = whatbuf (); grep_read_options (); cmd = sprintf ("%s\\bin\\rgrep.exe %s \"%s\" %s", getenv ("JED_ROOT"), grep_options_2_opt (), grep_textspec, grep_filespec); shell_perform_cmd (cmd); message (cmd); setbuf ("*grep*"); set_readonly (0); erase_buffer (); setbuf ("*shell-output*"); bob (); push_mark (); eob (); copy_region ("*grep*"); set_buffer_modified_flag (0); setbuf ("*grep*"); delbuf ("*shell-output*"); while (re_fsearch ("^File STDIN:")) { delete_line (); go_up_1 (); bol (); if (looking_at ("-File")) del (); } bob (); while (re_fsearch ("^-File")) delete_line (); bob (); set_buffer_modified_flag (0); set_readonly (1); onewindow(); sw2buf (buf); grep_view_results(); } %}}} #endif % \usage{Void jed_grep_text ()} define jed_grep_text () { FileTag = "File"; variable n, textspec; variable dir, file, flags, name, buf; (file, dir, buf,) = getbuf_info(); grep_read_options (); setbuf ("*grep*"); set_readonly (0); erase_buffer (); if ((grep_options & grep_opt_Regexp) == 0) textspec = str_quote_string (grep_textspec, "\\^$[]*.+?", '\\'); else textspec = grep_textspec; variable Masks = fixup_filemasks (grep_filespec); variable FSRec = file_find_start (dir, Masks, grep_options & grep_opt_Subdir); file = file_find_next(FSRec); while (file != Null_String) { flush (file); n = search_file (file, textspec, 512); if (n > 0) { vinsert ( "%s %s\n", FileTag, file, n); while (n > 0) { _stk_roll(-n); name = (); insert (" " + strtrim (name) + "\n"); n--; } } file = file_find_next(FSRec); } bob(); set_buffer_modified_flag (0); set_readonly (1); onewindow(); sw2buf (buf); grep_view_results(); } % \usage{Void grep_text ()} define grep_text () { jed_grep_text(); } % \usage{Void grep_word_count ()} define grep_word_count () { variable textspec; variable dir, file, buf; (file, dir, buf,) = getbuf_info(); variable Words = Assoc_Type[Integer_Type, 0]; grep_read_options (); if ((grep_options & grep_opt_Regexp) == 0) textspec = str_quote_string (grep_textspec, "\\^$[]*.+?", '\\'); else textspec = grep_textspec; setbuf ("*grep*"); set_readonly (0); erase_buffer (); variable Masks = fixup_filemasks (grep_filespec); variable FSRec = file_find_start (dir, Masks, grep_options & grep_opt_Subdir); variable found; file = file_find_next(FSRec); while (file != Null_String) { flush (file); if (insert_file (file) > 0) { bob (); variable len = re_fsearch (textspec); while (len > 0) { push_mark(); if (len > 1) { go_right (len - 1); found = bufsubstr(); Words[found] = Words[found] + 1; } else go_right (1); len = re_fsearch (textspec); } erase_buffer(); } file = file_find_next(FSRec); } setbuf ("*grep*"); erase_buffer(); variable key; foreach (Words) using ("keys") { key = (); vinsert ("%8d\t%s\n", Words[key], key); } bob (); onewindow(); sw2buf (buf); grep_view_results(); }