From: Stefan Bund Date: Tue, 13 Sep 2011 07:44:17 +0000 (+0200) Subject: implement egrep.el X-Git-Url: http://g0dil.de/git?a=commitdiff_plain;h=040181659de5ad47d609178c110207614cdf507c;p=emacs-egrep.git implement egrep.el --- diff --git a/egrep.el b/egrep.el new file mode 100644 index 0000000..07fa5eb --- /dev/null +++ b/egrep.el @@ -0,0 +1,82 @@ +(defun egrep-regexps-1 (result-buffer regexp predicate args) + (let ((file-name (buffer-file-name))) + (save-excursion + (goto-char (point-min)) + (while (re-search-forward regexp nil t) + (if (or (not predicate) (apply predicate args)) + (let ((line (count-lines (point-min) (point))) + (column (current-column)) + (text (buffer-substring (save-excursion (beginning-of-line) (point)) + (save-excursion (end-of-line) (point))))) + (with-current-buffer result-buffer + (let ((inhibit-read-only t)) + (insert (format "%s:%d:%d: %s\n" + (dired-make-relative file-name default-directory) + line column text)))))))))) + +(defun egrep-prepare-result-buffer () + (let ((result-buffer (get-buffer-create "*search*"))) + (with-current-buffer result-buffer + (let ((inhibit-read-only t)) + (grep-mode) + (erase-buffer) + (insert (format "; Searching for '%s' ...\n" regexp)))) + result-buffer)) + +(defun egrep-finalize-result-buffer (result-buffer) + (pop-to-buffer result-buffer) + (goto-char (point-min))) + +(defun egrep-regexps (regexp &optional predicate &rest args) + (interactive "sRegexp: ") + (let ((result-buffer (egrep-prepare-result-buffer))) + (egrep-regexps-1 result-buffer regexp predicate args) + (egrep-finalize-result-buffer result-buffer))) + +(defun egrep-font-face-predicate (face-symbol) + (eq (get-text-property (point) 'face) face-symbol)) + +(defun egrep-regexps-with-face (regexp face-symbol) + (interactive (list (read-string "Regexp: ") + (intern (completing-read "Face symbol: " + (mapcar (lambda (face) (symbol-name face)) (face-list)) + nil t)))) + (egrep-regexps regexp 'egrep-font-face-predicate face-symbol)) + +(defun egrep-regexps-dired-marked-files (regexp &optional prepare predicate &rest args) + (interactive "sRegexp: ") + (let ((result-buffer (egrep-prepare-result-buffer))) + (loop for file-name in (dired-get-marked-files nil current-prefix-arg) + do (let ((buffer (find-file-noselect file-name))) + (with-current-buffer buffer + (if prepare (funcall prepare)) + (egrep-regexps-1 result-buffer regexp predicate args)))) + (egrep-finalize-result-buffer result-buffer))) + +(defun egrep-string-startswith (string start) + (and (> (length string) (length start)) + (string= (substring string 0 (length start)) start))) + +(defun egrep-grep-list (func list) + (let (r) + (while list + (if (funcall func (car list)) + (setq r (cons (car list) r))) + (setq list (cdr list))) + (nreverse r))) + +(defun egrep-read-font-lock-face (prompt &optional predicate initial hist default inherit-input-method) + (intern (completing-read prompt + (egrep-grep-list (lambda (font) (egrep-string-startswith font "font-lock-")) + (mapcar (lambda (face) (symbol-name face)) (face-list))) + predicate t (or initial "font-lock-") hist default inherit-input-method))) + +(defun egrep-regexps-with-face-dired-marked-files (regexp face-symbol) + (interactive (list (read-string "Regexp: ") + (egrep-read-font-lock-face "Font symbol: "))) + (egrep-regexps-dired-marked-files regexp + (lambda () + (font-lock-fontify-buffer) + (htmlize-ensure-fontified)) + 'egrep-font-face-predicate + face-symbol))