Skip to content

Instantly share code, notes, and snippets.

@Chris2048
Forked from sbenhaim/anything-project-files.el
Created January 17, 2011 00:27
Show Gist options
  • Save Chris2048/782297 to your computer and use it in GitHub Desktop.
Save Chris2048/782297 to your computer and use it in GitHub Desktop.
;; The Common Lisp library for Emacs Lisp gives us keyword arguments for defun*
(require 'cl)
(defvar projects (list) "This keeps track of all available projects.")
(defvar project (list) "And here's our current project.")
(defvar project-index (list) "This will store the project index of files.")
(defvar project-default-file-types '("ASCII.*"))
;; A project will have a name, a list of directories, (recursive and non),
;; and a list of file-types that we want to index.
;; `&key' gives us keyword arguments, you can use a symbol or a list of
;; the form `(keyname default-value)' to designate a keyword.
;; Anything-candidates can be a list of `(DISPLAY . REAL)' pairs, so
;; we throw the name up front to serve as the `DISPLAY' component."
(defun* project-create (name &key (dirs nil) (rdirs nil)
(indexed-files project-default-file-types))
"Add a project to the list or projects."
(add-to-list 'projects
(cons name
`((:name . ,name)
(:dirs . ,dirs)
(:rdirs . ,rdirs)
(:indexed-files . ,indexed-files)))))
;; Elisp is a Lisp-2, so function can have the same name as variables.
(defun project (key)
(cdr (assoc key project)))
(defun project-reindex ()
"Update your projects index of files."
(interactive)
(let* ((rdirs (project :rdirs))
(dirs (project :dirs))
(file-type-string
(mapconcat (lambda (x) (concat " -e '" x "'")) (project :indexed-files) nil)))
(message "indexing...")
(setq project-index
(split-string
(concat
(if dirs
(shell-command-to-string
(concat "find " (mapconcat #'identity dirs " ")
" -maxdepth 1 -type f | file -Nf -| grep " file-type-string
"|cut -d':' -f1")))
(if rdirs
(shell-command-to-string
(concat "find " (mapconcat #'identity rdirs " ")
" -type f | file -Nf -| grep " file-type-string
"|cut -d':' -f1"))))))
(message "indexing done.")))
(defun project-load ()
(interactive)
(setq project
(anything
'((name . "Load Project")
(candidates . projects)
;; `anything' usually wants to do something with our
;; selected candidate, but here I'm just going to return it.
(action . (("Return" . identity))))))
(if (not project-index)
(project-reindex)))
(defun anything-project-files ()
(interactive)
(anything
'((name . "Project Find File")
(candidates . project-index)
(type . file))))
;; example project
(project-create "emacs"
:dirs '("/var/src.git")
:rdirs '("/var/src.git/escripts")
:indexed-files '(".*Lisp.*"))
@Chris2048
Copy link
Author

I was having problems with the Tramp async stuff, so I removed it; but maybe something like it should be put in later.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment