(defvar *files* nil)
(defconst fs-file-search-buffer-name "*file-search*")
(defvar fs-file-list-length 0)
(defvar fs-file-search-str "")
(defvar fs-match-regexp "")
(defvar fs-file-search-keymap (make-sparse-keymap))
(define-key fs-file-search-keymap (kbd "RET") 'fs-result-choose)
(defface fs-file-name-face '((t (:foreground "red" :bold t :underline t)))
"Face used to highlight the file name")
(defface fs-line-face '((t (:foreground "green" :bold nil :underline nil)))
"Face used to highlight the line number")
(defun fs-get-search-result-buffer()
(get-buffer-create fs-file-search-buffer-name))
(defun fs-insert-result (line file text)
(with-current-buffer (fs-get-search-result-buffer)
(setq buffer-read-only nil)
(goto-char (point-max))
(let ((bpt (point)))
(insert (file-name-nondirectory file))
(set-text-properties bpt (point-max) '(face fs-file-name-face))
(put-text-property bpt (point-max) 'keymap fs-file-search-keymap)
(put-text-property bpt (point-max) 'filename file)
(put-text-property bpt (point-max) 'nline line))
(goto-char (point-max))
(let ((bpt (point)))
(insert "(" (number-to-string line) "): ")
(set-text-properties bpt (point-max) '(face fs-line-face))
(insert text "\n"))
(insert text "\n")
(setq buffer-read-only t)))
(defun fs-search-file (file-name point)
(let ((buf (get-buffer (file-name-nondirectory file-name)))
(bpt 0))
(with-current-buffer (find-file-noselect file-name)
(if buf
(setq bpt (point)))
(while point
(goto-char point)
(setq point (re-search-forward fs-file-search-str nil '(nil)))
(if point
(let ((b-pt (line-beginning-position))
(e-pt (line-end-position)))
(fs-insert-result
(count-lines 1 point)
file-name
(buffer-substring b-pt e-pt)))))
(if buf
(goto-char bpt)
(kill-buffer (current-buffer))))))
(defun files-in-directory ( directory search-str file-rexp)
(setq *files* nil)
(setq fs-file-search-str search-str)
(let ((list (directory-files directory t)))
(while list
(if (file-directory-p (car list))
(progn
(if (or (string= (file-name-nondirectory (car list)) ".")
(string= (file-name-nondirectory (car list)) ".."))
(setq list (cdr list))
(progn
(setq list (append (cdr list)
(directory-files (car list) t)))
)))
(progn
;; (setq *files* (cons (car list) *files*))
(if (string-match file-rexp (file-name-nondirectory (car list)))
(fs-search-file (car list) 1)
)
(setq list (cdr list))))
))
(switch-to-buffer fs-file-search-buffer-name))
(defun fs-result-choose()
(interactive)
(let ((bpt (line-beginning-position)))
(let ((line (get-text-property bpt 'nline))
(file (get-text-property bpt 'filename)))
(select-window (next-window))
(find-file file)
(goto-line line))))
(defun list-count (list)
(let ((count 0))
(while list
(setq list (cdr list))
(setq count (+ count 1)))
count))
阅读(1551) | 评论(0) | 转发(0) |