Last active
October 27, 2018 01:30
-
-
Save s-fubuki/8223e5f2a4c9c344755731f54501854d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;;; bi-grep.el --- elisp grep -*- lexical-binding: t; -*- | |
;; Copyright (C) 2017, 2018 fubuki | |
;; Author: fubuki@*****.org | |
;; Keywords: tools | |
;; This program is free software: you can redistribute it and/or modify | |
;; it under the terms of the GNU General Public License as published by | |
;; the Free Software Foundation, either version 3 of the License, or | |
;; (at your option) any later version. | |
;; This program is distributed in the hope that it will be useful, | |
;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
;; GNU General Public License for more details. | |
;; You should have received a copy of the GNU General Public License | |
;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. | |
;;; Commentary: | |
;; It is GREP program by elisp. | |
;; | |
;; M-x bi-grep | |
;; | |
;; Since are using eshell's grab library, | |
;; can use pattern matching that can be used with eshell in file specification | |
;; and search down to the directory. | |
;;; Installation: | |
;; (require 'bi-grep) | |
;;; Code: | |
(require 'simple) | |
(require 'grep) | |
(require 'em-glob) | |
(defgroup bi-grep nil | |
"Builtin Elisp GREP Program." | |
:group 'tools | |
:version "25.3") | |
(defcustom big-case-fold-search t | |
"bi-grep default case mode. | |
NON-NIL is case-insensitive. | |
‘default-case-fold-search’ is obsolete since 23.2;" | |
:type 'boolean | |
:group 'bi-grep) | |
(defcustom big-print-coding-system nil | |
"bi-grep print coding-system." | |
:type 'boolean | |
:group 'bi-grep) | |
(defun builtin-grep (regexp file &optional case) | |
"Return a list of lines matching file REGEXP from FILE." | |
(let (result) | |
(with-temp-buffer | |
(let ((case-fold-search case)) | |
(insert-file-contents file) | |
(goto-char (point-min)) | |
(while (re-search-forward regexp nil t) | |
(save-excursion | |
(setq result | |
(cons (list | |
(line-number-at-pos) ; simple.el | |
buffer-file-coding-system | |
(buffer-substring | |
(progn (beginning-of-line) (point)) | |
(progn (end-of-line) (point)))) | |
result)))))) | |
(reverse result))) | |
(defvar big-regexp nil "Work value.") | |
(defvar big-file-match nil "Work value.") | |
(defun bi-grep (regexp file &optional case) | |
"Print lines matching REGEXP from the FILE." | |
(interactive | |
(let* ((regexp big-regexp) | |
(file big-file-match) | |
(case (if current-prefix-arg (not big-case-fold-search) big-case-fold-search)) | |
(prompt (if case "REGEXP" "Regexp"))) | |
(list (read-regexp | |
(format "%s%s: " prompt (if regexp (format " (default %s)" regexp) "")) | |
regexp 'regexp-history-last) | |
(read-file-name | |
(format "File%s: "(if file (format " (default %s)" file) "")) | |
nil file) | |
case))) | |
(let ((eshell-glob-show-progress 'show) | |
(coding-system big-print-coding-system) | |
(glob (progn | |
(message "Files glob: %s..." file) | |
(eshell-extended-glob file)))) | |
;; Save previous value. | |
(setq big-regexp regexp | |
big-file-match file) | |
(unless (consp glob) | |
(error "File no match.")) | |
(pop-to-buffer (generate-new-buffer "*bi-grep*") 'split) | |
(let ((case-fold-search case)) | |
(message "bi-grep \"%s\" %s..." regexp file) | |
(insert (format "-*- mode: grep; default-directory: \"%s\" -*-\n" default-directory) | |
"Grep started at " (current-time-string) | |
(format " Case %s\n\n" (if case-fold-search "insensitive" "sensitive")) | |
(format "builtin-grep \"%s\" %s\n" regexp file)) | |
(dolist (file glob) | |
(dolist (list (builtin-grep regexp file case-fold-search)) | |
(if (not coding-system) | |
(insert (format "%s:%d:%s\n" file (car list) (caddr list))) | |
(insert (format "%s:%d:%s:%s\n" | |
file | |
(car list) | |
(symbol-name (cadr list)) | |
(caddr list)))))) | |
(set-buffer-modified-p nil) | |
(grep-mode) | |
;; Coloring matched parts. | |
(goto-char (point-min)) | |
(while (re-search-forward regexp nil t) | |
(overlay-put | |
(make-overlay (match-beginning 0) (match-end 0)) 'face 'match)) | |
(goto-char (point-min)) | |
(message "bi-grep \"%s\" %s...done." regexp file)))) | |
(provide 'bi-grep) | |
;; Fin. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment