Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@nelhage
Created September 19, 2012 01:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nelhage/3747165 to your computer and use it in GitHub Desktop.
Save nelhage/3747165 to your computer and use it in GitHub Desktop.
(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))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment