Skip to content

Instantly share code, notes, and snippets.

@makerneo-com
Created November 30, 2013 10:49
Show Gist options
  • Select an option

  • Save makerneo-com/7717611 to your computer and use it in GitHub Desktop.

Select an option

Save makerneo-com/7717611 to your computer and use it in GitHub Desktop.
; ido
(require 'ido)
(ido-mode t)
(require 'project-root)
(setq project-roots
'(
("Git Project"
:root-contains-files (".git/index")
:on-hit (lambda (p)
(setq project-root-find-files-command "git ls-files")
))
("Mercurial Project"
:root-contains-files (".hg/store")
:on-hit (lambda (p)
(setq project-root-find-files-command "hg locate")
))
)
)
(defun ido-project-root-find-file ()
"Use ido to select a file from the project."
(interactive)
(with-project-root
(let (my-project-root project-files tbl)
(unless project-details (project-root-fetch))
(setq my-project-root (cdr project-details))
(let ((command (concat "cd "
my-project-root
" && "
project-root-find-files-command)))
;;(message command)
(setq project-files
(split-string
(shell-command-to-string command) "\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))
;; Remove path
(let ((split-key (split-string key "/")))
(if (> (length split-key) 1)
(setq key (concat (car (nthcdr (- (length split-key) 2) split-key))
"/"
(car (nthcdr (- (length split-key) 1) split-key))
))
)
)
(puthash key path tbl)
(push key ido-list)
)
project-files
)
(find-file (gethash (ido-completing-read "project-files: " ido-list) tbl))))))
(define-key evil-normal-state-map (kbd ",f") 'ido-project-root-find-file)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment