Create a gist now

Instantly share code, notes, and snippets.

(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+\\'"
(setq str (replace-match "" t t 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 ()
(chomp (shell-command-to-string "git rev-parse --show-cdup"))))
(defun git-grep ()
(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"
(let* ((my-project-root (git-wc-root))
(git-dir (chomp (shell-command-to-string "git rev-parse --git-dir")))
(concat "git --git-dir "
(shell-quote-argument git-dir)
" ls-files -c -o"))
;; 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))
(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))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment