summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Shepherd <davidshepherd7@gmail.com>2017-03-16 11:22:27 +0000
committerDavid Shepherd <davidshepherd7@gmail.com>2017-03-16 11:22:27 +0000
commit58d9f38bc10cac5787125a247f3115f6b5222bb7 (patch)
treea1c533cd40191731a40baf7655b9ab742acb1d6a
parentAdd build system (diff)
downloademacs-terminal-here-58d9f38bc10cac5787125a247f3115f6b5222bb7.tar.gz
Initial probably-working version
-rw-r--r--Cask9
-rw-r--r--Makefile3
-rw-r--r--terminal-here.el57
-rw-r--r--test/terminal-here-test.el40
-rw-r--r--test/test-helper.el13
5 files changed, 107 insertions, 15 deletions
diff --git a/Cask b/Cask
index 09fc48c..bd5331e 100644
--- a/Cask
+++ b/Cask
@@ -6,4 +6,11 @@
;; single file package).
(package-file "terminal-here.el")
-(source melpa-stable)
+(source gnu)
+
+(development
+ (depends-on "ert-runner")
+ (depends-on "el-mock")
+
+ ;; For testing defcustom types
+ (depends-on "validate"))
diff --git a/Makefile b/Makefile
index db738f9..9eada4c 100644
--- a/Makefile
+++ b/Makefile
@@ -10,6 +10,9 @@ build :
clean :
@rm -f *.elc
+test: build
+ ${CASK} exec ert-runner
+
install:
${CASK} install
diff --git a/terminal-here.el b/terminal-here.el
index ae4d1d1..707ebb5 100644
--- a/terminal-here.el
+++ b/terminal-here.el
@@ -4,37 +4,66 @@
;; Author: David Shepherd <davidshepherd7@gmail.com>
;; Version: 0.1
-;; Package-Requires: (TODO)
-;; Keywords: TODO
+;; Package-Requires: ((emacs "24"))
+;; Keywords: tools, frames
;; URL: https://github.com/davidshepherd7/terminal-here
;;; Commentary:
-;; TODO
+;; An Emacs package to help open external terminal emulators in the directory of
+;; the current buffer. See the readme.md file for more details.
;;; Code:
+
+
+(defun terminal-here--default-terminal-command ()
+ "Pick a good default command to use."
+ (cond
+ ((eq system-type 'darwin)
+ (lambda (dir) (list "open" "-a" "Terminal.app" dir)))
+
+ ((or (eq system-type 'windows-nt) (eq system-type 'ms-dos) (eq system-type 'cygwin))
+ (lambda (dir) (list "start" "/D" dir "cmd")))
+
+ ;; Probably X11!
+ (t '("x-terminal-emulator"))))
+
(defcustom terminal-here-terminal-command
- "x-terminal-emulator"
- "Terminal binary, or list of (binary arg1 arg2 ...)"
+ (terminal-here--default-terminal-command)
+ "The command used to start a terminal.
+
+Either a list of strings: (terminal-binary arg1 arg2 ...); or a
+function taking a directory and returning such a list."
:group 'terminal-here
- :type '(list string))
+ :type '(choice (repeat string)
+ (function)))
-(defun terminal-here-cwd ()
- "Launch a terminal in the working directory of the current buffer"
+
+
+(defun terminal-here-launch-in-directory (dir)
+ "Launch a terminal in directory DIR."
+ (let* ((term-command (if (functionp terminal-here-terminal-command)
+ (funcall terminal-here-terminal-command dir)
+ terminal-here-terminal-command))
+ (process-name (car term-command))
+ (default-directory dir))
+ (apply #'start-process process-name nil term-command)))
+
+(defun terminal-here-launch ()
+ "Launch a terminal in the current working directory.
+
+This is the directory of the current buffer unless you have
+changed it by running `cd'."
(interactive)
- (let ((terminal-command (if (listp terminal-here-terminal-command) (car terminal-here-terminal-command) terminal-here-terminal-command))
- (terminal-arguments (if (listp terminal-here-terminal-command) (cdr terminal-here-terminal-command) '())))
- (apply #'start-process
- terminal-command nil terminal-command
- terminal-arguments)))
+ (terminal-here-launch-in-directory default-directory))
+
(provide 'terminal-here)
-
;;; terminal-here.el ends here
diff --git a/test/terminal-here-test.el b/test/terminal-here-test.el
new file mode 100644
index 0000000..2eb9471
--- /dev/null
+++ b/test/terminal-here-test.el
@@ -0,0 +1,40 @@
+;;; -*- lexical-binding: t; -*-
+
+(ert-deftest linux-default-command ()
+ (with-mock
+ (mock (start-process "x-terminal-emulator" * "x-terminal-emulator"))
+ (let ((system-type 'gnu/linux))
+ (custom-reevaluate-setting 'terminal-here-terminal-command)
+ (terminal-here-launch-in-directory "adir"))))
+
+(ert-deftest osx-default-command ()
+ (with-mock
+ (mock (start-process "open" * "open" "-a" "Terminal.app" "adir"))
+ (let ((system-type 'darwin))
+ (custom-reevaluate-setting 'terminal-here-terminal-command)
+ (terminal-here-launch-in-directory "adir"))))
+
+(ert-deftest windows-default-command ()
+ (with-mock
+ (mock (start-process "start" * "start" "/D" "adir" "cmd"))
+ (let ((system-type 'windows-nt))
+ (custom-reevaluate-setting 'terminal-here-terminal-command)
+ (terminal-here-launch-in-directory "adir"))))
+
+(ert-deftest custom-terminal-command-as-list ()
+ (with-mock
+ (mock (start-process "1" * "1" "2" "3"))
+ (validate-setq terminal-here-terminal-command '("1" "2" "3"))
+ (terminal-here-launch-in-directory "adir")))
+
+(ert-deftest custom-terminal-command-as-function ()
+ (with-mock
+ (mock (start-process "1" * "1" "2" "3" "adir"))
+ (validate-setq terminal-here-terminal-command (lambda (dir) (list "1" "2" "3" dir)))
+ (terminal-here-launch-in-directory "adir")))
+
+(ert-deftest custom-terminal-command-as-junk-rejected ()
+ (with-mock
+ (should-error
+ (validate-setq terminal-here-terminal-command "astring")
+ :type 'user-error)))
diff --git a/test/test-helper.el b/test/test-helper.el
new file mode 100644
index 0000000..2ecee3b
--- /dev/null
+++ b/test/test-helper.el
@@ -0,0 +1,13 @@
+;; Don't load old byte-compiled versions!
+(setq load-prefer-newer t)
+
+;; Load terminal-here
+(require 'f)
+(add-to-list 'load-path (f-parent (f-dirname load-file-name)))
+(require 'terminal-here)
+
+;; Load test helpers
+(require 'el-mock)
+(eval-when-compile
+ (require 'cl)) ;; for el-mock
+(require 'validate)