public
Created

  • Download Gist
nopaste.txt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
(require 'ido)
(setq ido-max-directory-size (* 256 1024)
ido-enable-flex-matching t)
 
(defun chomp (str)
"Chomp leading and tailing whitespace from STR."
(while (string-match "\\`\n+\\|^\\s-+\\|\\s-+$\\|\n+\\'"
str)
(setq str (replace-match "" t t str)))
str)
 
(defun my-find-file (arg)
(interactive "P")
(call-interactively (if arg 'git-find-file 'ido-find-file)))
 
(global-set-key (kbd "C-x C-f") 'my-find-file)
 
(defun git-wc-root ()
(expand-file-name
(chomp (shell-command-to-string "git rev-parse --show-cdup"))))
 
(defun git-grep ()
(interactive)
(let ((grep-command "git grep -nH -e"))
(cd (git-wc-root))
(call-interactively 'grep)))
 
(defun git-find-file ()
"Use ido to select a file from the git repo"
(interactive)
(let* ((my-project-root (git-wc-root))
(git-dir (chomp (shell-command-to-string "git rev-parse --git-dir")))
(project-files
(split-string
(shell-command-to-string
(concat "git --git-dir "
(shell-quote-argument git-dir)
" ls-files -c -o"))
"\n")))
;; populate hash table (display repr => path)
(setq tbl (make-hash-table :test 'equal))
(let (ido-list)
(mapc (lambda (path)
;; format path for display in ido list
(setq key path)
;; strip project root
(setq key (replace-regexp-in-string my-project-root "" key))
;; remove trailing | or /
(setq key (replace-regexp-in-string "\\(|\\|/\\)$" "" key))
(puthash key (expand-file-name (concat my-project-root "/" path)) tbl)
(push key ido-list))
project-files)
(let ((ido-decorations (quote ("\n-> " "" "\n " "\n ..." "[" "]" " [No match]" " [Matched]" " [Not readable]" " [Too big]" " [Confirm]")))
(truncate-lines nil))
(find-file (gethash (ido-completing-read "files: " ido-list) tbl))))))

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.