2014-11-14 19:49:01 +00:00
|
|
|
;;; try.el --- Try out Emacs packages. -*- lexical-binding: t -*-
|
2014-11-14 12:21:18 +00:00
|
|
|
|
|
|
|
;; Copyright (C) 2014 Lars Tveito.
|
|
|
|
|
|
|
|
;; Author: Lars Tveito <larstvei@ifi.uio.no>
|
2014-11-14 22:27:23 +00:00
|
|
|
;; URL: http://github.com/larstvei/try
|
2014-11-14 22:36:02 +00:00
|
|
|
;; Created: 13th November 2014
|
|
|
|
;; Keywords: packages
|
|
|
|
;; Version: 0.0.1
|
2014-11-15 12:06:09 +00:00
|
|
|
;; Package-Requires: ((emacs "24"))
|
2014-11-14 12:21:18 +00:00
|
|
|
|
|
|
|
;; Contains code from GNU Emacs <https://www.gnu.org/software/emacs/>,
|
|
|
|
;; released under the GNU General Public License version 3 or later.
|
|
|
|
|
|
|
|
;; Try is free software; you can redistribute it and/or modify it under the
|
|
|
|
;; terms of the GNU General Public License as published by the Free Software
|
|
|
|
;; Foundation; either version 3, or (at your option) any later version.
|
|
|
|
;;
|
|
|
|
;; Try is distributed in the hope that it will be useful, but WITHOUT ANY
|
|
|
|
;; WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
|
|
|
;; FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
|
|
|
;; details.
|
|
|
|
;;
|
|
|
|
;; You should have received a copy of the GNU General Public License along
|
2014-11-14 19:49:01 +00:00
|
|
|
;; with Try. If not, see <http://www.gnu.org/licenses/>.
|
2014-11-14 12:21:18 +00:00
|
|
|
|
2014-11-14 22:36:02 +00:00
|
|
|
;;; Commentary:
|
|
|
|
|
|
|
|
;; # Try
|
|
|
|
|
|
|
|
;; Try is a packages that allow you to try out Emacs packages without
|
|
|
|
;; installing them. If you pass a URL to a plain-text `.el`-file it evaluates
|
|
|
|
;; the content, without storing the file.
|
|
|
|
|
|
|
|
;; Packages from ELPA will temporarily be stored in `/tmp/` by default.
|
|
|
|
|
2014-11-15 22:47:45 +00:00
|
|
|
;; ## Installation
|
|
|
|
|
|
|
|
;; You can install Try using elpa.
|
|
|
|
|
|
|
|
;; It's available on [marmalade](http://marmalade-repo.org/) and
|
|
|
|
;; [melpa](http://melpa.milkbox.net/):
|
|
|
|
|
|
|
|
;; M-x package-install try
|
|
|
|
|
2014-11-14 22:36:02 +00:00
|
|
|
;; ## Usage
|
|
|
|
|
2014-11-17 00:31:38 +00:00
|
|
|
;; To try out a package you can run
|
2014-11-14 22:36:02 +00:00
|
|
|
|
2014-11-15 22:47:45 +00:00
|
|
|
;; M-x try RET some-package
|
|
|
|
|
|
|
|
;; Or if you want to try out some package from the web, just paste in the URL
|
|
|
|
|
|
|
|
;; M-x try RET https://url.com/to/some/file.el
|
|
|
|
|
2014-11-14 22:36:02 +00:00
|
|
|
;; ### Example
|
|
|
|
|
|
|
|
;; If you for instance have [melpa](http://melpa.org/) in your
|
|
|
|
;; `package-archives` you can try
|
|
|
|
;; [multiple cursors](https://github.com/magnars/multiple-cursors.el) by
|
|
|
|
;; running:
|
|
|
|
|
|
|
|
;; M-x try RET multiple-cursors RET
|
|
|
|
|
|
|
|
;; If you on the other hand want to test out a single `.el`-file from somewhere
|
|
|
|
;; you can simply insert an URL. Trying out
|
|
|
|
;; [Leuven-theme](https://github.com/fniessen/emacs-leuven-theme) can be done
|
|
|
|
;; by running:
|
|
|
|
|
|
|
|
;; M-x try RET https://raw.githubusercontent.com/fniessen/emacs-leuven-theme/master/leuven-theme.el RET
|
|
|
|
|
|
|
|
;; Unfortunately, you won't be able to try Try with `M-x try RET try`.
|
|
|
|
|
2014-11-14 22:27:23 +00:00
|
|
|
;;; Code:
|
2014-11-13 22:20:08 +00:00
|
|
|
(require 'package)
|
|
|
|
(require 'url)
|
|
|
|
|
2014-11-17 00:31:38 +00:00
|
|
|
(defvar try-archive-contents-initial-value package-archive-contents
|
|
|
|
"Remember the initial value of `package-archive-contents' to be
|
|
|
|
able to detect the need to call `package-refresh-contents'.")
|
|
|
|
|
|
|
|
(defvar try-tmp-dir "/tmp/"
|
|
|
|
"The default place packages to try out is /tmp/.")
|
2014-11-13 22:20:08 +00:00
|
|
|
|
|
|
|
(defun try-raw-link-p (url)
|
2014-11-13 23:21:29 +00:00
|
|
|
"Returns non-nil if this looks like an URL to a .el file."
|
2014-11-13 22:20:08 +00:00
|
|
|
(string-match-p "[^:]*://\\([^?\r\n]+\\).*\.el?$" url))
|
|
|
|
|
|
|
|
(defun try-raw-link (url)
|
|
|
|
(with-current-buffer (url-retrieve-synchronously url)
|
|
|
|
(condition-case nil
|
|
|
|
(progn
|
|
|
|
(eval-region (search-forward-regexp "^$") (point-max))
|
2014-11-13 23:21:29 +00:00
|
|
|
(let ((str (car (last (split-string url "/")))))
|
|
|
|
(message "Trying %s!" str)))
|
2014-11-13 22:20:08 +00:00
|
|
|
((debug error)
|
|
|
|
(message "Could not parse %s" url) nil))))
|
|
|
|
|
2014-11-17 00:31:38 +00:00
|
|
|
(defun try-package-exists-p (package-name)
|
|
|
|
"Returns non-nil if the package is available for download."
|
|
|
|
(assq package-name package-archive-contents))
|
|
|
|
|
2014-11-13 22:20:08 +00:00
|
|
|
(defun try-compose (f g)
|
2014-11-13 23:21:29 +00:00
|
|
|
"Compose two functions."
|
2014-11-13 22:20:08 +00:00
|
|
|
(lambda (&rest x) (funcall f (apply g x))))
|
|
|
|
|
2014-11-14 22:27:23 +00:00
|
|
|
;;;###autoload
|
2014-11-13 22:20:08 +00:00
|
|
|
(defun try ()
|
2014-11-13 23:21:29 +00:00
|
|
|
"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."
|
2014-11-13 22:20:08 +00:00
|
|
|
(interactive)
|
2014-11-17 00:31:38 +00:00
|
|
|
;; Refresh package contents if needed.
|
|
|
|
(and (eq try-archive-contents-initial-value
|
|
|
|
package-archive-contents)
|
|
|
|
(< 1 (length package-archives))
|
|
|
|
(package-refresh-contents))
|
|
|
|
;; Completions for packages.
|
|
|
|
(let* ((url-or-package (completing-read
|
|
|
|
"url or package: "
|
|
|
|
(mapcar (try-compose 'symbol-name 'car)
|
|
|
|
package-archive-contents)))
|
|
|
|
(package-symbol (intern url-or-package)))
|
2014-11-13 22:20:08 +00:00
|
|
|
(cond ((try-raw-link-p url-or-package) (try-raw-link url-or-package))
|
2014-11-17 00:31:38 +00:00
|
|
|
((try-package-exists-p package-symbol)
|
2014-11-13 23:21:29 +00:00
|
|
|
(let ((package-user-dir try-tmp-dir)
|
|
|
|
(package-alist nil))
|
2014-11-17 00:31:38 +00:00
|
|
|
(package-install package-symbol)
|
2014-11-13 23:21:29 +00:00
|
|
|
(message "Trying %s!" url-or-package)))
|
|
|
|
(t (message (concat "Couldn't find a sensible way to try this. "
|
|
|
|
"Try running `package-refresh-contents'!"))))))
|
2014-11-13 22:20:08 +00:00
|
|
|
|
|
|
|
(provide 'try)
|
2014-11-14 22:27:23 +00:00
|
|
|
|
|
|
|
;;; try.el ends here.
|