From 5d759db6713420ed4ab5c5cbef891871399ad25d Mon Sep 17 00:00:00 2001 From: Steve Purcell Date: Sat, 18 Mar 2017 15:24:49 +1300 Subject: Use a defcustom to control the project root function --- terminal-here.el | 25 ++++++++++++++++++++----- test/terminal-here-test.el | 36 ++++++++++++++++-------------------- 2 files changed, 36 insertions(+), 25 deletions(-) diff --git a/terminal-here.el b/terminal-here.el index ee23e92..26dabaf 100644 --- a/terminal-here.el +++ b/terminal-here.el @@ -4,7 +4,7 @@ ;; Author: David Shepherd ;; Version: 0.1 -;; Package-Requires: ((emacs "24")) +;; Package-Requires: ((emacs "24") (cl-lib "0.5")) ;; Keywords: tools, frames ;; URL: https://github.com/davidshepherd7/terminal-here @@ -17,6 +17,7 @@ ;;; Code: +(require 'cl-lib) (defgroup terminal-here nil @@ -47,6 +48,18 @@ function taking a directory and returning such a list." :type '(choice (repeat string) (function))) +(defcustom terminal-here-project-root-function + (cl-find-if 'fboundp '(projectile-project-root vc-root-dir)) + "Function called to find the current project root directory. + +Good options include `projectile-project-root', which requires +you install the `projectile' package, or `vc-root-dir' which is +available in Emacs >= 25.1. + +The function should return nil or signal an error if the current +buffer is not in a project." + :group 'terminal-here + :type 'function) @@ -75,10 +88,12 @@ changed it by running `cd'." If projectile is installed the projectile root will be used, Otherwise `vc-root-dir' will be used." (interactive) - (terminal-here-launch-in-directory (cond - ((and (functionp 'projectile-project-root) (projectile-project-root))) - ((and (functionp 'vc-root-dir) (vc-root-dir))) - (t (signal 'user-error "Failed to detect project root, if you are in a version-controlled project try installing projectile or upgrading to emacs 25"))))) + (when (not terminal-here-project-root-function) + (signal 'user-error "No `terminal-here-project-root-function' is set.")) + (let ((root (funcall terminal-here-project-root-function))) + (when (not root) + (signal 'user-error "Not in any project according to `terminal-here-project-root-function'")) + (terminal-here-launch-in-directory root))) diff --git a/test/terminal-here-test.el b/test/terminal-here-test.el index 24f5ca4..58adbef 100644 --- a/test/terminal-here-test.el +++ b/test/terminal-here-test.el @@ -43,23 +43,19 @@ -(ert-deftest projectile-root () - (with-mock - (mock (projectile-project-root) => "project-root") - (mock (terminal-here-launch-in-directory "project-root")) - (terminal-here-project-launch))) - -(ert-deftest non-projectile-project-root () - (with-mock - (should (not (boundp 'projectile-project-root))) - (mock (vc-root-dir) => "vc-root") - (mock (terminal-here-launch-in-directory "vc-root")) - (terminal-here-project-launch))) - -(ert-deftest no-root-found () - (with-mock - (mock (projectile-project-root) => nil) - (mock (vc-root-dir) => nil) - (should-error - (terminal-here-project-launch) - :type 'user-error))) +(ert-deftest no-project-root-function () + ;; Can't use validate setq here because you aren't allowed to set this to nil + ;; by hand. + (setq terminal-here-project-root-function nil) + (should-error (terminal-here-project-launch) :type 'user-error)) + +(ert-deftest with-project-root-function () + (let ((project-root-finder (lambda () "" "vc-root"))) + (validate-setq terminal-here-project-root-function project-root-finder) + (with-mock + (mock (terminal-here-launch-in-directory "vc-root")) + (terminal-here-project-launch)))) + +(ert-deftest project-root-finds-nothing () + (validate-setq terminal-here-project-root-function (lambda () nil)) + (should-error (terminal-here-project-launch :type 'user-error))) -- cgit v1.3