diff options
| author | David Shepherd <davidshepherd7@gmail.com> | 2017-10-22 13:52:34 +0100 |
|---|---|---|
| committer | David Shepherd <davidshepherd7@gmail.com> | 2017-10-22 13:52:34 +0100 |
| commit | b3659e13d3d41503b4fc59dd2c7ea622631fc3ec (patch) | |
| tree | d9a09fba071e56c6161a324e904b9a9a3dd0fc9f | |
| parent | Update .travis.yml (diff) | |
| download | emacs-terminal-here-b3659e13d3d41503b4fc59dd2c7ea622631fc3ec.tar.gz | |
Add support for file paths opened using tramp and sudo
Fixes #7.
| -rw-r--r-- | terminal-here.el | 32 | ||||
| -rw-r--r-- | test/terminal-here-test.el | 22 |
2 files changed, 52 insertions, 2 deletions
diff --git a/terminal-here.el b/terminal-here.el index 1af5d37..4dc8cef 100644 --- a/terminal-here.el +++ b/terminal-here.el @@ -18,6 +18,11 @@ ;;; Code: (require 'cl-lib) + +;; TODO: it would be nice not to need to load all of tramp just for the file +;; name parsing. I'm not sure if that's possible though. +(require 'tramp) + (defgroup terminal-here nil @@ -65,7 +70,31 @@ buffer is not in a project." (defun terminal-here-launch-in-directory (dir) - "Launch a terminal in directory DIR." + "Launch a terminal in directory DIR. + +Handles tramp paths sensibly." + (terminal-here--do-launch (or (terminal-here-maybe-tramp-path-to-directory dir) + dir))) + + +(defun terminal-here-maybe-tramp-path-to-directory (dir) + "Extract the local part of a local tramp path. + +Given a tramp path returns the local part, otherwise returns nil." + (when (tramp-tramp-file-p dir) + (let ((file-name-struct (tramp-dissect-file-name dir))) + (cond + ((equal (tramp-file-name-method file-name-struct) "sudo") + (tramp-file-name-localname file-name-struct)) + (t (user-error "Terminal here cannot currently handle tramp files other than sudo")))))) + + +(defun terminal-here--do-launch (dir) + "Internal function to launch the terminal in directory DIR. + +For launching a terminal from emacs lisp you almost almost +certainly want to call `terminal-here-launch-in-directory' which +also handles tramp mappings." (let* ((term-command (if (functionp terminal-here-terminal-command) (funcall terminal-here-terminal-command dir) terminal-here-terminal-command)) @@ -82,6 +111,7 @@ buffer is not in a project." ;; Don't close when emacs closes, seems to only be necessary on Windows. (set-process-query-on-exit-flag proc nil))) + ;;;###autoload (defun terminal-here-launch () "Launch a terminal in the current working directory. diff --git a/test/terminal-here-test.el b/test/terminal-here-test.el index c53c01d..e92f92d 100644 --- a/test/terminal-here-test.el +++ b/test/terminal-here-test.el @@ -67,9 +67,29 @@ (let ((project-root-finder (lambda () "" "vc-root"))) (validate-setq terminal-here-project-root-function project-root-finder) (with-terminal-here-mocks - (mock (terminal-here-launch-in-directory "vc-root")) + (mock (terminal-here--do-launch "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))) + + + +(ert-deftest sudo-tramp () + (with-terminal-here-mocks + (mock (terminal-here--do-launch "/etc/emacs/")) + (terminal-here-launch-in-directory "/sudo:root@localhost:/etc/emacs/")) + + (with-terminal-here-mocks + (mock (terminal-here--do-launch "/etc/emacs/")) + (terminal-here-launch-in-directory "/sudo:postgres@localhost:/etc/emacs/")) + + (with-terminal-here-mocks + (mock (terminal-here--do-launch "/etc/emacs/")) + (terminal-here-launch-in-directory "/sudo:root@127.0.0.1:/etc/emacs/"))) + +(ert-deftest other-tramp-paths-not-handled () + (should-error + (terminal-here-launch-in-directory "/ssh:foo@bar.com:/home/foo/my-file") + :type 'user-error)) |
