From 9216930082ecf0592ccb99cf27d146eeea0768f2 Mon Sep 17 00:00:00 2001 From: larstvei Date: Fri, 14 Nov 2014 00:21:29 +0100 Subject: [PATCH] Working prototype. --- try.el | 39 ++++++++++++++------------------------- 1 file changed, 14 insertions(+), 25 deletions(-) diff --git a/try.el b/try.el index 642ab87..5a80b8d 100644 --- a/try.el +++ b/try.el @@ -4,18 +4,8 @@ (defvar try-tmp-dir "/tmp/") -(defun try-newest-package-installed-p (package) - "Return true if the newest available PACKAGE is installed." - (when (package-installed-p package) - (let* ((local-pkg-desc (or (assq package package-alist) - (assq package package--builtins))) - (newest-pkg-desc (assq package package-archive-contents))) - (and local-pkg-desc newest-pkg-desc - (version-list-= (package-desc-vers (cdr local-pkg-desc)) - (package-desc-vers (cdr newest-pkg-desc))))))) - - (defun try-raw-link-p (url) + "Returns non-nil if this looks like an URL to a .el file." (string-match-p "[^:]*://\\([^?\r\n]+\\).*\.el?$" url)) (defun try-raw-link (url) @@ -23,32 +13,31 @@ (condition-case nil (progn (eval-region (search-forward-regexp "^$") (point-max)) - (message "Trying!")) + (let ((str (car (last (split-string url "/"))))) + (message "Trying %s!" str))) ((debug error) (message "Could not parse %s" url) nil)))) (defun try-compose (f g) + "Compose two functions." (lambda (&rest x) (funcall f (apply g x)))) -(defun try-package (package) - (unless package--initialized - (package-initialize t)) - (unless package-archive-contents - (package-refresh-contents)) - (if (try-newest-package-installed-p package) - (message "Already installed.") - (message "Trying %s!" (symbol-name package)) - t)) - (defun try () - "Takes an URL to a .el-file, and evaluates it." + "Try out a package from your `package-archives' or pass a URL +to a raw .el file. Packages are stored in `try-tmp-dir' and raw +.el files are not stored at all." (interactive) (let ((url-or-package (completing-read "url or package: " (mapcar (try-compose 'symbol-name 'car) package-archive-contents)))) (cond ((try-raw-link-p url-or-package) (try-raw-link url-or-package)) - ((try-package (intern url-or-package))) - (t (message "Couldn't find a sensible way to try this."))))) + ((assq (intern url-or-package) package-archive-contents) + (let ((package-user-dir try-tmp-dir) + (package-alist nil)) + (package-install (intern url-or-package)) + (message "Trying %s!" url-or-package))) + (t (message (concat "Couldn't find a sensible way to try this. " + "Try running `package-refresh-contents'!")))))) (provide 'try)