Skip to content

Commit

Permalink
Merge pull request #599 from jeffvalk/master
Browse files Browse the repository at this point in the history
[Fix #552] Load resources from jar/zip files efficiently.
  • Loading branch information
bbatsov committed Jun 1, 2014
2 parents 994fac0 + 71e015a commit 2d93c7d
Showing 1 changed file with 29 additions and 39 deletions.
68 changes: 29 additions & 39 deletions cider-interaction.el
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,11 @@
(defconst cider-doc-buffer "*cider-doc*")
(defconst cider-result-buffer "*cider-result*")

(defcustom cider-use-local-resources t
"Use local resources under HOME if possible."
(define-obsolete-variable-alias 'cider-use-local-resources
'cider-prefer-local-resources "0.7.0")

(defcustom cider-prefer-local-resources nil
"Prefer local resources to remote (tramp) ones when both are available."
:type 'boolean
:group 'cider)

Expand Down Expand Up @@ -491,30 +494,16 @@ otherwise, nil."
localname)
name))

(defun cider-home-prefix-adjustment (resource)
"System-dependent HOME location will be adjusted in RESOURCE.
Removes any leading slash if on Windows."
(save-match-data
(cond ((string-match "^\\/\\(Users\\|home\\)\\/\\w+\\(\\/.+\\)" resource)
(concat (getenv "HOME") (match-string 2 resource)))
((and (eq system-type 'windows-nt)
(string-match "^/" resource)
(not (tramp-tramp-file-p resource)))
(substring resource 1))
(t
resource))))

(defun cider-emacs-or-clojure-side-adjustment (resource)
"Fix the RESOURCE path depending on `cider-use-local-resources`."
(let ((resource (cider-home-prefix-adjustment resource))
(clojure-side-res (concat (cider-tramp-prefix) resource))
(emacs-side-res resource))
(cond ((equal resource "") resource)
((and cider-use-local-resources
(file-exists-p emacs-side-res))
emacs-side-res)
((file-exists-p clojure-side-res)
clojure-side-res)
(defun cider-file-path (resource)
"Return RESOURCE's local or remote path using `cider-prefer-local-resources'."
(let ((local-path resource)
(remote-path (concat (cider-tramp-prefix) resource)))
(cond ((equal resource "") "")
((and cider-prefer-local-resources
(file-exists-p local-path))
local-path)
((file-exists-p remote-path)
remote-path)
(t
resource))))

Expand All @@ -523,7 +512,7 @@ Removes any leading slash if on Windows."
Adjusts for HOME location using `cider-home-prefix-adjustment'.
Uses `find-file'."
(let ((large-file-warning-threshold nil))
(find-file (cider-emacs-or-clojure-side-adjustment filename))))
(find-file (cider-file-path filename))))

(defun cider-find-resource (resource)
"Find and display RESOURCE."
Expand All @@ -532,18 +521,19 @@ Uses `find-file'."
((string-match "^\\(jar\\|zip\\):file:\\(.+\\)!/\\(.+\\)" resource)
(let* ((jar (match-string 2 resource))
(path (match-string 3 resource))
(buffer-already-open (get-buffer (file-name-nondirectory jar))))
(cider-find-file jar)
(goto-char (point-min))
;; Make sure the file path is followed by a newline to
;; prevent eg. clj matching cljs.
(search-forward (concat path "\n"))
;; moves up to matching line
(forward-line -1)
(let ((opened-buffer (current-buffer)))
(archive-extract)
(unless buffer-already-open
(kill-buffer opened-buffer)))))
(file (cider-file-path jar))
(name (format "%s:%s" jar path)))
(switch-to-buffer
(or (get-file-buffer name)
(with-current-buffer (generate-new-buffer
(file-name-nondirectory path))
(archive-zip-extract file path)
(set-visited-file-name name)
(setq-local default-directory (file-name-directory file))
(setq-local buffer-read-only t)
(set-buffer-modified-p nil)
(set-auto-mode)
(current-buffer))))))
(t (error "Unknown resource path %s" resource))))

(defun cider-jump-to-def-for (location)
Expand Down

0 comments on commit 2d93c7d

Please sign in to comment.