Created
October 28, 2014 06:48
-
-
Save anonymous/e0d440b0be2ab4106390 to your computer and use it in GitHub Desktop.
This file contains 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
(defun efind (dir pattern) | |
"find by elisp" | |
(interactive "DDirectory: \nspattern: ") | |
;; define variable | |
(let ((efind-temp (efind-store-files dir pattern)) | |
(efind-ffile-map (make-sparse-keymap))) | |
;; key map | |
(define-key efind-ffile-map [return] | |
(lambda () | |
(interactive) | |
(find-file (buffer-substring | |
(line-beginning-position) | |
(line-end-position))))) | |
(define-key efind-ffile-map "\C-m" | |
(lambda () | |
(interactive) | |
(find-file (buffer-substring | |
(line-beginning-position) | |
(line-end-position))))) | |
(define-key efind-ffile-map "f" | |
(lambda () | |
(interactive) | |
(find-file (buffer-substring | |
(line-beginning-position) | |
(line-end-position))))) | |
(define-key efind-ffile-map "v" | |
(lambda () | |
(interactive) | |
(view-file (buffer-substring | |
(line-beginning-position) | |
(line-end-position))))) | |
;; make buffer | |
(let ((temp-buffer-show-function 'switch-to-buffer)) | |
(with-output-to-temp-buffer "*Efind*" | |
(princ (format "%d matches for \"%s\" in dir: %s\n" | |
(length efind-temp) pattern dir)) | |
(princ (concat "\n" (mapconcat 'identity efind-temp "\n"))) | |
;; set face | |
(set-buffer "*Efind*") | |
(font-lock-mode 0) | |
(setq buffer-read-only nil) | |
(goto-char (point-min)) | |
(forward-line) | |
(dolist (x efind-temp) | |
(forward-line) | |
(if (file-directory-p x) | |
(add-text-properties | |
(line-beginning-position) | |
(line-end-position) | |
(list 'keymap efind-ffile-map | |
'face 'link)) | |
(add-text-properties | |
(line-beginning-position) | |
(line-end-position) | |
(list 'keymap efind-ffile-map | |
'face 'underline)))) | |
(view-mode t))))) | |
(defun efind-store-files (dir pattern) | |
(let ((case-fold-search t) | |
(tmp-files (directory-files dir t)) | |
(dir-box) (store-box) (dir-temp-box)) | |
;; initial set | |
(dolist (x tmp-files) | |
;; set store-box | |
(unless (equal "." (substring x -1)) | |
(when (string-match pattern (file-name-nondirectory x)) | |
(setq store-box (cons x store-box)))) | |
;; set dir-box | |
(when (file-directory-p x) | |
(unless (equal "." (substring x -1)) | |
(setq dir-box (cons x dir-box))))) | |
;; loop | |
(while (> (length dir-box) 0) | |
(dolist (y dir-box) | |
(setq tmp-files (directory-files y t)) | |
(dolist (x tmp-files) | |
;; set store-box | |
(unless (equal "." (substring x -1)) | |
(when (string-match pattern (file-name-nondirectory x)) | |
(setq store-box (cons x store-box)))) | |
;; set dir-temp-box | |
(when (file-directory-p x) | |
(unless (equal "." (substring x -1)) | |
(setq dir-temp-box (cons x dir-temp-box)))))) | |
(setq dir-box dir-temp-box) | |
(setq dir-temp-box ())) | |
(sort store-box 'string<))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment