#include "eel.h" /* * This file implements two commands: M-x grep and M-x project-grep. * It assumes the existence of a Unix compatible (GnU compatible) * grep in your path. * * M-x grep searches for a pattern in the current directory * M-x project-grep searches a series of directories. To use project-grep * you must set up the variables * project_directory - the root of the project * project_directories - list of form ".;foo;d:\baz", assuming * that project-directory is c:\ above, the list means * c:\, c:\foo, d:\baz. * * Successive matches of both commands can be visited as they are found * with the command next-error (C-x C-n) * * Enjoy, Shane Hartman */ char grep_regex[40] = ""; char grep_files[80] = "*.h *.c"; char project_directories[256] = ""; char project_directory[FNAMELEN] = ""; do_grep (files, last, fold) char *files; short last; { short result; char cmd[80]; char *caseflag; char *obufname = bufname; if (!fold) caseflag = "-i"; else caseflag = ""; sprintf(cmd, "grep %s -n %s %s", caseflag, grep_regex, files); if (!another) { zap("process"); if (concur_shell("", "")) { error("Couldn't exec: error %d", errno); } } to_buffer("process"); process_mode(); point = size(); bprintf("\n%s\n", cmd); if (last) bprintf("\nexit\n"); to_buffer(obufname); } command grep() { char cmd[80]; get_strdef (cmd, "Search for", grep_regex); if (!*cmd) quick_abort(); strcpy(grep_regex, cmd); get_strdef(cmd, "in files", grep_files); if (!*cmd) quick_abort(); strcpy(grep_files, cmd); do_grep(grep_files, 1, has_arg ? !case_fold : case_fold); } pathname_as_directory(spec) char *spec; { int i = strlen (spec); if (i && spec[i - 1] != '\\') { spec[i] = '\\'; spec[i + 1] = 0; } } char *strchr (s, c) char *s; char c; { while (*s) { if (*s == c) return (s); s++; } return (0); } command project_grep() { char cmd[80]; char str[sizeof (project_directories)]; char spec[80]; char *s = str; char *f; char *ss = s; char *fs; int i; iter = 0; get_strdef(cmd, "Search for", grep_regex); if (!*cmd) quick_abort(); strcpy(grep_regex, cmd); get_strdef (cmd, "in files", grep_files); if (!*cmd) quick_abort (); strcpy (grep_files, cmd); strcpy (s, project_directories); while (s) { ss = strchr (s, (char) ';'); if (ss) *ss++ = 0; if (strcmp (s, ".") == 0) { strcpy (spec, project_directory); } else { strcpy (spec, s); absolute (spec); if (strcmp (spec, s) != 0) { strcpy (spec, project_directory); pathname_as_directory (spec); strcat (spec, s); } } pathname_as_directory (spec); f = grep_files; while (f != NULL) { strcpy (cmd, spec); for (i = 0; f[i] != 0 && f[i] != ' '; i++) ; if (i > 0) { char save = f[i]; f[i] = 0; strcat (cmd, f); f[i] = save; for (; f[i] == ' '; i++) ; if (f[i] == 0) f = NULL; else f += i; } do_grep (cmd, (short) !ss && !f, has_arg ? !case_fold : case_fold); } s = ss; } }