.
.   
NEdit.org
   .

Home
 - News - Download - Documentation - Search - FTP     
.
.
  

Run grep

[Back]
alltext = get_range(0, $text_length)
if ($selection_start == -1)
  selec = string_dialog("Grep word:","O.K.")
else
  selec = get_selection()

if (selec != "") {
  result = shell_command("grep -Fn \"" selec "\"", alltext)
  dialog(result)
}
This version outputs the results to another window and saves the window as a grepResults file. Each successive grep is appended to the same file as long as the window is open. This way you can keep a history of your recent greps. If you want to start a new grep log, close the window. The file will be overwritten with the next result. By having the results in a window, you can select files to open with Open Selected and you can select line numbers to jump to with Goto Selected. The format of the output is fairly readable so you know what you grep'ed for and in what files. oh yeah, this version of the macro allows you to grep in other files using wild cards. The command line options on the grep command are changed because the SGI version of grep doesn't have the paramter -F.
alltext = get_range(0, $text_length)
cur_path = $file_path
cur_file = $file_name
if ($selection_start == -1)
  selec = string_dialog("Grep word:","OK")
else
  selec = get_selection()

if (selec != "")
{
   fileSelec = string_dialog("File Selection (i.e. *.h *.c)  [CWD: " $file_path "]", "OK", "Current File Only", "Cancel")

   if(($string_dialog_button == 1) && (fileSelec != ""))
      result = shell_command("grep -n \"" selec "\" " fileSelec, "")
   else if($string_dialog_button == 2)
      result = shell_command("grep -n \"" selec "\" ", alltext)
   else if($string_dialog_button != 0)
      result = "No Files Were Selected"

   if(($string_dialog_button != 0) && ($string_dialog_button != 3))
   {
      curwin = focus_window(cur_path "grepResults")
      if(curwin == "")
      {
         new()
         focus_window("last")
      }
      set_cursor_pos($text_length)
      slash = search_string(result, "\\", 0)
      modResult = replace_substring(result, 0, slash+1, "\n")
      if($string_dialog_button == 2)
         textOut = "Grep For " selec " (in " cur_path cur_file")\n" modResult "nn"
      else
         textOut = "Grep For " selec " (in " cur_path fileSelec")\n" modResult "nn"
      replace_range($cursor, $text_length, textOut)
      set_cursor_pos($text_length)
      save_as("grepResults")
      open("grepResults")
   }
}
Alternatively, grepfiles.nm allows you to open files containing a string using dialog() buttons to select the files from those found by grep. The file is opened (if not opened already) and the search string found within it. This macro uses the Element functions macro.

You can bind a call to grepfiles() in a language specific way, selecting the file name search pattern which best suits that language. Otherwise, you can always invoke it as grepfiles("*"), if you want it to check everything out.

Sadly, since the number of dialog buttons is limited and the dialog boxes may be quite large, only 5 files are presented at a time. What would be really nice would be a list-dialog in which the whole list could go ...


This macro runs grep on the current file and replaces the file with the result:
string = string_dialog("grep for", \
          "grep", "Cancel")
  if ($string_dialog_button == 2 || $string_dialog_button == 0)
      return
  if ($selection_start == -1)
      body = get_range(0, $text_length)
  else
      body = get_selection()
  cmdOutput = shell_command("grep " string " > grep.txt", body)
  if ($shell_cmd_status != 0)
      dialog("grep command returned failed exit status\n" cmdOutput)
  else if (cmdOutput != "")
      dialog(cmdOutput)

results = read_file( "grep.txt" )
select_all()
replace_selection( results )
beginning_of_file()
shell_command( "rm grep.txt", "")
If you don't want to lose your text, this version shows the results in a new file:
separate window:
string = string_dialog("grep for", \
          "grep", "Cancel")
  if ($string_dialog_button == 2 || $string_dialog_button == 0)
      return
  if ($selection_start == -1)
      body = get_range(0, $text_length)
  else
      body = get_selection()
  cmdOutput = shell_command("grep " string " > grep.txt", body)
  if ($shell_cmd_status != 0)
      dialog("grep command returned failed exit status\n" cmdOutput)
  else if (cmdOutput != "")
      dialog(cmdOutput)

open( "grep.txt" )
[Back]

. Released on  Wed, 21 Nov 2001  by C. Denat  

  
Visit our sponsor
Check Metrolink

© NEdit.org
1999-2001
All rights reserved


.