Skip to content

Instantly share code, notes, and snippets.

@klang
Created April 7, 2013 13:50
Show Gist options
  • Save klang/5330573 to your computer and use it in GitHub Desktop.
Save klang/5330573 to your computer and use it in GitHub Desktop.
patch for nrepl.el version 1.8 that will fix nrepl-jump (M-.) functionality when emacs and clojure are running on different host types (unix, mac, windows)
(defcustom nrepl-use-local-resources t
"use local resources under HOME if possible."
:type 'string
:group 'nrepl)
(defun nrepl-tramp-prefix ()
"use buffer-file-name on jump-origin to determine if the buffer is opened on a remote server.
M-. will have put the jump-origin on the tag-marker-ring, so we get the info there."
(save-match-data
(let ((jump-origin (buffer-file-name (marker-buffer (ring-ref find-tag-marker-ring 0)))))
(when (string-match "^\\(.*:\\)" jump-origin)
(match-string 1 jump-origin)))))
(defun nrepl-home-prefix-adjustment (resource)
"Difference in HOME location on different operating systems will be adjusted."
(save-match-data
(if (string-match "^\\/\\(Users\\|home\\)\\/\\w+\\(\\/.+\\)" resource)
(concat (getenv "HOME") (match-string 2 resource)))))
(defun nrepl-emacs-or-clojure-side-adjustment (resource)
"If 'nrepl-use-local-resources is set, resources on the same host as emacs will be preferred
otherwise, the true tramp-prefixed path to the resource will be used, if the resource exists
if not, the resource is returned to the caller unmodified.
The unmodified path WILL work, if emacs and clojure are running on the same host type."
(let ((clojure-side-res (concat (nrepl-tramp-prefix) resource))
(emacs-side-res (nrepl-home-prefix-adjustment resource)))
(cond ((equal resource "") resource)
((and nrepl-use-local-resources
(file-exists-p emacs-side-res))
emacs-side-res)
((file-exists-p clojure-side-res)
clojure-side-res)
(t
resource))))
(defun nrepl-find-file (filename)
"Switch to a buffer visiting FILENAME.
Removes any leading slash if on Windows. Uses `find-file'."
(let ((fn (if (and (eq system-type 'windows-nt)
(string-match "^/" filename))
(substring filename 1)
filename)))
(find-file (nrepl-emacs-or-clojure-side-adjustment fn))))
(defun nrepl-find-resource (resource)
"Find and display RESOURCE."
(cond ((string-match "^file:\\(.+\\)" resource)
(nrepl-find-file (match-string 1 resource)))
((string-match "^\\(jar\\|zip\\):file:\\(.+\\)!/\\(.+\\)" resource)
(let* ((jar (nrepl-emacs-or-clojure-side-adjustment (match-string 2 resource)))
(path (match-string 3 resource))
(buffer-already-open (get-buffer (file-name-nondirectory jar))))
(nrepl-find-file jar)
(goto-char (point-min))
(search-forward path)
(let ((opened-buffer (current-buffer)))
(archive-extract)
(when (not buffer-already-open)
(kill-buffer opened-buffer)))))
(t (error "Unknown resource path %s" resource))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment