Skip to content

Instantly share code, notes, and snippets.

@dhaley
Created May 31, 2013 20:49
Show Gist options
  • Save dhaley/5687871 to your computer and use it in GitHub Desktop.
Save dhaley/5687871 to your computer and use it in GitHub Desktop.
Get drush pcomplete working in eshell
(defun pcmpl-drush-commands ()
"Return the most common drush commands by parsing the drush output."
(with-temp-buffer
(call-process-shell-command "drush" nil (current-buffer) nil "help")
(goto-char 0)
(search-forward "Core drush commands: (core)")
;; (search-forward "available drush commands in")
(let (commands)
(while (re-search-forward
"^[[:blank:]]\\([[:word:]-.]+\\)"
nil t)
(push (match-string 1) commands)
(when (match-string 2)
(push (match-string 2) commands)))
(sort commands #'string<))))
(pcmpl-drush-commands)
(defconst pcmpl-drush-commands (pcmpl-drush-commands)
"List of `drush' commands.")
(defvar pcmpl-drush-ref-list-cmd "drush for-each-ref refs/ --format='%(refname)'"
"The `drush' command to run to get a list of refs.")
(defun pcmpl-drush-get-refs (type)
"Return a list of `drush' refs filtered by TYPE."
(with-temp-buffer
(insert (shell-command-to-string pcmpl-drush-ref-list-cmd))
(goto-char (point-min))
(let (refs)
(while (re-search-forward (concat "^refs/" type "/\\(.+\\)$") nil t)
(push (match-string 1) refs))
(nreverse refs))))
(defun pcmpl-drush-remotes ()
"Return a list of remote repositories."
(split-string (shell-command-to-string "drush remote")))
(defun pcomplete/drush ()
"Completion for `drush'."
;; Completion for the command argument.
(pcomplete-here* pcmpl-drush-commands)
(cond
((pcomplete-match "help" 1)
(pcomplete-here* pcmpl-drush-commands))
((pcomplete-match (regexp-opt '("pull" "push")) 1)
(pcomplete-here (pcmpl-drush-remotes)))
;; provide branch completion for the command `checkout'.
((pcomplete-match "checkout" 1)
(pcomplete-here* (append (pcmpl-drush-get-refs "heads")
(pcmpl-drush-get-refs "tags"))))
(t
(while (pcomplete-here (pcomplete-entries))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment