Hi, all:
I'd like to search for special regexp on a directory list and collect the results to a buffer, I write the following code:
(defun stringlist-to-string (list connector)
"translate string list to string , cons using connector"
(let ((result ""))
(mapcar (lambda (string)
(setq result (concat result string connector))
) list)
(substring result 0 -1)))
(defun def-in-dir (dir filename-exp regexp)
"find regexp in source directory"
(interactive)
(let ((ldir (if (not (string-equal (substring dir -1) "/"))
(concat dir "/")
dir))
command
files)
(if (file-exists-p ldir)
(progn
(setq default-directory ldir)
(setq files (directory-files
default-directory
nil filename-exp))
(if files
(progn
(setq command
(concat "egrep -nH -e '" regexp "' " (stringlist-to-string files " ")))
(grep command)))))))
(defun ygrep (regexp filename-exp dir-list)
"search special files in the directory list for matching regexp."
(interactive
(let* ((regexp (read-from-minibuffer "to search (in grep format): "))
(filename-exp (read-from-minibuffer "file expression (in emacs regexp format): "))
(dir-list (read-minibuffer "directory-list: "))
)
(list regexp filename-exp (symbol-value dir-list))))
(let ((result-buffer
(progn
(if (get-buffer "*grep result*")
(kill-buffer "*grep result*"))
(get-buffer-create "*grep result*")))
(total-result 0))
(switch-to-buffer result-buffer)
(compilation-mode)
(if buffer-read-only
(toggle-read-only))
(dolist (dir dir-list)
(def-in-dir dir filename-exp regexp)
(let ((grep-process (get-buffer-process (get-buffer "*grep*"))))
(while (and
grep-process
(not (equal (process-status grep-process) 'exit)))
(sleep-for 0 100)))
(switch-to-buffer "*grep*")
(goto-char 1)
(if (not (string-match "no matches" (buffer-string)))
(while (not (equal (point) (point-max)))
(let ((line (buffer-substring (line-beginning-position) (line-end-position))))
(if (string-match "\\(^.*\\)\\(:[0-9]*:\\)\\(.*\\)" line)
(if (file-exists-p (match-string 1 line))
(save-excursion
(switch-to-buffer result-buffer)
(setq total-result (+ total-result 1))
(insert (concat "\n"
(if (not (string-equal (substring dir -1) "/"))
(concat dir "/")
dir)
(match-string 1 line)
(match-string 2 line)
;;(format " [%d] " total-result) //for debug
(match-string 3 line)))
(goto-char (point-max))
))))
(switch-to-buffer "*grep*")
(forward-line)))
(kill-buffer "*grep*"))
(switch-to-buffer result-buffer)
(goto-char 1)
(insert (format "Grep search for regexp %s in file %s\n" regexp filename-exp))
(if (equal total-result 0)
(insert (format "Sorry no matches found! "))
(insert (format "%d matches found! " total-result)))
(toggle-read-only)
))
but, everytime I run:
(ygrep "(start|stop)-process" "\\.el$" load-path)
in the *scratch* buffer, I get a different result.
What's wrong??
Thanks
--
YuGang